Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp22705pxv; Tue, 13 Jul 2021 20:28:54 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzDGGQ3e21e7j2QflnNvcpvc+XbAlYU+HaOyaxLjIKlC2123/gCWcrlSv4XhGN6MpPDxpqz X-Received: by 2002:aa7:db50:: with SMTP id n16mr10328272edt.298.1626233333561; Tue, 13 Jul 2021 20:28:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626233333; cv=none; d=google.com; s=arc-20160816; b=G/Wqu+NPP1kDN3US3u+BmiWnQuiFjoxC5qvgs1GiusrPb/d6Az7T1Y32hgdfEZSs4H Y0j7zT8aRxmmCiYaYMgoKC7LXSQ+p0NZO4WfxjKcCTRkNRBSIysnDpapXTkcutFIpQ6R 3vjF0OQr8AN0260zqHWhc096hiJbtUMHzinODHSiSxQLn6AbPQ/o5kt1z9mBHE21kXW2 kGVq8GDtjeVtzo6tFRiVESAjOkhRhESsArhtTzjGOXWN1fBNGdF4hUC7DdGY3XTBgZeL 1WVvKHduskz31ASf4Kz+OOLtOuedYL2k7MzE8qv7WjUqnYzbYRMZJZo/pPv6lW9cCW14 EdjA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from:dkim-signature; bh=fs0dJT+CYtli8VWxcsALtkdK7y7s0huk1WucGWBqsvg=; b=jfA5h0SBgwfNp1G4iK+efRbH3UVqiPJNJfzW0BTguOugb5wEIX4nGWKHzxX44svO3k qNInmGWEaGnbjLutcQ9/HbSoDuJOxgrY4TA5BljLy8Cf+g3Dhp5rwAbHlnhJFrEXai5C KUyJnxnuxuyG6wUJDOPka1pOJ09W3GmdBu4Ldcjm5qL/rGc0tn4ALZqKuvyZ7JbA11fi oDeAJyRM7eWbn/3eQWyYfL/LjzC54G2QA5ld2jIeaqRpYSjeRDdwVihL0zKWtHogmBpv IeyEdSMcBNMq15y+myP8874sijky1iCwgxfD2qz8WpscWZG6EamOOXhdc6rw1KjjjZTE lAkA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=YSVZAGni; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id v1si1105361edx.481.2021.07.13.20.28.30; Tue, 13 Jul 2021 20:28:53 -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=@gmail.com header.s=20161025 header.b=YSVZAGni; 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=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=gmail.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S237798AbhGNDaU (ORCPT + 99 others); Tue, 13 Jul 2021 23:30:20 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:52048 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S237718AbhGNDaU (ORCPT ); Tue, 13 Jul 2021 23:30:20 -0400 Received: from mail-pg1-x52b.google.com (mail-pg1-x52b.google.com [IPv6:2607:f8b0:4864:20::52b]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4BACFC0613DD; Tue, 13 Jul 2021 20:27:29 -0700 (PDT) Received: by mail-pg1-x52b.google.com with SMTP id h4so951420pgp.5; Tue, 13 Jul 2021 20:27:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=fs0dJT+CYtli8VWxcsALtkdK7y7s0huk1WucGWBqsvg=; b=YSVZAGnid/hf0YZB3FKd81E5KtbuHdAr1ntK7uDTCuLdrCiu17CzsABDEyrexyZBB0 NSCmaRJLH0A4eEqMeFnes4OPXneQnKo67Tz6HrvZiwSG8QTd/NExNwBps5ob788rhEes 4osgtYqhCUHqxBwRy544Q8zvlTTH8eWC4HcH5oF3M10dx1jgmkBSNAgpGBH9uqfeb0ko vgXn5en82qCD5ltJf8Rd5aTdg7Vy/jBBbN/lMV50qn9FY9Y3Dai/WGQ82zC/+mdBlFzY Fm4yuuss0StGwxvC7+TdhisOx3XRxPC3uM1QjYVZCLyvoEdhYa3UPSYS5HEUiplt9BFP RM7A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=fs0dJT+CYtli8VWxcsALtkdK7y7s0huk1WucGWBqsvg=; b=g2KzBKO4T755A1DDgGsd5pdwN0+l5EpM4Sfg45gob9/XC+UMn3mtya5U86WdZW/N9L KLqH+G/NqoR1ZqPgGqYkfUav3dMRdXvwdalCb0KMnz70SHN93Y0hPOC6+3eclPZ/MeFE 1E3w4C6PqzcR5JgL3c3igPtelHQjpmCtAF/qUPuiWe4qiiGiRrFmHo4uW7Hqp16GbPZp zTQn2qaXN/sHvNttm65ooHCpCs+QHtiF/1z0stTI37tLd/uzJ5dwA54FmI/0PGCnNx71 HUN2ejdLjAITUmyNEqgWh8KfSVrDu8dWUL3/0iCErX3/KWl22GPRAQiZ16QNNQ/qw7ir O5fA== X-Gm-Message-State: AOAM530y4g4+AdGJSp5qLgdpLyNPpLZNLwgLhxDP79+BR5PA4TFFzqoD VnTlst1Te6s0Et7rgltzBvc= X-Received: by 2002:a65:6a42:: with SMTP id o2mr7141072pgu.316.1626233248729; Tue, 13 Jul 2021 20:27:28 -0700 (PDT) Received: from localhost.localdomain ([154.16.166.218]) by smtp.gmail.com with ESMTPSA id y5sm596970pfn.87.2021.07.13.20.27.24 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Jul 2021 20:27:28 -0700 (PDT) From: Dongliang Mu To: Pablo Neira Ayuso , Jozsef Kadlecsik , Florian Westphal , "David S. Miller" , Jakub Kicinski , Richard Guy Briggs , Paul Moore Cc: Dongliang Mu , syzbot , kernel test robot , netfilter-devel@vger.kernel.org, coreteam@netfilter.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH v3] audit: fix memory leak in nf_tables_commit Date: Wed, 14 Jul 2021 11:27:03 +0800 Message-Id: <20210714032703.505023-1-mudongliangabcd@gmail.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org In nf_tables_commit, if nf_tables_commit_audit_alloc fails, it does not free the adp variable. Fix this by adding nf_tables_commit_audit_free which frees the linked list with the head node adl. backtrace: kmalloc include/linux/slab.h:591 [inline] kzalloc include/linux/slab.h:721 [inline] nf_tables_commit_audit_alloc net/netfilter/nf_tables_api.c:8439 [inline] nf_tables_commit+0x16e/0x1760 net/netfilter/nf_tables_api.c:8508 nfnetlink_rcv_batch+0x512/0xa80 net/netfilter/nfnetlink.c:562 nfnetlink_rcv_skb_batch net/netfilter/nfnetlink.c:634 [inline] nfnetlink_rcv+0x1fa/0x220 net/netfilter/nfnetlink.c:652 netlink_unicast_kernel net/netlink/af_netlink.c:1314 [inline] netlink_unicast+0x2c7/0x3e0 net/netlink/af_netlink.c:1340 netlink_sendmsg+0x36b/0x6b0 net/netlink/af_netlink.c:1929 sock_sendmsg_nosec net/socket.c:702 [inline] sock_sendmsg+0x56/0x80 net/socket.c:722 Reported-by: syzbot Reported-by: kernel test robot Fixes: c520292f29b8 ("audit: log nftables configuration change events once per table") Signed-off-by: Dongliang Mu --- v1->v2: fix the compile issue v2->v3: modify the added function to a new name net/netfilter/nf_tables_api.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/net/netfilter/nf_tables_api.c b/net/netfilter/nf_tables_api.c index 390d4466567f..7f45b291be13 100644 --- a/net/netfilter/nf_tables_api.c +++ b/net/netfilter/nf_tables_api.c @@ -8444,6 +8444,16 @@ static int nf_tables_commit_audit_alloc(struct list_head *adl, return 0; } +static void nf_tables_commit_audit_free(struct list_head *adl) +{ + struct nft_audit_data *adp, *adn; + + list_for_each_entry_safe(adp, adn, adl, list) { + list_del(&adp->list); + kfree(adp); + } +} + static void nf_tables_commit_audit_collect(struct list_head *adl, struct nft_table *table, u32 op) { @@ -8508,6 +8518,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb) ret = nf_tables_commit_audit_alloc(&adl, trans->ctx.table); if (ret) { nf_tables_commit_chain_prepare_cancel(net); + nf_tables_commit_audit_free(&adl); return ret; } if (trans->msg_type == NFT_MSG_NEWRULE || @@ -8517,6 +8528,7 @@ static int nf_tables_commit(struct net *net, struct sk_buff *skb) ret = nf_tables_commit_chain_prepare(net, chain); if (ret < 0) { nf_tables_commit_chain_prepare_cancel(net); + nf_tables_commit_audit_free(&adl); return ret; } } -- 2.25.1