// https://syzkaller.appspot.com/bug?id=f47e98cd0b0abcb731eb137d938f08861dbae58a #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Copyright 2026 syzkaller project authors. All rights reserved. // Use of this source code is governed by Apache 2 LICENSE that can be found in the LICENSE file. // IMPORTANT: Do not copy the macros or definitions below directly into your reproducer. // Instead, add the following line to your reproducer: // #include "race_toolkit.h" // --- Race Condition Toolkit --- // Macros and snippets for CPU pinning, memory barriers, and userfaultfd. #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include // Unbuffered I/O: Ensure logs are written immediately. #define SETUP_UNBUFFERED_IO() setvbuf(stdout, NULL, _IONBF, 0) // CPU Pinning: Pin the current thread to a specific CPU core. #define PIN_TO_CPU(cpu) \ do { \ cpu_set_t mask; \ CPU_ZERO(&mask); \ CPU_SET(cpu, &mask); \ if (sched_setaffinity(0, sizeof(mask), &mask) == -1) { \ perror("sched_setaffinity"); \ } \ } while (0) // Memory Barrier: Ensure memory ordering. #define MB() __atomic_thread_fence(__ATOMIC_SEQ_CST) // Spin-wait Barrier: Wait until a memory location has a specific value. // Best for tight race windows (low latency, no context switches). #define WAIT_ON(addr, val) \ do { \ while (__atomic_load_n(addr, __ATOMIC_ACQUIRE) != (val)) \ ; \ } while (0) // Signal: Set a memory location to a specific value to release a WAIT_ON. #define SIGNAL(addr, val) __atomic_store_n(addr, val, __ATOMIC_RELEASE) // --- Timing Primitives --- // Robust timing loops in VM environments (using CLOCK_MONOTONIC to avoid time(NULL) jumps). static inline double timer_elapsed_sec(struct timespec* start) { struct timespec now; if (clock_gettime(CLOCK_MONOTONIC, &now) == -1) { perror("clock_gettime(CLOCK_MONOTONIC) elapsed"); exit(1); } return (double)(now.tv_sec - start->tv_sec) + (double)(now.tv_nsec - start->tv_nsec) / 1e9; } // Initialize a monotonic timer variable. #define TIMER_START(t) \ struct timespec t; \ if (clock_gettime(CLOCK_MONOTONIC, &t) == -1) { \ perror("clock_gettime(CLOCK_MONOTONIC) start"); \ exit(1); \ } // Check if the elapsed time since 't' is less than 'sec' seconds. #define TIMER_NOT_EXPIRED(t, sec) (timer_elapsed_sec(&(t)) < (double)(sec)) // Futex-based Event: Shared with syzkaller executor. // Best for general synchronization or longer waits to save CPU. typedef struct { int state; } event_t; static void event_init(event_t* ev) { ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE)) { fprintf(stderr, "event already set\n"); exit(1); } __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE); syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000); } static void event_wait(event_t* ev) { while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE)) syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0); } // userfaultfd setup: Register a memory range for page fault handling. static int setup_uffd(void* addr, size_t len) { int uffd = syscall(__NR_userfaultfd, O_CLOEXEC | O_NONBLOCK); if (uffd == -1) return -1; struct uffdio_api api = {.api = UFFD_API, .features = 0}; if (ioctl(uffd, UFFDIO_API, &api) == -1) { close(uffd); return -1; } struct uffdio_register reg = { .range = {.start = (uintptr_t)addr, .len = len}, .mode = UFFDIO_REGISTER_MODE_MISSING}; if (ioctl(uffd, UFFDIO_REGISTER, ®) == -1) { close(uffd); return -1; } return uffd; } // Lookup a kernel symbol (optionally "sym+0xoffset") via /proc/kallsyms. static inline uint64_t kallsyms_lookup(const char* spec) { size_t n = strcspn(spec, "+"); char sym[128]; if (n == 0 || n >= sizeof(sym)) return 0; memcpy(sym, spec, n); sym[n] = '\0'; uint64_t offset = spec[n] == '+' ? strtoull(spec + n + 1, NULL, 0) : 0; FILE* f = fopen("/proc/kallsyms", "r"); if (!f) return 0; uint64_t addr = 0; char line[512]; while (fgets(line, sizeof(line), f)) { unsigned long long a; char name[128]; if (sscanf(line, "%llx %*c %127s", &a, name) == 2 && a != 0 && strcmp(name, sym) == 0) { addr = (uint64_t)a + offset; break; } } fclose(f); return addr; } // Hardware breakpoint delay injector: opens a PERF_TYPE_BREAKPOINT on (pid, addr) and // attaches a bounded BPF_PROG_TYPE_PERF_EVENT loop that stalls inside the #DB handler for // delay_us microseconds (capped by the 32768-iteration loop bound, ~1-3 ms per hit in VMs). // Returns the perf_event fd (close it to detach), or -1 on error. // Call with pid=0 in main() before pthread_create() so all threads inherit the breakpoint. // Note: HW_BREAKPOINT_RW / HW_BREAKPOINT_W work on kernel data addresses even when // CONFIG_KPROBES=n; HW_BREAKPOINT_X requires CONFIG_KPROBES=y for kernel text addresses. static inline int setup_delay_bp(pid_t pid, uint64_t addr, uint32_t bp_type, uint64_t bp_len, uint32_t delay_us) { uint64_t klong = (addr > 0xffffffffULL || sizeof(long) == 8) ? sizeof(uint64_t) : sizeof(long); struct perf_event_attr pe = { .type = PERF_TYPE_BREAKPOINT, .size = sizeof(pe), .bp_type = bp_type, .bp_addr = addr, .bp_len = (bp_type == HW_BREAKPOINT_X) ? klong : bp_len, .sample_period = 1, .disabled = 1, .inherit = 1, .inherit_thread = 1, .remove_on_exec = 1, }; int pfd = syscall(__NR_perf_event_open, &pe, pid, -1, -1, PERF_FLAG_FD_CLOEXEC); if (pfd == -1) return -1; struct bpf_insn insns[] = { // R0 = bpf_ktime_get_ns(); R6 = R0; R7 = 32768 (bounded loop limit); // zero-extend uint32_t delay_us via 32-bit MOV then multiply by 1000 to avoid signed .imm overflow. {.code = BPF_JMP | BPF_CALL, .imm = BPF_FUNC_ktime_get_ns}, {.code = BPF_ALU64 | BPF_MOV | BPF_X, .dst_reg = BPF_REG_6, .src_reg = BPF_REG_0}, {.code = BPF_ALU64 | BPF_MOV | BPF_K, .dst_reg = BPF_REG_7, .imm = 32768}, {.code = BPF_ALU | BPF_MOV | BPF_K, .dst_reg = BPF_REG_8, .imm = (int32_t)delay_us}, {.code = BPF_ALU64 | BPF_MUL | BPF_K, .dst_reg = BPF_REG_8, .imm = 1000}, // loop: if (R7 == 0) goto exit (+4); R7 -= 1; {.code = BPF_JMP | BPF_JEQ | BPF_K, .dst_reg = BPF_REG_7, .off = 4, .imm = 0}, {.code = BPF_ALU64 | BPF_SUB | BPF_K, .dst_reg = BPF_REG_7, .imm = 1}, // R0 = bpf_ktime_get_ns() - R6; if (R0 < R8) goto loop (-5); {.code = BPF_JMP | BPF_CALL, .imm = BPF_FUNC_ktime_get_ns}, {.code = BPF_ALU64 | BPF_SUB | BPF_X, .dst_reg = BPF_REG_0, .src_reg = BPF_REG_6}, {.code = BPF_JMP | BPF_JLT | BPF_X, .dst_reg = BPF_REG_0, .src_reg = BPF_REG_8, .off = -5}, // exit: R0 = 0; exit {.code = BPF_ALU64 | BPF_MOV | BPF_K, .dst_reg = BPF_REG_0, .imm = 0}, {.code = BPF_JMP | BPF_EXIT}, }; union bpf_attr attr = { .prog_type = BPF_PROG_TYPE_PERF_EVENT, .insn_cnt = sizeof(insns) / sizeof(insns[0]), .insns = (uintptr_t)insns, .license = (uintptr_t)"GPL", }; int bfd = syscall(__NR_bpf, BPF_PROG_LOAD, &attr, sizeof(attr)); if (bfd == -1) { close(pfd); return -1; } int ok = ioctl(pfd, PERF_EVENT_IOC_SET_BPF, bfd) == 0 && ioctl(pfd, PERF_EVENT_IOC_ENABLE, 0) == 0; close(bfd); if (!ok) { close(pfd); return -1; } return pfd; } // --- Guidance on Usage --- // 1. Use WAIT_ON/SIGNAL for tight race conditions to avoid scheduling overhead. // 2. Use event_t (futexes) for general coordination or when waiting for longer periods. // 3. Always use PIN_TO_CPU to increase race probability on multi-core systems. // 4. Use setup_uffd to register a memory range for page fault handling, or setup_delay_bp // (with kallsyms_lookup) to inject targeted hardware-breakpoint delays on kernel accesses. // 5. Call SETUP_UNBUFFERED_IO() at the start of main() to ensure that logs are printed // immediately. This is essential for understanding the exact interleaving of events // when debugging race conditions. // 6. For timing-based loops (e.g., running a race for 10 seconds), do NOT use time(NULL) // or loops relying on real-time clocks, as VM clocks are highly unreliable and can fail or drift. // Instead, use the robust monotonic timing primitives TIMER_START and TIMER_NOT_EXPIRED: // TIMER_START(start); // while (TIMER_NOT_EXPIRED(start, 10.0)) { // // Your race logic here // } #define SECTOR_SIZE 512 #define CLUSTER_SIZE 512 #define RECORD_SIZE 1024 #define NUM_CLUSTERS 8192 #define IMAGE_SIZE (NUM_CLUSTERS * CLUSTER_SIZE) /* 4 MB */ #define CLST_BOOT 0 #define CLST_MFT 32 #define CLST_MFT_BMP 96 #define CLST_VOL_BMP 97 #define CLST_UPCASE 100 #define CLST_LOGFILE 356 #define ATTR_STD 0x10 #define ATTR_NAME 0x30 #define ATTR_VOL_INFO 0x70 #define ATTR_DATA 0x80 #define ATTR_ROOT 0x90 #define ATTR_BITMAP 0xB0 #define ATTR_EA 0xE0 #define ATTR_END 0xFFFFFFFF static void init_boot_sector(uint8_t *boot) { memset(boot, 0, SECTOR_SIZE); boot[0] = 0xEB; boot[1] = 0x52; boot[2] = 0x90; memcpy(boot + 0x03, "NTFS ", 8); boot[0x0B] = 0x00; boot[0x0C] = 0x02; /* bytes_per_sector: 512 */ boot[0x0D] = 1; /* sectors_per_clusters: 1 (512B cluster) */ boot[0x15] = 0xF8; /* media: 0xF8 */ *(uint16_t *)(boot + 0x18) = 63; /* sectors per track */ *(uint16_t *)(boot + 0x1A) = 255; /* heads */ boot[0x24] = 0x80; /* drive */ boot[0x26] = 0x80; /* signature */ *(uint64_t *)(boot + 0x28) = 8191; /* sectors_per_volume */ *(uint64_t *)(boot + 0x30) = CLST_MFT; /* mft_clst */ *(uint64_t *)(boot + 0x38) = CLST_MFT; /* mft2_clst */ boot[0x40] = (uint8_t)-10; /* record_size: -10 -> 1024 bytes */ boot[0x44] = (uint8_t)-12; /* index_size: -12 -> 4096 bytes */ *(uint64_t *)(boot + 0x48) = 0x1234567890ABCDEFULL; boot[0x1FE] = 0x55; boot[0x1FF] = 0xAA; } static void init_mft_record(uint8_t *rec, uint16_t seq, uint16_t flags, uint32_t rno) { memset(rec, 0, RECORD_SIZE); *(uint32_t *)(rec + 0x00) = 0x454C4946; /* 'FILE' */ *(uint16_t *)(rec + 0x04) = 0x30; /* fix_off */ *(uint16_t *)(rec + 0x06) = 3; /* fix_num */ *(uint64_t *)(rec + 0x08) = 0; /* lsn */ *(uint16_t *)(rec + 0x10) = seq; /* seq */ *(uint16_t *)(rec + 0x12) = 1; /* hard_links */ *(uint16_t *)(rec + 0x14) = 0x38; /* attr_off */ *(uint16_t *)(rec + 0x16) = flags; /* flags */ *(uint32_t *)(rec + 0x1C) = RECORD_SIZE;/* total */ *(uint32_t *)(rec + 0x2C) = rno; /* mft_record */ } static void add_std_info(uint8_t *rec, uint32_t *off, uint32_t fa, uint16_t id) { uint8_t *a = rec + *off; uint32_t asize = 0x48; memset(a, 0, asize); *(uint32_t *)(a + 0x00) = ATTR_STD; *(uint32_t *)(a + 0x04) = asize; *(uint16_t *)(a + 0x0A) = 0x18; *(uint16_t *)(a + 0x0E) = id; *(uint32_t *)(a + 0x10) = 48; *(uint16_t *)(a + 0x14) = 0x18; *(uint32_t *)(a + 0x38) = fa; *off += asize; } static void add_file_name(uint8_t *rec, uint32_t *off, const char *name, uint32_t fa, uint16_t id) { uint8_t *a = rec + *off; uint8_t len = (uint8_t)strlen(name); uint32_t dsize = 0x42 + len * 2; uint32_t asize = (0x18 + dsize + 7) & ~7; memset(a, 0, asize); *(uint32_t *)(a + 0x00) = ATTR_NAME; *(uint32_t *)(a + 0x04) = asize; *(uint16_t *)(a + 0x0A) = 0x18; *(uint16_t *)(a + 0x0E) = id; *(uint32_t *)(a + 0x10) = dsize; *(uint16_t *)(a + 0x14) = 0x18; a[0x16] = 1; uint8_t *fn = a + 0x18; *(uint32_t *)(fn + 0x00) = 5; *(uint16_t *)(fn + 0x04) = 5; *(uint32_t *)(fn + 0x28) = fa; fn[0x40] = len; fn[0x41] = 0; for (int i = 0; i < len; i++) { *(uint16_t *)(fn + 0x42 + i * 2) = (uint16_t)(uint8_t)name[i]; } *off += asize; } static void add_nonresident_attr(uint8_t *rec, uint32_t *off, uint32_t type, uint64_t svcn, uint64_t evcn, uint64_t alloc_size, uint64_t data_size, const uint8_t *runlist, size_t runlist_len, uint16_t id) { uint8_t *a = rec + *off; uint32_t asize = (0x40 + runlist_len + 7) & ~7; memset(a, 0, asize); *(uint32_t *)(a + 0x00) = type; *(uint32_t *)(a + 0x04) = asize; a[0x08] = 1; *(uint16_t *)(a + 0x0A) = 0x40; *(uint16_t *)(a + 0x0E) = id; *(uint64_t *)(a + 0x10) = svcn; *(uint64_t *)(a + 0x18) = evcn; *(uint16_t *)(a + 0x20) = 0x40; *(uint64_t *)(a + 0x28) = alloc_size; *(uint64_t *)(a + 0x30) = data_size; *(uint64_t *)(a + 0x38) = data_size; memcpy(a + 0x40, runlist, runlist_len); *off += asize; } static void add_vol_info(uint8_t *rec, uint32_t *off, uint16_t id) { uint8_t *a = rec + *off; uint32_t asize = 0x28; memset(a, 0, asize); *(uint32_t *)(a + 0x00) = ATTR_VOL_INFO; *(uint32_t *)(a + 0x04) = asize; *(uint16_t *)(a + 0x0A) = 0x18; *(uint16_t *)(a + 0x0E) = id; *(uint32_t *)(a + 0x10) = 12; *(uint16_t *)(a + 0x14) = 0x18; a[0x20] = 1; /* major_ver = 1 */ a[0x21] = 2; /* minor_ver = 2 */ *off += asize; } static void add_attr_def(uint8_t *rec, uint32_t *off, uint16_t id) { uint8_t *a = rec + *off; uint32_t dsize = 320; /* 2 entries * 160 bytes */ uint32_t asize = 0x18 + dsize; memset(a, 0, asize); *(uint32_t *)(a + 0x00) = ATTR_DATA; *(uint32_t *)(a + 0x04) = asize; *(uint16_t *)(a + 0x0A) = 0x18; *(uint16_t *)(a + 0x0E) = id; *(uint32_t *)(a + 0x10) = dsize; *(uint16_t *)(a + 0x14) = 0x18; /* Entry 0: ATTR_STD (0x10) */ uint8_t *e0 = a + 0x18; *(uint32_t *)(e0 + 0x80) = ATTR_STD; /* Entry 1: ATTR_EA (0xE0), max_sz = 64KiB */ uint8_t *e1 = a + 0x18 + 160; *(uint32_t *)(e1 + 0x80) = ATTR_EA; *(uint64_t *)(e1 + 0x98) = 0x10000ULL; *off += asize; } static void add_empty_resident_data(uint8_t *rec, uint32_t *off, uint16_t id) { uint8_t *a = rec + *off; uint32_t asize = 0x18; memset(a, 0, asize); *(uint32_t *)(a + 0x00) = ATTR_DATA; *(uint32_t *)(a + 0x04) = asize; *(uint16_t *)(a + 0x0A) = 0x18; *(uint16_t *)(a + 0x0E) = id; *(uint32_t *)(a + 0x10) = 0; *(uint16_t *)(a + 0x14) = 0x18; *off += asize; } static void add_root_dir(uint8_t *rec, uint32_t *off, uint16_t id) { uint8_t *a = rec + *off; uint32_t asize = 0x50; memset(a, 0, asize); *(uint32_t *)(a + 0x00) = ATTR_ROOT; *(uint32_t *)(a + 0x04) = asize; a[0x09] = 4; *(uint16_t *)(a + 0x0A) = 0x18; *(uint16_t *)(a + 0x0E) = id; *(uint32_t *)(a + 0x10) = 48; *(uint16_t *)(a + 0x14) = 0x20; uint16_t *name = (uint16_t *)(a + 0x18); name[0] = '$'; name[1] = 'I'; name[2] = '3'; name[3] = '0'; uint8_t *root = a + 0x20; *(uint32_t *)(root + 0x00) = ATTR_NAME; *(uint32_t *)(root + 0x04) = 1; *(uint32_t *)(root + 0x08) = 4096; root[0x0C] = 8; *(uint32_t *)(root + 0x10) = 0x10; *(uint32_t *)(root + 0x14) = 0x20; *(uint32_t *)(root + 0x18) = 0x20; uint8_t *de = root + 0x20; *(uint16_t *)(de + 0x08) = 0x10; *(uint16_t *)(de + 0x0A) = 0; *(uint16_t *)(de + 0x0C) = 2; /* NTFS_IE_LAST */ *off += asize; } static void add_end_marker(uint8_t *rec, uint32_t *off) { *(uint32_t *)(rec + *off) = ATTR_END; *(uint32_t *)(rec + *off + 4) = 0; *off += 8; *(uint32_t *)(rec + 0x18) = *off; } static const uint8_t run_mft_data[] = { 0x11, 0x40, 0x20, 0x00 }; static const uint8_t run_mft_bmp[] = { 0x11, 0x01, 0x60, 0x00 }; static const uint8_t run_logfile[] = { 0x22, 0x00, 0x02, 0x64, 0x01, 0x00 }; static const uint8_t run_vol_bmp[] = { 0x11, 0x02, 0x61, 0x00 }; static const uint8_t run_upcase[] = { 0x12, 0x00, 0x01, 0x64, 0x00 }; static void format_ntfs_image(uint8_t *image) { memset(image, 0, IMAGE_SIZE); init_boot_sector(image); uint8_t *mft = image + CLST_MFT * CLUSTER_SIZE; for (int rno = 0; rno < 16; rno++) { uint8_t *rec = mft + rno * RECORD_SIZE; uint16_t seq = (rno == 0) ? 1 : rno; uint16_t flags = (rno == 5) ? 3 : 1; init_mft_record(rec, seq, flags, rno); uint32_t off = 0x38; uint16_t id = 0; uint32_t fa = (rno == 5) ? 0x10 : 0x06; add_std_info(rec, &off, fa, id++); switch (rno) { case 0: add_file_name(rec, &off, "$MFT", fa, id++); add_nonresident_attr(rec, &off, ATTR_DATA, 0, 63, 32768, 32768, run_mft_data, sizeof(run_mft_data), id++); add_nonresident_attr(rec, &off, ATTR_BITMAP, 0, 0, 512, 512, run_mft_bmp, sizeof(run_mft_bmp), id++); break; case 1: add_file_name(rec, &off, "$MFTMirr", fa, id++); add_empty_resident_data(rec, &off, id++); break; case 2: add_file_name(rec, &off, "$LogFile", fa, id++); add_nonresident_attr(rec, &off, ATTR_DATA, 0, 511, 262144, 262144, run_logfile, sizeof(run_logfile), id++); break; case 3: add_file_name(rec, &off, "$Volume", fa, id++); add_vol_info(rec, &off, id++); add_empty_resident_data(rec, &off, id++); break; case 4: add_file_name(rec, &off, "$AttrDef", fa, id++); add_attr_def(rec, &off, id++); break; case 5: add_file_name(rec, &off, ".", fa, id++); add_root_dir(rec, &off, id++); break; case 6: add_file_name(rec, &off, "$Bitmap", fa, id++); add_nonresident_attr(rec, &off, ATTR_DATA, 0, 1, 1024, 1024, run_vol_bmp, sizeof(run_vol_bmp), id++); break; case 8: { static const uint8_t run_empty[] = { 0x00 }; add_file_name(rec, &off, "$BadClus", fa, id++); add_nonresident_attr(rec, &off, ATTR_DATA, 0, ~0ULL, 0, 0, run_empty, sizeof(run_empty), id++); break; } case 10: add_file_name(rec, &off, "$UpCase", fa, id++); add_nonresident_attr(rec, &off, ATTR_DATA, 0, 255, 131072, 131072, run_upcase, sizeof(run_upcase), id++); break; default: add_file_name(rec, &off, "dummy", fa, id++); add_empty_resident_data(rec, &off, id++); break; } add_end_marker(rec, &off); } uint8_t *mft_bmp = image + CLST_MFT_BMP * CLUSTER_SIZE; mft_bmp[0] = 0xFF; mft_bmp[1] = 0xFF; uint8_t *vol_bmp = image + CLST_VOL_BMP * CLUSTER_SIZE; memset(vol_bmp, 0xFF, 128); uint16_t *upcase = (uint16_t *)(image + CLST_UPCASE * CLUSTER_SIZE); for (int i = 0; i < 65536; i++) { upcase[i] = (uint16_t)i; } uint8_t *logfile = image + CLST_LOGFILE * CLUSTER_SIZE; memset(logfile, 0xFF, 512 * CLUSTER_SIZE); memcpy(image + 8191 * CLUSTER_SIZE, image, SECTOR_SIZE); } static volatile int g_stop = 0; static char *g_write_buf = NULL; static void sig_handler(int sig, siginfo_t *info, void *ucontext) { (void)sig; (void)info; (void)ucontext; if (g_write_buf) { syscall(SYS_mprotect, g_write_buf, 4096, PROT_READ | PROT_WRITE); } } static void *racer_thread(void *arg) { (void)arg; int ncpus = sysconf(_SC_NPROCESSORS_ONLN); if (ncpus > 1) { cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(1 % ncpus, &cpuset); sched_setaffinity(0, sizeof(cpuset), &cpuset); } while (!g_stop) { syscall(SYS_mprotect, g_write_buf, 4096, PROT_NONE); syscall(SYS_mprotect, g_write_buf, 4096, PROT_READ | PROT_WRITE); } return NULL; } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[+] Starting reproduction candidate for iomap_release_folio WARNING...\n"); uid_t uid = getuid(); printf("[+] Current UID: %u\n", (unsigned int)uid); if (uid != 0) { printf("[-] Root privileges required.\n"); exit(1); } int res = unshare(CLONE_NEWNS); if (res < 0) { printf("[-] Failed to unshare(CLONE_NEWNS): %s\n", strerror(errno)); exit(1); } printf("[+] unshare(CLONE_NEWNS) successful.\n"); res = mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL); if (res < 0) { printf("[-] Failed to remount / as MS_PRIVATE: %s\n", strerror(errno)); exit(1); } printf("[+] Remounted / as MS_PRIVATE successfully.\n"); uint8_t *image = malloc(IMAGE_SIZE); if (!image) { printf("[-] Failed to allocate %u bytes for image: %s\n", IMAGE_SIZE, strerror(errno)); exit(1); } printf("[+] Formatting 4MB NTFS filesystem in memory (512B cluster size)...\n"); format_ntfs_image(image); printf("[+] NTFS image formatted successfully.\n"); int mem_fd = memfd_create("ntfs_img", MFD_CLOEXEC); if (mem_fd < 0) { printf("[-] Failed to create memfd: %s\n", strerror(errno)); exit(1); } printf("[+] Created memfd successfully.\n"); ssize_t written = 0; while (written < IMAGE_SIZE) { ssize_t nw = write(mem_fd, image + written, IMAGE_SIZE - written); if (nw < 0) { printf("[-] Failed to write image to memfd: %s\n", strerror(errno)); exit(1); } written += nw; } free(image); printf("[+] Wrote %zd bytes to memfd.\n", written); int lctl = open("/dev/loop-control", O_RDWR); if (lctl < 0) { printf("[-] Failed to open /dev/loop-control: %s\n", strerror(errno)); exit(1); } int loop_num = ioctl(lctl, LOOP_CTL_GET_FREE); if (loop_num < 0) { printf("[-] LOOP_CTL_GET_FREE failed: %s\n", strerror(errno)); exit(1); } close(lctl); printf("[+] Obtained loop device index: %d\n", loop_num); char loop_dev[64]; snprintf(loop_dev, sizeof(loop_dev), "/dev/loop%d", loop_num); 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, mem_fd); if (res < 0) { printf("[-] Failed to LOOP_SET_FD: %s\n", strerror(errno)); exit(1); } printf("[+] Attached memfd to %s.\n", loop_dev); /* Close loop_fd and mem_fd immediately so bdev->bd_writers drops to 0. */ close(loop_fd); close(mem_fd); printf("[+] Closed loop_fd and mem_fd to relinquish write access before mount.\n"); char mnt_dir[] = "/tmp/mnt_ntfs3_XXXXXX"; if (!mkdtemp(mnt_dir)) { printf("[-] mkdtemp failed: %s\n", strerror(errno)); exit(1); } printf("[+] Created mount directory %s.\n", mnt_dir); res = mount(loop_dev, mnt_dir, "ntfs3", 0, NULL); if (res < 0) { printf("[-] Failed to mount %s on %s with ntfs3: %s\n", loop_dev, mnt_dir, strerror(errno)); exit(1); } printf("[+] Successfully mounted ntfs3 filesystem on %s.\n", mnt_dir); g_write_buf = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (g_write_buf == MAP_FAILED) { printf("[-] Failed to mmap g_write_buf: %s\n", strerror(errno)); exit(1); } memset(g_write_buf, 'A', 4096); printf("[+] Allocated and initialized g_write_buf at %p.\n", g_write_buf); struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_sigaction = sig_handler; sa.sa_flags = SA_SIGINFO; if (sigaction(SIGSEGV, &sa, NULL) < 0) { printf("[-] Failed to set sigaction(SIGSEGV): %s\n", strerror(errno)); exit(1); } if (sigaction(SIGBUS, &sa, NULL) < 0) { printf("[-] Failed to set sigaction(SIGBUS): %s\n", strerror(errno)); exit(1); } printf("[+] Signal handlers installed successfully.\n"); char file_path[PATH_MAX]; snprintf(file_path, sizeof(file_path), "%s/test.bin", mnt_dir); int fd = open(file_path, O_RDWR | O_CREAT | O_TRUNC, 0666); if (fd < 0) { printf("[-] Failed to open %s: %s\n", file_path, strerror(errno)); exit(1); } printf("[+] Opened test file %s successfully.\n", file_path); int ncpus = sysconf(_SC_NPROCESSORS_ONLN); if (ncpus > 1) { cpu_set_t cpuset; CPU_ZERO(&cpuset); CPU_SET(0, &cpuset); if (sched_setaffinity(0, sizeof(cpuset), &cpuset) < 0) { printf("[-] sched_setaffinity failed: %s\n", strerror(errno)); } else { printf("[+] Pinned main thread to CPU 0.\n"); } } pthread_t th; res = pthread_create(&th, NULL, racer_thread, NULL); if (res != 0) { printf("[-] Failed to create racer thread: %s\n", strerror(res)); exit(1); } printf("[+] Spawned racer thread successfully.\n"); char read_buf[4096]; int race_faults = 0; int write_successes = 0; int max_iters = 10000; printf("[+] Starting race loop (%d iterations or 5 seconds max)...\n", max_iters); TIMER_START(timer); int iter = 0; while (TIMER_NOT_EXPIRED(timer, 5.0) && iter < max_iters) { /* Step 1: Extend file past EOF without setting ni->i_valid */ res = ftruncate(fd, 8192); if (res < 0) { printf("[-] Failed to ftruncate(8192) at iter %d: %s\n", iter, strerror(errno)); exit(1); } /* Step 2: Issue sub-folio buffered write racing with PROT_NONE */ ssize_t pw = pwrite(fd, g_write_buf, 512, 0); if (pw < 0) { if (errno != EFAULT) { printf("[-] Unexpected pwrite error at iter %d: %s\n", iter, strerror(errno)); exit(1); } race_faults++; } else { write_successes++; } /* Step 3: Issue read; hits ntfs_read_folio's (ni->i_valid <= vbo) branch */ ssize_t pr = pread(fd, read_buf, 4096, 0); if (pr < 0) { printf("[-] Failed to pread at iter %d: %s\n", iter, strerror(errno)); exit(1); } /* Step 4: Release/invalidate folio to trigger ifs_free() -> WARN_ON_ONCE */ res = posix_fadvise(fd, 0, 4096, POSIX_FADV_DONTNEED); if (res != 0) { printf("[-] Failed to posix_fadvise at iter %d: %s\n", iter, strerror(res)); exit(1); } res = ftruncate(fd, 0); if (res < 0) { printf("[-] Failed to ftruncate(0) at iter %d: %s\n", iter, strerror(errno)); exit(1); } iter++; if (iter % 2000 == 0) { printf("[+] Completed %d iterations (faults: %d, writes: %d)...\n", iter, race_faults, write_successes); } } printf("[+] Race loop finished: %d iterations completed (race_faults: %d, write_successes: %d).\n", iter, race_faults, write_successes); g_stop = 1; res = pthread_join(th, NULL); if (res != 0) { printf("[-] pthread_join failed: %s\n", strerror(res)); } else { printf("[+] Joined racer thread successfully.\n"); } close(fd); unlink(file_path); int drop_fd = open("/proc/sys/vm/drop_caches", O_WRONLY); if (drop_fd >= 0) { if (write(drop_fd, "3\n", 2) < 0) { printf("[-] write to /proc/sys/vm/drop_caches failed: %s\n", strerror(errno)); } else { printf("[+] Successfully flushed caches via /proc/sys/vm/drop_caches.\n"); } close(drop_fd); } sync(); sleep(1); res = umount(mnt_dir); if (res < 0) { printf("[-] umount %s failed: %s\n", mnt_dir, strerror(errno)); } else { printf("[+] Successfully unmounted %s.\n", mnt_dir); } rmdir(mnt_dir); loop_fd = open(loop_dev, O_RDWR); if (loop_fd >= 0) { res = ioctl(loop_fd, LOOP_CLR_FD, 0); if (res < 0) { printf("[-] LOOP_CLR_FD failed: %s\n", strerror(errno)); } else { printf("[+] Cleared loop device %s.\n", loop_dev); } close(loop_fd); } printf("[+] Reproduction procedure finished successfully.\n"); return 0; }