All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jiayuan Chen <jiayuan.chen@linux.dev>
To: Kuniyuki Iwashima <kuniyu@google.com>,
	Jiayuan Chen <jiayuan.chen@linux.dev>
Cc: netdev@vger.kernel.org, Jiayuan Chen <jiayuan.chen@shopee.com>,
	Martin KaFai Lau <martin.lau@linux.dev>,
	Daniel Borkmann <daniel@iogearbox.net>,
	John Fastabend <john.fastabend@gmail.com>,
	Stanislav Fomichev <sdf@fomichev.me>,
	Alexei Starovoitov <ast@kernel.org>,
	Andrii Nakryiko <andrii@kernel.org>,
	Eduard Zingerman <eddyz87@gmail.com>, Song Liu <song@kernel.org>,
	Yonghong Song <yonghong.song@linux.dev>,
	KP Singh <kpsingh@kernel.org>, Hao Luo <haoluo@google.com>,
	Jiri Olsa <jolsa@kernel.org>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>, Shuah Khan <shuah@kernel.org>,
	bpf@vger.kernel.org, linux-kernel@vger.kernel.org,
	linux-kselftest@vger.kernel.org
Subject: Re: [PATCH bpf v2 2/2] selftests/bpf: Add protocol check test for bpf_sk_assign_tcp_reqsk()
Date: Fri, 27 Mar 2026 09:52:50 +0800	[thread overview]
Message-ID: <5ce664e8-8c65-4000-b73d-06a9423edb32@linux.dev> (raw)
In-Reply-To: <CAAVpQUAKAeU8SXpKQwcQS6P6aFJ_BxmevcrK=Hs+_rf_mnaLXA@mail.gmail.com>


On 3/27/26 1:33 AM, Kuniyuki Iwashima wrote:
[...]
>>          for (i = 0; i < ARRAY_SIZE(test_cases); i++) {
>> @@ -145,6 +145,82 @@ void test_tcp_custom_syncookie(void)
>>
>>   destroy_skel:
>>          system("tc qdisc del dev lo clsact");
>> +       test_tcp_custom_syncookie__destroy(skel);
>> +}
>>
>> +/* Test: bpf_sk_assign_tcp_reqsk() should reject non-TCP skb.
>> + *
>> + * Send a UDP packet through TC ingress where a BPF program calls
>> + * bpf_sk_assign_tcp_reqsk() on it. The kfunc should return an error
>> + * because the skb carries UDP, not TCP.
>> + */
>> +void test_tcp_custom_syncookie_protocol_check(void)
>> +{
>> +       int tcp_server = -1, udp_server = -1, udp_client = -1;
>> +       struct test_tcp_custom_syncookie *skel;
>> +       struct sockaddr_in udp_addr;
>> +       char buf[32] = "test";
>> +       int udp_port, ret;
>> +
>> +       if (setup_netns())
>> +               return;
>> +
>> +       skel = test_tcp_custom_syncookie__open_and_load();
>> +       if (!ASSERT_OK_PTR(skel, "open_and_load"))
>> +               return;
>> +
>> +       /* Create a TCP listener so the BPF can find a LISTEN socket */
>> +       tcp_server = start_server(AF_INET, SOCK_STREAM, "127.0.0.1", 0, 0);
> Can you add IPv6 test as well ?
> You can reuse test_tcp_custom_syncookie_case[].
>
>
>> +       if (!ASSERT_NEQ(tcp_server, -1, "start tcp_server"))
>> +               goto destroy_skel;
>> +
>> +       /* Create a UDP server to receive the packet as synchronization */
>> +       udp_server = start_server(AF_INET, SOCK_DGRAM, "127.0.0.1", 0, 0);
> You can specify the port to get_socket_local_port(tcp_server),
>
>
>> +       if (!ASSERT_NEQ(udp_server, -1, "start udp_server"))
>> +               goto close_tcp;
>> +
>> +       skel->bss->tcp_listener_port = ntohs(get_socket_local_port(tcp_server));
>> +       udp_port = ntohs(get_socket_local_port(udp_server));
>> +       skel->bss->udp_test_port = udp_port;
> then the 3 lines above will be unnecessary,
>
>
>> +
>> +       ret = bpf_program__fd(skel->progs.tcp_custom_syncookie_badproto);
>> +       if (setup_tc(ret))
>> +               goto close_udp_server;
>> +
>> +       udp_client = socket(AF_INET, SOCK_DGRAM, 0);
>> +       if (!ASSERT_NEQ(udp_client, -1, "udp socket"))
>> +               goto cleanup_tc;
>> +
>> +       memset(&udp_addr, 0, sizeof(udp_addr));
>> +       udp_addr.sin_family = AF_INET;
>> +       udp_addr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);
>> +       udp_addr.sin_port = htons(udp_port);
> and you can reuse get_socket_local_port(tcp_server) here too.
>
>
>> +
>> [...]
>> +       iph = (struct iphdr *)(eth + 1);
>> +       if (iph + 1 > data_end)
>> +               return TC_ACT_OK;
>> +
>> +       if (iph->protocol != IPPROTO_UDP)
>> +               return TC_ACT_OK;
>> +
>> +       udp = (struct udphdr *)(iph + 1);
>> +       if (udp + 1 > data_end)
>> +               return TC_ACT_OK;
>> +
>> +       if (bpf_ntohs(udp->dest) != udp_test_port)
>> +               return TC_ACT_OK;
> You don't need to worry about other program sending UDP
> packets in this netns created by unshare().
>
>
>> +
>> +       udp_intercepted = true;
>> +
>> +       tuple.ipv4.saddr = iph->saddr;
>> +       tuple.ipv4.daddr = iph->daddr;
>> +       tuple.ipv4.sport = udp->source;
>> +       tuple.ipv4.dport = bpf_htons(tcp_listener_port);
> and you can simply reuse dport here too.
>

Thanks for the review. I'll update the code to address all these points.


      reply	other threads:[~2026-03-27  1:53 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-03-26  6:26 [PATCH bpf v2 0/2] bpf: tcp: Fix null-ptr-deref in arbitrary SYN Cookie Jiayuan Chen
2026-03-26  6:26 ` [PATCH bpf v2 1/2] bpf: tcp: Reject non-TCP skb in bpf_sk_assign_tcp_reqsk() Jiayuan Chen
2026-03-26  6:26 ` [PATCH bpf v2 2/2] selftests/bpf: Add protocol check test for bpf_sk_assign_tcp_reqsk() Jiayuan Chen
2026-03-26 17:33   ` Kuniyuki Iwashima
2026-03-27  1:52     ` Jiayuan Chen [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=5ce664e8-8c65-4000-b73d-06a9423edb32@linux.dev \
    --to=jiayuan.chen@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=davem@davemloft.net \
    --cc=eddyz87@gmail.com \
    --cc=edumazet@google.com \
    --cc=haoluo@google.com \
    --cc=horms@kernel.org \
    --cc=jiayuan.chen@shopee.com \
    --cc=john.fastabend@gmail.com \
    --cc=jolsa@kernel.org \
    --cc=kpsingh@kernel.org \
    --cc=kuba@kernel.org \
    --cc=kuniyu@google.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=martin.lau@linux.dev \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sdf@fomichev.me \
    --cc=shuah@kernel.org \
    --cc=song@kernel.org \
    --cc=yonghong.song@linux.dev \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.