Thread View: linux.kernel.netdev
2 messages
2 total messages
Started by Juan Agustin Niccolini
Fri, 20 Nov 2026 17:43
[PATCH net 0/4] udp: Fix bind conflict check bypasses
Author: Juan Agustin Niccolini
Date: Fri, 20 Nov 2026 17:43
Date: Fri, 20 Nov 2026 17:43
32 lines
12179 bytes
12179 bytes
An unprivileged user can get a UDP socket bound to an address and port
that another user's socket holds, in a state bind() would have refused,
and receive that socket's datagrams. This series fixes three bugs that
allow that and adds a selftest.
These are sibling vulnerabilities of the TCP one fixed by my
commit f675070191f2 ("tcp: Fix the SO_REUSEPORT bind cache admitting a
conflicting socket"). The one in patch 1 has the same kind of cause: a
SO_REUSEPORT shortcut that lets one matching socket stand for every
socket on the port. The other two predate git. All were discovered
during an LLM-based audit.
Juan Agustin Niccolini (4):
udp: Fix bind conflict check stopping at the first reuseport socket
udp: Keep the bound device on disconnect unless connect() set it
udp: Ignore a connect()-set device in the bind conflict check
selftests: net: Add UDP bind conflict tests
include/net/sock.h | 4 +
net/core/sock.c | 3 +
net/ipv4/udp.c | 69 +++++-
net/ipv6/datagram.c | 25 +-
tools/testing/selftests/net/.gitignore | 1 +
tools/testing/selftests/net/Makefile | 1 +
.../testing/selftests/net/udp_bind_conflict.c | 228 ++++++++++++++++++
7 files changed, 317 insertions(+), 14 deletions(-)
base-commit: 19a227903e65c7a9c179156485660a4e017575fe
--
2.43.0
[PATCH net 4/4] selftests: net: Add UDP bind conflict tests
Author: Juan Agustin Niccolini
Date: Fri, 20 Nov 2026 17:43
Date: Fri, 20 Nov 2026 17:43
282 lines
19258 bytes
19258 bytes
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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads