netfilter: ipset: double free in hash resize vs inline gc
A message that is not in the archive (sent off-list or not yet received): [email protected]
Hello,
Quoted 36 lines · 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 callermtype_gc()takes gc.lock and checkst->resizingbefore the sweep. The second caller is at the top ofmtype_add(), which is the insertion routine shared by the control plane (hash_ip4_uadt) and the packet path (xt_setSET target, thenhash_ip4_kadt), and it neither takes gc.lock nor readst->resizing. On the other side,mtype_resize()copies every element of the old table into a new doubled table while holding onlyrcu_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 structip_set_comment_rcuthat 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 viaip_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 inrcu_free_sheaf, Free of addrffff8881069f1900by task ips03v2, and the Allocated-by stack is the kasan kmalloc helper, thenip_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 entersmtype_addon 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 withCAP_NET_ADMINinside a user namespace. No init-namespace capability, noCAP_NET_RAW(the packets are UDP to 127.0.0.1) and no special filesystem are needed. The kernel must haveCONFIG_IP_SET,CONFIG_IP_SET_HASH_IPandCONFIG_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 withkasan_multi_shotis 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 inlinemtype_gc_do()call at the top ofmtype_add(), versus the guarded call insidemtype_gc(). Alsomtype_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()andip_set_init_comment().6. Suggested fix
Give the inline sweep the same exclusion the workqueue sweep already has: takeh->gc.lockand skip the sweep whent->resizingis set. A more complete variant is to take t->hregion[r].lock around the per-region copy inmtype_resize(), so both sweep entry points are serialized with the resize. For context,cffcf57bf03conly fixed the workqueue caller, andf30415929be8anda442c8a89fb9fixed other sites.7. Mitigation
Do not create ipset sets that use the comment extension on affected kernels, or leaveCONFIG_NETFILTER_XT_SEToff so the packet path cannot entermtype_add. Restricting unprivileged user namespaces removes the unprivileged reachability but does not fix the bug.
Quoted 6 lines
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
Hi,
Quoted 11 lines · Willy Tarreau
Quoted 10 lines · 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 callermtype_gc()takes gc.lock and checkst->resizingbefore the sweep. The second caller is at the top ofmtype_add(), which is the insertion routine shared by the control plane (hash_ip4_uadt) and the packet path (xt_setSET target, thenhash_ip4_kadt), and it neither takes gc.lock nor readst->resizing. On the other side,mtype_resize()copies every element of the old table into a new doubled table while holding onlyrcu_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 structip_set_comment_rcuthat 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.
Quoted 29 lines
Quoted 22 lines
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 entersmtype_addon 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 withCAP_NET_ADMINinside a user namespace. No init-namespace capability, noCAP_NET_RAW(the packets are UDP to 127.0.0.1) and no special filesystem are needed. The kernel must haveCONFIG_IP_SET,CONFIG_IP_SET_HASH_IPandCONFIG_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 withkasan_multi_shotis 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 inlinemtype_gc_do()call at the top ofmtype_add(), versus the guarded call insidemtype_gc(). Alsomtype_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()andip_set_init_comment().6. Suggested fix
Give the inline sweep the same exclusion the workqueue sweep already has: takeh->gc.lockand skip the sweep whent->resizingis set. A more complete variant is to take t->hregion[r].lock around the per-region copy inmtype_resize(), so both sweep entry points are serialized with the resize. For context,cffcf57bf03conly fixed the workqueue caller, andf30415929be8anda442c8a89fb9fixed other sites.7. Mitigation
Do not create ipset sets that use the comment extension on affected kernels, or leaveCONFIG_NETFILTER_XT_SEToff so the packet path cannot entermtype_add. Restricting unprivileged user namespaces removes the unprivileged reachability but does not fix the bug.Quoted 6 lines
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.
Quoted 8 lines
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 listsCan 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
Pablo Neira Ayuso
maintainerHi,
Quoted 3 lines · Jozsef Kadlecsik
Hi,
On Fri, 20 Nov 2026, Willy Tarreau wrote:
[..]
Quoted 7 lines
Quoted 3 lines
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.
Quoted 8 lines
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.