libbpf: Validate ELF symbol table entry size
PATCH 1/1 libbpf: Validate ELF symbol table entry size
elf_sym_iter_new() calculates the number of symbols by dividing the symbol table data size by sh_entsize. A malformed ELF file with a zero sh_entsize causes a division by zero.
Reject symbol table sections with a zero entry size before calculating the number of symbols.
--- tools/lib/bpf/elf.c | 5 +++++ 1 file changed, 5 insertions(+)
tools/lib/bpf/elf.c +5 -0
@@ -118,6 +118,11 @@ static int elf_sym_iter_new(struct elf_sym_iter *iter,if (!gelf_getshdr (scn, &sh))return -EINVAL ; +if (!sh.sh_entsize) { +pr_warn ("elf: symbol table section has zero entry size in '%s'\n" , binary_path); +return -EINVAL ; + } + iter->strtabidx = sh.sh_link; iter->syms =elf_getdata (scn,0 );if (!iter->syms) {
---
base-commit: 10c4f610b215bf961235141161992f010cf7e451
change-id: 20260916-libbpf-elf-entsize-fix-a0f9440e79c7
Best regards,
Signature · 1 line
Luis Vieira <[email protected]>
[email protected]
AI reviewThis looks like a bug fix for the elf symbol iterator. Should this include:
Quoted 10 lines
libbpf: Validate ELF symbol table entry size
elf_sym_iter_new()calculates the number of symbols by dividing the symbol table data size bysh_entsize. A malformed ELF file with a zerosh_entsizecauses a division by zero.Reject symbol table sections with a zero entry size before calculating the number of symbols.
Signed-off-by Luis Vieira <[email protected]>
This looks like a bug fix for the elf symbol iterator. Should this include:
---
AI reviewed your patch. Please fix the bug or email reply why it's not a bug.
See: https://github.com/kernel-patches/vmtest/blob/master/ci/claude/README.md
CI run summary: https://github.com/kernel-patches/bpf/actions/runs/35154126900
Quoted 18 lines · Luis Vieira
elf_sym_iter_new()calculates the number of symbols by dividing the symbol table data size bysh_entsize. A malformed ELF file with a zerosh_entsizecauses a division by zero.Reject symbol table sections with a zero entry size before calculating the number of symbols.
Signed-off-by Luis Vieira <[email protected]>--- tools/lib/bpf/elf.c | 5 +++++ 1 file changed, 5 insertions(+)tools/lib/bpf/elf.c +0
-0
tools/lib/bpf/elf.c static int elf_sym_iter_new(struct elf_sym_iter *iter, · line 125
Quoted 8 lines
tools/lib/bpf/elf.c +5
-0@@ -118,6 +118,11 @@ static int elf_sym_iter_new(struct elf_sym_iter *iter,if (!gelf_getshdr (scn, &sh))return -EINVAL ; +if (!sh.sh_entsize) { +pr_warn ("elf: symbol table section has zero entry size in '%s'\n" , binary_path); +return -EINVAL ; + } +
modified as follows and applied to bpf-next
- if (!gelf_getshdr(scn, &sh))
+ if (!gelf_getshdr(scn, &sh) || !sh.sh_entsize)
return -EINVAL;
- if (!sh.sh_entsize) {
- pr_warn("elf: symbol table section has zero entry size
in '%s'\n", binary_path);
- return -EINVAL;
- }
-
Quoted 12 lines
tools/lib/bpf/elf.c +0
-1iter->strtabidx = sh.sh_link; iter->syms =elf_getdata (scn,0 );if (!iter->syms) { ---base-commit:
10c4f610b215bf961235141161992f010cf7e451
change-id: 20260916-libbpf-elf-entsize-fix-a0f9440e79c7Best regards,
--
Luis Vieira <[email protected]>