Skip to contentSkip to conversation

netfilter: nft_set_rbtree: fix use count leak on transaction abort

PATCH In review Marko Jevtic 3 participants 3 messages Apr 12 – Apr 14

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH v3 1/1 netfilter: nft_set_rbtree: fix use count leak on transaction abort

nft_rbtree_abort() does not handle elements moved to the expired list by inline GC during __nft_rbtree_insert(). When inline GC encounters expired elements during overlap detection, it calls nft_rbtree_gc_elem_move() which deactivates element data (decrementing chain/object use counts), removes the element from the rbtree, and queues it for deferred freeing. On commit, these elements are freed via nft_rbtree_gc_queue(). On abort, however, the expired list is ignored entirely.

This leaves use counts permanently decremented after abort.

This restores transactional semantics by ensuring that inline GC side effects are fully rolled back on abort:

- Introduce a separate tx_gc list for elements collected during insert (transaction-scoped), distinct from the existing expired list used by commit-time gc_scan (commit-scoped). This prevents abort from touching committed expired elements left over from a prior gc_queue OOM.

- On commit: splice tx_gc into expired after publishing the new binary search blob, then drain via gc_queue as before.

- On abort: iterate tx_gc, re-activate element data (restoring use counts), and re-insert into the rbtree. Elements remain expired and will be properly collected on the next successful commit.

- Extract nft_rbtree_node_insert() helper from __nft_rbtree_insert() to share the tree insertion logic with the abort restore path.

- Add WARN_ON_ONCE in commit early-return path to catch any violation of the invariant that tx_gc is empty when no tree changes occurred.

- Reset start_rbe_cookie on abort so insertion state from a failed transaction does not persist.

Fixes f6c383b8c31a ("netfilter: nf_tables: adapt set backend to use GC transaction API")

---
v3:
- add Fixes tag
- narrow the changelog to the abort-side use-count accounting bug

v2:
- introduce a transaction-scoped tx_gc list for insert-time GC
- restore tx_gc entries on abort and splice them to expired on commit
- export nft_setelem_data_activate() and factor out nft_rbtree_node_insert()

 include/net/netfilter/nf_tables.h |  3 +
 net/netfilter/nf_tables_api.c     |  4 +-
 net/netfilter/nft_set_rbtree.c    | 96 ++++++++++++++++++++++---------
 3 files changed, 74 insertions(+), 29 deletions(-)
include/net/netfilter/nf_tables.h +3 -0
index ec8a8ec9c..f8c912332 100644
@@ -1910,6 +1910,9 @@ struct nft_trans_gc *nft_trans_gc_catchall_async(struct nft_trans_gc *gc,
 						 unsigned int gc_seq);
 struct nft_trans_gc *nft_trans_gc_catchall_sync(struct nft_trans_gc *gc);
 
+void nft_setelem_data_activate(const struct net *net,
+				 const struct nft_set *set,
+				 struct nft_elem_priv *elem_priv);
 void nft_setelem_data_deactivate(const struct net *net,
 				 const struct nft_set *set,
 				 struct nft_elem_priv *elem_priv);
net/netfilter/nf_tables_api.c +6 -6
index 8c42247a1..8e783db3f 100644
@@ -5837,7 +5837,7 @@ static void nf_tables_unbind_set(const struct nft_ctx *ctx, struct nft_set *set,
 	}
 }
 
-static void nft_setelem_data_activate(const struct net *net,
-				      const struct nft_set *set,
-				      struct nft_elem_priv *elem_priv);
+void nft_setelem_data_activate(const struct net *net,
+			       const struct nft_set *set,
+			       struct nft_elem_priv *elem_priv);
 
@@ -7656,7 +7656,7 @@ static int nft_setelem_active_next(const struct net *net,
 	return nft_set_elem_active(ext, genmask);
 }
 
-static void nft_setelem_data_activate(const struct net *net,
-				      const struct nft_set *set,
-				      struct nft_elem_priv *elem_priv)
+void nft_setelem_data_activate(const struct net *net,
+			       const struct nft_set *set,
+			       struct nft_elem_priv *elem_priv)
 {
net/netfilter/nft_set_rbtree.c +69 -27

174 lines not on this page · raw message

Signature · 1 line
2.43.0
maintainer
#1 lore raw
Quoted 10 lines · Marko Jevtic

nft_rbtree_abort() does not handle elements moved to the expired list by inline GC during __nft_rbtree_insert(). When inline GC encounters expired elements during overlap detection, it calls nft_rbtree_gc_elem_move() which deactivates element data (decrementing chain/object use counts), removes the element from the rbtree, and queues it for deferred freeing. On commit, these elements are freed via nft_rbtree_gc_queue(). On abort, however, the expired list is ignored entirely.

This leaves use counts permanently decremented after abort.

I have not seen a reason/answer why this needs to be rolled back. GC is an implementation detail, its not part of the transaction.

It could also be done from work queue, for example, not just from insert or commit.

I see no reason to change the existing approach.

#2 lore raw
In reply to Marko Jevtic

Hi,

Quoted 10 lines · Marko Jevtic

nft_rbtree_abort() does not handle elements moved to the expired list by inline GC during __nft_rbtree_insert(). When inline GC encounters expired elements during overlap detection, it calls nft_rbtree_gc_elem_move() which deactivates element data (decrementing chain/object use counts), removes the element from the rbtree, and queues it for deferred freeing. On commit, these elements are freed via nft_rbtree_gc_queue(). On abort, however, the expired list is ignored entirely.

This leaves use counts permanently decremented after abort.

Yes, but that is expected?

Expired elements reside in priv->expired, these elements are already deactivated, ie. removed from the rbtree and chain reference is decremented.

From abort path, the deactivated element simply remains there until there is a commit run that gets rid of it.

I can't make any sense of this bug report so far.

Why do you think there is a need to restore an expired element?