Skip to content

Commit f0ce1bc

Browse files
kkdwvdgregkh
authored andcommitted
bpf: Fix crash due to out of bounds access into reg2btf_ids.
[ Upstream commit 45ce4b4 ] 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 <memxor@gmail.com> Signed-off-by: Alexei Starovoitov <ast@kernel.org> Link: https://lore.kernel.org/bpf/20220216201943.624869-1-memxor@gmail.com Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 1bd5b96 commit f0ce1bc

1 file changed

Lines changed: 3 additions & 2 deletions

File tree

‎kernel/bpf/btf.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5676,7 +5676,8 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
56765676
}
56775677
if (check_ctx_reg(env, reg, regno))
56785678
return -EINVAL;
5679-
} else if (is_kfunc && (reg->type == PTR_TO_BTF_ID || reg2btf_ids[reg->type])) {
5679+
} else if (is_kfunc && (reg->type == PTR_TO_BTF_ID ||
5680+
(reg2btf_ids[base_type(reg->type)] && !type_flag(reg->type)))) {
56805681
const struct btf_type *reg_ref_t;
56815682
const struct btf *reg_btf;
56825683
const char *reg_ref_tname;
@@ -5694,7 +5695,7 @@ static int btf_check_func_arg_match(struct bpf_verifier_env *env,
56945695
reg_ref_id = reg->btf_id;
56955696
} else {
56965697
reg_btf = btf_vmlinux;
5697-
reg_ref_id = *reg2btf_ids[reg->type];
5698+
reg_ref_id = *reg2btf_ids[base_type(reg->type)];
56985699
}
56995700

57005701
reg_ref_t = btf_type_skip_modifiers(reg_btf, reg_ref_id,

0 commit comments

Comments
 (0)