🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #21003

Re: [PATCH net v2] igmp: use copy-on-write RCU updates in ip_mc_source()

#21003
From: netdev-bot+sashiko
Date: Mon, 23 Nov 2026 13:13
194 lines
14264 bytes
Thank you for your contribution! Sashiko AI review found 3 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 1 · Low: 2

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).

- [Medium] In ip_mc_source() (net/ipv4/igmp.c), the rewritten add path
  makes the source list far larger than it needs to be.
- [Low] In ip_mc_source() (net/ipv4/igmp.c), removing the last source
  from an EXCLUDE-mode filter now sets pmc->sflist to NULL (`if…
- [Low] In ip_mc_source() (net/ipv4/igmp.c), the source-removal path
  (IP_DROP_SOURCE_MEMBERSHIP, IP_UNBLOCK_SOURCE,…

--

igmp: use copy-on-write RCU updates in ip_mc_source()

This commit changes ip_mc_source() so that adding or removing a source
filter entry builds a fully populated replacement ip_sf_socklist. It then
publishes that list with rcu_assign_pointer() and frees the old one with
kfree_rcu(). The goal is to stop ip_mc_sf_allow() readers from seeing a
list that is being changed in place.

> diff --git a/net/ipv4/igmp.c b/net/ipv4/igmp.c
> index 52cde8782eebd..75292f4550216 100644
> --- a/net/ipv4/igmp.c
> +++ b/net/ipv4/igmp.c

[ ... ]

> @@ -2556,13 +2556,33 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
>  			goto done;
>  		}
>  
> +		atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
> +			&sk->sk_omem_alloc);
> +		if (psl->sl_count == 1) {
> +			newpsl = NULL;

[Severity: Low]
Is this change in setsockopt behaviour intended? When the last source is
removed from an MCAST_EXCLUDE filter, this now publishes NULL into
pmc->sflist. The old code shifted the entries in place and left a non-NULL
list with sl_count == 0. The MCAST_INCLUDE case with one source becomes
leavegroup before it gets here.

The mode check at the top of ip_mc_source() only allows a mode switch
when sflist is NULL:

	if (pmc->sflist) {
		if (pmc->sfmode != omode) {
			err = -EINVAL;
			goto done;
		}
	} else if (pmc->sfmode != omode) {
		/* allow mode switches for empty-set filters */

Take this sequence: IP_ADD_MEMBERSHIP, IP_BLOCK_SOURCE S,
IP_UNBLOCK_SOURCE S, then IP_ADD_SOURCE_MEMBERSHIP S2. The last call used
to fail with -EINVAL. It now succeeds and switches the group to
MCAST_INCLUDE with S2.

This matches what ip_mc_msfilter() and ip6_mc_source() already do.
ip_mc_sf_allow() also treats a NULL MCAST_EXCLUDE list and an empty one
the same way.

Since this is a Fixes: tagged data race fix, should the commit message
mention the change?

> +		} else {
> +			newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr,
> +				psl->sl_count - 1), GFP_KERNEL);
> +			if (!newpsl) {
> +				atomic_add(struct_size(psl, sl_addr, psl->sl_max),
> +					&sk->sk_omem_alloc);
> +				err = -ENOBUFS;
> +				goto done;
> +			}

[Severity: Low]
Can removing a source now fail with -ENOBUFS? Before this patch,
IP_DROP_SOURCE_MEMBERSHIP, IP_UNBLOCK_SOURCE, MCAST_LEAVE_SOURCE_GROUP and
MCAST_UNBLOCK_SOURCE always succeeded once the source was found.

The old list's charge is subtracted first, so the optmem check usually
passes. However, sock_kmalloc() reads optmem_max at call time:

	int optmem_max = READ_ONCE(sock_net(sk)->core.sysctl_optmem_max);

	if ((unsigned int)size <= optmem_max &&
	    atomic_read(&sk->sk_omem_alloc) + size < optmem_max) {

This allocation can fail in two cases:

  - optmem_max was lowered below the socket's other option memory use
  - the GFP_KERNEL allocation fails for a large list

In either case userspace cannot drop a single source, and an
MCAST_EXCLUDE block stays in force.

The error path itself restores the accounting cleanly. The workarounds
are IP_MSFILTER with zero sources or leaving the group.
ip6_mc_source() can fail the same way.

> +			newpsl->sl_max = psl->sl_count - 1;
> +			newpsl->sl_count = psl->sl_count - 1;

[ ... ]

> @@ -2572,49 +2592,52 @@ int ip_mc_source(int add, int omode, struct sock *sk, struct
>  		err = -ENOBUFS;
>  		goto done;
>  	}
> -	if (!psl || psl->sl_count == psl->sl_max) {
> -		struct ip_sf_socklist *newpsl;
> -		int count = IP_SFBLOCK;

[ ... ]

> +	i = psl ? psl->sl_count + 1 : 1;
> +	count = IP_SFBLOCK;
> +	if (psl)
> +		count += psl->sl_max;
> +	newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr, count),
> +		GFP_KERNEL);
> +	if (!newpsl) {
> +		err = -ENOBUFS;
> +		goto done;
> +	}
> +	newpsl->sl_max = count;
> +	newpsl->sl_count = i;

[Severity: Medium]
Does sl_max now grow by IP_SFBLOCK on every add? The old code only
reallocated when sl_count == sl_max, so the spare slots were filled
before the list grew.

Now a new list is allocated on every add, but it is still sized as the
old sl_max plus IP_SFBLOCK. The spare slots are never used:

  add 1: sl_count = 1, sl_max = 10
  add 2: sl_count = 2, sl_max = 20
  add 3: sl_count = 3, sl_max = 30

After N adds the buffer has room for about 10*N entries instead of N.

The old list's charge is only removed after the new allocation succeeds:

	if (psl) {
		/* decrease mem now to avoid the memleak warning */
		atomic_sub(struct_size(psl, sl_addr, psl->sl_max),
			&sk->sk_omem_alloc);
	}

So both oversized buffers have to fit under optmem_max in sock_kmalloc()
at the same time.

Assume the default optmem_max of 128 KiB and a raised igmp_max_msf. In
that case IP_ADD_SOURCE_MEMBERSHIP, IP_BLOCK_SOURCE and
MCAST_JOIN_SOURCE_GROUP start returning -ENOBUFS at about 1600 sources
per group, compared with about 16000 before. Large lists also become
high-order allocations.

With the default igmp_max_msf of 10, sl_max stays at or below 100.

The delete path in this patch and ip_mc_msfilter() size the new list
exactly. So does ip6_mc_source() in net/ipv6/mcast.c:

	i = psl ? psl->sl_count + 1 : 1;
	newpsl = sock_kmalloc(sk, struct_size(newpsl, sl_addr, i),
			      GFP_KERNEL);
	...
	newpsl->sl_max = i;
	newpsl->sl_count = i;

Should the IPv4 add path use struct_size(newpsl, sl_addr, i) and
sl_max = i as well?

> +	if (psl) {
> +		for (j = 0; j < psl->sl_count; j++)
> +			newpsl->sl_addr[j] = psl->sl_addr[j];
> +	}
> +	newpsl->sl_addr[i - 1] = mreqs->imr_sourceaddr;

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261119125112.540125-1-18983559317%40163.com
Technical Details
Message-ID: <179543961529.3135.8092538762905615997@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-369382-linux-kernel-netdev=dewy.ru
References: <20261119125112.540125-1-18983559317@163.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-369382-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net v2] igmp: use copy-on-write RCU updates in ip_mc_source()
Date: Mon, 23 Nov 2026 13:13:35 +0000
Organization: Usenet Network
Message-ID: <179543961529.3135.8092538762905615997@kernel.org>
References: <20261119125112.540125-1-18983559317@163.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3885157"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:DfFPDQeMcpYeRzeoHJlAgSrxgrI= sha256:4rD8tIlQ/XU+GvpJE2sWN+LIB/WSZDvxsqf4p6ZmKQA=
	sha1:8jXVAi7g2vsK5BND15vHHl5lUwQ= sha256:nM7WKyHMzdhHrzA1jxks27V2JzAZtHOH3yS5rKH5tHs=
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 226F4125DAF; Mon, 23 Nov 2026 16:13:54 +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 MDHanSQzy5Md;
	Mon, 23 Nov 2026 16:13:50 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-369382-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 tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id 14E6E125DAD; Mon, 23 Nov 2026 16:13:41 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id A4E362AE00; Mon, 23 Nov 2026 13:13:40 +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=1795439617;
	b=wzbkvW+7v1g8rO2cDVsSAA3Jt7C9Jt8wS+x9MZQ9H8A45m/ttebHblw7PNlufnjYEeIa
	 t9iMUJiIbaDM+EfqjvRvwS9++4OSeLuoCS81OXLNgnmqo6cMR2VYrJWsjrWJrVh1AURep
	 FU7uOZODDw82BidSZr3QDxgiUilvLJHYXtK6+zx8Y3JDSJqo10R6BJ5nk/xv4LAvwQKYH
	 EvnwViHqKAi9Pw3QkAqnYaCM4wOJWMaaBU+SctM2Cy5SgGl1L6D1a1TA3S7o6BGU4kGjZ
	 f5IfSat8qbZ8TDJVTnMcZszvrQD9QSatD3vDZHnx0ikK/VnKnEadU/S2idPv+8TKEiA==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795439617;
	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=s4xiX9NuRtD/nUozH1kv7Vg68ZZWMqUGKWxJHU6wK94=;
	b=T3J7VchpgbDdn55ALdQfi4ed4AO9HaWb6TTUzZjiZBB+jccBevZtrK5FwgHZKWgv4q8i
	 mfiwJI0+mb5mTrpk8X8BUROYOP7HmkD44QowZyZpuJLg7WdIcPiOAwd/P2LxiRb9fe0mF
	 kQ3XkvhBlJHrbwJTj9+dB5tCasg21ks+qUaYHwU//l1ARkgawya5VjCZrMUzI6zZ7jIji
	 QGTSbzqRU3BQwQvY1RjDC8tDZBswMN6xSVHaG5izjmfY/nbSXdj7UyoOMhW68tZQ/KEI3
	 yvd8NhpQSkprC5g5qBMXB1oDxVfJFnWbDHabdWNm+orrRtDs3QBEoCy2H5rpTXkTsOA==
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=mdfux8c2;
	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 ACB00606A332;
	Mon, 23 Nov 2026 13:13:37 +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=mdfux8c2
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=1795439616;
	b=pwTO1f6z0GmSp01tCdlhxUnhq/XWvAybcT/qSmH3SwP8k5f4GnJOU7vW66gSiSnP9Rmk
	 kKfHGvHXN8oQTFuNarrbrUMvYfqAg5xIkTMm8+Svdufvb6sDtI1bqByez+cN245JFpdFk
	 767kSjxUpdxUrMuHM1iWbVLSehnEwTxGOeKsoiPqfunvMQ5TW6SlMtNu4Jn3OpFTixeiM
	 +/nwawE3lbRz5Ko3p6EYHKcgVn8CBAkMhSnLZSvCv3RAue0N8IPBK/UxRwanhoE9jY2oD
	 i5dD5JERW11kGfEMeAZ5380oeKyNWm3a6mQFib9k/uoJtOmSVqHUsPz5oQeKcngb+KQ==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795439616;
	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=s4xiX9NuRtD/nUozH1kv7Vg68ZZWMqUGKWxJHU6wK94=;
	b=FeXClYehirsOjTyFvg2iaqTnGBE6eUWoU09cigGpnFjfrWG7L2A+zQaHj3n1ULG44pN/
	 ohaA94pvk0D3iPBDnoemoO/Hj52SgvUl64QagwB8YuVV5NXJrPF+jcK67ImEnvARhsHe6
	 nAIuHB1wq2kDO56OnmESDBO7s60EBsQ33YSI3a1M1QUp7BFXeUiOVbDlTRJ6XhQfNV1FC
	 /pngi4z843BOu6DiPaTIc7tb+6jOnIUBOwMuFkozKHNEESmUVor3umlMX/nefnMs/SmCK
	 bXQGOLx5pGmxx/okFBW9R4Jl5whcgYYVASmYGxZCuPdygkLgkck4WYIJtvnowddEWNA==
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=mdfux8c2;
	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 C7896606A31A;
	Mon, 23 Nov 2026 13:13:36 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id BD3A71F000D8;
	Mon, 23 Nov 2026 13:13:35 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1795439616;
	bh=s4xiX9NuRtD/nUozH1kv7Vg68ZZWMqUGKWxJHU6wK94=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=mdfux8c2bpM++Nz5z/Wn9NcH8DoKCYL3wZx2FblfqxcC3VZMWFrlO6JpXp/7oWRyU
	 wGVl+umzjvhuoq9A6PkCZ06p0ceW+ofUtMqys75sx192kjjsTs4NV6SQA+c26zknWH
	 U5ebwHvaCOFSvMUYMOSzHM97qBohzQxCJiFaTxscGESBQoC8UIahGW2Q1Q/8MSn917
	 YZyIGvOPMVGPgmMN+z5ZHdDW03fB3z6i6IwX0yyFsiREj2QW3/tYkA+A1SGeR67f0X
	 iFhqc71r015agmGdfZHNU3dSfD5BC2CwT23ASaeAuQ5MNWz4kbL+qpbY18dmFMZUB+
	 Vx7iV3aE1i3yg==
X-Cc: dsahern@kernel.org, idosch@nvidia.com, davem@davemloft.net,
 edumazet@google.com, kuba@kernel.org, pabeni@redhat.com,
 netdev@vger.kernel.org, linux-kernel@vger.kernel.org
X-In-Reply-To: <20261119125112.540125-1-18983559317@163.com>
X-sashiko-severity: Medium
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