// https://syzkaller.appspot.com/bug?id=f5397141bfd2d68c1c692e5c46c2c9f7b818b9ec #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 #include #include #include #include #include #include #include #include #include #include #include #ifndef N_PPP #define N_PPP 3 #endif #ifndef N_TTY #define N_TTY 0 #endif #define NUM_CONNECT_WORKERS 2 static volatile int start_connect = 0; static volatile int start_disconnect = 0; static volatile int stop_race = 0; static volatile int terminate = 0; static volatile int g_chan_fd = -1; static volatile int g_unit = -1; static volatile int connect_ready = 0; static void* connect_worker(void* arg) { int cpu = (int)(long)arg; PIN_TO_CPU(cpu); while (!__atomic_load_n(&terminate, __ATOMIC_ACQUIRE)) { while (!__atomic_load_n(&start_connect, __ATOMIC_ACQUIRE) && !__atomic_load_n(&terminate, __ATOMIC_ACQUIRE)) { __builtin_ia32_pause(); } if (__atomic_load_n(&terminate, __ATOMIC_ACQUIRE)) break; int fd = __atomic_load_n(&g_chan_fd, __ATOMIC_ACQUIRE); int u = __atomic_load_n(&g_unit, __ATOMIC_ACQUIRE); __atomic_fetch_add(&connect_ready, 1, __ATOMIC_RELEASE); while (!__atomic_load_n(&stop_race, __ATOMIC_ACQUIRE)) { ioctl(fd, PPPIOCCONNECT, &u); } __atomic_fetch_sub(&connect_ready, 1, __ATOMIC_RELEASE); while (__atomic_load_n(&start_connect, __ATOMIC_ACQUIRE) && !__atomic_load_n(&terminate, __ATOMIC_ACQUIRE)) { __builtin_ia32_pause(); } } return NULL; } static void* disconnect_worker(void* arg) { int cpu = (int)(long)arg; PIN_TO_CPU(cpu); while (!__atomic_load_n(&terminate, __ATOMIC_ACQUIRE)) { while (!__atomic_load_n(&start_disconnect, __ATOMIC_ACQUIRE) && !__atomic_load_n(&terminate, __ATOMIC_ACQUIRE)) { __builtin_ia32_pause(); } if (__atomic_load_n(&terminate, __ATOMIC_ACQUIRE)) break; int fd = __atomic_load_n(&g_chan_fd, __ATOMIC_ACQUIRE); while (!__atomic_load_n(&stop_race, __ATOMIC_ACQUIRE) && __atomic_load_n(&start_disconnect, __ATOMIC_ACQUIRE)) { ioctl(fd, PPPIOCDISCONN, 0); } while (__atomic_load_n(&start_disconnect, __ATOMIC_ACQUIRE) && !__atomic_load_n(&terminate, __ATOMIC_ACQUIRE)) { __builtin_ia32_pause(); } } return NULL; } int main(void) { setvbuf(stdout, NULL, _IONBF, 0); printf("[*] Starting reproducer for list corruption in ppp_connect_channel...\n"); int res = unshare(CLONE_NEWUSER | CLONE_NEWNET); if (res < 0) { printf("[-] Failed to unshare(CLONE_NEWUSER | CLONE_NEWNET): %s\n", strerror(errno)); if (getuid() != 0) { printf("[-] Non-root user and unshare failed, exiting.\n"); exit(1); } printf("[*] Running as root (UID 0), continuing without unshare.\n"); } else { printf("[+] unshare(CLONE_NEWUSER | CLONE_NEWNET) successful.\n"); } PIN_TO_CPU(0); printf("[+] Main thread pinned to CPU 0.\n"); int unit_fd = open("/dev/ppp", O_RDWR); if (unit_fd < 0) { printf("[-] Failed to open /dev/ppp for unit: %s\n", strerror(errno)); exit(1); } printf("[+] open /dev/ppp for unit successful (fd=%d).\n", unit_fd); int unit = -1; res = ioctl(unit_fd, PPPIOCNEWUNIT, &unit); if (res < 0) { printf("[-] Failed to create new PPP unit (PPPIOCNEWUNIT): %s\n", strerror(errno)); exit(1); } printf("[+] ioctl(PPPIOCNEWUNIT) successful (unit=%d).\n", unit); int nprocs = sysconf(_SC_NPROCESSORS_ONLN); if (nprocs < 1) nprocs = 1; printf("[+] Detected %d online CPU cores.\n", nprocs); pthread_t th_conn[NUM_CONNECT_WORKERS]; for (int i = 0; i < NUM_CONNECT_WORKERS; i++) { int target_cpu = (i + 1) % nprocs; res = pthread_create(&th_conn[i], NULL, connect_worker, (void*)(long)target_cpu); if (res != 0) { printf("[-] Failed to create connect worker thread %d: %s\n", i, strerror(res)); exit(1); } } printf("[+] Created %d connect worker threads.\n", NUM_CONNECT_WORKERS); pthread_t th_disconn; res = pthread_create(&th_disconn, NULL, disconnect_worker, (void*)(long)(1 % nprocs)); if (res != 0) { printf("[-] Failed to create disconnect worker thread: %s\n", strerror(res)); exit(1); } printf("[+] Created disconnect worker thread.\n"); printf("[*] Commencing race iterations (up to 10000 cycles, 15s max)...\n"); TIMER_START(t_start); for (int iter = 0; iter < 10000 && TIMER_NOT_EXPIRED(t_start, 15.0); iter++) { int pty_fd = open("/dev/ptmx", O_RDWR | O_NOCTTY); if (pty_fd < 0) { printf("[-] Iteration %d: Failed to open /dev/ptmx: %s\n", iter, strerror(errno)); exit(1); } res = grantpt(pty_fd); if (res < 0) { printf("[-] Iteration %d: Failed to grantpt: %s\n", iter, strerror(errno)); exit(1); } res = unlockpt(pty_fd); if (res < 0) { printf("[-] Iteration %d: Failed to unlockpt: %s\n", iter, strerror(errno)); exit(1); } int ldisc = N_PPP; res = ioctl(pty_fd, TIOCSETD, &ldisc); if (res < 0) { printf("[-] Iteration %d: Failed to set N_PPP (TIOCSETD): %s\n", iter, strerror(errno)); exit(1); } int chindex = -1; res = ioctl(pty_fd, PPPIOCGCHAN, &chindex); if (res < 0) { printf("[-] Iteration %d: Failed to get channel index (PPPIOCGCHAN): %s\n", iter, strerror(errno)); exit(1); } int chan_fd = open("/dev/ppp", O_RDWR); if (chan_fd < 0) { printf("[-] Iteration %d: Failed to open /dev/ppp for channel: %s\n", iter, strerror(errno)); exit(1); } res = ioctl(chan_fd, PPPIOCATTCHAN, &chindex); if (res < 0) { printf("[-] Iteration %d: Failed to attach to channel (PPPIOCATTCHAN): %s\n", iter, strerror(errno)); exit(1); } res = ioctl(chan_fd, PPPIOCCONNECT, &unit); if (res < 0) { printf("[-] Iteration %d: Failed initial PPPIOCCONNECT: %s\n", iter, strerror(errno)); exit(1); } __atomic_store_n(&g_chan_fd, chan_fd, __ATOMIC_RELEASE); __atomic_store_n(&g_unit, unit, __ATOMIC_RELEASE); __atomic_store_n(&stop_race, 0, __ATOMIC_RELEASE); /* Phase A: Race PPPIOCCONNECT vs PPPIOCDISCONN */ __atomic_store_n(&start_disconnect, 1, __ATOMIC_RELEASE); __atomic_store_n(&start_connect, 1, __ATOMIC_RELEASE); while (__atomic_load_n(&connect_ready, __ATOMIC_ACQUIRE) < NUM_CONNECT_WORKERS) { __builtin_ia32_pause(); } for (volatile int spin = 0; spin < 300; spin++) { __builtin_ia32_pause(); } /* Phase B: Stop disconnect worker, re-connect channel, and race connect vs channel teardown */ __atomic_store_n(&start_disconnect, 0, __ATOMIC_RELEASE); /* Ensure channel is connected prior to teardown */ ioctl(chan_fd, PPPIOCCONNECT, &unit); if (iter % 2 == 0) { int ldisc_tty = N_TTY; ioctl(pty_fd, TIOCSETD, &ldisc_tty); } res = close(pty_fd); if (res < 0) { printf("[-] Iteration %d: Failed to close pty_fd: %s\n", iter, strerror(errno)); exit(1); } __atomic_store_n(&stop_race, 1, __ATOMIC_RELEASE); __atomic_store_n(&start_connect, 0, __ATOMIC_RELEASE); while (__atomic_load_n(&connect_ready, __ATOMIC_ACQUIRE) > 0) { __builtin_ia32_pause(); } res = close(chan_fd); if (res < 0) { printf("[-] Iteration %d: Failed to close chan_fd: %s\n", iter, strerror(errno)); exit(1); } if ((iter + 1) % 1000 == 0) { printf("[+] Completed %d race iterations...\n", iter + 1); } } printf("[*] Stopping worker threads...\n"); __atomic_store_n(&terminate, 1, __ATOMIC_RELEASE); __atomic_store_n(&start_connect, 1, __ATOMIC_RELEASE); __atomic_store_n(&start_disconnect, 1, __ATOMIC_RELEASE); for (int i = 0; i < NUM_CONNECT_WORKERS; i++) { pthread_join(th_conn[i], NULL); } pthread_join(th_disconn, NULL); res = close(unit_fd); if (res < 0) { printf("[-] Failed to close unit_fd: %s\n", strerror(errno)); exit(1); } printf("[+] close unit_fd successful.\n"); usleep(100000); printf("[+] Reproducer run completed.\n"); return 0; }