// https://syzkaller.appspot.com/bug?id=624e3138de7b6ec7eb35a6e699b1324ab607918c // 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 static unsigned long long procid; static __thread int skip_segv; static __thread jmp_buf segv_env; static void segv_handler(int sig, siginfo_t* info, void* ctx) { uintptr_t addr = (uintptr_t)info->si_addr; const uintptr_t prog_start = 1 << 20; const uintptr_t prog_end = 100 << 20; if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) && (addr < prog_start || addr > prog_end)) { _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(...) \ { \ __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \ if (_setjmp(segv_env) == 0) { \ __VA_ARGS__; \ } \ __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \ } 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 use_temporary_dir(void) { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } static void remove_dir(const char* dir) { DIR* dp; struct dirent* ep; dp = opendir(dir); if (dp == NULL) exit(1); while ((ep = readdir(dp))) { if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue; char filename[FILENAME_MAX]; snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } if (unlink(filename)) exit(1); } closedir(dp); if (rmdir(dir)) exit(1); } 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; for (i = 0; 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; } static void sandbox_common() { if (setsid() == -1) exit(1); 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 execute_one(void) { int i, call, thread; int collide = 0; again: for (call = 0; call < 14; 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 (collide && (call % 2) == 0) break; event_timedwait(&th->done, 45); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); if (!collide) { collide = 1; goto again; } } static void execute_one(void); #define WAIT_FLAGS 0 static void loop(void) { int iter; for (iter = 0;; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; sleep_ms(1); if (current_time_ms() - start < 5 * 1000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } #ifndef SYS_bind #define SYS_bind 104 #endif #ifndef SYS_getsockopt #define SYS_getsockopt 118 #endif #ifndef SYS_mmap #define SYS_mmap 197 #endif #ifndef SYS_open #define SYS_open 5 #endif #ifndef SYS_sendmsg #define SYS_sendmsg 28 #endif #ifndef SYS_socket #define SYS_socket 394 #endif #ifndef SYS_writev #define SYS_writev 121 #endif uint64_t r[5] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: NONFAILING(memcpy((void*)0x20000040, "./file0\000", 8)); syscall(SYS_open, 0x20000040ul, 0x615ul, 0ul); break; case 1: NONFAILING(memcpy((void*)0x20000100, "./file0\000", 8)); res = syscall(SYS_open, 0x20000100ul, 2ul, 0ul); if (res != -1) r[0] = res; break; case 2: NONFAILING(*(uint64_t*)0x20000340 = 0x20000180); NONFAILING(*(uint64_t*)0x20000348 = 0x81700); syscall(SYS_writev, r[0], 0x20000340ul, 0x1000000000000013ul); break; case 3: res = syscall(SYS_socket, 2ul, 0ul, 0); if (res != -1) r[1] = res; break; case 4: NONFAILING(memcpy((void*)0x20000100, "./file0\000", 8)); res = syscall(SYS_open, 0x20000100ul, 0x40000400000002c2ul, 1ul); if (res != -1) r[2] = res; break; case 5: NONFAILING(memcpy((void*)0x200002c0, "./file0\000", 8)); res = syscall(SYS_open, 0x200002c0ul, 0x10000ul, 0x18bul); if (res != -1) r[3] = res; break; case 6: syscall(SYS_mmap, 0x20000000ul, 0x200000ul, 3ul, 0x10ul, r[3], 0ul, 0ul); break; case 7: NONFAILING(*(uint32_t*)0x200008c0 = 0); syscall(SYS_getsockopt, r[1], 0, 0, 0ul, 0x200008c0ul); break; case 8: NONFAILING(memcpy((void*)0x20000100, "./file0\000", 8)); res = syscall(SYS_open, 0x20000100ul, 2ul, 0ul); if (res != -1) r[4] = res; break; case 9: NONFAILING(*(uint64_t*)0x20000340 = 0x20000180); NONFAILING(*(uint64_t*)0x20000348 = 0x81700); syscall(SYS_writev, r[4], 0x20000340ul, 0x1000000000000013ul); break; case 10: syscall(SYS_socket, 2ul, 0x10000000ul, 3); break; case 11: syscall(SYS_bind, -1, 0ul, 0ul); break; case 12: NONFAILING(*(uint64_t*)0x20000300 = 0); NONFAILING(*(uint32_t*)0x20000308 = 0); NONFAILING(*(uint64_t*)0x20000310 = 0x20000640); NONFAILING(*(uint64_t*)0x20000640 = 0x20000080); NONFAILING(*(uint64_t*)0x20000648 = 0); NONFAILING(*(uint64_t*)0x20000650 = 0); NONFAILING(*(uint64_t*)0x20000658 = 0); NONFAILING(*(uint64_t*)0x20000660 = 0); NONFAILING(*(uint64_t*)0x20000668 = 0); NONFAILING(*(uint64_t*)0x20000670 = 0x20000240); NONFAILING(*(uint64_t*)0x20000678 = 0); NONFAILING(*(uint64_t*)0x20000680 = 0x20000380); NONFAILING(memcpy( (void*)0x20000380, "\x2d\x1d\x18\xbc\xbc\xcd\x25\x74\x9f\x47\xb6\x7d\xbb\xdb\x9b\x4b\x2a" "\xd5\x59\x97\x17\x28\x3e\x2f\x23\x80\x8e\xf6\x33\x5b\x95\x72\xa9\xfb" "\x41\xc8\x3d\x06\xc8\x53\x0c\x90\x91\xf8\x95\xfc\x3c\xf1\x46\xc4\xdd" "\xeb\xe9\x4a\x60\xa1\x31\x11\x01\x4b\x16\x56\x7a\x20\xab\x81\x29\xbc" "\xec\xac\x1f\x89\xea\x55\x31\x87\x32\xa2\x3e\x16\x12\xa6\xd7\xee\x97" "\xc2\x57\x2d\xfb\x64\x4e\xd3\x9f\x79\x8c\x41\xa4\xca\xdd\xd8\xce\xd3" "\x0c\x69\xfb\x0b\xdb\x80\x37\xd3\x7b\xc3\xd1\x45\xbd\x7e\xf9\x1c\xc2" "\x71\x06\x58\x6f\x5b\x7c\xf3\xc6\x44\x04\x08\xab\x59\xe9\x02\x94\x20" "\xf2\x88\x47\xd8\x2c\xd0\x10\xbd\x98\xc7\x01\x01\x02\x02\x35\xea\x1e" "\x1c\x60\x8c\x88\x80\x43\x6c\x5c\x99\x11\xff\x11\xf5\xbd\x4e\x93\x0d" "\xfb\x82\x6b\x4a\xad\xce\x7f\xca\x7f\x38\x8c\xea\x23\x72\x04\xbd\x58" "\xbb\x90\x0e\xed\x3a\x73\x9b\x99\xec", 196)); NONFAILING(*(uint64_t*)0x20000688 = 0xc4); NONFAILING(*(uint64_t*)0x20000690 = 0x20000480); NONFAILING(*(uint64_t*)0x20000698 = 0); NONFAILING(*(uint64_t*)0x200006a0 = 0x20000540); NONFAILING(*(uint64_t*)0x200006a8 = 0); NONFAILING(*(uint64_t*)0x200006b0 = 0x200005c0); NONFAILING(memcpy((void*)0x200005c0, "\x25\x2c\x7f\x37\xd5\x20\x7c\x59\x26\x14\x8b\x90\x8b\x65" "\x2e\x3d\x7a\x65\x1f\x2b\x03\x56\x3a\x4e\xa6\x6e\x49\xa3" "\xdc\xb8\xdb\xe8\xf0\xab\x9a\x93\xff\xfd\x25\x0b\xd9\x95" "\x57\x6b\xf6\x0b\xc2\x84\x83\xad\x9c\x45\x15\xd8\x83\x03" "\x62\x6c\x0d\x7d\xf9\x75\xde\x1c\xcd\xb4\x06\xfc\xdc\x4f" "\x7c\xa2\x5a\xc5\xa4\xe5\x51\x82\x5d\xdc\x09\x26\xe1\xa5" "\xc4\x58\x6c\x90\xd3\xa3", 90)); NONFAILING(*(uint64_t*)0x200006b8 = 0x5a); NONFAILING(*(uint64_t*)0x20000318 = 8); NONFAILING(*(uint64_t*)0x20000320 = 0x200006c0); NONFAILING(*(uint64_t*)0x200006c0 = 0xf8); NONFAILING(*(uint32_t*)0x200006c8 = 1); NONFAILING(*(uint32_t*)0x200006cc = 7); NONFAILING(memcpy( (void*)0x200006d0, "\xad\x54\x1b\xaa\x1e\xc6\x89\x64\x50\xba\x95\xe9\xdc\x00\xd5\xfb\x7f" "\x4f\x71\xe3\x7a\xac\x55\x28\x6e\xe9\xf5\x47\xf3\x3a\xfc\xc5\xac\x7e" "\x37\x69\x75\x44\xea\x51\xac\x1e\x2d\xda\x0e\x6b\x3a\x4f\xeb\x52\x3b" "\x2a\x94\x3d\x89\x05\xb0\xd1\x6c\xc3\xd3\x2d\x56\xaa\xd0\x19\xe6\x73" "\x46\x2b\x1e\xad\x20\x77\x8f\xb6\xec\x85\xd6\x2a\xe6\x73\xdc\x66\x45" "\x1b\xc8\xe8\x75\x6f\x6a\xa2\xca\x79\x9a\xb8\xbb\xb7\xd3\x13\x4c\x10" "\xed\x2d\x22\xf0\x77\x58\xaa\x3f\x07\x7c\x08\xfc\x02\x2a\xbb\xc2\x37" "\xf9\x55\xe2\x5a\xc8\xba\xe8\xee\x2b\x01\xc2\x89\x13\x3c\x44\x1d\x21" "\x11\x01\xba\xfe\x93\xac\xc6\xf5\x9c\xd7\x52\xff\x4b\x9f\x13\x2f\x66" "\xcb\xee\xd3\x0a\x23\x9f\xe2\xe8\x46\x60\xa5\x81\xdd\xee\xc5\x9d\x00" "\x06\x7f\x15\x81\xcb\xf5\x13\x05\xa1\xfe\x41\x5c\xa0\x48\xfd\xf6\x3e" "\x7c\x67\x22\x61\xed\x75\x56\xae\x32\xf6\xca\xe7\x49\x14\x75\xfa\xf0" "\xbc\x8f\x51\x4e\x90\x33\x07\x5c\x01\xa2\x18\xc5\x8a\x8c\x03\xbb\x3a" "\x86\x68\x87\xdc\x40\x5c\x1e\xfc", 229)); NONFAILING(*(uint64_t*)0x20000328 = 0xf8); NONFAILING(*(uint32_t*)0x20000330 = 0); syscall(SYS_sendmsg, -1, 0x20000300ul, 0x400ul); break; case 13: NONFAILING(*(uint64_t*)0x20000340 = 0x20000180); NONFAILING(*(uint64_t*)0x20000348 = 0x81700); syscall(SYS_writev, r[2], 0x20000340ul, 0x1000000000000013ul); break; } } int main(void) { syscall(SYS_mmap, 0x20000000ul, 0x1000000ul, 3ul, 0x1012ul, -1, 0ul, 0ul); install_segv_handler(); for (procid = 0; procid < 6; procid++) { if (fork() == 0) { use_temporary_dir(); do_sandbox_none(); } } sleep(1000000); return 0; }