[PATCH net 1/4] udp: Fix bind conflict check stopping at the first reuseport socket

From: Juan Agustin Niccolini

Date: Fri Nov 20 2026 - 12:43:58 EST


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@xxxxxxxxxxxxxxx
Signed-off-by: Juan Agustin Niccolini <juanagusnicco@xxxxxxxxx>
---
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