From: netdev-bot+sashiko@kernel.org
To: clementwei90@163.com
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 1/2] tun: run the socket filter from tun_net_xmit()
Date: Tue, 24 Nov 2026 05:45:08 +0000 [thread overview]
Message-ID: <179549910801.3135.11117955228755728713@kernel.org> (raw)
In-Reply-To: <20261123054209.89946-2-clementwei90@163.com>
Thank you for your contribution! Sashiko AI review found more severe
issue(s) in other patches of this series. This mail is only an FYI, in case
you want to address the nit picks below when you respin.
Low priority / nit picks for your consideration:
- [Low] The commit message does not mention that the classic filter's
runtime and attach semantics change.
- [Low] The commit message understates the bug it fixes, and the subject
line is misleading.
- [Low] This Fixes-tagged patch (Fixes: 54f968d6efdb, from 2012) also
removes EXPORT_SYMBOL_GPL(sk_attach_filter) and…
--
tun: run the socket filter from tun_net_xmit()
This commit moves the classic BPF program installed by TUNATTACHFILTER
off the per-queue socket (sk_filter) and onto the device
(tun->cbpf_prog), and runs it directly from tun_net_xmit(). A per-queue
nofilter flag replaces the re-attach of the user filter in tun_attach(),
and the exports of sk_attach_filter() and sk_detach_filter() are removed.
> tun: run the socket filter from tun_net_xmit()
>
> TUNATTACHFILTER copies only the sock_fprog header, so tun->fprog.filter
> points into the address space of the process that issued the ioctl, and
> tun_attach() reads it again whenever a queue is attached to a persistent
> device. That read can fail, and with the inverted check below the
> failure was not fatal either: the queue was attached without a filter,
> while a successful read returned early and left the queue unpublished.
[Severity: Low]
Is "whenever a queue is attached to a persistent device" accurate? In
the baseline tun_attach(), the re-attach block depends only on
tun->filter_attached and not on IFF_PERSIST:
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;
}
TUNATTACHFILTER sets filter_attached whether or not IFF_PERSIST is set.
That means the inverted check is also reached in two more cases:
- adding a queue to a live, non-persistent multiqueue TAP through
tun_set_iff()
- every TUNSETQUEUE with IFF_ATTACH_QUEUE, because tun_set_queue()
always passes false for skip_filter:
ret = tun_attach(tun, file, false, tun->flags & IFF_NAPI,
tun->flags & IFF_NAPI_FRAGS, true);
In those cases a successful filter read makes TUNSETIFF or TUNSETQUEUE
return success for a queue that was never published.
A failed read on a detached tfile leaves err negative. TUNSETQUEUE then
reports failure for a queue that was in fact attached, without a filter.
Could the commit message describe the full scope? It matters when
deciding on backports.
The subject line also looks misleading. The baseline tun_net_xmit()
already ran the filter through sk_filter_reason(). What changes is where
the program is stored: per-socket sk_filter before, per-device
tun->cbpf_prog now.
> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
> index d4870bdc156f4..fe3e197997098 100644
> --- a/drivers/net/tun.c
> +++ b/drivers/net/tun.c
[ ... ]
> @@ -1096,10 +1089,20 @@ static netdev_tx_t tun_net_xmit(struct sk_buff *skb, struct net_device *dev)
> goto drop;
> }
>
> - if (tfile->socket.sk->sk_filter) {
> - drop_reason = sk_filter_reason(tfile->socket.sk, skb);
> - if (drop_reason)
> + /* Classic filter set with TUNATTACHFILTER. A queue attached with
> + * IFF_NOFILTER does not run it.
> + */
> + prog = rcu_dereference(tun->cbpf_prog);
> + if (prog && !READ_ONCE(tfile->nofilter)) {
> + /* A filter returning 0 drops the packet, any other value is
> + * the length to keep. pskb_trim() does nothing when that
> + * length is larger than the skb.
> + */
> + len = bpf_prog_run_clear_cb(prog->prog, skb);
> + if (!len || pskb_trim(skb, len)) {
> + drop_reason = SKB_DROP_REASON_SOCKET_FILTER;
> goto drop;
> + }
> }
>
> len = run_ebpf_filter(tun, skb, len);
[Severity: Low]
Should the commit message mention that the classic filter behaves
differently at runtime now?
The old sk_filter_reason() call goes through sk_filter_trim_cap(). Before
it runs the program, that function does three things:
- drops skb_pfmemalloc() skbs for sockets without SOCK_MEMALLOC
(SKB_DROP_REASON_PFMEMALLOC)
- runs BPF_CGROUP_RUN_PROG_INET_INGRESS
- calls security_sock_rcv_skb() (SKB_DROP_REASON_SECURITY_HOOK)
It also sets skb->sk while the program runs and uses
bpf_prog_run_save_cb().
The new code only calls bpf_prog_run_clear_cb() and pskb_trim(). Filtered
queues therefore lose the pfmemalloc drop, the LSM hook and those drop
reasons. Losing the cgroup hook changes nothing, because the tun socket
is AF_UNSPEC.
The attach side changes as well. The old sk_attach_filter() path charged
bpf_prog_size(prog->len) against net.core.optmem_max in
__sk_filter_charge() and returned -ENOMEM above that limit.
The new tun_attach_filter() uses bpf_prog_create_from_user(), which has
no such limit. TUNATTACHFILTER now accepts filters it used to reject.
This is a UAPI ioctl path in a Fixes-tagged patch. Could the changelog
list these differences, or explain why they are not needed?
[ ... ]
> diff --git a/net/core/filter.c b/net/core/filter.c
> index 53c042b267ab6..fdd7fd8542747 100644
> --- a/net/core/filter.c
> +++ b/net/core/filter.c
> @@ -1565,7 +1565,6 @@ int sk_attach_filter(struct sock_fprog *fprog, struct sock *sk)
>
> return 0;
> }
> -EXPORT_SYMBOL_GPL(sk_attach_filter);
>
> int sk_reuseport_attach_filter(struct sock_fprog *fprog, struct sock *sk)
> {
> @@ -11775,7 +11774,6 @@ int sk_detach_filter(struct sock *sk)
>
> return ret;
> }
> -EXPORT_SYMBOL_GPL(sk_detach_filter);
[Severity: Low]
This isn't a bug, but does the export removal belong in a Fixes-tagged
patch? The fix itself does not need it.
Nothing in-tree breaks. The only remaining callers are SO_ATTACH_FILTER
and SO_DETACH_FILTER in net/core/sock.c. However, a stable branch that
picks this up because of the Fixes tag would also lose two exported core
networking symbols.
Could the export removal go to net-next as a separate patch, so the fix
stays minimal?
--
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261123054209.89946-1-clementwei90%40163.com
next prev parent reply other threads:[~2026-11-24 5:45 UTC|newest]
Thread overview: 6+ 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 [this message]
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-23 6:49 ` [PATCH net v12 0/2] tun: fix re-attaching the socket filter Alexei Starovoitov
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=179549910801.3135.11117955228755728713@kernel.org \
--to=netdev-bot+sashiko@kernel.org \
--cc=alexei.starovoitov@gmail.com \
--cc=andrew+netdev@lunn.ch \
--cc=andrii@kernel.org \
--cc=ast@kernel.org \
--cc=bpf@vger.kernel.org \
--cc=clementwei90@163.com \
--cc=davem@davemloft.net \
--cc=jasowangio@gmail.com \
--cc=kuba@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