Skip to content

Commit d4c4435

Browse files
author
Alexei Starovoitov
committed
Merge branch 'fix-missing-process_iter_arg-type-check'
Kumar Kartikeya Dwivedi says: ==================== 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] ==================== Link: https://patch.msgid.link/[email protected] Signed-off-by: Alexei Starovoitov <[email protected]>
2 parents 537a252 + 7f71197 commit d4c4435

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-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/iters.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,4 +1486,30 @@ int iter_subprog_check_stacksafe(const void *ctx)
14861486
return 0;
14871487
}
14881488

1489+
struct bpf_iter_num global_it;
1490+
1491+
SEC("raw_tp")
1492+
__failure __msg("arg#0 expected pointer to an iterator on stack")
1493+
int iter_new_bad_arg(const void *ctx)
1494+
{
1495+
bpf_iter_num_new(&global_it, 0, 1);
1496+
return 0;
1497+
}
1498+
1499+
SEC("raw_tp")
1500+
__failure __msg("arg#0 expected pointer to an iterator on stack")
1501+
int iter_next_bad_arg(const void *ctx)
1502+
{
1503+
bpf_iter_num_next(&global_it);
1504+
return 0;
1505+
}
1506+
1507+
SEC("raw_tp")
1508+
__failure __msg("arg#0 expected pointer to an iterator on stack")
1509+
int iter_destroy_bad_arg(const void *ctx)
1510+
{
1511+
bpf_iter_num_destroy(&global_it);
1512+
return 0;
1513+
}
1514+
14891515
char _license[] SEC("license") = "GPL";

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)