From: netdev-bot+sashiko@kernel.org
To: juanagusnicco@gmail.com
Cc: davem@davemloft.net, edumazet@kernel.org, kuba@kernel.org,
pabeni@redhat.com, horms@kernel.org, kuniyu@google.com,
willemdebruijn.kernel@gmail.com, willemb@google.com, e@erig.me,
shuah@kernel.org, linux-kselftest@vger.kernel.org,
netdev@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net 2/4] udp: Keep the bound device on disconnect unless connect() set it
Date: Tue, 24 Nov 2026 18:30:02 +0000 [thread overview]
Message-ID: <179554500253.3135.7129341529911265513@kernel.org> (raw)
In-Reply-To: <20261120174341.35873-3-juanagusnicco@gmail.com>
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 0 · Medium: 1 · Low: 3
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] The commit message says that clearing sk_bound_dev_if on
disconnect "is only safe for a device that connect() set", and…
- [Low] The committed message leaves out or blurs user-visible behaviour
changes of a Cc: stable patch.
- [Low] __udp_disconnect() now does an unlocked read-modify-write of
sk->sk_userlocks: `WRITE_ONCE(sk->sk_userlocks, sk->sk_userlocks &…
- [Low] __udp_disconnect() takes a snapshot (`u8 userlocks =
READ_ONCE(sk->sk_userlocks)`) and later uses it to decide whether to…
--
udp: Keep the bound device on disconnect unless connect() set it
This patch adds a SOCK_CONNECT_DEV flag to sk_userlocks. The flag records
that __ip6_datagram_connect() set the bound device on a socket that had
none, and SO_BINDTODEVICE and SO_BINDTOIFINDEX drop it. __udp_disconnect()
now clears sk_bound_dev_if only when the flag is set, which stops a socket
from clearing a device that the user or a cgroup program set.
> This changes connect(AF_UNSPEC) for UDP, ping, raw and L2TP sockets,
> which all use __udp_disconnect(): a device that connect() did not set
> is now kept, so changing it afterwards needs CAP_NET_RAW, as on a
> socket that never disconnected.
[Severity: Low]
Does this paragraph cover all of the behaviour changes?
In __ip6_datagram_connect(), connect() to a link-local peer can replace
a VRF master M with a slave S. old_dif is non-zero there, so
ip6_datagram_connect_dev() does not set SOCK_CONNECT_DEV. Disconnect now
keeps S, where it used to reset the device to 0, and binding back to M
then needs CAP_NET_RAW.
So a device that connect() did set is now kept, and the sentence above
doesn't cover that. The change is only described in the notes below the
--- line, and those notes won't be in the commit that stable backporters
read.
__udp_disconnect() is also called from raw_abort() in net/ipv4/raw.c and
from udp_abort() (SOCK_DESTROY and bpf_sock_destroy). Those paths now keep
a device set by the user or by a cgroup program as well.
Could the commit message mention the VRF change and the abort paths?
> diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> index 446729b41d661..9f1f45afee532 100644
> --- a/net/ipv4/udp.c
> +++ b/net/ipv4/udp.c
> @@ -2195,6 +2195,7 @@ static int udp_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
>
> int __udp_disconnect(struct sock *sk, int flags)
> {
> + u8 userlocks = READ_ONCE(sk->sk_userlocks);
[Severity: Low]
Can this snapshot be stale by the time it is used to clear
sk_bound_dev_if?
Ping and raw sockets use __udp_disconnect() as their ->disconnect, and
l2tp_ip6_disconnect() calls it. All of them get there from
inet_dgram_connect(AF_UNSPEC) without lock_sock(). Only udp_disconnect()
takes the socket lock.
Take an IPv6 ping, raw or L2TP socket connected to a link-local peer, so
it has device X and SOCK_CONNECT_DEV set:
thread B: __udp_disconnect()
userlocks = READ_ONCE(sk->sk_userlocks); /* has SOCK_CONNECT_DEV */
thread C: __udp_disconnect() completes
sk_bound_dev_if = 0, SOCK_CONNECT_DEV cleared
thread A: setsockopt(SO_BINDTODEVICE, Y)
sock_bindtoindex_locked()
/* device is 0, so no CAP_NET_RAW check */
sk_bound_dev_if = Y, SOCK_CONNECT_DEV cleared
thread B resumes:
if (userlocks & SOCK_CONNECT_DEV) {
WRITE_ONCE(sk->sk_bound_dev_if, 0);
Disconnect then clears the device that SO_BINDTODEVICE set. For ping, raw
and L2TP this contradicts "a device that connect() did not set is now
kept". The result matches the pre-patch behaviour, and the window is
small.
> struct inet_sock *inet = inet_sk(sk);
> /*
> * 1003.1g - break association.
> @@ -2204,15 +2205,19 @@ 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 (!(sk->sk_userlocks & SOCK_BINDADDR_LOCK)) {
> + if (userlocks & SOCK_CONNECT_DEV) {
> + WRITE_ONCE(sk->sk_bound_dev_if, 0);
[Severity: Medium]
The commit message says clearing the device "is only safe for a device
that connect() set", and this still clears it without a bind conflict
check. Does that hold for L2TP/IPv6?
For UDP, the next patch in the series ("udp: Ignore a connect()-set device
in the bind conflict check") makes udp_lib_lport_inuse() and
udp_lib_lport_inuse2() treat a SOCK_CONNECT_DEV device as 0. That patch
only changes udp.c, and __l2tp_ip6_bind_lookup() still compares the raw
device:
net/l2tp/l2tp_ip6.c:__l2tp_ip6_bind_lookup() {
...
bound_dev_if = READ_ONCE(sk->sk_bound_dev_if);
if (bound_dev_if && dif && bound_dev_if != dif)
continue;
...
}
l2tp_ip6_connect() goes through __ip6_datagram_connect(), and
l2tp_ip6_disconnect() calls __udp_disconnect(). An l2tp_ip6 socket needs
no capability, because inet6_create() checks CAP_NET_RAW only for
SOCK_RAW. The module also loads automatically through
MODULE_ALIAS_NET_PF_PROTO_TYPE().
Could an unprivileged user do the following?
1. attacker: bind conn_id X with no device, which holds X on every device
2. attacker: connect(fe80::1%eth1), so the device becomes eth1 and
SOCK_CONNECT_DEV is set
3. victim: SO_BINDTODEVICE(eth0), bind conn_id X. This succeeds because
the attacker's eth1 is skipped.
4. attacker: connect(AF_UNSPEC), which puts the device back to 0 with no
conflict check
5. attacker: connect() to the victim's real peer G. The peer is not
scoped, so the device stays 0, and l2tp_ip6_connect() moves the socket
to the head of l2tp_ip6_bind_table.
6. l2tp_ip6_recv() takes the first match, so the attacker gets G's L2TP
packets for conn_id X that arrive on eth0
Without this sequence, the victim's bind in step 3 would fail with
EADDRINUSE.
A related point: l2tp_ip6_bind() never sets SOCK_BINDADDR_LOCK. So
__udp_disconnect() always calls inet_reset_saddr() on L2TP sockets, and
the bound address gets wider too.
There is also a VRF case that this patch introduces. Take a socket bound
to VRF master M that connects to a link-local peer on slave S. It gets
device S without SOCK_CONNECT_DEV because old_dif is non-zero, and it now
keeps S after disconnect instead of going back to 0.
Another VRF socket's bind of M:P is then allowed, because S != M in
udp_lib_lport_inuse(). Before this patch, that bind conflicted with the
socket's device 0.
__udp_disconnect() doesn't clear sk_v6_daddr. So for datagrams from its
former peer, the S-bound socket then scores higher than the M-bound one.
Is the series meant to handle the L2TP/IPv6 and VRF cases as well?
> + WRITE_ONCE(sk->sk_userlocks,
> + sk->sk_userlocks & ~SOCK_CONNECT_DEV);
[Severity: Low]
Is this read-modify-write of sk_userlocks safe without the socket lock?
For ping, raw and L2TP sockets this runs from inet_dgram_connect(AF_UNSPEC)
-> prot->disconnect without lock_sock(). Other code updates the same u8
with plain read-modify-writes under the socket lock, and that lock doesn't
exclude this path. Those writers include SO_SNDBUF in sk_setsockopt():
sk->sk_userlocks |= SOCK_SNDBUF_LOCK;
The others are SO_RCVBUF, SO_BUF_LOCK, sock_bindtoindex_locked() and
ip6_datagram_connect_dev().
An update that lands between this load and store is lost or undone. A
SOCK_SNDBUF_LOCK that was just set can vanish. A buffer lock bit that
SO_BUF_LOCK just cleared can come back. A SOCK_CONNECT_DEV set by a
concurrent connect() can be dropped.
The plain sk->sk_userlocks load inside WRITE_ONCE() also races with those
writers, so KCSAN is likely to flag it.
Before this patch, the unlocked path only read sk_userlocks. The notes
argue that a lost update cannot cause a bind overlap. Does that argument
also cover the other bits in this byte?
> if (!(userlocks & SOCK_BINDADDR_LOCK)) {
> inet_reset_saddr(sk);
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120174341.35873-1-juanagusnicco%40gmail.com
next prev parent reply other threads:[~2026-11-24 18:30 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-20 17:43 [PATCH net 0/4] udp: Fix bind conflict check bypasses Juan Agustin Niccolini
2026-11-20 17:43 ` [PATCH net 1/4] udp: Fix bind conflict check stopping at the first reuseport socket Juan Agustin Niccolini
2026-11-20 17:43 ` [PATCH net 2/4] udp: Keep the bound device on disconnect unless connect() set it Juan Agustin Niccolini
2026-11-24 18:30 ` netdev-bot+sashiko [this message]
2026-11-20 17:43 ` [PATCH net 3/4] udp: Ignore a connect()-set device in the bind conflict check Juan Agustin Niccolini
2026-11-24 18:30 ` netdev-bot+sashiko
2026-11-20 17:43 ` [PATCH net 4/4] selftests: net: Add UDP bind conflict tests Juan Agustin Niccolini
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=179554500253.3135.7129341529911265513@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=davem@davemloft.net \
--cc=e@erig.me \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=juanagusnicco@gmail.com \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux-kselftest@vger.kernel.org \
--cc=netdev@vger.kernel.org \
--cc=pabeni@redhat.com \
--cc=shuah@kernel.org \
--cc=willemb@google.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