Fix missing process_iter_arg type check
Subsystems (4)
Kumar Kartikeya Dwivedi
maintainerv2 cover Fix missing process_iter_arg type check
I am taking over Tao's earlier patch set that can be found at [0], after an offline discussion. The bug reported in that thread is that process_iter_arg missed a reg->type == PTR_TO_STACK check. Fix this by adding it in, and also address comments from Andrii on the earlier attempt. Include more selftests to ensure the error is caught.
[0]: https://lore.kernel.org/bpf/[email protected]
Changelog:
----------
v1 -> v2:
v1: https://lore.kernel.org/bpf/[email protected]
* Rebase on bpf/master (Andrii)
Kumar Kartikeya Dwivedi (1):
selftests/bpf: Add tests for iter arg check
Tao Lyu (1):
bpf: Ensure reg is PTR_TO_STACK in process_iter_arg
kernel/bpf/verifier.c | 5 ++++ tools/testing/selftests/bpf/progs/iters.c | 26 +++++++++++++++++++ .../selftests/bpf/progs/verifier_bits_iter.c | 4 +-- 3 files changed, 33 insertions(+), 2 deletions(-)
base-commit: 537a2525eaf76ea9b0dca62b994500d8670b39d5 --
2.43.5
Kumar Kartikeya Dwivedi
maintainerPATCH v2 1/2 bpf: Ensure reg is PTR_TO_STACK in process_iter_arg
From: Tao Lyu <[email protected]>
Currently, KF_ARG_PTR_TO_ITER handling missed checking the reg->type and ensuring it is PTR_TO_STACK. Instead of enforcing this in the caller of process_iter_arg, move the check into it instead so that all callers will gain the check by default. This is similar to process_dynptr_func.
An existing selftest in verifier_bits_iter.c fails due to this change, but it's because it was passing a NULL pointer into iter_next helper and getting an error further down the checks, but probably meant to pass an uninitialized iterator on the stack (as is done in the subsequent test below it). We will gain coverage for non-PTR_TO_STACK arguments in later patches hence just change the declaration to zero-ed stack object.
--- kernel/bpf/verifier.c | 5 +++++ tools/testing/selftests/bpf/progs/verifier_bits_iter.c | 4 ++-- 2 files changed, 7 insertions(+), 2 deletions(-)
kernel/bpf/verifier.c +5 -0
@@ -8189,6 +8189,11 @@ static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_idconst struct btf_type *t;int spi, err, i, nr_slots, btf_id; +if (reg->type !=PTR_TO_STACK ) { +verbose (env,"arg#%d expected pointer to an iterator on stack\n" , regno -1 ); +return -EINVAL ; + } +/* For iter_{new,next,destroy} functions, btf_check_iter_kfuncs() * ensures struct convention, so we wouldn't need to do any BTF * validation here. But given iter state can be passed as a parameter
tools/testing/selftests/bpf/progs/verifier_bits_iter.c +2 -2
@@ -35,9 +35,9 @@ __description("uninitialized iter in ->next()") __failure __msg("expected an initialized iter_bits as arg #1")int BPF_PROG (next_uninit ,struct bpf_iter_meta *meta,struct cgroup *cgrp) { -struct bpf_iter_bits *it =NULL ; +struct bpf_iter_bits it = {}; -bpf_iter_bits_next (it); +bpf_iter_bits_next (&it);return 0 ; }
Signature · 1 line
2.43.5
Kumar Kartikeya Dwivedi
maintainerPATCH v2 2/2 selftests/bpf: Add tests for iter arg check
In reply to Kumar Kartikeya DwivediAdd selftests to cover argument type check for iterator kfuncs, and cover all three kinds (new, next, destroy). Without the fix in the previous patch, the selftest would not cause a verifier error.
--- tools/testing/selftests/bpf/progs/iters.c | 26 +++++++++++++++++++++++ 1 file changed, 26 insertions(+)
tools/testing/selftests/bpf/progs/iters.c +26 -0
@@ -1486,4 +1486,30 @@ int iter_subprog_check_stacksafe(const void *ctx)return 0 ; } +struct bpf_iter_num global_it; + +SEC("raw_tp") +__failure __msg("arg#0 expected pointer to an iterator on stack") +int iter_new_bad_arg(const void *ctx) +{ +bpf_iter_num_new (&global_it,0 ,1 ); +return 0 ; +} + +SEC("raw_tp") +__failure __msg("arg#0 expected pointer to an iterator on stack") +int iter_next_bad_arg(const void *ctx) +{ +bpf_iter_num_next (&global_it); +return 0 ; +} + +SEC("raw_tp") +__failure __msg("arg#0 expected pointer to an iterator on stack") +int iter_destroy_bad_arg(const void *ctx) +{ +bpf_iter_num_destroy (&global_it); +return 0 ; +} + char _license[] SEC("license") = "GPL";
Signature · 1 line
2.43.5
This series was applied to bpf/bpf.git (master) by Alexei Starovoitov <[email protected]>:
Hello:
This series was applied to bpf/bpf.git (master)
by Alexei Starovoitov <[email protected]>:
Quoted 9 lines · On Mon, 2 Dec 2024 16:02:36 -0800 you wrote:
I am taking over Tao's earlier patch set that can be found at [0], after an offline discussion. The bug reported in that thread is that
process_iter_argmissed areg->type==PTR_TO_STACKcheck. Fix this by adding it in, and also address comments from Andrii on the earlier attempt. Include more selftests to ensure the error is caught.[0]: https://lore.kernel.org/bpf/[email protected]
[...]
Here is the summary with links:
- [bpf,v2,1/2] bpf: Ensure reg is PTR_TO_STACK in process_iter_arg
https://git.kernel.org/bpf/bpf/c/12659d28615d - [bpf,v2,2/2] selftests/bpf: Add tests for iter arg check https://git.kernel.org/bpf/bpf/c/7f71197001e3
You are awesome, thank you!
Signature · 2 lines
Deet-doot-dot, I am a bot. https://korg.docs.kernel.org/patchwork/pwbot.html