fix(allocator): reject malformed import metadata in PeImportFirst #11356

Merged
brainiac91 merged 2 commits from upstream-pr/11356 into main 2026-10-28 09:36:05 +00:00
brainiac91 commented 2026-10-26 08:06:19 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by brainiac91 on 2026-10-26T08:06:19Z.


What

Fixes #11286. Reject malformed PE32+ import metadata before any in-place descriptor update while preserving valid minimal optional headers.

  • Require a declared Import Directory (NumberOfRvaAndSizes >= 2) and its complete eight-byte entry at optional-header offsets 120–127. A valid 128-byte optional header with exactly two directories is accepted; a 127-byte header is rejected.
  • Validate the descriptor table and every DLL name before rotating descriptors. Names outside the file or without a NUL terminator fail without changing the file.
  • Cover minimal-header reordering through both APIs, complete descriptor movement, stable remaining order, unchanged surrounding bytes, repeated-call stability, and malformed-header file preservation.

Tested

  • Windows 11, WinLibs MinGW UCRT GCC 16.2. The final focused sources compile with -std=c++20 -O2 -Wall -Wextra -Werror; the resulting executable passes.
  • Red/green: the added 128-byte/two-directory fixture fails against the previous PR implementation with the image has no imports, then passes with the eight-byte bound. The 127-byte-header case and all existing malformed-input checks reject without modifying their files.
  • Rebased onto upstream main at f616a1397f9b618036ac7c89f461f0bd46fbd6d6. Neither affected file changed upstream between the original base and that commit.
  • After rebasing, cmake --build build-tests --target pe_import_first_tests pe_import_first succeeds in the full-build configuration with bundled dependencies, and ctest --test-dir build-tests -R '^pe_import_first$' -V passes 1/1.
  • The rebuilt CLI succeeds on a disposable copy of the built mimalloc.dll, moving mimalloc-redirect.dll ahead of ADVAPI32.dll and KERNEL32.dll. An independent PE parser verifies whole-descriptor rotation, stable remaining order, byte-for-byte preservation outside the descriptors, and no further change on a second run; the source DLL is unchanged.
  • tools/pr_overlap.py --branch fix/pe-import-malformed-metadata --repo boykopovar/AnyPS5 completes with only PR #11356 itself reported before the rebased branch is pushed; no other PR overlaps.
  • tools/check_conventions.py --base origin/main and git diff --check origin/main...HEAD pass.

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: Fixes #11286
  • Unblocks: none
  • GPUs tested: n/a
  • AI-assisted: yes
<!-- greyfinch-sync:boykopovar/AnyPS5:issue:5014085164 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11356). Originally posted by [brainiac91](https://github.com/brainiac91) on 2026-10-26T08:06:19Z. --- ### What Fixes #11286. Reject malformed PE32+ import metadata before any in-place descriptor update while preserving valid minimal optional headers. - Require a declared Import Directory (`NumberOfRvaAndSizes >= 2`) and its complete eight-byte entry at optional-header offsets 120–127. A valid 128-byte optional header with exactly two directories is accepted; a 127-byte header is rejected. - Validate the descriptor table and every DLL name before rotating descriptors. Names outside the file or without a NUL terminator fail without changing the file. - Cover minimal-header reordering through both APIs, complete descriptor movement, stable remaining order, unchanged surrounding bytes, repeated-call stability, and malformed-header file preservation. ### Tested - Windows 11, WinLibs MinGW UCRT GCC 16.2. The final focused sources compile with `-std=c++20 -O2 -Wall -Wextra -Werror`; the resulting executable passes. - Red/green: the added 128-byte/two-directory fixture fails against the previous PR implementation with `the image has no imports`, then passes with the eight-byte bound. The 127-byte-header case and all existing malformed-input checks reject without modifying their files. - Rebased onto upstream `main` at `f616a1397f9b618036ac7c89f461f0bd46fbd6d6`. Neither affected file changed upstream between the original base and that commit. - After rebasing, `cmake --build build-tests --target pe_import_first_tests pe_import_first` succeeds in the full-build configuration with bundled dependencies, and `ctest --test-dir build-tests -R '^pe_import_first$' -V` passes 1/1. - The rebuilt CLI succeeds on a disposable copy of the built `mimalloc.dll`, moving `mimalloc-redirect.dll` ahead of `ADVAPI32.dll` and `KERNEL32.dll`. An independent PE parser verifies whole-descriptor rotation, stable remaining order, byte-for-byte preservation outside the descriptors, and no further change on a second run; the source DLL is unchanged. - `tools/pr_overlap.py --branch fix/pe-import-malformed-metadata --repo boykopovar/AnyPS5` completes with only PR #11356 itself reported before the rebased branch is pushed; no other PR overlaps. - `tools/check_conventions.py --base origin/main` and `git diff --check origin/main...HEAD` pass. <!-- 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. --> ### 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: Fixes #11286 <!-- 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 -->
MarcZX10 commented 2026-10-26 08:41:02 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T08:41:02Z.

Review: CHANGES_REQUESTED


I independently built the exact PR sources (PeImportFirst.cpp and PeImportFirstTests.cpp) with g++ -std=c++20 -Wall -Wextra -Werror and added a valid minimal PE32+ fixture to the temporary test copy. The fixture declares exactly two data directories (NumberOfRvaAndSizes == 2), has SizeOfOptionalHeader == 128 (the 112-byte PE32+ fixed header plus the 16 bytes needed for export and import entries), and places the section table at the declared optional-header end. It includes valid imports and requests mimalloc-redirect.dll. The PR rejects it with the image has no imports.

The guard currently requires optionalSize >= ImportDirectory + 16, where ImportDirectory is already 120, so it requires 136 bytes. The Import Directory entry at offset 120 is eight bytes; the correct lower bound for this entry is 128 bytes. Please add the 128-byte/two-directory regression and accept/reorder this valid image, while retaining the malformed-header tests. The existing focused suite passes with the current PR code, but does not cover this valid minimal optional header.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5649352595 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11356#pullrequestreview-5649352595). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T08:41:02Z. **Review:** CHANGES_REQUESTED --- I independently built the exact PR sources (`PeImportFirst.cpp` and `PeImportFirstTests.cpp`) with `g++ -std=c++20 -Wall -Wextra -Werror` and added a valid minimal PE32+ fixture to the temporary test copy. The fixture declares exactly two data directories (`NumberOfRvaAndSizes == 2`), has `SizeOfOptionalHeader == 128` (the 112-byte PE32+ fixed header plus the 16 bytes needed for export and import entries), and places the section table at the declared optional-header end. It includes valid imports and requests `mimalloc-redirect.dll`. The PR rejects it with `the image has no imports`. The guard currently requires `optionalSize >= ImportDirectory + 16`, where `ImportDirectory` is already 120, so it requires 136 bytes. The Import Directory entry at offset 120 is eight bytes; the correct lower bound for this entry is 128 bytes. Please add the 128-byte/two-directory regression and accept/reorder this valid image, while retaining the malformed-header tests. The existing focused suite passes with the current PR code, but does not cover this valid minimal optional header.
MarcZX10 commented 2026-10-26 13:28:11 +00:00 (Migrated from github.com)

Imported from boykopovar/AnyPS5. Originally posted by MarcZX10 on 2026-10-26T13:28:11Z.

Review: DISMISSED


I rechecked the exact head c039d1c476e21317d25e00cb11846226139be377. The out-of-file Name RVA case I reported earlier is now rejected and has a regression test; that issue is resolved. One valid-input regression remains: a PE32+ optional header of 128 bytes with NumberOfRvaAndSizes == 2 contains the full Import Directory entry (directory table starts at offset 112; entry #1 occupies bytes 120–127). The current guard requires optionalSize >= ImportDirectory + 16 (136 bytes), so it rejects this valid minimal header. I reproduced this with a synthetic image whose section table starts at the declared optional-header end and whose imports are otherwise valid. Please change the bound to cover the eight-byte entry and add the positive 128-byte/two-directory test; keep the malformed-header and no-mutation cases.

This PR is based on 204c6747d0a1, while current main is 0e5d5a54098c. I found no intervening changes to the two modified files, so the source review still applies, but CONTRIBUTING requires syncing to current main and rerunning conventions. Hosted eligibility/conventions/intake/labels pass; product and relinker jobs are skipped by path policy. The author-reported MinGW build/test evidence is useful but does not cover the missing valid-header case.

<!-- greyfinch-sync:boykopovar/AnyPS5:review:5652703822 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/pull/11356#pullrequestreview-5652703822). Originally posted by [MarcZX10](https://github.com/MarcZX10) on 2026-10-26T13:28:11Z. **Review:** DISMISSED --- I rechecked the exact head `c039d1c476e21317d25e00cb11846226139be377`. The out-of-file Name RVA case I reported earlier is now rejected and has a regression test; that issue is resolved. One valid-input regression remains: a PE32+ optional header of 128 bytes with `NumberOfRvaAndSizes == 2` contains the full Import Directory entry (directory table starts at offset 112; entry #1 occupies bytes 120–127). The current guard requires `optionalSize >= ImportDirectory + 16` (136 bytes), so it rejects this valid minimal header. I reproduced this with a synthetic image whose section table starts at the declared optional-header end and whose imports are otherwise valid. Please change the bound to cover the eight-byte entry and add the positive 128-byte/two-directory test; keep the malformed-header and no-mutation cases. This PR is based on `204c6747d0a1`, while current `main` is `0e5d5a54098c`. I found no intervening changes to the two modified files, so the source review still applies, but CONTRIBUTING requires syncing to current main and rerunning conventions. Hosted eligibility/conventions/intake/labels pass; product and relinker jobs are skipped by path policy. The author-reported MinGW build/test evidence is useful but does not cover the missing valid-header case.
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!11356
No description provided.