Skip to content

Commit 8582d9a

Browse files
theihoranakryiko
authored andcommitted
libbpf: Verify section type in btf_find_elf_sections
A valid ELF file may contain a SHT_NOBITS .BTF section. This case is not handled correctly in btf_parse_elf, which leads to a segfault. Before attempting to load BTF section data, check that the section type is SHT_PROGBITS, which is the expected type for BTF data. Fail with an error if the type is different. Bug report: libbpf/libbpf#894 v1: https://lore.kernel.org/bpf/[email protected]/ Signed-off-by: Ihor Solodrai <[email protected]> Signed-off-by: Andrii Nakryiko <[email protected]> Link: https://lore.kernel.org/bpf/[email protected]
1 parent ee684de commit 8582d9a

File tree

1 file changed

+6
-0
lines changed

1 file changed

+6
-0
lines changed

tools/lib/bpf/btf.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1148,6 +1148,12 @@ static int btf_find_elf_sections(Elf *elf, const char *path, struct btf_elf_secs
11481148
else
11491149
continue;
11501150

1151+
if (sh.sh_type != SHT_PROGBITS) {
1152+
pr_warn("unexpected section type (%d) of section(%d, %s) from %s\n",
1153+
sh.sh_type, idx, name, path);
1154+
goto err;
1155+
}
1156+
11511157
data = elf_getdata(scn, 0);
11521158
if (!data) {
11531159
pr_warn("failed to get section(%d, %s) data from %s\n",

0 commit comments

Comments
 (0)