🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #22023

Re: [PATCH net 2/4] udp: Keep the bound device on disconnect unless connect() set it

#22023
From: netdev-bot+sashiko
Date: Tue, 24 Nov 2026 18:30
206 lines
16354 bytes
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.

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

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] The commit message says that clearing sk_bound_dev_if on
  disconnect "is only safe for a device that connect() set", and…
- [Low] The committed message leaves out or blurs user-visible behaviour
  changes of a Cc: stable patch.
- [Low] __udp_disconnect() now does an unlocked read-modify-write of
  sk->sk_userlocks: `WRITE_ONCE(sk->sk_userlocks, sk->sk_userlocks &…
- [Low] __udp_disconnect() takes a snapshot (`u8 userlocks =
  READ_ONCE(sk->sk_userlocks)`) and later uses it to decide whether to…

--

udp: Keep the bound device on disconnect unless connect() set it

This patch adds a SOCK_CONNECT_DEV flag to sk_userlocks. The flag records
that __ip6_datagram_connect() set the bound device on a socket that had
none, and SO_BINDTODEVICE and SO_BINDTOIFINDEX drop it. __udp_disconnect()
now clears sk_bound_dev_if only when the flag is set, which stops a socket
from clearing a device that the user or a cgroup program set.

> This changes connect(AF_UNSPEC) for UDP, ping, raw and L2TP sockets,
> which all use __udp_disconnect(): a device that connect() did not set
> is now kept, so changing it afterwards needs CAP_NET_RAW, as on a
> socket that never disconnected.

[Severity: Low]
Does this paragraph cover all of the behaviour changes?

In __ip6_datagram_connect(), connect() to a link-local peer can replace
a VRF master M with a slave S. old_dif is non-zero there, so
ip6_datagram_connect_dev() does not set SOCK_CONNECT_DEV. Disconnect now
keeps S, where it used to reset the device to 0, and binding back to M
then needs CAP_NET_RAW.

So a device that connect() did set is now kept, and the sentence above
doesn't cover that. The change is only described in the notes below the
--- line, and those notes won't be in the commit that stable backporters
read.

__udp_disconnect() is also called from raw_abort() in net/ipv4/raw.c and
from udp_abort() (SOCK_DESTROY and bpf_sock_destroy). Those paths now keep
a device set by the user or by a cgroup program as well.

Could the commit message mention the VRF change and the abort paths?

> diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> index 446729b41d661..9f1f45afee532 100644
> --- a/net/ipv4/udp.c
> +++ b/net/ipv4/udp.c
> @@ -2195,6 +2195,7 @@ static int udp_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
>  
>  int __udp_disconnect(struct sock *sk, int flags)
>  {
> +	u8 userlocks = READ_ONCE(sk->sk_userlocks);

[Severity: Low]
Can this snapshot be stale by the time it is used to clear
sk_bound_dev_if?

Ping and raw sockets use __udp_disconnect() as their ->disconnect, and
l2tp_ip6_disconnect() calls it. All of them get there from
inet_dgram_connect(AF_UNSPEC) without lock_sock(). Only udp_disconnect()
takes the socket lock.

Take an IPv6 ping, raw or L2TP socket connected to a link-local peer, so
it has device X and SOCK_CONNECT_DEV set:

thread B: __udp_disconnect()
    userlocks = READ_ONCE(sk->sk_userlocks);   /* has SOCK_CONNECT_DEV */

thread C: __udp_disconnect() completes
    sk_bound_dev_if = 0, SOCK_CONNECT_DEV cleared

thread A: setsockopt(SO_BINDTODEVICE, Y)
    sock_bindtoindex_locked()
        /* device is 0, so no CAP_NET_RAW check */
        sk_bound_dev_if = Y, SOCK_CONNECT_DEV cleared

thread B resumes:
    if (userlocks & SOCK_CONNECT_DEV) {
        WRITE_ONCE(sk->sk_bound_dev_if, 0);

Disconnect then clears the device that SO_BINDTODEVICE set. For ping, raw
and L2TP this contradicts "a device that connect() did not set is now
kept". The result matches the pre-patch behaviour, and the window is
small.

>  	struct inet_sock *inet = inet_sk(sk);
>  	/*
>  	 *	1003.1g - break association.
> @@ -2204,15 +2205,19 @@ int __udp_disconnect(struct sock *sk, int flags)
>  	WRITE_ONCE(inet->inet_daddr, 0);
>  	inet->inet_dport = 0;
>  	sock_rps_reset_rxhash(sk);
> -	WRITE_ONCE(sk->sk_bound_dev_if, 0);
> -	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) {
> +	if (userlocks & SOCK_CONNECT_DEV) {
> +		WRITE_ONCE(sk->sk_bound_dev_if, 0);

[Severity: Medium]
The commit message says clearing the device "is only safe for a device
that connect() set", and this still clears it without a bind conflict
check. Does that hold for L2TP/IPv6?

For UDP, the next patch in the series ("udp: Ignore a connect()-set device
in the bind conflict check") makes udp_lib_lport_inuse() and
udp_lib_lport_inuse2() treat a SOCK_CONNECT_DEV device as 0. That patch
only changes udp.c, and __l2tp_ip6_bind_lookup() still compares the raw
device:

net/l2tp/l2tp_ip6.c:__l2tp_ip6_bind_lookup() {
    ...
		bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
		if (bound_dev_if && dif && bound_dev_if != dif)
			continue;
    ...
}

l2tp_ip6_connect() goes through __ip6_datagram_connect(), and
l2tp_ip6_disconnect() calls __udp_disconnect(). An l2tp_ip6 socket needs
no capability, because inet6_create() checks CAP_NET_RAW only for
SOCK_RAW. The module also loads automatically through
MODULE_ALIAS_NET_PF_PROTO_TYPE().

Could an unprivileged user do the following?

1. attacker: bind conn_id X with no device, which holds X on every device
2. attacker: connect(fe80::1%eth1), so the device becomes eth1 and
   SOCK_CONNECT_DEV is set
3. victim: SO_BINDTODEVICE(eth0), bind conn_id X. This succeeds because
   the attacker's eth1 is skipped.
4. attacker: connect(AF_UNSPEC), which puts the device back to 0 with no
   conflict check
5. attacker: connect() to the victim's real peer G. The peer is not
   scoped, so the device stays 0, and l2tp_ip6_connect() moves the socket
   to the head of l2tp_ip6_bind_table.
6. l2tp_ip6_recv() takes the first match, so the attacker gets G's L2TP
   packets for conn_id X that arrive on eth0

Without this sequence, the victim's bind in step 3 would fail with
EADDRINUSE.

A related point: l2tp_ip6_bind() never sets SOCK_BINDADDR_LOCK. So
__udp_disconnect() always calls inet_reset_saddr() on L2TP sockets, and
the bound address gets wider too.

There is also a VRF case that this patch introduces. Take a socket bound
to VRF master M that connects to a link-local peer on slave S. It gets
device S without SOCK_CONNECT_DEV because old_dif is non-zero, and it now
keeps S after disconnect instead of going back to 0.

Another VRF socket's bind of M:P is then allowed, because S != M in
udp_lib_lport_inuse(). Before this patch, that bind conflicted with the
socket's device 0.

__udp_disconnect() doesn't clear sk_v6_daddr. So for datagrams from its
former peer, the S-bound socket then scores higher than the M-bound one.

Is the series meant to handle the L2TP/IPv6 and VRF cases as well?

> +		WRITE_ONCE(sk->sk_userlocks,
> +			   sk->sk_userlocks & ~SOCK_CONNECT_DEV);

[Severity: Low]
Is this read-modify-write of sk_userlocks safe without the socket lock?

For ping, raw and L2TP sockets this runs from inet_dgram_connect(AF_UNSPEC)
-> prot->disconnect without lock_sock(). Other code updates the same u8
with plain read-modify-writes under the socket lock, and that lock doesn't
exclude this path. Those writers include SO_SNDBUF in sk_setsockopt():

    sk->sk_userlocks |= SOCK_SNDBUF_LOCK;

The others are SO_RCVBUF, SO_BUF_LOCK, sock_bindtoindex_locked() and
ip6_datagram_connect_dev().

An update that lands between this load and store is lost or undone. A
SOCK_SNDBUF_LOCK that was just set can vanish. A buffer lock bit that
SO_BUF_LOCK just cleared can come back. A SOCK_CONNECT_DEV set by a
concurrent connect() can be dropped.

The plain sk->sk_userlocks load inside WRITE_ONCE() also races with those
writers, so KCSAN is likely to flag it.

Before this patch, the unlocked path only read sk_userlocks. The notes
argue that a lost update cannot cause a bind overlap. Does that argument
also cover the other bits in this byte?

>  	if (!(userlocks & SOCK_BINDADDR_LOCK)) {
>  		inet_reset_saddr(sk);

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120174341.35873-1-juanagusnicco%40gmail.com
Technical Details
Message-ID: <179554500253.3135.7129341529911265513@kernel.org>
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370679-linux-kernel-netdev=dewy.ru
References: <20261120174341.35873-3-juanagusnicco@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!srv.newsdeef.eu!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370679-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net 2/4] udp: Keep the bound device on disconnect unless
 connect() set it
Date: Tue, 24 Nov 2026 18:30:02 +0000
Organization: Usenet Network
Message-ID: <179554500253.3135.7129341529911265513@kernel.org>
References: <20261120174341.35873-3-juanagusnicco@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="9741"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:R+rofX00WQN0derSIrZk07kw454= sha256:rTYEwmm90Hin2D33M/PJeSnSF6q2G5V906JdHRDSsN4=
	sha1:1RHZneoUnNFXyRRDq3qNywSe/fE= sha256:PIIC15dGbdSfraCq5jVUzU/0A4uAd2Rkf8XSpI4pjMw=
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 79E7C125E2C; Tue, 24 Nov 2026 21:30:42 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.4
X-Spam-Status: No, score=-3.4 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, URIBL_BLOCKED=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 NxqJre10YemT;
	Tue, 24 Nov 2026 21:30:38 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-370679-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 2A30B125E22; Tue, 24 Nov 2026 21:30:30 +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 9BF3239725; Tue, 24 Nov 2026 18:30:05 +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=1795545005;
	b=O2d6PyJlOMNiqQ98HIt//1lDAYkIno6K9aDqHHvWUa3whoE+kqREvxPOzUQSVy363rfS
	 s7NHdB29EVrsKpv4R4MsFLWOzy67CQ92OerP2TlE4KGZRqmoblLS1k1PE0ZuTkN3eFg02
	 yp9RqbrR+i2zooZu5ZQAX/UwzYckBx0J0iEuC3yQrALsKGN5cMQtwlCSv99kihfkhYzcV
	 oO1YiImyPwuZlrQ9yJX8C26UdMP2iipHPGcg3zsoxU8MOSn7rHfhKS4vl+DULH7w9pFrx
	 7N0Rf+T7LNeWk5ZcEf/JKfkZG5Z9hRUWtT6hx3LhDTBTCkdAuabNJRo5wsjNEtg0uqg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795545005;
	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=DbMNuqLBralp6JqQQXwQMiUu0oCpgkS443CYex4YaM0=;
	b=AhCwOLIOAJSd5LiSgNLg4FqiVyXrPq7dVGluWkELyK/EHflj3j+6UGnRGARQsPTYqZbq
	 JDkKIknHpicRHYP+72VBBgy7jga1FZq1W7o9Fv78WKgPKQQyIB2XtAF2LbYZiSkEs+Qge
	 ygp58jq6CApg66xJdW7JQ1Cn9LBsfe9nid/fLDkkUAGFXrWiKSeEB6IvW7GmxOaGvXc5R
	 Zujr4YMomyiJosRhG7b0e8ZE/O8wQ/HrXr7LRTeOOJoIoM9TI9rlR7Z2hk1m+p9V3IiKE
	 uBaVMIJ0AvmYAH3jFdL0TXO32QZ5picUhVZ73wEsggy691+3myJbg2/s5KHfFjcp2dw==
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=Ll91411+;
	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 748E3606A32C;
	Tue, 24 Nov 2026 18:30:05 +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=Ll91411+
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=1795545004;
	b=W2Gf8tykwhGW0SnmdOMF2FNJcTQ2w9MvTiZfQY3j5zWExnq2rmouG9BNE4YbTjBitAGn
	 tNEl2lURG6X/ev3tQDDHyCrOJgJmXhQ4RxOygOig9T70mFT6SKP11aZcn4JNLh51zl2/z
	 yMnw0o9nIXiFYMIgA19dUu2zvDtO4p3GcA/+nBGb4ZrWqJnqfyq/NuETJdYVtDZskLR+w
	 EdkM94iHXpCbaUUSAqr29f4aEIA8hc2Ewt0ezry5/E0wUMbJOPSdwT/M9a4s1aOYRuA8a
	 01+bq/yDuIeWj497KB3pUou+NQi+lonaThAyvodsw1bguPyw8VYzR8XJKHbHkThzeSg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795545004;
	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=DbMNuqLBralp6JqQQXwQMiUu0oCpgkS443CYex4YaM0=;
	b=1RPA/o+afrRNLgwl6aP9l1iwqS/ql2boAnf4VKhgKDuyJzDp4t3ZxQ1HQYDJHyvuMvKD
	 +sVu7xrl0pHWBv6N3P6WcFbYRSIZQ3wN29AVVirPBkNI7xRtiDqZp9RF/AR8LA9zEymdU
	 srC2uvhDLdxYkGof+XSrhaStCsXSIYmsQ+CM23vMIQArRYOQlZOhnS5uDg8HKn7rZu8u1
	 KOToBvGperos/bjwwg9tIEJLBWkhtCterynUXcR2PY93zf7U8ZtZPagxLMugaqqc/xIy3
	 eqwwVKGTKYuv9Cg/h49q+UYiMJ9ajElbuuDM6wtEWtoj6VWTkT4IAOtslGajj4xl1YQ==
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=Ll91411+;
	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 9078F606A31E;
	Tue, 24 Nov 2026 18:30:04 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 1183B1F000C6;
	Tue, 24 Nov 2026 18:30:03 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1795545003;
	bh=DbMNuqLBralp6JqQQXwQMiUu0oCpgkS443CYex4YaM0=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=Ll91411+oopHN4Rwuq7hcIHToDhESzFrZSScQ7ullShnMnif+HUBeBc27kK2qiRqi
	 GQSVM9rLyznnR4eBu4GtwurJhw+H2C0pG/CF7dHO5OPl0KeFVGug7zwaZKKTS4vnVU
	 D7jdjUSkBMwEfOs3qQiWiWmCW+2JuJn0QHxd5Zn5JDm31+/P0k9MmiHFi4X2/eJkYB
	 zwmkcZLo175i/Hi0AAiEgDsvepjYEsP2fLA+v/78ZMQ6hHdGum6yiOb1gdHROKNjW1
	 iJEek1ihMW54dftJh/3mUYwZsOJ+xhXSNxhtTM31464p+EJsImvS/OydVWxhYCA6Hz
	 idLSSJ+v1kdDg==
X-Cc: davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
 pabeni@redhat.com, horms@kernel.org, kuniyu@google.com,
 willemdebruijn.kernel@gmail.com, willemb@google.com, e@erig.me,
 shuah@kernel.org, linux-kselftest@vger.kernel.org, netdev@vger.kernel.org,
 linux-kernel@vger.kernel.org
X-In-Reply-To: <20261120174341.35873-3-juanagusnicco@gmail.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