// https://syzkaller.appspot.com/bug?id=00e372ceca7e1ab06b8a7997fb9d72072d33e5cb #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 __NR_io_uring_setup #define __NR_io_uring_setup 425 #endif #ifndef __NR_io_uring_enter #define __NR_io_uring_enter 426 #endif int fd; int ptmx; void *thread_ioctl(void *arg) { PIN_TO_CPU(0); struct termios t; if (ioctl(fd, TCGETS, &t) < 0) { printf("[-] Failed to ioctl TCGETS: %s\n", strerror(errno)); exit(1); } TIMER_START(start); while (TIMER_NOT_EXPIRED(start, 5.0)) { t.c_lflag ^= ICANON; if (ioctl(fd, TCSETS, &t) < 0) { if (errno != EINTR && errno != EAGAIN) { printf("[-] Failed to ioctl TCSETS: %s\n", strerror(errno)); exit(1); } } } return NULL; } void *thread_poll(void *arg) { PIN_TO_CPU(1); struct pollfd pfd = { .fd = fd, .events = POLLIN }; TIMER_START(start); while (TIMER_NOT_EXPIRED(start, 5.0)) { if (poll(&pfd, 1, 0) < 0) { if (errno != EINTR && errno != EAGAIN) { printf("[-] Failed to poll: %s\n", strerror(errno)); exit(1); } } } return NULL; } void *thread_io_uring(void *arg) { PIN_TO_CPU(2); struct io_uring_params p; memset(&p, 0, sizeof(p)); int ring_fd = syscall(__NR_io_uring_setup, 2, &p); if (ring_fd < 0) { if (errno == ENOSYS) { printf("[!] io_uring_setup not supported, skipping io_uring thread.\n"); return NULL; } printf("[-] Failed to io_uring_setup: %s\n", strerror(errno)); exit(1); } int sq_ring_sz = p.sq_off.array + p.sq_entries * sizeof(unsigned); int cq_ring_sz = p.cq_off.cqes + p.cq_entries * sizeof(struct io_uring_cqe); if (p.features & IORING_FEAT_SINGLE_MMAP) { if (cq_ring_sz > sq_ring_sz) sq_ring_sz = cq_ring_sz; cq_ring_sz = sq_ring_sz; } char *sq_ptr = mmap(0, sq_ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, ring_fd, IORING_OFF_SQ_RING); if (sq_ptr == MAP_FAILED) { printf("[-] Failed to mmap sq_ring: %s\n", strerror(errno)); exit(1); } char *cq_ptr = (p.features & IORING_FEAT_SINGLE_MMAP) ? sq_ptr : mmap(0, cq_ring_sz, PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, ring_fd, IORING_OFF_CQ_RING); if (cq_ptr == MAP_FAILED) { printf("[-] Failed to mmap cq_ring: %s\n", strerror(errno)); exit(1); } struct io_uring_sqe *sqes = mmap(0, p.sq_entries * sizeof(struct io_uring_sqe), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_POPULATE, ring_fd, IORING_OFF_SQES); if (sqes == MAP_FAILED) { printf("[-] Failed to mmap sqes: %s\n", strerror(errno)); exit(1); } unsigned *sq_tail = (unsigned *)(sq_ptr + p.sq_off.tail); unsigned *sq_mask = (unsigned *)(sq_ptr + p.sq_off.ring_mask); unsigned *sq_array = (unsigned *)(sq_ptr + p.sq_off.array); unsigned *cq_head = (unsigned *)(cq_ptr + p.cq_off.head); unsigned *cq_tail = (unsigned *)(cq_ptr + p.cq_off.tail); TIMER_START(start); while (TIMER_NOT_EXPIRED(start, 5.0)) { unsigned tail = __atomic_load_n(sq_tail, __ATOMIC_ACQUIRE); unsigned index = tail & *sq_mask; struct io_uring_sqe *sqe = &sqes[index]; memset(sqe, 0, sizeof(*sqe)); sqe->opcode = IORING_OP_POLL_ADD; sqe->fd = fd; sqe->poll32_events = POLLIN; sq_array[index] = index; __atomic_store_n(sq_tail, tail + 1, __ATOMIC_RELEASE); if (syscall(__NR_io_uring_enter, ring_fd, 1, 1, IORING_ENTER_GETEVENTS, NULL, 0) < 0) { if (errno != EINTR && errno != EAGAIN && errno != EBUSY) { printf("[-] Failed to io_uring_enter: %s\n", strerror(errno)); exit(1); } } unsigned ctail = __atomic_load_n(cq_tail, __ATOMIC_ACQUIRE); unsigned chead = __atomic_load_n(cq_head, __ATOMIC_ACQUIRE); if (chead != ctail) { __atomic_store_n(cq_head, chead + 1, __ATOMIC_RELEASE); } } return NULL; } void *thread_write(void *arg) { TIMER_START(start); while (TIMER_NOT_EXPIRED(start, 5.0)) { if (write(ptmx, "a", 1) < 0) { if (errno != EAGAIN && errno != EINTR) { printf("[-] Failed to write to ptmx: %s\n", strerror(errno)); exit(1); } } usleep(1000); } return NULL; } int main(void) { SETUP_UNBUFFERED_IO(); ptmx = open("/dev/ptmx", O_RDWR | O_NOCTTY | O_NONBLOCK); if (ptmx < 0) { printf("[-] Failed to open ptmx: %s\n", strerror(errno)); exit(1); } printf("[+] Opened ptmx successfully.\n"); if (grantpt(ptmx) < 0) { printf("[-] Failed to grantpt: %s\n", strerror(errno)); exit(1); } printf("[+] grantpt successful.\n"); if (unlockpt(ptmx) < 0) { printf("[-] Failed to unlockpt: %s\n", strerror(errno)); exit(1); } printf("[+] unlockpt successful.\n"); char *pts = ptsname(ptmx); if (!pts) { printf("[-] Failed to ptsname: %s\n", strerror(errno)); exit(1); } printf("[+] ptsname successful.\n"); fd = open(pts, O_RDWR | O_NOCTTY); if (fd < 0) { printf("[-] Failed to open pts: %s\n", strerror(errno)); exit(1); } printf("[+] Opened pts successfully.\n"); pthread_t t1, t2, t3, t4; if (pthread_create(&t1, NULL, thread_ioctl, NULL) != 0) { printf("[-] Failed to create thread_ioctl: %s\n", strerror(errno)); exit(1); } if (pthread_create(&t2, NULL, thread_poll, NULL) != 0) { printf("[-] Failed to create thread_poll: %s\n", strerror(errno)); exit(1); } if (pthread_create(&t3, NULL, thread_io_uring, NULL) != 0) { printf("[-] Failed to create thread_io_uring: %s\n", strerror(errno)); exit(1); } if (pthread_create(&t4, NULL, thread_write, NULL) != 0) { printf("[-] Failed to create thread_write: %s\n", strerror(errno)); exit(1); } printf("[+] Threads created successfully.\n"); pthread_join(t1, NULL); pthread_join(t2, NULL); pthread_join(t3, NULL); pthread_join(t4, NULL); printf("[+] Reproducer finished.\n"); return 0; }