139 lines
6.9 KiB
C
139 lines
6.9 KiB
C
#ifndef GW_CLOSED_SEND_QUEUE_DISCARD_PRIVATE_H
|
|
#define GW_CLOSED_SEND_QUEUE_DISCARD_PRIVATE_H
|
|
#include <windows.h>
|
|
#include <stdint.h>
|
|
|
|
/* Original 70334 NetClient shutdown 141e45c60 disposes the two suspended
|
|
* SEND lists before releasing the client. Reuse only that list operation at
|
|
* the closed-session boundary; the full shutdown destroys the poll owner.
|
|
* Caller holds the original owner CS, has joined the Source worker, and has
|
|
* completed original state-2 Disconnect. Never hold the pipe SRW across an
|
|
* original engine call. Original SEND requires state 5 under the same CS.
|
|
*/
|
|
struct gw_closed_send_store {
|
|
uint64_t table;
|
|
void *data;
|
|
uint32_t capacity, length, cursor, pad;
|
|
};
|
|
struct gw_closed_send_node {
|
|
void *prev, *next;
|
|
unsigned char requested, pad[7];
|
|
struct gw_closed_send_store buffer;
|
|
};
|
|
_Static_assert(sizeof(struct gw_closed_send_node) == 56, "Closed SEND node ABI");
|
|
|
|
#ifndef GW_CLOSED_SEND_FIXTURE
|
|
static int gw_closed_send_readable(const void *p, SIZE_T n) {
|
|
MEMORY_BASIC_INFORMATION m;
|
|
uintptr_t begin = (uintptr_t)p, end = begin + n;
|
|
return begin >= 0x10000 && end >= begin &&
|
|
VirtualQuery(p, &m, sizeof(m)) && m.State == MEM_COMMIT &&
|
|
!(m.Protect & (PAGE_GUARD | PAGE_NOACCESS)) &&
|
|
end <= (uintptr_t)m.BaseAddress + m.RegionSize;
|
|
}
|
|
static int gw_closed_send_code(const void *p, const unsigned char *bytes, SIZE_T n) {
|
|
MEMORY_BASIC_INFORMATION m;
|
|
SIZE_T i;
|
|
if (!gw_closed_send_readable(p, n) || !VirtualQuery(p, &m, sizeof(m)) ||
|
|
!(m.Protect & (PAGE_EXECUTE | PAGE_EXECUTE_READ |
|
|
PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY))) return 0;
|
|
for (i = 0; i < n; ++i) if (((const unsigned char *)p)[i] != bytes[i]) return 0;
|
|
return 1;
|
|
}
|
|
#define GW_CLOSED_SEND_STORE_RELEASE ((void (WINAPI *)(void *, void **, uint32_t *))0x1445b7db0ULL)
|
|
#define GW_CLOSED_SEND_NODE_RELEASE ((unsigned char (WINAPI *)(void *, const char *, int, unsigned int))0x14452a5d0ULL)
|
|
#define GW_CLOSED_SEND_SOURCE ((const char *)0x145cdda90ULL)
|
|
/* The client re-protects idle Main code pages PAGE_NOACCESS and lifts that
|
|
* in its own handler on first execution (realm_seam_page.h). At the account
|
|
* screen both release pages are idle, and the byte check below would refuse
|
|
* them. A page in exactly that state is executed once through a lone in-page
|
|
* ret, as gw_realm_seam_page_prepare does, and then checked as before:
|
|
* page 0445b7000: 48 83 c4 20 5f c3 cc cc cc at 1445b7014 -> ret 1445b7019
|
|
* page 04452a000: 48 83 c4 30 5f c3 cc cc cc at 14452a5c3 -> ret 14452a5c8
|
|
* (saved original code pages, sha256 4bf42384f152b29a.. and 1224f55be01f0241..).
|
|
* Nothing else is executed and no byte is written. */
|
|
#define GW_CLOSED_SEND_MAIN_BASE ((uintptr_t)0x140000000ULL)
|
|
#define GW_CLOSED_SEND_STORE_PAGE_RET ((void (*)(void))0x1445b7019ULL)
|
|
#define GW_CLOSED_SEND_NODE_PAGE_RET ((void (*)(void))0x14452a5c8ULL)
|
|
static void gw_closed_send_page_prepare(const void *entry, void (*page_ret)(void)) {
|
|
MEMORY_BASIC_INFORMATION m;
|
|
if (VirtualQuery(entry, &m, sizeof(m)) && m.State == MEM_COMMIT &&
|
|
m.Protect == PAGE_NOACCESS && (m.Type == MEM_MAPPED || m.Type == MEM_IMAGE) &&
|
|
(uintptr_t)m.AllocationBase == GW_CLOSED_SEND_MAIN_BASE &&
|
|
((uintptr_t)entry >> 12) == ((uintptr_t)page_ret >> 12)) page_ret();
|
|
}
|
|
static int gw_closed_send_targets_ready(void) {
|
|
static const unsigned char store[] = {0x48,0x89,0x5c,0x24,0x08,0x57,0x48,0x83,0xec,0x20,0x41,0x83,0x38,0};
|
|
static const unsigned char release[] = {0x48,0x89,0x5c,0x24,0x10,0x48,0x89,0x6c,0x24,0x18};
|
|
gw_closed_send_page_prepare((const void *)GW_CLOSED_SEND_STORE_RELEASE, GW_CLOSED_SEND_STORE_PAGE_RET);
|
|
gw_closed_send_page_prepare((const void *)GW_CLOSED_SEND_NODE_RELEASE, GW_CLOSED_SEND_NODE_PAGE_RET);
|
|
return gw_closed_send_code((const void *)GW_CLOSED_SEND_STORE_RELEASE, store, sizeof(store)) &&
|
|
gw_closed_send_code((const void *)GW_CLOSED_SEND_NODE_RELEASE, release, sizeof(release));
|
|
}
|
|
#else
|
|
/* Fixture supplies the exact memory/allocator contracts, never a SEND hook. */
|
|
static int gw_closed_send_readable(const void *, SIZE_T);
|
|
static int gw_closed_send_targets_ready(void);
|
|
#endif
|
|
|
|
static int gw_closed_send_validate(void **head, unsigned int *total) {
|
|
struct gw_closed_send_node *node = (struct gw_closed_send_node *)head[1];
|
|
void *previous = head;
|
|
if (head[0] == head && head[1] == (void *)((uintptr_t)head | 1)) return 0;
|
|
while (node && !((uintptr_t)node & 1)) {
|
|
uintptr_t next;
|
|
if (++*total > 4096 || !gw_closed_send_readable(node, sizeof(*node)) ||
|
|
node->prev != previous || node->requested > 3 ||
|
|
node->buffer.table != 0x144c69b00ULL || node->buffer.cursor != 16 ||
|
|
node->buffer.length < 20 || node->buffer.length > node->buffer.capacity ||
|
|
node->buffer.capacity > 1048592 ||
|
|
!gw_closed_send_readable(node->buffer.data, node->buffer.length)) return -81;
|
|
next = (uintptr_t)node->next;
|
|
if (next & 1) return next == ((uintptr_t)head | 1) && head[0] == node ? 0 : -82;
|
|
if (!next || !gw_closed_send_readable((void *)next, 16) ||
|
|
((struct gw_closed_send_node *)next)->prev != node) return -82;
|
|
previous = node;
|
|
node = (struct gw_closed_send_node *)next;
|
|
}
|
|
return -82;
|
|
}
|
|
|
|
static int gw_closed_send_queues_discard(void *owner) {
|
|
unsigned char *p = (unsigned char *)owner;
|
|
void **heads[2];
|
|
unsigned int list, total = 0;
|
|
uint64_t holder;
|
|
int result;
|
|
if (!gw_closed_send_readable(owner, 0x230) || *(LONG *)(p + 0x98) != 2 ||
|
|
*(uint64_t *)(p + 0x1a0) || *(uint64_t *)(p + 0x1a8) ||
|
|
*(uint64_t *)(p + 0x1b0) || *(uint64_t *)(p + 0x1b8)) return -80;
|
|
holder = *(uint64_t *)(p + 0x228);
|
|
if (holder && (!gw_closed_send_readable((void *)(uintptr_t)holder, 8) ||
|
|
*(uint64_t *)(uintptr_t)holder)) return -80;
|
|
heads[0] = (void **)(p + 0x1e8);
|
|
heads[1] = (void **)(p + 0x200);
|
|
/* Qualify BOTH complete lists before releasing even the first allocation. */
|
|
for (list = 0; list < 2; ++list) {
|
|
result = gw_closed_send_validate(heads[list], &total);
|
|
if (result) return result;
|
|
}
|
|
if (!total) return 0;
|
|
if (!gw_closed_send_targets_ready()) return -83;
|
|
for (list = 0; list < 2; ++list) {
|
|
void **head = heads[list];
|
|
while (head[1] != (void *)((uintptr_t)head | 1)) {
|
|
struct gw_closed_send_node *node = (struct gw_closed_send_node *)head[1];
|
|
void *next = node->next;
|
|
GW_CLOSED_SEND_STORE_RELEASE(&node->buffer, &node->buffer.data, &node->buffer.capacity);
|
|
if (node->buffer.data || node->buffer.capacity) return -84;
|
|
if ((uintptr_t)next & 1) head[0] = head;
|
|
else ((struct gw_closed_send_node *)next)->prev = head;
|
|
head[1] = next;
|
|
node->prev = node->next = 0;
|
|
if (!GW_CLOSED_SEND_NODE_RELEASE(node, GW_CLOSED_SEND_SOURCE, -2, 0)) return -85;
|
|
}
|
|
}
|
|
return 0;
|
|
}
|
|
#endif
|