🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #10085

Re: [PATCH net v9 2/2] selftests: net: add TAP socket filter attach tests

#10085
From: netdev-bot+sashiko
Date: Tue, 03 Nov 2026 06:21
134 lines
12593 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 two new negative tests, attach_filter_bad_len_keeps_program()
  and attach_filter_bad_prog_keeps_program() in…
- [Low] The header comment of reattach_filter_without_user_buffer() in
  tools/testing/selftests/net/tun.c says 'a second queue is then…

--

selftests: net: add TAP socket filter attach tests

This adds TUNATTACHFILTER selftests to tools/testing/selftests/net/tun.c.
One reattaches a queue after the user buffer holding the program has been
made unreadable. Others check that IFF_NOFILTER and TUNDETACHFILTER keep a
filter off later queues, and that a bad length or bad instructions are
refused without touching the installed program.

> diff --git a/tools/testing/selftests/net/tun.c b/tools/testing/selftests/net/tun.c
> index 34de2580e32ec..3730fba859a46 100644
> --- a/tools/testing/selftests/net/tun.c
> +++ b/tools/testing/selftests/net/tun.c

[ ... ]

> @@ -548,6 +550,253 @@ TEST_F(tun, reattach_close_delete)

[ ... ]

> +/*
> + * A queue can be attached long after the filter was configured, from a
> + * process that does not necessarily map the buffer the program was copied
> + * from, so the kernel has to keep its own copy of the program.  Here the
> + * mapping is made unreadable before the queue is attached again, and a
> + * second queue is then attached from a fresh socket, which starts without a
> + * filter of its own.
> + */

[Severity: Low]
This isn't a bug, but is this really the second queue? FIXTURE_SETUP(tun)
has already attached self->fd and self->fd2:

    self->fd = tun_alloc(self->ifname);
    ...
    self->fd2 = tun_alloc(self->ifname);

That makes the fd that this test opens and attaches with TUNSETIFF the third
queue. The commit message uses the same wording:

  "It then attaches a second queue from a fresh socket, which starts
  without a filter of its own"

> +TEST_F(tun, reattach_filter_without_user_buffer)
> +{

[ ... ]

> +/* A TUNATTACHFILTER with instructions that bpf_check_classic() rejects must
> + * be refused, and must leave the program installed already in place.
> + */
> +TEST_F(tun, attach_filter_bad_prog_keeps_program)
> +{
> +	struct sock_fprog gf = { 0 };
> +	struct sock_filter bad[] = {
> +		BPF_STMT(BPF_LD | BPF_W | BPF_LEN, 0),
> +		BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0),
> +	};
> +	struct ifreq ifr = { 0 };
> +	short flags = 0;
> +	int fd;
> +
> +	ASSERT_EQ(filter_attach(self->fd, filter_accept,
> +				ARRAY_SIZE(filter_accept)), 0);
> +
> +	errno = 0;
> +	EXPECT_EQ(filter_attach(self->fd, bad, ARRAY_SIZE(bad)), -1);
> +	EXPECT_EQ(errno, EINVAL);

[Severity: Low]
This test and attach_filter_bad_len_keeps_program() both send the bad
ioctl while the two queues from FIXTURE_SETUP(tun) are still attached, so
numqueues is 2.

Would both tests still pass if the early checks in tun_copy_filter() were
removed?

drivers/net/tun.c:tun_copy_filter() {
    ...
	if (!fp.len || fp.len > BPF_MAXINSNS)
		return -EINVAL;
    ...
	err = bpf_prog_validate_classic(insns, fp.len);
    ...
}

Without those checks, tun_attach_filter() would call
sk_attach_filter_kern() on queue 0. bpf_prog_create() rejects len 0 in
bpf_check_basics_ok(). It rejects the bad jump program in
bpf_check_classic(), called from bpf_prepare_filter().

Both paths return -EINVAL before queue 0's sk_filter is changed. The
while (i--) rollback does nothing, and the saved program is not replaced.
So the tests would still see EINVAL, gf.len == 3, a clear IFF_NOFILTER, and
the new queue still getting the filter.

The early check only changes the result when tun->numqueues == 0. A queue
detached with TUNSETQUEUE/IFF_DETACH_QUEUE keeps tfile->tun, because
__tun_detach() only clears it when clean is true. TUNATTACHFILTER therefore
still reaches tun_attach_filter(), where the loop never runs:

	for (i = 0; i < tun->numqueues; i++) {
		...
	}

	tun->filter_attached = true;
	return 0;

Without the early check, the bad program would be saved there. The next
queue attach would then fail in sk_attach_filter_kern().

Could a variant be added that detaches both self->fd and self->fd2 first?
It would expect the bad TUNATTACHFILTER to fail with EINVAL, then reattach
and check that the old filter is installed again. That would test the
"refused before it is saved and before any queue is touched" behaviour that
the tun_copy_filter() comment describes.

[ ... ]

-- 
Sashiko AI review · https://netdev-ai.bots.linux.dev/sashiko/#/patchset/20261030060043.94719-1-clementwei90%40163.com
Technical Details
Message-ID: <179368687880.488856.3347749710559988882@kernel.org>
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-355241-linux-kernel-netdev=dewy.ru
References: <20261030060043.94719-3-clementwei90@163.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!archive.usenet.blueworldhosting.com!usenet.blueworldhosting.com!diablo1.usenet.blueworldhosting.com!eternal-september.org!feeder.eternal-september.org!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-355241-linux-kernel-netdev=dewy.ru
From: netdev-bot+sashiko@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH net v9 2/2] selftests: net: add TAP socket filter attach tests
Date: Tue, 03 Nov 2026 06:21:18 +0000
Organization: Usenet Network
Message-ID: <179368687880.488856.3347749710559988882@kernel.org>
References: <20261030060043.94719-3-clementwei90@163.com>
MIME-Version: 1.0
Content-Type: text/plain; charset="utf-8"
Content-Transfer-Encoding: 8bit
Injection-Info: usenet.network;
	logging-data="3107693"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:hece8EXNzNnXGcyM2cm8xanG544= sha256:nthX7xqbzoxzqAb6S5f9voWk/TyHxQ29rXMeru6sidM=
	sha1:eaeO5AoltOVBFvMwQUf5WWwuceg= sha256:W4fLeUtm0chDBEmIGnLpTOtk1kwQc7m57rKZiStOrPE=
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 9E5A811274A; Tue,  3 Nov 2026 09:24:32 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.401
X-Spam-Status: No, score=-3.401 tagged_above=-9999 required=6.31
	tests=[DCC_REPUT_70_89=0.1, 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 dFdkEZeUdmBJ;
	Tue,  3 Nov 2026 09:24:29 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.105.105.114; helo=tor.lore.kernel.org; envelope-from=netdev+bounces-355241-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 tor.lore.kernel.org (tor.lore.kernel.org [172.105.105.114])
	by mail.dewy.ru (Postfix) with ESMTPS id B351A112745; Tue,  3 Nov 2026 09:24:20 +0300 (MSK)
X-Received: from smtp.subspace.kernel.org (conduit.subspace.kernel.org [100.77.114.92])
	by tor.lore.kernel.org (Postfix) with ESMTP id EF52B2D0A8; Tue,  3 Nov 2026 06:21:24 +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=1793686881;
	b=g5HgzB86Ur3uXvLjnLJp38w+LwUctOJWtsBwHy9Uw84B8b1ervyeU7LTCZyS78tR8Fbz
	 QXQfOm82RQ17UYq8v9TBe6XG/gJLtYl7ZSNQIaX69yhGZjAnulJQtjkLMX2ZXk3ZPa+lN
	 aL/l2Za4wuspQCWQ1DAKqvizREH5e3FDkH9Fidu/c7NdSFZCaW8A58Prqc4BHRxQAD+hE
	 AgxOz1tqx2aC/9JU1N3bGzRkrKlWOuKicit/cQ38ptBfSXslYIvBaweAlsgmj3mPj0ZsL
	 A9n3a80u5A5ZnT3Rsm0KEgcleQ6OxGjR7dzDNSGnXVOLVM7ezXtISzUFl1O3A74SfMg==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793686881;
	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=Dog9q4g48AkCNCojwOcEK7M9FOfotE2But6QLcgKBhI=;
	b=xoYSdouQlmcMzwUschpaYDbh8yVHdLf+40VH0X77PbXnMpD2E8bYKhovNeA3F1J2j8tK
	 6UAfA/MiqsVdexY6SmUGsW5nuGgPaEz+LapX8Z8puoFgrT9HXVx+cCXNxbp+IyqGUAatu
	 I3A1Aunutyq5Iya7w13rpENF295NjXkEmsNEUh5T/kbpZnbpYt+p1KxZ9AsdiYrdZgHoc
	 5RgNnGlCf8lc0awCvNr7Nv0BXPR8BpT+wE343zScx3ByokLhz2fZ6yTqObErCnviCVo56
	 ZpBN51TNSR2vpqvzsiCAaIGlIMscmk5eb+o+YexKngmnKNZ3ZK23r51tf9FMNWgjsZQ==
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=kuCuGf9W;
	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 17103606A332;
	Tue,  3 Nov 2026 06:21:21 +0000 (UTC)
X-Authentication-Results: smtp.subspace.kernel.org;
	dkim=pass (2048-bit key, unprotected) header.d=kernel.org header.i=@kernel.org header.a=rsa-sha256 header.s=k20260515 header.b=kuCuGf9W
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=1793686880;
	b=R43OX5eR5Kuw6clI9sTXqKF/zAJwM4bLJHdlIqu1SG/Q7wj/JpnexBy9gCdw71HdXor2
	 F8g5OmiHV8NG7TrS/jQfgev97IOiFDqwn0Yfv8iSfwttqOYPKxCGglveeEz+y2VXwIDlf
	 TsdRFKBBj58KmFahpeAJMTnHVyTP83Fu3LsfTQyMyLDc3mpSwl9TfbV9GkoLcC3xORGcu
	 EGFzfGM+Rsi8jwWoRitWhiIwFsDAFNIFPeKhafmsQmhyBnAsc/rIiMCXmmR23wiRMN5jR
	 Ma8n1FtNjOXtclLk/mP22Em/0nWe5a1hUfCJtZJo7TWpWe6+YlBWd86fcE+oHpo/kKg==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1793686880;
	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=Dog9q4g48AkCNCojwOcEK7M9FOfotE2But6QLcgKBhI=;
	b=i42V0b4LrtUtycEepUlKKUPa/tU8qBB19Jj1TeADF3gMeG9YGVvp00bYMkB2zSwgiIJl
	 pvhtymuwcrMcv8ObQVqacU6KPiz9hTl+JL7Rdqc5WxZfpkZAziraVtzN1+npn2tT/m9wm
	 J0T0HXvH+kVRHZiM6yOHf1qxDdyqMZ43gfgZ9yhAEiV35wuqFHqx50SIOEhRYcLRkOf14
	 v+A574q/c5FegFPkliZDQUZqtqgC2795wbIN9K5TcSXMM5MttFqX3a/DFJG56T0oSO8cz
	 tx6pQyGOGFjMkl/f0Cw1Ekad1NRrWSFt9ljtVYJSAARULd4HZR5WKsoQbhFibWf5XMg==
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=kuCuGf9W;
	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 EAC36606D80D
	for <netdev@vger.kernel.org>; Tue,  3 Nov 2026 06:21:19 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id 35BEE1F0093A;
	Tue,  3 Nov 2026 06:21:19 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1793686879;
	bh=Dog9q4g48AkCNCojwOcEK7M9FOfotE2But6QLcgKBhI=;
	h=Subject:From:To:Cc:Date:In-Reply-To:References;
	b=kuCuGf9W1JEB54WQ9mtYhQSA/xdN2KviWW36qgomn26UZ4pJHKqLeyEpQaDcm/JB1
	 +Tc8YvfHRyGau53amzJhnZLZE77si+HYpl/C7IMCCYCDNcKPnp/tplTB6tHMrnrB3Y
	 hmdq3XzeMUmF6ZPqdbUVzwbo9ANyY3x/LtBFHR0uNTUtK4CTxnqyUagkib8trZXcPL
	 R4GaLkDMGzfBV61S42u71aA60ugpZ84gH2kw9lT+yzKzbBjGz+gjRKV9cXUFJC1hGY
	 RfvC7ER0azBufFZbBfyLDMXofBePDfUINZOfTI/Evlq7OGj8BRhYjSTA7+nnvZE/4/
	 XSb+dfVgmKwUQ==
X-Cc: netdev@vger.kernel.org, willemdebruijn.kernel@gmail.com,
 jasowangio@gmail.com, andrew+netdev@lunn.ch, davem@davemloft.net,
 kuba@kernel.org, weirongguang@kylinos.cn
X-In-Reply-To: <20261030060043.94719-3-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