* [PATCH net 0/2] udp: fix connected reuseport rescore fallback
@ 2026-11-16 3:55 Zihan Xi
2026-11-16 3:55 ` [PATCH net 1/2] udp: fix " Zihan Xi
2026-11-16 3:55 ` [PATCH net 2/2] selftests: net: cover connected UDP reuseport rescore Zihan Xi
0 siblings, 2 replies; 4+ messages in thread
From: Zihan Xi @ 2026-11-16 3:55 UTC (permalink / raw)
To: netdev, linux-kselftest, bpf
Cc: willemdebruijn.kernel, davem, edumazet, pabeni, horms, benh,
kuniyu, shuah, vega, zihanx
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(-)
--
2.43.0
^ permalink raw reply [flat|nested] 4+ messages in thread* [PATCH net 1/2] udp: fix reuseport rescore fallback 2026-11-16 3:55 [PATCH net 0/2] udp: fix connected reuseport rescore fallback Zihan Xi @ 2026-11-16 3:55 ` Zihan Xi 2026-11-16 4:51 ` Alexei Starovoitov 2026-11-16 3:55 ` [PATCH net 2/2] selftests: net: cover connected UDP reuseport rescore Zihan Xi 1 sibling, 1 reply; 4+ messages in thread From: Zihan Xi @ 2026-11-16 3:55 UTC (permalink / raw) To: netdev, linux-kselftest, bpf Cc: willemdebruijn.kernel, davem, edumazet, pabeni, horms, benh, kuniyu, shuah, vega, zihanx 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. Fixes: efc6b6f6c311 ("udp: Improve load balancing for SO_REUSEPORT.") Cc: stable@vger.kernel.org Reported-by: Vega <vega@nebusec.ai> Link: https://bugtracker.nebusec.ai/f/net/ipv6/udp.c||udp6_lib_lookup2||socket-hijack||m7q Signed-off-by: Zihan Xi <zihanx@nebusec.ai> --- net/ipv4/udp.c | 11 ++++++++--- net/ipv6/udp.c | 11 ++++++++--- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/net/ipv4/udp.c b/net/ipv4/udp.c index 2e5562f353f8..23a8c951559c 100644 --- a/net/ipv4/udp.c +++ b/net/ipv4/udp.c @@ -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; diff --git a/net/ipv6/udp.c b/net/ipv6/udp.c index 9dcefa162522..c5adeaaf5b42 100644 --- a/net/ipv6/udp.c +++ b/net/ipv6/udp.c @@ -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; -- 2.43.0 ^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [PATCH net 1/2] udp: fix reuseport rescore fallback 2026-11-16 3:55 ` [PATCH net 1/2] udp: fix " Zihan Xi @ 2026-11-16 4:51 ` Alexei Starovoitov 0 siblings, 0 replies; 4+ messages in thread From: Alexei Starovoitov @ 2026-11-16 4:51 UTC (permalink / raw) To: Zihan Xi, netdev, linux-kselftest, bpf Cc: willemdebruijn.kernel, davem, edumazet, pabeni, horms, benh, kuniyu, shuah, vega On Sun, Nov 15, 2026 at 07:55 PM Zihan Xi <zihanx@nebusec.ai> wrote: > 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. > Link: https://bugtracker.nebusec.ai/f/net/ipv6/udp.c||udp6_lib_lookup2||socket-hijack||m7q 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. > + 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. ^ permalink raw reply [flat|nested] 4+ messages in thread
* [PATCH net 2/2] selftests: net: cover connected UDP reuseport rescore 2026-11-16 3:55 [PATCH net 0/2] udp: fix connected reuseport rescore fallback Zihan Xi 2026-11-16 3:55 ` [PATCH net 1/2] udp: fix " Zihan Xi @ 2026-11-16 3:55 ` Zihan Xi 1 sibling, 0 replies; 4+ messages in thread From: Zihan Xi @ 2026-11-16 3:55 UTC (permalink / raw) To: netdev, linux-kselftest, bpf Cc: willemdebruijn.kernel, davem, edumazet, pabeni, horms, benh, kuniyu, shuah, vega, zihanx 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. Signed-off-by: Zihan Xi <zihanx@nebusec.ai> --- tools/testing/selftests/net/reuseport_bpf.c | 150 ++++++++++++++++++++ 1 file changed, 150 insertions(+) diff --git a/tools/testing/selftests/net/reuseport_bpf.c b/tools/testing/selftests/net/reuseport_bpf.c index 12e48b97b862..fbfe73dce211 100644 --- a/tools/testing/selftests/net/reuseport_bpf.c +++ b/tools/testing/selftests/net/reuseport_bpf.c @@ -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, -- 2.43.0 ^ permalink raw reply related [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-11-16 4:51 UTC | newest] Thread overview: 4+ messages (download: mbox.gz follow: Atom feed -- links below jump to the message on this page -- 2026-11-16 3:55 [PATCH net 0/2] udp: fix connected reuseport rescore fallback Zihan Xi 2026-11-16 3:55 ` [PATCH net 1/2] udp: fix " Zihan Xi 2026-11-16 4:51 ` Alexei Starovoitov 2026-11-16 3:55 ` [PATCH net 2/2] selftests: net: cover connected UDP reuseport rescore Zihan Xi
This is a public inbox, see mirroring instructions for how to clone and mirror all data and code used for this inbox