fix(relinker): reject zero section header entry size in ElfReader #1521
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/1521"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from boykopovar/AnyPS5. Originally posted by silentone12725 on 2026-10-08T11:02:29Z.
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 (#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 repositoryImported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T11:16:38Z.
Review: COMMENTED
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:
Minor notes:
This looks ready to merge once rebased and CI passes.
Imported from boykopovar/AnyPS5. Originally posted by klosixgaming-arch on 2026-10-08T11:23:04Z.
Review: APPROVED
Good correctness fixes addressing two important issues in the relinker.
Strengths:
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 corruptionTechnical notes:
These changes improve robustness without changing behavior for valid inputs. Ready to merge.
Imported from boykopovar/AnyPS5. Originally posted by silentone12725 on 2026-10-08T13:29:25Z.
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!
Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T14:28:31Z.
Measured this on Linux with the real
relinkerbinary, and I think both halves need a different home because neither guard can run on the current tree.What I measured (crafted ELF:
e_phentsize = 0withe_phnum = 0xffff, ande_shentsize = 0withe_shnum = 0xffff):The program-header guard is unreachable.
ReadProgramHeadersalready has, a few lines above your insertion point:entrySize == 0is a subset ofentrySize != 56, so main's check always fires first — which is why the run above reports that message and never yours. Your guard adds no behaviour.The section-header guard is unreachable too, for a different reason:
ReadSectionHeaders()has no caller in the tree. Searching every.cpp/.hppoutside the interface and its implementation turns up three callers ofReadProgramHeaders(RelinkerPipeline.cpp:41,GuestImageReader.cpp:32,StrictNidFilter.cpp:23) and none ofReadSectionHeaders. The loop cannot be entered, so a zero entry size cannot reach it.And the "memory exhaustion" framing does not hold on this input.
e_shnum/e_phnumare 16-bit, so the loop is bounded at 65535 iterations of eight reads — measured at 0.9 ms with no growth in peak RSS over the control (16.4 vs 16.5 MiB). It is not a DoS; it is a malformed file that currently converts, which is a weaker (but still arguable) claim.The first fix in the description is not in the patch.
gh pr diff 1521has 0 occurrences ofWindowsLoadImage,pageFlagsorstd::max— the diff is only the two guards inElfReader.cpp. And the shape it describes is not in the code either:WindowsLoadImage.cpphas nosegments.back()(the only search hit in the file is thestd::sortat line 29), and the upper bound is a runningendin a loop that throwsOverlapping PT_LOAD memory rangeswhen a segment starts before it — so two equalMappedAddressvalues are rejected before any ordering question arises, and the page loop is bounded by construction.What would make this land. Either a reachable path, or a test that demonstrably fails on
main—ReadSectionHeaders()is public inIElfReader, so a unit test could call it directly on a crafted image and assert the throw; that at least makes the guard provable rather than defensive-only. YourTestedsection runs three unrelated suites and, as noted there, the full build was not run locally, which is probably why the guards look live from the source alone.None of this is a criticism of the intent — hardening the parse path is the right instinct. It is just that on this tree,
mainalready refuses the program-header case, nothing calls the section-header reader, and the described Windows bound does not exist.Written with AI assistance.
Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-09T08:54:22Z.
Closing:
ReadSectionHeadershas no caller on main, so the relinker never reaches this guard; the test only gets there by calling the method directly. Please reopen if a code path starts reading the section table.Pull request closed