367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
112 lines
6.8 KiB
Python
112 lines
6.8 KiB
Python
"""Reversible per-object poll binding at the observed owned-child safepoint.
|
|
|
|
No engine calls, code writes, auth-state changes, Lua or global vtable changes.
|
|
Only the initialized, disconnected diagnostic NetClient may be bound.
|
|
"""
|
|
import hashlib,json,os,struct
|
|
from pathlib import Path
|
|
ROOT=Path(__file__).resolve().parents[1]
|
|
BASE=0x140000000
|
|
SIZE=1080
|
|
class Access:
|
|
def __init__(self,pid):
|
|
self.pid=pid;self.maps=[]
|
|
for line in Path(f'/proc/{pid}/maps').read_text().splitlines():
|
|
fields=line.split();low,high=(int(v,16) for v in fields[0].split('-'))
|
|
self.maps.append((low,high,fields[1],fields[2],fields[-1]))
|
|
self.fd=os.open(f'/proc/{pid}/mem',os.O_RDWR)
|
|
def close(self):os.close(self.fd)
|
|
def allowed(self,address,size,permission):
|
|
return any(low<=address and address+size<=high and permission in permissions for low,high,permissions,_,_ in self.maps)
|
|
def read(self,address,size):
|
|
if not 0<size<=2048 or not self.allowed(address,size,'r'):raise ValueError('Known binding data is unavailable')
|
|
data=os.pread(self.fd,size,address)
|
|
if len(data)!=size:raise ValueError('Incomplete known binding read')
|
|
return data
|
|
def u64(self,address):return struct.unpack('<Q',self.read(address,8))[0]
|
|
def write(self,address,data):
|
|
if not 0<len(data)<=2048 or not self.allowed(address,len(data),'w'):raise ValueError('Binding data is not writable')
|
|
if os.pwrite(self.fd,data,address)!=len(data) or self.read(address,len(data))!=data:raise ValueError('Binding data write failed')
|
|
def module(self):
|
|
path=ROOT/'runtime/native-wine-prefix/drive_c/windows/system32/GWNativeBridge.dll'
|
|
meta=json.loads((ROOT/'runtime/native-engine-pe.json').read_text())
|
|
if meta.get('restored') or hashlib.sha256(path.read_bytes()).hexdigest()!=meta['dll_sha256']:raise ValueError('Owned module identity differs')
|
|
bases=[low for low,_,_,offset,name in self.maps if offset=='00000000' and name==str(path)]
|
|
if len(bases)!=1:raise ValueError('Owned module header is ambiguous')
|
|
return bases[0],meta
|
|
|
|
if os.name=='nt':
|
|
from windows_process_access import Access
|
|
|
|
def require_safepoint(caller):
|
|
if caller!='0x1401dfad3':raise ValueError('Require the observed genuine frame-pump caller')
|
|
|
|
def install(pid,caller,packet=False,*,prepare_binding=None):
|
|
if prepare_binding is not None and not callable(prepare_binding):raise ValueError('Invalid binding preparation')
|
|
require_safepoint(caller);access=Access(pid)
|
|
try:
|
|
module,meta=access.module();data=module+meta['exports']['gw_poll_host_data'];callback=module+meta['exports']['gw_packet_poll_callback' if packet else 'gw_poll_host_callback']
|
|
if struct.unpack('<II',access.read(data,8))!=(1,SIZE) or not access.allowed(callback,1,'x'):raise ValueError('Owned poll-host ABI differs')
|
|
owner=access.u64(0x145e7a098)
|
|
if not owner or owner%8:raise ValueError('NetClient pointer is not aligned')
|
|
if struct.unpack('<I',access.read(owner+0x98,4))[0]!=2 or any(access.read(owner+0x1a0,32)):raise ValueError('Require initialized disconnected diagnostic NetClient')
|
|
queue=access.u64(owner+0xa0)
|
|
if not queue or access.u64(queue)!=owner:raise ValueError('Queue owner mismatch')
|
|
original=access.u64(owner)
|
|
if not BASE+0x47c2000<=original<BASE+0x5663024:raise ValueError('Original vtable outside pinned read-only image data')
|
|
entries=[]
|
|
for index in range(129):
|
|
value=access.u64(original+index*8)
|
|
if not BASE+0x1000<=value<BASE+0x47c145c:break
|
|
entries.append(value)
|
|
if not 6<=len(entries)<=128:raise ValueError('Cannot establish complete bounded vtable')
|
|
poll=entries[5]
|
|
if not access.allowed(poll,1,'x'):raise ValueError('Genuine poll method has not executed')
|
|
entries[5]=callback
|
|
if packet:
|
|
packet_data=module+meta['exports']['gw_packet_host_data']
|
|
receiver=module+meta['exports']['gw_packet_receive_callback']
|
|
if len(entries)<7 or entries[6]!=0x141e47960 or not access.allowed(receiver,1,'x'):raise ValueError('Queued receiver identity differs')
|
|
if struct.unpack('<II',access.read(packet_data,8))!=(1,16440):raise ValueError('Packet host ABI differs')
|
|
if any(access.read(packet_data+8,16)):raise ValueError('Packet mailbox is already published')
|
|
access.write(packet_data+48,struct.pack('<Q',entries[6]));entries[6]=receiver
|
|
table=[access.u64(original-8),*entries]+[0]*(128-len(entries))
|
|
payload=struct.pack('<6I3Q129Q',1,SIZE,1,0,0,0,owner,original,poll,*table)
|
|
clone=data+56
|
|
binding={'pid':pid,'starttime':meta.get('starttime'),'owner':owner,'original_vtable':original,'bound_vtable':clone,'data':data,
|
|
'original_poll':poll,'method_count':len(entries),'restored':False,'packet_host':packet}
|
|
# Persist rollback before publishing the only change to an engine object.
|
|
path=ROOT/'runtime/engine-poll-binding.json';path.write_text(json.dumps(binding,indent=2)+'\n');path.chmod(0o600)
|
|
try:
|
|
access.write(data,payload)
|
|
if prepare_binding is not None:prepare_binding(access,module,meta,binding)
|
|
# Finish every owned-data/table write before publishing the object
|
|
# pointer. This remains a stopped-safepoint transaction: the remote
|
|
# write below is not a target-side atomic compare/exchange.
|
|
access.write(data+8,struct.pack('<I',2))
|
|
access.write(owner,struct.pack('<Q',clone))
|
|
except BaseException:
|
|
if access.u64(owner)==clone:access.write(owner,struct.pack('<Q',original))
|
|
binding['restored']=True;path.write_text(json.dumps(binding,indent=2)+'\n')
|
|
raise
|
|
finally:access.close()
|
|
|
|
def install_packet(pid,caller):install(pid,caller,packet=True)
|
|
|
|
def restore(pid,caller):
|
|
require_safepoint(caller)
|
|
path=ROOT/'runtime/engine-poll-binding.json';binding=json.loads(path.read_text())
|
|
if binding['pid']!=pid:raise ValueError('Binding belongs to another child')
|
|
access=Access(pid)
|
|
try:
|
|
owner=binding['owner']
|
|
if access.u64(0x145e7a098)!=owner or access.u64(owner)!=binding['bound_vtable']:raise ValueError('Engine object identity changed; preserve it')
|
|
if binding.get('wire_account_data'):
|
|
account=binding['wire_account_data']
|
|
if struct.unpack('<II',access.read(account,8))!=(1,1732):raise ValueError('Owned account cancellation ABI differs')
|
|
access.write(account+48,struct.pack('<I',1))
|
|
access.write(owner,struct.pack('<Q',binding['original_vtable']))
|
|
access.write(binding['data']+8,struct.pack('<I',4))
|
|
binding['restored']=True;path.write_text(json.dumps(binding,indent=2)+'\n')
|
|
finally:access.close()
|