Skip to content

ELF: handle SHT_NOBITS and malformed version sections - #68

Merged
staticfloat merged 3 commits into
masterfrom
sf/verneed-nobits
Nov 16, 2026
Merged

staticfloat merged 3 commits into
masterfrom
sf/verneed-nobits

Conversation

@staticfloat

Copy link
Copy Markdown
Collaborator

Fixes ELF readers that parse garbage on split-debuginfo files (objcopy --only-keep-debug), which keep every section header but turn the loadable sections into SHT_NOBITS.

  • Don't read SHT_NOBITS contents. New exported section_has_contents(::ELFSection / ::ELFSectionRef). ELFVersionData, ELFVersionNeededData, ELFDynEntries (and so DynamicLinks/RPath) return empty results for NOBITS sections, and Symbols over a NOBITS .dynsym has no entries. Previously these returned bogus entries read from a meaningless file offset.
  • Bounds-check the .gnu.version_d / .gnu.version_r walks. A zero-length section yields no entries. An entry, aux, or .dynstr name offset outside its section throws an error instead of reading past it, and the walk is guaranteed to end.
  • ELFVerNeed.vn_file is UInt32 (an Elf_Word in the spec), not UInt16.

Tests use a new split-debuginfo fixture, test/splitdebug/ (regenerated by its build.sh), plus in-memory corrupted copies of it to cover the out-of-bounds cases.

🤖 Generated with Claude Code

https://claude.ai/code/session_01Jnarj125x8jtTirhjAV1jc

staticfloat and others added 3 commits November 15, 2026 17:56
A `SHT_NOBITS` section occupies no space in the file, so its `sh_offset`
and `sh_size` don't describe any readable bytes.  Split-debuginfo files
(`objcopy --only-keep-debug`) keep the headers of every loadable section
but turn them into `SHT_NOBITS`, so readers that trusted them parsed
whatever happened to be at the (meaningless) offset:

- `ELFVersionData` / `ELFVersionNeededData` returned a bogus all-zero
  entry (or could throw or walk garbage `vn_next`/`vd_next` chains).
- `ELFDynEntries` read garbage `.dynamic` entries until it happened to
  hit a zero, which also fed `DynamicLinks` and `RPath`.
- `Symbols` over a NOBITS `.dynsym` reported `sh_size / entsize` entries
  of garbage.

Add `section_has_contents(::ELFSection)` / `(::ELFSectionRef)` and make
these readers return empty results for NOBITS sections.  The version
readers also check `.dynstr`, and no longer call `StrTab(nothing)` when
there is no `.dynstr` at all.

Add a split-debuginfo fixture (`test/splitdebug`, regenerated by its
`build.sh`) and tests for both the original library and its debug file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jnarj125x8jtTirhjAV1jc
`ELFVersionData` and `ELFVersionNeededData` followed the
`vd_next`/`vda_next`/`vn_next`/`vna_next` chains without ever looking at
the section's size, so a zero-length or truncated section, or a bad
offset, made them parse whatever followed in the file, or hit EOF.

Read every entry relative to the section start and throw an error when
an entry, or a name offset into `.dynstr`, lies outside its section.  A
zero-length section yields no entries.  Since the `*_next` offsets are
unsigned, each step moves forward, so the bounds check also guarantees
the walk ends.  An aux chain that ends (`*_next == 0`) before
`vd_cnt`/`vn_cnt` entries stops there instead of re-reading the last
aux.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jnarj125x8jtTirhjAV1jc
`Elf32_Verneed`/`Elf64_Verneed` declare `vn_file` as an `Elf_Word`, but
`ELFVerNeed` declared it `UInt16`.  Alignment padding kept `vn_aux` and
`vn_next` at the right offsets, so this only read the low half of
`vn_file` in little-endian files (wrong once `.dynstr` exceeds 64 KiB),
and the high half, normally zero, in big-endian ones.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01Jnarj125x8jtTirhjAV1jc
@codecov

codecov Bot commented Nov 16, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 73.47%. Comparing base (c3e7dfa) to head (933f369).

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #68      +/-   ##
==========================================
+ Coverage   73.21%   73.47%   +0.25%     
==========================================
  Files          48       48              
  Lines        1572     1587      +15     
==========================================
+ Hits         1151     1166      +15     
  Misses        421      421              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@staticfloat
staticfloat merged commit e5de92b into master Nov 16, 2026
17 checks passed
@staticfloat
staticfloat deleted the sf/verneed-nobits branch November 16, 2026 02:13
@staticfloat staticfloat mentioned this pull request Nov 16, 2026
staticfloat referenced this pull request Nov 16, 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.

1 participant