[PATCH nf] netfilter: ipset: do not exceed maxelem when reusing a deleted slot
From: Qing Lee
Date: Fri Dec 04 2026 - 10:44:05 EST
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@xxxxxxxxx>
---
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