88 lines
6.4 KiB
C++
88 lines
6.4 KiB
C++
#include "native_account_name.h"
|
|
#include "native_account_reserve_code.h"
|
|
#include "windows_backend_ipc.hpp"
|
|
#include <array>
|
|
#include <cstdio>
|
|
#include <cstring>
|
|
#include <stdexcept>
|
|
#include <vector>
|
|
#define CHECK(x) do{if(!(x))throw std::runtime_error("failed: " #x);}while(0)
|
|
struct Heap {
|
|
gw_native_account_string* object=nullptr;std::array<char,64> bytes{};
|
|
unsigned reserves=0;bool deny=false,fail=false;
|
|
};
|
|
static int writable(void* p,size_t n,void* opaque){
|
|
auto& h=*static_cast<Heap*>(opaque);if(h.deny)return 0;
|
|
auto inside=[&](void* base,size_t size){auto a=(uintptr_t)p,b=(uintptr_t)base;return a>=b&&a-b<=size&&n<=size-(a-b);};
|
|
return inside(h.object,sizeof(*h.object))||inside(h.bytes.data(),h.bytes.size());
|
|
}
|
|
static int reserve(void* object,size_t n,void* opaque){
|
|
auto& h=*static_cast<Heap*>(opaque);++h.reserves;CHECK(object==h.object&&n<=32);
|
|
if(h.fail)return -1;
|
|
std::memcpy(h.bytes.data(),(const void*)(uintptr_t)h.object->pointer,h.object->size+1);
|
|
h.object->pointer=(uint64_t)(uintptr_t)h.bytes.data();h.object->capacity=63;return 0;
|
|
}
|
|
int main(){try{
|
|
static_assert(sizeof(gw_native_account_string)==40&&offsetof(gw_native_account_string,small)==24);
|
|
std::vector<unsigned char> image(GW_CURRENT_REALM_OWNER_RVA+8u);
|
|
std::array<unsigned char,0xc60> owner{};uint32_t address=0x46010046,id=70;uint64_t pointer=(uint64_t)(uintptr_t)owner.data();
|
|
memcpy(image.data(),gw_engine_shipped_main_header_bytes,1024);
|
|
memcpy(image.data()+GW_CURRENT_REALM_OWNER_RVA,&pointer,8);
|
|
memcpy(owner.data()+0x898,&address,4);memcpy(owner.data()+0x89c,&id,4);owner[0x8a0]='R';
|
|
auto* object=(gw_native_account_string*)(image.data()+GW_NATIVE_ACCOUNT_NAME_RVA);Heap heap;heap.object=object;
|
|
auto reset=[&]{*object={};object->pointer=(uint64_t)(uintptr_t)object->small;object->capacity=GW_NATIVE_ACCOUNT_SSO;heap.reserves=0;heap.fail=heap.deny=false;};
|
|
auto bind=[&](const char* name,size_t size,uint32_t build=70334,int bound=1,uint32_t addr=0x46010046,size_t span=GW_CURRENT_REALM_OWNER_RVA+8u,int early=1){
|
|
return gw_bind_native_account_name(image.data(),span,owner.data(),owner.size(),build,bound,early,addr,name,size,reserve,writable,&heap);};
|
|
// These are synthetic names. No fixture contains a real account or credential.
|
|
for(const char* name: {"TESTUSER","ABCDEFGHIJKLMNO","ABCDEFGHIJKLMNOP","ABCDEFGHIJKLMNOPQRSTUVWXYZ123456"}){
|
|
reset();std::array<unsigned char,56> prefix;std::array<unsigned char,16> suffix;
|
|
std::memset(image.data()+GW_NATIVE_ACCOUNT_NAME_RVA-56,0xa5,56);
|
|
std::memset(image.data()+GW_NATIVE_ACCOUNT_NAME_RVA+40,0x5a,16);
|
|
std::memcpy(prefix.data(),image.data()+GW_NATIVE_ACCOUNT_NAME_RVA-56,56);
|
|
std::memcpy(suffix.data(),image.data()+GW_NATIVE_ACCOUNT_NAME_RVA+40,16);auto old_owner=owner;
|
|
CHECK(bind(name,strlen(name)+1)==0);CHECK(object->size==strlen(name));
|
|
CHECK(!strcmp((char*)(uintptr_t)object->pointer,name));CHECK(heap.reserves==(strlen(name)>15?1u:0u));
|
|
CHECK(object->pointer!=(uint64_t)(uintptr_t)name);CHECK(owner==old_owner);
|
|
CHECK(!memcmp(prefix.data(),image.data()+GW_NATIVE_ACCOUNT_NAME_RVA-56,56));
|
|
CHECK(!memcmp(suffix.data(),image.data()+GW_NATIVE_ACCOUNT_NAME_RVA+40,16));
|
|
}
|
|
// Existing client-owned heap survives a later short account name: no foreign
|
|
// pointer or manual free/SSO rewrite, and old text is NUL-terminated away.
|
|
CHECK(bind("SHORT",6)==0&&heap.reserves==2&&object->pointer==(uint64_t)(uintptr_t)heap.bytes.data());
|
|
CHECK(object->size==5&&!strcmp(heap.bytes.data(),"SHORT"));
|
|
for(const char* invalid:{"","../bad","bad\\name","bad/name","NAME#123","BAD:NAME",".","..","NAME."," NAME","NAME ","ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567"}){
|
|
reset();auto before=*object;CHECK(bind(invalid,strlen(invalid)+1)==-1);CHECK(!memcmp(&before,object,sizeof(before)));CHECK(heap.reserves==0);
|
|
}
|
|
for(unsigned test=0;test<10;++test){
|
|
reset();auto before=*object;auto old_owner=owner;
|
|
if(test==0)image[0]^=1;
|
|
if(test==1)memset(image.data()+GW_CURRENT_REALM_OWNER_RVA,0,8);
|
|
if(test==2)owner[0x8a0]=0;
|
|
if(test==3)object->pointer++;
|
|
if(test==4)object->size=16;
|
|
if(test==5)heap.deny=true;
|
|
before=*object;
|
|
CHECK(bind("TESTUSER",9,test==6?70333:70334,test==7?0:1,test==8?0x46010047:address,test==9?1024:image.size())==-1);
|
|
CHECK(!memcmp(&before,object,sizeof(before)));CHECK(!heap.reserves);
|
|
image[0]=gw_engine_shipped_main_header_bytes[0];memcpy(image.data()+GW_CURRENT_REALM_OWNER_RVA,&pointer,8);owner=old_owner;
|
|
}
|
|
reset();object->pointer=1;object->capacity=63;{auto before=*object;
|
|
CHECK(bind("ABCDEFGHIJKLMNOP",17)==-1);CHECK(!heap.reserves);CHECK(!memcmp(&before,object,sizeof(before)));}
|
|
reset();{auto before=*object;CHECK(bind("TESTUSER",9,70334,1,address,image.size(),0)==-1);CHECK(!memcmp(&before,object,sizeof(before)));}
|
|
reset();heap.fail=true;auto before=*object;CHECK(bind("ABCDEFGHIJKLMNOP",17)==-1);CHECK(!memcmp(&before,object,sizeof(before)));
|
|
reset();CHECK(bind("TESTUSER",8)==-1);CHECK(!heap.reserves);
|
|
// New internal request/reply travels through existing typed Backend IPC,
|
|
// without inventing realm/character/Battle.net fields to carry the name.
|
|
namespace ipc=gateway::backend_ipc;
|
|
std::array<unsigned char,33> output{};gw_wire_call request{};request.abi=GW_WINE_WIRE_ABI;request.size=sizeof(request);
|
|
request.operation=GW_WIRE_ACCOUNT_NAME;request.transport=123;request.output=output.data();request.capacity=output.size();
|
|
ipc::Context context{1,1};auto wire=ipc::encode_request(request,context);auto decoded=ipc::decode_request(wire,context);
|
|
CHECK(decoded->call.operation==GW_WIRE_ACCOUNT_NAME&&decoded->call.input_bytes==0&&decoded->call.transport==123);
|
|
decoded->call.result=0;decoded->call.required=9;memcpy(decoded->call.output,"TESTUSER",9);
|
|
auto response=ipc::encode_response(decoded->call,0,context);CHECK(ipc::apply_response(response,request,context)==0);
|
|
CHECK(request.required==9&&!strcmp((char*)output.data(),"TESTUSER"));
|
|
bool rejected=false;try{ipc::operation(GW_WIRE_ACCOUNT_NAME+1);}catch(const std::exception&){rejected=true;}CHECK(rejected);
|
|
std::puts("native account name PASS: fake image/layout, SSO15/heap16..32, engine reserve, guards, isolated fields and IPC");
|
|
return 0;
|
|
}catch(const std::exception& e){std::fprintf(stderr,"%s\n",e.what());return 1;}}
|