Netdev List
 help / color / mirror / Atom feed
* [PATCH net v3] udp: preserve explicit sk_bound_dev_if across __udp_disconnect()
@ 2026-11-30 16:04 Dominik Maier
  2026-12-01 16:56 ` netdev-bot+sashiko
  0 siblings, 1 reply; 2+ messages 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] 2+ messages in thread

* Re: [PATCH net v3] udp: preserve explicit sk_bound_dev_if across __udp_disconnect()
  2026-11-30 16:04 [PATCH net v3] udp: preserve explicit sk_bound_dev_if across __udp_disconnect() Dominik Maier
@ 2026-12-01 16:56 ` netdev-bot+sashiko
  0 siblings, 0 replies; 2+ messages in thread
From: netdev-bot+sashiko @ 2026-12-01 16:56 UTC (permalink / raw)
  To: dmnk
  Cc: davem, edumazet, kuba, pabeni, dsahern, horms,
	willemdebruijn.kernel, kuniyu, netdev, dmnk+artist

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

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

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).

- [High] On IPv6 datagram sockets, an unprivileged user can still change
  an explicit device binding without CAP_NET_RAW.
- [Medium] ping: ping_bind() fails to clear connect ifindex when
  scoped_ifindex == dif
- [Low] The ping_bind() rework in net/ipv4/ping.c also fixes a second,
  independent CAP_NET_RAW bypass and changes a user-visible error, and…
- [Low] The patch adds two CONFIG_IPV6-only fields to struct inet_sock in
  include/net/inet_sock.h: `bool bound_dev_if_connect` after…

--

udp: preserve explicit sk_bound_dev_if across __udp_disconnect()

With this commit, __udp_disconnect() no longer always zeroes
sk_bound_dev_if. When an IPv6 connect() sets an implicit scoped interface,
the previous ifindex is recorded and then restored on disconnect. That
state is cleared on SO_BINDTODEVICE and scoped binds, and the socket lock
is now taken in the rawv6, pingv6 and l2tp_ip6 disconnect and bind paths.

> diff --git a/include/net/inet_sock.h b/include/net/inet_sock.h
> index 24cbabc245823..069b1586a10ad 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

[Severity: Low]
This isn't a bug, but Documentation/networking/net_cachelines/inet_sock.rst
lists the inet_sock fields and wasn't updated for bound_dev_if_connect and
bound_dev_if_orig. It still goes from convert_csum straight to uc_index and
ends at local_port_range.

Should entries for the two new fields be added there? The commit message
also makes a padding/layout claim that this file tracks.

[ ... ]

> diff --git a/net/ipv4/ping.c b/net/ipv4/ping.c
> index 223a0108b74cc..75416ab449ac2 100644
> --- a/net/ipv4/ping.c
> +++ b/net/ipv4/ping.c

[ ... ]

> @@ -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;
> +

[Severity: Medium]
Why is the clear here gated on scoped_ifindex != dif? Both
__inet6_bind() and rawv6_bind() call inet6_clear_connect_ifindex()
whenever the user supplies a scope id. ping_bind() skips it when the
scope id matches the current sk_bound_dev_if.

That matters when sk_bound_dev_if already equals the scope id because
of an earlier scoped connect(), so bound_dev_if_connect is still true.
This state is reachable:

- ping_bind() only rejects inet_num != 0 and never checks sk_state.
- inet_dgram_connect() drops the socket lock between inet_autobind()
  and prot->connect().
- A concurrent connect(AF_UNSPEC) in that window goes through
  udp_locked_disconnect() -> __udp_disconnect() -> ping_unhash(),
  which resets inet_num to 0.

The original connect then carries on:

    connect(fe80::y%X)
      inet_autobind()            inet_num = N, lock dropped
                                 connect(AF_UNSPEC): inet_num = 0
      __ip6_datagram_connect()   sk_bound_dev_if = X,
                                 bound_dev_if_orig = 0,
                                 bound_dev_if_connect = true
    bind(fe80::z%X)
      inet_num == 0, scoped_ifindex == dif == X
      inet6_clear_connect_ifindex() skipped
    connect(AF_UNSPEC)
      __udp_disconnect(): sk_bound_dev_if = bound_dev_if_orig (0)

After the last step the socket still has its link-local source address
under SOCK_BINDADDR_LOCK, but it has no interface binding. The user
explicitly bound it to X. This is the same silent loss of a binding
that this patch sets out to fix, and a ping_group_range user can
reach it without CAP_NET_RAW.

Should the condition simply be if (scoped_ifindex), matching
__inet6_bind() and rawv6_bind()? Or should ping_bind() also refuse a
socket that isn't in TCP_CLOSE, the way __inet6_bind() does?

[Severity: Low]
On this part of ping_bind(), the commit message body only says "hold the
socket lock across rawv6, pingv6, and l2tp_ip6 updates". The reordering
also changes behaviour in two ways that the body doesn't describe.

First, it closes a separate leak. Before this patch,
ping_check_bind_addr() wrote sk->sk_bound_dev_if = addr->sin6_scope_id
before lock_sock() and before the inet_num double-bind check. The -EINVAL
double-bind path never restored dif. So bind(fe80::x%B) on an
already-bound pingv6 socket left sk_bound_dev_if set to B and then
returned -EINVAL. That overrode an explicit SO_BINDTODEVICE without a
disconnect.

Second, it changes which error comes first. A second bind() on a bound
ping socket now returns -EINVAL before any address validation. Before, it
could return -EAFNOSUPPORT, -EINVAL, -ENODEV or -EADDRNOTAVAIL.

Could the commit message describe both of these? Right now the reasoning
is only in the v3 changelog below the --- line, which is dropped when the
patch is applied.

[ ... ]

> diff --git a/net/ipv6/af_inet6.c b/net/ipv6/af_inet6.c
> index b85125c3f14bd..7e05ec0b12075 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: High]
The commit message assumes that once a socket has an explicit device
binding, only CAP_NET_RAW can clear it. Does that hold for IPv6 datagram
sockets?

Here __inet6_bind() replaces sk_bound_dev_if with the user's
sin6_scope_id. There is no ns_capable(CAP_NET_RAW) or
sk_dev_equal_l3scope() check. The new value also stays in place if bind()
later fails with -ENODEV, -EADDRNOTAVAIL or -EADDRINUSE.

The same unchecked override is in rawv6_bind(), in ping_bind() through
scoped_ifindex, and in l2tp_ip6_bind(). At each of these assignments the
patch adds inet6_clear_connect_ifindex() but leaves the override itself
ungated.

__ip6_datagram_connect(), by contrast, rejects a scope id outside the
socket's L3 scope:

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

This works directly on a fresh device-bound socket (TCP_CLOSE,
inet_num == 0). For an autobound socket that is already in use, the
disconnect path changed here reopens bind(). SOCK_BINDPORT_LOCK is clear
in that case, so:

    __udp_disconnect()
      sk->sk_prot->unhash(sk)
        udp_lib_unhash()
          inet_sk(sk)->inet_num = 0;

So for a socket bound to device A through SO_BINDTODEVICE or ip vrf exec:

    bind(fe80::<addr on B>%B, 0)   /* or any fe80:: with IPV6_FREEBIND */
      sk_bound_dev_if: A -> B
    connect(AF_UNSPEC)
      unhash, inet_num = 0
    bind(::, 0)
      socket now bound to B with a wildcard address

Doesn't this still let an unprivileged user leave the device or VRF
binding that c427bfec18f2 restricts to CAP_NET_RAW? pingv6 (for
ping_group_range users) and l2tp_ip6 sockets behave the same way.

Should these scoped bind overrides get the same CAP_NET_RAW or l3scope
check? If not, should the commit message state the limitation? At the
moment only the v3 notes below the --- line say scoped bind behaviour is
unchanged.

[ ... ]

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

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

end of thread, other threads:[~2026-12-01 16:56 UTC | newest]

Thread overview: 2+ messages (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
2026-12-01 16:56 ` 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