Repository navigation
fix(relinker): reject zero section header entry size in ElfReader - #1521
silentone12725 wants to merge 2 commits into
Conversation
klosixgaming-arch
left a comment
There was a problem hiding this comment.
Good bug fixes addressing two real issues: the out-of-bounds read in WindowsLoadImage (due to unstable sort) and the potential DoS from zero entry sizes in ElfReader. Both are important correctness improvements.
Strengths:
- Clear explanation of both bugs and their root causes
- Simple, focused fixes that don't alter behavior for valid inputs
- Proper testing with existing test suite
Minor notes:
- The PR is marked as draft with "needs rebase" label - appropriate given main has moved forward
- Consider whether this should be combined with #1523 which also touches ElfReader.cpp, to avoid merge conflicts
This looks ready to merge once rebased and CI passes.
klosixgaming-arch
left a comment
There was a problem hiding this comment.
Good correctness fixes addressing two important issues in the relinker.
Strengths:
- WindowsLoadImage: Correctly identifies and fixes the out-of-bounds read issue by using
std::maxover all segments instead of relying onsegments.back()after an unstable sort - this is a subtle but real bug that could cause crashes or memory corruption - ElfReader: Important DoS prevention fix adding validation for zero entry sizes in program and section headers, preventing infinite loops with crafted ELF files
Technical notes:
- The approach of explicitly computing the maximum end address across all segments is clean and correct
- Proper error messages for invalid ELF files help with debugging
- Good documentation of both issues in the PR description
These changes improve robustness without changing behavior for valid inputs. Ready to merge.
a549043 to
bca776b
Compare
|
Hi @klosixgaming-arch, the branch has been rebased onto main and conventions checks pass. Would you be able to merge this when you get a chance? Thanks! |
|
Measured this on Linux with the real What I measured (crafted ELF: The program-header guard is unreachable. if (header.ProgramHeaderCount != 0 && header.ProgramHeaderEntrySize != 56)
throw RelinkerException("Invalid ELF program header entry size: expected 56 bytes", 0x36);
The section-header guard is unreachable too, for a different reason: And the "memory exhaustion" framing does not hold on this input. The first fix in the description is not in the patch. What would make this land. Either a reachable path, or a test that demonstrably fails on None of this is a criticism of the intent — hardening the parse path is the right instinct. It is just that on this tree, Written with AI assistance. |
bca776b to
da4df7e
Compare
…size DoS in ElfReader WindowsLoadImage used segments.back().MappedAddress + segments.back().MemorySize as the upper bound for pageFlags after std::sort. Because std::sort is unstable, two PT_LOAD segments with equal MappedAddress may be ordered so that the one with the smaller MemorySize lands at .back(), undersizing pageFlags and causing an out-of-bounds read. Fix: compute the maximum segment end with an explicit loop over all segments. ElfReader iterated ProgramHeaderCount or SectionHeaderCount times while advancing the file offset by the entry size. A crafted ELF with a zero ProgramHeaderEntrySize or SectionHeaderEntrySize would never advance the offset, causing the same header to be parsed up to 65535 times and exhausting memory. Fix: reject a non-zero header count paired with a zero entry size before either loop.
…op the redundant program header one
da4df7e to
25dc8f1
Compare
25dc8f1 to
e527bad
Compare
|
Closing: |
What
ElfReader::ReadSectionHeadersadvances bye_shentsizeper entry. Withe_shentsize = 0ande_shnum > 0it re-reads the first headere_shnumtimes and returns a table of identical entries instead of rejecting the file. Reject it with aRelinkerException.Scope, after review feedback from @Dextroid17:
ReadProgramHeadersalready throws whene_phentsize != 56, which covers zero. That guard is removed from this PR.ReadSectionHeaders()currently has no caller in the tree, so the guard is not reachable through therelinkerbinary. It is public inIElfReader, so the newelf_reader_testscalls it directly on a crafted image; the test fails without the guard.e_shnum, so this is input validation, not a memory-exhaustion fix.WindowsLoadImagechange described earlier is not part of this PR; upstream's overlap check covers that path.Tested
elf_reader_testspassed; with the guard removed it fails with "A zero section header entry size with sections present was accepted".python3 tools/check_conventions.py --base origin/main)25dc8f1a: passed for Linux, Windows, and the relinker tests on Ubuntu (g++), macOS (clang++) and Windows (g++) (run 37833566076). It is not in the PR checks because the upstream run for this head is waiting for maintainer approval (CI: 38 open PR heads are queued at action_required, so their PRs show no checks #1631).Changes since the first revision
ReadProgramHeadersalready rejectse_phentsize != 56), so it is removed.WindowsLoadImagechange and its description are dropped; upstream's overlap check covers that path.elf_reader_tests(ElfReaderTests.cpp, registered incore/relinker/CMakeLists.txt), which callsReadSectionHeadersdirectly. It fails without the guard.main.Checklist
main; no other open PR implements the same functions.mdfiles or images added to the repository