> 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 <r41k…@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 <r41k…@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