Commit 3917b10
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/20260120222638.3976562-1-ihor.solodrai@linux.dev/
Fixes: 64e1360 ("bpf: Verifier support for KF_IMPLICIT_ARGS")
Reported-by: Tejun Heo <tj@kernel.org>
Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Link: sched-ext/scx#3687 (comment)
Link: https://patch.msgid.link/20260713235223.1639022-2-ihor.solodrai@linux.dev
Signed-off-by: Eduard Zingerman <eddyz87@gmail.com>1 parent 4967bd5 commit 3917b10
3 files changed
Lines changed: 83 additions & 13 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
578 | 578 | | |
579 | 579 | | |
580 | 580 | | |
| 581 | + | |
581 | 582 | | |
582 | 583 | | |
583 | 584 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9114 | 9114 | | |
9115 | 9115 | | |
9116 | 9116 | | |
| 9117 | + | |
| 9118 | + | |
| 9119 | + | |
| 9120 | + | |
| 9121 | + | |
| 9122 | + | |
| 9123 | + | |
| 9124 | + | |
| 9125 | + | |
| 9126 | + | |
| 9127 | + | |
| 9128 | + | |
| 9129 | + | |
| 9130 | + | |
| 9131 | + | |
| 9132 | + | |
| 9133 | + | |
| 9134 | + | |
| 9135 | + | |
| 9136 | + | |
| 9137 | + | |
| 9138 | + | |
| 9139 | + | |
| 9140 | + | |
| 9141 | + | |
| 9142 | + | |
| 9143 | + | |
| 9144 | + | |
| 9145 | + | |
9117 | 9146 | | |
9118 | 9147 | | |
9119 | 9148 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2584 | 2584 | | |
2585 | 2585 | | |
2586 | 2586 | | |
2587 | | - | |
| 2587 | + | |
2588 | 2588 | | |
2589 | 2589 | | |
2590 | 2590 | | |
2591 | | - | |
2592 | 2591 | | |
| 2592 | + | |
2593 | 2593 | | |
2594 | 2594 | | |
2595 | 2595 | | |
2596 | | - | |
2597 | | - | |
2598 | | - | |
| 2596 | + | |
| 2597 | + | |
| 2598 | + | |
| 2599 | + | |
2599 | 2600 | | |
2600 | 2601 | | |
2601 | 2602 | | |
2602 | 2603 | | |
2603 | 2604 | | |
2604 | | - | |
| 2605 | + | |
2605 | 2606 | | |
2606 | 2607 | | |
2607 | 2608 | | |
| |||
2653 | 2654 | | |
2654 | 2655 | | |
2655 | 2656 | | |
2656 | | - | |
| 2657 | + | |
2657 | 2658 | | |
2658 | 2659 | | |
2659 | 2660 | | |
| |||
18880 | 18881 | | |
18881 | 18882 | | |
18882 | 18883 | | |
| 18884 | + | |
| 18885 | + | |
| 18886 | + | |
| 18887 | + | |
| 18888 | + | |
| 18889 | + | |
| 18890 | + | |
| 18891 | + | |
| 18892 | + | |
| 18893 | + | |
| 18894 | + | |
| 18895 | + | |
| 18896 | + | |
| 18897 | + | |
| 18898 | + | |
| 18899 | + | |
| 18900 | + | |
| 18901 | + | |
| 18902 | + | |
| 18903 | + | |
| 18904 | + | |
| 18905 | + | |
| 18906 | + | |
| 18907 | + | |
| 18908 | + | |
| 18909 | + | |
| 18910 | + | |
| 18911 | + | |
| 18912 | + | |
| 18913 | + | |
| 18914 | + | |
| 18915 | + | |
| 18916 | + | |
| 18917 | + | |
| 18918 | + | |
| 18919 | + | |
| 18920 | + | |
| 18921 | + | |
| 18922 | + | |
| 18923 | + | |
| 18924 | + | |
18883 | 18925 | | |
18884 | 18926 | | |
18885 | 18927 | | |
| |||
19128 | 19170 | | |
19129 | 19171 | | |
19130 | 19172 | | |
19131 | | - | |
19132 | | - | |
| 19173 | + | |
| 19174 | + | |
19133 | 19175 | | |
19134 | 19176 | | |
19135 | 19177 | | |
| |||
19412 | 19454 | | |
19413 | 19455 | | |
19414 | 19456 | | |
19415 | | - | |
19416 | | - | |
19417 | | - | |
19418 | | - | |
| 19457 | + | |
| 19458 | + | |
19419 | 19459 | | |
19420 | 19460 | | |
19421 | 19461 | | |
| |||
0 commit comments