]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
libbpf: Verify section type in btf_find_elf_sections
authorIhor Solodrai <ihor.solodrai@linux.dev>
Thu, 10 Apr 2025 18:28:23 +0000 (11:28 -0700)
committerAndrii Nakryiko <andrii@kernel.org>
Tue, 15 Apr 2025 22:18:55 +0000 (15:18 -0700)
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: https://github.com/libbpf/libbpf/issues/894
v1: https://lore.kernel.org/bpf/20250408184104.3962949-1-ihor.solodrai@linux.dev/

Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
Signed-off-by: Andrii Nakryiko <andrii@kernel.org>
Link: https://lore.kernel.org/bpf/20250410182823.1591681-1-ihor.solodrai@linux.dev
tools/lib/bpf/btf.c

index 38bc6b14b0666a72b1534a227fa5d53e16c668bc..24fc71ce56319e02c98cd117d0b6355cf29fe3ce 100644 (file)
@@ -1148,6 +1148,12 @@ static int btf_find_elf_sections(Elf *elf, const char *path, struct btf_elf_secs
                else
                        continue;
 
+               if (sh.sh_type != SHT_PROGBITS) {
+                       pr_warn("unexpected section type (%d) of section(%d, %s) from %s\n",
+                               sh.sh_type, idx, name, path);
+                       goto err;
+               }
+
                data = elf_getdata(scn, 0);
                if (!data) {
                        pr_warn("failed to get section(%d, %s) data from %s\n",