Skip to contentSkip to conversation

udp: fix connected reuseport rescore fallback

PATCH NAK Zihan Xi 4 participants 9 messages Nov 16 – Nov 19

Subsystems (5)
lore
Share & tools
lore

Zihan Xi

author
#0 lore raw

cover udp: fix connected reuseport rescore fallback

Hi Linux kernel maintainers,

We found a UDP socket misdelivery issue in the IPv4 and IPv6 reuseport lookup paths, in net/ipv4/udp.c and net/ipv6/udp.c. The issue affects local users able to create SO_REUSEPORT sockets and attach a classic reuseport BPF selector; no capability or namespace setup is required by the trigger itself.

---- details below ----

An LLM was used to help create this patch.

This bug is tracked at:
https://bugtracker.nebusec.ai/f/net/ipv6/udp.c||udp6_lib_lookup2||socket-hijack||m7q
(net/ipv6/udp.c||udp6_lib_lookup2||socket-hijack||m7q)

Bug details:

When a SO_REUSEPORT group contains connected sockets, udp*_lib_lookup2() rescored the socket selected by the reuseport program. If the selected socket did not match the packet, the failed score was ignored and the stale socket remained as the lookup result. The packet could then be delivered to a socket connected to a different peer.

The fix retains the BPF-selected socket only when its rescore is better than the current candidate; otherwise it restores the candidate that established the current score. The same correction is applied to IPv4 and IPv6. The unconnected/no-connected-group fast path is unchanged, and a selected connected socket that matches the packet still wins.

Reproducer:

Save the PoC below as poc.c, then build and run it on a pre-fix kernel:

gcc -O2 -Wall -Wextra -o poc poc.c
./poc

The reproducer uses loopback UDP sockets, a connected SO_REUSEPORT member, and a classic BPF selector fixed to socket index 1. Packetdrill is not a suitable harness for this case because the trigger depends on creating and connecting sockets and installing a SO_ATTACH_REUSEPORT_CBPF program; it does not depend on a special wire-level packet sequence. The source is included below.

The same mismatch and positive-match cases are covered by the new test_connected_reuseport_rescore() selftest in patch 2. The selftest harness creates a network namespace, so run it as root:

make -C tools/testing/selftests/net
tools/testing/selftests/net/reuseport_bpf

The selftest ran in a 2 vCPU, 2 GB x86-64 QEMU guest. Its test harness creates a network namespace and was run as root there; the standalone trigger itself was also exercised as an unprivileged user. On the pre-fix kernel, the IPv4 and IPv6 mismatch probes both failed at the unexpected socket assertion. On the fixed kernel, the full selftest exited 0 and printed SUCCESS; the standalone PoC did not reproduce misdelivery in four unprivileged guest runs.

------BEGIN PoC------

#define _GNU_SOURCE

#include <arpa/inet.h>
#include <errno.h>
#include <linux/filter.h>
#include <netinet/in.h>
#include <poll.h>
#include <sched.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <unistd.h>

#ifndef SO_ATTACH_REUSEPORT_CBPF
#define SO_ATTACH_REUSEPORT_CBPF 51
#endif

#define PAYLOAD "udp6-lib-lookup2-stale-result"
#define CONNECTED_PEER_PORT 40000
#define TEST_SPORT 12345

static void die(const char *what)

{
	perror(what);
	exit(1);
}

static void set_reuseport(int fd)

{
	int one = 1;

	if (setsockopt(fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0)
		die("setsockopt(SO_REUSEPORT)");
}

static int make_udp6_socket(void)

{
	int fd = socket(AF_INET6, SOCK_DGRAM, 0);

	if (fd < 0)
		die("socket(AF_INET6)");
	return fd;
}

static void bind_udp6(int fd, const struct in6_addr *addr, uint16_t port)

{
	struct sockaddr_in6 sa = {
		.sin6_family = AF_INET6,
		.sin6_port = htons(port),
		.sin6_addr = *addr,
	};

	if (bind(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
		die("bind(AF_INET6)");
}

static uint16_t get_bound_port(int fd)

{
	struct sockaddr_in6 sa;
	socklen_t salen = sizeof(sa);

	memset(&sa, 0, sizeof(sa));
	if (getsockname(fd, (struct sockaddr *)&sa, &salen) < 0)
		die("getsockname");
	return ntohs(sa.sin6_port);
}

static void connect_udp6(int fd, const struct in6_addr *addr, uint16_t port)

{
	struct sockaddr_in6 sa = {
		.sin6_family = AF_INET6,
		.sin6_port = htons(port),
		.sin6_addr = *addr,
	};

	if (connect(fd, (struct sockaddr *)&sa, sizeof(sa)) < 0)
		die("connect(AF_INET6)");
}

static void attach_constant_reuseport_cbpf(int fd, unsigned int index)

{
	struct sock_filter filter[] = {
		BPF_STMT(BPF_RET | BPF_K, index),
	};
	struct sock_fprog prog = {
		.len = (unsigned short)(sizeof(filter) / sizeof(filter[0])),
		.filter = filter,
	};

	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF,
		       &prog, sizeof(prog)) < 0)
		die("setsockopt(SO_ATTACH_REUSEPORT_CBPF)");
}

static void pin_cpu0(void)

{
	cpu_set_t set;

	CPU_ZERO(&set);
	CPU_SET(0, &set);
	if (sched_setaffinity(0, sizeof(set), &set) < 0)
		die("sched_setaffinity");
}

static bool recv_one(int fd, const char *name, uint16_t *sport_out)

{
	char buf[256];
	struct sockaddr_in6 peer;
	struct msghdr msg;
	struct iovec iov;
	ssize_t n;
	socklen_t salen = sizeof(peer);

	memset(&peer, 0, sizeof(peer));
	memset(&msg, 0, sizeof(msg));
	iov.iov_base = buf;
	iov.iov_len = sizeof(buf);
	msg.msg_name = &peer;
	msg.msg_namelen = salen;
	msg.msg_iov = &iov;
	msg.msg_iovlen = 1;

	n = recvmsg(fd, &msg, MSG_DONTWAIT);
	if (n < 0) {
		if (errno == EAGAIN || errno == EWOULDBLOCK)
			return false;
		die("recvmsg");
	}

	*sport_out = ntohs(peer.sin6_port);
	printf("%s received %zd bytes from sport=%u\n", name, n, *sport_out);
	return true;
}

int main(void)

{
	const struct in6_addr loop = IN6ADDR_LOOPBACK_INIT;
	struct sockaddr_in6 dst = {
		.sin6_family = AF_INET6,
		.sin6_port = 0,
		.sin6_addr = IN6ADDR_LOOPBACK_INIT,
	};
	const char payload[] = PAYLOAD;
	struct pollfd pfds[2];
	uint16_t dport;
	uint16_t bad_sport = 0;
	uint16_t good_sport = 0;
	ssize_t n;
	int good_fd;
	int bad_fd;
	int send_fd;

	pin_cpu0();

	good_fd = make_udp6_socket();
	set_reuseport(good_fd);
	bind_udp6(good_fd, &loop, 0);
	dport = get_bound_port(good_fd);

	bad_fd = make_udp6_socket();
	set_reuseport(bad_fd);
	bind_udp6(bad_fd, &loop, dport);
	connect_udp6(bad_fd, &loop, CONNECTED_PEER_PORT);

	attach_constant_reuseport_cbpf(good_fd, 1);

	send_fd = make_udp6_socket();
	bind_udp6(send_fd, &loop, TEST_SPORT);
	dst.sin6_port = htons(dport);

	printf("group port=%u, connected peer port=%u, sender sport=%u\n",
	       dport, CONNECTED_PEER_PORT, TEST_SPORT);

	n = sendto(send_fd, payload, sizeof(payload), 0,
		   (struct sockaddr *)&dst, sizeof(dst));
	if (n < 0)
		die("sendto");

	pfds[0].fd = good_fd;
	pfds[0].events = POLLIN;
	pfds[1].fd = bad_fd;
	pfds[1].events = POLLIN;

	if (poll(pfds, 2, 1000) < 0)
		die("poll");

	(void)recv_one(good_fd, "good", &good_sport);
	(void)recv_one(bad_fd, "bad-connected", &bad_sport);

	if (bad_sport == TEST_SPORT && good_sport == 0) {
		printf("BUG: packet from sport=%u was delivered to the socket connected to %u\n",
		       bad_sport, CONNECTED_PEER_PORT);
		return 0;
	}

	fprintf(stderr, "PoC did not observe the stale-result misdelivery\n");
	return 2;
}

------END PoC--------

This is a logic issue and does not crash the kernel; no crash log is available.

Best regards,
Zihan Xi

Zihan Xi (2):
udp: fix reuseport rescore fallback
selftests: net: cover connected UDP reuseport rescore

 net/ipv4/udp.c                              |  11 +-
 net/ipv6/udp.c                              |  11 +-
 tools/testing/selftests/net/reuseport_bpf.c | 150 ++++++++++++++++++++
 3 files changed, 166 insertions(+), 6 deletions(-)
Signature · 1 line
2.43.0
author
#1 lore raw

PATCH 1/2 udp: fix reuseport rescore fallback

When a reuseport group contains connected sockets, UDP lookup rescored the socket selected by the reuseport program. If that socket did not match the packet, lookup retained the stale selection while continuing the scan. This could deliver a packet to a socket connected to a different peer.

Restore the candidate that established the current score when the rescored socket is not a better match. Keep the selected socket only when its score is higher, and apply the same logic to IPv4 and IPv6 lookups.

---
 net/ipv4/udp.c | 11 ++++++++---
 net/ipv6/udp.c | 11 ++++++++---
 2 files changed, 16 insertions(+), 6 deletions(-)
net/ipv4/udp.c +8 -3
index 2e5562f353f8..23a8c951559c 100644
@@ -475,12 +475,17 @@ static struct sock *udp4_lib_lookup2(const struct net *net,
 rescore:
 		score = compute_score(need_rescore ? result : sk, net, saddr,
 				      sport, daddr, hnum, dif, sdif);
+		if (need_rescore) {
+			if (score > badness)
+				badness = score;
+			else
+				result = sk;
+			continue;
+		}
+
 		if (score > badness) {
 			badness = score;
 
-			if (need_rescore)
-				continue;
-
 			if (sk->sk_state == TCP_ESTABLISHED) {
 				result = sk;
 				continue;
net/ipv6/udp.c +8 -3
index 9dcefa162522..c5adeaaf5b42 100644
@@ -234,12 +234,17 @@ static struct sock *udp6_lib_lookup2(const struct net *net,
 rescore:
 		score = compute_score(need_rescore ? result : sk, net, saddr,
 				      sport, daddr, hnum, dif, sdif);
+		if (need_rescore) {
+			if (score > badness)
+				badness = score;
+			else
+				result = sk;
+			continue;
+		}
+
 		if (score > badness) {
 			badness = score;
 
-			if (need_rescore)
-				continue;
-
 			if (sk->sk_state == TCP_ESTABLISHED) {
 				result = sk;
 				continue;
Signature · 1 line
2.43.0
author
#6 lore raw

PATCH 2/2 selftests: net: cover connected UDP reuseport rescore

In reply to Zihan Xi

Test UDP SO_REUSEPORT lookup when a classic BPF program selects a connected socket. A packet from a different peer must fall back to the matching unconnected socket, while a packet from the connected peer must still select the connected socket.

Exercise both IPv4 and IPv6.

---
 tools/testing/selftests/net/reuseport_bpf.c | 150 ++++++++++++++++++++
 1 file changed, 150 insertions(+)
tools/testing/selftests/net/reuseport_bpf.c +150 -0
index 12e48b97b862..fbfe73dce211 100644
@@ -303,6 +303,154 @@ static void test_reuseport_cbpf(struct test_params p)
 		close(fd[i]);
 }
 
+static uint16_t get_sockaddr_port(const struct sockaddr *addr)
+{
+	if (addr->sa_family == AF_INET)
+		return ntohs(((const struct sockaddr_in *)addr)->sin_port);
+	return ntohs(((const struct sockaddr_in6 *)addr)->sin6_port);
+}
+
+static void attach_cbpf_index(int fd, uint32_t index)
+{
+	struct sock_filter code[] = {
+		{ BPF_RET | BPF_K, 0, 0, index },
+	};
+	struct sock_fprog p = {
+		.len = ARRAY_SIZE(code),
+		.filter = code,
+	};
+
+	if (setsockopt(fd, SOL_SOCKET, SO_ATTACH_REUSEPORT_CBPF, &p, sizeof(p)))
+		error(1, errno, "failed to set constant SO_REUSEPORT_CBPF");
+}
+
+static void send_udp_packet(int fd, struct sockaddr *dst, const char *payload)
+{
+	if (sendto(fd, payload, strlen(payload), 0, dst, sockaddr_size()) < 0)
+		error(1, errno, "failed to send UDP packet");
+}
+
+static void expect_udp_socket(int epfd, int good_fd, int connected_fd,
+			     int expected_fd, const char *payload)
+{
+	struct epoll_event ev;
+	char buf[32];
+	int n;
+
+	n = epoll_wait(epfd, &ev, 1, 1000);
+	if (n < 0)
+		error(1, errno, "epoll wait failed");
+	if (!n)
+		error(1, 0, "timed out waiting for UDP packet");
+	if (ev.data.fd != expected_fd)
+		error(1, 0, "UDP packet delivered to unexpected reuseport socket");
+
+	n = recv(ev.data.fd, buf, sizeof(buf), 0);
+	if (n < 0)
+		error(1, errno, "failed to receive UDP packet");
+	if ((size_t)n != strlen(payload) || memcmp(buf, payload, n))
+		error(1, 0, "received unexpected UDP payload");
+
+	/* No copy of the packet should be queued on the other socket. */
+	ev.events = EPOLLIN;
+	ev.data.fd = expected_fd == good_fd ? connected_fd : good_fd;
+	if (epoll_ctl(epfd, EPOLL_CTL_MOD, ev.data.fd, &ev))
+		error(1, errno, "failed to update epoll registration");
+	n = epoll_wait(epfd, &ev, 1, 0);
+	if (n < 0)
+		error(1, errno, "epoll wait failed");
+	if (n)
+		error(1, 0, "UDP packet was also queued on the other socket");
+}
+
+static void test_connected_reuseport_rescore(int family)
+{
+	static const char mismatch_payload[] = "mismatch";
+	static const char match_payload[] = "match";
+	struct sockaddr *good_addr, *peer_addr;
+	struct sockaddr *match_src, *other_src, *dst;
+	struct epoll_event ev;
+	int good_fd, connected_fd, match_send_fd, other_send_fd, epfd, one = 1;
+	uint16_t recv_port, peer_port;
+
+	fprintf(stderr, "Testing connected reuseport rescore, family %d...\n",
+		family);
+	good_fd = socket(family, SOCK_DGRAM, 0);
+	connected_fd = socket(family, SOCK_DGRAM, 0);
+	match_send_fd = socket(family, SOCK_DGRAM, 0);
+	other_send_fd = socket(family, SOCK_DGRAM, 0);
+	if (good_fd < 0 || connected_fd < 0 || match_send_fd < 0 ||
+	    other_send_fd < 0)
+		error(1, errno, "failed to create connected reuseport test sockets");
+	if (setsockopt(good_fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) ||
+	    setsockopt(connected_fd, SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)))
+		error(1, errno, "failed to set SO_REUSEPORT");
+
+	good_addr = new_loopback_sockaddr(family, 0);
+	if (bind(good_fd, good_addr, sockaddr_size()))
+		error(1, errno, "failed to bind good reuseport socket");
+	if (getsockname(good_fd, good_addr, (socklen_t []){ sockaddr_size() }))
+		error(1, errno, "failed to get reuseport port");
+	recv_port = get_sockaddr_port(good_addr);
+	if (bind(connected_fd, good_addr, sockaddr_size()))
+		error(1, errno, "failed to bind connected reuseport socket");
+
+	match_src = new_loopback_sockaddr(family, 0);
+	if (bind(match_send_fd, match_src, sockaddr_size()))
+		error(1, errno, "failed to bind matching source socket");
+	if (getsockname(match_send_fd, match_src,
+			(socklen_t []){ sockaddr_size() }))
+		error(1, errno, "failed to get matching source port");
+	peer_port = get_sockaddr_port(match_src);
+	peer_addr = new_loopback_sockaddr(family, peer_port);
+	if (connect(connected_fd, peer_addr, sockaddr_size()))
+		error(1, errno, "failed to connect reuseport socket");
+
+	/* Bind order makes good_fd index 0 and connected_fd index 1. */
+	attach_cbpf_index(good_fd, 1);
+	other_src = new_loopback_sockaddr(family, 0);
+	if (bind(other_send_fd, other_src, sockaddr_size()))
+		error(1, errno, "failed to bind non-matching source socket");
+	dst = new_loopback_sockaddr(family, recv_port);
+	if (getsockname(other_send_fd, other_src,
+			(socklen_t []){ sockaddr_size() }))
+		error(1, errno, "failed to get non-matching source port");
+	if (get_sockaddr_port(other_src) == peer_port)
+		error(1, 0, "source ports unexpectedly match");
+
+	epfd = epoll_create1(0);
+	if (epfd < 0)
+		error(1, errno, "failed to create epoll");
+	ev.events = EPOLLIN;
+	ev.data.fd = good_fd;
+	if (epoll_ctl(epfd, EPOLL_CTL_ADD, good_fd, &ev))
+		error(1, errno, "failed to register good socket");
+	ev.data.fd = connected_fd;
+	if (epoll_ctl(epfd, EPOLL_CTL_ADD, connected_fd, &ev))
+		error(1, errno, "failed to register connected socket");
+
+	/* An unmatched BPF-selected socket must not replace the valid candidate. */
+	send_udp_packet(other_send_fd, dst, mismatch_payload);
+	expect_udp_socket(epfd, good_fd, connected_fd, good_fd,
+			  mismatch_payload);
+
+	/* A matching selected socket must still win the positive rescore path. */
+	send_udp_packet(match_send_fd, dst, match_payload);
+	expect_udp_socket(epfd, good_fd, connected_fd, connected_fd,
+			  match_payload);
+
+	close(epfd);
+	close(good_fd);
+	close(connected_fd);
+	close(match_send_fd);
+	close(other_send_fd);
+	free(good_addr);
+	free(match_src);
+	free(other_src);
+	free(peer_addr);
+	free(dst);
+}
+
 static void test_extra_filter(const struct test_params p)
 {
 	struct sockaddr * const addr =
@@ -469,6 +617,7 @@ int main(void)
 	setup_netns();
 
 	fprintf(stderr, "---- IPv4 UDP ----\n");
+	test_connected_reuseport_rescore(AF_INET);
 	/* NOTE: UDP socket lookups traverse a different code path when there
 	 * are > 10 sockets in a group.  Run the bpf test through both paths.
 	 */
@@ -510,6 +659,7 @@ int main(void)
 		.recv_port = 8008});
 
 	fprintf(stderr, "---- IPv6 UDP ----\n");
+	test_connected_reuseport_rescore(AF_INET6);
 	test_reuseport_ebpf((struct test_params) {
 		.recv_family = AF_INET6,
 		.send_family = AF_INET6,
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH net 1/2] udp: fix reuseport rescore fallback

In reply to Zihan Xi
Quoted 4 lines · Zihan Xi

When a reuseport group contains connected sockets, UDP lookup rescored the socket selected by the reuseport program. If that socket did not match the packet, lookup retained the stale selection while continuing the scan. This could deliver a packet to a socket connected to a different peer.

Not since 6.13, unless CONFIG_BASE_SMALL=y.

Quoted 1 line

That's not a hijack.
udp_reuseport_add_sock() lets only sockets of one uid into a group. A member of the group attaches the prog.
The prog picks a wrong socket of its own user.

net/ipv4/udp.c static struct sock *udp4_lib_lookup2(const struct net *net, · line 484

Quoted 7 lines
net/ipv4/udp.c +7 -0
+		if (need_rescore) {
+			if (score > badness)
+				badness = score;
+			else
+				result = sk;
+			continue;
+		}

This makes it worse.
The unconnected sockets of a group have the same score. So the score of the picked socket is equal to badness, and the patch replaces the picked socket with sk.

Sockets A, B, C are not connected. A is the first one in hslot2. has_conns is set:

  reuseport picks   today   with the patch
  ---------------   -----   --------------
  A                 A       A
  B                 B       A
  C                 C       A

Nack.

author
#3 lore raw

Re: [PATCH net 1/2] udp: fix reuseport rescore fallback

Quoted 32 lines · Alexei Starovoitov
Quoted 4 lines · Zihan Xi

When a reuseport group contains connected sockets, UDP lookup rescored the socket selected by the reuseport program. If that socket did not match the packet, lookup retained the stale selection while continuing the scan. This could deliver a packet to a socket connected to a different peer.

Not since 6.13, unless CONFIG_BASE_SMALL=y.

Quoted 1 line

That's not a hijack.
udp_reuseport_add_sock() lets only sockets of one uid into a group. A member of the group attaches the prog.
The prog picks a wrong socket of its own user.

net/ipv4/udp.c static struct sock *udp4_lib_lookup2(const struct net *net, · line 484

Quoted 7 lines
net/ipv4/udp.c +7 -0
+             if (need_rescore) {
+                     if (score > badness)
+                             badness = score;
+                     else
+                             result = sk;
+                     continue;
+             }

This makes it worse.
The unconnected sockets of a group have the same score. So the score of the picked socket is equal to badness, and the patch replaces the picked socket with sk.

Sockets A, B, C are not connected. A is the first one in hslot2. has_conns is set:

Quoted 8 lines
  reuseport picks   today   with the patch
  ---------------   -----   --------------
  A                 A       A
  B                 B       A
  C                 C       A

Nack.

Thanks for the review. You are right on both points.

I missed the 4-tuple hash path added in 6.13, so this does not affect the normal configuration there; the lookup2 path remains relevant with CONFIG_BASE_SMALL=y.

The proposed score > badness check also changes equal-score behavior and can override a valid reuseport selection, as your example shows. I will withdraw this patch and the security framing rather than send the current series.

If I revisit the remaining CONFIG_BASE_SMALL behavior, I will first preserve valid equal-score selections and test both the mismatch fallback and equal-score reuseport cases.

This series is dropped

Thanks.

#4 lore raw

Re: [PATCH net 1/2] udp: fix reuseport rescore fallback

In reply to Zihan Xi
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] udp: score > badness check on rescore breaks SO_REUSEPORT selection --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] udp: score > badness check on rescore breaks SO_REUSEPORT selection
--

commit ab974582f7bf280a71fe95ae3563909536a788c1
Author: Zihan Xi <[email protected]>

udp: fix reuseport rescore fallback

This commit updates udp4_lib_lookup2() and udp6_lib_lookup2() to fall back to the current candidate socket sk when a socket selected from a reuseport group with connected sockets does not score higher on rescore.

Quoted 40 lines
net/ipv4/udp.c +8 -3
index 2e5562f353f85..23a8c951559c7 100644
@@ -475,12 +475,17 @@ static struct sock *udp4_lib_lookup2(const struct net *net,
 rescore:
 		score = compute_score(need_rescore ? result : sk, net, saddr,
 				      sport, daddr, hnum, dif, sdif);
+		if (need_rescore) {
+			if (score > badness)
+				badness = score;
+			else
+				result = sk;
+			continue;
+		}
+
 		if (score > badness) {
 			badness = score;
 
-			if (need_rescore)
-				continue;
-
 			if (sk->sk_state == TCP_ESTABLISHED) {
 				result = sk;
 				continue;
net/ipv6/udp.c +7 -0
index 9dcefa1625223..c5adeaaf5b428 100644
@@ -234,12 +234,17 @@ static struct sock *udp6_lib_lookup2(const struct net *net,
 rescore:
 		score = compute_score(need_rescore ? result : sk, net, saddr,
 				      sport, daddr, hnum, dif, sdif);
+		if (need_rescore) {
+			if (score > badness)
+				badness = score;
+			else
+				result = sk;
+			continue;
+		}

net/ipv6/udp.c static struct sock *udp6_lib_lookup2(const struct net *net, · line 250

Quoted 10 lines
net/ipv6/udp.c +1 -3
+
 		if (score > badness) {
 			badness = score;
 
-			if (need_rescore)
-				continue;
-
 			if (sk->sk_state == TCP_ESTABLISHED) {
 				result = sk;
 				continue;

[Severity: High]
Does checking score > badness when need_rescore is true discard the socket selected by reuseport whenever it has the same score as sk?

Before jumping to rescore, udp4_lib_lookup2() and udp6_lib_lookup2() already set badness = score for sk. When reuseport_has_conns(sk) is true and inet_lookup_reuseport() or inet6_lookup_reuseport() selects another unconnected socket in the group with an equal score, score == badness on the rescore pass. The strict score > badness check then takes the else branch (result = sk), regressing efc6b6f6c311 ("udp: Improve load balancing for SO_REUSEPORT.") by delivering all traffic for unconnected sockets in the group to the first matching socket in hslot2->head.

Checking score >= badness in the need_rescore block would keep result when it scores at least as well as sk while still falling back to sk when result scores lower.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#7 lore raw

Re: [PATCH net 2/2] selftests: net: cover connected UDP reuseport rescore

In reply to Zihan Xi
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] selftests: net: connect() in test_connected_reuseport_rescore() bypasses rescore --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] selftests: net: connect() in test_connected_reuseport_rescore() bypasses rescore
--

commit d5abe4da0e157c18e683e7d4c4cfb9dfd776e227
Author: Zihan Xi <[email protected]>

selftests: net: cover connected UDP reuseport rescore

This commit adds a selftest in reuseport_bpf to exercise UDP SO_REUSEPORT lookup when a cBPF program selects a connected socket over IPv4 and IPv6.

Quoted 4 lines
tools/testing/selftests/net/reuseport_bpf.c +0 -0
index 12e48b97b8625..fbfe73dce2111 100644

[ ... ]

Quoted 3 lines
tools/testing/selftests/net/reuseport_bpf.c +2 -0
@@ -303,6 +303,154 @@ static void test_reuseport_cbpf(struct test_params p)
+static void test_connected_reuseport_rescore(int family)
+{

[ ... ]

Quoted 22 lines
tools/testing/selftests/net/reuseport_bpf.c +22 -0
+	good_addr = new_loopback_sockaddr(family, 0);
+	if (bind(good_fd, good_addr, sockaddr_size()))
+		error(1, errno, "failed to bind good reuseport socket");
+	if (getsockname(good_fd, good_addr, (socklen_t []){ sockaddr_size() }))
+		error(1, errno, "failed to get reuseport port");
+	recv_port = get_sockaddr_port(good_addr);
+	if (bind(connected_fd, good_addr, sockaddr_size()))
+		error(1, errno, "failed to bind connected reuseport socket");
+
+	match_src = new_loopback_sockaddr(family, 0);
+	if (bind(match_send_fd, match_src, sockaddr_size()))
+		error(1, errno, "failed to bind matching source socket");
+	if (getsockname(match_send_fd, match_src,
+			(socklen_t []){ sockaddr_size() }))
+		error(1, errno, "failed to get matching source port");
+	peer_port = get_sockaddr_port(match_src);
+	peer_addr = new_loopback_sockaddr(family, peer_port);
+	if (connect(connected_fd, peer_addr, sockaddr_size()))
+		error(1, errno, "failed to connect reuseport socket");
+
+	/* Bind order makes good_fd index 0 and connected_fd index 1. */
+	attach_cbpf_index(good_fd, 1);

[ ... ]

tools/testing/selftests/net/reuseport_bpf.c static void test_reuseport_cbpf(struct test_params p) · line 440

Quoted 9 lines
tools/testing/selftests/net/reuseport_bpf.c +9 -0
+	/* An unmatched BPF-selected socket must not replace the valid candidate. */
+	send_udp_packet(other_send_fd, dst, mismatch_payload);
+	expect_udp_socket(epfd, good_fd, connected_fd, good_fd,
+			  mismatch_payload);
+
+	/* A matching selected socket must still win the positive rescore path. */
+	send_udp_packet(match_send_fd, dst, match_payload);
+	expect_udp_socket(epfd, good_fd, connected_fd, connected_fd,
+			  match_payload);

[Severity: Medium]
Does connecting connected_fd while bound to a specific loopback address remove it from reuse->socks[] and bypass the reuseport rescore path in udp4_lib_lookup2() and udp6_lib_lookup2()?

On !CONFIG_BASE_SMALL kernels, connecting a UDP socket bound to a non-wildcard address calls udp4_hash4() or udp6_hash4() -> udp_lib_hash4(), which detaches connected_fd from the reuseport group via reuseport_detach_sock() and inserts it into the 4-tuple hash table:

net/ipv4/udp.c:udp_lib_hash4() {
	...
	sock_set_flag(sk, SOCK_HASH4);
	hlist_add_head_rcu(&sk->sk_Value_node, &hslot4->head);
	...
	reuseport_detach_sock(sk);
}

That leaves only good_fd at index 0 with reuse->num_socks == 1. When mismatch_payload arrives, run_bpf_filter() sees index 1 >= socks (1) and returns NULL, so reuseport_select_sock() falls back to hashing and returns good_fd directly instead of testing fallback from connected_fd. When match_payload arrives, __udp4_lib_lookup() and __udp6_lib_lookup() find connected_fd directly in udp4_lib_lookup4() / udp6_lib_lookup4() without reaching udp4_lib_lookup2() / udp6_lib_lookup2(). As a result, the test passes even without the rescore logic.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
#5 lore raw

Re: [PATCH net 1/2] udp: fix reuseport rescore fallback

In reply to Zihan Xi
Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 1 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 0 · Low: 0

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] The patch adds a rescore branch to udp4_lib_lookup2() (net/ipv4/udp.c:478-484) and udp6_lib_lookup2()…

--

udp: fix reuseport rescore fallback

This changes the UDP lookup rescore path in udp4_lib_lookup2() and udp6_lib_lookup2(). When the reuseport group has connected sockets, the socket selected by reuseport is now kept only if its rescored value is strictly higher than the current badness. Otherwise the lookup goes back to the socket that set the current score.

Quoted 5 lines
net/ipv4/udp.c +0 -0
index 2e5562f353f85..23a8c951559c7 100644

net/ipv4/udp.c static struct sock *udp4_lib_lookup2(const struct net *net, · line 484

Quoted 10 lines
net/ipv4/udp.c +7 -0
@@ -475,12 +475,17 @@ static struct sock *udp4_lib_lookup2(const struct net *net,
 rescore:
 		score = compute_score(need_rescore ? result : sk, net, saddr,
 				      sport, daddr, hnum, dif, sdif);
+		if (need_rescore) {
+			if (score > badness)
+				badness = score;
+			else
+				result = sk;
+			continue;
+		}

[Severity: High]
Does this throw away the reuseport selection whenever the selected socket only ties with sk, and not just when it fails to match the packet?

All unconnected members of a reuseport group get the same base score from compute_score(). udp_reuseport_add_sock() requires the same family, ipv6_only, bound device and rcv_saddr, and unconnected sockets have no daddr or dport. The only thing that can differ is the incoming CPU bonus:

net/ipv4/udp.c:compute_score() {
    ...
	if (READ_ONCE(sk->sk_incoming_cpu) == raw_smp_processor_id())
		score++;
	return score;
}

The hash path only ever returns a non-established socket:

net/core/sock_reuseport.c:reuseport_select_sock_by_hash() {
    ...
		if (sk->sk_state != TCP_ESTABLISHED) {
			if (!READ_ONCE(reuse->incoming_cpu))
				return sk;
    ...
}

Once the group has at least one connected socket, reuseport_has_conns() is true and the lookup goes like this:

udp4_lib_lookup2()
first unconnected member sk scores > badness inet_lookup_reuseport() picks unconnected socket X goto rescore
compute_score(X) == badness
result = sk

If so, all traffic for the unconnected members would go to the first group member in the hslot2 chain. Wouldn't SK_REUSEPORT BPF and SO_ATTACH_REUSEPORT_CBPF steering to unconnected members also be ignored without any warning?

That looks like the behaviour that efc6b6f6c311, the Fixes: target of this patch, removed. Its commit message says:

"unconnected sockets have the same score, hence only does the first unconnected socket in udp_hslot always receive all packets ... the result of reuseport_select_sock() should be used for load balancing."

The old code kept X on a tie. The problem described in this commit message is the case where X does not match the packet at all: compute_score() returns -1, for example when BPF selects a socket connected to a different peer.

Could the fallback to sk happen only when score < 0 (or at most when score < badness)? That would fix that case and keep load balancing.

The same logic is added to udp6_lib_lookup2(), where badness starts at -1 and the rescored unconnected socket also ties with sk.

The commit message says "Keep the selected socket only when its score is higher". It does not mention the effect on load balancing or BPF steering. Also, the selftest added later in this series ("selftests: net: cover connected UDP reuseport rescore") only checks a CBPF program that selects the connected socket. It never selects an unconnected peer, so it would not catch this.

Quoted 4 lines
net/ipv4/udp.c +1 -0
+
 		if (score > badness) {
 			badness = score;
 

[ ... ]

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1794759652.git.zihanx%40nebusec.ai
#8 lore raw

Re: [PATCH net 2/2] selftests: net: cover connected UDP reuseport rescore

In reply to Zihan Xi
Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Thank you for your contribution! Sashiko AI review found 3 potential issue(s) to consider.

Critical: 0 · High: 1 · Medium: 1 · Low: 1

If you believe none of the issues are legitimate, please reply to each issue of Medium severity or higher and explain why the AI is wrong. If any of them are legitimate and you plan to respin, please let patchwork know by including "pw-bot: cr" as a separate line at the end of your reply (one such reply per series is enough).

- [High] This patch's parent commit in the series, "udp: fix reuseport rescore fallback", changes the rescore branch in udp4_lib_lookup2()…
- [Medium] On the default CONFIG_BASE_SMALL=n kernel, test_connected_reuseport_rescore() never has a BPF program select the…
- [Low] The comment on the second assertion in test_connected_reuseport_rescore()…

--

selftests: net: cover connected UDP reuseport rescore

This adds a reuseport_bpf selftest in which a constant classic BPF program selects a connected UDP socket in a reuseport group. A packet from a different peer should go to the unconnected socket, and a packet from the connected peer should still reach the connected socket. The test runs for both IPv4 and IPv6.

Quoted 5 lines
tools/testing/selftests/net/reuseport_bpf.c +0 -0
index 12e48b97b8625..fbfe73dce2111 100644
@@ -303,6 +303,154 @@ static void test_reuseport_cbpf(struct test_params p)

[ ... ]

Quoted 2 lines
tools/testing/selftests/net/reuseport_bpf.c +2 -0
+static void test_connected_reuseport_rescore(int family)
+{

[ ... ]

Quoted 10 lines
tools/testing/selftests/net/reuseport_bpf.c +10 -0
+	good_addr = new_loopback_sockaddr(family, 0);
+	if (bind(good_fd, good_addr, sockaddr_size()))
+		error(1, errno, "failed to bind good reuseport socket");
+	if (getsockname(good_fd, good_addr, (socklen_t []){ sockaddr_size() }))
+		error(1, errno, "failed to get reuseport port");
+	recv_port = get_sockaddr_port(good_addr);
+	if (bind(connected_fd, good_addr, sockaddr_size()))
+		error(1, errno, "failed to bind connected reuseport socket");
+
+	match_src = new_loopback_sockaddr(family, 0);

[ ... ]

tools/testing/selftests/net/reuseport_bpf.c static void test_reuseport_cbpf(struct test_params p) · line 410

Quoted 6 lines
tools/testing/selftests/net/reuseport_bpf.c +6 -0
+	peer_addr = new_loopback_sockaddr(family, peer_port);
+	if (connect(connected_fd, peer_addr, sockaddr_size()))
+		error(1, errno, "failed to connect reuseport socket");
+
+	/* Bind order makes good_fd index 0 and connected_fd index 1. */
+	attach_cbpf_index(good_fd, 1);

[Severity: Medium]
Is connected_fd still in the reuseport group at this point?

On a default CONFIG_BASE_SMALL=n kernel, connected_fd is bound to 127.0.0.1 or ::1. So udp_connect() calls udp4_hash4() (udpv6_connect() calls udp6_hash4()), and udp_lib_hash4() detaches the socket:

net/ipv4/udp.c:udp_lib_hash4() {
    ...
	spin_lock_bh(&hslot->lock);
	if (rcu_access_pointer(sk->sk_reuseport_cb))
		reuseport_detach_sock(sk);
    ...
}

That leaves good_fd as the only member (num_socks == 1), while has_conns stays set from __ip4_datagram_connect(). The cBPF return value of 1 is then out of range, and run_bpf_filter() rejects it:

net/core/sock_reuseport.c:run_bpf_filter() {
    ...
	if (index >= socks)
		return NULL;
    ...
}

reuseport_select_sock_by_hash() then picks good_fd.

As a result, the mismatch packet reaches good_fd on both the old and the new code. A good_fd rescored to an equal score is kept either way. The match packet is resolved by udp4_lib_lookup4() or udp6_lib_lookup4() in __udp4_lib_lookup() or __udp6_lib_lookup(), before udp*_lib_lookup2() runs.

The case the test is named after, a BPF-selected connected socket kept as a stale result, only seems to reproduce on CONFIG_BASE_SMALL=y kernels. There udp_lib_hash4() is a stub and connected_fd stays at index 1.

Could the setup keep a connected socket in the group on hash4 kernels? One option is to bind and connect connected_fd before good_fd binds, so that reuseport_add_sock() rebuilds the group around it, and then set has_conns. Failing that, could the test at least check the group membership it assumes, rather than passing silently?

[ ... ]

tools/testing/selftests/net/reuseport_bpf.c static void test_reuseport_cbpf(struct test_params p) · line 435

Quoted 4 lines
tools/testing/selftests/net/reuseport_bpf.c +4 -0
+	/* An unmatched BPF-selected socket must not replace the valid candidate. */
+	send_udp_packet(other_send_fd, dst, mismatch_payload);
+	expect_udp_socket(epfd, good_fd, connected_fd, good_fd,
+			  mismatch_payload);

[Severity: High]
This is about the parent commit 33c2c891d6d7 ("udp: fix reuseport rescore fallback"), but this test can't detect it. That commit changes the rescore branch in udp4_lib_lookup2() and udp6_lib_lookup2() to:

		if (need_rescore) {
			if (score > badness)
				badness = score;
			else
				result = sk;
			continue;
		}

Suppose the socket picked by the reuseport program or hash scores the same as sk, the socket that set badness. Won't the pick then be dropped and sk returned instead?

Before that change, an equal score left result unchanged, so the pick was kept. Only a lower score, the stale connected socket case, seems to need the fallback.

Unconnected sockets have sk_incoming_cpu == -1 unless SO_INCOMING_CPU is set, and only connected sockets update it. So equal scores are the usual case.

reuseport_has_conns_set() sets has_conns in __ip4_datagram_connect() and __ip6_datagram_connect() when a member bound to a specific address connects. The flag is never cleared, and it stays set after udp_lib_hash4() detaches the connected socket.

For example, take unconnected A and B plus connected C, all bound to 127.0.0.1:X, with the BPF program or hash picking B. B rescores equal to A, the else branch sets result = A, and A gets every packet from unconnected peers.

Does this override hash load balancing and cBPF/eBPF steering in every group with has_conns set? One such setup is a QUIC-style server with several unconnected workers and per-connection connected sockets on the same address and port.

The parent is tagged Cc: stable. Should it keep the pick unless score < badness?

This test uses only one unconnected and one connected socket, so it can't catch this. Could it add a second unconnected socket and have the BPF program select that one?

tools/testing/selftests/net/reuseport_bpf.c static void test_reuseport_cbpf(struct test_params p) · line 440

Quoted 5 lines
tools/testing/selftests/net/reuseport_bpf.c +5 -0
+
+	/* A matching selected socket must still win the positive rescore path. */
+	send_udp_packet(match_send_fd, dst, match_payload);
+	expect_udp_socket(epfd, good_fd, connected_fd, connected_fd,
+			  match_payload);

[Severity: Low]
Is this comment accurate? On every config, the match packet seems to be decided by something other than the positive rescore branch (score > badness).

On default CONFIG_BASE_SMALL=n kernels, connected_fd is in the 4-tuple hash. __udp4_lib_lookup() returns it from udp4_lib_lookup4() before udp4_lib_lookup2() runs, and IPv6 does the same through udp6_lib_lookup4().

On IPv4 with CONFIG_BASE_SMALL=y, connected_fd is bound second, and udp_lib_get_port() adds it at the head of hslot2. It is scanned first and taken by this shortcut, with no reuseport selection or rescore:

		if (sk->sk_state == TCP_ESTABLISHED) {
			result = sk;
			continue;
		}

On IPv6 with CONFIG_BASE_SMALL=y, the rescore does run. But connected_fd outscores good_fd on its own merits. Even if the positive branch fell back to result = sk, the next iteration would still pick connected_fd through the TCP_ESTABLISHED shortcut.

So can this assertion catch a regression in the badness = score branch?

Signature · 1 line
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/cover.1794759652.git.zihanx%40nebusec.ai