From: "Ihor Solodrai" <ihor.solodrai@linux.dev>
To: "Andrii Nakryiko" <andrii.nakryiko@gmail.com>
Cc: andrii@kernel.org, ast@kernel.org, daniel@iogearbox.net,
eddyz87@gmail.com, 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: Thu, 10 Apr 2025 17:34:46 +0000 [thread overview]
Message-ID: <9cc4191bf68d1943c49b68d8fefe89db8a114d2f@linux.dev> (raw)
In-Reply-To: <CAEf4BzYwLHDMgWW8m2_exZvGmU7otODRueJx3CvbUPoMGEPNuA@mail.gmail.com>
On 4/9/25 4:14 PM, Andrii Nakryiko wrote:
> On Tue, Apr 8, 2025 at 11:41 AM Ihor Solodrai <ihor.solodrai@linux.dev> 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;
>> + }
>> +
>
> let's handle this more generally for all BTF data sections in
> btf_find_elf_sections()?
Sure. I think it makes sense to check for the section type before
attempting to load a buffer like this:
@@ -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_NOBITS) {
+ pr_warn("failed to get section(%d, %s) data from %s\n",
+ idx, name, path);
+ goto err;
+ }
+
But then we might as well test for the expected section type, which is
supposed to be SHT_PROGBITS, if I understand correctly.
What I don't know is whether this is *the only* possible expected type
(for ".BTF", ".BTF.ext" and ".BTF.base"), or are there others?
Andrii, do you know if that's the case?
>
> 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
>>
>>
next prev parent reply other threads:[~2025-04-10 17:34 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
2025-04-09 23:14 ` Andrii Nakryiko
2025-04-10 17:34 ` Ihor Solodrai [this message]
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=9cc4191bf68d1943c49b68d8fefe89db8a114d2f@linux.dev \
--to=ihor.solodrai@linux.dev \
--cc=andrii.nakryiko@gmail.com \
--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 \
/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