Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Insert libmisfix from SystemHook, chosen by the spawned process's path

Lakrd44a0e9423f7
diff --git a/VPhoneGuestComponents/Shared/InjectionEnvironment.h b/VPhoneGuestComponents/Shared/InjectionEnvironment.h
index 506586d2..7a0a19f4 100644
--- a/VPhoneGuestComponents/Shared/InjectionEnvironment.h
+++ b/VPhoneGuestComponents/Shared/InjectionEnvironment.h
@@ -7,6 +7,23 @@
#define VP_SYSTEM_HOOK "/usr/lib/SystemHook-vphone.dylib"
+// The MIS hook. Inserted only into the processes that evaluate a code
+// signature or a provisioning profile — see `vpIsMISFixTarget` in
+// SystemHook-vphone.c — rather than carried by every spawn.
+//
+// It is inserted rather than linked, and that distinction is the point.
+// `cfw install` used to give installd and misagent an LC_LOAD_WEAK_DYLIB, which
+// makes the hook a dependency of the main executable. That is enough to
+// interpose calls the main executable makes itself, which is why misagent's
+// UDID override worked, and it is *not* enough for a call made between two
+// shared-cache images: installd's profile check is
+// `+[MICodeSigningVerifier _validateSignatureAndCopyInfoForURL:withOptions:error:]`
+// in MobileInstallation calling libmis, with installd's own image not involved,
+// and that one kept seeing the guest's real UDID. DYLD_INSERT_LIBRARIES loads
+// the hook ahead of everything else, which is where an interpose covers the
+// cache's own uses of a symbol too.
+#define VP_MIS_FIX "/usr/lib/libmisfix.dylib"
+
typedef struct {
char **values;
char *hook;
@@ -33,14 +50,15 @@ static int vpInjectionDisabled(char *const env[]) {
return vpEnvIsOne(env, "DISABLE_TWEAKS") || vpEnvIsOne(env, "_SafeMode") || vpEnvIsOne(env, "_MSSafeMode");
}
-static int vpHasHook(const char *paths) {
- if (!paths)
+// Whether `library` is already one of the colon-separated entries in `paths`.
+static int vpListHasPath(const char *paths, const char *library) {
+ if (!paths || !library)
return 0;
- const size_t length = strlen(VP_SYSTEM_HOOK);
+ const size_t length = strlen(library);
for (const char *start = paths; *start;) {
const char *end = strchr(start, ':');
size_t count = end ? (size_t)(end - start) : strlen(start);
- if (count == length && strncmp(start, VP_SYSTEM_HOOK, count) == 0)
+ if (count == length && strncmp(start, library, count) == 0)
return 1;
if (!end)
break;
@@ -49,8 +67,16 @@ static int vpHasHook(const char *paths) {
return 0;
}
-// Keep the bootstrap path with the injected hook across xpcproxy's new envp.
-static VPInjectionEnvironment vpInsertHook(char *const env[], const char *root) {
+static int vpHasHook(const char *paths) {
+ return vpListHasPath(paths, VP_SYSTEM_HOOK);
+}
+
+// Keep the bootstrap path with the injected hooks across xpcproxy's new envp.
+//
+// `extra` is a second library to insert alongside the system hook, or NULL.
+// Only the ones not already listed are added, so this is safe to run over an
+// environment that has been through here before.
+static VPInjectionEnvironment vpInsertHooks(char *const env[], const char *root, const char *extra) {
VPInjectionEnvironment result = {0};
size_t count = 0;
size_t dyld = (size_t)-1;
@@ -67,22 +93,33 @@ static VPInjectionEnvironment vpInsertHook(char *const env[], const char *root)
return result;
}
const char *existing = dyld == (size_t)-1 ? NULL : env[dyld] + 22;
- int addHook = !vpHasHook(existing);
+ int addHook = !vpListHasPath(existing, VP_SYSTEM_HOOK);
+ int addExtra = extra && *extra && !vpListHasPath(existing, extra);
int addRoot = root && *root &&
(jbRoot == (size_t)-1 || strcmp(env[jbRoot] + 15, root) != 0);
- if (!addHook && !addRoot)
+ if (!addHook && !addExtra && !addRoot)
return result;
- if (addHook) {
- size_t size = strlen("DYLD_INSERT_LIBRARIES=") + strlen(VP_SYSTEM_HOOK) + 1;
+ if (addHook || addExtra) {
+ size_t size = strlen("DYLD_INSERT_LIBRARIES=") + 1;
+ if (addHook)
+ size += strlen(VP_SYSTEM_HOOK) + 1;
+ if (addExtra)
+ size += strlen(extra) + 1;
if (existing && *existing)
size += strlen(existing) + 1;
result.hook = malloc(size);
if (!result.hook)
return result;
+ // The inserted libraries go first, before whatever the caller already
+ // had, so their interposes are in place before anything else loads.
+ int written = snprintf(result.hook, size, "DYLD_INSERT_LIBRARIES=");
+ if (addHook)
+ written += snprintf(result.hook + written, size - (size_t)written, "%s", VP_SYSTEM_HOOK);
+ if (addExtra)
+ written += snprintf(result.hook + written, size - (size_t)written, "%s%s",
+ addHook ? ":" : "", extra);
if (existing && *existing)
- snprintf(result.hook, size, "DYLD_INSERT_LIBRARIES=%s:%s", VP_SYSTEM_HOOK, existing);
- else
- snprintf(result.hook, size, "DYLD_INSERT_LIBRARIES=%s", VP_SYSTEM_HOOK);
+ snprintf(result.hook + written, size - (size_t)written, ":%s", existing);
}
if (addRoot) {
size_t size = strlen("VPHONE_JB_ROOT=") + strlen(root) + 1;
@@ -110,6 +147,11 @@ static VPInjectionEnvironment vpInsertHook(char *const env[], const char *root)
return result;
}
+// The system hook alone, which is what every spawn gets.
+static VPInjectionEnvironment vpInsertHook(char *const env[], const char *root) {
+ return vpInsertHooks(env, root, NULL);
+}
+
static void vpFreeEnvironment(VPInjectionEnvironment *environment) {
free(environment->values);
free(environment->hook);
diff --git a/VPhoneGuestComponents/SystemHook/SystemHook-vphone.c b/VPhoneGuestComponents/SystemHook/SystemHook-vphone.c
index 70fc62b4..d8e15562 100644
--- a/VPhoneGuestComponents/SystemHook/SystemHook-vphone.c
+++ b/VPhoneGuestComponents/SystemHook/SystemHook-vphone.c
@@ -45,6 +45,34 @@ static int vpIsInjectionTarget(const char *path) {
(vpInBootstrap && path[0] != '/');
}
+static int vpPathHasSuffix(const char *path, const char *suffix) {
+ size_t length = path ? strlen(path) : 0;
+ size_t want = strlen(suffix);
+ return length >= want && strcmp(path + length - want, suffix) == 0;
+}
+
+// The processes that evaluate a code signature or a provisioning profile, and
+// so the ones that have to agree about what device this is and what signatures
+// are acceptable. Everything else spawns without libmisfix.
+//
+// installd runs `+[MICodeSigningVerifier
+// _validateSignatureAndCopyInfoForURL:withOptions:error:]`, which
+// is in MobileInstallation and calls libmis. This is the install.
+// misagent installs the embedded profile and checks ProvisionedDevices.
+// SpringBoard asks MIS again at launch, which is the half neither daemon
+// covers: an app signed with a free personal-team certificate
+// could be installed and then refused at launch with 0xE8008026.
+//
+// Matched on the end of the path so a bootstrap or cryptex copy of the same
+// binary is caught too.
+static int vpIsMISFixTarget(const char *path) {
+ if (!path)
+ return 0;
+ return vpPathHasSuffix(path, "/usr/libexec/installd") ||
+ vpPathHasSuffix(path, "/usr/libexec/misagent") ||
+ vpPathHasSuffix(path, "/SpringBoard.app/SpringBoard");
+}
+
static int vpOpenLog(const char *name) {
char path[PATH_MAX];
int used = snprintf(path, sizeof(path), "/var/mobile/Library/Caches/%s", name);
@@ -90,12 +118,14 @@ static void vpPrepareLoaderLink(const char *path) {
// then skips ElleKit. Only bootstrap, app and camera targets are logged and
// get their loader links prepared.
static VPInjectionEnvironment vpPrepareChild(const char *path, char *const envp[], const char *kind) {
- VPInjectionEnvironment injected = vpInsertHook(envp, getenv("VPHONE_JB_ROOT"));
- if (vpIsInjectionTarget(path)) {
+ const int misFix = vpIsMISFixTarget(path);
+ VPInjectionEnvironment injected =
+ vpInsertHooks(envp, getenv("VPHONE_JB_ROOT"), misFix ? VP_MIS_FIX : NULL);
+ if (vpIsInjectionTarget(path) || misFix) {
vpPrepareLoaderLink(path);
- char decision[64];
- snprintf(decision, sizeof(decision), "%s%s%s", kind, !injected.values ? "unchanged" : "inserted",
- vpInjectionDisabled(envp) ? "-tweaks-disabled" : "");
+ char decision[80];
+ snprintf(decision, sizeof(decision), "%s%s%s%s", kind, !injected.values ? "unchanged" : "inserted",
+ misFix ? "+misfix" : "", vpInjectionDisabled(envp) ? "-tweaks-disabled" : "");
vpLogSpawn(path, decision);
}
return injected;