test(relinker): audit ELF header validation and section link bounds #15651

Merged
slopgobblercobbler merged 5 commits from upstream-pr/15651 into main 2026-11-08 01:47:08 +00:00
slopgobblercobbler commented 2026-11-03 23:47:18 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by slopgobblercobbler on 2026-11-03T23:47:18Z.


Depends on: none

What

  • Audit ELF64 header validation in ElfReader and ElfReaderBoundsTests.cpp, asserting strict validation of header sizes, SELF container signatures, ELF magic numbers, ELF64 class, little-endian data encoding, object versions, x86-64 machine type, and program header entry sizes.
  • Audit section header entry sizes (SHT_SYMTAB, SHT_DYNSYM, SHT_RELA, SHT_REL) and enforce sh_link boundary checks on symbol, relocation, and dynamic section associations.
  • Verify PT_NULL segments preserve unused fields and audit positive powers-of-two program header alignment.
  • Document ELF section header validation technical debt in docs/dev/debt/relinker.md.

Tested

  • Built and ran all 57 test cases in ElfReaderBoundsTests.cpp with zero failures.
  • Ran convention verification with python tools/check_conventions.py --base upstream/main --head HEAD: all checks passed with 0 findings.

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
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5146298447 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15651). Originally posted by [slopgobblercobbler](https://github.com/slopgobblercobbler) on 2026-11-03T23:47:18Z. --- Depends on: none ### What - Audit ELF64 header validation in `ElfReader` and `ElfReaderBoundsTests.cpp`, asserting strict validation of header sizes, SELF container signatures, ELF magic numbers, ELF64 class, little-endian data encoding, object versions, x86-64 machine type, and program header entry sizes. - Audit section header entry sizes (`SHT_SYMTAB`, `SHT_DYNSYM`, `SHT_RELA`, `SHT_REL`) and enforce `sh_link` boundary checks on symbol, relocation, and dynamic section associations. - Verify `PT_NULL` segments preserve unused fields and audit positive powers-of-two program header alignment. - Document ELF section header validation technical debt in `docs/dev/debt/relinker.md`. ### Tested - Built and ran all 57 test cases in `ElfReaderBoundsTests.cpp` with zero failures. - Ran convention verification with `python tools/check_conventions.py --base upstream/main --head HEAD`: all checks passed with 0 findings. ### 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
XairRrR commented 2026-11-03 23:57:53 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by XairRrR on 2026-11-03T23:57:53Z.

Review: DISMISSED


Reviewed — changes requested

The new ElfReader checks break two existing relinker tests. guest_load_ranges fails because a program-header alignment error now has a different message. optional_rela fails because a DT_RELSZ error now has a different message. Both tests pass again when the production files are restored from main.

What I ran

Head c2d64c45a5b12aab76be11c85e23d162f603358e, main 1c8c0efc8c41314402e6966da5bef6ea3d639d10, merged into main with --no-ff (merged tree a0c8808eea49c475f5f8d1be621388c795198863), Release build with GCC 14.2.0 and Ninja, Debian 13.

git diff --check 1c8c0efc8c41314402e6966da5bef6ea3d639d10...c2d64c45a5b12aab76be11c85e23d162f603358e
python3 tools/check_conventions.py --base 1c8c0efc8c41314402e6966da5bef6ea3d639d10
ctest --test-dir build -j8 --test-load 32 --output-on-failure -R "^(guest_load_ranges|optional_rela|elf_reader_bounds|dynamic_table_bounds|rela_bounds|reloc_table_bounds|jmprel_table_bounds)$"
# negative control
git show 1c8c0efc8c41314402e6966da5bef6ea3d639d10:core/relinker/relinker/src/parsing/ElfReader.cpp > core/relinker/relinker/src/parsing/ElfReader.cpp
git show 1c8c0efc8c41314402e6966da5bef6ea3d639d10:core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp > core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp
ninja -C build -j32 -l32 elf_reader_bounds_tests guest_load_ranges_tests relinker
ctest --test-dir build --output-on-failure -R "^(elf_reader_bounds|guest_load_ranges|optional_rela)$"
# restore the merged tree
git show a0c8808eea49c475f5f8d1be621388c795198863:<both paths> > <both paths>
ninja -C build -j32 -l32 elf_reader_bounds_tests guest_load_ranges_tests relinker
git status --porcelain

Results

Check Result
git diff --check clean
check_conventions.py All conventions checks passed
Configure / build / libs OK
Selected tests (793 run, script) 787 passed, 3 expected skips. 2 new failures: guest_load_ranges, optional_rela. 1 known failure on main: unwrap_self_..._group
elf_reader_bounds passed
dynamic_table_bounds, rela_bounds, reloc_table_bounds, jmprel_table_bounds passed
guest_load_ranges failed, and passes with main's production files
optional_rela failed, and passes with main's production files
guest_load_ranges
empty-last-alignment: Expected Invalid load segment alignment, got Program header alignment is not a power of two
empty-first-alignment: Expected Invalid load segment alignment, got Program header alignment is not a power of two
optional_rela
AssertionError: ('unsupported-18', False, 'linux', 2, 'Converting False-unsupported-18.elf\nSystem: Linux; unused-filter=0\n\n', 'FAIL: Invalid DT_RELSZ value: 24 bytes; expected a multiple of 16 bytes (offset 0x450)\n...')

The failure baseline came from an older main (1e658521a0). The negative control removes that doubt: with only ElfReader.cpp and ElfConstants.hpp restored from main (1c8c0efc8c), both tests pass. The failures therefore come from this PR.

Negative control

I restored core/relinker/relinker/src/parsing/ElfReader.cpp and core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp from main and rebuilt.

  • elf_reader_bounds fails in these cases:
    • section alignment
    • SHT_DYNSYM, SHT_RELA and SHT_REL entry size
    • all dynamic tag cases except the valid one
    • program header alignment
  • sh_link section header index out of bounds still passes without the PR. The fixture sets sh_name = 21, which is beyond the 17-byte .shstrtab, so main already rejects that section for its name. The test never reaches the new sh_link check.
  • The ELF header cases also pass on main: header size, SELF magic, ELF magic, class, encoding, version, machine, object version and phentsize. They test behaviour that already exists, which is fine for an audit, but they are not evidence for the code this PR adds.

Afterwards I restored the merged files and rebuilt. git status --porcelain is empty, and elf_reader_bounds passes again.

Findings

Blocking

  1. guest_load_ranges regresses (core/relinker/relinker/src/parsing/ElfReader.cpp:147-150).
    • ReadProgramHeaders now throws Program header alignment is not a power of two for every program header, including empty PT_LOAD segments.
    • GuestImageReader (core/relinker/relinker/src/guest/GuestImageReader.cpp:51) already rejects these segments with Invalid load segment alignment, and core/relinker/relinker/tests/GuestLoadRangesTests.cpp:60-64 expects that message.
    • The new check runs first, changes the error the user sees, and fails the test.
    • Fix: either drop the duplicate check, or keep it and update GuestLoadRangesTests.cpp, explaining in the PR why the error moves to ElfReader.
  2. optional_rela regresses, and DT_REL handling contradicts the pipeline (ElfReader.cpp:322-336).
    • The relinker does not support DT_REL: RelinkerPipeline.cpp:249-250, RelativeRelocationIndex.cpp:214-215, GuestImageReader.cpp:253 and StrictNidFilter.cpp:45 all throw DT_REL relocations are not supported, and test_optional_rela.py:126-127 checks that message.
    • The new DT_RELSZ and DT_RELENT size checks run earlier in ReadDynamicTags, so a DT_RELSZ of 24 now fails with Invalid DT_RELSZ value instead.
    • The new DT_PLTREL check accepts 17 (DT_REL), while the pipeline accepts only DT_RELA (7) (RelinkerPipeline.cpp:242, RelativeRelocationIndex.cpp:309).
    • Fix: drop the DT_REL, DT_RELSZ and DT_RELENT checks and the 17 exception. Validation of DT_RELA, DT_RELASZ and DT_RELAENT should also stay in one place: the pipeline already reports Unsupported DT_RELAENT and Invalid DT_RELASZ (test_optional_rela.py:119-120).
  3. The sh_link test does not cover the new check (core/relinker/relinker/tests/ElfReaderBoundsTests.cpp:470-476).
    • As the negative control shows, it passes on main, because sh_name = 21 is already out of bounds.
    • Fix: keep a valid sh_name and change only sh_link. Also assert the message Section link index out of bounds, as the other new Reject* cases do.
  4. The description does not match the change, and the PR has more than one topic.
    • The title and the "What" section describe an ELF header audit plus segment alignment.
    • The diff also adds section-header checks (link, alignment, DYNSYM/RELA/REL entry sizes) and seven dynamic-tag checks, all of which change behaviour.
    • "Tested" names only ElfReaderBoundsTests.cpp. The two other relinker tests this change breaks were evidently not run.
    • CONTRIBUTING asks for one topic per PR and for the tests to be run before opening.
    • Fix: split the PR, or update the description to list every change, and run the full relinker test set.

Non-blocking

  • ElfReader.cpp:322, :334: the literal 16 for REL entries, and the literals 24, 7 and 17, duplicate constants that exist or could exist next to kRelaEntrySize and kSymEntrySize. This becomes moot if the DT_REL checks are dropped (blocking finding 2).
  • ElfReader.cpp:233: the offset 0x28 is written as a literal, while the neighbouring checks use named offsets (kShdrAlignOffset, kShdrEntSizeOffset).
  • The debt entry in docs/dev/debt/relinker.md lists "dynamic entry size and alignment boundaries" as unmeasured on the console. If the DT_REL checks go, cut the entry to what remains.

Not verified

  • I did not run the full test suite, only the selected tests (by the script) and the focused relinker tests above.
  • I did not run the relinker on real title modules, so I cannot say whether retail modules contain non-power-of-two p_align or sh_addralign values, or section links ≥ e_shnum, which the new checks would now reject.
  • No Vulkan validation applies: no AGC driver, shader recompiler or VideoOut files are changed.
  • I did not build on Windows or macOS.

To get this to a good state

  1. Make guest_load_ranges and optional_rela pass. Either remove the checks that duplicate validation done later in the pipeline (DT_REL*, DT_PLTREL, DT_RELA*, and PT_LOAD alignment), or move the expectations deliberately and explain why in the PR.
  2. Rewrite the sh_link test so that it fails without the new check.
  3. Narrow the PR to one topic, or update the description to cover every behaviour change, and list all the relinker tests that were run.
<!-- greyfinch-sync:boykopovar/AnyPS5:review:5761192378 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/15651#pullrequestreview-5761192378). Originally posted by [XairRrR](https://github.com/XairRrR) on 2026-11-03T23:57:53Z. **Review:** DISMISSED --- # Reviewed — changes requested The new `ElfReader` checks break two existing relinker tests. `guest_load_ranges` fails because a program-header alignment error now has a different message. `optional_rela` fails because a `DT_RELSZ` error now has a different message. Both tests pass again when the production files are restored from `main`. ## What I ran Head `c2d64c45a5b12aab76be11c85e23d162f603358e`, `main` `1c8c0efc8c41314402e6966da5bef6ea3d639d10`, merged into `main` with `--no-ff` (merged tree `a0c8808eea49c475f5f8d1be621388c795198863`), Release build with GCC 14.2.0 and Ninja, Debian 13. ``` git diff --check 1c8c0efc8c41314402e6966da5bef6ea3d639d10...c2d64c45a5b12aab76be11c85e23d162f603358e python3 tools/check_conventions.py --base 1c8c0efc8c41314402e6966da5bef6ea3d639d10 ctest --test-dir build -j8 --test-load 32 --output-on-failure -R "^(guest_load_ranges|optional_rela|elf_reader_bounds|dynamic_table_bounds|rela_bounds|reloc_table_bounds|jmprel_table_bounds)$" # negative control git show 1c8c0efc8c41314402e6966da5bef6ea3d639d10:core/relinker/relinker/src/parsing/ElfReader.cpp > core/relinker/relinker/src/parsing/ElfReader.cpp git show 1c8c0efc8c41314402e6966da5bef6ea3d639d10:core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp > core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp ninja -C build -j32 -l32 elf_reader_bounds_tests guest_load_ranges_tests relinker ctest --test-dir build --output-on-failure -R "^(elf_reader_bounds|guest_load_ranges|optional_rela)$" # restore the merged tree git show a0c8808eea49c475f5f8d1be621388c795198863:<both paths> > <both paths> ninja -C build -j32 -l32 elf_reader_bounds_tests guest_load_ranges_tests relinker git status --porcelain ``` ## Results | Check | Result | |---|---| | `git diff --check` | clean | | `check_conventions.py` | All conventions checks passed | | Configure / build / libs | OK | | Selected tests (793 run, script) | 787 passed, 3 expected skips. 2 new failures: `guest_load_ranges`, `optional_rela`. 1 known failure on `main`: `unwrap_self_..._group` | | `elf_reader_bounds` | passed | | `dynamic_table_bounds`, `rela_bounds`, `reloc_table_bounds`, `jmprel_table_bounds` | passed | | `guest_load_ranges` | **failed**, and passes with `main`'s production files | | `optional_rela` | **failed**, and passes with `main`'s production files | <details><summary>guest_load_ranges</summary> ``` empty-last-alignment: Expected Invalid load segment alignment, got Program header alignment is not a power of two empty-first-alignment: Expected Invalid load segment alignment, got Program header alignment is not a power of two ``` </details> <details><summary>optional_rela</summary> ``` AssertionError: ('unsupported-18', False, 'linux', 2, 'Converting False-unsupported-18.elf\nSystem: Linux; unused-filter=0\n\n', 'FAIL: Invalid DT_RELSZ value: 24 bytes; expected a multiple of 16 bytes (offset 0x450)\n...') ``` </details> The failure baseline came from an older `main` (`1e658521a0`). The negative control removes that doubt: with only `ElfReader.cpp` and `ElfConstants.hpp` restored from `main` (`1c8c0efc8c`), both tests pass. The failures therefore come from this PR. ## Negative control I restored `core/relinker/relinker/src/parsing/ElfReader.cpp` and `core/relinker/elfpatcher/include/elfpatcher/general/ElfConstants.hpp` from `main` and rebuilt. - `elf_reader_bounds` fails in these cases: - section alignment - `SHT_DYNSYM`, `SHT_RELA` and `SHT_REL` entry size - all dynamic tag cases except the valid one - program header alignment - **`sh_link section header index out of bounds` still passes without the PR.** The fixture sets `sh_name = 21`, which is beyond the 17-byte `.shstrtab`, so `main` already rejects that section for its name. The test never reaches the new `sh_link` check. - The ELF header cases also pass on `main`: header size, SELF magic, ELF magic, class, encoding, version, machine, object version and phentsize. They test behaviour that already exists, which is fine for an audit, but they are not evidence for the code this PR adds. Afterwards I restored the merged files and rebuilt. `git status --porcelain` is empty, and `elf_reader_bounds` passes again. ## Findings ### Blocking 1. **`guest_load_ranges` regresses** (`core/relinker/relinker/src/parsing/ElfReader.cpp:147-150`). - `ReadProgramHeaders` now throws `Program header alignment is not a power of two` for every program header, including empty `PT_LOAD` segments. - `GuestImageReader` (`core/relinker/relinker/src/guest/GuestImageReader.cpp:51`) already rejects these segments with `Invalid load segment alignment`, and `core/relinker/relinker/tests/GuestLoadRangesTests.cpp:60-64` expects that message. - The new check runs first, changes the error the user sees, and fails the test. - Fix: either drop the duplicate check, or keep it and update `GuestLoadRangesTests.cpp`, explaining in the PR why the error moves to `ElfReader`. 2. **`optional_rela` regresses, and `DT_REL` handling contradicts the pipeline** (`ElfReader.cpp:322-336`). - The relinker does not support `DT_REL`: `RelinkerPipeline.cpp:249-250`, `RelativeRelocationIndex.cpp:214-215`, `GuestImageReader.cpp:253` and `StrictNidFilter.cpp:45` all throw `DT_REL relocations are not supported`, and `test_optional_rela.py:126-127` checks that message. - The new `DT_RELSZ` and `DT_RELENT` size checks run earlier in `ReadDynamicTags`, so a `DT_RELSZ` of 24 now fails with `Invalid DT_RELSZ value` instead. - The new `DT_PLTREL` check accepts `17` (`DT_REL`), while the pipeline accepts only `DT_RELA (7)` (`RelinkerPipeline.cpp:242`, `RelativeRelocationIndex.cpp:309`). - Fix: drop the `DT_REL`, `DT_RELSZ` and `DT_RELENT` checks and the `17` exception. Validation of `DT_RELA`, `DT_RELASZ` and `DT_RELAENT` should also stay in one place: the pipeline already reports `Unsupported DT_RELAENT` and `Invalid DT_RELASZ` (`test_optional_rela.py:119-120`). 3. **The `sh_link` test does not cover the new check** (`core/relinker/relinker/tests/ElfReaderBoundsTests.cpp:470-476`). - As the negative control shows, it passes on `main`, because `sh_name = 21` is already out of bounds. - Fix: keep a valid `sh_name` and change only `sh_link`. Also assert the message `Section link index out of bounds`, as the other new `Reject*` cases do. 4. **The description does not match the change, and the PR has more than one topic.** - The title and the "What" section describe an ELF header audit plus segment alignment. - The diff also adds section-header checks (link, alignment, `DYNSYM`/`RELA`/`REL` entry sizes) and seven dynamic-tag checks, all of which change behaviour. - "Tested" names only `ElfReaderBoundsTests.cpp`. The two other relinker tests this change breaks were evidently not run. - CONTRIBUTING asks for one topic per PR and for the tests to be run before opening. - Fix: split the PR, or update the description to list every change, and run the full relinker test set. ### Non-blocking - `ElfReader.cpp:322`, `:334`: the literal `16` for REL entries, and the literals `24`, `7` and `17`, duplicate constants that exist or could exist next to `kRelaEntrySize` and `kSymEntrySize`. This becomes moot if the `DT_REL` checks are dropped (blocking finding 2). - `ElfReader.cpp:233`: the offset `0x28` is written as a literal, while the neighbouring checks use named offsets (`kShdrAlignOffset`, `kShdrEntSizeOffset`). - The debt entry in `docs/dev/debt/relinker.md` lists "dynamic entry size and alignment boundaries" as unmeasured on the console. If the `DT_REL` checks go, cut the entry to what remains. ## Not verified - I did not run the full test suite, only the selected tests (by the script) and the focused relinker tests above. - I did not run the relinker on real title modules, so I cannot say whether retail modules contain non-power-of-two `p_align` or `sh_addralign` values, or section links ≥ `e_shnum`, which the new checks would now reject. - No Vulkan validation applies: no AGC driver, shader recompiler or VideoOut files are changed. - I did not build on Windows or macOS. ## To get this to a good state 1. Make `guest_load_ranges` and `optional_rela` pass. Either remove the checks that duplicate validation done later in the pipeline (`DT_REL*`, `DT_PLTREL`, `DT_RELA*`, and `PT_LOAD` alignment), or move the expectations deliberately and explain why in the PR. 2. Rewrite the `sh_link` test so that it fails without the new check. 3. Narrow the PR to one topic, or update the description to cover every behaviour change, and list all the relinker tests that were run. <!-- anyps5-bots job=3723 head=c2d64c45a5b12aab76be11c85e23d162f603358e -->
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!15651
No description provided.