Syzkaller hit 'memory leak in netlbl_mgmt_add_common - Fixed in v5.10.50' bug. executing program executing program BUG: memory leak unreferenced object 0xffff88800cc7fe80 (size 64): comm "syz-executor809", pid 381, jiffies 4294812444 (age 22.156s) hex dump (first 32 bytes): 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ ac 14 14 00 ac 14 14 00 01 00 00 00 00 00 00 00 ................ backtrace: [<000000004c614318>] kmalloc include/linux/slab.h:552 [inline] [<000000004c614318>] kzalloc include/linux/slab.h:664 [inline] [<000000004c614318>] netlbl_mgmt_add_common+0x105c/0x1770 net/netlabel/netlabel_mgmt.c:173 [<00000000c551115f>] netlbl_mgmt_adddef+0x25b/0x3a0 net/netlabel/netlabel_mgmt.c:562 [<00000000c77fae80>] genl_family_rcv_msg_doit+0x22d/0x330 net/netlink/genetlink.c:739 [<00000000c22a9668>] genl_family_rcv_msg net/netlink/genetlink.c:783 [inline] [<00000000c22a9668>] genl_rcv_msg+0x33c/0x5a0 net/netlink/genetlink.c:800 [<000000001c7ebc58>] netlink_rcv_skb+0x14b/0x430 net/netlink/af_netlink.c:2494 [<0000000041ff8321>] genl_rcv+0x24/0x40 net/netlink/genetlink.c:811 [<00000000e646903d>] netlink_unicast_kernel net/netlink/af_netlink.c:1304 [inline] [<00000000e646903d>] netlink_unicast+0x53a/0x7e0 net/netlink/af_netlink.c:1330 [<00000000baa4fe6a>] netlink_sendmsg+0x860/0xda0 net/netlink/af_netlink.c:1919 [<000000001c9cbc4c>] sock_sendmsg_nosec net/socket.c:651 [inline] [<000000001c9cbc4c>] sock_sendmsg+0x150/0x190 net/socket.c:671 [<0000000023c5a36b>] ____sys_sendmsg+0x709/0x870 net/socket.c:2353 [<000000007e50d7b6>] ___sys_sendmsg+0xf3/0x170 net/socket.c:2407 [<000000007d6d76d4>] __sys_sendmsg+0xe5/0x1b0 net/socket.c:2440 [<000000001b732217>] do_syscall_64+0x33/0x40 arch/x86/entry/common.c:46 [<0000000026e099b5>] entry_SYSCALL_64_after_hwframe+0x44/0xa9 BUG: memory leak unreferenced object 0xffff88800cc7fb80 (size 64): comm "syz-executor809", pid 382, jiffies 4294820959 (age 13.641s) hex dump (first 32 bytes): 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ ac 14 14 00 ac 14 14 00 01 00 00 00 00 00 00 00 ................ backtrace: [<000000004c614318>] kmalloc include/linux/slab.h:552 [inline] [<000000004c614318>] kzalloc include/linux/slab.h:664 [inline] [<000000004c614318>] netlbl_mgmt_add_common+0x105c/0x1770 net/netlabel/netlabel_mgmt.c:173 [<00000000c551115f>] netlbl_mgmt_adddef+0x25b/0x3a0 net/netlabel/netlabel_mgmt.c:562 [<00000000c77fae80>] genl_family_rcv_msg_doit+0x22d/0x330 net/netlink/genetlink.c:739 [<00000000c22a9668>] genl_family_rcv_msg net/netlink/genetlink.c:783 [inline] [<00000000c22a9668>] genl_rcv_msg+0x33c/0x5a0 net/netlink/genetlink.c:800 [<000000001c7ebc58>] netlink_rcv_skb+0x14b/0x430 net/netlink/af_netlink.c:2494 [<0000000041ff8321>] genl_rcv+0x24/0x40 net/netlink/genetlink.c:811 [<00000000e646903d>] netlink_unicast_kernel net/netlink/af_netlink.c:1304 [inline] [<00000000e646903d>] netlink_unicast+0x53a/0x7e0 net/netlink/af_netlink.c:1330 [<00000000baa4fe6a>] netlink_sendmsg+0x860/0xda0 net/netlink/af_netlink.c:1919 [<000000001c9cbc4c>] sock_sendmsg_nosec net/socket.c:651 [inline] [<000000001c9cbc4c>] sock_sendmsg+0x150/0x190 net/socket.c:671 [<0000000023c5a36b>] ____sys_sendmsg+0x709/0x870 net/socket.c:2353 [<000000007e50d7b6>] ___sys_sendmsg+0xf3/0x170 net/socket.c:2407 [<000000007d6d76d4>] __sys_sendmsg+0xe5/0x1b0 net/socket.c:2440 [<000000001b732217>] do_syscall_64+0x33/0x40 arch/x86/entry/common.c:46 [<0000000026e099b5>] entry_SYSCALL_64_after_hwframe+0x44/0xa9 Syzkaller reproducer: # {Threaded:false Collide:false Repeat:true RepeatTimes:0 Procs:1 Slowdown:1 Sandbox: Fault:false FaultCall:-1 FaultNth:0 Leak:true NetInjection:false NetDevices:false NetReset:false Cgroups:false BinfmtMisc:false CloseFDs:false KCSAN:false DevlinkPCI:false USB:false VhciInjection:false Wifi:false IEEE802154:false Sysctl:false UseTmpDir:false HandleSegv:false Repro:false Trace:false} r0 = syz_init_net_socket$nl_generic(0x10, 0x3, 0x10) r1 = syz_genetlink_get_family_id$netlbl_mgmt(&(0x7f00000002c0), r0) r2 = syz_init_net_socket$nl_generic(0x10, 0x3, 0x10) sendmsg$NLBL_MGMT_C_ADDDEF(r2, &(0x7f00000009c0)={0x0, 0x0, &(0x7f0000000980)={&(0x7f0000000000)={0x2c, r1, 0x221, 0x0, 0x0, {}, [@NLBL_MGMT_A_IPV4MASK={0x8, 0x8, @dev}, @NLBL_MGMT_A_PROTOCOL={0x8, 0x2, 0x5}, @NLBL_MGMT_A_IPV4ADDR={0x8, 0x7, @local}]}, 0x2c}}, 0x0) C reproducer: // 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 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; } 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; } const int kInitNetNsFd = 239; static long syz_init_net_socket(volatile long domain, volatile long type, volatile long proto) { return syscall(__NR_socket, domain, type, proto); } static long syz_genetlink_get_family_id(volatile long name, volatile long sock_arg) { bool dofail = false; int fd = sock_arg; if (fd < 0) { dofail = true; 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, dofail); if ((int)sock_arg < 0) close(fd); if (ret < 0) { return -1; } return ret; } 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 void setup_leak() { if (!write_file(KMEMLEAK_FILE, "scan")) exit(1); sleep(5); if (!write_file(KMEMLEAK_FILE, "scan")) exit(1); if (!write_file(KMEMLEAK_FILE, "clear")) exit(1); } 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 (;;) { 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; } check_leaks(); } } uint64_t r[3] = {0xffffffffffffffff, 0x0, 0xffffffffffffffff}; void execute_one(void) { intptr_t res = 0; res = -1; res = syz_init_net_socket(0x10, 3, 0x10); if (res != -1) r[0] = res; memcpy((void*)0x200002c0, "NLBL_MGMT\000", 10); res = -1; res = syz_genetlink_get_family_id(0x200002c0, r[0]); if (res != -1) r[1] = res; res = -1; res = syz_init_net_socket(0x10, 3, 0x10); if (res != -1) r[2] = res; *(uint64_t*)0x200009c0 = 0; *(uint32_t*)0x200009c8 = 0; *(uint64_t*)0x200009d0 = 0x20000980; *(uint64_t*)0x20000980 = 0x20000000; *(uint32_t*)0x20000000 = 0x2c; *(uint16_t*)0x20000004 = r[1]; *(uint16_t*)0x20000006 = 0x221; *(uint32_t*)0x20000008 = 0; *(uint32_t*)0x2000000c = 0; *(uint8_t*)0x20000010 = 4; *(uint8_t*)0x20000011 = 0; *(uint16_t*)0x20000012 = 0; *(uint16_t*)0x20000014 = 8; *(uint16_t*)0x20000016 = 8; *(uint8_t*)0x20000018 = 0xac; *(uint8_t*)0x20000019 = 0x14; *(uint8_t*)0x2000001a = 0x14; *(uint8_t*)0x2000001b = 0; *(uint16_t*)0x2000001c = 8; *(uint16_t*)0x2000001e = 2; *(uint32_t*)0x20000020 = 5; *(uint16_t*)0x20000024 = 8; *(uint16_t*)0x20000026 = 7; *(uint8_t*)0x20000028 = 0xac; *(uint8_t*)0x20000029 = 0x14; *(uint8_t*)0x2000002a = 0x14; *(uint8_t*)0x2000002b = 0xaa; *(uint64_t*)0x20000988 = 0x2c; *(uint64_t*)0x200009d8 = 1; *(uint64_t*)0x200009e0 = 0; *(uint64_t*)0x200009e8 = 0; *(uint32_t*)0x200009f0 = 0; syscall(__NR_sendmsg, r[2], 0x200009c0ul, 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); setup_leak(); loop(); return 0; }