// autogenerated by syzkaller (http://github.com/google/syzkaller) #ifndef __NR_sendmsg #define __NR_sendmsg 46 #endif #ifndef __NR_mmap #define __NR_mmap 9 #endif #ifndef __NR_socket #define __NR_socket 41 #endif #ifndef __NR_connect #define __NR_connect 42 #endif #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 #include #include #include #include #include #include #include #include #include const int kFailStatus = 67; const int kErrorStatus = 68; const int kRetryStatus = 69; __attribute__((noreturn)) void doexit(int status) { volatile unsigned i; syscall(__NR_exit_group, status); for (i = 0;; i++) { } } __attribute__((noreturn)) 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 ? kRetryStatus : kFailStatus); } __attribute__((noreturn)) 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 int flag_debug; void debug(const char* msg, ...) { if (!flag_debug) return; va_list args; va_start(args, msg); vfprintf(stdout, msg, args); va_end(args); fflush(stdout); } __thread int skip_segv; __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)) { debug("SIGSEGV on %p, skipping\n", addr); _longjmp(segv_env, 1); } debug("SIGSEGV on %p, exiting\n", addr); doexit(sig); for (;;) { } } static void install_segv_handler() { 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); \ } #define BITMASK_LEN(type, bf_len) (type)((1ull << (bf_len)) - 1) #define BITMASK_LEN_OFF(type, bf_off, bf_len) \ (type)(BITMASK_LEN(type, (bf_len)) << (bf_off)) #define STORE_BY_BITMASK(type, addr, val, bf_off, bf_len) \ if ((bf_off) == 0 && (bf_len) == 0) { \ *(type*)(addr) = (type)(val); \ } else { \ type new_val = *(type*)(addr); \ new_val &= ~BITMASK_LEN_OFF(type, (bf_off), (bf_len)); \ new_val |= ((type)(val)&BITMASK_LEN(type, (bf_len))) << (bf_off); \ *(type*)(addr) = new_val; \ } static uintptr_t execute_syscall(int nr, uintptr_t a0, uintptr_t a1, uintptr_t a2, uintptr_t a3, uintptr_t a4, uintptr_t a5, uintptr_t a6, uintptr_t a7, uintptr_t a8) { switch (nr) { default: return syscall(nr, a0, a1, a2, a3, a4, a5); } } static void setup_main_process() { 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); install_segv_handler(); 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 loop(); static void sandbox_common() { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); setpgrp(); setsid(); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = 128 << 20; setrlimit(RLIMIT_AS, &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); unshare(CLONE_NEWNS); unshare(CLONE_NEWIPC); unshare(CLONE_IO); } static int do_sandbox_none(int executor_pid, bool enable_tun) { int pid = fork(); if (pid) return pid; sandbox_common(); loop(); doexit(1); } long r[30]; void* thr(void* arg) { switch ((long)arg) { case 0: r[0] = execute_syscall(__NR_mmap, 0x20000000ul, 0x1f2000ul, 0x3ul, 0x32ul, 0xfffffffffffffffful, 0x0ul, 0, 0, 0); break; case 1: r[1] = execute_syscall(__NR_socket, 0x1aul, 0x2ul, 0x1ul, 0, 0, 0, 0, 0, 0); break; case 2: NONFAILING(*(uint16_t*)0x20000000 = (uint16_t)0x1f); NONFAILING(*(uint8_t*)0x20000002 = (uint8_t)0x8002000004); NONFAILING(*(uint8_t*)0x20000003 = (uint8_t)0x3); NONFAILING(*(uint8_t*)0x20000004 = (uint8_t)0x3); NONFAILING(*(uint8_t*)0x20000005 = (uint8_t)0xfff); NONFAILING(*(uint8_t*)0x20000006 = (uint8_t)0x401); NONFAILING(*(uint8_t*)0x20000007 = (uint8_t)0xef); NONFAILING(*(uint8_t*)0x20000008 = (uint8_t)0x9); r[10] = execute_syscall(__NR_connect, r[1], 0x20000000ul, 0xaul, 0, 0, 0, 0, 0, 0); break; case 3: NONFAILING(*(uint64_t*)0x20003000 = (uint64_t)0x20000000); NONFAILING(*(uint32_t*)0x20003008 = (uint32_t)0x60); NONFAILING(*(uint64_t*)0x20003010 = (uint64_t)0x20008000); NONFAILING(*(uint64_t*)0x20003018 = (uint64_t)0x0); NONFAILING(*(uint64_t*)0x20003020 = (uint64_t)0x20007000); NONFAILING(*(uint64_t*)0x20003028 = (uint64_t)0x10); NONFAILING(*(uint32_t*)0x20003030 = (uint32_t)0x5); NONFAILING(*(uint16_t*)0x20000000 = (uint16_t)0x27); NONFAILING(*(uint32_t*)0x20000004 = (uint32_t)0x0); NONFAILING(*(uint32_t*)0x20000008 = (uint32_t)0x0); NONFAILING(*(uint32_t*)0x2000000c = (uint32_t)0x0); NONFAILING(*(uint8_t*)0x20000010 = (uint8_t)0x0); NONFAILING(*(uint8_t*)0x20000011 = (uint8_t)0x0); NONFAILING(memcpy((void*)0x20000012, "\x4e\xa6\xf0\xe3\x26\x38\x41\x0f\x61\xc3\x84\x5a" "\xd3\xe8\x12\x96\xe2\x97\x1a\x49\xc7\x07\x55\xb5" "\x82\x0c\x91\x5b\xe8\xe3\xa2\x1e\x49\x17\x62\x02" "\x27\x36\xcd\x99\x61\x46\xb6\xa2\x93\x9e\xee\x01" "\xc9\xcb\xf2\xeb\x94\x9c\x90\xc2\x85\x56\x30\xea" "\x2a\x71\xf9", 63)); NONFAILING(*(uint64_t*)0x20000058 = (uint64_t)0x0); NONFAILING(*(uint64_t*)0x20007000 = (uint64_t)0x10); NONFAILING(*(uint32_t*)0x20007008 = (uint32_t)0x0); NONFAILING(*(uint32_t*)0x2000700c = (uint32_t)0x0); r[29] = execute_syscall(__NR_sendmsg, r[1], 0x20003000ul, 0x40880ul, 0, 0, 0, 0, 0, 0); break; } return 0; } void loop() { long i; pthread_t th[8]; memset(r, -1, sizeof(r)); srand(getpid()); for (i = 0; i < 4; i++) { pthread_create(&th[i], 0, thr, (void*)i); usleep(10000); } for (i = 0; i < 4; i++) { pthread_create(&th[4 + i], 0, thr, (void*)i); if (rand() % 2) usleep(rand() % 10000); } usleep(100000); } int main() { setup_main_process(); int pid = do_sandbox_none(0, false); int status = 0; while (waitpid(pid, &status, __WALL) != pid) { } return 0; }