Skip to content

Commit 8c815e4

Browse files
lvtao-secmehmetb0
authored andcommitted
bpf: Ensure reg is PTR_TO_STACK in process_iter_arg
BugLink: https://bugs.launchpad.net/bugs/2096827 [ 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]> [koichiroden: adjusted context due to missing commit: baebe9a ("bpf: allow passing struct bpf_iter_<type> as kfunc arguments")] Signed-off-by: Koichiro Den <[email protected]>
1 parent 8ae44c1 commit 8c815e4

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
@@ -7928,6 +7928,11 @@ static int process_iter_arg(struct bpf_verifier_env *env, int regno, int insn_id
79287928
int spi, err, i, nr_slots;
79297929
u32 btf_id;
79307930

7931+
if (reg->type != PTR_TO_STACK) {
7932+
verbose(env, "arg#%d expected pointer to an iterator on stack\n", regno - 1);
7933+
return -EINVAL;
7934+
}
7935+
79317936
/* btf_check_iter_kfuncs() ensures we don't need to validate anything here */
79327937
arg = &btf_params(meta->func_proto)[0];
79337938
t = btf_type_skip_modifiers(meta->btf, arg->type, NULL); /* PTR */

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

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

38-
bpf_iter_bits_next(it);
38+
bpf_iter_bits_next(&it);
3939
return 0;
4040
}
4141

0 commit comments

Comments
 (0)