Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp367023pxj; Fri, 11 Jun 2021 00:53:57 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzxqVWN0sHx6N3iW0qVCVdM7KSa2ziwojRaEWt7TWhPjuJpOp6eKKIwFzz+QGfjr/h5Ap34 X-Received: by 2002:a17:906:eb17:: with SMTP id mb23mr2527364ejb.239.1623398037338; Fri, 11 Jun 2021 00:53:57 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623398037; cv=none; d=google.com; s=arc-20160816; b=tZHzGCPEXEQ+d5c6oNyMa2rbXH3qkYzOrtmfNSfybwAAXkEDNefD73ThdJVgtVoJub k1UiSy/esmaIsekJT5l9piZ2TgpQRThgoEIcMOTAVlNELOMUVQ2ycCaaBRNsafW02Ez3 vAUQNWepAuEkOJjfTPjb2gUn/WEJCj7jIcvst3MVNbXLCvJV04AF/CmoVqyZTtFaeniV 7Kg13EmujfDH2jl1mcy1IYdLi72VOM18W0RYFIAiSDzCG67wGJgz1cRTSvy7RDxOiPfb sTljRihGhf7ojpDPtx6IYmmvB6lmU8cXlmD3jEZq588P2VgDkB0vi8VBtDODnO2CqvzA TnjQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:mime-version:message-id:date:subject:cc:to:from; bh=KZS+mB0S8Vf42mACDchGohJ+JdzUjOYU1j5VVu2tnUo=; b=oWYcblt04FrIua+RQGo73XLTV4m3bF8Dnpm+hO1IP+EJ0MffjNEPLCH09eFI1PSQ5B EF2anDZYL5VkSMxmeIY3DFRxCUL4JOg38m9U1P9iRQJXW8YqvtEMq3jDIkcfvjJZGGWA ppb2wn6wnFrgV6Tq+H3Rp7L3OM6DCVFWEsjCeGr0kr7IktiPstnnSTeXjnRq/2YZwhal Ctqvgx50Ouk2J8MKDsVsiq0O/B55goWmI6h/peABYLXSHktZINbN5Eujd8R0VTO3fn0j if1lBi3GdH/CP4IHkB4D1chTE9OdWEVgmVBkOc81zG2NDOruxYJHHxq03x8E84ovC4S9 3JtQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v5si4263128ejv.698.2021.06.11.00.53.34; Fri, 11 Jun 2021 00:53:57 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231249AbhFKHv6 (ORCPT + 99 others); Fri, 11 Jun 2021 03:51:58 -0400 Received: from szxga03-in.huawei.com ([45.249.212.189]:5386 "EHLO szxga03-in.huawei.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229733AbhFKHv5 (ORCPT ); Fri, 11 Jun 2021 03:51:57 -0400 Received: from dggemv703-chm.china.huawei.com (unknown [172.30.72.57]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4G1Xvw0Kcqz6w6X; Fri, 11 Jun 2021 15:46:04 +0800 (CST) Received: from dggpemm500009.china.huawei.com (7.185.36.225) by dggemv703-chm.china.huawei.com (10.3.19.46) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Fri, 11 Jun 2021 15:49:57 +0800 Received: from huawei.com (10.175.113.32) by dggpemm500009.china.huawei.com (7.185.36.225) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2176.2; Fri, 11 Jun 2021 15:49:57 +0800 From: Liu Shixin To: Paul Moore , Dongliang Mu , "David S. Miller" , "Jakub Kicinski" CC: , , , Liu Shixin Subject: [PATCH -next v2] netlabel: Fix memory leak in netlbl_mgmt_add_common Date: Fri, 11 Jun 2021 16:21:19 +0800 Message-ID: <20210611082119.2117194-1-liushixin2@huawei.com> X-Mailer: git-send-email 2.18.0.huawei.25 MIME-Version: 1.0 Content-Type: text/plain X-Originating-IP: [10.175.113.32] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To dggpemm500009.china.huawei.com (7.185.36.225) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hulk Robot reported memory leak in netlbl_mgmt_add_common. The problem is non-freed map in case of netlbl_domhsh_add() failed. BUG: memory leak unreferenced object 0xffff888100ab7080 (size 96): comm "syz-executor537", pid 360, jiffies 4294862456 (age 22.678s) hex dump (first 32 bytes): 05 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................ fe 00 00 00 00 00 00 00 00 00 00 00 00 00 00 01 ................ backtrace: [<0000000008b40026>] netlbl_mgmt_add_common.isra.0+0xb2a/0x1b40 [<000000003be10950>] netlbl_mgmt_add+0x271/0x3c0 [<00000000c70487ed>] genl_family_rcv_msg_doit.isra.0+0x20e/0x320 [<000000001f2ff614>] genl_rcv_msg+0x2bf/0x4f0 [<0000000089045792>] netlink_rcv_skb+0x134/0x3d0 [<0000000020e96fdd>] genl_rcv+0x24/0x40 [<0000000042810c66>] netlink_unicast+0x4a0/0x6a0 [<000000002e1659f0>] netlink_sendmsg+0x789/0xc70 [<000000006e43415f>] sock_sendmsg+0x139/0x170 [<00000000680a73d7>] ____sys_sendmsg+0x658/0x7d0 [<0000000065cbb8af>] ___sys_sendmsg+0xf8/0x170 [<0000000019932b6c>] __sys_sendmsg+0xd3/0x190 [<00000000643ac172>] do_syscall_64+0x37/0x90 [<000000009b79d6dc>] entry_SYSCALL_64_after_hwframe+0x44/0xae Fixes: 63c416887437 ("netlabel: Add network address selectors to the NetLabel/LSM domain mapping") Reported-by: Hulk Robot Signed-off-by: Liu Shixin --- v1->v2: According to Dongliang's and Paul's advices, simplify the code. net/netlabel/netlabel_mgmt.c | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/net/netlabel/netlabel_mgmt.c b/net/netlabel/netlabel_mgmt.c index e664ab990941..fa9e68e5f826 100644 --- a/net/netlabel/netlabel_mgmt.c +++ b/net/netlabel/netlabel_mgmt.c @@ -76,6 +76,7 @@ static const struct nla_policy netlbl_mgmt_genl_policy[NLBL_MGMT_A_MAX + 1] = { static int netlbl_mgmt_add_common(struct genl_info *info, struct netlbl_audit *audit_info) { + void * pmap = NULL; int ret_val = -EINVAL; struct netlbl_domaddr_map *addrmap = NULL; struct cipso_v4_doi *cipsov4 = NULL; @@ -175,6 +176,8 @@ static int netlbl_mgmt_add_common(struct genl_info *info, ret_val = -ENOMEM; goto add_free_addrmap; } + + pmap = map; map->list.addr = addr->s_addr & mask->s_addr; map->list.mask = mask->s_addr; map->list.valid = 1; @@ -183,14 +186,13 @@ static int netlbl_mgmt_add_common(struct genl_info *info, map->def.cipso = cipsov4; ret_val = netlbl_af4list_add(&map->list, &addrmap->list4); - if (ret_val != 0) { - kfree(map); - goto add_free_addrmap; - } + if (ret_val != 0) + goto add_free_map; entry->family = AF_INET; entry->def.type = NETLBL_NLTYPE_ADDRSELECT; entry->def.addrsel = addrmap; + #if IS_ENABLED(CONFIG_IPV6) } else if (info->attrs[NLBL_MGMT_A_IPV6ADDR]) { struct in6_addr *addr; @@ -223,6 +225,8 @@ static int netlbl_mgmt_add_common(struct genl_info *info, ret_val = -ENOMEM; goto add_free_addrmap; } + + pmap = map; map->list.addr = *addr; map->list.addr.s6_addr32[0] &= mask->s6_addr32[0]; map->list.addr.s6_addr32[1] &= mask->s6_addr32[1]; @@ -235,10 +239,8 @@ static int netlbl_mgmt_add_common(struct genl_info *info, map->def.calipso = calipso; ret_val = netlbl_af6list_add(&map->list, &addrmap->list6); - if (ret_val != 0) { - kfree(map); - goto add_free_addrmap; - } + if (ret_val != 0) + goto add_free_map; entry->family = AF_INET6; entry->def.type = NETLBL_NLTYPE_ADDRSELECT; @@ -248,10 +250,12 @@ static int netlbl_mgmt_add_common(struct genl_info *info, ret_val = netlbl_domhsh_add(entry, audit_info); if (ret_val != 0) - goto add_free_addrmap; + goto add_free_map; return 0; +add_free_map: + kfree(pmap); add_free_addrmap: kfree(addrmap); add_doi_put_def: -- 2.18.0.huawei.25