From abdf673b1ee5f1feaaaf2f23203514a7b36f448b Mon Sep 17 00:00:00 2001 From: "husari.orozco" Date: Mon, 5 Oct 2026 16:16:46 -0600 Subject: [PATCH] feat(relinker): support Windows guest TLS reads by ALU instructions Recognize the read-direction ALU forms (add, or, adc, sbb, and, sub, xor, cmp) that read a qword from fs:[disp32], such as the stack canary check `xor rbx, fs:[0x28]` emitted by stack-protected functions. They were rejected as unsupported, which blocked titles relinked on Windows. The stub loads the TLS value and applies the operation in place, keeping the final operation as the last flag-setting instruction and preserving the accumulator and counter. A destination of rax uses rdx as the value scratch so the original accumulator survives. Refs #217 --- .../src/windows/WindowsTlsBuilder.cpp | 55 +++++++++++++------ .../tests/test_tls_function_coverage.py | 34 ++++++++++-- 2 files changed, 68 insertions(+), 21 deletions(-) diff --git a/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp b/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp index 7a7b94fd4..a38b93257 100644 --- a/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp +++ b/core/relinker/elfpatcher/src/windows/WindowsTlsBuilder.cpp @@ -24,8 +24,13 @@ struct TlsAccess { std::uint32_t Immediate; std::uint8_t Register; std::uint32_t Displacement; + std::uint8_t AluOpcode; }; +bool isAluReadOpcode(std::uint8_t opcode) { + return opcode == 0x03 || opcode == 0x0b || opcode == 0x13 || opcode == 0x1b || opcode == 0x23 || opcode == 0x2b || opcode == 0x33 || opcode == 0x3b; +} + void patchAccess(std::vector& sections, const TlsAccess& access, const std::uint32_t target) { for (auto& section : sections) { if (access.Rva < section.Rva || access.Rva - section.Rva >= section.Data.size()) @@ -85,7 +90,8 @@ PeDirectory WindowsTlsBuilder::Build(const std::vector& source, co const auto loadRegister = info.Length - position == 7 ? static_cast(((bytes[position + 1] >> 3) & 7) | ((info.RexPrefix & 4) << 1)) : std::uint8_t{4}; const bool loadValue = supportedPrefixes && (info.RexPrefix == 0x48 || info.RexPrefix == 0x4c) && loadRegister != 4 && bytes[position] == 0x8b && (bytes[position + 1] & 0xc7) == 0x04 && bytes[position + 2] == 0x25; const bool storeImmediate = supportedPrefixes && !hasOperandSizePrefix && (info.RexPrefix == 0 || info.RexPrefix == 0x40) && info.Length - position == 11 && bytes[position] == 0xc7 && bytes[position + 1] == 0x04 && bytes[position + 2] == 0x25 && Io::ReadU32(source, header.Offset + offset + position + 3) == 0x28; - if (!loadValue && !storeImmediate) { + const bool aluRead = supportedPrefixes && (info.RexPrefix == 0x48 || info.RexPrefix == 0x4c) && loadRegister != 4 && info.Length - position == 7 && isAluReadOpcode(bytes[position]) && (bytes[position + 1] & 0xc7) == 0x04 && bytes[position + 2] == 0x25; + if (!loadValue && !storeImmediate && !aluRead) { std::ostringstream message; message << "Unsupported Windows guest TLS instruction (bytes:" << std::hex << std::setfill('0'); for (std::size_t index = 0; index < info.Length; ++index) @@ -93,7 +99,7 @@ PeDirectory WindowsTlsBuilder::Build(const std::vector& source, co message << ')'; throw Domain::RelinkerException(message.str(), header.Offset + offset); } - accesses.push_back({rva, header.Offset + offset, info.Length, storeImmediate, storeImmediate ? Io::ReadU32(source, header.Offset + offset + position + 7) : 0, storeImmediate ? std::uint8_t{0} : loadRegister, storeImmediate ? 0 : Io::ReadU32(source, header.Offset + offset + position + 3)}); + accesses.push_back({rva, header.Offset + offset, info.Length, storeImmediate, storeImmediate ? Io::ReadU32(source, header.Offset + offset + position + 7) : 0, storeImmediate ? std::uint8_t{0} : loadRegister, storeImmediate ? 0 : Io::ReadU32(source, header.Offset + offset + position + 3), aluRead ? bytes[position] : std::uint8_t{0}}); } } } @@ -155,24 +161,39 @@ PeDirectory WindowsTlsBuilder::Build(const std::vector& source, co if (target != branchTargets.end() && *target < access.Rva + access.Length) throw Domain::RelinkerException("Branch enters a guest TLS instruction", *target); patchAccess(sections, access, code.GetRva()); - const bool preserveAccumulator = access.StoreImmediate || access.Register != 0; - const bool preserveCounter = access.Register != 1; code.Emit({0x48, 0x8d, 0x64, 0x24, 0x80}); - if (preserveCounter) code.Emit({0x51}); - if (preserveAccumulator) code.Emit({0x50}); - loadPointer(); - if (!access.StoreImmediate && access.Displacement != 0) { - code.Emit({0x48, 0x8b, 0x80}); + if (access.AluOpcode != 0 && access.Register == 0) { + code.Emit({0x52}); + code.Emit({0x51}); + loadPointer(); + code.Emit({0x48, 0x8b, 0x90}); code.U32(access.Displacement); + code.Emit({0x48, access.AluOpcode, 0xc2}); + code.Emit({0x59}); + code.Emit({0x5a}); + } else { + const bool preserveAccumulator = access.StoreImmediate || access.Register != 0; + const bool preserveCounter = access.Register != 1; + if (preserveCounter) code.Emit({0x51}); + if (preserveAccumulator) code.Emit({0x50}); + loadPointer(); + if (access.AluOpcode != 0) { + code.Emit({0x48, 0x8b, 0x80}); + code.U32(access.Displacement); + code.Emit({static_cast(0x48 | ((access.Register >> 3) << 2)), access.AluOpcode, static_cast(0xc0 | ((access.Register & 7) << 3))}); + } else if (!access.StoreImmediate && access.Displacement != 0) { + code.Emit({0x48, 0x8b, 0x80}); + code.U32(access.Displacement); + } + if (access.StoreImmediate) { + code.Emit({0xc7, 0x40, 0x28}); + code.U32(access.Immediate); + } else if (access.AluOpcode == 0 && access.Register != 0) { + code.Emit({static_cast(0x48 | (access.Register >> 3)), 0x89, static_cast(0xc0 | (access.Register & 7))}); + } + if (preserveAccumulator) code.Emit({0x58}); + if (preserveCounter) code.Emit({0x59}); } - if (access.StoreImmediate) { - code.Emit({0xc7, 0x40, 0x28}); - code.U32(access.Immediate); - } else if (access.Register != 0) { - code.Emit({static_cast(0x48 | (access.Register >> 3)), 0x89, static_cast(0xc0 | (access.Register & 7))}); - } - if (preserveAccumulator) code.Emit({0x58}); - if (preserveCounter) code.Emit({0x59}); code.Emit({0x48, 0x8d, 0xa4, 0x24, 0x80, 0, 0, 0}); code.Rip({0xe9}, CheckedRva(access.Rva + access.Length)); } diff --git a/core/relinker/relinker/tests/test_tls_function_coverage.py b/core/relinker/relinker/tests/test_tls_function_coverage.py index f39c91929..4d68200c2 100644 --- a/core/relinker/relinker/tests/test_tls_function_coverage.py +++ b/core/relinker/relinker/tests/test_tls_function_coverage.py @@ -30,6 +30,13 @@ def fs_load(register, displacement): return bytes([0x64, 0x48 | (register >> 3) << 2, 0x8b, 0x04 | (register & 7) << 3, 0x25]) + struct.pack("> 3) << 2, opcode, 0x04 | (register & 7) << 3, 0x25]) + struct.pack("> 3) << 2, opcode, 0xc0 | (register & 7) << 3]) if register else bytes([0x48, opcode, 0xc2]) + assert value_load in stub, (case, stub.hex()) + assert operation in stub, (case, stub.hex()) rejected = { "rsp-displacement": fs_load(4, 40), + "rsp-alu": fs_alu(0x33, 4, 40), "dword-load": bytes.fromhex("64 8b 04 25 28 00 00 00"), "register-address": bytes.fromhex("64 48 8b 00"), "gs-load": bytes.fromhex("65 48 8b 04 25 28 00 00 00"), "rex-b-load": bytes.fromhex("64 49 8b 04 25 28 00 00 00"), "rex-x-load": bytes.fromhex("64 4a 8b 04 25 28 00 00 00"), - "compare": bytes.fromhex("64 48 3b 04 25 28 00 00 00"), - "subtract": bytes.fromhex("64 48 2b 04 25 28 00 00 00"), - "add": bytes.fromhex("64 48 03 04 25 28 00 00 00"), - "xor": bytes.fromhex("64 48 33 04 25 28 00 00 00"), } for name, instruction in rejected.items(): convert(name, make_image("register", "unwind", body=instruction + b"\xc3"),