🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Thread View: linux.kernel.netdev
3 messages
3 total messages Started by Juan Agustin Niccolini Fri, 20 Nov 2026 17:43
[PATCH net 2/4] udp: Keep the bound device on disconnect unless connect() set it
#19908
Author: Juan Agustin Niccolini
Date: Fri, 20 Nov 2026 17:43
165 lines
17224 bytes
__udp_disconnect() clears sk_bound_dev_if unconditionally, without a
bind conflict check.

This is only safe for a device that connect() set, which
__ip6_datagram_connect() does for a link-local or multicast peer. The
device can also come from:

- The user, with SO_BINDTODEVICE, SO_BINDTOIFINDEX or a bind() to a
  scoped IPv6 address.
- A cgroup program, as with ip vrf exec.

The bind conflict checks treat sockets bound to different devices as
not overlapping. As a result, an unprivileged user can bind an address
and port that another user's socket holds on another device, clear its
own device, and receive the other socket's datagrams:

  victim:   SO_BINDTODEVICE(lo), bind(0.0.0.0:P)
  attacker: SO_BINDTODEVICE(eth0), bind(127.0.0.1:P)
  attacker: connect(AF_UNSPEC)            /* clears the device */

Record in sk_userlocks that connect() set the device of a socket that
had none, and only clear the device on disconnect in that case.
SO_BINDTODEVICE and SO_BINDTOIFINDEX drop the record.

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.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
Notes:
- Within a VRF, connect() can replace the master with one of its
  interfaces, which is now kept on disconnect, so binding back to the
  master needs CAP_NET_RAW. Before, disconnect took the socket out of
  its VRF. Restoring the master is not safe: the bind conflict checks
  treat a master and its interfaces as different devices, so another
  socket may hold the master by then. Fixing that is a separate,
  VRF-wide change.
- Ping, raw and L2TP sockets call __udp_disconnect() without the socket
  lock, so the sk_userlocks update is unlocked there. That predates
  this change: commit 286c72deabaa ("udp: must lock the socket in
  udp_disconnect()") only added the lock for UDP. Fixing it means
  changing how ping, raw and L2TP handle disconnect, which is separate
  from this fix, and a lost update cannot cause a bind overlap anyway.

 include/net/sock.h  |  4 ++++
 net/core/sock.c     |  1 +
 net/ipv4/udp.c      | 13 +++++++++----
 net/ipv6/datagram.c | 17 +++++++++++++++--
 4 files changed, 29 insertions(+), 6 deletions(-)

diff --git a/include/net/sock.h b/include/net/sock.h
index 173b37b2b8bb..78fc65adbcb8 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1534,6 +1534,10 @@ static inline int __sk_prot_rehash(struct sock *sk)
  * define SOCK_CONNECT_BIND - &sock->sk_userlocks flag for auto-bind at connect() time
  */
 #define SOCK_CONNECT_BIND	16
+/**
+ * define SOCK_CONNECT_DEV - &sock->sk_userlocks flag for a connect()-set device
+ */
+#define SOCK_CONNECT_DEV	32
 
 struct socket_alloc {
 	struct socket socket;
diff --git a/net/core/sock.c b/net/core/sock.c
index e2401d735c5a..5ce4aefc514d 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -650,6 +650,7 @@ static int sock_bindtoindex_locked(struct sock *sk, int ifindex)
 
 	/* Paired with all READ_ONCE() done locklessly. */
 	WRITE_ONCE(sk->sk_bound_dev_if, ifindex);
+	WRITE_ONCE(sk->sk_userlocks, sk->sk_userlocks & ~SOCK_CONNECT_DEV);
 
 	if (sk->sk_prot->rehash)
 		sk->sk_prot->rehash(sk);
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 446729b41d66..9f1f45afee53 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);
 	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);
+		WRITE_ONCE(sk->sk_userlocks,
+			   sk->sk_userlocks & ~SOCK_CONNECT_DEV);
+	}
+	if (!(userlocks & SOCK_BINDADDR_LOCK)) {
 		inet_reset_saddr(sk);
 		if (sk->sk_prot->rehash &&
-		    (sk->sk_userlocks & SOCK_BINDPORT_LOCK))
+		    (userlocks & SOCK_BINDPORT_LOCK))
 			sk->sk_prot->rehash(sk);
 	}
 
-	if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
+	if (!(userlocks & SOCK_BINDPORT_LOCK)) {
 		sk->sk_prot->unhash(sk);
 		inet->inet_sport = 0;
 	}
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index ff7a7ffbf983..0296207dbeb0 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -139,6 +139,14 @@ void ip6_datagram_release_cb(struct sock *sk)
 }
 EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
 
+static void ip6_datagram_connect_dev(struct sock *sk, int old_dif, int dif)
+{
+	if (!old_dif && dif)
+		WRITE_ONCE(sk->sk_userlocks,
+			   sk->sk_userlocks | SOCK_CONNECT_DEV);
+	WRITE_ONCE(sk->sk_bound_dev_if, dif);
+}
+
 int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
 			   int addr_len)
 {
@@ -229,11 +237,13 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
 				err = -EINVAL;
 				goto out;
 			}
-			WRITE_ONCE(sk->sk_bound_dev_if, usin->sin6_scope_id);
+			ip6_datagram_connect_dev(sk, old_bound_dev_if,
+						 usin->sin6_scope_id);
 		}
 
 		if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
-			WRITE_ONCE(sk->sk_bound_dev_if, READ_ONCE(np->mcast_oif));
+			ip6_datagram_connect_dev(sk, old_bound_dev_if,
+						 READ_ONCE(np->mcast_oif));
 
 		/* Connect to link-local address requires an interface */
 		if (!sk->sk_bound_dev_if) {
@@ -265,6 +275,9 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
 		np->flow_label = old_fl6_flowlabel;
 		inet->inet_dport = old_dport;
 		WRITE_ONCE(sk->sk_bound_dev_if, old_bound_dev_if);
+		if (!old_bound_dev_if)
+			WRITE_ONCE(sk->sk_userlocks,
+				   sk->sk_userlocks & ~SOCK_CONNECT_DEV);
 		goto out;
 	}
 
-- 
2.43.0
Re: [PATCH net 2/4] udp: Keep the bound device on disconnect unless connect() set it
#22023
Author: 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
Re: [PATCH net 2/4] udp: Keep the bound device on disconnect unless connect() set it
#22052
Author: Juan Agustin Niccolini
Date: Tue, 24 Nov 2026 19:43
8 lines
10643 bytes
This series fixes the universal bugs, where simply binding a specific
way makes a socket vulnerable. It does not fix every instance of the
mistake, which was repeated more than once on every protocol I looked
at. I was already aware of the L2TP bug. Its fix does not belong on this
commit and I don't think it belongs on the series either but I will let
the maintainers decide. Also, the issue is pre-existing, sashiko
misclassified it, as usual.
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads