๐Ÿš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #20119

Re: [PATCH net] netfilter: ipset: skip inline gc during hash resize

#20119
From: netdev-bot+sashiko
Date: Sat, 21 Nov 2026 11:24
235 lines
16129 bytes
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
Technical Details
Message-ID: <179526028037.3135.6082902394676287459@kernel.org>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-368326-linux-kernel-netdev=dewy.ru
References: <20261117110619.46590-1-juny24602@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-368326-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net] netfilter: ipset: skip inline gc during hash resize
Date: Sat, 21 Nov 2026 11:24:40 +0000
Organization: Usenet Network
Message-ID: <179526028037.3135.6082902394676287459@kernel.org>
References: <20261117110619.46590-1-juny24602@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3399123"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:RE5qyWMI7AIx6HbJxwVXwmo2zFw= sha256:+PTLq8/PvKKrp9ueMCiJYtZwDYAJOjsoj3TAvDJjwUc=
	sha1:AcJjQpS7itjrT4UWvqmlBzLqxag= sha256:ETt9E02GwJ1FzwMydHNw+WGP+pQXZ8DUSeXqR9STxgE=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 7047F1153E0; Sat, 21 Nov 2026 14:25:03 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1,
	DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1,
	MAILING_LIST_MULTI=-1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001,
	SPF_PASS=-0.001] autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id 62d_C-6sEthQ;
	Sat, 21 Nov 2026 14:24:58 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-368326-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 4321E11584C; Sat, 21 Nov 2026 14:24:46 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 7B0E83FCF1; Sat, 21 Nov 2026 11:24:43 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795260283;
	b=kIWozrbm2DSJVsex1O8QW1SvDT8adalG4MJBty/1Vj9yP/xC4+idCsWhZXUzDItBW0WG
	 z2OMrA19bVYwFYJgqdP13gDL8lxCFvV9kuoZiGXCoMfsDGjoERtEPuriSm2LI0efHcL0o
	 MB2XMcCTWLSANMjoqamS+PvlW45q51PqCeMm285NbW2GMDkCEQSJG8NtW6SBDHyr4oOpu
	 KaiPTjlr1BOYPVjrlVsNGhCG4xCtzkLAUOA4qCe31WnO3tKozCf+4vcCMMGFjQ7Lp2c+i
	 3RyVpXdgTrd6LgyZJPCQJ2G3A6fEAQxeRjQ/brTfSBmpjcdFZVQQD54CAR+OtJQgfIw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795260283;
	h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc:
	 Date:Message-ID:In-Reply-To:References:X-sashiko-severity:
	 Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
	 List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
	 Precedence:MIME-Version;
	bh=LbixzkneRqLhhpaWwV4gyxp4t0JBzGNMSQT5/n1d4Jo=;
	b=WngoRdnKCgx3NFwOVG6P9YsUKreshlxuQ6zSVlMvE1GJ3kvhQarO9qEtG8V38j7ss2+/
	 kx1IdjGxU/6o9Oese5A7s5AbRGMgSHivBf0bZT+CqmuwFV5aJxjgbC/W7R0porTBktj0y
	 09bH58Fr3ukb5tfdVqgYyCn4CvlAO77DbsYJX7/7Ah5Ale75Sp0klGPtooh4tUn0Svblp
	 oM7Ms7QN4sg+ZZGvDxJC4R0K4YFSLYekJ/rVauqBfgKdfTyTY2/xjSzvoIViJWmFs4VXe
	 re2Ixf8M+80oiJkwJY4lFa43H9RZkogIzGkrHaUxkz2BbXubs3ZybYmbAaeUgb3zUSA==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BHLkPSte;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 1F112606A336;
	Sat, 21 Nov 2026 11:24:43 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BHLkPSte
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795260282;
	b=Czc76hVUEEfFQq4Cwir0t0Hb0Q9jmB5YMBvIX4/vO2sb8jDMguHvQ13UiqALomTxjN6n
	 9nhlJm7hsUCGvEydzWleX6iOTsadTuP3DCDmbeXmpEZVW78engEkXyOv90VTyeDpMmTH1
	 SLAsCYVozvTd/eVlApkxTNz6hKjH+HnwQJ8xcfYYkDHsVksRbCMnN+noCZe8CK7hFOksD
	 ZrlhM/mxHXFb4tkD3/CwG0BDGw7eHfQPJaZMZ+oQh7JoEB2czQ/wtDIs0XN8SxwmlqW18
	 F9DgmFvBXN9lJnQU5ayNLiykxEOaUOQlfHlp23/0VAfp7XZXEmcAgrhxMEaQLRrtZ5A==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795260282;
	h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID:
	 In-Reply-To:References:X-sashiko-severity:Content-Type:
	 Content-Transfer-Encoding:MIME-Version;
	bh=LbixzkneRqLhhpaWwV4gyxp4t0JBzGNMSQT5/n1d4Jo=;
	b=bQF0WK6B4hybLm23EjO4DhBTun5oITSRfb6dxbM1b4ohfOel/2GOMa+/F3Wi14HEDCkl
	 S4FaJgbHpgX3fMZgvUgTVF/ARpqy8+USaTs/zHH4QqpiRiUzufmi79RGovrmCB5OUWnkg
	 yFElfdUCng1N2PjecWEmEcApDh3b+4oM6U/q8fmqozhrAIynu09XynLgMQg/hpqc4dHTT
	 M7190ZHeEn3ajH2W72i74pDJCbo+Qzo9ljrb3nlqL+a6BOGCfQ+DkhPLN/BPWVMw84aXk
	 b/bSPzJyQ+jNS23gHJyR6RmgCP+QBJ4X8n2DM7XbV1u4RX0+nIzBxM6JtM8L+oAlfAw==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=BHLkPSte;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 3BD01606A320;
	Sat, 21 Nov 2026 11:24:42 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id CD58C1F000C4;
	Sat, 21 Nov 2026 11:24:40 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1795260281;
	bh=LbixzkneRqLhhpaWwV4gyxp4t0JBzGNMSQT5/n1d4Jo=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=BHLkPSteByQ7hIb7bMzVMXtYfsmpoibtfzKf4G45RltojxNBinYeSHBhkh7RVsgeu
	 9wLsrixCGCxwS0QvFG5MK95G+LkhoIsrf329mxoQZQezByXG+L3ueLSnPoI/Sxcy7n
	 OsHh3CLuvGvvduYChm6vThI4fyCLZuDUwMPJlU1QQDgCnT1AJjWYpsDs/11mNKXEaz
	 yd4aoDJ5nAxJSlfN5I+7BzI51I4rnTlRA2xtJdEsoNvkYDVwqFlCidN4zwJ6oBS2oO
	 D7mV1HTttgdgJN3RV25XgK7cL6UgDdZ187QbDQk146E8c7EeHQPyqu9Wlla0Kx+q6b
	 Lia00LXMg/UhQ==
X-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
X-In-Reply-To: <20261117110619.46590-1-juny24602@gmail.com>
X-sashiko-severity: High
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list