// https://syzkaller.appspot.com/bug?id=24c17416425edf118be6e3f39d34be912e037b14 // 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 #include #include #include #include #include #include #include #include #ifndef BLKBSZSET #define BLKBSZSET _IOW(0x12, 113, size_t) #endif int loop_fd = -1; int dummy_fd = -1; volatile int stop = 0; void *thread1(void *arg) { PIN_TO_CPU(0); int bsz1 = 1024; int bsz2 = 4096; while (!__atomic_load_n(&stop, __ATOMIC_RELAXED)) { // Alternating block sizes forces kill_bdev() -> page_cache_delete_batch() ioctl(loop_fd, BLKBSZSET, &bsz1); ioctl(loop_fd, BLKBSZSET, &bsz2); } return NULL; } void *thread2(void *arg) { PIN_TO_CPU(1); char buf[4096] = {0}; while (!__atomic_load_n(&stop, __ATOMIC_RELAXED)) { // Read 14 pages to fill the LRU batch (but not drain it) for (int i = 0; i < 14; i++) { ssize_t res = pread(loop_fd, buf, 4096, i * 4096); if (res < 0) { // Ignore errors here as loop device might be changing block size continue; } } // Spin a bit to widen the race window. This gives Thread 1 time to // call kill_bdev and set folio->mapping = NULL for the pages in our batch. for (volatile int j = 0; j < 10000; j++) {} // Drain the LRU batch on CPU 1 posix_fadvise(dummy_fd, 0, 4096, POSIX_FADV_DONTNEED); } return NULL; } int main() { SETUP_UNBUFFERED_IO(); printf("[*] Setting up reproducer...\n"); // Create a backing file for the loop device int fd = open("img", O_RDWR | O_CREAT | O_TRUNC, 0644); if (fd < 0) { printf("[-] Failed to open img: %s\n", strerror(errno)); exit(1); } printf("[+] open img successful.\n"); int res = ftruncate(fd, 1024 * 1024); if (res < 0) { printf("[-] Failed to ftruncate img: %s\n", strerror(errno)); exit(1); } printf("[+] ftruncate img successful.\n"); // Dummy fd used solely to trigger fadvise -> lru_add_drain dummy_fd = open("dummy", O_RDWR | O_CREAT | O_TRUNC, 0644); if (dummy_fd < 0) { printf("[-] Failed to open dummy: %s\n", strerror(errno)); exit(1); } printf("[+] open dummy successful.\n"); res = ftruncate(dummy_fd, 4096); if (res < 0) { printf("[-] Failed to ftruncate dummy: %s\n", strerror(errno)); exit(1); } printf("[+] ftruncate dummy successful.\n"); // Setup loop device int loop_ctl = open("/dev/loop-control", O_RDWR); if (loop_ctl < 0) { printf("[-] Failed to open /dev/loop-control: %s\n", strerror(errno)); exit(1); } printf("[+] open /dev/loop-control successful.\n"); int loop_num = ioctl(loop_ctl, LOOP_CTL_GET_FREE); if (loop_num < 0) { printf("[-] Failed to LOOP_CTL_GET_FREE: %s\n", strerror(errno)); exit(1); } printf("[+] LOOP_CTL_GET_FREE successful, loop_num=%d.\n", loop_num); char loop_name[64]; sprintf(loop_name, "/dev/loop%d", loop_num); 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); res = ioctl(loop_fd, LOOP_SET_FD, fd); if (res < 0) { printf("[-] Failed to LOOP_SET_FD: %s\n", strerror(errno)); exit(1); } printf("[+] LOOP_SET_FD successful.\n"); printf("[*] Starting race threads...\n"); pthread_t t1, t2; res = pthread_create(&t1, NULL, thread1, NULL); if (res != 0) { printf("[-] Failed to pthread_create t1: %s\n", strerror(res)); exit(1); } res = pthread_create(&t2, NULL, thread2, NULL); if (res != 0) { printf("[-] Failed to pthread_create t2: %s\n", strerror(res)); exit(1); } printf("[+] Threads started successfully.\n"); // Run the race for 5 seconds TIMER_START(start); while (TIMER_NOT_EXPIRED(start, 5.0)) { usleep(100000); } printf("[*] Stopping race threads...\n"); __atomic_store_n(&stop, 1, __ATOMIC_RELAXED); pthread_join(t1, NULL); pthread_join(t2, NULL); printf("[+] Threads stopped successfully.\n"); // Cleanup res = ioctl(loop_fd, LOOP_CLR_FD, 0); if (res < 0) { printf("[-] Failed to LOOP_CLR_FD: %s\n", strerror(errno)); } else { printf("[+] LOOP_CLR_FD successful.\n"); } close(loop_fd); close(loop_ctl); close(dummy_fd); close(fd); unlink("img"); unlink("dummy"); printf("[+] Cleanup successful.\n"); return 0; }