libbpf: Fix null-pointer dereference in find_prog_by_sec_insn()
authorShung-Hsi Yu <shung-hsi.yu@suse.com>
Wed, 12 Oct 2022 02:23:53 +0000 (10:23 +0800)
committerAdrian Bunk <bunk@debian.org>
Thu, 24 Apr 2025 12:56:45 +0000 (15:56 +0300)
commit934a37426ef9f1706adae10083029fb65ac7a21e
tree60518cd3ef6cf6cf756084deac9436cbbb883b57
parent0d75ffb01a87c7d2121f060dc9ac00e799f4f7ab
libbpf: Fix null-pointer dereference in find_prog_by_sec_insn()

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: db2b8b06423c ("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
Gbp-Pq: Name 0002-libbpf-Fix-null-pointer-dereference-in-find_prog_by_.patch
src/libbpf.c