fix(relinker): validate ELF identification before parsing #2262

Merged
NumberGuan merged 1 commit from upstream-pr/2262 into main 2026-10-10 04:46:17 +00:00
NumberGuan commented 2026-10-09 13:45:32 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by NumberGuan on 2026-10-09T13:45:32Z.


What

ElfReader::ReadHeader checks the magic but reads all remaining fields using the ELF64 layout and host byte order regardless of EI_CLASS, EI_DATA and EI_VERSION. Linux conversion accepts a synthetic image declaring ELF32, big-endian encoding or an invalid identification version, and writes an executable and registry.

Require ELF64, little-endian encoding and identification version 1 before reading the remaining header fields. These identification bytes define how the file is interpreted in the ELF gABI. The check also runs before --to-intel processing. Windows already rejects these inputs later in WindowsLoadImage; the shared check now rejects them before registry output.

Extend the registered elf_header regression with 36 unsupported-identification cases and eight valid controls. It covers Linux and Windows output, with and without --to-intel, using the existing executable fixture. Invalid, unsupported and reserved values for each identification byte must exit 2 with the diagnostic and write neither executable nor registry; input bytes must stay unchanged. ELF64 little-endian inputs with System V and FreeBSD OSABI values still convert to the requested output format.

Tested

Windows x64, WinLibs MinGW-w64 GCC 15.2.0 UCRT POSIX SEH r7, Ninja Release, Python 3.12.14. Based on upstream d70b8998; contribution a213a26b.

cmake -S . -B build-relinker -G Ninja -DCMAKE_BUILD_TYPE=Release -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON
cmake --build build-relinker --parallel 4
ctest --test-dir build-relinker --output-on-failure --timeout 120 --no-tests=error --parallel 2
python tools/check_test_registration.py build-relinker
python tools/check_conventions.py --base origin/main
git diff origin/main --check

All 40 registered test groups passed in 76.20 seconds. Registration, conventions and whitespace checks passed. The existing tls_function_coverage test took 70.62 seconds; no repository timeout was changed.

With only the regression added to upstream d70b8998, the registered test fails: ident-4-0 returns 0 and writes an executable and registry. The two-line fix passes the focused test in 2.78 seconds, including the existing short-file and magic diagnostics.

Hosted relinker CI passed on Windows, Linux and macOS for a213a26b. Hosted conventions also passed. The full Linux and Windows build jobs were skipped by the PR workflow. System-library builds and game/PS5 tests were not run locally.

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; no silent stubs added
  • No notes, investigation files or images added to the repository
  • New third-party code is a submodule built from source; none added
  • Generic behaviour, not specific to one title
  • Depends on: none
  • AI-assisted: yes, Codex
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4801278409 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2262). Originally posted by [NumberGuan](https://github.com/NumberGuan) on 2026-10-09T13:45:32Z. --- ### What `ElfReader::ReadHeader` checks the magic but reads all remaining fields using the ELF64 layout and host byte order regardless of `EI_CLASS`, `EI_DATA` and `EI_VERSION`. Linux conversion accepts a synthetic image declaring ELF32, big-endian encoding or an invalid identification version, and writes an executable and registry. Require ELF64, little-endian encoding and identification version 1 before reading the remaining header fields. These identification bytes define how the file is interpreted in the [ELF gABI](https://gabi.xinuos.com/elf/02-eheader.html#elf-identification). The check also runs before `--to-intel` processing. Windows already rejects these inputs later in `WindowsLoadImage`; the shared check now rejects them before registry output. Extend the registered `elf_header` regression with 36 unsupported-identification cases and eight valid controls. It covers Linux and Windows output, with and without `--to-intel`, using the existing executable fixture. Invalid, unsupported and reserved values for each identification byte must exit 2 with the diagnostic and write neither executable nor registry; input bytes must stay unchanged. ELF64 little-endian inputs with System V and FreeBSD OSABI values still convert to the requested output format. ### Tested Windows x64, WinLibs MinGW-w64 GCC 15.2.0 UCRT POSIX SEH r7, Ninja Release, Python 3.12.14. Based on upstream `d70b8998`; contribution `a213a26b`. ```text cmake -S . -B build-relinker -G Ninja -DCMAKE_BUILD_TYPE=Release -DANYPS5_RELINKER_ONLY=ON -DBUILD_TESTING=ON cmake --build build-relinker --parallel 4 ctest --test-dir build-relinker --output-on-failure --timeout 120 --no-tests=error --parallel 2 python tools/check_test_registration.py build-relinker python tools/check_conventions.py --base origin/main git diff origin/main --check ``` All 40 registered test groups passed in 76.20 seconds. Registration, conventions and whitespace checks passed. The existing `tls_function_coverage` test took 70.62 seconds; no repository timeout was changed. With only the regression added to upstream `d70b8998`, the registered test fails: `ident-4-0` returns 0 and writes an executable and registry. The two-line fix passes the focused test in 2.78 seconds, including the existing short-file and magic diagnostics. Hosted [relinker CI](https://github.com/boykopovar/AnyPS5/actions/runs/37939147898) passed on Windows, Linux and macOS for `a213a26b`. Hosted [conventions](https://github.com/boykopovar/AnyPS5/actions/runs/37939147765) also passed. The full Linux and Windows build jobs were skipped by the PR workflow. System-library builds and game/PS5 tests were not run locally. ### 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 - [x] Unimplemented paths throw; no silent stubs added - [x] No notes, investigation files or images added to the repository - [x] New third-party code is a submodule built from source; none added - [x] Generic behaviour, not specific to one title - [x] Depends on: none - [x] AI-assisted: yes, Codex
MGXAe commented 2026-10-09 14:04:22 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MGXAe on 2026-10-09T14:04:22Z.


Independently confirmed on native Windows 11 (MinGW-w64 GCC 15.2.0 WinLibs r7): this PR's test_elf_header.py fails against main's relinker (ident-4-0 converts with exit 0) and passes with this change.

One thing to flag: #1087 (open since Oct 7) adds the same class, data-encoding and version checks in the same place in ElfReader::ReadHeader (plus machine, ELF version and header-size checks), so the two will most likely conflict. The checklist line "no other open PR implements the same functions" may need updating.

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6082510253 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2262#issuecomment-6082510253). Originally posted by [MGXAe](https://github.com/MGXAe) on 2026-10-09T14:04:22Z. --- Independently confirmed on native Windows 11 (MinGW-w64 GCC 15.2.0 WinLibs r7): this PR's `test_elf_header.py` fails against `main`'s relinker (`ident-4-0` converts with exit 0) and passes with this change. One thing to flag: #1087 (open since Oct 7) adds the same class, data-encoding and version checks in the same place in `ElfReader::ReadHeader` (plus machine, ELF version and header-size checks), so the two will most likely conflict. The checklist line "no other open PR implements the same functions" may need updating.
Dextroid17 commented 2026-10-09 14:47:08 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Dextroid17 on 2026-10-09T14:47:08Z.

Review: COMMENTED


Verified locally on Linux — this one needs no GPU.

  • base d70b8998 (upstream/main) + head a213a26b, merged clean.
  • Arm A (PR head): elf_header Passed (0.05 s); linux_eh_frame_header and linux_load_alignment pass too.
  • Arm B (only ElfReader.cpp reverted to main, the PR's test kept): elf_header fails, and the failing case is ident-4-0 —
    AssertionError: (0, 'ident-4-0', ...), i.e. on main the relinker returns 0 for EI_CLASS = 0, which is precisely what the new test rejects.
  • Independently of the test harness — /bin/true with e_ident[4] patched to 1, so a valid ELF64 that claims ELF32:
    • main: rc=0, wrote a 63,551-byte output and a registry — the "accepts a synthetic image declaring ELF32 and writes an executable and registry" case from the description, reproduced.
    • PR: rc=2, no output, no registry, FAIL: Expected little-endian ELF64 version 1.
  • No regression on real input: the decrypted Ghost of Yōtei eboot.bin still converts, rc=0, output 43,239,463 bytes — identical in size to the same conversion on main.

Pre-checks done before measuring: ReadHeader already validates size >= 0x40, the SELF magic and the ELF magic, so the new guard is not shadowed (the #1521 failure mode), and ReadHeader is genuinely reachable (ReadProgramHeaders, StrictNidFilter).

One design detail worth keeping: EI_OSABI is left unchecked, and the test pins that by requiring a FreeBSD-flagged image (e_ident[7] = 9) to still convert. That matters here — PS5 modules carry unusual identification (e_type 0xfe18 on a title's own libc.prx), so a stricter guard would have cost real inputs.

This is a relinker fix, so it does not need a named title; it is provable against the ELF gABI by itself.

What I could not verify: a Windows host, and ELF producers beyond /bin/true.

Written with AI assistance.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5471618753 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2262#pullrequestreview-5471618753). Originally posted by [Dextroid17](https://github.com/Dextroid17) on 2026-10-09T14:47:08Z. **Review:** COMMENTED --- Verified locally on Linux — this one needs no GPU. - base `d70b8998` (upstream/main) + head `a213a26b`, merged clean. - **Arm A (PR head):** `elf_header` **Passed (0.05 s)**; `linux_eh_frame_header` and `linux_load_alignment` pass too. - **Arm B (only `ElfReader.cpp` reverted to `main`, the PR's test kept):** `elf_header` **fails**, and the failing case is `ident-4-0` — `AssertionError: (0, 'ident-4-0', ...)`, i.e. on `main` the relinker returns `0` for `EI_CLASS = 0`, which is precisely what the new test rejects. - **Independently of the test harness** — `/bin/true` with `e_ident[4]` patched to `1`, so a valid ELF64 that *claims* ELF32: - `main`: `rc=0`, wrote a **63,551-byte** output **and** a registry — the "accepts a synthetic image declaring ELF32 and writes an executable and registry" case from the description, reproduced. - PR: `rc=2`, **no** output, **no** registry, `FAIL: Expected little-endian ELF64 version 1`. - **No regression on real input:** the decrypted Ghost of Yōtei `eboot.bin` still converts, `rc=0`, output **43,239,463 bytes** — identical in size to the same conversion on `main`. Pre-checks done before measuring: `ReadHeader` already validates `size >= 0x40`, the SELF magic and the ELF magic, so the new guard is **not shadowed** (the #1521 failure mode), and `ReadHeader` is genuinely reachable (`ReadProgramHeaders`, `StrictNidFilter`). One design detail worth keeping: `EI_OSABI` is left unchecked, and the test pins that by requiring a FreeBSD-flagged image (`e_ident[7] = 9`) to still convert. That matters here — PS5 modules carry unusual identification (`e_type` `0xfe18` on a title's own `libc.prx`), so a stricter guard would have cost real inputs. This is a relinker fix, so it does not need a named title; it is provable against the ELF gABI by itself. What I could **not** verify: a Windows host, and ELF producers beyond `/bin/true`. Written with AI assistance.
github-actions[bot] commented 2026-10-09 22:08:41 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by github-actions[bot] on 2026-10-09T22:08:41Z.


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
#1087 core/relinker/relinker/tests/test_elf_header.py L5, L20-22
<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6090071427 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/2262#issuecomment-6090071427). Originally posted by [github-actions[bot]](https://github.com/apps/github-actions) on 2026-10-09T22:08:41Z. --- <!-- 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 | |---|---|---|---| | #1087 | `core/relinker/relinker/tests/test_elf_header.py` [L5](https://github.com/boykopovar/AnyPS5/blob/698254f048952385120dba5b7af7658be2fd49e6/core/relinker/relinker/tests/test_elf_header.py#L5), [L20-22](https://github.com/boykopovar/AnyPS5/blob/698254f048952385120dba5b7af7658be2fd49e6/core/relinker/relinker/tests/test_elf_header.py#L20-L22) | | |
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!2262
No description provided.