Skip to content

Commit 793a81a

Browse files
kkdwivediNobody
authored andcommitted
Fix crash due to OOB access when reg->type > __BPF_REG_TYPE_MAX
When commit e6ac245 ("bpf: Support bpf program calling kernel function") added kfunc support, it defined reg2btf_ids as a cheap way to translate the verifier reg type to the appropriate btf_vmlinux BTF ID, however commit c25b2ae ("bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL") moved the __BPF_REG_TYPE_MAX from the last member of bpf_reg_type enum to after the base register types, and defined other variants using type flag composition. However, now, the direct usage of reg->type to index into reg2btf_ids may no longer fall into __BPF_REG_TYPE_MAX range, and hence lead to out of bounds access and kernel crash on dereference of bad pointer. Cc: Martin KaFai Lau <[email protected]> Cc: Hao Luo <[email protected]> Fixes: c25b2ae ("bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL") Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
1 parent 5e5db69 commit 793a81a

File tree

1 file changed

+3
-2
lines changed

1 file changed

+3
-2
lines changed

kernel/bpf/btf.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5688,7 +5688,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
56885688
}
56895689
if (check_ptr_off_reg(env, reg, regno))
56905690
return -EINVAL;
5691-
} else if (is_kfunc && (reg->type == PTR_TO_BTF_ID || reg2btf_ids[reg->type])) {
5691+
} else if (is_kfunc && (reg->type == PTR_TO_BTF_ID ||
5692+
(reg2btf_ids[base_type(reg->type)] && !type_flag(reg->type)))) {
56925693
const struct btf_type *reg_ref_t;
56935694
const struct btf *reg_btf;
56945695
const char *reg_ref_tname;
@@ -5706,7 +5707,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
57065707
reg_ref_id = reg->btf_id;
57075708
} else {
57085709
reg_btf = btf_vmlinux;
5709-
reg_ref_id = *reg2btf_ids[reg->type];
5710+
reg_ref_id = *reg2btf_ids[base_type(reg->type)];
57105711
}
57115712

57125713
reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id,

0 commit comments

Comments
 (0)