BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Zihan Xi" <zihanx@nebusec.ai>, bpf@vger.kernel.org
Subject: Re: [PATCH net 2/2] selftests: net: cover connected UDP reuseport rescore
Date: Tue, 17 Nov 2026 03:55:53 +0000	[thread overview]
Message-ID: <sashiko-outbox-218171@kernel.org> (raw)
In-Reply-To: <4302d941304941ea6059b3d1fa167236635686c6.1794759652.git.zihanx@nebusec.ai>

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 <zihanx@nebusec.ai>

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.

> diff --git a/tools/testing/selftests/net/reuseport_bpf.c b/tools/testing/selftests/net/reuseport_bpf.c
> index 12e48b97b8625..fbfe73dce2111 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)
> +static void test_connected_reuseport_rescore(int family)
> +{

[ ... ]

> +	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);

[ ... ]

> +	/* 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.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/cover.1794759652.git.zihanx@nebusec.ai?part=2

      reply	other threads:[~2026-11-17  3:55 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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  5:08     ` zihan xi
2026-11-17  3:55   ` sashiko-bot
2026-11-16  3:55 ` [PATCH net 2/2] selftests: net: cover connected UDP reuseport rescore Zihan Xi
2026-11-17  3:55   ` sashiko-bot [this message]

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=sashiko-outbox-218171@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=zihanx@nebusec.ai \
    /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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox