fix(relinker): validate relative relocation table bounds #2222

Merged
EshaanJaiswal merged 2 commits from upstream-pr/2222 into main 2026-10-10 04:45:25 +00:00
EshaanJaiswal commented 2026-10-09 12:07:51 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EshaanJaiswal on 2026-10-09T12:07:51Z.


What

Validate complete DT_RELA and DT_JMPREL relocation table ranges before parsing them.

Malformed ELF metadata could declare a relocation table extending beyond its file-backed PT_LOAD segment. The parser could then interpret bytes outside the segment as relocation entries.

This change:

  • Validates the full relocation table against its containing PT_LOAD.
  • Validates the resulting file range against the ELF byte buffer.
  • Rejects relocation sizes that are not multiples of 24 bytes.
  • Uses overflow-safe checks for virtual and file offsets.
  • Applies the checks to both DT_RELA and DT_JMPREL.
  • Adds regression coverage for overextended, unaligned, and near-UINT64_MAX metadata.

Fixes #2221

Tested

Linux x86-64 (Ubuntu 26 LTS)

Tests use synthetic ELF byte arrays only. No PS5 hardware, controller, firmware, title, or game files are required.

Commands run:

cmake --build build --target strict_nid_filter_tests --parallel
ctest --test-dir build -R strict_nid_filter --output-on-failure

Result:

strict_nid_filter passed

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 in TechnicalDebt
  • No notes, investigation .md files or images added to the repository
  • New third-party code is a submodule built from source
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4800186745 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2222). Originally posted by [EshaanJaiswal](https://github.com/EshaanJaiswal) on 2026-10-09T12:07:51Z. --- ### What Validate complete `DT_RELA` and `DT_JMPREL` relocation table ranges before parsing them. Malformed ELF metadata could declare a relocation table extending beyond its file-backed `PT_LOAD` segment. The parser could then interpret bytes outside the segment as relocation entries. This change: - Validates the full relocation table against its containing `PT_LOAD`. - Validates the resulting file range against the ELF byte buffer. - Rejects relocation sizes that are not multiples of 24 bytes. - Uses overflow-safe checks for virtual and file offsets. - Applies the checks to both `DT_RELA` and `DT_JMPREL`. - Adds regression coverage for overextended, unaligned, and near-`UINT64_MAX` metadata. Fixes #2221 ### Tested Linux x86-64 (Ubuntu 26 LTS) Tests use synthetic ELF byte arrays only. No PS5 hardware, controller, firmware, title, or game files are required. Commands run: cmake --build build --target strict_nid_filter_tests --parallel ctest --test-dir build -R strict_nid_filter --output-on-failure Result: strict_nid_filter passed ### 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 in [TechnicalDebt](https://github.com/boykopovar/AnyPS5/blob/main/docs/dev/TechnicalDebt.md#silent-stubs) - [x] No notes, investigation `.md` files or images added to the repository - [x] New third-party code is a submodule built from source - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes
copilot-pull-request-reviewer[bot] commented 2026-10-09 12:07:57 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by copilot-pull-request-reviewer[bot] on 2026-10-09T12:07:57Z.

Review: COMMENTED


Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5469797669 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2222#pullrequestreview-5469797669). Originally posted by [copilot-pull-request-reviewer[bot]](https://github.com/apps/copilot-pull-request-reviewer) on 2026-10-09T12:07:57Z. **Review:** COMMENTED --- Copilot was unable to review this pull request because the user who requested the review has reached their quota limit.
MarcZX10 commented 2026-10-09 12:09:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T12:09:22Z.

Review: COMMENTED


Review note: collectRelativeEntries now skips only when both the address and size are zero. Previously it returned when either was zero. An ELF can have a relocation address tag with a zero relocation size; there are no entries to read, so requiring that address to map inside a PT_LOAD can reject an empty table. Please keep zero-sized tables as no-ops (while retaining the malformed nonzero-size checks) and add coverage for a nonzero table address with a zero size. I have not approved the PR; the current checks show Conventions only, so a Build result on this exact head is still needed as well.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5469811422 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2222#pullrequestreview-5469811422). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T12:09:22Z. **Review:** COMMENTED --- Review note: `collectRelativeEntries` now skips only when both the address and size are zero. Previously it returned when either was zero. An ELF can have a relocation address tag with a zero relocation size; there are no entries to read, so requiring that address to map inside a PT_LOAD can reject an empty table. Please keep zero-sized tables as no-ops (while retaining the malformed nonzero-size checks) and add coverage for a nonzero table address with a zero size. I have not approved the PR; the current checks show Conventions only, so a Build result on this exact head is still needed as well.
EshaanJaiswal commented 2026-10-09 12:12:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EshaanJaiswal on 2026-10-09T12:12:22Z.


@MarcZX100, are you spying on me or something ? xD
How can someone review stuff so fast ?

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6080632938 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2222#issuecomment-6080632938). Originally posted by [EshaanJaiswal](https://github.com/EshaanJaiswal) on 2026-10-09T12:12:22Z. --- @MarcZX100, are you spying on me or something ? xD How can someone review stuff so fast ?
EshaanJaiswal commented 2026-10-09 12:23:56 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EshaanJaiswal on 2026-10-09T12:23:56Z.


Review note: collectRelativeEntries now skips only when both the address and size are zero. Previously it returned when either was zero. An ELF can have a relocation address tag with a zero relocation size; there are no entries to read, so requiring that address to map inside a PT_LOAD can reject an empty table. Please keep zero-sized tables as no-ops (while retaining the malformed nonzero-size checks) and add coverage for a nonzero table address with a zero size. I have not approved the PR; the current checks show Conventions only, so a Build result on this exact head is still needed as well.

Implemented the requested empty-table handling.

Zero-sized DT_RELA and DT_JMPREL tables now return early before address translation, so an unmapped address with size zero is treated as an empty table. Added regression coverage for this case.

Focused tests pass:

  • strict_nid_filter_tests
  • ctest -R strict_nid_filter
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6080810398 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2222#issuecomment-6080810398). Originally posted by [EshaanJaiswal](https://github.com/EshaanJaiswal) on 2026-10-09T12:23:56Z. --- > Review note: `collectRelativeEntries` now skips only when both the address and size are zero. Previously it returned when either was zero. An ELF can have a relocation address tag with a zero relocation size; there are no entries to read, so requiring that address to map inside a PT_LOAD can reject an empty table. Please keep zero-sized tables as no-ops (while retaining the malformed nonzero-size checks) and add coverage for a nonzero table address with a zero size. I have not approved the PR; the current checks show Conventions only, so a Build result on this exact head is still needed as well. Implemented the requested empty-table handling. Zero-sized DT_RELA and DT_JMPREL tables now return early before address translation, so an unmapped address with size zero is treated as an empty table. Added regression coverage for this case. Focused tests pass: - strict_nid_filter_tests - ctest -R strict_nid_filter
MarcZX10 commented 2026-10-09 12:35:29 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T12:35:29Z.


Thanks, the follow-up addresses my empty-table concern: zero-sized tables now return before address translation, and the regression covers a nonzero unmapped address with zero size. The added bounds checks and malformed-size cases look consistent on the updated diff. The focused test results and Conventions are reported green; the upstream Build workflow is still not present on this head, so I am not submitting approval until the required Build jobs run.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6081002152 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2222#issuecomment-6081002152). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T12:35:29Z. --- Thanks, the follow-up addresses my empty-table concern: zero-sized tables now return before address translation, and the regression covers a nonzero unmapped address with zero size. The added bounds checks and malformed-size cases look consistent on the updated diff. The focused test results and Conventions are reported green; the upstream Build workflow is still not present on this head, so I am not submitting approval until the required Build jobs run.
EshaanJaiswal commented 2026-10-09 12:38:13 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by EshaanJaiswal on 2026-10-09T12:38:13Z.


@MarcZX100, Build Jobs won't be able to run unless I am a contributor or a maintainer approves this/my other PRs.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6081044909 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2222#issuecomment-6081044909). Originally posted by [EshaanJaiswal](https://github.com/EshaanJaiswal) on 2026-10-09T12:38:13Z. --- @MarcZX100, Build Jobs won't be able to run unless I am a contributor or a maintainer approves this/my other PRs.
rmz-24 commented 2026-10-09 13:31:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by rmz-24 on 2026-10-09T13:31:08Z.


Windows run: Windows 11 Pro 25H2 (10.0.26200.9457), RTX 5060 Ti (NVIDIA 617.42), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON, full build plus libs. 98b449f6 cherry-picked onto current main d70b8998 (applies cleanly).

  • strict_nid_filter passes.
  • Full ctest: 510/510.

AI-assisted: yes

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6081920116 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2222#issuecomment-6081920116). Originally posted by [rmz-24](https://github.com/rmz-24) on 2026-10-09T13:31:08Z. --- Windows run: Windows 11 Pro 25H2 (10.0.26200.9457), RTX 5060 Ti (NVIDIA 617.42), MinGW-w64 GCC 15.2.0 (WinLibs r7), Release, `-DANYPS5_ENABLE_SPIRV_TOOLS=ON`, full build plus `libs`. 98b449f6 cherry-picked onto current `main` d70b8998 (applies cleanly). - `strict_nid_filter` passes. - Full ctest: 510/510. AI-assisted: yes
MarcZX10 commented 2026-10-09 14:28:05 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T14:28:05Z.


I see the independent Windows report for this exact head cherry-picked onto d70b8998: full build plus CTest 510/510, including the focused regression. That closes the Windows evidence gap. The remaining contribution-check gap is the full Linux Build and the relinker matrix on the current head; the PR currently reports only the focused Linux test. Since the author cannot dispatch the upstream workflow, an authorized collaborator would need to run it before this review can be finalized.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6082926933 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2222#issuecomment-6082926933). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T14:28:05Z. --- I see the independent Windows report for this exact head cherry-picked onto `d70b8998`: full build plus CTest 510/510, including the focused regression. That closes the Windows evidence gap. The remaining contribution-check gap is the full Linux Build and the relinker matrix on the current head; the PR currently reports only the focused Linux test. Since the author cannot dispatch the upstream workflow, an authorized collaborator would need to run it before this review can be finalized.
MarcZX10 commented 2026-10-09 20:50:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-09T20:50:22Z.


The PR has now received independent Windows validation for the current head (full build plus 510/510 CTest on a clean cherry-pick). I also verified the repository-required Linux/Windows full build and Linux/Windows/macOS relinker matrix workflow, but this checkout cannot dispatch it: GitHub returned 403 “Must have admin rights to Repository.” The only current check remains Conventions. I cannot submit an approval without the required project checks; a maintainer/collaborator with write access needs to run Actions > Build > Run workflow for PR #2222 on main.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6089030881 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2222#issuecomment-6089030881). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-09T20:50:22Z. --- The PR has now received independent Windows validation for the current head (full build plus 510/510 CTest on a clean cherry-pick). I also verified the repository-required Linux/Windows full build and Linux/Windows/macOS relinker matrix workflow, but this checkout cannot dispatch it: GitHub returned 403 “Must have admin rights to Repository.” The only current check remains Conventions. I cannot submit an approval without the required project checks; a maintainer/collaborator with write access needs to run Actions > Build > Run workflow for PR #2222 on 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!2222
No description provided.