Skip to contentSkip to conversation

netfilter: nf_tables: unconditionally bump set->nelems before insertion

PATCH In review Steffen Pfendtner 2 participants 6 messages Oct 24 – Oct 25

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

PATCH 1/1 netfilter: nf_tables: unconditionally bump set->nelems before insertion

From: Pablo Neira Ayuso <[email protected]>

[ Upstream commit def602e498a4f951da95c95b1b8ce8ae68aa733a ]

In case that the set is full, a new element gets published then removed without waiting for the RCU grace period, while RCU reader can be walking over it already.

To address this issue, add the element transaction even if set is full, but toggle the set_full flag to report -ENFILE so the abort path safely unwinds the set to its previous state.

As for element updates, decrement set->nelems to restore it.

A simpler fix is to call synchronize_rcu() in the error path. However, with a large batch adding elements to already maxed-out set, this could cause noticeable slowdown of such batches.

Fixes 35d0ac9070ef ("netfilter: nf_tables: fix set->nelems counting with no NLM_F_EXCL")

[ Backport to 5.15.y: 5.15 has no nft_set_maxsize() helper (introduced by
8d738c1869f6 "netfilter: nf_tables: fix set size with rbtree backend", not in 5.15), so the per-set ceiling is inlined using 5.15's own
accounting: max = set->size + set->ndeact, and the element is only counted when set->size is non-zero (unlimited sets are untracked, as before). The matching atomic_dec() on the error path is guarded the same way. The update-path hunk from the upstream commit does not apply:
5.15 has no in-place element-update path here. ]

Assisted-by Claude:claude-opus-4-8

---
Not a clean cherry-pick, please see the bracketed backport note: 5.15 lacks nft_set_maxsize(), so the ceiling is inlined using 5.15's existing accounting. A review of the adaptation from the netfilter side would be appreciated.

Build-tested on arm64, and boot-tested under QEMU with KASAN: the set size limit is still enforced (ENFILE), element counts stay correct across delete/re-add and flush, unlimited sets are unaffected, no KASAN reports.

 net/netfilter/nf_tables_api.c | 25 +++++++++++++++----------
 1 file changed, 15 insertions(+), 10 deletions(-)
net/netfilter/nf_tables_api.c +15 -10
index b5273255b738..70f28038d1ad 100644
@@ -6255,6 +6255,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
 	struct nft_data_desc desc;
 	enum nft_registers dreg;
 	struct nft_trans *trans;
+	bool set_full = false;
 	u64 timeout;
 	u64 expiration;
 	int err, i;
@@ -6541,10 +6542,18 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
 	if (err < 0)
 		goto err_elem_expr;
 
+	if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size) {
+		unsigned int max = set->size + set->ndeact, nelems;
+
+		nelems = atomic_inc_return(&set->nelems);
+		if (nelems > max)
+			set_full = true;
+	}
+
 	trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
 	if (trans == NULL) {
 		err = -ENOMEM;
-		goto err_elem_expr;
+		goto err_set_size;
 	}
 
 	ext->genmask = nft_genmask_cur(ctx->net);
@@ -6576,20 +6585,16 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
 		goto err_element_clash;
 	}
 
-	if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size &&
-	    !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) {
-		err = -ENFILE;
-		goto err_set_full;
-	}
-
 	nft_trans_elem(trans) = elem;
 	nft_trans_commit_list_add_tail(ctx->net, trans);
-	return 0;
 
-err_set_full:
-	nft_setelem_remove(ctx->net, set, &elem);
+	return set_full ? -ENFILE : 0;
+
 err_element_clash:
 	kfree(trans);
+err_set_size:
+	if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size)
+		atomic_dec(&set->nelems);
 err_elem_expr:
 	nf_tables_set_elem_destroy(ctx, set, elem.priv);
 err_parse_data:
Signature · 1 line
2.50.1 (Apple Git-155)
maintainer
#1 lore raw
Quoted 6 lines

[ Backport to 5.15.y: 5.15 has no nft_set_maxsize() helper (introduced by 8d738c1869f6 "netfilter: nf_tables: fix set size with rbtree backend", not in 5.15), so the per-set ceiling is inlined using 5.15's own
accounting: max = set->size + set->ndeact, and the element is only counted when set->size is non-zero (unlimited sets are untracked, as before).

This needs the prerequisite d4b7f29eb85c ("netfilter: nf_tables: always increment set element count") first, as the 6.1 backport has it. Without it set->nelems can go negative, and the new unsigned nelems > max comparison can then return a spurious -ENFILE on dynset-bound sets.

It also needs a rebase on the current 5.15 queue: there is a one line conflict with 66bda7ca3059 ("netfilter: nf_tables: set->ops->insert returns opaque set element in case of EEXIST").

Could you send a v2 with the prerequisite included?

Signature · 2 lines
Thanks,
Sasha
#2 lore raw

v2 cover netfilter: nf_tables: unconditionally bump set->nelems before insertion

This is the 5.15.y backport of def602e498a4 ("netfilter: nf_tables:
unconditionally bump set->nelems before insertion"), now with its prerequisite d4b7f29eb85c ("netfilter: nf_tables: always increment set element count") as patch 1, as in 6.1.y.

Changes in v2:
- Add d4b7f29eb85c as patch 1/2 (Sasha). Without it, set->nelems is only incremented for sets with a size but always decremented, so it can go negative, and the new unsigned nelems > max check can then return a spurious -ENFILE on dynset-bound sets.
- 2/2: with the counter now always incremented, drop v1's set->size guards on the increment and on the error-path atomic_dec(); the ceiling is set->size ? set->size + set->ndeact : UINT_MAX, the same expression 1/2 inlines (5.15 has no nft_set_maxsize()).
- Rebased onto the current queue-5.15, including the one-line conflict with "netfilter: nf_tables: set->ops->insert returns opaque set element in case of EEXIST".

Patch 1/2 is a clean cherry-pick. Build-tested (arm64, W=1) and boot-tested under QEMU with KASAN on top of queue-5.15: the set size limit is still enforced (-ENFILE), counts stay correct across delete/re-add and flush, unlimited sets are unaffected, and mixed dynset (packet path) plus control-plane adds/deletes on sized and unsized dynamic sets give no spurious -ENFILE. No KASAN reports.

Florian Westphal (1):
netfilter: nf_tables: always increment set element count

Pablo Neira Ayuso (1):
netfilter: nf_tables: unconditionally bump set->nelems before
insertion

 net/netfilter/nf_tables_api.c | 26 ++++++++++++++++----------
 1 file changed, 16 insertions(+), 10 deletions(-)
Signature · 1 line
2.39.5
#3 lore raw

PATCH v2 1/2 netfilter: nf_tables: always increment set element count

From: Florian Westphal <[email protected]>

[ Upstream commit d4b7f29eb85c93893bc27388b37709efbc3c9a0e ]

At this time, set->nelems counter only increments when the set has a maximum size.

All set elements decrement the counter unconditionally, this is confusing.

Increment the counter unconditionally to make this symmetrical. This would also allow changing the set maximum size after set creation in a later patch.

Assisted-by Claude:claude-opus-5-5
---
 net/netfilter/nf_tables_api.c | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)
net/netfilter/nf_tables_api.c +7 -4
index b4c3615a2bf6..7a6bc137cdda 100644
@@ -6585,10 +6585,13 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
 		goto err_element_clash;
 	}
 
-	if (!(flags & NFT_SET_ELEM_CATCHALL) && set->size &&
-	    !atomic_add_unless(&set->nelems, 1, set->size + set->ndeact)) {
-		err = -ENFILE;
-		goto err_set_full;
+	if (!(flags & NFT_SET_ELEM_CATCHALL)) {
+		unsigned int max = set->size ? set->size + set->ndeact : UINT_MAX;
+
+		if (!atomic_add_unless(&set->nelems, 1, max)) {
+			err = -ENFILE;
+			goto err_set_full;
+		}
 	}
 
 	nft_trans_elem(trans) = elem;
Signature · 1 line
2.39.5
#4 lore raw

PATCH v2 2/2 netfilter: nf_tables: unconditionally bump set->nelems before insertion

In reply to Steffen Pfendtner

From: Pablo Neira Ayuso <[email protected]>

[ Upstream commit def602e498a4f951da95c95b1b8ce8ae68aa733a ]

In case that the set is full, a new element gets published then removed without waiting for the RCU grace period, while RCU reader can be walking over it already.

To address this issue, add the element transaction even if set is full, but toggle the set_full flag to report -ENFILE so the abort path safely unwinds the set to its previous state.

As for element updates, decrement set->nelems to restore it.

A simpler fix is to call synchronize_rcu() in the error path. However, with a large batch adding elements to already maxed-out set, this could cause noticeable slowdown of such batches.

Fixes 35d0ac9070ef ("netfilter: nf_tables: fix set->nelems counting with no NLM_F_EXCL")

[ Backport to 5.15.y: applies on top of d4b7f29eb85c ("netfilter:
nf_tables: always increment set element count"), as in 6.1.y. 5.15 has no nft_set_maxsize() helper (introduced by 8d738c1869f6 "netfilter: nf_tables: fix set size with rbtree backend", not in 5.15), so the ceiling is the same expression the prerequisite inlines:
set->size ? set->size + set->ndeact : UINT_MAX. The update-path hunk from the upstream commit does not apply: 5.15 has no in-place element-update path here. ]

Assisted-by Claude:claude-opus-5-5
---
 net/netfilter/nf_tables_api.c | 29 ++++++++++++++++-------------
 1 file changed, 16 insertions(+), 13 deletions(-)
net/netfilter/nf_tables_api.c +16 -13
index 7a6bc137cdda..9a71c19ce3a3 100644
@@ -6263,6 +6263,7 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
 	enum nft_registers dreg;
 	struct nft_trans *trans;
 	void *elem_priv;
+	bool set_full = false;
 	u64 timeout;
 	u64 expiration;
 	int err, i;
@@ -6549,10 +6550,19 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
 	if (err < 0)
 		goto err_elem_expr;
 
+	if (!(flags & NFT_SET_ELEM_CATCHALL)) {
+		unsigned int max = set->size ? set->size + set->ndeact : UINT_MAX;
+		unsigned int nelems;
+
+		nelems = atomic_inc_return(&set->nelems);
+		if (nelems > max)
+			set_full = true;
+	}
+
 	trans = nft_trans_elem_alloc(ctx, NFT_MSG_NEWSETELEM, set);
 	if (trans == NULL) {
 		err = -ENOMEM;
-		goto err_elem_expr;
+		goto err_set_size;
 	}
 
 	ext->genmask = nft_genmask_cur(ctx->net);
@@ -6585,23 +6595,16 @@ static int nft_add_set_elem(struct nft_ctx *ctx, struct nft_set *set,
 		goto err_element_clash;
 	}
 
-	if (!(flags & NFT_SET_ELEM_CATCHALL)) {
-		unsigned int max = set->size ? set->size + set->ndeact : UINT_MAX;
-
-		if (!atomic_add_unless(&set->nelems, 1, max)) {
-			err = -ENFILE;
-			goto err_set_full;
-		}
-	}
-
 	nft_trans_elem(trans) = elem;
 	nft_trans_commit_list_add_tail(ctx->net, trans);
-	return 0;
 
-err_set_full:
-	nft_setelem_remove(ctx->net, set, &elem);
+	return set_full ? -ENFILE : 0;
+
 err_element_clash:
 	kfree(trans);
+err_set_size:
+	if (!(flags & NFT_SET_ELEM_CATCHALL))
+		atomic_dec(&set->nelems);
 err_elem_expr:
 	nf_tables_set_elem_destroy(ctx, set, elem.priv);
 err_parse_data:
Signature · 1 line
2.39.5
maintainer Applied
#5 lore raw
In reply to Steffen Pfendtner
Quoted 4 lines

This is the 5.15.y backport of def602e498a4 ("netfilter: nf_tables:
unconditionally bump set->nelems before insertion"), now with its prerequisite d4b7f29eb85c ("netfilter: nf_tables: always increment set element count") as patch 1, as in 6.1.y.

Queued the series for 5.15, thanks.

Signature · 2 lines
Thanks,
Sasha