Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Merge branch 'bpf-fix-null-deref-when-storing-scalar-into-kptr-slot'

Mykyta Yatsenko says:

====================
bpf: Fix NULL deref when storing scalar into kptr slot

map_kptr_match_type() accesses reg->btf before confirming the register
is PTR_TO_BTF_ID. A scalar store into a kptr slot has no btf, causing
a NULL pointer dereference. Guard base_type() first.

Signed-off-by: Mykyta Yatsenko <yatsenko@meta.com>
====================

Link: https://patch.msgid.link/20260416-kptr_crash-v1-0-5589356584b4@meta.com
Signed-off-by: Alexei Starovoitov <ast@kernel.org>

+19 -1
+4 -1
kernel/bpf/verifier.c
··· 4549 4549 int perm_flags; 4550 4550 const char *reg_name = ""; 4551 4551 4552 + if (base_type(reg->type) != PTR_TO_BTF_ID) 4553 + goto bad_type; 4554 + 4552 4555 if (btf_is_kernel(reg->btf)) { 4553 4556 perm_flags = PTR_MAYBE_NULL | PTR_TRUSTED | MEM_RCU; 4554 4557 ··· 4564 4561 perm_flags |= MEM_PERCPU; 4565 4562 } 4566 4563 4567 - if (base_type(reg->type) != PTR_TO_BTF_ID || (type_flag(reg->type) & ~perm_flags)) 4564 + if (type_flag(reg->type) & ~perm_flags) 4568 4565 goto bad_type; 4569 4566 4570 4567 /* We need to verify reg->type and reg->btf, before accessing reg->btf */
+15
tools/testing/selftests/bpf/progs/map_kptr_fail.c
··· 385 385 return 0; 386 386 } 387 387 388 + SEC("?tc") 389 + __failure __msg("invalid kptr access, R") 390 + int reject_scalar_store_to_kptr(struct __sk_buff *ctx) 391 + { 392 + struct map_value *v; 393 + int key = 0; 394 + 395 + v = bpf_map_lookup_elem(&array_map, &key); 396 + if (!v) 397 + return 0; 398 + 399 + *(volatile u64 *)&v->unref_ptr = 0xBADC0DE; 400 + return 0; 401 + } 402 + 388 403 char _license[] SEC("license") = "GPL";