mirror of
https://github.com/boykopovar/AnyPS5.git
synced 2026-10-10 01:28:44 +02:00
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
This commit is contained in:
@@ -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<PeSection>& 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<std::uint8_t>& source, co
|
||||
const auto loadRegister = info.Length - position == 7 ? static_cast<std::uint8_t>(((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<std::uint8_t>& 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<std::uint8_t>& 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<std::uint8_t>(0x48 | ((access.Register >> 3) << 2)), access.AluOpcode, static_cast<std::uint8_t>(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<std::uint8_t>(0x48 | (access.Register >> 3)), 0x89, static_cast<std::uint8_t>(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<std::uint8_t>(0x48 | (access.Register >> 3)), 0x89, static_cast<std::uint8_t>(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));
|
||||
}
|
||||
|
||||
@@ -30,6 +30,13 @@ def fs_load(register, displacement):
|
||||
return bytes([0x64, 0x48 | (register >> 3) << 2, 0x8b, 0x04 | (register & 7) << 3, 0x25]) + struct.pack("<i", displacement)
|
||||
|
||||
|
||||
ALU_READ = {"add": 0x03, "or": 0x0b, "adc": 0x13, "sbb": 0x1b, "and": 0x23, "sub": 0x2b, "xor": 0x33, "cmp": 0x3b}
|
||||
|
||||
|
||||
def fs_alu(opcode, register, displacement):
|
||||
return bytes([0x64, 0x48 | (register >> 3) << 2, opcode, 0x04 | (register & 7) << 3, 0x25]) + struct.pack("<i", displacement)
|
||||
|
||||
|
||||
def displacement_load(register, displacement, flags, round_trip=False):
|
||||
saved = (3, 5, 6, 7, 12, 13, 14, 15)
|
||||
code = bytearray()
|
||||
@@ -276,17 +283,36 @@ def main():
|
||||
convert(name, image, tls_address=address, displacement=displacement)
|
||||
for name, image, error in displacement_bounds_cases():
|
||||
convert(name, image, error, error_offset=0x1240)
|
||||
for name, opcode in ALU_READ.items():
|
||||
for register in (0, 3, 8, 13):
|
||||
for displacement in (0, 40, -8):
|
||||
body = fs_alu(opcode, register, displacement) + b"\xc3"
|
||||
case = f"alu-{name}-{register}-{displacement}"
|
||||
image = make_image("register", "unwind", body=body)
|
||||
source = work / (case + ".elf")
|
||||
output = source.with_suffix(".exe")
|
||||
source.write_bytes(image)
|
||||
result = subprocess.run([str(relinker), "--skip-sce-module", "--windows", str(source), str(output)],
|
||||
capture_output=True, text=True, timeout=30)
|
||||
assert result.returncode == 0, (case, result.stderr)
|
||||
pe = output.read_bytes()
|
||||
patched_address = 0x10000 + 0x1240
|
||||
patched = pe_bytes_at(pe, patched_address, 5)
|
||||
assert patched[0] == 0xe9, case
|
||||
stub_address = patched_address + 5 + struct.unpack_from("<i", patched, 1)[0]
|
||||
stub = pe_bytes_at(pe, stub_address, 96)
|
||||
value_load = bytes.fromhex("48 8b 80" if register else "48 8b 90") + struct.pack("<i", displacement)
|
||||
operation = bytes([0x48 | (register >> 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"),
|
||||
|
||||
Reference in New Issue
Block a user