Skip to content

pe_loader: an image with sub-page section alignment keeps executable code - #8513

Open
MartinIndra02 wants to merge 1 commit into
ItsNotRexed:mainfrom
MartinIndra02:fix/pe-loader-low-section-alignment
Open

MartinIndra02 wants to merge 1 commit into
ItsNotRexed:mainfrom
MartinIndra02:fix/pe-loader-low-section-alignment

Conversation

@MartinIndra02

Copy link
Copy Markdown

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. VirtualProtect works on whole pages, so when an image's SectionAlignment is 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 ended PAGE_READONLY (0x2, from .reloc), so the first call into the image faults under DEP. With a writable .data last it would be PAGE_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:512 and loaded with LoadLibrary: the header, code and data pages all report PAGE_EXECUTE_WRITECOPY (0x80), the export runs, and the data can be written. The fix does the same when SectionAlignment < dwPageSize: the whole image gets PAGE_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_test gains test_low_section_alignment. It builds a synthetic PE32+ DLL byte by byte, with SectionAlignment and FileAlignment set to 0x200 and .text (mov eax, 42; ret), .data and an empty .reloc block all inside the first page. The test loads the DLL with rt::Loader, checks with VirtualQuery that 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 (ctest pe_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_test after 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.
  • Full ctest on 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.py on both files and git diff --check: clean.

AI-assisted: yes (Claude Code)

Checklist

  • No game data, keys or decryption (CONTRIBUTING.md), including in logs or reports quoted here.
  • A new translator rule is in the contract table of docs/design.md, with how it was confirmed.

🤖 Generated with Claude Code

…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>
@lohant27-netizen

Copy link
Copy Markdown

Tested 5dd8a17dc on Windows 11 (10.0.26300), NVIDIA GeForce RTX 5070 (driver 32.0.16.2101, subgroup 32), MSVC with SDK 10.0.26100, Release, merged onto main 867d922f0 together with #8542, #8529, #8517, #8513, #8512 and #8530 (no conflicts): pass.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants