fix(relinker): reject executable exports outside PT_LOAD and PT_TLS memory #10646
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/10646"
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 roboterspinne on 2026-10-24T14:34:56Z.
What
RelinkerPipeline::Relinkcopies every export of the input executable (a defined symbol that is not local, hidden or internal, withDT_SCE_SYMTABSZ) into the output.dynsymas it is, without looking atst_value,st_size,st_shndxor the type.GuestImageReader.cpphas required of every symbol of every shipped.prxthat[st_value, st_value + max(st_size, 1))lies inside aPT_LOADsegment, executable forSTT_FUNC, that a non-TLS symbol's section index is belowSHN_LORESERVEunless it isSHN_ABS, and that anSTT_TLSsymbol lies insidePT_TLS;MacOsMachOPatcher.cppapplied the bounds and the section rule to the executable's exports on macOS. Of the thirteen rejected shapes in the new test,mainaccepts twelve on the default Linux output (it rejects a section index of 0xff01 at the fixed base), all thirteen with--lazy-binding, ten on Windows (its code analysis rejects anSTT_FUNCexport with a nonzero size outside the code) and one on macOS (a size-0STT_FUNCexport in the writable segment). In the Linux output such an export is adlsymor GOT target outside the image.STT_TLSexport needs a regular section index, aPT_TLSheader andst_value + st_size <= p_memsz; a non-TLS export inSHN_ABSis kept without a range check, as the Linux patcher leaves it unrebased and macOS exports it as absolute; every other export needs a section index belowSHN_LORESERVEand[st_value, st_value + max(st_size, 1))inside onePT_LOAD, withPF_XforSTT_FUNC. The check runs in the pipeline, so an export it rejects fails with the same line on all four outputs, for exampleExecutable export Foo at 0x9000 (6 bytes) does not fit inside an executable PT_LOAD segmentorExecutable TLS export exceeds PT_TLS: Foo, with the file offset of the symbol entry. Local, hidden, internal and undefined symbols are not exports and are untouched, and so is an input withoutDT_SCE_SYMTABSZ, which exports nothing through this loop.PT_LOADthat no otherPT_LOADcontinues is rejected, as it is for guest modules and on macOS; #4985, which would have accepted such symbols, was closed because no title is known to reach it. A size-0STT_FUNCexport in a non-executable segment is rejected on every output; every output accepted it before. A size-0STT_TLSexport exactly at the end ofPT_TLSpasses, as for guest modules, and the macOS writer still rejects it later, as before. APT_SCE_RELROrange counts as loaded, becauseElfReaderturns an uncoveredPT_SCE_RELROinto aPT_LOAD. The guest-module rule has applied to every symbol of every bundled module since1379ec001b; the one title it caught exportedSHN_ABSsymbols (PPSA12544, #698), which stay exempt here.test_executable_export_bounds.py: twelve accepted shapes (a function in text and at the last text byte, an object inside and one filling its segment to the end, a size-0 object inside, a weak object, anSHN_ABSobject, a TLS export insidePT_TLSand one filling it, and a hidden, an internal and a local symbol outside the image) and thirteen rejected ones (outside every load, past or across a segment end, size-0 at a segment end, functions in the writable segment with and without a size, a protected symbol outside, a 0xff01 section, TLS beyond, without or in a special section ofPT_TLS), each relinked for the default and--lazy-bindingLinux, Windows and macOS outputs; a rejected case asserts the identicalFAILline, offset included, on all four.test_macos_executable_exports.pyexpects the pipeline's line for itsunmapped,tls-missingandreservedcases, which the macOS checks no longer reach. Those checks (MacOsMachOPatcher.cpplines 89 to 96) and the fixed-base section check inLinuxElfPatcher.cppare now unreachable for executable exports; they stay for a follow-up PR, because #9408 edits the lines right after the macOS ones.pr_overlap.pycannot compare this branch with #5021, #7173 and #9408 (core/relinker/CMakeLists.txt) or #8076 (RelinkerPipeline.cpp), because those do not merge withmain, and reports no other shared lines. Checked by hand: theirCMakeLists.txthunks start at lines 52 to 390, 505 and 542, this PR adds two lines after 567; #8076 changesRelinkerPipeline.cppfrom line 374 (unwind ranges), not the export loop. #10402 adds the same containment predicate and wording toLinuxElfPatcher.cppfore_entry, relocation offsets andDT_PLTGOT; the two share no lines.Tested
Windows 11, GCC 15.2.0 (WinLibs UCRT), relinker-only Release build (
-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON); synthetic fixtures only, no title or homebrew, relinked outputs not run.ctest --test-dir build-relinker -j 6: 242 tests, 240 passed,long_pathsskipped;select_testsfails on this machine onmainas well (FileExistsErrorintools/tests/test_select_tests.py, a Windows symlink problem unrelated to this change).executable_export_boundsagainst amainbuild (6cd36e6517) with this test file: all 48 accepted runs pass; of the 52 rejected runs, 36 exit 0 with an output file and 16 exit 2 with a target-specific line (Linux: the 0xff01 section; Windows: the three nonzero-sizeSTT_FUNCexports, in code analysis; macOS: all but the size-0 function in data). With this change all 100 runs behave as asserted, in 2.7 s.macos_executable_exportsfails against this change with its old expectations (not contained in PT_LOADis no longer printed) and passes with the three updated ones.python tools/check_conventions.py --base upstream/mainwith this title and description passed.executable_tls_relocationswith about 170 relinker runs takes 0.5 s (#10607's job), so the 100 runs here stay far below 30 s.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)Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-24T14:45:08Z.
Review: APPROVED
Reviewed exact head
f1a7f913c726b782dcdaf11fb496fd3915b80eb3. The pipeline now applies the guest-module export bounds at the common export-copy point, preserving theSHN_ABSexemption and validating TLS exports againstPT_TLS; the fixture covers accepted and rejected symbol shapes across Linux default/lazy binding, Windows, and macOS outputs.git diff --checkand the local conventions check pass. I built the relinker-only Release configuration locally and ran the full CTest suite: 242/245 passed. The directly affectedexecutable_export_boundsandmacos_executable_exportstests both pass. The other local failures werepr_overlap_encoding(its git-root test harness failed in this isolated worktree) and two host instruction tests exiting withIllegal; I have not attributed those to this change. Hosted relinker tests pass on Ubuntu, Windows, macOS ARM64 and macOS x86_64, and relinker ASan passes. Product builds were skipped by eligibility. The additional Windows 11 fixture runs in the description are author-reported, not independently reproduced. No blocking issue found.