Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp757502pxj; Fri, 11 Jun 2021 10:39:07 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwnIKZnTgSbszo+F3lv3MrkRBNVvktzFHvyqqFtnfA9we5M9fczAR/pIIgemDHQhnxo31y8 X-Received: by 2002:a17:906:840b:: with SMTP id n11mr4736672ejx.509.1623433147440; Fri, 11 Jun 2021 10:39:07 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1623433147; cv=none; d=google.com; s=arc-20160816; b=lYVjL8VImTkYxBP1ERGvrGx+guckROhzOzqQJRUTXypK9A34Q9uA1Ko3mSHvc1kXqT ot3py5oIv0vi2892Gm0bU/cGiea9Y6LbnmmYYp44mZZ9qEIgyh/frkthNZgpPcH++S4F 44OGYCyJeQd8E/gLMGSd0oFSwNPXs8NfxfFC1KI+5U0JiTOKwlrK5eFU08rTbmKoF4MR kluo+/8KE/2YcawXOitYxiZbaCNbYxJN4jH0hSTtNc2JJXq+LAAoftZcy0lICTIotjWQ RFrE8UfSGPy+HOiJ4spZS+aGY3NMk2BlkE2n9um734uKoibUT66W357rcKmNGACtRVXA ucng== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:cc:to:subject:message-id:date:from:in-reply-to :references:mime-version:dkim-signature; bh=kicSdfyPUPG0oZw0Yyms2mDBTb2EqDMsn04Ocyed7OA=; b=Y/yJjTwK0t4xpsTcIV+KxEH7c+ydHTLB6hxqfNXdPp4C/tDl5FKlvvimgw6bSyBw6M lIcKWJ+YXafcZpHaJ+6vxsNuFDn8Azwl6p1VtzvPRWZdOzvhDT//j/alWxBRof1+HveT eM1xbxCLSB/DttTbNz/SDP1XBVwI3BxkCw7tq+msadmr7amlZodtHaYMGVAo6yYNbZGw 5GWQ3RF5xfRjk7hsaZZVdcOafCak3hv62AW3vMi+rPfbQqEt3etGG26z8kFpCG2h68AX BUO0PIhh0/AqVdSR71dmC0J9JTaLruyWNgoD+nMGQd/lySb3zCdkgeQDRmHDbA3hu2nS +X7A== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@paul-moore-com.20150623.gappssmtp.com header.s=20150623 header.b=xB5cCJAt; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ap18si5295951ejc.166.2021.06.11.10.38.43; Fri, 11 Jun 2021 10:39:07 -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; dkim=pass header.i=@paul-moore-com.20150623.gappssmtp.com header.s=20150623 header.b=xB5cCJAt; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230355AbhFKRji (ORCPT + 99 others); Fri, 11 Jun 2021 13:39:38 -0400 Received: from mail-ed1-f53.google.com ([209.85.208.53]:42536 "EHLO mail-ed1-f53.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229548AbhFKRjh (ORCPT ); Fri, 11 Jun 2021 13:39:37 -0400 Received: by mail-ed1-f53.google.com with SMTP id i13so38026432edb.9 for ; Fri, 11 Jun 2021 10:37:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paul-moore-com.20150623.gappssmtp.com; s=20150623; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc; bh=kicSdfyPUPG0oZw0Yyms2mDBTb2EqDMsn04Ocyed7OA=; b=xB5cCJAthgJGLgDcW5FU+YuJlwc9WkRntsmvFSTnAC8U6tvjQ5SDTlmFxMTNrH1pSp X+L07kyYXu8Tz+dl1ZA6u6m0+mb9UDaM4ofl5hLaFgJlZoiA3ue6VvFqv96WlefCBOse HqKqcBp+bsDnAtZbcDTAaOIGvRUZ4dxXVQEc0G6D1wXMqiUZwLI2P3qiwDFMrH214BeJ DHtTrO/kvGwCqketn8SYpwictc/Hf/z+NyBnso7vry4ONhbf4uTTK4/R4wRCA5cg9NWs 9xGNVVcP6ZP0T1l0ldjLugscRSXyvQ59DibfLXUvflBIyDwSn1MXCy+anjNcIxMjNzUz X7iA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc; bh=kicSdfyPUPG0oZw0Yyms2mDBTb2EqDMsn04Ocyed7OA=; b=mo18U7F2a82D/Bsj9u71i85PBbCBOjriCSrNcS+nqOk7MTKtGsu+1bV0HiP2/Fiewj gMA28onEmED2Hbm/8z8pOV9bZS18657nXAOK4rw05wlztEZP9lpfHg/mLTeRVfIhtpAo CbQfCg2F0ke0PZZA8bkujpcCs6dd2fdzX6C+grwQU5BdOoqwpK0XIX3HR5024ArHFH17 q/2BWBRjprvaWlOOxpml0r21Px0WoSGwaPjp5DSDPtg2HR1wg2h2qqMld9xp7PSReRZr CHaIjJ4JBG+3tRmYcB9R//juqj8MF8zPPnpzi0v/Ek0uqXdQHxXXgnF4LQE4Ywe7k7Nk r3Vg== X-Gm-Message-State: AOAM532DIB8vSOL5Z7P/s6n5mS6Ibf4EJoKO03mihNTK/c9LJKxcpfCz w/g+YyNKNo3kPnHKQuGdy1niMCcserG6SnZ5fLJA X-Received: by 2002:aa7:c0d3:: with SMTP id j19mr5029543edp.196.1623432998161; Fri, 11 Jun 2021 10:36:38 -0700 (PDT) MIME-Version: 1.0 References: <20210611082119.2117194-1-liushixin2@huawei.com> In-Reply-To: <20210611082119.2117194-1-liushixin2@huawei.com> From: Paul Moore Date: Fri, 11 Jun 2021 13:36:27 -0400 Message-ID: Subject: Re: [PATCH -next v2] netlabel: Fix memory leak in netlbl_mgmt_add_common To: Liu Shixin Cc: Dongliang Mu , "David S. Miller" , Jakub Kicinski , netdev@vger.kernel.org, linux-security-module@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Fri, Jun 11, 2021 at 3:50 AM Liu Shixin wrote: > > 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; You should use the 'void *pmap = NULL;' style that is used in the rest of this function, and most everywhere in the kernel. > 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; There is no need for the extra vertical whitespace here. > 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; > + Please don't add extra vertical whitespace here. > #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; Same thing, no extra vertical whitespace please. -- paul moore www.paul-moore.com