πŸš€ go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #21530

Re: [PATCH net v12 1/2] tun: run the socket filter from tun_net_xmit()

#21530
From: netdev-bot+sashiko
Date: Tue, 24 Nov 2026 05:45
167 lines
14145 bytes
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
Technical Details
Message-ID: <179549910801.3135.11117955228755728713@kernel.org>
Path: mbox-mo1.pugleaf.net!news-sfo.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370021-linux-kernel-netdev=dewy.ru
References: <20261123054209.89946-2-clementwei90@163.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-sfo.pugleaf.net!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-370021-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
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
Organization: Usenet Network
Message-ID: <179549910801.3135.11117955228755728713@kernel.org>
References: <20261123054209.89946-2-clementwei90@163.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="4062375"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:QDXzanEPq4xehZqODCmYcsdfvC4= sha256:ceft3ovebYcafNstPhUgE0nH8dt9S37WMPDTcvVvuhA=
	sha1:tdUTW9X/mQTkO+NqcREBaH+7lX8= sha256:N7TdqJqhHavm8jeTWuq5a/zTCglEtCEhVWyeHmnZqlA=
List-ID: <netdev.vger.kernel.org>
X-Mailpost-Empty-Hdrs: X-Spam-Level
X-Received: from localhost (localhost [127.0.0.1])
	by mail.dewy.ru (Postfix) with ESMTP id 9DCF7125DD0; Tue, 24 Nov 2026 08:45:27 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.501
X-Spam-Status: No, score=-3.501 tagged_above=-9999 required=6.31
	tests=[DKIMWL_WL_HIGH=-0.001, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, MAILING_LIST_MULTI=-1,
	RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001]
	autolearn=ham autolearn_force=no
X-Authentication-Results: mail.dewy.ru (amavisd-new); dkim=pass (2048-bit key)
	header.d=kernel.org
X-Received: from mail.dewy.ru ([127.0.0.1])
	by localhost (mail.dewy.ru [127.0.0.1]) (amavisd-new, port 10024)
	with ESMTP id 837NgJW7gFDa;
	Tue, 24 Nov 2026 08:45:23 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-370021-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=pass (p=quarantine dis=none) header.from=kernel.org
X-Received: from sto.lore.kernel.org (sto.lore.kernel.org [172.232.135.74])
	by mail.dewy.ru (Postfix) with ESMTPS id 3235A125DCE; Tue, 24 Nov 2026 08:45:14 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sto.lore.kernel.org (Postfix) with ESMTP id 8CC4A601820; Tue, 24 Nov 2026 05:45:13 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org; arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-ARC-Seal: i=2; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=pass;
	t=1795499110;
	b=hh4GGW4a9kARYOt3f1MkeL93cNVSQHD5hf1ddNsWtSf55Va07Oy2vmmNTBVBQ4Bop43o
	 gpGr+kW7e+EK9Bmvne3GdRtGhppkNvxXGSBGUBFEigjtVLHaDwdQOKOUIe3IrpNqfA69y
	 4JnMc3HvrHF0G5AHb15onRXWEzV+j3aVWcNrpCPFk7OTNwQkvUUk8U/5uZ82crDnhCizV
	 b3RU30nj67mxZ/aspGu6tt/K76Ny+b7rjPF8h01HSTiRdquT0fE70T6/TjR/4LoetbpvX
	 14XOV3YyTYqtIpPLnu72Wn0qYeN/IYFd/FqB5VB0zJdbEBhtxYmrOYbRjjFZ2MvI2ww==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795499110;
	h=X-Original-To:Received:Received:DKIM-Signature:Subject:From:To:Cc:
	 Date:Message-ID:In-Reply-To:References:X-sashiko-severity:
	 Content-Type:Content-Transfer-Encoding:X-Mailing-List:List-Id:
	 List-Post:List-Help:List-Subscribe:List-Unsubscribe:List-Owner:
	 Precedence:MIME-Version;
	bh=aRffa5QGJyeDL2fDxuIlhM+fU5kvvSRm5hMdsycYtrw=;
	b=FSjfvuP1gbOC1yR76PlRh99rhBGNYTGa4LovYqGW7uEQubwxErDRtCAfAHKtE03hAWtp
	 05OfgeBN1EHM/huZaX9GaoNWXj+KjM7RRCirEUYXkEmwoEJbSW2zlBvUy3zyv5znfRHPA
	 VCgzRk661iz0jPHGGe6EQzHfXh0/TXP+LQvu8LvC6zVEK8JwOTUkd+p3xyOiAenU04jq2
	 sszgzv4v/1zfg03bnndD3p6NGQx9WLEdOcbP1IMm/dA2xbRUjppCKSwZ6+fJVhdWAgxTB
	 8eUVueoXyeYLeefccASl6nfRcPP6WdH0+uD5Rb3VOa/djFde8DiGMv3p+cmG0Lp85TQ==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=eJ54UdvX;
	arc=pass header.oldest-pass=0 smtp.remote-ip=127.0.0.1
X-Received: from localhost.localdomain (localhost [127.0.0.1])
	by smtp.subspace.kernel.org (Postfix) with ESMTP id 7311D606A336;
	Tue, 24 Nov 2026 05:45:10 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=eJ54UdvX
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=100.103.45.18
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1795499109;
	b=uWO2TyqddpJj1S0b2VJEll5XAIlkWkIBqQjUef6k2Mxzgx/OX47mNKMjR107hHpb5quE
	 llhZRYm1bxkiAA2sZDZaykQjDFd7/gJe4TMQ7mFlZCVgGNUcy4S944Z5WLMcesy+1kZ+A
	 vXcXSTEGufuE9nmZvelffKQbz06OMkgC+BJW1rjHvIRpmBJCbZLQbuIibLxtF1FqSkaev
	 bORouSfJkzD9+8Js4yahkV8P6WlgXDrk9ul+9IMs4v/z3Yn7pmBOGoEb07kFQnodJcRTI
	 PqsD+rDnHK1f9eB/hpoMOGoH4e18F/dl9NWr5QShN5T4nGvDBxzHztRpG2Zc82EFvsg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1795499109;
	h=Received:DKIM-Signature:Subject:From:To:Cc:Date:Message-ID:
	 In-Reply-To:References:X-sashiko-severity:Content-Type:
	 Content-Transfer-Encoding:MIME-Version;
	bh=aRffa5QGJyeDL2fDxuIlhM+fU5kvvSRm5hMdsycYtrw=;
	b=fYMMJdxjZMNB8vJoxWsSHprzBnsBRy7PlV+c5BCy2c4DzmsjfLWlvBF9yZQdvH+uGmv2
	 xn5Vk4lmRYR+6lueDJLyZwYfCtTQ49/1Nw4PksK9xFKWX+VjnSR1kKrIOgLz7dU23gci5
	 2fU3hvG5yd9NZczgHN1eyHU2htLfUq57NDw4JJM9QmXyt0+8ZEix8bowsKjmo6dT/61gx
	 yJHTbnqz2psKV2EqjBrTKIj8Q/EVOc8GFNtSrYoXV5dRn2mF3YIt3vKU23BzfrNpAyJNM
	 0gjieD5nZ+7ztMjD8yirHr1fhoTYxLBE3wLPafKqYX/dXb5JnJIIDm6fSr+fqhn/MCg==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dkim=pass header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=eJ54UdvX;
	arc=none smtp.remote-ip=100.103.45.18
X-Received: from smtp.kernel.org (quasi.space.kernel.org [100.103.45.18])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1))
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 8F2B4606A31E;
	Tue, 24 Nov 2026 05:45:09 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 7B09C1F000C6;
	Tue, 24 Nov 2026 05:45:08 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1795499109;
	bh=aRffa5QGJyeDL2fDxuIlhM+fU5kvvSRm5hMdsycYtrw=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=eJ54UdvXTIbCw6STyIHaf4Jsz7ZjHQmIkuCxT4ozyeVyAJPnm5BWF30vBfvRpLq2N
	 gQl229cL36hQlq3hnPUDagQzdMNmg0FkU/qc6PSEq6IKnYomQS89oRESJD13yFqKYG
	 9/BHYtL48HIzxWvGxHT1AhXxAXvKYeopoIyWpJDwC8gRpNp8u5tU5u615fT2sAC84O
	 jNmNn9KmF2s288MhIGEP3DZF/OU0hxIGTGy93ktgsgUg4pWEN3smxvNGjrs0Nax4G1
	 DqFgbYTVjGV1k9ivno02YFSIMEazoJ6CU0sVd2tfzJnrv+WKOOkztDPuwHKjaN98P+
	 d4r2VgGdYbBIQ==
X-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
X-In-Reply-To: <20261123054209.89946-2-clementwei90@163.com>
X-sashiko-severity: Low
X-Mailing-List: netdev@vger.kernel.org
X-List-Post: <mailto:netdev@vger.kernel.org>
X-List-Help: <mailto:netdev+help@vger.kernel.org>
X-List-Subscribe: <mailto:netdev+subscribe@vger.kernel.org>
X-List-Unsubscribe: <mailto:netdev+unsubscribe@vger.kernel.org>
X-List-Owner: <mailto:netdev+owner@vger.kernel.org>
X-Precedence: list