Skip to contentSkip to conversation

libbpf: verify section type in btf_find_elf_sections

PATCH Applied Ihor Solodrai 2 participants 2 messages Apr 10, 2025 – Apr 15, 2025

Subsystems (2)
lore
Share & tools
lore

Ihor Solodrai

author reviewer
#0 lore raw

PATCH v2 1/1 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: https://github.com/libbpf/libbpf/issues/894
v1: https://lore.kernel.org/bpf/[email protected]/

---
 tools/lib/bpf/btf.c | 6 ++++++
 1 file changed, 6 insertions(+)
tools/lib/bpf/btf.c +6 -0
index 38bc6b14b066..24fc71ce5631 100644
@@ -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",
Signature · 1 line
2.49.0
#1 lore raw
This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Hello:

This patch was applied to bpf/bpf-next.git (master) by Andrii Nakryiko <[email protected]>:

Quoted 8 lines · On Thu, 10 Apr 2025 11:28:23 -0700 you wrote:

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.

[...]

Here is the summary with links:
- [bpf-next,v2] libbpf: verify section type in btf_find_elf_sections

    https://git.kernel.org/bpf/bpf-next/c/8582d9ab3efd

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html