Skip to content

Commit 80b2b5c

Browse files
andreimateianakryiko
authored andcommitted
libbpf: Fail early when loading programs with unspecified type
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: Andrei Matei <andreimatei1@gmail.com> Signed-off-by: Andrii Nakryiko <andrii@kernel.org> Link: https://lore.kernel.org/bpf/20201203043410.59699-1-andreimatei1@gmail.com
1 parent a8b415c commit 80b2b5c

File tree

1 file changed

+14
-1
lines changed

1 file changed

+14
-1
lines changed

tools/lib/bpf/libbpf.c

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6629,6 +6629,16 @@ load_program(struct bpf_program *prog, struct bpf_insn *insns, int insns_cnt,
66296629
char *log_buf = NULL;
66306630
int btf_fd, ret;
66316631

6632+
if (prog->type == BPF_PROG_TYPE_UNSPEC) {
6633+
/*
6634+
* The program type must be set. Most likely we couldn't find a proper
6635+
* section definition at load time, and thus we didn't infer the type.
6636+
*/
6637+
pr_warn("prog '%s': missing BPF prog type, check ELF section name '%s'\n",
6638+
prog->name, prog->sec_name);
6639+
return -EINVAL;
6640+
}
6641+
66326642
if (!insns || !insns_cnt)
66336643
return -EINVAL;
66346644

@@ -6920,9 +6930,12 @@ __bpf_object__open(const char *path, const void *obj_buf, size_t obj_buf_sz,
69206930

69216931
bpf_object__for_each_program(prog, obj) {
69226932
prog->sec_def = find_sec_def(prog->sec_name);
6923-
if (!prog->sec_def)
6933+
if (!prog->sec_def) {
69246934
/* couldn't guess, but user might manually specify */
6935+
pr_debug("prog '%s': unrecognized ELF section name '%s'\n",
6936+
prog->name, prog->sec_name);
69256937
continue;
6938+
}
69266939

69276940
if (prog->sec_def->is_sleepable)
69286941
prog->prog_flags |= BPF_F_SLEEPABLE;

0 commit comments

Comments
 (0)