// https://syzkaller.appspot.com/bug?id=fb3d5ed66d40353df9475fbbd7e4dd0b34cb915a // 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 #include #include #include #include #include #ifndef __NR_dup #define __NR_dup 23 #endif #ifndef __NR_memfd_create #define __NR_memfd_create 279 #endif #ifndef __NR_mmap #define __NR_mmap 222 #endif #ifndef __NR_mount #define __NR_mount 40 #endif #ifndef __NR_openat #define __NR_openat 56 #endif #ifndef __NR_pipe2 #define __NR_pipe2 59 #endif #ifndef __NR_write #define __NR_write 64 #endif static unsigned long long procid; 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_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(...) \ ({ \ 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; \ }) 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 thread_start(void* (*fn)(void*), void* arg) { pthread_t th; pthread_attr_t attr; pthread_attr_init(&attr); pthread_attr_setstacksize(&attr, 128 << 10); int i = 0; for (; i < 100; i++) { if (pthread_create(&th, &attr, fn, arg) == 0) { pthread_attr_destroy(&attr); return; } if (errno == EAGAIN) { usleep(50); continue; } break; } exit(1); } typedef struct { int state; } event_t; static void event_init(event_t* ev) { ev->state = 0; } static void event_reset(event_t* ev) { ev->state = 0; } static void event_set(event_t* ev) { if (ev->state) exit(1); __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE); syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000); } static void event_wait(event_t* ev) { while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE)) syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0); } static int event_isset(event_t* ev) { return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE); } static int event_timedwait(event_t* ev, uint64_t timeout) { uint64_t start = current_time_ms(); uint64_t now = start; for (;;) { uint64_t remain = timeout - (now - start); struct timespec ts; ts.tv_sec = remain / 1000; ts.tv_nsec = (remain % 1000) * 1000 * 1000; syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts); if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE)) return 1; now = current_time_ms(); if (now - start > timeout) return 0; } } static bool write_file(const char* file, const char* what, ...) { char buf[1024]; va_list args; va_start(args, what); vsnprintf(buf, sizeof(buf), what, args); va_end(args); buf[sizeof(buf) - 1] = 0; int len = strlen(buf); int fd = open(file, O_WRONLY | O_CLOEXEC); if (fd == -1) return false; if (write(fd, buf, len) != len) { int err = errno; close(fd); errno = err; return false; } close(fd); return true; } #define MAX_FDS 30 struct fs_image_segment { void* data; uintptr_t size; uintptr_t offset; }; static int setup_loop_device(long unsigned size, long unsigned nsegs, struct fs_image_segment* segs, const char* loopname, int* memfd_p, int* loopfd_p) { int err = 0, loopfd = -1; int memfd = syscall(__NR_memfd_create, "syzkaller", 0); if (memfd == -1) { err = errno; goto error; } if (ftruncate(memfd, size)) { err = errno; goto error_close_memfd; } for (size_t i = 0; i < nsegs; i++) { if (pwrite(memfd, segs[i].data, segs[i].size, segs[i].offset) < 0) { } } loopfd = open(loopname, O_RDWR); if (loopfd == -1) { err = errno; goto error_close_memfd; } if (ioctl(loopfd, LOOP_SET_FD, memfd)) { if (errno != EBUSY) { err = errno; goto error_close_loop; } ioctl(loopfd, LOOP_CLR_FD, 0); usleep(1000); if (ioctl(loopfd, LOOP_SET_FD, memfd)) { err = errno; goto error_close_loop; } } *memfd_p = memfd; *loopfd_p = loopfd; return 0; error_close_loop: close(loopfd); error_close_memfd: close(memfd); error: errno = err; return -1; } static long syz_mount_image(volatile long fsarg, volatile long dir, volatile unsigned long size, volatile unsigned long nsegs, volatile long segments, volatile long flags, volatile long optsarg, volatile long change_dir) { struct fs_image_segment* segs = (struct fs_image_segment*)segments; int res = -1, err = 0, loopfd = -1, memfd = -1, need_loop_device = !!segs; char* mount_opts = (char*)optsarg; char* target = (char*)dir; char* fs = (char*)fsarg; char* source = NULL; char loopname[64]; if (need_loop_device) { memset(loopname, 0, sizeof(loopname)); snprintf(loopname, sizeof(loopname), "/dev/loop%llu", procid); if (setup_loop_device(size, nsegs, segs, loopname, &memfd, &loopfd) == -1) return -1; source = loopname; } mkdir(target, 0777); char opts[256]; memset(opts, 0, sizeof(opts)); if (strlen(mount_opts) > (sizeof(opts) - 32)) { } strncpy(opts, mount_opts, sizeof(opts) - 32); if (strcmp(fs, "iso9660") == 0) { flags |= MS_RDONLY; } else if (strncmp(fs, "ext", 3) == 0) { if (strstr(opts, "errors=panic") || strstr(opts, "errors=remount-ro") == 0) strcat(opts, ",errors=continue"); } else if (strcmp(fs, "xfs") == 0) { strcat(opts, ",nouuid"); } res = mount(source, target, fs, flags, opts); if (res == -1) { err = errno; goto error_clear_loop; } res = open(target, O_RDONLY | O_DIRECTORY); if (res == -1) { err = errno; goto error_clear_loop; } if (change_dir) { res = chdir(target); if (res == -1) { err = errno; } } error_clear_loop: if (need_loop_device) { ioctl(loopfd, LOOP_CLR_FD, 0); close(loopfd); close(memfd); } errno = err; return res; } #define XT_TABLE_SIZE 1536 #define XT_MAX_ENTRIES 10 struct xt_counters { uint64_t pcnt, bcnt; }; struct ipt_getinfo { char name[32]; unsigned int valid_hooks; unsigned int hook_entry[5]; unsigned int underflow[5]; unsigned int num_entries; unsigned int size; }; struct ipt_get_entries { char name[32]; unsigned int size; uint64_t entrytable[XT_TABLE_SIZE / sizeof(uint64_t)]; }; struct ipt_replace { char name[32]; unsigned int valid_hooks; unsigned int num_entries; unsigned int size; unsigned int hook_entry[5]; unsigned int underflow[5]; unsigned int num_counters; struct xt_counters* counters; uint64_t entrytable[XT_TABLE_SIZE / sizeof(uint64_t)]; }; struct ipt_table_desc { const char* name; struct ipt_getinfo info; struct ipt_replace replace; }; static struct ipt_table_desc ipv4_tables[] = { {.name = "filter"}, {.name = "nat"}, {.name = "mangle"}, {.name = "raw"}, {.name = "security"}, }; static struct ipt_table_desc ipv6_tables[] = { {.name = "filter"}, {.name = "nat"}, {.name = "mangle"}, {.name = "raw"}, {.name = "security"}, }; #define IPT_BASE_CTL 64 #define IPT_SO_SET_REPLACE (IPT_BASE_CTL) #define IPT_SO_GET_INFO (IPT_BASE_CTL) #define IPT_SO_GET_ENTRIES (IPT_BASE_CTL + 1) struct arpt_getinfo { char name[32]; unsigned int valid_hooks; unsigned int hook_entry[3]; unsigned int underflow[3]; unsigned int num_entries; unsigned int size; }; struct arpt_get_entries { char name[32]; unsigned int size; uint64_t entrytable[XT_TABLE_SIZE / sizeof(uint64_t)]; }; struct arpt_replace { char name[32]; unsigned int valid_hooks; unsigned int num_entries; unsigned int size; unsigned int hook_entry[3]; unsigned int underflow[3]; unsigned int num_counters; struct xt_counters* counters; uint64_t entrytable[XT_TABLE_SIZE / sizeof(uint64_t)]; }; struct arpt_table_desc { const char* name; struct arpt_getinfo info; struct arpt_replace replace; }; static struct arpt_table_desc arpt_tables[] = { {.name = "filter"}, }; #define ARPT_BASE_CTL 96 #define ARPT_SO_SET_REPLACE (ARPT_BASE_CTL) #define ARPT_SO_GET_INFO (ARPT_BASE_CTL) #define ARPT_SO_GET_ENTRIES (ARPT_BASE_CTL + 1) static void checkpoint_iptables(struct ipt_table_desc* tables, int num_tables, int family, int level) { int fd = socket(family, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) { switch (errno) { case EAFNOSUPPORT: case ENOPROTOOPT: return; } exit(1); } for (int i = 0; i < num_tables; i++) { struct ipt_table_desc* table = &tables[i]; strcpy(table->info.name, table->name); strcpy(table->replace.name, table->name); socklen_t optlen = sizeof(table->info); if (getsockopt(fd, level, IPT_SO_GET_INFO, &table->info, &optlen)) { switch (errno) { case EPERM: case ENOENT: case ENOPROTOOPT: continue; } exit(1); } if (table->info.size > sizeof(table->replace.entrytable)) exit(1); if (table->info.num_entries > XT_MAX_ENTRIES) exit(1); struct ipt_get_entries entries; memset(&entries, 0, sizeof(entries)); strcpy(entries.name, table->name); entries.size = table->info.size; optlen = sizeof(entries) - sizeof(entries.entrytable) + table->info.size; if (getsockopt(fd, level, IPT_SO_GET_ENTRIES, &entries, &optlen)) exit(1); table->replace.valid_hooks = table->info.valid_hooks; table->replace.num_entries = table->info.num_entries; table->replace.size = table->info.size; memcpy(table->replace.hook_entry, table->info.hook_entry, sizeof(table->replace.hook_entry)); memcpy(table->replace.underflow, table->info.underflow, sizeof(table->replace.underflow)); memcpy(table->replace.entrytable, entries.entrytable, table->info.size); } close(fd); } static void reset_iptables(struct ipt_table_desc* tables, int num_tables, int family, int level) { int fd = socket(family, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) { switch (errno) { case EAFNOSUPPORT: case ENOPROTOOPT: return; } exit(1); } for (int i = 0; i < num_tables; i++) { struct ipt_table_desc* table = &tables[i]; if (table->info.valid_hooks == 0) continue; struct ipt_getinfo info; memset(&info, 0, sizeof(info)); strcpy(info.name, table->name); socklen_t optlen = sizeof(info); if (getsockopt(fd, level, IPT_SO_GET_INFO, &info, &optlen)) exit(1); if (memcmp(&table->info, &info, sizeof(table->info)) == 0) { struct ipt_get_entries entries; memset(&entries, 0, sizeof(entries)); strcpy(entries.name, table->name); entries.size = table->info.size; optlen = sizeof(entries) - sizeof(entries.entrytable) + entries.size; if (getsockopt(fd, level, IPT_SO_GET_ENTRIES, &entries, &optlen)) exit(1); if (memcmp(table->replace.entrytable, entries.entrytable, table->info.size) == 0) continue; } struct xt_counters counters[XT_MAX_ENTRIES]; table->replace.num_counters = info.num_entries; table->replace.counters = counters; optlen = sizeof(table->replace) - sizeof(table->replace.entrytable) + table->replace.size; if (setsockopt(fd, level, IPT_SO_SET_REPLACE, &table->replace, optlen)) exit(1); } close(fd); } static void checkpoint_arptables(void) { int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) { switch (errno) { case EAFNOSUPPORT: case ENOPROTOOPT: return; } exit(1); } for (unsigned i = 0; i < sizeof(arpt_tables) / sizeof(arpt_tables[0]); i++) { struct arpt_table_desc* table = &arpt_tables[i]; strcpy(table->info.name, table->name); strcpy(table->replace.name, table->name); socklen_t optlen = sizeof(table->info); if (getsockopt(fd, SOL_IP, ARPT_SO_GET_INFO, &table->info, &optlen)) { switch (errno) { case EPERM: case ENOENT: case ENOPROTOOPT: continue; } exit(1); } if (table->info.size > sizeof(table->replace.entrytable)) exit(1); if (table->info.num_entries > XT_MAX_ENTRIES) exit(1); struct arpt_get_entries entries; memset(&entries, 0, sizeof(entries)); strcpy(entries.name, table->name); entries.size = table->info.size; optlen = sizeof(entries) - sizeof(entries.entrytable) + table->info.size; if (getsockopt(fd, SOL_IP, ARPT_SO_GET_ENTRIES, &entries, &optlen)) exit(1); table->replace.valid_hooks = table->info.valid_hooks; table->replace.num_entries = table->info.num_entries; table->replace.size = table->info.size; memcpy(table->replace.hook_entry, table->info.hook_entry, sizeof(table->replace.hook_entry)); memcpy(table->replace.underflow, table->info.underflow, sizeof(table->replace.underflow)); memcpy(table->replace.entrytable, entries.entrytable, table->info.size); } close(fd); } static void reset_arptables() { int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) { switch (errno) { case EAFNOSUPPORT: case ENOPROTOOPT: return; } exit(1); } for (unsigned i = 0; i < sizeof(arpt_tables) / sizeof(arpt_tables[0]); i++) { struct arpt_table_desc* table = &arpt_tables[i]; if (table->info.valid_hooks == 0) continue; struct arpt_getinfo info; memset(&info, 0, sizeof(info)); strcpy(info.name, table->name); socklen_t optlen = sizeof(info); if (getsockopt(fd, SOL_IP, ARPT_SO_GET_INFO, &info, &optlen)) exit(1); if (memcmp(&table->info, &info, sizeof(table->info)) == 0) { struct arpt_get_entries entries; memset(&entries, 0, sizeof(entries)); strcpy(entries.name, table->name); entries.size = table->info.size; optlen = sizeof(entries) - sizeof(entries.entrytable) + entries.size; if (getsockopt(fd, SOL_IP, ARPT_SO_GET_ENTRIES, &entries, &optlen)) exit(1); if (memcmp(table->replace.entrytable, entries.entrytable, table->info.size) == 0) continue; } else { } struct xt_counters counters[XT_MAX_ENTRIES]; table->replace.num_counters = info.num_entries; table->replace.counters = counters; optlen = sizeof(table->replace) - sizeof(table->replace.entrytable) + table->replace.size; if (setsockopt(fd, SOL_IP, ARPT_SO_SET_REPLACE, &table->replace, optlen)) exit(1); } close(fd); } #define NF_BR_NUMHOOKS 6 #define EBT_TABLE_MAXNAMELEN 32 #define EBT_CHAIN_MAXNAMELEN 32 #define EBT_BASE_CTL 128 #define EBT_SO_SET_ENTRIES (EBT_BASE_CTL) #define EBT_SO_GET_INFO (EBT_BASE_CTL) #define EBT_SO_GET_ENTRIES (EBT_SO_GET_INFO + 1) #define EBT_SO_GET_INIT_INFO (EBT_SO_GET_ENTRIES + 1) #define EBT_SO_GET_INIT_ENTRIES (EBT_SO_GET_INIT_INFO + 1) struct ebt_replace { char name[EBT_TABLE_MAXNAMELEN]; unsigned int valid_hooks; unsigned int nentries; unsigned int entries_size; struct ebt_entries* hook_entry[NF_BR_NUMHOOKS]; unsigned int num_counters; struct ebt_counter* counters; char* entries; }; struct ebt_entries { unsigned int distinguisher; char name[EBT_CHAIN_MAXNAMELEN]; unsigned int counter_offset; int policy; unsigned int nentries; char data[0] __attribute__((aligned(__alignof__(struct ebt_replace)))); }; struct ebt_table_desc { const char* name; struct ebt_replace replace; char entrytable[XT_TABLE_SIZE]; }; static struct ebt_table_desc ebt_tables[] = { {.name = "filter"}, {.name = "nat"}, {.name = "broute"}, }; static void checkpoint_ebtables(void) { int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) { switch (errno) { case EAFNOSUPPORT: case ENOPROTOOPT: return; } exit(1); } for (size_t i = 0; i < sizeof(ebt_tables) / sizeof(ebt_tables[0]); i++) { struct ebt_table_desc* table = &ebt_tables[i]; strcpy(table->replace.name, table->name); socklen_t optlen = sizeof(table->replace); if (getsockopt(fd, SOL_IP, EBT_SO_GET_INIT_INFO, &table->replace, &optlen)) { switch (errno) { case EPERM: case ENOENT: case ENOPROTOOPT: continue; } exit(1); } if (table->replace.entries_size > sizeof(table->entrytable)) exit(1); table->replace.num_counters = 0; table->replace.entries = table->entrytable; optlen = sizeof(table->replace) + table->replace.entries_size; if (getsockopt(fd, SOL_IP, EBT_SO_GET_INIT_ENTRIES, &table->replace, &optlen)) exit(1); } close(fd); } static void reset_ebtables() { int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); if (fd == -1) { switch (errno) { case EAFNOSUPPORT: case ENOPROTOOPT: return; } exit(1); } for (unsigned i = 0; i < sizeof(ebt_tables) / sizeof(ebt_tables[0]); i++) { struct ebt_table_desc* table = &ebt_tables[i]; if (table->replace.valid_hooks == 0) continue; struct ebt_replace replace; memset(&replace, 0, sizeof(replace)); strcpy(replace.name, table->name); socklen_t optlen = sizeof(replace); if (getsockopt(fd, SOL_IP, EBT_SO_GET_INFO, &replace, &optlen)) exit(1); replace.num_counters = 0; table->replace.entries = 0; for (unsigned h = 0; h < NF_BR_NUMHOOKS; h++) table->replace.hook_entry[h] = 0; if (memcmp(&table->replace, &replace, sizeof(table->replace)) == 0) { char entrytable[XT_TABLE_SIZE]; memset(&entrytable, 0, sizeof(entrytable)); replace.entries = entrytable; optlen = sizeof(replace) + replace.entries_size; if (getsockopt(fd, SOL_IP, EBT_SO_GET_ENTRIES, &replace, &optlen)) exit(1); if (memcmp(table->entrytable, entrytable, replace.entries_size) == 0) continue; } for (unsigned j = 0, h = 0; h < NF_BR_NUMHOOKS; h++) { if (table->replace.valid_hooks & (1 << h)) { table->replace.hook_entry[h] = (struct ebt_entries*)table->entrytable + j; j++; } } table->replace.entries = table->entrytable; optlen = sizeof(table->replace) + table->replace.entries_size; if (setsockopt(fd, SOL_IP, EBT_SO_SET_ENTRIES, &table->replace, optlen)) exit(1); } close(fd); } static void checkpoint_net_namespace(void) { checkpoint_ebtables(); checkpoint_arptables(); checkpoint_iptables(ipv4_tables, sizeof(ipv4_tables) / sizeof(ipv4_tables[0]), AF_INET, SOL_IP); checkpoint_iptables(ipv6_tables, sizeof(ipv6_tables) / sizeof(ipv6_tables[0]), AF_INET6, SOL_IPV6); } static void reset_net_namespace(void) { reset_ebtables(); reset_arptables(); reset_iptables(ipv4_tables, sizeof(ipv4_tables) / sizeof(ipv4_tables[0]), AF_INET, SOL_IP); reset_iptables(ipv6_tables, sizeof(ipv6_tables) / sizeof(ipv6_tables[0]), AF_INET6, SOL_IPV6); } static void setup_common() { if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) { } } static void setup_binderfs() { if (mkdir("/dev/binderfs", 0777)) { } if (mount("binder", "/dev/binderfs", "binder", 0, NULL)) { } if (symlink("/dev/binderfs", "./binderfs")) { } } static void loop(); static void sandbox_common() { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); setsid(); struct rlimit rlim; rlim.rlim_cur = rlim.rlim_max = (200 << 20); setrlimit(RLIMIT_AS, &rlim); rlim.rlim_cur = rlim.rlim_max = 32 << 20; setrlimit(RLIMIT_MEMLOCK, &rlim); rlim.rlim_cur = rlim.rlim_max = 136 << 20; setrlimit(RLIMIT_FSIZE, &rlim); rlim.rlim_cur = rlim.rlim_max = 1 << 20; setrlimit(RLIMIT_STACK, &rlim); rlim.rlim_cur = rlim.rlim_max = 128 << 20; setrlimit(RLIMIT_CORE, &rlim); rlim.rlim_cur = rlim.rlim_max = 256; setrlimit(RLIMIT_NOFILE, &rlim); if (unshare(CLONE_NEWNS)) { } if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) { } if (unshare(CLONE_NEWIPC)) { } if (unshare(0x02000000)) { } if (unshare(CLONE_NEWUTS)) { } if (unshare(CLONE_SYSVSEM)) { } typedef struct { const char* name; const char* value; } sysctl_t; static const sysctl_t sysctls[] = { {"/proc/sys/kernel/shmmax", "16777216"}, {"/proc/sys/kernel/shmall", "536870912"}, {"/proc/sys/kernel/shmmni", "1024"}, {"/proc/sys/kernel/msgmax", "8192"}, {"/proc/sys/kernel/msgmni", "1024"}, {"/proc/sys/kernel/msgmnb", "1024"}, {"/proc/sys/kernel/sem", "1024 1048576 500 1024"}, }; unsigned i; for (i = 0; i < sizeof(sysctls) / sizeof(sysctls[0]); i++) write_file(sysctls[i].name, sysctls[i].value); } static int wait_for_loop(int pid) { if (pid < 0) exit(1); int status = 0; while (waitpid(-1, &status, __WALL) != pid) { } return WEXITSTATUS(status); } static void drop_caps(void) { struct __user_cap_header_struct cap_hdr = {}; struct __user_cap_data_struct cap_data[2] = {}; cap_hdr.version = _LINUX_CAPABILITY_VERSION_3; cap_hdr.pid = getpid(); if (syscall(SYS_capget, &cap_hdr, &cap_data)) exit(1); const int drop = (1 << CAP_SYS_PTRACE) | (1 << CAP_SYS_NICE); cap_data[0].effective &= ~drop; cap_data[0].permitted &= ~drop; cap_data[0].inheritable &= ~drop; if (syscall(SYS_capset, &cap_hdr, &cap_data)) exit(1); } static int do_sandbox_none(void) { if (unshare(CLONE_NEWPID)) { } int pid = fork(); if (pid != 0) return wait_for_loop(pid); setup_common(); sandbox_common(); drop_caps(); if (unshare(CLONE_NEWNET)) { } write_file("/proc/sys/net/ipv4/ping_group_range", "0 65535"); setup_binderfs(); loop(); exit(1); } static void kill_and_wait(int pid, int* status) { kill(-pid, SIGKILL); kill(pid, SIGKILL); for (int i = 0; i < 100; i++) { if (waitpid(-1, status, WNOHANG | __WALL) == pid) return; usleep(1000); } DIR* dir = opendir("/sys/fs/fuse/connections"); if (dir) { for (;;) { struct dirent* ent = readdir(dir); if (!ent) break; if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue; char abort[300]; snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort", ent->d_name); int fd = open(abort, O_WRONLY); if (fd == -1) { continue; } if (write(fd, abort, 1) < 0) { } close(fd); } closedir(dir); } else { } while (waitpid(-1, status, __WALL) != pid) { } } static void setup_loop() { checkpoint_net_namespace(); } static void reset_loop() { char buf[64]; snprintf(buf, sizeof(buf), "/dev/loop%llu", procid); int loopfd = open(buf, O_RDWR); if (loopfd != -1) { ioctl(loopfd, LOOP_CLR_FD, 0); close(loopfd); } reset_net_namespace(); } static void setup_test() { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); setpgrp(); write_file("/proc/self/oom_score_adj", "1000"); } static void close_fds() { for (int fd = 3; fd < MAX_FDS; fd++) close(fd); } static void setup_usb() { if (chmod("/dev/raw-gadget", 0666)) exit(1); } static void setup_sysctl() { char mypid[32]; snprintf(mypid, sizeof(mypid), "%d", getpid()); struct { const char* name; const char* data; } files[] = { {"/proc/sys/kernel/hung_task_check_interval_secs", "20"}, {"/proc/sys/net/core/bpf_jit_kallsyms", "1"}, {"/proc/sys/net/core/bpf_jit_harden", "0"}, {"/proc/sys/kernel/kptr_restrict", "0"}, {"/proc/sys/kernel/softlockup_all_cpu_backtrace", "1"}, {"/proc/sys/fs/mount-max", "100"}, {"/proc/sys/vm/oom_dump_tasks", "0"}, {"/proc/sys/debug/exception-trace", "0"}, {"/proc/sys/kernel/printk", "7 4 1 3"}, {"/proc/sys/kernel/keys/gc_delay", "1"}, {"/proc/sys/vm/oom_kill_allocating_task", "1"}, {"/proc/sys/kernel/ctrl-alt-del", "0"}, {"/proc/sys/kernel/cad_pid", mypid}, }; for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); i++) { if (!write_file(files[i].name, files[i].data)) printf("write to %s failed: %s\n", files[i].name, strerror(errno)); } } 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; for (call = 0; call < 10; 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 (call == 2 || call == 4 || call == 6 || call == 8) break; event_timedwait(&th->done, 50 + (call == 2 ? 50 : 0)); break; } } for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++) sleep_ms(1); close_fds(); } static void execute_one(void); #define WAIT_FLAGS __WALL static void loop(void) { setup_loop(); int iter = 0; for (;; iter++) { reset_loop(); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { setup_test(); 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 < 5000) continue; kill_and_wait(pid, &status); break; } } } uint64_t r[4] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: NONFAILING(memcpy((void*)0x20000000, "/proc/self/exe\000", 15)); res = syscall(__NR_openat, 0xffffff9c, 0x20000000ul, 0ul, 0ul); if (res != -1) r[0] = res; break; case 1: syscall(__NR_mmap, 0x20000000ul, 0x800000ul, 0x1800003ul, 0x12ul, r[0], 0ul); break; case 2: NONFAILING(memcpy((void*)0x20000080, "./file0\000", 8)); NONFAILING(*(uint64_t*)0x20001740 = 0x20000240); NONFAILING(memcpy((void*)0x20000240, "\xd7\x1d\xeb\x19\xd2", 5)); NONFAILING(*(uint64_t*)0x20001748 = 5); NONFAILING(*(uint64_t*)0x20001750 = 3); NONFAILING(*(uint64_t*)0x20001758 = 0x200006c0); NONFAILING(memcpy( (void*)0x200006c0, "\xa5\xe6\x36\x36\xf3\xcb\xeb\xef\x08\x63\x8e\xdd\x8f\x63\xf5\x09\x7f" "\xc1\xce\xbd\xa2\x35\xfd\x1c\xc2\x6a\xe3\x4c\xa5\xa4\xe9\x53\x0f\xa8" "\x9c\x2c\xb4\x0a\xf5\x08\xb7\x10\x79\x52\x4e\x36\x2c\xbc\x25\xf7\x2a" "\x6a\xab\x12\x19\x8a\xa8\xd6\xd9\x5a\x0a\xf7\x7d\x00\x22\xcf\xea\x38" "\x19\xba\x0a\xe8\x5f\x63\x96\x80\x34\xb8\x22\x5d\x84\xdc\x49\xd0\x53" "\xd9\xbc\xe4\x07\xf7\xe1\x19\xba\x5e\x70\x9c\x01\x41\xc4\xcc\x82\xc2" "\x46\xb2\x36\xa8\x23\xcd\x66\x42\xa5\x1e\xd6\x47\xca\x7c\x3e\x10\xb3" "\x16\x2f\x40\xa2\xca\x2e\xd2\x53\x68\x19\xee\x8f\x16\x36\x3e\xb0\xab" "\x34\x72\x61\x83\x95\x88\x1a\x82\xda\x70\xfc\xf5\xbb\xa5\x90\xa0\x55" "\x3a\xe6\x1e\x9f\xd2\xae\x41\xfa\xaf\x8a\x1f\xfb\x0e\x38\x9e\x49\xf0" "\xf3\xe4\xeb\xa5\x5a\x55\xf1\x2c\x80\xfb\x58\x9b\xd5\xf5\x6a\xb8\xac" "\x9f\xf9\x7c\x4c\x09\x6d\xc5\xad\x38\x50\x72\x2e\x01\x2a\x3a\x73\x6d" "\x75\x81\xe0\x89\x67\x49\x2f\x89\xbf\x4f\x63\x7a\x69\xe9\xd7\x26\x2a" "\x96\x65\x91\xd7\xac\x46\x9e\xc8\x13\x80\xab\xe8\x14\x36\x3d\xae\x93" "\x35\xa1\x19\x93\x91\xb7\xa3\x1d\x1c\x6c", 248)); NONFAILING(*(uint64_t*)0x20001760 = 0xf8); NONFAILING(*(uint64_t*)0x20001768 = 8); NONFAILING(*(uint64_t*)0x20001770 = 0x20000400); NONFAILING(memcpy( (void*)0x20000400, "\x46\xc0\xfa\xf9\xb7\xdb\xe0\x3f\xe5\x61\x8f\x08\x08\x87\x2d\x1e\xd2" "\xfe\xba\x1e\xbd\x1f\xef\x15\x72\x82\x6e\x65\x36\xee\x66\x97\xce\xb5" "\x6f\x70\x14\x78\x75\xf3\xbc\xa0\x89\xdb\xea\xb0\xd0\x4b\x8a\x0c\xcc" "\xb9\x3d\xb4\x77\x27\x60\x64\x06\x18\x0d\xf4\xa8\x4b\x07\x44\x80\xe1" "\xc5\xa1\x0f\x40\xd6\x3e\x3a\x5b\x20\x59\x13\xcf\x33\x62\x42\xda\x4c" "\xb2\xa7\xed\xa7\xf9\x54\xf0\x35\x6b\x6a\xc4\xc8\x86\xf5\xf3\x9b\x0e" "\xd9\xc6\xde\x8c\x96\xc1\xeb\xab\x18\xe3\x9c\x89\x9d\x6e\x5b\xee\xb1" "\xbe\x0c\xd9\x98\x9a\xff\x58\x8c\xa1\x66\xf7\xba\xbb\x20\xa4\x58\xf0" "\x76\xb0\x41\x34\x01\x19\x39\xf2\xf7\xbd\x0d\x8e\x81\x65\x39\x08\x51" "\x63\xe2\xd2\x21\x7e\x88\x5e\x92\x13\x1b\x0f\x17\x99\x66\xdc\xa6\xe0" "\x26\xe2\xac\x15\xf4\x5f", 176)); NONFAILING(*(uint64_t*)0x20001778 = 0xb0); NONFAILING(*(uint64_t*)0x20001780 = 0x100); NONFAILING(*(uint64_t*)0x20001788 = 0x200004c0); NONFAILING(memcpy( (void*)0x200004c0, "\x82\xc8\x96\x0c\x0b\x1a\x3f\xc1\xbb\x5a\xff\x64\x1e\xda\xcc\xd6\xbd" "\x08\x8e\xa8\x63\x00\xc8\xe0\xce\x55\xb2\x47\x9f\xc2\x55\x1d\x9e\xe1" "\x95\x7c\x06\x01\x2e\xb5\x59\xc3\x39\xc2\x98\xa8\xc5\x3b\xa2\xf8\x69" "\x9c\x59\xbe\xad\xba\x63\x8b\xc7\xb3\x0e\xfa\x8e\x86\x25\x0c\x5c\xb5" "\xdf\xa3\xfb\x38\x2e\xfc\x0d\x45\x82\xb1\x5a\x84\x06\x73\x51\x14\x3f" "\xd5\xd5\x38\xec\xc5\x00\x33\x42\xc4\x3b\xbc\x91\xd9\x47\xf4\x6b\xe1" "\x5c\x12\xbe\xed\x28\x57\x02\x2f\xed\xcd\x78\xd2\x80\xbf\xf4\x5f\xd3" "\x78\x6e\x8a\x65\x0b\x08\x76\xa7\x1b\x53\xf5\x55\x3b\x96\xae\x60\xb6" "\xbe\xd1\x41\xdd\x47\x21\x34\xd2\x89\xb8\xa5\x5e\xbb\xb9\x44\xdf\x2c" "\x5b\x7c\xe3\xe1\x1e\x42\x4f\x93\x30\x36\xf9\xfc\xcb\x45\x55\x16\x5f" "\x32\x22\xa1\x25\x69\x1d\xac\xe3\x89\xb3\x71\x7b\x72\x02\x12\x52\x9b" "\x63\x7c\xdf\xc8\x4f\xa4\x62\xe7\xe3\xce\x14\xa1\xa7\x07\xf6\x03\xab" "\x6c\xf0\xf0\xca\x35\xe9\x27\x5f\x30\x6e\x47\x47\x16\x88\xe5\x11\x75" "\xaf\xc6\x3f\xac\x2d\xe5\x90\xe8\x18\xcd\x89\xda\x30\x4b\x7b\x48\xe5" "\xf1\xe5\x5b\xf1\x16\xc2\x0f\x1a\x70\xad\x75\x84\x50\xf0\xcb\xca", 254)); NONFAILING(*(uint64_t*)0x20001790 = 0xfe); NONFAILING(*(uint64_t*)0x20001798 = 0xfff); NONFAILING(*(uint64_t*)0x200017a0 = 0x200017c0); NONFAILING(memcpy( (void*)0x200017c0, "\xf0\x4b\x09\xef\x99\xfa\x47\x3c\xe0\xea\xa7\x0a\x6d\x2a\xf6\x33\x12" "\xe5\xe6\x8c\x29\xc9\xe1\xc5\x6f\x5a\xcc\x60\xd0\xf7\x3a\xf0\x26\x97" "\x80\xc6\xfd\xcc\x5d\xa2\x1d\x38\x96\xba\x8e\xb2\x47\x75\xa1\x36\xff" "\xb4\x41\xb6\x9d\x6e\x2d\x59\x78\x5e\x56\x75\x20\xa5\xde\xee\x19\x4e" "\x1f\xf4\xa6\x9b\x0d\xb1\xbe\x71\x24\xd0\xf2\x5a\xd4\x41\x3d\xac\x54" "\x06\x7d\xaa\x11\x5f\xe6\xb3\x36\x76\xcf\xdd\xde\xbf\x19\xe4\x09\x32" "\xfc\xe3\xf9\xdb\x80\xee\xb9\x76\xda\x46\x92\x01\x24\x85\xf6\x0c\x72" "\x9f\x02\x45\xed\xc6\x3c\x60\x7e\x3b\xbe\x29\x3f\xd6\xf0\x17\xd4\x30" "\xe4\xd0\xca\x1c\xb5\x9f\x9c\xc6\x6e\xab\x66\x1d\x48\x22\xc7\x03\xfa" "\x74\x99\xf8\x7b\x9c\xf5\x15\x65\x39\x3f\x2b\xb0\x46\x23\xf8\x8e\x02" "\x3b\x1d\xde\xf3\x51\xbb\x7b\xc1\x80\x23\xfb\xa8\x97\x18\x05\x2c\x86" "\xaa\x9c\xc3\x5f\xba\x36\xa9\xef\x68\x3f\x07\x3d\x5d\xae\x2b\x35\x38" "\xec\x1a\x4e\x24\x1c\x56\x21\xf4\xbd\x66\xae\xa0\x00\x93\x97\x6a\xa3" "\x26\xac\x10\x6e\x8d\xd9\x9d\x0f\xa1\xa9\x3b\xe3\x44\xf8\x04\xc8\xb1" "\x90\x32\xe2\x99\x66\xeb\x06\x00\x6a\x1f\x40\x2c\x42\xa9\x4c\x65\x00" "\xbb\x04\x45\x0f\x1e\x72\xca\x2d\x47\xe9\x2e\x4e\x23\x04\x8c\xab\x16" "\xe1\xa5\x22\xdd\x8b\xda\xd7\x65\x69\x86\xd9\x68\x91\xd4\x3c\xb4\xc5" "\x2f\x44\x1d\x0c\x5d\x1b\xe4\xd7\x7d\x4f\xe0\x2a\x12\x2b\xc5\x81\x6c" "\x10\x35\xd1\x4a\x3f\xc2\x82\xef\x82\xcf\x7d\xd3\xcc\x0a\x2f\x36\x96" "\x02\x73\x42\xac\x4a\x81\x51\xd3\x0c\xcb\x2a\x67\xa6\x88\x1e\x25\x16" "\x7d", 341)); NONFAILING(*(uint64_t*)0x200017a8 = 0x155); NONFAILING(*(uint64_t*)0x200017b0 = 0xa4c2); NONFAILING(*(uint64_t*)0x200017b8 = 0x20000340); NONFAILING(memcpy( (void*)0x20000340, "\x23\x88\xdc\x2a\xc6\xc4\x6f\x62\xb4\x3c\xe5\x7f\x44\x0c\x82\x60\x3d" "\xe8\x22\x56\x96\xf1\xf3\xcd\xe0\x2d\x9b\x3b\xb3\x75\x1d\xd8\x48\xc3" "\xcf\xf7\x39\xcb\x15\xba\x37\xeb\xc4\xba\x67\xa4\xb3\x16\xc5\x26\x75" "\x6f\x66\xc0\xe2\x64\x79\xa7\xd2\x2e\xd4\xe8\x4b\x6d", 64)); NONFAILING(*(uint64_t*)0x200017c0 = 0x40); NONFAILING(*(uint64_t*)0x200017c8 = 0xd998); NONFAILING( syz_mount_image(0, 0x20000080, 0xd9d8, 6, 0x20001740, 0x800, 0, 0)); break; case 3: res = syscall(__NR_pipe2, 0x20000180ul, 0ul); if (res != -1) { NONFAILING(r[1] = *(uint32_t*)0x20000180); NONFAILING(r[2] = *(uint32_t*)0x20000184); } break; case 4: NONFAILING(memcpy((void*)0x20000000, "\x15\x00\x00\x00\x65\xff\xff", 7)); syscall(__NR_write, r[2], 0x20000000ul, 0x15ul); break; case 5: res = syscall(__NR_dup, r[2]); if (res != -1) r[3] = res; break; case 6: NONFAILING(memcpy((void*)0x20000040, "./file0\000", 8)); NONFAILING(memcpy((void*)0x200001c0, "9p\000", 3)); NONFAILING(memcpy((void*)0x20000300, "trans=fd,", 9)); NONFAILING(memcpy((void*)0x20000309, "rfdno", 5)); NONFAILING(*(uint8_t*)0x2000030e = 0x3d); NONFAILING(sprintf((char*)0x2000030f, "0x%016llx", (long long)r[1])); NONFAILING(*(uint8_t*)0x20000321 = 0x2c); NONFAILING(memcpy((void*)0x20000322, "wfdno", 5)); NONFAILING(*(uint8_t*)0x20000327 = 0x3d); NONFAILING(sprintf((char*)0x20000328, "0x%016llx", (long long)r[3])); NONFAILING(*(uint8_t*)0x2000033a = 0x2c); NONFAILING(*(uint8_t*)0x2000033b = 0); syscall(__NR_mount, 0ul, 0x20000040ul, 0x200001c0ul, 0ul, 0x20000300ul); break; case 7: syscall(__NR_write, r[3], 0ul, 0ul); break; case 8: NONFAILING(memcpy((void*)0x20000200, "\xb0\x00\x00\x00\x00\x00\x00", 7)); NONFAILING(*(uint64_t*)0x20000207 = -1); syscall(__NR_write, r[3], 0x20000200ul, 0xb0ul); { int i; for (i = 0; i < 32; i++) { syscall(__NR_write, r[3], 0x20000200ul, 0xb0ul); } } break; case 9: NONFAILING(memcpy((void*)0x200002c0, "./file0\000", 8)); NONFAILING(memcpy((void*)0x200000c0, "9p\000", 3)); NONFAILING(memcpy((void*)0x200003c0, "trans=fd,", 9)); NONFAILING(memcpy((void*)0x200003c9, "rfdno", 5)); NONFAILING(*(uint8_t*)0x200003ce = 0x3d); NONFAILING(sprintf((char*)0x200003cf, "0x%016llx", (long long)r[1])); NONFAILING(*(uint8_t*)0x200003e1 = 0x2c); NONFAILING(memcpy((void*)0x200003e2, "wfdno", 5)); NONFAILING(*(uint8_t*)0x200003e7 = 0x3d); NONFAILING(sprintf((char*)0x200003e8, "0x%016llx", (long long)r[2])); NONFAILING(*(uint8_t*)0x200003fa = 0x2c); NONFAILING(*(uint8_t*)0x200003fb = 0); syscall(__NR_mount, 0ul, 0x200002c0ul, 0x200000c0ul, 0ul, 0x200003c0ul); { int i; for (i = 0; i < 32; i++) { syscall(__NR_mount, 0ul, 0x200002c0ul, 0x200000c0ul, 0ul, 0x200003c0ul); } } break; } } int main(void) { syscall(__NR_mmap, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul); syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul); syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul); setup_sysctl(); setup_usb(); install_segv_handler(); for (procid = 0; procid < 6; procid++) { if (fork() == 0) { do_sandbox_none(); } } sleep(1000000); return 0; }