diff mbox series

[v2] audit: fix memory leak in nf_tables_commit

Message ID 20210713130344.473646-1-mudongliangabcd@gmail.com (mailing list archive)
State Superseded
Delegated to: Netdev Maintainers
Headers show
Series [v2] audit: fix memory leak in nf_tables_commit | expand

Checks

Context Check Description
netdev/cover_letter success Link
netdev/fixes_present success Link
netdev/patch_count success Link
netdev/tree_selection success Guessed tree name to be net-next
netdev/subject_prefix warning Target tree name not specified in the subject
netdev/cc_maintainers success CCed 10 of 10 maintainers
netdev/source_inline success Was 0 now: 0
netdev/verify_signedoff success Link
netdev/module_param success Was 0 now: 0
netdev/build_32bit success Errors and warnings before: 16 this patch: 16
netdev/kdoc success Errors and warnings before: 0 this patch: 0
netdev/verify_fixes success Link
netdev/checkpatch success total: 0 errors, 0 warnings, 0 checks, 30 lines checked
netdev/build_allmodconfig_warn success Errors and warnings before: 16 this patch: 16
netdev/header_inline success Link

Commit Message

Dongliang Mu July 13, 2021, 1:03 p.m. UTC
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 <syzkaller@googlegroups.com>
Reported-by: kernel test robot <lkp@intel.com>
Fixes: c520292f29b8 ("audit: log nftables configuration change events once per table")
Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
---
v1->v2: fix the compile issue
 net/netfilter/nf_tables_api.c | 12 ++++++++++++
 1 file changed, 12 insertions(+)

Comments

Florian Westphal July 13, 2021, 1:20 p.m. UTC | #1
Dongliang Mu <mudongliangabcd@gmail.com> wrote:
> 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 <syzkaller@googlegroups.com>
> Reported-by: kernel test robot <lkp@intel.com>
> Fixes: c520292f29b8 ("audit: log nftables configuration change events once per table")
> Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
> ---
> 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)

nf_tables_commit_audit_free?

Aside from that, there should be a followup patch (for nf-next),
adding empty inline functions in case of CONFIG_AUDITSYSCALL=n.

Right now it does pointless aggregation for the AUDIT=n case.
Dongliang Mu July 13, 2021, 2:40 p.m. UTC | #2
On Tue, Jul 13, 2021 at 9:21 PM Florian Westphal <fw@strlen.de> wrote:
>
> Dongliang Mu <mudongliangabcd@gmail.com> wrote:
> > 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 <syzkaller@googlegroups.com>
> > Reported-by: kernel test robot <lkp@intel.com>
> > Fixes: c520292f29b8 ("audit: log nftables configuration change events once per table")
> > Signed-off-by: Dongliang Mu <mudongliangabcd@gmail.com>
> > ---
> > 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)
>
> nf_tables_commit_audit_free?

What do you mean? Modify the name of newly added function to
nf_tables_commit_audit_free?

>
> Aside from that, there should be a followup patch (for nf-next),
> adding empty inline functions in case of CONFIG_AUDITSYSCALL=n.

I see. I prefer to send them (two implementations of the newly added
function) in version v2.

>
> Right now it does pointless aggregation for the AUDIT=n case.
Florian Westphal July 13, 2021, 3:31 p.m. UTC | #3
Dongliang Mu <mudongliangabcd@gmail.com> wrote:
> > > +static void nf_tables_commit_free(struct list_head *adl)
> >
> > nf_tables_commit_audit_free?
> 
> What do you mean? Modify the name of newly added function to
> nf_tables_commit_audit_free?

Yes, this function is audit related, and it does the inverse
of existing '...audit_alloc'.
Dongliang Mu July 14, 2021, 2:18 a.m. UTC | #4
On Tue, Jul 13, 2021 at 11:31 PM Florian Westphal <fw@strlen.de> wrote:
>
> Dongliang Mu <mudongliangabcd@gmail.com> wrote:
> > > > +static void nf_tables_commit_free(struct list_head *adl)
> > >
> > > nf_tables_commit_audit_free?
> >
> > What do you mean? Modify the name of newly added function to
> > nf_tables_commit_audit_free?
>
> Yes, this function is audit related, and it does the inverse
> of existing '...audit_alloc'.

Hi Florian,

I double-check the patch, and it seems there is no need to send a
followup patch (nf-next).

I will send a v3 patch with the new function name.

Best regards
Dongliang Mu
diff mbox series

Patch

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;
 			}
 		}