Run a virtual iPhone on an Apple Silicon Mac.
Swift
Count amfid holding the wanted requirement as adoption
Jackyd6b138dea3cb
diff --git a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/AMFIDTask.c b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/AMFIDTask.cindex 9683afc7..fc64ace4 100644--- a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/AMFIDTask.c+++ b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/AMFIDTask.c@@ -2,10 +2,12 @@// amfid, reading and writing its memory, and locating its singleton.#include "Escalator.h"+#include <arpa/inet.h>#include <dlfcn.h>#include <libproc.h>#include <objc/message.h>#include <ptrauth.h>+#include <Security/Security.h>#include <spawn.h>#include <stdio.h>#include <stdlib.h>@@ -187,6 +189,13 @@ int attach_amfid(const char *self_path) {return 1;}+static int peek_amfid(mach_vm_address_t addr, void *buf, size_t len) {+ mach_vm_size_t got = 0;+ return mach_vm_read_overwrite(g_task, addr, len, (mach_vm_address_t)buf, &got) ==+ KERN_SUCCESS &&+ got == len;+}+int read_amfid(mach_vm_address_t addr, void *buf, size_t len) {mach_vm_size_t got = 0;kern_return_t kr = mach_vm_read_overwrite(g_task, addr, len, (mach_vm_address_t)buf, &got);@@ -279,3 +288,67 @@ mach_vm_address_t amfid_manager(const char *self_path) {}return (mach_vm_address_t)remote;}++// --------------------------------------------------------------------------+// the requirement amfid holds+// --------------------------------------------------------------------------++// Where a SecRequirementRef keeps the pointer to its blob. Found in a+// requirement this process builds, by contents, the way singleton_slot finds+// its slot: the word whose target is that requirement's own blob. amfid links+// the same Security framework, so its requirements keep the pointer there too.+static ptrdiff_t requirement_blob_offset(void) {+ static ptrdiff_t offset = -1;+ static bool looked = false;+ if (looked) return offset;+ looked = true;++ SecRequirementRef req = NULL;+ CFDataRef data = NULL;+ if (SecRequirementCreateWithString(CFSTR("anchor apple"), kSecCSDefaultFlags, &req) !=+ errSecSuccess ||+ SecRequirementCopyData(req, kSecCSDefaultFlags, &data) != errSecSuccess) {+ if (req) CFRelease(req);+ return -1;+ }+ const UInt8 *want = CFDataGetBytePtr(data);+ size_t len = (size_t)CFDataGetLength(data);+ uint8_t got[256];+ if (len > 0 && len <= sizeof(got)) {+ for (ptrdiff_t at = 0; at < 256; at += sizeof(uintptr_t)) {+ uintptr_t word = 0;+ if (!peek_self((uintptr_t)req + at, &word, sizeof(word))) break;+ word = (uintptr_t)strip_pac((void *)word);+ if (!word || word == (uintptr_t)req) continue;+ if (peek_self(word, got, len) && memcmp(got, want, len) == 0) {+ offset = at;+ break;+ }+ }+ }+ CFRelease(data);+ CFRelease(req);+ return offset;+}++// The blob of the SecRequirementRef at `req` in amfid, or NULL when it cannot+// be read as one.+CFDataRef copy_amfid_requirement_data(mach_vm_address_t req) {+ ptrdiff_t off = requirement_blob_offset();+ req = (mach_vm_address_t)strip_pac((void *)req);+ if (off < 0 || !req) return NULL;+ uintptr_t blob = 0;+ if (!peek_amfid(req + (mach_vm_address_t)off, &blob, sizeof(blob))) return NULL;+ blob = (uintptr_t)strip_pac((void *)blob);+ uint32_t header[2] = {0, 0}; // magic and length, both big-endian+ if (!blob || !peek_amfid(blob, header, sizeof(header))) return NULL;+ uint32_t length = ntohl(header[1]);+ if (ntohl(header[0]) != 0xfade0c00 || length < sizeof(header) || length > 64 * 1024)+ return NULL;+ UInt8 *bytes = malloc(length);+ if (!bytes) return NULL;+ CFDataRef data = NULL;+ if (peek_amfid(blob, bytes, length)) data = CFDataCreate(NULL, bytes, (CFIndex)length);+ free(bytes);+ return data;+}diff --git a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Commands.c b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Commands.cindex 2ef47adc..f56450bb 100644--- a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Commands.c+++ b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Commands.c@@ -55,26 +55,58 @@ void report(const char *self_path) {(unsigned long)restricted,restricted ? "" : " (nothing would be allowed)");++ CFStringRef preference = copy_preference_requirement();+ if (preference) {+ CFDataRef wanted = copy_requirement_data(preference);+ CFDataRef held = restricted ? copy_amfid_requirement_data(restricted) : NULL;+ printf(+ " requirement in force %s\n",+ !held ? "unreadable"+ : wanted && CFEqual(held, wanted) ? "the preference's Entitlements"+ : "not the preference's Entitlements"+ );+ if (held) CFRelease(held);+ if (wanted) CFRelease(wanted);+ CFRelease(preference);+ }}// --------------------------------------------------------------------------+// Whether the SecRequirementRef at `req` in amfid is the requirement `wanted`.+static bool holds_requirement(uintptr_t req, CFDataRef wanted) {+ if (!req || !wanted) return false;+ CFDataRef held = copy_amfid_requirement_data((mach_vm_address_t)req);+ bool same = held && CFEqual(held, wanted);+ if (held) CFRelease(held);+ return same;+}+int cmd_allow(const char *self_path, int count, char **paths, int hold_seconds) {- if (update_allow_preferences(count, paths)) return 1;- if (!attach_amfid(self_path)) return 1;+ CFStringRef requirement = NULL;+ if (update_allow_preferences(count, paths, &requirement)) return 1;+ CFDataRef wanted = copy_requirement_data(requirement);+ CFRelease(requirement);+ int status = 1;+ if (!attach_amfid(self_path)) goto done;mach_vm_address_t mgr = amfid_manager(self_path);- if (!mgr) return 1;+ if (!mgr) goto done;// amfid is already holding the stock requirement, so "non-NULL" proves- // nothing. Adoption is the pointer changing: the preference path releases- // the old SecRequirementRef and stores a freshly built one.+ // nothing. The preference path releases the old SecRequirementRef and+ // stores a freshly built one, so the pointer usually changes. It does not+ // always: on one Mac amfid logged the reload and the new cdhash launched,+ // while the pointer stayed where it was (the new object can land in the+ // block the old one just freed). So the requirement it points at being the+ // one the preference asks for counts too.ptrdiff_t req_off = ivar_offset("_restrictedRequirement");uintptr_t before = 0;- if (!read_amfid(mgr + req_off, &before, sizeof(before))) return 1;+ if (!read_amfid(mgr + req_off, &before, sizeof(before))) goto done;printf("amfid pid %d: _restrictedRequirement was %#lx\n", g_amfid, (unsigned long)before);ptrdiff_t off = ivar_offset("_isRunningInternalBuild");- if (!write_amfid_byte(mgr + off, 1)) return 1;+ if (!write_amfid_byte(mgr + off, 1)) goto done;printf("amfid pid %d: %s+%#lx = 1\n", g_amfid, MANAGER_CLASS, (long)off);trigger_reload();@@ -90,16 +122,26 @@ int cmd_allow(const char *self_path, int count, char **paths, int hold_seconds));goto live;}+ if (holds_requirement(restricted, wanted)) {+ printf(+ "amfid holds the requirement (_restrictedRequirement %#lx is the one the "+ "preference asks for)\n",+ (unsigned long)restricted+ );+ goto live;+ }usleep(100 * 1000);}fprintf(stderr,"error: amfid did not adopt the requirement within 3s "- "(_restrictedRequirement never moved off %#lx)\n",- (unsigned long)before);- return 1;+ "(_restrictedRequirement never moved off %#lx%s)\n",+ (unsigned long)before,+ wanted ? "" : ", and the requirement could not be compiled to compare");+ goto done;live:- if (hold_seconds <= 0) return 0;+ status = 0;+ if (hold_seconds <= 0) goto done;// amfid has EnablePressuredExit, so it can be replaced under us; a new one// starts with the byte back at zero. Re-apply for as long as asked.@@ -110,12 +152,21 @@ live:printf("amfid restarted; re-applying\n");mach_port_deallocate(mach_task_self(), g_task);g_task = MACH_PORT_NULL;- if (!attach_amfid(self_path)) return 1;+ if (!attach_amfid(self_path)) {+ status = 1;+ goto done;+ }mgr = amfid_manager(self_path);- if (!mgr || !write_amfid_byte(mgr + off, 1)) return 1;+ if (!mgr || !write_amfid_byte(mgr + off, 1)) {+ status = 1;+ goto done;+ }trigger_reload();}- return 0;++done:+ if (wanted) CFRelease(wanted);+ return status;}int cmd_off(const char *self_path) {diff --git a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Escalator.h b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Escalator.hindex 1e7f4bf7..df808910 100644--- a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Escalator.h+++ b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Escalator.h@@ -38,6 +38,7 @@ int attach_amfid(const char *self_path);int read_amfid(mach_vm_address_t addr, void *buf, size_t len);int write_amfid_byte(mach_vm_address_t addr, uint8_t value);mach_vm_address_t amfid_manager(const char *self_path);+CFDataRef copy_amfid_requirement_data(mach_vm_address_t req);// MARK: - Requirements.c@@ -45,12 +46,14 @@ CFStringRef stock_requirement(void);CFMutableArrayRef cdhashes_for(int count, char **paths);CFStringRef requirement_with_hashes(CFStringRef base, CFArrayRef hashes);bool valid_hashes(CFArrayRef hashes);+CFDataRef copy_requirement_data(CFStringRef requirement);// MARK: - Preferences.cvoid trigger_reload(void);-int update_allow_preferences(int count, char **paths);+int update_allow_preferences(int count, char **paths, CFStringRef *requirement);int remove_allow_preferences(bool *changed);+CFStringRef copy_preference_requirement(void);// MARK: - Commands.cdiff --git a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Preferences.c b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Preferences.cindex 77070a50..2d4c6778 100644--- a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Preferences.c+++ b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Preferences.c@@ -100,7 +100,7 @@ void trigger_reload(void) { utimes(PREFS_PATH, NULL); }// ---------------------------------------------------------------------------int update_allow_preferences(int count, char **paths) {+int update_allow_preferences(int count, char **paths, CFStringRef *requirement) {bool existed = false;CFMutableDictionaryRef prefs = read_prefs(&existed);if (!prefs) return 1;@@ -192,7 +192,7 @@ int update_allow_preferences(int count, char **paths) {} else {printf("cdhash already allowed; left %s unchanged\n", PREFS_PATH);}- CFRelease(req);+ *requirement = req;CFRelease(hashes);if (!managed) CFRelease(base);CFRelease(prefs);@@ -247,3 +247,14 @@ int remove_allow_preferences(bool *changed) {*changed = true;return 1;}++// The Entitlements string amfid reads out of the preference, or NULL.+CFStringRef copy_preference_requirement(void) {+ bool existed = false;+ CFMutableDictionaryRef prefs = read_prefs(&existed);+ if (!prefs) return NULL;+ CFTypeRef value = CFDictionaryGetValue(prefs, CFSTR("Entitlements"));+ CFStringRef text = value && CFGetTypeID(value) == CFStringGetTypeID() ? CFRetain(value) : NULL;+ CFRelease(prefs);+ return text;+}diff --git a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Requirements.c b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Requirements.cindex b3e6660f..1058b289 100644--- a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Requirements.c+++ b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Requirements.c@@ -122,3 +122,15 @@ bool valid_hashes(CFArrayRef hashes) {}return true;}++// The blob of a requirement string: what amfid builds when the preference's+// Entitlements string replaces its _restrictedRequirement.+CFDataRef copy_requirement_data(CFStringRef requirement) {+ SecRequirementRef parsed = NULL;+ CFDataRef data = NULL;+ if (SecRequirementCreateWithString(requirement, kSecCSDefaultFlags, &parsed) != errSecSuccess)+ return NULL;+ if (SecRequirementCopyData(parsed, kSecCSDefaultFlags, &data) != errSecSuccess) data = NULL;+ CFRelease(parsed);+ return data;+}