// https://syzkaller.appspot.com/bug?id=7b424c7842078f9cd42221ccc836b1c45e0e29c7 // 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 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); if (pthread_create(&th, &attr, fn, arg)) exit(1); pthread_attr_destroy(&attr); } 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 __syscall syscall static uintptr_t syz_open_pts(void) { int master, slave; if (openpty(&master, &slave, NULL, NULL, NULL) == -1) return -1; if (dup2(master, master + 100) != -1) close(master); return slave; } 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 < 9; 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); } } uint64_t r[3] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff}; void execute_call(int call) { long res; switch (call) { case 0: res = syz_open_pts(); if (res != -1) r[0] = res; break; case 1: NONFAILING(memcpy( (void*)0x200006c0, "\x58\x27\x20\xef\xab\xd1\x6e\xba\xe6\x32\x25\x25\x95\x60\xf8\xe5\x81" "\x5f\x73\xf2\xa0\x44\xfd\x33\x05\x55\x52\xfb\xd5\xe4\x17\x89\x63\x2d" "\xfc\x94\xb0\x61\x98\x82\x4b\x3d\x03\x00\x00\x00\x7b\x3b\xd5\x79\x45" "\x6f\xc7\xd4\x09\x6e\x8c\x8b\x6b\x87\xcb\x2d\x72\xb8\xbc\xdb\x9a\x1b" "\xab\x5a\x41\x2f\xec\xe2\xa4\xf5\xf7\x28\x19\x3e\xf1\x6a\xb9\x3f\x12" "\xfc\x4d\x5f\x16\xb1\x36\xa5\xd0\x0d\x16\x28\xb5\x3a\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\xa4\x67\x8e\x48\x21\xb3\xf6\x6c\xb4\x48\xfa\xca" "\xfc\xcc\x77\x9e\x9b\x12\x23\xb1\xeb\xe0\x6a\xf2\x90\xaa\xf4\xb1\x07" "\xf6\x5c\xa4\x81\x57\xb1\xfd\xd5\x28\x2b\xaf\xba\x25\x4a\x77\x89\xb2" "\x7b\x3a\x09\xed\x20\x01\x5f\xbb\x06\x73\x03\x67\x66\x0d\x05\x2d\x7a" "\x83\x73\x58\x3b\xd4\x95\xaa\x5e\x98\x15\x83\x68\x64\xff\x85\x24\x19" "\x0f\xbc\x3c\x83\xb0\xb1\x0d\x62\x25\x8c\xbb\x43\x13\x7c\x9e\xfa\xc7" "\xc6\x1d\x51\x7b\x89\xca\x33\x61\xf4\x95\xcc\x05\x7e\x49\x63\x1f\x0a" "\x9f\x8c\x11\x24\x8a\x9b\xf4\xd1\xa6\x6e\xab\x49\x57\x19\x91\xc5\x50" "\x84\xbd\xb7\x56\x80\xce\x72\x93\xe0\xe6\xbe\x28\xa1\xa8\x7e\x1d\x92" "\x70\xd4\x79", 258)); syscall(SYS_write, r[0], 0x200006c0, 0x102); break; case 2: NONFAILING(memcpy( (void*)0x200004c0, "\x3e\x2a\x79\x13\xe4\xba\xd2\x1c\x71\x4f\x4d\xd3\x6a\x27\xe5\xe2\x7b" "\x21\x6d\x67\x35\x51\xc0\xc0\x1b\x3d\xfa\xf9\x0f\x05\x00\x00\x00\xe5" "\x52\xaa\xee\xfe\xaf\xf0\xf2\xc8\x5e\x28\x31\xc6\x1a\xd4\x49\x01\x1f" "\x78\x00\x00\x00\x00\x00\x00\x01\x3c\x1f\xe9\x00\x20\x49\xfa\x9d\xa3" "\x98\xbc\xd6\x2b\x10\x34\x34\x82\x0a\xbc\x4b\xca\xbb\xa3\x44\x40\x02" "\x00\x7e\x14\x29\x5f\xba\xab\xbb\xaf\xe2\x14\xde\xa7\x9d\x8e\x17\x36" "\xa0\x1e\xd0\x1d\x72\xf5\xdd\x1d\x95\xe8\xf6\xb9\xd3\xb3\xc1\x90\xfe" "\xf0\xc9\xea\x73\x90\x6e\x2a\xdd\xdc\xb9\x5a\xfd\xc2\x7b\x8c\x98\x31" "\x35\x1d\x74\x12\x2a\xb9\xbd\x51\x0e\xb0\x0b\xb2\xc4\xc7\xa1\x8e\xf0" "\x02\x35\x68\x5c\x20\x01\xfc\x17\x00\x00\xde\xee\x2f\x87\x3c\x1e\x71" "\x1d\x5b\x65\xd2\x71\x2d\x93\xae\x73\x1f\xfe\xe5\xa6\x27\xd0\x76\x71" "\x06\xbd\x31\xb4\x60\x5f\x1d\x00\x08\xfd\x32\x3e\x6a\x8b\xab\x89\x05" "\x73\x94\x67\x2c\x25\xbc\x15\xa6\x02", 213)); syscall(SYS_write, r[0], 0x200004c0, 0xd5); break; case 3: res = syz_open_pts(); if (res != -1) r[1] = res; break; case 4: syscall(SYS_close, r[1]); break; case 5: syscall(SYS_dup, r[1]); break; case 6: res = syz_open_pts(); if (res != -1) r[2] = res; break; case 7: NONFAILING(*(uint32_t*)0x20000200 = -1); NONFAILING(*(uint16_t*)0x20000204 = 0x20); NONFAILING(*(uint16_t*)0x20000206 = 0); NONFAILING(*(uint32_t*)0x20000208 = r[1]); NONFAILING(*(uint16_t*)0x2000020c = 0xa); NONFAILING(*(uint16_t*)0x2000020e = 0); NONFAILING(*(uint32_t*)0x20000210 = -1); NONFAILING(*(uint16_t*)0x20000214 = 5); NONFAILING(*(uint16_t*)0x20000216 = 0); NONFAILING(*(uint32_t*)0x20000218 = r[2]); NONFAILING(*(uint16_t*)0x2000021c = 0x100); NONFAILING(*(uint16_t*)0x2000021e = 0); NONFAILING(*(uint32_t*)0x20000220 = r[2]); NONFAILING(*(uint16_t*)0x20000224 = 1); NONFAILING(*(uint16_t*)0x20000226 = 0); NONFAILING(*(uint32_t*)0x20000228 = r[0]); NONFAILING(*(uint16_t*)0x2000022c = 0); NONFAILING(*(uint16_t*)0x2000022e = 0); NONFAILING(*(uint32_t*)0x20000230 = r[2]); NONFAILING(*(uint16_t*)0x20000234 = 0x100); NONFAILING(*(uint16_t*)0x20000236 = 0); NONFAILING(*(uint32_t*)0x20000238 = r[0]); NONFAILING(*(uint16_t*)0x2000023c = 0x140); NONFAILING(*(uint16_t*)0x2000023e = 0); NONFAILING(*(uint32_t*)0x20000240 = r[1]); NONFAILING(*(uint16_t*)0x20000244 = 0x80); NONFAILING(*(uint16_t*)0x20000246 = 0); syscall(SYS_poll, 0x20000200, 9, 0x5a77); break; case 8: syscall(SYS_fsync, r[0]); break; } } int main(void) { syscall(SYS_mmap, 0x20000000, 0x1000000, 3, 0x1012, -1, 0, 0); install_segv_handler(); for (procid = 0; procid < 2; procid++) { if (fork() == 0) { use_temporary_dir(); do_sandbox_none(); } } sleep(1000000); return 0; }