Skip to content

ELF: load files with section headers past EOF - #856

Open
zardus wants to merge 1 commit into
masterfrom
feature/fix-c-367
Open

zardus wants to merge 1 commit into
masterfrom
feature/fix-c-367

Conversation

@zardus

@zardus zardus commented Oct 3, 2026 •

Copy link
Copy Markdown
Member

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Problem

When an ELF file has an e_shoff past EOF, pyelftools can raise ELFParseError during RELRO detection. The exception escapes Loader.__init__ before CLE can use its existing fallback for damaged section headers.

main_opts={"discard_section_headers": True} raises the same error because the RELRO probe runs first.

Traceback (most recent call last):
  ... cle/loader.py __init__ -> _internal_load -> _load_object_isolated ...
  File "cle/backends/elf/elf.py", line 101, in __init__
    super().__init__(*args, **kwargs)
  File "cle/backends/elf/metaelf.py", line 66, in __init__
    self.relro = _get_relro(tmp_reader)
  File "cle/backends/elf/metaelf.py", line 41, in _get_relro
    if not any(seg.header.p_type == "PT_GNU_RELRO" for seg in elf.iter_segments()):
  ... pyelftools: iter_segments -> _make_segment -> DynamicSegment.__init__ -> iter_sections ...
  File "elftools/elf/elffile.py", line 713, in _get_section_header
    raise ELFParseError(msg)
elftools.common.exceptions.ELFParseError: Reading section 0 at offset 4528 past EOF 4164

Cause

MetaELF.__init__ calls _get_relro before the body of ELF.__init__ runs. Two paths in _get_relro can read the section header table:

  • elf.iter_segments() can build a DynamicSegment for PT_DYNAMIC. Its constructor calls elffile.iter_sections().
  • elf.get_section_by_name(".dynamic") scans the sections to build its name map.

ELFFile._get_section_header raises when a section header starts past stream_len. A file with PT_DYNAMIC, such as the public fixture, fails on the first path. A file without PT_DYNAMIC, such as the sample, fails on the second path.

ELF.__init__ already recovers from this condition after super().__init__(). It installs a PatchedStream that clears e_shoff, e_shentsize, e_shnum, and e_shstrndx. Pyelftools can then load the file from its program headers. The RELRO probe currently fails before that recovery and before discard_section_headers takes effect.

2a8adaa8 added the fallback in 2018 and changed extract_soname to use program headers in #113. 4a7e4f7a added the RELRO section read in #278.

Fix

Guard the RELRO probe so that an ELFError does not end the load. The loader can then reach its existing recovery. The fallback RELRO value is Relro.NONE.

A parse error can occur while checking for PT_GNU_RELRO, so this code cannot distinguish no RELRO from an unknown RELRO level. The only consumer in CLE or angr, MetaELF._block_references_addr, compares the value with Relro.FULL. That answer requires the dynamic table that could not be read.

The guard belongs at the call site because either section read can fail. #815 moves the first read to the program headers. The segment path can still fail in iter_tags() when a PT_DYNAMIC segment has no string table, as described in #731. Those changes can reduce how often the guard runs, but they do not replace it.

Reproducer

tests/armel/fauxware.truncated.elf is tests/armel/fauxware cut at 0xf04 + 0x140, the end of the last PT_LOAD file data. All bytes described as loadable remain. The section header table at 0x11b0 is absent. tests/test_truncated_section_headers.py covers this input.

The sample cannot be shared. SHA-256 7cb8df1ec5e810bf42b3cd9fca3c28a2117f696cd800dc8620f34fdf622448bd. It is an ARM ET_EXEC whose e_shoff is past EOF.

Run these commands from the cle worktree with angr/binaries beside it. Set OBJECT to the sample:

$ pytest -q tests/test_truncated_section_headers.py
$ python -c 'import cle; print(cle.Loader(OBJECT).main_object)'
$ python -c 'import angr; print(len(angr.Project(OBJECT, auto_load_libs=False).analyses.CFGFast(normalize=True).kb.functions))'
$ cmp -n 4164 ../binaries/tests/armel/fauxware ../binaries/tests/armel/fauxware.truncated.elf

The fixture is new. At 9eb02bd1032e1d96e729df2739f9a10a337bd154, none of the 858 files under tests/ that begin with \x7fELF have a section header table past EOF.

Merge angr/binaries#246 first.

sync: angr/binaries#246

MetaELF.__init__ computes the RELRO level before ELF.__init__ gets to run, and
pyelftools reads the section header table to answer it: iter_segments() builds a
DynamicSegment for PT_DYNAMIC and that constructor walks iter_sections(), while
get_section_by_name(".dynamic") builds a name map over the whole table. On a file
whose e_shoff points past the end, pyelftools' ELFParseError escapes
Loader.__init__ and the whole load is lost -- including when
discard_section_headers is set, which is documented for section headers that are
corrupt or malicious.

ELF.__init__ already recovers from a section header table it cannot walk, by
reloading the file with the section header fields zeroed so that pyelftools reads
it from the program headers alone. Guarding the RELRO probe lets that recovery
happen. RELRO is a property CLE reports rather than one the load needs, and the
level is reported as none because the parse can fail inside the PT_GNU_RELRO test
itself, which leaves "no RELRO" and "cannot tell" indistinguishable; only
Relro.FULL changes any behaviour, and FULL is what needs the dynamic table.

tests/test_truncated_section_headers.py loads
tests/armel/fauxware.truncated.elf, which is fauxware cut at the end of its last
PT_LOAD.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@zardus

zardus commented Oct 3, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

This is the full load result for tests/armel/fauxware.truncated.elf before and after the change.

The sample cannot be shared. SHA-256 7cb8df1ec5e810bf42b3cd9fca3c28a2117f696cd800dc8620f34fdf622448bd. It also raises ELFParseError on the base and loads with this change. Its counts are in the validation record.

Before. Pyelftools raises ELFParseError from Loader.__init__. CLE does not load the segments, entry point, or dependencies.

cle master 997d432
Traceback (most recent call last):
  ... cle/loader.py __init__ -> _internal_load -> _load_object_isolated ...
  File "cle/backends/elf/elf.py", line 101, in __init__
    super().__init__(*args, **kwargs)
  File "cle/backends/elf/metaelf.py", line 66, in __init__
    self.relro = _get_relro(tmp_reader)
  File "cle/backends/elf/metaelf.py", line 41, in _get_relro
    if not any(seg.header.p_type == "PT_GNU_RELRO" for seg in elf.iter_segments()):
  File "elftools/elf/elffile.py", line 255, in iter_segments
    segment = self.get_segment(i)
  File "elftools/elf/elffile.py", line 245, in get_segment
    return self._make_segment(segment_header)
  File "elftools/elf/elffile.py", line 700, in _make_segment
    return DynamicSegment(segment_header, self.stream, self)
  File "elftools/elf/dynamic.py", line 296, in __init__
    stringtable = next(
  File "elftools/elf/dynamic.py", line 299, in <genexpr>
    for section in elffile.iter_sections()
  File "elftools/elf/elffile.py", line 222, in iter_sections
    section = self.get_section(i)
  File "elftools/elf/elffile.py", line 167, in get_section
    section_header = self._get_section_header(n)
  File "elftools/elf/elffile.py", line 713, in _get_section_header
    raise ELFParseError(msg)
elftools.common.exceptions.ELFParseError: Reading section 0 at offset 4528 past EOF 4164

After. The file loads from its program headers through the existing fallback in ELF.__init__.

with this change
object        ELF  arch <Arch ARMEL (LE)>  entry 0x84ad
os            UNIX - System V   linking dynamic   pic False   execstack False
relro         Relro.NONE
object range  [0x8000:0x1104f]   mapped_base 0x8000
loader range  [0x8000:0x100028]   (includes the extern object)
deps          ['libc.so.6', 'ld-linux-armhf.so.3']
segments      3
  vaddr 0x8000  memsize 0x7e0  filesize 0x7e0  offset 0x0  relro False
  vaddr 0x10f04  memsize 0xfc  filesize 0xfc  offset 0xf04  relro True
  vaddr 0x11000  memsize 0x50  filesize 0x44  offset 0x1000  relro False
sections      0
symbols       12
  '' rebased 0x0 size 0 type SymbolType.TYPE_NONE import False export False
  '__gmon_start__' rebased 0x0 size 0 type SymbolType.TYPE_NONE import True export False
  '__libc_start_main' rebased 0x846c size 0 type SymbolType.TYPE_FUNCTION import True export False
  '__stack_chk_fail' rebased 0x8454 size 0 type SymbolType.TYPE_FUNCTION import True export False
  '__stack_chk_guard' rebased 0x11048 size 4 type SymbolType.TYPE_OBJECT import False export True
  'abort' rebased 0x84a0 size 0 type SymbolType.TYPE_FUNCTION import True export False
  'exit' rebased 0x8494 size 0 type SymbolType.TYPE_FUNCTION import True export False
  'open' rebased 0x8488 size 0 type SymbolType.TYPE_FUNCTION import True export False
  'printf' rebased 0x843c size 0 type SymbolType.TYPE_FUNCTION import True export False
  'puts' rebased 0x8460 size 0 type SymbolType.TYPE_FUNCTION import True export False
  'read' rebased 0x8448 size 0 type SymbolType.TYPE_FUNCTION import True export False
  'strcmp' rebased 0x8430 size 0 type SymbolType.TYPE_FUNCTION import True export False
relocations   12
imports       ['__gmon_start__', '__libc_start_main', '__stack_chk_fail', 'abort', 'exit', 'open', 'printf', 'puts', 'read', 'strcmp']

@zardus

zardus commented Oct 3, 2026 •

Copy link
Copy Markdown
Member Author

THIS MESSAGE WAS GENERATED BY AN AUTOMATED PROCESS

Validation record for head 72d88761a6ece4ae084567d6583d1fba2ed5a8f2 against baseline 997d4322678fe5876d9f83e0dd34e1052813b9db, with angr/binaries at e6e7dc44fac6059734935ab75cf1beadb6c61ebf (angr/binaries#246) on both arms.

Summary. The sample and the public fixture raise ELFParseError on the baseline. Both load on the head. The sample yields 14207 CFG nodes, 19236 edges, and 3079 functions. The 947 other objects in the regression set have the same result on both revisions.

Sample. The sample cannot be shared. SHA-256 7cb8df1ec5e810bf42b3cd9fca3c28a2117f696cd800dc8620f34fdf622448bd. It is an ARM ET_EXEC. Its e_shoff is 1293496 in a 1010694-byte file. It has PT_GNU_RELRO and no PT_DYNAMIC.

The sample is a load-error in sweep epoch 2026-09-28-deep-virusshare-r210720. The error comes from elffile.py:713:_get_section_header. The corpus declares backend: elf with no options, so its recipe is main_opts={}.

  • Baseline: cle.Loader(OBJECT) raises load-error elftools.common.exceptions.ELFParseError: Reading section 0 at offset 1293496 past EOF 1010694. It raises the same error with main_opts={"discard_section_headers": True}.
  • Head: the sample loads as ELF with Arch ARMEL (LE), entry 0x8130, Relro.NONE, three segments, no sections, and linking=static. The Loader range is [0x8000:0x148d7f]. The result is the same with discard_section_headers.
  • angr.Project(OBJECT, auto_load_libs=False) raises on the baseline. On the head, analyses.CFGFast(normalize=True) returns the counts in the summary.

Public reproducer. tests/armel/fauxware.truncated.elf loads through cle.Loader(path, auto_load_libs=False). The baseline raises load-error ELFParseError: Reading section 0 at offset 4528 past EOF 4164. The head loads entry 0x84ad, three segments, no sections, 12 symbols, and linking=dynamic. Its dependencies are ['libc.so.6', 'ld-linux-armhf.so.3'].

The untruncated tests/armel/fauxware loads the same way on both revisions. It has Relro.PARTIAL, 30 sections, 133 symbols, entry 0x84ad, and range [0x8000:0x1104f]. The truncated fixture has the same range when it loads. The fixture fails in iter_segments. The sample fails in get_section_by_name.

Scope. Thirteen catalog rows declare elf.shdr_table_beyond_eof. All 13 are VirusShare objects. Twelve already load on the baseline because their program headers contain neither PT_GNU_RELRO nor PT_DYNAMIC. All 13 load on the head.

The regression set contains 947 other objects. It covers the 858 ELF paths under angr/binaries/tests/, the remaining 77 objects in the sample's shard, and the other 12 catalog rows with the same malformed section table. None of those 947 objects changes between the baseline and head.

Recorded commands. The evidence used diff -rq, cmp, PYTHONHASHSEED=0, pytest --import-mode=append -q tests/test_truncated_section_headers.py, and diff. It also used run-ci-diff-checks.py --repository cle --base 997d4322678fe5876d9f83e0dd34e1052813b9db, git merge-tree --write-tree, and git rev-parse. The resolver inspection used head -n 1.

Limits.

  • The guard catches elftools.common.exceptions.ELFError, which the measured objects raise. Pyelftools can assert on other malformed inputs, as described in ELF: Handle missing dynamic string tables #731. This change does not catch those assertions.
  • discard_section_headers=True reaches the existing fallback after ELF.__init__ tries iter_sections(). This change restores the option's result but does not remove that extra read.
  • Relro.NONE means CLE could not determine the level for an affected file. It does not prove that the file has no RELRO. Both measured inputs contain PT_GNU_RELRO.

@angr-bot

angr-bot commented Oct 4, 2026

Copy link
Copy Markdown
Member

Corpus decompilation diffs can be found at angr/dec-snapshots@master...angr/cle_856

@zardus zardus changed the title ELF: do not lose the load when the section header table starts past EOF ELF: load files whose section header table starts past EOF instead of raising ELFParseError Oct 15, 2026
@zardus zardus changed the title ELF: load files whose section header table starts past EOF instead of raising ELFParseError ELF: load files with section headers past EOF Dec 3, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants