fix(tools): bound PE audit directories by optional header size #7460

Merged
Lyrilika merged 1 commit from upstream-pr/7460 into main 2026-11-02 07:56:03 +00:00
Lyrilika commented 2026-10-18 08:27:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by Lyrilika on 2026-10-18T08:27:37Z.


What

import_audit.py checks that the optional header contains an export-directory slot, but does not check whether the declared data-directory count fits the header. A PE header with space for 16 directories and a count of 17 is accepted; the audit can succeed and replace an existing JSON report using malformed input. With a shorter header, the import reader can interpret section-table bytes as an import directory.

Validate the directory count against the remaining optional-header bytes in the shared PE header parser. Invalid counts now raise AuditError before either reader processes directories, so the CLI returns 2 and preserves the previous report. Variable-length PE32 and PE32+ headers remain supported. This follows Microsoft's optional-header bounds requirements.

Tested

Arch Linux x86-64, Python 3.14.8. Tested on main at e5c2b3c325e4. No game/homebrew execution, Windows runtime run or GPU testing.

  • New regression cases failed on the original implementation (18 failing subtests across two methods); valid variable-header cases already passed.
  • python3 tools/tests/test_import_audit.py: 109 tests run, 108 passed, one Windows-only test skipped. Covers PE32/PE32+, truncated directory arrays, a partial trailing slot, count overflow beyond a full header, maximum 32-bit count, valid shorter headers, CLI exit status and preservation of an existing report.
  • Relinker-only Release build succeeded; ctest --test-dir build-relinker --output-on-failure -j4: all 194 tests passed.
  • python3 tools/check_conventions.py --base origin/main and git diff --check: passed.
  • Repository overlap checker: no overlap with open PRs. Checked the prepared change against the 786 PR heads in the 2026-10-18 08:18 UTC snapshot; reviewed related import-audit changes separately.

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:4907590136 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7460). Originally posted by [Lyrilika](https://github.com/Lyrilika) on 2026-10-18T08:27:37Z. --- ### What `import_audit.py` checks that the optional header contains an export-directory slot, but does not check whether the declared data-directory count fits the header. A PE header with space for 16 directories and a count of 17 is accepted; the audit can succeed and replace an existing JSON report using malformed input. With a shorter header, the import reader can interpret section-table bytes as an import directory. Validate the directory count against the remaining optional-header bytes in the shared PE header parser. Invalid counts now raise `AuditError` before either reader processes directories, so the CLI returns 2 and preserves the previous report. Variable-length PE32 and PE32+ headers remain supported. This follows [Microsoft's optional-header bounds requirements](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#optional-header-image-only). ### 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. --> Arch Linux x86-64, Python 3.14.8. Tested on `main` at `e5c2b3c325e4`. No game/homebrew execution, Windows runtime run or GPU testing. - New regression cases failed on the original implementation (18 failing subtests across two methods); valid variable-header cases already passed. - `python3 tools/tests/test_import_audit.py`: 109 tests run, 108 passed, one Windows-only test skipped. Covers PE32/PE32+, truncated directory arrays, a partial trailing slot, count overflow beyond a full header, maximum 32-bit count, valid shorter headers, CLI exit status and preservation of an existing report. - Relinker-only Release build succeeded; `ctest --test-dir build-relinker --output-on-failure -j4`: all 194 tests passed. - `python3 tools/check_conventions.py --base origin/main` and `git diff --check`: passed. - Repository overlap checker: no overlap with open PRs. Checked the prepared change against the 786 PR heads in the 2026-10-18 08:18 UTC snapshot; reviewed related import-audit changes separately. ### 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 <!-- yes / no -->
xXPBSXx commented 2026-10-21 03:11:38 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T03:11:38Z.


Linux data point for 70986ca71f (WSL2, Ubuntu 24.04, kernel 6.18 - a virtualised kernel, not bare-metal Linux; Python 3.12.3), this head merged onto main 5d10ea9ea (clean merge), python3 -I tools/tests/test_import_audit.py:

  • With the PR: 124 (1 skipped: Windows read-only behaviour) tests OK.
  • Without it (the PR's test_import_audit.py on top of main without the PR's source change): FAILED (18 failures).
    Not tested: macOS, Windows (Windows data from another run is not part of this comment).

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6307083088 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7460#issuecomment-6307083088). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T03:11:38Z. --- Linux data point for `70986ca71f` (WSL2, Ubuntu 24.04, kernel 6.18 - a virtualised kernel, not bare-metal Linux; Python 3.12.3), this head merged onto `main` `5d10ea9ea` (clean merge), `python3 -I tools/tests/test_import_audit.py`: - With the PR: 124 (1 skipped: Windows read-only behaviour) tests OK. - Without it (the PR's `test_import_audit.py` on top of `main` without the PR's source change): FAILED (18 failures). Not tested: macOS, Windows (Windows data from another run is not part of this comment). AI-assisted (Claude Code).
xXPBSXx commented 2026-10-21 03:36:54 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by xXPBSXx on 2026-10-21T03:36:54Z.


Windows data point for 70986ca71f: Windows 10 19045, Python 3.12.10, WinLibs GCC 15.2.0 and Ninja on PATH, this head merged onto main 05f9995704 (clean merge), python -I tools/tests/test_import_audit.py:

  • With the PR: 124 tests OK, 3 skipped.
  • Without it (the PR's test_import_audit.py on top of main without the PR's source change): FAILED (18 failures).
    Not tested: Linux/macOS in this comment.

AI-assisted (Claude Code).

<!-- greyfinch-sync:boykopovar/AnyPS5:comment:6307413048 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7460#issuecomment-6307413048). Originally posted by [xXPBSXx](https://github.com/xXPBSXx) on 2026-10-21T03:36:54Z. --- Windows data point for `70986ca71f`: Windows 10 19045, Python 3.12.10, WinLibs GCC 15.2.0 and Ninja on PATH, this head merged onto `main` `05f9995704` (clean merge), `python -I tools/tests/test_import_audit.py`: - With the PR: 124 tests OK, 3 skipped. - Without it (the PR's `test_import_audit.py` on top of `main` without the PR's source change): FAILED (18 failures). Not tested: Linux/macOS in this comment. AI-assisted (Claude Code).
harisibrar1 commented 2026-10-21 05:32:37 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by harisibrar1 on 2026-10-21T05:32:37Z.

Review: APPROVED


Verified on Windows 11, Python 3.14:

  • python tools/tests/test_import_audit.py on the PR head: 109 tests, OK (3 skipped: two POSIX permission tests, one symlink test that needs a privilege I don't have).
  • Negative control: with tools/import_audit.py from main, the new tests fail with 18 failures: 16 subtests in test_pe_data_directories_must_fit_inside_optional_header and 2 in test_invalid_pe_directory_count_preserves_audit_report. test_pe_variable_optional_headers_keep_declared_directories passes on both versions, as expected.
  • The bound (optional_size - directory) // 8 uses the 96/112 data-directory offsets for PE32/PE32+, rejects a partial trailing slot and accepts exact fits (dir+16 with count 2, dir+128 with count 16). It sits in the shared pe_headers, so both readers are covered.
  • Real PEs are unaffected: System32 and SysWOW64 kernel32.dll and ntdll.dll still parse exports and imports.
  • git merge-tree against current main is clean. With the merged tools/ tree, test_import_audit.py runs 125 tests, OK (3 skipped).
  • python tools/check_conventions.py --base main: passed.

Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5595354151 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7460#pullrequestreview-5595354151). Originally posted by [harisibrar1](https://github.com/harisibrar1) on 2026-10-21T05:32:37Z. **Review:** APPROVED --- Verified on Windows 11, Python 3.14: - `python tools/tests/test_import_audit.py` on the PR head: 109 tests, OK (3 skipped: two POSIX permission tests, one symlink test that needs a privilege I don't have). - Negative control: with `tools/import_audit.py` from main, the new tests fail with 18 failures: 16 subtests in `test_pe_data_directories_must_fit_inside_optional_header` and 2 in `test_invalid_pe_directory_count_preserves_audit_report`. `test_pe_variable_optional_headers_keep_declared_directories` passes on both versions, as expected. - The bound `(optional_size - directory) // 8` uses the 96/112 data-directory offsets for PE32/PE32+, rejects a partial trailing slot and accepts exact fits (dir+16 with count 2, dir+128 with count 16). It sits in the shared `pe_headers`, so both readers are covered. - Real PEs are unaffected: System32 and SysWOW64 `kernel32.dll` and `ntdll.dll` still parse exports and imports. - `git merge-tree` against current main is clean. With the merged `tools/` tree, `test_import_audit.py` runs 125 tests, OK (3 skipped). - `python tools/check_conventions.py --base main`: passed. Reviewed with AI assistance (Claude Code); the checks above were run locally on my machine.
THEMANnt commented 2026-10-21 18:57:45 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by THEMANnt on 2026-10-21T18:57:45Z.

Review: APPROVED


Reviewed at 70986ca71f on Windows 11, Python 3.11.9; cherry-picked cleanly onto local main f894ba69d300. No actionable findings.

  • Local audit suite: 122 passed, 3 skipped (125 run). Exact PR-head source snapshot: 105 passed, 4 skipped (109 run); its additional skip requires a Git checkout, while the local suite runs in the checkout.
  • The new regression methods reproduce 18 failing subcases against main, including malformed input replacing an existing audit report.
  • Independent PE32/PE32+ grid: 768 valid export/import reader checks pass and 772 oversized-count checks are rejected. This covers capacities 1–24, all 0–7 trailing-byte lengths, and UINT32_MAX counts; no fixed 16-entry assumption is introduced.
  • Export and import results match main for all 361 real binaries tested: the 32/64-bit System32/SysWOW64 kernel32/ntdll DLLs and 357 locally built PRX libraries. All 722 reader comparisons succeeded.
  • Conventions and whitespace checks pass. The shared-parser bound agrees with Microsoft's optional-header requirements.

The three local skips are POSIX permission checks and a symlink-privilege check. No Linux/macOS run, native rebuild, game or GPU validation was performed for this Python parser change.

AI-assisted: yes (Codex); all stated local checks were run on this machine.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5604717471 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/7460#pullrequestreview-5604717471). Originally posted by [THEMANnt](https://github.com/THEMANnt) on 2026-10-21T18:57:45Z. **Review:** APPROVED --- Reviewed at `70986ca71f` on Windows 11, Python 3.11.9; cherry-picked cleanly onto local main `f894ba69d300`. No actionable findings. - Local audit suite: 122 passed, 3 skipped (125 run). Exact PR-head source snapshot: 105 passed, 4 skipped (109 run); its additional skip requires a Git checkout, while the local suite runs in the checkout. - The new regression methods reproduce 18 failing subcases against main, including malformed input replacing an existing audit report. - Independent PE32/PE32+ grid: 768 valid export/import reader checks pass and 772 oversized-count checks are rejected. This covers capacities 1–24, all 0–7 trailing-byte lengths, and UINT32_MAX counts; no fixed 16-entry assumption is introduced. - Export and import results match main for all 361 real binaries tested: the 32/64-bit System32/SysWOW64 kernel32/ntdll DLLs and 357 locally built PRX libraries. All 722 reader comparisons succeeded. - Conventions and whitespace checks pass. The shared-parser bound agrees with [Microsoft's optional-header requirements](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#optional-header-image-only). The three local skips are POSIX permission checks and a symlink-privilege check. No Linux/macOS run, native rebuild, game or GPU validation was performed for this Python parser change. AI-assisted: yes (Codex); all stated local checks were run on this machine.
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!7460
No description provided.