Netdev List
 help / color / mirror / Atom feed
From: Juan Agustin Niccolini <juanagusnicco@gmail.com>
To: davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com
Cc: horms@kernel.org, kuniyu@google.com, willemb@google.com,
	willemdebruijn.kernel@gmail.com, dsahern@kernel.org,
	idosch@nvidia.com, shuah@kernel.org, tpluszz77@gmail.com,
	brauner@kernel.org, alice.kernel@fastmail.im, kylebot@openai.com,
	joe@dama.to, leitao@debian.org, xudu@redhat.com,
	netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH net v2 1/3] udp: Keep the bound device on disconnect unless connect() set it
Date: Mon, 30 Nov 2026 00:07:27 -0500	[thread overview]
Message-ID: <20261130050729.893-2-juanagusnicco@gmail.com> (raw)
In-Reply-To: <20261130050729.893-1-juanagusnicco@gmail.com>

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

Disconnect now writes sk_userlocks, which ping, raw and L2TP sockets
reach without the socket lock. Take it in their IPv6 disconnect
callbacks, as udp_disconnect() does. IPv4 sockets never get the record.

Note that this changes what disconnect does in two cases, for UDP, ping,
raw and L2TP sockets, which all use __udp_disconnect(). A program that
relied on disconnect to clear a device it set itself now finds the
device still there, and needs CAP_NET_RAW to change it, as on a socket
that never connected. And a VRF socket that connect() moved from the
master to one of its interfaces now keeps the interface after
disconnect, where it used to leave the VRF. Putting the master back is
not safe, because another socket may have bound the same port on the
master in the meantime.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
v2:
- Take the socket lock on IPv6 ping, raw and L2TP disconnect (Willem).
- Move the VRF note into the commit message (Willem).
- v1: https://lore.kernel.org/netdev/20261120174341.35873-3-juanagusnicco@gmail.com/

 include/net/ipv6.h  |  1 +
 include/net/sock.h  |  4 ++++
 net/core/sock.c     |  1 +
 net/ipv4/udp.c      | 13 +++++++++----
 net/ipv6/datagram.c | 27 +++++++++++++++++++++++++--
 net/ipv6/ping.c     |  2 +-
 net/ipv6/raw.c      |  2 +-
 net/l2tp/l2tp_ip6.c |  2 +-
 8 files changed, 43 insertions(+), 9 deletions(-)

diff --git a/include/net/ipv6.h b/include/net/ipv6.h
index 3de07e738538..b55b4dd73593 100644
--- a/include/net/ipv6.h
+++ b/include/net/ipv6.h
@@ -1149,6 +1149,7 @@ int ipv6_getsockopt(struct sock *sk, int level, int optname,
 int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *addr,
 			   int addr_len);
 int ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *addr, int addr_len);
+int ip6_datagram_disconnect(struct sock *sk, int flags);
 int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr_unsized *addr,
 				 int addr_len);
 int ip6_datagram_dst_update(struct sock *sk, bool fix_sk_saddr);
diff --git a/include/net/sock.h b/include/net/sock.h
index 3144efad8c36..478c6366d054 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 5675053625e6..8d5ed878fab7 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 d784faf939c3..ff4f4845f2d0 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -2196,6 +2196,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.
@@ -2205,15 +2206,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..9fbb78aa9d68 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -29,6 +29,7 @@
 #include <net/tcp_states.h>
 #include <net/dsfield.h>
 #include <net/sock_reuseport.h>
+#include <net/udp.h>
 
 #include <linux/errqueue.h>
 #include <linux/uaccess.h>
@@ -139,6 +140,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 +238,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 +276,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;
 	}
 
@@ -287,6 +301,15 @@ int ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int ad
 }
 EXPORT_SYMBOL_GPL(ip6_datagram_connect);
 
+int ip6_datagram_disconnect(struct sock *sk, int flags)
+{
+	lock_sock(sk);
+	__udp_disconnect(sk, flags);
+	release_sock(sk);
+	return 0;
+}
+EXPORT_SYMBOL_GPL(ip6_datagram_disconnect);
+
 int ip6_datagram_connect_v6_only(struct sock *sk, struct sockaddr_unsized *uaddr,
 				 int addr_len)
 {
diff --git a/net/ipv6/ping.c b/net/ipv6/ping.c
index 6e90d0bf9f3d..ac61d809de61 100644
--- a/net/ipv6/ping.c
+++ b/net/ipv6/ping.c
@@ -200,7 +200,7 @@ struct proto pingv6_prot = {
 	.close =	ping_close,
 	.pre_connect =	ping_v6_pre_connect,
 	.connect =	ip6_datagram_connect_v6_only,
-	.disconnect =	__udp_disconnect,
+	.disconnect =	ip6_datagram_disconnect,
 	.setsockopt =	ipv6_setsockopt,
 	.getsockopt =	ipv6_getsockopt,
 	.sendmsg =	ping_v6_sendmsg,
diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c
index 00d832c7cdae..10cb28d864d5 100644
--- a/net/ipv6/raw.c
+++ b/net/ipv6/raw.c
@@ -1214,7 +1214,7 @@ struct proto rawv6_prot = {
 	.close		   = rawv6_close,
 	.destroy	   = raw6_destroy,
 	.connect	   = ip6_datagram_connect_v6_only,
-	.disconnect	   = __udp_disconnect,
+	.disconnect	   = ip6_datagram_disconnect,
 	.ioctl		   = rawv6_ioctl,
 	.init		   = rawv6_init_sk,
 	.setsockopt	   = rawv6_setsockopt,
diff --git a/net/l2tp/l2tp_ip6.c b/net/l2tp/l2tp_ip6.c
index bdaae1b64d25..36f79b098113 100644
--- a/net/l2tp/l2tp_ip6.c
+++ b/net/l2tp/l2tp_ip6.c
@@ -441,7 +441,7 @@ static int l2tp_ip6_disconnect(struct sock *sk, int flags)
 	if (sock_flag(sk, SOCK_ZAPPED))
 		return 0;
 
-	return __udp_disconnect(sk, flags);
+	return ip6_datagram_disconnect(sk, flags);
 }
 
 static int l2tp_ip6_getname(struct socket *sock, struct sockaddr *uaddr,
-- 
2.43.0


  reply	other threads:[~2026-11-30  5:07 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  5:07 [PATCH net v2 0/3] udp: Fix bind conflict check bypasses Juan Agustin Niccolini
2026-11-30  5:07 ` Juan Agustin Niccolini [this message]
2026-11-30  5:07 ` [PATCH net v2 2/3] udp: Ignore a connect()-set device in the bind conflict check Juan Agustin Niccolini
2026-11-30  5:07 ` [PATCH net v2 3/3] selftests: net: Add UDP bind conflict tests Juan Agustin Niccolini

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=20261130050729.893-2-juanagusnicco@gmail.com \
    --to=juanagusnicco@gmail.com \
    --cc=alice.kernel@fastmail.im \
    --cc=brauner@kernel.org \
    --cc=davem@davemloft.net \
    --cc=dsahern@kernel.org \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=idosch@nvidia.com \
    --cc=joe@dama.to \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=kylebot@openai.com \
    --cc=leitao@debian.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=shuah@kernel.org \
    --cc=tpluszz77@gmail.com \
    --cc=willemb@google.com \
    --cc=willemdebruijn.kernel@gmail.com \
    --cc=xudu@redhat.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