* [PATCH net v2] udp: preserve explicit sk_bound_dev_if across __udp_disconnect()
@ 2026-11-24 18:06 Dominik Maier
0 siblings, 0 replies; only message in thread
From: Dominik Maier @ 2026-11-24 18:06 UTC (permalink / raw)
To: Willem de Bruijn, Eric Dumazet, Kuniyuki Iwashima, Paolo Abeni,
David S. Miller, Jakub Kicinski
Cc: Simon Horman, David Ahern, netdev, dmnk+artist
sock_bindtoindex_locked() lets unprivileged users bind an unbound socket
to an interface via SO_BINDTODEVICE or SO_BINDTOIFINDEX, and requires
CAP_NET_RAW to clear or change sk_bound_dev_if afterwards. Sockets can
also get sk_bound_dev_if from a scoped bind() address or cgroup BPF
(such as ip vrf exec).
__udp_disconnect() unconditionally zeroes sk->sk_bound_dev_if on
AF_UNSPEC disconnect, which is only needed to undo an implicit scoped
interface selected at connect() time by __ip6_datagram_connect(). An
unprivileged user can bind a UDP socket to a disjoint interface alongside
another user's SO_BINDTODEVICE listener on the same port, disconnect via
AF_UNSPEC, and stay hashed on that port with sk_bound_dev_if reset to 0
(or clear a VRF binding without CAP_NET_RAW).
Save the pre-connect sk_bound_dev_if in struct inet_sock (using existing
padding holes on 64-bit) whenever __ip6_datagram_connect() updates
sk_bound_dev_if, so __udp_disconnect() restores either 0 (for previously
unbound sockets) or a prior VRF L3 master ifindex (when a VRF-bound
socket connected to an enslaved link-local peer). Clear the saved state
on explicit SO_BINDTODEVICE or scoped bind(), and hold lock_sock() across
rawv6, pingv6, and l2tp_ip6 disconnect callbacks to serialize with
connect(), bind(), and setsockopt().
Fixes: c427bfec18f2 ("net: core: enable SO_BINDTODEVICE for non-root users")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
---
v2:
- Save the prior sk_bound_dev_if in inet_sock instead of using an
sk_userlocks flag, restoring a prior VRF L3 master binding when a
scoped connect() to an enslaved device is disconnected and avoiding
any RMW on sk_userlocks in __udp_disconnect().
- Hold lock_sock() in rawv6, pingv6, and l2tp_ip6 disconnect callbacks,
and also clear saved connect state on ping6 and l2tp_ip6 binds.
- Move the testing note below ---.
- v1: https://lore.kernel.org/netdev/20261119192138.2635070-1-dmnk@google.com/
Found with AI-assisted code review; tested in QEMU across unprivileged
SO_BINDTODEVICE (including non-IPv6 sockets), scoped IPv6 bind and
connect, VRF master with multiple enslaved link-local interfaces, and
concurrent rawv6 disconnect.
include/net/inet_sock.h | 20 ++++++++++++++++++++
include/net/udp.h | 1 +
net/core/sock.c | 1 +
net/ipv4/ping.c | 8 ++++++--
net/ipv4/udp.c | 17 ++++++++++++++++-
net/ipv6/af_inet6.c | 1 +
net/ipv6/datagram.c | 13 ++++++++++++-
net/ipv6/ping.c | 2 +-
net/ipv6/raw.c | 3 ++-
net/l2tp/l2tp_ip6.c | 14 ++++++++++----
10 files changed, 70 insertions(+), 10 deletions(-)
diff --git a/include/net/inet_sock.h b/include/net/inet_sock.h
index 24cbabc24582..069b1586a10a 100644
--- a/include/net/inet_sock.h
+++ b/include/net/inet_sock.h
@@ -241,10 +241,16 @@ struct inet_sock {
__u8 pmtudisc;
__u8 rcv_tos;
__u8 convert_csum;
+#if IS_ENABLED(CONFIG_IPV6)
+ bool bound_dev_if_connect;
+#endif
int uc_index;
int mc_index;
__be32 mc_addr;
u32 local_port_range; /* high << 16 | low */
+#if IS_ENABLED(CONFIG_IPV6)
+ int bound_dev_if_orig;
+#endif
struct ip_mc_socklist __rcu *mc_list;
struct inet_cork_full cork;
@@ -450,4 +456,18 @@ static inline bool inet_addr_valid_or_nonlocal(struct net *net,
addr_type == RTN_BROADCAST;
}
+static inline void inet6_clear_connect_ifindex(struct sock *sk)
+{
+#if IS_ENABLED(CONFIG_IPV6)
+ if (sk->sk_family == AF_INET6 && sk_fullsock(sk) &&
+ sk->sk_prot->ipv6_pinfo_offset &&
+ sk->sk_prot->obj_size >= sizeof(struct inet_sock)) {
+ struct inet_sock *inet = inet_sk(sk);
+
+ inet->bound_dev_if_connect = false;
+ inet->bound_dev_if_orig = 0;
+ }
+#endif
+}
+
#endif /* _INET_SOCK_H */
diff --git a/include/net/udp.h b/include/net/udp.h
index 5bc25dcf25fb..65c59686e347 100644
--- a/include/net/udp.h
+++ b/include/net/udp.h
@@ -424,6 +424,7 @@ int udp_rcv(struct sk_buff *skb);
int udp_ioctl(struct sock *sk, int cmd, int *karg);
int udp_pre_connect(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len);
int __udp_disconnect(struct sock *sk, int flags);
+int udp_locked_disconnect(struct sock *sk, int flags);
int udp_disconnect(struct sock *sk, int flags);
__poll_t udp_poll(struct file *file, struct socket *sock, poll_table *wait);
struct sk_buff *skb_udp_tunnel_segment(struct sk_buff *skb,
diff --git a/net/core/sock.c b/net/core/sock.c
index 5675053625e6..54078b53d973 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);
+ inet6_clear_connect_ifindex(sk);
if (sk->sk_prot->rehash)
sk->sk_prot->rehash(sk);
diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
index 223a0108b74c..a160c24323d9 100644
--- a/net/ipv4/ping.c
+++ b/net/ipv4/ping.c
@@ -442,8 +442,12 @@ int ping_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
if (sk->sk_family == AF_INET && isk->inet_rcv_saddr)
sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
#if IS_ENABLED(CONFIG_IPV6)
- if (sk->sk_family == AF_INET6 && !ipv6_addr_any(&sk->sk_v6_rcv_saddr))
- sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
+ if (sk->sk_family == AF_INET6) {
+ if (!ipv6_addr_any(&sk->sk_v6_rcv_saddr))
+ sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
+ if (sk->sk_bound_dev_if != dif)
+ inet6_clear_connect_ifindex(sk);
+ }
#endif
if (snum)
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 2e5562f353f8..2673ff3b01d9 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -2190,7 +2190,13 @@ 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 IS_ENABLED(CONFIG_IPV6)
+ if (sk->sk_family == AF_INET6 && inet->bound_dev_if_connect) {
+ WRITE_ONCE(sk->sk_bound_dev_if, inet->bound_dev_if_orig);
+ inet->bound_dev_if_orig = 0;
+ inet->bound_dev_if_connect = false;
+ }
+#endif
if (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) {
inet_reset_saddr(sk);
if (sk->sk_prot->rehash &&
@@ -2207,6 +2213,15 @@ int __udp_disconnect(struct sock *sk, int flags)
}
EXPORT_SYMBOL(__udp_disconnect);
+int udp_locked_disconnect(struct sock *sk, int flags)
+{
+ lock_sock(sk);
+ __udp_disconnect(sk, flags);
+ release_sock(sk);
+ return 0;
+}
+EXPORT_SYMBOL(udp_locked_disconnect);
+
/* __udp_disconnect() takes a socket out of the 4-tuple hash table only via
* ->rehash() or ->unhash(), and neither runs for a socket bound to a
* specific address and port. Remove it here, before its peer is cleared.
diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
index a9b8b4b42a1a..3724129b00e7 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;
+ inet6_clear_connect_ifindex(sk);
}
/* Binding to link-local address requires an interface */
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index ff7a7ffbf983..fc29b48b7411 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -149,7 +149,8 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
__be32 fl6_flowlabel = 0;
__be32 old_fl6_flowlabel;
__be16 old_dport;
- int old_bound_dev_if;
+ int old_bound_dev_if, old_orig_if;
+ bool old_connect_if;
int addr_type;
int err;
@@ -221,6 +222,8 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
}
old_bound_dev_if = sk->sk_bound_dev_if;
+ old_connect_if = inet->bound_dev_if_connect;
+ old_orig_if = inet->bound_dev_if_orig;
if (__ipv6_addr_needs_scope_id(addr_type)) {
if (addr_len >= sizeof(struct sockaddr_in6) &&
@@ -240,6 +243,12 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
err = -EINVAL;
goto out;
}
+
+ if (sk->sk_bound_dev_if != old_bound_dev_if &&
+ !inet->bound_dev_if_connect) {
+ inet->bound_dev_if_orig = old_bound_dev_if;
+ inet->bound_dev_if_connect = true;
+ }
}
/* save the current peer information before updating it */
@@ -264,6 +273,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;
+ inet->bound_dev_if_connect = old_connect_if;
+ inet->bound_dev_if_orig = old_orig_if;
WRITE_ONCE(sk->sk_bound_dev_if, old_bound_dev_if);
goto out;
}
diff --git a/net/ipv6/ping.c b/net/ipv6/ping.c
index 6e90d0bf9f3d..481d966d2129 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 = udp_locked_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..71c35f5230c0 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;
+ inet6_clear_connect_ifindex(sk);
}
/* Binding to link-local address requires an interface */
@@ -1214,7 +1215,7 @@ struct proto rawv6_prot = {
.close = rawv6_close,
.destroy = raw6_destroy,
.connect = ip6_datagram_connect_v6_only,
- .disconnect = __udp_disconnect,
+ .disconnect = udp_locked_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..08fb1b6422cc 100644
--- a/net/l2tp/l2tp_ip6.c
+++ b/net/l2tp/l2tp_ip6.c
@@ -362,7 +362,10 @@ static int l2tp_ip6_bind(struct sock *sk, struct sockaddr_unsized *uaddr,
inet->inet_saddr = v4addr;
inet->inet_rcv_saddr = v4addr;
- sk->sk_bound_dev_if = bound_dev_if;
+ if (sk->sk_bound_dev_if != bound_dev_if) {
+ sk->sk_bound_dev_if = bound_dev_if;
+ inet6_clear_connect_ifindex(sk);
+ }
sk->sk_v6_rcv_saddr = addr->l2tp_addr;
np->saddr = addr->l2tp_addr;
@@ -438,10 +441,13 @@ static int l2tp_ip6_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
static int l2tp_ip6_disconnect(struct sock *sk, int flags)
{
- if (sock_flag(sk, SOCK_ZAPPED))
- return 0;
+ int err = 0;
- return __udp_disconnect(sk, flags);
+ lock_sock(sk);
+ if (!sock_flag(sk, SOCK_ZAPPED))
+ err = __udp_disconnect(sk, flags);
+ release_sock(sk);
+ return err;
}
static int l2tp_ip6_getname(struct socket *sock, struct sockaddr *uaddr,
--
2.56.1.831.g720f3f5937-goog
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-24 18:06 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-24 18:06 [PATCH net v2] udp: preserve explicit sk_bound_dev_if across __udp_disconnect() Dominik Maier
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox