Skip to content

fix(relinker): preserve Linux guest RELRO - #20573

Merged
oneandonlydean merged 1 commit into
boykopovar:mainfrom
Queuse:codex/fix-linux-guest-relro-20261115
Nov 22, 2026
Merged

oneandonlydean merged 1 commit into
boykopovar:mainfrom
Queuse:codex/fix-linux-guest-relro-20261115

Conversation

@Queuse

@Queuse Queuse commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

What

Preserve a valid guest PT_GNU_RELRO header, including normalized PT_SCE_RELRO, when writing Linux modules. Previously the writer dropped it even while setting DF_BIND_NOW, leaving relocated data writable.

Validate the pages the Linux loader actually protects: both range boundaries round down, partial trailing pages remain outside the protected interval, and already read-only LOAD mappings remain valid. Reject overflow, missing mappings and unsupported multiple nonempty RELRO headers. Keep LOAD offsets and virtual addresses unchanged. The loader applies the protection separately from eager binding (glibc implementation).

Tested

Windows / WinLibs GCC 15.2 / Python 3.12: relinker-only build and seven focused CTests passed: linux_guest_relro, sce_relro, guest_module_directories, linux_gnu_segments, linux_guest_stack, guest_load_start_module_defer, and linux_load_bounds.

Linux x86_64 in WSL / GCC 15.2 / Python 3.14: native build and linux_guest_relro, sce_relro, guest_module_directories passed. Native subprocess checks confirm relocation/read success, SIGSEGV on writes to protected pages, and writes succeeding outside those pages. Fixtures cover standalone/covered SCE and GNU headers, BSS, empty/subpage ranges, partial/unmapped tails, R/RX/RW LOADs, adjacent mappings, deferred initialization, and invalid/multiple ranges. The original executable failed the preserved-header regression; added valid boundary cases also caught and corrected acceptance errors during review.

python tools/check_conventions.py --base origin/main and git diff --check passed. The required branch overlap scan could not complete 17 shared-CMake comparisons: #6149, #7531, #10861, #11628, #11661, #11689, #11698, #12678, #13486, #13716, #15245, #15642, #15896, #17451, #17473, #17557, #17558. Every reported CMake patch was reviewed manually; none registers this target/fixture or implements guest RELRO. #19706 moves LOAD file offsets while retaining the virtual addresses used for RELRO, so it is independent. Composition with those branches was not built. Based on current main; affected sources are unchanged between the tested and rebased base revisions. No title or performance claim.

Checklist

  • Based on current main; no other open PR implements the same functions
  • One topic per PR; follow-ups go in a new PR
  • No comments in code except technical debt and the licence notice of code adapted from upstream (see AGENTS.md)
  • Unimplemented paths throw (NotImplemented_nid_no_patch); silent stubs are listed under Silent stubs in their library's technical debt file
  • No notes, investigation .md files or images added to the repository (attach them to this PR)
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • Fixes: none
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes

@MarcZX10 MarcZX10 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Reviewed — looks good

Nothing blocks this change; the focused checks reported by the author cover the preserved RELRO header and Linux page protection behavior.

What I ran

Reviewed head 5fbcbe58d187e3110e3139ec7c7dfea84afcb019. Read description.md, commits.md, manifest.json, diff.patch, core/relinker/CMakeLists.txt, core/relinker/elfpatcher/src/linux/LinuxGuestModuleWriter.cpp, core/relinker/elfpatcher/src/general/ProgramHeaderLayoutBuilder.cpp, and core/relinker/relinker/src/parsing/ElfReader.cpp. The complete changed-file texts were included in the review folder; no changed files were excluded. No commands or tests were run in this review. The author-reported build and test results are summarized separately below.

Results

Check Result Note
Patch and targeted code review passed RELRO preservation and validation inspected
Author-reported focused tests passed Windows and Linux runs reported in PR description
Tests run during review not run No local build or test execution
Branch composition not run Author reports shared-CMake comparisons were incomplete

Findings

Blocking

None.

Non-blocking

None.

Not verified

I did not independently run the builds, focused CTests, convention check, or diff check. Composition with the listed PR branches was not built. No physical-console behavior was verified.

To get this to a good state

Nothing is required.

@ycbuzi

ycbuzi commented Nov 15, 2026

Copy link
Copy Markdown
Contributor

Tested 5fbcbe58d1 merged into main, with the whole ctest suite on each machine below. Nothing fails that does not also fail on main on the same machine.

  • Linux: Debian 13 in ChromeOS's Linux environment on a Pentium Silver N6000 (4 cores, SSE4.2, no AVX), GCC 14.2.0, relinker-only build (-DANYPS5_RELINKER_ONLY=ON): run 1: 99% tests passed, 5 tests failed out of 928; run 2: 99% tests passed, 5 tests failed out of 928; run 3: 99% tests passed, 5 tests failed out of 928. 5 of the failing tests fail on main too.

AI-assisted: yes (Claude Code); the runs are automated, this summary was written from their reports.

@github-actions

github-actions Bot commented Nov 15, 2026 •

Copy link
Copy Markdown

Some comparisons could not be completed because a pull request conflicts with the base branch. Review these shared files manually before concluding that there is no overlap.

Pull request Shared files not compared
PR 6149 core/relinker/CMakeLists.txt
PR 10861 core/relinker/CMakeLists.txt
PR 11628 core/relinker/CMakeLists.txt
PR 11661 core/relinker/CMakeLists.txt
PR 11689 core/relinker/CMakeLists.txt
PR 11698 core/relinker/CMakeLists.txt
PR 12678 core/relinker/CMakeLists.txt
PR 13486 core/relinker/CMakeLists.txt
PR 13716 core/relinker/CMakeLists.txt
PR 15245 core/relinker/CMakeLists.txt
PR 20917 core/relinker/CMakeLists.txt

@oneandonlydean
oneandonlydean merged commit 574d916 into boykopovar:main Nov 22, 2026
14 checks passed
@JJenkx

JJenkx commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Regression report: since this merged, the Linux relinker rejects a title's bundled module that 314bcdbc2171 relinked and ran, so the title no longer starts on current main.

Error. PPSA05595, Linux, relinking eboot.elf with --module-dir sce_module. main 2cf83089e9e0 stops while converting sce_module/libSceFace.prx:

FAIL: PT_GNU_RELRO is not covered by PT_LOAD pages (offset 0x48000)

Segments. readelf -lW of libSceFace.prx:

LOAD       0x04c000 0x48000 0x48000 0x0011b0 0x0011b0 RW  0x4000
GNU_RELRO  0x04c000 0x48000 0x48000 0x0011b0 0x004000 R   0x1
LOAD       0x050000 0x4c000 0x4c000 0x0000c8 0x0000d8 RW  0x4000

What validateRelroCoverage does. It works in kDefaultLoadAlignment (0x1000) pages:

  • The RELRO range runs from 0x48000 to (0x48000 + 0x4000) & ~0xfff = 0x4c000.
  • The RW PT_LOAD at 0x48000 has only 0x11b0 bytes, so it covers up to 0x4a000.
  • The next PT_LOAD starts at 0x4c000.
  • 0x4a000–0x4c000 is therefore uncovered, and it throws.

On the console these are 16 KiB pages (p_align 0x4000), so the RELRO memsz of 0x4000 is exactly the one page the RW segment occupies.

Why plain acceptance isn't enough. On a 4 KiB host, 0x4a000–0x4c000 really is unmapped in the output, so protecting the full RELRO range would fail at load time. A fix probably needs to clip the emitted RELRO to the pages the segment actually maps, here 0x48000–0x4a000, rather than reject the module.

The title's other bundled PRX files may have the same layout. The same tree with 314bcdbc2171's relinker relinks this module and the title runs.

AI-assisted: yes

@JJenkx

JJenkx commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Follow-up on the impact. I scanned the modules of 83 locally staged PS5 titles. Each has at least one PRX whose PT_GNU_RELRO extends past its read-write segment's 4 KiB pages into that segment's 16 KiB alignment, as in libSceFace.prx above.

The modules are mostly the usual bundled system ones: libSceJobManager.prx, libSceFace.prx, libSceFaceTracker.prx, libSceNpCppWebApi.prx, and some libc.prx.

The scan is approximate: it does not skip p_flags == 0 loads the way validateRelroCoverage does. So current main likely fails to relink most PS5 titles that ran before this merge. I'll open a fix shortly.

AI-assisted: yes

@JJenkx

JJenkx commented Nov 22, 2026

Copy link
Copy Markdown
Contributor

Fix opened as #25786: it trims a RELRO tail that lies in its segment's own alignment padding to the mapped pages, and keeps this PR's other rejections, past-writable-end included.

AI-assisted: yes

@Queuse

Queuse commented Nov 22, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @JJenkx for catching and fixing the 16 KiB p_align RELRO regression on bundled PS5 PRXs. I reviewed #25786 (aae2639499) and trimming the alignment-padding tail to the mapped 4 KiB pages looks right while keeping the uncovered-page, gap, and non-writable rejections intact.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants