BPF List
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Pragyansh Chaturvedi" <r41k0u@ubuntu.com>, bpf@vger.kernel.org
Subject: Re: [PATCH bpf 1/2] bpf: Give a raw_tp test_run program the largest context when it does tail calls
Date: Sat, 05 Dec 2026 09:26:53 +0000	[thread overview]
Message-ID: <sashiko-outbox-251344@kernel.org> (raw)
In-Reply-To: <20261205090934.3813585-1-r41k0u@ubuntu.com>

Thank you for your contribution! Sashiko AI review found 3 potential
issues to consider:
- [High] bpf: Missing freplace tail_call_reachable check for raw_tp
  targets
- [Medium] bpf: Over-allocating U16_MAX via kvzalloc() in
  bpf_prog_test_run_raw_tp()
- [Low] bpf: Incorrect Fixes trailer citing 04fd61ab36ec instead of
  1b4d60ec162f
--

commit c2e6212f3159dac850a188417860abf3db30dfb8
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>

[Severity: Low]
Commit 04fd61ab36ec ("bpf: allow bpf programs to tail-call other bpf
programs") predates raw_tp test_run support.  Should the Fixes tag cite
the commit that introduced bpf_prog_test_run_raw_tp() instead?

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

> diff --git a/net/bpf/test_run.c b/net/bpf/test_run.c
> index d6c9dc62182ee..f951a41082cd9 100644
> --- 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);

[Severity: High]
Can a BPF_PROG_TYPE_EXT extension attached to a non-tail-call raw_tp
program bypass this check and trigger a NULL pointer dereference or slab
out-of-bounds read?

prog->aux->tail_call_reachable is never updated when an freplace extension
attaches, and bpf_tracing_prog_attach() does not reject tail-call-reachable
extensions on non-tail-call BPF_PROG_TYPE_RAW_TRACEPOINT targets.

Running BPF_PROG_TEST_RUN on such a raw_tp target with ctx_size_in 0 leaves
info.ctx NULL, so a tail-called raw_tp callee reading *(u64 *)(ctx + 88)
dereferences NULL (or reads past an 8-byte buffer when ctx_size_in is 8).

Either bpf_tracing_prog_attach() should reject tail-call-reachable
extensions on non-tail-call raw_tp targets, or bpf_prog_test_run_raw_tp()
should unconditionally allocate the full 96-byte raw_tp context.

> +
> +	if (size) {
> +		info.ctx = kvzalloc(size, GFP_KERNEL_ACCOUNT | __GFP_NOWARN);
> +		if (!info.ctx)
> +			return -ENOMEM;

[Severity: Medium]
Why pad info.ctx to U16_MAX with kvzalloc() instead of
MAX_BPF_FUNC_ARGS * sizeof(u64) (96 bytes)?

bpf_tracing_ctx_access() rejects any BPF_PROG_TYPE_RAW_TRACEPOINT context
access at or above 96 bytes, and raw_tp programs can only tail-call other
raw_tp programs, so no reachable target can ever access past 96 bytes.

Allocating 65,535 bytes on every test_run (which does not support repeat)
bypasses SLUB caches, hitting order-4 buddy allocations or vmalloc/vfree
while zeroing 64 KiB instead of 96 bytes.  Padding to
MAX_BPF_FUNC_ARGS * sizeof(u64) via kzalloc() avoids this.

The commit message example of *(u64 *)(ctx + 96) is also inaccurate, since
bpf_tracing_ctx_access() rejects offsets >= 96.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261205090934.3813585-1-r41k0u@ubuntu.com?part=1

  parent reply	other threads:[~2026-12-05  9:26 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-05  9:06 bpf: tail-call targets bypass max_ctx_offset checks (OOB read, KASAN splats) Pragyansh Chaturvedi
2026-12-05  9:09 ` [PATCH bpf 1/2] bpf: Give a raw_tp test_run program the largest context when it does tail calls Pragyansh Chaturvedi
2026-12-05  9:09   ` [PATCH bpf 2/2] selftests/bpf: Test raw_tp test_run ctx padding for " Pragyansh Chaturvedi
2026-12-05  9:25     ` sashiko-bot
2026-12-05  9:35     ` bot+bpf-ci
2026-12-05  9:26   ` sashiko-bot [this message]
2026-12-05  9:35   ` [PATCH bpf 1/2] bpf: Give a raw_tp test_run program the largest context when it does " bot+bpf-ci

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=sashiko-outbox-251344@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=r41k0u@ubuntu.com \
    --cc=sashiko-reviews@lists.linux.dev \
    /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