Repository navigation
ELF: handle SHT_NOBITS and malformed version sections - #68
Merged
Merged
Conversation
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 Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes ELF readers that parse garbage on split-debuginfo files (
objcopy --only-keep-debug), which keep every section header but turn the loadable sections intoSHT_NOBITS.SHT_NOBITScontents. New exportedsection_has_contents(::ELFSection / ::ELFSectionRef).ELFVersionData,ELFVersionNeededData,ELFDynEntries(and soDynamicLinks/RPath) return empty results for NOBITS sections, andSymbolsover a NOBITS.dynsymhas no entries. Previously these returned bogus entries read from a meaningless file offset..gnu.version_d/.gnu.version_rwalks. A zero-length section yields no entries. An entry, aux, or.dynstrname offset outside its section throws an error instead of reading past it, and the walk is guaranteed to end.ELFVerNeed.vn_fileisUInt32(anElf_Wordin the spec), notUInt16.Tests use a new split-debuginfo fixture,
test/splitdebug/(regenerated by itsbuild.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