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 <noreply@anthropic.com>
This commit is contained in:
Celegans12
2026-10-05 16:32:49 +02:00
parent e248f404ef
commit 823c3caf4f
3 changed files with 48 additions and 1 deletions
+2
View File
@@ -202,6 +202,8 @@ if(BUILD_TESTING)
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_linux_entry_argv.py $<TARGET_FILE:relinker>)
add_test(NAME windows_gui COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_gui.py $<TARGET_FILE:relinker>)
add_test(NAME windows_address_space COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_address_space.py $<TARGET_FILE:relinker>)
add_test(NAME windows_icon COMMAND ${Python3_EXECUTABLE}
${CMAKE_CURRENT_SOURCE_DIR}/relinker/tests/test_windows_icon.py $<TARGET_FILE:relinker>)
add_test(NAME guest_intel_trampolines COMMAND ${Python3_EXECUTABLE}
@@ -30,7 +30,7 @@ std::vector<std::uint8_t> WindowsPeWriter::Write(const std::vector<PeSection>& 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);
@@ -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("<I", pe, 0x3c)[0]
return struct.unpack_from("<H", pe, pe_offset + 24 + 70)[0]
def main():
relinker = Path(sys.argv[1]).resolve()
with tempfile.TemporaryDirectory(prefix="anyps5-address-space-") as directory:
work = Path(directory)
image = fixture()
image[0x210:0x218] = bytes.fromhex("4889e0" "48c1e820" "c3")
source = work / "input.elf"
source.write_bytes(image)
output = work / "output.exe"
result = subprocess.run([str(relinker), "--skip-sce-module", "--windows", str(source), str(output)],
capture_output=True, text=True, timeout=20)
assert result.returncode == 0, (result.stdout, result.stderr)
characteristics = dll_characteristics(output.read_bytes())
assert characteristics == DYNAMIC_BASE | NX_COMPAT, f"DllCharacteristics {characteristics:#x}"
if os.name == "nt":
for _ in range(8):
executed = subprocess.run([str(output)], capture_output=True, text=True, timeout=20)
assert executed.returncode < GUEST_ARENA_START >> 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()