* [PATCH net 1/4] udp: Fix bind conflict check stopping at the first reuseport socket
2026-11-20 17:43 [PATCH net 0/4] udp: Fix bind conflict check bypasses Juan Agustin Niccolini
@ 2026-11-20 17:43 ` 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
` (2 subsequent siblings)
3 siblings, 0 replies; 9+ messages in thread
From: Juan Agustin Niccolini @ 2026-11-20 17:43 UTC (permalink / raw)
To: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Kuniyuki Iwashima, Willem de Bruijn,
Willem de Bruijn, Eric Garver, Shuah Khan, linux-kselftest,
netdev, linux-kernel
Since commit df560056d960 ("udp: inuse checks can quit early for
reuseport"), udp_lib_lport_inuse() and udp_lib_lport_inuse2() report
the port as free as soon as they find one overlapping socket that has
SO_REUSEPORT and the same owner, without looking at the rest of the
chain.
This is only safe if overlap is transitive, and it is not:
- A wildcard address overlaps specific addresses that do not overlap
each other.
- A socket with no bound device overlaps sockets bound to different
devices.
As a result, an unprivileged user can bind an address and port that
another user's socket holds, and receive its datagrams:
victim: bind(127.0.0.2:P)
attacker: SO_REUSEPORT, bind(127.0.0.3:P)
attacker: SO_REUSEPORT, bind(0.0.0.0:P) /* should fail */
attacker: SO_REUSEPORT, bind(127.0.0.2:P) /* should fail */
Only stop at a socket that matches the new one in family, IPV6_V6ONLY,
bound device, SO_REUSEADDR and local address, and keep scanning past
the other compatible sockets. Identical SO_REUSEPORT sockets, the
workload the early stop was added for, still stop at the first one
(binding 16384 of them on a KASAN build: 223 ms before, 222 ms after,
3727 ms with the full scan every time).
Fixes: df560056d960 ("udp: inuse checks can quit early for reuseport")
Cc: stable@vger.kernel.org
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
net/ipv4/udp.c | 18 ++++++++++++++++--
1 file changed, 16 insertions(+), 2 deletions(-)
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 303d0102519a..446729b41d66 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -131,6 +131,15 @@ 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_lport_same_bind(const struct sock *sk, const struct sock *sk2)
+{
+ 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 &&
+ sk2->sk_reuse == sk->sk_reuse &&
+ inet_rcv_saddr_equal(sk, sk2, false);
+}
+
static int udp_lib_lport_inuse(struct net *net, __u16 num,
const struct udp_hslot *hslot,
unsigned long *bitmap,
@@ -154,8 +163,11 @@ static int udp_lib_lport_inuse(struct net *net, __u16 num,
if (!bitmap) {
if (sk2->sk_reuseport && sk->sk_reuseport &&
!rcu_access_pointer(sk->sk_reuseport_cb) &&
- uid_eq(uid, sk_uid(sk2)))
- return 0;
+ uid_eq(uid, sk_uid(sk2))) {
+ if (udp_lport_same_bind(sk, sk2))
+ return 0;
+ continue;
+ }
return 1;
}
__set_bit(udp_sk(sk2)->udp_port_hash >> log, bitmap);
@@ -188,6 +200,8 @@ static int udp_lib_lport_inuse2(struct net *net, __u16 num,
if (sk2->sk_reuseport && sk->sk_reuseport &&
!rcu_access_pointer(sk->sk_reuseport_cb) &&
uid_eq(uid, sk_uid(sk2))) {
+ if (!udp_lport_same_bind(sk, sk2))
+ continue;
res = 0;
} else {
res = 1;
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread* [PATCH net 2/4] udp: Keep the bound device on disconnect unless connect() set it
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 ` Juan Agustin Niccolini
2026-11-24 18:30 ` netdev-bot+sashiko
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-20 17:43 ` [PATCH net 4/4] selftests: net: Add UDP bind conflict tests Juan Agustin Niccolini
3 siblings, 1 reply; 9+ messages in thread
From: Juan Agustin Niccolini @ 2026-11-20 17:43 UTC (permalink / raw)
To: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Kuniyuki Iwashima, Willem de Bruijn,
Willem de Bruijn, Eric Garver, Shuah Khan, linux-kselftest,
netdev, linux-kernel
__udp_disconnect() clears sk_bound_dev_if unconditionally, without a
bind conflict check.
This is only safe for a device that connect() set, which
__ip6_datagram_connect() does for a link-local or multicast peer. The
device can also come from:
- The user, with SO_BINDTODEVICE, SO_BINDTOIFINDEX or a bind() to a
scoped IPv6 address.
- A cgroup program, as with ip vrf exec.
The bind conflict checks treat sockets bound to different devices as
not overlapping. As a result, an unprivileged user can bind an address
and port that another user's socket holds on another device, clear its
own device, and receive the other socket's datagrams:
victim: SO_BINDTODEVICE(lo), bind(0.0.0.0:P)
attacker: SO_BINDTODEVICE(eth0), bind(127.0.0.1:P)
attacker: connect(AF_UNSPEC) /* clears the device */
Record in sk_userlocks that connect() set the device of a socket that
had none, and only clear the device on disconnect in that case.
SO_BINDTODEVICE and SO_BINDTOIFINDEX drop the record.
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.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
Notes:
- Within a VRF, connect() can replace the master with one of its
interfaces, which is now kept on disconnect, so binding back to the
master needs CAP_NET_RAW. Before, disconnect took the socket out of
its VRF. Restoring the master is not safe: the bind conflict checks
treat a master and its interfaces as different devices, so another
socket may hold the master by then. Fixing that is a separate,
VRF-wide change.
- Ping, raw and L2TP sockets call __udp_disconnect() without the socket
lock, so the sk_userlocks update is unlocked there. That predates
this change: commit 286c72deabaa ("udp: must lock the socket in
udp_disconnect()") only added the lock for UDP. Fixing it means
changing how ping, raw and L2TP handle disconnect, which is separate
from this fix, and a lost update cannot cause a bind overlap anyway.
include/net/sock.h | 4 ++++
net/core/sock.c | 1 +
net/ipv4/udp.c | 13 +++++++++----
net/ipv6/datagram.c | 17 +++++++++++++++--
4 files changed, 29 insertions(+), 6 deletions(-)
diff --git a/include/net/sock.h b/include/net/sock.h
index 173b37b2b8bb..78fc65adbcb8 100644
--- a/include/net/sock.h
+++ b/include/net/sock.h
@@ -1534,6 +1534,10 @@ static inline int __sk_prot_rehash(struct sock *sk)
* define SOCK_CONNECT_BIND - &sock->sk_userlocks flag for auto-bind at connect() time
*/
#define SOCK_CONNECT_BIND 16
+/**
+ * define SOCK_CONNECT_DEV - &sock->sk_userlocks flag for a connect()-set device
+ */
+#define SOCK_CONNECT_DEV 32
struct socket_alloc {
struct socket socket;
diff --git a/net/core/sock.c b/net/core/sock.c
index e2401d735c5a..5ce4aefc514d 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);
+ WRITE_ONCE(sk->sk_userlocks, sk->sk_userlocks & ~SOCK_CONNECT_DEV);
if (sk->sk_prot->rehash)
sk->sk_prot->rehash(sk);
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 446729b41d66..9f1f45afee53 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);
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);
+ WRITE_ONCE(sk->sk_userlocks,
+ sk->sk_userlocks & ~SOCK_CONNECT_DEV);
+ }
+ if (!(userlocks & SOCK_BINDADDR_LOCK)) {
inet_reset_saddr(sk);
if (sk->sk_prot->rehash &&
- (sk->sk_userlocks & SOCK_BINDPORT_LOCK))
+ (userlocks & SOCK_BINDPORT_LOCK))
sk->sk_prot->rehash(sk);
}
- if (!(sk->sk_userlocks & SOCK_BINDPORT_LOCK)) {
+ if (!(userlocks & SOCK_BINDPORT_LOCK)) {
sk->sk_prot->unhash(sk);
inet->inet_sport = 0;
}
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index ff7a7ffbf983..0296207dbeb0 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -139,6 +139,14 @@ void ip6_datagram_release_cb(struct sock *sk)
}
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)
+ WRITE_ONCE(sk->sk_userlocks,
+ sk->sk_userlocks | SOCK_CONNECT_DEV);
+ WRITE_ONCE(sk->sk_bound_dev_if, dif);
+}
+
int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
int addr_len)
{
@@ -229,11 +237,13 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
err = -EINVAL;
goto out;
}
- WRITE_ONCE(sk->sk_bound_dev_if, usin->sin6_scope_id);
+ ip6_datagram_connect_dev(sk, old_bound_dev_if,
+ usin->sin6_scope_id);
}
if (!sk->sk_bound_dev_if && (addr_type & IPV6_ADDR_MULTICAST))
- WRITE_ONCE(sk->sk_bound_dev_if, READ_ONCE(np->mcast_oif));
+ ip6_datagram_connect_dev(sk, old_bound_dev_if,
+ READ_ONCE(np->mcast_oif));
/* Connect to link-local address requires an interface */
if (!sk->sk_bound_dev_if) {
@@ -265,6 +275,9 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
np->flow_label = old_fl6_flowlabel;
inet->inet_dport = old_dport;
WRITE_ONCE(sk->sk_bound_dev_if, old_bound_dev_if);
+ if (!old_bound_dev_if)
+ WRITE_ONCE(sk->sk_userlocks,
+ sk->sk_userlocks & ~SOCK_CONNECT_DEV);
goto out;
}
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH net 2/4] udp: Keep the bound device on disconnect unless connect() set it
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
2026-11-24 19:43 ` Juan Agustin Niccolini
0 siblings, 1 reply; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 18:30 UTC (permalink / raw)
To: juanagusnicco
Cc: davem, edumazet, kuba, pabeni, horms, kuniyu,
willemdebruijn.kernel, willemb, e, shuah, linux-kselftest,
netdev, linux-kernel
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
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH net 2/4] udp: Keep the bound device on disconnect unless connect() set it
2026-11-24 18:30 ` netdev-bot+sashiko
@ 2026-11-24 19:43 ` Juan Agustin Niccolini
0 siblings, 0 replies; 9+ messages in thread
From: Juan Agustin Niccolini @ 2026-11-24 19:43 UTC (permalink / raw)
To: netdev-bot+sashiko
Cc: davem, edumazet, kuba, pabeni, horms, kuniyu,
willemdebruijn.kernel, willemb, e, shuah, linux-kselftest,
netdev, linux-kernel
This series fixes the universal bugs, where simply binding a specific
way makes a socket vulnerable. It does not fix every instance of the
mistake, which was repeated more than once on every protocol I looked
at. I was already aware of the L2TP bug. Its fix does not belong on this
commit and I don't think it belongs on the series either but I will let
the maintainers decide. Also, the issue is pre-existing, sashiko
misclassified it, as usual.
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH net 3/4] udp: Ignore a connect()-set device in the bind conflict check
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-20 17:43 ` 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
3 siblings, 1 reply; 9+ messages in thread
From: Juan Agustin Niccolini @ 2026-11-20 17:43 UTC (permalink / raw)
To: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Kuniyuki Iwashima, Willem de Bruijn,
Willem de Bruijn, Eric Garver, Shuah Khan, linux-kselftest,
netdev, linux-kernel
The bind conflict checks compare sk_bound_dev_if, which connect() also
writes, for a link-local or multicast IPv6 peer.
This is only safe if the device is the one that bind() reserved. A
socket bound with no device holds its port on every device, but while
it is connected to such a peer it looks bound to one device, so a bind
of the same port on another device is admitted.
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)
Compare the device that bind() reserved instead: none for a socket
whose device connect() set, which SOCK_CONNECT_DEV records. A VRF
master that connect() narrowed to one of its interfaces does not get
the flag and is compared as before.
Note that the checks read the flag without the other socket's lock, so
it is ordered against the device with barriers: seeing the device
before the flag would trust a connect()-set device again.
Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2")
Cc: stable@vger.kernel.org
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
Notes:
- A connect() that sets the device and is undone again, both between
the two reads of the flag, can still be seen as a bound device, so a
conflicting bind can be admitted. Not fixed here because closing it
needs a sequence count in struct sock, a larger change for a window
that the other socket's owner has to open within two loads.
- connect() also narrows the local address of a wildcard-bound
socket, which opens the same window through the address. It is not
fixed here: connect() moves the socket to the secondary hash slot of
the narrowed address, so a specific-address bind on a busy port never
scans it. Closing that needs either a full scan on every bind or no
rehash on connect, which slows down binds or lookups on busy ports,
so I leave it for a separate patch.
net/core/sock.c | 2 ++
net/ipv4/udp.c | 46 +++++++++++++++++++++++++++++++++++----------
net/ipv6/datagram.c | 12 ++++++++++--
3 files changed, 48 insertions(+), 12 deletions(-)
diff --git a/net/core/sock.c b/net/core/sock.c
index 5ce4aefc514d..48ec344a3a8b 100644
--- a/net/core/sock.c
+++ b/net/core/sock.c
@@ -650,6 +650,8 @@ 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);
+ /* Pairs with the first smp_rmb() in udp_bound_dev_if(). */
+ smp_wmb();
WRITE_ONCE(sk->sk_userlocks, sk->sk_userlocks & ~SOCK_CONNECT_DEV);
if (sk->sk_prot->rehash)
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 9f1f45afee53..a13bf32cbdb9 100644
--- a/net/ipv4/udp.c
+++ b/net/ipv4/udp.c
@@ -131,11 +131,35 @@ 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_lport_same_bind(const struct sock *sk, const 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;
+ /* 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;
+}
+
+static bool udp_bound_dev_overlap(int dif, const struct sock *sk2)
+{
+ int dif2 = udp_bound_dev_if(sk2);
+
+ return !dif || !dif2 || dif == dif2;
+}
+
+static bool udp_lport_same_bind(const struct sock *sk, int dif,
+ const struct sock *sk2)
{
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 &&
+ udp_bound_dev_if(sk2) == dif &&
sk2->sk_reuse == sk->sk_reuse &&
inet_rcv_saddr_equal(sk, sk2, false);
}
@@ -145,6 +169,7 @@ static int udp_lib_lport_inuse(struct net *net, __u16 num,
unsigned long *bitmap,
struct sock *sk, unsigned int log)
{
+ int dif = udp_bound_dev_if(sk);
kuid_t uid = sk_uid(sk);
struct sock *sk2;
@@ -157,14 +182,13 @@ 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)) {
+ inet_rcv_saddr_equal(sk, sk2, true) &&
+ udp_bound_dev_overlap(dif, sk2)) {
if (!bitmap) {
if (sk2->sk_reuseport && sk->sk_reuseport &&
!rcu_access_pointer(sk->sk_reuseport_cb) &&
uid_eq(uid, sk_uid(sk2))) {
- if (udp_lport_same_bind(sk, sk2))
+ if (udp_lport_same_bind(sk, dif, sk2))
return 0;
continue;
}
@@ -184,6 +208,7 @@ static int udp_lib_lport_inuse2(struct net *net, __u16 num,
struct udp_hslot *hslot2,
struct sock *sk)
{
+ int dif = udp_bound_dev_if(sk);
kuid_t uid = sk_uid(sk);
struct sock *sk2;
int res = 0;
@@ -194,13 +219,12 @@ static int udp_lib_lport_inuse2(struct net *net, __u16 num,
sk2 != sk &&
(udp_sk(sk2)->udp_port_hash == num) &&
(!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)) {
+ inet_rcv_saddr_equal(sk, sk2, true) &&
+ udp_bound_dev_overlap(dif, sk2)) {
if (sk2->sk_reuseport && sk->sk_reuseport &&
!rcu_access_pointer(sk->sk_reuseport_cb) &&
uid_eq(uid, sk_uid(sk2))) {
- if (!udp_lport_same_bind(sk, sk2))
+ if (!udp_lport_same_bind(sk, dif, sk2))
continue;
res = 0;
} else {
@@ -2207,6 +2231,8 @@ int __udp_disconnect(struct sock *sk, int flags)
sock_rps_reset_rxhash(sk);
if (userlocks & SOCK_CONNECT_DEV) {
WRITE_ONCE(sk->sk_bound_dev_if, 0);
+ /* Pairs with the first smp_rmb() in udp_bound_dev_if(). */
+ smp_wmb();
WRITE_ONCE(sk->sk_userlocks,
sk->sk_userlocks & ~SOCK_CONNECT_DEV);
}
diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
index 0296207dbeb0..37a093f74724 100644
--- a/net/ipv6/datagram.c
+++ b/net/ipv6/datagram.c
@@ -141,9 +141,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);
}
@@ -275,9 +278,14 @@ int __ip6_datagram_connect(struct sock *sk, struct sockaddr_unsized *uaddr,
np->flow_label = old_fl6_flowlabel;
inet->inet_dport = old_dport;
WRITE_ONCE(sk->sk_bound_dev_if, old_bound_dev_if);
- if (!old_bound_dev_if)
+ if (!old_bound_dev_if) {
+ /* Pairs with the first smp_rmb() in
+ * udp_bound_dev_if().
+ */
+ smp_wmb();
WRITE_ONCE(sk->sk_userlocks,
sk->sk_userlocks & ~SOCK_CONNECT_DEV);
+ }
goto out;
}
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH net 3/4] udp: Ignore a connect()-set device in the bind conflict check
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-24 19:40 ` Juan Agustin Niccolini
0 siblings, 1 reply; 9+ messages in thread
From: netdev-bot+sashiko @ 2026-11-24 18:30 UTC (permalink / raw)
To: juanagusnicco
Cc: davem, edumazet, kuba, pabeni, horms, kuniyu,
willemdebruijn.kernel, willemb, e, shuah, linux-kselftest,
netdev, linux-kernel
Thank you for your contribution! Sashiko AI review found 4 potential
issue(s) to consider.
Critical: 0 · High: 1 · Medium: 3 · 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).
- [High] Incomplete fix: the port hijack in the commit message still
works through the local address that connect() narrows.
- [Medium] Incomplete fix: the L2TP/IPv6 bind conflict check still trusts
a device that connect() set.
- [Medium] On a socket bound to a VRF (L3 master), the bind conflict
check still uses a device that connect() narrowed, not the device…
- [Medium] Race: the lockless check in udp_bound_dev_if()
(net/ipv4/udp.c:134) reads flag, then device, then flag again, and…
--
udp: Ignore a connect()-set device in the bind conflict check
This patch changes the UDP bind conflict checks in udp_lib_lport_inuse()
and udp_lib_lport_inuse2() so they ignore a device that connect() set on
another socket. SOCK_CONNECT_DEV records that case. A new lockless helper,
udp_bound_dev_if(), reads the flag, then the device, then the flag again.
Matching smp_wmb() calls are added in the connect, disconnect and
SO_BINDTODEVICE paths.
> diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> index 9f1f45afee532..a13bf32cbdb99 100644
> --- a/net/ipv4/udp.c
> +++ b/net/ipv4/udp.c
> @@ -131,11 +131,35 @@ 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_lport_same_bind(const struct sock *sk, const 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;
> + /* 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 full connect() and disconnect() cycle on sk2 happen entirely
between the two loads of sk_userlocks here?
The writers are ip6_datagram_connect_dev(), __udp_disconnect(), the
error path in __ip6_datagram_connect() and sock_bindtoindex_locked().
They hold only lock_sock() of the other socket. This reader holds only
hslot->lock or hslot2->lock.
Say the victim runs connect(fe80::1%eth0) and then connect(AF_UNSPEC)
between the two flag loads:
CPU1 (attacker bind) CPU2 (victim)
udp_bound_dev_if(sk2)
READ_ONCE(sk_userlocks) /* clear */
ip6_datagram_connect_dev()
set SOCK_CONNECT_DEV
sk_bound_dev_if = eth0
READ_ONCE(sk_bound_dev_if) /* eth0 */
__udp_disconnect()
sk_bound_dev_if = 0
clear SOCK_CONNECT_DEV
READ_ONCE(sk_userlocks) /* clear */
return eth0
udp_bound_dev_overlap() then compares eth0 with the attacker's eth1 and
admits the bind. The victim, however, holds the port on every device.
On PREEMPT_RT, spin_lock_bh() in udp_lib_get_port() does not disable
preemption, so the binding task can be preempted between the loads. On
other kernels a hardirq or NMI can open the same window. The attacker
can retry SO_BINDTODEVICE plus bind() at almost no cost.
The notes after the commit message acknowledge this window and leave it
open. Since this is tagged for stable, would it be better to close it
here? A sequence count, a lock shared with the writers, or a single
field that records the bind-time device would each do it.
[ ... ]
> @@ -157,14 +182,13 @@ 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)) {
> + inet_rcv_saddr_equal(sk, sk2, true) &&
> + udp_bound_dev_overlap(dif, sk2)) {
[Severity: High]
Does the hijack in the commit message still work if the attacker binds
a specific address instead of [::]?
On connect, a socket bound to a wildcard address gets a narrowed local
address and is rehashed:
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;
if (sk->sk_prot->rehash)
sk->sk_prot->rehash(sk);
...
}
__ip4_datagram_connect() does the same for IPv4:
if (!inet->inet_rcv_saddr) {
WRITE_ONCE(inet->inet_rcv_saddr, fl4->saddr);
if (sk->sk_prot->rehash)
sk->sk_prot->rehash(sk);
}
Suppose the victim is narrowed to one specific address and the attacker
binds a different one. inet_rcv_saddr_equal() returns false before
udp_bound_dev_overlap() runs, so the bind is admitted. In
udp_lib_lport_inuse2(), the rehashed victim is not in any of the hslot2
buckets that get scanned.
Later the victim calls connect(AF_UNSPEC). A wildcard bind never sets
SOCK_BINDADDR_LOCK, so __udp_disconnect() calls inet_reset_saddr() and
the victim goes back to the wildcard address. The attacker's more
specific socket then wins the lookup.
This IPv4 sequence needs neither a link-local peer nor SO_BINDTODEVICE:
victim: bind(0.0.0.0:P), connect(8.8.8.8:53) /* narrows to 192.168.1.5 */
attacker: bind(127.0.0.1:P) /* admitted */
victim: connect(AF_UNSPEC)
After this, the attacker receives the victim's loopback traffic to
port P. The commit's own example also looks exploitable if the attacker
binds [2001:db8::a]:P instead of [::]:P.
The notes after the commit message mention this window and leave it for
a separate patch, and nothing later in the series addresses it. The
commit message says the patch stops an unprivileged user from binding a
port that another user's socket holds, and it is tagged for stable.
Should the address case be fixed together with the device case?
[ ... ]
> @@ -194,13 +219,12 @@ static int udp_lib_lport_inuse2(struct net *net, __u16 num,
> sk2 != sk &&
> (udp_sk(sk2)->udp_port_hash == num) &&
> (!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)) {
> + inet_rcv_saddr_equal(sk, sk2, true) &&
> + udp_bound_dev_overlap(dif, sk2)) {
[Severity: Medium]
This converts the UDP conflict checks. Does the L2TP/IPv6 bind check
still trust a device that connect() set?
l2tp_ip6_connect() calls __ip6_datagram_connect(). On an L2TP/IPv6
socket bound with no device, ip6_datagram_connect_dev() therefore sets
both sk_bound_dev_if and SOCK_CONNECT_DEV. __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;
...
}
Take the same sequence as in the commit message:
victim: bind([::], conn_id X), connect(fe80::1%eth0)
attacker: SO_BINDTODEVICE(eth1), bind([::], conn_id X)
victim: connect(AF_UNSPEC)
l2tp_ip6_bind() skips the victim (eth0 vs eth1) and admits the
attacker. Then l2tp_ip6_disconnect()->__udp_disconnect() clears the
victim's device because SOCK_CONNECT_DEV is set. The victim now holds
conn_id X on every device, while the attacker's socket is still in
l2tp_ip6_bind_table.
sk_add_bind_node() added the attacker's socket at the head of that
table. So l2tp_ip6_recv() would match it first for control frames from
fe80::1 that arrive on eth1 for tunnel X.
The previous patch in the series lists L2TP as a __udp_disconnect()
user, and nothing later in the series changes this lookup. Should
__l2tp_ip6_bind_lookup() also ignore a device that connect() set?
[ ... ]
> diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
> index 0296207dbeb0b..37a093f74724e 100644
> --- a/net/ipv6/datagram.c
> +++ b/net/ipv6/datagram.c
> @@ -141,9 +141,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: Medium]
For a socket bound to a VRF master, can udp_bound_dev_if() still return
the slave that connect() picked instead of the device bind() reserved?
In __ip6_datagram_connect(), sk_dev_equal_l3scope() accepts a slave0
scope id on a socket bound to vrf0. ip6_datagram_connect_dev(sk, vrf0,
slave0) then writes sk_bound_dev_if = slave0. It does not set
SOCK_CONNECT_DEV, because old_dif is non-zero.
udp_bound_dev_if() then returns slave0 for that socket.
udp_bound_dev_overlap() and udp_lport_same_bind() treat slave0 as the
reserved device.
For example, user A binds vrf0 X:P and connects to fe80::1%slave0.
Another user then binds vrf0 X:P. udp_bound_dev_overlap(vrf0, A)
compares vrf0 with slave0, finds no overlap, and admits the bind, even
though A reserved vrf0.
With the earlier patch in this series, __udp_disconnect() keeps slave0
because the flag is clear. A never returns to vrf0, and the other user
alone receives the port's traffic on the other slaves of vrf0.
The early return for reuseport can go wrong the same way. Say B is
owned by A's user, uses SO_REUSEPORT and is bound to slave0, and its
scan reaches A first. Then udp_lport_same_bind(B, slave0, A) is true,
and udp_lib_lport_inuse() returns 0 before it sees socket C. C belongs
to another user and is bound to slave0 X:P.
The commit message says:
A VRF master that connect() narrowed to one of its interfaces does not
get the flag and is compared as before.
Is it safe to compare as before here? Would recording the bind-time
device, instead of using a flag, also cover this case?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120174341.35873-1-juanagusnicco%40gmail.com
^ permalink raw reply [flat|nested] 9+ messages in thread* Re: [PATCH net 3/4] udp: Ignore a connect()-set device in the bind conflict check
2026-11-24 18:30 ` netdev-bot+sashiko
@ 2026-11-24 19:40 ` Juan Agustin Niccolini
0 siblings, 0 replies; 9+ messages in thread
From: Juan Agustin Niccolini @ 2026-11-24 19:40 UTC (permalink / raw)
To: netdev-bot+sashiko
Cc: davem, edumazet, kuba, pabeni, horms, kuniyu,
willemdebruijn.kernel, willemb, e, shuah, linux-kselftest,
netdev, linux-kernel
> - [High] Incomplete fix: the port hijack in the commit message still
> works through the local address that connect() narrows.
Addressed in note 2 of this patch:
"- connect() also narrows the local address of a wildcard-bound
socket, which opens the same window through the address. It is not
fixed here: connect() moves the socket to the secondary hash slot of
the narrowed address, so a specific-address bind on a busy port never
scans it. Closing that needs either a full scan on every bind or no
rehash on connect, which slows down binds or lookups on busy ports,
so I leave it for a separate patch."
> - [Medium] Incomplete fix: the L2TP/IPv6 bind conflict check still trusts
> a device that connect() set.
I didn't include a note for this one but I was already aware. I think it
needs a separate patch that does not belong in this series but
maintainers can decide.
> - [Medium] Race: the lockless check in udp_bound_dev_if()
> (net/ipv4/udp.c:134) reads flag, then device, then flag again, and…
Addressed in note 1 of this patch:
"- A connect() that sets the device and is undone again, both between
the two reads of the flag, can still be seen as a bound device, so a
conflicting bind can be admitted. Not fixed here because closing it
needs a sequence count in struct sock, a larger change for a window
that the other socket's owner has to open within two loads."
> - [Medium] On a socket bound to a VRF (L3 master), the bind conflict
> check still uses a device that connect() narrowed, not the device…
This is pre-existing and needs a VRF to be configured: connect() has
replaced the master with one of its interfaces without a recheck since
before this series. As the notes on patch 2/4 say, fixing it is a
separate, VRF-wide change, so it belongs in its own patch.
Maintainers, let me know if you want any of these addressed in this
series.
Best,
Agustin
On Tue, Nov 24, 2026 at 1:30 PM <netdev-bot+sashiko@kernel.org> wrote:
>
> Thank you for your contribution! Sashiko AI review found 4 potential
> issue(s) to consider.
>
> Critical: 0 · High: 1 · Medium: 3 · 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).
>
> - [High] Incomplete fix: the port hijack in the commit message still
> works through the local address that connect() narrows.
> - [Medium] Incomplete fix: the L2TP/IPv6 bind conflict check still trusts
> a device that connect() set.
> - [Medium] On a socket bound to a VRF (L3 master), the bind conflict
> check still uses a device that connect() narrowed, not the device…
> - [Medium] Race: the lockless check in udp_bound_dev_if()
> (net/ipv4/udp.c:134) reads flag, then device, then flag again, and…
>
> --
>
> udp: Ignore a connect()-set device in the bind conflict check
>
> This patch changes the UDP bind conflict checks in udp_lib_lport_inuse()
> and udp_lib_lport_inuse2() so they ignore a device that connect() set on
> another socket. SOCK_CONNECT_DEV records that case. A new lockless helper,
> udp_bound_dev_if(), reads the flag, then the device, then the flag again.
> Matching smp_wmb() calls are added in the connect, disconnect and
> SO_BINDTODEVICE paths.
>
> > diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
> > index 9f1f45afee532..a13bf32cbdb99 100644
> > --- a/net/ipv4/udp.c
> > +++ b/net/ipv4/udp.c
> > @@ -131,11 +131,35 @@ 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_lport_same_bind(const struct sock *sk, const 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;
> > + /* 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 full connect() and disconnect() cycle on sk2 happen entirely
> between the two loads of sk_userlocks here?
>
> The writers are ip6_datagram_connect_dev(), __udp_disconnect(), the
> error path in __ip6_datagram_connect() and sock_bindtoindex_locked().
> They hold only lock_sock() of the other socket. This reader holds only
> hslot->lock or hslot2->lock.
>
> Say the victim runs connect(fe80::1%eth0) and then connect(AF_UNSPEC)
> between the two flag loads:
>
> CPU1 (attacker bind) CPU2 (victim)
> udp_bound_dev_if(sk2)
> READ_ONCE(sk_userlocks) /* clear */
> ip6_datagram_connect_dev()
> set SOCK_CONNECT_DEV
> sk_bound_dev_if = eth0
> READ_ONCE(sk_bound_dev_if) /* eth0 */
> __udp_disconnect()
> sk_bound_dev_if = 0
> clear SOCK_CONNECT_DEV
> READ_ONCE(sk_userlocks) /* clear */
> return eth0
>
> udp_bound_dev_overlap() then compares eth0 with the attacker's eth1 and
> admits the bind. The victim, however, holds the port on every device.
>
> On PREEMPT_RT, spin_lock_bh() in udp_lib_get_port() does not disable
> preemption, so the binding task can be preempted between the loads. On
> other kernels a hardirq or NMI can open the same window. The attacker
> can retry SO_BINDTODEVICE plus bind() at almost no cost.
>
> The notes after the commit message acknowledge this window and leave it
> open. Since this is tagged for stable, would it be better to close it
> here? A sequence count, a lock shared with the writers, or a single
> field that records the bind-time device would each do it.
>
> [ ... ]
>
> > @@ -157,14 +182,13 @@ 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)) {
> > + inet_rcv_saddr_equal(sk, sk2, true) &&
> > + udp_bound_dev_overlap(dif, sk2)) {
>
> [Severity: High]
> Does the hijack in the commit message still work if the attacker binds
> a specific address instead of [::]?
>
> On connect, a socket bound to a wildcard address gets a narrowed local
> address and is rehashed:
>
> 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;
> if (sk->sk_prot->rehash)
> sk->sk_prot->rehash(sk);
> ...
> }
>
> __ip4_datagram_connect() does the same for IPv4:
>
> if (!inet->inet_rcv_saddr) {
> WRITE_ONCE(inet->inet_rcv_saddr, fl4->saddr);
> if (sk->sk_prot->rehash)
> sk->sk_prot->rehash(sk);
> }
>
> Suppose the victim is narrowed to one specific address and the attacker
> binds a different one. inet_rcv_saddr_equal() returns false before
> udp_bound_dev_overlap() runs, so the bind is admitted. In
> udp_lib_lport_inuse2(), the rehashed victim is not in any of the hslot2
> buckets that get scanned.
>
> Later the victim calls connect(AF_UNSPEC). A wildcard bind never sets
> SOCK_BINDADDR_LOCK, so __udp_disconnect() calls inet_reset_saddr() and
> the victim goes back to the wildcard address. The attacker's more
> specific socket then wins the lookup.
>
> This IPv4 sequence needs neither a link-local peer nor SO_BINDTODEVICE:
>
> victim: bind(0.0.0.0:P), connect(8.8.8.8:53) /* narrows to 192.168.1.5 */
> attacker: bind(127.0.0.1:P) /* admitted */
> victim: connect(AF_UNSPEC)
>
> After this, the attacker receives the victim's loopback traffic to
> port P. The commit's own example also looks exploitable if the attacker
> binds [2001:db8::a]:P instead of [::]:P.
>
> The notes after the commit message mention this window and leave it for
> a separate patch, and nothing later in the series addresses it. The
> commit message says the patch stops an unprivileged user from binding a
> port that another user's socket holds, and it is tagged for stable.
> Should the address case be fixed together with the device case?
>
> [ ... ]
>
> > @@ -194,13 +219,12 @@ static int udp_lib_lport_inuse2(struct net *net, __u16 num,
> > sk2 != sk &&
> > (udp_sk(sk2)->udp_port_hash == num) &&
> > (!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)) {
> > + inet_rcv_saddr_equal(sk, sk2, true) &&
> > + udp_bound_dev_overlap(dif, sk2)) {
>
> [Severity: Medium]
> This converts the UDP conflict checks. Does the L2TP/IPv6 bind check
> still trust a device that connect() set?
>
> l2tp_ip6_connect() calls __ip6_datagram_connect(). On an L2TP/IPv6
> socket bound with no device, ip6_datagram_connect_dev() therefore sets
> both sk_bound_dev_if and SOCK_CONNECT_DEV. __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;
> ...
> }
>
> Take the same sequence as in the commit message:
>
> victim: bind([::], conn_id X), connect(fe80::1%eth0)
> attacker: SO_BINDTODEVICE(eth1), bind([::], conn_id X)
> victim: connect(AF_UNSPEC)
>
> l2tp_ip6_bind() skips the victim (eth0 vs eth1) and admits the
> attacker. Then l2tp_ip6_disconnect()->__udp_disconnect() clears the
> victim's device because SOCK_CONNECT_DEV is set. The victim now holds
> conn_id X on every device, while the attacker's socket is still in
> l2tp_ip6_bind_table.
>
> sk_add_bind_node() added the attacker's socket at the head of that
> table. So l2tp_ip6_recv() would match it first for control frames from
> fe80::1 that arrive on eth1 for tunnel X.
>
> The previous patch in the series lists L2TP as a __udp_disconnect()
> user, and nothing later in the series changes this lookup. Should
> __l2tp_ip6_bind_lookup() also ignore a device that connect() set?
>
> [ ... ]
>
> > diff --git a/net/ipv6/datagram.c b/net/ipv6/datagram.c
> > index 0296207dbeb0b..37a093f74724e 100644
> > --- a/net/ipv6/datagram.c
> > +++ b/net/ipv6/datagram.c
> > @@ -141,9 +141,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: Medium]
> For a socket bound to a VRF master, can udp_bound_dev_if() still return
> the slave that connect() picked instead of the device bind() reserved?
>
> In __ip6_datagram_connect(), sk_dev_equal_l3scope() accepts a slave0
> scope id on a socket bound to vrf0. ip6_datagram_connect_dev(sk, vrf0,
> slave0) then writes sk_bound_dev_if = slave0. It does not set
> SOCK_CONNECT_DEV, because old_dif is non-zero.
>
> udp_bound_dev_if() then returns slave0 for that socket.
> udp_bound_dev_overlap() and udp_lport_same_bind() treat slave0 as the
> reserved device.
>
> For example, user A binds vrf0 X:P and connects to fe80::1%slave0.
> Another user then binds vrf0 X:P. udp_bound_dev_overlap(vrf0, A)
> compares vrf0 with slave0, finds no overlap, and admits the bind, even
> though A reserved vrf0.
>
> With the earlier patch in this series, __udp_disconnect() keeps slave0
> because the flag is clear. A never returns to vrf0, and the other user
> alone receives the port's traffic on the other slaves of vrf0.
>
> The early return for reuseport can go wrong the same way. Say B is
> owned by A's user, uses SO_REUSEPORT and is bound to slave0, and its
> scan reaches A first. Then udp_lport_same_bind(B, slave0, A) is true,
> and udp_lib_lport_inuse() returns 0 before it sees socket C. C belongs
> to another user and is bound to slave0 X:P.
>
> The commit message says:
>
> A VRF master that connect() narrowed to one of its interfaces does not
> get the flag and is compared as before.
>
> Is it safe to compare as before here? Would recording the bind-time
> device, instead of using a flag, also cover this case?
>
> --
> Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261120174341.35873-1-juanagusnicco%40gmail.com
^ permalink raw reply [flat|nested] 9+ messages in thread
* [PATCH net 4/4] selftests: net: Add UDP bind conflict tests
2026-11-20 17:43 [PATCH net 0/4] udp: Fix bind conflict check bypasses Juan Agustin Niccolini
` (2 preceding siblings ...)
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-20 17:43 ` Juan Agustin Niccolini
3 siblings, 0 replies; 9+ messages in thread
From: Juan Agustin Niccolini @ 2026-11-20 17:43 UTC (permalink / raw)
To: David S . Miller, Eric Dumazet, Jakub Kicinski, Paolo Abeni
Cc: Simon Horman, Kuniyuki Iwashima, Willem de Bruijn,
Willem de Bruijn, Eric Garver, Shuah Khan, linux-kselftest,
netdev, linux-kernel
Add tests for the three bind conflict fixes in this series:
- reuseport_scan_{address,device,hash2}: a SO_REUSEPORT bind is refused
when a conflicting socket sits behind a compatible one, on the primary
and on the secondary hash.
- disconnect_keeps_{device,scoped_device}: connect(AF_UNSPEC) keeps a
device set with SO_BINDTOIFINDEX or with the scope id of bind().
- connect_device_ignored: a device set by connect() does not let a
socket on another device bind next to it.
reuseport_share and disconnect_clears_connect_device check that the
sharing and the clearing that should still happen still do.
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@gmail.com>
---
tools/testing/selftests/net/.gitignore | 1 +
tools/testing/selftests/net/Makefile | 1 +
.../testing/selftests/net/udp_bind_conflict.c | 228 ++++++++++++++++++
3 files changed, 230 insertions(+)
create mode 100644 tools/testing/selftests/net/udp_bind_conflict.c
diff --git a/tools/testing/selftests/net/.gitignore b/tools/testing/selftests/net/.gitignore
index 21412510ac26..35f6d5220156 100644
--- a/tools/testing/selftests/net/.gitignore
+++ b/tools/testing/selftests/net/.gitignore
@@ -56,6 +56,7 @@ tools
tun
txring_overwrite
txtimestamp
+udp_bind_conflict
udp_splice_checksum
udpgso
udpgso_bench_rx
diff --git a/tools/testing/selftests/net/Makefile b/tools/testing/selftests/net/Makefile
index 9180b9bec515..16b804559051 100644
--- a/tools/testing/selftests/net/Makefile
+++ b/tools/testing/selftests/net/Makefile
@@ -202,6 +202,7 @@ TEST_GEN_PROGS := \
tap \
tcp_port_share \
tls \
+ udp_bind_conflict \
udp_splice_checksum \
# end of TEST_GEN_PROGS
diff --git a/tools/testing/selftests/net/udp_bind_conflict.c b/tools/testing/selftests/net/udp_bind_conflict.c
new file mode 100644
index 000000000000..9a67f58c348a
--- /dev/null
+++ b/tools/testing/selftests/net/udp_bind_conflict.c
@@ -0,0 +1,228 @@
+// SPDX-License-Identifier: GPL-2.0
+/* Check that bind() refuses a UDP socket that overlaps another one in
+ * the cases that SO_REUSEPORT, disconnect and connect() used to let
+ * through.
+ */
+
+#include <arpa/inet.h>
+#include <net/if.h>
+#include <sched.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "kselftest_harness.h"
+
+#define PORT 34567
+#define MAX_FDS 32
+
+FIXTURE(udp_bind_conflict)
+{
+ int fd[MAX_FDS];
+ int nr_fds;
+ int lo;
+ int d0;
+};
+
+FIXTURE_SETUP(udp_bind_conflict)
+{
+ ASSERT_EQ(unshare(CLONE_NEWNET), 0);
+ ASSERT_EQ(system("ip link set dev lo up"), 0);
+ ASSERT_EQ(system("ip link add d0 type dummy"), 0);
+ ASSERT_EQ(system("ip link set dev d0 up"), 0);
+ ASSERT_EQ(system("ip addr add fe80::1/64 dev d0 nodad"), 0);
+
+ self->lo = if_nametoindex("lo");
+ self->d0 = if_nametoindex("d0");
+ ASSERT_NE(self->lo, 0);
+ ASSERT_NE(self->d0, 0);
+ self->nr_fds = 0;
+}
+
+FIXTURE_TEARDOWN(udp_bind_conflict)
+{
+ while (self->nr_fds)
+ close(self->fd[--self->nr_fds]);
+}
+
+/* Bind a UDP socket to addr:PORT, on device @ifindex if not 0: through
+ * the scope id for a link-local address, SO_BINDTOIFINDEX otherwise.
+ * Returns the socket, or -errno.
+ */
+static int udp_bind(FIXTURE_DATA(udp_bind_conflict) *self, const char *addr,
+ int ifindex, bool reuseport)
+{
+ union {
+ struct sockaddr sa;
+ struct sockaddr_in v4;
+ struct sockaddr_in6 v6;
+ } sa = {};
+ socklen_t len;
+ int one = 1;
+ int fd, err;
+
+ if (self->nr_fds == MAX_FDS)
+ return -ENOSPC;
+
+ if (inet_pton(AF_INET, addr, &sa.v4.sin_addr) == 1) {
+ sa.v4.sin_family = AF_INET;
+ sa.v4.sin_port = htons(PORT);
+ len = sizeof(sa.v4);
+ } else if (inet_pton(AF_INET6, addr, &sa.v6.sin6_addr) == 1) {
+ sa.v6.sin6_family = AF_INET6;
+ sa.v6.sin6_port = htons(PORT);
+ len = sizeof(sa.v6);
+ if (IN6_IS_ADDR_LINKLOCAL(&sa.v6.sin6_addr)) {
+ sa.v6.sin6_scope_id = ifindex;
+ ifindex = 0;
+ }
+ } else {
+ return -EINVAL;
+ }
+
+ fd = socket(sa.sa.sa_family, SOCK_DGRAM, 0);
+ if (fd < 0)
+ return -errno;
+
+ if ((reuseport &&
+ setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one))) ||
+ (ifindex &&
+ setsockopt(fd, SOL_SOCKET, SO_BINDTOIFINDEX,
+ &ifindex, sizeof(ifindex))) ||
+ bind(fd, &sa.sa, len)) {
+ err = -errno;
+ close(fd);
+ return err;
+ }
+
+ self->fd[self->nr_fds++] = fd;
+ return fd;
+}
+
+static int disconnect(int fd)
+{
+ struct sockaddr sa = { .sa_family = AF_UNSPEC };
+
+ return connect(fd, &sa, sizeof(sa));
+}
+
+/* connect() to a link-local peer, which sets the device to @ifindex */
+static int connect_link_local(int fd, int ifindex)
+{
+ struct sockaddr_in6 sa = {
+ .sin6_family = AF_INET6,
+ .sin6_port = htons(PORT),
+ .sin6_scope_id = ifindex,
+ };
+
+ inet_pton(AF_INET6, "fe80::2", &sa.sin6_addr);
+ return connect(fd, (struct sockaddr *)&sa, sizeof(sa));
+}
+
+static int bound_ifindex(int fd)
+{
+ socklen_t len = sizeof(int);
+ int ifindex;
+
+ if (getsockopt(fd, SOL_SOCKET, SO_BINDTOIFINDEX, &ifindex, &len))
+ return -errno;
+ return ifindex;
+}
+
+/* The conflict scan used to stop at the first SO_REUSEPORT socket of the
+ * same owner that overlapped the new one, before reaching 127.0.0.2.
+ */
+TEST_F(udp_bind_conflict, reuseport_scan_address)
+{
+ ASSERT_LE(0, udp_bind(self, "127.0.0.2", 0, false));
+ ASSERT_LE(0, udp_bind(self, "127.0.0.3", 0, true));
+ ASSERT_EQ(-EADDRINUSE, udp_bind(self, "0.0.0.0", 0, true));
+}
+
+TEST_F(udp_bind_conflict, reuseport_scan_device)
+{
+ ASSERT_LE(0, udp_bind(self, "0.0.0.0", self->lo, false));
+ ASSERT_LE(0, udp_bind(self, "0.0.0.0", self->d0, true));
+ ASSERT_EQ(-EADDRINUSE, udp_bind(self, "0.0.0.0", 0, true));
+}
+
+/* More than 10 sockets on the port: the secondary hash is scanned. */
+TEST_F(udp_bind_conflict, reuseport_scan_hash2)
+{
+ char addr[INET_ADDRSTRLEN];
+ int i;
+
+ ASSERT_LE(0, udp_bind(self, "127.0.0.2", self->lo, false));
+ for (i = 3; i < 14; i++) {
+ snprintf(addr, sizeof(addr), "127.0.0.%d", i);
+ ASSERT_LE(0, udp_bind(self, addr, 0, true));
+ }
+ ASSERT_LE(0, udp_bind(self, "127.0.0.2", self->d0, true));
+ ASSERT_EQ(-EADDRINUSE, udp_bind(self, "127.0.0.2", 0, true));
+}
+
+/* Sockets of one owner that SO_REUSEPORT lets share a port still can. */
+TEST_F(udp_bind_conflict, reuseport_share)
+{
+ int i;
+
+ for (i = 0; i < 16; i++)
+ ASSERT_LE(0, udp_bind(self, "127.0.0.1", 0, true));
+ ASSERT_LE(0, udp_bind(self, "0.0.0.0", 0, true));
+ ASSERT_LE(0, udp_bind(self, "0.0.0.0", self->lo, true));
+ ASSERT_LE(0, udp_bind(self, "127.0.0.1", self->d0, true));
+}
+
+/* Disconnect used to clear the device the socket was bound to, leaving it
+ * on top of 0.0.0.0%lo where bind() had refused it.
+ */
+TEST_F(udp_bind_conflict, disconnect_keeps_device)
+{
+ int fd;
+
+ ASSERT_LE(0, udp_bind(self, "0.0.0.0", self->lo, false));
+ ASSERT_EQ(-EADDRINUSE, udp_bind(self, "127.0.0.1", 0, false));
+
+ fd = udp_bind(self, "127.0.0.1", self->d0, false);
+ ASSERT_LE(0, fd);
+ ASSERT_EQ(0, disconnect(fd));
+ ASSERT_EQ(self->d0, bound_ifindex(fd));
+}
+
+TEST_F(udp_bind_conflict, disconnect_keeps_scoped_device)
+{
+ int fd;
+
+ fd = udp_bind(self, "fe80::1", self->d0, false);
+ ASSERT_LE(0, fd);
+ ASSERT_EQ(0, disconnect(fd));
+ ASSERT_EQ(self->d0, bound_ifindex(fd));
+}
+
+/* A device that connect() set is still cleared by disconnect. */
+TEST_F(udp_bind_conflict, disconnect_clears_connect_device)
+{
+ int fd;
+
+ fd = udp_bind(self, "::", 0, false);
+ ASSERT_LE(0, fd);
+ ASSERT_EQ(0, connect_link_local(fd, self->d0));
+ ASSERT_EQ(self->d0, bound_ifindex(fd));
+ ASSERT_EQ(0, disconnect(fd));
+ ASSERT_EQ(0, bound_ifindex(fd));
+}
+
+/* A device that connect() set used to count in the conflict check, so a
+ * socket on another device could bind next to [::]:PORT and overlap it
+ * once it disconnected.
+ */
+TEST_F(udp_bind_conflict, connect_device_ignored)
+{
+ int fd;
+
+ fd = udp_bind(self, "::", 0, false);
+ ASSERT_LE(0, fd);
+ ASSERT_EQ(0, connect_link_local(fd, self->d0));
+ ASSERT_EQ(-EADDRINUSE, udp_bind(self, "::", self->lo, false));
+}
+
+TEST_HARNESS_MAIN
--
2.43.0
^ permalink raw reply [flat|nested] 9+ messages in thread