Skip to content

Commit 938c72e

Browse files
kkdwivediNobody
authored and
Nobody
committed
bpf: Check for NULL return from bpf_get_btf_vmlinux
When CONFIG_DEBUG_INFO_BTF is disabled, bpf_get_btf_vmlinux can return a NULL pointer. Check for it in btf_get_module_btf to prevent a NULL pointer dereference. While kernel test robot only complained about this specific case, let's also check for NULL in other call sites of bpf_get_btf_vmlinux. Fixes: 9492450 ("bpf: Always raise reference in btf_get_module_btf") Reported-by: kernel test robot <[email protected]> Signed-off-by: Kumar Kartikeya Dwivedi <[email protected]>
1 parent 7db93bc commit 938c72e

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

kernel/bpf/btf.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -534,6 +534,8 @@ static s32 bpf_find_btf_id(const char *name, u32 kind, struct btf **btf_p)
534534
btf = bpf_get_btf_vmlinux();
535535
if (IS_ERR(btf))
536536
return PTR_ERR(btf);
537+
if (!btf)
538+
return -EINVAL;
537539

538540
ret = btf_find_by_name_kind(btf, name, kind);
539541
/* ret is never zero, since btf_find_by_name_kind returns
@@ -6584,7 +6586,7 @@ static struct btf *btf_get_module_btf(const struct module *module)
65846586

65856587
if (!module) {
65866588
btf = bpf_get_btf_vmlinux();
6587-
if (!IS_ERR(btf))
6589+
if (!IS_ERR_OR_NULL(btf))
65886590
btf_get(btf);
65896591
return btf;
65906592
}
@@ -7180,6 +7182,8 @@ bpf_core_find_cands(struct bpf_core_ctx *ctx, u32 local_type_id)
71807182
main_btf = bpf_get_btf_vmlinux();
71817183
if (IS_ERR(main_btf))
71827184
return ERR_CAST(main_btf);
7185+
if (!main_btf)
7186+
return ERR_PTR(-EINVAL);
71837187

71847188
local_type = btf_type_by_id(local_btf, local_type_id);
71857189
if (!local_type)

net/core/bpf_sk_storage.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,8 @@ static bool bpf_sk_storage_tracing_allowed(const struct bpf_prog *prog)
405405
case BPF_TRACE_FENTRY:
406406
case BPF_TRACE_FEXIT:
407407
btf_vmlinux = bpf_get_btf_vmlinux();
408+
if (IS_ERR_OR_NULL(btf_vmlinux))
409+
return false;
408410
btf_id = prog->aux->attach_btf_id;
409411
t = btf_type_by_id(btf_vmlinux, btf_id);
410412
tname = btf_name_by_offset(btf_vmlinux, t->name_off);

0 commit comments

Comments
 (0)