fix(relinker): validate ELF program header table bounds #14001

Closed
bro4cvf-hash wants to merge 1 commit from upstream-pr/14001 into main
bro4cvf-hash commented 2026-10-31 15:07:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by bro4cvf-hash on 2026-10-31T15:07:19Z.


What

Validate ELF program header table bounds before reading headers in ElfReader. Throw RelinkerException ("Program header table out of bounds") if the program header table offset and total size exceed the file buffer, and add unit tests covering program header table truncation and offset overflow.

Tested

Windows 11 (x86_64), ctest --test-dir build-relinker -R elf_reader_bounds -V:
All unit tests in elf_reader_bounds_tests passed (100% tests passed, 0 failures), including the new test cases:

  • program header table truncation
  • program header table offset overflow

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:5098753891 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14001). Originally posted by [bro4cvf-hash](https://github.com/bro4cvf-hash) on 2026-10-31T15:07:19Z. --- ### What Validate ELF program header table bounds before reading headers in `ElfReader`. Throw `RelinkerException` ("Program header table out of bounds") if the program header table offset and total size exceed the file buffer, and add unit tests covering program header table truncation and offset overflow. ### Tested Windows 11 (x86_64), `ctest --test-dir build-relinker -R elf_reader_bounds -V`: All unit tests in `elf_reader_bounds_tests` passed (100% tests passed, 0 failures), including the new test cases: - `program header table truncation` - `program header table offset overflow` ### Checklist <!-- Rules behind each item: https://github.com/boykopovar/AnyPS5/blob/main/CONTRIBUTING.md --> - [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
MarcZX10 commented 2026-10-31 15:09:27 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T15:09:27Z.

Review: APPROVED


Reviewed — looks good

At head fc3ad26b7c71af11d80d6d8eddc76f9d4239f527 against base 73f4f7e06e4f210a8c6570e335fd654dc0a86134, the new program-header range check uses the existing overflow-safe _rangeFits helper, and the tests cover a truncated table and an out-of-file offset (implementation, tests). git diff --check, conventions/template validation, and the current-base merge preview passed. The Windows CTest result in the PR description is author-reported; I did not build or run the test locally, and no product build job is present in the current hosted check set.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5723471089 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14001#pullrequestreview-5723471089). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T15:09:27Z. **Review:** APPROVED --- # Reviewed — looks good At head `fc3ad26b7c71af11d80d6d8eddc76f9d4239f527` against base `73f4f7e06e4f210a8c6570e335fd654dc0a86134`, the new program-header range check uses the existing overflow-safe `_rangeFits` helper, and the tests cover a truncated table and an out-of-file offset ([implementation](https://github.com/boykopovar/AnyPS5/blob/fc3ad26b7c71af11d80d6d8eddc76f9d4239f527/core/relinker/relinker/src/parsing/ElfReader.cpp#L128-L132), [tests](https://github.com/boykopovar/AnyPS5/blob/fc3ad26b7c71af11d80d6d8eddc76f9d4239f527/core/relinker/relinker/tests/ElfReaderBoundsTests.cpp#L109-L119)). `git diff --check`, conventions/template validation, and the current-base merge preview passed. The Windows CTest result in the PR description is author-reported; I did not build or run the test locally, and no product build job is present in the current hosted check set.
MarcZX10 commented 2026-10-31 15:29:59 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-31T15:29:59Z.

Review: APPROVED


Reviewed — looks good

The change validates the complete program-header table range before reading entries and adds focused cases for truncation and an extreme offset; I found no blocking issue.

What I ran

Reviewed PR head fc3ad26b7c71af11d80d6d8eddc76f9d4239f527 with its reported base 73f4f7e06e4f210a8c6570e335fd654dc0a86134. The clean merge preview used current main 474030e58424fefdb65e5df9f3820810fbb4553c. Environment: Linux x86_64, Python 3.11.2, Git 2.39.5; no compiler or local test binary was used.

python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 14001
git diff --check 73f4f7e06e4f210a8c6570e335fd654dc0a86134...fc3ad26b7c71af11d80d6d8eddc76f9d4239f527
python3 tools/pr_stack_check.py 14001 --repo boykopovar/AnyPS5
gh api repos/boykopovar/AnyPS5/commits/fc3ad26b7c71af11d80d6d8eddc76f9d4239f527/check-runs --jq '.check_runs[] | [.name,.status,(.conclusion // "pending")] | @tsv'

I also inspected the range helper, validation order, added test cases, and existing CTest registration.

Results

Check Result
Diff whitespace Passed
Repository conventions and PR template Passed
Test registration Existing elf_reader_bounds CTest target includes this test executable
Merge preview against current main Clean
Hosted conventions, intake, labels Passed
Hosted build and relinker jobs No current build run is present in the check snapshot
Test-quality heuristic Could not execute because its helper was absent from the isolated snapshot
Author-reported targeted test Windows 11: ctest --test-dir build-relinker -R elf_reader_bounds -V reported all tests in elf_reader_bounds_tests passed; I did not independently run it
Unedited check output

Diff check exited 0 with no output.

Conventions:

All conventions checks passed.

CTest registration:

73f4f7e06e4f210a8c6570e335fd654dc0a86134:core/relinker/CMakeLists.txt:264:    add_test_executable(elf_reader_bounds_tests
73f4f7e06e4f210a8c6570e335fd654dc0a86134:core/relinker/CMakeLists.txt:267:    target_include_directories(elf_reader_bounds_tests PRIVATE
73f4f7e06e4f210a8c6570e335fd654dc0a86134:core/relinker/CMakeLists.txt:271:    add_test(NAME elf_reader_bounds COMMAND elf_reader_bounds_tests)
73f4f7e06e4f210a8c6570e335fd654dc0a86134:core/relinker/CMakeLists.txt:272:    set_tests_properties(elf_reader_bounds PROPERTIES TIMEOUT 20)

Merge preview:

- PR #14001: **clean** against current base; head `fc3ad26b7c71af11d80d6d8eddc76f9d4239f527`; changed paths: none reported

No declared PR dependencies were found in the target PR chain.

Hosted check runs:

labels	completed	success
conventions	completed	success
conventions	completed	cancelled
labels	completed	success
intake	completed	success
labels	completed	success
conventions	completed	success

Test-quality helper:

/usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-14001-fc3ad26b7c71/head-fc3ad26b7c71-2d2cae77/tools/pr_test_quality.py': [Errno 2] No such file or directory

Findings

Blocking: None.

Sources

Not verified

I did not build or run CTest locally. The targeted Windows result is author-reported; hosted build jobs were not present in this check snapshot.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5723552867 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/14001#pullrequestreview-5723552867). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-31T15:29:59Z. **Review:** APPROVED --- # Reviewed — looks good The change validates the complete program-header table range before reading entries and adds focused cases for truncation and an extreme offset; I found no blocking issue. ## What I ran Reviewed PR head `fc3ad26b7c71af11d80d6d8eddc76f9d4239f527` with its reported base `73f4f7e06e4f210a8c6570e335fd654dc0a86134`. The clean merge preview used current `main` `474030e58424fefdb65e5df9f3820810fbb4553c`. Environment: Linux x86_64, Python 3.11.2, Git 2.39.5; no compiler or local test binary was used. ```sh python3 tools/pr_precheck.py --repo boykopovar/AnyPS5 --pr 14001 git diff --check 73f4f7e06e4f210a8c6570e335fd654dc0a86134...fc3ad26b7c71af11d80d6d8eddc76f9d4239f527 python3 tools/pr_stack_check.py 14001 --repo boykopovar/AnyPS5 gh api repos/boykopovar/AnyPS5/commits/fc3ad26b7c71af11d80d6d8eddc76f9d4239f527/check-runs --jq '.check_runs[] | [.name,.status,(.conclusion // "pending")] | @tsv' ``` I also inspected the range helper, validation order, added test cases, and existing CTest registration. ## Results | Check | Result | |---|---| | Diff whitespace | Passed | | Repository conventions and PR template | Passed | | Test registration | Existing `elf_reader_bounds` CTest target includes this test executable | | Merge preview against current `main` | Clean | | Hosted conventions, intake, labels | Passed | | Hosted build and relinker jobs | No current build run is present in the check snapshot | | Test-quality heuristic | Could not execute because its helper was absent from the isolated snapshot | | Author-reported targeted test | Windows 11: `ctest --test-dir build-relinker -R elf_reader_bounds -V` reported all tests in `elf_reader_bounds_tests` passed; I did not independently run it | <details><summary>Unedited check output</summary> Diff check exited 0 with no output. Conventions: ```text All conventions checks passed. ``` CTest registration: ```text 73f4f7e06e4f210a8c6570e335fd654dc0a86134:core/relinker/CMakeLists.txt:264: add_test_executable(elf_reader_bounds_tests 73f4f7e06e4f210a8c6570e335fd654dc0a86134:core/relinker/CMakeLists.txt:267: target_include_directories(elf_reader_bounds_tests PRIVATE 73f4f7e06e4f210a8c6570e335fd654dc0a86134:core/relinker/CMakeLists.txt:271: add_test(NAME elf_reader_bounds COMMAND elf_reader_bounds_tests) 73f4f7e06e4f210a8c6570e335fd654dc0a86134:core/relinker/CMakeLists.txt:272: set_tests_properties(elf_reader_bounds PROPERTIES TIMEOUT 20) ``` Merge preview: ```text - PR #14001: **clean** against current base; head `fc3ad26b7c71af11d80d6d8eddc76f9d4239f527`; changed paths: none reported No declared PR dependencies were found in the target PR chain. ``` Hosted check runs: ```text labels completed success conventions completed success conventions completed cancelled labels completed success intake completed success labels completed success conventions completed success ``` Test-quality helper: ```text /usr/bin/python3: can't open file '/tmp/anyps5-pr-precheck/boykopovar-AnyPS5-pr-14001-fc3ad26b7c71/head-fc3ad26b7c71-2d2cae77/tools/pr_test_quality.py': [Errno 2] No such file or directory ``` </details> ## Findings **Blocking:** None. ## Sources - [The exact parser change](https://github.com/boykopovar/AnyPS5/blob/fc3ad26b7c71af11d80d6d8eddc76f9d4239f527/core/relinker/relinker/src/parsing/ElfReader.cpp#L123-L154) - [The added boundary cases](https://github.com/boykopovar/AnyPS5/blob/fc3ad26b7c71af11d80d6d8eddc76f9d4239f527/core/relinker/relinker/tests/ElfReaderBoundsTests.cpp#L107-L118) - [CTest registration at base 73f4f7e06e4f210a8c6570e335fd654dc0a86134](https://github.com/boykopovar/AnyPS5/blob/73f4f7e06e4f210a8c6570e335fd654dc0a86134/core/relinker/CMakeLists.txt#L264-L272) ## Not verified I did not build or run CTest locally. The targeted Windows result is author-reported; hosted build jobs were not present in this check snapshot.

Pull request closed

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!14001
No description provided.