// https://syzkaller.appspot.com/bug?id=af63f5e2fae13601dc704abdefe541a3aa6fb3a0 // 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 #include #include #include #include #include #include #include static __thread int clone_ongoing; static __thread int skip_segv; static __thread jmp_buf segv_env; static void segv_handler(int sig, siginfo_t* info, void* ctx) { if (__atomic_load_n(&clone_ongoing, __ATOMIC_RELAXED) != 0) { exit(sig); } uintptr_t addr = (uintptr_t)info->si_addr; const uintptr_t prog_start = 1 << 20; const uintptr_t prog_end = 100 << 20; int skip = __atomic_load_n(&skip_segv, __ATOMIC_RELAXED) != 0; int valid = addr < prog_start || addr > prog_end; if (skip && valid) { _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(...) ({ int ok = 1; __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); if (_setjmp(segv_env) == 0) { __VA_ARGS__; } else ok = 0; __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); ok; }) #define CAST 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 wait_for_loop(int pid) { if (pid < 0) exit(1); int status = 0; while (waitpid(-1, &status, WUNTRACED) != pid) { } return WEXITSTATUS(status); } static int do_sandbox_setuid(void) { int pid = fork(); if (pid != 0) return wait_for_loop(pid); sandbox_common(); char pwbuf[1024]; struct passwd *pw, pwres; if (getpwnam_r("nobody", &pwres, pwbuf, sizeof(pwbuf), &pw) != 0 || !pw) exit(1); if (setgroups(0, NULL)) exit(1); if (setgid(pw->pw_gid)) exit(1); if (setuid(pw->pw_uid)) exit(1); loop(); exit(1); } uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff}; void loop(void) { intptr_t res = 0; NONFAILING(*(uint32_t*)0x20000000 = 0); NONFAILING(*(uint32_t*)0x20000004 = 0); NONFAILING(*(uint64_t*)0x20000008 = 0); NONFAILING(*(uint64_t*)0x20000010 = 0x10000000000001); NONFAILING(((intptr_t(*)(intptr_t,intptr_t,intptr_t))CAST(ioctl))(/*fd=*/-1, /*cmd=*/0x80185760, /*arg=*/0x20000000)); NONFAILING(*(uint16_t*)0x200000c0 = 0); NONFAILING(*(uint16_t*)0x200000c2 = 0); NONFAILING(*(uint64_t*)0x200000c8 = 0); NONFAILING(*(uint64_t*)0x200000d0 = 0x1000080000800); NONFAILING(*(uint32_t*)0x200000d8 = 0); NONFAILING(((intptr_t(*)(intptr_t,intptr_t,intptr_t))CAST(fcntl))(/*fd=*/-1, /*cmd=*/0, /*lock=*/0x200000c0)); NONFAILING(*(uint16_t*)0x200000c0 = 0x18e6); NONFAILING(*(uint8_t*)0x200000c2 = 7); NONFAILING(*(uint32_t*)0x200000c4 = 0); NONFAILING(((intptr_t(*)(intptr_t,intptr_t,intptr_t))CAST(connect))(/*fd=*/-1, /*addr=*/0x200000c0, /*addrlen=*/0x1c)); NONFAILING(((intptr_t(*)(intptr_t,intptr_t,intptr_t))CAST(mprotect))(/*addr=*/0x20000000, /*len=*/0x800000, /*prot=*/5)); res = -1; NONFAILING(res = ((intptr_t(*)(intptr_t,intptr_t,intptr_t))CAST(socket))(/*domain=*/0x18, /*type=*/1, /*proto=*/0)); if (res != -1) r[0] = res; NONFAILING(((intptr_t(*)(intptr_t))CAST(close))(/*fd=*/r[0])); res = -1; NONFAILING(res = ((intptr_t(*)(intptr_t,intptr_t,intptr_t))CAST(socket))(/*domain=*/0x18, /*type=*/1, /*proto=*/0)); if (res != -1) r[1] = res; NONFAILING(memcpy((void*)0x20000000, "\xeb\xff\xcb\xff\x13\xb9\xfd\x81\x2e\xaa\x4e\x71\x30\x48\xe6\x99\x31\x92\x96\x48", 20)); NONFAILING(((intptr_t(*)(intptr_t,intptr_t,intptr_t,intptr_t,intptr_t))CAST(setsockopt))(/*fd=*/r[1], /*level=*/0x29, /*optname=*/0x2e, /*optval=*/0x20000000, /*optlen=*/0x14)); NONFAILING(*(uint16_t*)0x200000c0 = 0); NONFAILING(*(uint8_t*)0x200000c2 = 7); NONFAILING(*(uint32_t*)0x200000c4 = 0); NONFAILING(((intptr_t(*)(intptr_t,intptr_t,intptr_t))CAST(connect))(/*fd=*/r[0], /*addr=*/0x200000c0, /*addrlen=*/0x1c)); } int main(void) { NONFAILING(((intptr_t(*)(intptr_t,intptr_t,intptr_t,intptr_t,intptr_t,intptr_t))CAST(mmap))(/*addr=*/0x20000000, /*len=*/0x1000000, /*prot=*/3, /*flags=*/0x1012, /*fd=*/-1, /*offset=*/0)); install_segv_handler(); do_sandbox_setuid(); return 0; }