From 823c3caf4fdccd351992d803b26cc28c9673b374 Mon Sep 17 00:00:00 2001 From: Celegans12 <53881228+Celegans12@users.noreply.github.com> Date: Mon, 5 Oct 2026 16:32:49 +0200 Subject: [PATCH] fix(relinker): keep Windows startup stacks below the guest arena The PE writer marked relocatable executables HIGH_ENTROPY_VA. Windows then places the main thread stack, TEB, PEB and the loader threads' stacks, which exist before libc reserves the guest arena from 0x2_0000_0000, anywhere in the first TiB. Nearly half the time they land inside the arena and stay there as holes that fixed guest mappings cannot use. DllCharacteristics is now 0x140 (DYNAMIC_BASE, NX_COMPAT). The image is still relocated and bottom-up allocations keep the 8-bit randomization, but they start near the bottom of the address space, far below the arena. DLL placement does not depend on this flag. windows_address_space checks the flags and, on Windows, runs a relinked guest that returns the high dword of its stack pointer. Co-Authored-By: Claude Opus 5.5 --- core/relinker/CMakeLists.txt | 2 + .../src/windows/WindowsPeWriter.cpp | 2 +- .../tests/test_windows_address_space.py | 45 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 core/relinker/relinker/tests/test_windows_address_space.py diff --git a/core/relinker/CMakeLists.txt b/core/relinker/CMakeLists.txt index a388ec7a3..c704aaf97 100644 --- a/core/relinker/CMakeLists.txt +++ b/core/relinker/CMakeLists.txt @@ -202,6 +202,8 @@ if(BUILD_TESTING) ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_linux_entry_argv.py $) add_test(NAME windows_gui COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_gui.py $) + add_test(NAME windows_address_space COMMAND ${Python3_EXECUTABLE} + ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_address_space.py $) add_test(NAME windows_icon COMMAND ${Python3_EXECUTABLE} ${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_icon.py $) add_test(NAME guest_intel_trampolines COMMAND ${Python3_EXECUTABLE} diff --git a/core/relinker/elfpatcher/src/windows/WindowsPeWriter.cpp b/core/relinker/elfpatcher/src/windows/WindowsPeWriter.cpp index 61c0a567d..3c4e9ac34 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsPeWriter.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsPeWriter.cpp @@ -30,7 +30,7 @@ std::vector WindowsPeWriter::Write(const std::vector& s Io::WriteU16(result, optionalOffset + 50, 2); Io::WriteU32(result, optionalOffset + 60, LoadRva); Io::WriteU16(result, optionalOffset + 68, windowsGui ? 2 : 3); - Io::WriteU16(result, optionalOffset + 70, directories[5].Size == 0 ? 0x100 : 0x160); + Io::WriteU16(result, optionalOffset + 70, directories[5].Size == 0 ? 0x100 : 0x140); Io::WriteU64(result, optionalOffset + 72, 0x210000); Io::WriteU64(result, optionalOffset + 80, 0x200000); Io::WriteU64(result, optionalOffset + 88, 0x100000); diff --git a/core/relinker/relinker/tests/test_windows_address_space.py b/core/relinker/relinker/tests/test_windows_address_space.py new file mode 100644 index 000000000..d2720daf6 --- /dev/null +++ b/core/relinker/relinker/tests/test_windows_address_space.py @@ -0,0 +1,45 @@ +"""Keep the main thread stack that Windows creates before libc reserves the guest arena below it.""" + +import os +from pathlib import Path +import struct +import subprocess +import sys +import tempfile + +from test_optional_plt import fixture + +GUEST_ARENA_START = 0x200000000 +DYNAMIC_BASE = 0x40 +NX_COMPAT = 0x100 + + +def dll_characteristics(pe): + pe_offset = struct.unpack_from("> 32, \ + f"stack not below {GUEST_ARENA_START:#x}: exit code {executed.returncode:#x}" + print("Windows address space integration tests passed") + + +if __name__ == "__main__": + main()