fix(relinker): bound the symbol and relocation table reads #1272

Merged
Dextroid17 merged 1 commit from upstream-pr/1272 into main 2026-10-09 22:42:33 +00:00
Dextroid17 commented 2026-10-07 20:57:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-07T20:57:22Z.


What

Two reads of the dynamic tables check bounds with arithmetic that can wrap, or not at all.

The symbol table, RelinkerPipeline.cpp:210. dynSymTabOffset comes straight from DT_OS_SYMTAB and is never bounded, and the check adds four to the offset:

dynSymTabOffset = 0xFFFFFFFFFFFFFFFE
symOff          = 0xFFFFFFFFFFFFFFFE
check:  symOff + 4  wraps to 2   <= raw.size()   -> passes
read:   memcpy(raw.data() + 0xFFFFFFFFFFFFFFFE, 4)

extractRelative, RelinkerPipeline.cpp:293. pos = relaOff + off, then memcpy(raw.data() + pos, 8) with no bounds check at all, while its sibling extractRela at least has one.

Both now compare against raw.size() without adding to the offset first, and reject an offset below the table start, which is what a wrapped sum produces. The symbol table has no size tag to bound it with, so that check stays per entry, where it was.

The symbol table case was first reported by @muhzain05 in #917's thread; this makes it a bound on the read rather than another case of the same arithmetic.

Tested

core/relinker/relinker/tests/DynamicTableBoundsTests.cpp crafts an ELF whose DT_OS_SYMTAB is 0xFFFFFFFFFFFFFFFE, and requires the relinker to report the entry as out of bounds. It is registered as dynamic_table_bounds and drives the real CLI through RelinkerProcess.hpp.

Failing on main, proven by reverting only the implementation and rebuilding:

FAIL: Dynamic string is not NUL-terminated within DT_STRSZ

The wrapped offset lands on a byte that is not the entry, so the relinker reports an unrelated problem in another function. With the change it reports Symbol table entry out of bounds (offset ...).

Full suite on Linux (CachyOS, GCC 16.2.1): 405/405 pass, including the new test.

What the test does not cover: the extractRelative check has no differential case. extractRela reads the same entries first and its own arithmetic wraps identically, so on main the garbage entry usually trips a later check before extractRelative is reached; the guard matters for the case where that garbage entry happens to look relative and is skipped. Saying so rather than implying a second proven case.

This does not touch extractRela, which #917 repairs — that path already has an open PR.

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 (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
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4778393859 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1272). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-07T20:57:22Z. --- ### What Two reads of the dynamic tables check bounds with arithmetic that can wrap, or not at all. **The symbol table, `RelinkerPipeline.cpp:210`.** `dynSymTabOffset` comes straight from `DT_OS_SYMTAB` and is never bounded, and the check adds four to the offset: ``` dynSymTabOffset = 0xFFFFFFFFFFFFFFFE symOff = 0xFFFFFFFFFFFFFFFE check: symOff + 4 wraps to 2 <= raw.size() -> passes read: memcpy(raw.data() + 0xFFFFFFFFFFFFFFFE, 4) ``` **`extractRelative`, `RelinkerPipeline.cpp:293`.** `pos = relaOff + off`, then `memcpy(raw.data() + pos, 8)` with no bounds check at all, while its sibling `extractRela` at least has one. Both now compare against `raw.size()` without adding to the offset first, and reject an offset below the table start, which is what a wrapped sum produces. The symbol table has no size tag to bound it with, so that check stays per entry, where it was. The symbol table case was first reported by @muhzain05 in #917's thread; this makes it a bound on the read rather than another case of the same arithmetic. ### Tested `core/relinker/relinker/tests/DynamicTableBoundsTests.cpp` crafts an ELF whose `DT_OS_SYMTAB` is `0xFFFFFFFFFFFFFFFE`, and requires the relinker to report the entry as out of bounds. It is registered as `dynamic_table_bounds` and drives the real CLI through `RelinkerProcess.hpp`. **Failing on `main`, proven by reverting only the implementation and rebuilding:** ``` FAIL: Dynamic string is not NUL-terminated within DT_STRSZ ``` The wrapped offset lands on a byte that is not the entry, so the relinker reports an unrelated problem in another function. With the change it reports `Symbol table entry out of bounds (offset ...)`. Full suite on Linux (CachyOS, GCC 16.2.1): **405/405 pass**, including the new test. **What the test does not cover:** the `extractRelative` check has no differential case. `extractRela` reads the same entries first and its own arithmetic wraps identically, so on `main` the garbage entry usually trips a later check before `extractRelative` is reached; the guard matters for the case where that garbage entry happens to look relative and is skipped. Saying so rather than implying a second proven case. This does not touch `extractRela`, which #917 repairs — that path already has an open PR. ### 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 (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] AI-assisted: yes
Dextroid17 commented 2026-10-08 03:10:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T03:10:37Z.


@oneandonlydean ready for eyes — bounds the symbol and relocation table reads in the relinker pipeline, with a test that fails on an unbounded read. Four checks green.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6051380590 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1272#issuecomment-6051380590). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T03:10:37Z. --- @oneandonlydean ready for eyes — bounds the symbol and relocation table reads in the relinker pipeline, with a test that fails on an unbounded read. Four checks green.
oneandonlydean commented 2026-10-08 06:37:20 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-08T06:37:20Z.


#917 is on main and now bounds extractRelative through relaEntryPos, so that hunk is no longer needed and the PR conflicts in RelinkerPipeline.cpp and core/relinker/CMakeLists.txt. Please rebase with just the symbol table check and its test.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6054011986 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1272#issuecomment-6054011986). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-08T06:37:20Z. --- #917 is on main and now bounds `extractRelative` through `relaEntryPos`, so that hunk is no longer needed and the PR conflicts in `RelinkerPipeline.cpp` and `core/relinker/CMakeLists.txt`. Please rebase with just the symbol table check and its test.
Dextroid17 commented 2026-10-08 10:37:40 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T10:37:40Z.


Agreed — #917 now bounds extractRelative through relaEntryPos on main, so that hunk is stale. I'll rebase down to just the symbol-table bounds check and its test and drop the extractRelative hunk, then re-run the suite on the rebased head before asking for another look.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6058025054 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1272#issuecomment-6058025054). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T10:37:40Z. --- Agreed — #917 now bounds `extractRelative` through `relaEntryPos` on `main`, so that hunk is stale. I'll rebase down to just the symbol-table bounds check and its test and drop the `extractRelative` hunk, then re-run the suite on the rebased head before asking for another look.
Dextroid17 commented 2026-10-08 10:46:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T10:46:30Z.


Rebased on current main as asked — the relocation hunk is gone, since relaEntryPos from #917 now bounds extractRelative. What is left is the symbol-table half:

-            if (symOff + 4 > raw.size())
+            if (symOff < dynSymTabOffset || symOff > raw.size() || raw.size() - symOff < 4)
                 throw RelinkerException("Symbol table entry out of bounds", symOff);

DT_OS_SYMTAB is returned raw by readAsOffset, so a bogus value reaches this check — and symOff + 4 wraps for symOff == 0xfffffffffffffffe, so the memcpy reads the name pointer from base - 2 inside the file and the failure surfaces somewhere else entirely.

Negative control — test kept, only the implementation reverted to main:

Test #441: dynamic_table_bounds .............***Failed
dynamic_table_bounds_tests: Relinker did not report the symbol table entry as out of bounds:
...
FAIL: Dynamic string is not NUL-terminated within DT_STRSZ

So on main the bogus offset is not rejected at the read, and what a title would see is a message that names no cause. With the bound in place the run stops at Symbol table entry out of bounds.

  • Linux (CachyOS, GCC, Release, -DANYPS5_ENABLE_SPIRV_TOOLS=ON): full suite 441/441 passed (29 s).
  • python3 tools/check_conventions.py --base upstream/main passes.
  • The fixture keeps one RELA entry with symIdx = 1 on purpose: with no relocation referencing a symbol the symbol table is never read, so there is nothing to bound. That one-entry table is DT_RELASZ = 24.
  • Also fixed the missing trailing newline in the test file.

dynamic_table_bounds is registered next to #917's rela_bounds, so both halves of the dynamic table are covered by a test that fails on the code without the guard.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6058167060 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1272#issuecomment-6058167060). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T10:46:30Z. --- Rebased on current `main` as asked — the relocation hunk is gone, since `relaEntryPos` from #917 now bounds `extractRelative`. What is left is the symbol-table half: ```diff - if (symOff + 4 > raw.size()) + if (symOff < dynSymTabOffset || symOff > raw.size() || raw.size() - symOff < 4) throw RelinkerException("Symbol table entry out of bounds", symOff); ``` `DT_OS_SYMTAB` is returned raw by `readAsOffset`, so a bogus value reaches this check — and `symOff + 4` wraps for `symOff == 0xfffffffffffffffe`, so the `memcpy` reads the name pointer from `base - 2` inside the file and the failure surfaces somewhere else entirely. **Negative control** — test kept, only the implementation reverted to `main`: ``` Test #441: dynamic_table_bounds .............***Failed dynamic_table_bounds_tests: Relinker did not report the symbol table entry as out of bounds: ... FAIL: Dynamic string is not NUL-terminated within DT_STRSZ ``` So on `main` the bogus offset is not rejected at the read, and what a title would see is a message that names no cause. With the bound in place the run stops at `Symbol table entry out of bounds`. - Linux (CachyOS, GCC, Release, `-DANYPS5_ENABLE_SPIRV_TOOLS=ON`): full suite **441/441 passed** (29 s). - `python3 tools/check_conventions.py --base upstream/main` passes. - The fixture keeps one RELA entry with `symIdx = 1` on purpose: with no relocation referencing a symbol the symbol table is never read, so there is nothing to bound. That one-entry table is `DT_RELASZ = 24`. - Also fixed the missing trailing newline in the test file. `dynamic_table_bounds` is registered next to #917's `rela_bounds`, so both halves of the dynamic table are covered by a test that fails on the code without the guard. Written with AI assistance.
github-actions[bot] commented 2026-10-08 14:49:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-08T14:49:18Z.


Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in Depends on.

Pull request Conflicting files (lines on main) Same exports Same new files
#1446 core/relinker/CMakeLists.txt L310
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6062493156 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1272#issuecomment-6062493156). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-08T14:49:18Z. --- <!-- pr-overlap --> Other open pull requests touch the same code. Merging one will break the other, or both implement the same thing. If this one builds on them, list them in **Depends on**. | Pull request | Conflicting files (lines on `main`) | Same exports | Same new files | |---|---|---|---| | #1446 | `core/relinker/CMakeLists.txt` [L310](https://github.com/boykopovar/AnyPS5/blob/64fe74399528c84fd74c3f736e632f89f4a7f211/core/relinker/CMakeLists.txt#L310) | | |
Dextroid17 commented 2026-10-08 16:08:58 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T16:08:58Z.


Rebased onto current main (4b005b22) and the four checks are green again as of today:

  linux ✅  windows ✅  conventions ✅  report ✅      MERGEABLE / CLEAN

Nothing further is planned on this branch — it is ready for a review pass whenever you have a moment. Command output for both runs is in my comment from earlier: 441/441 on main with the old test, then the negative control failing on the non-NUL-terminated string, then green with the bound in place.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6064050535 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1272#issuecomment-6064050535). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T16:08:58Z. --- Rebased onto current `main` (`4b005b22`) and the four checks are green again as of today: ``` linux ✅ windows ✅ conventions ✅ report ✅ MERGEABLE / CLEAN ``` Nothing further is planned on this branch — it is ready for a review pass whenever you have a moment. Command output for both runs is in my comment from earlier: `441/441` on `main` with the old test, then the negative control failing on the non-NUL-terminated string, then green with the bound in place. Written with AI assistance.
Dextroid17 commented 2026-10-08 17:40:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-08T17:40:08Z.


Status, since it affects how this one lands:

  • @MarcZX100 closed #1682 this evening as a duplicate of this PR — "its current head already contains the same subtraction-based symbol-entry bounds check and has all four upstream checks passing" — so this is the canonical implementation of the bound.
  • #1690 is stacked on this one and supplies the integration regressions. I verified the pair on today's main (2b297842): both merges apply in order without conflict, the full relinker suite is 38/38 on Linux, and with this bound reverted to main's symOff + 4 > raw.size() both new tests fail — so they gate the fix rather than passing beside it.
  • This PR itself: MERGEABLE / CLEAN, all four checks SUCCESS, rebased onto current main.

Merge this first, then #1690 on top.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6065623377 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1272#issuecomment-6065623377). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-08T17:40:08Z. --- Status, since it affects how this one lands: - `@MarcZX100` closed #1682 this evening **as a duplicate of this PR** — "its current head already contains the same subtraction-based symbol-entry bounds check and has all four upstream checks passing" — so this is the canonical implementation of the bound. - #1690 is stacked on this one and supplies the integration regressions. I verified the pair on today's `main` (`2b297842`): both merges apply in order without conflict, the full relinker suite is 38/38 on Linux, and with this bound reverted to `main`'s `symOff + 4 > raw.size()` both new tests fail — so they gate the fix rather than passing beside it. - This PR itself: `MERGEABLE / CLEAN`, all four checks `SUCCESS`, rebased onto current `main`. Merge this first, then #1690 on top. Written with AI assistance.
adriadam10 commented 2026-10-09 22:42:30 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by adriadam10 on 2026-10-09T22:42:30Z.


Checked on a Linux build merged onto current main: full ctest 551/551 passes. Reverting only the check and rebuilding relinker makes dynamic_table_bounds fail (Dynamic string is not NUL-terminated within DT_STRSZ); with the check it reports Symbol table entry out of bounds. The test is registered explicitly in ctest. Merging.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090499226 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/1272#issuecomment-6090499226). Originally posted by [adriadam10](https://github.com/adriadam10) on 2026-10-09T22:42:30Z. --- Checked on a Linux build merged onto current `main`: full ctest 551/551 passes. Reverting only the check and rebuilding `relinker` makes `dynamic_table_bounds` fail (`Dynamic string is not NUL-terminated within DT_STRSZ`); with the check it reports `Symbol table entry out of bounds`. The test is registered explicitly in ctest. Merging.
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!1272
No description provided.