pe: keep section protections from faulting pe_map during the map #412

Merged
grahamster2 merged 3 commits from upstream-pr/418 into main 2026-11-22 08:06:23 +00:00
grahamster2 commented 2026-11-22 01:05:12 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T01:05:12Z.


Fixes two ways that pe_map_image faults inside the map and ends the host process, on files that LoadLibraryExW maps or refuses cleanly.

1. .pdata in a section without read access

RtlAddFunctionTable reads the table to find its address range. pe_map registered .pdata after applying section protections, so for a .pdata section whose Characteristics has no read, write or execute flag, the page was already PAGE_NOACCESS and the call faulted:

OSError: exception: access violation reading 0x00000166265B3000

On Windows 11 build 26200, LoadLibraryExW maps the same copy and leaves .pdata PAGE_NOACCESS (checked with VirtualQuery), so the Windows loader does not read the table at load time. pe_map now registers .pdata before applying protections and removes it again if a header or section protection then fails. Final protections are unchanged.

2. A section that does not start on SectionAlignment

VirtualProtect works on whole pages. pe_map checked only VirtualAddress >= SizeOfHeaders, so a section at RVA 0x400 passed, and its no-access protection also covered page 0 with the headers. The next header read faulted at base + 0x3c. The fuzzer reached this with SizeOfOptionalHeader 8 bytes larger, which shifts the section table. Windows refuses that file with error 193.

pe_map now refuses:

  • a SectionAlignment that is not a power of two (MAP_FORMAT);
  • a SectionAlignment below the page size (MAP_UNSUPPORTED). Windows maps such images as one view when FileAlignment matches, which pe_map does not model;
  • a section whose VirtualAddress does not start on SectionAlignment (MAP_FORMAT).

Measured with LoadLibraryExW on copies of cpu_switch_original that change only SectionAlignment: 0x1000 loads; 0x2000, 0x10000, 0x3000, 0x1001 and 0x800 each fail with error 193.

Tests

cpu_native_section gains two checks on the register probe:

  • Every section's page protection after pe_map must equal LoadLibraryExW's, on the original and on a copy with an unreadable .pdata, and Windows must map .pdata PAGE_NOACCESS.
  • Copies with SectionAlignment 0x2000, 0x1001 and 0x800 must be refused by LoadLibraryExW and by pe_map, with the expected status and reason.

Against main's pe_map.cpp, the protection check ends the test with the access violation above, and main maps the 0x2000 copy with status 0.

ctest --preset dev --no-tests=error -R '^cpu_native_section$' -V

Windows 11 Pro build 26200, VS Build Tools 2026 18.3 (MSVC 19.50.35725), Windows SDK 10.0.26100, Python 3.12, on 6549244. The full Debug suite passes, 143 of 143 (the three tests/intake cases were not run on this machine).

How it was found

A local structure-aware mutation fuzzer changes 1–2 fields per copy: header, data-directory, section, import descriptor and thunk, export, relocation block and .pdata fields, set to boundary and nearby values. It uses five source-built fixtures with no entry point and no TLS, so none of their own code runs. Each copy is mapped in a child process with a dummy resolver, and the child then looks up exports and releases the image. 16 of 3,000 copies faulted, and all came from these two causes. With the change, 12,000 further copies (seeds 2–5, six fixtures) mapped or were refused without a fault. Both reproducers are covered by the tests above.

Overlap

#122, #143, #171 and #208 also edit tools/pe_map.cpp. This change touches the header check after the section-table bounds and the protection block near the end of pe_map_image. A rebase onto whichever lands first should be mechanical, and I'll do it.

AI-assisted (Claude Code).

<!-- greyfinch-sync:R0Y00/Xbox2Windows:issue:6536630073 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/418). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-22T01:05:12Z. --- Fixes two ways that `pe_map_image` faults inside the map and ends the host process, on files that `LoadLibraryExW` maps or refuses cleanly. ## 1. `.pdata` in a section without read access `RtlAddFunctionTable` reads the table to find its address range. `pe_map` registered `.pdata` after applying section protections, so for a `.pdata` section whose `Characteristics` has no read, write or execute flag, the page was already `PAGE_NOACCESS` and the call faulted: OSError: exception: access violation reading 0x00000166265B3000 On Windows 11 build 26200, `LoadLibraryExW` maps the same copy and leaves `.pdata` `PAGE_NOACCESS` (checked with `VirtualQuery`), so the Windows loader does not read the table at load time. `pe_map` now registers `.pdata` before applying protections and removes it again if a header or section protection then fails. Final protections are unchanged. ## 2. A section that does not start on `SectionAlignment` `VirtualProtect` works on whole pages. `pe_map` checked only `VirtualAddress >= SizeOfHeaders`, so a section at RVA `0x400` passed, and its no-access protection also covered page 0 with the headers. The next header read faulted at `base + 0x3c`. The fuzzer reached this with `SizeOfOptionalHeader` 8 bytes larger, which shifts the section table. Windows refuses that file with error 193. `pe_map` now refuses: - a `SectionAlignment` that is not a power of two (`MAP_FORMAT`); - a `SectionAlignment` below the page size (`MAP_UNSUPPORTED`). Windows maps such images as one view when `FileAlignment` matches, which `pe_map` does not model; - a section whose `VirtualAddress` does not start on `SectionAlignment` (`MAP_FORMAT`). Measured with `LoadLibraryExW` on copies of `cpu_switch_original` that change only `SectionAlignment`: `0x1000` loads; `0x2000`, `0x10000`, `0x3000`, `0x1001` and `0x800` each fail with error 193. ## Tests `cpu_native_section` gains two checks on the register probe: - Every section's page protection after `pe_map` must equal `LoadLibraryExW`'s, on the original and on a copy with an unreadable `.pdata`, and Windows must map `.pdata` `PAGE_NOACCESS`. - Copies with `SectionAlignment` `0x2000`, `0x1001` and `0x800` must be refused by `LoadLibraryExW` and by `pe_map`, with the expected status and reason. Against `main`'s `pe_map.cpp`, the protection check ends the test with the access violation above, and `main` maps the `0x2000` copy with status 0. ctest --preset dev --no-tests=error -R '^cpu_native_section$' -V Windows 11 Pro build 26200, VS Build Tools 2026 18.3 (MSVC 19.50.35725), Windows SDK 10.0.26100, Python 3.12, on `6549244`. The full Debug suite passes, 143 of 143 (the three `tests/intake` cases were not run on this machine). ## How it was found A local structure-aware mutation fuzzer changes 1–2 fields per copy: header, data-directory, section, import descriptor and thunk, export, relocation block and `.pdata` fields, set to boundary and nearby values. It uses five source-built fixtures with no entry point and no TLS, so none of their own code runs. Each copy is mapped in a child process with a dummy resolver, and the child then looks up exports and releases the image. 16 of 3,000 copies faulted, and all came from these two causes. With the change, 12,000 further copies (seeds 2–5, six fixtures) mapped or were refused without a fault. Both reproducers are covered by the tests above. ## Overlap #122, #143, #171 and #208 also edit `tools/pe_map.cpp`. This change touches the header check after the section-table bounds and the protection block near the end of `pe_map_image`. A rebase onto whichever lands first should be mechanical, and I'll do it. _AI-assisted (Claude Code)._
upstream-sync closed this pull request 2026-11-24 06:42:56 +00:00
grahamster2 commented 2026-11-24 06:48:16 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T01:31:42Z.


The Windows job hung in cpu_native_section until the 20-minute limit canceled it. check_alignments() loads copies that Windows refuses with ERROR_BAD_EXE_FORMAT, and without SEM_FAILCRITICALERRORS the loader shows a "Bad Image" hard-error dialog and waits; on the headless runner nothing dismisses it. 19452e7 sets SEM_FAILCRITICALERRORS at the start of main(), as import_check.py does. With the error mode forced to 0, the previous script logged three Bad Image popups (System log, Application Popup event 26), one per refused alignment; the fixed script logs none and finishes in 0.14 s.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7072259802 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/418#issuecomment-7072259802). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-22T01:31:42Z. --- The Windows job hung in `cpu_native_section` until the 20-minute limit canceled it. `check_alignments()` loads copies that Windows refuses with ERROR_BAD_EXE_FORMAT, and without SEM_FAILCRITICALERRORS the loader shows a "Bad Image" hard-error dialog and waits; on the headless runner nothing dismisses it. 19452e7 sets SEM_FAILCRITICALERRORS at the start of `main()`, as `import_check.py` does. With the error mode forced to 0, the previous script logged three Bad Image popups (System log, Application Popup event 26), one per refused alignment; the fixed script logs none and finishes in 0.14 s.
grahamster2 commented 2026-11-24 06:48:22 +00:00 (Migrated from github.com)

Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T03:26:12Z.


Additional fuzzing evidence for this change, from a second round aimed at areas the first round didn't mutate: the TLS directory and callback slots, delay-import descriptors and name tables, export tables and lookups, and NumberOfRvaAndSizes.

  • 20,000 mutated copies of cpu_tls_original, cpu_frame_guest and cpu_delay_route, each mapped by main (fb4fb85) and by this PR (19452e7), Debug and Release, with imports resolved for real so entry points and TLS callbacks run.
  • Each successful map then went through export lookups (fixed names, a 5,000-character name, truncated and one-letter-changed variants of the real names, and the string at each mutated AddressOfNames entry), a thread attach and detach through pe_map.dll's DllMain, the same lookups again, release, and one more thread after release.
  • Faults were classified by the faulting instruction's module, unwinding out of System32 to the first non-system frame.
  • main: 53 faults inside pe_map.dll, all of them the unreadable-.pdata case this PR fixes (RtlAddFunctionTable called from pe_map). This PR: 0 faults inside pe_map.dll. About 13,400 copies mapped and completed the full sequence.
  • A 30-second stress run per build with 4 mapping threads, 3 thread-churn threads and a cross-thread releaser (about 470,000 maps and 500,000 threads) produced no fault.

Windows 11 Pro build 26200, MSVC 19.50.35725.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073534029 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/418#issuecomment-7073534029). Originally posted by [grahamster2](https://github.com/grahamster2) on 2026-11-22T03:26:12Z. --- Additional fuzzing evidence for this change, from a second round aimed at areas the first round didn't mutate: the TLS directory and callback slots, delay-import descriptors and name tables, export tables and lookups, and `NumberOfRvaAndSizes`. - 20,000 mutated copies of `cpu_tls_original`, `cpu_frame_guest` and `cpu_delay_route`, each mapped by `main` (`fb4fb85`) and by this PR (`19452e7`), Debug and Release, with imports resolved for real so entry points and TLS callbacks run. - Each successful map then went through export lookups (fixed names, a 5,000-character name, truncated and one-letter-changed variants of the real names, and the string at each mutated `AddressOfNames` entry), a thread attach and detach through `pe_map.dll`'s `DllMain`, the same lookups again, release, and one more thread after release. - Faults were classified by the faulting instruction's module, unwinding out of System32 to the first non-system frame. - `main`: 53 faults inside `pe_map.dll`, all of them the unreadable-`.pdata` case this PR fixes (`RtlAddFunctionTable` called from `pe_map`). This PR: 0 faults inside `pe_map.dll`. About 13,400 copies mapped and completed the full sequence. - A 30-second stress run per build with 4 mapping threads, 3 thread-churn threads and a cross-thread releaser (about 470,000 maps and 500,000 threads) produced no fault. Windows 11 Pro build 26200, MSVC 19.50.35725.
R0Y00 commented 2026-11-24 06:48:23 +00:00 (Migrated from github.com)

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


The change addresses registration before final protections, but the dynamic function table pointer still points into the mapping for the entire registration lifetime. With a final no-access table page, a successful map/release comparison does not establish that the registered table remains usable for unwind lookup. Please preserve a readable registration for its full lifetime, or return an explicit unsupported result for that configuration before registering. Keep the existing mapping/alignment and cleanup checks, and document the post-map unwind guarantee.

<!-- greyfinch-sync:R0Y00/Xbox2Windows:comment:7073861342 --> Imported from [R0Y00/Xbox2Windows](https://github.com/R0Y00/Xbox2Windows/pull/418#issuecomment-7073861342). Originally posted by [R0Y00](https://github.com/R0Y00) on 2026-11-22T03:54:43Z. --- The change addresses registration before final protections, but the dynamic function table pointer still points into the mapping for the entire registration lifetime. With a final no-access table page, a successful map/release comparison does not establish that the registered table remains usable for unwind lookup. Please preserve a readable registration for its full lifetime, or return an explicit unsupported result for that configuration before registering. Keep the existing mapping/alignment and cleanup checks, and document the post-map unwind guarantee.
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!412
No description provided.