提交 b763ba2a 编写于 作者: A Andrei Matei 提交者: Zheng Zengkai

libbpf: Fail early when loading programs with unspecified type

mainline inclusion
from mainline-5.11-rc1
commit 80b2b5c3
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=80b2b5c3a701d56de98d00d99bc9cc384fb316d9

-------------------------------------------------

Before this patch, a program with unspecified type
(BPF_PROG_TYPE_UNSPEC) would be passed to the BPF syscall, only to have
the kernel reject it with an opaque invalid argument error. This patch
makes libbpf reject such programs with a nicer error message - in
particular libbpf now tries to diagnose bad ELF section names at both
open time and load time.
Signed-off-by: NAndrei Matei <andreimatei1@gmail.com>
Signed-off-by: NAndrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20201203043410.59699-1-andreimatei1@gmail.com
(cherry picked from commit 80b2b5c3)
Signed-off-by: NWang Yufen <wangyufen@huawei.com>
上级 5036be89
...@@ -6687,6 +6687,16 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt, ...@@ -6687,6 +6687,16 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
char *log_buf = NULL; char *log_buf = NULL;
int btf_fd, ret; int btf_fd, ret;
if (prog->type == BPF_PROG_TYPE_UNSPEC) {
/*
* The program type must be set. Most likely we couldn't find a proper
* section definition at load time, and thus we didn't infer the type.
*/
pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n",
prog->name, prog->sec_name);
return -EINVAL;
}
if (!insns || !insns_cnt) if (!insns || !insns_cnt)
return -EINVAL; return -EINVAL;
...@@ -6982,9 +6992,12 @@ __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz, ...@@ -6982,9 +6992,12 @@ __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
bpf_object__for_each_program(prog, obj) { bpf_object__for_each_program(prog, obj) {
prog->sec_def = find_sec_def(prog->sec_name); prog->sec_def = find_sec_def(prog->sec_name);
if (!prog->sec_def) if (!prog->sec_def) {
/* couldn't guess, but user might manually specify */ /* couldn't guess, but user might manually specify */
pr_debug("prog '%s': unrecognized ELF section name '%s'\n",
prog->name, prog->sec_name);
continue; continue;
}
if (prog->sec_def->is_sleepable) if (prog->sec_def->is_sleepable)
prog->prog_flags |= BPF_F_SLEEPABLE; prog->prog_flags |= BPF_F_SLEEPABLE;
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册