fix(relinker): reject an executable PT_LOAD alignment that is not a power of two #10518
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10518"
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 lulebo on 2026-10-24T07:35:52Z.
What
ElfReader::ReadProgramHeadersaccepts anyp_alignon an executable'sPT_LOAD, and on the uncoveredPT_SCE_RELROit turns into one. The gABI saysp_alignshould be 0, 1 or a positive integral power of 2. Linux and glibc skip any other value when they align the image, FreeBSD refuses it with ENOEXEC since 13.1, and FreeBSD 11.4's loader does not readp_align. This reader already refuses such a value forPT_TLS(#7098).On
main, the Linux conversion at the default base refuses such a value only because the base is not a multiple of it:At a base that is, such as
--linux-fixed-base 0x30000, with--lazy-binding, and to Windows or macOS, the executable converts. The lastPT_LOADof the input below hasp_align3 and starts on a page that the first segment maps with other bytes, so the Linux patcher copies it to the end of the output withmin(p_align, 0x1000)as itsp_align(#11487), and the Linux output declaresp_align3 as well.Both segment kinds are now refused when read, next to the file size and range checks #5741 added for the same pair:
GuestImageReadertakes its headers from the same function (GuestImageReader.cpp:36), so the power-of-two half of itsInvalid load segment alignmentcheck can no longer fail and is dropped. A guest module with such an alignment now fails with the message above, with the module as the input; the offset congruence half of that check stays. #13818'sguest_load_rangestest gives an emptyPT_LOADan alignment of 3, so that case now expects the message above; its congruence case still reaches theGuestImageReadercheck.The input
p_alignPT_LOAD[0x0, 0x1100)PT_DYNAMIC[0x400, 0x480)PT_LOAD[0x1200, 0x1220)PT_LOAD[0x1800, 0x1802)SHA-256
bcde1d6fd2e2b89cf54a51805e0779a27a2a790fd842d947da6469b2f69baf94. Onmain,relinker --skip-sce-module input.elf output.elffails with the base message above, andrelinker --lazy-binding --skip-sce-module input.elf output.elfwrites a 10,672-byte output whosereadelf -lWlists the moved segment as:With this change, both fail at once:
Tested
WSL 2 Ubuntu 26.04 x86-64, GCC 15.2.0, CMake 4.2.3, Ninja 1.13.2, Python 3.14.4. Relinker-only builds of
main(9ab6a281ea) and of this branch:elf_reader_bounds: the new rejection case fails onmain(Invalid program header was accepted) and passes with this change. 3, 0x7fffffff and 0xfffffffffffffffb are refused and 0, 1, 2, 8 and 0x4000 kept, forPT_LOADand for an uncoveredPT_SCE_RELRO, which also gets thePT_GNU_RELROthat #10662 adds.guest_load_ranges: with this change and the test as onmain,empty-first-alignmentandempty-last-alignmentfail (Expected Invalid load segment alignment, got PT_LOAD alignment is not a power of two); with the new expectation, it passes.--skip-sce-module: onmainthe base message at the default base, a 14,768-byte output at--linux-fixed-base 0x30000and a 10,672-byte one with--lazy-binding, both declaringp_align3 for the moved segment, and a 247,296-byte--windowsand a 77,888-byte--macosoutput. With this change, all five fail at once with the message above. With its last segment asPT_SCE_RELRO,mainbehaves the same at the default base and with--lazy-binding, and this change fails withPT_SCE_RELRO alignment is not a power of two (offset 0x800). Withp_align0xfffffffffffffffb, the value a fuzzer first found,mainfails with the base message and converts with--lazy-binding, declaring 0x1000 for the moved segment; this change refuses both.relinker-asanjob, and the same with-fsanitize=undefined -fno-sanitize-recover=undefined: 686 of 686 pass, with no ASan or UBSan report.tools/check_conventions.py --base origin/mainpasses.tools/pr_overlap.py --branchfinds no shared lines with the open pull requests it can compare. It cannot compare eight that conflict withmain, so each was trial-merged with this branch at its current head. #11628, #11661, #11689 and #11698 are built on an earlier head of this PR and list it in Depends on; they also conflict with it inElfReaderBoundsTests.cpp, where this head updates its accepting case for #10662. #15345 moves thePT_SCE_RELROsize and range checks that this PR's check follows, and adds itsElfReaderBoundsTests.cppcases where this PR adds its own, so whichever of the two merges second keeps both. #13486, #14001 and #15032 conflict with this branch only where they conflict withmain.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
Imported from boykopovar/AnyPS5. Originally posted by roboterspinne on 2026-10-24T10:49:45Z.
Review: COMMENTED
Reviewed head ccdbdc068 merged onto
upstream/mainaeeaefad9(2026-10-24); the merge is clean, and so is a merge onto the currentmain60465afd3, which does not touch the two files.Environment: Windows 11 Home 10.0.26200, WinLibs GCC 15.2.0 UCRT, CMake 4.3.1, Ninja 1.13.2, Python 3.13.9,
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, Release.Results
mainaeeaefad9(idle machine): 238 tests, 0 failed,long_pathsskipped. PR merged:elf_reader_boundspasses (0.2 s). I could not get a clean full-suite run today: both full runs happened under heavy unrelated CPU load on my machine and their failures were timeouts in unrelated tests (shader_cache_runtime_sources,nid_database_cache,select_tests,configure_seed*, two Python tests), every one of which passes when rerun alone; none touches this PR's files. CI's relinker matrix is green on all three OSes.ElfReader.cpphunk reverted and the PR's test kept,elf_reader_boundsfails withload and uncovered SCE RELRO alignment is not a power of two: Invalid program header was acceptedwhile the "zero, one or a power of two" case and the existing TLS case pass; restored, it passes.test_linux_load_alignment's fixture (onePT_LOADat vaddr 0, offset 0x4000):p_align3, 0x7fffffff and 0xfffffffffffffffb each exit 2 within about 50 ms withFAIL: PT_LOAD alignment is not a power of two (offset 0x4000); 0, 1, 2, 0x1000, 0x4000 and 0x10000 convert. An uncoveredPT_SCE_RELRO(vaddr 0x9000, filesz 0, memsz 0x10) withp_align3 exits 2 withFAIL: PT_SCE_RELRO alignment is not a power of two (offset 0x5000)and converts with 0x1000; a coveredPT_SCE_RELROwithp_align3 converts, which is right because its alignment is never consumed; aPT_LOADwith memsz 0 andp_align3 is refused, consistent withGuestImageReader.mainon a bad-alignment input. The mechanism is visible inLinuxElfPatcher.cpp:139-141(SeparateSharedLoadPages): withalignment = max(p_align, 0x1000)the loopwhile ((buf.size() + prefix) % alignment != load.MappedAddress % alignment) buf.push_back(0);cannot reach the target for a value near 2^64 and grows until allocation fails. The reader check stops such inputs before the patcher.check_conventions.py --base upstream/mainpasses;pr_overlap.pyreports no overlap.Should fix
p_alignis still unbounded:std::has_single_bitadmits 2^31 to 2^63, and nothing between the reader andSeparateSharedLoadPages(run atLinuxElfPatcher.cpp:556, beforeWriteLayoutat :573) bounds it, so a segment on a shared page with differing bytes still pads the Linux output byte by byte top_alignand ends as a 2 GiB file or as the genericFAIL: std::bad_allocfrommain.cpp:479-482(exit 2), as the body says. CONTRIBUTING asks that known gaps be recorded in technical debt anddocs/dev/debt/relinker.mdhas no entry for this. Please either add one line there (a power-of-twop_alignabove the page size pads the Linux output up top_align - 1bytes and fails only atstd::bad_alloc) or boundp_alignin the two new checks. For reference, FreeBSD refuses aPT_LOADp_alignthat is not a power of two or exceedsmaxsalign(PAGE_SIZE * 1024, raised to the largest superpage size: 1 GiB on amd64 with 1 GiB pages, otherwise 4 MiB) withInvalid segment alignment/ ENOEXEC, which would also refuse the body's 0x80000000 example; but that check dates from December 2021 (714d6d09b57e, 36df8f540fa3, first in 13.1) and is absent from stable/11 and stable/12, so it says nothing about the PS5's own loader.Nits
p_aligntext says "Values 0 and 1 mean no alignment is required. Otherwise, p_align should be a positive, integral power of 2"; its only "must" is the page-size congruence ofp_vaddrandp_offset. Linux (fs/binfmt_elf.c,maximum_alignment: "skip non-power of two alignments as invalid") and glibc (elf/dl-load.c,powerof2filter onp_align_max) ignore such values; FreeBSD rejects them. Suggested: "The gABI says p_align should be 0, 1 or a power of two; FreeBSD's loader refuses anything else with ENOEXEC, while Linux and glibc ignore such a value."GuestImageReadergets its headers fromElfReader(bytes).ReadProgramHeaders()(GuestImageReader.cpp:34), so a guest.prxwith a non-power-of-twoPT_LOADp_alignis now refused by the new check withPT_LOAD alignment is not a power of two (offset 0x...)before line 46 can sayInvalid load segment alignment(checked with the PR build on atest_windows_import_modules.providermodule patched top_align3; the offset-incongruence half of line 46 still fires forp_align0x2000). The!std::has_single_bithalf of line 46 is therefore unreachable, and the body's "already refuses other values" describesmain, not this branch; no test references the guest message, which is why CI is unaffected. Either note the changed module diagnostic in the body or drop the dead half of line 46 here so one check owns the rule.RejectProgramHeadersaccepts anyRelinkerExceptionwithout looking at the message (the file's convention, so fine); the kept list could include 2, and a coveredPT_SCE_RELROwith a bad alignment staying accepted is deliberate but unpinned. Optional.FAILlogs are not attached, although the rules put notes and logs in the PR; the description omits the first segment'sp_offsetand the second segment'sp_align, so the 3,221-byte input cannot be rebuilt exactly. The commit message says 48 s, the body 46 s.Notes
p_offset ≡ p_vaddrmodulo the page (modules get that check inGuestImageReader.cpp:46). APT_LOADwith vaddr 0x800 and offset 0x4000 converts on the PR build to an output whose header hasoff=0x4000 vaddr=0x400800; as far as I can tell fromelf_mapinfs/binfmt_elf.cthe kernel cannot map that, but I have not run it on Linux. Follow-up material, not a defect here.ElfReader.cppinReadDynamicTags, far from these hunks, so either order merges. For disclosure, I have open PRs in this area: #10564 rejects overlappingPT_LOADranges inSeparateSharedLoadPages, before the padding loop; it would stop the body's fuzzer input at that check (its second segment overlaps the first) but not a disjoint layout (first segment ending mid-page, a later one starting on that page), so the two changes do not replace each other; and #10402 (Linux entry and relocation bounds). Neither touches this PR's files.Not checked:
mainon any bad-alignment input (deliberately); the 46 s, 12.9 GiB and 2 GiB figures; ASan/UBSan builds; Linux or macOS hosts and execution of Linux outputs; the exact fuzzer input; a clean full-suite run on an idle machine.I found no blocking issue: the two checks are placed and scoped correctly, the test is red without them and green with them, and the inputs behave as the body describes. The one thing I would like settled before merge is the residual power-of-two case, recorded or bounded.
Written with AI assistance (Claude Code). I ran the builds, tests and CLI checks above on my Windows 11 machine.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T12:05:32Z.
Review: CHANGES_REQUESTED
The non-power-of-two checks address the malformed alignment that caused the allocation blow-up, and I agree the existing test covers both PT_LOAD and uncovered PT_SCE_RELRO. One dangerous case remains known in the PR description: a valid power-of-two alignment such as 0x80000000 can still make SeparateSharedLoadPages pad toward a multi-gigabyte output, and sufficiently large values fail later as std::bad_alloc. CONTRIBUTING requires known gaps to be recorded in technical debt. Please either document this remaining behavior in docs/dev/debt/relinker.md, or add an evidence-based bound at the parser/patcher boundary with a regression test. I have not independently reproduced the reported memory growth; this request is based on the code path and measurements documented in the existing review.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T21:54:40Z.
Review: APPROVED
Re-reviewed exact head
e22e048dffca16949411221e6bdccf6cc8bb3874.The current update resolves my prior request: the residual power-of-two padding behavior is now documented in
docs/dev/debt/relinker.md, and the description explains the current-base behavior. The non-power-of-two checks cover both executablePT_LOADand uncoveredPT_SCE_RELRO; the test covers rejected non-powers and accepted zero, one, and power-of-two alignments. The earlier concern about the module reader's now-unreachable power-of-two check is also addressed in the current diff.I built
elf_reader_bounds_testsfrom this exact head using GCC 12.2 / CMake Release and ranctest --test-dir build-review -R '^elf_reader_bounds$' --output-on-failure: 1/1 passed.check_conventions.py --base upstream/main --head upstream/pr-10518andgit diff --checkpassed. Hosted relinker jobs on Ubuntu, Windows, macOS arm64, macOS x86_64, ASan, eligibility, conventions, and labels pass; product jobs are skipped for this relinker-only change.The documented large-output behavior for very large power-of-two alignments remains a known limitation rather than an unrecorded gap. No blocking findings on this head.
Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-17T02:31:21Z.
Review: COMMENTED
Reviewed relinker PT_LOAD alignment check — no blocking findings
Nothing blocks this; the points below are optional.
What I ran
9dcfb99b73b95f8524c03d53789336d259f0e5f9, basemain. Diff read from the console'sdiff.patch; no merge preview.repo_read.py searchandcatat the head, plus the changed-file copies in the run folder.Results
p_alignoffset andPT_OS_RELROvalueGuestImageReaderreads headers fromReadProgramHeadersPT_LOADFindings
Blocking
None.
Non-blocking
core/relinker/relinker/src/parsing/ElfReader.cpp:181— the newPT_SCE_RELROcheck runs only in the uncovered branch. A coveredPT_SCE_RELROwith a non-power-of-twop_alignis not rejected, and no test covers that case. This matches the description's wording ("uncovered"), so it is a test gap rather than a defect.core/relinker/relinker/src/guest/GuestImageReader.cpp:52— the description citesGuestImageReader.cpp:36for theElfReadercall. At the head that call is on line 37; line 36 setsOutputName. The claim itself holds.Sources
ElfReader.cppat the head, lines 157–158 and 181–182GuestImageReader.cppat the head, lines 37 and 52Not verified
9dcfb99b73b9. Approval needs it confirmed, so this review is a comment.elf_reader_boundsandguest_load_ranges. The results in the description are the author's, not mine.tools/check_conventions.pyandtools/pr_overlap.py, and the open pull requests the description lists.min(p_align, 0x1000), which I did not trace.ElfReaderBoundsTests.cppandGuestLoadRangesTests.cpp. I read the changed hunks, theFixtureandRejecthelpers, and the surrounding code. I did not read the whole files, and I did not readRejectProgramHeadersto see whether it checks the message.GuestImageReader.cpp,ElfReader.cpp,ElfReaderBoundsTests.cpp,GuestLoadRangesTests.cpp. Excluded by rule: none.To get this to a good state
Nothing is required.
Optional:
PT_SCE_RELROcase with alignment 3 toElfReaderBoundsTests.cpp, or note in the description why it is out of scope.GuestImageReader.cpp:36reference in the description to line 37.Imported from boykopovar/AnyPS5. Originally posted by catminou4 on 2026-11-25T05:48:50Z.
Review: COMMENTED
Reviewed merged into current
main(61d110125e, clean merge) on Ubuntu 22.04, GCC 11.4, relinker-only build (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON).ctest -R '^(elf_reader_bounds|guest_load_ranges)$': both pass.GuestImageReader.cppandElfReader.cppfrommain, both fail:elf_reader_boundsreportsload and uncovered SCE RELRO alignment is not a power of two: Invalid program header was accepted, andguest_load_rangesgetsInvalid load segment alignmentinstead of the new message (empty-first-alignment,empty-last-alignment). Restoring the PR sources passes again.ctest -j8: everything passes excepttools_headers_manifest(test_hash_rejects_changes_that_preserve_file_size_and_mtime), which is a Python tools test unrelated to these files.ElfReader::ReadProgramHeadersmakes every reader reject it, not onlyGuestImageReader; 0 and 1 still pass as the ELF spec allows.Mergeable.
Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T11:58:22Z.
Review: COMMENTED
Built this head with
ANYPS5_RELINKER_ONLY=ONand ranguest_load_rangesandelf_reader_boundson Linux. Both passed, including the load-alignment regression.AI-assisted: yes.