Skip to content

Commit 12659d2

Browse files
lvtao-secAlexei Starovoitov
authored andcommitted
bpf: Ensure reg is PTR_TO_STACK in process_iter_arg
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. Fixes: 06accc8 ("bpf: add support for open-coded iterator loops") Suggested-by: Andrii Nakryiko <[email protected]> Signed-off-by: Tao Lyu <[email protected]> [ Kartikeya: move check into process_iter_arg, rewrite commit log ] Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
1 parent 537a252 commit 12659d2

File tree

2 files changed

+7
-2
lines changed

2 files changed

+7
-2
lines changed

kernel/bpf/verifier.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8189,6 +8189,11 @@ static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_id
81898189
const struct btf_type *t;
81908190
int spi, err, i, nr_slots, btf_id;
81918191

8192+
if (reg->type != PTR_TO_STACK) {
8193+
verbose(env, "arg#%d expected pointer to an iterator on stack\n", regno - 1);
8194+
return -EINVAL;
8195+
}
8196+
81928197
/* For iter_{new,next,destroy} functions, btf_check_iter_kfuncs()
81938198
* ensures struct convention, so we wouldn't need to do any BTF
81948199
* validation here. But given iter state can be passed as a parameter

tools/testing/selftests/bpf/progs/verifier_bits_iter.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,9 @@ __description("uninitialized iter in ->next()")
3535
__failure __msg("expected an initialized iter_bits as arg #1")
3636
int BPF_PROG(next_uninit, struct bpf_iter_meta *meta, struct cgroup *cgrp)
3737
{
38-
struct bpf_iter_bits *it = NULL;
38+
struct bpf_iter_bits it = {};
3939

40-
bpf_iter_bits_next(it);
40+
bpf_iter_bits_next(&it);
4141
return 0;
4242
}
4343

0 commit comments

Comments
 (0)