[PATCH bpf-next v1 0/6] bpf: Scope callback arguments to their frame
From: Ihor Solodrai
Date: Mon Sep 21 2026 - 21:06:25 EST
A callback-calling helper or kfunc can pass its callback a pointer
that is only valid for the duration of the call, and the verifier has
no way to express that.
Two helpers need fixing:
* bpf_user_ringbuf_drain() passes a CONST_PTR_TO_DYNPTR over a
sample it releases as soon as the callback returns. When parked in
callback_ctx, that register describes reused kernel stack and
gives the program an arbitrary kernel read and write (bpf-next
only, see patch #2).
* bpf_for_each_map_elem() over an (percpu-) array map passes the
address of a u32 held in bpf_for_each_array_elem()'s own frame,
leaking four bytes of kernel stack.
Neither has a local fix: both arguments point into a helper's own
frame, with nothing longer-lived to anchor them to.
Introduce REF_TYPE_FRAME for this use case: a reference owned by a
callee frame, dropped when the frame is popped. Then use this
mechanism in both bpf_user_ringbuf_drain() and bpf_for_each_map_elem()
callee state setup.
---
The series is composed as follows:
* patch #1 implements REF_TYPE_FRAME and relevant infra code, but
it's not used yet
* patches #2 and #5 use the new mark_frame_scoped_arg() helper
* patch #3 adds relevant diagnostics
* patches #4 and #6 add selftests to cover the changes
---
Ihor Solodrai (6):
bpf: Introduce REF_TYPE_FRAME in the verifier
bpf: Scope the bpf_user_ringbuf_drain() dynptr to its callback frame
bpf: Name the callback in frame-release diagnostics
selftests/bpf: Cover the user ringbuf callback dynptr lifetime
bpf: Scope the bpf_for_each_map_elem() array key to the callback frame
selftests/bpf: Cover callback-frame map key lifetime
include/linux/bpf.h | 1 +
include/linux/bpf_verifier.h | 11 +-
kernel/bpf/arraymap.c | 18 ++-
kernel/bpf/diagnostics.c | 3 +
kernel/bpf/diagnostics.h | 1 +
kernel/bpf/states.c | 4 +
kernel/bpf/verifier.c | 149 +++++++++++++++---
.../selftests/bpf/prog_tests/cb_refs.c | 4 +-
.../selftests/bpf/progs/exceptions_fail.c | 20 +++
.../selftests/bpf/progs/user_ringbuf_fail.c | 143 +++++++++++++++++
.../bpf/progs/verifier_iterating_callbacks.c | 91 +++++++++++
11 files changed, 419 insertions(+), 26 deletions(-)
base-commit: 79dc258c9392051420a26f1504c647bd3d27c66a
--
2.55.0