// https://syzkaller.appspot.com/bug?id=0614ec41aac6e534a0756a4405379ad5c2bb58ad // 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_bpf #define __NR_bpf 321 #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 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 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); } #define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off)) #define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len) \ *(type*)(addr) = \ htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) | \ (((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len)))) 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; } static int runcmdline(char* cmdline) { int ret = system(cmdline); if (ret) { } return ret; } #define MAX_FDS 30 #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: case ENOENT: 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: case ENOENT: 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: case ENOENT: 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: case ENOENT: 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: case ENOENT: 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: case ENOENT: 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 mount_cgroups(const char* dir, const char** controllers, int count) { if (mkdir(dir, 0777)) { return; } char enabled[128] = {0}; int i = 0; for (; i < count; i++) { if (mount("none", dir, "cgroup", 0, controllers[i])) { continue; } umount(dir); strcat(enabled, ","); strcat(enabled, controllers[i]); } if (enabled[0] == 0) { if (rmdir(dir) && errno != EBUSY) exit(1); return; } if (mount("none", dir, "cgroup", 0, enabled + 1)) { if (rmdir(dir) && errno != EBUSY) exit(1); } if (chmod(dir, 0777)) { } } static void mount_cgroups2(const char** controllers, int count) { if (mkdir("/syzcgroup/unified", 0777)) { return; } if (mount("none", "/syzcgroup/unified", "cgroup2", 0, NULL)) { if (rmdir("/syzcgroup/unified") && errno != EBUSY) exit(1); return; } if (chmod("/syzcgroup/unified", 0777)) { } int control = open("/syzcgroup/unified/cgroup.subtree_control", O_WRONLY); if (control == -1) return; int i; for (i = 0; i < count; i++) if (write(control, controllers[i], strlen(controllers[i])) < 0) { } close(control); } static void setup_cgroups() { const char* unified_controllers[] = {"+cpu", "+io", "+pids"}; const char* net_controllers[] = {"net", "net_prio", "devices", "blkio", "freezer"}; const char* cpu_controllers[] = {"cpuset", "cpuacct", "hugetlb", "rlimit", "memory"}; if (mkdir("/syzcgroup", 0777)) { return; } mount_cgroups2(unified_controllers, sizeof(unified_controllers) / sizeof(unified_controllers[0])); mount_cgroups("/syzcgroup/net", net_controllers, sizeof(net_controllers) / sizeof(net_controllers[0])); mount_cgroups("/syzcgroup/cpu", cpu_controllers, sizeof(cpu_controllers) / sizeof(cpu_controllers[0])); write_file("/syzcgroup/cpu/cgroup.clone_children", "1"); write_file("/syzcgroup/cpu/cpuset.memory_pressure_enabled", "1"); } static void setup_cgroups_loop() { int pid = getpid(); char file[128]; char cgroupdir[64]; snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/unified/syz%llu", procid); if (mkdir(cgroupdir, 0777)) { } snprintf(file, sizeof(file), "%s/pids.max", cgroupdir); write_file(file, "32"); snprintf(file, sizeof(file), "%s/cgroup.procs", cgroupdir); write_file(file, "%d", pid); snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/cpu/syz%llu", procid); if (mkdir(cgroupdir, 0777)) { } snprintf(file, sizeof(file), "%s/cgroup.procs", cgroupdir); write_file(file, "%d", pid); snprintf(file, sizeof(file), "%s/memory.soft_limit_in_bytes", cgroupdir); write_file(file, "%d", 299 << 20); snprintf(file, sizeof(file), "%s/memory.limit_in_bytes", cgroupdir); write_file(file, "%d", 300 << 20); snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/net/syz%llu", procid); if (mkdir(cgroupdir, 0777)) { } snprintf(file, sizeof(file), "%s/cgroup.procs", cgroupdir); write_file(file, "%d", pid); } static void setup_cgroups_test() { char cgroupdir[64]; snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/unified/syz%llu", procid); if (symlink(cgroupdir, "./cgroup")) { } snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/cpu/syz%llu", procid); if (symlink(cgroupdir, "./cgroup.cpu")) { } snprintf(cgroupdir, sizeof(cgroupdir), "/syzcgroup/net/syz%llu", procid); if (symlink(cgroupdir, "./cgroup.net")) { } } static void initialize_cgroups() { if (mkdir("./syz-tmp/newroot/syzcgroup", 0700)) exit(1); if (mkdir("./syz-tmp/newroot/syzcgroup/unified", 0700)) exit(1); if (mkdir("./syz-tmp/newroot/syzcgroup/cpu", 0700)) exit(1); if (mkdir("./syz-tmp/newroot/syzcgroup/net", 0700)) exit(1); unsigned bind_mount_flags = MS_BIND | MS_REC | MS_PRIVATE; if (mount("/syzcgroup/unified", "./syz-tmp/newroot/syzcgroup/unified", NULL, bind_mount_flags, NULL)) { } if (mount("/syzcgroup/cpu", "./syz-tmp/newroot/syzcgroup/cpu", NULL, bind_mount_flags, NULL)) { } if (mount("/syzcgroup/net", "./syz-tmp/newroot/syzcgroup/net", NULL, bind_mount_flags, NULL)) { } } static void setup_gadgetfs(); static void setup_binderfs(); static void setup_fusectl(); static void sandbox_common_mount_tmpfs(void) { write_file("/proc/sys/fs/mount-max", "100000"); if (mkdir("./syz-tmp", 0777)) exit(1); if (mount("", "./syz-tmp", "tmpfs", 0, NULL)) exit(1); if (mkdir("./syz-tmp/newroot", 0777)) exit(1); if (mkdir("./syz-tmp/newroot/dev", 0700)) exit(1); unsigned bind_mount_flags = MS_BIND | MS_REC | MS_PRIVATE; if (mount("/dev", "./syz-tmp/newroot/dev", NULL, bind_mount_flags, NULL)) exit(1); if (mkdir("./syz-tmp/newroot/proc", 0700)) exit(1); if (mount("syz-proc", "./syz-tmp/newroot/proc", "proc", 0, NULL)) exit(1); if (mkdir("./syz-tmp/newroot/selinux", 0700)) exit(1); const char* selinux_path = "./syz-tmp/newroot/selinux"; if (mount("/selinux", selinux_path, NULL, bind_mount_flags, NULL)) { if (errno != ENOENT) exit(1); if (mount("/sys/fs/selinux", selinux_path, NULL, bind_mount_flags, NULL) && errno != ENOENT) exit(1); } if (mkdir("./syz-tmp/newroot/sys", 0700)) exit(1); if (mount("/sys", "./syz-tmp/newroot/sys", 0, bind_mount_flags, NULL)) exit(1); if (mount("/sys/kernel/debug", "./syz-tmp/newroot/sys/kernel/debug", NULL, bind_mount_flags, NULL) && errno != ENOENT) exit(1); if (mount("/sys/fs/smackfs", "./syz-tmp/newroot/sys/fs/smackfs", NULL, bind_mount_flags, NULL) && errno != ENOENT) exit(1); if (mount("/proc/sys/fs/binfmt_misc", "./syz-tmp/newroot/proc/sys/fs/binfmt_misc", NULL, bind_mount_flags, NULL) && errno != ENOENT) exit(1); if (mkdir("./syz-tmp/newroot/syz-inputs", 0700)) exit(1); if (mount("/syz-inputs", "./syz-tmp/newroot/syz-inputs", NULL, bind_mount_flags | MS_RDONLY, NULL) && errno != ENOENT) exit(1); initialize_cgroups(); if (mkdir("./syz-tmp/pivot", 0777)) exit(1); if (syscall(SYS_pivot_root, "./syz-tmp", "./syz-tmp/pivot")) { if (chdir("./syz-tmp")) exit(1); } else { if (chdir("/")) exit(1); if (umount2("./pivot", MNT_DETACH)) exit(1); } if (chroot("./newroot")) exit(1); if (chdir("/")) exit(1); setup_gadgetfs(); setup_binderfs(); setup_fusectl(); } static void setup_gadgetfs() { if (mkdir("/dev/gadgetfs", 0777)) { } if (mount("gadgetfs", "/dev/gadgetfs", "gadgetfs", 0, NULL)) { } } static void setup_fusectl() { 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)) { } } static void loop(); static void sandbox_common() { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); if (getppid() == 1) exit(1); 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); sandbox_common(); drop_caps(); if (unshare(CLONE_NEWNET)) { } write_file("/proc/sys/net/ipv4/ping_group_range", "0 65535"); sandbox_common_mount_tmpfs(); loop(); exit(1); } #define FS_IOC_SETFLAGS _IOW('f', 2, long) static void remove_dir(const char* dir) { int iter = 0; DIR* dp = 0; const int umount_flags = MNT_FORCE | UMOUNT_NOFOLLOW; retry: while (umount2(dir, umount_flags) == 0) { } dp = opendir(dir); if (dp == NULL) { if (errno == EMFILE) { exit(1); } exit(1); } struct dirent* ep = 0; 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); while (umount2(filename, umount_flags) == 0) { } struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } int i; for (i = 0;; i++) { if (unlink(filename) == 0) break; if (errno == EPERM) { int fd = open(filename, O_RDONLY); if (fd != -1) { long flags = 0; if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) { } close(fd); continue; } } if (errno == EROFS) { break; } if (errno != EBUSY || i > 100) exit(1); if (umount2(filename, umount_flags)) exit(1); } } closedir(dp); for (int i = 0;; i++) { if (rmdir(dir) == 0) break; if (i < 100) { if (errno == EPERM) { int fd = open(dir, O_RDONLY); if (fd != -1) { long flags = 0; if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) { } close(fd); continue; } } if (errno == EROFS) { break; } if (errno == EBUSY) { if (umount2(dir, umount_flags)) exit(1); continue; } if (errno == ENOTEMPTY) { if (iter < 100) { iter++; goto retry; } } } 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() { setup_cgroups_loop(); checkpoint_net_namespace(); } static void reset_loop() { reset_net_namespace(); } static void setup_test() { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); setpgrp(); setup_cgroups_test(); write_file("/proc/self/oom_score_adj", "1000"); if (symlink("/dev/binderfs", "./binderfs")) { } } static void close_fds() { for (int fd = 3; fd < MAX_FDS; fd++) close(fd); } static const char* setup_binfmt_misc() { if (mount(0, "/proc/sys/fs/binfmt_misc", "binfmt_misc", 0, 0) && errno != EBUSY) { return NULL; } if (!write_file("/proc/sys/fs/binfmt_misc/register", ":syz0:M:0:\x01::./file0:") || !write_file("/proc/sys/fs/binfmt_misc/register", ":syz1:M:1:\x02::./file0:POC")) return "write(/proc/sys/fs/binfmt_misc/register) failed"; return NULL; } static const char* setup_usb() { if (chmod("/dev/raw-gadget", 0666)) return "failed to chmod /dev/raw-gadget"; return NULL; } static void setup_sysctl() { int cad_pid = fork(); if (cad_pid < 0) exit(1); if (cad_pid == 0) { for (;;) sleep(100); } char tmppid[32]; snprintf(tmppid, sizeof(tmppid), "%d", cad_pid); struct { const char* name; const char* data; } files[] = { {"/sys/kernel/debug/x86/nmi_longest_ns", "10000000000"}, {"/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", tmppid}, }; for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); i++) { if (!write_file(files[i].name, files[i].data)) { } } kill(cad_pid, SIGKILL); while (waitpid(cad_pid, NULL, 0) != cad_pid) ; } #define SWAP_FILE "./swap-file" #define SWAP_FILE_SIZE (128 * 1000 * 1000) static const char* setup_swap() { swapoff(SWAP_FILE); unlink(SWAP_FILE); int fd = open(SWAP_FILE, O_CREAT | O_WRONLY | O_CLOEXEC, 0600); if (fd == -1) return "swap file open failed"; fallocate(fd, FALLOC_FL_ZERO_RANGE, 0, SWAP_FILE_SIZE); close(fd); char cmdline[64]; sprintf(cmdline, "mkswap %s", SWAP_FILE); if (runcmdline(cmdline)) return "mkswap failed"; if (swapon(SWAP_FILE, SWAP_FLAG_PREFER) == 1) return "swapon failed"; return NULL; } 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) { if (write(1, "executing program\n", sizeof("executing program\n") - 1)) { } int i, call, thread; for (call = 0; call < 17; 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); event_timedwait(&th->done, 50 + (call == 2 ? 500 : 0) + (call == 4 ? 500 : 0) + (call == 10 ? 500 : 0) + (call == 15 ? 500 : 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++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); reset_loop(); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); setup_test(); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { sleep_ms(10); if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; if (current_time_ms() - start < 5000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } uint64_t r[3] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff}; void execute_call(int call) { intptr_t res = 0; switch (call) { case 0: // bpf$MAP_CREATE arguments: [ // cmd: const = 0x0 (8 bytes) // arg: nil // size: len = 0x48 (8 bytes) // ] // returns fd_bpf_map syscall(__NR_bpf, /*cmd=*/0ul, /*arg=*/0ul, /*size=*/0x48ul); break; case 1: // bpf$MAP_CREATE_CONST_STR arguments: [ // cmd: const = 0x0 (8 bytes) // arg: nil // size: len = 0x48 (8 bytes) // ] // returns fd_bpf_const_str_map syscall(__NR_bpf, /*cmd=*/0ul, /*arg=*/0ul, /*size=*/0x48ul); break; case 2: // bpf$BPF_RAW_TRACEPOINT_OPEN arguments: [ // cmd: const = 0x11 (8 bytes) // arg: ptr[in, bpf_raw_tracepoint] { // bpf_raw_tracepoint { // name: ptr[in, buffer] { // buffer: {73 63 68 65 64 5f 73 77 69 74 63 68 00} (length 0xd) // } // prog_fd: fd_bpf_prog_raw_tracepoint (resource) // pad: const = 0x0 (4 bytes) // cookie: int64 = 0x0 (8 bytes) // } // } // size: len = 0x10 (8 bytes) // ] // returns fd_perf_base NONFAILING(*(uint64_t*)0x200000000180 = 0x200000000140); NONFAILING(memcpy((void*)0x200000000140, "sched_switch\000", 13)); NONFAILING(*(uint32_t*)0x200000000188 = -1); NONFAILING(*(uint32_t*)0x20000000018c = 0); NONFAILING(*(uint64_t*)0x200000000190 = 0); syscall(__NR_bpf, /*cmd=*/0x11ul, /*arg=*/0x200000000180ul, /*size=*/0x10ul); break; case 3: // perf_event_open arguments: [ // attr: ptr[in, perf_event_attr] { // perf_event_attr { // type: perf_event_type = 0x1 (4 bytes) // size: len = 0x80 (4 bytes) // config0: int8 = 0x0 (1 bytes) // config1: int8 = 0x0 (1 bytes) // config2: int8 = 0x0 (1 bytes) // config3: int8 = 0x0 (1 bytes) // config4: const = 0x0 (4 bytes) // sample_freq: int64 = 0x200 (8 bytes) // sample_type: perf_sample_type = 0x20 (8 bytes) // read_format: perf_read_format = 0x3 (8 bytes) // disabled: int64 = 0x0 (0 bytes) // inherit: int64 = 0x0 (0 bytes) // pinned: int64 = 0x0 (0 bytes) // exclusive: int64 = 0x0 (0 bytes) // exclude_user: int64 = 0x0 (0 bytes) // exclude_kernel: int64 = 0x0 (0 bytes) // exclude_hv: int64 = 0x0 (0 bytes) // exclude_idle: int64 = 0x0 (0 bytes) // mmap: int64 = 0x0 (0 bytes) // comm: int64 = 0x0 (0 bytes) // freq: int64 = 0x0 (0 bytes) // inherit_stat: int64 = 0x0 (0 bytes) // enable_on_exec: int64 = 0x0 (0 bytes) // task: int64 = 0x0 (0 bytes) // watermark: int64 = 0x0 (0 bytes) // precise_ip: int64 = 0x0 (0 bytes) // mmap_data: int64 = 0x0 (0 bytes) // sample_id_all: int64 = 0x0 (0 bytes) // exclude_host: int64 = 0x0 (0 bytes) // exclude_guest: int64 = 0x0 (0 bytes) // exclude_callchain_kernel: int64 = 0x0 (0 bytes) // exclude_callchain_user: int64 = 0x0 (0 bytes) // mmap2: int64 = 0x0 (0 bytes) // comm_exec: int64 = 0x0 (0 bytes) // use_clockid: int64 = 0x0 (0 bytes) // context_switch: int64 = 0x0 (0 bytes) // write_backward: int64 = 0x0 (0 bytes) // namespaces: int64 = 0x0 (0 bytes) // ksymbol: int64 = 0x0 (0 bytes) // bpf_event: int64 = 0x0 (0 bytes) // aux_output: int64 = 0x0 (0 bytes) // cgroup: int64 = 0x0 (0 bytes) // text_poke: int64 = 0x0 (0 bytes) // build_id: int64 = 0x0 (0 bytes) // inherit_thread: int64 = 0x0 (0 bytes) // remove_on_exec: int64 = 0x0 (0 bytes) // sigtrap: int64 = 0x0 (0 bytes) // __reserved_1: const = 0x0 (8 bytes) // wakeup_events: int32 = 0x0 (4 bytes) // bp_type: perf_bp_type = 0x0 (4 bytes) // bp_config: union perf_bp_config { // perf_bp: perf_bp { // bp_addr: nil // bp_len: perf_bp_lens = 0x4 (8 bytes) // } // } // branch_sample_type: perf_branch_sample_type = 0x0 (8 bytes) // sample_regs_user: int64 = 0x0 (8 bytes) // sample_stack_user: int32 = 0x0 (4 bytes) // clockid: clock_type = 0x0 (4 bytes) // sample_regs_intr: int64 = 0x0 (8 bytes) // aux_watermark: int32 = 0x0 (4 bytes) // sample_max_stack: int16 = 0x0 (2 bytes) // __reserved_2: const = 0x0 (2 bytes) // aux_sample_size: int32 = 0x0 (4 bytes) // __reserved_3: const = 0x0 (4 bytes) // sig_data: int64 = 0x0 (8 bytes) // } // } // pid: pid (resource) // cpu: intptr = 0xffffffffffffffff (8 bytes) // group: fd_perf (resource) // flags: perf_flags = 0x9 (8 bytes) // ] // returns fd_perf NONFAILING(*(uint32_t*)0x200000000100 = 1); NONFAILING(*(uint32_t*)0x200000000104 = 0x80); NONFAILING(*(uint8_t*)0x200000000108 = 0); NONFAILING(*(uint8_t*)0x200000000109 = 0); NONFAILING(*(uint8_t*)0x20000000010a = 0); NONFAILING(*(uint8_t*)0x20000000010b = 0); NONFAILING(*(uint32_t*)0x20000000010c = 0); NONFAILING(*(uint64_t*)0x200000000110 = 0x200); NONFAILING(*(uint64_t*)0x200000000118 = 0x20); NONFAILING(*(uint64_t*)0x200000000120 = 3); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 0, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 1, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 2, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 3, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 4, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 5, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 6, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 7, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 8, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 9, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 10, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 11, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 12, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 13, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 14, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 15, 2)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 17, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 18, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 19, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 20, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 21, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 22, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 23, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 24, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 25, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 26, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 27, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 28, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 29, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 30, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 31, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 32, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 33, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 34, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 35, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 36, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 37, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 38, 26)); NONFAILING(*(uint32_t*)0x200000000130 = 0); NONFAILING(*(uint32_t*)0x200000000134 = 0); NONFAILING(*(uint64_t*)0x200000000138 = 0); NONFAILING(*(uint64_t*)0x200000000140 = 4); NONFAILING(*(uint64_t*)0x200000000148 = 0); NONFAILING(*(uint64_t*)0x200000000150 = 0); NONFAILING(*(uint32_t*)0x200000000158 = 0); NONFAILING(*(uint32_t*)0x20000000015c = 0); NONFAILING(*(uint64_t*)0x200000000160 = 0); NONFAILING(*(uint32_t*)0x200000000168 = 0); NONFAILING(*(uint16_t*)0x20000000016c = 0); NONFAILING(*(uint16_t*)0x20000000016e = 0); NONFAILING(*(uint32_t*)0x200000000170 = 0); NONFAILING(*(uint32_t*)0x200000000174 = 0); NONFAILING(*(uint64_t*)0x200000000178 = 0); syscall(__NR_perf_event_open, /*attr=*/0x200000000100ul, /*pid=*/0, /*cpu=*/(intptr_t)-1, /*group=*/(intptr_t)-1, /*flags=PERF_FLAG_FD_CLOEXEC|PERF_FLAG_FD_NO_GROUP*/ 9ul); break; case 4: // bpf$BPF_RAW_TRACEPOINT_OPEN arguments: [ // cmd: const = 0x11 (8 bytes) // arg: nil // size: len = 0x0 (8 bytes) // ] // returns fd_perf_base syscall(__NR_bpf, /*cmd=*/0x11ul, /*arg=*/0ul, /*size=*/0ul); break; case 5: // socketpair arguments: [ // domain: socket_domain = 0x1e (8 bytes) // type: socket_type = 0x1 (8 bytes) // proto: int32 = 0x0 (4 bytes) // fds: nil // ] syscall(__NR_socketpair, /*domain=AF_TIPC*/ 0x1eul, /*type=SOCK_STREAM*/ 1ul, /*proto=*/0, /*fds=*/0ul); break; case 6: // close arguments: [ // fd: fd (resource) // ] syscall(__NR_close, /*fd=*/(intptr_t)-1); break; case 7: // perf_event_open arguments: [ // attr: ptr[in, perf_event_attr] { // perf_event_attr { // type: perf_event_type = 0x1 (4 bytes) // size: len = 0x80 (4 bytes) // config0: int8 = 0x2 (1 bytes) // config1: int8 = 0x0 (1 bytes) // config2: int8 = 0x0 (1 bytes) // config3: int8 = 0x0 (1 bytes) // config4: const = 0x0 (4 bytes) // sample_freq: int64 = 0x400 (8 bytes) // sample_type: perf_sample_type = 0x1e37cf (8 bytes) // read_format: perf_read_format = 0x0 (8 bytes) // disabled: int64 = 0x0 (0 bytes) // inherit: int64 = 0x0 (0 bytes) // pinned: int64 = 0x0 (0 bytes) // exclusive: int64 = 0x0 (0 bytes) // exclude_user: int64 = 0x0 (0 bytes) // exclude_kernel: int64 = 0x0 (0 bytes) // exclude_hv: int64 = 0x0 (0 bytes) // exclude_idle: int64 = 0x0 (0 bytes) // mmap: int64 = 0x0 (0 bytes) // comm: int64 = 0x0 (0 bytes) // freq: int64 = 0x0 (0 bytes) // inherit_stat: int64 = 0x0 (0 bytes) // enable_on_exec: int64 = 0x0 (0 bytes) // task: int64 = 0x0 (0 bytes) // watermark: int64 = 0x0 (0 bytes) // precise_ip: int64 = 0x0 (0 bytes) // mmap_data: int64 = 0x0 (0 bytes) // sample_id_all: int64 = 0x0 (0 bytes) // exclude_host: int64 = 0x0 (0 bytes) // exclude_guest: int64 = 0x0 (0 bytes) // exclude_callchain_kernel: int64 = 0x0 (0 bytes) // exclude_callchain_user: int64 = 0x0 (0 bytes) // mmap2: int64 = 0x0 (0 bytes) // comm_exec: int64 = 0x0 (0 bytes) // use_clockid: int64 = 0x0 (0 bytes) // context_switch: int64 = 0x0 (0 bytes) // write_backward: int64 = 0x0 (0 bytes) // namespaces: int64 = 0x0 (0 bytes) // ksymbol: int64 = 0x0 (0 bytes) // bpf_event: int64 = 0x0 (0 bytes) // aux_output: int64 = 0x0 (0 bytes) // cgroup: int64 = 0x0 (0 bytes) // text_poke: int64 = 0x0 (0 bytes) // build_id: int64 = 0x0 (0 bytes) // inherit_thread: int64 = 0x0 (0 bytes) // remove_on_exec: int64 = 0x0 (0 bytes) // sigtrap: int64 = 0x0 (0 bytes) // __reserved_1: const = 0x0 (8 bytes) // wakeup_events: int32 = 0x0 (4 bytes) // bp_type: perf_bp_type = 0x0 (4 bytes) // bp_config: union perf_bp_config { // perf_config_ext: perf_config_ext { // config1: int64 = 0x0 (8 bytes) // config2: int64 = 0x0 (8 bytes) // } // } // branch_sample_type: perf_branch_sample_type = 0x0 (8 bytes) // sample_regs_user: int64 = 0x401 (8 bytes) // sample_stack_user: int32 = 0x200 (4 bytes) // clockid: clock_type = 0x0 (4 bytes) // sample_regs_intr: int64 = 0x7 (8 bytes) // aux_watermark: int32 = 0x0 (4 bytes) // sample_max_stack: int16 = 0x100 (2 bytes) // __reserved_2: const = 0x0 (2 bytes) // aux_sample_size: int32 = 0x0 (4 bytes) // __reserved_3: const = 0x0 (4 bytes) // sig_data: int64 = 0x0 (8 bytes) // } // } // pid: pid (resource) // cpu: intptr = 0xffefffffffffffff (8 bytes) // group: fd_perf (resource) // flags: perf_flags = 0x0 (8 bytes) // ] // returns fd_perf NONFAILING(*(uint32_t*)0x200000000a00 = 1); NONFAILING(*(uint32_t*)0x200000000a04 = 0x80); NONFAILING(*(uint8_t*)0x200000000a08 = 2); NONFAILING(*(uint8_t*)0x200000000a09 = 0); NONFAILING(*(uint8_t*)0x200000000a0a = 0); NONFAILING(*(uint8_t*)0x200000000a0b = 0); NONFAILING(*(uint32_t*)0x200000000a0c = 0); NONFAILING(*(uint64_t*)0x200000000a10 = 0x400); NONFAILING(*(uint64_t*)0x200000000a18 = 0x1e37cf); NONFAILING(*(uint64_t*)0x200000000a20 = 0); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 0, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 1, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 2, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 3, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 4, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 5, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 6, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 7, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 8, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 9, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 10, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 11, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 12, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 13, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 14, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 15, 2)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 17, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 18, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 19, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 20, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 21, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 22, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 23, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 24, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 25, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 26, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 27, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 28, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 29, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 30, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 31, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 32, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 33, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 34, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 35, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 36, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 37, 1)); NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000a28, 0, 38, 26)); NONFAILING(*(uint32_t*)0x200000000a30 = 0); NONFAILING(*(uint32_t*)0x200000000a34 = 0); NONFAILING(*(uint64_t*)0x200000000a38 = 0); NONFAILING(*(uint64_t*)0x200000000a40 = 0); NONFAILING(*(uint64_t*)0x200000000a48 = 0); NONFAILING(*(uint64_t*)0x200000000a50 = 0x401); NONFAILING(*(uint32_t*)0x200000000a58 = 0x200); NONFAILING(*(uint32_t*)0x200000000a5c = 0); NONFAILING(*(uint64_t*)0x200000000a60 = 7); NONFAILING(*(uint32_t*)0x200000000a68 = 0); NONFAILING(*(uint16_t*)0x200000000a6c = 0x100); NONFAILING(*(uint16_t*)0x200000000a6e = 0); NONFAILING(*(uint32_t*)0x200000000a70 = 0); NONFAILING(*(uint32_t*)0x200000000a74 = 0); NONFAILING(*(uint64_t*)0x200000000a78 = 0); res = syscall(__NR_perf_event_open, /*attr=*/0x200000000a00ul, /*pid=*/0, /*cpu=*/0xffeffffffffffffful, /*group=*/(intptr_t)-1, /*flags=*/0ul); if (res != -1) r[0] = res; break; case 8: // bpf$ENABLE_STATS arguments: [ // cmd: const = 0x20 (8 bytes) // arg: nil // size: len = 0x0 (8 bytes) // ] syscall(__NR_bpf, /*cmd=*/0x20ul, /*arg=*/0ul, /*size=*/0ul); break; case 9: // bpf$PROG_LOAD arguments: [ // cmd: const = 0x5 (8 bytes) // arg: ptr[in, bpf_prog_t[flags[bpf_prog_type, int32], // bpf_prog_attach_types, bpf_btf_id[opt], fd_bpf_prog[opt]]] { // bpf_prog_t[flags[bpf_prog_type, int32], bpf_prog_attach_types, // bpf_btf_id[opt], fd_bpf_prog[opt]] { // type: bpf_prog_type = 0x7 (4 bytes) // ninsn: bytesize8 = 0x6 (4 bytes) // insns: ptr[inout, array[ANYUNION]] { // array[ANYUNION] { // union ANYUNION { // ANYBLOB: buffer: {05 00 00 00 00 00 00 00 71 11 ae 00 00 00 // 00 00 85 10 00 00 02 00 00 00 85 00 00 00 07 00 00 00 95 00 // 00 00 00 00 00 00 95 00 a5 05 00 00 00 00} (length 0x30) // } // } // } // license: ptr[in, buffer] { // buffer: {47 50 4c 00} (length 0x4) // } // loglev: int32 = 0x5 (4 bytes) // logsize: len = 0x29e (4 bytes) // log: ptr[out, buffer] { // buffer: (DirOut) // } // kern_version: bpf_kern_version = 0x0 (4 bytes) // flags: bpf_prog_load_flags = 0x0 (4 bytes) // prog_name: buffer: {00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 // 00} (length 0x10) prog_ifindex: ifindex (resource) // expected_attach_type: union bpf_prog_attach_types { // fallback: bpf_attach_types = 0x0 (4 bytes) // } // btf_fd: fd_btf (resource) // func_info_rec_size: const = 0x6 (4 bytes) // func_info: nil // func_info_cnt: len = 0x0 (4 bytes) // line_info_rec_size: const = 0x10 (4 bytes) // line_info: nil // line_info_cnt: len = 0x0 (4 bytes) // attach_btf_id: bpf_btf_id (resource) // attach_prog_fd: fd_bpf_prog (resource) // core_relo_cnt: len = 0x0 (4 bytes) // fd_array: nil // core_relos: nil // core_relo_rec_size: const = 0x10 (4 bytes) // log_true_size: int32 = 0x0 (4 bytes) // prog_token_fd: union _bpf_prog_t[flags[bpf_prog_type, int32], // bpf_prog_attach_types, bpf_btf_id[opt], // fd_bpf_prog[opt]]_prog_token_fd_wrapper { // void: buffer: {} (length 0x0) // } // pad: union _bpf_prog_t[flags[bpf_prog_type, int32], // bpf_prog_attach_types, bpf_btf_id[opt], // fd_bpf_prog[opt]]_pad_wrapper { // value: const = 0x0 (4 bytes) // } // } // } // size: len = 0x70 (8 bytes) // ] // returns fd_bpf_prog NONFAILING(*(uint32_t*)0x200000000440 = 7); NONFAILING(*(uint32_t*)0x200000000444 = 6); NONFAILING(*(uint64_t*)0x200000000448 = 0x200000000000); NONFAILING(memcpy( (void*)0x200000000000, "\x05\x00\x00\x00\x00\x00\x00\x00\x71\x11\xae\x00\x00\x00\x00\x00\x85" "\x10\x00\x00\x02\x00\x00\x00\x85\x00\x00\x00\x07\x00\x00\x00\x95\x00" "\x00\x00\x00\x00\x00\x00\x95\x00\xa5\x05\x00\x00\x00\x00", 48)); NONFAILING(*(uint64_t*)0x200000000450 = 0x200000000080); NONFAILING(memcpy((void*)0x200000000080, "GPL\000", 4)); NONFAILING(*(uint32_t*)0x200000000458 = 5); NONFAILING(*(uint32_t*)0x20000000045c = 0x29e); NONFAILING(*(uint64_t*)0x200000000460 = 0x20000000cf3d); NONFAILING(*(uint32_t*)0x200000000468 = 0); NONFAILING(*(uint32_t*)0x20000000046c = 0); NONFAILING(memset((void*)0x200000000470, 0, 16)); NONFAILING(*(uint32_t*)0x200000000480 = 0); NONFAILING(*(uint32_t*)0x200000000484 = 0); NONFAILING(*(uint32_t*)0x200000000488 = -1); NONFAILING(*(uint32_t*)0x20000000048c = 6); NONFAILING(*(uint64_t*)0x200000000490 = 0); NONFAILING(*(uint32_t*)0x200000000498 = 0); NONFAILING(*(uint32_t*)0x20000000049c = 0x10); NONFAILING(*(uint64_t*)0x2000000004a0 = 0); NONFAILING(*(uint32_t*)0x2000000004a8 = 0); NONFAILING(*(uint32_t*)0x2000000004ac = 0); NONFAILING(*(uint32_t*)0x2000000004b0 = -1); NONFAILING(*(uint32_t*)0x2000000004b4 = 0); NONFAILING(*(uint64_t*)0x2000000004b8 = 0); NONFAILING(*(uint64_t*)0x2000000004c0 = 0); NONFAILING(*(uint32_t*)0x2000000004c8 = 0x10); NONFAILING(*(uint32_t*)0x2000000004cc = 0); NONFAILING(*(uint32_t*)0x2000000004d0 = 0); res = syscall(__NR_bpf, /*cmd=*/5ul, /*arg=*/0x200000000440ul, /*size=*/0x70ul); if (res != -1) r[1] = res; break; case 10: // bpf$BPF_PROG_WITH_BTFID_LOAD arguments: [ // cmd: const = 0x5 (8 bytes) // arg: nil // size: len = 0x0 (8 bytes) // ] // returns fd_bpf_prog_with_btfid syscall(__NR_bpf, /*cmd=*/5ul, /*arg=*/0ul, /*size=*/0ul); break; case 11: // ioctl$PERF_EVENT_IOC_SET_BPF arguments: [ // fd: fd_perf (resource) // cmd: const = 0x40042408 (4 bytes) // prog: fd_bpf_prog (resource) // ] syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=*/0x40042408, /*prog=*/r[1]); break; case 12: // openat$cgroup_ro arguments: [ // fd: fd_cgroup (resource) // file: ptr[in, buffer] { // buffer: {6d 65 6d 6f 72 79 2e 63 75 72 72 65 6e 74 00} (length 0xf) // } // flags: const = 0x275a (4 bytes) // mode: const = 0x0 (2 bytes) // ] // returns fd NONFAILING(memcpy((void*)0x2000000001c0, "memory.current\000", 15)); res = syscall(__NR_openat, /*fd=*/0xffffff9c, /*file=*/0x2000000001c0ul, /*flags=*/0x275a, /*mode=*/0); if (res != -1) r[2] = res; break; case 13: // write$cgroup_pid arguments: [ // fd: fd_cgroup_pid (resource) // buf: ptr[in, pid] { // pid (resource) // } // len: bytesize = 0x2a979d (8 bytes) // ] NONFAILING(sprintf((char*)0x200000000000, "0x%016llx", (long long)0)); syscall(__NR_write, /*fd=*/r[2], /*buf=*/0x200000000000ul, /*len=*/0x2a979dul); break; case 14: // socketpair arguments: [ // domain: socket_domain = 0x1e (8 bytes) // type: socket_type = 0x1 (8 bytes) // proto: int32 = 0x0 (4 bytes) // fds: nil // ] syscall(__NR_socketpair, /*domain=AF_TIPC*/ 0x1eul, /*type=SOCK_STREAM*/ 1ul, /*proto=*/0, /*fds=*/0ul); break; case 15: // bpf$BPF_PROG_RAW_TRACEPOINT_LOAD arguments: [ // cmd: const = 0x5 (8 bytes) // arg: nil // size: len = 0x0 (8 bytes) // ] // returns fd_bpf_prog_raw_tracepoint syscall(__NR_bpf, /*cmd=*/5ul, /*arg=*/0ul, /*size=*/0ul); break; case 16: // bpf$BPF_BTF_LOAD arguments: [ // cmd: const = 0x12 (8 bytes) // arg: nil // size: len = 0x0 (8 bytes) // ] // returns fd_btf syscall(__NR_bpf, /*cmd=*/0x12ul, /*arg=*/0ul, /*size=*/0ul); break; } } int main(void) { syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul); syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul); syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul); setup_sysctl(); setup_cgroups(); const char* reason; (void)reason; if ((reason = setup_binfmt_misc())) printf("the reproducer may not work as expected: binfmt_misc setup failed: " "%s\n", reason); if ((reason = setup_usb())) printf("the reproducer may not work as expected: USB injection setup " "failed: %s\n", reason); if ((reason = setup_swap())) printf("the reproducer may not work as expected: swap setup failed: %s\n", reason); install_segv_handler(); for (procid = 0; procid < 5; procid++) { if (fork() == 0) { use_temporary_dir(); do_sandbox_none(); } } sleep(1000000); return 0; }