BPF List
 help / color / mirror / Atom feed
From: "Ihor Solodrai" <ihor.solodrai@linux.dev>
To: "Mykyta Yatsenko" <mykyta.yatsenko5@gmail.com>,
	andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net,
	eddyz87@gmail.com
Cc: bpf@vger.kernel.org, mykolal@fb.com, kernel-team@meta.com
Subject: Re: [PATCH] libbpf: check for empty BTF data section in btf_parse_elf
Date: Wed, 09 Apr 2025 15:44:41 +0000	[thread overview]
Message-ID: <93d7f092eedbd7ec5618d0eb399528979013f1b0@linux.dev> (raw)
In-Reply-To: <743d6d28-b3b3-4e55-b9e7-3655bd25ad70@gmail.com>

On 4/9/25 5:09 AM, Mykyta Yatsenko wrote:
> On 08/04/2025 19:41, Ihor Solodrai 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@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); 
>
> is `secs.btf_data->d_size` non-zero in this case, making it access `secs.btf_data->d_buf`?

Yes, as it turns out.

This is also described in the spec in relation to Elf32_Shdr [1]:

    sh_size This member gives the section's size in bytes. Unless the section type is
    SHT_NOBITS, the section occupies sh_size bytes in the file. A section
    of type SHT_NOBITS may have a non-zero size, but it occupies no space
    in the file.

[1] https://refspecs.linuxfoundation.org/elf/elf.pdf

>
> Acked-by: Mykyta Yatsenko <mykyta.yatsenko5@gmail.com>

  reply	other threads:[~2025-04-09 15:44 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2025-04-09 23:14 ` Andrii Nakryiko
2025-04-10 17:34   ` Ihor Solodrai
2025-04-10 17:39     ` Andrii Nakryiko

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=93d7f092eedbd7ec5618d0eb399528979013f1b0@linux.dev \
    --to=ihor.solodrai@linux.dev \
    --cc=andrii@kernel.org \
    --cc=ast@kernel.org \
    --cc=bpf@vger.kernel.org \
    --cc=daniel@iogearbox.net \
    --cc=eddyz87@gmail.com \
    --cc=kernel-team@meta.com \
    --cc=mykolal@fb.com \
    --cc=mykyta.yatsenko5@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox