// https://syzkaller.appspot.com/bug?id=d62e34f70127a31cf5723745b97580345e1e44c7 #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #define IMG_SIZE (1024 * 1024) static inline void write_le16(void *buf, uint16_t val) { uint8_t *p = (uint8_t *)buf; p[0] = val & 0xff; p[1] = (val >> 8) & 0xff; } static inline void write_le32(void *buf, uint32_t val) { uint8_t *p = (uint8_t *)buf; p[0] = val & 0xff; p[1] = (val >> 8) & 0xff; p[2] = (val >> 16) & 0xff; p[3] = (val >> 24) & 0xff; } static inline void write_le64(void *buf, uint64_t val) { uint8_t *p = (uint8_t *)buf; p[0] = val & 0xff; p[1] = (val >> 8) & 0xff; p[2] = (val >> 16) & 0xff; p[3] = (val >> 24) & 0xff; p[4] = (val >> 32) & 0xff; p[5] = (val >> 40) & 0xff; p[6] = (val >> 48) & 0xff; p[7] = (val >> 56) & 0xff; } static void construct_jfs_image(uint8_t *img) { /* 1. Primary Superblock (offset 0x8000, Block 8) */ memcpy(img + 0x8000 + 0x00, "JFS1", 4); write_le32(img + 0x8000 + 0x04, 2); /* s_version = 2 */ write_le64(img + 0x8000 + 0x08, 2048); /* s_size = 2048 (512-byte sectors) */ write_le32(img + 0x8000 + 0x10, 4096); /* s_bsize = 4096 */ write_le16(img + 0x8000 + 0x14, 12); /* s_l2bsize = 12 */ write_le16(img + 0x8000 + 0x16, 3); /* s_l2bfactor = 3 */ write_le32(img + 0x8000 + 0x18, 512); /* s_pbsize = 512 */ write_le16(img + 0x8000 + 0x1c, 9); /* s_l2pbsize = 9 */ write_le16(img + 0x8000 + 0x1e, 0); /* pad = 0 */ write_le32(img + 0x8000 + 0x20, 256); /* s_agsize = 256 */ write_le32(img + 0x8000 + 0x24, 0x10010100); /* s_flag = JFS_LINUX | JFS_BAD_SAIT | JFS_GROUPCOMMIT */ write_le32(img + 0x8000 + 0x28, 0); /* s_state = FM_CLEAN */ /* 2. Aggregate Inode Map Control Page (offset 0x9000, Block 9) */ write_le32(img + 0x9000 + 0x00, 0xffffffff); /* in_freeiag = -1 */ write_le32(img + 0x9000 + 0x04, 1); /* in_nextiag = 1 */ write_le32(img + 0x9000 + 0x10, 4); /* in_nbperiext = 4 */ write_le32(img + 0x9000 + 0x14, 2); /* in_l2nbperiext = 2 */ /* 3. Aggregate Inode Table (offset 0xb000, Block 11) */ /* Inode 1: AGGREGATE_I (offset 0xb200) */ write_le32(img + 0xb200 + 0x04, 1); /* di_fileset = 1 */ write_le32(img + 0xb200 + 0x08, 1); /* di_number = 1 */ write_le64(img + 0xb200 + 0x18, 8192); /* di_size = 8192 */ write_le64(img + 0xb200 + 0x20, 2); /* di_nblocks = 2 */ write_le32(img + 0xb200 + 0x28, 1); /* di_nlink = 1 */ write_le32(img + 0xb200 + 0x34, 0100600); /* di_mode = S_IFREG | 0600 */ img[0xb200 + 0xe0 + 0x10] = 0xb3; /* di_xtroot header.flag = 0xb3 */ write_le16(img + 0xb200 + 0xe0 + 0x12, 3); /* header.nextindex = 3 */ write_le16(img + 0xb200 + 0xe0 + 0x14, 10); /* header.maxentry = 10 */ write_le32(img + 0xb200 + 0xe0 + 0x20 + 0x08, 2); /* xad[2].loc.len_addr = 2 */ write_le32(img + 0xb200 + 0xe0 + 0x20 + 0x0c, 9); /* xad[2].loc.addr2 = 9 (blocks 9-10) */ /* Inode 2: BMAP_I (offset 0xb400) */ write_le32(img + 0xb400 + 0x04, 2); /* di_fileset = 2 */ write_le32(img + 0xb400 + 0x08, 2); /* di_number = 2 */ write_le64(img + 0xb400 + 0x18, 4096); /* di_size = 4096 */ write_le64(img + 0xb400 + 0x20, 1); /* di_nblocks = 1 */ write_le32(img + 0xb400 + 0x28, 1); /* di_nlink = 1 */ write_le32(img + 0xb400 + 0x34, 0100600); /* di_mode = S_IFREG | 0600 */ img[0xb400 + 0xe0 + 0x10] = 0xb3; /* di_xtroot header.flag = 0xb3 */ write_le16(img + 0xb400 + 0xe0 + 0x12, 3); /* header.nextindex = 3 */ write_le16(img + 0xb400 + 0xe0 + 0x14, 10); /* header.maxentry = 10 */ write_le32(img + 0xb400 + 0xe0 + 0x20 + 0x08, 1); /* xad[2].loc.len_addr = 1 */ write_le32(img + 0xb400 + 0xe0 + 0x20 + 0x0c, 16); /* xad[2].loc.addr2 = 16 (block 16) */ /* Inode 16: FILESYSTEM_I (offset 0xd000, Block 13) */ write_le32(img + 0xd000 + 0x04, 16); /* di_fileset = 16 */ write_le32(img + 0xd000 + 0x08, 16); /* di_number = 16 */ write_le64(img + 0xd000 + 0x18, 8192); /* di_size = 8192 */ write_le64(img + 0xd000 + 0x20, 2); /* di_nblocks = 2 */ write_le32(img + 0xd000 + 0x28, 1); /* di_nlink = 1 */ write_le32(img + 0xd000 + 0x34, 0100600); /* di_mode = S_IFREG | 0600 */ img[0xd000 + 0xe0 + 0x10] = 0xb3; /* di_xtroot header.flag = 0xb3 */ write_le16(img + 0xd000 + 0xe0 + 0x12, 3); /* header.nextindex = 3 */ write_le16(img + 0xd000 + 0xe0 + 0x14, 10); /* header.maxentry = 10 */ write_le32(img + 0xd000 + 0xe0 + 0x20 + 0x08, 2); /* xad[2].loc.len_addr = 2 */ write_le32(img + 0xd000 + 0xe0 + 0x20 + 0x0c, 18); /* xad[2].loc.addr2 = 18 (blocks 18-19) */ /* 4. Block Allocation Map (offset 0x10000, Block 16) */ write_le64(img + 0x10000 + 0x00, 256); /* dn_mapsize = 256 */ write_le64(img + 0x10000 + 0x08, 200); /* dn_nfree = 200 */ write_le32(img + 0x10000 + 0x10, 0); /* dn_l2nbperpage = 0 */ write_le32(img + 0x10000 + 0x14, 1); /* dn_numag = 1 */ write_le32(img + 0x10000 + 0x18, 0); /* dn_maxlevel = 0 */ write_le32(img + 0x10000 + 0x1c, 0); /* dn_maxag = 0 */ write_le32(img + 0x10000 + 0x20, 0); /* dn_agpref = 0 */ write_le32(img + 0x10000 + 0x24, 0); /* dn_aglevel = 0 */ write_le32(img + 0x10000 + 0x28, 0); /* dn_agheight = 0 */ write_le32(img + 0x10000 + 0x2c, 1); /* dn_agwidth = 1 */ write_le32(img + 0x10000 + 0x30, 0); /* dn_agstart = 0 */ write_le32(img + 0x10000 + 0x34, 8); /* dn_agl2size = 8 */ write_le64(img + 0x10000 + 0x38, 200); /* dn_agfree[0] = 200 */ write_le64(img + 0x10000 + 0x438, 256); /* dn_agsize = 256 */ img[0x10000 + 0x440] = 0xff; /* dn_maxfreebud = -1 */ /* 5. Fileset Inode Map Control Page (offset 0x12000, Block 18) */ write_le32(img + 0x12000 + 0x00, 0xffffffff); /* in_freeiag = -1 */ write_le32(img + 0x12000 + 0x04, 1); /* in_nextiag = 1 */ write_le32(img + 0x12000 + 0x08, 32); /* in_numinos = 32 */ write_le32(img + 0x12000 + 0x0c, 29); /* in_numfree = 29 */ write_le32(img + 0x12000 + 0x10, 4); /* in_nbperiext = 4 */ write_le32(img + 0x12000 + 0x14, 2); /* in_l2nbperiext = 2 */ write_le32(img + 0x12000 + 0x800 + 0x00, 0); /* in_agctl[0].inofree = 0 */ write_le32(img + 0x12000 + 0x800 + 0x04, 0); /* in_agctl[0].extfree = 0 */ write_le32(img + 0x12000 + 0x800 + 0x08, 32); /* in_agctl[0].numinos = 32 */ write_le32(img + 0x12000 + 0x800 + 0x0c, 29); /* in_agctl[0].numfree = 29 */ for (int i = 1; i < 128; i++) { write_le32(img + 0x12000 + 0x800 + i * 16 + 0x00, 0xffffffff); /* inofree = -1 */ write_le32(img + 0x12000 + 0x800 + i * 16 + 0x04, 0xffffffff); /* extfree = -1 */ write_le32(img + 0x12000 + 0x800 + i * 16 + 0x08, 0); /* numinos = 0 */ write_le32(img + 0x12000 + 0x800 + i * 16 + 0x0c, 0); /* numfree = 0 */ } /* Fileset IAG 0 (offset 0x13000, Block 19) */ write_le64(img + 0x13000 + 0x00, 0); /* agstart = 0 */ write_le32(img + 0x13000 + 0x08, 0); /* iagnum = 0 */ write_le32(img + 0x13000 + 0x0c, 0xffffffff); /* inofreefwd = -1 */ write_le32(img + 0x13000 + 0x10, 0xffffffff); /* inofreeback = -1 */ write_le32(img + 0x13000 + 0x14, 0xffffffff); /* extfreefwd = -1 */ write_le32(img + 0x13000 + 0x18, 0xffffffff); /* extfreeback = -1 */ write_le32(img + 0x13000 + 0x1c, 0xffffffff); /* iagfree = -1 */ write_le32(img + 0x13000 + 0x30, 0xffffffff); /* extsmap[0] = 0xffffffff */ write_le32(img + 0x13000 + 0x40, 29); /* nfreeinos = 29 */ write_le32(img + 0x13000 + 0x44, 127); /* nfreeexts = 127 */ write_le32(img + 0x13000 + 0x800, 0xf8000000);/* wmap[0] marks inodes 0..4 allocated */ write_le32(img + 0x13000 + 0xa00, 0xf8000000);/* pmap[0] marks inodes 0..4 allocated */ write_le32(img + 0x13000 + 0xc00, 4); /* inoext[0].loc.len_addr = 4 */ write_le32(img + 0x13000 + 0xc04, 20); /* inoext[0].loc.addr2 = 20 (block 20) */ /* 6. Fileset Inode Extent 0 (offset 0x14000, Block 20) */ /* Inode 2: ROOT_I (offset 0x14400) */ write_le32(img + 0x14400 + 0x04, 16); /* di_fileset = 16 */ write_le32(img + 0x14400 + 0x08, 2); /* di_number = 2 */ write_le32(img + 0x14400 + 0x10, 4); /* di_ixpxd.len_addr = 4 */ write_le32(img + 0x14400 + 0x14, 20); /* di_ixpxd.addr2 = 20 */ write_le64(img + 0x14400 + 0x18, 256); /* di_size = 256 */ write_le64(img + 0x14400 + 0x20, 0); /* di_nblocks = 0 */ write_le32(img + 0x14400 + 0x28, 1); /* di_nlink = 1 (critical under-count) */ write_le32(img + 0x14400 + 0x34, 0040755); /* di_mode = S_IFDIR | 0755 */ /* ROOT_I di_dtroot (offset 0x14400 + 0xe0 = 0x144e0) */ img[0x144e0 + 0x10] = 0xb3; /* header.flag = 0xb3 */ img[0x144e0 + 0x11] = 2; /* header.nextindex = 2 entries */ img[0x144e0 + 0x12] = 6; /* header.freecnt = 6 */ img[0x144e0 + 0x13] = 3; /* header.freelist = 3 */ write_le32(img + 0x144e0 + 0x14, 2); /* header.idotdot = 2 */ img[0x144e0 + 0x18] = 1; /* header.stbl[0] = 1 ("d1") */ img[0x144e0 + 0x19] = 2; /* header.stbl[1] = 2 ("d2") */ /* slot[1] (entry for "d1" at 0x144e0 + 0x20) */ write_le32(img + 0x144e0 + 0x20 + 0x00, 3); /* inumber = 3 */ img[0x144e0 + 0x20 + 0x04] = 0xff; /* next = -1 */ img[0x144e0 + 0x20 + 0x05] = 2; /* namlen = 2 */ write_le16(img + 0x144e0 + 0x20 + 0x06, 0x0064); /* 'd' */ write_le16(img + 0x144e0 + 0x20 + 0x08, 0x0031); /* '1' */ /* slot[2] (entry for "d2" at 0x144e0 + 0x40) */ write_le32(img + 0x144e0 + 0x40 + 0x00, 4); /* inumber = 4 */ img[0x144e0 + 0x40 + 0x04] = 0xff; /* next = -1 */ img[0x144e0 + 0x40 + 0x05] = 2; /* namlen = 2 */ write_le16(img + 0x144e0 + 0x40 + 0x06, 0x0064); /* 'd' */ write_le16(img + 0x144e0 + 0x40 + 0x08, 0x0032); /* '2' */ /* Freelist chain for slots 3..8 */ img[0x144e0 + 0x60] = 4; /* slot[3].next = 4 */ img[0x144e0 + 0x80] = 5; /* slot[4].next = 5 */ img[0x144e0 + 0xa0] = 6; /* slot[5].next = 6 */ img[0x144e0 + 0xc0] = 7; /* slot[6].next = 7 */ img[0x144e0 + 0xe0] = 8; /* slot[7].next = 8 */ img[0x144e0 + 0x100] = 0xff; /* slot[8].next = -1 */ /* Inode 3: Subdirectory "d1" (offset 0x14600) */ write_le32(img + 0x14600 + 0x04, 16); /* di_fileset = 16 */ write_le32(img + 0x14600 + 0x08, 3); /* di_number = 3 */ write_le32(img + 0x14600 + 0x10, 4); /* di_ixpxd.len_addr = 4 */ write_le32(img + 0x14600 + 0x14, 20); /* di_ixpxd.addr2 = 20 */ write_le64(img + 0x14600 + 0x18, 256); /* di_size = 256 */ write_le64(img + 0x14600 + 0x20, 0); /* di_nblocks = 0 */ write_le32(img + 0x14600 + 0x28, 2); /* di_nlink = 2 */ write_le32(img + 0x14600 + 0x34, 0040755); /* di_mode = S_IFDIR | 0755 */ img[0x14600 + 0xe0 + 0x10] = 0xb3; /* header.flag = 0xb3 */ img[0x14600 + 0xe0 + 0x11] = 0; /* header.nextindex = 0 */ img[0x14600 + 0xe0 + 0x12] = 8; /* header.freecnt = 8 */ img[0x14600 + 0xe0 + 0x13] = 1; /* header.freelist = 1 */ write_le32(img + 0x14600 + 0xe0 + 0x14, 2); /* header.idotdot = 2 */ for (int s = 1; s <= 7; s++) { img[0x14600 + 0xe0 + s * 32] = (uint8_t)(s + 1); } img[0x14600 + 0xe0 + 8 * 32] = 0xff; /* Inode 4: Subdirectory "d2" (offset 0x14800) */ write_le32(img + 0x14800 + 0x04, 16); /* di_fileset = 16 */ write_le32(img + 0x14800 + 0x08, 4); /* di_number = 4 */ write_le32(img + 0x14800 + 0x10, 4); /* di_ixpxd.len_addr = 4 */ write_le32(img + 0x14800 + 0x14, 20); /* di_ixpxd.addr2 = 20 */ write_le64(img + 0x14800 + 0x18, 256); /* di_size = 256 */ write_le64(img + 0x14800 + 0x20, 0); /* di_nblocks = 0 */ write_le32(img + 0x14800 + 0x28, 2); /* di_nlink = 2 */ write_le32(img + 0x14800 + 0x34, 0040755); /* di_mode = S_IFDIR | 0755 */ img[0x14800 + 0xe0 + 0x10] = 0xb3; /* header.flag = 0xb3 */ img[0x14800 + 0xe0 + 0x11] = 0; /* header.nextindex = 0 */ img[0x14800 + 0xe0 + 0x12] = 8; /* header.freecnt = 8 */ img[0x14800 + 0xe0 + 0x13] = 1; /* header.freelist = 1 */ write_le32(img + 0x14800 + 0xe0 + 0x14, 2); /* header.idotdot = 2 */ for (int s = 1; s <= 7; s++) { img[0x14800 + 0xe0 + s * 32] = (uint8_t)(s + 1); } img[0x14800 + 0xe0 + 8 * 32] = 0xff; } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[*] Starting JFS drop_nlink bug reproducer...\n"); /* Allocate and construct zeroed 1 MiB JFS filesystem image */ uint8_t *img = (uint8_t *)calloc(1, IMG_SIZE); if (!img) { printf("[-] Failed to allocate image buffer: %s\n", strerror(errno)); exit(1); } printf("[+] Allocated %d bytes for JFS image.\n", IMG_SIZE); construct_jfs_image(img); printf("[+] Successfully constructed JFS image with undercounted root i_nlink.\n"); const char *img_path = "/tmp/jfs_repro.img"; int img_fd = open(img_path, O_CREAT | O_RDWR | O_TRUNC, 0600); if (img_fd < 0) { printf("[-] Failed to open %s: %s\n", img_path, strerror(errno)); free(img); exit(1); } printf("[+] open(\"%s\") successful.\n", img_path); ssize_t written = write(img_fd, img, IMG_SIZE); if (written != IMG_SIZE) { printf("[-] Failed to write full image (written=%zd): %s\n", written, strerror(errno)); close(img_fd); unlink(img_path); free(img); exit(1); } printf("[+] Written %zd bytes to %s.\n", written, img_path); free(img); int res = fsync(img_fd); if (res < 0) { printf("[-] Failed to fsync image: %s\n", strerror(errno)); close(img_fd); unlink(img_path); exit(1); } printf("[+] fsync(img_fd) successful.\n"); /* Attach image to a loop device */ int loop_ctl_fd = open("/dev/loop-control", O_RDWR); if (loop_ctl_fd < 0) { printf("[-] Failed to open /dev/loop-control: %s\n", strerror(errno)); close(img_fd); unlink(img_path); exit(1); } printf("[+] open(\"/dev/loop-control\") successful.\n"); int dev_nr = ioctl(loop_ctl_fd, LOOP_CTL_GET_FREE); if (dev_nr < 0) { printf("[-] ioctl(LOOP_CTL_GET_FREE) failed: %s\n", strerror(errno)); close(loop_ctl_fd); close(img_fd); unlink(img_path); exit(1); } printf("[+] ioctl(LOOP_CTL_GET_FREE) allocated loop device %d.\n", dev_nr); res = close(loop_ctl_fd); if (res < 0) { printf("[-] Failed to close /dev/loop-control: %s\n", strerror(errno)); close(img_fd); unlink(img_path); exit(1); } printf("[+] close(loop_ctl_fd) successful.\n"); char loop_dev[64]; snprintf(loop_dev, sizeof(loop_dev), "/dev/loop%d", dev_nr); int loop_fd = open(loop_dev, O_RDWR); if (loop_fd < 0) { printf("[-] Failed to open %s: %s\n", loop_dev, strerror(errno)); close(img_fd); unlink(img_path); exit(1); } printf("[+] open(\"%s\") successful.\n", loop_dev); res = ioctl(loop_fd, LOOP_SET_FD, img_fd); if (res < 0) { printf("[-] ioctl(LOOP_SET_FD) failed on %s: %s\n", loop_dev, strerror(errno)); close(loop_fd); close(img_fd); unlink(img_path); exit(1); } printf("[+] Attached image to %s via ioctl(LOOP_SET_FD).\n", loop_dev); /* * Close both img_fd and loop_fd before calling mount(). * mount() requests BLK_OPEN_RESTRICT_WRITES, which fails with -EBUSY * if any user process holds an open file descriptor with write access (O_RDWR). */ res = close(loop_fd); if (res < 0) { printf("[-] Failed to close loop_fd before mount: %s\n", strerror(errno)); close(img_fd); unlink(img_path); exit(1); } printf("[+] close(loop_fd) before mount successful.\n"); res = close(img_fd); if (res < 0) { printf("[-] Failed to close img_fd before mount: %s\n", strerror(errno)); unlink(img_path); exit(1); } printf("[+] close(img_fd) before mount successful.\n"); /* Create mount point and mount JFS with nointegrity */ char mnt_dir[] = "/tmp/jfs_mnt_XXXXXX"; if (mkdtemp(mnt_dir) == NULL) { printf("[-] Failed to create temporary mount directory: %s\n", strerror(errno)); unlink(img_path); exit(1); } printf("[+] mkdtemp successful: %s.\n", mnt_dir); res = mount(loop_dev, mnt_dir, "jfs", 0, "nointegrity"); if (res < 0) { printf("[-] Failed to mount %s on %s: %s\n", loop_dev, mnt_dir, strerror(errno)); rmdir(mnt_dir); int clr_fd = open(loop_dev, O_RDWR); if (clr_fd >= 0) { ioctl(clr_fd, LOOP_CLR_FD, 0); close(clr_fd); } unlink(img_path); exit(1); } printf("[+] mount(\"%s\", \"%s\", \"jfs\", 0, \"nointegrity\") successful.\n", loop_dev, mnt_dir); /* Construct paths to subdirectories d1 and d2 */ char path_d1[256]; char path_d2[256]; snprintf(path_d1, sizeof(path_d1), "%s/d1", mnt_dir); snprintf(path_d2, sizeof(path_d2), "%s/d2", mnt_dir); /* First rmdir: removes d1, drops ROOT_I link count from 1 to 0 */ printf("[*] Removing first directory %s...\n", path_d1); res = unlinkat(AT_FDCWD, path_d1, AT_REMOVEDIR); if (res < 0) { printf("[-] unlinkat(\"%s\", AT_REMOVEDIR) failed: %s\n", path_d1, strerror(errno)); umount(mnt_dir); rmdir(mnt_dir); int clr_fd = open(loop_dev, O_RDWR); if (clr_fd >= 0) { ioctl(clr_fd, LOOP_CLR_FD, 0); close(clr_fd); } unlink(img_path); exit(1); } printf("[+] First directory %s removed successfully (ROOT_I link count dropped to 0).\n", path_d1); /* Second rmdir: removes d2, calls drop_nlink() when ROOT_I link count is 0 -> WARNING */ printf("[*] Removing second directory %s (triggering WARNING in jfs_rmdir)...\n", path_d2); res = unlinkat(AT_FDCWD, path_d2, AT_REMOVEDIR); if (res < 0) { printf("[-] unlinkat(\"%s\", AT_REMOVEDIR) failed: %s\n", path_d2, strerror(errno)); } else { printf("[+] Second directory %s removed successfully.\n", path_d2); } /* Wait briefly to allow any async kernel warnings to flush */ sleep(1); /* Cleanup */ printf("[*] Cleaning up mount and loop device...\n"); res = umount(mnt_dir); if (res < 0) { printf("[-] Failed to umount %s: %s\n", mnt_dir, strerror(errno)); } else { printf("[+] umount(\"%s\") successful.\n", mnt_dir); } res = rmdir(mnt_dir); if (res < 0) { printf("[-] Failed to rmdir %s: %s\n", mnt_dir, strerror(errno)); } else { printf("[+] rmdir(\"%s\") successful.\n", mnt_dir); } loop_fd = open(loop_dev, O_RDWR); if (loop_fd < 0) { printf("[-] Failed to re-open %s for cleanup: %s\n", loop_dev, strerror(errno)); } else { res = ioctl(loop_fd, LOOP_CLR_FD, 0); if (res < 0) { printf("[-] ioctl(LOOP_CLR_FD) failed on %s: %s\n", loop_dev, strerror(errno)); } else { printf("[+] ioctl(LOOP_CLR_FD) on %s successful.\n", loop_dev); } close(loop_fd); } res = unlink(img_path); if (res < 0) { printf("[-] Failed to unlink %s: %s\n", img_path, strerror(errno)); } else { printf("[+] unlink(\"%s\") successful.\n", img_path); } printf("[+] Reproduction sequence completed.\n"); return 0; }