From 5bf97cce6b2a011a75b7893cc4514da51433466a Mon Sep 17 00:00:00 2001 From: Bram910 Date: Wed, 21 Oct 2026 22:01:19 +0200 Subject: [PATCH] fix(tools): validate ELF import table file ranges --- tools/import_audit.py | 3 ++ tools/tests/test_import_audit.py | 89 ++++++++++++++++++++++++++++++++ 2 files changed, 92 insertions(+) diff --git a/tools/import_audit.py b/tools/import_audit.py index 75540f1667..5c2573996a 100644 --- a/tools/import_audit.py +++ b/tools/import_audit.py @@ -84,6 +84,7 @@ def elf_program_dynamic_symbols(path, data): _, _, dynamic_offset, _, _, dynamic_size, _, _ = dynamic_segments[0] if dynamic_size % 16: raise AuditError(f"{path}: dynamic segment size {dynamic_size} is not a multiple of 16") + take(data, dynamic_offset, dynamic_size, path, "dynamic segment") dynamic = {} needed_offsets = [] for position in range(0, dynamic_size, 16): @@ -197,6 +198,7 @@ def elf_imports(path, data): if kind == 11: if entry != 24 or size % 24 or link >= len(sections) or sections[link][1] != 3: raise AuditError(f"{path}: invalid dynamic symbol table (entry size {entry}, size {size}, string table section {link})") + take(data, offset, size, path, "dynamic symbol table") strings_offset, strings_size = sections[link][4], sections[link][5] take(data, strings_offset, strings_size, path, "dynamic string table") for position in range(0, size, 24): @@ -207,6 +209,7 @@ def elf_imports(path, data): elif kind == 6: if entry != 16 or size % 16 or link >= len(sections) or sections[link][1] != 3: raise AuditError(f"{path}: invalid dynamic section (entry size {entry}, size {size}, string table section {link})") + take(data, offset, size, path, "dynamic section") strings_offset, strings_size = sections[link][4], sections[link][5] take(data, strings_offset, strings_size, path, "dynamic string table") for position in range(0, size, 16): diff --git a/tools/tests/test_import_audit.py b/tools/tests/test_import_audit.py index b719d91dc6..7a6e7f7009 100644 --- a/tools/tests/test_import_audit.py +++ b/tools/tests/test_import_audit.py @@ -104,6 +104,13 @@ def exporting(*names): return build_elf([(name, 1, GLOBAL_FUNCTION) for name in names]) +def elf_dynamic_range_fields(image): + sections = struct.unpack_from("