Skip to contentSkip to conversation

libbpf: Validate ELF symbol table entry size

PATCH In review Luis Vieira 3 participants 3 messages Sep 16 – Sep 21

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

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
index fe136d025967..ba0b3df37a59 100644
@@ -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]>
#1 lore raw
This 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 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.

This looks like a bug fix for the elf symbol iterator. Should this include:

Fixes 3774705db171 ("libbpf: Add elf symbol iterator")

---
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

#2 lore raw
In reply to Luis Vieira
Quoted 18 lines · Luis Vieira

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 +0 -0
index fe136d025967..ba0b3df37a59 100644

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 -1
        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,
--
Luis Vieira <[email protected]>