PeImportFirst rewrites malformed import metadata #11286

Closed
opened 2026-10-26 00:06:54 +00:00 by malekaifa · 0 comments
malekaifa commented 2026-10-26 00:06:54 +00:00 (Migrated from github.com)
Contributor

Imported from boykopovar/AnyPS5. Originally posted by malekaifa on 2026-10-26T00:06:54Z.


What happens

core/libs/allocator/PeImportFirst.cpp can rewrite malformed PE32+ import metadata instead of rejecting it before the in-place update.

Two deterministic cases reproduce on current main acb900c194325b9ee8c23c10768a9a491454f0b2:

  1. Import directory not declared. If NumberOfRvaAndSizes is 0 or 1, stale nonzero bytes in the optional-header slot where Import Directory entry #1 would normally live are still read as an import RVA. A synthetic image with valid descriptor bytes at that RVA exits successfully and has its import descriptors reordered even though the PE declares no Import Directory entry.
  2. Unterminated import name. If an import descriptor's Name RVA points to bytes that run to EOF without a NUL terminator, the current loop accepts the entire tail as the DLL name. If those bytes equal the requested DLL name, the helper exits successfully and rewrites the descriptors instead of reporting a truncated/malformed image.

MoveImportFirstInFile is an in-place build-time PE rewriter, and its existing tests already require malformed inputs to fail without changing the file. These cases violate that boundary and can hide a damaged artifact by modifying it further.

Reproduction

Repository-only synthetic PE fixtures are sufficient; no title, GPU or external binary is involved.

For the directory-count case, start from the existing PeImportFirstTests.cpp image for kernel32.dll + mimalloc-redirect.dll, set the PE32+ NumberOfRvaAndSizes field at optional-header offset +108 to 0 (or 1), leave the stale import-directory RVA/descriptors in place, and call MoveImportFirstInFile(..., mimalloc-redirect.dll).

Current result: success and descriptor reordering. Expected: failure such as the image has no imports, with the file byte-for-byte unchanged.

For the unterminated-name case, start from the same image and truncate the file immediately after the bytes of the final mimalloc-redirect.dll name, removing its terminating NUL while leaving the descriptor and its Name RVA intact.

Current result: success and descriptor reordering. Expected: failure such as unterminated import name, with the file byte-for-byte unchanged.

Both current-main failures were reproduced independently before preparing the fix.

Proposed fix

  • Require the PE32+ optional header to contain Import Directory entry #1 and require NumberOfRvaAndSizes >= 2 before reading it.
  • Require every import name scan to encounter a NUL before EOF.
  • Reuse the existing failsWithoutTouchingTheFile(...) helper for regression coverage of directory counts 0/1, a too-short optional header, and an unterminated DLL name.

A tested candidate is published at malekaifa/AnyPS5:fix/pe-import-directory-count (21d93e844c7ba135459961f85124af9d883fc318). The focused PeImportFirstTests.cpp executable passes under WSL/g++ and tools/check_conventions.py --base origin/main plus git diff --check pass. No independent GPT-6.1 review has been run yet; that is deferred until this becomes the PR being submitted.

AI-assisted: yes (OpenAI Codex).

<!-- greyfinch-sync:boykopovar/AnyPS5:issue:6030925100 --> Imported from [boykopovar/AnyPS5](https://github.com/boykopovar/AnyPS5/issues/11286). Originally posted by [malekaifa](https://github.com/malekaifa) on 2026-10-26T00:06:54Z. --- ### What happens `core/libs/allocator/PeImportFirst.cpp` can rewrite malformed PE32+ import metadata instead of rejecting it before the in-place update. Two deterministic cases reproduce on current `main` `acb900c194325b9ee8c23c10768a9a491454f0b2`: 1. **Import directory not declared.** If `NumberOfRvaAndSizes` is `0` or `1`, stale nonzero bytes in the optional-header slot where Import Directory entry #1 would normally live are still read as an import RVA. A synthetic image with valid descriptor bytes at that RVA exits successfully and has its import descriptors reordered even though the PE declares no Import Directory entry. 2. **Unterminated import name.** If an import descriptor's Name RVA points to bytes that run to EOF without a NUL terminator, the current loop accepts the entire tail as the DLL name. If those bytes equal the requested DLL name, the helper exits successfully and rewrites the descriptors instead of reporting a truncated/malformed image. `MoveImportFirstInFile` is an in-place build-time PE rewriter, and its existing tests already require malformed inputs to fail without changing the file. These cases violate that boundary and can hide a damaged artifact by modifying it further. ### Reproduction Repository-only synthetic PE fixtures are sufficient; no title, GPU or external binary is involved. For the directory-count case, start from the existing `PeImportFirstTests.cpp` image for `kernel32.dll` + `mimalloc-redirect.dll`, set the PE32+ `NumberOfRvaAndSizes` field at optional-header offset `+108` to `0` (or `1`), leave the stale import-directory RVA/descriptors in place, and call `MoveImportFirstInFile(..., mimalloc-redirect.dll)`. Current result: success and descriptor reordering. Expected: failure such as `the image has no imports`, with the file byte-for-byte unchanged. For the unterminated-name case, start from the same image and truncate the file immediately after the bytes of the final `mimalloc-redirect.dll` name, removing its terminating NUL while leaving the descriptor and its Name RVA intact. Current result: success and descriptor reordering. Expected: failure such as `unterminated import name`, with the file byte-for-byte unchanged. Both current-main failures were reproduced independently before preparing the fix. ### Proposed fix - Require the PE32+ optional header to contain Import Directory entry #1 and require `NumberOfRvaAndSizes >= 2` before reading it. - Require every import name scan to encounter a NUL before EOF. - Reuse the existing `failsWithoutTouchingTheFile(...)` helper for regression coverage of directory counts `0`/`1`, a too-short optional header, and an unterminated DLL name. A tested candidate is published at `malekaifa/AnyPS5:fix/pe-import-directory-count` (`21d93e844c7ba135459961f85124af9d883fc318`). The focused `PeImportFirstTests.cpp` executable passes under WSL/g++ and `tools/check_conventions.py --base origin/main` plus `git diff --check` pass. No independent GPT-6.1 review has been run yet; that is deferred until this becomes the PR being submitted. AI-assisted: yes (OpenAI Codex).
Sign in to join this conversation.
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#11286
No description provided.