All of lore.kernel.org
 help / color / mirror / Atom feed
From: Juan Agustin Niccolini <juanagusnicco@gmail.com>
To: "David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@kernel.org>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>
Cc: Simon Horman <horms@kernel.org>,
	Kuniyuki Iwashima <kuniyu@google.com>,
	Willem de Bruijn <willemdebruijn.kernel@gmail.com>,
	Willem de Bruijn <willemb@google.com>, Eric Garver <e@erig.me>,
	Shuah Khan <shuah@kernel.org>,
	linux-kselftest@vger.kernel.org, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org
Subject: [PATCH net 1/4] udp: Fix bind conflict check stopping at the first reuseport socket
Date: Fri, 20 Nov 2026 12:43:38 -0500	[thread overview]
Message-ID: <20261120174341.35873-2-juanagusnicco@gmail.com> (raw)
In-Reply-To: <20261120174341.35873-1-juanagusnicco@gmail.com>

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


  reply	other threads:[~2026-11-20 17:43 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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
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
2026-11-20 17:43 ` [PATCH net 4/4] selftests: net: Add UDP bind conflict tests Juan Agustin Niccolini
2026-11-29 14:39 ` [PATCH net 0/4] udp: Fix bind conflict check bypasses willemb

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=20261120174341.35873-2-juanagusnicco@gmail.com \
    --to=juanagusnicco@gmail.com \
    --cc=davem@davemloft.net \
    --cc=e@erig.me \
    --cc=edumazet@kernel.org \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --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=willemb@google.com \
    --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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.