BPF List
 help / color / mirror / Atom feed
* [PATCH] libbpf: check for empty BTF data section in btf_parse_elf
@ 2025-04-08 18:41 Ihor Solodrai
  2025-04-09 12:09 ` Mykyta Yatsenko
  2025-04-09 23:14 ` Andrii Nakryiko
  0 siblings, 2 replies; 6+ messages in thread
From: Ihor Solodrai @ 2025-04-08 18:41 UTC (permalink / raw)
  To: andrii, ast, daniel, eddyz87; +Cc: bpf, mykolal, kernel-team

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.

Add a null check for a buffer returned by elf_getdata() before
proceeding with its processing.

Bug report: https://github.com/libbpf/libbpf/issues/894

Signed-off-by: Ihor Solodrai <ihor.solodrai@linux.dev>
---
 tools/lib/bpf/btf.c | 6 ++++++
 1 file changed, 6 insertions(+)

diff --git a/tools/lib/bpf/btf.c b/tools/lib/bpf/btf.c
index 38bc6b14b066..90599f0311bd 100644
--- a/tools/lib/bpf/btf.c
+++ b/tools/lib/bpf/btf.c
@@ -1201,6 +1201,12 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf,
 		goto done;
 	}
 
+	if (!secs.btf_data->d_buf) {
+		pr_warn("BTF data is empty in %s\n", path);
+		err = -ENODATA;
+		goto done;
+	}
+
 	if (secs.btf_base_data) {
 		dist_base_btf = btf_new(secs.btf_base_data->d_buf, secs.btf_base_data->d_size,
 					NULL);
-- 
2.49.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2025-04-10 17:40 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-04-08 18:41 [PATCH] libbpf: check for empty BTF data section in btf_parse_elf Ihor Solodrai
2025-04-09 12:09 ` Mykyta Yatsenko
2025-04-09 15:44   ` Ihor Solodrai
2025-04-09 23:14 ` Andrii Nakryiko
2025-04-10 17:34   ` Ihor Solodrai
2025-04-10 17:39     ` Andrii Nakryiko

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox