#!/usr/bin/env python3 """Locate the original cache's public getuid stub without rewriting any input.""" import hashlib import json from pathlib import Path import struct import subprocess ROOT = Path(__file__).resolve().parents[1] source = ROOT / 'refer/dyld' revision = subprocess.check_output(['git', '-C', source, 'rev-parse', 'HEAD'], text=True).strip() if revision != 'a571176e8e00c47e95b95e3156820ebec0cbd5e6': raise SystemExit('Expected the pinned Darwin 23 dyld format source') out = ROOT / 'build/cache-leaf-host' out.mkdir(parents=True, exist_ok=True) unit = out / 'offsets.cpp' unit.write_text('''#include #include #include #include #include "dyld_cache_format.h" int main() { printf("%zu %zu\\n", offsetof(dyld_cache_header, imagesOffset), sizeof(dyld_cache_image_info)); } ''') subprocess.run(['xcrun', 'clang++', '-std=c++17', '-Wall', '-Wextra', '-Werror', '-I', source / 'cache-builder', unit, '-o', out / 'offsets'], check=True) images_field, image_size = map(int, subprocess.check_output([out / 'offsets'], text=True).split()) if image_size != 32: raise SystemExit('Unexpected original image table ABI') directory = ROOT / '.cache/firmware/target-volumes/systemos-mount/System/Library/Caches/com.apple.dyld' main = (directory / 'dyld_shared_cache_arm64').read_bytes() audit = json.loads((ROOT / 'build/target-userspace/dyld-cache-audit.json').read_text()) def read(va, size): for row in audit['cache_files']: for mapping in row['mappings']: start = int(mapping['address'], 16) if start <= va and va + size <= start + mapping['size']: offset = mapping['fileoff'] + va - start with (directory / row['file']).open('rb') as stream: stream.seek(offset) data = stream.read(size) if len(data) != size: raise SystemExit('Short original cache read') return data, row['file'], offset raise SystemExit('Address is outside actual target mappings') at, count = struct.unpack_from(' 4096: raise SystemExit('The original getuid success path is not self-contained in one native page') path = directory / filename with path.open('rb') as stream: cache_header = stream.read(56) sig_offset, sig_size = struct.unpack_from('I', signature, 8)[0]): _, offset = struct.unpack_from('>II', signature, 12 + index * 8) magic, length = struct.unpack_from('>II', signature, offset) if magic == 0xfade0c02: cd = signature[offset:offset + length] code_directories.append((hashlib.sha256(cd).digest()[:20].hex(), cd[39])) expected = next(r for r in audit['cache_files'] if r['file'] == filename)['code_directories'] if len(code_directories) != 1 or code_directories != [(expected[0]['cdhash'], 14)]: raise SystemExit('Expected the actual unchanged 16K-signed original cache file') digest = hashlib.sha256() with path.open('rb') as stream: for chunk in iter(lambda: stream.read(1024 * 1024), b''): digest.update(chunk) report = dict(source_revision=revision, library='/usr/lib/system/libsystem_kernel.dylib', image_address=hex(image), public_export='_getuid', original_address=hex(target), file=filename, file_bytes=path.stat().st_size, file_sha256=digest.hexdigest(), file_offset=file_offset, native_page_offset=file_offset & ~4095, within_page_offset=file_offset & 4095, function_bytes=code.hex(), original_page_sha256=hashlib.sha256(native_page).hexdigest(), success_return_offset=36, syscall_number=24, signature_offset=sig_offset, signature_size=sig_size, original_cdhash=code_directories[0][0], signing_block_bytes=16384, original_input_modified=False, host_source_analysis_only=True, runtime_executed=False) (out / 'getuid.json').write_text(json.dumps(report, indent=2) + '\n') print('PASS: actual public getuid export located in original 16K-signed cache; runtime still unqualified') print(filename, hex(file_offset), digest.hexdigest())