fix(tools): validate ELF import table file ranges #10352

Merged
Bram910 merged 1 commit from upstream-pr/10352 into main 2026-10-26 00:31:44 +00:00
Bram910 commented 2026-10-23 18:51:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Bram910 on 2026-10-23T18:51:42Z.


What

Reject malformed ELF import metadata before reporting a successful audit. Check the complete PT_DYNAMIC and SHT_DYNAMIC file ranges before walking their entries, even when an early DT_NULL would hide a truncated declared end. Also check the SHT_DYNSYM import range when it contains no entries, matching the existing export-reader validation.

The ELF ABI defines file-backed ranges through p_offset/p_filesz and sh_offset/sh_size. Valid tables ending at EOF, zero-sized symbol tables at EOF and ignored entries after DT_NULL retain their behavior. Errors use the existing file-qualified AuditError diagnostics and leave a previous JSON report unchanged.

This is independent of #10311, which rejects missing DT_NULL terminators. That behavior is deliberately not included here; #10311 still accepts the invalid ranges covered by this PR.

Tested

  • Based on main e78acf4bda; tested PR head 5bf97cce6b. Publication recheck against newer main ad19afc31e: audit source/tests are unchanged upstream and the branch merges cleanly. No title, GPU, macOS or full-runtime run; this changes Python tooling only.
  • Negative controls: the three rejecting/report-preservation methods produce 14 failed subtests against main's unchanged audit parser. The same controls produce 14 failures against #10311's parser at 66b5034fd3. All five new methods pass with this fix.
  • Windows x64, Python 3.13.5, MinGW-w64 GCC 14.1.0 UCRT, Release, ANYPS5_RELINKER_ONLY=ON, BUILD_TESTING=ON: cmake --build build-relinker --parallel 4 and ctest --test-dir build-relinker --output-on-failure --parallel 4; 235/235 CTest entries pass. Independently, python tools/tests/test_import_audit.py -v runs 131 tests successfully, with three existing platform/permission skips (two POSIX permission cases and Windows symlink creation without the required privilege).
  • Ubuntu 22.04 under WSL, Python 3.10.12: python3 tools/tests/test_import_audit.py -v runs 131 tests successfully, with the existing Windows-only skip.
  • Real GCC 11.4.0 shared libraries with SysV-only, GNU-only and combined hash tables: imports, exports and dependencies match in all six sectioned/sectionless views. All six oversized dynamic ranges and all three out-of-file, zero-sized sectioned symbol-table ranges are rejected.
  • Tests cover zero-length symbol tables at EOF versus outside the file, extreme 64-bit offsets/sizes, truncated dynamic tables with early terminators, valid EOF ranges, ignored post-terminator entries and preservation of previous JSON output.
  • Compatibility with #10311 at 66b5034fd3: both merge orders succeed with the same resulting tree. The combined parser/test modules run all 133 tests on Windows (three skips) and Linux (one skip) successfully in a Git-blob loading harness. Its subprocess UTF-8 smoke test uses this branch's on-disk CLI; the parser/API and in-process command tests use the merged module.
  • python tools/check_conventions.py --base origin/main and git diff --check pass.
  • tools/pr_overlap.py --branch fix/audit-dynamic-tables reports no confirmed conflicts. It cannot compare #6171 and #7454 because they already conflict with main. Their current shared-file patches were reviewed manually: #6171 adds module identities, including adjacent dynamic-loop additions; #7454 changes dependency-name resolution. Neither validates these file ranges. This does not claim their existing base conflicts are resolved.

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, Codex
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:4987756651 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10352). Originally posted by [Bram910](https://github.com/Bram910) on 2026-10-23T18:51:42Z. --- ### What Reject malformed ELF import metadata before reporting a successful audit. Check the complete `PT_DYNAMIC` and `SHT_DYNAMIC` file ranges before walking their entries, even when an early `DT_NULL` would hide a truncated declared end. Also check the `SHT_DYNSYM` import range when it contains no entries, matching the existing export-reader validation. The ELF ABI defines file-backed ranges through [`p_offset`/`p_filesz`](https://gabi.xinuos.com/elf/07-pheader.html) and [`sh_offset`/`sh_size`](https://gabi.xinuos.com/elf/03-sheader.html). Valid tables ending at EOF, zero-sized symbol tables at EOF and ignored entries after `DT_NULL` retain their behavior. Errors use the existing file-qualified `AuditError` diagnostics and leave a previous JSON report unchanged. This is independent of #10311, which rejects missing `DT_NULL` terminators. That behavior is deliberately not included here; #10311 still accepts the invalid ranges covered by this PR. ### Tested <!-- OS, title or homebrew, what was checked. "Not tested" is a valid answer. A performance change: the summary lines of main and of this PR from the same machine and scene, with the traces attached, captured as the Performance report issue form describes. --> - Based on `main` `e78acf4bda`; tested PR head `5bf97cce6b`. Publication recheck against newer `main` `ad19afc31e`: audit source/tests are unchanged upstream and the branch merges cleanly. No title, GPU, macOS or full-runtime run; this changes Python tooling only. - Negative controls: the three rejecting/report-preservation methods produce 14 failed subtests against main's unchanged audit parser. The same controls produce 14 failures against #10311's parser at `66b5034fd3`. All five new methods pass with this fix. - Windows x64, Python 3.13.5, MinGW-w64 GCC 14.1.0 UCRT, Release, `ANYPS5_RELINKER_ONLY=ON`, `BUILD_TESTING=ON`: `cmake --build build-relinker --parallel 4` and `ctest --test-dir build-relinker --output-on-failure --parallel 4`; 235/235 CTest entries pass. Independently, `python tools/tests/test_import_audit.py -v` runs 131 tests successfully, with three existing platform/permission skips (two POSIX permission cases and Windows symlink creation without the required privilege). - Ubuntu 22.04 under WSL, Python 3.10.12: `python3 tools/tests/test_import_audit.py -v` runs 131 tests successfully, with the existing Windows-only skip. - Real GCC 11.4.0 shared libraries with SysV-only, GNU-only and combined hash tables: imports, exports and dependencies match in all six sectioned/sectionless views. All six oversized dynamic ranges and all three out-of-file, zero-sized sectioned symbol-table ranges are rejected. - Tests cover zero-length symbol tables at EOF versus outside the file, extreme 64-bit offsets/sizes, truncated dynamic tables with early terminators, valid EOF ranges, ignored post-terminator entries and preservation of previous JSON output. - Compatibility with #10311 at `66b5034fd3`: both merge orders succeed with the same resulting tree. The combined parser/test modules run all 133 tests on Windows (three skips) and Linux (one skip) successfully in a Git-blob loading harness. Its subprocess UTF-8 smoke test uses this branch's on-disk CLI; the parser/API and in-process command tests use the merged module. - `python tools/check_conventions.py --base origin/main` and `git diff --check` pass. - `tools/pr_overlap.py --branch fix/audit-dynamic-tables` reports no confirmed conflicts. It cannot compare #6171 and #7454 because they already conflict with main. Their current shared-file patches were reviewed manually: #6171 adds module identities, including adjacent dynamic-loop additions; #7454 changes dependency-name resolution. Neither validates these file ranges. This does not claim their existing base conflicts are resolved. ### 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 <!-- #PR, or none --> - [x] Fixes: none <!-- issue it closes as Fixes #N, or none --> - [x] Unblocks: none <!-- title ID and Game bring-up discussion, e.g. PPSA01325 #3232, or none --> - [x] GPUs tested: n/a <!-- for AGC, shader recompiler or VideoOut changes: vendor, model, driver and OS of each GPU the GPU tests ran on; otherwise n/a --> - [x] AI-assisted: yes, Codex <!-- yes / no -->
MarcZX10 commented 2026-10-23 19:04:42 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-23T19:04:42Z.

Review: APPROVED


Reviewed the parser paths and added boundary cases. The new take calls validate the entire declared file-backed range before iterating dynamic entries or accepting an empty symbol table, while preserving valid EOF-sized ranges and post-DT_NULL behavior. The tests exercise oversized/truncated ranges, zero-sized out-of-file tables, previous-report preservation, and compatibility with an early terminator. Reported Windows/Linux Python tests and Windows relinker CTest passed; current hosted eligibility, conventions, intake, labels and relinker matrix also pass. Product build jobs are skipped because this is tooling-only. I found no correctness issue in the changed paths.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5633204688 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10352#pullrequestreview-5633204688). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-23T19:04:42Z. **Review:** APPROVED --- Reviewed the parser paths and added boundary cases. The new `take` calls validate the entire declared file-backed range before iterating dynamic entries or accepting an empty symbol table, while preserving valid EOF-sized ranges and post-`DT_NULL` behavior. The tests exercise oversized/truncated ranges, zero-sized out-of-file tables, previous-report preservation, and compatibility with an early terminator. Reported Windows/Linux Python tests and Windows relinker CTest passed; current hosted eligibility, conventions, intake, labels and relinker matrix also pass. Product build jobs are skipped because this is tooling-only. I found no correctness issue in the changed paths.
THEMANnt commented 2026-10-24 23:06:36 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by THEMANnt on 2026-10-24T23:06:36Z.

Review: APPROVED


Reviewed 5bf97cce6b2a against author merge base e78acf4bdac0. No actionable findings in this focused review.

The three added range checks validate the complete declared file extent before any early DT_NULL can stop iteration, and cover empty dynamic symbol tables whose offsets still lie beyond EOF. The existing take bounds check allows exact EOF endpoints and uses Python integer arithmetic for extreme 64-bit values. Import callers retain the same error propagation, and malformed inputs reach the existing diagnostic path before a previous JSON report is replaced. This matches the file-backed range definitions in the ELF program-header specification and section-header specification.

On Windows x64 / CPython 3.11, python -X utf8 tools/tests/test_import_audit.py -v ran 131 methods successfully with three skips (128 passed). The unchanged merge-base suite ran 126 with three skips (123 passed). Grafting only the new tests onto baseline production produced 14 expected assertion failures, zero errors, and three skips: invalid ranges that the old parser accepted are rejected at the head, while valid EOF/terminated-padding cases remain accepted. Conventions, whitespace and test registration passed.

GitHub reports successful relinker matrix/ASan, conventions and intake checks. Full product jobs were skipped; no full native build, Linux/macOS local execution or title run is claimed. Validation covers the Python parser and CLI behavior.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5640507386 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/10352#pullrequestreview-5640507386). Originally posted by [THEMANnt](https://github.com/THEMANnt) on 2026-10-24T23:06:36Z. **Review:** APPROVED --- Reviewed [5bf97cce6b2a](https://github.com/boykopovar/AnyPS5/commit/5bf97cce6b2a011a75b7893cc4514da51433466a) against author merge base [e78acf4bdac0](https://github.com/boykopovar/AnyPS5/commit/e78acf4bdac085393eb8365785d97f2df1faab27). No actionable findings in this focused review. The three added range checks validate the complete declared file extent before any early `DT_NULL` can stop iteration, and cover empty dynamic symbol tables whose offsets still lie beyond EOF. The existing `take` bounds check allows exact EOF endpoints and uses Python integer arithmetic for extreme 64-bit values. Import callers retain the same error propagation, and malformed inputs reach the existing diagnostic path before a previous JSON report is replaced. This matches the file-backed range definitions in the [ELF program-header specification](https://gabi.xinuos.com/elf/07-pheader.html) and [section-header specification](https://gabi.xinuos.com/elf/03-sheader.html). On Windows x64 / CPython 3.11, `python -X utf8 tools/tests/test_import_audit.py -v` ran 131 methods successfully with three skips (128 passed). The unchanged merge-base suite ran 126 with three skips (123 passed). Grafting only the new tests onto baseline production produced 14 expected assertion failures, zero errors, and three skips: invalid ranges that the old parser accepted are rejected at the head, while valid EOF/terminated-padding cases remain accepted. Conventions, whitespace and test registration passed. GitHub reports successful relinker matrix/ASan, conventions and intake checks. Full product jobs were skipped; no full native build, Linux/macOS local execution or title run is claimed. Validation covers the Python parser and CLI behavior.
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!10352
No description provided.