// https://syzkaller.appspot.com/bug?id=2472748edcb84f4f0353ee11292fd8a28d9a3a94 #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include struct fid_info { int active; int is_dir; }; static struct fid_info fids[4096]; static uint64_t global_file_size = 4096; static pthread_mutex_t p9_lock = PTHREAD_MUTEX_INITIALIZER; static ssize_t read_exact(int fd, void *buf, size_t count) { size_t total = 0; while (total < count) { ssize_t n = read(fd, (char *)buf + total, count - total); if (n <= 0) { if (n < 0 && (errno == EINTR || errno == EAGAIN)) continue; return total > 0 ? (ssize_t)total : n; } total += n; } return total; } static ssize_t write_exact(int fd, const void *buf, size_t count) { size_t total = 0; while (total < count) { ssize_t n = write(fd, (const char *)buf + total, count - total); if (n <= 0) { if (n < 0 && (errno == EINTR || errno == EAGAIN)) continue; return total > 0 ? (ssize_t)total : n; } total += n; } return total; } static void *p9_server_thread(void *arg) { int fd = (int)(intptr_t)arg; uint8_t req[65536]; uint8_t resp[65536]; while (1) { uint32_t size = 0; ssize_t n = read_exact(fd, &size, 4); if (n != 4) break; if (size < 7 || size > sizeof(req)) break; memcpy(req, &size, 4); n = read_exact(fd, req + 4, size - 4); if (n != (ssize_t)(size - 4)) break; uint8_t id = req[4]; uint16_t tag = *(uint16_t *)(req + 5); uint32_t resp_len = 0; pthread_mutex_lock(&p9_lock); switch (id) { case 100: { /* P9_TVERSION */ uint32_t msize = *(uint32_t *)(req + 7); if (msize > 8192) msize = 8192; resp[4] = 101; /* P9_RVERSION */ *(uint16_t *)(resp + 5) = tag; *(uint32_t *)(resp + 7) = msize; *(uint16_t *)(resp + 11) = 8; memcpy(resp + 13, "9P2000.L", 8); resp_len = 21; *(uint32_t *)resp = resp_len; break; } case 104: { /* P9_TATTACH */ uint32_t fid = *(uint32_t *)(req + 7); if (fid < 4096) { fids[fid].active = 1; fids[fid].is_dir = 1; } resp[4] = 105; /* P9_RATTACH */ *(uint16_t *)(resp + 5) = tag; resp[7] = 0x80; /* P9_QTDIR */ *(uint32_t *)(resp + 8) = 1; /* version = 1 (CRITICAL: non-zero) */ *(uint64_t *)(resp + 12) = 1; /* path = 1 */ resp_len = 20; *(uint32_t *)resp = resp_len; break; } case 8: { /* P9_TSTATFS */ resp[4] = 9; /* P9_RSTATFS */ *(uint16_t *)(resp + 5) = tag; *(uint32_t *)(resp + 7) = 0x01021997; *(uint32_t *)(resp + 11) = 4096; *(uint64_t *)(resp + 15) = 1048576; *(uint64_t *)(resp + 23) = 1000000; *(uint64_t *)(resp + 31) = 1000000; *(uint64_t *)(resp + 39) = 1000000; *(uint64_t *)(resp + 47) = 1000000; *(uint64_t *)(resp + 55) = 0; *(uint32_t *)(resp + 63) = 255; resp_len = 67; *(uint32_t *)resp = resp_len; break; } case 110: { /* P9_TWALK */ uint32_t fid = *(uint32_t *)(req + 7); uint32_t newfid = *(uint32_t *)(req + 11); uint16_t nwname = *(uint16_t *)(req + 15); resp[4] = 111; /* P9_RWALK */ *(uint16_t *)(resp + 5) = tag; *(uint16_t *)(resp + 7) = nwname; if (nwname == 0) { if (newfid < 4096 && fid < 4096) fids[newfid] = fids[fid]; resp_len = 9; } else { for (int i = 0; i < nwname; i++) { uint8_t *p = resp + 9 + i * 13; p[0] = 0x00; /* P9_QTFILE */ *(uint32_t *)(p + 1) = 1; /* version = 1 */ *(uint64_t *)(p + 5) = 2; /* path = 2 */ } if (newfid < 4096) { fids[newfid].active = 1; fids[newfid].is_dir = 0; } resp_len = 9 + nwname * 13; } *(uint32_t *)resp = resp_len; break; } case 12: { /* P9_TLOPEN */ uint32_t fid = *(uint32_t *)(req + 7); int is_dir = (fid < 4096 ? fids[fid].is_dir : 0); resp[4] = 13; /* P9_RLOPEN */ *(uint16_t *)(resp + 5) = tag; resp[7] = is_dir ? 0x80 : 0x00; *(uint32_t *)(resp + 8) = 1; /* version = 1 */ *(uint64_t *)(resp + 12) = is_dir ? 1 : 2; *(uint32_t *)(resp + 20) = 8192; resp_len = 24; *(uint32_t *)resp = resp_len; break; } case 14: { /* P9_TLCREATE */ uint32_t fid = *(uint32_t *)(req + 7); if (fid < 4096) { fids[fid].active = 1; fids[fid].is_dir = 0; } resp[4] = 15; /* P9_RLCREATE */ *(uint16_t *)(resp + 5) = tag; resp[7] = 0x00; *(uint32_t *)(resp + 8) = 1; /* version = 1 */ *(uint64_t *)(resp + 12) = 2; *(uint32_t *)(resp + 20) = 8192; resp_len = 24; *(uint32_t *)resp = resp_len; break; } case 24: { /* P9_TGETATTR */ uint32_t fid = *(uint32_t *)(req + 7); int is_dir = (fid < 4096 ? fids[fid].is_dir : 0); resp[4] = 25; /* P9_RGETATTR */ *(uint16_t *)(resp + 5) = tag; uint8_t *p = resp + 7; *(uint64_t *)p = 0x00003fffULL; p += 8; /* valid mask */ *p = is_dir ? 0x80 : 0x00; p += 1; /* qid.type */ *(uint32_t *)p = 1; p += 4; /* qid.version = 1 */ *(uint64_t *)p = is_dir ? 1 : 2; p += 8;/* qid.path */ *(uint32_t *)p = is_dir ? (S_IFDIR | 0777) : (S_IFREG | 0666); p += 4; *(uint32_t *)p = 0; p += 4; /* uid */ *(uint32_t *)p = 0; p += 4; /* gid */ *(uint64_t *)p = is_dir ? 2 : 1; p += 8;/* nlink */ *(uint64_t *)p = 0; p += 8; /* rdev */ uint64_t cur_sz = is_dir ? 4096 : global_file_size; *(uint64_t *)p = cur_sz; p += 8; /* size */ *(uint64_t *)p = 4096; p += 8; /* blksize */ *(uint64_t *)p = (cur_sz + 511) / 512; p += 8; /* blocks */ *(uint64_t *)p = 1700000000; p += 8; /* atime_sec */ *(uint64_t *)p = 0; p += 8; /* atime_nsec */ *(uint64_t *)p = 1700000000; p += 8; /* mtime_sec */ *(uint64_t *)p = 0; p += 8; /* mtime_nsec */ *(uint64_t *)p = 1700000000; p += 8; /* ctime_sec */ *(uint64_t *)p = 0; p += 8; /* ctime_nsec */ *(uint64_t *)p = 1700000000; p += 8; /* btime_sec */ *(uint64_t *)p = 0; p += 8; /* btime_nsec */ *(uint64_t *)p = 0; p += 8; /* gen */ *(uint64_t *)p = 1; p += 8; /* data_version */ resp_len = (uint32_t)(p - resp); *(uint32_t *)resp = resp_len; break; } case 26: { /* P9_TSETATTR */ uint32_t valid = *(uint32_t *)(req + 11); if (valid & (1 << 3)) { /* P9_ATTR_SIZE */ global_file_size = *(uint64_t *)(req + 27); } resp[4] = 27; /* P9_RSETATTR */ *(uint16_t *)(resp + 5) = tag; resp_len = 7; *(uint32_t *)resp = resp_len; break; } case 116: { /* P9_TREAD */ uint64_t offset = *(uint64_t *)(req + 11); uint32_t count = *(uint32_t *)(req + 19); uint32_t ret_count = 0; if (offset < global_file_size) { if (offset + count > global_file_size) ret_count = (uint32_t)(global_file_size - offset); else ret_count = count; } if (ret_count > 8192) ret_count = 8192; resp[4] = 117; /* P9_RREAD */ *(uint16_t *)(resp + 5) = tag; *(uint32_t *)(resp + 7) = ret_count; memset(resp + 11, 'A', ret_count); resp_len = 11 + ret_count; *(uint32_t *)resp = resp_len; break; } case 118: { /* P9_TWRITE */ uint64_t offset = *(uint64_t *)(req + 11); uint32_t count = *(uint32_t *)(req + 19); if (offset + count > global_file_size) global_file_size = offset + count; resp[4] = 119; /* P9_RWRITE */ *(uint16_t *)(resp + 5) = tag; *(uint32_t *)(resp + 7) = count; resp_len = 11; *(uint32_t *)resp = resp_len; break; } case 120: { /* P9_TCLUNK */ uint32_t fid = *(uint32_t *)(req + 7); if (fid < 4096) fids[fid].active = 0; resp[4] = 121; /* P9_RCLUNK */ *(uint16_t *)(resp + 5) = tag; resp_len = 7; *(uint32_t *)resp = resp_len; break; } case 50: { /* P9_TFSYNC */ resp[4] = 51; /* P9_RFSYNC */ *(uint16_t *)(resp + 5) = tag; resp_len = 7; *(uint32_t *)resp = resp_len; break; } case 108: { /* P9_TFLUSH */ resp[4] = 109; /* P9_RFLUSH */ *(uint16_t *)(resp + 5) = tag; resp_len = 7; *(uint32_t *)resp = resp_len; break; } case 40: { /* P9_TREADDIR */ resp[4] = 41; /* P9_RREADDIR */ *(uint16_t *)(resp + 5) = tag; *(uint32_t *)(resp + 7) = 0; resp_len = 11; *(uint32_t *)resp = resp_len; break; } default: { /* P9_RLERROR */ resp[4] = 7; /* P9_RLERROR */ *(uint16_t *)(resp + 5) = tag; *(uint32_t *)(resp + 7) = 95; /* EOPNOTSUPP */ resp_len = 11; *(uint32_t *)resp = resp_len; break; } } pthread_mutex_unlock(&p9_lock); n = write_exact(fd, resp, resp_len); if (n != (ssize_t)resp_len) break; } close(fd); return NULL; } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[*] Starting reproducer for circular locking dependency in __cachefiles_write...\n"); /* Step 1: Mount securityfs and configure IMA measurement policy */ printf("[*] Step 1: Setting up IMA measurement policy...\n"); struct stat st; if (stat("/sys/kernel/security/ima", &st) < 0) { int res = mount("securityfs", "/sys/kernel/security", "securityfs", 0, NULL); if (res < 0 && errno != EBUSY) { printf("[-] Failed to mount securityfs: %s\n", strerror(errno)); exit(1); } printf("[+] Mounted securityfs at /sys/kernel/security.\n"); } else { printf("[+] securityfs already mounted.\n"); } int pfd = open("/sys/kernel/security/ima/policy", O_WRONLY); if (pfd >= 0) { const char *policy_rule = "measure func=FILE_CHECK\n"; ssize_t wn = write(pfd, policy_rule, strlen(policy_rule)); if (wn < 0) { printf("[*] Notice: IMA policy write returned: %s (continuing with default policy)\n", strerror(errno)); } else { printf("[+] Wrote 'measure func=FILE_CHECK' into IMA policy.\n"); } close(pfd); } else { printf("[*] Notice: Could not open IMA policy file: %s (continuing with default policy)\n", strerror(errno)); } /* Step 2: Initialize and bind CacheFiles */ printf("[*] Step 2: Setting up CacheFiles backing cache...\n"); int res = mkdir("./cachefiles_dir", 0777); if (res < 0 && errno != EEXIST) { printf("[-] Failed to mkdir ./cachefiles_dir: %s\n", strerror(errno)); exit(1); } printf("[+] Backing cache directory ./cachefiles_dir ready.\n"); char cwd[1024]; if (!getcwd(cwd, sizeof(cwd))) { printf("[-] Failed to getcwd: %s\n", strerror(errno)); exit(1); } char cache_dir[1200]; snprintf(cache_dir, sizeof(cache_dir), "%s/cachefiles_dir", cwd); int cfd = open("/dev/cachefiles", O_RDWR); if (cfd < 0) { printf("[-] Failed to open /dev/cachefiles: %s\n", strerror(errno)); exit(1); } printf("[+] /dev/cachefiles opened successfully.\n"); char cmd[1280]; snprintf(cmd, sizeof(cmd), "dir %s\n", cache_dir); res = write(cfd, cmd, strlen(cmd)); if (res < 0) { printf("[-] Failed to write 'dir' command to /dev/cachefiles: %s\n", strerror(errno)); exit(1); } printf("[+] 'dir' command sent to /dev/cachefiles.\n"); res = write(cfd, "bind\n", 5); if (res < 0) { printf("[-] Failed to write 'bind' command to /dev/cachefiles: %s\n", strerror(errno)); exit(1); } printf("[+] CacheFiles cache bound successfully.\n"); /* Step 3: Start 9P server over socketpair */ printf("[*] Step 3: Starting 9P protocol server over socketpair...\n"); int sv[2]; res = socketpair(AF_UNIX, SOCK_STREAM, 0, sv); if (res < 0) { printf("[-] Failed to create socketpair: %s\n", strerror(errno)); exit(1); } printf("[+] socketpair created successfully.\n"); pthread_t th; res = pthread_create(&th, NULL, p9_server_thread, (void *)(intptr_t)sv[1]); if (res != 0) { printf("[-] Failed to create 9P server thread: %s\n", strerror(res)); exit(1); } printf("[+] 9P server thread spawned successfully.\n"); /* Step 4: Mount 9P filesystem with fscache and ignoreqv */ printf("[*] Step 4: Mounting 9P filesystem with cache=fscache,ignoreqv...\n"); res = mkdir("./mnt_9p", 0777); if (res < 0 && errno != EEXIST) { printf("[-] Failed to mkdir ./mnt_9p: %s\n", strerror(errno)); exit(1); } char mnt_opts[256]; snprintf(mnt_opts, sizeof(mnt_opts), "trans=fd,rfdno=%d,wfdno=%d,cache=fscache,cachetag=mycache,ignoreqv,version=9p2000.L", sv[0], sv[0]); res = mount("none", "./mnt_9p", "9p", 0, mnt_opts); if (res < 0) { printf("[-] Failed to mount 9p filesystem: %s\n", strerror(errno)); exit(1); } printf("[+] 9P filesystem mounted at ./mnt_9p.\n"); /* Step 5: Establish Edge 1 (sb_writers -> ima_iint_mutex) */ printf("[*] Step 5: Establishing lockdep Edge 1 (sb_writers -> ima_iint_mutex)...\n"); int lfd = open("./cachefiles_dir/edge1_file", O_CREAT | O_RDWR, 0666); if (lfd < 0) { printf("[-] Failed to create ./cachefiles_dir/edge1_file: %s\n", strerror(errno)); exit(1); } res = write(lfd, "init", 4); if (res < 0) { printf("[-] Failed to write to edge1_file: %s\n", strerror(errno)); exit(1); } close(lfd); /* Opening existing file with O_RDWR | O_TRUNC acquires sb_start_write before ima_file_check */ lfd = open("./cachefiles_dir/edge1_file", O_RDWR | O_TRUNC); if (lfd < 0) { printf("[-] Failed to open edge1_file with O_RDWR | O_TRUNC: %s\n", strerror(errno)); exit(1); } close(lfd); printf("[+] Edge 1 established (sb_writers -> ima_iint_mutex).\n"); /* Step 6: Establish Edge 2 (ima_iint_mutex -> 9p_inode->i_rwsem) and activate cookie */ printf("[*] Step 6: Establishing lockdep Edge 2 (ima_iint_mutex -> 9p_inode->i_rwsem)...\n"); char buf[4096]; memset(buf, 'A', sizeof(buf)); /* Open with O_RDONLY and read: ima_file_check holds iint->mutex and reads file, locking i_rwsem */ int rfd = open("./mnt_9p/testfile", O_RDONLY); if (rfd < 0) { printf("[-] Failed to open ./mnt_9p/testfile with O_RDONLY: %s\n", strerror(errno)); exit(1); } res = read(rfd, buf, sizeof(buf)); if (res < 0) { printf("[-] Failed to read from ./mnt_9p/testfile: %s\n", strerror(errno)); exit(1); } close(rfd); printf("[+] Edge 2 established (ima_iint_mutex -> 9p_inode->i_rwsem).\n"); /* Wait for background fscache lookup to complete and cookie state to become ACTIVE */ usleep(100000); /* Step 7 & 8: Populate dirty folios and trigger Inversion Edge (9p_inode->i_rwsem -> sb_writers) */ printf("[*] Step 7: Triggering circular locking dependency (9p_inode->i_rwsem -> sb_writers)...\n"); for (int iter = 0; iter < 5; iter++) { /* Refresh Edge 1 */ lfd = open("./cachefiles_dir/edge1_file", O_RDWR | O_TRUNC); if (lfd >= 0) close(lfd); /* Refresh Edge 2 */ rfd = open("./mnt_9p/testfile", O_RDONLY); if (rfd >= 0) { read(rfd, buf, sizeof(buf)); close(rfd); } /* Open writeable fd and KEEP IT OPEN so fid is registered on inode for writeback */ int keep_fd = open("./mnt_9p/testfile", O_RDWR); if (keep_fd < 0) { printf("[-] Failed to open ./mnt_9p/testfile for writing: %s\n", strerror(errno)); exit(1); } /* Write 4096 bytes into page cache to dirty folios */ res = write(keep_fd, buf, sizeof(buf)); if (res < 0) { printf("[-] Failed to dirty folios in testfile: %s\n", strerror(errno)); exit(1); } /* Allow cookie state machine to settle */ usleep(50000); /* Truncate via open(O_TRUNC) while dirty folios exist and keep_fd is open */ int tr_fd = open("./mnt_9p/testfile", O_RDWR | O_TRUNC); if (tr_fd >= 0) close(tr_fd); /* Also truncate via ftruncate */ res = ftruncate(keep_fd, 0); if (res < 0) { printf("[*] ftruncate notice: %s\n", strerror(errno)); } close(keep_fd); } printf("[+] Reproduction sequence completed. Waiting for lockdep output...\n"); sleep(2); close(cfd); close(sv[0]); printf("[+] Done.\n"); return 0; }