bpf: Fix tracing of kfuncs with implicit args
Subsystems (4)
Ihor Solodrai
reviewerv3 cover bpf: Fix tracing of kfuncs with implicit args
Tejun reported an issue where a BPF program tracing a kfunc with KF_IMPLICIT_ARGS can crash the kernel [1]. This is caused by a bug in bpf_check_attach_target(): the btf_func_model for such a kfunc is computed from a wrong BTF prototype. For more details see the commit message of patch #1.
The second patch adds a selftest that can catch this situation.
The fix is a candidate for 7.1 backport.
[1] https://github.com/sched-ext/scx/issues/3687#issuecomment-4906694106
---
v2->v3:
* Replace btf_kfunc_accumulated_flags() with btf_kfunc_check_flag() following a discussion with Eduard. Inlining the hook walk is a worse option than a helper, because BTF_KFUNC_HOOK_MAX and co are internal to btf.c and exposing them is uglier.
* remove reduntant btf_is_func check (Jiri)
* formatting nit (Eduard)
v2: https://lore.kernel.org/bpf/[email protected]/
v1->v2:
* Take a module reference in btf_attach_func_proto() around the btf_kfunc_accumulated_flags() call (sashiko)
v1: https://lore.kernel.org/bpf/[email protected]/
---
Ihor Solodrai (1):
bpf: Fix tracing of kfuncs with implicit args
Kumar Kartikeya Dwivedi (1):
selftests/bpf: Cover tracing implicit kfunc args
include/linux/btf.h | 1 + kernel/bpf/btf.c | 29 +++++++ kernel/bpf/verifier.c | 66 ++++++++++++---- .../prog_tests/kfunc_implicit_args_tracing.c | 36 +++++++++ .../bpf/progs/kfunc_implicit_args_tracing.c | 77 +++++++++++++++++++ 5 files changed, 196 insertions(+), 13 deletions(-) create mode 100644 tools/testing/selftests/bpf/prog_tests/kfunc_implicit_args_tracing.c create mode 100644 tools/testing/selftests/bpf/progs/kfunc_implicit_args_tracing.c
Signature · 1 line
2.55.0
Ihor Solodrai
reviewerPATCH v3 1/2 bpf: Fix tracing of kfuncs with implicit args
A kfunc marked with KF_IMPLICIT_ARGS flag takes implicit arguments (such as bpf_prog_aux) that the verifier injects at load time. resolve_btfids strips those from the kfunc's BTF-visible prototype and keeps the real kernel ABI in a counterpart _impl prototype [1].
fentry/fexit/fmod_ret/fsession programs may attach to the BPF kernel functions, including those with implicit args. However bpf_check_attach_target() and bpf_check_attach_btf_id_multi() extract the struct btf_func_model from the wrong BTF prototype of the kfunc. The btf_func_model is later read to construct the trampoline, which then causes the injected implicit argument to be clobbered and the kfunc dereferencing garbage.
Add btf_attach_func_proto() to resolve the real ABI prototype of the kfunc the way the call site does: by looking up the _impl prototype for a KF_IMPLICIT_ARGS kfunc. Use it at both attach-target model construction sites.
To enable this, make two supporting changes:
* pass bpf_verifier_log instead of bpf_verifier_env to find_kfunc_impl_proto(), so it can be reused from the attach path
* add btf_kfunc_check_flag() to test a flag across all of a kfunc's hook sets, because a program attaching to a kfunc is not in the kfunc's call-set
KF_IMPLICIT_ARGS must be consistent across the sets, so btf_kfunc_check_flag() returns -EINVAL on inconsistency.
btf_kfunc_check_flag() reads the kfunc's flags from the target's kfunc_set_tab. For a module BTF that table is stable only after the module is live, so take a module reference around the read, mirroring how the kfunc call path gates the same lookup with btf_try_get_module().
The remaining call sites of btf_distill_func_proto() are safe as is. The BPF_TRACE_ITER case distills a registered iterator's prototype, and bpf_struct_ops_desc_init() distills the function-pointer members of a struct_ops type. Neither is a kfunc, and so can't have implicit arguments.
[1] https://lore.kernel.org/all/[email protected]/
--- include/linux/btf.h | 1 + kernel/bpf/btf.c | 29 +++++++++++++++++++ kernel/bpf/verifier.c | 66 ++++++++++++++++++++++++++++++++++--------- 3 files changed, 83 insertions(+), 13 deletions(-)
include/linux/btf.h +1 -0
@@ -578,6 +578,7 @@ const char *btf_str_by_offset(const struct btf *btf, u32 offset);struct btf *btf_parse_vmlinux (void );struct btf *bpf_prog_get_target_btf (const struct bpf_prog *prog);u32 *btf_kfunc_flags (const struct btf *btf,u32 kfunc_btf_id,const struct bpf_prog *prog); +int btf_kfunc_check_flag (const struct btf *btf,u32 kfunc_btf_id,u32 flag);bool btf_kfunc_is_allowed (const struct btf *btf,u32 kfunc_btf_id,const struct bpf_prog *prog);u32 *btf_kfunc_is_modify_return (const struct btf *btf,u32 kfunc_btf_id,const struct bpf_prog *prog);
kernel/bpf/btf.c +29 -0
@@ -9114,6 +9114,35 @@ u32 *btf_kfunc_flags(const struct btf *btf, u32 kfunc_btf_id, const struct bpf_preturn btf_kfunc_id_set_contains (btf, hook, kfunc_btf_id); } +/* +* Check a single KF_* @flag on a kfunc across all of its hook sets. +* Returns: +* * 1 if @flag is set +* * 0 if @flag is not set +* * -EINVAL if @flag is set inconsistently across the sets +* * -ENOENT if kfunc_btf_id is not a registered kfunc +*/ +int btf_kfunc_check_flag (const struct btf *btf,u32 kfunc_btf_id,u32 flag) +{ +enum btf_kfunc_hook hook; +int res = -ENOENT ; +bool is_set; +u32 *flags; + +for (hook =0 ; hook <BTF_KFUNC_HOOK_MAX ; hook++) { + flags =btf_kfunc_id_set_contains (btf, hook, kfunc_btf_id); +if (!flags) +continue ; + is_set = *flags & flag; +if (res <0 ) + res = is_set; +else if (res != is_set) +return -EINVAL ; + } + +return res; +} +u32 *btf_kfunc_is_modify_return (const struct btf *btf,u32 kfunc_btf_id,const struct bpf_prog *prog) {
kernel/bpf/verifier.c +53 -13
108 lines not on this page · raw message
Signature · 1 line
2.55.0
Ihor Solodrai
reviewerPATCH v3 2/2 selftests/bpf: Cover tracing implicit kfunc args
In reply to Ihor SolodraiFrom: Kumar Kartikeya Dwivedi <[email protected]>
KF_IMPLICIT_ARGS kfuncs have a BPF-call prototype and a real kernel target prototype. Add a tracing selftest that attaches fentry and fexit programs to bpf_kfunc_implicit_arg(), runs a syscall BPF program that calls it, and checks that the tracing context exposes both the explicit argument and the implicit prog aux pointer.
--- .../prog_tests/kfunc_implicit_args_tracing.c | 36 +++++++++ .../bpf/progs/kfunc_implicit_args_tracing.c | 77 +++++++++++++++++++ 2 files changed, 113 insertions(+) create mode 100644 tools/testing/selftests/bpf/prog_tests/kfunc_implicit_args_tracing.c create mode 100644 tools/testing/selftests/bpf/progs/kfunc_implicit_args_tracing.c
tools/testing/selftests/bpf/prog_tests/kfunc_implicit_args_tracing.c new file +36 -0
@@ -0,0 +1,36 @@ +// SPDX-License-Identifier: GPL-2.0 +/* Copyright (c) 2026 Meta Platforms, Inc. and affiliates. */ + +#include <test_progs.h> +#include "kfunc_implicit_args_tracing.skel.h" + +void test_kfunc_implicit_args_tracing (void ) +{ +struct kfunc_implicit_args_tracing *skel; +LIBBPF_OPTS (bpf_test_run_opts, topts); +int err, fd; + + skel =kfunc_implicit_args_tracing__open_and_load (); +if (!ASSERT_OK_PTR (skel,"open_and_load" )) +return ; + + err =kfunc_implicit_args_tracing__attach (skel); +if (!ASSERT_OK (err,"attach" )) + goto cleanup; + + fd =bpf_program__fd (skel->progs.trigger_implicit_arg); + err =bpf_prog_test_run_opts (fd, &topts); +if (!ASSERT_OK (err,"test_run" )) + goto cleanup; + +ASSERT_EQ (topts.retval,5 ,"kfunc_retval" ); +ASSERT_EQ (skel->bss->fentry_arg_cnt,2 ,"fentry_arg_cnt" ); +ASSERT_NEQ (skel->bss->fentry_aux_arg,0 ,"fentry_aux_arg" ); +ASSERT_EQ (skel->bss->fentry_result,1 ,"fentry_result" ); +ASSERT_EQ (skel->bss->fexit_arg_cnt,2 ,"fexit_arg_cnt" ); +ASSERT_NEQ (skel->bss->fexit_aux_arg,0 ,"fexit_aux_arg" ); +ASSERT_EQ (skel->bss->fexit_result,1 ,"fexit_result" ); + +cleanup: +kfunc_implicit_args_tracing__destroy (skel); +}
tools/testing/selftests/bpf/progs/kfunc_implicit_args_tracing.c new file +77 -0
77 lines not on this page · raw message
Signature · 1 line
2.55.0
This series was applied to bpf/bpf.git (master) by Eduard Zingerman <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Eduard Zingerman <[email protected]>:
Quoted 9 lines · On Mon, 13 Jul 2026 16:52:21 -0700 you wrote:
Tejun reported an issue where a BPF program tracing a kfunc with
KF_IMPLICIT_ARGScan crash the kernel [1]. This is caused by a bug inbpf_check_attach_target(): thebtf_func_modelfor such a kfunc is computed from a wrong BTF prototype. For more details see the commit message of patch #1.The second patch adds a selftest that can catch this situation.
[...]
Here is the summary with links:
- [bpf,v3,1/2] bpf: Fix tracing of kfuncs with implicit args
https://git.kernel.org/bpf/bpf/c/3917b1012ee2 - [bpf,v3,2/2] selftests/bpf: Cover tracing implicit kfunc args https://git.kernel.org/bpf/bpf/c/0af15f3057a6
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html