// https://syzkaller.appspot.com/bug?id=129ec3f99859e81fb0336181af13789fbefdc1b4 #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include /* * Standalone reproducer for: * Circular locking dependency in ocfs2: * &osb->journal_shutdown_sem -> shrinker_mutex -> fs_reclaim -> &osb->journal_shutdown_sem */ #define BLOCK_SIZE 4096 #define NUM_BLOCKS 4096 /* 16 MB filesystem */ #define JOURNAL_BLOCKS 1024 /* 4 MB journal */ #define JOURNAL_START_BLK 128 #define NUM_TEST_FILES 510 #define TEST_FILES_START_BLK 1200 #define OCFS2_VALID_FL 0x00000001 #define OCFS2_SYSTEM_FL 0x00000010 #define OCFS2_SUPER_BLOCK_FL 0x00000020 #define OCFS2_LOCAL_ALLOC_FL 0x00000040 #define OCFS2_BITMAP_FL 0x00000080 #define OCFS2_JOURNAL_FL 0x00000100 #define OCFS2_HEARTBEAT_FL 0x00000200 #define OCFS2_CHAIN_FL 0x00000400 #define OCFS2_DEALLOC_FL 0x00000800 #define OCFS2_FEATURE_INCOMPAT_LOCAL_MOUNT 0x0008 #define JBD2_MAGIC_NUMBER 0xc03b3998U #define JBD2_SUPERBLOCK_V2 4 /* Extent list structures */ struct ocfs2_extent_rec { uint32_t e_cpos; uint16_t e_leaf_clusters; uint8_t e_reserved1; uint8_t e_flags; uint64_t e_blkno; } __attribute__((packed)); struct ocfs2_extent_list { uint16_t l_tree_depth; uint16_t l_count; uint16_t l_next_free_rec; uint16_t l_reserved1; uint64_t l_reserved2; struct ocfs2_extent_rec l_recs[243]; } __attribute__((packed)); /* Chain list structures */ struct ocfs2_chain_rec { uint32_t c_free; uint32_t c_total; uint64_t c_blkno; } __attribute__((packed)); struct ocfs2_chain_list { uint16_t cl_cpg; uint16_t cl_bpc; uint16_t cl_count; uint16_t cl_next_free_rec; uint64_t cl_reserved1; struct ocfs2_chain_rec cl_recs[243]; } __attribute__((packed)); /* Superblock structure matching kernel layout */ struct ocfs2_super_block { uint16_t s_major_rev_level; uint16_t s_minor_rev_level; uint16_t s_mnt_count; uint16_t s_max_mnt_count; uint16_t s_state; uint16_t s_errors; uint32_t s_checkinterval; uint64_t s_lastcheck; uint32_t s_creator_os; uint32_t s_feature_compat; uint32_t s_feature_incompat; uint32_t s_feature_ro_compat; uint64_t s_root_blkno; uint64_t s_system_dir_blkno; uint32_t s_blocksize_bits; uint32_t s_clustersize_bits; uint16_t s_max_slots; uint16_t s_tunefs_flag; uint32_t s_uuid_hash; uint64_t s_first_cluster_group; uint8_t s_label[64]; uint8_t s_uuid[16]; uint8_t s_cluster_info[24]; uint16_t s_xattr_inline_size; uint16_t s_reserved0; uint32_t s_dx_seed[3]; uint64_t s_reserved2[15]; } __attribute__((packed)); /* Local alloc structure */ struct ocfs2_local_alloc { uint32_t la_bm_off; uint16_t la_size; uint16_t la_reserved1; uint64_t la_reserved2; uint8_t la_bitmap[3888]; } __attribute__((packed)); /* Truncate log structure */ struct ocfs2_truncate_rec { uint32_t t_start; uint32_t t_clusters; } __attribute__((packed)); struct ocfs2_truncate_log { uint16_t tl_count; uint16_t tl_used; uint32_t tl_reserved1; struct ocfs2_truncate_rec tl_recs[487]; } __attribute__((packed)); /* Dinode structure matching kernel layout */ struct ocfs2_dinode { uint8_t i_signature[8]; uint32_t i_generation; uint16_t i_suballoc_slot; uint16_t i_suballoc_bit; uint16_t i_links_count_hi; uint16_t i_xattr_inline_size; uint32_t i_clusters; uint32_t i_uid; uint32_t i_gid; uint64_t i_size; uint16_t i_mode; uint16_t i_links_count; uint32_t i_flags; uint64_t i_atime; uint64_t i_ctime; uint64_t i_mtime; uint64_t i_dtime; uint64_t i_blkno; uint64_t i_last_eb_blk; uint32_t i_fs_generation; uint32_t i_atime_nsec; uint32_t i_ctime_nsec; uint32_t i_mtime_nsec; uint32_t i_attr; uint16_t i_orphaned_slot; uint16_t i_dyn_features; uint64_t i_xattr_loc; uint8_t i_check[8]; uint64_t i_dx_root; uint64_t i_refcount_loc; uint64_t i_suballoc_loc; uint16_t i_dio_orphaned_slot; uint16_t i_reserved1[3]; uint64_t i_reserved2[2]; /* id1 at offset 0xB8 (184 bytes) */ union { uint64_t i_pad1; struct { uint32_t i_used; uint32_t i_total; } bitmap1; struct { uint32_t ij_flags; uint32_t ij_recovery_generation; } journal1; struct { uint64_t i_rdev; } dev1; } id1; /* id2 at offset 0xC0 (192 bytes) */ union { struct ocfs2_super_block i_super; struct ocfs2_local_alloc i_lab; struct ocfs2_chain_list i_chain; struct ocfs2_extent_list i_list; struct ocfs2_truncate_log i_dealloc; uint8_t data[BLOCK_SIZE - 192]; } id2; } __attribute__((packed)); /* Group descriptor */ struct ocfs2_group_desc { uint8_t bg_signature[8]; uint16_t bg_size; uint16_t bg_bits; uint16_t bg_free_bits_count; uint16_t bg_chain; uint32_t bg_generation; uint16_t bg_contig_free_bits; uint16_t bg_reserved1; uint64_t bg_next_group; uint64_t bg_parent_dinode; uint64_t bg_blkno; uint8_t bg_check[8]; uint64_t bg_reserved2; uint8_t bg_bitmap[BLOCK_SIZE - 64]; } __attribute__((packed)); /* Directory entry */ struct ocfs2_dir_entry { uint64_t inode; uint16_t rec_len; uint8_t name_len; uint8_t file_type; char name[255]; } __attribute__((packed)); /* JBD2 Superblock */ struct journal_header_s { uint32_t h_magic; uint32_t h_blocktype; uint32_t h_sequence; } __attribute__((packed)); struct journal_superblock_s { struct journal_header_s s_header; uint32_t s_blocksize; uint32_t s_maxlen; uint32_t s_first; uint32_t s_sequence; uint32_t s_start; uint32_t s_errno; uint32_t s_feature_compat; uint32_t s_feature_incompat; uint32_t s_feature_ro_compat; uint8_t s_uuid[16]; uint32_t s_nr_users; uint32_t s_dynsuper; uint32_t s_max_transaction; uint32_t s_max_trans_data; } __attribute__((packed)); static void init_dinode_common(struct ocfs2_dinode *di, uint64_t blkno, uint32_t flags, uint16_t mode) { memcpy(di->i_signature, "INODE01\0", 8); di->i_blkno = htole64(blkno); di->i_flags = htole32(flags); di->i_mode = htole16(mode); di->i_links_count = htole16(S_ISDIR(mode) ? 2 : 1); di->i_fs_generation = htole32(1); if (flags & OCFS2_SYSTEM_FL) { di->i_suballoc_slot = htole16(0xFFFF); di->i_suballoc_bit = htole16(0); } else { di->i_suballoc_slot = htole16(0); di->i_suballoc_bit = htole16(0); } } static size_t add_dirent(char *buf, size_t offset, uint64_t ino, uint8_t ftype, const char *name, uint16_t force_rec_len) { struct ocfs2_dir_entry *de = (struct ocfs2_dir_entry *)(buf + offset); de->inode = htole64(ino); de->file_type = ftype; de->name_len = strlen(name); memcpy(de->name, name, de->name_len); uint16_t rec_len = (12 + de->name_len + 3) & ~3; if (force_rec_len > 0) rec_len = force_rec_len; de->rec_len = htole16(rec_len); return offset + rec_len; } static void create_ocfs2_image(const char *path) { size_t img_size = (size_t)NUM_BLOCKS * BLOCK_SIZE; char *buf = calloc(1, img_size); if (!buf) { printf("[-] Failed to allocate image buffer: %s\n", strerror(errno)); exit(1); } /* 1. Superblock Dinode (Block 2) */ struct ocfs2_dinode *sb_di = (struct ocfs2_dinode *)(buf + 2 * BLOCK_SIZE); memcpy(sb_di->i_signature, "OCFSV2\0", 7); sb_di->i_blkno = htole64(2); sb_di->i_flags = htole32(OCFS2_VALID_FL | OCFS2_SYSTEM_FL | OCFS2_SUPER_BLOCK_FL); sb_di->i_mode = htole16(S_IFREG | 0644); sb_di->i_links_count = htole16(1); sb_di->i_fs_generation = htole32(1); sb_di->i_suballoc_slot = htole16(0xFFFF); sb_di->i_clusters = htole32(NUM_BLOCKS); struct ocfs2_super_block *sb = &sb_di->id2.i_super; sb->s_major_rev_level = htole16(0); sb->s_minor_rev_level = htole16(90); sb->s_blocksize_bits = htole32(12); sb->s_clustersize_bits = htole32(12); sb->s_max_slots = htole16(1); sb->s_feature_incompat = htole32(OCFS2_FEATURE_INCOMPAT_LOCAL_MOUNT); sb->s_root_blkno = htole64(7); sb->s_system_dir_blkno = htole64(5); sb->s_first_cluster_group = htole64(4); sb->s_uuid_hash = htole32(0x12345678); strncpy((char *)sb->s_label, "repro", sizeof(sb->s_label)); memset(sb->s_uuid, 0x42, 16); /* 2. Global Bitmap Dinode (Block 3) & Group Desc (Block 4) */ struct ocfs2_dinode *gb_di = (struct ocfs2_dinode *)(buf + 3 * BLOCK_SIZE); init_dinode_common(gb_di, 3, OCFS2_VALID_FL | OCFS2_SYSTEM_FL | OCFS2_BITMAP_FL | OCFS2_CHAIN_FL, S_IFREG | 0644); gb_di->i_size = htole64((uint64_t)NUM_BLOCKS * BLOCK_SIZE); gb_di->i_clusters = htole32(NUM_BLOCKS); gb_di->id1.bitmap1.i_used = htole32(1800); gb_di->id1.bitmap1.i_total = htole32(NUM_BLOCKS); gb_di->id2.i_chain.cl_cpg = htole16(NUM_BLOCKS); gb_di->id2.i_chain.cl_bpc = htole16(1); gb_di->id2.i_chain.cl_count = htole16(243); gb_di->id2.i_chain.cl_next_free_rec = htole16(1); gb_di->id2.i_chain.cl_recs[0].c_free = htole32(NUM_BLOCKS - 1800); gb_di->id2.i_chain.cl_recs[0].c_total = htole32(NUM_BLOCKS); gb_di->id2.i_chain.cl_recs[0].c_blkno = htole64(4); struct ocfs2_group_desc *gd = (struct ocfs2_group_desc *)(buf + 4 * BLOCK_SIZE); memcpy(gd->bg_signature, "GROUP01", 7); gd->bg_size = htole16(BLOCK_SIZE - 64); gd->bg_bits = htole16(NUM_BLOCKS); gd->bg_free_bits_count = htole16(NUM_BLOCKS - 1800); gd->bg_chain = htole16(0); gd->bg_generation = htole32(1); gd->bg_parent_dinode = htole64(3); gd->bg_blkno = htole64(4); memset(gd->bg_bitmap, 0xFF, 1800 / 8); /* Mark first 1800 blocks as used */ /* 3. System Directory Dinode (Block 5) & Data (Block 6) */ struct ocfs2_dinode *sd_di = (struct ocfs2_dinode *)(buf + 5 * BLOCK_SIZE); init_dinode_common(sd_di, 5, OCFS2_VALID_FL | OCFS2_SYSTEM_FL, S_IFDIR | 0755); sd_di->i_size = htole64(BLOCK_SIZE); sd_di->i_clusters = htole32(1); sd_di->id2.i_list.l_count = htole16(243); sd_di->id2.i_list.l_next_free_rec = htole16(1); sd_di->id2.i_list.l_recs[0].e_leaf_clusters = htole16(1); sd_di->id2.i_list.l_recs[0].e_blkno = htole64(6); char *sd_data = buf + 6 * BLOCK_SIZE; size_t off = 0; off = add_dirent(sd_data, off, 5, 2, ".", 0); off = add_dirent(sd_data, off, 5, 2, "..", 0); off = add_dirent(sd_data, off, 14, 1, "global_inode_alloc", 0); off = add_dirent(sd_data, off, 10, 1, "slot_map", 0); off = add_dirent(sd_data, off, 15, 1, "heartbeat", 0); off = add_dirent(sd_data, off, 3, 1, "global_bitmap", 0); off = add_dirent(sd_data, off, 12, 2, "orphan_dir:0000", 0); off = add_dirent(sd_data, off, 16, 1, "extent_alloc:0000", 0); off = add_dirent(sd_data, off, 17, 1, "inode_alloc:0000", 0); off = add_dirent(sd_data, off, 20, 1, "journal:0000", 0); off = add_dirent(sd_data, off, 18, 1, "local_alloc:0000", 0); off = add_dirent(sd_data, off, 19, 1, "truncate_log:0000", BLOCK_SIZE - off); /* 4. Root Directory Dinode (Block 7) & Data (Blocks 8 and 9) */ struct ocfs2_dinode *rd_di = (struct ocfs2_dinode *)(buf + 7 * BLOCK_SIZE); init_dinode_common(rd_di, 7, OCFS2_VALID_FL | OCFS2_SYSTEM_FL, S_IFDIR | 0755); rd_di->i_size = htole64(2 * BLOCK_SIZE); rd_di->i_clusters = htole32(2); rd_di->id2.i_list.l_count = htole16(243); rd_di->id2.i_list.l_next_free_rec = htole16(1); rd_di->id2.i_list.l_recs[0].e_cpos = 0; rd_di->id2.i_list.l_recs[0].e_leaf_clusters = htole16(2); rd_di->id2.i_list.l_recs[0].e_blkno = htole64(8); /* Block 8: '.' (16 bytes), '..' (16 bytes), and 254 entries f000 .. f253 (16 bytes each) */ char *rd_data0 = buf + 8 * BLOCK_SIZE; off = 0; off = add_dirent(rd_data0, off, 7, 2, ".", 16); off = add_dirent(rd_data0, off, 7, 2, "..", 16); for (int i = 0; i < 254; i++) { char fname[16]; snprintf(fname, sizeof(fname), "f%03d", i); off = add_dirent(rd_data0, off, TEST_FILES_START_BLK + i, 1, fname, 16); } /* Block 9: 256 entries f254 .. f509 (16 bytes each) */ char *rd_data1 = buf + 9 * BLOCK_SIZE; off = 0; for (int i = 254; i < NUM_TEST_FILES; i++) { char fname[16]; snprintf(fname, sizeof(fname), "f%03d", i); off = add_dirent(rd_data1, off, TEST_FILES_START_BLK + i, 1, fname, 16); } /* 5. Slot Map (Block 10 dinode, Block 11 data) */ struct ocfs2_dinode *sm_di = (struct ocfs2_dinode *)(buf + 10 * BLOCK_SIZE); init_dinode_common(sm_di, 10, OCFS2_VALID_FL | OCFS2_SYSTEM_FL, S_IFREG | 0644); sm_di->i_size = htole64(BLOCK_SIZE); sm_di->i_clusters = htole32(1); sm_di->id2.i_list.l_count = htole16(243); sm_di->id2.i_list.l_next_free_rec = htole16(1); sm_di->id2.i_list.l_recs[0].e_leaf_clusters = htole16(1); sm_di->id2.i_list.l_recs[0].e_blkno = htole64(11); memset(buf + 11 * BLOCK_SIZE, 0xFF, BLOCK_SIZE); /* 6. Orphan Dir (Block 12 dinode, Block 13 data) */ struct ocfs2_dinode *od_di = (struct ocfs2_dinode *)(buf + 12 * BLOCK_SIZE); init_dinode_common(od_di, 12, OCFS2_VALID_FL | OCFS2_SYSTEM_FL, S_IFDIR | 0755); od_di->i_size = htole64(BLOCK_SIZE); od_di->i_clusters = htole32(1); od_di->id2.i_list.l_count = htole16(243); od_di->id2.i_list.l_next_free_rec = htole16(1); od_di->id2.i_list.l_recs[0].e_leaf_clusters = htole16(1); od_di->id2.i_list.l_recs[0].e_blkno = htole64(13); char *od_data = buf + 13 * BLOCK_SIZE; off = 0; off = add_dirent(od_data, off, 12, 2, ".", 0); off = add_dirent(od_data, off, 12, 2, "..", BLOCK_SIZE - off); /* 7. global_inode_alloc (Block 14), extent_alloc:0000 (Block 16), inode_alloc:0000 (Block 17) */ uint64_t alloc_blks[] = { 14, 16, 17 }; for (int i = 0; i < 3; i++) { struct ocfs2_dinode *di = (struct ocfs2_dinode *)(buf + alloc_blks[i] * BLOCK_SIZE); init_dinode_common(di, alloc_blks[i], OCFS2_VALID_FL | OCFS2_SYSTEM_FL | OCFS2_BITMAP_FL | OCFS2_CHAIN_FL, S_IFREG | 0644); di->id2.i_chain.cl_cpg = htole16(1024); di->id2.i_chain.cl_bpc = htole16(1); di->id2.i_chain.cl_count = htole16(243); di->id2.i_chain.cl_next_free_rec = htole16(0); } /* 8. Heartbeat (Block 15) */ struct ocfs2_dinode *hb_di = (struct ocfs2_dinode *)(buf + 15 * BLOCK_SIZE); init_dinode_common(hb_di, 15, OCFS2_VALID_FL | OCFS2_SYSTEM_FL | OCFS2_HEARTBEAT_FL, S_IFREG | 0644); hb_di->id2.i_list.l_count = htole16(243); hb_di->id2.i_list.l_next_free_rec = htole16(0); /* 9. local_alloc:0000 (Block 18) */ struct ocfs2_dinode *la_di = (struct ocfs2_dinode *)(buf + 18 * BLOCK_SIZE); init_dinode_common(la_di, 18, OCFS2_VALID_FL | OCFS2_SYSTEM_FL | OCFS2_BITMAP_FL | OCFS2_LOCAL_ALLOC_FL, S_IFREG | 0644); la_di->id2.i_lab.la_size = htole16(3888); /* 10. truncate_log:0000 (Block 19) */ struct ocfs2_dinode *tl_di = (struct ocfs2_dinode *)(buf + 19 * BLOCK_SIZE); init_dinode_common(tl_di, 19, OCFS2_VALID_FL | OCFS2_SYSTEM_FL | OCFS2_DEALLOC_FL, S_IFREG | 0644); tl_di->id2.i_dealloc.tl_count = htole16(487); tl_di->id2.i_dealloc.tl_used = htole16(0); /* 11. journal:0000 (Block 20) */ struct ocfs2_dinode *j_di = (struct ocfs2_dinode *)(buf + 20 * BLOCK_SIZE); init_dinode_common(j_di, 20, OCFS2_VALID_FL | OCFS2_SYSTEM_FL | OCFS2_JOURNAL_FL, S_IFREG | 0644); j_di->i_size = htole64((uint64_t)JOURNAL_BLOCKS * BLOCK_SIZE); j_di->i_clusters = htole32(JOURNAL_BLOCKS); j_di->id1.journal1.ij_flags = 0; j_di->id1.journal1.ij_recovery_generation = htole32(1); j_di->id2.i_list.l_count = htole16(243); j_di->id2.i_list.l_next_free_rec = htole16(1); j_di->id2.i_list.l_recs[0].e_cpos = 0; j_di->id2.i_list.l_recs[0].e_leaf_clusters = htole16(JOURNAL_BLOCKS); j_di->id2.i_list.l_recs[0].e_blkno = htole64(JOURNAL_START_BLK); /* JBD2 Superblock at Block 128 (Big Endian) */ struct journal_superblock_s *jsb = (struct journal_superblock_s *)(buf + JOURNAL_START_BLK * BLOCK_SIZE); jsb->s_header.h_magic = htobe32(JBD2_MAGIC_NUMBER); jsb->s_header.h_blocktype = htobe32(JBD2_SUPERBLOCK_V2); jsb->s_header.h_sequence = 0; jsb->s_blocksize = htobe32(BLOCK_SIZE); jsb->s_maxlen = htobe32(JOURNAL_BLOCKS); jsb->s_first = htobe32(1); jsb->s_sequence = htobe32(1); jsb->s_start = 0; jsb->s_nr_users = htobe32(1); /* 12. Test Files (Blocks 1200..1709, f000..f509) */ for (int i = 0; i < NUM_TEST_FILES; i++) { uint64_t blk = TEST_FILES_START_BLK + i; struct ocfs2_dinode *f_di = (struct ocfs2_dinode *)(buf + blk * BLOCK_SIZE); init_dinode_common(f_di, blk, OCFS2_VALID_FL, S_IFREG | 0644); f_di->i_suballoc_slot = 0; f_di->i_suballoc_bit = htole16(i); f_di->i_size = 0; f_di->i_clusters = 0; f_di->id2.i_list.l_count = htole16(243); f_di->id2.i_list.l_next_free_rec = htole16(0); } int fd = open(path, O_RDWR | O_CREAT | O_TRUNC, 0644); if (fd < 0) { printf("[-] Failed to open image file %s: %s\n", path, strerror(errno)); exit(1); } ssize_t written = write(fd, buf, img_size); if (written != (ssize_t)img_size) { printf("[-] Failed to write complete image: %s\n", strerror(errno)); close(fd); exit(1); } close(fd); free(buf); printf("[+] Successfully constructed 16MB OCFS2 filesystem image with %d files at %s.\n", NUM_TEST_FILES, path); } static char g_cg_path[512] = {0}; static void trigger_edge2_cgroup(void) { printf("[*] Setting up cgroup directory to establish shrinker_mutex -> fs_reclaim...\n"); FILE *fp = fopen("/proc/mounts", "r"); char line[512]; char v1_memcg[256] = {0}; if (fp) { while (fgets(line, sizeof(line), fp)) { char dev[128], mountpoint[128], fstype[64], options[256]; if (sscanf(line, "%127s %127s %63s %255s", dev, mountpoint, fstype, options) == 4) { if (strcmp(fstype, "cgroup") == 0 && strstr(options, "memory")) { printf("[+] Found cgroup v1 memory mount at %s\n", mountpoint); strncpy(v1_memcg, mountpoint, sizeof(v1_memcg) - 1); break; } } } fclose(fp); } if (v1_memcg[0]) { snprintf(g_cg_path, sizeof(g_cg_path), "%s/repro_cg_%d", v1_memcg, getpid()); } else { snprintf(g_cg_path, sizeof(g_cg_path), "/sys/fs/cgroup/memory/repro_cg_%d", getpid()); } /* * Only mkdir() is required to trigger cgroup_mkdir() -> cgroup_apply_control_enable() * -> mem_cgroup_css_online() -> alloc_shrinker_info() (Edge #2). * Do NOT bind the process to tasks so dentries remain on the root cgroup LRU. */ int res = mkdir(g_cg_path, 0755); if (res < 0 && errno != EEXIST) { printf("[-] Failed to mkdir %s: %s\n", g_cg_path, strerror(errno)); } else { printf("[+] Created cgroup directory: %s (shrinker_mutex -> fs_reclaim recorded!)\n", g_cg_path); } } static size_t get_mem_total_kb(void) { FILE *f = fopen("/proc/meminfo", "r"); if (!f) return 4000ULL * 1024; char line[256]; size_t mem_kb = 0; while (fgets(line, sizeof(line), f)) { if (sscanf(line, "MemTotal: %zu kB", &mem_kb) == 1) break; } fclose(f); if (mem_kb > 0) return mem_kb; return 4000ULL * 1024; } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[+] Starting reproducer for OCFS2 circular locking dependency...\n"); /* Step 1: Establish Edge #2: shrinker_mutex -> fs_reclaim via cgroup_mkdir() */ trigger_edge2_cgroup(); /* Step 2: Create raw OCFS2 filesystem image with 510 file dinodes */ const char *img_path = "/tmp/repro_ocfs2.img"; create_ocfs2_image(img_path); int img_fd = open(img_path, O_RDWR); if (img_fd < 0) { printf("[-] Failed to open image %s: %s\n", img_path, strerror(errno)); exit(1); } printf("[+] Opened image file %s\n", img_path); /* Step 3: Attach image to a loop device */ int ctl_fd = open("/dev/loop-control", O_RDWR); if (ctl_fd < 0) { printf("[-] Failed to open /dev/loop-control: %s\n", strerror(errno)); close(img_fd); exit(1); } int dev_nr = ioctl(ctl_fd, LOOP_CTL_GET_FREE); close(ctl_fd); if (dev_nr < 0) { printf("[-] Failed to get free loop dev: %s\n", strerror(errno)); close(img_fd); exit(1); } printf("[+] Allocated loop device number: %d\n", dev_nr); char loop_path[64]; snprintf(loop_path, sizeof(loop_path), "/dev/loop%d", dev_nr); int loop_fd = open(loop_path, O_RDWR); if (loop_fd < 0) { printf("[-] Failed to open %s: %s\n", loop_path, strerror(errno)); close(img_fd); exit(1); } int res = ioctl(loop_fd, LOOP_SET_FD, img_fd); if (res < 0) { printf("[-] Failed to LOOP_SET_FD on %s: %s\n", loop_path, strerror(errno)); close(loop_fd); close(img_fd); exit(1); } printf("[+] Attached %s to %s via LOOP_SET_FD.\n", img_path, loop_path); /* Close loop_fd and img_fd prior to mount to avoid -EBUSY */ close(loop_fd); close(img_fd); printf("[+] Closed loop_fd and img_fd prior to mount.\n"); const char *mnt_point = "/tmp/repro_mnt_ocfs2"; mkdir(mnt_point, 0755); /* * Step 4: Establish Edge #1: &osb->journal_shutdown_sem -> shrinker_mutex. * Mount and unmount ocfs2. * ocfs2_dismount_volume() -> ocfs2_journal_shutdown() holds down_write(&osb->journal_shutdown_sem) * and calls jbd2_journal_destroy() -> shrinker_free() -> mutex_lock(&shrinker_mutex). */ printf("[*] Mounting %s at %s to establish journal_shutdown_sem -> shrinker_mutex...\n", loop_path, mnt_point); res = mount(loop_path, mnt_point, "ocfs2", 0, "heartbeat=none"); if (res < 0) { printf("[-] First mount failed: %s\n", strerror(errno)); goto cleanup_loop; } printf("[+] First mount succeeded. Unmounting now...\n"); res = umount(mnt_point); if (res < 0) { printf("[-] Failed to unmount: %s\n", strerror(errno)); goto cleanup_loop; } printf("[+] Dismount complete (&osb->journal_shutdown_sem -> shrinker_mutex recorded).\n"); /* * Step 5: Mount again and instantiate all 510 dentries EXACTLY ONCE on root LRU. */ printf("[*] Remounting ocfs2 filesystem...\n"); res = mount(loop_path, mnt_point, "ocfs2", 0, "heartbeat=none"); if (res < 0) { printf("[-] Remount failed: %s\n", strerror(errno)); goto cleanup_loop; } printf("[+] Remount succeeded.\n"); printf("[*] Opening and closing all %d files once to populate root dentry LRU...\n", NUM_TEST_FILES); for (int i = 0; i < NUM_TEST_FILES; i++) { char path[128]; snprintf(path, sizeof(path), "%s/f%03d", mnt_point, i); int fd = open(path, O_RDONLY); if (fd >= 0) { close(fd); } else { printf("[-] Failed to open %s: %s\n", path, strerror(errno)); } } printf("[+] All %d files instantiated and cached (unreferenced, count=0, DCACHE_REFERENCED=0).\n", NUM_TEST_FILES); /* * Step 6: Aggressive Watermark Adjustment. * Shift WMARK_MIN to 55%, WMARK_LOW to ~68%, and WMARK_HIGH to ~82% of total RAM. */ size_t mem_total_kb = get_mem_total_kb(); printf("[+] MemTotal from /proc/meminfo: %zu kB\n", mem_total_kb); size_t min_free_kb = (mem_total_kb * 55) / 100; char val_buf[64]; snprintf(val_buf, sizeof(val_buf), "%zu\n", min_free_kb); int mfk_fd = open("/proc/sys/vm/min_free_kbytes", O_WRONLY); if (mfk_fd >= 0) { write(mfk_fd, val_buf, strlen(val_buf)); close(mfk_fd); printf("[+] Set /proc/sys/vm/min_free_kbytes to %s", val_buf); } else { printf("[-] Failed to open /proc/sys/vm/min_free_kbytes: %s\n", strerror(errno)); } int wmk_fd = open("/proc/sys/vm/watermark_scale_factor", O_WRONLY); if (wmk_fd >= 0) { write(wmk_fd, "1000\n", 5); close(wmk_fd); printf("[+] Set /proc/sys/vm/watermark_scale_factor to 1000\n"); } else { printf("[-] Failed to open /proc/sys/vm/watermark_scale_factor: %s\n", strerror(errno)); } int vfs_fd = open("/proc/sys/vm/vfs_cache_pressure", O_WRONLY); if (vfs_fd >= 0) { write(vfs_fd, "10000\n", 6); close(vfs_fd); printf("[+] Set /proc/sys/vm/vfs_cache_pressure to 10000\n"); } else { printf("[-] Failed to open /proc/sys/vm/vfs_cache_pressure: %s\n", strerror(errno)); } int oom_fd = open("/proc/self/oom_score_adj", O_WRONLY); if (oom_fd >= 0) { write(oom_fd, "-1000\n", 6); close(oom_fd); printf("[+] Set parent oom_score_adj to -1000\n"); } /* * Step 7: Fast Direct Reclaim Trigger (Edge #0: fs_reclaim -> &osb->journal_shutdown_sem). * Single child consumer process touching 1 byte per 4096-byte page. * With min_free_kbytes at 55% of total memory, allocating ~75% of memory will breach * watermarks immediately and force synchronous direct reclaim (__perform_reclaim) * and kswapd to call super_cache_scan() -> prune_dcache_sb() -> dentry_kill() * -> ocfs2_dentry_iput() -> ocfs2_clear_inode() -> down_read(&osb->journal_shutdown_sem). */ size_t target_kb = (mem_total_kb * 75) / 100; #define CHUNK_SZ (16 * 1024 * 1024) size_t nr_chunks = (target_kb * 1024) / CHUNK_SZ; printf("[+] Target allocation: %zu kB (%zu chunks of 16MB)\n", target_kb, nr_chunks); pid_t child = fork(); if (child < 0) { printf("[-] Fork failed: %s\n", strerror(errno)); goto cleanup_loop; } if (child == 0) { int oom = open("/proc/self/oom_score_adj", O_WRONLY); if (oom >= 0) { write(oom, "1000\n", 5); close(oom); } size_t allocated = 0; for (size_t i = 0; i < nr_chunks; i++) { void *p = mmap(NULL, CHUNK_SZ, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (p == MAP_FAILED) { printf("[-] Child mmap failed at chunk %zu: %s\n", i, strerror(errno)); break; } allocated++; volatile char *cp = (volatile char *)p; for (size_t off = 0; off < CHUNK_SZ; off += 4096) { cp[off] = 1; } } printf("[+] Child successfully faulted %zu chunks (%zu MB).\n", allocated, (allocated * CHUNK_SZ) / (1024 * 1024)); /* Additional rolling allocations to keep direct reclaim pulsing */ for (int r = 0; r < 50; r++) { void *rp = mmap(NULL, CHUNK_SZ, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (rp != MAP_FAILED) { volatile char *rcp = (volatile char *)rp; for (size_t off = 0; off < CHUNK_SZ; off += 4096) { rcp[off] = 1; } munmap(rp, CHUNK_SZ); } usleep(20000); } printf("[+] Child completed allocation sequence. Sleeping 3s...\n"); sleep(3); exit(0); } /* Step 8: Parent monitors child with 30s timeout */ printf("[*] Parent waiting for child (PID %d) to force direct reclaim...\n", child); int status = 0; int child_done = 0; for (int t = 0; t < 30; t++) { pid_t ret = waitpid(child, &status, WNOHANG); if (ret == child) { child_done = 1; if (WIFEXITED(status)) printf("[+] Child exited with code %d\n", WEXITSTATUS(status)); else if (WIFSIGNALED(status)) printf("[+] Child terminated by signal %d (likely OOM killer)\n", WTERMSIG(status)); break; } sleep(1); } if (!child_done) { printf("[*] Child timed out after 30s, terminating...\n"); kill(child, SIGKILL); waitpid(child, &status, 0); } printf("[*] Sleeping 2s to allow kswapd to complete in-flight reclaim...\n"); sleep(2); umount(mnt_point); cleanup_loop: loop_fd = open(loop_path, O_RDWR); if (loop_fd >= 0) { ioctl(loop_fd, LOOP_CLR_FD, 0); close(loop_fd); printf("[+] Released loop device %s via LOOP_CLR_FD.\n", loop_path); } unlink(img_path); rmdir(mnt_point); if (g_cg_path[0]) rmdir(g_cg_path); printf("[+] Reproducer sequence finished successfully.\n"); return 0; }