Netdev List
 help / color / mirror / Atom feed
From: Qing Lee <l.qing95@gmail.com>
To: Pablo Neira Ayuso <pablo@netfilter.org>,
	Florian Westphal <fw@strlen.de>, Phil Sutter <phil@nwl.cc>
Cc: netfilter-devel@vger.kernel.org, coreteam@netfilter.org,
	netdev@vger.kernel.org, linux-kernel@vger.kernel.org,
	Qing Lee <l.qing95@gmail.com>
Subject: [PATCH nf] netfilter: ipset: do not exceed maxelem when reusing a deleted slot
Date: Fri,  4 Dec 2026 16:43:07 +0100	[thread overview]
Message-ID: <20261204154307.178115-1-l.qing95@gmail.com> (raw)

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


             reply	other threads:[~2026-12-04 15:43 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-04 15:43 Qing Lee [this message]
2026-12-04 15:47 ` [PATCH nf] netfilter: ipset: do not exceed maxelem when reusing a deleted slot netdev-bot+sinfo
2026-12-04 16:04   ` Qing Lee

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261204154307.178115-1-l.qing95@gmail.com \
    --to=l.qing95@gmail.com \
    --cc=coreteam@netfilter.org \
    --cc=fw@strlen.de \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pablo@netfilter.org \
    --cc=phil@nwl.cc \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox