// https://syzkaller.appspot.com/bug?id=04e73e1e506402ef7dfa6bdb6cee2efe4b99b540 // 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 unsigned long long procid; 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 == 5) 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[2] = {0xffffffffffffffff, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: // open arguments: [ // file: ptr[in, buffer] { // buffer: {2e 2f 66 69 6c 65 30 00} (length 0x8) // } // flags: open_flags = 0x80000000000206 (8 bytes) // mode: open_mode = 0x0 (8 bytes) // ] // returns fd memcpy((void*)0x200000000140, "./file0\000", 8); res = syscall( SYS_open, /*file=*/0x200000000140ul, /*flags=O_NONBLOCK|O_CREAT|O_RDWR|0x80000000000000*/ 0x80000000000206ul, /*mode=*/0ul); if (res != -1) r[0] = res; break; case 1: // ftruncate arguments: [ // fd: fd (resource) // len: intptr = 0x8531 (8 bytes) // ] syscall(SYS_ftruncate, /*fd=*/(intptr_t)-1, /*len=*/0x8531ul); break; case 2: // 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 = 0x2 (8 bytes) // mode: const = 0x0 (8 bytes) // ] // returns fd_netmap memcpy((void*)0x200000000180, "/dev/netmap\000", 12); res = syscall(SYS_openat, /*fd=*/0xffffff9cul, /*file=*/0x200000000180ul, /*flags=O_RDWR*/ 2ul, /*mode=*/0ul); if (res != -1) r[1] = res; break; case 3: // ioctl$NIOCREGIF arguments: [ // fd: fd_netmap (resource) // cmd: const = 0xc03c6992 (8 bytes) // arg: ptr[inout, nmreq] { // nmreq { // nr_name: buffer: {00 00 04 00 08 00 00 00 50 f0 00 00 00 00 00 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 = 0x9fff (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 = 0x7ffb (4 bytes) // nr_flags: nmreq_register_modes = 0xfff7 (4 bytes) // spare2: array[int32] { // int32 = 0x0 (4 bytes) // } // } // } // ] memcpy((void*)0x200000000300, "\x00\x00\x04\x00\x08\x00\x00\x00\x50\xf0\x00\x00\x00\x00\x00\x00", 16); *(uint32_t*)0x200000000310 = 0x7fffffff; *(uint32_t*)0x200000000314 = 5; *(uint32_t*)0x200000000318 = 6; *(uint32_t*)0x20000000031c = 1; *(uint32_t*)0x200000000320 = 8; *(uint16_t*)0x200000000324 = 2; *(uint16_t*)0x200000000326 = 0x16; *(uint16_t*)0x200000000328 = 0x9fff; *(uint16_t*)0x20000000032a = 0; *(uint16_t*)0x20000000032c = 9; *(uint16_t*)0x20000000032e = 1; *(uint32_t*)0x200000000330 = 0x7ffb; *(uint32_t*)0x200000000334 = 0xfff7; *(uint32_t*)0x200000000338 = 0; syscall(SYS_ioctl, /*fd=*/r[1], /*cmd=*/0xc03c6992ul, /*arg=*/0x200000000300ul); break; case 4: // mmap arguments: [ // addr: VMA[0x2000] // len: len = 0x2000 (8 bytes) // prot: mmap_prot = 0x2 (8 bytes) // flags: mmap_flags = 0x11 (8 bytes) // fd: fd (resource) // offset: intptr = 0x400000000000000 (8 bytes) // ] syscall(SYS_mmap, /*addr=*/0x200000000000ul, /*len=*/0x2000ul, /*prot=PROT_WRITE*/ 2ul, /*flags=MAP_FIXED|MAP_SHARED*/ 0x11ul, /*fd=*/r[1], /*offset=*/0x400000000000000ul); break; case 5: // msync arguments: [ // addr: VMA[0x1000] // len: len = 0x1000 (8 bytes) // f: msync_flags = 0x2 (8 bytes) // ] syscall(SYS_msync, /*addr=*/0x200000000000ul, /*len=*/0x1000ul, /*f=MS_INVALIDATE*/ 2ul); break; case 6: // writev arguments: [ // fd: fd (resource) // vec: ptr[in, array[iovec_in]] { // array[iovec_in] { // iovec_in { // addr: ptr[in, buffer] { // buffer: {23} (length 0x1) // } // len: len = 0x100000 (8 bytes) // } // } // } // vlen: len = 0x1 (8 bytes) // ] *(uint64_t*)0x200000000480 = 0x2000000004c0; memset((void*)0x2000000004c0, 35, 1); *(uint64_t*)0x200000000488 = 0x100000; syscall(SYS_writev, /*fd=*/r[0], /*vec=*/0x200000000480ul, /*vlen=*/1ul); break; case 7: // open$dir arguments: [ // file: nil // flags: open_flags = 0x3e0 (8 bytes) // mode: open_mode = 0x0 (8 bytes) // ] // returns fd_dir syscall(SYS_open, /*file=*/0ul, /*flags=O_SYNC|O_NOFOLLOW|O_CREAT|FASYNC|0x20*/ 0x3e0ul, /*mode=*/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; for (procid = 0; procid < 4; procid++) { if (fork() == 0) { loop(); } } sleep(1000000); return 0; }