// https://syzkaller.appspot.com/bug?id=20a0f004a11f8375d28283c91ef5dd3f42228aae #define _GNU_SOURCE #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 // 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 // } #ifndef MS_SILENT #define MS_SILENT 32768 #endif char loop_name[64]; // Set up a loop device with a 1MB zeroed backing file. // This ensures that the mount attempt reads blocks and populates the bh_lrus cache, // even though the ext4 filesystem validation will subsequently fail. void setup_loop() { int ctl_fd = open("/dev/loop-control", O_RDWR); int dev_nr = -1; if (ctl_fd >= 0) { dev_nr = ioctl(ctl_fd, LOOP_CTL_GET_FREE); if (dev_nr < 0) { printf("[-] Failed to ioctl LOOP_CTL_GET_FREE: %s\n", strerror(errno)); exit(1); } printf("[+] ioctl LOOP_CTL_GET_FREE successful.\n"); if (close(ctl_fd) < 0) { printf("[-] Failed to close /dev/loop-control: %s\n", strerror(errno)); exit(1); } printf("[+] close /dev/loop-control successful.\n"); } if (dev_nr >= 0) { sprintf(loop_name, "/dev/loop%d", dev_nr); } else { strcpy(loop_name, "/dev/loop0"); } int fd = open("/tmp/backing_file", O_RDWR | O_CREAT, 0644); if (fd < 0) { printf("[-] Failed to open /tmp/backing_file: %s\n", strerror(errno)); exit(1); } printf("[+] open /tmp/backing_file successful.\n"); if (ftruncate(fd, 1024 * 1024) < 0) { printf("[-] Failed to ftruncate /tmp/backing_file: %s\n", strerror(errno)); exit(1); } printf("[+] ftruncate /tmp/backing_file successful.\n"); int loop_fd = open(loop_name, O_RDWR); if (loop_fd < 0) { printf("[-] Failed to open %s: %s\n", loop_name, strerror(errno)); exit(1); } printf("[+] open %s successful.\n", loop_name); // Ignore error on LOOP_CLR_FD just in case it's not set ioctl(loop_fd, LOOP_CLR_FD, 0); if (ioctl(loop_fd, LOOP_SET_FD, fd) < 0) { printf("[-] Failed to ioctl LOOP_SET_FD: %s\n", strerror(errno)); exit(1); } printf("[+] ioctl LOOP_SET_FD successful.\n"); if (close(loop_fd) < 0) { printf("[-] Failed to close loop_fd: %s\n", strerror(errno)); exit(1); } printf("[+] close loop_fd successful.\n"); if (close(fd) < 0) { printf("[-] Failed to close backing_file fd: %s\n", strerror(errno)); exit(1); } printf("[+] close backing_file fd successful.\n"); } // Thread 1: Pinned to CPU 0. Repeatedly attempt to mount the loop device. // The mount will read blocks (populating bh_lrus), fail validation, // and close the block device. The close triggers kill_bdev() -> invalidate_bh_lrus(), // which clears the bh_lrus cache on CPU 0. void *thread_mount(void *arg) { PIN_TO_CPU(0); if (mkdir("/tmp/mnt", 0777) < 0 && errno != EEXIST) { printf("[-] Failed to mkdir /tmp/mnt: %s\n", strerror(errno)); exit(1); } printf("[+] mkdir /tmp/mnt successful.\n"); umount("/tmp/mnt"); // Ensure it's not already mounted while (1) { // MS_SILENT suppresses console errors if (mount(loop_name, "/tmp/mnt", "ext4", MS_SILENT, NULL) == 0) { umount("/tmp/mnt"); } } return NULL; } // Thread 2: Pinned to CPU 1. Repeatedly call mbind() with MPOL_MF_MOVE to trigger // lru_cache_disable() and __lru_add_drain_all(), which iterates over all CPUs and // calls has_bh_in_lru() on remote CPUs (including CPU 0). void *thread_mbind(void *arg) { PIN_TO_CPU(1); void *p = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); if (p == MAP_FAILED) { printf("[-] Failed to mmap: %s\n", strerror(errno)); exit(1); } printf("[+] mmap successful.\n"); while (1) { // mode = 0 (MPOL_DEFAULT), maxnode = 1, flags = 2 (MPOL_MF_MOVE) syscall(__NR_mbind, p, 4096, 0, NULL, 1, 2); } return NULL; } int main() { SETUP_UNBUFFERED_IO(); setup_loop(); pthread_t t1, t2; if (pthread_create(&t1, NULL, thread_mount, NULL) != 0) { printf("[-] Failed to pthread_create t1: %s\n", strerror(errno)); exit(1); } printf("[+] pthread_create t1 successful.\n"); if (pthread_create(&t2, NULL, thread_mbind, NULL) != 0) { printf("[-] Failed to pthread_create t2: %s\n", strerror(errno)); exit(1); } printf("[+] pthread_create t2 successful.\n"); // Sleep infinitely to prevent voluntary exit, avoiding the do_notify_parent_cldstop collision. // The syzkaller executor will eventually terminate this process with SIGKILL. while (1) { sleep(1); } return 0; }