345 lines
15 KiB
C
345 lines
15 KiB
C
#include "owner_poll_self.private.h"
|
|
#include <stddef.h>
|
|
|
|
_Static_assert(sizeof(struct GwPollHost) == 1080, "poll host ABI");
|
|
_Static_assert(offsetof(struct GwPollHost, methods[1]) == 56, "clone ABI");
|
|
|
|
struct PollBinding {
|
|
volatile LONG stage;
|
|
volatile LONG inflight;
|
|
uint64_t generation;
|
|
void *owner;
|
|
uintptr_t owner_global;
|
|
void *original;
|
|
void *clone;
|
|
DWORD thread;
|
|
struct GwPollHost *host;
|
|
void (WINAPI *callback)(void *);
|
|
int (WINAPI *original_receive)(void *, uint32_t, uint32_t,
|
|
const unsigned char *, uint32_t);
|
|
HMODULE pinned_module;
|
|
};
|
|
|
|
static struct PollBinding binding;
|
|
static volatile LONG roster_headers, restriction_headers, collection_headers;
|
|
extern void gw_bridge_nohelper_log(uint32_t event,uint32_t result,uint32_t error);
|
|
|
|
struct GwCollectionPageCapture {
|
|
uint64_t address;
|
|
uint32_t query_bytes, query_error, state, protection, type;
|
|
uint32_t allocation_base_match, region_covers_page;
|
|
uint32_t read_ok, read_bytes, read_error;
|
|
unsigned char bytes[4096];
|
|
};
|
|
struct GwCollectionPagesCapture {
|
|
uint32_t abi, size;
|
|
volatile LONG stage; /* 0 empty, 1 writing, 2 complete */
|
|
uint32_t reserved;
|
|
struct GwCollectionPageCapture page[2];
|
|
};
|
|
_Static_assert(sizeof(struct GwCollectionPageCapture) == 4144, "page capture ABI");
|
|
_Static_assert(sizeof(struct GwCollectionPagesCapture) == 8304, "capture ABI");
|
|
__declspec(dllexport) struct GwCollectionPagesCapture gw_poll_collection_pages_v33 = {
|
|
1, sizeof(struct GwCollectionPagesCapture), 0, 0
|
|
};
|
|
|
|
static void capture_collection_page(struct GwCollectionPageCapture *row,
|
|
uintptr_t address) {
|
|
MEMORY_BASIC_INFORMATION info;
|
|
SIZE_T transferred = 0, queried;
|
|
BOOL read_result;
|
|
row->address = address;
|
|
queried = VirtualQuery((const void *)address, &info, sizeof(info));
|
|
row->query_bytes = (uint32_t)queried;
|
|
row->query_error = queried == sizeof(info) ? 0 : GetLastError();
|
|
if (queried == sizeof(info)) {
|
|
row->state = info.State;
|
|
row->protection = info.Protect;
|
|
row->type = info.Type;
|
|
row->allocation_base_match =
|
|
(uintptr_t)info.AllocationBase == (uintptr_t)GetModuleHandleW(0);
|
|
row->region_covers_page =
|
|
address >= (uintptr_t)info.BaseAddress &&
|
|
address + 4096 <= (uintptr_t)info.BaseAddress + info.RegionSize;
|
|
}
|
|
read_result = ReadProcessMemory(GetCurrentProcess(), (const void *)address,
|
|
row->bytes, sizeof(row->bytes), &transferred);
|
|
row->read_bytes = (uint32_t)transferred;
|
|
row->read_error = read_result ? 0 : GetLastError();
|
|
row->read_ok = read_result && transferred == sizeof(row->bytes);
|
|
}
|
|
|
|
static void capture_collection_pages_once(void) {
|
|
struct GwCollectionPagesCapture *capture = &gw_poll_collection_pages_v33;
|
|
uint32_t complete = 0;
|
|
if (InterlockedCompareExchange(&capture->stage, 1, 0) != 0) return;
|
|
capture_collection_page(&capture->page[0], 0x1408ef000ULL);
|
|
capture_collection_page(&capture->page[1], 0x14241f000ULL);
|
|
if (capture->page[0].read_ok) complete |= 1;
|
|
if (capture->page[1].read_ok) complete |= 2;
|
|
MemoryBarrier();
|
|
InterlockedExchange(&capture->stage, 2);
|
|
gw_bridge_nohelper_log(42, 2, complete);
|
|
}
|
|
|
|
static int page_allows(uintptr_t address, SIZE_T size, int kind) {
|
|
MEMORY_BASIC_INFORMATION info;
|
|
DWORD protection;
|
|
uintptr_t end;
|
|
if (!address || !size || address > UINTPTR_MAX - size) return 0;
|
|
end = address + size;
|
|
if (VirtualQuery((const void *)address, &info, sizeof(info)) != sizeof(info) ||
|
|
info.State != MEM_COMMIT ||
|
|
address < (uintptr_t)info.BaseAddress ||
|
|
end > (uintptr_t)info.BaseAddress + info.RegionSize) return 0;
|
|
if (info.Protect & (PAGE_GUARD | PAGE_NOACCESS)) return 0;
|
|
protection = info.Protect & 0xff;
|
|
if (kind == 2)
|
|
return protection == PAGE_READWRITE || protection == PAGE_WRITECOPY ||
|
|
protection == PAGE_EXECUTE_READWRITE || protection == PAGE_EXECUTE_WRITECOPY;
|
|
if (kind == 1)
|
|
return protection == PAGE_EXECUTE || protection == PAGE_EXECUTE_READ ||
|
|
protection == PAGE_EXECUTE_READWRITE || protection == PAGE_EXECUTE_WRITECOPY;
|
|
return protection == PAGE_READONLY || protection == PAGE_READWRITE ||
|
|
protection == PAGE_WRITECOPY || protection == PAGE_EXECUTE_READ ||
|
|
protection == PAGE_EXECUTE_READWRITE || protection == PAGE_EXECUTE_WRITECOPY;
|
|
}
|
|
|
|
static int read_exact(uintptr_t address, void *destination, SIZE_T size) {
|
|
SIZE_T transferred = 0;
|
|
return page_allows(address, size, 0) &&
|
|
ReadProcessMemory(GetCurrentProcess(), (const void *)address,
|
|
destination, size, &transferred) && transferred == size;
|
|
}
|
|
|
|
void WINAPI gw_poll_self_dispatch(void *owner) {
|
|
struct PollBinding *active = &binding;
|
|
void (WINAPI *original)(void *) = 0;
|
|
InterlockedIncrement(&active->inflight);
|
|
if (active->host && active->owner == owner) {
|
|
original = (void (WINAPI *)(void *))(uintptr_t)active->host->original_poll;
|
|
if (InterlockedCompareExchange(&active->stage, 2, 2) == 2 &&
|
|
active->callback && GetCurrentThreadId() == active->thread) {
|
|
active->callback(owner);
|
|
InterlockedDecrement(&active->inflight);
|
|
return;
|
|
}
|
|
}
|
|
if (original) original(owner);
|
|
InterlockedDecrement(&active->inflight);
|
|
}
|
|
|
|
/* The already owned clone forwards one exact original receive slot. Header
|
|
* metadata only; no packet body, state flag, or queue content is changed. */
|
|
int WINAPI gw_poll_self_receive(void *owner, uint32_t queued,
|
|
uint32_t processing,
|
|
const unsigned char *data, uint32_t bytes) {
|
|
struct PollBinding *active = &binding;
|
|
int (WINAPI *original)(void *,uint32_t,uint32_t,const unsigned char *,uint32_t) =
|
|
active->original_receive;
|
|
uintptr_t table = 0;
|
|
uint32_t tracked_opcode = 0;
|
|
int forwarded;
|
|
if (original && active->owner == owner && active->host &&
|
|
active->host->owner == (uintptr_t)owner &&
|
|
active->host->methods[7] == (uintptr_t)&gw_poll_self_receive &&
|
|
InterlockedCompareExchange(&active->stage,2,2)==2 &&
|
|
read_exact((uintptr_t)owner, &table, sizeof(table)) &&
|
|
table == (uintptr_t)active->clone && data && bytes >= 4) {
|
|
uint32_t opcode = 0;
|
|
if (read_exact((uintptr_t)data, &opcode, sizeof(opcode))) {
|
|
volatile LONG *counter = opcode==0x470018u ? &roster_headers :
|
|
opcode==0x470019u ? &restriction_headers :
|
|
opcode==0x470363u ? &collection_headers : 0;
|
|
if (counter) {
|
|
tracked_opcode = opcode;
|
|
LONG count=InterlockedIncrement(counter);
|
|
if (count>0 && count<=65535 && bytes<=65535)
|
|
gw_bridge_nohelper_log(38,opcode,((uint32_t)count<<16)|bytes);
|
|
}
|
|
}
|
|
}
|
|
if (tracked_opcode) {
|
|
uint32_t owner_state = 0, admission = 0;
|
|
uintptr_t global_owner = 0;
|
|
if (read_exact((uintptr_t)owner + 0x98, &owner_state,
|
|
sizeof(owner_state))) {
|
|
admission |= 1u << 16;
|
|
if (owner_state == 5) admission |= 1u << 17;
|
|
}
|
|
if (read_exact(active->owner_global, &global_owner,
|
|
sizeof(global_owner))) {
|
|
admission |= 1u << 18;
|
|
if (global_owner == (uintptr_t)owner) admission |= 1u << 19;
|
|
}
|
|
gw_bridge_nohelper_log(41, tracked_opcode,
|
|
(owner_state & 0xffffu) | admission);
|
|
}
|
|
if (tracked_opcode == 0x470363u) capture_collection_pages_once();
|
|
forwarded = original ? original(owner,queued,processing,data,bytes) : -1;
|
|
if (tracked_opcode) {
|
|
unsigned char gate = 0, consumed = 0;
|
|
uintptr_t pending = 0;
|
|
uint32_t flags = 0;
|
|
if (read_exact(0x1456ad44dULL, &gate, sizeof(gate))) {
|
|
flags |= 1;
|
|
if (!gate) flags |= 2;
|
|
}
|
|
if (read_exact((uintptr_t)owner + 0xc90, &pending, sizeof(pending))) {
|
|
flags |= 4;
|
|
if (pending) flags |= 8;
|
|
}
|
|
if (read_exact((uintptr_t)owner + 0xca0, &consumed, sizeof(consumed))) {
|
|
flags |= 16;
|
|
if (consumed) flags |= 32;
|
|
}
|
|
gw_bridge_nohelper_log(40, (uint32_t)forwarded,
|
|
(tracked_opcode << 8) | flags);
|
|
}
|
|
return forwarded;
|
|
}
|
|
|
|
int gw_poll_bind_once(const struct GwPollBindRegion *region,
|
|
struct GwPollHost *host, void **frame_host_slot,
|
|
void (WINAPI *frame_callback)(void *)) {
|
|
uintptr_t owner = 0, original = 0, queue = 0, entry = 0, locator = 0;
|
|
uint32_t state = 0;
|
|
unsigned char connections[32];
|
|
uint64_t entries[128];
|
|
unsigned count = 0, index;
|
|
void *clone;
|
|
HMODULE module = 0;
|
|
if (!region || !host || !frame_host_slot || !frame_callback ||
|
|
InterlockedCompareExchange(&binding.stage, 1, 0) != 0)
|
|
return GW_POLL_REFUSED_STATE;
|
|
if (GetCurrentThreadId() != region->primary_thread ||
|
|
!page_allows((uintptr_t)host, sizeof(*host), 2) ||
|
|
!page_allows((uintptr_t)frame_host_slot, sizeof(*frame_host_slot), 2) ||
|
|
*frame_host_slot || host->abi != 1 || host->size != sizeof(*host))
|
|
goto layout;
|
|
if (!read_exact(region->owner_global, &owner, sizeof(owner)) ||
|
|
!owner || (owner & 7) ||
|
|
!read_exact(owner + 0x98, &state, sizeof(state)) || state != 2 ||
|
|
!read_exact(owner + 0xa0, &queue, sizeof(queue)) ||
|
|
!queue || !read_exact(queue, &entry, sizeof(entry)) || entry != owner ||
|
|
!read_exact(owner + 0x1a0, connections, sizeof(connections)))
|
|
goto owner_refusal;
|
|
for (index = 0; index < sizeof(connections); ++index)
|
|
if (connections[index]) goto owner_refusal;
|
|
if (!read_exact(owner, &original, sizeof(original)) ||
|
|
original < region->vtable_low || original >= region->vtable_high ||
|
|
!page_allows(owner, sizeof(void *), 2) ||
|
|
!read_exact(original - 8, &locator, sizeof(locator)))
|
|
goto layout;
|
|
for (index = 0; index < 128; ++index) {
|
|
if (!read_exact(original + index * 8, &entry, sizeof(entry))) goto page;
|
|
if (entry < region->code_low || entry >= region->code_high) break;
|
|
entries[count++] = entry;
|
|
}
|
|
/* The receive slot can become executable later in the original lifecycle.
|
|
* Keep its exact identity; record preopen protection without gating bind. */
|
|
{
|
|
uint32_t gate = 0;
|
|
if (count >= 7 && count <= 128) gate |= 1;
|
|
if ((gate & 1) && entries[6] == 0x141e47960ULL) gate |= 2;
|
|
if ((gate & 2) && page_allows((uintptr_t)entries[6], 1, 1)) gate |= 4;
|
|
gw_bridge_nohelper_log(39, count, gate);
|
|
if ((gate & 3) != 3 || !page_allows((uintptr_t)entries[5], 1, 1))
|
|
goto layout;
|
|
}
|
|
if (!GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
|
|
GET_MODULE_HANDLE_EX_FLAG_PIN,
|
|
(LPCWSTR)(uintptr_t)&gw_poll_self_dispatch, &module))
|
|
goto module_refusal;
|
|
for (index = 0; index < 129; ++index)
|
|
((volatile uint64_t *)host->methods)[index] = 0;
|
|
host->stage = 1;
|
|
host->calls = host->thread = host->error = 0;
|
|
host->owner = owner;
|
|
host->original_vtable = original;
|
|
host->original_poll = entries[5];
|
|
host->methods[0] = locator;
|
|
for (index = 0; index < count; ++index) host->methods[index + 1] = entries[index];
|
|
host->methods[6] = (uintptr_t)&gw_poll_self_dispatch;
|
|
host->methods[7] = (uintptr_t)&gw_poll_self_receive;
|
|
clone = &host->methods[1];
|
|
binding.generation = 1;
|
|
binding.owner = (void *)owner;
|
|
binding.owner_global = region->owner_global;
|
|
binding.original = (void *)original;
|
|
binding.clone = clone;
|
|
binding.thread = region->primary_thread;
|
|
binding.host = host;
|
|
binding.callback = frame_callback;
|
|
binding.original_receive = (int (WINAPI *)(void *,uint32_t,uint32_t,
|
|
const unsigned char *,uint32_t))(uintptr_t)entries[6];
|
|
binding.pinned_module = module;
|
|
*frame_host_slot = host;
|
|
MemoryBarrier();
|
|
if (InterlockedCompareExchangePointer((PVOID volatile *)owner, clone,
|
|
(void *)original) != (void *)original) {
|
|
*frame_host_slot = 0;
|
|
InterlockedExchange(&binding.stage, 4);
|
|
return GW_POLL_REFUSED_CAS;
|
|
}
|
|
host->stage = 2;
|
|
InterlockedExchange(&binding.stage, 2);
|
|
return GW_POLL_BOUND;
|
|
|
|
module_refusal:
|
|
InterlockedExchange(&binding.stage, 0);
|
|
return GW_POLL_REFUSED_MODULE;
|
|
page:
|
|
InterlockedExchange(&binding.stage, 0);
|
|
return GW_POLL_REFUSED_PAGE;
|
|
owner_refusal:
|
|
InterlockedExchange(&binding.stage, 0);
|
|
return GW_POLL_REFUSED_OWNER;
|
|
layout:
|
|
InterlockedExchange(&binding.stage, 0);
|
|
return GW_POLL_REFUSED_LAYOUT;
|
|
}
|
|
|
|
int gw_poll_restore_once(const struct GwPollBindRegion *region) {
|
|
uintptr_t current = 0, object_table = 0;
|
|
if (!region || GetCurrentThreadId() != binding.thread ||
|
|
InterlockedCompareExchange(&binding.stage, 3, 2) != 2)
|
|
return GW_POLL_REFUSED_STATE;
|
|
if (!read_exact(region->owner_global, ¤t, sizeof(current)) ||
|
|
current != (uintptr_t)binding.owner ||
|
|
!read_exact(current, &object_table, sizeof(object_table)) ||
|
|
object_table != (uintptr_t)binding.clone) {
|
|
InterlockedExchange(&binding.stage, 2);
|
|
return GW_POLL_REFUSED_OWNER;
|
|
}
|
|
if (InterlockedCompareExchangePointer((PVOID volatile *)current,
|
|
binding.original, binding.clone) != binding.clone) {
|
|
InterlockedExchange(&binding.stage, 2);
|
|
return GW_POLL_REFUSED_CAS;
|
|
}
|
|
binding.host->stage = 4;
|
|
InterlockedExchange(&binding.stage, 4);
|
|
/* Keep the module and clone resident: an already-fetched vptr may still
|
|
enter the dispatcher after the object pointer has been restored. */
|
|
return GW_POLL_BOUND;
|
|
}
|
|
|
|
int gw_poll_owner_snapshot(const struct GwPollBindRegion *region,
|
|
struct GwPollOwnerSnapshot *out) {
|
|
uintptr_t current = 0, table = 0;
|
|
if (!region || !out || GetCurrentThreadId() != binding.thread ||
|
|
InterlockedCompareExchange(&binding.stage, 2, 2) != 2)
|
|
return GW_POLL_REFUSED_STATE;
|
|
if (!read_exact(region->owner_global, ¤t, sizeof(current)) ||
|
|
current != (uintptr_t)binding.owner ||
|
|
!read_exact(current, &table, sizeof(table)) ||
|
|
table != (uintptr_t)binding.clone)
|
|
return GW_POLL_REFUSED_OWNER;
|
|
out->owner = current;
|
|
out->original_vtable = (uintptr_t)binding.original;
|
|
out->clone_vtable = (uintptr_t)binding.clone;
|
|
out->generation = binding.generation;
|
|
out->primary_thread = binding.thread;
|
|
return GW_POLL_BOUND;
|
|
}
|