Linux kernel -stable discussions
 help / color / mirror / Atom feed
* [PATCH net] netfilter: ipset: exclude add-path GC during resize
@ 2026-11-17  9:26 onLeaf
  2026-11-17  9:57 ` Greg KH
  0 siblings, 1 reply; 5+ messages in thread
From: onLeaf @ 2026-11-17  9:26 UTC (permalink / raw)
  To: onleaf322; +Cc: stable

mtype_resize() copies elements to the new table including their extension
pointers. Garbage collection of an expired element in the old table after
the copy therefore frees an extension still referenced by the new table.

Commit cffcf57bf03c ("netfilter: ipset: exclude gc when resize is in
progress") prevents the delayed-work collector from running during resize.
However, mtype_add() also invokes mtype_gc_do() directly when its target
region is full. The packet path can enter this call while a userspace
resize is copying the old table. It then frees the old copy's comment, and
collection of the published copy queues the same object for freeing again.

An unprivileged process can reach packet-side ipset add with CAP_NET_ADMIN
in newly created user and network namespaces. The resulting duplicate free
was reproduced with KASAN on x86-64 and exploited from UID 1000 to UID 0 in
the initial user namespace on a non-KASAN build.

Serialize the direct collector with the same GC lock and honor the pinned
table's resizing flag, just as the delayed-work collector does. The table
remains pinned by uref while the lock is held.

The issue was found through AI-assisted source review. On the fixed kernel,
the same copy/old-GC/publish/new-GC ordering completed without producing a
duplicate object; /etc/passwd remained unchanged and the test ended in
LPE_FAIL.

This change was prepared with assistance from OpenAI Codex.

Fixes: f66ee0410b1c ("netfilter: ipset: Fix "INFO: rcu detected stall in hash_xxx" reports")
Cc: stable@vger.kernel.org
Signed-off-by: onLeaf <onleaf322@gmail.com>
---
 net/netfilter/ipset/ip_set_hash_gen.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h
index 80ca523f304b..70f768ba7b44 100644
--- a/net/netfilter/ipset/ip_set_hash_gen.h
+++ b/net/netfilter/ipset/ip_set_hash_gen.h
@@ -1009,8 +1009,12 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
 	maxelem = t->maxelem;
 	if (elements >= maxelem) {
 		u32 e;
-		if (SET_WITH_TIMEOUT(set))
-			mtype_gc_do(set, h, t, r);
+		if (SET_WITH_TIMEOUT(set)) {
+			spin_lock_bh(&h->gc.lock);
+			if (!t->resizing)
+				mtype_gc_do(set, h, t, r);
+			spin_unlock_bh(&h->gc.lock);
+		}
 		maxelem = h->maxelem;
 		elements = 0;
 		for (e = 0; e < ahash_numof_locks(t->htable_bits); e++)
-- 
2.43.0


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

* [PATCH net] netfilter: ipset: exclude add-path GC during resize
@ 2026-11-17  9:27 onLeaf
  2026-11-17  9:57 ` Greg KH
  2026-11-19 21:27 ` netdev-bot+sashiko
  0 siblings, 2 replies; 5+ messages in thread
From: onLeaf @ 2026-11-17  9:27 UTC (permalink / raw)
  To: pablo, fw
  Cc: phil, kadlec, davem, edumazet, kuba, pabeni, horms,
	netfilter-devel, coreteam, netdev, linux-kernel, onLeaf, stable

mtype_resize() copies elements to the new table including their extension
pointers. Garbage collection of an expired element in the old table after
the copy therefore frees an extension still referenced by the new table.

Commit cffcf57bf03c ("netfilter: ipset: exclude gc when resize is in
progress") prevents the delayed-work collector from running during resize.
However, mtype_add() also invokes mtype_gc_do() directly when its target
region is full. The packet path can enter this call while a userspace
resize is copying the old table. It then frees the old copy's comment, and
collection of the published copy queues the same object for freeing again.

An unprivileged process can reach packet-side ipset add with CAP_NET_ADMIN
in newly created user and network namespaces. The resulting duplicate free
was reproduced with KASAN on x86-64 and exploited from UID 1000 to UID 0 in
the initial user namespace on a non-KASAN build.

Serialize the direct collector with the same GC lock and honor the pinned
table's resizing flag, just as the delayed-work collector does. The table
remains pinned by uref while the lock is held.

The issue was found through AI-assisted source review. On the fixed kernel,
the same copy/old-GC/publish/new-GC ordering completed without producing a
duplicate object; /etc/passwd remained unchanged and the test ended in
LPE_FAIL.

This change was prepared with assistance from OpenAI Codex.

Fixes: f66ee0410b1c ("netfilter: ipset: Fix "INFO: rcu detected stall in hash_xxx" reports")
Cc: stable@vger.kernel.org
Signed-off-by: onLeaf <onleaf322@gmail.com>
---
 net/netfilter/ipset/ip_set_hash_gen.h | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h
index 80ca523f304b..70f768ba7b44 100644
--- a/net/netfilter/ipset/ip_set_hash_gen.h
+++ b/net/netfilter/ipset/ip_set_hash_gen.h
@@ -1009,8 +1009,12 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
 	maxelem = t->maxelem;
 	if (elements >= maxelem) {
 		u32 e;
-		if (SET_WITH_TIMEOUT(set))
-			mtype_gc_do(set, h, t, r);
+		if (SET_WITH_TIMEOUT(set)) {
+			spin_lock_bh(&h->gc.lock);
+			if (!t->resizing)
+				mtype_gc_do(set, h, t, r);
+			spin_unlock_bh(&h->gc.lock);
+		}
 		maxelem = h->maxelem;
 		elements = 0;
 		for (e = 0; e < ahash_numof_locks(t->htable_bits); e++)
-- 
2.43.0


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

* Re: [PATCH net] netfilter: ipset: exclude add-path GC during resize
  2026-11-17  9:26 onLeaf
@ 2026-11-17  9:57 ` Greg KH
  0 siblings, 0 replies; 5+ messages in thread
From: Greg KH @ 2026-11-17  9:57 UTC (permalink / raw)
  To: onLeaf; +Cc: stable

On Tue, Nov 17, 2026 at 06:26:31PM +0900, onLeaf wrote:
> mtype_resize() copies elements to the new table including their extension
> pointers. Garbage collection of an expired element in the old table after
> the copy therefore frees an extension still referenced by the new table.
> 
> Commit cffcf57bf03c ("netfilter: ipset: exclude gc when resize is in
> progress") prevents the delayed-work collector from running during resize.
> However, mtype_add() also invokes mtype_gc_do() directly when its target
> region is full. The packet path can enter this call while a userspace
> resize is copying the old table. It then frees the old copy's comment, and
> collection of the published copy queues the same object for freeing again.
> 
> An unprivileged process can reach packet-side ipset add with CAP_NET_ADMIN
> in newly created user and network namespaces. The resulting duplicate free
> was reproduced with KASAN on x86-64 and exploited from UID 1000 to UID 0 in
> the initial user namespace on a non-KASAN build.
> 
> Serialize the direct collector with the same GC lock and honor the pinned
> table's resizing flag, just as the delayed-work collector does. The table
> remains pinned by uref while the lock is held.
> 
> The issue was found through AI-assisted source review. On the fixed kernel,
> the same copy/old-GC/publish/new-GC ordering completed without producing a
> duplicate object; /etc/passwd remained unchanged and the test ended in
> LPE_FAIL.
> 
> This change was prepared with assistance from OpenAI Codex.
> 
> Fixes: f66ee0410b1c ("netfilter: ipset: Fix "INFO: rcu detected stall in hash_xxx" reports")
> Cc: stable@vger.kernel.org
> Signed-off-by: onLeaf <onleaf322@gmail.com>
> ---
>  net/netfilter/ipset/ip_set_hash_gen.h | 8 ++++++--
>  1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h
> index 80ca523f304b..70f768ba7b44 100644
> --- a/net/netfilter/ipset/ip_set_hash_gen.h
> +++ b/net/netfilter/ipset/ip_set_hash_gen.h
> @@ -1009,8 +1009,12 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
>  	maxelem = t->maxelem;
>  	if (elements >= maxelem) {
>  		u32 e;
> -		if (SET_WITH_TIMEOUT(set))
> -			mtype_gc_do(set, h, t, r);
> +		if (SET_WITH_TIMEOUT(set)) {
> +			spin_lock_bh(&h->gc.lock);
> +			if (!t->resizing)
> +				mtype_gc_do(set, h, t, r);
> +			spin_unlock_bh(&h->gc.lock);
> +		}
>  		maxelem = h->maxelem;
>  		elements = 0;
>  		for (e = 0; e < ahash_numof_locks(t->htable_bits); e++)
> -- 
> 2.43.0
> 
> 

<formletter>

This is not the correct way to submit patches for inclusion in the
stable kernel tree.  Please read:
    https://www.kernel.org/doc/html/latest/process/stable-kernel-rules.html
for how to do this properly.

</formletter>

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

* Re: [PATCH net] netfilter: ipset: exclude add-path GC during resize
  2026-11-17  9:27 [PATCH net] netfilter: ipset: exclude add-path GC during resize onLeaf
@ 2026-11-17  9:57 ` Greg KH
  2026-11-19 21:27 ` netdev-bot+sashiko
  1 sibling, 0 replies; 5+ messages in thread
From: Greg KH @ 2026-11-17  9:57 UTC (permalink / raw)
  To: onLeaf
  Cc: pablo, fw, phil, kadlec, davem, edumazet, kuba, pabeni, horms,
	netfilter-devel, coreteam, netdev, linux-kernel, stable

On Tue, Nov 17, 2026 at 06:27:31PM +0900, onLeaf wrote:
> mtype_resize() copies elements to the new table including their extension
> pointers. Garbage collection of an expired element in the old table after
> the copy therefore frees an extension still referenced by the new table.
> 
> Commit cffcf57bf03c ("netfilter: ipset: exclude gc when resize is in
> progress") prevents the delayed-work collector from running during resize.
> However, mtype_add() also invokes mtype_gc_do() directly when its target
> region is full. The packet path can enter this call while a userspace
> resize is copying the old table. It then frees the old copy's comment, and
> collection of the published copy queues the same object for freeing again.
> 
> An unprivileged process can reach packet-side ipset add with CAP_NET_ADMIN
> in newly created user and network namespaces. The resulting duplicate free
> was reproduced with KASAN on x86-64 and exploited from UID 1000 to UID 0 in
> the initial user namespace on a non-KASAN build.
> 
> Serialize the direct collector with the same GC lock and honor the pinned
> table's resizing flag, just as the delayed-work collector does. The table
> remains pinned by uref while the lock is held.
> 
> The issue was found through AI-assisted source review. On the fixed kernel,
> the same copy/old-GC/publish/new-GC ordering completed without producing a
> duplicate object; /etc/passwd remained unchanged and the test ended in
> LPE_FAIL.
> 
> This change was prepared with assistance from OpenAI Codex.
> 
> Fixes: f66ee0410b1c ("netfilter: ipset: Fix "INFO: rcu detected stall in hash_xxx" reports")
> Cc: stable@vger.kernel.org
> Signed-off-by: onLeaf <onleaf322@gmail.com>

Real name please.

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

* Re: [PATCH net] netfilter: ipset: exclude add-path GC during resize
  2026-11-17  9:27 [PATCH net] netfilter: ipset: exclude add-path GC during resize onLeaf
  2026-11-17  9:57 ` Greg KH
@ 2026-11-19 21:27 ` netdev-bot+sashiko
  1 sibling, 0 replies; 5+ messages in thread
From: netdev-bot+sashiko @ 2026-11-19 21:27 UTC (permalink / raw)
  To: onleaf322
  Cc: pablo, fw, phil, kadlec, davem, edumazet, kuba, pabeni, horms,
	netfilter-devel, coreteam, netdev, linux-kernel, stable

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 1 · High: 0 (1) · Medium: 0 · Low: 1

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Critical] The fix is incomplete.
- [Low] The commit carries `Cc: stable@vger.kernel.org` and `Fixes:
  f66ee0410b1c`, a 2020 commit, so stable maintainers will try to apply…

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but a successful
  resize leaks the comment extensions of elements that had already…

--

netfilter: ipset: exclude add-path GC during resize

This patch takes h->gc.lock around the direct mtype_gc_do() call in
mtype_add() and skips the collection when the pinned table has resizing
set. The goal is to keep the packet-side add path from freeing extensions
of old-table elements that mtype_resize() has already copied into the new
table.

> Fixes: f66ee0410b1c ("netfilter: ipset: Fix "INFO: rcu detected stall in hash_xxx" reports")
> Cc: stable@vger.kernel.org

[Severity: Low]
The new code uses h->gc.lock and t->resizing. Both were added by
cffcf57bf03c ("netfilter: ipset: exclude gc when resize is in progress"),
which has no Fixes: or Cc: stable tag. The commit message mentions it only
as background.

Stable trees that have f66ee0410b1c but not cffcf57bf03c won't build this
patch. A hand-adapted backport could also add the add-path check without
the matching resize-side flag and lock.

Should the stable tag record the dependency, for example
"Cc: stable@vger.kernel.org # depends on cffcf57bf03c", or ask for
cffcf57bf03c to be backported as well?

> diff --git a/net/netfilter/ipset/ip_set_hash_gen.h b/net/netfilter/ipset/ip_set_hash_gen.h
> index 80ca523f304bb..70f768ba7b443 100644
> --- a/net/netfilter/ipset/ip_set_hash_gen.h
> +++ b/net/netfilter/ipset/ip_set_hash_gen.h
> @@ -1009,8 +1009,12 @@ mtype_add(struct ip_set *set, void *value, const struct ip_set_ext *ext,
>  	maxelem = t->maxelem;
>  	if (elements >= maxelem) {
>  		u32 e;
> -		if (SET_WITH_TIMEOUT(set))
> -			mtype_gc_do(set, h, t, r);
> +		if (SET_WITH_TIMEOUT(set)) {
> +			spin_lock_bh(&h->gc.lock);
> +			if (!t->resizing)
> +				mtype_gc_do(set, h, t, r);
> +			spin_unlock_bh(&h->gc.lock);
> +		}
>  		maxelem = h->maxelem;
>  		elements = 0;
>  		for (e = 0; e < ahash_numof_locks(t->htable_bits); e++)

[Severity: Critical]
Does this fully close the double free? Only the direct mtype_gc_do() call
is guarded. Other packet-path sites still call ip_set_ext_destroy() on
elements of the pinned old table while mtype_resize() runs.

mtype_resize() makes a shallow copy of each live element. It holds only
rcu_read_lock_bh() and no hregion locks:

    d = ahash_data(m, m->pos, dsize);
    memcpy(d, data, dsize);

After the copy, both tables point to the same ip_set_comment_rcu. Hash
types set region_lock, so packet-side ip_set_add() and ip_set_del() can
run at the same time as this copy.

The first site is the reuse/forceadd branch later in mtype_add():

    if (reuse || forceadd) {
        if (j == -1)
            j = 0;
        data = ahash_data(n, j, set->dsize);
        if (!deleted) {
            mtype_del_cidr_all(set, h, data);
            ip_set_ext_destroy(set, data);
            t->hregion[r].elements--;
        }
        goto copy_data;
    }

This branch can free the comment of an element that was live when copied
and has expired since. That is the same timing as the race in the commit
message. With forceadd it evicts slot 0, a live element that has already
been copied, so no expiry timing is needed.

GC is now skipped on a resizing table. When expired entries are present,
the recomputed elements stays >= maxelem. Doesn't that make forceadd more
likely to take this eviction path?

The second site is mtype_del():

    mtype_del_cidr_all(set, h, d);
    ip_set_ext_destroy(set, data);

    if (t->resizing && ext && ext->target) {
        ...
            x->ad = IPSET_DEL;

This frees the comment in the old table and queues an IPSET_DEL record.
After the new table is published, mtype_resize() replays the record:

    mtype_del(set, &x->d, NULL, NULL, 0);

The replay finds the copied element in the new table and calls
ip_set_ext_destroy() again on the same comment pointer.

ip_set_comment_free() clears only the slot it is given:

    atomic64_sub(sizeof(*c) + strlen(c->str) + 1, &set->ext_size);
    rcu_assign_pointer(comment->c, NULL);
    kfree_rcu(c, rcu);

In each case the new table keeps a dangling pointer. The next GC, delete,
flush/destroy or backlog replay on it then calls strlen() on freed memory
and does a second kfree_rcu(). A dump would also read freed memory
through ip_set_put_comment().

Would it be better to stop or defer all extension destruction on the old
table while t->resizing is set? Another option is to have the new table
take ownership of the extensions during resize, by transfer or deep copy.

[Severity: High]
This is a pre-existing issue, but does a successful resize leak the
comment extensions of elements that had already expired but had not been
collected yet?

mtype_resize() skips those elements during the copy:

    if (SET_ELEM_EXPIRED(set, data))
        continue;

It then frees the old table without extension cleanup:

    if (atomic_dec_and_test(&orig->uref)) {
        pr_debug("Table destroy by resize %p\n", orig);
        mtype_ahash_destroy(set, orig, false);
    }

With ext_destroy set to false, mtype_ahash_destroy() skips
mtype_ext_cleanup(). orig->resizing stays true after a successful resize,
so mtype_gc() never runs mtype_gc_do() on orig. With this patch the add
path doesn't either:

    if (!t->resizing)
        mtype_gc_do(set, h, t, r);

So the ip_set_comment_rcu objects that ip_set_init_comment() allocated
for those elements are never freed. set->ext_size is never reduced for
them either.

Before this patch, the add-path GC could still free some of those
uncopied expired comments while running on orig. This check closes that
window as well.

ipset netlink commands need only CAP_NET_ADMIN in the caller's own user
and network namespace, and these allocations are not memcg accounted. Can
a user repeat create, expire, resize and destroy to use up host memory?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261117092731.14656-1-onleaf322%40gmail.com

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

end of thread, other threads:[~2026-11-19 21:27 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-17  9:27 [PATCH net] netfilter: ipset: exclude add-path GC during resize onLeaf
2026-11-17  9:57 ` Greg KH
2026-11-19 21:27 ` netdev-bot+sashiko
  -- strict thread matches above, loose matches on Subject: below --
2026-11-17  9:26 onLeaf
2026-11-17  9:57 ` Greg KH

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