367 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
89 lines
3.7 KiB
Python
89 lines
3.7 KiB
Python
"""Capture only the guarded validator code page on its natural first use.
|
|
|
|
The address is from the actual Enter World rejection trace. This observer does
|
|
not invoke the function, read arguments/data buffers, or change protections.
|
|
It checks the mapping's execute permission independently of read permission;
|
|
execute-only mappings are still eligible for one bounded /proc memory read.
|
|
If the mapping is no longer executable or the read is denied, it reports that
|
|
and leaves it alone.
|
|
"""
|
|
import hashlib
|
|
import os
|
|
from pathlib import Path
|
|
|
|
ENTRY = 0x140208810
|
|
PAGE = ENTRY & ~4095
|
|
PREFIX = b''
|
|
OBSERVE_NATURAL_FIRST_USE = True
|
|
|
|
|
|
def inspect(read, executable, page=PAGE):
|
|
if not executable(page, 4096):
|
|
return {'available': False, 'reason': 'fixed-validator-page-not-executable',
|
|
'engine_calls_made': False, 'arguments_read': False,
|
|
'memory_protection_changed': False}
|
|
code = read(page, 4096)
|
|
if len(code) != 4096:
|
|
raise ValueError('Incomplete fixed validator code page')
|
|
return {'available': True, 'page': hex(page),
|
|
'sha256': hashlib.sha256(code).hexdigest(), 'code': code,
|
|
'engine_calls_made': False, 'arguments_read': False,
|
|
'memory_protection_changed': False}
|
|
|
|
|
|
def capture_code_page(pid, registers, entry, page):
|
|
if registers.rip != entry:
|
|
return {'available': False, 'reason': 'different-entry',
|
|
'engine_calls_made': False, 'arguments_read': False}
|
|
intervals = []
|
|
for line in Path(f'/proc/{pid}/maps').read_text().splitlines():
|
|
fields = line.split()
|
|
lo, hi = (int(value, 16) for value in fields[0].split('-'))
|
|
intervals.append((lo, hi, fields[1]))
|
|
fd = os.open(f'/proc/{pid}/mem', os.O_RDONLY)
|
|
try:
|
|
def mapping(address, size):
|
|
return any(lo <= address and address + size <= hi and
|
|
'x' in permissions for lo, hi, permissions in intervals)
|
|
|
|
def readable(address, size):
|
|
return any(lo <= address and address + size <= hi and
|
|
'r' in permissions for lo, hi, permissions in intervals)
|
|
|
|
def read(address, size):
|
|
if address != page or size != 4096 or not mapping(address, size):
|
|
raise ValueError('Only the fixed executable validator page may be read')
|
|
code = os.pread(fd, size, address)
|
|
if len(code) != size:
|
|
raise ValueError('Incomplete fixed validator code page')
|
|
return code
|
|
|
|
try:
|
|
result = inspect(read, mapping, page)
|
|
code = result.pop('code', None)
|
|
if code is not None:
|
|
folder = Path(__file__).resolve().parents[1] / 'runtime/native-handler-code'
|
|
folder.mkdir(exist_ok=True)
|
|
path = folder / f'{page:x}-{result["sha256"][:16]}.bin'
|
|
if path.exists() and path.read_bytes() != code:
|
|
raise ValueError('Preserve existing validator capture')
|
|
if not path.exists():
|
|
path.write_bytes(code)
|
|
path.chmod(0o600)
|
|
result['capture'] = str(path.relative_to(Path(__file__).resolve().parents[1]))
|
|
result['kind'] = 'natural-boundary-code-only'
|
|
result['arguments_read'] = False
|
|
return result
|
|
except (OSError, ValueError):
|
|
return {'available': False,
|
|
'reason': 'fixed-code-read-denied' if mapping(page, 4096) and not readable(page, 4096)
|
|
else 'fixed-code-unavailable',
|
|
'engine_calls_made': False, 'arguments_read': False,
|
|
'memory_protection_changed': False}
|
|
finally:
|
|
os.close(fd)
|
|
|
|
|
|
def capture(pid, registers):
|
|
return capture_code_page(pid, registers, ENTRY, PAGE)
|