mirror of https://lore.kernel.org/lkml/
 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
  2026-11-20  8:28   ` Jozsef Kadlecsik
  0 siblings, 1 reply; 3+ messages in thread
From: Willy Tarreau @ 2026-11-20  5:37 UTC (permalink / raw)
  To: n3rd
  Cc: netfilter-devel, linux-kernel, Pablo Neira Ayuso,
	Florian Westphal, security

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] 3+ messages in thread

* Re: [SECURITY] netfilter: ipset: double free in hash resize vs inline gc
  2026-11-20  5:37 ` [SECURITY] netfilter: ipset: double free in hash resize vs inline gc Willy Tarreau
@ 2026-11-20  8:28   ` Jozsef Kadlecsik
  2026-11-20  9:59     ` Pablo Neira Ayuso
  0 siblings, 1 reply; 3+ messages in thread
From: Jozsef Kadlecsik @ 2026-11-20  8:28 UTC (permalink / raw)
  To: Willy Tarreau
  Cc: n3rd, netfilter-devel, linux-kernel, Pablo Neira Ayuso,
	Florian Westphal, security

[-- Attachment #1: Type: text/plain, Size: 6271 bytes --]

Hi,

On Fri, 20 Nov 2026, Willy Tarreau wrote:

> On Fri, Nov 20, 2026 at 10:13:34AM +0800, n3rd wrote:
>>
>> 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 tab
 le 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.

The current custom hash implementation backend suffers major internal 
issues which cannot properly be solved without a complete redesign.

Florian worked really hard and rewroted the custom hash implementation 
with an rhashtable based one which fixes the issue in question and other 
issues lurking in the current ipset code as well, without loosing any 
functionality.

It is heading into nf-next and at the end going to be backported to the 
stable branches too. Thus all known/reported ipset related issues will be 
fixed in "one step".

Best regards,
Jozsef

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [SECURITY] netfilter: ipset: double free in hash resize vs inline gc
  2026-11-20  8:28   ` Jozsef Kadlecsik
@ 2026-11-20  9:59     ` Pablo Neira Ayuso
  0 siblings, 0 replies; 3+ messages in thread
From: Pablo Neira Ayuso @ 2026-11-20  9:59 UTC (permalink / raw)
  To: Jozsef Kadlecsik
  Cc: Willy Tarreau, n3rd, netfilter-devel, linux-kernel,
	Florian Westphal, security

Hi,

On Fri, Nov 20, 2026 at 09:28:02AM +0100, Jozsef Kadlecsik wrote:
> Hi,
> 
> On Fri, 20 Nov 2026, Willy Tarreau wrote:
[..]
> > 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.
> 
> The current custom hash implementation backend suffers major internal issues
> which cannot properly be solved without a complete redesign.
> 
> Florian worked really hard and rewroted the custom hash implementation with
> an rhashtable based one which fixes the issue in question and other issues
> lurking in the current ipset code as well, without loosing any
> functionality.
> 
> It is heading into nf-next and at the end going to be backported to the
> stable branches too. Thus all known/reported ipset related issues will be
> fixed in "one step".

This is all coming in the next nf-next.git PR for this week, I'm
working on this.

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2026-11-20 10:00 UTC | newest]

Thread overview: 3+ messages (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
2026-11-20  8:28   ` Jozsef Kadlecsik
2026-11-20  9:59     ` Pablo Neira Ayuso

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

all inboxes | Powered by JetHome®