Skip to content

Commit 35efc69

Browse files
committed
bpf: Fix crash due to out of bounds access into reg2btf_ids.
jira VULN-136 cve CVE-2022-0500 commit-author Kumar Kartikeya Dwivedi <[email protected]> commit 45ce4b4 upstream-diff commit 3363bd0 ("bpf: Extend kfunc with PTR_TO_CTX, PTR_TO_MEM argument support") was introduced after 5.15 and contains an out of bound reg2btf_ids access. Since that commit hasn't been backported, this patch doesn't include fix to that access. If we backport that commit in future, we need to fix its faulting access as well 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. Fixes: c25b2ae ("bpf: Replace PTR_TO_XXX_OR_NULL with PTR_TO_XXX | PTR_MAYBE_NULL") Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]> Signed-off-by: Alexei Starovoitov <[email protected]> Link: https://lore.kernel.org/bpf/[email protected] (cherry picked from commit 45ce4b4) Signed-off-by: Brett Mastbergen <[email protected]>
1 parent 1d42df1 commit 35efc69

File tree

1 file changed

+2
-2
lines changed

1 file changed

+2
-2
lines changed

kernel/bpf/btf.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5458,9 +5458,9 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
54585458
if (reg->type == PTR_TO_BTF_ID) {
54595459
reg_btf = reg->btf;
54605460
reg_ref_id = reg->btf_id;
5461-
} else if (reg2btf_ids[reg->type]) {
5461+
} else if (reg2btf_ids[base_type(reg->type)]) {
54625462
reg_btf = btf_vmlinux;
5463-
reg_ref_id = *reg2btf_ids[reg->type];
5463+
reg_ref_id = *reg2btf_ids[base_type(reg->type)];
54645464
} else {
54655465
bpf_log(log, "kernel function %s args#%d expected pointer to %s %s but R%d is not a pointer to btf_id\n",
54665466
func_name, i,

0 commit comments

Comments
 (0)