* [PATCH net] udp: only quit inuse scan early on identical SO_REUSEPORT sockets
@ 2026-11-19 19:21 Dominik Maier
0 siblings, 0 replies; only message in thread
From: Dominik Maier @ 2026-11-19 19:21 UTC (permalink / raw)
To: Willem de Bruijn
Cc: Eric Dumazet, Kuniyuki Iwashima, David S. Miller, Jakub Kicinski,
netdev, dmnk+artist, Dominik Maier
Commit df560056d960 ("udp: inuse checks can quit early for reuseport")
optimized udp_lib_lport_inuse() and udp_lib_lport_inuse2() to return 0
immediately upon encountering the first same-UID SO_REUSEPORT socket
that matches under wildcard conflict rules.
However, a socket that matches under wildcard rules (or shares only a
relaxed subset of bind attributes) does not guarantee that the remaining
sockets in hslot->head or hslot2->head are conflict-free for the new
socket. Because IPv4 UDP sockets prepend to the head of the hash chain,
an unprivileged local user can bind a non-conflicting SO_REUSEPORT
socket (e.g., 127.0.0.2:PORT) ahead of another user's exclusive
127.0.0.1:PORT UDP listener, then bind 0.0.0.0:PORT (returning early on
127.0.0.2:PORT without reaching the exclusive listener), and finally
bind 127.0.0.1:PORT at the head of hslot2->head to intercept cross-UID
UDP datagrams.
Only take the O(1) early exit when sk and sk2 share the exact same
conflict domain (sk_family, ipv6_only_sock(), sk_bound_dev_if,
sk_reuse, and exact receive address via inet_rcv_saddr_equal() with
match_wildcard=false), and continue scanning the hash chain otherwise.
This preserves O(1) bind scaling for identical SO_REUSEPORT groups while
preventing cross-UID conflict checks from being skipped.
This issue was discovered with an AI-assisted code review tool (ARTIST)
and reproduced in QEMU/KVM on an unmodified v7.4-rc3 kernel without
kernel modifications or error injection; a standalone reproducer is
available on request. This revised exact-match fastpath patch was
compile-tested with -Werror and checked with checkpatch.pl --strict; it
has not yet been runtime-tested against the reproducer or kernel
selftests.
Fixes: df560056d960 ("udp: inuse checks can quit early for reuseport")
Cc: stable@vger.kernel.org
Signed-off-by: Dominik Maier <dmnk@google.com>
---
net/ipv4/udp.c | 25 ++++++++++++++++++++-----
1 file changed, 20 insertions(+), 5 deletions(-)
diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c
index 2e5562f..d784faf 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_sk_reuseport_match(struct sock *sk, 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_sk_reuseport_match(sk, sk2))
+ return 0;
+ continue;
+ }
return 1;
}
__set_bit(udp_sk(sk2)->udp_port_hash >> log, bitmap);
@@ -188,10 +200,13 @@ 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))) {
- res = 0;
- } else {
- res = 1;
+ if (udp_sk_reuseport_match(sk, sk2)) {
+ res = 0;
+ break;
+ }
+ continue;
}
+ res = 1;
break;
}
}
--
2.56.1.750.gf70dc682cf-goog
^ permalink raw reply related [flat|nested] only message in thread
only message in thread, other threads:[~2026-11-19 19:21 UTC | newest]
Thread overview: (only message) (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-11-19 19:21 [PATCH net] udp: only quit inuse scan early on identical SO_REUSEPORT sockets Dominik Maier
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox