883 files, from working commit b1a079a. Co-Authored-By: Claude Fable 5.2 <noreply@anthropic.com>
73 lines
3.3 KiB
Python
73 lines
3.3 KiB
Python
"""Exercise the bounded race-producer observer against real process memory."""
|
|
|
|
import ctypes
|
|
import hashlib
|
|
import os
|
|
import struct
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
sys.path.insert(0, str(Path(__file__).resolve().parents[1] / "ops"))
|
|
import engine_boundary_catalog as catalog
|
|
import engine_create_race_producer_observation as observer
|
|
|
|
|
|
def main():
|
|
libc = ctypes.CDLL(None, use_errno=True)
|
|
libc.mmap.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int,
|
|
ctypes.c_int, ctypes.c_int, ctypes.c_long]
|
|
libc.mmap.restype = ctypes.c_void_p
|
|
libc.mprotect.argtypes = [ctypes.c_void_p, ctypes.c_size_t, ctypes.c_int]
|
|
libc.munmap.argtypes = [ctypes.c_void_p, ctypes.c_size_t]
|
|
caller_page = observer.CALL_SITE & ~4095
|
|
target_page = observer.ENTRY & ~4095
|
|
stack_page = 0x150000000
|
|
pages = (caller_page, target_page, stack_page)
|
|
mapped = []
|
|
try:
|
|
for page in pages:
|
|
result = libc.mmap(page, 4096, 3, 0x100022, -1, 0)
|
|
if result != page:
|
|
raise RuntimeError("Fixed isolated fixture mapping unavailable")
|
|
mapped.append(page)
|
|
body = bytes((i * 17) % 256 for i in range(observer.BODY_BYTES))
|
|
ctypes.memmove(observer.CALL_SITE, observer.CALL_BYTES, len(observer.CALL_BYTES))
|
|
ctypes.memmove(observer.ENTRY, body, len(body))
|
|
ctypes.memmove(stack_page, struct.pack("<Q", observer.RETURN_ADDRESS), 8)
|
|
if libc.mprotect(caller_page, 4096, 5) or libc.mprotect(target_page, 4096, 5):
|
|
raise RuntimeError("Fixture executable mapping failed")
|
|
|
|
fd = os.open("/proc/self/mem", os.O_RDONLY)
|
|
reads = []
|
|
try:
|
|
def read(address, size):
|
|
reads.append((address, size))
|
|
return os.pread(fd, size, address)
|
|
|
|
readable = lambda address, size: stack_page <= address and address + size <= stack_page + 4096
|
|
executable = lambda address, size: any(page <= address and address + size <= page + 4096
|
|
for page in (caller_page, target_page))
|
|
result = observer.inspect(read, readable, executable, stack_page)
|
|
assert result["available"] and result["caller_verified"]
|
|
assert result["code"] == body and result["sha256"] == hashlib.sha256(body).hexdigest()
|
|
assert reads == [(stack_page, 8), (observer.CALL_SITE, 5), (observer.ENTRY, 375)]
|
|
assert not result["arguments_read"] and not result["packet_payloads_read"]
|
|
|
|
ctypes.memmove(stack_page, struct.pack("<Q", observer.RETURN_ADDRESS + 1), 8)
|
|
reads.clear()
|
|
rejected = observer.inspect(read, readable, executable, stack_page)
|
|
assert not rejected["available"] and rejected["reason"] == "different-original-caller"
|
|
assert reads == [(stack_page, 8)]
|
|
assert catalog.natural_first_use("create-race-producer", observer.ENTRY, observer)
|
|
assert not catalog.natural_first_use("create-race-producer", observer.ENTRY + 1, observer)
|
|
finally:
|
|
os.close(fd)
|
|
finally:
|
|
for page in mapped:
|
|
libc.munmap(page, 4096)
|
|
print("Bounded natural race-producer observer passed isolated memory checks.")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|