// https://syzkaller.appspot.com/bug?id=e557a28047ff9100b9b66ec1674d2cc5aad15e50 #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #define IMG_SIZE (2 * 1024 * 1024) #define ALIGN8(x) (((x) + 7) & ~7) #pragma pack(push, 1) struct MFT_REC { uint32_t magic; // 0x00: 'FILE' (0x454C4946) uint16_t fix_off; // 0x04: 0x30 uint16_t fix_num; // 0x06: 3 uint64_t lsn; // 0x08: 0 uint16_t seq; // 0x10: sequence number uint16_t hard_links; // 0x12: 1 uint16_t attr_off; // 0x14: 0x38 uint16_t flags; // 0x16: flags | 1 uint32_t used; // 0x18: dynamically tracked uint32_t total; // 0x1C: 1024 uint64_t parent_ref; // 0x20: 0 uint16_t next_attr_id; // 0x28: 0 uint16_t res; // 0x2A: 0 uint32_t mft_record; // 0x2C: rno uint16_t fixups[3]; // 0x30: fixup sample & tails uint16_t pad; // 0x36: 0 }; #pragma pack(pop) static void init_rec(uint8_t *rec, uint32_t rno, uint16_t flags) { memset(rec, 0, 1024); struct MFT_REC *m = (struct MFT_REC *)rec; m->magic = 0x454C4946; m->fix_off = 0x30; m->fix_num = 3; m->seq = rno ? rno : 1; m->hard_links = 1; m->attr_off = 0x38; m->flags = flags | 1; m->used = 0x38; m->total = 1024; m->mft_record = rno; } static void end_rec(uint8_t *rec) { struct MFT_REC *m = (struct MFT_REC *)rec; uint32_t off = m->used; *(uint32_t *)(rec + off) = 0xFFFFFFFF; *(uint32_t *)(rec + off + 4) = 0; m->used = off + 8; uint16_t sample = 0x0042; m->fixups[0] = sample; m->fixups[1] = *(uint16_t *)(rec + 510); m->fixups[2] = *(uint16_t *)(rec + 1022); *(uint16_t *)(rec + 510) = sample; *(uint16_t *)(rec + 1022) = sample; } static void add_res_attr_named(uint8_t *rec, uint32_t type, const uint16_t *name, uint8_t name_len, const void *data, uint32_t data_len) { struct MFT_REC *m = (struct MFT_REC *)rec; uint32_t off = m->used; uint8_t *p = rec + off; uint16_t name_off = 0; uint16_t data_off = 0x18; if (name_len > 0) { name_off = 0x18; data_off = ALIGN8(0x18 + name_len * sizeof(uint16_t)); } uint32_t attr_size = ALIGN8(data_off + data_len); *(uint32_t *)(p + 0x00) = type; *(uint32_t *)(p + 0x04) = attr_size; *(uint8_t *)(p + 0x08) = 0; // resident *(uint8_t *)(p + 0x09) = name_len; *(uint16_t *)(p + 0x0A) = name_off; *(uint16_t *)(p + 0x0C) = 0; *(uint16_t *)(p + 0x0E) = m->next_attr_id++; *(uint32_t *)(p + 0x10) = data_len; *(uint16_t *)(p + 0x14) = data_off; *(uint8_t *)(p + 0x16) = (type == 0x30) ? 1 : 0; *(uint8_t *)(p + 0x17) = 0; if (name_len > 0 && name) { memcpy(p + name_off, name, name_len * sizeof(uint16_t)); } if (data_len > 0 && data) { memcpy(p + data_off, data, data_len); } m->used = off + attr_size; } static void add_res_attr(uint8_t *rec, uint32_t type, const void *data, uint32_t data_len) { add_res_attr_named(rec, type, NULL, 0, data, data_len); } static void add_std_attr(uint8_t *rec, uint32_t fa) { uint8_t std[48]; memset(std, 0, sizeof(std)); *(uint32_t *)(std + 0x20) = fa; add_res_attr(rec, 0x10, std, sizeof(std)); } static void add_name_attr(uint8_t *rec, const char *name_str) { uint8_t buf[256]; memset(buf, 0, sizeof(buf)); // MFT_REF home: rno = 5, seq = 5 *(uint32_t *)(buf + 0x00) = 5; *(uint16_t *)(buf + 0x04) = 0; *(uint16_t *)(buf + 0x06) = 5; size_t len = strlen(name_str); *(uint8_t *)(buf + 0x40) = (uint8_t)len; *(uint8_t *)(buf + 0x41) = 0; // FILE_NAME_POSIX uint16_t *wname = (uint16_t *)(buf + 0x42); for (size_t i = 0; i < len; i++) { wname[i] = (uint16_t)(uint8_t)name_str[i]; } uint32_t fname_size = 0x42 + (uint32_t)(len * sizeof(uint16_t)); if (fname_size < 0x44) { fname_size = 0x44; } add_res_attr(rec, 0x30, buf, fname_size); } static void add_nonres_attr(uint8_t *rec, uint32_t type, uint64_t svcn, uint64_t evcn, uint64_t alloc_sz, uint64_t data_sz, const uint8_t *run, uint32_t run_len) { struct MFT_REC *m = (struct MFT_REC *)rec; uint32_t off = m->used; uint8_t *p = rec + off; uint16_t run_off = 0x40; uint32_t attr_size = ALIGN8(run_off + run_len); *(uint32_t *)(p + 0x00) = type; *(uint32_t *)(p + 0x04) = attr_size; *(uint8_t *)(p + 0x08) = 1; // non-resident *(uint8_t *)(p + 0x09) = 0; *(uint16_t *)(p + 0x0A) = 0; *(uint16_t *)(p + 0x0C) = 0; *(uint16_t *)(p + 0x0E) = m->next_attr_id++; *(uint64_t *)(p + 0x10) = svcn; *(uint64_t *)(p + 0x18) = evcn; *(uint16_t *)(p + 0x20) = run_off; *(uint8_t *)(p + 0x22) = 0; memset(p + 0x23, 0, 5); *(uint64_t *)(p + 0x28) = alloc_sz; *(uint64_t *)(p + 0x30) = data_sz; *(uint64_t *)(p + 0x38) = data_sz; // valid_size if (run_len > 0 && run) { memcpy(p + run_off, run, run_len); } m->used = off + attr_size; } static void add_root_attr(uint8_t *rec) { uint8_t root_data[48]; memset(root_data, 0, sizeof(root_data)); *(uint32_t *)(root_data + 0x00) = 0x30; // ATTR_NAME *(uint32_t *)(root_data + 0x04) = 1; // COLLATION_FILE_NAME *(uint32_t *)(root_data + 0x08) = 4096; // index_block_size *(uint8_t *)(root_data + 0x0C) = 1; // index_block_clst // INDEX_HDR at offset 0x10 *(uint32_t *)(root_data + 0x10) = 16; // de_off *(uint32_t *)(root_data + 0x14) = 32; // used *(uint32_t *)(root_data + 0x18) = 32; // total *(uint32_t *)(root_data + 0x1C) = 0; // flags // NTFS_DE at offset 0x20 *(uint16_t *)(root_data + 0x28) = 16; // size *(uint16_t *)(root_data + 0x2A) = 0; // key_size *(uint16_t *)(root_data + 0x2C) = 2; // NTFS_IE_LAST *(uint16_t *)(root_data + 0x2E) = 0; uint16_t i30_name[4] = { '$', 'I', '3', '0' }; add_res_attr_named(rec, 0x90, i30_name, 4, root_data, sizeof(root_data)); } static void create_ntfs_image(uint8_t *img) { memset(img, 0, IMG_SIZE); // 1. Boot sector at sector 0 img[0] = 0xEB; img[1] = 0x52; img[2] = 0x90; memcpy(img + 0x03, "NTFS ", 8); img[0x0B] = 0x00; img[0x0C] = 0x02; // 512 bytes / sector img[0x0D] = 8; // 8 sectors / cluster (4096 bytes) img[0x15] = 0xF8; // media *(uint16_t *)(img + 0x18) = 63; // sectors per track *(uint16_t *)(img + 0x1A) = 255; // heads img[0x24] = 0x80; // bios drive num img[0x26] = 0x80; // extended signature *(uint64_t *)(img + 0x28) = 4095; // sectors_per_volume *(uint64_t *)(img + 0x30) = 4; // mft_clst *(uint64_t *)(img + 0x38) = 4; // mft2_clst *(int8_t *)(img + 0x40) = -10; // record_size (1024) *(int8_t *)(img + 0x44) = 1; // index_size (4096) *(uint64_t *)(img + 0x48) = 0x1234567812345678ULL; img[0x1FE] = 0x55; img[0x1FF] = 0xAA; // Backup boot sector at sector 4095 memcpy(img + 4095 * 512, img, 512); // 2. MFT records at clusters 4..7 (offset 16384) uint8_t *mft = img + 4 * 4096; // Record 0: $MFT init_rec(mft + 0 * 1024, 0, 0); add_std_attr(mft + 0 * 1024, 0x20); add_name_attr(mft + 0 * 1024, "$MFT"); const uint8_t run_mft[] = { 0x11, 0x04, 0x04, 0x00 }; // 4 clusters at LCN 4 add_nonres_attr(mft + 0 * 1024, 0x80, 0, 3, 16384, 16384, run_mft, sizeof(run_mft)); const uint8_t run_mft_bmp[] = { 0x11, 0x01, 0x28, 0x00 }; // 1 cluster at LCN 40 add_nonres_attr(mft + 0 * 1024, 0xB0, 0, 0, 4096, 2, run_mft_bmp, sizeof(run_mft_bmp)); end_rec(mft + 0 * 1024); // Record 1: $MFTMirr init_rec(mft + 1 * 1024, 1, 0); add_std_attr(mft + 1 * 1024, 0x20); add_name_attr(mft + 1 * 1024, "$MFTMirr"); const uint8_t run_mirr[] = { 0x11, 0x04, 0x04, 0x00 }; add_nonres_attr(mft + 1 * 1024, 0x80, 0, 3, 16384, 16384, run_mirr, sizeof(run_mirr)); end_rec(mft + 1 * 1024); // Record 2: $LogFile init_rec(mft + 2 * 1024, 2, 0); add_std_attr(mft + 2 * 1024, 0x20); add_name_attr(mft + 2 * 1024, "$LogFile"); const uint8_t run_log[] = { 0x11, 0x40, 0x2A, 0x00 }; // 64 clusters at LCN 42 add_nonres_attr(mft + 2 * 1024, 0x80, 0, 63, 262144, 262144, run_log, sizeof(run_log)); end_rec(mft + 2 * 1024); // Record 3: $Volume init_rec(mft + 3 * 1024, 3, 0); add_std_attr(mft + 3 * 1024, 0x20); add_name_attr(mft + 3 * 1024, "$Volume"); uint8_t vinfo[12] = { 0 }; vinfo[8] = 1; // major_ver = 1 vinfo[9] = 2; // minor_ver = 2 add_res_attr(mft + 3 * 1024, 0x70, vinfo, sizeof(vinfo)); add_res_attr(mft + 3 * 1024, 0x80, NULL, 0); // Empty resident ATTR_DATA to set S_IFREG end_rec(mft + 3 * 1024); // Record 4: $AttrDef init_rec(mft + 4 * 1024, 4, 0); add_std_attr(mft + 4 * 1024, 0x20); add_name_attr(mft + 4 * 1024, "$AttrDef"); uint8_t adef[160] = { 0 }; *(uint32_t *)(adef + 0x80) = 0x10; // type = ATTR_STD *(uint64_t *)(adef + 0x98) = 0x48; // max_sz = 0x48 add_res_attr(mft + 4 * 1024, 0x80, adef, sizeof(adef)); end_rec(mft + 4 * 1024); // Record 5: . (Root directory) init_rec(mft + 5 * 1024, 5, 2); // RECORD_FLAG_DIR add_std_attr(mft + 5 * 1024, 0x10); add_name_attr(mft + 5 * 1024, "."); add_root_attr(mft + 5 * 1024); end_rec(mft + 5 * 1024); // Record 6: $Bitmap init_rec(mft + 6 * 1024, 6, 0); add_std_attr(mft + 6 * 1024, 0x20); add_name_attr(mft + 6 * 1024, "$Bitmap"); const uint8_t run_vol_bmp[] = { 0x11, 0x01, 0x29, 0x00 }; // 1 cluster at LCN 41 add_nonres_attr(mft + 6 * 1024, 0x80, 0, 0, 4096, 64, run_vol_bmp, sizeof(run_vol_bmp)); end_rec(mft + 6 * 1024); // Record 7: unused init_rec(mft + 7 * 1024, 7, 0); end_rec(mft + 7 * 1024); // Record 8: $BadClus init_rec(mft + 8 * 1024, 8, 0); add_std_attr(mft + 8 * 1024, 0x20); add_name_attr(mft + 8 * 1024, "$BadClus"); add_nonres_attr(mft + 8 * 1024, 0x80, 0, (uint64_t)-1, 0, 0, NULL, 0); end_rec(mft + 8 * 1024); // Record 9: unused init_rec(mft + 9 * 1024, 9, 0); end_rec(mft + 9 * 1024); // Record 10: $UpCase init_rec(mft + 10 * 1024, 10, 0); add_std_attr(mft + 10 * 1024, 0x20); add_name_attr(mft + 10 * 1024, "$UpCase"); const uint8_t run_upcase[] = { 0x11, 0x20, 0x08, 0x00 }; // 32 clusters at LCN 8 add_nonres_attr(mft + 10 * 1024, 0x80, 0, 31, 131072, 131072, run_upcase, sizeof(run_upcase)); end_rec(mft + 10 * 1024); for (int r = 11; r < 16; r++) { init_rec(mft + r * 1024, r, 0); end_rec(mft + r * 1024); } // 3. Populate on-disk cluster data // Clusters 8..39: $UpCase identity table (128 KB) uint16_t *upcase = (uint16_t *)(img + 8 * 4096); for (int i = 0; i < 65536; i++) { upcase[i] = (uint16_t)i; } // Cluster 40: MFT bitmap (records 0..15 in use) img[40 * 4096] = 0xFF; img[40 * 4096 + 1] = 0xFF; // Cluster 41: Volume bitmap (clusters 0..127 in use) memset(img + 41 * 4096, 0xFF, 16); // Clusters 42..105: $LogFile data (64 clusters of 0xFF) memset(img + 42 * 4096, 0xFF, 64 * 4096); } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[*] Starting reproducer for ntfs_put_super proc registration bug...\n"); /* 1. Mount debugfs if not already mounted */ mkdir("/sys/kernel/debug", 0755); int res = mount("none", "/sys/kernel/debug", "debugfs", 0, NULL); if (res == 0) { printf("[+] debugfs mounted successfully.\n"); } else if (errno == EBUSY) { printf("[+] debugfs already mounted.\n"); } else { printf("[-] Failed to mount debugfs: %s\n", strerror(errno)); exit(1); } /* 2. Configure failslab fault-injection settings */ int ign_fd = open("/sys/kernel/debug/failslab/ignore-gfp-wait", O_WRONLY); if (ign_fd < 0) { printf("[-] Failed to open /sys/kernel/debug/failslab/ignore-gfp-wait: %s\n", strerror(errno)); exit(1); } if (write(ign_fd, "N\n", 2) != 2) { printf("[-] Failed to write to ignore-gfp-wait: %s\n", strerror(errno)); exit(1); } close(ign_fd); printf("[+] Configured failslab ignore-gfp-wait to 'N'.\n"); int cf_fd = open("/sys/kernel/debug/failslab/cache-filter", O_WRONLY); if (cf_fd < 0) { printf("[-] Failed to open /sys/kernel/debug/failslab/cache-filter: %s\n", strerror(errno)); exit(1); } if (write(cf_fd, "N\n", 2) != 2) { printf("[-] Failed to write to cache-filter: %s\n", strerror(errno)); exit(1); } close(cf_fd); printf("[+] Configured failslab cache-filter to 'N'.\n"); int times_fd = open("/sys/kernel/debug/failslab/times", O_WRONLY); if (times_fd >= 0) { if (write(times_fd, "100000\n", 7) != 7) { printf("[-] Failed to write to times: %s\n", strerror(errno)); exit(1); } close(times_fd); printf("[+] Configured failslab times to 100000.\n"); } /* 3. Generate the NTFS image buffer and write to /tmp/ntfs.img */ uint8_t *img = malloc(IMG_SIZE); if (!img) { printf("[-] Failed to allocate memory for NTFS image: %s\n", strerror(errno)); exit(1); } create_ntfs_image(img); const char *img_path = "/tmp/ntfs.img"; int img_fd = open(img_path, O_RDWR | O_CREAT | O_TRUNC, 0666); if (img_fd < 0) { printf("[-] Failed to open %s: %s\n", img_path, strerror(errno)); exit(1); } ssize_t written = write(img_fd, img, IMG_SIZE); if (written != IMG_SIZE) { printf("[-] Failed to write image data to %s: %s\n", img_path, strerror(errno)); exit(1); } free(img); printf("[+] Created NTFS image file %s (%d bytes).\n", img_path, IMG_SIZE); /* 4. Associate the image with a 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 devnr = ioctl(ctrl_fd, LOOP_CTL_GET_FREE); if (devnr < 0) { printf("[-] Failed to get free loop device: %s\n", strerror(errno)); exit(1); } close(ctrl_fd); printf("[+] Allocated loop device index: %d\n", devnr); char loop_path[64]; snprintf(loop_path, sizeof(loop_path), "/dev/loop%d", devnr); int loop_fd = open(loop_path, O_RDWR); if (loop_fd < 0) { printf("[-] Failed to open %s: %s\n", loop_path, strerror(errno)); exit(1); } if (ioctl(loop_fd, LOOP_SET_FD, img_fd) < 0) { printf("[-] Failed to LOOP_SET_FD on %s: %s\n", loop_path, strerror(errno)); exit(1); } close(img_fd); close(loop_fd); /* CRITICAL: Close loop_fd immediately so bd_writers becomes 0 */ printf("[+] Associated %s with %s and closed loop_fd.\n", loop_path, img_path); /* 5. Create mount point directory */ const char *mnt_path = "/tmp/ntfs_mnt"; if (mkdir(mnt_path, 0755) < 0 && errno != EEXIST) { printf("[-] Failed to mkdir %s: %s\n", mnt_path, strerror(errno)); exit(1); } printf("[+] Created mount point %s.\n", mnt_path); /* 6. Verify baseline mount without fault-injection */ res = mount(loop_path, mnt_path, "ntfs3", MS_RDONLY, NULL); if (res < 0) { printf("[-] Baseline mount failed: %s\n", strerror(errno)); exit(1); } printf("[+] Baseline mount successful! In-memory NTFS structures are valid.\n"); res = umount2(mnt_path, MNT_FORCE); if (res < 0) { printf("[-] Baseline umount failed: %s\n", strerror(errno)); exit(1); } printf("[+] Baseline unmount successful.\n"); /* 7. Open fail-nth interface */ int fail_fd = open("/proc/self/fail-nth", O_RDWR); if (fail_fd < 0) { fail_fd = open("/proc/thread-self/fail-nth", O_RDWR); } if (fail_fd < 0) { char tid_path[64]; snprintf(tid_path, sizeof(tid_path), "/proc/self/task/%ld/fail-nth", syscall(SYS_gettid)); fail_fd = open(tid_path, O_RDWR); } if (fail_fd < 0) { printf("[-] Failed to open fail-nth: %s\n", strerror(errno)); exit(1); } printf("[+] fail-nth interface opened successfully.\n"); /* 8. Sweep fail-nth across mount attempts to inject allocation failure * into proc_create_data("volinfo", ...) during ntfs_create_procdir. */ printf("[*] Sweeping fail-nth to trigger proc registration bug...\n"); int consecutive_clean_mounts = 0; for (int nth = 1; nth <= 1000; nth++) { char val[32]; int vlen = snprintf(val, sizeof(val), "%d\n", nth); if (pwrite(fail_fd, val, vlen, 0) < 0) { printf("[-] Failed to set fail-nth to %d: %s\n", nth, strerror(errno)); exit(1); } int mres = mount(loop_path, mnt_path, "ntfs3", MS_RDONLY, NULL); // Immediately read remaining count and disable fail-nth for cleanup operations char rbuf[32] = {0}; if (pread(fail_fd, rbuf, sizeof(rbuf) - 1, 0) < 0) { printf("[-] Failed to read fail-nth: %s\n", strerror(errno)); exit(1); } int rem = atoi(rbuf); if (pwrite(fail_fd, "0\n", 2, 0) < 0) { printf("[-] Failed to reset fail-nth: %s\n", strerror(errno)); exit(1); } if (mres == 0) { // Unmount to trigger ntfs_put_super() -> ntfs_remove_procdir() int ures = umount2(mnt_path, MNT_FORCE); if (ures < 0) { printf("[-] umount2 failed on %s: %s\n", mnt_path, strerror(errno)); exit(1); } if (rem > 0) { consecutive_clean_mounts++; if (consecutive_clean_mounts >= 20) { printf("[*] Reached end of allocation chain at nth=%d (rem=%d).\n", nth, rem); break; } } else { consecutive_clean_mounts = 0; printf("[+] Mount succeeded with fault injected on nth=%d (rem=0). Unmounted successfully.\n", nth); } } else { consecutive_clean_mounts = 0; } if (nth % 50 == 0) { printf("[*] Tested up to nth=%d...\n", nth); } } close(fail_fd); /* 9. Cleanup loop device and files */ int clr_fd = open(loop_path, O_RDWR); if (clr_fd >= 0) { if (ioctl(clr_fd, LOOP_CLR_FD, 0) < 0) { printf("[-] Failed to LOOP_CLR_FD on %s: %s\n", loop_path, strerror(errno)); } else { printf("[+] Disassociated loop device %s.\n", loop_path); } close(clr_fd); } if (unlink(img_path) < 0) { printf("[-] Failed to unlink %s: %s\n", img_path, strerror(errno)); } if (rmdir(mnt_path) < 0) { printf("[-] Failed to rmdir %s: %s\n", mnt_path, strerror(errno)); } /* 10. Allow kernel logging to settle */ printf("[+] Completed test sweep. Waiting for asynchronous warnings...\n"); sleep(2); printf("[+] Reproducer completed successfully.\n"); return 0; }