The Linux Kernel Mailing List
 help / color / mirror / Atom feed
* Re: [SECURITY] netfilter: ipset: double free in hash resize vs inline gc
       [not found] <3a7a0984.dc22e.1a1f6bb0630.Coremail.18670333770@163.com>
@ 2026-11-20  5:37 ` Willy Tarreau
  0 siblings, 0 replies; only message in thread
From: Willy Tarreau @ 2026-11-20  5:37 UTC (permalink / raw)
  To: n3rd
  Cc: netfilter-devel@vger.kernel.org, linux-kernel@vger.kernel.org,
	Pablo Neira Ayuso, Florian Westphal, security@kernel.org

Hello,

On Fri, Nov 20, 2026 at 10:13:34AM +0800, n3rd wrote:
> 
> 
> Hello,
> 
> 
> I am reporting a security bug in the Linux kernel.
> 
> 
> 1. Affected kernel versions
> Linux v7.3-rc7 (mainline, tested as 7.3.0-rc7) is affected and was reproduced twice on fresh QEMU guests. The same unguarded call site is present unchanged in v7.2.9 (stable) and in the v7.3-rc series; those were checked at source level only and were not re-run dynamically. No bisection was performed, so the affected range is stated from the source of the call site rather than from a tested lower bound.
> 
> 
> 2. Problem description
> ipset hash sets are generated from one template, net/netfilter/ipset/ip_set_hash_gen.h. mtype_gc_do() has two callers. The workqueue caller mtype_gc() takes gc.lock and checks t->resizing before the sweep. The second caller is at the top of mtype_add(), which is the insertion routine shared by the control plane (hash_ip4_uadt) and the packet path (xt_set SET target, then hash_ip4_kadt), and it neither takes gc.lock nor reads t->resizing. On the other side, mtype_resize() copies every element of the old table into a new doubled table while holding only rcu_read_lock_bh(); it never takes t->hregion[r].lock, which is the one lock the inline sweep takes. The copy is a memcpy of the element extension area, and the comment extension in that area is only an 8-byte pointer to a separately allocated struct ip_set_comment_rcu that has no reference count. So when an element X expires while the copy loop is still running, the packet path can sweep X as expired, free X's comment on the old table via ip_set_comment_free() and clear the pointer there, while the new table already holds the same pointer. When the set is destroyed later, the new table walks the same element and frees that comment a second time. Observed on a KASAN kernel: BUG: KASAN: double-free in rcu_free_sheaf, Free of addr ffff8881069f1900 by task ips03v2, and the Allocated-by stack is the kasan kmalloc helper, then ip_set_init_comment, hash_ip4_add, hash_ip4_uadt, call_ad, ip_set_ad, ip_set_uadd, nfnetlink_rcv_msg. The object is in cache kmalloc-32. The same object is also read after it was freed: BUG: KASAN: slab-use-after-free in strlen, Read of size 1, with strlen, ip_set_comment_free, hash_ip4_ext_cleanup, hash_ip4_ahash_destroy, hash_ip4_destroy, ip_set_destroy_set_work. Two fresh runs produced 16 plus 14 and 30 plus 14 of those reports; a control run with the packet path disabled produced zero reports while performing exactly the same 15 resizes.
> 
> 
> 3. Reproducer
> A static x86-64 trigger program speaks the ipset nfnetlink protocol directly. It creates a hash:ip set with the timeout and comment extensions, installs an iptables OUTPUT rule with the SET target and --add-set, sends UDP packets to 127.0.0.1 from a bound source address so that the packet path enters mtype_add on every packet, and arranges the timeouts of 1500 filler elements so that their deadlines fall inside a table resize. The complete reproducer, the guest initramfs scripts and the full console logs are available on request; I did not post them publicly.
> 
> 
> 4. Conditions
> Local attacker with CAP_NET_ADMIN inside a user namespace. No init-namespace capability, no CAP_NET_RAW (the packets are UDP to 127.0.0.1) and no special filesystem are needed. The kernel must have CONFIG_IP_SET, CONFIG_IP_SET_HASH_IP and CONFIG_NETFILTER_XT_SET, and the set must use the timeout and comment extensions. This is a race, so the trigger runs several attempts per boot; on TCG it hit within the first one to three of five attempts. KASAN with kasan_multi_shot is required to see it, because stock KASAN stops reporting after the first finding.
> 
> 
> 5. Suspected location
> net/netfilter/ipset/ip_set_hash_gen.h: the inline mtype_gc_do() call at the top of mtype_add(), versus the guarded call inside mtype_gc(). Also mtype_resize() and the memcpy of the element extension area that carries the comment pointer. net/netfilter/ipset/ip_set_core.c: ip_set_comment_free() and ip_set_init_comment().
> 
> 
> 6. Suggested fix
> Give the inline sweep the same exclusion the workqueue sweep already has: take h->gc.lock and skip the sweep when t->resizing is set. A more complete variant is to take t->hregion[r].lock around the per-region copy in mtype_resize(), so both sweep entry points are serialized with the resize. For context, cffcf57bf03c only fixed the workqueue caller, and f30415929be8 and a442c8a89fb9 fixed other sites.
> 
> 
> 7. Mitigation
> Do not create ipset sets that use the comment extension on affected kernels, or leave CONFIG_NETFILTER_XT_SET off so the packet path cannot enter mtype_add. Restricting unprivileged user namespaces removes the unprivileged reachability but does not fix the bug.
> 
> 
> 8. Disclosure
> Public. This report is being sent to the public netfilter list; no embargo is requested. The reproducer is not posted publicly; it is available on request.
> 
> 
> 9. AI-assistance note
> This report was prepared with an AI coding assistant. Per the kernel security process I treat the issue as public and am not posting the reproducer publicly.

If you used AI assistance, your LLM ought to have told you that our doc
requests that:
  - a tested fix is always provided, and in a format that can be applied
  - the security list isn't CCed when the message is posted to public lists

Can you please propose patches for your different reports ? This is generally
trivial to do for LLMs after they find a bug because they have a complete
view of the problem.

Thanks,
Willy

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

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

Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <3a7a0984.dc22e.1a1f6bb0630.Coremail.18670333770@163.com>
2026-11-20  5:37 ` [SECURITY] netfilter: ipset: double free in hash resize vs inline gc Willy Tarreau

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