Skip to content

Commit

Permalink
libbpf: Fix null-pointer dereference in find_prog_by_sec_insn()
Browse files Browse the repository at this point in the history
When there are no program sections, obj->programs is left unallocated,
and find_prog_by_sec_insn()'s search lands on &obj->programs[0] == NULL,
and will cause null-pointer dereference in the following access to
prog->sec_idx.

Guard the search with obj->nr_programs similar to what's being done in
__bpf_program__iter() to prevent null-pointer access from happening.

Fixes: db2b8b0 ("libbpf: Support CO-RE relocations for multi-prog sections")
Signed-off-by: Shung-Hsi Yu <shung-hsi.yu@suse.com>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20221012022353.7350-4-shung-hsi.yu@suse.com
  • Loading branch information
Shung-Hsi Yu authored and Andrii Nakryiko committed Oct 13, 2022
1 parent 35a8555 commit d0d382f
Showing 1 changed file with 3 additions and 0 deletions.
3 changes: 3 additions & 0 deletions tools/lib/bpf/libbpf.c
Original file line number Diff line number Diff line change
Expand Up @@ -4115,6 +4115,9 @@ static struct bpf_program *find_prog_by_sec_insn(const struct bpf_object *obj,
int l = 0, r = obj->nr_programs - 1, m;
struct bpf_program *prog;

if (!obj->nr_programs)
return NULL;

while (l < r) {
m = l + (r - l + 1) / 2;
prog = &obj->programs[m];
Expand Down

0 comments on commit d0d382f

Please sign in to comment.