Netdev List
 help / color / mirror / Atom feed
* [PATCH net v2] udp: preserve explicit sk_bound_dev_if across __udp_disconnect()
@ 2026-11-24 18:06 Dominik Maier
  2026-11-28 19:03 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages 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] 2+ messages in thread

* Re: [PATCH net v2] udp: preserve explicit sk_bound_dev_if across __udp_disconnect()
  2026-11-24 18:06 [PATCH net v2] udp: preserve explicit sk_bound_dev_if across __udp_disconnect() Dominik Maier
@ 2026-11-28 19:03 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-11-28 19:03 UTC (permalink / raw)
  To: dmnk
  Cc: willemdebruijn.kernel, edumazet, kuniyu, pabeni, davem, kuba,
	horms, dsahern, netdev, dmnk+artist

Thank you for your contribution! Sashiko AI review found 2 potential
issue(s) to consider.

Critical: 0 · High: 0 · Medium: 2 · Low: 0

If you believe none of the issues are legitimate, please reply to each
issue of Medium severity or higher and explain why the AI is wrong.
If any of them are legitimate and you plan to respin, please let patchwork
know by including "pw-bot: cr" as a separate line at the end of your reply
(one such reply per series is enough).

- [Medium] Every IPv6 bind() path still lets an unprivileged user change
  sk_bound_dev_if, with no CAP_NET_RAW check and no L3-scope check.
- [Medium] ping_bind() changes sk->sk_bound_dev_if without holding the
  socket lock, and on its -EINVAL path it never restores the old value.

--

udp: preserve explicit sk_bound_dev_if across __udp_disconnect()

This patch stops __udp_disconnect() from always zeroing sk_bound_dev_if.
When __ip6_datagram_connect() changed the binding for a scoped peer, the
old value is saved in struct inet_sock and restored on disconnect. The
saved state is cleared on SO_BINDTODEVICE and scoped bind(), and the
rawv6, pingv6 and l2tp_ip6 disconnect callbacks now take lock_sock().

> diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
> index 223a0108b74cc..a160c24323d9f 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

[Severity: Medium]
The commit message says that holding lock_sock() in the pingv6
disconnect callback serializes with connect(), bind() and setsockopt().
Is that true for ping_bind()?

ping_bind() reads dif and writes sk_bound_dev_if before it takes
lock_sock():

net/ipv4/ping.c:ping_bind() {
	int dif = sk->sk_bound_dev_if;

	err = ping_check_bind_addr(sk, isk, uaddr, addr_len);
	if (err)
		return err;

	lock_sock(sk);
    ...
}

For scoped addresses, ping_check_bind_addr() does a plain store:

	if (scoped)
		sk->sk_bound_dev_if = addr->sin6_scope_id;

Could this store land after __ip6_datagram_connect() samples
old_bound_dev_if but before its rollback? Could it also land while
udp_locked_disconnect() is restoring bound_dev_if_orig?

The error path has a separate problem. A pingv6 socket that is already
bound, or that connect() autobound through inet_dgram_connect(), has
inet_num != 0. A second bind() to fe80::<own LL>%Y writes Y and then
takes this branch:

	err = -EINVAL;
	if (isk->inet_num != 0)
		goto out;

Only the ping_get_port() failure path restores dif. Here bind() returns
-EINVAL, but sk_bound_dev_if stays at Y on the hashed socket.

Could an unprivileged ping_group_range user use this to leave an
SO_BINDTODEVICE or VRF binding without CAP_NET_RAW? The new
inet6_clear_connect_ifindex() call runs only on the success path. On
this path, bound_dev_if_connect and bound_dev_if_orig are not updated
to match the new ifindex.

> diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
> index a9b8b4b42a1af..3724129b00e7d 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 */

[Severity: Medium]
The commit message says that after SO_BINDTODEVICE the kernel "requires
CAP_NET_RAW to clear or change sk_bound_dev_if afterwards". It also says
the patch closes the case where a user can "clear a VRF binding without
CAP_NET_RAW". Does this bind() path enforce that rule?

__inet6_bind() overrides sk_bound_dev_if for any scoped address with a
non-zero sin6_scope_id. After that it only checks that the device exists
and calls ipv6_chk_addr(). The ipv6_chk_addr() check is skipped when
ipv6_can_nonlocal_bind() is true, for example with IPV6_FREEBIND. There
is no CAP_NET_RAW check and no L3 scope check.

ping_check_bind_addr() and rawv6_bind() have the same unchecked
override.

By contrast, __ip6_datagram_connect() rejects a scope_id that is outside
the socket's L3 domain:

	if (!sk_dev_equal_l3scope(sk, usin->sin6_scope_id)) {
		err = -EINVAL;
		goto out;
	}

Take a UDPv6 or pingv6 socket created under ip vrf exec, or one bound
with an unprivileged SO_BINDTODEVICE. Could it call bind() on
fe80::<own LL>%Y, with Y outside the VRF, and leave its device binding
without CAP_NET_RAW and without any disconnect?

For UDPv6, if get_port() fails after the override, __inet6_bind() only
calls inet_reset_saddr(). sk_bound_dev_if stays at Y and inet_num stays
0, so the bind() can be retried.

This path can't produce ifindex 0, so the port squatting case in the
commit message is still fixed. The override itself is long-standing
("Override any existing binding, if another one is supplied by user").
The patch adds inet6_clear_connect_ifindex() next to it, which treats
the override as a valid explicit binding.

Should these bind() paths get an sk_dev_equal_l3scope() or capability
check? Or should the commit message be narrowed to the disconnect case?

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261124180603.72523-1-dmnk%40google.com

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2026-11-28 19:03 UTC | newest]

Thread overview: 2+ messages (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
2026-11-28 19:03 ` netdev-bot+sashiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox