// https://syzkaller.appspot.com/bug?id=23b42017412e73fb730cffe0826ecc55030f8583 // 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 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 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; } const int kInitNetNsFd = 201; static long syz_init_net_socket(volatile long domain, volatile long type, volatile long proto) { return syscall(__NR_socket, domain, type, proto); } 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"); } #define KMEMLEAK_FILE "/sys/kernel/debug/kmemleak" static const char* setup_leak() { if (!write_file(KMEMLEAK_FILE, "scan=off")) { if (errno == EBUSY) return "KMEMLEAK disabled: increase CONFIG_DEBUG_KMEMLEAK_EARLY_LOG_SIZE" " or unset CONFIG_DEBUG_KMEMLEAK_DEFAULT_OFF"; return "failed to write(kmemleak, \"scan=off\")"; } if (!write_file(KMEMLEAK_FILE, "scan")) return "failed to write(kmemleak, \"scan\")"; sleep(5); if (!write_file(KMEMLEAK_FILE, "scan")) return "failed to write(kmemleak, \"scan\")"; if (!write_file(KMEMLEAK_FILE, "clear")) return "failed to write(kmemleak, \"clear\")"; return NULL; } static void check_leaks(void) { int fd = open(KMEMLEAK_FILE, O_RDWR); if (fd == -1) exit(1); uint64_t start = current_time_ms(); if (write(fd, "scan", 4) != 4) exit(1); sleep(1); while (current_time_ms() - start < 4 * 1000) sleep(1); if (write(fd, "scan", 4) != 4) exit(1); static char buf[128 << 10]; ssize_t n = read(fd, buf, sizeof(buf) - 1); if (n < 0) exit(1); int nleaks = 0; if (n != 0) { sleep(1); if (write(fd, "scan", 4) != 4) exit(1); if (lseek(fd, 0, SEEK_SET) < 0) exit(1); n = read(fd, buf, sizeof(buf) - 1); if (n < 0) exit(1); buf[n] = 0; char* pos = buf; char* end = buf + n; while (pos < end) { char* next = strstr(pos + 1, "unreferenced object"); if (!next) next = end; char prev = *next; *next = 0; fprintf(stderr, "BUG: memory leak\n%s\n", pos); *next = prev; pos = next; nleaks++; } } if (write(fd, "clear", 5) != 5) exit(1); close(fd); if (nleaks) exit(1); } static void execute_one(void); #define WAIT_FLAGS __WALL static void loop(void) { int iter = 0; for (;; iter++) { int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { setup_test(); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { 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; } check_leaks(); } } uint64_t r[4] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff}; void execute_one(void) { intptr_t res = 0; if (write(1, "executing program\n", sizeof("executing program\n") - 1)) { } // socket$inet arguments: [ // domain: const = 0x2 (8 bytes) // type: socket_type = 0x4000000000000001 (8 bytes) // proto: int32 = 0x0 (4 bytes) // ] // returns sock_in syscall(__NR_socket, /*domain=*/2ul, /*type=SOCK_STREAM|0x4000000000000000*/ 0x4000000000000001ul, /*proto=*/0); // socket$inet_sctp arguments: [ // domain: const = 0x2 (8 bytes) // type: sctp_socket_type = 0x1 (8 bytes) // proto: const = 0x84 (4 bytes) // ] // returns sock_sctp syscall(__NR_socket, /*domain=*/2ul, /*type=SOCK_STREAM*/ 1ul, /*proto=*/0x84); // socket$inet6_sctp arguments: [ // domain: const = 0xa (8 bytes) // type: sctp_socket_type = 0x5 (8 bytes) // proto: const = 0x84 (4 bytes) // ] // returns sock_sctp6 syscall(__NR_socket, /*domain=*/0xaul, /*type=SOCK_SEQPACKET*/ 5ul, /*proto=*/0x84); // socket$nl_route arguments: [ // domain: const = 0x10 (8 bytes) // type: const = 0x3 (8 bytes) // proto: const = 0x0 (4 bytes) // ] // returns sock_nl_route syscall(__NR_socket, /*domain=*/0x10ul, /*type=*/3ul, /*proto=*/0); // openat$nci arguments: [ // fd: const = 0xffffffffffffff9c (8 bytes) // file: ptr[in, buffer] { // buffer: {2f 64 65 76 2f 76 69 72 74 75 61 6c 5f 6e 63 69 00} (length // 0x11) // } // flags: const = 0x2 (4 bytes) // mode: const = 0x0 (2 bytes) // ] // returns fd_nci memcpy((void*)0x200000000080, "/dev/virtual_nci\000", 17); syscall(__NR_openat, /*fd=*/0xffffffffffffff9cul, /*file=*/0x200000000080ul, /*flags=*/2, /*mode=*/0); // syz_init_net_socket$nl_generic arguments: [ // domain: const = 0x10 (8 bytes) // type: const = 0x3 (8 bytes) // proto: const = 0x10 (8 bytes) // ] // returns sock_nl_generic_init res = -1; res = syz_init_net_socket(/*domain=*/0x10, /*type=*/3, /*proto=*/0x10); if (res != -1) r[0] = res; // syz_init_net_socket$nl_generic arguments: [ // domain: const = 0x10 (8 bytes) // type: const = 0x3 (8 bytes) // proto: const = 0x10 (8 bytes) // ] // returns sock_nl_generic_init syz_init_net_socket(/*domain=*/0x10, /*type=*/3, /*proto=*/0x10); // socket$nl_route arguments: [ // domain: const = 0x10 (8 bytes) // type: const = 0x3 (8 bytes) // proto: const = 0x0 (4 bytes) // ] // returns sock_nl_route syscall(__NR_socket, /*domain=*/0x10ul, /*type=*/3ul, /*proto=*/0); // socketpair$unix arguments: [ // domain: const = 0x1 (8 bytes) // type: unix_socket_type = 0x2 (8 bytes) // proto: const = 0x0 (4 bytes) // fds: ptr[out, unix_pair] { // unix_pair { // fd0: sock_unix (resource) // fd1: sock_unix (resource) // } // } // ] res = syscall(__NR_socketpair, /*domain=*/1ul, /*type=SOCK_DGRAM*/ 2ul, /*proto=*/0, /*fds=*/0x200000000040ul); if (res != -1) { r[1] = *(uint32_t*)0x200000000040; r[2] = *(uint32_t*)0x200000000044; } // sendmsg$inet arguments: [ // fd: sock (resource) // msg: ptr[in, msghdr_inet] { // msghdr_inet { // msg_name: nil // msg_namelen: len = 0x0 (4 bytes) // pad = 0x0 (4 bytes) // msg_iov: nil // msg_iovlen: len = 0x0 (8 bytes) // msg_control: ptr[inout, array[ANYUNION]] { // array[ANYUNION] { // union ANYUNION { // ANYBLOB: buffer: {28 01 00 00 00 00 00 00 01 00 00 00 01} // (length 0xd) // } // } // } // msg_controllen: bytesize = 0x128 (8 bytes) // msg_flags: const = 0x0 (4 bytes) // pad = 0x0 (4 bytes) // } // } // f: send_flags = 0x0 (8 bytes) // ] *(uint64_t*)0x200000001b00 = 0; *(uint32_t*)0x200000001b08 = 0; *(uint64_t*)0x200000001b10 = 0; *(uint64_t*)0x200000001b18 = 0; *(uint64_t*)0x200000001b20 = 0x200000001d80; memcpy((void*)0x200000001d80, "\x28\x01\x00\x00\x00\x00\x00\x00\x01\x00\x00\x00\x01", 13); *(uint64_t*)0x200000001b28 = 0x128; *(uint32_t*)0x200000001b30 = 0; syscall(__NR_sendmsg, /*fd=*/r[2], /*msg=*/0x200000001b00ul, /*f=*/0ul); // recvmsg$unix arguments: [ // fd: sock_unix (resource) // msg: ptr[inout, recv_msghdr_un] { // recv_msghdr_un { // addr: nil // addrlen: len = 0x0 (4 bytes) // pad = 0x0 (4 bytes) // vec: nil // vlen: len = 0x0 (8 bytes) // ctrl: ptr[out, array[cmsghdr_un]] { // array[cmsghdr_un] { // union cmsghdr_un { // cred: cmsghdr_t[SOL_SOCKET, SCM_CREDENTIALS, cmsg_un_cred] { // unaligned: cmsghdr_unaligned[SOL_SOCKET, SCM_CREDENTIALS, // cmsg_un_cred] { // cmsg_len: len = 0x1c (8 bytes) // cmsg_level: const = 0x1 (4 bytes) // cmsg_type: const = 0x2 (4 bytes) // data: cmsg_un_cred { // pid: pid (resource) // uid: uid (resource) // gid: gid (resource) // } // } // pad = 0x0 (4 bytes) // } // } // union cmsghdr_un { // cred: cmsghdr_t[SOL_SOCKET, SCM_CREDENTIALS, cmsg_un_cred] { // unaligned: cmsghdr_unaligned[SOL_SOCKET, SCM_CREDENTIALS, // cmsg_un_cred] { // cmsg_len: len = 0x1c (8 bytes) // cmsg_level: const = 0x1 (4 bytes) // cmsg_type: const = 0x2 (4 bytes) // data: cmsg_un_cred { // pid: pid (resource) // uid: uid (resource) // gid: gid (resource) // } // } // pad = 0x0 (4 bytes) // } // } // union cmsghdr_un { // cred: cmsghdr_t[SOL_SOCKET, SCM_CREDENTIALS, cmsg_un_cred] { // unaligned: cmsghdr_unaligned[SOL_SOCKET, SCM_CREDENTIALS, // cmsg_un_cred] { // cmsg_len: len = 0x1c (8 bytes) // cmsg_level: const = 0x1 (4 bytes) // cmsg_type: const = 0x2 (4 bytes) // data: cmsg_un_cred { // pid: pid (resource) // uid: uid (resource) // gid: gid (resource) // } // } // pad = 0x0 (4 bytes) // } // } // union cmsghdr_un { // rights: cmsghdr_t[SOL_SOCKET, SCM_RIGHTS, array[fd]] { // unaligned: cmsghdr_unaligned[SOL_SOCKET, SCM_RIGHTS, // array[fd]] { // cmsg_len: len = 0x18 (8 bytes) // cmsg_level: const = 0x1 (4 bytes) // cmsg_type: const = 0x1 (4 bytes) // data: array[fd] { // fd (resource) // fd (resource) // } // } // } // } // } // } // ctrllen: bytesize = 0x78 (8 bytes) // f: const = 0x0 (4 bytes) // pad = 0x0 (4 bytes) // } // } // f: recv_flags = 0x0 (8 bytes) // ] *(uint64_t*)0x200000000000 = 0; *(uint32_t*)0x200000000008 = 0; *(uint64_t*)0x200000000010 = 0; *(uint64_t*)0x200000000018 = 0; *(uint64_t*)0x200000000020 = 0x200000000f40; *(uint64_t*)0x200000000028 = 0x78; *(uint32_t*)0x200000000030 = 0; res = syscall(__NR_recvmsg, /*fd=*/r[1], /*msg=*/0x200000000000ul, /*f=*/0ul, 0); if (res != -1) r[3] = *(uint32_t*)0x200000000fb4; // sendmsg$NFC_CMD_DEV_UP arguments: [ // fd: sock_nl_generic_init (resource) // msg: ptr[in, msghdr_netlink[netlink_msg_t[genl_nfc_family_id, // genlmsghdr_t[NFC_CMD_DEV_UP], nfc_genl_policy_dev_index]]] { // msghdr_netlink[netlink_msg_t[genl_nfc_family_id, // genlmsghdr_t[NFC_CMD_DEV_UP], nfc_genl_policy_dev_index]] { // addr: nil // addrlen: len = 0x0 (4 bytes) // pad = 0x0 (4 bytes) // vec: ptr[in, iovec[in, netlink_msg_t[genl_nfc_family_id, // genlmsghdr_t[NFC_CMD_DEV_UP], nfc_genl_policy_dev_index]]] { // iovec[in, netlink_msg_t[genl_nfc_family_id, // genlmsghdr_t[NFC_CMD_DEV_UP], nfc_genl_policy_dev_index]] { // addr: ptr[inout, array[ANYUNION]] { // array[ANYUNION] { // union ANYUNION { // ANYBLOB: buffer: {1c 00 00 00} (length 0x4) // } // union ANYUNION { // ANYRES16: ANYRES16 (resource) // } // union ANYUNION { // ANYBLOB: buffer: {01 00 28 bd 70 00 07 00 00 00 02 00 00 // 00 08 00 01} (length 0x11) // } // } // } // len: len = 0x1c (8 bytes) // } // } // vlen: const = 0x1 (8 bytes) // ctrl: const = 0x0 (8 bytes) // ctrllen: const = 0x0 (8 bytes) // f: send_flags = 0x40089 (4 bytes) // pad = 0x0 (4 bytes) // } // } // f: send_flags = 0x8004 (8 bytes) // ] *(uint64_t*)0x200000000180 = 0; *(uint32_t*)0x200000000188 = 0; *(uint64_t*)0x200000000190 = 0x200000000280; *(uint64_t*)0x200000000280 = 0x200000000140; memcpy((void*)0x200000000140, "\x1c\x00\x00\x00", 4); *(uint16_t*)0x200000000144 = r[3]; memcpy((void*)0x200000000146, "\x01\x00\x28\xbd\x70\x00\x07\x00\x00\x00\x02\x00\x00\x00\x08\x00\x01", 17); *(uint64_t*)0x200000000288 = 0x1c; *(uint64_t*)0x200000000198 = 1; *(uint64_t*)0x2000000001a0 = 0; *(uint64_t*)0x2000000001a8 = 0; *(uint32_t*)0x2000000001b0 = 0x40089; syscall(__NR_sendmsg, /*fd=*/r[0], /*msg=*/0x200000000180ul, /*f=MSG_MORE|MSG_DONTROUTE*/ 0x8004ul); } 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); const char* reason; (void)reason; if ((reason = setup_leak())) printf("the reproducer may not work as expected: leak checking setup " "failed: %s\n", reason); loop(); return 0; }