Netdev List
 help / color / mirror / Atom feed
From: Dominik Maier <dmnk@google.com>
To: Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
	Eric Dumazet <edumazet@kernel.org>,
	 Kuniyuki Iwashima <kuniyu@google.com>,
	Paolo Abeni <pabeni@redhat.com>,
	 "David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>
Cc: Simon Horman <horms@kernel.org>, David Ahern <dsahern@gmail.com>,
	netdev@vger.kernel.org,  dmnk+artist@google.com,
	Dominik Maier <dmnk@google.com>
Subject: [PATCH net] udp: preserve explicit sk_bound_dev_if across __udp_disconnect()
Date: Thu, 19 Nov 2026 20:21:38 +0100	[thread overview]
Message-ID: <20261119192138.2635070-1-dmnk@google.com> (raw)

sock_bindtoindex_locked() allows unprivileged users to bind an unbound
socket to a network interface via SO_BINDTODEVICE or SO_BINDTOIFINDEX,
and enforces CAP_NET_RAW before unbinding or changing sk_bound_dev_if
once set. Sockets may also have sk_bound_dev_if set via a scoped bind()
address or via cgroup BPF (such as ip vrf exec).

However, __udp_disconnect() unconditionally zeroes sk->sk_bound_dev_if
when disconnecting via AF_UNSPEC, which is only intended to undo
implicit scoped-address bindings assigned at connect() time by
__ip6_datagram_connect(). When a socket was also explicitly bound to an
address and port (SOCK_BINDADDR_LOCK | SOCK_BINDPORT_LOCK), it remains
hashed at the head of its UDP portaddr hash chain with sk_bound_dev_if
reset to 0. An unprivileged local user can therefore bind to a disjoint
interface index alongside another user's exclusive SO_BINDTODEVICE or
scoped UDP listener, disconnect via AF_UNSPEC, and intercept cross-UID
UDP packets on all interfaces (or clear a BPF-assigned VRF binding).

Add SOCK_CONNECT_IFINDEX to sk->sk_userlocks (matching
SOCK_CONNECT_BIND), set it in __ip6_datagram_connect() only when
sk->sk_bound_dev_if transitions from 0 to non-zero (clearing it again if
ip6_datagram_dst_update() fails), clear it when an explicit interface or
scoped bind overrides sk_bound_dev_if, and only reset
sk->sk_bound_dev_if in __udp_disconnect() when SOCK_CONNECT_IFINDEX is
set.

This issue was discovered with an AI-assisted code review tool (ARTIST)
and reproduced in QEMU/KVM on an unmodified v7.4-rc3 kernel without
kernel modifications or error injection; a standalone reproducer is
available on request. This revised SOCK_CONNECT_IFINDEX patch was
compile-tested with -Werror and checked with checkpatch.pl --strict; it
has not yet been runtime-tested against the reproducer or kernel
selftests.

Fixes: c427bfec18f2 ("net: core: enable SO_BINDTODEVICE for non-root users")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
---
 include/net/sock.h  | 4 ++++
 net/core/sock.c     | 1 +
 net/ipv4/udp.c      | 5 ++++-
 net/ipv6/af_inet6.c | 1 +
 net/ipv6/datagram.c | 4 ++++
 net/ipv6/raw.c      | 1 +
 6 files changed, 15 insertions(+), 1 deletion(-)

diff --git a/include/net/sock.h b/include/net/sock.h
index 3144efa..5700807 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_IFINDEX - &sock->sk_userlocks flag for scoped connect() ifindex
+ */
+#define SOCK_CONNECT_IFINDEX	32
 
 struct socket_alloc {
 	struct socket socket;
diff --git a/net/core/sock.c b/net/core/sock.c
index ad41487..211b7f1 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);
+	sk->sk_userlocks &= ~SOCK_CONNECT_IFINDEX;
 
 	if (sk->sk_prot->rehash)
 		sk->sk_prot->rehash(sk);
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 2e5562f..4943afe 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -2190,7 +2190,10 @@ 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_CONNECT_IFINDEX) {
+		WRITE_ONCE(sk->sk_bound_dev_if, 0);
+		sk->sk_userlocks &= ~SOCK_CONNECT_IFINDEX;
+	}
 	if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) {
 		inet_reset_saddr(sk);
 		if (sk->sk_prot->rehash &&
diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
index a9b8b4b..b402f0e 100644
--- a/net/ipv6/af_inet6.c
+++ b/net/ipv6/af_inet6.c
@@ -341,6 +341,7 @@ int __inet6_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len,
 					 * is supplied by user.
 					 */
 					sk->sk_bound_dev_if = addr->sin6_scope_id;
+					sk->sk_userlocks &= ~SOCK_CONNECT_IFINDEX;
 				}
 
 				/* Binding to link-local address requires an interface */
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index ff7a7ff..36581c6 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -240,6 +240,8 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
 			err = -EINVAL;
 			goto out;
 		}
+		if (!old_bound_dev_if)
+			sk->sk_userlocks |= SOCK_CONNECT_IFINDEX;
 	}
 
 	/* save the current peer information before updating it */
@@ -264,6 +266,8 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
 		sk->sk_v6_daddr = old_daddr;
 		np->flow_label = old_fl6_flowlabel;
 		inet->inet_dport = old_dport;
+		if (!old_bound_dev_if)
+			sk->sk_userlocks &= ~SOCK_CONNECT_IFINDEX;
 		WRITE_ONCE(sk->sk_bound_dev_if, old_bound_dev_if);
 		goto out;
 	}
diff --git a/net/ipv6/raw.c b/net/ipv6/raw.c
index 00d832c..643f732 100644
--- a/net/ipv6/raw.c
+++ b/net/ipv6/raw.c
@@ -255,6 +255,7 @@ static int rawv6_bind(struct sock *sk, struct sockaddr_unsized *uaddr,
 				 * one is supplied by user.
 				 */
 				sk->sk_bound_dev_if = addr->sin6_scope_id;
+				sk->sk_userlocks &= ~SOCK_CONNECT_IFINDEX;
 			}
 
 			/* Binding to link-local address requires an interface */
-- 
2.56.1.750.gf70dc682cf-goog


                 reply	other threads:[~2026-11-19 19:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261119192138.2635070-1-dmnk@google.com \
    --to=dmnk@google.com \
    --cc=davem@davemloft.net \
    --cc=dmnk+artist@google.com \
    --cc=dsahern@gmail.com \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=willemdebruijn.kernel@gmail.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