Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp3880330pxv; Tue, 13 Jul 2021 06:05:22 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzsHSCxUzQEZQzaeqb5MOHL1eQGYvYsPP8zBL+1/LLpfqXVvMeY00eB5mVzoWoLH+ST9RCH X-Received: by 2002:a02:3781:: with SMTP id r123mr3969931jar.26.1626181522237; Tue, 13 Jul 2021 06:05:22 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626181522; cv=none; d=google.com; s=arc-20160816; b=S3/J3xnUb6newhF4TiKanlTudby1gzMHF0S/XfyDFEiqcVguqgOYPurSx2ZgyhNS+U U0hJgAkVVrMOQ8GUlMflAeDA9RlXAF6a7ks3S1y6t6JenrhiGimRgQUnwVgv3Zn/oWVI nAcV0B3+Se+/ze0+9SEaRkqwoVkcspWNJWRw20gAyEJQHwZZmKnElqMBenXP70a3L7+e PaCKKx703TJmw8AxUCjSjZWXIQmN9WKGITfMnK2266Ne9eKaMGpEw4d0rsltvOr0cnIj 0CKH6zycxgdZASxdBo9goQaVBNre/9Mf81MxmQ69d3J5dL5BPtGoJFvYhj5bR5mrCNTU 3CIQ== 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=wICO7mAwhqJD1edb3nvOclBABQKgJ4d++G/031aYfk4=; b=wkaEWLdsp4E87LcrSlEnw6n33WPhRRvICwoKgwnS/MNkVVRJv9qPK+V84Book+zydc fR/BpKqVRZmd3KY5FxT2MQGMQ0I1Kksx1pF7+0bIqQg0HHHqeb/m1fN7nYOdJ8ZDENyZ ROXn00utJHyQaBWbDmyqtjlvdh+BDZZ5/nHjHMyZhT01etAA+raAw38K1H7z4Mp4xkmj wZHpbjvhQb53pM4+nzYBE2Y1faB0x3FXE5b12MKfwqoBpa0F0vUP6cFVTabY2C/C45Vl 87d2VopIDLjLcQfXdxyIoQw1WoI9ixBBdavQaJCYQZUjKEI98rVKO/zJc+7xdxs8jbii s/LA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gmail.com header.s=20161025 header.b=NeSEYEwp; 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 b8si1760547jav.94.2021.07.13.06.05.08; Tue, 13 Jul 2021 06:05:22 -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=NeSEYEwp; 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 S236276AbhGMNGz (ORCPT + 99 others); Tue, 13 Jul 2021 09:06:55 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53068 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S236249AbhGMNGx (ORCPT ); Tue, 13 Jul 2021 09:06:53 -0400 Received: from mail-pg1-x52c.google.com (mail-pg1-x52c.google.com [IPv6:2607:f8b0:4864:20::52c]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 902F3C0613DD; Tue, 13 Jul 2021 06:04:02 -0700 (PDT) Received: by mail-pg1-x52c.google.com with SMTP id k20so14581458pgg.7; Tue, 13 Jul 2021 06:04:02 -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=wICO7mAwhqJD1edb3nvOclBABQKgJ4d++G/031aYfk4=; b=NeSEYEwpwaMAY/u6wfT8YqxYJ29jDeD4nKlvqwUM2Q2EhDJhGPqnAJL7BXdPCC6YdD XkDaCKAWWILQJkAQG32yIaGA6opOub7y7pgqeaQOxxBgJlqElvz1PAMQINvdgzlgu5mi EcRgCskZ56y6pKWRQRoQ68lK0iBp5vdb9jVecO/RlowkRnxISCaz1EnLSL5lnTkoMkoK hhq4+N2HO2HyudtZyuEoR92SLtA7bbU/sdKgr7M/xYQ/Q4X4/KmUhoPCP2lbZhhTRZTA MJk4spuP6i+GyS2+YhdYkCOqPu3XcAsXrOQieeByWWQ7+8WG5J6mfkM/kODxrpClgYbD 9bJw== 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=wICO7mAwhqJD1edb3nvOclBABQKgJ4d++G/031aYfk4=; b=QenehPU+/eEtkT12s0KmDsdJnrtuuyCGHBs5NvQ1kv7DQOirkGzSLD8hM1ddj2T586 SztqzrhLm8jcciXetuiu+N+HSS3VNt+jpPREhXiZe76ZmTEN6mgEp0Us4T2EEimo5b3M lxnnMQPTGmW0JuLm85xvIqK3reGeQ3yG6GQIPKThYOdG5ZyBMKt3/fJ6+qJfrbDru5P2 EChGb/WSFzLxMB5XRRav1hqKt/J3M4oi7RtiTD+WJXwVzpTlZOPN/eWuFjarC/Wff3at SGE2+EICJrHetMH4Mif6F0Dkcr/cBVjlakgJGXorX0LjZyNe4IJIc2KLRinybaZnthl/ AhGg== X-Gm-Message-State: AOAM532HpTmuQy2Bih1tFb3cSry77RNKf2PUSTevHwwIFS2ZLM6733YL ZG+MWU/okno9xE39zfmItPg= X-Received: by 2002:a63:f64d:: with SMTP id u13mr4256994pgj.156.1626181441973; Tue, 13 Jul 2021 06:04:01 -0700 (PDT) Received: from localhost.localdomain ([154.16.166.218]) by smtp.gmail.com with ESMTPSA id w2sm3184238pjf.2.2021.07.13.06.03.57 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Jul 2021 06:04:01 -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 v2] audit: fix memory leak in nf_tables_commit Date: Tue, 13 Jul 2021 21:03:44 +0800 Message-Id: <20210713130344.473646-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 freeing the linked list with head 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 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_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_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_free(&adl); return ret; } } -- 2.25.1