"""Pure guards for the original dyld prefix/stack proof; no native execution.""" import copy import importlib import struct import unittest from pathlib import Path from unittest.mock import Mock, patch from differential import State, state_dict suite = importlib.import_module('dyld-entry-differential') def thin(): data = bytearray(0x40000) struct.pack_into('<8I', data, 0, 0xfeedfacf, 0x100000c, 0x80000002, 7, 2, 360, 0, 0) struct.pack_into('<2I16s4Q4I', data, 32, 0x19, 72, b'__TEXT', 0, len(data), 0, len(data), 5, 5, 0, 0) struct.pack_into('<4I', data, 104, 5, 288, 6, 68) struct.pack_into('2I', container, 0, 0xcafebabe, 1) struct.pack_into('>5I', container, 8, 0x100000c, 0x80000002, 4096, len(image), 12) self.assertEqual(suite.classic_slice(container), (4096, image)) for position, value in [(12, 0x8000000c), (16, 4097), (20, len(image) + 1), (24, 32)]: changed = bytearray(container) struct.pack_into('>I', changed, position, value) with self.assertRaises(AssertionError): suite.classic_slice(changed) with self.assertRaises(AssertionError): suite.classic_slice(b'bad') def test_original_prefix_thread_and_pac_boundary_are_required(self): data = thin() view = suite.parse_dyld(data) self.assertEqual(view['prefix'], struct.pack('<5I', *suite.PREFIX_WORDS)) self.assertEqual(len(view['thread_command']), 288) for offset in [suite.ENTRY, suite.ENTRY + 16, suite.TARGET, 104 + 8, 104 + 272, 36]: changed = bytearray(data) changed[offset] ^= 1 with self.assertRaises(AssertionError): suite.parse_dyld(changed) def test_math_preserves_full_vector_fp_and_non_entry_register_state(self): initial = State() initial.x[:] = range(1, 32) initial.sp, initial.nzcv, initial.reserved = 0x220004000008, 0xf0000000, 0xa5a5 initial.fpcr, initial.fpsr = 0x03c00000, 0x0800001f expected = suite.expected_state(initial) self.assertEqual(expected.x[0], initial.sp) self.assertEqual(expected.sp, initial.sp & ~15) self.assertEqual((expected.x[29], expected.x[30]), (0, 0)) self.assertEqual(list(expected.x)[1:29], list(initial.x)[1:29]) self.assertEqual(bytes(expected)[256:], bytes(initial)[256:]) def test_original_regions_preserve_file_padding_and_zero_only_segment_tail(self): image = thin() image[-1] = 0x5a struct.pack_into('> suite.A64_MOVE_IMMEDIATE_SHIFT) & 0xffff, errno) self.assertEqual(region['address'], base + offset) self.assertEqual(region['data'][:len(source) - offset], source[offset:]) self.assertEqual(region['initial_protection'], suite.UC_PROT_READ | suite.UC_PROT_EXEC) with self.assertRaises(AssertionError): suite.branch_with_link(0, 1) with self.assertRaises(AssertionError): suite.branch_with_link(0, 1 << (suite.A64_BRANCH_IMMEDIATE_BITS + 1)) def test_helper_audit_rejects_state_memory_and_descriptor_drift(self): initial = suite.states(all_flags=True)[15] destination = suite.A64_ZERO_REGISTER_INDEX source, base, instruction_pc = b'probe', suite.MACH_FIXTURE_BASES[0], 0x1000 regions = [{'address': base, 'virtual_bytes': 16, 'data': bytes(16)}] snapshot = [{'address': base, 'bytes': 16, 'sha256': suite.digest(bytes(16))}] native = {'kind': 'native_block_run', 'abi': suite.THREAD_REGISTER_PAUTH_ABI, 'complete_binary_verified': False, 'source_sha256': suite.digest(source), 'guest_base': base, 'state': state_dict(initial), 'memory': {'memory_before': snapshot, 'memory_after': snapshot}, 'thread_context': {'thread_specific_data': 1}, 'outcome': {'thread_register_events': [{ 'instruction': {'destination': destination, 'instruction_pc': instruction_pc, 'next_pc': instruction_pc + suite.A64_INSTRUCTION_BYTES}, 'next_pc': instruction_pc + suite.A64_INSTRUCTION_BYTES, 'value': 1}]}} machine = Mock() machine.reg_read.return_value, machine.mem_read.return_value = 1, bytes(16) with patch.object(suite, 'capture_arm', return_value=initial): suite.compare_thread_register_run(native, machine, initial, regions, base, source, destination, instruction_pc) mutations = [] for key, value in [('abi', suite.ABI), ('source_sha256', 'other'), ('complete_binary_verified', True)]: changed = copy.deepcopy(native) changed[key] = value mutations.append(changed) changed = copy.deepcopy(native) changed['state']['v'][31][1] ^= 1 mutations.append(changed) changed = copy.deepcopy(native) changed['memory']['memory_after'] = [] mutations.append(changed) changed = copy.deepcopy(native) changed['thread_context']['thread_specific_data'] = 0 mutations.append(changed) changed = copy.deepcopy(native) changed['outcome']['thread_register_events'][0]['instruction']['destination'] = 0 mutations.append(changed) for changed in mutations: with self.assertRaises(AssertionError): suite.compare_thread_register_run(changed, machine, initial, regions, base, source, destination, instruction_pc) class StartupJoinGuards(unittest.TestCase): def test_timer_pause_counts_post_counter_instruction_only_on_resume(self): oracle = suite.StartupOracle.__new__(suite.StartupOracle) oracle.base = suite.MACH_FIXTURE_BASES[0] offset = suite.STARTUP_COUNTER_CONTINUATION oracle.image = bytes(offset) + b'\x01\x02\x03\x04' oracle.counter, oracle.pcs, oracle.checkpoints = None, [], [] machine = Mock() machine.mem_read.return_value = oracle.image[offset:] machine.reg_read.return_value = 123 oracle.code(machine, oracle.base + offset, suite.A64_INSTRUCTION_BYTES, None) self.assertEqual((oracle.counter, oracle.pcs), (123, [])) machine.emu_stop.assert_called_once() oracle.code(machine, oracle.base + offset, suite.A64_INSTRUCTION_BYTES, None) self.assertEqual(oracle.pcs, [offset]) machine.mem_read.return_value = b'changed' with self.assertRaises(AssertionError): oracle.code(machine, oracle.base + offset, suite.A64_INSTRUCTION_BYTES, None) def test_crossarch_audit_rejects_extra_service_and_unrelated_state_changes(self): before = state_dict(State()) before['x'][3], before['nzcv'] = 0x123456, 0xf0000000 after = copy.deepcopy(before) after['x'][0] = suite.DARWIN_ENOTSUP oracle = Mock(serviced=True, svcs=[{'before': before, 'after': after}]) event = {'declined': None, 'request': {'number': suite.DARWIN_CROSSARCH_TRAP, 'arguments': [0], 'prototype': {'argument_count': 1, 'return_type': 'int'}, 'indirect': False}, 'completion': {'kind': 'error', 'errno': suite.DARWIN_ENOTSUP}} native = {'crossarch_service_events': [event], 'crossarch_service_profile': 'darwin-crossarch-unavailable-v1'} suite.audit_startup_crossarch(native, oracle) for field, value in [('declined', 'unsupported'), ('completion', {'kind': 'error', 'errno': 22}), ('request', {'number': suite.DARWIN_CROSSARCH_TRAP, 'arguments': [0, 0]})]: changed = copy.deepcopy(native) changed['crossarch_service_events'][0][field] = value with self.assertRaises(AssertionError): suite.audit_startup_crossarch(changed, oracle) for index in (1, 3, 30): changed = copy.deepcopy(after) changed['x'][index] ^= 1 with self.assertRaises(AssertionError): suite.audit_startup_crossarch(native, Mock(serviced=True, svcs=[{'before': before, 'after': changed}])) pending = Mock(serviced=False, svcs=[{'before': before, 'after': copy.deepcopy(before)}]) suite.audit_startup_crossarch({'crossarch_service_events': [], 'crossarch_service_profile': None}, pending) with self.assertRaises(AssertionError): suite.audit_startup_crossarch(native, pending) def test_clock_audit_requires_declared_fields_unchanged_bytes_and_single_counter_read(self): pac = Mock(CLOCK_PROFILE='clock', CLOCK_BASE=0x10000, CLOCK_BYTES=16) pac.clock_page.return_value = bytes(16) snapshot = {'counter': 123, 'sleep_ticks': suite.STARTUP_SLEEP_TICKS} native = {'clock': {'profile_id': pac.CLOCK_PROFILE, 'legacy_configuration': {'kdebug': 'disabled'}, 'legacy_commpage_profile': suite.STARTUP_CLOCK_PROFILE, 'source_id': 'explicit-finite-replay-v1', 'snapshot': snapshot, 'events': [{'sequence': index, 'snapshot': snapshot} for index in (0, 1)], 'page_sha256': suite.digest(bytes(16)), 'region': {'guest_address': pac.CLOCK_BASE, 'virtual_bytes': pac.CLOCK_BYTES, 'readable': True, 'writable': False, 'executable': False}}, 'outcome': {'clock_events': [{'instruction': {'operation': {'kind': 'read_virtual_counter'}}, 'counter': 123}]}} suite.audit_startup_clock(native, pac, 123) for field, value in [('legacy_configuration', {'kdebug': None}), ('legacy_commpage_profile', 'other'), ('page_sha256', suite.digest(bytes(15))), ('events', [])]: changed = copy.deepcopy(native) changed['clock'][field] = value with self.assertRaises(AssertionError): suite.audit_startup_clock(changed, pac, 123) changed = copy.deepcopy(native) changed['outcome']['clock_events'] *= 2 with self.assertRaises(AssertionError): suite.audit_startup_clock(changed, pac, 123) if __name__ == '__main__': unittest.main()