pe: keep section protections from faulting pe_map during the map #412
Loading…
Reference in a new issue
No description provided.
Delete branch "upstream-pr/418"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T01:05:12Z.
Fixes two ways that
pe_map_imagefaults inside the map and ends the host process, on files thatLoadLibraryExWmaps or refuses cleanly.1.
.pdatain a section without read accessRtlAddFunctionTablereads the table to find its address range.pe_mapregistered.pdataafter applying section protections, so for a.pdatasection whoseCharacteristicshas no read, write or execute flag, the page was alreadyPAGE_NOACCESSand the call faulted:On Windows 11 build 26200,
LoadLibraryExWmaps the same copy and leaves.pdataPAGE_NOACCESS(checked withVirtualQuery), so the Windows loader does not read the table at load time.pe_mapnow registers.pdatabefore 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
SectionAlignmentVirtualProtectworks on whole pages.pe_mapchecked onlyVirtualAddress >= SizeOfHeaders, so a section at RVA0x400passed, and its no-access protection also covered page 0 with the headers. The next header read faulted atbase + 0x3c. The fuzzer reached this withSizeOfOptionalHeader8 bytes larger, which shifts the section table. Windows refuses that file with error 193.pe_mapnow refuses:SectionAlignmentthat is not a power of two (MAP_FORMAT);SectionAlignmentbelow the page size (MAP_UNSUPPORTED). Windows maps such images as one view whenFileAlignmentmatches, whichpe_mapdoes not model;VirtualAddressdoes not start onSectionAlignment(MAP_FORMAT).Measured with
LoadLibraryExWon copies ofcpu_switch_originalthat change onlySectionAlignment:0x1000loads;0x2000,0x10000,0x3000,0x1001and0x800each fail with error 193.Tests
cpu_native_sectiongains two checks on the register probe:pe_mapmust equalLoadLibraryExW's, on the original and on a copy with an unreadable.pdata, and Windows must map.pdataPAGE_NOACCESS.SectionAlignment0x2000,0x1001and0x800must be refused byLoadLibraryExWand bype_map, with the expected status and reason.Against
main'spe_map.cpp, the protection check ends the test with the access violation above, andmainmaps the0x2000copy with status 0.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 threetests/intakecases 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
.pdatafields, 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 ofpe_map_image. A rebase onto whichever lands first should be mechanical, and I'll do it.AI-assisted (Claude Code).
Imported from R0Y00/Xbox2Windows. Originally posted by grahamster2 on 2026-11-22T01:31:42Z.
The Windows job hung in
cpu_native_sectionuntil 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.19452e7sets SEM_FAILCRITICALERRORS at the start ofmain(), asimport_check.pydoes. 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.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.cpu_tls_original,cpu_frame_guestandcpu_delay_route, each mapped bymain(fb4fb85) and by this PR (19452e7), Debug and Release, with imports resolved for real so entry points and TLS callbacks run.AddressOfNamesentry), a thread attach and detach throughpe_map.dll'sDllMain, the same lookups again, release, and one more thread after release.main: 53 faults insidepe_map.dll, all of them the unreadable-.pdatacase this PR fixes (RtlAddFunctionTablecalled frompe_map). This PR: 0 faults insidepe_map.dll. About 13,400 copies mapped and completed the full sequence.Windows 11 Pro build 26200, MSVC 19.50.35725.
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.