// https://syzkaller.appspot.com/bug?id=130cae4a4387fae6614fccf5eed180400ea30948 // 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 #include #include #include #include #include #include #include #include #include #include #include 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); } #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)))) 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; } struct nlmsg { char* pos; int nesting; struct nlattr* nested[8]; char buf[4096]; }; static void netlink_init(struct nlmsg* nlmsg, int typ, int flags, const void* data, int size) { memset(nlmsg, 0, sizeof(*nlmsg)); struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf; hdr->nlmsg_type = typ; hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags; memcpy(hdr + 1, data, size); nlmsg->pos = (char*)(hdr + 1) + NLMSG_ALIGN(size); } static void netlink_attr(struct nlmsg* nlmsg, int typ, const void* data, int size) { struct nlattr* attr = (struct nlattr*)nlmsg->pos; attr->nla_len = sizeof(*attr) + size; attr->nla_type = typ; if (size > 0) memcpy(attr + 1, data, size); nlmsg->pos += NLMSG_ALIGN(attr->nla_len); } static int netlink_send_ext(struct nlmsg* nlmsg, int sock, uint16_t reply_type, int* reply_len, bool dofail) { if (nlmsg->pos > nlmsg->buf + sizeof(nlmsg->buf) || nlmsg->nesting) exit(1); struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf; hdr->nlmsg_len = nlmsg->pos - nlmsg->buf; struct sockaddr_nl addr; memset(&addr, 0, sizeof(addr)); addr.nl_family = AF_NETLINK; ssize_t n = sendto(sock, nlmsg->buf, hdr->nlmsg_len, 0, (struct sockaddr*)&addr, sizeof(addr)); if (n != (ssize_t)hdr->nlmsg_len) { if (dofail) exit(1); return -1; } n = recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0); if (reply_len) *reply_len = 0; if (n < 0) { if (dofail) exit(1); return -1; } if (n < (ssize_t)sizeof(struct nlmsghdr)) { errno = EINVAL; if (dofail) exit(1); return -1; } if (hdr->nlmsg_type == NLMSG_DONE) return 0; if (reply_len && hdr->nlmsg_type == reply_type) { *reply_len = n; return 0; } if (n < (ssize_t)(sizeof(struct nlmsghdr) + sizeof(struct nlmsgerr))) { errno = EINVAL; if (dofail) exit(1); return -1; } if (hdr->nlmsg_type != NLMSG_ERROR) { errno = EINVAL; if (dofail) exit(1); return -1; } errno = -((struct nlmsgerr*)(hdr + 1))->error; return -errno; } static int netlink_send(struct nlmsg* nlmsg, int sock) { return netlink_send_ext(nlmsg, sock, 0, NULL, true); } static int netlink_query_family_id(struct nlmsg* nlmsg, int sock, const char* family_name, bool dofail) { struct genlmsghdr genlhdr; memset(&genlhdr, 0, sizeof(genlhdr)); genlhdr.cmd = CTRL_CMD_GETFAMILY; netlink_init(nlmsg, GENL_ID_CTRL, 0, &genlhdr, sizeof(genlhdr)); netlink_attr(nlmsg, CTRL_ATTR_FAMILY_NAME, family_name, strnlen(family_name, GENL_NAMSIZ - 1) + 1); int n = 0; int err = netlink_send_ext(nlmsg, sock, GENL_ID_CTRL, &n, dofail); if (err < 0) { return -1; } uint16_t id = 0; struct nlattr* attr = (struct nlattr*)(nlmsg->buf + NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(genlhdr))); for (; (char*)attr < nlmsg->buf + n; attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) { if (attr->nla_type == CTRL_ATTR_FAMILY_ID) { id = *(uint16_t*)(attr + 1); break; } } if (!id) { errno = EINVAL; return -1; } recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0); return id; } static void netlink_device_change(struct nlmsg* nlmsg, int sock, const char* name, bool up, const char* master, const void* mac, int macsize, const char* new_name) { struct ifinfomsg hdr; memset(&hdr, 0, sizeof(hdr)); if (up) hdr.ifi_flags = hdr.ifi_change = IFF_UP; hdr.ifi_index = if_nametoindex(name); netlink_init(nlmsg, RTM_NEWLINK, 0, &hdr, sizeof(hdr)); if (new_name) netlink_attr(nlmsg, IFLA_IFNAME, new_name, strlen(new_name)); if (master) { int ifindex = if_nametoindex(master); netlink_attr(nlmsg, IFLA_MASTER, &ifindex, sizeof(ifindex)); } if (macsize) netlink_attr(nlmsg, IFLA_ADDRESS, mac, macsize); int err = netlink_send(nlmsg, sock); if (err < 0) { } } static int netlink_add_addr(struct nlmsg* nlmsg, int sock, const char* dev, const void* addr, int addrsize) { struct ifaddrmsg hdr; memset(&hdr, 0, sizeof(hdr)); hdr.ifa_family = addrsize == 4 ? AF_INET : AF_INET6; hdr.ifa_prefixlen = addrsize == 4 ? 24 : 120; hdr.ifa_scope = RT_SCOPE_UNIVERSE; hdr.ifa_index = if_nametoindex(dev); netlink_init(nlmsg, RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, &hdr, sizeof(hdr)); netlink_attr(nlmsg, IFA_LOCAL, addr, addrsize); netlink_attr(nlmsg, IFA_ADDRESS, addr, addrsize); return netlink_send(nlmsg, sock); } static void netlink_add_addr4(struct nlmsg* nlmsg, int sock, const char* dev, const char* addr) { struct in_addr in_addr; inet_pton(AF_INET, addr, &in_addr); int err = netlink_add_addr(nlmsg, sock, dev, &in_addr, sizeof(in_addr)); if (err < 0) { } } static void netlink_add_addr6(struct nlmsg* nlmsg, int sock, const char* dev, const char* addr) { struct in6_addr in6_addr; inet_pton(AF_INET6, addr, &in6_addr); int err = netlink_add_addr(nlmsg, sock, dev, &in6_addr, sizeof(in6_addr)); if (err < 0) { } } static void netlink_add_neigh(struct nlmsg* nlmsg, int sock, const char* name, const void* addr, int addrsize, const void* mac, int macsize) { struct ndmsg hdr; memset(&hdr, 0, sizeof(hdr)); hdr.ndm_family = addrsize == 4 ? AF_INET : AF_INET6; hdr.ndm_ifindex = if_nametoindex(name); hdr.ndm_state = NUD_PERMANENT; netlink_init(nlmsg, RTM_NEWNEIGH, NLM_F_EXCL | NLM_F_CREATE, &hdr, sizeof(hdr)); netlink_attr(nlmsg, NDA_DST, addr, addrsize); netlink_attr(nlmsg, NDA_LLADDR, mac, macsize); int err = netlink_send(nlmsg, sock); if (err < 0) { } } static struct nlmsg nlmsg; static int tunfd = -1; #define TUN_IFACE "syz_tun" #define LOCAL_MAC 0xaaaaaaaaaaaa #define REMOTE_MAC 0xaaaaaaaaaabb #define LOCAL_IPV4 "172.20.20.170" #define REMOTE_IPV4 "172.20.20.187" #define LOCAL_IPV6 "fe80::aa" #define REMOTE_IPV6 "fe80::bb" #define IFF_NAPI 0x0010 static void initialize_tun(void) { tunfd = open("/dev/net/tun", O_RDWR | O_NONBLOCK); if (tunfd == -1) { printf("tun: can't open /dev/net/tun: please enable CONFIG_TUN=y\n"); printf("otherwise fuzzing or reproducing might not work as intended\n"); return; } const int kTunFd = 240; if (dup2(tunfd, kTunFd) < 0) exit(1); close(tunfd); tunfd = kTunFd; struct ifreq ifr; memset(&ifr, 0, sizeof(ifr)); strncpy(ifr.ifr_name, TUN_IFACE, IFNAMSIZ); ifr.ifr_flags = IFF_TAP | IFF_NO_PI; if (ioctl(tunfd, TUNSETIFF, (void*)&ifr) < 0) { exit(1); } char sysctl[64]; sprintf(sysctl, "/proc/sys/net/ipv6/conf/%s/accept_dad", TUN_IFACE); write_file(sysctl, "0"); sprintf(sysctl, "/proc/sys/net/ipv6/conf/%s/router_solicitations", TUN_IFACE); write_file(sysctl, "0"); int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); if (sock == -1) exit(1); netlink_add_addr4(&nlmsg, sock, TUN_IFACE, LOCAL_IPV4); netlink_add_addr6(&nlmsg, sock, TUN_IFACE, LOCAL_IPV6); uint64_t macaddr = REMOTE_MAC; struct in_addr in_addr; inet_pton(AF_INET, REMOTE_IPV4, &in_addr); netlink_add_neigh(&nlmsg, sock, TUN_IFACE, &in_addr, sizeof(in_addr), &macaddr, ETH_ALEN); struct in6_addr in6_addr; inet_pton(AF_INET6, REMOTE_IPV6, &in6_addr); netlink_add_neigh(&nlmsg, sock, TUN_IFACE, &in6_addr, sizeof(in6_addr), &macaddr, ETH_ALEN); macaddr = LOCAL_MAC; netlink_device_change(&nlmsg, sock, TUN_IFACE, true, 0, &macaddr, ETH_ALEN, NULL); close(sock); } #define WIFI_INITIAL_DEVICE_COUNT 2 #define WIFI_MAC_BASE \ { \ 0x08, 0x02, 0x11, 0x00, 0x00, 0x00 \ } #define WIFI_IBSS_BSSID \ { \ 0x50, 0x50, 0x50, 0x50, 0x50, 0x50 \ } #define WIFI_IBSS_SSID \ { \ 0x10, 0x10, 0x10, 0x10, 0x10, 0x10 \ } #define WIFI_DEFAULT_FREQUENCY 2412 #define WIFI_DEFAULT_SIGNAL 0 #define WIFI_DEFAULT_RX_RATE 1 #define HWSIM_CMD_REGISTER 1 #define HWSIM_CMD_FRAME 2 #define HWSIM_CMD_NEW_RADIO 4 #define HWSIM_ATTR_SUPPORT_P2P_DEVICE 14 #define HWSIM_ATTR_PERM_ADDR 22 #define IF_OPER_UP 6 struct join_ibss_props { int wiphy_freq; bool wiphy_freq_fixed; uint8_t* mac; uint8_t* ssid; int ssid_len; }; static int set_interface_state(const char* interface_name, int on) { struct ifreq ifr; int sock = socket(AF_INET, SOCK_DGRAM, 0); if (sock < 0) { return -1; } memset(&ifr, 0, sizeof(ifr)); strcpy(ifr.ifr_name, interface_name); int ret = ioctl(sock, SIOCGIFFLAGS, &ifr); if (ret < 0) { close(sock); return -1; } if (on) ifr.ifr_flags |= IFF_UP; else ifr.ifr_flags &= ~IFF_UP; ret = ioctl(sock, SIOCSIFFLAGS, &ifr); close(sock); if (ret < 0) { return -1; } return 0; } static int nl80211_set_interface(struct nlmsg* nlmsg, int sock, int nl80211_family, uint32_t ifindex, uint32_t iftype) { struct genlmsghdr genlhdr; memset(&genlhdr, 0, sizeof(genlhdr)); genlhdr.cmd = NL80211_CMD_SET_INTERFACE; netlink_init(nlmsg, nl80211_family, 0, &genlhdr, sizeof(genlhdr)); netlink_attr(nlmsg, NL80211_ATTR_IFINDEX, &ifindex, sizeof(ifindex)); netlink_attr(nlmsg, NL80211_ATTR_IFTYPE, &iftype, sizeof(iftype)); int err = netlink_send(nlmsg, sock); if (err < 0) { } return err; } static int nl80211_join_ibss(struct nlmsg* nlmsg, int sock, int nl80211_family, uint32_t ifindex, struct join_ibss_props* props) { struct genlmsghdr genlhdr; memset(&genlhdr, 0, sizeof(genlhdr)); genlhdr.cmd = NL80211_CMD_JOIN_IBSS; netlink_init(nlmsg, nl80211_family, 0, &genlhdr, sizeof(genlhdr)); netlink_attr(nlmsg, NL80211_ATTR_IFINDEX, &ifindex, sizeof(ifindex)); netlink_attr(nlmsg, NL80211_ATTR_SSID, props->ssid, props->ssid_len); netlink_attr(nlmsg, NL80211_ATTR_WIPHY_FREQ, &(props->wiphy_freq), sizeof(props->wiphy_freq)); if (props->mac) netlink_attr(nlmsg, NL80211_ATTR_MAC, props->mac, ETH_ALEN); if (props->wiphy_freq_fixed) netlink_attr(nlmsg, NL80211_ATTR_FREQ_FIXED, NULL, 0); int err = netlink_send(nlmsg, sock); if (err < 0) { } return err; } static int get_ifla_operstate(struct nlmsg* nlmsg, int ifindex) { struct ifinfomsg info; memset(&info, 0, sizeof(info)); info.ifi_family = AF_UNSPEC; info.ifi_index = ifindex; int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); if (sock == -1) { return -1; } netlink_init(nlmsg, RTM_GETLINK, 0, &info, sizeof(info)); int n; int err = netlink_send_ext(nlmsg, sock, RTM_NEWLINK, &n, true); close(sock); if (err) { return -1; } struct rtattr* attr = IFLA_RTA(NLMSG_DATA(nlmsg->buf)); for (; RTA_OK(attr, n); attr = RTA_NEXT(attr, n)) { if (attr->rta_type == IFLA_OPERSTATE) return *((int32_t*)RTA_DATA(attr)); } return -1; } static int await_ifla_operstate(struct nlmsg* nlmsg, char* interface, int operstate) { int ifindex = if_nametoindex(interface); while (true) { usleep(1000); int ret = get_ifla_operstate(nlmsg, ifindex); if (ret < 0) return ret; if (ret == operstate) return 0; } return 0; } static int nl80211_setup_ibss_interface(struct nlmsg* nlmsg, int sock, int nl80211_family_id, char* interface, struct join_ibss_props* ibss_props) { int ifindex = if_nametoindex(interface); if (ifindex == 0) { return -1; } int ret = nl80211_set_interface(nlmsg, sock, nl80211_family_id, ifindex, NL80211_IFTYPE_ADHOC); if (ret < 0) { return -1; } ret = set_interface_state(interface, 1); if (ret < 0) { return -1; } ret = nl80211_join_ibss(nlmsg, sock, nl80211_family_id, ifindex, ibss_props); if (ret < 0) { return -1; } return 0; } static int hwsim80211_create_device(struct nlmsg* nlmsg, int sock, int hwsim_family, uint8_t mac_addr[ETH_ALEN]) { struct genlmsghdr genlhdr; memset(&genlhdr, 0, sizeof(genlhdr)); genlhdr.cmd = HWSIM_CMD_NEW_RADIO; netlink_init(nlmsg, hwsim_family, 0, &genlhdr, sizeof(genlhdr)); netlink_attr(nlmsg, HWSIM_ATTR_SUPPORT_P2P_DEVICE, NULL, 0); netlink_attr(nlmsg, HWSIM_ATTR_PERM_ADDR, mac_addr, ETH_ALEN); int err = netlink_send(nlmsg, sock); if (err < 0) { } return err; } static void initialize_wifi_devices(void) { int rfkill = open("/dev/rfkill", O_RDWR); if (rfkill == -1) { if (errno != ENOENT && errno != EACCES) exit(1); } else { struct rfkill_event event = {0}; event.type = RFKILL_TYPE_ALL; event.op = RFKILL_OP_CHANGE_ALL; if (write(rfkill, &event, sizeof(event)) != (ssize_t)(sizeof(event))) exit(1); close(rfkill); } uint8_t mac_addr[6] = WIFI_MAC_BASE; int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); if (sock < 0) { return; } int hwsim_family_id = netlink_query_family_id(&nlmsg, sock, "MAC80211_HWSIM", true); int nl80211_family_id = netlink_query_family_id(&nlmsg, sock, "nl80211", true); uint8_t ssid[] = WIFI_IBSS_SSID; uint8_t bssid[] = WIFI_IBSS_BSSID; struct join_ibss_props ibss_props = {.wiphy_freq = WIFI_DEFAULT_FREQUENCY, .wiphy_freq_fixed = true, .mac = bssid, .ssid = ssid, .ssid_len = sizeof(ssid)}; for (int device_id = 0; device_id < WIFI_INITIAL_DEVICE_COUNT; device_id++) { mac_addr[5] = device_id; int ret = hwsim80211_create_device(&nlmsg, sock, hwsim_family_id, mac_addr); if (ret < 0) exit(1); char interface[6] = "wlan0"; interface[4] += device_id; if (nl80211_setup_ibss_interface(&nlmsg, sock, nl80211_family_id, interface, &ibss_props) < 0) exit(1); } for (int device_id = 0; device_id < WIFI_INITIAL_DEVICE_COUNT; device_id++) { char interface[6] = "wlan0"; interface[4] += device_id; int ret = await_ifla_operstate(&nlmsg, interface, IF_OPER_UP); if (ret < 0) exit(1); } close(sock); } static int read_tun(char* data, int size) { if (tunfd < 0) return -1; int rv = read(tunfd, data, size); if (rv < 0) { if (errno == EAGAIN || errno == EBADFD) return -1; exit(1); } return rv; } static void flush_tun() { char data[1000]; while (read_tun(&data[0], sizeof(data)) != -1) { } } #define MAX_FDS 30 static void setup_common() { if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) { } } 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 = 0; 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)) { } initialize_tun(); initialize_wifi_devices(); loop(); exit(1); } #define FS_IOC_SETFLAGS _IOW('f', 2, long) static void remove_dir(const char* dir) { int iter = 0; DIR* dp = 0; retry: while (umount2(dir, MNT_DETACH) == 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, MNT_DETACH) == 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, MNT_DETACH)) 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, MNT_DETACH)) 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_test() { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); setpgrp(); write_file("/proc/self/oom_score_adj", "1000"); flush_tun(); } static void close_fds() { for (int fd = 3; fd < MAX_FDS; fd++) close(fd); } static void execute_one(void); #define WAIT_FLAGS __WALL static void loop(void) { int iter = 0; for (;; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); setup_test(); execute_one(); close_fds(); 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; } remove_dir(cwdbuf); } } #ifndef __NR_bpf #define __NR_bpf 321 #endif uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff}; void execute_one(void) { intptr_t res = 0; memcpy((void*)0x20000180, "memory.events\000", 14); syscall(__NR_openat, 0xffffff9c, 0x20000180ul, 0x26e1ul, 0ul); syscall(__NR_socketpair, 1ul, 5ul, 0, 0x20002e00ul); *(uint32_t*)0x200001c0 = 2; *(uint32_t*)0x200001c4 = 0x70; *(uint8_t*)0x200001c8 = 0xd7; *(uint8_t*)0x200001c9 = 0; *(uint8_t*)0x200001ca = 0; *(uint8_t*)0x200001cb = 0; *(uint32_t*)0x200001cc = 0; *(uint64_t*)0x200001d0 = 0; *(uint64_t*)0x200001d8 = 0; *(uint64_t*)0x200001e0 = 0; STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 0, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 1, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 2, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 3, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 4, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 5, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 6, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 7, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 8, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 9, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 10, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 11, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 12, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 13, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 14, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 15, 2); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 17, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 18, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 19, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 20, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 21, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 22, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 23, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 24, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 25, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 26, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 27, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 28, 1); STORE_BY_BITMASK(uint64_t, , 0x200001e8, 0, 29, 35); *(uint32_t*)0x200001f0 = 0; *(uint32_t*)0x200001f4 = 0; *(uint64_t*)0x200001f8 = 0; *(uint64_t*)0x20000200 = 0; *(uint64_t*)0x20000208 = 0; *(uint64_t*)0x20000210 = 0; *(uint32_t*)0x20000218 = 0; *(uint32_t*)0x2000021c = 0; *(uint64_t*)0x20000220 = 0; *(uint32_t*)0x20000228 = 0; *(uint16_t*)0x2000022c = 0; *(uint16_t*)0x2000022e = 0; syscall(__NR_perf_event_open, 0x200001c0ul, 0, 0ul, -1, 0ul); *(uint32_t*)0x20000040 = 2; *(uint32_t*)0x20000044 = 0x70; *(uint8_t*)0x20000048 = 0xcc; *(uint8_t*)0x20000049 = 0; *(uint8_t*)0x2000004a = 0; *(uint8_t*)0x2000004b = 0; *(uint32_t*)0x2000004c = 0; *(uint64_t*)0x20000050 = 0; *(uint64_t*)0x20000058 = 0; *(uint64_t*)0x20000060 = 0; STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 0, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 1, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 2, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 3, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 4, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 5, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 6, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 7, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 8, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 9, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 10, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 11, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 12, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 13, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 14, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 15, 2); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 17, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 18, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 19, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 20, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 21, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 22, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 23, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 24, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 25, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 26, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 27, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 28, 1); STORE_BY_BITMASK(uint64_t, , 0x20000068, 0, 29, 35); *(uint32_t*)0x20000070 = 0; *(uint32_t*)0x20000074 = 0; *(uint64_t*)0x20000078 = 0; *(uint64_t*)0x20000080 = 0; *(uint64_t*)0x20000088 = 0; *(uint64_t*)0x20000090 = 0; *(uint32_t*)0x20000098 = 0; *(uint32_t*)0x2000009c = 0; *(uint64_t*)0x200000a0 = 0; *(uint32_t*)0x200000a8 = 0; *(uint16_t*)0x200000ac = 0; *(uint16_t*)0x200000ae = 0; syscall(__NR_perf_event_open, 0x20000040ul, 0, 0ul, -1, 0ul); syscall(__NR_socket, 0x29ul, 2ul, 0); memcpy((void*)0x200000c0, "pids.current\000", 13); syscall(__NR_openat, 0xffffff9c, 0x200000c0ul, 0x26e1ul, 0ul); syscall(__NR_ioctl, -1, 0x400454ca, 0ul); syscall(__NR_socketpair, 1ul, 1ul, 0, 0x20000740ul); syscall(__NR_setsockopt, -1, 0x10d, 0xb, 0ul, 0ul); *(uint32_t*)0x20000280 = 6; *(uint32_t*)0x20000284 = 4; *(uint64_t*)0x20000288 = 0x20001e40; memcpy( (void*)0x20001e40, "\x18\x02\x00\x00\xe2\xff\xff\xff\x00\x00\x00\x00\x00\x00\x00\x00\x85\x00" "\x00\x00\x36\x00\x00\x00\x95\x00\x00\x18\x00\x00\x00\x00\x92\x2a\xe8\x37" "\x13\xab\x96\x62\xce\x3a\xe3\x56\x53\x8d\xda\x12\x00\x00\x01\x00\x00\x80" "\x1b\x10\xfb\x39\xa8\xcb\x72\xd2\x8d\x82\xde\x5a\xc5\x4e\x32\xad\x55\x8c" "\x46\xff\xf4\x20\x8d\x49\x63\x19\x79\xa4\x2d\x68\x84\xec\x11\xce\x14\x13" "\x8b\x8f\xe9\x03\xdd\xc7\x02\xe4\x04\xe1\x9a\x51\x83\xd7\x69\x67\x65\x20" "\xe9\x8a\x26\x33\x45\xe4\x4d\x51\x87\xfa\xb3\xc4\xd8\x6a\xbe\xb1\x23\x03" "\xff\x13\x9f\xe0\xd0\x00\x00\x00\xd6\x04\x00\x00\x00\x00\x00\x00\x00\x8a" "\xff\x66\xd6\xb3\x18\x1f\xfc\x1d\x62\xa3\x95\x4c\x11\xd3\x78\x39\xdc\x00" "\x7c\x4d\x29\x6e\x73\x59\xea\x79\xa7\x5d\x81\x00\x00\x00\xfa\x13\xae\xe4" "\x8c\xa9\xe8\x96\x9f\xae\xbf\x31\x83\xfe\x80\x3a\xbb\xf5\x02\x4b\x52\xdc" "\x2c\xc2\xdf\x2e\x26\x5b\x36\xfc\x9d\xae\x00\xa0\xd0\x95\x6d\x25\x2b\xd8" "\xb6\x46\x4e\xf3\xc6\xa7\x35\x2c\xe7\x43\x90\x5f\xd6\xde\xf8\xba\xd3\xca" "\x6e\x3a\xbd\xb2\xdf\xc6\x16\x96\xe3\x40\xbb\x8e\x2a\x09\x3a\xdc\x57\x19" "\x6b\x40\xde\xf3\x85\x8e\xf5\x69\x14\x7f\xa4\x10\x83\x28\x39\x2d\x32\x2a" "\xb4\xdf\x10\xa2\xf6\x9a\x6b\xdf\x72\x57\xd3\x27\x07\x0e\x42\x41\x0f\x57" "\x46\x6f\x59\xae\xa2\x54\x40\x47\xd6\xd8\xac\x44\x2e\x00\x00\x00\x00\x00" "\xee\x16\xc7\x29\x30\x0d\x23\x01\x80\x00\x00\x00\x00\x00\x00\x28\xa0\xb3" "\x67\x54\xed\x52\x90\xa8\xcd\x84\x70\xe7\x76\xd6\x1e\x52\x54\x75\x3f\x78" "\x3d\x86\xd1\x0d\x1a\xb8\x98\x19\x6b\x8f\xb8\x06\x30\xd6\xcb\xde\x49\xb2" "\x9a\x6c\xb5\xf4\xfc\x00\x01\x00\x00\x00\x00\x4b\x58\x8c\x74\x5c\x38\x0e" "\x5f\xe5\x72\x38\xae\xad\xa5\xac\xf3\x20\x9a\x08\x43\x9f\xc6\x31\x03\x86" "\x59\x77\x60\x52\x5b\xfe\x5f\xe1\xf6\x97\xbc\x11\x4c\xd1\x77\x8e\x97\xa3" "\xf0\x29\x5f\x94\x69\x74\xcd\xb4\x58\xbe\x32\x34\xcf\x92\x4d\xc3\x6b\x22" "\xeb\x29\x71\x25\xfd\x60\xc5\x55\x8f\xbf\x17\xa7\x6f\x35\x47\x49\x7a\xba" "\x50\x86\xe3\x59\xc1\xb1\xe3\xdb\xbf\x13\x0e\xc8\xa5\x7c\x81\x43\x82\xff" "\xab\x04\x5c\xa0\x77\xa9\xd1\x52\x51\x87\x54\x32\xe7\x4b\x54\xaf\xaf\x49" "\x85\x67\x2a\x1c\x7b\x3c\x20\x00\x21\xde\x95\xae\x7b\x68\x13\x6b\x00\x46" "\xd5\x35\xdd\x39\xc0\xf3\x54\x69\x86\x9e\x9b\x34\x2b\x95\x3f\x81\x44\x7e" "\x6b\x9e\x52\x2d\x62\xb1\xe6\xff\xca\xab\x30\x4f\x13\x43\x06\x33\x5f\xc7" "\xa4\x41\x95\x25\x4b\x45\xa6\xc1\x31\x2a\x13\x69\x6c\x72\x02\xdf\x5f\x76" "\x47\x13\x50\x4f\x94\xc5\xe0\xfb\xc7\x0b\xcb\x97\x5f\x97\xed\x7b\x03\x00" "\x00\x00\x00\x00\x00\xed\xe4\x4e\x90\x72\xa2\x2d\x91\x74\x4a\x33\x2e\x2f" "\xa8\x06\xe6\x3c\x5c\xd9\x8a\x85\x69\xa6\xd6\xbc\xfb\x00\x00\x00\x2c\xf6" "\xc7\x3d\xc6\x3f\x04\xaf\x77\xc9\x72\x14\x59\xab\xfc\xfa\x1e\x97\x73\xb2" "\xb7\x13\x0e\xae\x67\xe0\xeb\xe3\x80\xd0\xf6\x48\x71\x3e\x68\x15\x35\x79" "\xc0\x2d\x71\xc5\x8d\x14\x7b\x00\x82\x1a\xb9\xa6\x47\x5b\x31\xe1\xeb\xf1" "\x36\x9a\xfe\x98\x68\x2e\xfb\xf3\x98\x3f\x28\x3f\x2f\xaf\x8f\x40\xe3\x99" "\x27\xac\xa9\xec\x52\x7f\xb5\xb6\xbf\x7e\x7b\x03\x74\x81\x4d\x63\x4b\x0a" "\x38\x85\x69\x29\xe7\xd8\xb1\xb0\x6c\x9b\xd5\xd7\xe5\x49\x0f\x3b\x85\x96" "\xb6\x94\xea\x94\x83\xbd\x4b\xd2\x87\xc8\x3d\xf9\x98\xa7\x46\x94\x26\xec" "\x8b\x00\x00\x00\x00\x00\x00\x00\x10\xff\x2c\xd1\x8b\xdd\x8a\xb7\x98\x3b" "\xc9\x07\x70\xbb\xd2\x6a\x82\xb9\xd9\x9d\x17\xc0\x2a\x97\xb5\x23\x04\x87" "\x78\x2c\xa0\x0e\xdf\x8e\x47\xa7\x1b\xcc\x73\x8e\xf6\x36\xd3\x2b\x01\x93" "\x35\x56\x79\xaf\xe7\x72\xcd\x45\xaf\x0a\x40\x1f\x69\x93\x05\xfa\x1e\x70" "\x0b\xd3\xc0\x6c\xb7\x8a\xef\xfe\x27\x53\x08\xa9\x0a\xcb\x1a\x21\x0b\x22" "\x45\x3b\x05\xed\x4c\x63\x8a\x04\xfa\x02\xaf\xf7\xd3\x52\xdc\xf7\x2b\xe8" "\x3e\x7c\x4c\x27\x10\x4a\xc2\x12\x6b\xb2\xbf\xc2\x16\x2f\x6e\x46\xc6\xc4" "\x19\x05\x4e\x5d\xc5\xc0\x95\x4d\x21\xef\x4f\x42\xfc\x63\xd3\x9b\x94\xa7" "\xf8\x38\xe0\x4b\xa7\x7f\x13\x67\xc1\xa2\x8c\x73\xa6\x99\xee\x47\x69\x95" "\x0b\xc8\xb3\xbb\xd0\x78\x61\x02\x00\x00\x00\x00\x00\x00\x00\xe3\xc1\x3f" "\x7d\x3a\x43\x31\x58\x27\xe2\xa4\xbc\x47\x44\xef\x9d\x64\xfd\xfa\xd9\x1c" "\x77\x60\xba\x4a\xa9\xf3\x85\x0d\xba\x7c\xa4\x2e\x00\x72\xcc\x0b\x34\x6d" "\xce\xbe\x06\x44\x22\xf0\x80\x73\x81\x2e\xc5\xe7\xcd\xcc\x26\x49\x98\xb4" "\xa6\x99\x4e\xfd\x9f\x6b\x7a\x9b\x5d\x15\x24\x7b\xf4\xfa\xbc\xff\x7c\x89" "\x0c\x23\x8f\x87\x3e\x6f\x52\xad\xf0\xf5\xaa\xf5\x12\x5a\x0a\x68\x3c\x05" "\xd5\x60\x63\x0b\x9f\x88\x44\xbe\x77\xe8\x43\x64\xfe\x4e\x39\x29\xea\x4c" "\x0d\xc8\x9a\x63\x52\xfe\x5a\xd1\xa1\x8d\x09\x3d\x89\xbd\x9b\xfc\x59\xe6" "\x8a\x6b\xb5\xe0\x91\x2f\x19\x67\x3d\x1b\xc4\x21\x07\x2f\x3a\x98\xb3\x1d" "\x38\x1a\x1d\xf1\xb9\x7e\x39\x34\x09\xd4\x27\x18\xc2\x0d\x41\x50\x01\x70" "\x33\xc4\xf7\x04\x5c\x79\x3d\xfa\xed\x00\xed\x70\x5d\x7e\xf8\xaa\x7d\xff" "\xde\xec\x68\x0c\x3b\xba\xd5\x59\x5d\xa7\x04\x90\x34\xe7\xf5\x1c\xc4\x07" "\x8c\x58\x0f\x8c\x97\x39\x6b\x26\xb2\xd0\x17\xc2\x74\x56\x0c\xc7\xdf\x0d" "\xe2\x44\xd7\x20\x09\xd2\x3d\x83\x83\x20\xac\x67\x7b\xb1\x4c\x34\xd1\x75" "\x98\x0a\xaf\xbb\x2e\xfb\xab\x23\x0e\x00\x00\x2c\x07\x36\xcd\x7a\x53\x1b" "\xe8\x0d\x64\x67\x9a\xfb\x87\xff\x2c\xb1\x54\x1a\xa7\x2e\x1b\xad\x33\x25" "\x83\x58\x9f\x2b\x66\x34\xbb\x6b\x64\xc4\xd1\x33\x42\x31\x77\x9d\x38\xf5" "\x9f\x67\x65\x90\x99\x4b\xf8\x6b\xd3\x68\xa3\x37\x25\xec\xb4\x73\x9c\x45" "\xa8\x15\x6c\x7a\xdb\x10\xff\x24\x0d\x3a\x24\x2a\x0c\xc8\x9d\xea\x64\xb8" "\x7b", 1243); *(uint64_t*)0x20000290 = 0x20000040; memcpy((void*)0x20000040, "GPL\000", 4); *(uint32_t*)0x20000298 = 4; *(uint32_t*)0x2000029c = 0x1076; *(uint64_t*)0x200002a0 = 0x20000300; *(uint32_t*)0x200002a8 = 0; *(uint32_t*)0x200002ac = 0; *(uint8_t*)0x200002b0 = 0; *(uint8_t*)0x200002b1 = 0; *(uint8_t*)0x200002b2 = 0; *(uint8_t*)0x200002b3 = 0; *(uint8_t*)0x200002b4 = 0; *(uint8_t*)0x200002b5 = 0; *(uint8_t*)0x200002b6 = 0; *(uint8_t*)0x200002b7 = 0; *(uint8_t*)0x200002b8 = 0; *(uint8_t*)0x200002b9 = 0; *(uint8_t*)0x200002ba = 0; *(uint8_t*)0x200002bb = 0; *(uint8_t*)0x200002bc = 0; *(uint8_t*)0x200002bd = 0; *(uint8_t*)0x200002be = 0; *(uint8_t*)0x200002bf = 0; *(uint32_t*)0x200002c0 = 0; *(uint32_t*)0x200002c4 = 0; *(uint32_t*)0x200002c8 = -1; *(uint32_t*)0x200002cc = 8; *(uint64_t*)0x200002d0 = 0; *(uint32_t*)0x200002d8 = 0; *(uint32_t*)0x200002dc = 0x10; *(uint64_t*)0x200002e0 = 0; *(uint32_t*)0x200002e8 = 0; *(uint32_t*)0x200002ec = 0; *(uint32_t*)0x200002f0 = -1; res = syscall(__NR_bpf, 5ul, 0x20000280ul, 0x70ul); if (res != -1) r[0] = res; memcpy((void*)0x20000240, "cpuacct.usage_percpu_sys\000", 25); res = syscall(__NR_openat, 0xffffff9c, 0x20000240ul, 0x26e1ul, 0ul); if (res != -1) r[1] = res; *(uint32_t*)0x20001300 = r[0]; *(uint32_t*)0x20001304 = r[1]; *(uint32_t*)0x20001308 = 0x25; *(uint32_t*)0x2000130c = 0; syscall(__NR_bpf, 0x1cul, 0x20001300ul, 0x10ul); } 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); use_temporary_dir(); do_sandbox_none(); return 0; }