Run a virtual iPhone on an Apple Silicon Mac.
Swift
Split vphone-escalator into files by concern
Lakr30082845ae35
diff --git a/AGENTS.md b/AGENTS.mdindex 0e1cebda..79936ad1 100644--- a/AGENTS.md+++ b/AGENTS.md@@ -110,8 +110,8 @@ There is none, and adding any is a regression.`Build/ValidateBundle.sh` cannot see.- There is no counter-example left. `amfidont` used to be cited as one — athird-party tool the user installed into their own Python — and it is gone- too: the AMFI allowlist tool is `VPhoneExecutable/VPhoneEscalator`, one C- file built by Xcode for arm64e, with no runtime interpreter or LLDB.+ too: the AMFI allowlist tool is `VPhoneExecutable/VPhoneEscalator`, a few C+ files built by Xcode for arm64e, with no runtime interpreter or LLDB.### Kernel patcher guardrailsdiff --git a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/AMFIDTask.c b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/AMFIDTask.cnew file mode 100644index 00000000..9683afc7--- /dev/null+++ b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/AMFIDTask.c@@ -0,0 +1,281 @@+// This process's view of AMFIRequirementsManager, and amfid's: attaching to+// amfid, reading and writing its memory, and locating its singleton.+#include "Escalator.h"++#include <dlfcn.h>+#include <libproc.h>+#include <objc/message.h>+#include <ptrauth.h>+#include <spawn.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/sysctl.h>+#include <sys/wait.h>+#include <unistd.h>++#define SCAN_INSNS 64 // +sharedManager is short; this is generous++mach_port_t g_task = MACH_PORT_NULL;+pid_t g_amfid = -1;++void *strip_pac(void *addr) {+#if defined(__arm64__)+ static uint32_t bits = 0;+ static int have_bits = 0;+ if (!have_bits) {+ size_t len = sizeof(bits);+ if (sysctlbyname("machdep.virtual_address_size", &bits, &len, NULL, 0) != 0) bits = -1;+ have_bits = 1;+ }+ return (void *)((uintptr_t)addr & ((1UL << bits) - 1));+#else+ return addr;+#endif+}++// --------------------------------------------------------------------------+// this process+// --------------------------------------------------------------------------++Class manager_class(void) {+ static Class cls = Nil;+ if (cls) return cls;+ if (!dlopen(AMFI_FRAMEWORK, RTLD_NOW)) {+ fprintf(stderr, "error: dlopen AppleMobileFileIntegrity: %s\n", dlerror());+ exit(1);+ }+ cls = objc_getClass(MANAGER_CLASS);+ if (!cls) {+ fprintf(+ stderr,+ "error: class %s not found — this macOS build is not supported\n",+ MANAGER_CLASS+ );+ exit(1);+ }+ return cls;+}++ptrdiff_t ivar_offset(const char *name) {+ Ivar iv = class_getInstanceVariable(manager_class(), name);+ if (!iv) {+ fprintf(+ stderr,+ "error: ivar %s not found on %s — this macOS build is not supported\n",+ name,+ MANAGER_CLASS+ );+ exit(1);+ }+ return ivar_getOffset(iv);+}++// Read from this process without faulting on an unmapped address.+static int peek_self(uintptr_t addr, void *buf, size_t len) {+ mach_vm_size_t got = 0;+ return mach_vm_read_overwrite(mach_task_self(), addr, len, (mach_vm_address_t)buf, &got) ==+ KERN_SUCCESS &&+ got == len;+}++// Decode +[AMFIRequirementsManager sharedManager] and return the static slot it+// loads the singleton out of. The slot is identified by its contents — the+// singleton this process just built — so a recompiled method that keeps the+// same shape still resolves, and one that does not is reported instead of+// guessed at.+mach_vm_address_t singleton_slot(void) {+ Class cls = manager_class();+ id local = ((id(*)(id, SEL))objc_msgSend)((id)cls, sel_registerName("sharedManager"));+ if (!local) {+ fprintf(stderr, "error: +[%s sharedManager] returned nil\n", MANAGER_CLASS);+ exit(1);+ }++ Method m = class_getClassMethod(cls, sel_registerName("sharedManager"));+ const uint32_t *code =+ ptrauth_strip((void *)method_getImplementation(m), ptrauth_key_function_pointer);++ uint64_t page[32];+ int have[32];+ memset(have, 0, sizeof(have));+ mach_vm_address_t found = 0;+ int matches = 0;++ for (int i = 0; i < SCAN_INSNS; i++) {+ uint32_t w = code[i];+ if ((w & 0x9F000000u) == 0x90000000u) { // ADRP Xd, #imm+ int rd = (int)(w & 0x1F);+ int64_t imm = (int64_t)((((w >> 5) & 0x7FFFF) << 2) | ((w >> 29) & 3));+ if (imm & (1 << 20)) imm -= (1 << 21); // sign extend 21 bits+ page[rd] = (((uint64_t)(uintptr_t)&code[i]) & ~0xFFFULL) + (uint64_t)(imm << 12);+ have[rd] = 1;+ } else if ((w & 0xFFC00000u) == 0xF9400000u) { // LDR Xt, [Xn, #imm12*8]+ int rn = (int)((w >> 5) & 0x1F);+ if (!have[rn]) continue;+ mach_vm_address_t slot = (mach_vm_address_t)(page[rn] + (((w >> 10) & 0xFFF) * 8));+ uintptr_t value = 0;+ if (peek_self((uintptr_t)slot, &value, sizeof(value)) && value == (uintptr_t)local) {+ if (found && found != slot) matches++;+ found = slot;+ matches++;+ }+ } else if (w == 0xD65F0FFFu || w == 0xD65F03C0u) { // retab / ret+ break;+ }+ }++ if (!found) {+ fprintf(stderr,+ "error: could not find the singleton slot in +[%s sharedManager] — "+ "this macOS build is not supported\n",+ MANAGER_CLASS);+ exit(1);+ }+ (void)matches; // several loads of the same slot are normal+ return found;+}++// --------------------------------------------------------------------------+// amfid+// --------------------------------------------------------------------------++pid_t find_amfid(void) {+ pid_t pids[8192];+ int n = proc_listpids(PROC_ALL_PIDS, 0, pids, (int)sizeof(pids)) / (int)sizeof(pid_t);+ char path[PROC_PIDPATHINFO_MAXSIZE];+ for (int i = 0; i < n; i++)+ if (pids[i] > 0 && proc_pidpath(pids[i], path, sizeof(path)) > 0 &&+ strcmp(path, AMFID_PATH) == 0)+ return pids[i];+ return -1;+}++// amfid is launch-on-demand. Validating this binary — ad-hoc signed, so the+// kernel has to ask — both starts it and makes it build the singleton.+static void poke_amfid(const char *self_path) {+ pid_t child = 0;+ char *const argv[] = {(char *)self_path, (char *)"--nop", NULL};+ if (posix_spawn(&child, self_path, NULL, NULL, argv, NULL) == 0) {+ int status = 0;+ waitpid(child, &status, 0);+ }+}++int attach_amfid(const char *self_path) {+ g_amfid = find_amfid();+ if (g_amfid < 0) {+ poke_amfid(self_path);+ g_amfid = find_amfid();+ }+ if (g_amfid < 0) {+ fprintf(stderr, "error: amfid is not running\n");+ return 0;+ }+ kern_return_t kr = task_for_pid(mach_task_self(), g_amfid, &g_task);+ if (kr != KERN_SUCCESS) {+ fprintf(+ stderr,+ "error: task_for_pid(%d): %s\n"+ " needs root and SIP debugging restrictions off "+ "(csrutil enable --without debug)\n",+ g_amfid,+ mach_error_string(kr)+ );+ return 0;+ }+ return 1;+}++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);+ if (kr != KERN_SUCCESS) {+ fprintf(+ stderr,+ "error: read %#llx from amfid: %s\n",+ (unsigned long long)addr,+ mach_error_string(kr)+ );+ return 0;+ }+ return got == len;+}++// One byte, into a malloc'd object. No protection change, no COW of a+// shared-cache page, nothing executable — which is the entire point.+int write_amfid_byte(mach_vm_address_t addr, uint8_t value) {+ uint8_t current = 0;+ if (!read_amfid(addr, ¤t, 1)) return 0;+ if (current == value) return 1; // never write what is already there+ kern_return_t kr = mach_vm_write(g_task, addr, (vm_offset_t)&value, 1);+ if (kr != KERN_SUCCESS) {+ fprintf(+ stderr,+ "error: write %#llx in amfid: %s\n",+ (unsigned long long)addr,+ mach_error_string(kr)+ );+ return 0;+ }+ uint8_t back = 0;+ if (!read_amfid(addr, &back, 1) || back != value) {+ fprintf(stderr, "error: write did not take (read back %u, wanted %u)\n", back, value);+ return 0;+ }+ return 1;+}++// The singleton, with enough checking that a wrong answer stops the tool.+mach_vm_address_t amfid_manager(const char *self_path) {+ mach_vm_address_t slot = singleton_slot();+ uintptr_t remote = 0;+ if (!read_amfid(slot, &remote, sizeof(remote))) return 0;++ if (!remote) { // amfid is up but has not validated anything yet+ poke_amfid(self_path);+ usleep(200 * 1000);+ if (!read_amfid(slot, &remote, sizeof(remote))) return 0;+ }+ if (!remote) {+ fprintf(stderr, "error: amfid has not created its %s yet\n", MANAGER_CLASS);+ return 0;+ }++ remote = (uintptr_t)strip_pac((void *)remote);++ // An ObjC object here, not a stale word: the class bits of its isa have to+ // match the class bits of ours.+ const uintptr_t kISAClassBits = 0x0000000FFFFFFFF8ULL;+ uintptr_t remote_isa = 0, local_isa = (uintptr_t)manager_class();+ if (!read_amfid((mach_vm_address_t)remote, &remote_isa, sizeof(remote_isa))) return 0;+ if ((remote_isa & kISAClassBits) != (local_isa & kISAClassBits)) {+ fprintf(+ stderr,+ "error: %#lx in amfid is not an %s (isa %#lx)\n",+ (unsigned long)remote,+ MANAGER_CLASS,+ (unsigned long)remote_isa+ );+ return 0;+ }++ // Both BOOL ivars must actually read as booleans.+ uint8_t flags[2] = {0, 0};+ if (!read_amfid((mach_vm_address_t)remote + ivar_offset("_allowUnsafeDynamicLinking"),+ &flags[0], 1) ||+ !read_amfid((mach_vm_address_t)remote + ivar_offset("_isRunningInternalBuild"), &flags[1],+ 1))+ return 0;+ if (flags[0] > 1 || flags[1] > 1) {+ fprintf(+ stderr,+ "error: %s ivars do not read as booleans (%u, %u)\n",+ MANAGER_CLASS,+ flags[0],+ flags[1]+ );+ return 0;+ }+ return (mach_vm_address_t)remote;+}diff --git a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Commands.c b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Commands.cnew file mode 100644index 00000000..2ef47adc--- /dev/null+++ b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Commands.c@@ -0,0 +1,136 @@+// The status, allow, and off verbs.+#include "Escalator.h"++#include <errno.h>+#include <signal.h>+#include <stdio.h>+#include <string.h>+#include <sys/sysctl.h>+#include <unistd.h>++// --------------------------------------------------------------------------+// reporting+// --------------------------------------------------------------------------++static int sysctl_int(const char *name) {+ int value = 0;+ size_t len = sizeof(value);+ return sysctlbyname(name, &value, &len, NULL, 0) == 0 ? value : -1;+}++void report(const char *self_path) {+ printf("host\n");+ printf(+ " vm.cs_system_enforcement %d%s\n",+ sysctl_int("vm.cs_system_enforcement"),+ sysctl_int("vm.cs_system_enforcement") == 1 ? " (code patching is fatal here)" : ""+ );+ printf(" preference file %s\n",+ access(PREFS_PATH, R_OK) == 0 ? PREFS_PATH : PREFS_PATH " (absent)");++ CFStringRef stock = stock_requirement();+ char buf[4096];+ if (CFStringGetCString(stock, buf, sizeof(buf), kCFStringEncodingUTF8))+ printf(" stock requirement %s\n", buf);+ CFRelease(stock);++ if (!attach_amfid(self_path)) return;+ printf("amfid\n");+ printf(" pid %d\n", g_amfid);+ printf(" singleton slot %#llx (shared cache, same in every process)\n",+ (unsigned long long)singleton_slot());++ mach_vm_address_t mgr = amfid_manager(self_path);+ if (!mgr) return;+ uint8_t internal = 0, unsafe_linking = 0;+ uintptr_t restricted = 0;+ read_amfid(mgr + ivar_offset("_isRunningInternalBuild"), &internal, 1);+ read_amfid(mgr + ivar_offset("_allowUnsafeDynamicLinking"), &unsafe_linking, 1);+ read_amfid(mgr + ivar_offset("_restrictedRequirement"), &restricted, sizeof(restricted));+ printf(" %s %#llx\n", MANAGER_CLASS, (unsigned long long)mgr);+ printf(" _isRunningInternalBuild %u%s\n", internal, internal ? " (preference honoured)" : "");+ printf(" _allowUnsafeDynamicLinking %u\n", unsafe_linking);+ printf(+ " _restrictedRequirement %#lx%s\n",+ (unsigned long)restricted,+ restricted ? "" : " (nothing would be allowed)"+ );+}++// --------------------------------------------------------------------------++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;+ mach_vm_address_t mgr = amfid_manager(self_path);+ if (!mgr) return 1;++ // 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.+ ptrdiff_t req_off = ivar_offset("_restrictedRequirement");+ uintptr_t before = 0;+ if (!read_amfid(mgr + req_off, &before, sizeof(before))) return 1;+ 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;+ printf("amfid pid %d: %s+%#lx = 1\n", g_amfid, MANAGER_CLASS, (long)off);++ trigger_reload();++ for (int i = 0; i < 30; i++) {+ uintptr_t restricted = 0;+ if (read_amfid(mgr + req_off, &restricted, sizeof(restricted)) && restricted &&+ restricted != before) {+ printf(+ "amfid adopted the requirement (_restrictedRequirement %#lx -> %#lx)\n",+ (unsigned long)before,+ (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;++live:+ if (hold_seconds <= 0) return 0;++ // 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.+ printf("holding for %ds (re-applying if amfid restarts); ^C to stop\n", hold_seconds);+ for (int elapsed = 0; elapsed < hold_seconds * 5; elapsed++) {+ usleep(200 * 1000);+ if (find_amfid() == g_amfid) continue;+ 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;+ mgr = amfid_manager(self_path);+ if (!mgr || !write_amfid_byte(mgr + off, 1)) return 1;+ trigger_reload();+ }+ return 0;+}++int cmd_off(const char *self_path) {+ bool changed = false;+ if (!remove_allow_preferences(&changed)) return 1;+ if (!changed) return 0;+ // The requirement amfid already built lives in its heap; the honest way to+ // drop it is to let launchd hand us a fresh amfid.+ pid_t pid = find_amfid();+ if (pid > 0) {+ if (kill(pid, SIGKILL) == 0)+ printf("killed amfid pid %d (launch-on-demand; it comes back clean)\n", pid);+ else+ fprintf(stderr, "error: kill %d: %s\n", pid, strerror(errno));+ }+ (void)self_path;+ return 0;+}diff --git a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Escalator.h b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Escalator.hnew file mode 100644index 00000000..1e7f4bf7--- /dev/null+++ b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Escalator.h@@ -0,0 +1,61 @@+// Shared declarations for vphone-escalator. See VPhoneEscalator.c for what+// the tool does and why.+#ifndef VPHONE_ESCALATOR_H+#define VPHONE_ESCALATOR_H++#include <CoreFoundation/CoreFoundation.h>+#include <mach/mach.h>+#include <mach/mach_vm.h>+#include <objc/runtime.h>+#include <stdbool.h>+#include <stddef.h>+#include <stdint.h>+#include <sys/types.h>++#define AMFID_PATH "/usr/libexec/amfid"+#define AMFI_FRAMEWORK \+ "/System/Library/PrivateFrameworks/AppleMobileFileIntegrity.framework/" \+ "AppleMobileFileIntegrity"+#define MANAGER_CLASS "AMFIRequirementsManager"+#ifndef PREFS_PATH+#define PREFS_PATH "/Library/Preferences/com.apple.security.coderequirements.plist"+#endif+// Persistent preference key; changing the executable name must not orphan+// hashes already written under this key.+#define MANAGED_KEY CFSTR("VPhoneEscalator")++extern mach_port_t g_task;+extern pid_t g_amfid;++// MARK: - AMFIDTask.c++void *strip_pac(void *addr);+Class manager_class(void);+ptrdiff_t ivar_offset(const char *name);+mach_vm_address_t singleton_slot(void);+pid_t find_amfid(void);+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);++// MARK: - Requirements.c++CFStringRef stock_requirement(void);+CFMutableArrayRef cdhashes_for(int count, char **paths);+CFStringRef requirement_with_hashes(CFStringRef base, CFArrayRef hashes);+bool valid_hashes(CFArrayRef hashes);++// MARK: - Preferences.c++void trigger_reload(void);+int update_allow_preferences(int count, char **paths);+int remove_allow_preferences(bool *changed);++// MARK: - Commands.c++void report(const char *self_path);+int cmd_allow(const char *self_path, int count, char **paths, int hold_seconds);+int cmd_off(const char *self_path);++#endifdiff --git a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Preferences.c b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Preferences.cnew file mode 100644index 00000000..77070a50--- /dev/null+++ b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Preferences.c@@ -0,0 +1,249 @@+// The coderequirements preference: reading, writing, reloading, and the+// vphone-escalator entries kept in it.+#include "Escalator.h"++#include <errno.h>+#include <fcntl.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>+#include <sys/stat.h>+#include <sys/time.h>+#include <unistd.h>++static CFMutableDictionaryRef read_prefs(bool *existed) {+ int fd = open(PREFS_PATH, O_RDONLY | O_NOFOLLOW);+ if (fd < 0 && errno == ENOENT) {+ *existed = false;+ return CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks,+ &kCFTypeDictionaryValueCallBacks);+ }+ if (fd < 0) {+ fprintf(stderr, "error: open %s: %s\n", PREFS_PATH, strerror(errno));+ return NULL;+ }+ *existed = true;+ struct stat st;+ if (fstat(fd, &st) || !S_ISREG(st.st_mode) || st.st_size < 0 || st.st_size > 1024 * 1024) {+ fprintf(stderr, "error: %s is not a regular plist under 1 MiB\n", PREFS_PATH);+ close(fd);+ return NULL;+ }+ UInt8 *bytes = malloc(st.st_size ? (size_t)st.st_size : 1);+ if (!bytes) { close(fd); return NULL; }+ size_t done = 0;+ while (done < (size_t)st.st_size) {+ ssize_t n = read(fd, bytes + done, (size_t)st.st_size - done);+ if (n <= 0) break;+ done += (size_t)n;+ }+ close(fd);+ if (done != (size_t)st.st_size) {+ fprintf(stderr, "error: could not read %s completely\n", PREFS_PATH);+ free(bytes);+ return NULL;+ }+ CFDataRef data = CFDataCreate(NULL, bytes, (CFIndex)done);+ free(bytes);+ CFPropertyListRef plist = CFPropertyListCreateWithData(NULL, data,+ kCFPropertyListMutableContainersAndLeaves, NULL, NULL);+ CFRelease(data);+ if (!plist || CFGetTypeID(plist) != CFDictionaryGetTypeID()) {+ fprintf(stderr, "error: %s is not a valid dictionary plist\n", PREFS_PATH);+ if (plist) CFRelease(plist);+ return NULL;+ }+ return (CFMutableDictionaryRef)plist;+}++static int write_prefs(CFDictionaryRef d) {+ CFDataRef data = CFPropertyListCreateData(NULL, d, kCFPropertyListXMLFormat_v1_0, 0, NULL);+ if (!data) return 0;++ char tmp[] = PREFS_PATH ".XXXXXX";+ int fd = mkstemp(tmp);+ if (fd < 0) {+ fprintf(stderr, "error: %s: %s\n", tmp, strerror(errno));+ CFRelease(data);+ return 0;+ }+ const UInt8 *bytes = CFDataGetBytePtr(data);+ size_t length = (size_t)CFDataGetLength(data), done = 0;+ while (done < length) {+ ssize_t n = write(fd, bytes + done, length - done);+ if (n <= 0) break;+ done += (size_t)n;+ }+ CFRelease(data);+ struct stat old;+ bool had_old = stat(PREFS_PATH, &old) == 0;+ int ok = done == length && fchmod(fd, had_old ? old.st_mode & 0777 : 0644) == 0 &&+ fchown(fd, had_old ? old.st_uid : geteuid(),+ had_old ? old.st_gid : getegid()) == 0 &&+ fsync(fd) == 0;+ if (close(fd) != 0) ok = 0;+ if (!ok) {+ fprintf(stderr, "error: could not write %s: %s\n", tmp, strerror(errno));+ unlink(tmp);+ return 0;+ }+ if (rename(tmp, PREFS_PATH) != 0) {+ fprintf(stderr, "error: rename %s: %s\n", PREFS_PATH, strerror(errno));+ unlink(tmp);+ return 0;+ }+ return 1;+}++// launchd watches this path and pokes amfid; touching it is the reload.+void trigger_reload(void) { utimes(PREFS_PATH, NULL); }++// --------------------------------------------------------------------------++int update_allow_preferences(int count, char **paths) {+ bool existed = false;+ CFMutableDictionaryRef prefs = read_prefs(&existed);+ if (!prefs) return 1;+ CFTypeRef current = CFDictionaryGetValue(prefs, CFSTR("Entitlements"));+ CFTypeRef unsafe = CFDictionaryGetValue(prefs, CFSTR("AllowUnsafeDynamicLinking"));+ CFDictionaryRef managed = CFDictionaryGetValue(prefs, MANAGED_KEY);+ if ((current && CFGetTypeID(current) != CFStringGetTypeID()) ||+ (managed && CFGetTypeID(managed) != CFDictionaryGetTypeID())) {+ fprintf(stderr, "error: %s has unexpected value types\n", PREFS_PATH);+ CFRelease(prefs);+ return 1;+ }+ CFStringRef base = NULL;+ CFMutableArrayRef hashes = NULL;+ bool had_entitlements = current != NULL;+ if (managed) {+ base = CFDictionaryGetValue(managed, CFSTR("Base"));+ CFArrayRef saved = CFDictionaryGetValue(managed, CFSTR("Hashes"));+ CFTypeRef original_entitlements = CFDictionaryGetValue(managed, CFSTR("HadEntitlements"));+ if (!base || CFGetTypeID(base) != CFStringGetTypeID() || !valid_hashes(saved) ||+ !original_entitlements ||+ CFGetTypeID(original_entitlements) != CFBooleanGetTypeID()) {+ fprintf(stderr, "error: invalid %s metadata in %s\n", "vphone-escalator", PREFS_PATH);+ CFRelease(prefs);+ return 1;+ }+ hashes = CFArrayCreateMutableCopy(NULL, 0, saved);+ CFStringRef expected = requirement_with_hashes(base, hashes);+ bool matches = current && CFEqual(current, expected);+ CFRelease(expected);+ if (!matches) {+ fprintf(stderr, "error: Entitlements changed since vphone-escalator wrote it\n");+ CFRelease(hashes);+ CFRelease(prefs);+ return 1;+ }+ had_entitlements = CFBooleanGetValue(original_entitlements);+ } else {+ base = current ? CFRetain(current) : stock_requirement();+ hashes = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);+ }++ CFMutableArrayRef incoming = cdhashes_for(count, paths);+ bool added = false;+ for (CFIndex i = 0; i < CFArrayGetCount(incoming); i++) {+ CFStringRef hash = CFArrayGetValueAtIndex(incoming, i);+ CFMutableStringRef clause = CFStringCreateMutable(NULL, 0);+ CFStringAppend(clause, CFSTR("cdhash H\""));+ CFStringAppend(clause, hash);+ CFStringAppend(clause, CFSTR("\""));+ bool already_present = CFStringFind(base, clause, kCFCompareCaseInsensitive).location !=+ kCFNotFound;+ CFRelease(clause);+ if (!already_present && !CFArrayContainsValue(hashes, CFRangeMake(0, CFArrayGetCount(hashes)), hash)) {+ CFArrayAppendValue(hashes, hash);+ added = true;+ }+ }+ CFRelease(incoming);+ CFStringRef req = requirement_with_hashes(base, hashes);+ char buf[4096];+ if (CFStringGetCString(req, buf, sizeof(buf), kCFStringEncodingUTF8))+ printf("requirement: %s\n", buf);+ else+ printf("requirement: %ld characters\n", CFStringGetLength(req));++ if (added) {+ CFMutableDictionaryRef tracking = CFDictionaryCreateMutable(NULL, 0,+ &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);+ CFDictionarySetValue(tracking, CFSTR("Base"), base);+ CFDictionarySetValue(tracking, CFSTR("Hashes"), hashes);+ CFDictionarySetValue(tracking, CFSTR("HadEntitlements"), had_entitlements ? kCFBooleanTrue : kCFBooleanFalse);+ CFDictionarySetValue(prefs, MANAGED_KEY, tracking);+ CFRelease(tracking);+ CFDictionarySetValue(prefs, CFSTR("Entitlements"), req);+ }+ // Entitlements without this key enables unsafe dynamic linking globally.+ // Leave any existing value untouched, including values set by another tool.+ if (!unsafe) CFDictionarySetValue(prefs, CFSTR("AllowUnsafeDynamicLinking"), kCFBooleanFalse);+ if (added || !unsafe) {+ if (!write_prefs(prefs)) {+ CFRelease(req);+ CFRelease(hashes);+ if (!managed) CFRelease(base);+ CFRelease(prefs);+ return 1;+ }+ printf("updated %s\n", PREFS_PATH);+ } else {+ printf("cdhash already allowed; left %s unchanged\n", PREFS_PATH);+ }+ CFRelease(req);+ CFRelease(hashes);+ if (!managed) CFRelease(base);+ CFRelease(prefs);+ return 0;+}++int remove_allow_preferences(bool *changed) {+ *changed = false;+ bool existed = false;+ CFMutableDictionaryRef prefs = read_prefs(&existed);+ if (!prefs) return 0;+ CFDictionaryRef managed = CFDictionaryGetValue(prefs, MANAGED_KEY);+ if (managed) {+ if (CFGetTypeID(managed) != CFDictionaryGetTypeID()) {+ fprintf(stderr, "error: invalid vphone-escalator metadata\n");+ CFRelease(prefs);+ return 0;+ }+ CFStringRef base = CFDictionaryGetValue(managed, CFSTR("Base"));+ CFArrayRef hashes = CFDictionaryGetValue(managed, CFSTR("Hashes"));+ CFTypeRef had_entitlements = CFDictionaryGetValue(managed, CFSTR("HadEntitlements"));+ if (!base || CFGetTypeID(base) != CFStringGetTypeID() || !valid_hashes(hashes) ||+ !had_entitlements ||+ CFGetTypeID(had_entitlements) != CFBooleanGetTypeID()) {+ fprintf(stderr, "error: invalid vphone-escalator metadata\n");+ CFRelease(prefs);+ return 0;+ }+ CFStringRef expected = requirement_with_hashes(base, hashes);+ CFTypeRef current = CFDictionaryGetValue(prefs, CFSTR("Entitlements"));+ bool matches = current && CFGetTypeID(current) == CFStringGetTypeID() &&+ CFEqual(current, expected);+ CFRelease(expected);+ if (!matches) {+ fprintf(stderr, "error: Entitlements changed since vphone-escalator wrote it; refusing to remove other rules\n");+ CFRelease(prefs);+ return 0;+ }+ if (CFBooleanGetValue(had_entitlements))+ CFDictionarySetValue(prefs, CFSTR("Entitlements"), base);+ else+ CFDictionaryRemoveValue(prefs, CFSTR("Entitlements"));+ CFDictionaryRemoveValue(prefs, MANAGED_KEY);+ if (!write_prefs(prefs)) { CFRelease(prefs); return 0; }+ printf("removed vphone-escalator cdhashes from %s\n", PREFS_PATH);+ } else {+ printf("no vphone-escalator entries to remove\n");+ CFRelease(prefs);+ return 1;+ }+ CFRelease(prefs);+ *changed = true;+ return 1;+}diff --git a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Requirements.c b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Requirements.cnew file mode 100644index 00000000..b3e6660f--- /dev/null+++ b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/Requirements.c@@ -0,0 +1,124 @@+// cdhash collection and the code requirement strings built from them.+#include "Escalator.h"++#include <Security/Security.h>+#include <objc/message.h>+#include <stdio.h>+#include <stdlib.h>+#include <string.h>++// --------------------------------------------------------------------------+// cdhashes and the preference+// --------------------------------------------------------------------------++// What amfid would use if this tool had never run. -init builds it through+// -resetRestrictedRequirement, so this process's own singleton is carrying it+// right now: ask that one rather than hardcoding a string that changes with+// the OS. Ours is then this, plus a cdhash — strictly more permissive, never+// less.+CFStringRef stock_requirement(void) {+ Class cls = manager_class();+ id mgr = ((id(*)(id, SEL))objc_msgSend)((id)cls, sel_registerName("sharedManager"));+ SecRequirementRef stock = ((SecRequirementRef(*)(id, SEL))objc_msgSend)(+ mgr,+ sel_registerName("restrictedRequirement")+ );+ CFStringRef text = NULL;+ if (!stock || SecRequirementCopyString(stock, kSecCSDefaultFlags, &text) != errSecSuccess ||+ !text) {+ fprintf(stderr, "error: cannot read the stock restricted requirement — "+ "this macOS build is not supported\n");+ exit(1);+ }+ return text;+}++CFMutableArrayRef cdhashes_for(int count, char **paths) {+ CFMutableArrayRef result = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);+ for (int i = 0; i < count; i++) {+ CFURLRef url = CFURLCreateFromFileSystemRepresentation(+ NULL,+ (const UInt8 *)paths[i],+ (CFIndex)strlen(paths[i]),+ false+ );+ SecStaticCodeRef code = NULL;+ OSStatus st = SecStaticCodeCreateWithPath(url, kSecCSDefaultFlags, &code);+ CFRelease(url);+ if (st != errSecSuccess) {+ fprintf(stderr, "error: %s: not signed code (%d)\n", paths[i], (int)st);+ exit(1);+ }++ // kSecCSSigningInformation is what fills in kSecCodeInfoCdHashes — every+ // code directory, so a universal binary gets each slice allowlisted+ // rather than only the one kSecCodeInfoUnique happens to name.+ CFDictionaryRef info = NULL;+ st = SecCodeCopySigningInformation(code, kSecCSSigningInformation, &info);+ CFRelease(code);+ if (st != errSecSuccess) {+ fprintf(+ stderr,+ "error: %s: cannot read signing information (%d)\n",+ paths[i],+ (int)st+ );+ exit(1);+ }++ CFArrayRef hashes = CFDictionaryGetValue(info, kSecCodeInfoCdHashes);+ CFDataRef unique = CFDictionaryGetValue(info, kSecCodeInfoUnique);+ CFIndex n = hashes ? CFArrayGetCount(hashes) : (unique ? 1 : 0);+ if (n == 0) {+ fprintf(stderr, "error: %s: no cdhash\n", paths[i]);+ exit(1);+ }+ for (CFIndex h = 0; h < n; h++) {+ CFDataRef d = hashes ? CFArrayGetValueAtIndex(hashes, h) : unique;+ CFMutableStringRef hash = CFStringCreateMutable(NULL, 0);+ const UInt8 *b = CFDataGetBytePtr(d);+ for (CFIndex k = 0; k < CFDataGetLength(d); k++)+ CFStringAppendFormat(hash, NULL, CFSTR("%02x"), b[k]);+ if (!CFArrayContainsValue(result, CFRangeMake(0, CFArrayGetCount(result)), hash))+ CFArrayAppendValue(result, hash);+ CFRelease(hash);+ }+ CFRelease(info);+ }+ return result;+}++CFStringRef requirement_with_hashes(CFStringRef base, CFArrayRef hashes) {+ CFMutableStringRef req = CFStringCreateMutableCopy(NULL, 0, base);+ for (CFIndex i = 0; i < CFArrayGetCount(hashes); i++) {+ CFStringAppend(req, CFSTR(" or cdhash H\""));+ CFStringAppend(req, CFArrayGetValueAtIndex(hashes, i));+ CFStringAppend(req, CFSTR("\""));+ }++ // It has to compile here, or amfid would silently keep the old one.+ SecRequirementRef parsed = NULL;+ OSStatus st = SecRequirementCreateWithString(req, kSecCSDefaultFlags, &parsed);+ if (st != errSecSuccess) {+ fprintf(stderr, "error: the requirement does not compile (%d)\n", (int)st);+ exit(1);+ }+ CFRelease(parsed);+ return req;+}++bool valid_hashes(CFArrayRef hashes) {+ if (!hashes || CFGetTypeID(hashes) != CFArrayGetTypeID()) return false;+ for (CFIndex i = 0; i < CFArrayGetCount(hashes); i++) {+ CFTypeRef value = CFArrayGetValueAtIndex(hashes, i);+ if (CFGetTypeID(value) != CFStringGetTypeID()) return false;+ CFStringRef hash = value;+ CFIndex length = CFStringGetLength(hash);+ if (length < 2 || length > 128 || length % 2) return false;+ for (CFIndex j = 0; j < length; j++) {+ UniChar c = CFStringGetCharacterAtIndex(hash, j);+ if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return false;+ }+ }+ return true;+}diff --git a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/VPhoneEscalator.c b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/VPhoneEscalator.cindex 58c6ad23..6deb5d56 100644--- a/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/VPhoneEscalator.c+++ b/VPhoneExecutable/VPhoneEscalator/VPhoneEscalator/VPhoneEscalator.c@@ -13,6 +13,16 @@// Build with the VPhoneEscalator Xcode target (arm64e, then ad-hoc signed// during bundle staging). No Python or LLDB is needed.//+// Source layout:+//+// VPhoneEscalator.c this comment and main()+// Escalator.h shared paths, keys, globals, and declarations+// AMFIDTask.c the manager class and singleton slot, attaching to+// amfid, and reading and writing its memory+// Requirements.c cdhashes and the requirement strings built from them+// Preferences.c the coderequirements preference and its entries+// Commands.c the status, allow, and off verbs+//// ---------------------------------------------------------------------------// Why this exists// ---------------------------------------------------------------------------@@ -82,783 +92,13 @@//// `off` removes only hashes this tool added, then restarts amfid. It preserves// the preference and every other value, including AllowUnsafeDynamicLinking.-#include <CoreFoundation/CoreFoundation.h>-#include <Security/Security.h>-#include <dlfcn.h>-#include <errno.h>-#include <fcntl.h>-#include <libproc.h>-#include <mach/mach.h>-#include <mach/mach_vm.h>-#include <objc/message.h>-#include <objc/runtime.h>-#include <ptrauth.h>-#include <signal.h>-#include <spawn.h>+#include "Escalator.h"+#include <stdio.h>#include <stdlib.h>#include <string.h>-#include <sys/stat.h>-#include <sys/sysctl.h>-#include <sys/time.h>#include <unistd.h>-#define AMFID_PATH "/usr/libexec/amfid"-#define AMFI_FRAMEWORK \- "/System/Library/PrivateFrameworks/AppleMobileFileIntegrity.framework/" \- "AppleMobileFileIntegrity"-#define MANAGER_CLASS "AMFIRequirementsManager"-#ifndef PREFS_PATH-#define PREFS_PATH "/Library/Preferences/com.apple.security.coderequirements.plist"-#endif-// Persistent preference key; changing the executable name must not orphan-// hashes already written under this key.-#define MANAGED_KEY CFSTR("VPhoneEscalator")--#define SCAN_INSNS 64 // +sharedManager is short; this is generous--static mach_port_t g_task = MACH_PORT_NULL;-static pid_t g_amfid = -1;--static void *strip_pac(void *addr) {-#if defined(__arm64__)- static uint32_t bits = 0;- static int have_bits = 0;- if (!have_bits) {- size_t len = sizeof(bits);- if (sysctlbyname("machdep.virtual_address_size", &bits, &len, NULL, 0) != 0) bits = -1;- have_bits = 1;- }- return (void *)((uintptr_t)addr & ((1UL << bits) - 1));-#else- return addr;-#endif-}--// ---------------------------------------------------------------------------// this process-// ----------------------------------------------------------------------------static Class manager_class(void) {- static Class cls = Nil;- if (cls) return cls;- if (!dlopen(AMFI_FRAMEWORK, RTLD_NOW)) {- fprintf(stderr, "error: dlopen AppleMobileFileIntegrity: %s\n", dlerror());- exit(1);- }- cls = objc_getClass(MANAGER_CLASS);- if (!cls) {- fprintf(- stderr,- "error: class %s not found — this macOS build is not supported\n",- MANAGER_CLASS- );- exit(1);- }- return cls;-}--static ptrdiff_t ivar_offset(const char *name) {- Ivar iv = class_getInstanceVariable(manager_class(), name);- if (!iv) {- fprintf(- stderr,- "error: ivar %s not found on %s — this macOS build is not supported\n",- name,- MANAGER_CLASS- );- exit(1);- }- return ivar_getOffset(iv);-}--// Read from this process without faulting on an unmapped address.-static int peek_self(uintptr_t addr, void *buf, size_t len) {- mach_vm_size_t got = 0;- return mach_vm_read_overwrite(mach_task_self(), addr, len, (mach_vm_address_t)buf, &got) ==- KERN_SUCCESS &&- got == len;-}--// Decode +[AMFIRequirementsManager sharedManager] and return the static slot it-// loads the singleton out of. The slot is identified by its contents — the-// singleton this process just built — so a recompiled method that keeps the-// same shape still resolves, and one that does not is reported instead of-// guessed at.-static mach_vm_address_t singleton_slot(void) {- Class cls = manager_class();- id local = ((id(*)(id, SEL))objc_msgSend)((id)cls, sel_registerName("sharedManager"));- if (!local) {- fprintf(stderr, "error: +[%s sharedManager] returned nil\n", MANAGER_CLASS);- exit(1);- }-- Method m = class_getClassMethod(cls, sel_registerName("sharedManager"));- const uint32_t *code =- ptrauth_strip((void *)method_getImplementation(m), ptrauth_key_function_pointer);-- uint64_t page[32];- int have[32];- memset(have, 0, sizeof(have));- mach_vm_address_t found = 0;- int matches = 0;-- for (int i = 0; i < SCAN_INSNS; i++) {- uint32_t w = code[i];- if ((w & 0x9F000000u) == 0x90000000u) { // ADRP Xd, #imm- int rd = (int)(w & 0x1F);- int64_t imm = (int64_t)((((w >> 5) & 0x7FFFF) << 2) | ((w >> 29) & 3));- if (imm & (1 << 20)) imm -= (1 << 21); // sign extend 21 bits- page[rd] = (((uint64_t)(uintptr_t)&code[i]) & ~0xFFFULL) + (uint64_t)(imm << 12);- have[rd] = 1;- } else if ((w & 0xFFC00000u) == 0xF9400000u) { // LDR Xt, [Xn, #imm12*8]- int rn = (int)((w >> 5) & 0x1F);- if (!have[rn]) continue;- mach_vm_address_t slot = (mach_vm_address_t)(page[rn] + (((w >> 10) & 0xFFF) * 8));- uintptr_t value = 0;- if (peek_self((uintptr_t)slot, &value, sizeof(value)) && value == (uintptr_t)local) {- if (found && found != slot) matches++;- found = slot;- matches++;- }- } else if (w == 0xD65F0FFFu || w == 0xD65F03C0u) { // retab / ret- break;- }- }-- if (!found) {- fprintf(stderr,- "error: could not find the singleton slot in +[%s sharedManager] — "- "this macOS build is not supported\n",- MANAGER_CLASS);- exit(1);- }- (void)matches; // several loads of the same slot are normal- return found;-}--// ---------------------------------------------------------------------------// amfid-// ----------------------------------------------------------------------------static pid_t find_amfid(void) {- pid_t pids[8192];- int n = proc_listpids(PROC_ALL_PIDS, 0, pids, (int)sizeof(pids)) / (int)sizeof(pid_t);- char path[PROC_PIDPATHINFO_MAXSIZE];- for (int i = 0; i < n; i++)- if (pids[i] > 0 && proc_pidpath(pids[i], path, sizeof(path)) > 0 &&- strcmp(path, AMFID_PATH) == 0)- return pids[i];- return -1;-}--// amfid is launch-on-demand. Validating this binary — ad-hoc signed, so the-// kernel has to ask — both starts it and makes it build the singleton.-static void poke_amfid(const char *self_path) {- pid_t child = 0;- char *const argv[] = {(char *)self_path, (char *)"--nop", NULL};- if (posix_spawn(&child, self_path, NULL, NULL, argv, NULL) == 0) {- int status = 0;- waitpid(child, &status, 0);- }-}--static int attach_amfid(const char *self_path) {- g_amfid = find_amfid();- if (g_amfid < 0) {- poke_amfid(self_path);- g_amfid = find_amfid();- }- if (g_amfid < 0) {- fprintf(stderr, "error: amfid is not running\n");- return 0;- }- kern_return_t kr = task_for_pid(mach_task_self(), g_amfid, &g_task);- if (kr != KERN_SUCCESS) {- fprintf(- stderr,- "error: task_for_pid(%d): %s\n"- " needs root and SIP debugging restrictions off "- "(csrutil enable --without debug)\n",- g_amfid,- mach_error_string(kr)- );- return 0;- }- return 1;-}--static 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);- if (kr != KERN_SUCCESS) {- fprintf(- stderr,- "error: read %#llx from amfid: %s\n",- (unsigned long long)addr,- mach_error_string(kr)- );- return 0;- }- return got == len;-}--// One byte, into a malloc'd object. No protection change, no COW of a-// shared-cache page, nothing executable — which is the entire point.-static int write_amfid_byte(mach_vm_address_t addr, uint8_t value) {- uint8_t current = 0;- if (!read_amfid(addr, ¤t, 1)) return 0;- if (current == value) return 1; // never write what is already there- kern_return_t kr = mach_vm_write(g_task, addr, (vm_offset_t)&value, 1);- if (kr != KERN_SUCCESS) {- fprintf(- stderr,- "error: write %#llx in amfid: %s\n",- (unsigned long long)addr,- mach_error_string(kr)- );- return 0;- }- uint8_t back = 0;- if (!read_amfid(addr, &back, 1) || back != value) {- fprintf(stderr, "error: write did not take (read back %u, wanted %u)\n", back, value);- return 0;- }- return 1;-}--// The singleton, with enough checking that a wrong answer stops the tool.-static mach_vm_address_t amfid_manager(const char *self_path) {- mach_vm_address_t slot = singleton_slot();- uintptr_t remote = 0;- if (!read_amfid(slot, &remote, sizeof(remote))) return 0;-- if (!remote) { // amfid is up but has not validated anything yet- poke_amfid(self_path);- usleep(200 * 1000);- if (!read_amfid(slot, &remote, sizeof(remote))) return 0;- }- if (!remote) {- fprintf(stderr, "error: amfid has not created its %s yet\n", MANAGER_CLASS);- return 0;- }-- remote = (uintptr_t)strip_pac((void *)remote);-- // An ObjC object here, not a stale word: the class bits of its isa have to- // match the class bits of ours.- const uintptr_t kISAClassBits = 0x0000000FFFFFFFF8ULL;- uintptr_t remote_isa = 0, local_isa = (uintptr_t)manager_class();- if (!read_amfid((mach_vm_address_t)remote, &remote_isa, sizeof(remote_isa))) return 0;- if ((remote_isa & kISAClassBits) != (local_isa & kISAClassBits)) {- fprintf(- stderr,- "error: %#lx in amfid is not an %s (isa %#lx)\n",- (unsigned long)remote,- MANAGER_CLASS,- (unsigned long)remote_isa- );- return 0;- }-- // Both BOOL ivars must actually read as booleans.- uint8_t flags[2] = {0, 0};- if (!read_amfid((mach_vm_address_t)remote + ivar_offset("_allowUnsafeDynamicLinking"),- &flags[0], 1) ||- !read_amfid((mach_vm_address_t)remote + ivar_offset("_isRunningInternalBuild"), &flags[1],- 1))- return 0;- if (flags[0] > 1 || flags[1] > 1) {- fprintf(- stderr,- "error: %s ivars do not read as booleans (%u, %u)\n",- MANAGER_CLASS,- flags[0],- flags[1]- );- return 0;- }- return (mach_vm_address_t)remote;-}--// ---------------------------------------------------------------------------// cdhashes and the preference-// ----------------------------------------------------------------------------// What amfid would use if this tool had never run. -init builds it through-// -resetRestrictedRequirement, so this process's own singleton is carrying it-// right now: ask that one rather than hardcoding a string that changes with-// the OS. Ours is then this, plus a cdhash — strictly more permissive, never-// less.-static CFStringRef stock_requirement(void) {- Class cls = manager_class();- id mgr = ((id(*)(id, SEL))objc_msgSend)((id)cls, sel_registerName("sharedManager"));- SecRequirementRef stock = ((SecRequirementRef(*)(id, SEL))objc_msgSend)(- mgr,- sel_registerName("restrictedRequirement")- );- CFStringRef text = NULL;- if (!stock || SecRequirementCopyString(stock, kSecCSDefaultFlags, &text) != errSecSuccess ||- !text) {- fprintf(stderr, "error: cannot read the stock restricted requirement — "- "this macOS build is not supported\n");- exit(1);- }- return text;-}--static CFMutableArrayRef cdhashes_for(int count, char **paths) {- CFMutableArrayRef result = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);- for (int i = 0; i < count; i++) {- CFURLRef url = CFURLCreateFromFileSystemRepresentation(- NULL,- (const UInt8 *)paths[i],- (CFIndex)strlen(paths[i]),- false- );- SecStaticCodeRef code = NULL;- OSStatus st = SecStaticCodeCreateWithPath(url, kSecCSDefaultFlags, &code);- CFRelease(url);- if (st != errSecSuccess) {- fprintf(stderr, "error: %s: not signed code (%d)\n", paths[i], (int)st);- exit(1);- }-- // kSecCSSigningInformation is what fills in kSecCodeInfoCdHashes — every- // code directory, so a universal binary gets each slice allowlisted- // rather than only the one kSecCodeInfoUnique happens to name.- CFDictionaryRef info = NULL;- st = SecCodeCopySigningInformation(code, kSecCSSigningInformation, &info);- CFRelease(code);- if (st != errSecSuccess) {- fprintf(- stderr,- "error: %s: cannot read signing information (%d)\n",- paths[i],- (int)st- );- exit(1);- }-- CFArrayRef hashes = CFDictionaryGetValue(info, kSecCodeInfoCdHashes);- CFDataRef unique = CFDictionaryGetValue(info, kSecCodeInfoUnique);- CFIndex n = hashes ? CFArrayGetCount(hashes) : (unique ? 1 : 0);- if (n == 0) {- fprintf(stderr, "error: %s: no cdhash\n", paths[i]);- exit(1);- }- for (CFIndex h = 0; h < n; h++) {- CFDataRef d = hashes ? CFArrayGetValueAtIndex(hashes, h) : unique;- CFMutableStringRef hash = CFStringCreateMutable(NULL, 0);- const UInt8 *b = CFDataGetBytePtr(d);- for (CFIndex k = 0; k < CFDataGetLength(d); k++)- CFStringAppendFormat(hash, NULL, CFSTR("%02x"), b[k]);- if (!CFArrayContainsValue(result, CFRangeMake(0, CFArrayGetCount(result)), hash))- CFArrayAppendValue(result, hash);- CFRelease(hash);- }- CFRelease(info);- }- return result;-}--static CFStringRef requirement_with_hashes(CFStringRef base, CFArrayRef hashes) {- CFMutableStringRef req = CFStringCreateMutableCopy(NULL, 0, base);- for (CFIndex i = 0; i < CFArrayGetCount(hashes); i++) {- CFStringAppend(req, CFSTR(" or cdhash H\""));- CFStringAppend(req, CFArrayGetValueAtIndex(hashes, i));- CFStringAppend(req, CFSTR("\""));- }-- // It has to compile here, or amfid would silently keep the old one.- SecRequirementRef parsed = NULL;- OSStatus st = SecRequirementCreateWithString(req, kSecCSDefaultFlags, &parsed);- if (st != errSecSuccess) {- fprintf(stderr, "error: the requirement does not compile (%d)\n", (int)st);- exit(1);- }- CFRelease(parsed);- return req;-}--static bool valid_hashes(CFArrayRef hashes) {- if (!hashes || CFGetTypeID(hashes) != CFArrayGetTypeID()) return false;- for (CFIndex i = 0; i < CFArrayGetCount(hashes); i++) {- CFTypeRef value = CFArrayGetValueAtIndex(hashes, i);- if (CFGetTypeID(value) != CFStringGetTypeID()) return false;- CFStringRef hash = value;- CFIndex length = CFStringGetLength(hash);- if (length < 2 || length > 128 || length % 2) return false;- for (CFIndex j = 0; j < length; j++) {- UniChar c = CFStringGetCharacterAtIndex(hash, j);- if (!((c >= '0' && c <= '9') || (c >= 'a' && c <= 'f'))) return false;- }- }- return true;-}--static CFMutableDictionaryRef read_prefs(bool *existed) {- int fd = open(PREFS_PATH, O_RDONLY | O_NOFOLLOW);- if (fd < 0 && errno == ENOENT) {- *existed = false;- return CFDictionaryCreateMutable(NULL, 0, &kCFTypeDictionaryKeyCallBacks,- &kCFTypeDictionaryValueCallBacks);- }- if (fd < 0) {- fprintf(stderr, "error: open %s: %s\n", PREFS_PATH, strerror(errno));- return NULL;- }- *existed = true;- struct stat st;- if (fstat(fd, &st) || !S_ISREG(st.st_mode) || st.st_size < 0 || st.st_size > 1024 * 1024) {- fprintf(stderr, "error: %s is not a regular plist under 1 MiB\n", PREFS_PATH);- close(fd);- return NULL;- }- UInt8 *bytes = malloc(st.st_size ? (size_t)st.st_size : 1);- if (!bytes) { close(fd); return NULL; }- size_t done = 0;- while (done < (size_t)st.st_size) {- ssize_t n = read(fd, bytes + done, (size_t)st.st_size - done);- if (n <= 0) break;- done += (size_t)n;- }- close(fd);- if (done != (size_t)st.st_size) {- fprintf(stderr, "error: could not read %s completely\n", PREFS_PATH);- free(bytes);- return NULL;- }- CFDataRef data = CFDataCreate(NULL, bytes, (CFIndex)done);- free(bytes);- CFPropertyListRef plist = CFPropertyListCreateWithData(NULL, data,- kCFPropertyListMutableContainersAndLeaves, NULL, NULL);- CFRelease(data);- if (!plist || CFGetTypeID(plist) != CFDictionaryGetTypeID()) {- fprintf(stderr, "error: %s is not a valid dictionary plist\n", PREFS_PATH);- if (plist) CFRelease(plist);- return NULL;- }- return (CFMutableDictionaryRef)plist;-}--static int write_prefs(CFDictionaryRef d) {- CFDataRef data = CFPropertyListCreateData(NULL, d, kCFPropertyListXMLFormat_v1_0, 0, NULL);- if (!data) return 0;-- char tmp[] = PREFS_PATH ".XXXXXX";- int fd = mkstemp(tmp);- if (fd < 0) {- fprintf(stderr, "error: %s: %s\n", tmp, strerror(errno));- CFRelease(data);- return 0;- }- const UInt8 *bytes = CFDataGetBytePtr(data);- size_t length = (size_t)CFDataGetLength(data), done = 0;- while (done < length) {- ssize_t n = write(fd, bytes + done, length - done);- if (n <= 0) break;- done += (size_t)n;- }- CFRelease(data);- struct stat old;- bool had_old = stat(PREFS_PATH, &old) == 0;- int ok = done == length && fchmod(fd, had_old ? old.st_mode & 0777 : 0644) == 0 &&- fchown(fd, had_old ? old.st_uid : geteuid(),- had_old ? old.st_gid : getegid()) == 0 &&- fsync(fd) == 0;- if (close(fd) != 0) ok = 0;- if (!ok) {- fprintf(stderr, "error: could not write %s: %s\n", tmp, strerror(errno));- unlink(tmp);- return 0;- }- if (rename(tmp, PREFS_PATH) != 0) {- fprintf(stderr, "error: rename %s: %s\n", PREFS_PATH, strerror(errno));- unlink(tmp);- return 0;- }- return 1;-}--// launchd watches this path and pokes amfid; touching it is the reload.-static void trigger_reload(void) { utimes(PREFS_PATH, NULL); }--// ---------------------------------------------------------------------------// reporting-// ----------------------------------------------------------------------------static int sysctl_int(const char *name) {- int value = 0;- size_t len = sizeof(value);- return sysctlbyname(name, &value, &len, NULL, 0) == 0 ? value : -1;-}--static void report(const char *self_path) {- printf("host\n");- printf(- " vm.cs_system_enforcement %d%s\n",- sysctl_int("vm.cs_system_enforcement"),- sysctl_int("vm.cs_system_enforcement") == 1 ? " (code patching is fatal here)" : ""- );- printf(" preference file %s\n",- access(PREFS_PATH, R_OK) == 0 ? PREFS_PATH : PREFS_PATH " (absent)");-- CFStringRef stock = stock_requirement();- char buf[4096];- if (CFStringGetCString(stock, buf, sizeof(buf), kCFStringEncodingUTF8))- printf(" stock requirement %s\n", buf);- CFRelease(stock);-- if (!attach_amfid(self_path)) return;- printf("amfid\n");- printf(" pid %d\n", g_amfid);- printf(" singleton slot %#llx (shared cache, same in every process)\n",- (unsigned long long)singleton_slot());-- mach_vm_address_t mgr = amfid_manager(self_path);- if (!mgr) return;- uint8_t internal = 0, unsafe_linking = 0;- uintptr_t restricted = 0;- read_amfid(mgr + ivar_offset("_isRunningInternalBuild"), &internal, 1);- read_amfid(mgr + ivar_offset("_allowUnsafeDynamicLinking"), &unsafe_linking, 1);- read_amfid(mgr + ivar_offset("_restrictedRequirement"), &restricted, sizeof(restricted));- printf(" %s %#llx\n", MANAGER_CLASS, (unsigned long long)mgr);- printf(" _isRunningInternalBuild %u%s\n", internal, internal ? " (preference honoured)" : "");- printf(" _allowUnsafeDynamicLinking %u\n", unsafe_linking);- printf(- " _restrictedRequirement %#lx%s\n",- (unsigned long)restricted,- restricted ? "" : " (nothing would be allowed)"- );-}--// ----------------------------------------------------------------------------static int update_allow_preferences(int count, char **paths) {- bool existed = false;- CFMutableDictionaryRef prefs = read_prefs(&existed);- if (!prefs) return 1;- CFTypeRef current = CFDictionaryGetValue(prefs, CFSTR("Entitlements"));- CFTypeRef unsafe = CFDictionaryGetValue(prefs, CFSTR("AllowUnsafeDynamicLinking"));- CFDictionaryRef managed = CFDictionaryGetValue(prefs, MANAGED_KEY);- if ((current && CFGetTypeID(current) != CFStringGetTypeID()) ||- (managed && CFGetTypeID(managed) != CFDictionaryGetTypeID())) {- fprintf(stderr, "error: %s has unexpected value types\n", PREFS_PATH);- CFRelease(prefs);- return 1;- }- CFStringRef base = NULL;- CFMutableArrayRef hashes = NULL;- bool had_entitlements = current != NULL;- if (managed) {- base = CFDictionaryGetValue(managed, CFSTR("Base"));- CFArrayRef saved = CFDictionaryGetValue(managed, CFSTR("Hashes"));- CFTypeRef original_entitlements = CFDictionaryGetValue(managed, CFSTR("HadEntitlements"));- if (!base || CFGetTypeID(base) != CFStringGetTypeID() || !valid_hashes(saved) ||- !original_entitlements ||- CFGetTypeID(original_entitlements) != CFBooleanGetTypeID()) {- fprintf(stderr, "error: invalid %s metadata in %s\n", "vphone-escalator", PREFS_PATH);- CFRelease(prefs);- return 1;- }- hashes = CFArrayCreateMutableCopy(NULL, 0, saved);- CFStringRef expected = requirement_with_hashes(base, hashes);- bool matches = current && CFEqual(current, expected);- CFRelease(expected);- if (!matches) {- fprintf(stderr, "error: Entitlements changed since vphone-escalator wrote it\n");- CFRelease(hashes);- CFRelease(prefs);- return 1;- }- had_entitlements = CFBooleanGetValue(original_entitlements);- } else {- base = current ? CFRetain(current) : stock_requirement();- hashes = CFArrayCreateMutable(NULL, 0, &kCFTypeArrayCallBacks);- }-- CFMutableArrayRef incoming = cdhashes_for(count, paths);- bool added = false;- for (CFIndex i = 0; i < CFArrayGetCount(incoming); i++) {- CFStringRef hash = CFArrayGetValueAtIndex(incoming, i);- CFMutableStringRef clause = CFStringCreateMutable(NULL, 0);- CFStringAppend(clause, CFSTR("cdhash H\""));- CFStringAppend(clause, hash);- CFStringAppend(clause, CFSTR("\""));- bool already_present = CFStringFind(base, clause, kCFCompareCaseInsensitive).location !=- kCFNotFound;- CFRelease(clause);- if (!already_present && !CFArrayContainsValue(hashes, CFRangeMake(0, CFArrayGetCount(hashes)), hash)) {- CFArrayAppendValue(hashes, hash);- added = true;- }- }- CFRelease(incoming);- CFStringRef req = requirement_with_hashes(base, hashes);- char buf[4096];- if (CFStringGetCString(req, buf, sizeof(buf), kCFStringEncodingUTF8))- printf("requirement: %s\n", buf);- else- printf("requirement: %ld characters\n", CFStringGetLength(req));-- if (added) {- CFMutableDictionaryRef tracking = CFDictionaryCreateMutable(NULL, 0,- &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);- CFDictionarySetValue(tracking, CFSTR("Base"), base);- CFDictionarySetValue(tracking, CFSTR("Hashes"), hashes);- CFDictionarySetValue(tracking, CFSTR("HadEntitlements"), had_entitlements ? kCFBooleanTrue : kCFBooleanFalse);- CFDictionarySetValue(prefs, MANAGED_KEY, tracking);- CFRelease(tracking);- CFDictionarySetValue(prefs, CFSTR("Entitlements"), req);- }- // Entitlements without this key enables unsafe dynamic linking globally.- // Leave any existing value untouched, including values set by another tool.- if (!unsafe) CFDictionarySetValue(prefs, CFSTR("AllowUnsafeDynamicLinking"), kCFBooleanFalse);- if (added || !unsafe) {- if (!write_prefs(prefs)) {- CFRelease(req);- CFRelease(hashes);- if (!managed) CFRelease(base);- CFRelease(prefs);- return 1;- }- printf("updated %s\n", PREFS_PATH);- } else {- printf("cdhash already allowed; left %s unchanged\n", PREFS_PATH);- }- CFRelease(req);- CFRelease(hashes);- if (!managed) CFRelease(base);- CFRelease(prefs);- return 0;-}--static 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;- mach_vm_address_t mgr = amfid_manager(self_path);- if (!mgr) return 1;-- // 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.- ptrdiff_t req_off = ivar_offset("_restrictedRequirement");- uintptr_t before = 0;- if (!read_amfid(mgr + req_off, &before, sizeof(before))) return 1;- 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;- printf("amfid pid %d: %s+%#lx = 1\n", g_amfid, MANAGER_CLASS, (long)off);-- trigger_reload();-- for (int i = 0; i < 30; i++) {- uintptr_t restricted = 0;- if (read_amfid(mgr + req_off, &restricted, sizeof(restricted)) && restricted &&- restricted != before) {- printf(- "amfid adopted the requirement (_restrictedRequirement %#lx -> %#lx)\n",- (unsigned long)before,- (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;--live:- if (hold_seconds <= 0) return 0;-- // 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.- printf("holding for %ds (re-applying if amfid restarts); ^C to stop\n", hold_seconds);- for (int elapsed = 0; elapsed < hold_seconds * 5; elapsed++) {- usleep(200 * 1000);- if (find_amfid() == g_amfid) continue;- 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;- mgr = amfid_manager(self_path);- if (!mgr || !write_amfid_byte(mgr + off, 1)) return 1;- trigger_reload();- }- return 0;-}--static int remove_allow_preferences(bool *changed) {- *changed = false;- bool existed = false;- CFMutableDictionaryRef prefs = read_prefs(&existed);- if (!prefs) return 0;- CFDictionaryRef managed = CFDictionaryGetValue(prefs, MANAGED_KEY);- if (managed) {- if (CFGetTypeID(managed) != CFDictionaryGetTypeID()) {- fprintf(stderr, "error: invalid vphone-escalator metadata\n");- CFRelease(prefs);- return 0;- }- CFStringRef base = CFDictionaryGetValue(managed, CFSTR("Base"));- CFArrayRef hashes = CFDictionaryGetValue(managed, CFSTR("Hashes"));- CFTypeRef had_entitlements = CFDictionaryGetValue(managed, CFSTR("HadEntitlements"));- if (!base || CFGetTypeID(base) != CFStringGetTypeID() || !valid_hashes(hashes) ||- !had_entitlements ||- CFGetTypeID(had_entitlements) != CFBooleanGetTypeID()) {- fprintf(stderr, "error: invalid vphone-escalator metadata\n");- CFRelease(prefs);- return 0;- }- CFStringRef expected = requirement_with_hashes(base, hashes);- CFTypeRef current = CFDictionaryGetValue(prefs, CFSTR("Entitlements"));- bool matches = current && CFGetTypeID(current) == CFStringGetTypeID() &&- CFEqual(current, expected);- CFRelease(expected);- if (!matches) {- fprintf(stderr, "error: Entitlements changed since vphone-escalator wrote it; refusing to remove other rules\n");- CFRelease(prefs);- return 0;- }- if (CFBooleanGetValue(had_entitlements))- CFDictionarySetValue(prefs, CFSTR("Entitlements"), base);- else- CFDictionaryRemoveValue(prefs, CFSTR("Entitlements"));- CFDictionaryRemoveValue(prefs, MANAGED_KEY);- if (!write_prefs(prefs)) { CFRelease(prefs); return 0; }- printf("removed vphone-escalator cdhashes from %s\n", PREFS_PATH);- } else {- printf("no vphone-escalator entries to remove\n");- CFRelease(prefs);- return 1;- }- CFRelease(prefs);- *changed = true;- return 1;-}--static int cmd_off(const char *self_path) {- bool changed = false;- if (!remove_allow_preferences(&changed)) return 1;- if (!changed) return 0;- // The requirement amfid already built lives in its heap; the honest way to- // drop it is to let launchd hand us a fresh amfid.- pid_t pid = find_amfid();- if (pid > 0) {- if (kill(pid, SIGKILL) == 0)- printf("killed amfid pid %d (launch-on-demand; it comes back clean)\n", pid);- else- fprintf(stderr, "error: kill %d: %s\n", pid, strerror(errno));- }- (void)self_path;- return 0;-}-int main(int argc, char **argv) {if (argc > 1 && strcmp(argv[1], "--nop") == 0) return 0; // the amfid poke