Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: juny24602@gmail.com
Cc: pablo@netfilter.org, fw@strlen.de, phil@nwl.cc,
	davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, horms@kernel.org, kadlec@netfilter.org,
	echaudro@redhat.com, aconole@redhat.com,
	david.lee@trailofbits.com, manizada@pm.me, yifanwucs@gmail.com,
	netfilter-devel@vger.kernel.org, coreteam@netfilter.org,
	netdev@vger.kernel.org, junvyyang@tencent.com,
	corvus@tencent.com, stable@kernel.org
Subject: Re: [PATCH net] netfilter: ipset: skip inline gc during hash resize
Date: Sat, 21 Nov 2026 11:24:40 +0000	[thread overview]
Message-ID: <179526028037.3135.6082902394676287459@kernel.org> (raw)
In-Reply-To: <20261117110619.46590-1-juny24602@gmail.com>

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

Critical: 0 · High: 1 (1) · Medium: 0 (1) · Low: 1 (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).

- [High] The fix is incomplete.
- [Low] Undocumented behaviour change.

Pre-existing issues:
- [High] This problem wasn't introduced by this patch, but it is a double
  free of the same kind.
- [Medium] Memory leak (pre-existing).
- [Low] Stale comments (pre-existing).

--

netfilter: ipset: skip inline gc during hash resize

This patch takes h->gc.lock and checks t->resizing before mtype_add()
runs inline garbage collection on a full hash region. The goal is to
avoid freeing a comment extension in the old table after mtype_resize()
has shallow-copied it into the new table, since the new table would
later free it again.

> 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);
> +		}

[Severity: High]
Does this fully close the double free described in the commit message?

Skipping mtype_gc_do() leaves the expired element in the old table. The
same mtype_add() call can then free its extensions a few lines later
through the reuse branch:

net/netfilter/ipset/ip_set_hash_gen.h:mtype_add() {
    ...
		/* Reuse first timed out entry */
		if (SET_ELEM_EXPIRED(set, data) && j == -1) {
			j = i;
			reuse = true;
		}
	}
	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;
	}
    ...
}

Hash types set region_lock, so ip_set_add() from the SET target takes
no set->lock. mtype_resize() copies elements under rcu_read_lock_bh()
only. That seems to allow this sequence:

1. mtype_resize() memcpy()s a live element E, including its comment
   pointer C, into the new table.
2. E expires.
3. A SET target add of a different value hashes to E's bucket in the
   old table. It finds E expired and sets reuse, with deleted still
   false.
4. ip_set_ext_destroy()->ip_set_comment_free() calls kfree_rcu(C) and
   clears only the old table's pointer.
5. The new table's copy still holds C. Periodic GC, mtype_flush(), or
   mtype_destroy()->mtype_ahash_destroy(set, t, true)->
   mtype_ext_cleanup() then frees C again, after reading strlen(c->str)
   from freed memory.

The forceadd case also looks easier to reach now. With GC skipped,
elements stays at or above maxelem, so SET_WITH_FORCEADD sets get:

		if (elements >= maxelem && SET_WITH_FORCEADD(set))
			forceadd = true;

The reuse/forceadd branch then evicts slot 0. That slot may hold a live
element that resize has already copied.

Guarding only mtype_gc_do() doesn't seem to be enough. Would the fix
need to block every old table extension free while t->resizing is set
(this branch and mtype_del() too), or transfer ownership of the
comment instead?

>  		maxelem = h->maxelem;
>  		elements = 0;
>  		for (e = 0; e < ahash_numof_locks(t->htable_bits); e++)

[Severity: Low]
Is this behavior change intended, and should the commit message mention
it? While t->resizing is set, inline GC is skipped, so the elements
total recomputed here still counts expired entries.

Suppose that total is >= h->maxelem, forceadd is not set, and the
target bucket has no expired or deleted slot to reuse. mtype_add() now
reaches:

	if (elements >= maxelem)
		goto set_full;

or the set_full path for an empty bucket. The set_full path skips the
resize: block, which is where kernel side adds get queued for replay:

	if (t->resizing && ext && ext->target) {
		/* Resize is in process and kernel side add, save values */

The add returns -IPSET_ERR_HASH_FULL and may log the ratelimited
"Set %s is full" warning. It is never replayed into the new table,
which would have had room because resize does not copy expired entries.

Before this patch, inline GC lowered the counts, so the add succeeded
and was queued.

[Severity: High]
This isn't a bug introduced by this patch, but a SET target delete
(--del-set) that runs during mtype_resize() causes a similar double
free.

mtype_del() destroys the extension on the old table element and then
queues the delete for replay:

net/netfilter/ipset/ip_set_hash_gen.h:mtype_del() {
    ...
		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
			 */
			x = kzalloc_obj(struct mtype_resize_ad, GFP_ATOMIC);
    ...
}

After the table switch and synchronize_rcu(), mtype_resize() replays
the delete on the new table:

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

Suppose element E was already copied into the new table as E'. The
replay finds E' and calls ip_set_ext_destroy() on the same comment C a
second time. ip_set_comment_free() then reads strlen(c->str) from freed
memory and calls kfree_rcu(c) again.

If E' has expired by the time of the replay, the replay skips it. A
later GC, flush or destroy of the new table still frees C again. No
expiry is needed: the delete only has to land after the element's
region was copied and before the table switch.

Could this be fixed together with the add side?

[Severity: Low]
This is a pre-existing issue, but the file header comment in
ip_set_hash_gen.h says:

 * are serialized by the nfnl mutex. During resizing the set is
 * read-locked, so the only possible concurrent operations are
 * the kernel side readers. Those must be protected by proper RCU locking.

That contradicts the comment in mtype_resize():

	/* There can't be another parallel resizing,
	 * but dumping and kernel side add/del are possible
	 */

It also contradicts the t->resizing replay handling in mtype_add() and
mtype_del().

The comment above ip_set_comment_free() in ip_set_core.c says:

/* Called from uadd/udel, flush or the garbage collectors protected
 * by the set spinlock.

However, mtype_gc_do() never holds set->lock, including at the call
site changed here. It holds only the region lock, plus h->gc.lock after
this patch.

Both comments state locking assumptions that the double free cases
above show to be wrong. Could they be updated?

[Severity: Medium]
This is a pre-existing issue, but does mtype_resize() leak the comment
of each expired element it skips? The copy loop does:

				if (SET_ELEM_EXPIRED(set, data))
					continue;

It never calls ip_set_ext_destroy() on these elements. The old table is
then released with:

		mtype_ahash_destroy(set, orig, false);

With ext_destroy false, mtype_ahash_destroy() skips
mtype_ext_cleanup():

		if (set->extensions & IPSET_EXT_DESTROY && ext_destroy)
			mtype_ext_cleanup(set, n);

So each skipped element's ip_set_comment_rcu is never freed, and
set->ext_size is never decremented for it. The last uref paths in
mtype_add(), mtype_del() and mtype_gc() release the old table the same
way.

This patch skips inline GC while resizing, so it can leave a few more
expired entries in the old table for resize to drop.

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

      reply	other threads:[~2026-11-21 11:24 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 11:06 [PATCH net] netfilter: ipset: skip inline gc during hash resize Jun Yang
2026-11-21 11:24 ` netdev-bot+sashiko [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=179526028037.3135.6082902394676287459@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=aconole@redhat.com \
    --cc=coreteam@netfilter.org \
    --cc=corvus@tencent.com \
    --cc=davem@davemloft.net \
    --cc=david.lee@trailofbits.com \
    --cc=echaudro@redhat.com \
    --cc=edumazet@kernel.org \
    --cc=fw@strlen.de \
    --cc=horms@kernel.org \
    --cc=junvyyang@tencent.com \
    --cc=juny24602@gmail.com \
    --cc=kadlec@netfilter.org \
    --cc=kuba@kernel.org \
    --cc=manizada@pm.me \
    --cc=netdev@vger.kernel.org \
    --cc=netfilter-devel@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=pablo@netfilter.org \
    --cc=phil@nwl.cc \
    --cc=stable@kernel.org \
    --cc=yifanwucs@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox