Free fork of NotProton: Windows Steam games in Mac Steam, on CodeWeavers' Wine and Apple's Game Porting Toolkit
Swift apple-silicond3dmetaldxmtdxvkgame-porting-toolkitgptkmac-gamingmacosmetalfxrosetta-2steamsteam-playwinewinecx
feat(ntdll): check shipped detours read the pinned wm, refuse unsafe wm (#1782)
Raystorms789f03c756a2
diff --git a/Makefile b/Makefileindex 0b74d29..bdd1b29 100644--- a/Makefile+++ b/Makefile@@ -84,7 +84,7 @@ DEPS := $(OBJS:.o=.d)anchorcheck callscheck sigdb-fixtures webpatch-fixtures peicon-fixtures panel-behavior app-tests \tests-list overlay-shim overlay-shim-install overlay-shim-tests \overlay-shim-bench iconmaker icon \- appinfo helpers-install ntdll-resolve bridge runcheck compatcheck \+ appinfo helpers-install ntdll-resolve detourcheck bridge runcheck compatcheck \compatsvc-check prefixstate-check shortcutcheck scriptcheck envcheck buildcheck settingscheck routecheck bridgecheck envpasscheck prefixnotecheck powernotecheck exitcheck anticheatcheck launchprogresscheck gamedrivecheck \gamefixescheck launchercheck padcheck launch-shell activatecheck stamp-check versioncheck cicheck compat-reported-check FORCE@@ -99,7 +99,7 @@ dylib: $(TARGET)$(APP_PAYLOAD):@mkdir -p $(APP_PAYLOAD)-TEST_PATHS := dylib/tests overlay-shim/tests app/Tests scripts/tests+TEST_PATHS := dylib/tests overlay-shim/tests app/Tests scripts/tests ntdll-patch/teststests-list:@for p in $(TEST_PATHS); do echo $$p; done@@ -483,6 +483,13 @@ panel-behavior:CX_ROOT ?= /Applications/CrossOver Preview.app+# Every shipped x86 detour payload hands the detour the register its ntdll keeps wm in,+# and resolve.py refuses a wm the shims cannot read. No capstone needed; the two tests+# that disassemble skip without it.+detourcheck:+ @if [ ! -f ntdll-patch/shimcheck.py ]; then $(call SKIP,detourcheck,ntdll-patch/shimcheck.py); exit 0; fi; \+ python3 ntdll-patch/shimcheck.py && python3 -m unittest discover -s ntdll-patch/tests+ntdll-resolve:@if [ ! -f ntdll-patch/resolve.py ]; then \$(call SKIP,ntdll-resolve,ntdll-patch/resolve.py); exit 0; fi; \@@ -917,7 +924,7 @@ CI_CHECKS := mincheck l10ncheck \sigcheck callscheck np-test-batch-check webpatch-fixtures peicon-fixtures panel-behavior spawn-env \activatecheck compat-reported-import-check compatcheck compatsvc-check prefixstate-check shortcutcheck launch-shell envcheck \buildcheck settingscheck routecheck bridgecheck envpasscheck prefixnotecheck powernotecheck exitcheck anticheatcheck launchprogresscheck gamedrivecheck launchercheck \- seedcheck gamefixescheck versioncheck cicheck padcheck compat-reported-check np-inventory-check np-native-tests native-shim-tests \+ seedcheck gamefixescheck versioncheck cicheck padcheck detourcheck compat-reported-check np-inventory-check np-native-tests native-shim-tests \app-tests native-installercheck# app.yml runs the checks that need the built app in its build job and the rest in adiff --git a/docs/UPSTREAM.md b/docs/UPSTREAM.mdindex a153ec4..3ec79ca 100644--- a/docs/UPSTREAM.md+++ b/docs/UPSTREAM.md@@ -79,6 +79,8 @@ To support another WineHQ build, run `ntdll-patch/resolve.py` against its Wine tpayloads with `ntdll-patch/build.sh` / `build32.sh`, and add the hashes to `SupportedRunners`,`NtdllPatcher.byBuild` and `FreeEngine`.+- Any new or bumped engine needs a 32-bit game smoke test: launch a 32-bit game from Steam on it and check that it reaches Steam. `make detourcheck` only proves each detour reads the register its pin names.+Please read NOTICE for license information.Please open issue reports with any issues. PRs are welcome and encouraged.diff --git a/docs/WINECX.md b/docs/WINECX.mdindex 4650e84..ded74dd 100644--- a/docs/WINECX.md+++ b/docs/WINECX.md@@ -385,6 +385,8 @@ python3 resolve.py ../Libraries/Wine # self-tests against PINNEDpython3 apply.py <ntdll.dll> <out> <detour bin> # patched hashes```+- Any new or bumped engine needs a 32-bit game smoke test: launch a 32-bit game from Steam on it and check that it reaches Steam. `make detourcheck` only proves each detour reads the register its pin names.+4.7.3 (`winecx-11.17`): both detours fit in `.text` padding. The detour payloads andevery other shipped file are in [Shipped binaries](#shipped-binaries).diff --git a/ntdll-patch/resolve.py b/ntdll-patch/resolve.pyindex dbe5a12..a73ac54 100755--- a/ntdll-patch/resolve.py+++ b/ntdll-patch/resolve.py@@ -3,6 +3,7 @@# all archs. Internal development tool, uglyimport hashlib, pathlib, struct, sys, re, bisect, capstonefrom capstone import x86 as X86, arm64 as A64+from wmguard import refuse_unsafe, refuse_writesARCH = {0x8664: 'x86_64-windows', 0x14c: 'i386-windows', 0xaa64: 'aarch64-windows'}@@ -341,6 +342,12 @@ def steal(insns, k, need=5):return taken, total+def window(insns):+ """What wmguard.refuse_writes needs from each capstone instruction."""+ return [(i.address, f"{i.mnemonic} {i.op_str}".strip(), i.mnemonic,+ [i.reg_name(r) for r in i.regs_access()[1]]) for i in insns]++def resolve_amd64(pe):md = pe.cs()tvrva, text = pe.text()@@ -368,19 +375,22 @@ def resolve_amd64(pe):if len(calls) != 1:raise SystemExit(f"{pe.path}: {len(calls)} alloc_module calls in build_module, want 1")- k, wm = calls[0] + 1, None+ k, wm, wm_k = calls[0] + 1, None, Nonewhile True:i = body[k]if i.mnemonic == 'test' and i.op_str == 'rax, rax':k += 1elif i.mnemonic == 'mov' and i.op_str.endswith(', rax'):- wm = i.op_str.split(',')[0].strip()+ wm, wm_k = i.op_str.split(',')[0].strip(), kk += 1elif i.mnemonic in ('je', 'jz'):k += 1else:breakhook = body[k]+ refuse_unsafe(pe.path, 'x86_64', wm)+ refuse_writes(pe.path, 'x86_64', wm, window(body[wm_k + 1:k]),+ f"between the move at {body[wm_k].address:#x} and the hook at {hook.address:#x}")slot = amd64_load_path(pe, body, hook.address)return {'build_module': bm, 'alloc_module': am, 'wm': wm, 'load_path': slot,'hookRVA': hook.address, 'stolen': hook.bytes.hex(), 'insn': f"{hook.mnemonic} {hook.op_str}"}@@ -494,6 +504,11 @@ def resolve_i386(pe):flags_slot = mem[0].mem.disptaken, total = steal(body, hook_k)+ # The shim reads wm at the hook, but it is found at the MODREF test below it.+ refuse_unsafe(pe.path, 'i386', wm)+ refuse_writes(pe.path, 'i386', wm, window(body[hook_k:anchor]),+ f"between the hook at {body[hook_k].address:#x} and the MODREF test at "+ f"{body[anchor].address:#x}")jk = Nonefor k in range(gate + 1, min(gate + 8, len(body))):diff --git a/ntdll-patch/shimcheck.py b/ntdll-patch/shimcheck.pynew file mode 100755index 0000000..eeef78b--- /dev/null+++ b/ntdll-patch/shimcheck.py@@ -0,0 +1,440 @@+#!/usr/bin/env python3+# Checks that every shipped x86 detour payload hands the detour the register its ntdll keeps+# the MODREF (wm) in. The payload pins only prove the bytes match a build; a payload that was+# never rebuilt after a shim change still matches its own pin (WineCX 11.15 i386 did).+#+# Each payload's opening runs symbolically up to its call into the detour, and the+# arguments are compared with resolve.py's PINNED entry for the ntdll. The ntdll is found by+# the payload's sha256, and the NtdllPatch that ships the file must agree with that entry on+# the hook and the cave. No capstone needed: anything the small decoder below does not know+# fails the check.+#+# shimcheck.py every detour32*.bin and detour2*.bin in Resources+# shimcheck.py [--root DIR] [file...] another checkout, or only these payloads+import argparse, ast, hashlib, pathlib, re, struct, sys++sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))+from wmguard import GPR # noqa: E402++RESOURCES = 'app/Sources/BridgeplayApp/Resources'+REGS = {32: GPR['i386'], 64: GPR['x86_64']}+++class Unsupported(Exception):+ pass+++class Machine:+ """Just enough x86 to follow a shim to its call, on symbolic values."""++ def __init__(self, bits):+ self.bits, self.w = bits, bits // 8+ self.regs = {r: r for r in REGS[bits]}+ self.sp = ('entry', 0) # the stack pointer as (base, offset)+ self.mem = {} # stack slots written so far+ self.aligned = 0++ def sp_name(self):+ return REGS[self.bits][4]++ def read(self, r):+ if r == self.sp_name():+ return f"sp:{self.sp[0]}{self.sp[1]:+#x}"+ return self.regs[r]++ def write(self, r, v):+ if r == self.sp_name():+ raise Unsupported(f"moves a value into {r}")+ self.regs[r] = v++ def slot(self, base, disp):+ if base == self.sp_name():+ return (self.sp[0], self.sp[1] + disp)+ v = self.regs.get(base, '')+ m = re.fullmatch(r'sp:(\w+)([+-]0x[0-9a-f]+)', v)+ if m:+ return (m.group(1), int(m.group(2), 16) + disp)+ return None++ def load(self, base, disp):+ if base == 'rip':+ return f"[rip{disp:+#x}]"+ s = self.slot(base, disp)+ if s is None:+ raise Unsupported(f"reads memory through {base}")+ return self.mem.get(s, f"[{s[0]}{s[1]:+#x}]")++ def store(self, base, disp, v):+ s = self.slot(base, disp) if base not in (None, 'rip') else None+ if s is None:+ raise Unsupported("writes memory outside the stack")+ self.mem[s] = v++ def push(self, v):+ self.sp = (self.sp[0], self.sp[1] - self.w)+ self.mem[self.sp] = v++ def pop(self):+ v = self.mem.get(self.sp, f"[{self.sp[0]}{self.sp[1]:+#x}]")+ self.sp = (self.sp[0], self.sp[1] + self.w)+ return v+++def modrm(code, o, bits, rex):+ """(reg field, operand, next offset); operand is ('reg', name) or ('mem', base, disp)."""+ names = REGS[bits]+ m = code[o]+ o += 1+ mod, reg, rm = m >> 6, ((m >> 3) & 7) | ((rex & 4) << 1), m & 7+ if mod == 3:+ return names[reg], ('reg', names[rm | ((rex & 1) << 3)]), o+ disp32 = False+ if rm == 4:+ sib = code[o]+ o += 1+ if (((sib >> 3) & 7) | ((rex & 2) << 2)) != 4:+ raise Unsupported("uses an indexed address")+ base = names[(sib & 7) | ((rex & 1) << 3)]+ if sib & 7 == 5 and mod == 0:+ base, disp32 = None, True+ elif rm == 5 and mod == 0:+ base, disp32 = ('rip' if bits == 64 else None), True+ else:+ base = names[rm | ((rex & 1) << 3)]+ if mod == 1:+ disp = struct.unpack_from('<b', code, o)[0]+ o += 1+ elif mod == 2 or disp32:+ disp = struct.unpack_from('<i', code, o)[0]+ o += 4+ else:+ disp = 0+ return names[reg], ('mem', base, disp), o+++def run(code, bits, limit=64):+ """Follow the shim to its first real call. Returns the machine at that call."""+ vm, o = Machine(bits), 0+ for _ in range(limit):+ at, rex = o, 0+ if bits == 64 and 0x40 <= code[o] <= 0x4f:+ rex = code[o]+ o += 1+ op = code[o]+ o += 1+ try:+ if 0x50 <= op <= 0x57:+ vm.push(vm.read(REGS[bits][(op - 0x50) | ((rex & 1) << 3)]))+ elif 0x58 <= op <= 0x5f:+ vm.write(REGS[bits][(op - 0x58) | ((rex & 1) << 3)], vm.pop())+ elif op == 0x60 and bits == 32:+ before = vm.read('esp')+ for r in REGS[32]:+ vm.push(before if r == 'esp' else vm.read(r))+ elif op == 0xe8:+ rel = struct.unpack_from('<i', code, o)[0]+ o += 4+ if rel:+ return vm, at+ vm.push('eip') # call $+5, for the load delta+ elif op in (0x89, 0x8b, 0x8d, 0x81, 0x83):+ if bits == 64 and not rex & 8:+ raise Unsupported("is not a 64-bit operation")+ reg, rm, o = modrm(code, o, bits, rex)+ if op == 0x89:+ if rm[0] == 'reg':+ vm.write(rm[1], vm.read(reg))+ else:+ vm.store(rm[1], rm[2], vm.read(reg))+ elif op == 0x8b:+ vm.write(reg, vm.read(rm[1]) if rm[0] == 'reg' else vm.load(rm[1], rm[2]))+ elif op == 0x8d:+ if rm[0] != 'mem':+ raise Unsupported("is a lea without an address")+ s = vm.slot(rm[1], rm[2]) if rm[1] not in (None, 'rip') else None+ vm.write(reg, f"sp:{s[0]}{s[1]:+#x}" if s else f"&[{rm[1]}{rm[2]:+#x}]")+ else:+ if op == 0x81:+ imm = struct.unpack_from('<i', code, o)[0]+ o += 4+ else:+ imm = struct.unpack_from('<b', code, o)[0]+ o += 1+ kind = {0: '+', 4: '&', 5: '-'}.get(REGS[bits].index(reg) & 7)+ if rm[0] != 'reg' or kind is None:+ raise Unsupported("is an arithmetic form the check does not model")+ if rm[1] == vm.sp_name():+ if kind == '&':+ vm.aligned += 1+ vm.sp = (f"aligned{vm.aligned}", 0)+ else:+ vm.sp = (vm.sp[0], vm.sp[1] + (imm if kind == '+' else -imm))+ else:+ vm.write(rm[1], f"({vm.read(rm[1])}{kind}{imm:#x})")+ else:+ raise Unsupported(f"is opcode {op:#04x}")+ except Unsupported as e:+ raise Unsupported(f"the instruction at +{at:#x} ({code[at:o + 4].hex()}) {e}")+ raise Unsupported(f"no call into the detour in the first {limit} instructions")+++def check(data, pin):+ """None if the payload hands the detour pin['wm'], else what it hands it instead."""+ wm = pin['wm']+ bits = 32 if wm in REGS[32] else 64+ try:+ vm, at = run(data, bits)+ except Unsupported as e:+ return f"cannot follow the shim: {e}"+ if bits == 32:+ # detour_build_module32(ctx, wm, fp), cdecl+ got_wm, got_fp = vm.load('esp', 4), vm.load('esp', 8)+ if got_wm != wm or got_fp != 'ebp':+ return (f"the detour call at +{at:#x} gets wm={got_wm} fp={got_fp}, "+ f"the pin says wm={wm} fp=ebp")+ return None+ # detour_build_module(ctx, wm, load_path), Win64: rcx rdx r8+ got_wm, got_lp = vm.read('rdx'), vm.read('r8')+ want_lp = f"[entry{pin['load_path']:+#x}]"+ if got_wm != wm or got_lp != want_lp:+ return (f"the detour call at +{at:#x} gets wm={got_wm} load_path={got_lp}, "+ f"the pin says wm={wm} load_path={want_lp}")+ return None+++def pinned(resolve_py):+ """resolve.py's PINNED table, read without importing it (that needs capstone)."""+ tree = ast.parse(pathlib.Path(resolve_py).read_text())+ for n in tree.body:+ if isinstance(n, ast.Assign) and getattr(n.targets[0], 'id', '') == 'PINNED':+ return ast.literal_eval(n.value)+ raise SystemExit(f"{resolve_py}: no PINNED table")+++def string_end(t, k):+ """If a Swift string literal starts at t[k], the index just past it, else None.++ Handles raw (#"..."#), multi-line and interpolated ("\(f("x"))") strings.+ """+ n = len(t[k:]) - len(t[k:].lstrip('#'))+ q = k + n+ if not t.startswith('"', q):+ return None+ quote = '"""' if t.startswith('"""', q) else '"'+ close, escape = quote + '#' * n, '\\' + '#' * n+ e = q + len(quote)+ while e < len(t):+ if t.startswith(close, e):+ return e + len(close)+ if t.startswith(escape, e):+ e += len(escape)+ if t.startswith('(', e):+ e = closing(t, e) + 1+ else:+ e += 1+ else:+ e += 1+ raise SystemExit(f"unterminated string at {k}")+++def comment_end(t, k):+ """If a Swift comment starts at t[k], the index just past it, else None."""+ if t.startswith('//', k):+ e = t.find('\n', k)+ return len(t) if e < 0 else e+ if not t.startswith('/*', k):+ return None+ depth, e = 0, k+ while e < len(t): # Swift block comments nest+ if t.startswith('/*', e):+ depth, e = depth + 1, e + 2+ elif t.startswith('*/', e):+ depth, e = depth - 1, e + 2+ if not depth:+ return e+ else:+ e += 1+ raise SystemExit(f"unterminated comment at {k}")+++def uncommented(text):+ """Swift source with its comments blanked, so a commented-out pin is not read."""+ out, k = [], 0+ while k < len(text):+ e = comment_end(text, k)+ if e is not None:+ out.append(' ')+ k = e+ continue+ e = string_end(text, k) if text[k] in '#"' else None+ if e is not None:+ out.append(text[k:e])+ k = e+ continue+ out.append(text[k])+ k += 1+ return ''.join(out)+++def closing(text, i):+ """Index of the bracket that closes text[i], skipping Swift strings and comments."""+ depth, k = 0, i+ while k < len(text):+ e = comment_end(text, k)+ if e is None and text[k] in '#"':+ e = string_end(text, k)+ if e is not None:+ k = e+ continue+ if text[k] in '([':+ depth += 1+ elif text[k] in ')]':+ depth -= 1+ if not depth:+ return k+ k += 1+ raise SystemExit(f"unbalanced bracket at {i}")+++def hexint(v):+ return int(v.replace('_', ''), 16)+++def swift_pins(sources):+ """What the app ships: (patches by payloadResource, clean ntdlls by build id and arch).++ A patch belongs to the build whose NtdllPatcher.byBuild entry or WinecxRelease holds it,+ and that build's RunnerBuild (rebuilds included) lists the clean ntdlls it patches.+ """+ patches, builds = {}, {}+ for f in sorted(pathlib.Path(sources).rglob('*.swift')):+ text = uncommented(f.read_text())+ owners = []+ for m in re.finditer(r'\bRunnerBuild\(', text):+ body = text[m.end():closing(text, m.end() - 1)]+ ver = re.search(r'\bbundleVersion:\s*"([^"]+)"', body)+ if not ver:+ continue+ flavor = re.search(r'\bflavor:\s*(?:nil|"([^"]+)")', body)+ bid = ver.group(1) + (f"-{flavor.group(1)}" if flavor and flavor.group(1) else '')+ clean = builds.setdefault(bid, {})+ for c in re.finditer(r'\bcleanNtdll:\s*\[', body):+ block = body[c.end():closing(body, c.end() - 1)]+ for arch, h in re.findall(r'\.(\w+):\s*"([0-9a-f]{64})"', block):+ clean.setdefault(arch, set()).add(h)+ table = re.search(r'\bstatic let byBuild\b[^=]*=\s*\[', text)+ if table:+ end = closing(text, table.end() - 1)+ for k in re.finditer(r'"([^"]+)":\s*\[', text[table.end():end]):+ a = table.end() + k.end() - 1+ owners.append((a, closing(text, a), k.group(1)))+ for m in re.finditer(r'\bWinecxRelease\(', text):+ a = m.end() - 1+ ver = re.search(r'\bRunnerBuild\(\s*bundleVersion:\s*"([^"]+)"', text[a:closing(text, a)])+ if ver:+ owners.append((a, closing(text, a), ver.group(1)))+ for m in re.finditer(r'\bNtdllPatch\(', text):+ body = text[m.end():closing(text, m.end() - 1)]+ res = re.search(r'\bpayloadResource:\s*"([^"]+)"', body)+ if not res:+ continue+ field = {k: re.search(pat, body) for k, pat in (+ ('arch', r'\barch:\s*\.(\w+)'), ('sha', r'\bpayloadSHA256:\s*"([0-9a-f]{64})"'),+ ('cave', r'\bcaveRVA:\s*(0x[0-9a-fA-F_]+)'), ('hook', r'NtdllHook\(rva:\s*(0x[0-9a-fA-F_]+)'))}+ missing = [k for k, v in field.items() if not v]+ if missing:+ raise SystemExit(f"{f.name}: NtdllPatch for {res.group(1)} has no {', '.join(missing)}")+ inside = [o for o in owners if o[0] < m.start() < o[1]]+ patches.setdefault(res.group(1), []).append({+ 'arch': field['arch'].group(1), 'sha': field['sha'].group(1),+ 'cave': hexint(field['cave'].group(1)), 'hook': hexint(field['hook'].group(1)),+ 'build': min(inside, key=lambda o: o[1] - o[0])[2] if inside else None,+ 'file': f.name})+ return patches, builds+++def shipped(name, sha, carriers, ships, builds, pins):+ """None if every build that ships this payload patches an ntdll it was built for.++ carriers are the PINNED ntdlls whose payload is this one.+ """+ if not ships:+ return f"no NtdllPatch ships {name}"+ for s in ships:+ where = f"{s['file']} ({s['build'] or 'no build'})"+ if s['sha'] != sha:+ return f"{where} pins {name} as {s['sha'][:12]}, the file is {sha[:12]}"+ if s['build'] is None:+ return f"{s['file']}: cannot tell which build ships {name}"+ clean = builds.get(s['build'], {}).get(s['arch'], set())+ if not carriers & clean:+ return (f"{where} patches the {s['arch']} ntdll "+ f"{', '.join(sorted(h[:12] for h in clean)) or '(none pinned)'}, but this "+ f"payload was built for {', '.join(sorted(h[:12] for h in carriers))}")+ for h in sorted(clean & set(pins)):+ pin = pins[h]+ if pin.get('payload') != sha:+ return (f"{where} also patches {h[:12]}, whose PINNED payload is "+ f"{str(pin.get('payload'))[:12]}, not this one")+ if (s['hook'], s['cave']) != (pin.get('hookRVA'), pin.get('caveRVA')):+ return (f"{where} hooks {s['hook']:#x} with the cave at {s['cave']:#x}, but "+ f"{h[:12]}, which this payload was built for, hooks "+ f"{pin.get('hookRVA', 0):#x} with the cave at {pin.get('caveRVA', 0):#x}")+ return None+++def main(argv):+ ap = argparse.ArgumentParser(description='Check that shipped x86 detours read the pinned wm.')+ ap.add_argument('--root', default=str(pathlib.Path(__file__).resolve().parent.parent),+ help='the checkout whose resolve.py, Swift pins and payloads to use')+ ap.add_argument('payloads', nargs='*')+ a = ap.parse_args(argv)+ root = pathlib.Path(a.root)+ pins = pinned(root / 'ntdll-patch/resolve.py')+ by_payload = {}+ for k, v in pins.items():+ if 'payload' in v:+ by_payload.setdefault(v['payload'], set()).add(k)+ patches, builds = swift_pins(root / 'app/Sources')+ files = [pathlib.Path(p) for p in a.payloads] or sorted(+ p for g in ('detour32*.bin', 'detour2*.bin') for p in (root / RESOURCES).glob(g))+ if not files:+ raise SystemExit(f"no detour payloads under {root / RESOURCES}")++ bad = 0+ for f in files:+ data = f.read_bytes()+ sha = hashlib.sha256(data).hexdigest()+ carriers = by_payload.get(sha, set())+ problem = None if carriers else f"no PINNED ntdll in resolve.py carries payload {sha[:12]}"+ problem = problem or shipped(f.stem, sha, carriers, patches.get(f.stem), builds, pins)+ # Every pinned ntdll this payload goes into, or was built for, must keep wm where the+ # shim reads it.+ ntdlls = sorted(carriers | {h for s in patches.get(f.stem, []) if s['build']+ for h in builds.get(s['build'], {}).get(s['arch'], set())+ if h in pins})+ for h in ntdlls:+ if problem:+ break+ if 'wm' not in pins[h]:+ problem = f"PINNED {h[:12]} is not an x86 build"+ elif check(data, pins[h]):+ problem = f"for ntdll {h[:12]}, {check(data, pins[h])}"+ if problem:+ bad += 1+ print(f"FAIL {f.name}: {problem}")+ continue+ print(f"ok {f.name}: " + ', '.join(f"ntdll {h[:12]} keeps wm in {pins[h]['wm']}"+ for h in ntdlls) + ", and the shim hands the detour "+ + '/'.join(sorted({pins[h]['wm'] for h in ntdlls})))+ for s in patches[f.stem]:+ for h in sorted(builds[s['build']][s['arch']] - set(pins)):+ print(f" {s['build']} also patches {h[:12]}, which resolve.py does not "+ f"pin, so its wm is not checked")+ print(f"==> detourcheck: {len(files) - bad} of {len(files)} payloads read the pinned wm"+ + ("" if not bad else f", {bad} FAILED"))+ return 1 if bad else 0+++if __name__ == '__main__':+ sys.exit(main(sys.argv[1:]))diff --git a/ntdll-patch/tests/test_shimcheck.py b/ntdll-patch/tests/test_shimcheck.pynew file mode 100644index 0000000..851f79c--- /dev/null+++ b/ntdll-patch/tests/test_shimcheck.py@@ -0,0 +1,223 @@+# shimcheck.py on the shipped payloads, on the 11.15 and 11.17 payloads from before their+# fixes, and on payloads for a wm the shim cannot read.+import hashlib, io, pathlib, shutil, sys, tempfile, unittest+from contextlib import redirect_stdout++HERE = pathlib.Path(__file__).resolve().parent+ROOT = HERE.parent.parent+sys.path.insert(0, str(HERE.parent))+import shimcheck # noqa: E402++RES = ROOT / shimcheck.RESOURCES+PINS = shimcheck.pinned(ROOT / 'ntdll-patch/resolve.py')+BY_PAYLOAD = {v['payload']: v for v in PINS.values() if 'payload' in v}+++def payload(name):+ data = (RES / name).read_bytes()+ return bytearray(data), BY_PAYLOAD[hashlib.sha256(data).hexdigest()]+++def quiet(argv):+ out = io.StringIO()+ with redirect_stdout(out):+ rc = shimcheck.main(argv)+ return rc, out.getvalue()+++class Shipped(unittest.TestCase):+ def test_every_shipped_payload_reads_its_pinned_wm(self):+ rc, out = quiet([])+ self.assertEqual(rc, 0, out)+ self.assertEqual(out.count('\nok ') + out.startswith('ok '), 20, out)+++class BeforeTheFix(unittest.TestCase):+ # The pre-fix payloads differ from the shipped ones only in the order of the two moves+ # after `sub ecx, imm32`: mov ebx, ebp ran before mov esi, ebx.+ OLD = {'detour32-winecx-11.15.bin':+ '37daf5f795535df91bc8fcbd356f22f4fd1ab379ec4acc97ddff44037d265556', # before 7af57dc+ 'detour32-winecx-11.17.bin':+ '0676104e0bd56c4a1e8c473d9cbcd21fb2ba105fc7dc6cf5a70d37692c129e28'} # before aa17158++ def test_old_payloads_fail(self):+ for name, old_sha in self.OLD.items():+ with self.subTest(name):+ data, pin = payload(name)+ self.assertEqual(data[13:17], bytes.fromhex('89de89eb'))+ data[13:17] = bytes.fromhex('89eb89de')+ self.assertEqual(hashlib.sha256(data).hexdigest(), old_sha,+ 'not the byte stream that shipped before the fix')+ self.assertIsNone(shimcheck.check(bytes(payload(name)[0]), pin))+ problem = shimcheck.check(bytes(data), pin)+ self.assertIsNotNone(problem)+ self.assertIn('wm=ebp', problem)++ def test_old_payload_fails_the_whole_run(self):+ # A checkout where the old 11.17 payload ships with its old pins+ with tempfile.TemporaryDirectory() as d:+ d = pathlib.Path(d)+ shutil.copytree(ROOT / 'app/Sources', d / 'app/Sources')+ (d / 'ntdll-patch').mkdir()+ data, _ = payload('detour32-winecx-11.17.bin')+ new_sha = hashlib.sha256(data).hexdigest()+ data[13:17] = bytes.fromhex('89eb89de')+ old_sha = self.OLD['detour32-winecx-11.17.bin']+ (d / shimcheck.RESOURCES / 'detour32-winecx-11.17.bin').write_bytes(data)+ for rel in ('ntdll-patch/resolve.py', 'app/Sources/BridgeplayApp/Model/FreeEngineWinecx.swift'):+ src = (ROOT / rel).read_text()+ self.assertIn(new_sha, src)+ (d / rel).write_text(src.replace(new_sha, old_sha))+ rc, out = quiet(['--root', str(d)])+ self.assertEqual(rc, 1, out)+ self.assertIn('FAIL detour32-winecx-11.17.bin: for ntdll cc8a9035cea0, the detour call at +0x68 gets wm=ebp', out)+ self.assertIn('19 of 20', out)+++class Unreadable(unittest.TestCase):+ def test_i386_wm_in_ecx_fails_in_either_order(self):+ data, pin = payload('detour32-cx26.bin')+ self.assertEqual(data[13:17], bytes.fromhex('89eb89f6')) # mov ebx, ebp ; mov esi, esi+ pin = dict(pin, wm='ecx')+ for order in ('89eb89ce', '89ce89eb'): # mov esi, ecx before or after+ with self.subTest(order):+ data[13:17] = bytes.fromhex(order)+ problem = shimcheck.check(bytes(data), pin)+ self.assertIsNotNone(problem)+ self.assertNotIn('gets wm=ecx', problem)++ def test_x86_64_wm_the_shim_overwrites_fails(self):+ data, pin = payload('detour2.bin')+ at = 0x4f+ self.assertEqual(data[at:at + 3], bytes.fromhex('4889f2')) # mov rdx, rsi+ for reg, insn in (('rax', '4889c2'), ('rcx', '4889ca'), ('r10', '4c89d2')):+ with self.subTest(reg):+ data[at:at + 3] = bytes.fromhex(insn)+ problem = shimcheck.check(bytes(data), dict(pin, wm=reg))+ self.assertIsNotNone(problem)+ self.assertNotIn(f'gets wm={reg} ', problem)+ # rbx is this payload's scratch register, so it carries load_path by then+ data[at:at + 3] = bytes.fromhex('4889da') # mov rdx, rbx+ self.assertIn('gets wm=[entry+0xd0]', shimcheck.check(bytes(data), dict(pin, wm='rbx')))+ data[at:at + 3] = bytes.fromhex('4889fa') # mov rdx, rdi+ self.assertIsNone(shimcheck.check(bytes(data), dict(pin, wm='rdi')))++ def test_wrong_pin_fails(self):+ data, pin = payload('detour32-41069.bin')+ self.assertIsNone(shimcheck.check(bytes(data), pin))+ self.assertIn('wm=edi', shimcheck.check(bytes(data), dict(pin, wm='esi')))+ data, pin = payload('detour2-winehq-11.15.bin')+ self.assertIsNone(shimcheck.check(bytes(data), pin))+ self.assertIn('load_path', shimcheck.check(bytes(data), dict(pin, load_path=0x58)))++ def test_unknown_instruction_fails(self):+ data, pin = payload('detour32.bin')+ data[13] = 0x0f+ self.assertIn('cannot follow the shim', shimcheck.check(bytes(data), pin))+++class Pins(unittest.TestCase):+ def test_swift_pin_for_another_ntdll_fails(self):+ with tempfile.TemporaryDirectory() as d:+ d = pathlib.Path(d)+ shutil.copytree(ROOT / 'app/Sources', d / 'app/Sources')+ (d / 'ntdll-patch').mkdir()+ shutil.copy(ROOT / 'ntdll-patch/resolve.py', d / 'ntdll-patch/resolve.py')+ swift = d / 'app/Sources/BridgeplayApp/Model/FreeEngineWinecx.swift'+ text = swift.read_text()+ self.assertIn('NtdllHook(rva: 0x4f880,', text)+ swift.write_text(text.replace('NtdllHook(rva: 0x4f880,', 'NtdllHook(rva: 0x4f7c0,'))+ rc, out = quiet(['--root', str(d)])+ self.assertEqual(rc, 1, out)+ self.assertIn('FAIL detour32-winecx-11.17.bin: FreeEngineWinecx.swift (winecx-11.17) hooks 0x4f7c0', out)++ def test_bumped_engine_with_the_old_payload_fails(self):+ # A new ntdll keeps the hook and cave RVAs, but the old payload and patch are kept+ # for it: the payload still matches the old ntdll's pin, not the engine's.+ with tempfile.TemporaryDirectory() as d:+ d = pathlib.Path(d)+ shutil.copytree(ROOT / 'app/Sources', d / 'app/Sources')+ (d / 'ntdll-patch').mkdir()+ shutil.copy(ROOT / 'ntdll-patch/resolve.py', d / 'ntdll-patch/resolve.py')+ swift = d / 'app/Sources/BridgeplayApp/Model/FreeEngineWinecx.swift'+ old = 'cc8a9035cea0e26c5d333ef220f1becefc27ae44d2cdcdaea3278e8739d26fd9'+ text = swift.read_text()+ self.assertEqual(text.count(old), 1)+ swift.write_text(text.replace(old, 'ab' * 32))+ rc, out = quiet(['--root', str(d)])+ self.assertEqual(rc, 1, out)+ self.assertIn('FAIL detour32-winecx-11.17.bin: FreeEngineWinecx.swift (winecx-11.17) '+ 'patches the i386Windows ntdll abababababab, but this payload was built '+ 'for cc8a9035cea0', out)+ self.assertIn('19 of 20', out)++ def test_commented_out_clean_hash_is_not_read(self):+ old = 'cc8a9035cea0e26c5d333ef220f1becefc27ae44d2cdcdaea3278e8739d26fd9'+ for keep in ('// .i386Windows: "{old}",\n', '/* .i386Windows: "{old}", /* nested */ */\n'):+ with self.subTest(keep[:2]), tempfile.TemporaryDirectory() as d:+ d = pathlib.Path(d)+ shutil.copytree(ROOT / 'app/Sources', d / 'app/Sources')+ (d / 'ntdll-patch').mkdir()+ shutil.copy(ROOT / 'ntdll-patch/resolve.py', d / 'ntdll-patch/resolve.py')+ swift = d / 'app/Sources/BridgeplayApp/Model/FreeEngineWinecx.swift'+ text = swift.read_text()+ line = f'.i386Windows: "{old}",\n'+ self.assertEqual(text.count(line), 1)+ swift.write_text(text.replace(line, keep.format(old=old) + f'.i386Windows: "{"ab" * 32}",\n'))+ rc, out = quiet(['--root', str(d)])+ self.assertEqual(rc, 1, out)+ self.assertIn('patches the i386Windows ntdll abababababab, but this payload', out)++ def test_swift_lexing(self):+ src = ('a("x\\(f("y)"))z", #"q"z\\("#, """\nm "" \\(g(")"))\n""") // c )\n'+ '/* a /* b */ ) */ b ]')+ self.assertEqual(shimcheck.closing(src, 1), src.index('""")') + 3)+ self.assertNotIn('// c', shimcheck.uncommented(src))+ self.assertNotIn('/* a', shimcheck.uncommented(src))+ self.assertIn('"x\\(f("y)"))z"', shimcheck.uncommented(src))+ self.assertEqual(shimcheck.uncommented('x = "// not a comment" // one'), 'x = "// not a comment" ')+ for f in (ROOT / 'app/Sources').rglob('*.swift'):+ shimcheck.uncommented(f.read_text())++ def test_every_pinned_ntdll_the_payload_serves_is_checked(self):+ # The 26.3 China rebuild's i386 ntdll pinned with the cx26 payload, but keeping wm in+ # ebx: the payload hands over esi, so it is wrong for that ntdll.+ rebuild = '21e7d0a6d6868f1853489f5a9798e706509a20a65ee370f50b37ab7b1cdcaae3'+ cx26 = '2c60ee6b00dd13b7f6cb11017778a041ba6a321eaea194f1fa0dca7eab8403e2'+ for wm, rc_want in (('esi', 0), ('ebx', 1)):+ with self.subTest(wm), tempfile.TemporaryDirectory() as d:+ d = pathlib.Path(d)+ shutil.copytree(ROOT / 'app/Sources', d / 'app/Sources')+ (d / 'ntdll-patch').mkdir()+ src = (ROOT / 'ntdll-patch/resolve.py').read_text()+ entry = dict(PINS[cx26], wm=wm)+ (d / 'ntdll-patch/resolve.py').write_text(+ src.replace('PINNED = {\n', f'PINNED = {{\n {rebuild!r}: {entry!r},\n', 1))+ rc, out = quiet(['--root', str(d)])+ self.assertEqual(rc, rc_want, out)+ if rc_want:+ self.assertIn(f'FAIL detour32-cx26.bin: for ntdll {rebuild[:12]}, the detour '+ f'call at +0x68 gets wm=esi', out)+ else:+ self.assertIn(f'ntdll {rebuild[:12]} keeps wm in esi', out)++ def test_every_payload_has_its_build(self):+ patches, builds = shimcheck.swift_pins(ROOT / 'app/Sources')+ for f in RES.glob('detour*.bin'):+ with self.subTest(f.name):+ ships = patches.get(f.stem)+ self.assertTrue(ships)+ for s in ships:+ self.assertIn(s['arch'], builds[s['build']])++ def test_unpinned_payload_fails(self):+ with tempfile.TemporaryDirectory() as d:+ f = pathlib.Path(d) / 'detour32-x.bin'+ f.write_bytes(b'\x90' * 16)+ rc, out = quiet([str(f)])+ self.assertEqual(rc, 1, out)+ self.assertIn('no PINNED ntdll', out)+++if __name__ == '__main__':+ unittest.main()diff --git a/ntdll-patch/tests/test_wmguard.py b/ntdll-patch/tests/test_wmguard.pynew file mode 100644index 0000000..3e6bff2--- /dev/null+++ b/ntdll-patch/tests/test_wmguard.py@@ -0,0 +1,104 @@+# The resolver's refusals: a wm register the shim overwrites, and a wm register written+# between the hook and the place the resolver found it.+import pathlib, sys, unittest++HERE = pathlib.Path(__file__).resolve().parent+sys.path.insert(0, str(HERE.parent))+import wmguard # noqa: E402++try:+ import capstone+except ImportError:+ capstone = None+++def refused(fn, *args):+ try:+ fn(*args)+ except SystemExit as e:+ return str(e)+ return None+++class Unsafe(unittest.TestCase):+ def test_registers_the_shims_overwrite(self):+ for arch, regs in (('i386', ('ecx', 'esp')), ('x86_64', ('rax', 'rcx', 'r10', 'rsp'))):+ for r in regs:+ with self.subTest(r):+ self.assertIn(f'MODREF is in {r}', refused(wmguard.refuse_unsafe, 'nt', arch, r))++ def test_registers_the_shims_read(self):+ for arch, regs in (('i386', ('eax', 'edx', 'ebx', 'ebp', 'esi', 'edi')),+ ('x86_64', ('rbx', 'rsi', 'rdi', 'rdx', 'r8', 'r9', 'r11', 'r14'))):+ for r in regs:+ with self.subTest(r):+ self.assertIsNone(refused(wmguard.refuse_unsafe, 'nt', arch, r))++ def test_no_register(self):+ self.assertIn('no register', refused(wmguard.refuse_unsafe, 'nt', 'x86_64', None))+ self.assertIn('not a general register',+ refused(wmguard.refuse_unsafe, 'nt', 'x86_64', 'qword ptr [rsp + 0x20]'))+ self.assertIn('not a general register', refused(wmguard.refuse_unsafe, 'nt', 'i386', 'rbx'))+++class Writes(unittest.TestCase):+ def test_partial_names(self):+ for arch, wm, names in (+ ('i386', 'eax', ('al', 'ah', 'ax', 'eax')), ('i386', 'ebx', ('bl', 'bh', 'bx', 'ebx')),+ ('i386', 'ecx', ('cl', 'ch', 'cx', 'ecx')), ('i386', 'edx', ('dl', 'dh', 'dx', 'edx')),+ ('i386', 'esi', ('si', 'esi')), ('i386', 'edi', ('di', 'edi')),+ ('i386', 'ebp', ('bp', 'ebp')),+ ('x86_64', 'rbx', ('bl', 'bh', 'bx', 'ebx', 'rbx')),+ ('x86_64', 'rsi', ('sil', 'si', 'esi', 'rsi')),+ ('x86_64', 'r14', ('r14b', 'r14w', 'r14d', 'r14'))):+ for n in names:+ self.assertEqual(wmguard.full(arch, n), wm, n)+ self.assertEqual(wmguard.full('i386', 'eflags'), 'eflags')++ def test_write_in_the_window(self):+ win = [(0x10, 'test byte ptr [ebp - 0x4c], 2', 'test', ['eflags']),+ (0x14, 'jne 0x40', 'jne', []),+ (0x16, 'mov eax, dword ptr [ebp - 0x20]', 'mov', ['eax'])]+ self.assertIsNone(refused(wmguard.refuse_writes, 'nt', 'i386', 'ebx', win, 'here'))+ for name in ('bl', 'bh', 'ebx'):+ got = refused(wmguard.refuse_writes, 'nt', 'i386', 'ebx',+ win + [(0x19, f'mov {name}, 1', 'mov', [name])], 'here')+ self.assertIn('ebx is written at 0x19', got)+ got = refused(wmguard.refuse_writes, 'nt', 'x86_64', 'r14',+ [(0x20, 'xor r14d, r14d', 'xor', ['r14d', 'rflags'])], 'here')+ self.assertIn('r14 is written at 0x20', got)++ def test_leaving_the_line(self):+ for m in ('jmp', 'call', 'ret'):+ got = refused(wmguard.refuse_writes, 'nt', 'i386', 'ebx',+ [(0x30, f'{m} 0x50', m, ['esp'])], 'here')+ self.assertIn('not a straight line', got)++[email protected](capstone is None, 'capstone not installed (pip3 install capstone)')+class WithCapstone(unittest.TestCase):+ # resolve.window turns capstone's write sets into what refuse_writes reads. The bytes are+ # WineCX 11.17's i386 hook up to its MODREF test.+ HOOK = bytes.fromhex('f645b402' '7529' '8b45e0' 'f6401720' '0f842d020000')++ def window(self, code):+ import resolve+ md = capstone.Cs(capstone.CS_ARCH_X86, capstone.CS_MODE_32)+ md.detail = True+ return resolve.window(list(md.disasm(code, 0x4f880)))++ def test_real_hook_passes(self):+ self.assertIsNone(refused(wmguard.refuse_writes, 'nt', 'i386', 'ebx',+ self.window(self.HOOK), 'here'))++ def test_reload_is_refused(self):+ for insn in ('8b5de0', '8a5de0', '8a7de0', '31db'): # mov ebx / bl / bh, xor ebx+ code = self.HOOK[:6] + bytes.fromhex(insn) + self.HOOK[9:]+ with self.subTest(insn):+ self.assertIn('ebx is written at 0x4f886',+ refused(wmguard.refuse_writes, 'nt', 'i386', 'ebx',+ self.window(code), 'here'))+++if __name__ == '__main__':+ unittest.main()diff --git a/ntdll-patch/wmguard.py b/ntdll-patch/wmguard.pynew file mode 100644index 0000000..d3fedf2--- /dev/null+++ b/ntdll-patch/wmguard.py@@ -0,0 +1,72 @@+# Which register may carry the MODREF (wm) into an entry shim, shared by resolve.py and+# shimcheck.py. No capstone here, so the checks that import it run without it.++# What each shim overwrites before it reads WM_REG. shim32.S: pushad moves esp and+# `pop ecx` takes the load delta. ebx is fine only because WM_IN_EBX reads it before fp+# goes in. shim.S: the pushes move rsp, r10 keeps the unaligned rsp, rax and rcx carry+# the ctx table.+UNSAFE = {+ 'i386': {'ecx': 'shim32.S pops the load delta into it first',+ 'esp': 'pushad has already moved it'},+ 'x86_64': {'rax': 'shim.S builds the ctx table in it first',+ 'rcx': 'shim.S puts &ctx in it first',+ 'r10': 'shim.S saves the unaligned rsp in it first',+ 'rsp': 'the shim has pushed ten registers by then'},+}++GPR = {+ 'i386': ['eax', 'ecx', 'edx', 'ebx', 'esp', 'ebp', 'esi', 'edi'],+ 'x86_64': ['rax', 'rcx', 'rdx', 'rbx', 'rsp', 'rbp', 'rsi', 'rdi',+ 'r8', 'r9', 'r10', 'r11', 'r12', 'r13', 'r14', 'r15'],+}++# Every name that writes part of a GPR, mapped to the GPR, so a write to bl or ebx counts+# as a write to ebx or rbx.+_PARTS = {}+for _n in 'abcd':+ _r = _n + 'x'+ for _p in (_n + 'l', _n + 'h', _r, 'e' + _r):+ _PARTS.setdefault('i386', {})[_p] = 'e' + _r+ _PARTS.setdefault('x86_64', {})[_p] = 'r' + _r+for _r in ('sp', 'bp', 'si', 'di'):+ for _p in (_r + 'l', _r, 'e' + _r):+ _PARTS['i386'][_p] = 'e' + _r+ _PARTS['x86_64'][_p] = 'r' + _r+for _n in range(8, 16):+ for _s in ('b', 'w', 'd', ''):+ _PARTS['x86_64'][f'r{_n}{_s}'] = f'r{_n}'+for _arch in GPR:+ for _r in GPR[_arch]:+ _PARTS[_arch][_r] = _r+++def full(arch, name):+ """The GPR a register name is part of, or the name itself if it is no GPR."""+ return _PARTS[arch].get(name, name)+++def refuse_unsafe(path, arch, wm):+ """Stop when the shim cannot read wm from this register."""+ if wm is None:+ raise SystemExit(f"{path}: no register holds the MODREF at the hook")+ if wm not in GPR[arch]:+ raise SystemExit(f"{path}: the MODREF is in {wm!r}, not a general register the shim can read")+ if wm in UNSAFE[arch]:+ raise SystemExit(f"{path}: the MODREF is in {wm}, but {UNSAFE[arch][wm]}, so the shim "+ f"cannot hand it to the detour. Change the shim before patching this build")+++def refuse_writes(path, arch, wm, window, where):+ """Stop when anything in window writes wm or leaves the straight line.++ window is (address, text, mnemonic, written register names) for every instruction+ between the point the shim reads wm and the point the resolver saw it, so wm must hold+ the same value at both.+ """+ for address, text, mnemonic, writes in window:+ if wm in {full(arch, w) for w in writes}:+ raise SystemExit(f"{path}: {wm} is written at {address:#x} ({text}), {where}, "+ f"so the shim would read another value than the one found")+ if mnemonic in ('jmp', 'ljmp', 'call', 'lcall', 'ret', 'retf', 'int', 'syscall', 'sysenter'):+ raise SystemExit(f"{path}: {text} at {address:#x}, {where}, so the way from the "+ f"hook to the MODREF is not a straight line")