From: Rongguang Wei <clementwei90@163.com>
To: netdev-bot+sashiko@kernel.org
Cc: netdev@vger.kernel.org, bpf@vger.kernel.org,
willemdebruijn.kernel@gmail.com, jasowangio@gmail.com,
andrew+netdev@lunn.ch, davem@davemloft.net, kuba@kernel.org,
ast@kernel.org, alexei.starovoitov@gmail.com, andrii@kernel.org,
weirongguang@kylinos.cn
Subject: Re: [PATCH net v12 2/2] selftests: net: add TAP socket filter attach tests
Date: Tue, 24 Nov 2026 16:00:49 +0800 [thread overview]
Message-ID: <47389671-2a71-4a6b-8ed3-9c1fb4de0557@163.com> (raw)
In-Reply-To: <179549910931.3135.4958196655811648969@kernel.org>
on 2026/11/24 13:45, netdev-bot+sashiko@kernel.org wrote:
> Thank you for your contribution! Sashiko AI review found 2 potential
> issue(s) to consider.
>
> Critical: 0 · High: 0 · 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).
>
> - [Medium] The selftest misses the half of the bug where the queue is
> left unpublished.
> - [Low] None of the new tests check whether the cBPF program actually
> runs on packets.
>
> --
>
> selftests: net: add TAP socket filter attach tests
>
> This commit adds kselftests in tools/testing/selftests/net/tun.c for
> TUNATTACHFILTER and TUNDETACHFILTER on a multi-queue TAP device. They cover
> re-attaching a queue after the original filter buffer becomes unreadable,
> bad lengths and bad programs with and without attached queues, a filter
> set before any queue is attached, and IFF_NOFILTER handling. Every check
> is made through TUNGETIFF and TUNGETFILTER.
>
>> diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
>> index 34de2580e32ec..ac326193867b0 100644
>> --- a/tools/testing/selftests/net/tun.c
>> +++ b/tools/testing/selftests/net/tun.c
>
> [ ... ]
>
>> @@ -548,6 +550,362 @@ TEST_F(tun, reattach_close_delete)
>> EXPECT_EQ(tun_delete(self->ifname), 0);
>> }
>>
>> +/* accept: return skb->len. Three instructions on purpose, so that the
>> + * length read back through TUNGETFILTER tells it apart from a rejected
>> + * program that is shorter.
>> + */
>> +static const struct sock_filter filter_accept[] = {
>> + BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
>> + BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 0),
>> + BPF_STMT(BPF_RET | BPF_A, 0),
>> +};
>
> [Severity: Low]
>
> Do any of the new tests check that the classic filter actually runs on
> packets?
>
> filter_accept is the only valid program the tests use. It returns
> skb->len, so it can never show a drop or a trim. None of the new tests
> write a frame to the TAP fds or read one back.
>
> Every assertion reads either the IFF_NOFILTER bit from TUNGETIFF or the
> fprog header from TUNGETFILTER. TUNGETIFF builds that bit from its own
> expression in tun_chr_ioctl():
>
> if (tfile->nofilter || !rcu_access_pointer(tun->cbpf_prog))
> ifr.ifr_flags |= IFF_NOFILTER;
>
> The companion commit "tun: run the socket filter from tun_net_xmit()" adds
> the execution site as a separate check in tun_net_xmit():
>
> prog = rcu_dereference(tun->cbpf_prog);
> if (prog && !READ_ONCE(tfile->nofilter)) {
> len = bpf_prog_run_clear_cb(prog->prog, skb);
> if (!len || pskb_trim(skb, len)) {
>
> Would these tests still pass if tun_net_xmit() never ran cbpf_prog, ran it
> on IFF_NOFILTER queues, or ignored its verdict or trim length?
>
> The test comments say a queue "still gets the filter", "must not get the
> filter", or is covered by it. reattach_filter_without_user_buffer already
> says its checks "cannot tell an installed filter from no filter". That
> limit applies to the other new tests as well.
>
> A test could send a frame through a rejecting or truncating program on a
> TAP queue, then read it back or confirm it was dropped. That would cover
> the datapath and the IFF_NOFILTER bypass.
>
> There is also a smaller inaccuracy. The comment above
> attach_filter_clears_nofilter_queue says:
>
> A later TUNATTACHFILTER covers every queue of the device, including one
> that was attached with IFF_NOFILTER.
>
> tun_attach_filter() only clears nofilter for the active queues:
>
> for (i = 0; i < tun->numqueues; i++) {
> tfile = rtnl_dereference(tun->tfiles[i]);
> WRITE_ONCE(tfile->nofilter, false);
> }
>
> A queue opened with IFF_NOFILTER and then detached keeps nofilter set.
> TUNGETIFF keeps reporting IFF_NOFILTER for it until TUNSETQUEUE attaches
> it again. Should the comment refer to the active queues only?
>
> [ ... ]
>
>> + /* The queue gets that program again, from the device */
>> + EXPECT_EQ(tun_attach(self->fd, self->ifname), 0);
>> +
>> + flags = tun_get_iff_flags(self->fd);
>> + EXPECT_GE(flags, 0);
>> + EXPECT_EQ(flags & IFF_NOFILTER, 0);
>> +}
>> +
>> +/* A program configured while no queue is attached must cover the queues that
>> + * are attached afterwards.
>> + */
>> +TEST_F(tun, attach_filter_without_queues_then_attach)
>> +{
>> + short flags = 0;
>> +
>> + EXPECT_EQ(tun_detach(self->fd, self->ifname), 0);
>> + EXPECT_EQ(tun_detach(self->fd2, self->ifname), 0);
>> +
>> + ASSERT_EQ(filter_attach(self->fd, filter_accept,
>> + ARRAY_SIZE(filter_accept)), 0);
>> +
>> + EXPECT_EQ(tun_attach(self->fd, self->ifname), 0);
>> +
>> + flags = tun_get_iff_flags(self->fd);
>> + EXPECT_GE(flags, 0);
>> + EXPECT_EQ(flags & IFF_NOFILTER, 0);
>> +}
>
> [Severity: Medium]
>
> Should attach_filter_without_queues_then_attach also check
> IFF_DETACH_QUEUE after the re-attach?
>
> The companion commit "tun: run the socket filter from tun_net_xmit()"
> describes two failure modes of the old check in tun_attach(). In the
> first, a failed read of the user buffer attached the queue without a
> filter. In the second, "a successful read returned early and left the
> queue unpublished".
>
> reattach_filter_without_user_buffer is the only test that checks
> IFF_DETACH_QUEUE. Its buffer is PROT_NONE, so it only exercises the
> failed read case.
>
> In this test filter_accept is in readable .rodata. On the baseline
> kernel, TUNSETQUEUE(IFF_ATTACH_QUEUE) therefore takes the early return in
> tun_attach():
>
> /* Re-attach the filter to persist device */
> if (!skip_filter && (tun->filter_attached == true)) {
> lock_sock(tfile->socket.sk);
> err = sk_attach_filter(&tun->fprog, tfile->socket.sk);
> release_sock(tfile->socket.sk);
> if (!err)
> goto out;
> }
>
> That path returns 0 before tun_enable_queue() and before tun->tfiles[] is
> published. TUNGETIFF then reports IFF_DETACH_QUEUE set, because
> tfile->detached is still set. It also reports IFF_NOFILTER clear, because
> sk_filter is set.
>
> Doesn't that let all three assertions here pass on the unfixed kernel
> while the queue stays disabled?
>
> The final re-attach in attach_filter_bad_keeps_program_without_queues has
> the same gap. That test still fails on the baseline kernel, but only
> because of its earlier bad length and bad program checks.
>
> Adding this after the tun_attach() helper call in both tests would cover
> the unpublished queue case:
>
> EXPECT_EQ(flags & IFF_DETACH_QUEUE, 0);
>
The assertion is missing, yes, but I would leave the test as it is.
The early return that returns success without publishing the queue is the
code patch 1 deletes. With it gone, tun_attach() has a single success path:
returning 0 means the queue was added to tun->tfiles[] and tfile->detached
was cleared, so on the patched kernel this test cannot pass with a queue
that stayed disabled. reattach_filter_without_user_buffer() also asserts
that the queue is not left with IFF_DETACH_QUEUE after a re-attach.
Adding the check here would only guard against reintroducing the code the
series removes, so I did not include it. I'll pick it up if the series
needs another revision anyway.
next prev parent reply other threads:[~2026-11-24 8:01 UTC|newest]
Thread overview: 8+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-11-23 5:42 [PATCH net v12 0/2] tun: fix re-attaching the socket filter Rongguang Wei
2026-11-23 5:42 ` [PATCH net v12 1/2] tun: run the socket filter from tun_net_xmit() Rongguang Wei
2026-11-24 5:45 ` netdev-bot+sashiko
2026-11-23 5:42 ` [PATCH net v12 2/2] selftests: net: add TAP socket filter attach tests Rongguang Wei
2026-11-24 5:45 ` netdev-bot+sashiko
2026-11-24 8:00 ` Rongguang Wei [this message]
2026-11-23 6:49 ` [PATCH net v12 0/2] tun: fix re-attaching the socket filter Alexei Starovoitov
2026-11-30 16:03 ` willemb
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=47389671-2a71-4a6b-8ed3-9c1fb4de0557@163.com \
--to=clementwei90@163.com \
--cc=alexei.starovoitov@gmail.com \
--cc=andrew+netdev@lunn.ch \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=davem@davemloft.net \
--cc=jasowangio@gmail.com \
--cc=kuba@kernel.org \
--cc=netdev-bot+sashiko@kernel.org \
--cc=netdev@vger.kernel.org \
--cc=weirongguang@kylinos.cn \
--cc=willemdebruijn.kernel@gmail.com \
/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