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

Manually merged
github merged 3 commits from gh-pr/418/head into gh-pr/418/base 2026-11-26 21:50:22 +00:00
Owner

@⁠grahamster2 on GitHub · 2026-11-22 01:05 UTC · original
grahamster2:pe/pdata-before-protect → main · +113 −1 · 3 files · 3 commits · merged 2026-11-22 08:06 UTC

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).

**@⁠grahamster2** on GitHub · 2026-11-22 01:05 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/418) `grahamster2:pe/pdata-before-protect` → `main` · +113 −1 · 3 files · 3 commits · merged 2026-11-22 08:06 UTC 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)._
Two header layouts made pe_map_image fault inside the map and end the
host process, where LoadLibraryExW maps or refuses the same file.

RtlAddFunctionTable reads the table to find its address range, but pe_map
registered .pdata after applying section protections. A .pdata section
without IMAGE_SCN_MEM_READ is PAGE_NOACCESS by then, so the call faulted.
Windows maps such an image and leaves the page PAGE_NOACCESS. Register
.pdata first, and remove it again if a later protection fails.

VirtualProtect works on pages, so a section that does not start on its own
page changes the protection of the page it shares, the headers included.
Refuse a SectionAlignment that is not a power of two or lies below the page
size, and a section that does not start on it. Windows refuses copies of
the register probe with SectionAlignment 0x2000, 0x1001 and 0x800 with
error 193.

cpu_native_section compares every section's protection with Windows on the
probe and on a copy with an unreadable .pdata, and checks the three
alignments against LoadLibraryExW.
check_alignments() loads images that Windows refuses with ERROR_BAD_EXE_FORMAT.
Without SEM_FAILCRITICALERRORS, which the CI runner does not inherit, the
loader raises a Bad Image hard-error dialog and waits for it to be dismissed.
github-bot locked as GitHub sync and limited conversation to collaborators 2026-11-26 21:50:20 +00:00
github manually merged commit 42a88874be into gh-pr/418/base 2026-11-26 21:50:22 +00:00
Author
Owner

@⁠grahamster2 on GitHub · 2026-11-22 01:31 UTC · original

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** on GitHub · 2026-11-22 01:31 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/418#issuecomment-7072259802) 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.
Author
Owner

@⁠grahamster2 on GitHub · 2026-11-22 03:26 UTC · original

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.

**@⁠grahamster2** on GitHub · 2026-11-22 03:26 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/418#issuecomment-7073534029) 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.
Author
Owner

@⁠R0Y00 on GitHub · 2026-11-22 03:54 UTC · original

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.

**@⁠R0Y00** on GitHub · 2026-11-22 03:54 UTC · [original](https://github.com/R0Y00/Xbox2Windows/pull/418#issuecomment-7073861342) 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.
This discussion has been locked. Commenting is limited to contributors.
No description provided.