// https://syzkaller.appspot.com/bug?id=4acb2aa300d1adc7695b387fb986b9ef00680908 // 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 #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_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 long syz_genetlink_get_family_id(volatile long name, volatile long sock_arg) { int fd = sock_arg; if (fd < 0) { fd = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); if (fd == -1) { return -1; } } struct nlmsg nlmsg_tmp; int ret = netlink_query_family_id(&nlmsg_tmp, fd, (char*)name, false); if ((int)sock_arg < 0) close(fd); if (ret < 0) { return -1; } return ret; } 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); } uint64_t r[4] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0x0}; void loop(void) { intptr_t res = 0; res = syscall(__NR_socket, 0x10ul, 3ul, 0); if (res != -1) r[0] = res; res = syscall(__NR_socket, 0x10ul, 3ul, 0); if (res != -1) r[1] = res; res = syscall(__NR_socket, 0x10ul, 0x803ul, 0); if (res != -1) r[2] = res; memcpy((void*)0x200000c0, "mptcp_pm\000", 9); syz_genetlink_get_family_id(0x200000c0, r[2]); *(uint32_t*)0x20000080 = 0x14; res = syscall(__NR_getsockname, r[2], 0x20000100ul, 0x20000080ul); if (res != -1) r[3] = *(uint32_t*)0x20000104; *(uint64_t*)0x20005840 = 0; *(uint32_t*)0x20005848 = 0; *(uint64_t*)0x20005850 = 0x200005c0; *(uint64_t*)0x200005c0 = 0x20001240; memcpy((void*)0x20001240, "\x48\x00\x00\x00\x24\x00\x51\x86\x00\x00\x00\x00\x00\xff\x00\x00\x00" "\x00\x00\x00", 20); *(uint32_t*)0x20001254 = r[3]; memcpy((void*)0x20001258, "\x00\x00\x00\x00\xff\xff\xff\xff\x00\x00\x00\x00\x08\x00\x01\x00\x68" "\x74\x62\x00\x1c\x00\x02\x00\x18\x00\x02\x00\x03", 29); *(uint64_t*)0x200005c8 = 0x48; *(uint64_t*)0x20005858 = 1; *(uint64_t*)0x20005860 = 0; *(uint64_t*)0x20005868 = 0; *(uint32_t*)0x20005870 = 0; syscall(__NR_sendmsg, r[1], 0x20005840ul, 0ul); *(uint64_t*)0x200001c0 = 0; *(uint32_t*)0x200001c8 = 0; *(uint64_t*)0x200001d0 = 0x20000180; *(uint64_t*)0x20000180 = 0x200010c0; *(uint32_t*)0x200010c0 = 0x70; *(uint16_t*)0x200010c4 = 0x2c; *(uint16_t*)0x200010c6 = 0xd27; *(uint32_t*)0x200010c8 = 0; *(uint32_t*)0x200010cc = 0; *(uint8_t*)0x200010d0 = 0x60; *(uint8_t*)0x200010d1 = 0; *(uint16_t*)0x200010d2 = 0; *(uint32_t*)0x200010d4 = r[3]; *(uint16_t*)0x200010d8 = 0; *(uint16_t*)0x200010da = 0; *(uint16_t*)0x200010dc = 0; *(uint16_t*)0x200010de = 0; *(uint16_t*)0x200010e0 = 2; *(uint16_t*)0x200010e2 = 0; *(uint16_t*)0x200010e4 = 0xa; *(uint16_t*)0x200010e6 = 1; memcpy((void*)0x200010e8, "basic\000", 6); *(uint16_t*)0x200010f0 = 0x40; *(uint16_t*)0x200010f2 = 2; *(uint16_t*)0x200010f4 = 0x3c; STORE_BY_BITMASK(uint16_t, , 0x200010f6, 2, 0, 14); STORE_BY_BITMASK(uint16_t, , 0x200010f7, 0, 6, 1); STORE_BY_BITMASK(uint16_t, , 0x200010f7, 1, 7, 1); *(uint16_t*)0x200010f8 = 0x30; STORE_BY_BITMASK(uint16_t, , 0x200010fa, 2, 0, 14); STORE_BY_BITMASK(uint16_t, , 0x200010fb, 0, 6, 1); STORE_BY_BITMASK(uint16_t, , 0x200010fb, 1, 7, 1); *(uint16_t*)0x200010fc = 0x2c; STORE_BY_BITMASK(uint16_t, , 0x200010fe, 1, 0, 14); STORE_BY_BITMASK(uint16_t, , 0x200010ff, 0, 6, 1); STORE_BY_BITMASK(uint16_t, , 0x200010ff, 0, 7, 1); *(uint16_t*)0x20001100 = 0; *(uint16_t*)0x20001102 = 9; *(uint16_t*)0x20001104 = 0; *(uint16_t*)0x20001106 = 0; *(uint16_t*)0x20001108 = 8; *(uint16_t*)0x2000110a = 1; *(uint32_t*)0x2000110c = 0; *(uint16_t*)0x20001110 = 5; *(uint16_t*)0x20001112 = 4; *(uint8_t*)0x20001114 = 0x10; *(uint16_t*)0x20001118 = 0xb; *(uint16_t*)0x2000111a = 2; memcpy((void*)0x2000111c, "policy\000", 7); *(uint16_t*)0x20001124 = 4; *(uint16_t*)0x20001126 = 5; *(uint16_t*)0x20001128 = 8; *(uint16_t*)0x2000112a = 1; *(uint16_t*)0x2000112c = 2; *(uint16_t*)0x2000112e = 0; *(uint64_t*)0x20000188 = 0x70; *(uint64_t*)0x200001d8 = 1; *(uint64_t*)0x200001e0 = 0; *(uint64_t*)0x200001e8 = 0; *(uint32_t*)0x200001f0 = 0; syscall(__NR_sendmsg, r[0], 0x200001c0ul, 0ul); } 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); do_sandbox_none(); return 0; }