[Memory/input reliability] Validate ELF symbol ranges and preserve allocator regressions #1666

Closed
opened 2026-10-08 16:03:05 +00:00 by Sanjaysrinivas · 3 comments
Sanjaysrinivas commented 2026-10-08 16:03:05 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by Sanjaysrinivas on 2026-10-08T16:03:05Z.


Split from #1659. Category: memory and input reliability. This tracks conversion correctness and stable allocation behavior during emulator use.

Problem and expected outcome

The relinker must reject inconsistent ELF metadata before reading outside the input image. Failed allocation requests must leave existing allocations usable. These are prerequisites for diagnosing conversion failures and crashes reliably: an invalid read or damaged allocation can make later symptoms unrelated to their original cause.

The remaining concrete source-level concern is the symbol-entry calculation in RelinkerPipeline::Relink. The original allocator-resize and RELA-entry findings have already been fixed upstream and should remain regression controls.

Evidence and affected code

Baseline checked: upstream 4b005b229e24be9ec3f185def9e59cbc03aa9e4a.

  • RelinkerPipeline.cpp, symbol lookup calculates symOff = dynSymTabOffset + symIdx * symEntSize, checks symOff + 4 > raw.size(), and then copies four bytes from raw.data() + symOff.
  • DT_OS_SYMTAB is accepted as a file offset through readAsOffset. The symbol-table base is not first checked against the input size at this lookup. The base-plus-index addition and the subsequent + 4 check can wrap, so the condition does not establish that the bytes are in the input image.
  • The neighboring RELA calculation now uses subtraction-based checks following #917. Its fix does not cover the symbol calculation above.
  • #1110 fixes overflowing mspace resize requests. tests/Mspace.cpp already checks near-maximum sizes, allocation contents, usable size, and allocator statistics. Extend or retain these tests instead of recreating them.

Evidence level: the remaining symbol-offset concern was verified by source inspection at this baseline. This issue does not claim a new runtime reproduction or a game-specific crash from that concern. The earlier allocator/RELA findings are resolved work, not new open defects.

Reproduction and diagnosis procedure

Use the synthetic ELF builder in test_optional_plt.py as the starting point; no game files are needed.

  1. Convert the unchanged fixture first to establish that the current binary and command work.
  2. Make its relocation a symbol-referencing relocation supported by the relinker, rather than RELATIVE, which skips symbol lookup.
  3. Replace the ordinary symbol-table tag with the OS file-offset variant. Exercise a base beyond EOF, a base near the maximum offset value, and a base whose indexed entry crosses the end of the image.
  4. Run the actual conversion pipeline using relinker --skip-sce-module --windows <input.elf> <output.exe>. Also cover the Linux output path where the fixture supports it.
  5. Capture the conversion status, diagnostic, and whether an output was created. Run the same inputs under instrumented parser tests when that CI lane is available.

Expected behavior is a controlled conversion error identifying the invalid table/entry. The test must not pass merely because any nonzero process exit occurred: a crash is a failure, not valid rejection.

Implementation work

  • Validate the symbol-table base against the input size before deriving a pointer or indexed offset.
  • Validate the indexed entry using available bytes or checked arithmetic, so neither address calculation nor the final range check can wrap. State whether the parser requires the complete 24-byte ELF64 symbol entry or only the fields it intentionally reads.
  • Keep valid OS-tag and ordinary ELF-tag behavior unchanged, including optional/empty PLT handling and supported relocation types.
  • Review adjacent table/string/segment reads along this same pipeline for the same arithmetic pattern. Record additional concrete findings with location and reproducer; avoid unrelated parser rewrites.
  • Add focused integration regressions to the existing relinker fixture suite, including an unchanged valid control.
  • Execute and retain the existing allocator and RELA regressions. A rejected resize must preserve the original allocation's contents, usable size, statistics, and subsequent free/reallocation behavior.

Validation matrix

Case Required result
Valid symbol entry and import name Existing successful conversion and expected import/relocation preserved
Symbol-table base beyond EOF Controlled bounds diagnostic; no completed output
Base near the maximum file-offset value Controlled rejection before pointer arithmetic or reading
Base plus symbol index crosses EOF or wraps Controlled rejection of the derived entry
Entry/name field truncated at EOF Controlled rejection, with no invalid read
Valid entry at the chosen boundary Correct acceptance according to the documented entry-size requirement
Malformed RELA metadata covered by #917 Existing rejection retained
Failed ordinary/aligned allocator resize covered by #1110 Null/error result and original allocation remains intact

Run applicable fixture tests on Windows and Linux and in Release. The testing/release category owns the shared instrumented CI lane; this category owns the input cases and behavioral assertions.

Acceptance criteria

  • The checked symbol-entry path cannot read before or after the input buffer for the boundary cases above.
  • Rejections are distinguishable from crashes, and valid conversions remain unchanged.
  • Tests execute through the real parser/pipeline, rather than only testing a newly introduced arithmetic helper.
  • Existing #1110/#917 regressions still pass, including post-failure allocator usability.
  • Verification identifies the tested commit, toolchain, host OS, commands, and results. Any remaining concrete gap is explicitly linked and scoped.

Scope and dependencies

Keep the relinker's C++20 standard-library constraint. Start with the concrete symbol-entry check and focused tests; additional infrastructure is unnecessary for the first fix. This issue can progress before the shared CI improvements. No claim about complete title compatibility follows from parser or allocator tests alone.

  • Shared instrumented CI and regression execution: #1669.
  • Title-level conversion/crash evidence: #1671.
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5765002252 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1666). Originally posted by [Sanjaysrinivas](https://github.com/Sanjaysrinivas) on 2026-10-08T16:03:05Z. --- Split from #1659. Category: **memory and input reliability**. This tracks conversion correctness and stable allocation behavior during emulator use. ## Problem and expected outcome The relinker must reject inconsistent ELF metadata before reading outside the input image. Failed allocation requests must leave existing allocations usable. These are prerequisites for diagnosing conversion failures and crashes reliably: an invalid read or damaged allocation can make later symptoms unrelated to their original cause. The remaining concrete source-level concern is the symbol-entry calculation in `RelinkerPipeline::Relink`. The original allocator-resize and RELA-entry findings have already been fixed upstream and should remain regression controls. ## Evidence and affected code Baseline checked: upstream `4b005b229e24be9ec3f185def9e59cbc03aa9e4a`. - [`RelinkerPipeline.cpp`, symbol lookup](https://github.com/boykopovar/AnyPS5/blob/4b005b229e24be9ec3f185def9e59cbc03aa9e4a/core/relinker/relinker/src/pipeline/RelinkerPipeline.cpp#L218) calculates `symOff = dynSymTabOffset + symIdx * symEntSize`, checks `symOff + 4 > raw.size()`, and then copies four bytes from `raw.data() + symOff`. - `DT_OS_SYMTAB` is accepted as a file offset through `readAsOffset`. The symbol-table base is not first checked against the input size at this lookup. The base-plus-index addition and the subsequent `+ 4` check can wrap, so the condition does not establish that the bytes are in the input image. - The neighboring RELA calculation now uses subtraction-based checks following #917. Its fix does not cover the symbol calculation above. - #1110 fixes overflowing `mspace` resize requests. [`tests/Mspace.cpp`](https://github.com/boykopovar/AnyPS5/blob/4b005b229e24be9ec3f185def9e59cbc03aa9e4a/core/libs/tests/Mspace.cpp#L47) already checks near-maximum sizes, allocation contents, usable size, and allocator statistics. Extend or retain these tests instead of recreating them. **Evidence level:** the remaining symbol-offset concern was verified by source inspection at this baseline. This issue does not claim a new runtime reproduction or a game-specific crash from that concern. The earlier allocator/RELA findings are resolved work, not new open defects. ## Reproduction and diagnosis procedure Use the synthetic ELF builder in [`test_optional_plt.py`](https://github.com/boykopovar/AnyPS5/blob/4b005b229e24be9ec3f185def9e59cbc03aa9e4a/core/relinker/relinker/tests/test_optional_plt.py) as the starting point; no game files are needed. 1. Convert the unchanged fixture first to establish that the current binary and command work. 2. Make its relocation a symbol-referencing relocation supported by the relinker, rather than `RELATIVE`, which skips symbol lookup. 3. Replace the ordinary symbol-table tag with the OS file-offset variant. Exercise a base beyond EOF, a base near the maximum offset value, and a base whose indexed entry crosses the end of the image. 4. Run the actual conversion pipeline using `relinker --skip-sce-module --windows <input.elf> <output.exe>`. Also cover the Linux output path where the fixture supports it. 5. Capture the conversion status, diagnostic, and whether an output was created. Run the same inputs under instrumented parser tests when that CI lane is available. Expected behavior is a controlled conversion error identifying the invalid table/entry. The test must not pass merely because any nonzero process exit occurred: a crash is a failure, not valid rejection. ## Implementation work - [ ] Validate the symbol-table base against the input size before deriving a pointer or indexed offset. - [ ] Validate the indexed entry using available bytes or checked arithmetic, so neither address calculation nor the final range check can wrap. State whether the parser requires the complete 24-byte ELF64 symbol entry or only the fields it intentionally reads. - [ ] Keep valid OS-tag and ordinary ELF-tag behavior unchanged, including optional/empty PLT handling and supported relocation types. - [ ] Review adjacent table/string/segment reads along this same pipeline for the same arithmetic pattern. Record additional concrete findings with location and reproducer; avoid unrelated parser rewrites. - [ ] Add focused integration regressions to the existing relinker fixture suite, including an unchanged valid control. - [ ] Execute and retain the existing allocator and RELA regressions. A rejected resize must preserve the original allocation's contents, usable size, statistics, and subsequent free/reallocation behavior. ## Validation matrix | Case | Required result | | --- | --- | | Valid symbol entry and import name | Existing successful conversion and expected import/relocation preserved | | Symbol-table base beyond EOF | Controlled bounds diagnostic; no completed output | | Base near the maximum file-offset value | Controlled rejection before pointer arithmetic or reading | | Base plus symbol index crosses EOF or wraps | Controlled rejection of the derived entry | | Entry/name field truncated at EOF | Controlled rejection, with no invalid read | | Valid entry at the chosen boundary | Correct acceptance according to the documented entry-size requirement | | Malformed RELA metadata covered by #917 | Existing rejection retained | | Failed ordinary/aligned allocator resize covered by #1110 | Null/error result and original allocation remains intact | Run applicable fixture tests on Windows and Linux and in Release. The testing/release category owns the shared instrumented CI lane; this category owns the input cases and behavioral assertions. ## Acceptance criteria - [ ] The checked symbol-entry path cannot read before or after the input buffer for the boundary cases above. - [ ] Rejections are distinguishable from crashes, and valid conversions remain unchanged. - [ ] Tests execute through the real parser/pipeline, rather than only testing a newly introduced arithmetic helper. - [ ] Existing #1110/#917 regressions still pass, including post-failure allocator usability. - [ ] Verification identifies the tested commit, toolchain, host OS, commands, and results. Any remaining concrete gap is explicitly linked and scoped. ## Scope and dependencies Keep the relinker's C++20 standard-library constraint. Start with the concrete symbol-entry check and focused tests; additional infrastructure is unnecessary for the first fix. This issue can progress before the shared CI improvements. No claim about complete title compatibility follows from parser or allocator tests alone. ## Related category issues - Shared instrumented CI and regression execution: #1669. - Title-level conversion/crash evidence: #1671.
StarMrGamer commented 2026-10-09 11:52:09 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by StarMrGamer on 2026-10-09T11:52:09Z.


Checked the open pull requests in this cluster against main (d70b8998). They are complementary, not duplicative: each bounds a different read, and in RelinkerPipeline.cpp they touch different hunks (around lines 59-90, 165 and 218) or different files, so they can land in any order with at most a trivial rebase.

Each branch was built in its own worktree, relinker-only (-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON, GCC 16.2.1, Linux x86-64), and ctest is clean in all five:

PR Read it bounds Where Build
#1272 symbol-table entry RelinkerPipeline.cpp:218 OK, 44/44
#1446 DT_RELA table range RelinkerPipeline.cpp:165 OK, 39/39
#1938 duplicate PT_DYNAMIC and duplicate DT tag RelinkerPipeline.cpp:59-90 OK, 39/39
#2196 entry point collector header and symbol reads EntryPointCollector.cpp OK, 44/44
#2091 call-site target range wrap CallSiteResolver.cpp OK, 39/39

The three 39-test branches are based on an older main than the 44-test ones (five tests have been added since); #1446 is CONFLICTING against current main but builds fine as a branch.

Against this issue's acceptance criteria, #1272 is the one that covers the symbol-entry row. It replaces if (symOff + 4 > raw.size()) with if (symOff < dynSymTabOffset || symOff > raw.size() || raw.size() - symOff < 4), which closes both the wrapping sum and the range check.

One item in the implementation checklist is still implicit: "State whether the parser requires the complete 24-byte ELF64 symbol entry or only the fields it intentionally reads." #1272 requires only the 4 bytes it dereferences, so a truncated entry that still carries st_name is accepted and the name is then resolved through the separately bounded string table. That is memory-safe, but it is a deliberate choice and should be stated, because the matrix row for a valid entry at the boundary expects acceptance "according to the documented entry-size requirement", and nothing documents that requirement yet.

For the adjacent-read review item, these five PRs are the concrete findings that came out of it; I did not find an unchecked read pattern along this pipeline that none of them covers.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6080301157 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1666#issuecomment-6080301157). Originally posted by [StarMrGamer](https://github.com/StarMrGamer) on 2026-10-09T11:52:09Z. --- Checked the open pull requests in this cluster against `main` (`d70b8998`). They are complementary, not duplicative: each bounds a different read, and in `RelinkerPipeline.cpp` they touch different hunks (around lines 59-90, 165 and 218) or different files, so they can land in any order with at most a trivial rebase. Each branch was built in its own worktree, relinker-only (`-DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON`, GCC 16.2.1, Linux x86-64), and `ctest` is clean in all five: | PR | Read it bounds | Where | Build | |---|---|---|---| | #1272 | symbol-table entry | `RelinkerPipeline.cpp:218` | OK, 44/44 | | #1446 | `DT_RELA` table range | `RelinkerPipeline.cpp:165` | OK, 39/39 | | #1938 | duplicate `PT_DYNAMIC` and duplicate DT tag | `RelinkerPipeline.cpp:59-90` | OK, 39/39 | | #2196 | entry point collector header and symbol reads | `EntryPointCollector.cpp` | OK, 44/44 | | #2091 | call-site target range wrap | `CallSiteResolver.cpp` | OK, 39/39 | The three 39-test branches are based on an older `main` than the 44-test ones (five tests have been added since); `#1446` is `CONFLICTING` against current `main` but builds fine as a branch. Against this issue's acceptance criteria, **#1272 is the one that covers the symbol-entry row.** It replaces `if (symOff + 4 > raw.size())` with `if (symOff < dynSymTabOffset || symOff > raw.size() || raw.size() - symOff < 4)`, which closes both the wrapping sum and the range check. One item in the implementation checklist is still implicit: *"State whether the parser requires the complete 24-byte ELF64 symbol entry or only the fields it intentionally reads."* #1272 requires only the 4 bytes it dereferences, so a truncated entry that still carries `st_name` is accepted and the name is then resolved through the separately bounded string table. That is memory-safe, but it is a deliberate choice and should be stated, because the matrix row for a valid entry at the boundary expects acceptance "according to the documented entry-size requirement", and nothing documents that requirement yet. For the adjacent-read review item, these five PRs are the concrete findings that came out of it; I did not find an unchecked read pattern along this pipeline that none of them covers.
StarMrGamer commented 2026-10-09 16:57:59 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by StarMrGamer on 2026-10-09T16:57:59Z.


Update to the table above: #1446 has been rebased (5de529ff → c917df8f, the same two commits) and now reports MERGEABLE instead of CONFLICTING, so the last paragraph of that comment no longer applies.

Re-verified the new head here: configure and build clean, ctest 44/44 (it picks up the five tests added to main since it was branched), check_conventions.py clean.

All five pull requests are now MERGEABLE, and the other four heads are unchanged from what I built: #1272 84d851bf, #1938 8ee83315, #2196 fcba0b0d, #2091 d5d6e577.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6085430771 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1666#issuecomment-6085430771). Originally posted by [StarMrGamer](https://github.com/StarMrGamer) on 2026-10-09T16:57:59Z. --- Update to the table above: **#1446 has been rebased** (`5de529ff` → `c917df8f`, the same two commits) and now reports `MERGEABLE` instead of `CONFLICTING`, so the last paragraph of that comment no longer applies. Re-verified the new head here: configure and build clean, `ctest` 44/44 (it picks up the five tests added to `main` since it was branched), `check_conventions.py` clean. All five pull requests are now `MERGEABLE`, and the other four heads are unchanged from what I built: #1272 `84d851bf`, #1938 `8ee83315`, #2196 `fcba0b0d`, #2091 `d5d6e577`.
oneandonlydean commented 2026-10-10 03:07:53 +00:00 (Migrated from github.com)
Author
Contributor

Imported from boykopovar/AnyPS5. Originally posted by oneandonlydean on 2026-10-10T03:07:53Z.


Fixed by #1272: the symbol-entry read in RelinkerPipeline.cpp now rejects a wrapped or out-of-range offset ("Symbol table entry out of bounds"), and #1446 bounds the DT_RELA range. The allocator items were already fixed, so closing.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6093145717 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/1666#issuecomment-6093145717). Originally posted by [oneandonlydean](https://github.com/oneandonlydean) on 2026-10-10T03:07:53Z. --- Fixed by #1272: the symbol-entry read in `RelinkerPipeline.cpp` now rejects a wrapped or out-of-range offset ("Symbol table entry out of bounds"), and #1446 bounds the `DT_RELA` range. The allocator items were already fixed, so closing.
Sign in to join this conversation.
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#1666
No description provided.