Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: dmnk@google.com
Cc: davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
	pabeni@redhat.com, dsahern@kernel.org, horms@kernel.org,
	willemdebruijn.kernel@gmail.com, kuniyu@google.com,
	netdev@vger.kernel.org, dmnk+artist@google.com
Subject: Re: [PATCH net v3] udp: preserve explicit sk_bound_dev_if across __udp_disconnect()
Date: Tue, 01 Dec 2026 16:56:48 +0000	[thread overview]
Message-ID: <179614420812.3146.6814639156432707669@kernel.org> (raw)
In-Reply-To: <20261130160430.2657088-1-dmnk@google.com>

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

      reply	other threads:[~2026-12-01 16:56 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 message]

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=179614420812.3146.6814639156432707669@kernel.org \
    --to=netdev-bot+sashiko@kernel.org \
    --cc=davem@davemloft.net \
    --cc=dmnk+artist@google.com \
    --cc=dmnk@google.com \
    --cc=dsahern@kernel.org \
    --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