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,
willemb@google.com, willemdebruijn.kernel@gmail.com,
dsahern@kernel.org, idosch@nvidia.com, shuah@kernel.org,
tpluszz77@gmail.com, brauner@kernel.org,
alice.kernel@fastmail.im, kylebot@openai.com, joe@dama.to,
leitao@debian.org, xudu@redhat.com, netdev@vger.kernel.org,
linux-kselftest@vger.kernel.org, linux-kernel@vger.kernel.org
Subject: Re: [PATCH net v2 2/3] udp: Ignore a connect()-set device in the bind conflict check
Date: Tue, 01 Dec 2026 05:29:13 +0000 [thread overview]
Message-ID: <179610295315.3146.15580051983330492569@kernel.org> (raw)
In-Reply-To: <20261130050729.893-3-juanagusnicco@gmail.com>
Thank you for your contribution! Sashiko AI review found 5 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 3 · Low: 1
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] The fix covers UDP but not L2TP-over-IPv6, which has the same
bug.
- [Medium] The commit message presents the cross-user takeover as closed,
but the same victim sequence still allows an equivalent takeover…
- [Medium] The patch changed udp_sk_reuseport_match() to compare dif2 =
udp_bound_dev_if(sk2), the device bind() reserved, which is 0 for a…
- [Medium] udp_bound_dev_if() reads another socket's SOCK_CONNECT_DEV
flag, then its sk_bound_dev_if, then the flag again, without that…
- [Low] The patch adds lockless READ_ONCE(sk2->sk_userlocks) reads of
other, already-hashed sockets in udp_bound_dev_if(), called from…
--
udp: Ignore a connect()-set device in the bind conflict check
This patch makes the UDP bind conflict checks in udp_lib_lport_inuse() and
udp_lib_lport_inuse2() ignore a device that connect() set, which
SOCK_CONNECT_DEV records. A wildcard-device socket connected to a
link-local or multicast IPv6 peer therefore still conflicts with binds on
other devices. The flag and sk_bound_dev_if of the other socket are read
locklessly through a new udp_bound_dev_if() helper, ordered by
smp_rmb()/smp_wmb() pairs.
> As a result, an unprivileged user can bind a port that another user's
> socket holds, and receive the datagrams that arrive on its device once
> that socket disconnects:
>
> victim: bind([::]:P), connect(fe80::1%eth0)
> attacker: SO_BINDTODEVICE(eth1), bind([::]:P) /* should fail */
> victim: connect(AF_UNSPEC)
[Severity: Medium]
The commit message reads as if this takeover is now closed. Can the same
victim sequence still be used through the local address instead of the
device?
When the victim does bind([::]:P) and then connect(fe80::1%eth0),
__ip6_datagram_connect()->ip6_datagram_dst_update() narrows the wildcard
address:
net/ipv6/datagram.c:ip6_datagram_dst_update() {
...
if (ipv6_addr_any(&sk->sk_v6_rcv_saddr)) {
sk->sk_v6_rcv_saddr = fl6->saddr;
inet->inet_rcv_saddr = LOOPBACK4_IPV6;
...
}
Say the attacker skips SO_BINDTODEVICE and binds a specific address on
port P, such as eth1's global address. udp_lib_lport_inuse() then calls
inet_rcv_saddr_equal(sk, sk2, true) against the victim's narrowed address,
finds no match and reports no conflict. The new udp_bound_dev_if() check
doesn't come into play.
On connect(AF_UNSPEC), __udp_disconnect() restores the wildcard, because
SOCK_BINDADDR_LOCK is not set for a wildcard bind:
if (!(userlocks & SOCK_BINDADDR_LOCK)) {
inet_reset_saddr(sk);
After that the attacker's more specific socket wins for datagrams to that
address. IPv4 looks the same, since __ip4_datagram_connect() narrows
inet_rcv_saddr.
The notes below the --- line mention this. However, they won't reach git
history or the stable backporters that Cc: stable brings in. Could the
commit message say that the address-based variant is still open?
> diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> index ff4f4845f2d0e..6807ce5f3e2e4 100644
> --- a/net/ipv4/udp.c
> +++ b/net/ipv4/udp.c
> @@ -131,11 +131,28 @@ EXPORT_PER_CPU_SYMBOL_GPL(udp_memory_per_cpu_fw_alloc);
> #define MAX_UDP_PORTS 65536
> #define PORTS_PER_CHAIN (MAX_UDP_PORTS / UDP_HTABLE_SIZE_MIN_PERNET)
>
> -static bool udp_sk_reuseport_match(struct sock *sk, struct sock *sk2)
> +static int udp_bound_dev_if(const struct sock *sk)
> +{
> + int dif;
> +
> + if (READ_ONCE(sk->sk_userlocks) & SOCK_CONNECT_DEV)
> + return 0;
[Severity: Low]
This isn't a functional bug, but this adds lockless READ_ONCE() reads of
sk_userlocks on other sockets that are already hashed. Several writers of
that field still do plain read-modify-write stores under lock_sock only.
One example is __inet6_bind(), which runs after get_port() has hashed the
socket:
net/ipv6/af_inet6.c:__inet6_bind() {
...
sk->sk_userlocks |= SOCK_BINDADDR_LOCK;
...
}
__inet_bind() and the SO_RCVBUF/SO_SNDBUF/SO_BUF_LOCK paths in
net/core/sock.c do the same. Would KCSAN report a concurrent bind of the
same port on another CPU as a marked-read versus plain-RMW data race?
The field is a u8, and SOCK_CONNECT_DEV only changes under lock_sock, so
there should be no visible runtime effect. inet_diag's
READ_ONCE(sk->sk_userlocks) already follows the same pattern.
> + /* Pairs with the smp_wmb() before SOCK_CONNECT_DEV is cleared. */
> + smp_rmb();
> + dif = READ_ONCE(sk->sk_bound_dev_if);
> + /* Pairs with the smp_wmb() after SOCK_CONNECT_DEV is set. */
> + smp_rmb();
> + if (READ_ONCE(sk->sk_userlocks) & SOCK_CONNECT_DEV)
> + return 0;
> + return dif;
> +}
[Severity: Medium]
Can a set-then-clear of the device between the two flag loads still be
missed here? The writers hold only sk2's lock_sock, never the hslot->lock
that the reader holds:
CPU1 (attacker bind scan) CPU2 (victim connect)
udp_bound_dev_if(sk2)
flag clear
ip6_datagram_connect_dev()
set SOCK_CONNECT_DEV
sk_bound_dev_if = eth0
dif = eth0
ip6_datagram_dst_update() fails
sk_bound_dev_if = 0
clear SOCK_CONNECT_DEV
flag clear
return eth0
The caller in udp_lib_lport_inuse{,2}() then does:
if (dif2 && sk->sk_bound_dev_if &&
dif2 != sk->sk_bound_dev_if)
continue;
That admits the conflicting eth1 bind while the victim holds the port on
every device.
The whole set-then-clear fits inside one connect() call. It happens when
ip6_datagram_dst_update() fails before any rehash, because the error path
in __ip6_datagram_connect() never takes hslot->lock:
WRITE_ONCE(sk->sk_bound_dev_if, old_bound_dev_if);
if (!old_bound_dev_if) {
smp_wmb();
WRITE_ONCE(sk->sk_userlocks,
sk->sk_userlocks & ~SOCK_CONNECT_DEV);
For a wildcard-bound victim whose connect succeeds, the rehash blocks on
the reader's hslot->lock. So this needs either the connect error path or
a victim bound to a specific address.
The reader's window can also be longer than two loads. On PREEMPT_RT,
spin_lock_bh(&hslot->lock) is preemptible, and elsewhere hardirqs, NMIs
or vCPU preemption can stretch it. The attacker can also retry its bind
freely.
The notes below --- describe this window as "within two loads", and they
are not part of the commit message. Would it take a sequence count, a
separate field for the bind-reserved device, or taking hslot->lock in the
writers to close it?
> +
> +static bool udp_sk_reuseport_match(struct sock *sk, struct sock *sk2,
> + int dif2)
> {
> return sk2->sk_family == sk->sk_family &&
> ipv6_only_sock(sk2) == ipv6_only_sock(sk) &&
> - sk2->sk_bound_dev_if == sk->sk_bound_dev_if &&
> + dif2 == sk->sk_bound_dev_if &&
> sk2->sk_reuse == sk->sk_reuse &&
> inet_rcv_saddr_equal(sk, sk2, false);
> }
> @@ -157,14 +174,18 @@ static int udp_lib_lport_inuse(struct net *net, __u16 num,
> sk2 != sk &&
> (bitmap || udp_sk(sk2)->udp_port_hash == num) &&
> (bitmap || !sk2->sk_reuse || !sk->sk_reuse) &&
> - (!sk2->sk_bound_dev_if || !sk->sk_bound_dev_if ||
> - sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
> inet_rcv_saddr_equal(sk, sk2, true)) {
> + int dif2 = udp_bound_dev_if(sk2);
> +
> + if (dif2 && sk->sk_bound_dev_if &&
> + dif2 != sk->sk_bound_dev_if)
> + continue;
> if (!bitmap) {
> if (sk2->sk_reuseport && sk->sk_reuseport &&
> !rcu_access_pointer(sk->sk_reuseport_cb) &&
> uid_eq(uid, sk_uid(sk2))) {
> - if (udp_sk_reuseport_match(sk, sk2))
> + if (udp_sk_reuseport_match(sk, sk2,
> + dif2))
> return 0;
[Severity: Medium]
Now that dif2 comes from udp_bound_dev_if(), can udp_sk_reuseport_match()
and udp_reuseport_add_sock() disagree about the same sk2?
udp_lib_get_port() calls udp_reuseport_add_sock() right after this scan,
and that function still compares the raw device:
net/ipv4/udp.c:udp_reuseport_add_sock() {
...
(sk2->sk_bound_dev_if == sk->sk_bound_dev_if) &&
...
return reuseport_alloc(sk, inet_rcv_saddr_any(sk));
}
Take sk2 as a same-uid SO_REUSEPORT socket bound to G:P with no device and
connected to fe80::1%eth0, so dif2 is 0. A new SO_REUSEPORT G:P bind with
no device matches it, and the scan returns 0 here. udp_reuseport_add_sock()
then sees eth0 != 0 and calls reuseport_alloc() for a separate group.
Because of the early return, the rest of the chain is never checked.
Consider a non-reuseport [::]:P socket further down the chain, bound
before sk2 and disconnected afterwards. Before this patch the scan would
continue past sk2, reach that socket and refuse the bind. With this
patch the bind is accepted. Building this across users seems to also need
the existing address-narrowing window.
Commit d77700039d09 ("udp: only quit inuse scan early on identical
SO_REUSEPORT sockets") limited this early exit to sockets that sk will
actually share a group with. Does this bring back an early exit on a
socket sk does not share a group with?
The reverse case looks possible too. If sk is bound to eth0 with
SO_BINDTODEVICE, the match fails (0 != eth0) and the scan continues. But
udp_reuseport_add_sock() then puts sk into sk2's group, which the check
had just treated as not matching.
> continue;
> }
[ ... ]
> diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
> index 9fbb78aa9d686..89870e1428359 100644
> --- a/net/ipv6/datagram.c
> +++ b/net/ipv6/datagram.c
> @@ -142,9 +142,12 @@ EXPORT_SYMBOL_GPL(ip6_datagram_release_cb);
>
> static void ip6_datagram_connect_dev(struct sock *sk, int old_dif, int dif)
> {
> - if (!old_dif && dif)
> + if (!old_dif && dif) {
> WRITE_ONCE(sk->sk_userlocks,
> sk->sk_userlocks | SOCK_CONNECT_DEV);
> + /* Pairs with the second smp_rmb() in udp_bound_dev_if(). */
> + smp_wmb();
> + }
> WRITE_ONCE(sk->sk_bound_dev_if, dif);
> }
[Severity: High]
Is L2TP over IPv6 still open to the same takeover?
l2tp_ip6_connect() calls __ip6_datagram_connect(), so this function sets
SOCK_CONNECT_DEV and the device for a link-local peer on L2TP/IPv6
sockets too. l2tp_ip6_disconnect() reaches __udp_disconnect() through
ip6_datagram_disconnect(), which the earlier patch in this series ("udp:
Keep the bound device on disconnect unless connect() set it") changed to
clear that device.
The L2TP bind conflict check still trusts the device that connect() set:
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;
...
}
udp_bound_dev_if() is static to net/ipv4/udp.c, so only UDP gets the
fix. The sequence from the commit message seems to work with
IPPROTO_L2TP:
victim: bind(conn_id X, ::), connect(fe80::1%eth0)
attacker: SO_BINDTODEVICE(eth1), bind(conn_id X, ::)
victim: connect(AF_UNSPEC)
l2tp_ip6_bind() calls __l2tp_ip6_bind_lookup() with raddr NULL and dif set
to eth1. It skips the victim because eth0 != eth1 and accepts the bind.
After the disconnect, the victim's device goes back to 0. The attacker's
socket was inserted at the head of l2tp_ip6_bind_table after the victim's
connect() re-insert. l2tp_ip6_recv() takes the first match, so the
attacker would receive the L2TP control frames for conn_id X that arrive
on eth1.
An L2TP/IPv6 socket is SOCK_DGRAM and needs no capability, and the module
autoloads. The selftest patch later in the series only touches tools/, so
the kernel code is unchanged here at the end of the series.
Should __l2tp_ip6_bind_lookup() also ignore a SOCK_CONNECT_DEV device,
for example by sharing the helper?
[ ... ]
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261130050729.893-1-juanagusnicco%40gmail.com
next prev parent reply other threads:[~2026-12-01 5:29 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-30 5:07 [PATCH net v2 0/3] udp: Fix bind conflict check bypasses Juan Agustin Niccolini
2026-11-30 5:07 ` [PATCH net v2 1/3] udp: Keep the bound device on disconnect unless connect() set it Juan Agustin Niccolini
2026-12-01 5:29 ` netdev-bot+sashiko
2026-11-30 5:07 ` [PATCH net v2 2/3] udp: Ignore a connect()-set device in the bind conflict check Juan Agustin Niccolini
2026-12-01 5:29 ` netdev-bot+sashiko [this message]
2026-11-30 5:07 ` [PATCH net v2 3/3] selftests: net: Add UDP bind conflict tests Juan Agustin Niccolini
2026-12-01 5:29 ` netdev-bot+sashiko
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=179610295315.3146.15580051983330492569@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=alice.kernel@fastmail.im \
--cc=brauner@kernel.org \
--cc=davem@davemloft.net \
--cc=dsahern@kernel.org \
--cc=edumazet@kernel.org \
--cc=horms@kernel.org \
--cc=idosch@nvidia.com \
--cc=joe@dama.to \
--cc=juanagusnicco@gmail.com \
--cc=kuba@kernel.org \
--cc=kuniyu@google.com \
--cc=kylebot@openai.com \
--cc=leitao@debian.org \
--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=tpluszz77@gmail.com \
--cc=willemb@google.com \
--cc=willemdebruijn.kernel@gmail.com \
--cc=xudu@redhat.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