Reject trailing relocation bytes, odd block sizes and unaligned .pdata directories in pe_map #121

Merged
PrivacyOnion merged 2 commits from upstream-pr/122 into main 2026-11-22 06:37:54 +00:00
PrivacyOnion commented 2026-11-21 16:38:49 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by PrivacyOnion on 2026-11-21T16:38:49Z.


Change

pe_map_image walked base-relocation blocks only while offset + sizeof(IMAGE_BASE_RELOCATION) <= relocs.Size and accepted any SizeOfBlock >= sizeof(IMAGE_BASE_RELOCATION) within the remaining directory bytes. As a result:

  • a relocation directory with trailing bytes smaller than sizeof(IMAGE_BASE_RELOCATION) (or a Size of 1..7 bytes) stopped the loop early without failing;
  • a block whose SizeOfBlock was not a multiple of sizeof(WORD) silently ignored the trailing odd byte.

For .pdata registration, pe_map_image checked pdata.Size only and passed pdata.Size / sizeof(RUNTIME_FUNCTION) to image.at and RtlAddFunctionTable. A non-zero VirtualAddress with Size == 0, or a Size not divisible by sizeof(RUNTIME_FUNCTION) (12 bytes, including 1..11 bytes where integer division yields 0 entries), was not rejected.

pe_map_image now requires length % sizeof(WORD) == 0 for each relocation block, requires offset == relocs.Size after walking the relocation directory, and rejects any present .pdata directory (VirtualAddress || Size) whose Size is zero or not a multiple of sizeof(RUNTIME_FUNCTION).

Verification

Verified on Windows 11 with VS Build Tools 2022 17.14 (MSVC 19.44.35228.0), Windows SDK 10.0.26100.0, Python 3.12:

ctest --preset dev -R '^(cpu_native_map|cpu_native_seh)$' --no-tests=error -V
  • cpu_native_map: passed (0.95s), including negative controls for a 4-byte truncated relocation directory, an odd SizeOfBlock (orig_block_len - 1), trailing bytes after a valid relocation block (relocs.Size + 4), a .pdata directory with VirtualAddress != 0 and Size == 0, and a .pdata directory with Size == pdata.Size + 4.
  • cpu_native_seh: passed (0.57s).

Evidence and follow-up

All negative controls are in-memory byte patches applied to temporary copies of the source-built cpu_import_original, cpu_ordinal_original and cpu_reloc_original fixtures at test time.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6531532928 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/122). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-21T16:38:49Z. --- ## Change `pe_map_image` walked base-relocation blocks only while `offset + sizeof(IMAGE_BASE_RELOCATION) <= relocs.Size` and accepted any `SizeOfBlock >= sizeof(IMAGE_BASE_RELOCATION)` within the remaining directory bytes. As a result: - a relocation directory with trailing bytes smaller than `sizeof(IMAGE_BASE_RELOCATION)` (or a `Size` of 1..7 bytes) stopped the loop early without failing; - a block whose `SizeOfBlock` was not a multiple of `sizeof(WORD)` silently ignored the trailing odd byte. For `.pdata` registration, `pe_map_image` checked `pdata.Size` only and passed `pdata.Size / sizeof(RUNTIME_FUNCTION)` to `image.at` and `RtlAddFunctionTable`. A non-zero `VirtualAddress` with `Size == 0`, or a `Size` not divisible by `sizeof(RUNTIME_FUNCTION)` (`12` bytes, including `1..11` bytes where integer division yields `0` entries), was not rejected. `pe_map_image` now requires `length % sizeof(WORD) == 0` for each relocation block, requires `offset == relocs.Size` after walking the relocation directory, and rejects any present `.pdata` directory (`VirtualAddress || Size`) whose `Size` is zero or not a multiple of `sizeof(RUNTIME_FUNCTION)`. ## Verification Verified on Windows 11 with VS Build Tools 2022 17.14 (`MSVC 19.44.35228.0`), Windows SDK `10.0.26100.0`, Python `3.12`: ```powershell ctest --preset dev -R '^(cpu_native_map|cpu_native_seh)$' --no-tests=error -V ``` - `cpu_native_map`: passed (`0.95s`), including negative controls for a 4-byte truncated relocation directory, an odd `SizeOfBlock` (`orig_block_len - 1`), trailing bytes after a valid relocation block (`relocs.Size + 4`), a `.pdata` directory with `VirtualAddress != 0` and `Size == 0`, and a `.pdata` directory with `Size == pdata.Size + 4`. - `cpu_native_seh`: passed (`0.57s`). ## Evidence and follow-up All negative controls are in-memory byte patches applied to temporary copies of the source-built `cpu_import_original`, `cpu_ordinal_original` and `cpu_reloc_original` fixtures at test time.
upstream-sync closed this pull request 2026-11-24 06:41:25 +00:00
R0Y00 commented 2026-11-24 06:47:40 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T17:16:21Z.


Please sync these mapper checks on current main and compare the proposed malformed-directory rejections with the Windows loader, documenting any intentionally stricter rule. Keep the relocation-free fixture in #115 working.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066347704 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/122#issuecomment-7066347704). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T17:16:21Z. --- Please sync these mapper checks on current main and compare the proposed malformed-directory rejections with the Windows loader, documenting any intentionally stricter rule. Keep the relocation-free fixture in #115 working.
PrivacyOnion commented 2026-11-24 06:47:43 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by PrivacyOnion on 2026-11-21T17:44:15Z.


Rebased onto main and added a direct side-by-side comparison (check_reloc_and_pdata_bounds) against LoadLibraryExW and RtlLookupFunctionEntry, documenting both shared behavior and intentionally stricter rules in tools/pe_map.cpp:

  1. Empty relocation directory (VirtualAddress == 0 || Size == 0):
    • Like the Windows loader, pe_map treats an empty relocation directory as a no-op when IMAGE_FILE_RELOCS_STRIPPED is clear, keeping relocation-free fixtures (cpu_import_original in #115) working.
  2. Trailing relocation bytes (1..7 bytes remaining after all blocks, e.g. Size = 4 or Size = built + 4, or SizeOfBlock = built - 1 with Size = built):
    • Both LoadLibraryExW (ERROR_NOACCESS = 998) and pe_map (MAP_RELOCATION, "bad relocation block") reject the image.
  3. Odd SizeOfBlock matching relocs.Size (SizeOfBlock = built - 1, Size = built - 1):
    • LoadLibraryExW loads the image by truncating (SizeOfBlock - 8) / 2; pe_map is intentionally stricter and requires 2-byte WORD alignment (length % sizeof(WORD) == 0) for every relocation block.
  4. Malformed .pdata sizes (Size = 0 with non-zero RVA, Size = 11, Size = 20):
    • LoadLibraryExW maps the image without calling RtlAddFunctionTable and divides pdata.Size by sizeof(RUNTIME_FUNCTION) (12) at lookup/unwind time (RtlLookupFunctionEntry finds 0 entries for Size = 0 and 11, and 1 entry for Size = 20). Because pe_map registers .pdata explicitly via RtlAddFunctionTable, it is intentionally stricter and rejects any non-empty exception directory whose Size is zero or not a multiple of sizeof(RUNTIME_FUNCTION) (MAP_FUNCTION_TABLE, "cannot register .pdata").
1/4 Test #62: cpu_native_map ...................   Passed    0.34 sec
2/4 Test #63: cpu_native_seh ...................   Passed    0.44 sec
3/4 Test #64: cpu_native_section ...............   Passed    0.18 sec
4/4 Test #65: cpu_native_tls ...................   Passed    2.69 sec
<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066706542 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/122#issuecomment-7066706542). Originally posted by [PrivacyOnion](https://github.com/PrivacyOnion) on 2026-11-21T17:44:15Z. --- Rebased onto `main` and added a direct side-by-side comparison (`check_reloc_and_pdata_bounds`) against `LoadLibraryExW` and `RtlLookupFunctionEntry`, documenting both shared behavior and intentionally stricter rules in `tools/pe_map.cpp`: 1. **Empty relocation directory (`VirtualAddress == 0 || Size == 0`)**: - Like the Windows loader, `pe_map` treats an empty relocation directory as a no-op when `IMAGE_FILE_RELOCS_STRIPPED` is clear, keeping relocation-free fixtures (`cpu_import_original` in #115) working. 2. **Trailing relocation bytes (`1..7` bytes remaining after all blocks, e.g. `Size = 4` or `Size = built + 4`, or `SizeOfBlock = built - 1` with `Size = built`)**: - **Both** `LoadLibraryExW` (`ERROR_NOACCESS = 998`) and `pe_map` (`MAP_RELOCATION`, `"bad relocation block"`) reject the image. 3. **Odd `SizeOfBlock` matching `relocs.Size` (`SizeOfBlock = built - 1`, `Size = built - 1`)**: - `LoadLibraryExW` loads the image by truncating `(SizeOfBlock - 8) / 2`; `pe_map` is **intentionally stricter** and requires 2-byte `WORD` alignment (`length % sizeof(WORD) == 0`) for every relocation block. 4. **Malformed `.pdata` sizes (`Size = 0` with non-zero RVA, `Size = 11`, `Size = 20`)**: - `LoadLibraryExW` maps the image without calling `RtlAddFunctionTable` and divides `pdata.Size` by `sizeof(RUNTIME_FUNCTION)` (`12`) at lookup/unwind time (`RtlLookupFunctionEntry` finds 0 entries for `Size = 0` and `11`, and 1 entry for `Size = 20`). Because `pe_map` registers `.pdata` explicitly via `RtlAddFunctionTable`, it is **intentionally stricter** and rejects any non-empty exception directory whose `Size` is zero or not a multiple of `sizeof(RUNTIME_FUNCTION)` (`MAP_FUNCTION_TABLE`, `"cannot register .pdata"`). ```text 1/4 Test #62: cpu_native_map ................... Passed 0.34 sec 2/4 Test #63: cpu_native_seh ................... Passed 0.44 sec 3/4 Test #64: cpu_native_section ............... Passed 0.18 sec 4/4 Test #65: cpu_native_tls ................... Passed 2.69 sec ```
R0Y00 commented 2026-11-24 06:47:44 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-21T17:57:53Z.


Thanks for adding the Windows loader comparison. The required Windows job still fails in cpu_native_map on the reloc Size=4 case. LoadLibraryExW returned a module and err=0 on the runner. Please check whether the image actually needed relocation during this comparison and make the test distinguish the two conditions. Please rerun cpu_native_map and cpu_native_reloc_free on Windows. Failing job

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7066899077 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/122#issuecomment-7066899077). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-21T17:57:53Z. --- Thanks for adding the Windows loader comparison. The required Windows job still fails in cpu_native_map on the reloc Size=4 case. LoadLibraryExW returned a module and err=0 on the runner. Please check whether the image actually needed relocation during this comparison and make the test distinguish the two conditions. Please rerun cpu_native_map and cpu_native_reloc_free on Windows. [Failing job](https://github.com/R0Y00/Xbox2Windows/actions/runs/44931673030/job/136929438471)
R0Y00 commented 2026-11-24 06:48:26 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by R0Y00 on 2026-11-22T06:32:43Z.


I integrated current main and the agreed directory rules at 01e2539. Empty .pdata registers nothing, and a nonempty table must consist of complete in-image RUNTIME_FUNCTION entries. The comparison records native loader observations with the Windows build; the mapper assertions independently enforce the bounds. cpu_native_map and cpu_native_reloc_free both passed locally on Windows. CI is now testing this head.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7075387547 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/122#issuecomment-7075387547). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T06:32:43Z. --- I integrated current main and the agreed directory rules at 01e2539. Empty .pdata registers nothing, and a nonempty table must consist of complete in-image RUNTIME_FUNCTION entries. The comparison records native loader observations with the Windows build; the mapper assertions independently enforce the bounds. cpu_native_map and cpu_native_reloc_free both passed locally on Windows. CI is now testing this head.
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
R0Y00/Xbox2Windows!121
No description provided.