// https://syzkaller.appspot.com/bug?id=9fcc32fe46ce159d6a3e64b64ab3402a7ac2e9b8 // autogenerated by syzkaller (http://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 const int kFailStatus = 67; const int kRetryStatus = 69; __attribute__((noreturn)) static void doexit(int status) { volatile unsigned i; syscall(__NR_exit_group, status); for (i = 0;; i++) { } } __attribute__((noreturn)) static void fail(const char* msg, ...) { int e = errno; fflush(stdout); va_list args; va_start(args, msg); vfprintf(stderr, msg, args); va_end(args); fprintf(stderr, " (errno %d)\n", e); doexit((e == ENOMEM || e == EAGAIN) ? kRetryStatus : kFailStatus); } __attribute__((noreturn)) static void exitf(const char* msg, ...) { int e = errno; fflush(stdout); va_list args; va_start(args, msg); vfprintf(stderr, msg, args); va_end(args); fprintf(stderr, " (errno %d)\n", e); doexit(kRetryStatus); } static __thread int skip_segv; static __thread jmp_buf segv_env; static void segv_handler(int sig, siginfo_t* info, void* uctx) { 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); } doexit(sig); for (;;) { } } static void install_segv_handler() { 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(...) \ { \ __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 use_temporary_dir() { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) fail("failed to mkdtemp"); if (chmod(tmpdir, 0777)) fail("failed to chmod"); if (chdir(tmpdir)) fail("failed to chdir"); } static void remove_dir(const char* dir) { DIR* dp; struct dirent* ep; int iter = 0; retry: dp = opendir(dir); if (dp == NULL) { if (errno == EMFILE) { exitf("opendir(%s) failed due to NOFILE, exiting"); } exitf("opendir(%s) failed", dir); } 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)) exitf("lstat(%s) failed", filename); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } int i; for (i = 0;; i++) { if (unlink(filename) == 0) break; if (errno == EROFS) { break; } if (errno != EBUSY || i > 100) exitf("unlink(%s) failed", filename); if (umount2(filename, MNT_DETACH)) exitf("umount(%s) failed", filename); } } closedir(dp); int i; for (i = 0;; i++) { if (rmdir(dir) == 0) break; if (i < 100) { if (errno == EROFS) { break; } if (errno == EBUSY) { if (umount2(dir, MNT_DETACH)) exitf("umount(%s) failed", dir); continue; } if (errno == ENOTEMPTY) { if (iter < 100) { iter++; goto retry; } } } exitf("rmdir(%s) failed", dir); } } static uint64_t current_time_ms() { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) fail("clock_gettime failed"); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void test(); void loop() { int iter; for (iter = 0;; iter++) { char cwdbuf[256]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) fail("failed to mkdir"); int pid = fork(); if (pid < 0) fail("clone failed"); if (pid == 0) { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); setpgrp(); if (chdir(cwdbuf)) fail("failed to chdir"); test(); doexit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { int res = waitpid(-1, &status, __WALL | WNOHANG); if (res == pid) break; usleep(1000); if (current_time_ms() - start > 5 * 1000) { kill(-pid, SIGKILL); kill(pid, SIGKILL); while (waitpid(-1, &status, __WALL) != pid) { } break; } } remove_dir(cwdbuf); } } long r[41]; void* thr(void* arg) { switch ((long)arg) { case 0: r[0] = syscall(__NR_mmap, 0x20000000ul, 0xfff000ul, 0x3ul, 0x32ul, 0xfffffffffffffffful, 0x0ul); break; case 1: NONFAILING(*(uint32_t*)0x20e7af88 = (uint32_t)0x1); NONFAILING(*(uint32_t*)0x20e7af8c = (uint32_t)0x78); NONFAILING(*(uint8_t*)0x20e7af90 = (uint8_t)0x0); NONFAILING(*(uint8_t*)0x20e7af91 = (uint8_t)0x0); NONFAILING(*(uint8_t*)0x20e7af92 = (uint8_t)0x0); NONFAILING(*(uint8_t*)0x20e7af93 = (uint8_t)0x0); NONFAILING(*(uint32_t*)0x20e7af94 = (uint32_t)0x0); NONFAILING(*(uint64_t*)0x20e7af98 = (uint64_t)0x6); NONFAILING(*(uint64_t*)0x20e7afa0 = (uint64_t)0x0); NONFAILING(*(uint64_t*)0x20e7afa8 = (uint64_t)0x0); NONFAILING(*(uint8_t*)0x20e7afb0 = (uint8_t)0xd34); NONFAILING(*(uint8_t*)0x20e7afb1 = (uint8_t)0x0); NONFAILING(*(uint8_t*)0x20e7afb2 = (uint8_t)0x0); NONFAILING(*(uint8_t*)0x20e7afb3 = (uint8_t)0x0); NONFAILING(*(uint32_t*)0x20e7afb4 = (uint32_t)0x0); NONFAILING(*(uint32_t*)0x20e7afb8 = (uint32_t)0x0); NONFAILING(*(uint32_t*)0x20e7afbc = (uint32_t)0x0); NONFAILING(*(uint64_t*)0x20e7afc0 = (uint64_t)0x0); NONFAILING(*(uint64_t*)0x20e7afc8 = (uint64_t)0x0); NONFAILING(*(uint64_t*)0x20e7afd0 = (uint64_t)0x0); NONFAILING(*(uint64_t*)0x20e7afd8 = (uint64_t)0x0); NONFAILING(*(uint64_t*)0x20e7afe0 = (uint64_t)0x0); NONFAILING(*(uint32_t*)0x20e7afe8 = (uint32_t)0x0); NONFAILING(*(uint64_t*)0x20e7aff0 = (uint64_t)0x0); NONFAILING(*(uint32_t*)0x20e7aff8 = (uint32_t)0x0); NONFAILING(*(uint16_t*)0x20e7affc = (uint16_t)0x0); NONFAILING(*(uint16_t*)0x20e7affe = (uint16_t)0x0); r[28] = syscall(__NR_perf_event_open, 0x20e7af88ul, 0x0ul, 0xfffffffful, 0xfffffffffffffffful, 0x0ul); break; case 2: r[29] = syscall(__NR_fcntl, 0xfffffffffffffffful, 0x0ul, 0xfffffffffffffffful); break; case 3: r[30] = syscall(__NR_socket, 0x26ul, 0x5ul, 0x0ul); break; case 4: NONFAILING(*(uint16_t*)0x20037fa8 = (uint16_t)0x26); NONFAILING(memcpy( (void*)0x20037faa, "\x68\x61\x73\x68\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00", 14)); NONFAILING(*(uint32_t*)0x20037fb8 = (uint32_t)0x0); NONFAILING(*(uint32_t*)0x20037fbc = (uint32_t)0x0); NONFAILING(memcpy((void*)0x20037fc0, "\x68\x6d\x61\x63\x28\x73\x68\x61\x31\x29\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00", 64)); r[36] = syscall(__NR_bind, r[30], 0x20037fa8ul, 0x58ul); break; case 5: NONFAILING(memcpy( (void*)0x20ed2000, "\x3f\xd2\x34\x16\x1b\xd9\xf9\xd9\xa6\x9d\xc2\x14\x02\x4e\x01" "\xb5\x58\x0e\x9a\x76\xca\xbe\xe1\x54\x3b\x00\x00\x00\xcc\x9d" "\x2b\x9e\x71\x9a\xb4\xde\xea\x29\xb3\x45\x02\x24\x6a\x0d\x72" "\x34\x6b\x1a\xac\x07\xf0\x7f\x07\xbb\x00\x86\xdc\xce\x82\x8a" "\xf7\x2c\x47\x54\x2f\x1f\x7c\x4b\xb8\x62\x71\x49\x79\x13\x99" "\xcb\x1b\xad\x73\x3e\x0d\x2e\x2f\xff\x44\xbe\xc7\x3b\x62\x5d" "\x4c\x63\xe8\x69\x00\xc6\x25\x51\xe7\x83\xaa\xe2\x7d\x60\xca" "\xf7\xa6\xcd\xf0\x88\xc1\x10\x68\xdf\xd9\xe4\xde\xfc\xb0\xc8" "\x4e\x33\x8f\xc4\x6e\x66\x95\xf0\x08\x8a\x75\x57\x18\x58\xac" "\xf7\xb1\x81\x99\x56\xdd\xd6\x5b\xb7\x86\x63\x73\xcc\x0c\x79" "\x24\x12\xd2\x97\xc2\x89\x5f\x91\x7b\x82\x86\x1f\xc8\xbb\x0c" "\x8b\xdc\xb2\xf8\x66\x95\xd6\xbe\xca\xe0\xfd\xb3\x14\xca\xb1" "\x85\x9f\x8a\x71\xe0\x77", 186)); r[38] = syscall(__NR_setsockopt, r[30], 0x117ul, 0x1ul, 0x20ed2000ul, 0xbaul); break; case 6: NONFAILING(memcpy((void*)0x2043fff7, "\x2f\x64\x65\x76\x2f\x6b\x76\x6d\x00", 9)); r[40] = syscall(__NR_openat, 0xffffffffffffff9cul, 0x2043fff7ul, 0x1ul, 0x0ul); break; } return 0; } void test() { long i; pthread_t th[14]; memset(r, -1, sizeof(r)); srand(getpid()); for (i = 0; i < 7; i++) { pthread_create(&th[i], 0, thr, (void*)i); usleep(rand() % 10000); } for (i = 0; i < 7; i++) { pthread_create(&th[7 + i], 0, thr, (void*)i); if (rand() % 2) usleep(rand() % 10000); } usleep(rand() % 100000); } int main() { int i; for (i = 0; i < 8; i++) { if (fork() == 0) { install_segv_handler(); use_temporary_dir(); loop(); return 0; } } sleep(1000000); return 0; }