mirror of https://lore.kernel.org/lkml/
 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 2/3] udp: Ignore a connect()-set device in the bind conflict check
Date: Mon, 30 Nov 2026 00:07:28 -0500	[thread overview]
Message-ID: <20261130050729.893-3-juanagusnicco@gmail.com> (raw)
In-Reply-To: <20261130050729.893-1-juanagusnicco@gmail.com>

The bind conflict checks compare sk_bound_dev_if, which connect() also
writes, for a link-local or multicast IPv6 peer.

This is only safe if the device is the one that bind() reserved. A
socket bound with no device holds its port on every device, but while
it is connected to such a peer it looks bound to one device, so a bind
of the same port on another device is admitted.

As a result, an unprivileged user can bind a port that another user's
socket holds, and receive the datagrams that arrive on its device once
that socket disconnects:

  victim:   bind([::]:P), connect(fe80::1%eth0)
  attacker: SO_BINDTODEVICE(eth1), bind([::]:P)   /* should fail */
  victim:   connect(AF_UNSPEC)

Compare the device that bind() reserved instead: none for a socket
whose device connect() set, which SOCK_CONNECT_DEV records. A VRF
master that connect() narrowed to one of its interfaces does not get
the flag and is compared as before.

Note that the checks read the flag without the other socket's lock, so
it is ordered against the device with barriers: seeing the device
before the flag would trust a connect()-set device again.

Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
v2:
- Read the device of the socket being bound directly, and load the
  other socket's device once and pass it to udp_sk_reuseport_match()
  (Willem).
- v1: https://lore.kernel.org/netdev/20261120174341.35873-4-juanagusnicco@gmail.com/

Notes:
- A connect() that sets the device and is undone again, both between
  the two reads of the flag, can still be seen as a bound device, so a
  conflicting bind can be admitted. Not fixed here because closing it
  needs a sequence count in struct sock, a larger change for a window
  that the other socket's owner has to open within two loads.
- connect() also narrows the local address of a wildcard-bound
  socket, which opens the same window through the address. It is not
  fixed here: connect() moves the socket to the secondary hash slot of
  the narrowed address, so a specific-address bind on a busy port never
  scans it. Closing that needs either a full scan on every bind or no
  rehash on connect, which slows down binds or lookups on busy ports,
  so I leave it for a separate patch.

 net/core/sock.c     |  2 ++
 net/ipv4/udp.c      | 42 ++++++++++++++++++++++++++++++++++--------
 net/ipv6/datagram.c | 12 ++++++++++--
 3 files changed, 46 insertions(+), 10 deletions(-)

diff --git a/net/core/sock.c b/net/core/sock.c
index 8d5ed878fab7..f55249f11da1 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -650,6 +650,8 @@ 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);
+	/* Pairs with the first smp_rmb() in udp_bound_dev_if(). */
+	smp_wmb();
 	WRITE_ONCE(sk->sk_userlocks, sk->sk_userlocks & ~SOCK_CONNECT_DEV);
 
 	if (sk->sk_prot->rehash)
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index ff4f4845f2d0..6807ce5f3e2e 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -131,11 +131,28 @@ EXPORT_PER_CPU_SYMBOL_GPL(udp_memory_per_cpu_fw_alloc);
 #define MAX_UDP_PORTS 65536
 #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN_PERNET)
 
-static bool udp_sk_reuseport_match(struct sock *sk, struct sock *sk2)
+static int udp_bound_dev_if(const struct sock *sk)
+{
+	int dif;
+
+	if (READ_ONCE(sk->sk_userlocks) & SOCK_CONNECT_DEV)
+		return 0;
+	/* Pairs with the smp_wmb() before SOCK_CONNECT_DEV is cleared. */
+	smp_rmb();
+	dif = READ_ONCE(sk->sk_bound_dev_if);
+	/* Pairs with the smp_wmb() after SOCK_CONNECT_DEV is set. */
+	smp_rmb();
+	if (READ_ONCE(sk->sk_userlocks) & SOCK_CONNECT_DEV)
+		return 0;
+	return dif;
+}
+
+static bool udp_sk_reuseport_match(struct sock *sk, struct sock *sk2,
+				   int dif2)
 {
 	return sk2->sk_family == sk->sk_family &&
 	       ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
-	       sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
+	       dif2 == sk->sk_bound_dev_if &&
 	       sk2->sk_reuse == sk->sk_reuse &&
 	       inet_rcv_saddr_equal(sk, sk2, false);
 }
@@ -157,14 +174,18 @@ static int udp_lib_lport_inuse(struct net *net, __u16 num,
 		    sk2 != sk &&
 		    (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
 		    (bitmap || !sk2->sk_reuse || !sk->sk_reuse) &&
-		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
-		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
 		    inet_rcv_saddr_equal(sk, sk2, true)) {
+			int dif2 = udp_bound_dev_if(sk2);
+
+			if (dif2 && sk->sk_bound_dev_if &&
+			    dif2 != sk->sk_bound_dev_if)
+				continue;
 			if (!bitmap) {
 				if (sk2->sk_reuseport && sk->sk_reuseport &&
 				    !rcu_access_pointer(sk->sk_reuseport_cb) &&
 				    uid_eq(uid, sk_uid(sk2))) {
-					if (udp_sk_reuseport_match(sk, sk2))
+					if (udp_sk_reuseport_match(sk, sk2,
+								   dif2))
 						return 0;
 					continue;
 				}
@@ -194,13 +215,16 @@ static int udp_lib_lport_inuse2(struct net *net, __u16 num,
 		    sk2 != sk &&
 		    (udp_sk(sk2)->udp_port_hash == num) &&
 		    (!sk2->sk_reuse || !sk->sk_reuse) &&
-		    (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
-		     sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
 		    inet_rcv_saddr_equal(sk, sk2, true)) {
+			int dif2 = udp_bound_dev_if(sk2);
+
+			if (dif2 && sk->sk_bound_dev_if &&
+			    dif2 != sk->sk_bound_dev_if)
+				continue;
 			if (sk2->sk_reuseport && sk->sk_reuseport &&
 			    !rcu_access_pointer(sk->sk_reuseport_cb) &&
 			    uid_eq(uid, sk_uid(sk2))) {
-				if (udp_sk_reuseport_match(sk, sk2)) {
+				if (udp_sk_reuseport_match(sk, sk2, dif2)) {
 					res = 0;
 					break;
 				}
@@ -2208,6 +2232,8 @@ int __udp_disconnect(struct sock *sk, int flags)
 	sock_rps_reset_rxhash(sk);
 	if (userlocks & SOCK_CONNECT_DEV) {
 		WRITE_ONCE(sk->sk_bound_dev_if, 0);
+		/* Pairs with the first smp_rmb() in udp_bound_dev_if(). */
+		smp_wmb();
 		WRITE_ONCE(sk->sk_userlocks,
 			   sk->sk_userlocks & ~SOCK_CONNECT_DEV);
 	}
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index 9fbb78aa9d68..89870e142835 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -142,9 +142,12 @@ 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)
+	if (!old_dif && dif) {
 		WRITE_ONCE(sk->sk_userlocks,
 			   sk->sk_userlocks | SOCK_CONNECT_DEV);
+		/* Pairs with the second smp_rmb() in udp_bound_dev_if(). */
+		smp_wmb();
+	}
 	WRITE_ONCE(sk->sk_bound_dev_if, dif);
 }
 
@@ -276,9 +279,14 @@ 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)
+		if (!old_bound_dev_if) {
+			/* Pairs with the first smp_rmb() in
+			 * udp_bound_dev_if().
+			 */
+			smp_wmb();
 			WRITE_ONCE(sk->sk_userlocks,
 				   sk->sk_userlocks & ~SOCK_CONNECT_DEV);
+		}
 		goto out;
 	}
 
-- 
2.43.0


  parent 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 ` [PATCH net v2 1/3] udp: Keep the bound device on disconnect unless connect() set it Juan Agustin Niccolini
2026-11-30  5:07 ` Juan Agustin Niccolini [this message]
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-3-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

all inboxes | Powered by JetHome®