fix(relinker): check PT_LOAD and uncovered PT_SCE_RELRO file ranges #5741
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/5741"
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 IshmamDC217 on 2026-10-14T08:52:07Z.
What
ElfReader::ReadProgramHeaderschecked that aPT_LOAD's file size does not exceed its memory size, but not that its file range lies inside the input.Invalid PT_LOAD file range).PT_LOADs and appends its own block at the old end of the file, so the conversion succeeds. A load that reaches past the input then maps the relinker's own data into the guest segment:.dynstr(\0$ORIGIN/libs…),.dynsym,.dynamicand the entry stub.A
PT_SCE_RELROthat noPT_LOADcovers (the PS4 layout) is converted to aPT_LOADafter the per-header loop, so it skipped both checks.readelfreports the resulting segment as invalid.This change rejects a
PT_LOADwhose file range exceeds the input, reusing the existingSegment offset out of boundserror, and runs both checks on an uncoveredPT_SCE_RELRObefore converting it.Tested
Linux x86-64, GCC 13, relinker-only build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON): all 158 tests pass.Three new cases in
ElfReaderBoundsTests.cpp(elf_reader_bounds):PT_LOADreaching past the end of the file;PT_SCE_RELROwith file size larger than memory size;PT_SCE_RELROreaching past the end of the file.Before the change all three fail with
Invalid program header was accepted. After it they pass.End to end, the
test_sce_relro.pyfixture with aPT_LOADat 0x2000 + 0x2000 in a 0x3000-byte file:.dynstr.--windowsand--macosall fail withSegment offset out of bounds (offset 0x2000).python3 tools/check_conventions.py --base origin/mainpasses. Windows and macOS were not built.Checklist
main; no other open PR implements the same functionsNotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file.mdfiles or images added to the repository (attach them to this PR)🤖 Generated with Claude Code
https://claude.ai/code/session_01P9JfvrCsztJtbkd5SATAzL
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-14T13:46:12Z.
Validated at head
3b75f928(basemain2582ec11, no relinker diff vs the PR's parent) on Windows 11, MinGW-w64 g++ 15.2.0, relinker-only Release build.elf_reader_boundswith this PR's test file built against base: all 3 new cases fail withInvalid program header was accepted; on head they pass.sce_relro,elf_offsets,linux_load_alignment).test_sce_relro.pyfixture (0x3000 bytes):PT_LOAD0x2000+0x2000: Linux exits 0.readelfshowsLOAD 0x002000 FileSiz 0x2000in a 0x32fe-byte output, and offset 0x3000 holds\0$ORIGIN/libs.readelf: Error: the segment's file size is larger than its memory size.--windowsand--macos.0xffffffffffffff00+ 0x200 is rejected without wrapping. bssPT_LOAD/uncovered RELRO (filesz < memsz) still convert on all three targets. Executable loads were already caught byReadSegment; this mainly closes the data-segment and RELRO gaps.Nits (non-blocking):
TranslateVirtualAddress'sPT_LOAD file mapping out of boundscheck is now unreachable; the twotranslation … file …cases now pass viaReadProgramHeaders.PT_SCE_RELRO file range exceeds the input filediffers from the… out of boundswording used nearby (Dynamic segment out of bounds,Section offset out of bounds).PT_LOADwith offset past EOF is now rejected on Linux too (Windows/macOS already did).Not tested: native Linux/macOS builds, real PS4/PS5 titles, running the outputs.
AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by IshmamDC217 on 2026-10-14T14:19:51Z.
@wachi35676 Thanks for validating on Windows. Pushed 2ab7a63a: removed the now-unreachable range check in TranslateVirtualAddress (the two translation cases now fail in ReadProgramHeaders), switched the uncovered RELRO range error to Segment offset out of bounds to match PT_LOAD, and added an exact-fit case (offset + filesz == file size) for both PT_LOAD and uncovered PT_SCE_RELRO. I left the filesz-0 case as is: rejecting an offset past EOF on Linux matches what Windows and macOS already do. Relinker-only ctest 158/158 on Linux.
Imported from boykopovar/AnyPS5. Originally posted by wachi35676 on 2026-10-14T14:52:32Z.
Follow-up: re-validated at
2ab7a63a3(Windows 11, MinGW g++, relinker-only Release). Thanks for the quick turnaround.main(f0854887d). It merges cleanly.TranslateVirtualAddressis safe. It callsReadProgramHeaders()itself, so every PT_LOAD it can match has already passed one of the two new range checks. The two translation tests still pass and now act as end-to-end guards through that path. No test expected the oldPT_LOAD file mapping out of boundstext.Segment offset out of bounds, matching PT_LOAD and the existingtest_elf_offsets.pyexpectation. Resolved.FileSize + 1) makes it fail. With the CLI on all three targets, a segment ending exactly at EOF is accepted (rc=0) and one byte past it is rejected (rc=2). Resolved.--windowsand--macosalready reject it (Invalid PT_LOAD file range (offset 0x5000)), so rejecting it on Linux makes the three targets consistent.Not tested: Linux/macOS hosts, real game binaries.
AI-assisted: yes (Claude Code); results above were run on this machine.
Imported from boykopovar/AnyPS5. Originally posted by IshmamDC217 on 2026-10-15T11:17:33Z.
Rebased onto current main (
8276a1d05); relinker-only ctest 172/172 on Linux and it still merges cleanly.Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-15T12:22:40Z.
Checked on Linux (relinker scope, 134/134 ctest): the three new
elf_reader_boundscases fail without theElfReader.cppchange and pass with it;_rangeFitsis overflow-safe and the exact-fit cases are covered. Dropping the check inTranslateVirtualAddressis safe since it goes throughReadProgramHeaders. Also confirmed by @wachi35676 on Windows/MinGW; merging.