// https://syzkaller.appspot.com/bug?id=06aa5c3514c3d8739b7566521420673284f89f3b // 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 #include #include #include #include #include #include #include #include #include #include #include #include #include #include static __thread int clone_ongoing; static __thread int skip_segv; static __thread jmp_buf segv_env; static void segv_handler(int sig, siginfo_t* info, void* ctx) { if (__atomic_load_n(&clone_ongoing, __ATOMIC_RELAXED) != 0) { exit(sig); } uintptr_t addr = (uintptr_t)info->si_addr; const uintptr_t prog_start = 1 << 20; const uintptr_t prog_end = 100 << 20; int skip = __atomic_load_n(&skip_segv, __ATOMIC_RELAXED) != 0; int valid = addr < prog_start || addr > prog_end; if (skip && valid) { _longjmp(segv_env, 1); } exit(sig); } static void install_segv_handler(void) { struct sigaction sa; memset(&sa, 0, sizeof(sa)); sa.sa_sigaction = segv_handler; sa.sa_flags = SA_NODEFER | SA_SIGINFO; sigaction(SIGSEGV, &sa, NULL); sigaction(SIGBUS, &sa, NULL); } #define NONFAILING(...) ({ int ok = 1; __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); if (_setjmp(segv_env) == 0) { __VA_ARGS__; } else ok = 0; __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); ok; }) 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; } #define CAST static void sandbox_common() { struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 8 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 0; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); } static void loop(); static int do_sandbox_none(void) { sandbox_common(); loop(); return 0; } 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 loop(void) { int i, call, thread; for (call = 0; call < 6; 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 || call == 4) break; event_timedwait(&th->done, 50); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); } uint64_t r[2] = {0xffffffffffffffff, 0x0}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: NONFAILING(memcpy((void*)0x20000080, "./file0\000", 8)); res = -1; NONFAILING(res = ((intptr_t(*)(intptr_t,intptr_t,intptr_t))CAST(open))(/*file=*/0x20000080, /*flags=O_RSYNC|O_NOFOLLOW|O_TRUNC|O_CREAT|0xe*/0x78e, /*mode=*/0)); if (res != -1) r[0] = res; break; case 1: NONFAILING(*(uint64_t*)0x20000500 = 0); NONFAILING(*(uint64_t*)0x20000508 = 0); NONFAILING(*(uint64_t*)0x20000510 = 0x20000580); NONFAILING(memcpy((void*)0x20000580, "\x07\x90\xf7\x88\xff\x66\x4b\x51\xb0\x4b\xc2\x3b\xcb\x35\xcc\x6c\x1c\x38\xc4\xe3\x93\x05\xa6\x58\xa9\x19\x8f\x4f\xf7\x9f\x96\xfa\x45\x8d\xcf\xe7\xd9\x2c\x7e\x3c\xa5\xbe\x12\x83\x30\xcb\x26\x49\x3e\xd7\xb8\x86\x0b\xb4\x0d\x9b\xba\x8d\x13\x54\x84\x49\x79\x80\x40\x5e\x50\x25\x02\xb9\x33\x84\x0c\xaa\xc9\x31\xa3\x50\xbc\x38\x35\x88\xc1\x20\xe5\x53\x73\x3d\xca\x6e\xec\x22\x3f\xd2\x8b\x6d\xa7\xca\x71\xf3\xf8\x33\x67\xf4\x50\x5c\x0d\x6f\xcf\xfd\x40\x6a\x72\xb2\x48\x6a\xe7\xbe\x86\xa8\xb6\x2d\xec\x6c\x4b\xd2\x68\x5b\xd9\x68\x29\x2d\x7f\xf7\x99\x07\xb1\x9b\x1c\xed\x01\x39\xff\x29\x42\xa5\x69\x41\xc7\x5a\x90\xfd\x72\x13\x3e\x1e\x8c\xb1\x4e\x47\x7a\x28\xf3\x88\x1f\x99\x4b\xab\xef\x8f\x8a\xb4\x15\x73\xdf\x86\x2d\x4c\xfc\x26\x21\xba\x12\x9c\x10\xc8\xbe\x61\xfc\x4c\xb3\xb4\x35\x98\x54\x0f\x62\x79\xbe", 199)); NONFAILING(*(uint64_t*)0x20000518 = 0xc7); NONFAILING(*(uint64_t*)0x20000520 = 0); NONFAILING(*(uint64_t*)0x20000528 = 0); NONFAILING(*(uint64_t*)0x20000530 = 0); NONFAILING(*(uint64_t*)0x20000538 = 0); NONFAILING(((intptr_t(*)(intptr_t,intptr_t,intptr_t))CAST(writev))(/*fd=*/r[0], /*vec=*/0x20000500, /*vlen=*/4)); break; case 2: NONFAILING(((intptr_t(*)(intptr_t,intptr_t,intptr_t,intptr_t,intptr_t,intptr_t))CAST(mmap))(/*addr=*/0x20000000, /*len=*/0x13000, /*prot=PROT_READ|PROT_EXEC*/5, /*flags=MAP_FIXED*/0x10, /*fd=*/r[0], /*offset=*/0)); break; case 3: res = -1; NONFAILING(res = ((intptr_t(*)(intptr_t,intptr_t,intptr_t))CAST(semget))(/*key=*/0, /*nsems=*/0x4000000009, /*flags=*/0x82)); if (res != -1) r[1] = res; break; case 4: NONFAILING(*(uint16_t*)0x200002c0 = 3); NONFAILING(*(uint16_t*)0x200002c2 = 0xbf4e); NONFAILING(*(uint16_t*)0x200002c4 = 0x1800); NONFAILING(*(uint16_t*)0x200002c6 = 2); NONFAILING(*(uint16_t*)0x200002c8 = 0x1f); NONFAILING(*(uint16_t*)0x200002ca = 0); NONFAILING(*(uint16_t*)0x200002cc = 1); NONFAILING(*(uint16_t*)0x200002ce = 4); NONFAILING(*(uint16_t*)0x200002d0 = 0x1800); NONFAILING(*(uint16_t*)0x200002d2 = 4); NONFAILING(*(uint16_t*)0x200002d4 = 0xe5f1); NONFAILING(*(uint16_t*)0x200002d6 = 0); NONFAILING(*(uint16_t*)0x200002d8 = 0); NONFAILING(*(uint16_t*)0x200002da = 0xffd); NONFAILING(*(uint16_t*)0x200002dc = 0x1000); NONFAILING(*(uint16_t*)0x200002de = 4); NONFAILING(*(uint16_t*)0x200002e0 = 0x53); NONFAILING(*(uint16_t*)0x200002e2 = 0x1000); NONFAILING(*(uint16_t*)0x200002e4 = 2); NONFAILING(*(uint16_t*)0x200002e6 = 2); NONFAILING(*(uint16_t*)0x200002e8 = 0x1000); NONFAILING(*(uint16_t*)0x200002ea = 2); NONFAILING(*(uint16_t*)0x200002ec = 0); NONFAILING(*(uint16_t*)0x200002ee = 0x800); NONFAILING(*(uint16_t*)0x200002f0 = 2); NONFAILING(*(uint16_t*)0x200002f2 = 0xfffd); NONFAILING(*(uint16_t*)0x200002f4 = 0); NONFAILING(*(uint16_t*)0x200002f6 = 0); NONFAILING(*(uint16_t*)0x200002f8 = 9); NONFAILING(*(uint16_t*)0x200002fa = 0x1800); NONFAILING(((intptr_t(*)(intptr_t,intptr_t,intptr_t))CAST(semop))(/*semid=*/r[1], /*ops=*/0x200002c0, /*nops=*/0xa)); break; case 5: NONFAILING(((intptr_t(*)(intptr_t,intptr_t,intptr_t,intptr_t))CAST(semctl))(/*semid=*/r[1], /*semnum=*/0, /*cmd=*/0, 0)); break; } } int main(void) { NONFAILING(((intptr_t(*)(intptr_t,intptr_t,intptr_t,intptr_t,intptr_t,intptr_t))CAST(mmap))(/*addr=*/0x20000000, /*len=*/0x1000000, /*prot=PROT_WRITE|PROT_READ*/3, /*flags=MAP_ANONYMOUS|MAP_FIXED|MAP_PRIVATE*/0x1012, /*fd=*/-1, /*offset=*/0)); install_segv_handler(); do_sandbox_none(); return 0; }