// https://syzkaller.appspot.com/bug?id=4f94742c0ef80b9ac04a6cf031fefa4302671e12 // 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 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_handler = SIG_IGN; syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8); syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8); 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 { 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 (ev->state) 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); } static int event_isset(event_t* ev) { return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE); } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; for (;;) { uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts); if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE)) return 1; now = current_time_ms(); if (now - start > timeout) 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) { if (write(1, "executing program\n", sizeof("executing program\n") - 1)) { } 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); 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*)0x20000000, "./file0\000", 8)); syscall(__NR_mkdirat, /*fd=*/0xffffff9c, /*path=*/0x20000000ul, /*mode=S_IXGRP|S_IRGRP*/ 0x28ul); break; case 1: NONFAILING(memcpy((void*)0x200001c0, "syz\000", 4)); NONFAILING(memcpy((void*)0x20000480, "./file0\000", 8)); NONFAILING(memcpy((void*)0x200004c0, "9p\000", 3)); NONFAILING( memcpy((void*)0x20000840, "\x78\x22\x6e\x6f\x65\x78\x61\x63\x63\x81\x73\x73\x3d\x61\x6e" "\x79\x2c\x63\x61\x63\x68\x65\x3d\x66\x73\x63\x61\x63\x68\x65", 30)); syscall(__NR_mount, /*src=*/0x200001c0ul, /*dst=*/0x20000480ul, /*type=*/0x200004c0ul, /*flags=*/0ul, /*opts=*/0x20000840ul); break; case 2: NONFAILING(memcpy((void*)0x20000280, "./file0\000", 8)); syscall(__NR_chdir, /*dir=*/0x20000280ul); break; case 3: NONFAILING(memcpy((void*)0x20000000, "./file0\000", 8)); res = syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x20000000ul, /*flags=O_NONBLOCK|O_NOFOLLOW|O_APPEND|O_WRONLY*/ 0x20c01ul, /*mode=*/0ul); if (res != -1) r[0] = res; break; case 4: res = syscall(__NR_io_setup, /*n=*/0x1fe, /*ctx=*/0x20000200ul); if (res != -1) NONFAILING(r[1] = *(uint64_t*)0x20000200); break; case 5: NONFAILING(*(uint64_t*)0x20000700 = 0x20000440); NONFAILING(*(uint64_t*)0x20000440 = 0x18); NONFAILING(*(uint32_t*)0x20000448 = 0x7000000); NONFAILING(*(uint32_t*)0x2000044c = 4); NONFAILING(*(uint16_t*)0x20000450 = 1); NONFAILING(*(uint16_t*)0x20000452 = 0); NONFAILING(*(uint32_t*)0x20000454 = r[0]); NONFAILING(*(uint64_t*)0x20000458 = 0x20000180); NONFAILING(memset((void*)0x20000180, 0, 1)); NONFAILING(*(uint64_t*)0x20000460 = 0x37000); NONFAILING(*(uint64_t*)0x20000468 = 0); NONFAILING(*(uint64_t*)0x20000470 = 0); NONFAILING(*(uint32_t*)0x20000478 = 0); NONFAILING(*(uint32_t*)0x2000047c = -1); syscall(__NR_io_submit, /*ctx=*/r[1], /*nr=*/0x140bul, /*iocbpp=*/0x20000700ul); break; } } int main(void) { syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1, /*offset=*/0ul); syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1, /*offset=*/0ul); syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/-1, /*offset=*/0ul); const char* reason; (void)reason; install_segv_handler(); loop(); return 0; }