From ab788aaa7c2f98702ceeb40c8f6c52e72cd9ee2c Mon Sep 17 00:00:00 2001 From: Andrii Nakryiko Date: Wed, 24 Aug 2022 11:24:57 +0800 Subject: [PATCH] libbpf: Improve sanity checking during BTF fix up mainline inclusion from mainline-5.17-rc1 commit 88918dc12dc357a06d8d722a684617b1c87a4654 category: feature bugzilla: https://gitee.com/openeuler/kernel/issues/I5EUVD CVE: NA Reference: https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=88918dc12dc357a06d8d722a684617b1c87a4654 ------------------------------------------------- If BTF is corrupted DATASEC's variable type ID might be incorrect. Prevent this easy to detect situation with extra NULL check. Reported by oss-fuzz project. Signed-off-by: Andrii Nakryiko Signed-off-by: Alexei Starovoitov Acked-by: Yonghong Song Link: https://lore.kernel.org/bpf/20211103173213.1376990-3-andrii@kernel.org (cherry picked from commit 88918dc12dc357a06d8d722a684617b1c87a4654) Signed-off-by: Wang Yufen --- tools/lib/bpf/libbpf.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/tools/lib/bpf/libbpf.c b/tools/lib/bpf/libbpf.c index 7997075b1ae0..c26e65761048 100644 --- a/tools/lib/bpf/libbpf.c +++ b/tools/lib/bpf/libbpf.c @@ -2709,13 +2709,12 @@ static int btf_fixup_datasec(struct bpf_object *obj, struct btf *btf, for (i = 0, vsi = btf_var_secinfos(t); i < vars; i++, vsi++) { t_var = btf__type_by_id(btf, vsi->type); - var = btf_var(t_var); - - if (!btf_is_var(t_var)) { + if (!t_var || !btf_is_var(t_var)) { pr_debug("Non-VAR type seen in section %s\n", name); return -EINVAL; } + var = btf_var(t_var); if (var->linkage == BTF_VAR_STATIC) continue; -- GitLab