Netdev List
 help / color / mirror / Atom feed
From: Dominik Maier <dmnk@google.com>
To: Willem de Bruijn <willemdebruijn.kernel@gmail.com>
Cc: Eric Dumazet <edumazet@kernel.org>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	 "David S. Miller" <davem@davemloft.net>,
	Jakub Kicinski <kuba@kernel.org>,
	netdev@vger.kernel.org,  dmnk+artist@google.com,
	Dominik Maier <dmnk@google.com>
Subject: [PATCH net] udp: only quit inuse scan early on identical SO_REUSEPORT sockets
Date: Thu, 19 Nov 2026 20:21:42 +0100	[thread overview]
Message-ID: <20261119192142.2637952-1-dmnk@google.com> (raw)

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


                 reply	other threads:[~2026-11-19 19:21 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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=20261119192142.2637952-1-dmnk@google.com \
    --to=dmnk@google.com \
    --cc=davem@davemloft.net \
    --cc=dmnk+artist@google.com \
    --cc=edumazet@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=netdev@vger.kernel.org \
    --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