libbpf: check for empty BTF data section in btf_parse_elf
Ihor Solodrai
reviewerPATCH 1/1 libbpf: check for empty BTF data section in btf_parse_elf
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
--- tools/lib/bpf/btf.c | 6 ++++++ 1 file changed, 6 insertions(+)
tools/lib/bpf/btf.c +6 -0
@@ -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 );
Signature · 1 line
2.49.0
Quoted 20 lines · Ihor Solodrai
A valid ELF file may contain a
SHT_NOBITS.BTF section. This case is not handled correctly inbtf_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 <[email protected]>--- tools/lib/bpf/btf.c | 6 ++++++ 1 file changed, 6 insertions(+)tools/lib/bpf/btf.c +0
-0@@ -1201,6 +1201,12 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf, goto done; }
tools/lib/bpf/btf.c static struct btf *btf_parse_elf(const char *path, struct btf *base_btf, · line 1212
Quoted 10 lines
tools/lib/bpf/btf.c +6
-0+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?
Ihor Solodrai
reviewerQuoted 24 lines · Mykyta Yatsenko
Quoted 23 lines · Ihor Solodrai
A valid ELF file may contain a
SHT_NOBITS.BTF section. This case is not handled correctly inbtf_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 <[email protected]>--- tools/lib/bpf/btf.c | 6 ++++++ 1 file changed, 6 insertions(+)tools/lib/bpf/btf.c +2
-0@@ -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 ;
Quoted 8 lines
tools/lib/bpf/btf.c static struct btf *btf_parse_elf(const char *path, struct btf *base_btf, · line 1212
Quoted 6 lines
tools/lib/bpf/btf.c +3
-0+ 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_sizenon-zero in this case, making it accesssecs.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
Quoted 2 lines
Acked-by Mykyta Yatsenko <[email protected]>
Quoted 19 lines · Ihor Solodrai
A valid ELF file may contain a
SHT_NOBITS.BTF section. This case is not handled correctly inbtf_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 <[email protected]>--- tools/lib/bpf/btf.c | 6 ++++++ 1 file changed, 6 insertions(+)tools/lib/bpf/btf.c +0
-0
tools/lib/bpf/btf.c static struct btf *btf_parse_elf(const char *path, struct btf *base_btf, · line 1209
Quoted 9 lines
tools/lib/bpf/btf.c +6
-0@@ -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
Quoted 7 lines
tools/lib/bpf/btf.c +0
-1if (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
Ihor Solodrai
reviewerQuoted 23 lines · Andrii Nakryiko
Quoted 22 lines · Ihor Solodrai
A valid ELF file may contain a
SHT_NOBITS.BTF section. This case is not handled correctly inbtf_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 <[email protected]>--- tools/lib/bpf/btf.c | 6 ++++++ 1 file changed, 6 insertions(+)tools/lib/bpf/btf.c +0
-0@@ -1201,6 +1201,12 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf, goto done; }
Quoted 9 lines
tools/lib/bpf/btf.c static struct btf *btf_parse_elf(const char *path, struct btf *base_btf, · line 1209
Quoted 6 lines
tools/lib/bpf/btf.c +6
-0+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?
Quoted 16 lines
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
Quoted 7 lines
tools/lib/bpf/btf.c +0
-1if (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
Quoted 52 lines · Ihor Solodrai
Quoted 23 lines · Andrii Nakryiko
Quoted 22 lines · Ihor Solodrai
A valid ELF file may contain a
SHT_NOBITS.BTF section. This case is not handled correctly inbtf_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 <[email protected]>--- tools/lib/bpf/btf.c | 6 ++++++ 1 file changed, 6 insertions(+)tools/lib/bpf/btf.c +0
-0@@ -1201,6 +1201,12 @@ static struct btf *btf_parse_elf(const char *path, struct btf *base_btf, goto done; }Quoted 9 lines
tools/lib/bpf/btf.c static struct btf *btf_parse_elf(const char *path, struct btf *base_btf, · line 1209
Quoted 6 lines
tools/lib/bpf/btf.c +6
-0+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:
(quoted diff) +6
-0@@ -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.
Quoted 4 lines
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?
yeah, I think it has to be SHT_PROGBITS, everything else is either zero-initialized section (SHT_NOBITS), which is useless for BTF data. Or it's some other special type of section.
Quoted 17 lines
Quoted 17 lines
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
Quoted 7 lines
tools/lib/bpf/btf.c +0
-1if (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