Run a virtual iPhone on an Apple Silicon Mac.
Swift
Let the spawn hooks insert more than one library into a process
Jackyfc80ed6448f9
diff --git a/VPhoneGuestComponents/LaunchHook/launchdhook-vphone.c b/VPhoneGuestComponents/LaunchHook/launchdhook-vphone.cindex 37c95d6a..c447f6cd 100644--- a/VPhoneGuestComponents/LaunchHook/launchdhook-vphone.c+++ b/VPhoneGuestComponents/LaunchHook/launchdhook-vphone.c@@ -93,17 +93,23 @@ static int vpSpawnWith(VPSpawnFunction spawn, pid_t *restrict pid, const char *rif (!path || strcmp(path, "/sbin/launchd") == 0)return spawn(pid, path, actions, attributes, argv, envp);// launchd starts some jobs itself rather than through xpcproxy — SpringBoard- // is one — so the MIS, battery health and DeviceHub hooks have to be- // decided here as well as in SystemHook.- const char *library = vpInsertedLibraryFor(path);- VPInjectionEnvironment injected = vpInsertHooks(envp, vpBootRoot, library);+ // is one — so the MIS, battery health, DeviceHub and device name hooks+ // have to be decided here as well as in SystemHook.+ const VPInsertedLibraries libraries = vpInsertedLibrariesFor(path);+ VPInjectionEnvironment injected = vpInsertHooksFor(envp, vpBootRoot, &libraries);int status = spawn(pid, path, actions, attributes, argv, injected.values ? injected.values : envp);- if (bootstrapProgram || appProgram || library || strcmp(path, "/usr/libexec/xpcproxy") == 0) {- const char *event = !injected.values ? "unchanged" :- library && strcmp(library, VP_MIS_FIX) == 0 ? "inserted+misfix" :- library && strcmp(library, VP_BATTERY_HEALTH_FIX) == 0 ? "inserted+batteryhealthfix" :- library ? "inserted+devicehubfix" :- vpInjectionDisabled(envp) ? "inserted-tweaks-disabled" : "inserted";+ if (bootstrapProgram || appProgram || libraries.count || strcmp(path, "/usr/libexec/xpcproxy") == 0) {+ char event[128];+ if (!injected.values) {+ snprintf(event, sizeof(event), "unchanged");+ } else if (libraries.count) {+ char names[96];+ vpDescribeInsertedLibraries(&libraries, names, sizeof(names));+ snprintf(event, sizeof(event), "inserted%s", names);+ } else {+ snprintf(event, sizeof(event), "%s",+ vpInjectionDisabled(envp) ? "inserted-tweaks-disabled" : "inserted");+ }vpLogInjection(event, path, status);vpLogSpawn(event, path, status == 0 && pid ? *pid : -1, status);}diff --git a/VPhoneGuestComponents/Makefile b/VPhoneGuestComponents/Makefileindex 5c348465..eb571a03 100644--- a/VPhoneGuestComponents/Makefile+++ b/VPhoneGuestComponents/Makefile@@ -125,6 +125,7 @@ test-loader-links:test-injection-environment:@mkdir -p $(OUT)/usr/bin/clang -Wall -Wextra -Werror -Wno-unused-function -Wno-write-strings \+ -DVP_TEST_LIBRARY_DIR='"$(OUT)/injection-libraries"' \-o $(OUT)/InjectionEnvironmentTests Tests/InjectionEnvironmentTests.c@$(OUT)/InjectionEnvironmentTestsdiff --git a/VPhoneGuestComponents/Shared/InjectionEnvironment.h b/VPhoneGuestComponents/Shared/InjectionEnvironment.hindex 38c76910..fd1e8150 100644--- a/VPhoneGuestComponents/Shared/InjectionEnvironment.h+++ b/VPhoneGuestComponents/Shared/InjectionEnvironment.h@@ -12,7 +12,12 @@// signature or a provisioning profile — see `vpIsMISFixTarget` below —// rather than carried by every spawn. This insertion is the only way it gets// there: no guest binary carries a load command for it.+//+// The inserted libraries' paths can be predefined, so the host tests can point+// them at files of their own; the guest builds use these.+#ifndef VP_MIS_FIX#define VP_MIS_FIX "/usr/lib/libmisfix.dylib"+#endifstatic int vpPathHasSuffix(const char *path, const char *suffix) {size_t length = path ? strlen(path) : 0;@@ -66,7 +71,9 @@ static const char *vpMISFixFor(const char *path) {// BatteryHealthFix/libbatteryhealthfix.c. Inserted rather than loaded by// SystemHook because it interposes: the table has to be in place before// BatteryUsageUI.bundle is bound.+#ifndef VP_BATTERY_HEALTH_FIX#define VP_BATTERY_HEALTH_FIX "/usr/lib/libbatteryhealthfix.dylib"+#endif// Settings, the one process that shows Battery Health. Asked by both spawn// hooks, like the MIS targets, so it holds whichever of them starts the app.@@ -83,7 +90,9 @@ static const char *vpBatteryHealthFixFor(const char *path) {// The DeviceHub hook, which lets Xcode's device viewer stream the guest's// screen; see DeviceHubFix/libdevicehubfix.c. Inserted because its// interposes have to be in place when the target's imports are bound.+#ifndef VP_DEVICEHUB_FIX#define VP_DEVICEHUB_FIX "/usr/lib/libdevicehubfix.dylib"+#endif// cryptexd, which stages and grafts the developer disk image, and the DDI's// display server. The DDI is mounted under /System/Developer, so the@@ -98,13 +107,79 @@ static const char *vpDeviceHubFixFor(const char *path) {return vpIsDeviceHubFixTarget(path) && access(VP_DEVICEHUB_FIX, R_OK) == 0 ? VP_DEVICEHUB_FIX : NULL;}-// The one library, if any, the spawn hooks insert beside SystemHook for-// `path`. The MIS, battery health and DeviceHub targets do not overlap.+// The device name hook, which pins the name the host chose; see+// DeviceName/libdevicename.c. Inserted because it interposes the+// SystemConfiguration imports of the two processes below, which have to be+// rebound before either runs.+#ifndef VP_DEVICE_NAME+#define VP_DEVICE_NAME "/usr/lib/libdevicename.dylib"+#endif++// configd publishes the name into the dynamic store, and lockdownd is where a+// host or Settings renames the device. lockdownd also takes libmisfix, so it+// is the one process that gets two libraries.+static int vpIsDeviceNameTarget(const char *path) {+ return path && (vpPathHasSuffix(path, "/usr/libexec/configd") ||+ vpPathHasSuffix(path, "/usr/libexec/lockdownd"));+}++static const char *vpDeviceNameFor(const char *path) {+ return vpIsDeviceNameTarget(path) && access(VP_DEVICE_NAME, R_OK) == 0 ? VP_DEVICE_NAME : NULL;+}++// The most libraries one process takes beside SystemHook.+#define VP_INSERTED_LIBRARIES_MAX 4++typedef struct {+ const char *paths[VP_INSERTED_LIBRARIES_MAX];+ size_t count;+} VPInsertedLibraries;++// The libraries the spawn hooks insert beside SystemHook for `path`, in+// insertion order. Each is listed only when it is installed. The MIS, battery+// health and DeviceHub targets do not overlap; the device name targets overlap+// the MIS ones at lockdownd, which gets libmisfix and then libdevicename.+static VPInsertedLibraries vpInsertedLibrariesFor(const char *path) {+ VPInsertedLibraries result = {{0}, 0};+ const char *const candidates[] = {+ vpMISFixFor(path),+ vpBatteryHealthFixFor(path),+ vpDeviceHubFixFor(path),+ vpDeviceNameFor(path),+ };+ for (size_t index = 0; index < sizeof(candidates) / sizeof(candidates[0]); index++) {+ if (candidates[index] && result.count < VP_INSERTED_LIBRARIES_MAX)+ result.paths[result.count++] = candidates[index];+ }+ return result;+}++// The first of those libraries, or NULL.static const char *vpInsertedLibraryFor(const char *path) {- const char *library = vpMISFixFor(path);- if (!library)- library = vpBatteryHealthFixFor(path);- return library ? library : vpDeviceHubFixFor(path);+ VPInsertedLibraries libraries = vpInsertedLibrariesFor(path);+ return libraries.count ? libraries.paths[0] : NULL;+}++// "+misfix+devicename" for the libraries' file names, for the spawn logs:+// "/usr/lib/libmisfix.dylib" is logged as "+misfix", as it always was.+static void vpDescribeInsertedLibraries(const VPInsertedLibraries *libraries, char *buffer, size_t size) {+ if (!size)+ return;+ buffer[0] = '\0';+ size_t used = 0;+ for (size_t index = 0; libraries && index < libraries->count; index++) {+ const char *name = strrchr(libraries->paths[index], '/');+ name = name ? name + 1 : libraries->paths[index];+ if (strncmp(name, "lib", 3) == 0)+ name += 3;+ size_t length = strlen(name);+ if (length > 6 && strcmp(name + length - 6, ".dylib") == 0)+ length -= 6;+ int written = snprintf(buffer + used, size - used, "+%.*s", (int)length, name);+ if (written < 0 || (size_t)written >= size - used)+ return;+ used += (size_t)written;+ }}typedef struct {@@ -156,10 +231,12 @@ static int vpHasHook(const char *paths) {// 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) {+// `extras` are `extraCount` libraries to insert alongside the system hook, in+// order; NULL and empty entries are skipped. 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 vpInsertHookLibraries(char *const env[], const char *root,+ const char *const extras[], size_t extraCount) {VPInjectionEnvironment result = {0};size_t count = 0;size_t dyld = (size_t)-1;@@ -177,7 +254,20 @@ static VPInjectionEnvironment vpInsertHooks(char *const env[], const char *root,}const char *existing = dyld == (size_t)-1 ? NULL : env[dyld] + 22;int addHook = !vpListHasPath(existing, VP_SYSTEM_HOOK);- int addExtra = extra && *extra && !vpListHasPath(existing, extra);+ // The extras still missing, without repeats, in the caller's order.+ const char *adding[VP_INSERTED_LIBRARIES_MAX];+ size_t addCount = 0;+ for (size_t index = 0; extras && index < extraCount && addCount < VP_INSERTED_LIBRARIES_MAX; index++) {+ const char *extra = extras[index];+ if (!extra || !*extra || strcmp(extra, VP_SYSTEM_HOOK) == 0 || vpListHasPath(existing, extra))+ continue;+ int repeated = 0;+ for (size_t earlier = 0; earlier < addCount; earlier++)+ repeated = repeated || strcmp(adding[earlier], extra) == 0;+ if (!repeated)+ adding[addCount++] = extra;+ }+ int addExtra = addCount > 0;int addRoot = root && *root &&(jbRoot == (size_t)-1 || strcmp(env[jbRoot] + 15, root) != 0);if (!addHook && !addExtra && !addRoot)@@ -186,8 +276,8 @@ static VPInjectionEnvironment vpInsertHooks(char *const env[], const char *root,size_t size = strlen("DYLD_INSERT_LIBRARIES=") + 1;if (addHook)size += strlen(VP_SYSTEM_HOOK) + 1;- if (addExtra)- size += strlen(extra) + 1;+ for (size_t index = 0; index < addCount; index++)+ size += strlen(adding[index]) + 1;if (existing && *existing)size += strlen(existing) + 1;result.hook = malloc(size);@@ -196,11 +286,16 @@ static VPInjectionEnvironment vpInsertHooks(char *const env[], const char *root,// 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)+ int listed = 0;+ if (addHook) {written += snprintf(result.hook + written, size - (size_t)written, "%s", VP_SYSTEM_HOOK);- if (addExtra)+ listed = 1;+ }+ for (size_t index = 0; index < addCount; index++) {written += snprintf(result.hook + written, size - (size_t)written, "%s%s",- addHook ? ":" : "", extra);+ listed ? ":" : "", adding[index]);+ listed = 1;+ }if (existing && *existing)snprintf(result.hook + written, size - (size_t)written, ":%s", existing);}@@ -233,6 +328,19 @@ static VPInjectionEnvironment vpInsertHooks(char *const env[], const char *root,return result;}+// One extra library, or none when `extra` is NULL.+static VPInjectionEnvironment vpInsertHooks(char *const env[], const char *root, const char *extra) {+ const char *const extras[] = {extra};+ return vpInsertHookLibraries(env, root, extras, extra ? 1 : 0);+}++// The libraries `vpInsertedLibrariesFor` chose.+static VPInjectionEnvironment vpInsertHooksFor(char *const env[], const char *root,+ const VPInsertedLibraries *libraries) {+ return vpInsertHookLibraries(env, root, libraries ? libraries->paths : NULL,+ libraries ? libraries->count : 0);+}+static void vpFreeEnvironment(VPInjectionEnvironment *environment) {free(environment->values);free(environment->hook);diff --git a/VPhoneGuestComponents/SystemHook/SystemHook-vphone.c b/VPhoneGuestComponents/SystemHook/SystemHook-vphone.cindex 4cc70ddf..70931afd 100644--- a/VPhoneGuestComponents/SystemHook/SystemHook-vphone.c+++ b/VPhoneGuestComponents/SystemHook/SystemHook-vphone.c@@ -98,16 +98,16 @@ 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) {- const int misFix = vpIsMISFixTarget(path);- const int batteryHealthFix = vpIsBatteryHealthFixTarget(path);- const int deviceHubFix = vpIsDeviceHubFixTarget(path);- VPInjectionEnvironment injected = vpInsertHooks(envp, getenv("VPHONE_JB_ROOT"), vpInsertedLibraryFor(path));- if (vpIsInjectionTarget(path) || misFix || deviceHubFix) {+ const VPInsertedLibraries libraries = vpInsertedLibrariesFor(path);+ VPInjectionEnvironment injected = vpInsertHooksFor(envp, getenv("VPHONE_JB_ROOT"), &libraries);+ if (vpIsInjectionTarget(path) || vpIsMISFixTarget(path) || vpIsDeviceHubFixTarget(path) ||+ vpIsDeviceNameTarget(path)) {vpPrepareLoaderLink(path);- char decision[96];+ char names[96];+ vpDescribeInsertedLibraries(&libraries, names, sizeof(names));+ char decision[160];snprintf(decision, sizeof(decision), "%s%s%s%s", kind, !injected.values ? "unchanged" : "inserted",- misFix ? "+misfix" : batteryHealthFix ? "+batteryhealthfix" : deviceHubFix ? "+devicehubfix" : "",- vpInjectionDisabled(envp) ? "-tweaks-disabled" : "");+ names, vpInjectionDisabled(envp) ? "-tweaks-disabled" : "");vpLogSpawn(path, decision);}return injected;diff --git a/VPhoneGuestComponents/Tests/InjectionEnvironmentTests.c b/VPhoneGuestComponents/Tests/InjectionEnvironmentTests.cindex 8393b9e8..9a9904bb 100644--- a/VPhoneGuestComponents/Tests/InjectionEnvironmentTests.c+++ b/VPhoneGuestComponents/Tests/InjectionEnvironmentTests.c@@ -1,5 +1,18 @@+// The inserted libraries live under a directory of the test's own, which the+// Makefile names, so the selection can be checked with every one installed.+#ifndef VP_TEST_LIBRARY_DIR+#define VP_TEST_LIBRARY_DIR "/tmp/vphone-injection-environment-tests"+#endif+#define VP_MIS_FIX VP_TEST_LIBRARY_DIR "/libmisfix.dylib"+#define VP_BATTERY_HEALTH_FIX VP_TEST_LIBRARY_DIR "/libbatteryhealthfix.dylib"+#define VP_DEVICEHUB_FIX VP_TEST_LIBRARY_DIR "/libdevicehubfix.dylib"+#define VP_DEVICE_NAME VP_TEST_LIBRARY_DIR "/libdevicename.dylib"+#include "../Shared/InjectionEnvironment.h"#include <assert.h>+#include <errno.h>+#include <fcntl.h>+#include <sys/stat.h>#define EXTRA "/usr/lib/extra.dylib"@@ -77,6 +90,114 @@ static void namesTheDeviceHubFixTarget(void) {assert(!vpIsBatteryHealthFixTarget("/System/Developer/usr/libexec/dtremotedisplayd"));}+static void namesTheDeviceNameTargets(void) {+ assert(vpIsDeviceNameTarget("/usr/libexec/configd"));+ assert(vpIsDeviceNameTarget("/usr/libexec/lockdownd"));+ assert(!vpIsDeviceNameTarget("/usr/libexec/configdx"));+ assert(!vpIsDeviceNameTarget("/usr/libexec/lockdowndx"));+ assert(!vpIsDeviceNameTarget("/usr/libexec/configd/"));+ assert(!vpIsDeviceNameTarget("/usr/sbin/configd"));+ assert(!vpIsDeviceNameTarget("/usr/libexec/remoted"));+ assert(!vpIsDeviceNameTarget("/usr/libexec/installd"));+ assert(!vpIsDeviceNameTarget("configd"));+ assert(!vpIsDeviceNameTarget(NULL));+ // configd is nobody else's target; lockdownd is libmisfix's too.+ assert(!vpIsMISFixTarget("/usr/libexec/configd"));+ assert(!vpIsBatteryHealthFixTarget("/usr/libexec/configd"));+ assert(!vpIsDeviceHubFixTarget("/usr/libexec/configd"));+ assert(vpIsMISFixTarget("/usr/libexec/lockdownd"));+}++static void installLibraries(void) {+ if (mkdir(VP_TEST_LIBRARY_DIR, 0755) != 0)+ assert(errno == EEXIST);+ const char *const paths[] = {VP_MIS_FIX, VP_BATTERY_HEALTH_FIX, VP_DEVICEHUB_FIX, VP_DEVICE_NAME};+ for (size_t index = 0; index < sizeof(paths) / sizeof(paths[0]); index++) {+ int fd = open(paths[index], O_WRONLY | O_CREAT | O_TRUNC, 0644);+ assert(fd >= 0);+ close(fd);+ }+}++// With every library installed, each process gets its own, and lockdownd gets+// two: libmisfix first, as before, then libdevicename.+static void selectsEveryLibraryOfATarget(void) {+ installLibraries();+ VPInsertedLibraries lockdownd = vpInsertedLibrariesFor("/usr/libexec/lockdownd");+ assert(lockdownd.count == 2);+ assert(strcmp(lockdownd.paths[0], VP_MIS_FIX) == 0);+ assert(strcmp(lockdownd.paths[1], VP_DEVICE_NAME) == 0);+ assert(strcmp(vpInsertedLibraryFor("/usr/libexec/lockdownd"), VP_MIS_FIX) == 0);++ VPInsertedLibraries configd = vpInsertedLibrariesFor("/usr/libexec/configd");+ assert(configd.count == 1);+ assert(strcmp(configd.paths[0], VP_DEVICE_NAME) == 0);++ VPInsertedLibraries installd = vpInsertedLibrariesFor("/usr/libexec/installd");+ assert(installd.count == 1 && strcmp(installd.paths[0], VP_MIS_FIX) == 0);+ VPInsertedLibraries settings = vpInsertedLibrariesFor("/Applications/Preferences.app/Preferences");+ assert(settings.count == 1 && strcmp(settings.paths[0], VP_BATTERY_HEALTH_FIX) == 0);+ VPInsertedLibraries cryptexd = vpInsertedLibrariesFor("/usr/libexec/cryptexd");+ assert(cryptexd.count == 1 && strcmp(cryptexd.paths[0], VP_DEVICEHUB_FIX) == 0);+ assert(vpInsertedLibrariesFor("/usr/libexec/xpcproxy").count == 0);+ assert(vpInsertedLibrariesFor(NULL).count == 0);++ char names[96];+ vpDescribeInsertedLibraries(&lockdownd, names, sizeof(names));+ assert(strcmp(names, "+misfix+devicename") == 0);+ vpDescribeInsertedLibraries(&settings, names, sizeof(names));+ assert(strcmp(names, "+batteryhealthfix") == 0);+ vpDescribeInsertedLibraries(&(VPInsertedLibraries){{0}, 0}, names, sizeof(names));+ assert(strcmp(names, "") == 0);++ // A missing library is left out, and the other still goes in.+ assert(unlink(VP_DEVICE_NAME) == 0);+ lockdownd = vpInsertedLibrariesFor("/usr/libexec/lockdownd");+ assert(lockdownd.count == 1 && strcmp(lockdownd.paths[0], VP_MIS_FIX) == 0);+ assert(vpInsertedLibrariesFor("/usr/libexec/configd").count == 0);+}++// lockdownd's two libraries go in after SystemHook, in order.+static void insertsSeveralLibraries(void) {+ char *env[] = {"HOME=/var/root", NULL};+ const VPInsertedLibraries libraries = {{VP_MIS_FIX, VP_DEVICE_NAME}, 2};+ VPInjectionEnvironment result = vpInsertHooksFor(env, NULL, &libraries);+ assert(result.values);+ assert(count(result.values) == 2);+ assert(strcmp(value(result.values, "DYLD_INSERT_LIBRARIES"),+ VP_SYSTEM_HOOK ":" VP_MIS_FIX ":" VP_DEVICE_NAME) == 0);+ vpFreeEnvironment(&result);+}++// An environment that already carries SystemHook and libmisfix — launchd's+// own, or one from before libdevicename existed — gains only the missing one.+static void addsOnlyTheMissingLibrary(void) {+ char *env[] = {"DYLD_INSERT_LIBRARIES=" VP_SYSTEM_HOOK ":" VP_MIS_FIX, NULL};+ const VPInsertedLibraries libraries = {{VP_MIS_FIX, VP_DEVICE_NAME}, 2};+ VPInjectionEnvironment result = vpInsertHooksFor(env, NULL, &libraries);+ assert(result.values);+ assert(count(result.values) == 1);+ assert(strcmp(value(result.values, "DYLD_INSERT_LIBRARIES"),+ VP_DEVICE_NAME ":" VP_SYSTEM_HOOK ":" VP_MIS_FIX) == 0);+ vpFreeEnvironment(&result);++ char *complete[] = {"DYLD_INSERT_LIBRARIES=" VP_SYSTEM_HOOK ":" VP_MIS_FIX ":" VP_DEVICE_NAME, NULL};+ result = vpInsertHooksFor(complete, NULL, &libraries);+ assert(!result.values);+ vpFreeEnvironment(&result);+}++// Repeats, empty entries and SystemHook itself are not listed twice.+static void skipsRepeatedLibraries(void) {+ char *env[] = {NULL};+ const char *const extras[] = {VP_MIS_FIX, "", NULL, VP_MIS_FIX, VP_SYSTEM_HOOK, VP_DEVICE_NAME};+ VPInjectionEnvironment result = vpInsertHookLibraries(env, NULL, extras, sizeof(extras) / sizeof(extras[0]));+ assert(result.values);+ assert(strcmp(value(result.values, "DYLD_INSERT_LIBRARIES"),+ VP_SYSTEM_HOOK ":" VP_MIS_FIX ":" VP_DEVICE_NAME) == 0);+ vpFreeEnvironment(&result);+}+int main(void) {insertsBoth();addsExtraBesideAnExistingHook();@@ -84,6 +205,11 @@ int main(void) {namesTheMISFixTargets();namesTheBatteryHealthFixTarget();namesTheDeviceHubFixTarget();+ namesTheDeviceNameTargets();+ selectsEveryLibraryOfATarget();+ insertsSeveralLibraries();+ addsOnlyTheMissingLibrary();+ skipsRepeatedLibraries();puts("InjectionEnvironmentTests: ok");return 0;}