// https://syzkaller.appspot.com/bug?id=5e0091d9bc564bf3177c0f92e8ca0977018a38ca #define _GNU_SOURCE // 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 // 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; } // --- 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. This allows you to // pause a thread accessing that memory until you handle the fault, creating a reliable // and controllable race window. // 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 // } #include #include #include #define PIPEFS_DIR "/tmp/pipefs" #define NUM_NFS_WORKERS 4 static int stop_racing = 0; struct nfs_worker_args { int id; int cpu; }; static void *pipefs_worker(void *arg) { (void)arg; PIN_TO_CPU(0); while (!__atomic_load_n(&stop_racing, __ATOMIC_ACQUIRE)) { int res = mount(NULL, PIPEFS_DIR, "rpc_pipefs", 0, NULL); if (res == 0) { umount2(PIPEFS_DIR, MNT_DETACH); } } return NULL; } static void *nfs_worker(void *arg) { struct nfs_worker_args *wargs = (struct nfs_worker_args *)arg; PIN_TO_CPU(wargs->cpu); char target[64]; snprintf(target, sizeof(target), "/tmp/nfs%d", wargs->id); unsigned int iter = 0; while (!__atomic_load_n(&stop_racing, __ATOMIC_ACQUIRE)) { char src[64]; char opts[128]; unsigned int ip_last = (iter % 250) + 1; snprintf(src, sizeof(src), "127.0.%d.%u:/", wargs->id, ip_last); snprintf(opts, sizeof(opts), "vers=4,addr=127.0.%d.%u,clientaddr=127.0.0.1,timeo=1,retrans=1", wargs->id, ip_last); int res = mount(src, target, "nfs", 0, opts); if (res == 0) { umount2(target, MNT_DETACH); } iter++; } return NULL; } int main(void) { SETUP_UNBUFFERED_IO(); printf("[*] Starting reproducer for KASAN slab-use-after-free in rpc_create_client_dir...\n"); /* Step 1: Create isolated network and mount namespaces */ int res = unshare(CLONE_NEWNS | CLONE_NEWNET); if (res < 0) { printf("[-] Failed to unshare(CLONE_NEWNS | CLONE_NEWNET): %s\n", strerror(errno)); exit(1); } printf("[+] unshare(CLONE_NEWNS | CLONE_NEWNET) successful.\n"); /* Step 2: Create directory for rpc_pipefs mount */ res = mkdir(PIPEFS_DIR, 0777); if (res < 0 && errno != EEXIST) { printf("[-] Failed to mkdir %s: %s\n", PIPEFS_DIR, strerror(errno)); exit(1); } printf("[+] mkdir %s successful.\n", PIPEFS_DIR); /* Step 3: Create directories for NFS worker mounts */ for (int i = 0; i < NUM_NFS_WORKERS; i++) { char target[64]; snprintf(target, sizeof(target), "/tmp/nfs%d", i); res = mkdir(target, 0777); if (res < 0 && errno != EEXIST) { printf("[-] Failed to mkdir %s: %s\n", target, strerror(errno)); exit(1); } printf("[+] mkdir %s successful.\n", target); } /* Step 4: Verify initial mount and umount of rpc_pipefs */ res = mount(NULL, PIPEFS_DIR, "rpc_pipefs", 0, NULL); if (res < 0) { printf("[-] Failed to verify mount %s: %s\n", PIPEFS_DIR, strerror(errno)); exit(1); } printf("[+] Verify mount %s successful.\n", PIPEFS_DIR); res = umount2(PIPEFS_DIR, MNT_DETACH); if (res < 0) { printf("[-] Failed to verify umount %s: %s\n", PIPEFS_DIR, strerror(errno)); exit(1); } printf("[+] Verify umount %s successful.\n", PIPEFS_DIR); /* Step 5: Detect available online CPUs */ long ncpus = sysconf(_SC_NPROCESSORS_ONLN); if (ncpus < 1) { printf("[-] Failed to get online CPUs: %s\n", strerror(errno)); exit(1); } printf("[+] Online CPUs detected: %ld\n", ncpus); /* Step 6: Start PipeFS mounting worker thread */ pthread_t pipefs_th; res = pthread_create(&pipefs_th, NULL, pipefs_worker, NULL); if (res != 0) { printf("[-] Failed to pthread_create pipefs_worker: %s\n", strerror(res)); exit(1); } printf("[+] Started pipefs_worker thread (pinned to CPU 0).\n"); /* Step 7: Start NFS client creation worker threads */ pthread_t nfs_th[NUM_NFS_WORKERS]; struct nfs_worker_args wargs[NUM_NFS_WORKERS]; for (int i = 0; i < NUM_NFS_WORKERS; i++) { wargs[i].id = i; wargs[i].cpu = (int)((i + 1) % ncpus); res = pthread_create(&nfs_th[i], NULL, nfs_worker, &wargs[i]); if (res != 0) { printf("[-] Failed to pthread_create nfs_worker %d: %s\n", i, strerror(res)); exit(1); } printf("[+] Started nfs_worker thread %d (pinned to CPU %d).\n", i, wargs[i].cpu); } /* Step 8: Race execution loop */ printf("[*] Racing PipeFS mount against NFS client creation for 25 seconds...\n"); TIMER_START(start); int elapsed = 0; while (TIMER_NOT_EXPIRED(start, 25.0)) { sleep(1); elapsed++; if (elapsed % 5 == 0) { printf("[*] Race in progress: %d/25 seconds...\n", elapsed); } } printf("[+] Race duration completed.\n"); /* Step 9: Signal stop and join threads */ __atomic_store_n(&stop_racing, 1, __ATOMIC_RELEASE); res = pthread_join(pipefs_th, NULL); if (res != 0) { printf("[-] Failed to join pipefs_worker: %s\n", strerror(res)); exit(1); } printf("[+] Joined pipefs_worker thread.\n"); for (int i = 0; i < NUM_NFS_WORKERS; i++) { res = pthread_join(nfs_th[i], NULL); if (res != 0) { printf("[-] Failed to join nfs_worker %d: %s\n", i, strerror(res)); exit(1); } printf("[+] Joined nfs_worker thread %d.\n", i); } /* Step 10: Cleanup directories */ res = rmdir(PIPEFS_DIR); if (res < 0) { printf("[-] Failed to rmdir %s: %s\n", PIPEFS_DIR, strerror(errno)); exit(1); } printf("[+] rmdir %s successful.\n", PIPEFS_DIR); for (int i = 0; i < NUM_NFS_WORKERS; i++) { char target[64]; snprintf(target, sizeof(target), "/tmp/nfs%d", i); res = rmdir(target); if (res < 0) { printf("[-] Failed to rmdir %s: %s\n", target, strerror(errno)); exit(1); } printf("[+] rmdir %s successful.\n", target); } printf("[+] Reproducer finished without crash.\n"); return 0; }