// https://syzkaller.appspot.com/bug?id=3df48ebd71a690590efc7be9f08025dd934b29ad #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #ifndef LOOP_SET_FD #define LOOP_SET_FD 0x4C00 #endif #ifndef LOOP_CLR_FD #define LOOP_CLR_FD 0x4C01 #endif /* Minimal raw ext4 superblock layout */ struct ext4_super_block_raw { uint32_t s_inodes_count; uint32_t s_blocks_count_lo; uint32_t s_r_blocks_count_lo; uint32_t s_free_blocks_count_lo; uint32_t s_free_inodes_count; uint32_t s_first_data_block; uint32_t s_log_block_size; uint32_t s_log_cluster_size; uint32_t s_blocks_per_group; uint32_t s_clusters_per_group; uint32_t s_inodes_per_group; uint32_t s_mtime; uint32_t s_wtime; uint16_t s_mnt_count; uint16_t s_max_mnt_count; uint16_t s_magic; uint16_t s_state; uint16_t s_errors; uint16_t s_minor_rev_level; uint32_t s_lastcheck; uint32_t s_checkinterval; uint32_t s_creator_os; uint32_t s_rev_level; uint16_t s_def_resuid; uint16_t s_def_resgid; uint32_t s_first_ino; uint16_t s_inode_size; uint16_t s_block_group_nr; uint32_t s_feature_compat; uint32_t s_feature_incompat; uint32_t s_feature_ro_compat; uint8_t s_uuid[16]; char s_volume_name[16]; char s_last_mounted[64]; uint32_t s_algorithm_usage_bitmap; } __attribute__((packed)); /* Minimal raw ext4 block group descriptor layout */ struct ext4_group_desc_raw { uint32_t bg_block_bitmap_lo; uint32_t bg_inode_bitmap_lo; uint32_t bg_inode_table_lo; uint16_t bg_free_blocks_count_lo; uint16_t bg_free_inodes_count_lo; uint16_t bg_used_dirs_count_lo; uint16_t bg_flags; uint32_t bg_exclude_bitmap_lo; uint16_t bg_block_bitmap_csum_lo; uint16_t bg_inode_bitmap_csum_lo; uint16_t bg_itable_unused_lo; uint16_t bg_checksum; } __attribute__((packed)); /* Minimal raw 128-byte ext4 inode layout */ struct ext4_inode_raw { uint16_t i_mode; uint16_t i_uid; uint32_t i_size_lo; uint32_t i_atime; uint32_t i_ctime; uint32_t i_mtime; uint32_t i_dtime; uint16_t i_gid; uint16_t i_links_count; uint32_t i_blocks_lo; uint32_t i_flags; uint32_t osd1; uint32_t i_block[15]; uint32_t i_generation; uint32_t i_file_acl_lo; uint32_t i_size_high; uint32_t i_obso_faddr; uint8_t osd2[24]; } __attribute__((packed)); /* Minimal raw ext4 directory entry layout */ struct ext4_dir_entry_raw { uint32_t inode; uint16_t rec_len; uint8_t name_len; uint8_t file_type; char name[]; } __attribute__((packed)); static int read_all(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) return -1; total += n; } return 0; } static int write_all(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) return -1; total += n; } return 0; } static void create_ext4_image(const char *path) { const uint32_t block_size = 4096; const uint32_t total_blocks = 4096; /* 16 MB filesystem */ const uint32_t total_inodes = 4096; const uint32_t inode_size = 128; size_t img_size = (size_t)total_blocks * block_size; uint8_t *img = calloc(1, img_size); if (!img) { printf("[-] Failed to allocate memory for ext4 image: %s\n", strerror(errno)); exit(1); } /* Inode table size: 4096 * 128 = 524288 bytes = 128 blocks (blocks 4..131) */ /* Root dir data block: block 132 */ /* Total metadata blocks used: 133 (blocks 0..132) */ const uint32_t used_blocks = 133; const uint32_t free_blocks = total_blocks - used_blocks; /* 3963 */ const uint32_t free_inodes = total_inodes - 10; /* 4086 */ /* 1. Superblock at offset 1024 */ struct ext4_super_block_raw *sb = (struct ext4_super_block_raw *)(img + 1024); sb->s_inodes_count = total_inodes; sb->s_blocks_count_lo = total_blocks; sb->s_r_blocks_count_lo = 0; sb->s_free_blocks_count_lo = free_blocks; sb->s_free_inodes_count = free_inodes; sb->s_first_data_block = 0; sb->s_log_block_size = 2; /* 1024 << 2 = 4096 */ sb->s_log_cluster_size = 2; sb->s_blocks_per_group = 8192; sb->s_clusters_per_group = 8192; sb->s_inodes_per_group = total_inodes; sb->s_mtime = 1600000000; sb->s_wtime = 1600000000; sb->s_mnt_count = 0; sb->s_max_mnt_count = 0xffff; sb->s_magic = 0xEF53; sb->s_state = 1; /* clean */ sb->s_errors = 1; /* continue */ sb->s_creator_os = 0; /* Linux */ sb->s_rev_level = 1; /* Dynamic */ sb->s_first_ino = 11; sb->s_inode_size = inode_size; sb->s_block_group_nr = 0; sb->s_feature_compat = 0x0008; /* EXT4_FEATURE_COMPAT_EXT_ATTR */ sb->s_feature_incompat = 0x0002; /* EXT2_FEATURE_INCOMPAT_FILETYPE */ sb->s_feature_ro_compat = 0; memset(sb->s_uuid, 0x42, 16); strncpy(sb->s_volume_name, "test_ext4", 16); /* 2. Block Group Descriptor 0 at block 1 (offset 4096) */ struct ext4_group_desc_raw *gd = (struct ext4_group_desc_raw *)(img + block_size); gd->bg_block_bitmap_lo = 2; gd->bg_inode_bitmap_lo = 3; gd->bg_inode_table_lo = 4; gd->bg_free_blocks_count_lo = free_blocks; gd->bg_free_inodes_count_lo = free_inodes; gd->bg_used_dirs_count_lo = 1; /* 3. Block Bitmap at block 2 (offset 8192) */ uint8_t *bbm = img + 2 * block_size; /* Blocks 0..132 used (133 bits): 16 full bytes (128 bits) + 5 bits */ memset(bbm, 0xff, 16); bbm[16] = 0x1f; /* Blocks 133..4095 free (bytes 17..511 already 0) */ /* Mark blocks beyond total_blocks (>= 4096) as used */ memset(bbm + (total_blocks / 8), 0xff, block_size - (total_blocks / 8)); /* 4. Inode Bitmap at block 3 (offset 12288) */ uint8_t *ibm = img + 3 * block_size; /* Inodes 1..10 used (10 bits) */ ibm[0] = 0xff; ibm[1] = 0x03; /* Inodes 11..4096 free (bytes 2..511 already 0) */ /* Mark inodes beyond total_inodes (>= 4096) as used */ memset(ibm + (total_inodes / 8), 0xff, block_size - (total_inodes / 8)); /* 5. Inode Table at blocks 4..131 (offset 16384) */ /* Root inode is inode 2 (index 1) */ struct ext4_inode_raw *root_ino = (struct ext4_inode_raw *)(img + 4 * block_size + 1 * inode_size); root_ino->i_mode = 0040755; /* S_IFDIR | 0755 */ root_ino->i_uid = 0; root_ino->i_size_lo = block_size; root_ino->i_atime = 1600000000; root_ino->i_ctime = 1600000000; root_ino->i_mtime = 1600000000; root_ino->i_gid = 0; root_ino->i_links_count = 2; root_ino->i_blocks_lo = 8; root_ino->i_block[0] = 132; /* Block 132 contains dir entries */ root_ino->i_generation = 1; /* 6. Root Directory Data at block 132 (offset 132 * 4096 = 540672) */ uint8_t *dir_blk = img + 132 * block_size; /* Entry 1: "." */ struct ext4_dir_entry_raw *de1 = (struct ext4_dir_entry_raw *)dir_blk; de1->inode = 2; de1->rec_len = 12; de1->name_len = 1; de1->file_type = 2; /* EXT4_FT_DIR */ memcpy(de1->name, ".\0\0\0", 4); /* Entry 2: ".." */ struct ext4_dir_entry_raw *de2 = (struct ext4_dir_entry_raw *)(dir_blk + 12); de2->inode = 2; de2->rec_len = block_size - 12; de2->name_len = 2; de2->file_type = 2; /* EXT4_FT_DIR */ memcpy(de2->name, "..\0\0", 4); int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644); if (fd < 0) { printf("[-] Failed to open %s for writing: %s\n", path, strerror(errno)); exit(1); } if (write_all(fd, img, img_size) < 0) { printf("[-] Failed to write ext4 image: %s\n", strerror(errno)); exit(1); } close(fd); free(img); printf("[+] Synthesized ext4 image at %s (%zu bytes, 4096 inodes).\n", path, img_size); } /* In-process 9P2000.L server state */ struct fid_state { uint8_t qtype; uint32_t qversion; uint64_t qpath; uint64_t size; int is_dir; }; static struct fid_state fids[65536]; static void *p9_server(void *arg) { int fd = (int)(intptr_t)arg; uint8_t req_buf[65536]; uint8_t resp_buf[65536]; while (1) { uint32_t size = 0; if (read_all(fd, &size, 4) < 0) break; if (size < 7 || size > sizeof(req_buf)) break; memcpy(req_buf, &size, 4); if (read_all(fd, req_buf + 4, size - 4) < 0) break; uint8_t type = req_buf[4]; uint16_t tag = 0; memcpy(&tag, req_buf + 5, 2); uint32_t resp_len = 0; uint8_t resp_type = 0; switch (type) { case 100: { /* P9_TVERSION */ resp_type = 101; /* P9_RVERSION */ uint32_t msize = 8192; uint16_t vlen = 8; const char *ver = "9P2000.L"; memcpy(resp_buf + 7, &msize, 4); memcpy(resp_buf + 11, &vlen, 2); memcpy(resp_buf + 13, ver, 8); resp_len = 7 + 14; break; } case 104: { /* P9_TATTACH */ uint32_t fid = 0; memcpy(&fid, req_buf + 7, 4); if (fid < 65536) { fids[fid].qtype = 0x80; /* P9_QTDIR */ fids[fid].qversion = 0; fids[fid].qpath = 1; fids[fid].size = 4096; fids[fid].is_dir = 1; } resp_type = 105; /* P9_RATTACH */ resp_buf[7] = 0x80; /* P9_QTDIR */ uint32_t qver = 0; uint64_t qpath = 1; memcpy(resp_buf + 8, &qver, 4); memcpy(resp_buf + 12, &qpath, 8); resp_len = 7 + 13; break; } case 8: { /* P9_TSTATFS */ resp_type = 9; /* P9_RSTATFS */ uint32_t st_type = 0x01021997; uint32_t bsize = 4096; uint64_t blocks = 100000; uint64_t bfree = 100000; uint64_t bavail = 100000; uint64_t files = 100000; uint64_t ffree = 100000; uint64_t fsid = 0; uint32_t namelen = 255; memcpy(resp_buf + 7, &st_type, 4); memcpy(resp_buf + 11, &bsize, 4); memcpy(resp_buf + 15, &blocks, 8); memcpy(resp_buf + 23, &bfree, 8); memcpy(resp_buf + 31, &bavail, 8); memcpy(resp_buf + 39, &files, 8); memcpy(resp_buf + 47, &ffree, 8); memcpy(resp_buf + 55, &fsid, 8); memcpy(resp_buf + 63, &namelen, 4); resp_len = 7 + 60; break; } case 110: { /* P9_TWALK */ uint32_t fid = 0, newfid = 0; uint16_t nwname = 0; memcpy(&fid, req_buf + 7, 4); memcpy(&newfid, req_buf + 11, 4); memcpy(&nwname, req_buf + 15, 2); resp_type = 111; /* P9_RWALK */ if (nwname == 0) { if (newfid < 65536 && fid < 65536) fids[newfid] = fids[fid]; uint16_t nwqids = 0; memcpy(resp_buf + 7, &nwqids, 2); resp_len = 7 + 2; } else { uint16_t nwqids = nwname; memcpy(resp_buf + 7, &nwqids, 2); int req_off = 17; int resp_off = 9; uint64_t path = 1000; uint8_t qtype = 0x00; /* Regular file */ uint32_t qversion = 1; for (int i = 0; i < nwname; i++) { uint16_t nlen = 0; memcpy(&nlen, req_buf + req_off, 2); req_off += 2; for (int c = 0; c < nlen; c++) { path = path * 31 + (uint8_t)req_buf[req_off + c]; } req_off += nlen; resp_buf[resp_off++] = qtype; memcpy(resp_buf + resp_off, &qversion, 4); resp_off += 4; memcpy(resp_buf + resp_off, &path, 8); resp_off += 8; } if (newfid < 65536) { fids[newfid].qtype = qtype; fids[newfid].qversion = qversion; fids[newfid].qpath = path; fids[newfid].size = 4096; fids[newfid].is_dir = 0; } resp_len = resp_off; } break; } case 24: { /* P9_TGETATTR */ uint32_t fid = 0; uint64_t mask = 0; memcpy(&fid, req_buf + 7, 4); memcpy(&mask, req_buf + 11, 8); resp_type = 25; /* P9_RGETATTR */ struct fid_state *fs = (fid < 65536 ? &fids[fid] : &fids[0]); uint64_t valid = mask ? mask : 0x00003fffULL; /* P9_STATS_ALL */ uint8_t qtype = fs->qtype; uint32_t qver = fs->qversion; uint64_t qpath = fs->qpath; uint32_t mode = fs->is_dir ? (S_IFDIR | 0755) : (S_IFREG | 0644); uint32_t uid = 0, gid = 0; uint64_t nlink = fs->is_dir ? 2 : 1; uint64_t rdev = 0; uint64_t fsize = fs->size; uint64_t blksize = 4096; uint64_t blocks = (fs->size + 511) / 512; uint64_t zero64 = 0; uint64_t one64 = 1; uint64_t time_val = 1600000000; int off = 7; memcpy(resp_buf + off, &valid, 8); off += 8; resp_buf[off++] = qtype; memcpy(resp_buf + off, &qver, 4); off += 4; memcpy(resp_buf + off, &qpath, 8); off += 8; memcpy(resp_buf + off, &mode, 4); off += 4; memcpy(resp_buf + off, &uid, 4); off += 4; memcpy(resp_buf + off, &gid, 4); off += 4; memcpy(resp_buf + off, &nlink, 8); off += 8; memcpy(resp_buf + off, &rdev, 8); off += 8; memcpy(resp_buf + off, &fsize, 8); off += 8; memcpy(resp_buf + off, &blksize, 8); off += 8; memcpy(resp_buf + off, &blocks, 8); off += 8; memcpy(resp_buf + off, &time_val, 8); off += 8; /* atime sec */ memcpy(resp_buf + off, &zero64, 8); off += 8; /* atime nsec */ memcpy(resp_buf + off, &time_val, 8); off += 8; /* mtime sec */ memcpy(resp_buf + off, &zero64, 8); off += 8; /* mtime nsec */ memcpy(resp_buf + off, &time_val, 8); off += 8; /* ctime sec */ memcpy(resp_buf + off, &zero64, 8); off += 8; /* ctime nsec */ memcpy(resp_buf + off, &time_val, 8); off += 8; /* btime sec */ memcpy(resp_buf + off, &zero64, 8); off += 8; /* btime nsec */ memcpy(resp_buf + off, &zero64, 8); off += 8; /* gen */ memcpy(resp_buf + off, &one64, 8); off += 8; /* data_version */ resp_len = off; break; } case 12: { /* P9_TLOPEN */ uint32_t fid = 0; memcpy(&fid, req_buf + 7, 4); resp_type = 13; /* P9_RLOPEN */ struct fid_state *fs = (fid < 65536 ? &fids[fid] : &fids[0]); resp_buf[7] = fs->qtype; uint32_t qver = fs->qversion; uint64_t qpath = fs->qpath; uint32_t iounit = 4096; memcpy(resp_buf + 8, &qver, 4); memcpy(resp_buf + 12, &qpath, 8); memcpy(resp_buf + 20, &iounit, 4); resp_len = 7 + 17; break; } case 116: { /* P9_TREAD */ uint32_t fid = 0; uint64_t offset = 0; uint32_t count = 0; memcpy(&fid, req_buf + 7, 4); memcpy(&offset, req_buf + 11, 8); memcpy(&count, req_buf + 19, 4); resp_type = 117; /* P9_RREAD */ struct fid_state *fs = (fid < 65536 ? &fids[fid] : &fids[0]); if (offset >= fs->size) { count = 0; } else if (offset + count > fs->size) { count = fs->size - offset; } if (count > 4096) count = 4096; memcpy(resp_buf + 7, &count, 4); memset(resp_buf + 11, 0, count); resp_len = 7 + 4 + count; break; } case 118: { /* P9_TWRITE */ uint32_t fid = 0; uint64_t offset = 0; uint32_t count = 0; memcpy(&fid, req_buf + 7, 4); memcpy(&offset, req_buf + 11, 8); memcpy(&count, req_buf + 19, 4); resp_type = 119; /* P9_RWRITE */ if (fid < 65536) { if (offset + count > fids[fid].size) fids[fid].size = offset + count; } memcpy(resp_buf + 7, &count, 4); resp_len = 7 + 4; break; } case 26: { /* P9_TSETATTR */ uint32_t fid = 0; uint32_t valid = 0; uint64_t size = 0; memcpy(&fid, req_buf + 7, 4); memcpy(&valid, req_buf + 11, 4); memcpy(&size, req_buf + 27, 8); if ((valid & 8) && fid < 65536) { /* ATTR_SIZE = (1 << 3) */ fids[fid].size = size; } resp_type = 27; /* P9_RSETATTR */ resp_len = 7; break; } case 120: { /* P9_TCLUNK */ uint32_t fid = 0; memcpy(&fid, req_buf + 7, 4); if (fid < 65536) fids[fid].is_dir = 0; resp_type = 121; /* P9_RCLUNK */ resp_len = 7; break; } case 50: { /* P9_TFSYNC */ resp_type = 51; /* P9_RFSYNC */ resp_len = 7; break; } case 40: { /* P9_TREADDIR */ resp_type = 41; /* P9_RREADDIR */ uint32_t count = 0; memcpy(resp_buf + 7, &count, 4); resp_len = 7 + 4; break; } case 108: { /* P9_TFLUSH */ resp_type = 109; /* P9_RFLUSH */ resp_len = 7; break; } case 14: { /* P9_TLCREATE */ resp_type = 15; /* P9_RLCREATE */ resp_buf[7] = 0x00; uint32_t qver = 1; uint64_t qpath = 2; uint32_t iounit = 4096; memcpy(resp_buf + 8, &qver, 4); memcpy(resp_buf + 12, &qpath, 8); memcpy(resp_buf + 20, &iounit, 4); resp_len = 7 + 17; break; } default: { resp_type = 7; /* P9_RLERROR */ uint32_t ecode = 61; /* ENODATA */ memcpy(resp_buf + 7, &ecode, 4); resp_len = 7 + 4; break; } } memcpy(resp_buf, &resp_len, 4); resp_buf[4] = resp_type; memcpy(resp_buf + 5, &tag, 2); if (write_all(fd, resp_buf, resp_len) < 0) break; } return NULL; } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[*] Reproducer for deadlock in ima_path_truncate starting...\n"); /* Cleanup any existing mounts */ umount2("/tmp/9p", MNT_DETACH); umount2("/tmp/ovl", MNT_DETACH); umount2("/tmp/upper_fs", MNT_DETACH); /* 1. Mount securityfs and configure IMA policy */ mkdir("/sys/kernel/security", 0755); 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 (or already mounted).\n"); int pfd = open("/sys/kernel/security/ima/policy", O_WRONLY); if (pfd >= 0) { const char *rule = "measure func=FILE_CHECK\n"; ssize_t w = write(pfd, rule, strlen(rule)); if (w < 0) { printf("[*] Note: Writing IMA policy returned: %s\n", strerror(errno)); } else { printf("[+] Loaded IMA policy 'measure func=FILE_CHECK'.\n"); } close(pfd); } else { printf("[*] Note: Open /sys/kernel/security/ima/policy returned: %s\n", strerror(errno)); } /* 2. Create ext4 filesystem image with 4096 inodes and bind to /dev/loop0 */ create_ext4_image("/tmp/ext4.img"); int img_fd = open("/tmp/ext4.img", O_RDWR); if (img_fd < 0) { printf("[-] Failed to open /tmp/ext4.img: %s\n", strerror(errno)); exit(1); } int loop_fd = open("/dev/loop0", O_RDWR); if (loop_fd < 0) { printf("[-] Failed to open /dev/loop0: %s\n", strerror(errno)); exit(1); } ioctl(loop_fd, LOOP_CLR_FD, 0); /* Clear any old binding */ res = ioctl(loop_fd, LOOP_SET_FD, img_fd); if (res < 0) { printf("[-] Failed to bind image to loop0: %s\n", strerror(errno)); exit(1); } printf("[+] Bound /tmp/ext4.img to /dev/loop0.\n"); close(img_fd); close(loop_fd); res = mkdir("/tmp/upper_fs", 0755); if (res < 0 && errno != EEXIST) { printf("[-] Failed to mkdir /tmp/upper_fs: %s\n", strerror(errno)); exit(1); } res = mount("/dev/loop0", "/tmp/upper_fs", "ext4", 0, "user_xattr"); if (res < 0) { printf("[-] Failed to mount ext4 on /tmp/upper_fs: %s\n", strerror(errno)); exit(1); } printf("[+] Mounted ext4 on /tmp/upper_fs.\n"); /* 3. Setup overlayfs */ mkdir("/tmp/upper_fs/upper", 0777); mkdir("/tmp/upper_fs/work", 0777); mkdir("/tmp/lower", 0777); mkdir("/tmp/ovl", 0777); res = mount("overlay", "/tmp/ovl", "overlay", 0, "lowerdir=/tmp/lower,upperdir=/tmp/upper_fs/upper,workdir=/tmp/upper_fs/work"); if (res < 0) { printf("[-] Failed to mount overlay: %s\n", strerror(errno)); exit(1); } printf("[+] Mounted overlay on /tmp/ovl.\n"); /* 4. Setup CacheFiles bound to overlayfs with fstop 0% */ mkdir("/tmp/ovl/cache", 0777); int cfd = open("/dev/cachefiles", O_RDWR); if (cfd < 0) { printf("[-] Failed to open /dev/cachefiles: %s\n", strerror(errno)); exit(1); } printf("[+] Opened /dev/cachefiles.\n"); res = write(cfd, "dir /tmp/ovl/cache\n", strlen("dir /tmp/ovl/cache\n")); if (res < 0) { printf("[-] Failed to set cachefiles dir: %s\n", strerror(errno)); exit(1); } printf("[+] Set cachefiles dir /tmp/ovl/cache.\n"); write(cfd, "fstop 0%\n", strlen("fstop 0%\n")); write(cfd, "fcull 1%\n", strlen("fcull 1%\n")); write(cfd, "frun 2%\n", strlen("frun 2%\n")); write(cfd, "bstop 0%\n", strlen("bstop 0%\n")); write(cfd, "bcull 1%\n", strlen("bcull 1%\n")); write(cfd, "brun 2%\n", strlen("brun 2%\n")); res = write(cfd, "bind\n", strlen("bind\n")); if (res < 0) { printf("[-] Failed to bind cachefiles: %s\n", strerror(errno)); exit(1); } printf("[+] Bound cachefiles.\n"); /* 5. Start in-process 9P server and mount 9P filesystem with fscache */ 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("[+] Created socketpair.\n"); pthread_t th; res = pthread_create(&th, NULL, p9_server, (void *)(intptr_t)sv[1]); if (res != 0) { printf("[-] Failed to start 9p server pthread: %s\n", strerror(res)); exit(1); } printf("[+] Started 9P server thread.\n"); mkdir("/tmp/9p", 0755); char opts[256]; snprintf(opts, sizeof(opts), "trans=fd,rfdno=%d,wfdno=%d,version=9p2000.L,cache=fscache", sv[0], sv[0]); res = mount("9p", "/tmp/9p", "9p", 0, opts); if (res < 0) { printf("[-] Failed to mount 9p: %s\n", strerror(errno)); exit(1); } printf("[+] Mounted 9p with fscache on /tmp/9p.\n"); /* 6. Trigger Circular Locking Dependency: * Link 1: ima_iint_mutex -> 9p i_rwsem (via ima_file_check on 9p open) * Link 3: ovl_i_mutex -> ext4 sb_writers (via cachefiles_preset_object_xattr on cookie lookup) * Link 2: 9p i_rwsem -> ovl_i_mutex (via filemap_fdatawrite in 9p truncate -> cachefiles write) * Link 0: ext4 sb_writers -> ima_iint_mutex (via vfs_truncate -> ima_path_truncate) */ printf("[*] Step 1: Opening 9p file f1 to trigger IMA measurement and fscache cookie lookup...\n"); int fd9p = open("/tmp/9p/f1", O_RDWR); if (fd9p < 0) { printf("[-] Failed to open /tmp/9p/f1: %s\n", strerror(errno)); exit(1); } printf("[+] Opened /tmp/9p/f1.\n"); char rbuf[128]; read(fd9p, rbuf, sizeof(rbuf)); /* Sleep to allow asynchronous fscache_cookie_worker to complete cachefiles_preset_object_xattr */ printf("[*] Waiting 500ms for fscache worker (Link 3: ovl_i_mutex -> sb_writers)...\n"); usleep(500000); /* Step 2: Dirty 9p pagecache and truncate to trigger 9p i_rwsem -> ovl_i_mutex */ printf("[*] Step 2: Dirtying 9p pagecache and truncating to trigger Link 2...\n"); char wbuf[4096]; memset(wbuf, 'X', sizeof(wbuf)); res = pwrite(fd9p, wbuf, sizeof(wbuf), 0); if (res < 0) { printf("[-] Failed to pwrite /tmp/9p/f1: %s\n", strerror(errno)); } else { printf("[+] Wrote 4096 bytes to /tmp/9p/f1.\n"); } int fd_trunc = open("/tmp/9p/f1", O_WRONLY | O_TRUNC); if (fd_trunc >= 0) { printf("[+] Opened /tmp/9p/f1 with O_TRUNC.\n"); close(fd_trunc); } close(fd9p); usleep(200000); /* Step 3: Trigger Deadlock Warning (Link 0: sb_writers#12 -> &ima_iint_mutex_key) */ printf("[*] Step 3: Creating target file on upper_fs and overlayfs...\n"); int ufd = open("/tmp/upper_fs/target", O_CREAT | O_RDWR, 0644); if (ufd >= 0) { write(ufd, "data1234", 8); close(ufd); printf("[+] Created /tmp/upper_fs/target.\n"); } else { printf("[-] Failed to create /tmp/upper_fs/target: %s\n", strerror(errno)); } /* Open read-only to let IMA attach iint and measure */ ufd = open("/tmp/upper_fs/target", O_RDONLY); if (ufd >= 0) { read(ufd, rbuf, sizeof(rbuf)); close(ufd); printf("[+] Opened /tmp/upper_fs/target O_RDONLY to attach IMA iint.\n"); } else { printf("[-] Failed to open /tmp/upper_fs/target O_RDONLY: %s\n", strerror(errno)); } int ofd = open("/tmp/ovl/target", O_CREAT | O_RDWR, 0644); if (ofd >= 0) { write(ofd, "data1234", 8); close(ofd); printf("[+] Created /tmp/ovl/target.\n"); } else { printf("[-] Failed to create /tmp/ovl/target: %s\n", strerror(errno)); } ofd = open("/tmp/ovl/target", O_RDONLY); if (ofd >= 0) { read(ofd, rbuf, sizeof(rbuf)); close(ofd); printf("[+] Opened /tmp/ovl/target O_RDONLY to attach IMA iint.\n"); } else { printf("[-] Failed to open /tmp/ovl/target O_RDONLY: %s\n", strerror(errno)); } printf("[*] Truncating target files to trigger circular locking dependency warning...\n"); truncate("/tmp/upper_fs/target", 0); truncate("/tmp/ovl/target", 0); /* Also run a loop on multiple distinct files to guarantee all lockdep paths are exercised */ for (int i = 0; i < 5; i++) { char name9p[64], nameupper[64], nameovl[64]; snprintf(name9p, sizeof(name9p), "/tmp/9p/f_%d", i); snprintf(nameupper, sizeof(nameupper), "/tmp/upper_fs/target_%d", i); snprintf(nameovl, sizeof(nameovl), "/tmp/ovl/target_%d", i); int f = open(name9p, O_RDWR); if (f >= 0) { read(f, rbuf, sizeof(rbuf)); pwrite(f, wbuf, sizeof(wbuf), 0); usleep(100000); int ft = open(name9p, O_WRONLY | O_TRUNC); if (ft >= 0) close(ft); truncate(name9p, 0); close(f); } int u = open(nameupper, O_CREAT | O_RDWR, 0644); if (u >= 0) { write(u, "test", 4); close(u); } u = open(nameupper, O_RDONLY); if (u >= 0) { read(u, rbuf, sizeof(rbuf)); close(u); } truncate(nameupper, 0); int o = open(nameovl, O_CREAT | O_RDWR, 0644); if (o >= 0) { write(o, "test", 4); close(o); } o = open(nameovl, O_RDONLY); if (o >= 0) { read(o, rbuf, sizeof(rbuf)); close(o); } truncate(nameovl, 0); } printf("[+] Reproducer completed all steps successfully.\n"); sleep(1); return 0; }