🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #8139

Re: [PATCH net v7 1/2] tun: keep a kernel copy of the socket filter program

#8139
From: Rongguang Wei
Date: Wed, 28 Oct 2026 06:48
140 lines
14373 bytes
on 2026/10/27 22:05, Willem de Bruijn wrote:
> netdev-bot+sashiko@ wrote:
>> Thank you for your contribution! Sashiko AI review found 2 potential
>> issue(s) to consider.
>>
>> Critical: 0 · High: 1 · Medium: 0 (2) · 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).
>>
>> - [High] bpf_prog_validate_classic() does not run "the same checks that
>>   bpf_prog_create() performs", although its kernel-doc and the commit…
>> - [Low] The kernel-doc for the new exported helper
>>   bpf_prog_validate_classic() (net/core/filter.c) says it returns "0 if…
>>
>> Pre-existing issues:
>> - [Medium] This problem wasn't introduced by this patch, but a filter
>>   replacement that passes tun_copy_filter() and then fails on some queue…
>> - [Medium] This problem wasn't introduced by this patch, but
>>   TUNDETACHFILTER leaves disabled queues with their old socket filter,…
>>
>> --
>>
>> tun: keep a kernel copy of the socket filter program
>>
>> This commit keeps a kernel copy of the classic BPF program passed to
>> TUNATTACHFILTER in tun->fprog_kern. Each queue's filter is now built from
>> that copy with a new sk_attach_filter_kern() helper. It also fixes the
>> inverted error check in tun_attach(), and it validates the program up
>> front in tun_copy_filter() with a new bpf_prog_validate_classic() helper.
>>
>> Link: https://lore.kernel.org/netdev/179027275318.2160803.4185895144088175048@kernel.org/
>>
>>> diff --git a/drivers/net/tun.c b/drivers/net/tun.c
>>> index 242899f7fd071..e7d9f4480a89f 100644
>>> --- a/drivers/net/tun.c
>>> +++ b/drivers/net/tun.c
>>
>> [ ... ]
>>
>>> +	/* The instructions are only verified when a program is built from
>>> +	 * them, which tun_attach_filter() does once per attached queue.
>>> +	 * Check them here as well: with no queue attached that would not
>>> +	 * happen at all, and, now that tun_attach() treats that error as
>>> +	 * fatal, every later attach would fail with -EINVAL instead of
>>> +	 * rejecting the program now.
>>> +	 */
>>> +	err = bpf_prog_validate_classic(insns, fprog.len);
>>> +	if (err) {
>>> +		kfree(insns);
>>> +		return err;
>>> +	}
>>
>> [Severity: High]
>> Does bpf_prog_validate_classic() really reject everything that
>> bpf_prog_create() rejects? Its kernel-doc and the commit message both say
>> it does:
>>
>>   "The instructions are verified with the new bpf_prog_validate_classic(),
>>   which runs the same checks as bpf_prog_create() on the copied program."
>>
>> The helper only runs bpf_check_basics_ok() and bpf_check_classic().
>> bpf_prog_create() also goes through this path:
>>
>> bpf_prog_create()
>>   bpf_prepare_filter()
>>     bpf_migrate_filter()
>>       bpf_convert_filter()
>>     bpf_prog_select_runtime()
>>       __bpf_prog_select_runtime()
>>
>> bpf_convert_filter() rejects any jump whose converted offset does not fit
>> in s16:
>>
>> 	if (off < off_min || off > off_max)
>> 		goto err;
>>
>> __bpf_prog_select_runtime() can also return -ENOTSUPP when a JIT is
>> required but did not run.
>>
>> Take a 4096 instruction program: BPF_JMP|BPF_JA with k=4094 at pc 0, then
>> 4094 BPF_LD|BPF_B|BPF_ABS loads of offset 0, then BPF_RET|BPF_K.
>> bpf_check_classic() accepts it. convert_bpf_ld_abs() expands each load
>> into 12 eBPF instructions, so the JA offset grows to about 49128, and
>> bpf_convert_filter() fails with -EINVAL.
>>
>> Suppose every queue on a multiqueue TAP has been disabled with
>> IFF_DETACH_QUEUE. tun_copy_filter() stores this program, and
>> tun_attach_filter() loops over zero queues and then does:
>>
>> 	tun->filter_attached = true;
>>
>> >From then on, every IFF_ATTACH_QUEUE, and every TUNSETIFF without
>> IFF_NOFILTER, calls sk_attach_filter_kern() in tun_attach() and gets
>> -EINVAL. With the corrected check, tun_attach() then takes the goto out
>> path and the queue is never published.
>>
>> Isn't this the "every later attach would fail with -EINVAL" case that the
>> comment above says is avoided?
>>
>> When queues are active, the same program fails at queue 0, and
>> tun_attach_filter() calls tun_detach_filter(tun, 0). That frees fprog_kern
>> and clears filter_attached, which doesn't seem to match this part of the
>> commit message:
>>
>>   "a TUNATTACHFILTER that no verifier would accept now fails early and
>>   leaves the previously attached filter in place."
>>
>> The sk_attach_filter_kern() kernel-doc in this same patch lists -EINVAL
>> from bpf_prog_create() and -ENOTSUPP as possible results. That also
>> suggests the two checks are not equivalent.
>>
>> Would it be more reliable for tun_copy_filter() to build the program once
>> with bpf_prog_create() and then destroy it, instead of calling
>> bpf_prog_validate_classic()?
> 
> This sounds to me as the perfect being the enemy of the good.
> 
> Even the addition of calling bpf_prog_validate_classic is a nice to have.
> This series fixes a real issue.
> 
> Yes, some issues are still only discovered later when the BPF program is
> built. But as long that late discovery is also handled well, that is
> acceptable.
Thanks Willem.

Rather than building the program twice, v8 keeps the cheap check and
commits the copy only after every queue accepted it. A late failure
drops the copies already installed and leaves tun->fprog, fprog_kern and
filter_attached alone. With numqueues == 0 the program is stored and the
first attach fails with -EINVAL.
In selftest, attach_filter_bad_prog_keeps_program() now also checks 
IFF_NOFILTER and a newly attached queue.


pw-bot: cr
Technical Details
Message-ID: <9708bebe-2e54-46bc-b9a8-97ad59b37793@163.com>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-352746-linux-kernel-netdev=dewy.ru
References: <20261023092239.186665-2-clementwei90@163.com> <179309578401.3134.11519975030981154705@kernel.org> <willemdebruijn.kernel.39836b1948cab@gmail.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-352746-linux-kernel-netdev=dewy.ru
From: clementwei90@163.com (Rongguang Wei)
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net v7 1/2] tun: keep a kernel copy of the socket filter
 program
Date: Wed, 28 Oct 2026 14:48:37 +0800
Organization: Usenet Network
Message-ID: <9708bebe-2e54-46bc-b9a8-97ad59b37793@163.com>
References: <20261023092239.186665-2-clementwei90@163.com>
 <179309578401.3134.11519975030981154705@kernel.org>
 <willemdebruijn.kernel.39836b1948cab@gmail.com>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="1716413"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:sD0nWA0Ucn3nt/uX49OIPoed8Gc= sha256:7SLpZK75Zk/If1pIrBu6OlBNJ80MRM25eMdKth5PjpY=
	sha1:a7s4yMtEywIc7ZCCwxPJb5dwKp8= sha256:kfLkQO9IdHRlNhZy8O0eCClNyNfoL4RGl/nBuow2Uz0=
Content-Language: en-US
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 73ACC125C2F; Wed, 28 Oct 2026 09:49:36 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.048
X-Spam-Status: No, score=-3.048 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, DKIM_SIGNED=0.1, DKIM_VALID=-0.1,
	DKIM_VALID_AU=-0.1, FREEMAIL_FORGED_FROMDOMAIN=0.001,
	FREEMAIL_FROM=0.001, HEADER_FROM_DIFFERENT_DOMAINS=0.25,
	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 (1024-bit key)
	header.d=163.com
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 E-0ZSXqoJk2F;
	Wed, 28 Oct 2026 09:49:28 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.234.253.10; helo=sea.lore.kernel.org; envelope-from=netdev+bounces-352746-linux-kernel-netdev=dewy.ru@vger.kernel.org; receiver=<UNKNOWN> 
X-Authentication-Results: OpenDMARC; dmarc=fail (p=none dis=none) header.from=163.com
X-Received: from sea.lore.kernel.org (sea.lore.kernel.org [172.234.253.10])
	by mail.dewy.ru (Postfix) with ESMTPS id 243A0125C2D; Wed, 28 Oct 2026 09:49:19 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by sea.lore.kernel.org (Postfix) with ESMTP id 348672E654; Wed, 28 Oct 2026 06:49:12 +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=1793170151;
	b=lo8d90gr8geVQUkFHo8rZTiWGYJnDah+5grqwhEsYyN6Eeludyc5LyIlHq9t1xtEFk1x
	 lx4r1JOtGS1DIUmAEyobrrB2aFK3zajeCuYVZM7USSp13HpGqhytRUMxDwvCgKxEfAfV3
	 qIocTwLeEWpXTk0Vk5lJQbtd86Hf0k8hna5Yfl8oUZKg/OJS+GBqq/H6inmUX8Ya7ydL6
	 A2Jhv0Nc5VMG90T8GZoMI/C4XQ14b4u1Cp7O63a2RV30ppYjt8m9CWRYL5jdufaTQuCtU
	 02nR+p6DeNAa5gH9lfZdm33Z7RO/mMWO9S3D6mBhXre97VZAMX/R3L1vsK/u+kFjwFQ==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793170151;
	h=X-Original-To:Received:DMARC-Filter:DKIM-Signature:Message-ID:Date:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:User-Agent:
	 Subject:To:Cc:References:Content-Language:From:In-Reply-To:
	 Content-Type:Content-Transfer-Encoding:X-CM-TRANSID:
	 X-Coremail-Antispam:X-CM-SenderInfo;
	bh=LLbPG2usd0qbwQ4hO+AgmOgJRcy+RI4oQWi93X1+FLE=;
	b=RL8KVZC3zkBxQSIubYQaPA7+LAJSnCogfX4WxC8enwGOuprhzCdEG98DFcfpRYQxxgFW
	 NZ+10KMeF9XCwhlo+x9dKgiwpxVRUeIxpt4P/WyTSJe+qfkNCGXgZYCDs/U+54AF5eIrX
	 9YAO6LvMAAWtT3YJgvSnUGdXrIM+lg+at+agF8200gT8lVbUjGNy4tG04nUuUjQ3X9hI7
	 8gWJl6AL4Aub0uCcdPOanPcw3j2J8vBiWcivoKfLIU9lpsSN9Zs6Wt+UhQq929gcQxkpg
	 MxV6sc5L2R4AoM8PwKq6qKs8zmUa/1c8iLZmXdsgmvqilKCUIgEEdc1i8quHkJWavpw==
X-ARC-Authentication-Results: i=2; smtp.subspace.kernel.org; 
	dkim=pass header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=a3YPGqOw;
	dmarc=pass header.from=163.com;
	spf=pass smtp.mailfrom=163.com;
	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 B5B53606A323;
	Wed, 28 Oct 2026 06:49:11 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (1024-bit key, unprotected) header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=a3YPGqOw
X-Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=117.135.210.4
X-ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none;
	t=1793170150;
	b=aBCx/3Ul42ORT8aubALhQUzH755MB+N/b0ahnGuzMUP3pzz/4SaKOX7baufdUE25zT2b
	 Aq5L+qa8uPWeeeAxkULwRmInQ4qVrHxnCvJmVe8TOOoE/jmLJic7IkRCbtXX0DA+ZsW5b
	 oUogoTUQuIjR9/lyKTEE9BEhtw3sebJKcyrYlO15GaCDaVfVXuGgnssHURoqalFnEdPji
	 31Eqte2bjMWoBmM5IcSCIRz6n5Zp9KZzdydkbMtbXbDWSBxYljM3igJqPvgg6LihBhtUs
	 h3EbX0DFCo1lhcqmhE8cWQW2KOgaavlkruwaGN/sJjNQGWSpcIxaT5HvrtRqDk0i1lA==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793170150;
	h=DMARC-Filter:DKIM-Signature:Message-ID:Date:MIME-Version:User-Agent:
	 Subject:To:Cc:References:Content-Language:From:In-Reply-To:
	 Content-Type:Content-Transfer-Encoding:X-CM-TRANSID:
	 X-Coremail-Antispam:X-CM-SenderInfo;
	bh=LLbPG2usd0qbwQ4hO+AgmOgJRcy+RI4oQWi93X1+FLE=;
	b=5HJ0qdSn4TvIwBqeNZAXHZqZNdw330NXlLKFoOVkb/+6YcFIarLaaPBt4dk9gjRV4zI9
	 xMpeILwIWYb0HeCPnAXXBR+2SJI3qzQFRGVo5GCWg46KBh0nWcjITt7KisP+Eca77drbI
	 4/ziQ6VnARuRorExpgdbtvLEvLiFMBaCMeUycC/YQb+Af23gZLwS2wZQYkQP7YxYGTdVy
	 17TdLqMFFe2oaD8Zu07uzFyoRV2Qcy/7qnUIivcmaj4FprhWhfl/lKSOf6kcOTk5TiSf6
	 T5vFfPNNbeW/Pac5vZWjT0T/LIgR8RaJfWNpMMS9WMCmMxxpCJcdQM/r5srOA9lCn+Q==
X-ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; 
	dmarc=pass header.from=163.com;
	spf=pass smtp.mailfrom=163.com;
	dkim=pass header.d=163.com header.i=@163.com header.a=rsa-sha256 header.s=s110527 header.b=a3YPGqOw;
	arc=none smtp.remote-ip=117.135.210.4
X-Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.4])
	(using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
	 key-exchange X25519 server-signature ECDSA (prime256v1) server-digest SHA256)
	(No client certificate requested)
	by smtp.subspace.kernel.org (Postfix) with ESMTPS id 06C98606A31A
	for <netdev@vger.kernel.org>; Wed, 28 Oct 2026 06:49:07 +0000 (UTC)
X-DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org 06C98606A31A
X-Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=163.com
X-Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=163.com
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=163.com;
	s=s110527; h=Message-ID:Date:MIME-Version:Subject:To:From:
	Content-Type; bh=LLbPG2usd0qbwQ4hO+AgmOgJRcy+RI4oQWi93X1+FLE=;
	b=a3YPGqOwr+umV+b6QAXPr7c0PbfoYooCI76ZH/DQ+tufl04FT5ooyw+wus7O5R
	Oep5C1at267coBCdLSlaF+HRpKCxfCpdxVCTIBARc/8d7LKoACgv+pqHRpnLrvfo
	DdVOxKr0Rz23aSK4QVfK3BFHD/q3gc39GrdjJ0E0IpgYo=
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
X-User-Agent: Mozilla Thunderbird
X-Cc: netdev@vger.kernel.org, jasowangio@gmail.com, andrew+netdev@lunn.ch,
 davem@davemloft.net, kuba@kernel.org, weirongguang@kylinos.cn
X-In-Reply-To: <willemdebruijn.kernel.39836b1948cab@gmail.com>
X-CM-TRANSID: PygvCgBXWG_FmuFqNe8yCg--.56742S2
X-Coremail-Antispam: 1Uf129KBjvJXoWxtFyfJw1kCrW7Jw1fArykGrg_yoW7trWxpF
	WfWa4jvr1DWr40qwn2yryUZFyftwsrWF47Xrn8J348uFsIgryxu3y3Ca909asxArWrGw1F
	qa1jvasFgwn8ZaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2
	9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07UsvttUUUUU=
X-CM-SenderInfo: 5fohzv5qwzvxizq6il2tof0z/xtbC4gnrV2rhmsktowAA3B