🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: linux.kernel.netdev
Article #27318

Re: [PATCH bpf 1/2] bpf: Give a raw_tp test_run program the largest context when it does tail calls

#27318
From: bot+bpf-ci
Date: Sat, 05 Dec 2026 09:35
163 lines
15077 bytes
--===============8229385683500932510==
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

> bpf: Give a raw_tp test_run program the largest context when it does tail calls

TL;DR:
- Fix may be incomplete: an extension adding tail calls to a non-tail-call
  raw_tp program can still cause an out-of-bounds ctx read
- U16_MAX padding looks excessive; 96 bytes suffices
- Changelog example and Fixes: tag look wrong

> diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
> --- a/net/bpf/test_run.c
> +++ b/net/bpf/test_run.c

[ ... ]

> @@ -809,10 +810,18 @@ int bpf_prog_test_run_raw_tp(struct bpf_prog *prog,
>  	if (prog->sleepable && (kattr->test.flags & BPF_F_TEST_RUN_ON_CPU))
>  		return -EINVAL;
>
> -	if (ctx_size_in) {
> -		info.ctx = memdup_user(ctx_in, ctx_size_in);
> -		if (IS_ERR(info.ctx))
> -			return PTR_ERR(info.ctx);
> +	/* the target of a tail call can use that much */
> +	if (prog->aux->tail_call_reachable)
> +		size = max_t(u32, size, U16_MAX);
                 ^^^^

Is this fix complete?  It only pads the context when the program being run
is itself tail_call_reachable, but an extension attached to it can add a
tail call.

The commit message calls this the raw_tp instance of 08c987904970, which
also changed bpf_tracing_prog_attach() so an extension cannot add tail
calls to a program that has none:

    if (prog->type == BPF_PROG_TYPE_EXT && tgt_prog->type == BPF_PROG_TYPE_SYSCALL &&
        prog->aux->tail_call_reachable && !tgt_prog->aux->tail_call_reachable) {

There is no equivalent check for BPF_PROG_TYPE_RAW_TRACEPOINT, and nothing
in bpf_check_attach_target(), bpf_tracing_prog_attach() or
bpf_freplace_link_tgt_prog() looks at tail_call_reachable.

Consider this sequence:

1. Load raw_tp program A with a global subprog
   sub(struct bpf_raw_tracepoint_args *ctx) and no tail calls.
   A->aux->tail_call_reachable is false and max_ctx_offset is 0.

2. Load extension E for sub() that calls bpf_tail_call(ctx, &jmp, 0).
   E's prog array takes the owner type RAW_TRACEPOINT via
   resolve_prog_type(), and bpf_prog_hook_key() is 0 for both E and a
   raw_tp program.

3. Put raw_tp program B, which reads *(u64 *)(ctx + 88), into jmp.
   Attach E to A.  The max_ctx_offset check passes because E's is 0.

4. Run BPF_PROG_TEST_RUN on A with ctx_size_in = 8.

Since tail_call_reachable is false for A, kvzalloc(8) is used.  A calls E,
E tail-calls B, and B reads 88 bytes past the allocation, which is the same
slab-out-of-bounds read this commit fixes.  With ctx_size_in = 0,
info.ctx is NULL and B dereferences NULL + 8.

Would a check in bpf_tracing_prog_attach() that rejects an EXT program with
tail_call_reachable on a RAW_TRACEPOINT target without it, matching the
SYSCALL check, close this?  The other patch in the series (90faffad4172
"selftests/bpf: Test raw_tp test_run ctx padding for tail calls") only adds
a selftest.

This isn't a bug, but is U16_MAX the right bound here?  Only
BPF_PROG_TYPE_SYSCALL gets variable context offsets bounded by U16_MAX
(is_var_ctx_off_allowed()).  A raw_tp program, and so any tail call target
(same prog type per __bpf_prog_map_compatible()), reaches its context only
through bpf_tracing_ctx_access():

    if (off < 0 || off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS)
        return false;

So no raw_tp target can use more than MAX_BPF_FUNC_ARGS * sizeof(u64) = 96
bytes, and bpf_prog_test_run_raw_tp() already caps ctx_size_in at that
value a few lines up.  The comment "the target of a tail call can use that
much" does not hold for raw_tp.

Every test run of a tail-calling raw_tp program now allocates and zeroes
64 KiB instead of 96 bytes.  That is above PAGE_SIZE, so kvmalloc tries a
high-order kmalloc without direct reclaim and then falls back to vmalloc,
all charged to the memcg.

Could the padding be MAX_BPF_FUNC_ARGS * sizeof(u64), matching the existing
upper bound?

[ ... ]

commit c281503673d7081d45e1237362eed5e7f4872c12
Author: Pragyansh Chaturvedi <r41k0u@ubuntu.com>

>     bpf: Give a raw_tp test_run program the largest context when it does tail calls
>
>     BPF_PROG_RUN for RAW_TRACEPOINT compares ctx_size_in with the
>     max_ctx_offset of the program that it runs. The target of a tail call
>     gets the same context:
>       first:   bpf_tail_call(ctx, ...)      max_ctx_offset 0, ctx_size_in 8
>       target:  r0 = *(u64 *)(ctx + 96)      out of bounds
>     The target is not known before the run. Pad the copy of the context
>     with zeros up to U16_MAX, no program can use more.
>     This is the raw_tp instance of commit 08c987904970 ("bpf: Give a
>     syscall program the largest context when it does tail calls"), which
>     applied the same fix to the syscall test_run path only. With KASAN
>     and BPF_JIT_KASAN the raw_tp path reports:
>       BUG: KASAN: slab-out-of-bounds in bpf_prog_..._target+0x2a/0x259
>       Read of size 8 at addr ffff88810486a818 by task rawtp_chain_leak/285
>        bpf_prog_..._target+0x2a/0x259
>        __bpf_prog_test_run_raw_tp+0x5b8/0x900
>        bpf_prog_test_run_raw_tp+0x42f/0x7e0
>       The buggy address belongs to the cache memdup_user-8 of size 8
>     The copy is accounted now. It comes from kmalloc-cg or vmalloc, not
>     from the caches of memdup_user().
>     Fixes: 04fd61ab36ec ("bpf: allow bpf programs to tail-call other bpf programs")
>     Signed-off-by: Pragyansh Chaturvedi <r41k0u@ubuntu.com>

The example target in the changelog cannot be loaded:

>       target:  r0 = *(u64 *)(ctx + 96)      out of bounds

raw_tp_prog_is_valid_access() calls bpf_tracing_ctx_access(), which rejects
off >= sizeof(__u64) * MAX_BPF_FUNC_ARGS (96), so the verifier refuses this
load.  The largest valid 8-byte read is at ctx + 88, which is the last load
in the selftest added in 90faffad4172.

The changelog also says:

>     This is the raw_tp instance of commit 08c987904970 ("bpf: Give a
>     syscall program the largest context when it does tail calls")

but it leaves out that commit's extension check in bpf_tracing_prog_attach()
(see the first comment above).  Also, 08c987904970 is not an ancestor of
this tree, where bpf_prog_test_run_syscall() still uses memdup_user().

The Fixes: tag looks wrong:

>     Fixes: 04fd61ab36ec ("bpf: allow bpf programs to tail-call other bpf programs")

That commit predates BPF_PROG_TEST_RUN for raw_tracepoint and does not
touch the test_run ctx handling.  The out-of-bounds read comes from
bpf_prog_test_run_raw_tp() sizing the ctx copy from ctx_size_in and
checking it only against the first program's max_ctx_offset, which was
introduced by 1b4d60ec162f.  Should this be:

  Fixes: 1b4d60ec162f ("bpf: Enable BPF_PROG_TEST_RUN for raw_tracepoint")


---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md

CI run summary: https://github.com/kernel-patches/bpf/actions/runs/47503569594
--===============8229385683500932510==--
Technical Details
Message-ID: <5d85b4a1b480921710d7d38567f5aecf9867d43a3ec6ee8dd3f88203c617f7d5@mail.kernel.org>
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377919-linux-kernel-netdev=dewy.ru
References: <20261205090934.3813585-1-r41k0u@ubuntu.com>

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!feed-us-txt.newsdeef.eu!feed-us-ny.newsdeef.eu!srv.newsdeef.eu!feed-eu-txt.newsdeef.eu!feed-eu-ch.newsdeef.eu!srl.newsdeef.eu!news.mb-net.net!open-news-network.org!news.nntp4.net!nntp.xenet.de!news.neodome.net!usenet.network!.POSTED!vger.kernel.org!netdev+bounces-377919-linux-kernel-netdev=dewy.ru
From: bot+bpf-ci@kernel.org
Newsgroups: linux.kernel.netdev
Subject: Re: [PATCH bpf 1/2] bpf: Give a raw_tp test_run program the largest context when it does tail calls
Date: Sat,  5 Dec 2026 09:35:21 +0000 (UTC)
Organization: Usenet Network
Message-ID: <5d85b4a1b480921710d7d38567f5aecf9867d43a3ec6ee8dd3f88203c617f7d5@mail.kernel.org>
References: <20261205090934.3813585-1-r41k0u@ubuntu.com>
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="===============8229385683500932510=="
Injection-Info: usenet.network;
	logging-data="2998988"; mail-complaints-to="abuse@usenet.network"
Cancel-Lock: sha1:iYfXBec3q9LXxkdgTuomjxZmU0M= sha256:tTO9klUKNyjiJZHYnWK+LdP8QPBggihONK7wEtHrj3M=
	sha1:0L9OxRT5fC6kp/aGqFKc21AIaUU= sha256:n7ETvp1WSCcAPUUkqLtXi7kEsyE628PhpWRi4KxWVe0=
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 10F84117DC9; Sat,  5 Dec 2026 12:35:49 +0300 (MSK)
X-Virus-Scanned: Debian amavisd-new at usenet.network
X-Spam-Flag: NO
X-Spam-Score: -3.642
X-Spam-Status: No, score=-3.642 tagged_above=-9999 required=6.31
	tests=[DKIMWL_WL_HIGH=-0.142, 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 AICucFJVgnCR;
	Sat,  5 Dec 2026 12:35:45 +0300 (MSK)
X-Received-SPF: Pass (mailfrom) identity=mailfrom; client-ip=172.232.135.74; helo=sto.lore.kernel.org; envelope-from=netdev+bounces-377919-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 19B8A117DC3; Sat,  5 Dec 2026 12:35:33 +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 0531B605E94; Sat,  5 Dec 2026 09:35:31 +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=1796463329;
	b=k8gExzcoM9M8Q7JXf551zSasqZTBdN0zwXWLWHlaNAfH6yQew6xLEJPo69on/COhcsz0
	 6789nnfJq6HX4kK3Kym/eQ2Mpn0WqYEOUHcs/45XGHW3aS1yaSeHagZl/+r4IJU0hRYSf
	 RSMibwohVXZKhI9eszVvMShNbEqeDQXx/LB5GUxRRfRgSfS4tY/9AHPU/xwXTvhNakEU2
	 w/GfPOIsrTcAo4WzbnmoXAUy+6WBRK8JJ2ILTC39Zv5+Q2D2MsgD/FdPZMrQX9LTYsY4t
	 9peCEKvg65zASxIoD0w2QEtNQlhodbPfCA/rp86M/xj5tOlwBZaStiv40XFtsFjjxFw==
X-ARC-Message-Signature: i=2; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796463329;
	h=X-Original-To:Received:Received:DKIM-Signature:Content-Type:
	 X-Mailing-List:List-Id:List-Post:List-Help:List-Subscribe:
	 List-Unsubscribe:List-Owner:Precedence:MIME-Version:Message-Id:
	 In-Reply-To:References:Subject:From:To:Cc:Date;
	bh=wyFp9xkJLbBaisQNimxOba15tTTs2Q5UmIDKQF1xduU=;
	b=gqL5RzjZF3KfrAAWm0cw+eON2HNITZBrM9CqM7tPJApzPVo0vDdR8RV5e+/Rowb1/vHf
	 Ft4+1YiLv1P/UjE19opGYnyY+N4rpzhDu8NqsOUJSJE0EEyd6t1qEzQVoGKM4rRJt+ZCu
	 1RWX/UwBVPRKL7yk+E3EIOzL80ehixaVz1kiwYaFlyxZBXd2ipHuGTBy3yb98O7pC/rAs
	 4wiEnPaVLV0tq26hF+n015BGDxIxY5EzvNEgo+Ca8q6NT45WYYt52zoHJA9VxnJeq2ezl
	 O1yXi8QbwFN6DM613Qs936bnv9zFSXIf9vYc3np1jmmH61KU9qBAPIoZo0/KisO5JoQ==
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=S6bmuWQB;
	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 DC054606A680;
	Sat,  5 Dec 2026 09:35:29 +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=S6bmuWQB
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=1796463323;
	b=wMN9T70JDLRACJ5a8byRu37lYvO95G8M0CyyyOtJJvWmMqKt2rFaxvWWcITpczhweiu9
	 Uk4uUj+RvvzNVoVgRFmnbR34UF81nRdQ3hRqyLUQ4m85HrtIO06jDKqrWZtwgaWrA3x8B
	 qRZuGWtN0xm+vprot4kDzpRrrE34dc70YeDqTOySBm7uiwKWR9eyPMvReWydQehs+/ijZ
	 EhAhrADS7uUmjetsEPYxc9zW6pELeEJTrcYa8wcKrWdPVprMDL6YQw2DRWqaobeYNAhEv
	 2dk3GSOJWyd4MhT84eOJq4ciPR6AKGLfX6j3HTytZGl2TlE+SJcZwJxhf0aZeHVKDhw==
X-ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015;
	a=rsa-sha256; c=relaxed/relaxed; t=1796463323;
	h=Received:DKIM-Signature:Content-Type:MIME-Version:Message-Id:
	 In-Reply-To:References:Subject:From:To:Cc:Date;
	bh=wyFp9xkJLbBaisQNimxOba15tTTs2Q5UmIDKQF1xduU=;
	b=jDno2+JM5v5oT1Igq5NK/izbNF3SmhJHI39NKaX2dkiMHYNiorcUdrkha+5XqbfXG/VE
	 L/SboJg/iGYfV9EfWp++OVQdIVs1PVd6VjGnanP4wGu1nfy5qdQXgo4TLjc/DRCMniupq
	 cRPyWeO+h8oRi8I/U86ahEWhxxlmyBR6fE8VwKZX0o4zfO7tcCazWJO+SBDBlcfc6Dgtj
	 JZn5ttVhFDPxoMoxdlBs/g84X7K6erw8mW+tJmFtQo+wqErN2kDSJ/oaZiyDMsf+6I6Vl
	 KqT6Byi7UnFW/EKBU/cbMFPHufKZTIoZURWPdTQRkQJO7KwZejycyoaCy3ChwRCg8pg==
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=S6bmuWQB;
	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 D5706606A322;
	Sat,  5 Dec 2026 09:35:23 +0000 (UTC)
X-Received: by smtp.kernel.org (Postfix) with ESMTPSA id BF5BD1F0092B;
	Sat,  5 Dec 2026 09:35:21 +0000 (UTC)
X-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=kernel.org;
	s=k20260515; t=1796463323;
	bh=wyFp9xkJLbBaisQNimxOba15tTTs2Q5UmIDKQF1xduU=;
	h=In-Reply-To:References:Subject:From:To:Cc:Date;
	b=S6bmuWQBNv/qAT3KSOXsOt8Vc1hco1FtjLmnWWmx8mznLIDra/1c8QWGKYpnGTUE9
	 Hay51IWyBNFngAjL3BNqebelbAuIYJHi/6diOEhuir7r5yk/4s3VTu9hRcNTxZIlcn
	 PRzE7udJHDP3F1/f3pjP7FPEDE8u5bw6M4On7vG8xe1K1bm8ld0gXZ7LtlkcFRPXUz
	 awdaA7RZUA9WzU6UiIaUZk0fACJdPmwBzbrhh52Nm5GYvDeEF5pHDUC31UG8rgqp7v
	 mmchEqOguyEXQjHeymk/bPwdF3nbx+dph5ywpBMeJR3mUOzsJAnLzCwjff2rBpvIzc
	 g864vr50NY7ZQ==
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-In-Reply-To: <20261205090934.3813585-1-r41k0u@ubuntu.com>
X-Cc: ast@kernel.org,daniel@iogearbox.net,andrii@kernel.org,eddyz87@gmail.com,varunrmallya@gmail.com,kc@kcsrk.info,yonghong.song@linux.dev,memxor@gmail.com,ngs@cse.iitm.ac.in,netdev@vger.kernel.org,linux-kselftest@vger.kernel.org,r41k0u@ubuntu.com,ast@kernel.org,andrii@kernel.org,daniel@iogearbox.net,martin.lau@kernel.org,eddyz87@gmail.com,yonghong.song@linux.dev,mason@kernel.org,ihor.solodrai@linux.dev