// https://syzkaller.appspot.com/bug?id=efba70aa85a9f8096a8e4fa956c02e209806a7a1 // 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 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; } 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 < 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 (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[2] = {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: *(uint32_t*)0x20000080 = 0; *(uint32_t*)0x20000084 = 0; *(uint32_t*)0x20000088 = 0x90d2; *(uint32_t*)0x2000008c = 0; memcpy((void*)0x20000090, "\xd7\x30\xc1\xe7\xbb\x6f\xc6\xe2\x3c\x5b\x00\x00\x00\x00\x00\x00" "\x00\xe7\x4d\xe4", 20); *(uint32_t*)0x200000a4 = 0; *(uint32_t*)0x200000a8 = 0; syscall(SYS_ioctl, r[0], 0x802c7416, 0x20000080); break; case 2: memcpy( (void*)0x20000200, "\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\x17\x39\xee\x6c\xfd\x0f" "\x85\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\x00\x00\x00\x00\x00\x00\x01\xff\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\x99\x17\xdd\x1d\x95\xe8\xf6\xb9\xd3\x60\xee\x90\x6e\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\x00\x00\x71\x06\xbd\x31\xb4\x60\x5f\x1d\x00\x08\xfd\xbc\x15" "\xa6\x02\x9b\x9a\x5b\x80\x6d\x3e\xa7\xb6\xa7\xd9\x6d\xb3\xa6\x7d\x37" "\x36\xb2\x69\x79\x0d\x96\xf5\xb1\x01\xbc\x48\xce\xbd\x1f\x74\x80\x40" "\x91\xd0\xcf\x27\xda\x72\x1a\xb3\xfe\x68\xbf\xdd\xd2\x7d\x0a\x11\x0f" "\xeb\xf1\xb4\xd9\x97\x76\x52\xce\x45\xf4\x87\x17\x74\xc7\x80\x9d\x2b" "\x4b\x3c\xe5\x9d\xf4\x98\x97\x87\x2d\x4b\x02\x92\x85\x1d\x36\xee\xae" "\x7e\xbf\xb4\xc9\xd7\xb3\x1f\x67\x8d\xf4\x4f\xf0\xff", 285); syscall(SYS_write, r[0], 0x20000200, 0x11d); break; case 3: res = syz_open_pts(); if (res != -1) r[1] = res; break; case 4: syscall(SYS_fsync, r[0]); break; case 5: syscall(SYS_mmap, 0x20fff000, 0x1000, 6, 0x11, r[1], 0, 0); break; case 6: syscall(SYS_dup, r[1]); break; case 7: syz_open_pts(); break; } } int main(void) { syscall(SYS_mmap, 0x20000000, 0x1000000, 3, 0x1012, -1, 0, 0); use_temporary_dir(); loop(); return 0; }