Skip to contentSkip to conversation

netfilter: nf_tables: fix memory leak when more than 255 elements expired

PATCH In review Florian Westphal 2 participants 4 messages Sep 19, 2023

Subsystems (2)
lore
Share & tools
lore

Florian Westphal

author maintainer
#0 lore raw

PATCH 1/1 netfilter: nf_tables: fix memory leak when more than 255 elements expired

When more than 255 elements expire we're supposed to switch to a new gc container structure, but nft_trans_gc_space() always returns false in this case because u8 type is too narrow for the boundary.

While at it, don't deref 'gc' after we've passed it to call_rcu.

Fixes 5f68718b34a5 ("netfilter: nf_tables: GC transaction API to avoid race with control plane")
---
 include/net/netfilter/nf_tables.h |  2 +-
 net/netfilter/nf_tables_api.c     | 10 ++++++++--
 2 files changed, 9 insertions(+), 3 deletions(-)
include/net/netfilter/nf_tables.h +1 -1
index dd40c75011d2..a338d7d4f69b 100644
@@ -1682,7 +1682,7 @@ struct nft_trans_gc {
 	struct net		*net;
 	struct nft_set		*set;
 	u32			seq;
-	u8			count;
+	u16			count;
 	void			*priv[NFT_TRANS_GC_BATCHCOUNT];
 	struct rcu_head		rcu;
 };
net/netfilter/nf_tables_api.c +8 -2
index e429ebba74b3..96d40d62dd5a 100644
@@ -9562,12 +9562,15 @@ static int nft_trans_gc_space(struct nft_trans_gc *trans)
 struct nft_trans_gc *nft_trans_gc_queue_async(struct nft_trans_gc *gc,
 					      unsigned int gc_seq, gfp_t gfp)
 {
+	struct nft_set *set;
+
 	if (nft_trans_gc_space(gc))
 		return gc;
 
+	set = gc->set;
 	nft_trans_gc_queue_work(gc);
 
-	return nft_trans_gc_alloc(gc->set, gc_seq, gfp);
+	return nft_trans_gc_alloc(set, gc_seq, gfp);
 }
 
 void nft_trans_gc_queue_async_done(struct nft_trans_gc *trans)
@@ -9582,15 +9585,18 @@ void nft_trans_gc_queue_async_done(struct nft_trans_gc *trans)
 
 struct nft_trans_gc *nft_trans_gc_queue_sync(struct nft_trans_gc *gc, gfp_t gfp)
 {
+	struct nft_set *set;
+
 	if (WARN_ON_ONCE(!lockdep_commit_lock_is_held(gc->net)))
 		return NULL;
 
 	if (nft_trans_gc_space(gc))
 		return gc;
 
+	set = gc->set;
 	call_rcu(&gc->rcu, nft_trans_gc_trans_free);
 
-	return nft_trans_gc_alloc(gc->set, 0, gfp);
+	return nft_trans_gc_alloc(set, 0, gfp);
 }
 
 void nft_trans_gc_queue_sync_done(struct nft_trans_gc *trans)
Signature · 1 line
2.41.0
author maintainer
#1 lore raw
Quoted 2 lines · Florian Westphal

When more than 255 elements expire we're supposed to switch to a new gc container structure, but nft_trans_gc_space() always returns false in this

Grrr. This should read 'always returns true' or 'never returns false', but not *THIS*. I'll fix this up when applying this, probably tomorrow morning.

#2 lore raw
Quoted 7 lines · Florian Westphal
Quoted 2 lines · Florian Westphal

When more than 255 elements expire we're supposed to switch to a new gc container structure, but nft_trans_gc_space() always returns false in this

Grrr. This should read 'always returns true' or 'never returns false', but not *THIS*. I'll fix this up when applying this, probably tomorrow morning.

You could amend the fix when applying this.

#3 lore raw
Quoted 10 lines · Pablo Neira Ayuso
Quoted 7 lines · Florian Westphal
Quoted 2 lines · Florian Westphal

When more than 255 elements expire we're supposed to switch to a new gc container structure, but nft_trans_gc_space() always returns false in this

Grrr. This should read 'always returns true' or 'never returns false', but not *THIS*. I'll fix this up when applying this, probably tomorrow morning.

You could amend the fix when applying this.

I mean, this patch description, as you prefer.