123 lines
6.4 KiB
C
123 lines
6.4 KiB
C
#ifndef GW_NATIVE_DISCONNECT_RETIREMENT_PRIVATE_H
|
|
#define GW_NATIVE_DISCONNECT_RETIREMENT_PRIVATE_H
|
|
#include <windows.h>
|
|
#include <stdint.h>
|
|
#include <string.h>
|
|
#include "../wine_session_retirement.h"
|
|
/* Original 70334 base NetClient::Disconnect(owner, reason), called by the
|
|
* native owner's override at 1422bd4d9..4e2. Its return is not a status.
|
|
*
|
|
* Caller contract: exact owned post-batch main-frame callback, joined Source
|
|
* worker/account_retirement_ready==0, released lease and the old owned pipe
|
|
* already closed. Do NOT hold Pipe SRW across this native call: original
|
|
* kill/index/holder cleanup invokes virtual callbacks. Native owner CS is
|
|
* acquired with the existing bounded helper and may be reentered by original.
|
|
* This helper never calls Shutdown, clears native fields, or disposes nodes.
|
|
* Suspended SEND queues remain the caller's separate qualified responsibility.
|
|
*/
|
|
#ifndef GW_NATIVE_DISCONNECT_CURRENT_OWNER
|
|
#define GW_NATIVE_DISCONNECT_CURRENT_OWNER (*(void**)0x145e7a098ULL)
|
|
#endif
|
|
#ifndef GW_NATIVE_DISCONNECT_CODE
|
|
#define GW_NATIVE_DISCONNECT_CODE ((const unsigned char*)0x141e464f0ULL)
|
|
#endif
|
|
#ifndef GW_NATIVE_DISCONNECT_CALL
|
|
#define GW_NATIVE_DISCONNECT_CALL(owner,reason) \
|
|
((void (WINAPI *)(void*,uint32_t))0x141e464f0ULL)((owner),(reason))
|
|
#endif
|
|
static int gw_native_disconnect_readable(const void* pointer,SIZE_T length){
|
|
MEMORY_BASIC_INFORMATION m;uintptr_t start=(uintptr_t)pointer,end;
|
|
if(start<0x10000 || !length || start>UINTPTR_MAX-length)return 0;
|
|
end=start+length;
|
|
return VirtualQuery(pointer,&m,sizeof(m)) && m.State==MEM_COMMIT &&
|
|
!(m.Protect&(PAGE_NOACCESS|PAGE_GUARD)) &&
|
|
start>=(uintptr_t)m.BaseAddress && end<=(uintptr_t)m.BaseAddress+m.RegionSize;
|
|
}
|
|
#ifndef GW_NATIVE_DISCONNECT_MAIN_BASE
|
|
#define GW_NATIVE_DISCONNECT_MAIN_BASE ((uintptr_t)0x140000000ULL)
|
|
#endif
|
|
/* Lone C3 on the same original page01e46000: the epilogue at 141e46f57 is
|
|
* 48 83 c4 40 5f c3 followed by cc padding (saved original netclient-code,
|
|
* sha256 132a4468...). Executing it changes no state besides the client's own
|
|
* first-execution handling of that page, as in realm_seam_page.c. */
|
|
#ifndef GW_NATIVE_DISCONNECT_PAGE_TOUCH
|
|
#define GW_NATIVE_DISCONNECT_PAGE_TOUCH() ((void (*)(void))0x141e46f5cULL)()
|
|
#endif
|
|
/* 0 when the exact original entry may be called. Each failed part of the
|
|
* code contract has its own result so one actual run names the cause:
|
|
* -43 no committed page, -45 page type/allocation, -46 protection,
|
|
* -47 entry bytes, -48 lazily protected (PAGE_NOACCESS) Main page.
|
|
* The actual Wine guest maps Main code MEM_MAPPED with allocation base
|
|
* 0x140000000 and PAGE_EXECUTE_READ while live (realm_seam_page.h,
|
|
* login_site_pages.c, constructor_seam_plan.c); native Windows reports
|
|
* MEM_IMAGE. A mapped page is admitted only in that pinned profile. On
|
|
* either platform the client re-protects idle code pages, so at the account
|
|
* screen this page is normally PAGE_NOACCESS again. */
|
|
static int gw_native_disconnect_code_state(void){
|
|
/* Frozen readable original page01e46000; exact base Disconnect entry. */
|
|
static const unsigned char prefix[]={0x89,0x54,0x24,0x10,0x55,0x56,0x41,0x54,
|
|
0x41,0x55,0x48,0x8d,0xac,0x24,0x18,0xff,0xff,0xff};
|
|
MEMORY_BASIC_INFORMATION m;const unsigned char* target=GW_NATIVE_DISCONNECT_CODE;
|
|
if(!VirtualQuery(target,&m,sizeof(m)) || m.State!=MEM_COMMIT)return -43;
|
|
if(m.Type!=MEM_MAPPED && m.Type!=MEM_IMAGE)return -45;
|
|
if(m.Type==MEM_MAPPED && (uintptr_t)m.AllocationBase!=GW_NATIVE_DISCONNECT_MAIN_BASE)return -45;
|
|
/* Idle pages are re-protected (realm_seam_page.h); that is the state at the
|
|
* account screen. Only a Main page may be executed to lift it. */
|
|
if(m.Protect==PAGE_NOACCESS)return (uintptr_t)m.AllocationBase==GW_NATIVE_DISCONNECT_MAIN_BASE?-48:-46;
|
|
if(m.Type==MEM_MAPPED){
|
|
if(m.Protect!=PAGE_EXECUTE_READ)return -46;
|
|
}else if(!(m.Protect&(PAGE_EXECUTE|PAGE_EXECUTE_READ|PAGE_EXECUTE_READWRITE|PAGE_EXECUTE_WRITECOPY)))return -46;
|
|
if(!gw_native_disconnect_readable(target,sizeof(prefix)))return -43;
|
|
return memcmp(target,prefix,sizeof(prefix))?-47:0;
|
|
}
|
|
/* The client keeps idle Main code pages PAGE_NOACCESS and decrypts them in its
|
|
* own handler on first execution. Its own call of this entry would simply run
|
|
* through that handler; this helper has to read the entry bytes first, so a
|
|
* page in exactly that state is executed once through the lone C3 and then
|
|
* checked again. -48 remains only if the page stayed protected. */
|
|
static int gw_native_disconnect_code_prepare(void){
|
|
int state=gw_native_disconnect_code_state();
|
|
if(state!=-48)return state;
|
|
GW_NATIVE_DISCONNECT_PAGE_TOUCH();
|
|
return gw_native_disconnect_code_state();
|
|
}
|
|
/* Returns0 only for an already-clean state2 or checked original cleanup.
|
|
* -40 caller/thread, -41 object, -42 native state/flags/secondary channels,
|
|
* -43/-45..-48 code contract (see gw_native_disconnect_code_state),
|
|
* -44 original postcondition. Existing native-lock errors (including
|
|
* transient-23) are propagated unchanged. */
|
|
static int gw_native_disconnect_retained_primary(void* owner,DWORD primary_thread){
|
|
unsigned char* p=(unsigned char*)owner;uint64_t holder;int result;
|
|
if(!owner || !primary_thread || GetCurrentThreadId()!=primary_thread)return -40;
|
|
if(!gw_native_disconnect_readable(owner,0xc4b))return -41;
|
|
result=gw_wire_channel_retirement_lock(owner);
|
|
if(result)return result;
|
|
if(GW_NATIVE_DISCONNECT_CURRENT_OWNER!=owner ||
|
|
*(LONG*)(p+0x98)!=2 || p[0xc49] || p[0xc4a] ||
|
|
*(uint64_t*)(p+0x1a8) || *(uint64_t*)(p+0x1b0) || *(uint64_t*)(p+0x1b8)){
|
|
result=-42;goto done;
|
|
}
|
|
holder=*(uint64_t*)(p+0x228);
|
|
if(!*(uint64_t*)(p+0x1a0)){
|
|
result=holder && (!gw_native_disconnect_readable((void*)(uintptr_t)holder,8) ||
|
|
*(uint64_t*)(uintptr_t)holder)?-44:0;goto done;
|
|
}
|
|
result=gw_native_disconnect_code_prepare();
|
|
if(result)goto done;
|
|
GW_NATIVE_DISCONNECT_CALL(owner,14u);
|
|
/* No native return-value interpretation and no forced pointer correction. */
|
|
if(GW_NATIVE_DISCONNECT_CURRENT_OWNER!=owner ||
|
|
!gw_native_disconnect_readable(owner,0xc4b) ||
|
|
*(LONG*)(p+0x98)!=2 || *(uint64_t*)(p+0x1a0) ||
|
|
p[0xc49] || p[0xc4a] || *(uint64_t*)(p+0x1a8) ||
|
|
*(uint64_t*)(p+0x1b0) || *(uint64_t*)(p+0x1b8)){
|
|
result=-44;goto done;
|
|
}
|
|
holder=*(uint64_t*)(p+0x228);
|
|
result=holder && (!gw_native_disconnect_readable((void*)(uintptr_t)holder,8) ||
|
|
*(uint64_t*)(uintptr_t)holder)?-44:0;
|
|
done:
|
|
gw_wire_channel_retirement_unlock(owner);return result;
|
|
}
|
|
#endif
|