Files

719 lines
31 KiB
C

#include <windows.h>
#pragma pack(8) /* Match accepted Win64 native ABI after Wine SDK header. */
#include <stdint.h>
#include <string.h>
#include "direct_two_site_install.h"
#include "constructor_seam_plan.h"
#include "realm_install_diagnostic.h"
#include "login_site_pages.h"
extern void gw_direct_login_thunk(void);
extern void gw_bridge_nohelper_log(uint32_t event, uint32_t result,
uint32_t error);
/* Isolated diagnostic: original wrapper setup remains intact. Only the exact
* first 127.0.0.1:1119 dispatcher caller bypasses C49 socket creation. */
static const uintptr_t front_dispatch_call = (uintptr_t)0x142253143ULL;
static const uint8_t front_dispatch_expected[5] =
{0xe8, 0x98, 0xf9, 0xff, 0xff};
static const uintptr_t front_function_start = (uintptr_t)0x142253090ULL;
#include "front_wrapper_preimage.private.h"
extern void gw_front_wrapper_thunk(void);
/* A call at the original site enters this thunk through a near absolute-jump
* relay. Stack and all argument state are restored before the unmatched tail
* jump to original C49. Matched calls return to wrapper continuation 0x148. */
__asm__(
".intel_syntax noprefix\n"
".text\n.p2align 4\n.globl gw_front_wrapper_thunk\n"
"gw_front_wrapper_thunk:\n"
"pushfq\n push rax\n push rcx\n push rdx\n push r8\n push r9\n push r10\n push r11\n"
"test rcx, rcx\n jz .Lgw_front_unmatched\n"
"cmp dword ptr [rsp + 0xa0], 0x4308180e\n jne .Lgw_front_unmatched\n"
"cmp dword ptr [rsp + 0xa4], 1\n jne .Lgw_front_unmatched\n"
"cmp dword ptr [rcx + 0x110], 1\n jne .Lgw_front_unmatched\n"
"cmp dword ptr [rcx + 0x100], 0x0100007f\n jne .Lgw_front_unmatched\n"
"cmp word ptr [rcx + 0x118], 0x045f\n jne .Lgw_front_unmatched\n"
/* Every exact front peer/caller match bypasses original C49. */
"mov eax, 1\n jmp .Lgw_front_log\n"
".Lgw_front_unmatched:\n xor eax, eax\n"
".Lgw_front_log:\n sub rsp, 0x88\n mov dword ptr [rsp + 0x80], eax\n"
"movdqu xmmword ptr [rsp + 0x20], xmm0\n"
"movdqu xmmword ptr [rsp + 0x30], xmm1\n"
"movdqu xmmword ptr [rsp + 0x40], xmm2\n"
"movdqu xmmword ptr [rsp + 0x50], xmm3\n"
"movdqu xmmword ptr [rsp + 0x60], xmm4\n"
"movdqu xmmword ptr [rsp + 0x70], xmm5\n"
"mov ecx, 81\n mov edx, eax\n xor r8d, r8d\n call gw_bridge_nohelper_log\n"
"movdqu xmm0, xmmword ptr [rsp + 0x20]\n"
"movdqu xmm1, xmmword ptr [rsp + 0x30]\n"
"movdqu xmm2, xmmword ptr [rsp + 0x40]\n"
"movdqu xmm3, xmmword ptr [rsp + 0x50]\n"
"movdqu xmm4, xmmword ptr [rsp + 0x60]\n"
"movdqu xmm5, xmmword ptr [rsp + 0x70]\n"
"cmp dword ptr [rsp + 0x80], 0\n je .Lgw_front_original\n"
"add rsp, 0x88\n pop r11\n pop r10\n pop r9\n pop r8\n pop rdx\n pop rcx\n pop rax\n popfq\n ret\n"
".Lgw_front_original:\n"
"add rsp, 0x88\n pop r11\n pop r10\n pop r9\n pop r8\n pop rdx\n pop rcx\n pop rax\n popfq\n"
".byte 0xff, 0x25, 0, 0, 0, 0\n.quad 0x142252ae0\n"
".att_syntax prefix\n"
);
static struct GwStartupPatch front_dispatch_patch;
static volatile LONG login_retired;
static const uintptr_t login_site = (uintptr_t)0x14242e199ULL;
static const uintptr_t realm_site = (uintptr_t)0x141e448adULL;
struct GwRealmInstallDiagnostic gw_realm_install_diagnostic;
static SIZE_T last_exact_read_got;
static DWORD last_exact_read_error;
static int exact_read(uintptr_t address, void *out, SIZE_T size)
{
SIZE_T got = 0;
BOOL ok = ReadProcessMemory(GetCurrentProcess(), (const void *)address,
out, size, &got);
last_exact_read_got = got;
last_exact_read_error = ok ? 0u : GetLastError();
return ok && got == size;
}
/* One pre-credential Windows loader-list witness. The host log receives only
* a full-width module base, PE image size and fixed identity bits. No path,
* original code, account data or loader status is exported. */
static WCHAR simple_lower(WCHAR value)
{
return value >= L'A' && value <= L'Z' ? value + (L'a' - L'A') : value;
}
static uint32_t module_path_identity(HMODULE loader)
{
static const WCHAR wanted[] = L"WowB_loader.dll";
WCHAR game_path[512], loader_path[512];
DWORD game_len = GetModuleFileNameW(NULL, game_path, 512);
DWORD loader_len = GetModuleFileNameW(loader, loader_path, 512);
DWORD game_dir = 0, loader_dir = 0, i;
uint32_t bits = 0;
if (!game_len || !loader_len || game_len >= 512 || loader_len >= 512)
return 0;
for (i = 0; i < game_len; ++i)
if (game_path[i] == L'\\' || game_path[i] == L'/') game_dir = i + 1;
for (i = 0; i < loader_len; ++i)
if (loader_path[i] == L'\\' || loader_path[i] == L'/') loader_dir = i + 1;
if (!game_dir || !loader_dir ||
loader_len - loader_dir != (sizeof(wanted) / sizeof(wanted[0])) - 1)
return 0;
for (i = 0; i < loader_len - loader_dir; ++i)
if (simple_lower(loader_path[loader_dir + i]) != simple_lower(wanted[i]))
return 0;
bits |= 1u;
if (game_dir != loader_dir) return bits;
for (i = 0; i < game_dir; ++i)
if (simple_lower(game_path[i]) != simple_lower(loader_path[i])) return bits;
return bits | 2u;
}
static void record_loader_base_once(void)
{
HMODULE loader = GetModuleHandleW(L"WowB_loader.dll");
uintptr_t base = (uintptr_t)loader;
IMAGE_DOS_HEADER dos = {0};
IMAGE_NT_HEADERS64 nt = {0};
MEMORY_BASIC_INFORMATION page = {0}, writer = {0};
uint32_t bits = 0, image_size = 0;
if (loader) {
bits |= module_path_identity(loader);
if (exact_read(base, &dos, sizeof(dos)) &&
dos.e_magic == IMAGE_DOS_SIGNATURE &&
dos.e_lfanew > 0 && dos.e_lfanew <= 4096 &&
exact_read(base + (uintptr_t)dos.e_lfanew, &nt, sizeof(nt)) &&
nt.Signature == IMAGE_NT_SIGNATURE &&
nt.OptionalHeader.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC &&
nt.OptionalHeader.SizeOfImage == 0x29a0000u) {
bits |= 4u;
image_size = nt.OptionalHeader.SizeOfImage;
}
if (VirtualQuery((const void *)base, &page, sizeof(page)) == sizeof(page) &&
page.State == MEM_COMMIT &&
(page.Type == MEM_IMAGE || page.Type == MEM_MAPPED) &&
(uintptr_t)page.AllocationBase == base)
bits |= 8u;
/* Wine may map the writer section with its own AllocationBase. The
* observer independently checks base+RVA, PE span and live prefix. */
if (base <= UINTPTR_MAX - 0xa27ca0u &&
VirtualQuery((const void *)(base + 0xa27ca0u), &writer,
sizeof(writer)) == sizeof(writer) &&
writer.State == MEM_COMMIT &&
(writer.Protect == PAGE_EXECUTE ||
writer.Protect == PAGE_EXECUTE_READ ||
writer.Protect == PAGE_EXECUTE_READWRITE ||
writer.Protect == PAGE_EXECUTE_WRITECOPY))
bits |= 16u;
}
if (bits == 31u) {
gw_bridge_nohelper_log(85, (uint32_t)base, (uint32_t)(base >> 32));
gw_bridge_nohelper_log(86, image_size, bits);
} else {
gw_bridge_nohelper_log(87, bits, writer.Protect);
}
}
struct FrontPageState {
uint32_t state, protect, type, bits;
int admitted;
};
static volatile LONG front_page_report_state;
static volatile LONG upstream_page_probe_once;
static struct GwLoginSitePages login_pages;
static struct FrontPageState observed_page_state(
const struct GwRealmInstallDiagnostic *page)
{
struct FrontPageState out = {0};
out.state = page->page_state;
out.protect = page->page_protect;
out.type = page->page_type;
out.bits = (page->page_identity_bits & 1u) |
((page->page_query_bytes == sizeof(MEMORY_BASIC_INFORMATION)) << 1) |
((page->page_identity_bits & 2u) << 1) |
((page->page_identity_bits & 4u) << 1) |
((out.type == MEM_MAPPED || out.type == MEM_IMAGE) << 4);
out.admitted = out.bits == 31u && out.state == MEM_COMMIT &&
out.protect == PAGE_EXECUTE_READ;
return out;
}
static struct FrontPageState site_page_state(uintptr_t address, SIZE_T bytes)
{
struct GwRealmInstallDiagnostic page = {0};
gw_realm_install_probe_page(address, bytes, &page);
return observed_page_state(&page);
}
static int report_front_pages(struct FrontPageState front,
struct FrontPageState login)
{
if (InterlockedCompareExchange(&upstream_page_probe_once, 1, 0) == 0) {
const uintptr_t sites[3] = {
(uintptr_t)0x1430598f0ULL, /* original first-1119 initiator */
(uintptr_t)0x14307bca0ULL, /* original owner orchestrator */
(uintptr_t)0x143081809ULL /* original dispatcher callsite */
};
unsigned i;
for (i = 0; i < 3; ++i) {
struct FrontPageState state = site_page_state(sites[i], 5);
gw_bridge_nohelper_log(93u + i * 2u, state.state, state.protect);
gw_bridge_nohelper_log(94u + i * 2u, state.type, state.bits);
}
}
if (!front.admitted || !login.admitted) {
if (InterlockedCompareExchange(&front_page_report_state, 1, 0) == 0) {
gw_bridge_nohelper_log(88, front.state, front.protect);
gw_bridge_nohelper_log(89, front.type, front.bits);
gw_bridge_nohelper_log(90, login.state, login.protect);
gw_bridge_nohelper_log(91, login.type, login.bits);
}
return 0;
}
if (InterlockedCompareExchange(&front_page_report_state, 2, 1) == 1)
gw_bridge_nohelper_log(92, 1, 0);
return 1;
}
static int front_page_admitted(void)
{
return report_front_pages(site_page_state(front_function_start,
sizeof(front_function_expected)),
site_page_state(login_site,
GW_DIRECT_LOGIN_PREFIX_SIZE));
}
int gw_direct_front_pages_prepare(void)
{
const struct GwLoginSitePage sites[GW_LOGIN_SITE_PAGE_COUNT] = {
/* Both are lone C3 instructions, entered directly with our call's
* return address. Front is pinned in front_wrapper_preimage.private.h
* (add rsp,0x50; pop rdi; ret). Login's original epilogue is pinned by
* the saved full live image (add rsp,0x70; pop rdi; ret). No function
* body, dispatcher, Login call or credentials are entered. */
{front_function_start, 0x14225315eULL, sizeof(front_function_expected)},
{login_site, 0x14242e22fULL, GW_DIRECT_LOGIN_PREFIX_SIZE}
};
uint64_t now = GetTickCount64();
uint32_t touched = 0, before = login_pages.attempts;
int ready = gw_login_site_pages_prepare(&login_pages, sites, now,
gw_realm_install_probe_page,
&touched);
unsigned i;
for (i = 0; i < GW_LOGIN_SITE_PAGE_COUNT; ++i)
if (touched & (1u << i))
gw_bridge_nohelper_log(103u + i, login_pages.page[i].page_protect,
login_pages.page[i].page_identity_bits);
if (login_pages.attempts != before)
report_front_pages(observed_page_state(&login_pages.page[0]),
observed_page_state(&login_pages.page[1]));
if (ready != 2)
gw_bridge_nohelper_log(ready == 1 ? 107u : 106u,
(uint32_t)(now - login_pages.started_at),
login_pages.attempts);
return ready;
}
void gw_realm_install_probe_page(uintptr_t address, size_t bytes,
struct GwRealmInstallDiagnostic *diagnostic)
{
MEMORY_BASIC_INFORMATION info = {0};
uintptr_t main_base, region_start, region_end;
SIZE_T queried;
if (!diagnostic) return;
main_base = (uintptr_t)GetModuleHandleW(NULL);
if (main_base == (uintptr_t)0x140000000ULL)
diagnostic->page_identity_bits |= 1u;
queried = VirtualQuery((const void *)address, &info, sizeof(info));
diagnostic->page_query_bytes = (uint32_t)queried;
if (!queried) {
diagnostic->page_query_error = GetLastError();
return;
}
diagnostic->page_state = info.State;
diagnostic->page_protect = info.Protect;
diagnostic->page_type = info.Type;
if (main_base && (uintptr_t)info.AllocationBase == main_base)
diagnostic->page_identity_bits |= 2u;
region_start = (uintptr_t)info.BaseAddress;
region_end = region_start + info.RegionSize;
if (bytes && region_end >= region_start && address >= region_start &&
address <= region_end && bytes <= region_end - address)
diagnostic->page_identity_bits |= 4u;
if (info.Type == MEM_IMAGE)
diagnostic->page_identity_bits |= 8u;
}
static void reverse_plan(const struct GwStartupPatch *in,
struct GwStartupPatch *out)
{
*out = *in;
memcpy(out->before, in->after, in->size);
memcpy(out->after, in->before, in->size);
}
#if GW_OWNED_SIGNIN_ONLY
/* Owned-form-only build: relay 0 is prepared (the relay page layout is shared
* with the realm relay at +16) but no original byte ever jumps to it. If
* anything did, it stops here instead of entering any capture code. */
static void owned_signin_unused_relay(void) { __builtin_trap(); }
#endif
static void release_modules(struct GwDirectInstallation *installation)
{
if (installation->pipe_module) FreeLibrary(installation->pipe_module);
if (installation->login_module) FreeLibrary(installation->login_module);
installation->pipe_module = installation->login_module = 0;
}
int gw_direct_install_login_site(const struct GwDirectInstallConfig *config,
struct GwDirectInstallation *installation)
{
uint8_t login_prefix[GW_DIRECT_LOGIN_PREFIX_SIZE];
uint8_t front_prefix[sizeof(front_dispatch_expected)];
struct GwStartupPatch combined[2];
uintptr_t sites[2] = {login_site, realm_site};
uintptr_t thunks[2], resume;
int result;
if (!config || !installation || config->abi != 1 ||
config->size != sizeof(*config) || !config->frame_admitted ||
!config->pipe_return_thunk || config->login.abi != 1 ||
config->login.size != sizeof(config->login) ||
GetCurrentThreadId() != config->login.client_thread ||
!config->login.original_owner_slot ||
*config->login.original_owner_slot != config->login.owner ||
!config->login.profile_ready ||
!config->login.profile_ready(config->login.owner,
config->login.generation)) return -1;
if (installation->state) return -2;
memset(installation, 0, sizeof(*installation));
installation->abi = 1;
installation->size = sizeof(*installation);
installation->primary_thread = config->login.client_thread;
installation->owner = config->login.owner;
installation->original_owner_slot = config->login.original_owner_slot;
memset(&front_dispatch_patch, 0, sizeof(front_dispatch_patch));
InterlockedExchange(&login_retired, 0);
/* Hold both containing modules through publication and any installed
* callback lifetime. The integration owner must supply final teardown. */
#if GW_OWNED_SIGNIN_ONLY
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCWSTR)(uintptr_t)&owned_signin_unused_relay,
&installation->login_module)) return -3;
#else
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCWSTR)(uintptr_t)&gw_direct_login_thunk,
&installation->login_module)) return -3;
#endif
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS,
(LPCWSTR)config->pipe_return_thunk,
&installation->pipe_module)) {
release_modules(installation); return -3;
}
#if GW_OWNED_SIGNIN_ONLY
thunks[0] = (uintptr_t)&owned_signin_unused_relay;
(void)login_prefix;
/* Login-site read-failure witnesses (events 82-84) have no reader here. */
(void)last_exact_read_got; (void)last_exact_read_error;
#else
thunks[0] = (uintptr_t)&gw_direct_login_thunk;
#endif
thunks[1] = config->pipe_return_thunk;
#if !GW_OWNED_SIGNIN_ONLY
if (!exact_read(login_site, login_prefix, sizeof(login_prefix))) {
MEMORY_BASIC_INFORMATION page = {0};
SIZE_T queried = VirtualQuery((const void *)login_site, &page,
sizeof(page));
/* Failure-only numeric witness: no original bytes or credentials. */
gw_bridge_nohelper_log(82, (uint32_t)last_exact_read_got,
(uint32_t)last_exact_read_error);
gw_bridge_nohelper_log(83, page.State, page.Protect);
gw_bridge_nohelper_log(84, page.Type,
(uint32_t)(queried == sizeof(page)) |
(uint32_t)(((uintptr_t)page.AllocationBase ==
(uintptr_t)0x140000000ULL) << 1));
result = -4; goto fail;
}
#endif
result = gw_direct_relays_prepare(sites, thunks,
&installation->relays);
if (result) { result = -5; goto fail; }
/* The two accepted relays remain at +0 and +16. A third absolute jump at
* +32 is prepared before either original site is published. */
{
uint8_t *front_relay = (uint8_t *)installation->relays.page + 32;
uint8_t jump[14] = {0xff, 0x25, 0, 0, 0, 0};
uintptr_t thunk = (uintptr_t)&gw_front_wrapper_thunk;
DWORD old_protect = 0, ignored = 0;
memcpy(jump + 6, &thunk, sizeof(thunk));
if (!VirtualProtect(front_relay, sizeof(jump), PAGE_READWRITE,
&old_protect)) { result = -14; goto fail; }
memcpy(front_relay, jump, sizeof(jump));
if (!VirtualProtect(front_relay, sizeof(jump), old_protect,
&ignored) ||
!FlushInstructionCache(GetCurrentProcess(), front_relay,
sizeof(jump))) { result = -14; goto fail; }
}
#if GW_OWNED_SIGNIN_ONLY
(void)resume; /* patches[0] stays all-zero: no Login-site plan exists. */
#else
if (gw_direct_login_patch_plan(login_prefix, sizeof(login_prefix),
installation->relays.relay[0],
&installation->patches[0], &resume) ||
resume != (uintptr_t)0x14242e19eULL) {
result = -6; goto fail;
}
#endif
if (!front_page_admitted()) { result = -12; goto fail; }
{
uint8_t full_function[sizeof(front_function_expected)];
if (!exact_read(front_function_start, full_function, sizeof(full_function)) ||
memcmp(full_function, front_function_expected,
sizeof(front_function_expected)) != 0) {
result = -13; goto fail;
}
}
if (!exact_read(front_dispatch_call, front_prefix, sizeof(front_prefix))) {
result = -10; goto fail;
}
if (memcmp(front_prefix, front_dispatch_expected,
sizeof(front_dispatch_expected)) != 0) {
result = -11; goto fail;
}
front_dispatch_patch.address = front_dispatch_call;
front_dispatch_patch.size = sizeof(front_dispatch_expected);
memcpy(front_dispatch_patch.before, front_dispatch_expected,
sizeof(front_dispatch_expected));
{
uint8_t *front_relay = (uint8_t *)installation->relays.page + 32;
int64_t relative = (int64_t)(uintptr_t)front_relay -
(int64_t)(front_dispatch_call + 5);
int32_t encoded;
if (relative < INT32_MIN || relative > INT32_MAX) {
result = -15; goto fail;
}
encoded = (int32_t)relative;
front_dispatch_patch.after[0] = 0xe8;
memcpy(front_dispatch_patch.after + 1, &encoded, sizeof(encoded));
}
#if GW_OWNED_SIGNIN_ONLY
/* Only the front dispatcher bypass is published; the stock Login site
* keeps its original bytes for the life of the process. */
combined[0] = front_dispatch_patch;
result = gw_direct_apply_two_patches(combined, 1);
if (result) { result = -7; goto fail; }
installation->state = 1;
result = gw_direct_login_bind(&config->login);
if (result) {
struct GwStartupPatch reverse[1];
reverse_plan(&front_dispatch_patch, &reverse[0]);
if (gw_direct_apply_two_patches(reverse, 1) != 0) {
installation->state = 3; installation->error = 8;
return -8;
}
installation->state = 0;
result = -9; goto fail;
}
/* "Retired" from the start: the coordinator never retires or re-arms a
* Login-site patch, and restore reverses only the front dispatcher. */
InterlockedExchange(&login_retired, 1);
installation->state = 1;
gw_bridge_nohelper_log(56, 1, 0);
gw_bridge_nohelper_log(122, 1, 0); /* owned-form-only mode */
record_loader_base_once();
return 0;
#else
combined[0] = front_dispatch_patch;
combined[1] = installation->patches[0];
result = gw_direct_apply_two_patches(combined, 2);
if (result) { result = -7; goto fail; }
installation->state = 1;
result = gw_direct_login_bind(&config->login);
if (result) {
struct GwStartupPatch reverse[2];
reverse_plan(&installation->patches[0], &reverse[0]);
reverse_plan(&front_dispatch_patch, &reverse[1]);
if (gw_direct_apply_two_patches(reverse, 2) != 0) {
installation->state = 3; installation->error = 8;
return -8; /* Keep relays/modules alive for safe fail-stop. */
}
installation->state = 0;
result = -9; goto fail;
}
installation->state = 1;
gw_bridge_nohelper_log(56, 1, 0);
record_loader_base_once();
return 0;
#endif
fail:
if (installation->relays.page)
gw_direct_relays_release(&installation->relays);
release_modules(installation);
installation->error = (uint32_t)-result;
gw_bridge_nohelper_log(56, 0, (uint32_t)-result);
return result;
}
int gw_direct_install_realm_site(struct GwDirectInstallation *installation,
int pre_open_admitted)
{
uint8_t realm_prefix[GW_CONSTRUCTOR_SEAM_PREFIX_SIZE];
MEMORY_BASIC_INFORMATION page_info = {0};
struct GwConstructorSeamPage page;
uintptr_t resume;
SIZE_T read_bytes = 0;
int result, read_ok;
memset(&gw_realm_install_diagnostic, 0,
sizeof(gw_realm_install_diagnostic));
if (!installation || installation->abi != 1 ||
installation->size != sizeof(*installation)) {
gw_realm_install_diagnostic.failure_class = 1;
gw_realm_install_diagnostic.detail = 1;
return -1;
}
if (installation->state != 1) {
gw_realm_install_diagnostic.failure_class = 1;
gw_realm_install_diagnostic.detail = 2;
return -1;
}
if (!pre_open_admitted) {
gw_realm_install_diagnostic.failure_class = 1;
gw_realm_install_diagnostic.detail = 3;
return -1;
}
if (GetCurrentThreadId() != installation->primary_thread) {
gw_realm_install_diagnostic.failure_class = 1;
gw_realm_install_diagnostic.detail = 4;
return -1;
}
if (!installation->original_owner_slot ||
*installation->original_owner_slot != installation->owner) {
gw_realm_install_diagnostic.failure_class = 1;
gw_realm_install_diagnostic.detail = 5;
return -1;
}
/* One same-PID preopen query before any protection change or code read. */
gw_realm_install_diagnostic.page_query_bytes = (uint32_t)VirtualQuery(
(const void *)realm_site, &page_info, sizeof(page_info));
if (gw_realm_install_diagnostic.page_query_bytes != sizeof(page_info)) {
gw_realm_install_diagnostic.failure_class = 2;
gw_realm_install_diagnostic.page_query_error = GetLastError();
return -2;
}
page = (struct GwConstructorSeamPage){
(uintptr_t)page_info.BaseAddress, (uintptr_t)page_info.AllocationBase,
page_info.RegionSize, page_info.State, page_info.Protect,
page_info.Type};
gw_realm_install_diagnostic.page_state = page.state;
gw_realm_install_diagnostic.page_protect = page.protect;
gw_realm_install_diagnostic.page_type = page.type;
if ((uintptr_t)GetModuleHandleW(NULL) == 0x140000000ULL)
gw_realm_install_diagnostic.page_identity_bits |= 1u;
if (page.allocation_base == 0x140000000ULL)
gw_realm_install_diagnostic.page_identity_bits |= 2u;
if (page.base <= realm_site && page.region_size >= sizeof(realm_prefix) &&
realm_site - page.base <= page.region_size - sizeof(realm_prefix))
gw_realm_install_diagnostic.page_identity_bits |= 4u;
if (page.state != MEM_COMMIT || page.protect != PAGE_EXECUTE_READ ||
page.type != MEM_MAPPED ||
gw_realm_install_diagnostic.page_identity_bits != 7u) {
gw_realm_install_diagnostic.failure_class = 2;
gw_realm_install_diagnostic.detail = 6;
return -2;
}
read_ok = ReadProcessMemory(GetCurrentProcess(), (const void *)realm_site,
realm_prefix, sizeof(realm_prefix), &read_bytes);
gw_realm_install_diagnostic.read_bytes = (uint32_t)read_bytes;
if (!read_ok || read_bytes != sizeof(realm_prefix)) {
gw_realm_install_diagnostic.failure_class = 2;
gw_realm_install_diagnostic.detail = read_ok ? 1u : 0u;
gw_realm_install_diagnostic.winerror = read_ok ? 0u : GetLastError();
return -2;
}
result = gw_constructor_seam_plan(&page, realm_prefix,
sizeof(realm_prefix),
installation->relays.relay[1],
&installation->patches[1], &resume);
if (result || resume != realm_site + 5) {
gw_realm_install_diagnostic.failure_class = 3;
gw_realm_install_diagnostic.detail = result == -1 ? 7u :
result == -2 ? 8u : 9u;
return -3;
}
result = gw_direct_apply_two_patches(&installation->patches[1], 1);
if (result) {
gw_realm_install_diagnostic.failure_class = 4;
gw_realm_install_diagnostic.detail = (uint32_t)result;
gw_realm_install_diagnostic.admission_reason = gw_direct_admission.reason;
gw_realm_install_diagnostic.winerror = gw_direct_admission.winerror;
gw_realm_install_diagnostic.page_state = gw_direct_admission.state;
gw_realm_install_diagnostic.page_protect = gw_direct_admission.protect;
return -4;
}
installation->state = 2;
return 0;
}
int gw_direct_login_site_retired(void)
{
return InterlockedCompareExchange(&login_retired, 0, 0) == 1;
}
struct GwDirectRetireDiagnostic gw_direct_retire_diagnostic;
int gw_direct_retire_login_site_after_capture(
struct GwDirectInstallation *installation, uint32_t generation,
void *owner)
{
struct GwStartupPatch reverse;
int transaction;
gw_direct_retire_diagnostic = (struct GwDirectRetireDiagnostic){0};
if (!installation || installation->abi != 1 ||
installation->size != sizeof(*installation) ||
(installation->state != 1 && installation->state != 2) ||
gw_direct_login_site_retired() || !generation ||
installation->owner != owner ||
GetCurrentThreadId() != installation->primary_thread ||
!installation->original_owner_slot ||
*installation->original_owner_slot != owner ||
gw_direct_login_published_generation() != generation)
return -1;
#if GW_OWNED_SIGNIN_ONLY
(void)reverse;
return -1; /* unreachable: the site is retired from install onward */
#endif
/* The engine may already have restored the exact original one-shot site.
* That is a completed retirement, not an unexpected preimage to overwrite. */
{
uint8_t current[sizeof(installation->patches[0].before)];
const uint32_t size = installation->patches[0].size;
if (!size || size > sizeof(current) || !installation->patches[0].address) return -1;
if (exact_read(installation->patches[0].address, current, size) &&
memcmp(current, installation->patches[0].before, size) == 0) {
InterlockedExchange(&login_retired, 1);
return 0;
}
}
reverse_plan(&installation->patches[0], &reverse);
transaction = gw_direct_apply_two_patches(&reverse, 1);
gw_direct_retire_diagnostic.transaction_result = (uint32_t)transaction;
gw_direct_retire_diagnostic.reason = gw_direct_admission.reason;
gw_direct_retire_diagnostic.index = gw_direct_admission.index;
gw_direct_retire_diagnostic.winerror = gw_direct_admission.winerror;
gw_direct_retire_diagnostic.state = gw_direct_admission.state;
gw_direct_retire_diagnostic.protect = gw_direct_admission.protect;
if (transaction != 0)
return gw_direct_patch_retryable(transaction, gw_direct_admission.reason) ? -5 : -2;
InterlockedExchange(&login_retired, 1);
return 0;
}
/* Re-arm the retired one-shot Login site for the next credential generation:
* the exact original bytes must be present, then the same verified patch is
* re-applied. Keeps the patch absent between capture and the next login. */
int gw_direct_rearm_login_site(struct GwDirectInstallation *installation)
{
uint8_t current[sizeof(installation->patches[0].before)];
uint32_t size;
if (!installation || installation->abi != 1 ||
installation->size != sizeof(*installation) ||
(installation->state != 1 && installation->state != 2) ||
!gw_direct_login_site_retired() ||
GetCurrentThreadId() != installation->primary_thread)
return -1;
#if GW_OWNED_SIGNIN_ONLY
/* Nothing to re-arm: the next generation is bound by the coordinator's
* gw_direct_login_rebind only; the stock site stays unpatched. */
(void)current; (void)size;
return 0;
#endif
size = installation->patches[0].size;
if (!size || size > sizeof(current) || !installation->patches[0].address)
return -2;
if (!exact_read(installation->patches[0].address, current, size))
return -5; /* page not readable yet: caller retries next frame */
if (memcmp(current, installation->patches[0].before, size) != 0)
return -3;
if (gw_direct_apply_two_patches(&installation->patches[0], 1) != 0)
return -4;
InterlockedExchange(&login_retired, 0);
return 0;
}
int gw_direct_restore_sites(struct GwDirectInstallation *installation,
int owned_quiescent)
{
struct GwStartupPatch reverse[2];
struct GwStartupPatch login_front_reverse[2];
if (!installation || installation->abi != 1 ||
installation->size != sizeof(*installation) ||
(installation->state != 1 && installation->state != 2) ||
!owned_quiescent) return -1;
if (!gw_direct_login_site_retired())
reverse_plan(&installation->patches[0], &reverse[0]);
if (installation->state == 2)
reverse_plan(&installation->patches[1], &reverse[1]);
if (installation->state == 2 &&
gw_direct_apply_two_patches(&reverse[1], 1) != 0) return -2;
if (gw_direct_login_site_retired()) {
reverse_plan(&front_dispatch_patch, &login_front_reverse[0]);
if (gw_direct_apply_two_patches(login_front_reverse, 1) != 0)
return -3;
} else {
login_front_reverse[0] = reverse[0];
reverse_plan(&front_dispatch_patch, &login_front_reverse[1]);
if (gw_direct_apply_two_patches(login_front_reverse, 2) != 0)
return -3;
}
installation->state = 0;
gw_bridge_nohelper_log(56, 2, 0);
if (gw_direct_relays_release(&installation->relays) != 0) return -4;
release_modules(installation);
return 0;
}