* [PATCH net v3] udp: preserve explicit sk_bound_dev_if across __udp_disconnect()
@ 2026-11-30 16:04 Dominik Maier
0 siblings, 0 replies; only message in thread
From: Dominik Maier @ 2026-11-30 16:04 UTC (permalink / raw)
To: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: David Ahern, Simon Horman, Willem de Bruijn, Kuniyuki Iwashima,
netdev, dmnk+artist
Disconnecting a UDP socket with AF_UNSPEC clears an explicit interface
binding without requiring CAP_NET_RAW.
Unprivileged callers can bind an unbound socket to a device with
SO_BINDTODEVICE, after which clearing it requires CAP_NET_RAW. UDP
disconnect unconditionally zeroes sk_bound_dev_if to undo a temporary
scoped IPv6 connect interface, wiping explicit device, scoped bind, and
cgroup BPF bindings while leaving the socket bound to its port across
all interfaces.
Save the prior device index (in existing 64-bit inet_sock padding) when
an IPv6 connect sets an implicit interface, restore it on disconnect,
reset it on explicit device or scoped binds, and hold the socket lock
across rawv6, pingv6, and l2tp_ip6 updates.
Fixes: c427bfec18f2 ("net: core: enable SO_BINDTODEVICE for non-root users")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
---
v3:
- Hold lock_sock() in ping_bind() before checking/updating sk_bound_dev_if
after isk->inet_num == 0, and restore with WRITE_ONCE() on ping_get_port()
failure so neither concurrent disconnect/connect nor -EINVAL double-bind
can race or leak a modified sk_bound_dev_if (Sashiko).
- Narrow the commit message to AF_UNSPEC disconnect zeroing, noting
pre-existing scoped IPv6 bind() behavior is unchanged (Sashiko).
- v2: https://lore.kernel.org/netdev/20261124180603.72523-1-dmnk@google.com/
- v1: https://lore.kernel.org/netdev/20261119192138.2635070-1-dmnk@google.com/
Found with an AI-assisted code review tool (ARTIST) and verified in
QEMU/KVM across SO_BINDTODEVICE, VRF enslaved link-local connect, and
pingv6/rawv6 bind + disconnect.
include/net/inet_sock.h | 20 ++++++++++++++++++++
include/net/udp.h | 1 +
net/core/sock.c | 1 +
net/ipv4/ping.c | 34 ++++++++++++++++++++++------------
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, 86 insertions(+), 20 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..75416ab449ac 100644
--- a/net/ipv4/ping.c
+++ b/net/ipv4/ping.c
@@ -298,9 +298,10 @@ static int ping_pre_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
return BPF_CGROUP_RUN_PROG_INET4_CONNECT_LOCK(sk, uaddr, &addr_len);
}
-/* Checks the bind address and possibly modifies sk->sk_bound_dev_if. */
+/* Checks the bind address and records any scoped IPv6 ifindex. */
static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
- struct sockaddr_unsized *uaddr, int addr_len)
+ struct sockaddr_unsized *uaddr, int addr_len,
+ int *scoped_ifindex)
{
struct net *net = sock_net(sk);
if (sk->sk_family == AF_INET) {
@@ -378,7 +379,7 @@ static int ping_check_bind_addr(struct sock *sk, struct inet_sock *isk,
return -EADDRNOTAVAIL;
if (scoped)
- sk->sk_bound_dev_if = addr->sin6_scope_id;
+ *scoped_ifindex = addr->sin6_scope_id;
#endif
} else {
return -EAFNOSUPPORT;
@@ -411,13 +412,10 @@ static void ping_set_saddr(struct sock *sk, struct sockaddr_unsized *saddr)
int ping_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
{
struct inet_sock *isk = inet_sk(sk);
+ int scoped_ifindex = 0;
unsigned short snum;
+ int dif;
int err;
- int dif = sk->sk_bound_dev_if;
-
- err = ping_check_bind_addr(sk, isk, uaddr, addr_len);
- if (err)
- return err;
lock_sock(sk);
@@ -425,11 +423,19 @@ int ping_bind(struct sock *sk, struct sockaddr_unsized *uaddr, int addr_len)
if (isk->inet_num != 0)
goto out;
+ dif = sk->sk_bound_dev_if;
+ err = ping_check_bind_addr(sk, isk, uaddr, addr_len, &scoped_ifindex);
+ if (err)
+ goto out;
+
+ if (scoped_ifindex)
+ WRITE_ONCE(sk->sk_bound_dev_if, scoped_ifindex);
+
err = -EADDRINUSE;
snum = ntohs(((struct sockaddr_in *)uaddr)->sin_port);
if (ping_get_port(sk, snum) != 0) {
- /* Restore possibly modified sk->sk_bound_dev_if by ping_check_bind_addr(). */
- sk->sk_bound_dev_if = dif;
+ if (scoped_ifindex)
+ WRITE_ONCE(sk->sk_bound_dev_if, dif);
goto out;
}
ping_set_saddr(sk, uaddr);
@@ -442,8 +448,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 (scoped_ifindex && scoped_ifindex != dif)
+ inet6_clear_connect_ifindex(sk);
+ }
#endif
if (snum)
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index d784faf939c3..0d3fd2f43432 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -2205,7 +2205,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 &&
@@ -2222,6 +2228,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 b85125c3f14b..7e05ec0b1207 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,
base-commit: e2fb9368a41662bc54624f9be87ea8989b109306
--
2.56.1.911.g883d8bcb20-goog
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-30 16:04 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-30 16:04 [PATCH net v3] 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