Skip to content

Commit 6982653

Browse files
mykyta5gregkh
authored andcommitted
bpf: Fix NULL deref in map_kptr_match_type for scalar regs
[ Upstream commit 4d0a375 ] Commit ab6c637 ("bpf: Fix a bpf_kptr_xchg() issue with local kptr") refactored map_kptr_match_type() to branch on btf_is_kernel() before checking base_type(). A scalar register stored into a kptr slot has no btf, so the btf_is_kernel(reg->btf) call dereferences NULL. Move the base_type() != PTR_TO_BTF_ID guard before any reg->btf access. Fixes: ab6c637 ("bpf: Fix a bpf_kptr_xchg() issue with local kptr") Reported-by: Hiker Cl <clhiker365@gmail.com> Closes: https://bugzilla.kernel.org/show_bug.cgi?id=221372 Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com> Acked-by: Paul Chaignon <paul.chaignon@gmail.com> Link: https://lore.kernel.org/r/20260416-kptr_crash-v1-1-5589356584b4@meta.com Signed-off-by: Alexei Starovoitov <ast@kernel.org> Signed-off-by: Sasha Levin <sashal@kernel.org>
1 parent 5c04f89 commit 6982653

1 file changed

Lines changed: 4 additions & 1 deletion

File tree

kernel/bpf/verifier.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5843,6 +5843,9 @@ static int map_kptr_match_type(struct bpf_verifier_env *env,
58435843
int perm_flags;
58445844
const char *reg_name = "";
58455845

5846+
if (base_type(reg->type) != PTR_TO_BTF_ID)
5847+
goto bad_type;
5848+
58465849
if (btf_is_kernel(reg->btf)) {
58475850
perm_flags = PTR_MAYBE_NULL | PTR_TRUSTED | MEM_RCU;
58485851

@@ -5855,7 +5858,7 @@ static int map_kptr_match_type(struct bpf_verifier_env *env,
58555858
perm_flags |= MEM_PERCPU;
58565859
}
58575860

5858-
if (base_type(reg->type) != PTR_TO_BTF_ID || (type_flag(reg->type) & ~perm_flags))
5861+
if (type_flag(reg->type) & ~perm_flags)
58595862
goto bad_type;
58605863

58615864
/* We need to verify reg->type and reg->btf, before accessing reg->btf */

0 commit comments

Comments
 (0)