Netdev List
 help / color / mirror / Atom feed
From: netdev-bot+sashiko@kernel.org
To: dmnk@google.com
Cc: willemdebruijn.kernel@gmail.com, edumazet@kernel.org,
	kuniyu@google.com, pabeni@redhat.com, davem@davemloft.net,
	kuba@kernel.org, horms@kernel.org, dsahern@kernel.org,
	netdev@vger.kernel.org, dmnk+artist@google.com
Subject: Re: [PATCH net v2] udp: preserve explicit sk_bound_dev_if across __udp_disconnect()
Date: Sat, 28 Nov 2026 19:03:09 +0000	[thread overview]
Message-ID: <179589258952.3135.7000335148299611948@kernel.org> (raw)
In-Reply-To: <20261124180603.72523-1-dmnk@google.com>

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

      reply	other threads:[~2026-11-28 19:03 UTC|newest]

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