Repository navigation
pe_loader: an image with sub-page section alignment keeps executable code - #8513
Open
MartinIndra02 wants to merge 1 commit into
Open
MartinIndra02 wants to merge 1 commit into
MartinIndra02 wants to merge 1 commit into
Conversation
…code protect_sections() protects the headers and then each section in turn. VirtualProtect works on whole pages, so when SectionAlignment is below the page size (sections share pages, e.g. a DLL linked /ALIGN:512 /FILEALIGN:512) every shared page ends up with the last section's protection: the code page became PAGE_READONLY or PAGE_READWRITE and the first call into it faulted under DEP. The Windows loader maps such an image with one protection for every page (PAGE_EXECUTE_WRITECOPY, measured on Windows 11). The mapping here is private memory, so the whole image gets PAGE_EXECUTE_READWRITE. pe_loader_test builds a synthetic low-alignment DLL (code, data and relocation sections in the first page) and checks the code page is executable, the data page writable, and that the code runs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This was referenced Nov 23, 2026
|
Tested
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Claimed in the coordination issue as: #8344 (comment)
Depends on: none
What and why
Loader::protect_sections(runtime/src/pe_loader.cpp:1664) protects the headers and then each section in turn.VirtualProtectworks on whole pages, so when an image'sSectionAlignmentis below the page size (the headers and sections share pages, for example a DLL linked/ALIGN:512 /FILEALIGN:512), every shared page ends up with the protection of the last section on it. In the test image the code page endedPAGE_READONLY(0x2, from.reloc), so the first call into the image faults under DEP. With a writable.datalast it would bePAGE_READWRITE, still not executable.The Windows loader maps such an image with one protection for every page. Measured on Windows 11 with a small DLL linked
/ALIGN:512 /FILEALIGN:512and loaded withLoadLibrary: the header, code and data pages all reportPAGE_EXECUTE_WRITECOPY(0x80), the export runs, and the data can be written. The fix does the same whenSectionAlignment < dwPageSize: the whole image getsPAGE_EXECUTE_READWRITE, because this mapping is private memory and not a file view. Images with page-aligned sections, including every MSVC default build, go through the unchanged per-section path.pe_loader_testgainstest_low_section_alignment. It builds a synthetic PE32+ DLL byte by byte, withSectionAlignmentandFileAlignmentset to 0x200 and.text(mov eax, 42; ret),.dataand an empty.relocblock all inside the first page. The test loads the DLL withrt::Loader, checks withVirtualQuerythat the code page is executable and the data page is writable, then calls the code and writes the data.Tested
Windows 11 x64, MSVC 19.44.35220, NVIDIA GeForce RTX 3080 (driver 621.24 / 32.0.16.2124).
pe_loader_test(ctestpe_loader) before the fix:FAILED (83 checks, 2 failures).code page protect 0x2, data page protect 0x2, and the "stays executable" and "stays writable" checks fail.pe_loader_testafter the fix:OK (86 checks). It now also checks that the code runs and returns 42 and that the data reads 7 and can be written.ctest -R "loader|module_names|code_hooks|dll_thread|run_": 28/28 passed.cteston main before the change showed only the known failures (d3d12_vulkan_device,d3d12_indirect_dispatch_count,d3d12_depth_resolve).python -I scripts/check_no_game_data.pyon both files andgit diff --check: clean.AI-assisted: yes (Claude Code)
Checklist
docs/design.md, with how it was confirmed.🤖 Generated with Claude Code