Skip to content

Run a virtual iPhone on an Apple Silicon Mac.

Swift

Chain load bootstrap TweakLoader from SystemHook

Lakr969c9130d1d4
diff --git a/Research/0_binary_patch_comparison.md b/Research/0_binary_patch_comparison.md
index 7120a847..4b6020ba 100644
--- a/Research/0_binary_patch_comparison.md
+++ b/Research/0_binary_patch_comparison.md
@@ -41,8 +41,14 @@
> path, and spawn status under `/var/mobile/Library/Caches`. SystemHook logs
> there when permitted and falls back to the app's own `Library/Caches` under
> its sandboxed home directory.
-> It still does not load ElleKit, TweakLoader, or individual tweaks in target
-> processes. Tweak selection and sandbox behavior require separate validation.
+> SystemHook now carries the selected physical bootstrap path in
+> `VPHONE_JB_ROOT` and loads that root's `usr/lib/TweakLoader.dylib` for App
+> and bootstrap executables when it exists. ElleKit owns individual tweak
+> selection and loading. `xpcproxy` only propagates the hook; unrelated
+> system daemons do not load TweakLoader. Injected App and bootstrap processes
+> also propagate to targeted `posix_spawn`, `posix_spawnp`, and `execve` calls.
+> RootHide sandbox behavior and real ElleKit tweak loading still require
+> runtime validation.
> On the rootless iOS 26.6.2 clone, `xpcproxy` called `posix_spawnp` for the
> package probe, and the final daemon reported
> `DYLD_INSERT_LIBRARIES=/usr/lib/SystemHook-vphone.dylib` and
@@ -62,6 +68,20 @@
> injection enabled; the app's own
> `Library/Caches/vphone-systemhook.log` recorded the same PID and Calculator
> executable path. The app reached the foreground and rendered normally.
+> In the subsequent chain-load test, a signed diagnostic `TweakLoader.dylib`
+> under the rootless bootstrap ran its constructor in the final daemon and in
+> Calculator PID 403. Calculator's sandboxed container recorded the physical
+> bootstrap path and a successful `dlopen`; the App stayed frontmost. Three
+> separate RunAtLoad daemon probes with `DISABLE_TWEAKS=1`, `_SafeMode=1`, and
+> `_MSSafeMode=1` each reported no `DYLD_INSERT_LIBRARIES` and
+> `systemhook_loaded=0` after reboot. This diagnostic library proves the
+> loading path, not real ElleKit or a tweak package.
+> A bootstrap CLI probe then spawned itself with explicit stripped
+> environments. Its `posix_spawn`, `posix_spawnp` (absolute program path), and
+> `execve` children all reported `systemhook_loaded=1`,
+> `VPHONE_JB_ROOT=/private/var/jb`, and the expected DYLD insertion. The first
+> attempt exposed an alias gap: a `/var/jb/...` path was not recognized as
+> the physical `/private/var/jb/...` root; both forms are now accepted.
> The previous central-log-only probe could not observe sandboxed apps even
> when SystemHook was loaded.
> The cloned `vphone-launchdhook-lab-26.6.2` booted with the weak dylib and
diff --git a/VPhoneGuestComponents/LaunchHook/launchdhook-vphone.c b/VPhoneGuestComponents/LaunchHook/launchdhook-vphone.c
index 59bb1bd7..85869741 100644
--- a/VPhoneGuestComponents/LaunchHook/launchdhook-vphone.c
+++ b/VPhoneGuestComponents/LaunchHook/launchdhook-vphone.c
@@ -62,7 +62,7 @@ static int vpSpawn(pid_t *restrict pid, const char *restrict path, const posix_s
vpLogSpawn("app-disabled", path, status == 0 && pid ? *pid : -1, status);
return status;
}
- VPInjectionEnvironment injected = vpInsertHook(envp);
+ VPInjectionEnvironment injected = vpInsertHook(envp, vpBootRoot);
int status = vpOriginalSpawn(pid, path, actions, attributes, argv, injected.values ? injected.values : envp);
vpLogInjection(injected.values ? "inserted" : "unchanged", path, status);
vpLogSpawn(injected.values ? "inserted" : "unchanged", path, status == 0 && pid ? *pid : -1,
@@ -92,6 +92,7 @@ static void vpInstallSpawnHook(const char *root) {
snprintf(vpBootRoot, sizeof(vpBootRoot), "%s", root);
hook((void *)posix_spawn, (void *)vpSpawn, (void **)&vpOriginalSpawn);
installed = vpOriginalSpawn != NULL;
+ vpLogInjection("bootstrap-root", vpBootRoot, 0);
vpLogInjection(installed ? "installed" : "install-failed", path, 0);
}
diff --git a/VPhoneGuestComponents/README.md b/VPhoneGuestComponents/README.md
index e15721ff..00968e1a 100644
--- a/VPhoneGuestComponents/README.md
+++ b/VPhoneGuestComponents/README.md
@@ -17,9 +17,13 @@ The archive is a local build artifact, not a VM bootstrap. `cfw install` places
both hooks in `/usr/lib`. After a bootstrap installs ElleKit, the launchd hook
inserts SystemHook into `xpcproxy`, bootstrap executables, and apps started
directly by launchd. Inside `xpcproxy`, SystemHook carries itself into the
-final executable through `posix_spawnp`. It logs PID and executable path to
+final executable through `posix_spawnp`. Injected App and bootstrap processes
+carry the hook to their child executables through `posix_spawn`, `posix_spawnp`,
+and `execve`. SystemHook loads the selected bootstrap's
+`usr/lib/TweakLoader.dylib` in App and bootstrap processes when it exists;
+ElleKit owns tweak selection and loading. It logs PID and executable path to
`/var/mobile/Library/Caches/vphone-systemhook.log`, falling back to the app's
-own `Library/Caches` when sandboxed. It does not load ElleKit or tweaks.
+own `Library/Caches` when sandboxed.
`DISABLE_TWEAKS=1` and the safe-mode flags skip injection.
Irisin installs ElleKit's own `TweakLoader.dylib` in the selected bootstrap.
The required GPU bundle is extracted from the selected PCC firmware by
diff --git a/VPhoneGuestComponents/Shared/InjectionEnvironment.h b/VPhoneGuestComponents/Shared/InjectionEnvironment.h
index 99edea0f..506586d2 100644
--- a/VPhoneGuestComponents/Shared/InjectionEnvironment.h
+++ b/VPhoneGuestComponents/Shared/InjectionEnvironment.h
@@ -9,19 +9,24 @@
typedef struct {
char **values;
- char *inserted;
+ char *hook;
+ char *root;
} VPInjectionEnvironment;
-static int vpEnvIsOne(char *const env[], const char *name) {
+static const char *vpEnvValue(char *const env[], const char *name) {
if (!env)
- return 0;
+ return NULL;
size_t length = strlen(name);
for (size_t i = 0; env[i]; i++) {
- if (strncmp(env[i], name, length) == 0 && env[i][length] == '=') {
- return strcmp(env[i] + length + 1, "1") == 0;
- }
+ if (strncmp(env[i], name, length) == 0 && env[i][length] == '=')
+ return env[i] + length + 1;
}
- return 0;
+ return NULL;
+}
+
+static int vpEnvIsOne(char *const env[], const char *name) {
+ const char *value = vpEnvValue(env, name);
+ return value && strcmp(value, "1") == 0;
}
static int vpInjectionDisabled(char *const env[]) {
@@ -44,48 +49,71 @@ static int vpHasHook(const char *paths) {
return 0;
}
-// Returns an owned environment only when the hook needs to be added.
-static VPInjectionEnvironment vpInsertHook(char *const env[]) {
+// Keep the bootstrap path with the injected hook across xpcproxy's new envp.
+static VPInjectionEnvironment vpInsertHook(char *const env[], const char *root) {
VPInjectionEnvironment result = {0};
size_t count = 0;
size_t dyld = (size_t)-1;
+ size_t jbRoot = (size_t)-1;
if (env) {
while (count < 4096 && env[count]) {
if (strncmp(env[count], "DYLD_INSERT_LIBRARIES=", 22) == 0)
dyld = count;
+ if (strncmp(env[count], "VPHONE_JB_ROOT=", 15) == 0)
+ jbRoot = count;
count++;
}
if (count == 4096)
return result;
}
const char *existing = dyld == (size_t)-1 ? NULL : env[dyld] + 22;
- if (vpHasHook(existing))
+ int addHook = !vpHasHook(existing);
+ int addRoot = root && *root &&
+ (jbRoot == (size_t)-1 || strcmp(env[jbRoot] + 15, root) != 0);
+ if (!addHook && !addRoot)
return result;
- size_t size = strlen("DYLD_INSERT_LIBRARIES=") + strlen(VP_SYSTEM_HOOK) + 1;
- if (existing && *existing)
- size += strlen(existing) + 1;
- result.inserted = malloc(size);
- result.values = calloc(count + (dyld == (size_t)-1 ? 2 : 1), sizeof(char *));
- if (!result.inserted || !result.values) {
- free(result.inserted);
- free(result.values);
- return (VPInjectionEnvironment){0};
+ if (addHook) {
+ size_t size = strlen("DYLD_INSERT_LIBRARIES=") + strlen(VP_SYSTEM_HOOK) + 1;
+ if (existing && *existing)
+ size += strlen(existing) + 1;
+ result.hook = malloc(size);
+ if (!result.hook)
+ return result;
+ 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);
}
- if (existing && *existing) {
- snprintf(result.inserted, size, "DYLD_INSERT_LIBRARIES=%s:%s", VP_SYSTEM_HOOK, existing);
- } else {
- snprintf(result.inserted, size, "DYLD_INSERT_LIBRARIES=%s", VP_SYSTEM_HOOK);
+ if (addRoot) {
+ size_t size = strlen("VPHONE_JB_ROOT=") + strlen(root) + 1;
+ result.root = malloc(size);
+ if (!result.root) {
+ free(result.hook);
+ return (VPInjectionEnvironment){0};
+ }
+ snprintf(result.root, size, "VPHONE_JB_ROOT=%s", root);
+ }
+ result.values = calloc(count + (addHook && dyld == (size_t)-1) +
+ (addRoot && jbRoot == (size_t)-1) + 1, sizeof(char *));
+ if (!result.values) {
+ free(result.hook);
+ free(result.root);
+ return (VPInjectionEnvironment){0};
}
for (size_t i = 0; i < count; i++)
- result.values[i] = i == dyld ? result.inserted : env[i];
- if (dyld == (size_t)-1)
- result.values[count] = result.inserted;
+ result.values[i] = addHook && i == dyld ? result.hook :
+ addRoot && i == jbRoot ? result.root : env[i];
+ if (addHook && dyld == (size_t)-1)
+ result.values[count++] = result.hook;
+ if (addRoot && jbRoot == (size_t)-1)
+ result.values[count] = result.root;
return result;
}
static void vpFreeEnvironment(VPInjectionEnvironment *environment) {
free(environment->values);
- free(environment->inserted);
+ free(environment->hook);
+ free(environment->root);
}
#endif
diff --git a/VPhoneGuestComponents/SystemHook/SystemHook-vphone.c b/VPhoneGuestComponents/SystemHook/SystemHook-vphone.c
index 47b8cef5..e610f292 100644
--- a/VPhoneGuestComponents/SystemHook/SystemHook-vphone.c
+++ b/VPhoneGuestComponents/SystemHook/SystemHook-vphone.c
@@ -1,5 +1,7 @@
#include "../Shared/InjectionEnvironment.h"
#include <crt_externs.h>
+#include <dlfcn.h>
+#include <errno.h>
#include <fcntl.h>
#include <limits.h>
#include <mach-o/dyld.h>
@@ -10,6 +12,26 @@
#include <unistd.h>
static int vpInXPCProxy;
+static int vpInBootstrap;
+
+static int vpIsBootstrapPath(const char *path, const char *root) {
+ if (path && strncmp(path, "/var/jb/", 8) == 0)
+ return 1;
+ size_t length = root ? strlen(root) : 0;
+ return path && length && strncmp(path, root, length) == 0 && path[length] == '/';
+}
+
+static int vpIsAppPath(const char *path) {
+ return path && path[0] == '/' && strstr(path, ".app/") != NULL;
+}
+
+static int vpIsInjectionTarget(const char *path) {
+ if (!path)
+ return 0;
+ const char *root = getenv("VPHONE_JB_ROOT");
+ return vpIsBootstrapPath(path, root) || vpIsAppPath(path) ||
+ (vpInBootstrap && path[0] != '/');
+}
static int vpOpenLog(const char *name) {
char path[PATH_MAX];
@@ -41,19 +63,51 @@ static void vpLogSpawn(const char *path, const char *decision) {
static int vpSpawnP(pid_t *restrict pid, const char *restrict path, const posix_spawn_file_actions_t *restrict actions,
const posix_spawnattr_t *restrict attributes, char *const argv[restrict],
char *const envp[restrict]) {
- if (!vpInXPCProxy)
+ if (!vpIsInjectionTarget(path))
return posix_spawnp(pid, path, actions, attributes, argv, envp);
- if (!path || vpInjectionDisabled(envp)) {
+ if (vpInjectionDisabled(envp)) {
vpLogSpawn(path, "disabled");
return posix_spawnp(pid, path, actions, attributes, argv, envp);
}
- VPInjectionEnvironment injected = vpInsertHook(envp);
+ VPInjectionEnvironment injected = vpInsertHook(envp, getenv("VPHONE_JB_ROOT"));
vpLogSpawn(path, injected.values ? "inserted" : "unchanged");
int status = posix_spawnp(pid, path, actions, attributes, argv, injected.values ? injected.values : envp);
vpFreeEnvironment(&injected);
return status;
}
+static int vpSpawn(pid_t *restrict pid, const char *restrict path, const posix_spawn_file_actions_t *restrict actions,
+ const posix_spawnattr_t *restrict attributes, char *const argv[restrict],
+ char *const envp[restrict]) {
+ if (!vpIsInjectionTarget(path))
+ return posix_spawn(pid, path, actions, attributes, argv, envp);
+ if (vpInjectionDisabled(envp)) {
+ vpLogSpawn(path, "disabled");
+ return posix_spawn(pid, path, actions, attributes, argv, envp);
+ }
+ VPInjectionEnvironment injected = vpInsertHook(envp, getenv("VPHONE_JB_ROOT"));
+ vpLogSpawn(path, injected.values ? "inserted" : "unchanged");
+ int status = posix_spawn(pid, path, actions, attributes, argv, injected.values ? injected.values : envp);
+ vpFreeEnvironment(&injected);
+ return status;
+}
+
+static int vpExecve(const char *path, char *const argv[], char *const envp[]) {
+ if (!vpIsInjectionTarget(path))
+ return execve(path, argv, envp);
+ if (vpInjectionDisabled(envp)) {
+ vpLogSpawn(path, "exec-disabled");
+ return execve(path, argv, envp);
+ }
+ VPInjectionEnvironment injected = vpInsertHook(envp, getenv("VPHONE_JB_ROOT"));
+ vpLogSpawn(path, injected.values ? "exec-inserted" : "exec-unchanged");
+ int status = execve(path, argv, injected.values ? injected.values : envp);
+ int savedErrno = errno;
+ vpFreeEnvironment(&injected);
+ errno = savedErrno;
+ return status;
+}
+
// xpcproxy needs this bridge because launchd only spawns the proxy, not its target.
__attribute__((constructor)) static void vpLogProcess(void) {
char path[PATH_MAX];
@@ -61,14 +115,52 @@ __attribute__((constructor)) static void vpLogProcess(void) {
if (_NSGetExecutablePath(path, &length) != 0)
return;
vpInXPCProxy = strcmp(path, "/usr/libexec/xpcproxy") == 0;
+ vpInBootstrap = vpIsBootstrapPath(path, getenv("VPHONE_JB_ROOT"));
int fd = vpOpenLog("vphone-systemhook.log");
- if (fd < 0)
+ if (fd >= 0) {
+ char **arguments = *_NSGetArgv();
+ dprintf(fd, "pid=%d path=%s label=%s root=%s\n", getpid(), path,
+ vpInXPCProxy && arguments && arguments[1] ? arguments[1] : "-",
+ getenv("VPHONE_JB_ROOT") ? getenv("VPHONE_JB_ROOT") : "<absent>");
+ close(fd);
+ }
+
+ if (vpInXPCProxy || getpid() == 1 || vpInjectionDisabled(*_NSGetEnviron()))
return;
- char **arguments = *_NSGetArgv();
- dprintf(fd, "pid=%d path=%s label=%s\n", getpid(), path,
- vpInXPCProxy && arguments && arguments[1] ? arguments[1] : "-");
- close(fd);
+ const char *name = strrchr(path, '/');
+ name = name ? name + 1 : path;
+ if (strcmp(name, "vphoned") == 0 || strcmp(name, "logd") == 0 ||
+ strcmp(name, "notifyd") == 0 || strcmp(name, "usermanagerd") == 0)
+ return;
+ const char *root = getenv("VPHONE_JB_ROOT");
+ if (!root || !*root)
+ root = "/var/jb";
+ if (!vpInBootstrap && !vpIsAppPath(path))
+ return;
+ char loader[PATH_MAX];
+ int used = snprintf(loader, sizeof(loader), "%s/usr/lib/TweakLoader.dylib", root);
+ if (used <= 0 || (size_t)used >= sizeof(loader))
+ return;
+ if (access(loader, R_OK) != 0) {
+ int accessError = errno;
+ if (accessError != ENOENT) {
+ fd = vpOpenLog("vphone-systemhook.log");
+ if (fd >= 0) {
+ dprintf(fd, "pid=%d tweakloader=%s access_errno=%d\n", getpid(), loader, accessError);
+ close(fd);
+ }
+ }
+ return;
+ }
+ void *loaded = dlopen(loader, RTLD_NOW | RTLD_LOCAL);
+ fd = vpOpenLog("vphone-systemhook.log");
+ if (fd >= 0) {
+ const char *error = loaded ? NULL : dlerror();
+ dprintf(fd, "pid=%d tweakloader=%s result=%s\n", getpid(), loader,
+ loaded ? "loaded" : error ? error : "unknown error");
+ close(fd);
+ }
}
int vphone_systemhook_version(void) { return 1; }
@@ -78,4 +170,6 @@ __attribute__((used, section("__DATA,__interpose"))) static const struct {
const void *replacee;
} vpInterpose[] = {
{(const void *)vpSpawnP, (const void *)posix_spawnp},
+ {(const void *)vpSpawn, (const void *)posix_spawn},
+ {(const void *)vpExecve, (const void *)execve},
};