Skip to content

Commit 918ed6a

Browse files
lvtao-secSasha Levin
authored andcommitted
bpf: Ensure reg is PTR_TO_STACK in process_iter_arg
[ Upstream commit 12659d2 ] 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]> Signed-off-by: Sasha Levin <[email protected]>
1 parent 75fae1a commit 918ed6a

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
@@ -8021,6 +8021,11 @@ static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_id
80218021
const struct btf_type *t;
80228022
int spi, err, i, nr_slots, btf_id;
80238023

8024+
if (reg->type != PTR_TO_STACK) {
8025+
verbose(env, "arg#%d expected pointer to an iterator on stack\n", regno - 1);
8026+
return -EINVAL;
8027+
}
8028+
80248029
/* For iter_{new,next,destroy} functions, btf_check_iter_kfuncs()
80258030
* ensures struct convention, so we wouldn't need to do any BTF
80268031
* 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)