// https://syzkaller.appspot.com/bug?id=1bff1ed4d15dc4110fdd6aae26b0c0e70957e559 // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static void kill_and_wait(int pid, int* status) { kill(pid, SIGKILL); while (waitpid(-1, status, 0) != pid) { } } static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void thread_start(void* (*fn)(void*), void* arg) { pthread_t th; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 128 << 10); int i = 0; for (; i < 100; i++) { if (pthread_create(&th, &attr, fn, arg) == 0) { pthread_attr_destroy(&attr); return; } if (errno == EAGAIN) { usleep(50); continue; } break; } exit(1); } typedef struct { pthread_mutex_t mu; pthread_cond_t cv; int state; } event_t; static void event_init(event_t* ev) { if (pthread_mutex_init(&ev->mu, 0)) exit(1); if (pthread_cond_init(&ev->cv, 0)) exit(1); ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { pthread_mutex_lock(&ev->mu); if (ev->state) exit(1); ev->state = 1; pthread_mutex_unlock(&ev->mu); pthread_cond_broadcast(&ev->cv); } static void event_wait(event_t* ev) { pthread_mutex_lock(&ev->mu); while (!ev->state) pthread_cond_wait(&ev->cv, &ev->mu); pthread_mutex_unlock(&ev->mu); } static int event_isset(event_t* ev) { pthread_mutex_lock(&ev->mu); int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; pthread_mutex_lock(&ev->mu); for (;;) { if (ev->state) break; uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; pthread_cond_timedwait(&ev->cv, &ev->mu, &ts); now = current_time_ms(); if (now - start > timeout) break; } int res = ev->state; pthread_mutex_unlock(&ev->mu); return res; } struct thread_t { int created, call; event_t ready, done; }; static struct thread_t threads[16]; static void execute_call(int call); static int running; static void* thr(void* arg) { struct thread_t* th = (struct thread_t*)arg; for (;;) { event_wait(&th->ready); event_reset(&th->ready); execute_call(th->call); __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED); event_set(&th->done); } return 0; } static void execute_one(void) { if (write(1, "executing program\n", sizeof("executing program\n") - 1)) { } int i, call, thread; for (call = 0; call < 8; call++) { for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0])); thread++) { struct thread_t* th = &threads[thread]; if (!th->created) { th->created = 1; event_init(&th->ready); event_init(&th->done); event_set(&th->done); thread_start(thr, th); } if (!event_isset(&th->done)) continue; event_reset(&th->done); th->call = call; __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED); event_set(&th->ready); if (call == 1) break; event_timedwait(&th->done, 50); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter = 0; for (;; iter++) { int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { sleep_ms(10); if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; if (current_time_ms() - start < 5000) continue; kill_and_wait(pid, &status); break; } } } uint64_t r[1] = {0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: // openat$netmap arguments: [ // fd: const = 0xffffff9c (8 bytes) // file: ptr[in, buffer] { // buffer: {2f 64 65 76 2f 6e 65 74 6d 61 70 00} (length 0xc) // } // flags: open_flags = 0x80000 (8 bytes) // mode: const = 0x0 (8 bytes) // ] // returns fd_netmap memcpy((void*)0x2000000000c0, "/dev/netmap\000", 12); res = syscall(SYS_openat, /*fd=*/0xffffff9cul, /*file=*/0x2000000000c0ul, /*flags=O_TTY_INIT*/ 0x80000ul, /*mode=*/0ul); if (res != -1) r[0] = res; break; case 1: // ioctl$NIOCREGIF arguments: [ // fd: fd_netmap (resource) // cmd: const = 0xc03c6992 (8 bytes) // arg: ptr[inout, nmreq] { // nmreq { // nr_name: buffer: {22 83 92 e9 00 00 00 00 81 00 00 00 00 00 f0 00} // (length 0x10) nr_version: int32 = 0x7fffffff (4 bytes) nr_offset: // int32 = 0x5 (4 bytes) nr_memsize: int32 = 0x6 (4 bytes) // nr_tx_slots: int32 = 0x1 (4 bytes) // nr_rx_slots: int32 = 0x8 (4 bytes) // nr_tx_rings: int16 = 0x2 (2 bytes) // nr_rx_rings: int16 = 0x16 (2 bytes) // nr_ringid: nmreq_register_ringid = 0x1fff (2 bytes) // nr_cmd: nmreq_nr_cmds = 0x0 (2 bytes) // nr_arg1: int16 = 0x9 (2 bytes) // nr_arg2: int16 = 0x1 (2 bytes) // nr_arg3: int32 = 0x3 (4 bytes) // nr_flags: nmreq_register_modes = 0x7 (4 bytes) // spare2: array[int32] { // int32 = 0x0 (4 bytes) // } // } // } // ] memcpy((void*)0x200000000100, "\x22\x83\x92\xe9\x00\x00\x00\x00\x81\x00\x00\x00\x00\x00\xf0\x00", 16); *(uint32_t*)0x200000000110 = 0x7fffffff; *(uint32_t*)0x200000000114 = 5; *(uint32_t*)0x200000000118 = 6; *(uint32_t*)0x20000000011c = 1; *(uint32_t*)0x200000000120 = 8; *(uint16_t*)0x200000000124 = 2; *(uint16_t*)0x200000000126 = 0x16; *(uint16_t*)0x200000000128 = 0x1fff; *(uint16_t*)0x20000000012a = 0; *(uint16_t*)0x20000000012c = 9; *(uint16_t*)0x20000000012e = 1; *(uint32_t*)0x200000000130 = 3; *(uint32_t*)0x200000000134 = 7; *(uint32_t*)0x200000000138 = 0; syscall(SYS_ioctl, /*fd=*/r[0], /*cmd=*/0xc03c6992ul, /*arg=*/0x200000000100ul); break; case 2: // select arguments: [ // n: len = 0x40 (8 bytes) // inp: ptr[inout, fd_set] { // fd_set { // mask0: int64 = 0x772 (8 bytes) // mask1: int64 = 0x7 (8 bytes) // mask2: int64 = 0x9 (8 bytes) // mask3: int64 = 0x800 (8 bytes) // mask4: int64 = 0x1e (8 bytes) // mask5: int64 = 0x5 (8 bytes) // mask6: int64 = 0x55 (8 bytes) // mask7: int64 = 0x4 (8 bytes) // } // } // outp: ptr[inout, fd_set] { // fd_set { // mask0: int64 = 0x8 (8 bytes) // mask1: int64 = 0xfffffffffffffffb (8 bytes) // mask2: int64 = 0x792 (8 bytes) // mask3: int64 = 0x8ec (8 bytes) // mask4: int64 = 0x3 (8 bytes) // mask5: int64 = 0xfffffffffffffffc (8 bytes) // mask6: int64 = 0x0 (8 bytes) // mask7: int64 = 0x1 (8 bytes) // } // } // exp: nil // tvp: nil // ] *(uint64_t*)0x200000000140 = 0x772; *(uint64_t*)0x200000000148 = 7; *(uint64_t*)0x200000000150 = 9; *(uint64_t*)0x200000000158 = 0x800; *(uint64_t*)0x200000000160 = 0x1e; *(uint64_t*)0x200000000168 = 5; *(uint64_t*)0x200000000170 = 0x55; *(uint64_t*)0x200000000178 = 4; *(uint64_t*)0x200000000180 = 8; *(uint64_t*)0x200000000188 = 0xfffffffffffffffb; *(uint64_t*)0x200000000190 = 0x792; *(uint64_t*)0x200000000198 = 0x8ec; *(uint64_t*)0x2000000001a0 = 3; *(uint64_t*)0x2000000001a8 = 0xfffffffffffffffc; *(uint64_t*)0x2000000001b0 = 0; *(uint64_t*)0x2000000001b8 = 1; syscall(SYS_select, /*n=*/0x40ul, /*inp=*/0x200000000140ul, /*outp=*/0x200000000180ul, /*exp=*/0ul, /*tvp=*/0ul); for (int i = 0; i < 32; i++) { syscall(SYS_select, /*n=*/0x40ul, /*inp=*/0x200000000140ul, /*outp=*/0x200000000180ul, /*exp=*/0ul, /*tvp=*/0ul); } break; case 3: // setsockopt$sock_int arguments: [ // fd: sock (resource) // level: const = 0xffff (4 bytes) // optname: sockopt_opt_sock_int = 0x8000 (4 bytes) // optval: nil // optlen: len = 0x0 (8 bytes) // ] syscall(SYS_setsockopt, /*fd=*/(intptr_t)-1, /*level=*/0xffff, /*optname=SO_NO_DDP*/ 0x8000, /*optval=*/0ul, /*optlen=*/0ul); break; case 4: // socketpair$unix arguments: [ // domain: const = 0x1 (8 bytes) // type: unix_socket_type = 0x5 (8 bytes) // proto: const = 0x0 (1 bytes) // fds: nil // ] syscall(SYS_socketpair, /*domain=*/1ul, /*type=SOCK_SEQPACKET*/ 5ul, /*proto=*/0, /*fds=*/0ul); for (int i = 0; i < 32; i++) { syscall(SYS_socketpair, /*domain=*/1ul, /*type=SOCK_SEQPACKET*/ 5ul, /*proto=*/0, /*fds=*/0ul); } break; case 5: // setsockopt$sock_int arguments: [ // fd: sock (resource) // level: const = 0xffff (4 bytes) // optname: sockopt_opt_sock_int = 0x1001 (4 bytes) // optval: nil // optlen: len = 0x0 (8 bytes) // ] syscall(SYS_setsockopt, /*fd=*/(intptr_t)-1, /*level=*/0xffff, /*optname=SO_SNDBUF*/ 0x1001, /*optval=*/0ul, /*optlen=*/0ul); break; case 6: // read arguments: [ // fd: fd (resource) // buf: ptr[out, buffer] { // buffer: (DirOut) // } // count: len = 0x12 (8 bytes) // ] syscall(SYS_read, /*fd=*/(intptr_t)-1, /*buf=*/0x200000000580ul, /*count=*/0x12ul); break; case 7: // msync arguments: [ // addr: VMA[0x1000] // len: len = 0x1000 (8 bytes) // f: msync_flags = 0x0 (8 bytes) // ] syscall(SYS_msync, /*addr=*/0x200000000000ul, /*len=*/0x1000ul, /*f=*/0ul); break; } } int main(void) { syscall(SYS_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x1012ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul); const char* reason; (void)reason; loop(); return 0; }