On Tue, Apr 8, 2025 at 11:41 AM Ihor Solodrai <ihor.solodrai@xxxxxxxxx> 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. > > 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@xxxxxxxxx> > --- > 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; > + } > + let's handle this more generally for all BTF data sections in btf_find_elf_sections()? let's also use similar style of warning messaging to others, maybe something like "failed to get section(%s, %d) data from %s\n" ? pw-bot: cr > 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 > >