Netdev List
 help / color / mirror / Atom feed
* [PATCH nf] netfilter: ipset: do not destroy shared extensions on hash resize
@ 2026-11-20 11:18 Jun Yang
  0 siblings, 0 replies; only message in thread
From: Jun Yang @ 2026-11-20 11:18 UTC (permalink / raw)
  To: pablo, fw
  Cc: phil, davem, edumazet, kuba, pabeni, horms, kadlec, aconole,
	yifanwucs, manizada, david.lee, netfilter-devel, coreteam, netdev,
	junvyyang, corvus, stable

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

^ permalink raw reply related	[flat|nested] only message in thread

only message in thread, other threads:[~2026-11-20 11:19 UTC | newest]

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-20 11:18 [PATCH nf] netfilter: ipset: do not destroy shared extensions on hash resize Jun Yang

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