Netdev List
 help / color / mirror / Atom feed
From: Jun Yang <juny24602@gmail.com>
To: pablo@netfilter.org, fw@strlen.de
Cc: phil@nwl.cc, davem@davemloft.net, edumazet@kernel.org,
	kuba@kernel.org, pabeni@redhat.com, horms@kernel.org,
	kadlec@netfilter.org, aconole@redhat.com, yifanwucs@gmail.com,
	manizada@pm.me, david.lee@trailofbits.com,
	netfilter-devel@vger.kernel.org, coreteam@netfilter.org,
	netdev@vger.kernel.org, junvyyang@tencent.com,
	corvus@tencent.com, stable@kernel.org
Subject: [PATCH nf] netfilter: ipset: do not destroy shared extensions on hash resize
Date: Fri, 20 Nov 2026 19:18:57 +0800	[thread overview]
Message-ID: <20261120111910.67379-1-juny24602@gmail.com> (raw)

From: Jun Yang <junvyyang@tencent.com>

mtype_resize() copies the elements into the replacement table with a
plain memcpy(), so the comment extension is shared between an element
and its copy until the resize completes.

Commit cffcf57bf03cb ("netfilter: ipset: exclude gc when resize is in
progress") stopped gc from freeing it behind the copy, but the packet
path still does: mtype_add() reusing a timed out element and
mtype_del() destroy the extension on the old table before queueing the
operation for replay.  The replay finds the copy in the replacement
table and frees the same ip_set_comment_rcu again:

  BUG: KASAN: double-free in rcu_free_sheaf+0x1b/0xe0
  Free of addr ffff8881056b9480 by task poc/798
   kfree+0x12f/0x4e0
   hash_ip4_resize+0x1268/0x18e0 [ip_set_hash_ip]
   call_ad+0x367/0x8e0 [ip_set]
  Allocated by task 798:
   ip_set_init_comment+0xbe/0x220 [ip_set]
   hash_ip4_add+0x19c7/0x22f0 [ip_set_hash_ip]

A replayed add can instead leave the copy holding the stale pointer,
because ip_set_init_comment() is skipped for kernel side adds since
commit a442c8a89fb9 ("netfilter: ipset: do not update comments from
kernel-side adds"), and the second free then happens at set destroy.

Destroy the extension on the old table only when the operation is not
queued for replay: the replacement table owns the element from the
replay on, and the old table is freed with ext_destroy == false.  An
element deleted before the copy loop reaches it is not copied, so its
comment is leaked instead of freed twice.

Fixes: f66ee0410b1c ("netfilter: ipset: Fix "INFO: rcu detected stall in hash_xxx" reports")
Cc: stable@kernel.org
Reported-by: TencentOS Corvus AI <corvus@tencent.com>
Signed-off-by: Jun Yang <junvyyang@tencent.com>
---
A KASAN reproducer for this issue is available if requested.

 net/netfilter/ipset/ip_set_hash_gen.h | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)

diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h
index 80ca523f304b..e84341ae9ab6 100644
--- a/net/netfilter/ipset/ip_set_hash_gen.h
+++ b/net/netfilter/ipset/ip_set_hash_gen.h
@@ -1068,7 +1068,11 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
 		data = ahash_data(n, j, set->dsize);
 		if (!deleted) {
 			mtype_del_cidr_all(set, h, data);
-			ip_set_ext_destroy(set, data);
+			/* The resize copy shares this extension:
+			 * the replayed add destroys it.
+			 */
+			if (!(t->resizing && ext && ext->target))
+				ip_set_ext_destroy(set, data);
 			t->hregion[r].elements--;
 		}
 		goto copy_data;
@@ -1223,11 +1227,10 @@ mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext,
 			smp_store_release(&n->pos, --pos);
 		t->hregion[r].elements--;
 		mtype_del_cidr_all(set, h, d);
-		ip_set_ext_destroy(set, data);
-
 		if (t->resizing && ext && ext->target) {
-			/* Resize is in process and kernel side del,
-			 * save values
+			/* Resize is in process and kernel side del, save
+			 * values.  The resize copy shares the extension:
+			 * the replayed del destroys it.
 			 */
 			x = kzalloc_obj(struct mtype_resize_ad, GFP_ATOMIC);
 			if (x) {
@@ -1236,6 +1239,8 @@ mtype_del(struct ip_set *set, void *value, const struct ip_set_ext *ext,
 				       sizeof(struct mtype_elem));
 				x->flags = flags;
 			}
+		} else {
+			ip_set_ext_destroy(set, data);
 		}
 		for (; i < pos; i++) {
 			if (!test_bit(i, n->used))

base-commit: 4d67cf2fd2352f130c42494a933ba9158b925c65
-- 
2.56.1

                 reply	other threads:[~2026-11-20 11:19 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261120111910.67379-1-juny24602@gmail.com \
    --to=juny24602@gmail.com \
    --cc=aconole@redhat.com \
    --cc=coreteam@netfilter.org \
    --cc=corvus@tencent.com \
    --cc=davem@davemloft.net \
    --cc=david.lee@trailofbits.com \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=horms@kernel.org \
    --cc=junvyyang@tencent.com \
    --cc=kadlec@netfilter.org \
    --cc=kuba@kernel.org \
    --cc=manizada@pm.me \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pablo@netfilter.org \
    --cc=phil@nwl.cc \
    --cc=stable@kernel.org \
    --cc=yifanwucs@gmail.com \
    /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