fix(relinker): make PT_SCE_RELRO read-only in Windows output as FreeBSD rtld does #18523

Open
Ragbie wants to merge 4 commits from upstream-pr/18523 into main
Ragbie commented 2026-11-11 20:03:31 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Ragbie on 2026-11-11T20:03:31Z.


What

Windows output left every PT_SCE_RELRO range writable after startup (debt, "the Windows and macOS entry stubs have no equivalent of it at all"). The entry stub now makes each range read-only with VirtualProtect(..., PAGE_READONLY, ...), in the order and rounding of FreeBSD 11.4's rtld (libexec/rtld-elf/rtld.c):

  • Order. A guest module's ranges are protected after the stub has written every import, before any guest module initializer runs, as rtld does for objects other than the main program at the end of relocate_object (:2917). The executable's ranges are protected after the guest initializers and before control enters the ELF entry, as rtld does for the main program after objlist_call_init (:786, :798).
  • Ranges. WindowsLoadImage::GetRelroRanges takes the last PT_GNU_RELRO header that ElfReader produces from PT_SCE_RELRO. rtld keeps one range per object and overwrites it for each header (:1459-1460), so with several headers only the last is protected; Linux output throws in that case instead. The range is rounded as rtld rounds it: the start down to a page, the size up to whole pages. Pages are 4 KiB here and 16 KiB on the console. The range is left writable unless every page it covers is writable in the image, as hasWritableLoadCoverage does for Linux output.
  • Failure. If VirtualProtect fails, the stub prints FAIL: cannot make a RELRO range read-only with GetLastError and raises, as the other startup failures do. rtld fails the load in the same case (:5338).
  • Nothing writes later. On Windows the stub writes all imports at startup, including --trace-imports thunks. Unresolved lazy imports get their stubs at relink time, and the TLS resolver only reads. Deferred guest initialization writes only the .ginit table, which is in its own section and not part of a RELRO range. So --lazy-binding keeps protection on Windows, unlike Linux output.
  • Debt. The relinker.md entry now lists only macOS and Linux --lazy-binding output, and records that 4 KiB rounding can differ from the console's 16 KiB pages in both directions. The multiple-range entry notes that Windows protects only the last range. The overlapping-PT_LOAD entry no longer says Windows has no RELRO step.

Overlaps: #18373 (macOS) changes the same relinker.md entry; whichever lands second drops its target from the list. #15245 and #11628, #11661, #11689 and #11698 add lines next to the ones this PR adds in WindowsEntryStubBuilder.hpp (Build signature), GuestRuntime.hpp and WindowsLoadImage.hpp. #3965, #7594 and #7704 change other hunks of WindowsEntryStubBuilder.cpp.

Tested

Windows 11 Pro 26200, WinLibs GCC 15.2.0, relinker-only Release build.

  • sce_relro gains Windows run cases. The executable's entry stores one byte, then jumps through a relocated slot inside the range to a function that returns 42. A guest module's DT_INIT stores one byte into its own range.
    • With the test on main, all five cases exit 42: covered and standalone executable ranges, the guest range, and the two writable controls.
    • With the change, a store into a covered or standalone executable range and into a guest range ends with 0xc0000005. The same store into a plain read-write page, in the executable and in the guest, still exits 42. The original cases still exit 42, so the relocated slot is read after protection.
    • With two ranges, a store into the earlier one exits 42 and a store into the last one ends with 0xc0000005.
  • ctest --test-dir build-relinker: 867 of 867 passed.
  • Not tested on a title.

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
  • 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 (Claude Code)
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5266029987 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18523). Originally posted by [Ragbie](https://github.com/Ragbie) on 2026-11-11T20:03:31Z. --- ### What Windows output left every `PT_SCE_RELRO` range writable after startup ([debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/debt/relinker.md), "the Windows and macOS entry stubs have no equivalent of it at all"). The entry stub now makes each range read-only with `VirtualProtect(..., PAGE_READONLY, ...)`, in the order and rounding of FreeBSD 11.4's rtld (`libexec/rtld-elf/rtld.c`): - **Order.** A guest module's ranges are protected after the stub has written every import, before any guest module initializer runs, as rtld does for objects other than the main program at the end of `relocate_object` (`:2917`). The executable's ranges are protected after the guest initializers and before control enters the ELF entry, as rtld does for the main program after `objlist_call_init` (`:786`, `:798`). - **Ranges.** `WindowsLoadImage::GetRelroRanges` takes the last `PT_GNU_RELRO` header that `ElfReader` produces from `PT_SCE_RELRO`. rtld keeps one range per object and overwrites it for each header (`:1459-1460`), so with several headers only the last is protected; Linux output throws in that case instead. The range is rounded as rtld rounds it: the start down to a page, the size up to whole pages. Pages are 4 KiB here and 16 KiB on the console. The range is left writable unless every page it covers is writable in the image, as `hasWritableLoadCoverage` does for Linux output. - **Failure.** If `VirtualProtect` fails, the stub prints `FAIL: cannot make a RELRO range read-only` with `GetLastError` and raises, as the other startup failures do. rtld fails the load in the same case (`:5338`). - **Nothing writes later.** On Windows the stub writes all imports at startup, including `--trace-imports` thunks. Unresolved lazy imports get their stubs at relink time, and the TLS resolver only reads. Deferred guest initialization writes only the `.ginit` table, which is in its own section and not part of a RELRO range. So `--lazy-binding` keeps protection on Windows, unlike Linux output. - **Debt.** The `relinker.md` entry now lists only macOS and Linux `--lazy-binding` output, and records that 4 KiB rounding can differ from the console's 16 KiB pages in both directions. The multiple-range entry notes that Windows protects only the last range. The overlapping-`PT_LOAD` entry no longer says Windows has no RELRO step. **Overlaps:** #18373 (macOS) changes the same `relinker.md` entry; whichever lands second drops its target from the list. #15245 and #11628, #11661, #11689 and #11698 add lines next to the ones this PR adds in `WindowsEntryStubBuilder.hpp` (`Build` signature), `GuestRuntime.hpp` and `WindowsLoadImage.hpp`. #3965, #7594 and #7704 change other hunks of `WindowsEntryStubBuilder.cpp`. ### Tested Windows 11 Pro 26200, WinLibs GCC 15.2.0, relinker-only Release build. - `sce_relro` gains Windows run cases. The executable's entry stores one byte, then jumps through a relocated slot inside the range to a function that returns 42. A guest module's `DT_INIT` stores one byte into its own range. - With the test on `main`, all five cases exit 42: covered and standalone executable ranges, the guest range, and the two writable controls. - With the change, a store into a covered or standalone executable range and into a guest range ends with `0xc0000005`. The same store into a plain read-write page, in the executable and in the guest, still exits 42. The original cases still exit 42, so the relocated slot is read after protection. - With two ranges, a store into the earlier one exits 42 and a store into the last one ends with `0xc0000005`. - `ctest --test-dir build-relinker`: 867 of 867 passed. - Not tested on a title. ### Checklist - [x] Based on current `main`; no other open PR implements the same functions - [x] One topic per PR; follow-ups go in a new PR - [x] No comments in code except [technical debt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/CONVENTIONS.md) - [x] Unimplemented paths throw (`NotImplemented_nid_no_patch`); silent stubs are listed under Silent stubs in their library's [technical debt](https://github.com/boykopovar/AnyPS5/tree/main/docs/dev/debt) file - [x] No notes, investigation `.md` files or images added to the repository (attach them to this PR) - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] Fixes: none - [x] Unblocks: none - [x] GPUs tested: n/a - [x] AI-assisted: yes (Claude Code)
MarcZX10 commented 2026-11-21 04:35:50 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-11-21T04:35:50Z.

Review: CHANGES_REQUESTED


Reviewed — changes requested

The RELRO range calculation can leave part of an unaligned range writable because it rounds the size without accounting for the starting address’s page offset.

What I ran

Reviewed head 92233aa31a0c8eced17669da4931e6aadffe6085. I read the prepared description, commits, manifest, diff and file listing, then used repo_read.py to inspect changed code and relevant context. I ran no build or tests.

Results

Check Result Note
Diff and source review passed Found an unaligned-range defect
Build and tests not run No local execution performed

Negative control

Could not run a negative control; no test execution was performed.

Findings

Blocking

  • Unaligned RELRO ranges can remain partly writable. GetRelroRanges floors the start to a page at line 163, but computes the number of protected pages from MemorySize alone at line 164. For a range that starts partway into a page, the rounded-up size must include that starting offset; if it crosses a page boundary, the current calculation can omit the last page. The emitted protection range can therefore fail to cover the whole RELRO range. Calculate the rounded end from the range end (or include the start’s page offset in the size). Changed line: core/relinker/elfpatcher/src/windows/WindowsLoadImage.cpp:164. Source at reviewed commit

Sources

Not verified

No build or tests were run, and console behavior was not verified. Read the prepared review documents and diff, plus targeted source ranges in all 10 changed files; the complete repository file texts were not all read. The file listing showed all 10 changed files included. files.json was not available through the reader.

To get this to a good state

Fix page-count rounding to cover the complete range for unaligned starts, add a test for an unaligned range that crosses a page boundary, then run the relevant tests on the new head.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6002862492 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18523#pullrequestreview-6002862492). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-11-21T04:35:50Z. **Review:** CHANGES_REQUESTED --- # Reviewed — changes requested The RELRO range calculation can leave part of an unaligned range writable because it rounds the size without accounting for the starting address’s page offset. ## What I ran Reviewed head `92233aa31a0c8eced17669da4931e6aadffe6085`. I read the prepared description, commits, manifest, diff and file listing, then used `repo_read.py` to inspect changed code and relevant context. I ran no build or tests. ## Results | Check | Result | Note | |---|---|---| | Diff and source review | passed | Found an unaligned-range defect | | Build and tests | not run | No local execution performed | ## Negative control Could not run a negative control; no test execution was performed. ## Findings ### Blocking - **Unaligned RELRO ranges can remain partly writable.** `GetRelroRanges` floors the start to a page at line 163, but computes the number of protected pages from `MemorySize` alone at line 164. For a range that starts partway into a page, the rounded-up size must include that starting offset; if it crosses a page boundary, the current calculation can omit the last page. The emitted protection range can therefore fail to cover the whole RELRO range. Calculate the rounded end from the range end (or include the start’s page offset in the size). Changed line: `core/relinker/elfpatcher/src/windows/WindowsLoadImage.cpp:164`. [Source at reviewed commit](https://github.com/boykopovar/AnyPS5/blob/92233aa31a0c8eced17669da4931e6aadffe6085/core/relinker/elfpatcher/src/windows/WindowsLoadImage.cpp#L159-L170) ## Sources - [Windows RELRO range calculation at reviewed commit](https://github.com/boykopovar/AnyPS5/blob/92233aa31a0c8eced17669da4931e6aadffe6085/core/relinker/elfpatcher/src/windows/WindowsLoadImage.cpp#L159-L170) - [Windows protection call at reviewed commit](https://github.com/boykopovar/AnyPS5/blob/92233aa31a0c8eced17669da4931e6aadffe6085/core/relinker/elfpatcher/src/windows/WindowsEntryStubBuilder.cpp#L806-L821) ## Not verified No build or tests were run, and console behavior was not verified. Read the prepared review documents and diff, plus targeted source ranges in all 10 changed files; the complete repository file texts were not all read. The file listing showed all 10 changed files included. `files.json` was not available through the reader. ## To get this to a good state Fix page-count rounding to cover the complete range for unaligned starts, add a test for an unaligned range that crosses a page boundary, then run the relevant tests on the new head.
ligmaSec commented 2026-12-03 11:58:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by ligmaSec on 2026-12-03T11:58:02Z.

Review: COMMENTED


Built this head with ANYPS5_RELINKER_ONLY=ON and ran sce_relro on Linux. It passed the Windows output checks for the protected segment. Native Windows execution remains outside this run.

AI-assisted: yes.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:6169302745 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/18523#pullrequestreview-6169302745). Originally posted by [ligmaSec](https://github.com/ligmaSec) on 2026-12-03T11:58:02Z. **Review:** COMMENTED --- Built this head with `ANYPS5_RELINKER_ONLY=ON` and ran `sce_relro` on Linux. It passed the Windows output checks for the protected segment. Native Windows execution remains outside this run. AI-assisted: yes.
This pull request cannot be merged automatically due to conflicts.
Merge manually to resolve the conflicts.
View command line instructions

Checkout

From your project repository, check out a new branch and test the changes.
git fetch -u origin 'upstream-pr/18523:upstream-pr/18523'
git switch 'upstream-pr/18523'

Merge

Merge the changes and update on Forgejo.

Warning: The "Autodetect manual merge" setting is not enabled for this repository, you will have to mark this pull request as manually merged afterwards.

git switch 'main'
git merge --no-ff 'upstream-pr/18523'
git switch 'upstream-pr/18523'
git rebase 'main'
git switch 'main'
git merge --ff-only 'upstream-pr/18523'
git switch 'upstream-pr/18523'
git rebase 'main'
git switch 'main'
git merge --no-ff 'upstream-pr/18523'
git switch 'main'
git merge --squash 'upstream-pr/18523'
git switch 'main'
git merge --ff-only 'upstream-pr/18523'
git switch 'main'
git merge 'upstream-pr/18523'
git push origin 'main'
Sign in to join this conversation.
No reviewers
No labels
No milestone
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Reference
boykopovar/AnyPS5!18523
No description provided.