Netdev List
 help / color / mirror / Atom feed
* [PATCH nf] netfilter: ipset: do not exceed maxelem when reusing a deleted slot
@ 2026-12-04 15:43 Qing Lee
  2026-12-04 15:47 ` netdev-bot+sinfo
  0 siblings, 1 reply; 3+ messages in thread
From: Qing Lee @ 2026-12-04 15:43 UTC (permalink / raw)
  To: Pablo Neira Ayuso, Florian Westphal, Phil Sutter
  Cc: netfilter-devel, coreteam, netdev, linux-kernel, Qing Lee

In mtype_add(), reusing a previously deleted slot (!test_bit(i, n->used))
sets deleted = reuse = true and jumps directly to copy_data, incrementing
elements without checking elements >= maxelem. Because a deleted slot was
already decremented from elements when removed, reusing it when the set is
full allows the number of elements to exceed maxelem.

Only select a deleted slot for reuse when elements < maxelem. This also
allows the loop to keep scanning for a timed-out entry in the same bucket
when the set is full. For forceadd when no timed-out entry was found, pick
the first in-use slot with find_first_bit() instead of hardcoding slot 0
(which may be deleted), and fall back to set_full if the bucket has no
in-use entries.

Fixes: 18f84d41d34f ("netfilter: ipset: Introduce RCU locking in hash:* types")
Signed-off-by: Qing Lee <l.qing95@gmail.com>
---
 net/netfilter/ipset/ip_set_hash_gen.h | 11 +++++++----
 1 file changed, 7 insertions(+), 4 deletions(-)

diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h
index 80ca523f3..fe56bf857 100644
--- a/net/netfilter/ipset/ip_set_hash_gen.h
+++ b/net/netfilter/ipset/ip_set_hash_gen.h
@@ -1039,8 +1039,8 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
 	npos = smp_load_acquire(&n->pos);
 	for (i = 0; i < npos; i++) {
 		if (!test_bit(i, n->used)) {
-			/* Reuse first deleted entry */
-			if (j == -1) {
+			/* Reuse first deleted entry only if set is not full */
+			if (j == -1 && elements < maxelem) {
 				deleted = reuse = true;
 				j = i;
 			}
@@ -1063,8 +1063,11 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
 		}
 	}
 	if (reuse || forceadd) {
-		if (j == -1)
-			j = 0;
+		if (j == -1) {
+			j = find_first_bit(n->used, npos);
+			if (j >= npos)
+				goto set_full;
+		}
 		data = ahash_data(n, j, set->dsize);
 		if (!deleted) {
 			mtype_del_cidr_all(set, h, data);
-- 
2.47.3


^ permalink raw reply related	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-12-04 16:04 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-12-04 15:43 [PATCH nf] netfilter: ipset: do not exceed maxelem when reusing a deleted slot Qing Lee
2026-12-04 15:47 ` netdev-bot+sinfo
2026-12-04 16:04   ` Qing Lee

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox