Apple's open-source XNU kernel on Android phones: its own loader and drivers, Metal on the Adreno. Runs from RAM; a reboot gives the phone back to Android.
C
Tweaks run on the port: ElleKit for the hooking, our own loader to find and open them
vcvkk4db3f2de528a
diff --git a/tools/mk-jb.sh b/tools/mk-jb.shindex 6745596..7c27978 100755--- a/tools/mk-jb.sh+++ b/tools/mk-jb.sh@@ -67,6 +67,37 @@ mkdir -p "$TMP/control" && tar -C "$TMP/control" -xf "$TMP"/control.tar.* 2>/devecho} >> "$OUT/root/var/jb/Library/dpkg/status"+# ElleKit: the tweak-injection framework (Cydia Substrate's modern replacement). It is what loads a+# tweak's .dylib into a process and does the function and method hooking; on a rootless jailbreak the+# jailbreak itself installs it, and it is not in the apt repository, so it is fetched here from its own+# distribution. Its libraries are fat (arm64 and arm64e): the arm64 slice runs on this phone's+# Qualcomm chip. mobilesubstrate and org.coolstar.libhooker are virtual packages it Provides, which is+# what preferenceloader and nearly every tweak depend on. The loader /var/jb/usr/lib/TweakLoader.dylib+# (ellekit/libinjector) reads /var/jb/usr/lib/TweakInject/*.plist, matches the process by the Filter+# and dlopens the tweak; src/init.c (etc/inject.conf) inserts it into SpringBoard, Preferences and the+# apps. Tweaks link /var/jb/usr/lib/lib{substrate,hooker}.dylib and CydiaSubstrate.framework, all+# ElleKit.+EK=ellekit_${ELLEKIT:-0.4.5}_iphoneos-arm64.deb+get "https://ellekit.space/pool/$EK" "$EK"+EKTMP=$(mktemp -d); trap 'rm -rf "$TMP" "$EKTMP"' EXIT+(cd "$EKTMP" && ar x "$OUT/debs/$EK")+mkdir -p "$EKTMP/data" && { tar -C "$EKTMP/data" -xf "$EKTMP"/data.tar.* 2>/dev/null ||+ zstd -dc "$EKTMP"/data.tar.zst | tar -C "$EKTMP/data" -xf -; }+cp -a "$EKTMP/data/var/jb/." "$OUT/root/var/jb/"+mkdir -p "$OUT/root/var/jb/usr/lib/TweakInject"+# the old tweak directory, a symlink to the new one, as ElleKit's own preinst makes it+mkdir -p "$OUT/root/var/jb/Library/MobileSubstrate"+rm -rf "$OUT/root/var/jb/Library/MobileSubstrate/DynamicLibraries"+ln -s /var/jb/usr/lib/TweakInject "$OUT/root/var/jb/Library/MobileSubstrate/DynamicLibraries"+# dpkg is to know ElleKit is installed, with what it Provides, so apt satisfies tweaks' "mobilesubstrate"+{+ mkdir -p "$EKTMP/control" && { tar -C "$EKTMP/control" -xf "$EKTMP"/control.tar.* 2>/dev/null ||+ zstd -dc "$EKTMP"/control.tar.zst | tar -C "$EKTMP/control" -xf -; }+ sed '/^$/d' "$EKTMP/control/control"+ echo "Status: install ok installed"+ echo+} >> "$OUT/root/var/jb/Library/dpkg/status"+# the repositories Sileo starts with: Procursus itself, where everything here came frommkdir -p "$OUT/root/var/jb/etc/apt/sources.list.d"cat > "$OUT/root/var/jb/etc/apt/sources.list.d/procursus.sources" <<EOFdiff --git a/userland/build.sh b/userland/build.shindex e534e49..946d311 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -81,9 +81,13 @@ IOS_JOBS=${JOBS-"# What Sileo runs from /bin (see the package manager's prefix below): the system image's /bin, or the# boot volume's bin, bound over the iOS volume's /bin when that is the root (src/init.c, flash_root).SILEO_BIN="chmod cp mkdir mv rm"+# (libredwood-tweaks is the tweak loader of the package manager's prefix, src/inject_tweaks.c: in+# SpringBoard the library above opens it, here it is named for the programs that have no library of+# ours -- Settings, where a tweak's own pane belongs. Add a program's name to have tweaks in it.)INJECT=${INJECT-"/redwood/lib/libredwood.dylib backboardd containermanagerd containermanagerd_system/redwood/lib/libredwood-sb.dylib SpringBoard-/redwood/lib/libredwood-media.dylib mediaserverd"}+/redwood/lib/libredwood-media.dylib mediaserverd+/redwood/lib/libredwood-tweaks.dylib Preferences"}mkdir -p objif [ -z "$PREBUILT" ] && ! command -v ld64 >/dev/null; then PREBUILT=1; fi@@ -155,6 +159,10 @@ if [ -d $IOS/rootfs/usr/lib ]; then-o obj/libredwood.dylib src/inject.c$DYN -dynamiclib -install_name /redwood/lib/libredwood-sb.dylib -framework CoreFoundation -framework IOKit -lobjc \-o obj/libredwood-sb.dylib src/inject_sb.c+ # ... the tweak loader of the package manager's prefix (src/inject_tweaks.c): it goes into a+ # program either as the one library the kernel inserts or opened by a library of ours+ $DYN -dynamiclib -install_name /redwood/lib/libredwood-tweaks.dylib -framework CoreFoundation -lobjc \+ -o obj/libredwood-tweaks.dylib src/inject_tweaks.c# ... and inside an application, to say what killed it (src/inject_app.c)$DYN -dynamiclib -install_name /redwood/lib/libredwood-app.dylib -lobjc \-o obj/libredwood-app.dylib src/inject_app.c@@ -203,6 +211,7 @@ if [ -d $IOS/rootfs/usr/lib ]; thencp obj/libredwood.dylib obj/libredwood-sb.dylib root/lib/[ -f obj/libredwood-media.dylib ] && cp obj/libredwood-media.dylib root/lib/ # (as with cpu_test)[ -f obj/libredwood-app.dylib ] && cp obj/libredwood-app.dylib root/lib/+ [ -f obj/libredwood-tweaks.dylib ] && cp obj/libredwood-tweaks.dylib root/lib/if [ -f obj/RedwoodAudio ]; thenmkdir -p root/lib/RedwoodAudio.drivercp obj/RedwoodAudio root/lib/RedwoodAudio.driver/@@ -215,7 +224,13 @@ if [ -d $IOS/rootfs/usr/lib ]; thenfi# a program is not started with a library that is not there: it would not start at allecho "$INJECT" | while read -r library names; do- [ -z "$library" ] || [ ! -f "root/lib/${library#/redwood/lib/}" ] || echo "$library $names"+ # a /redwood/lib library only if it was actually built; any other path (e.g. a tweak loader+ # in /var/jb) as it is, since it lives on the prefix and is there at run time+ case "$library" in+ "") ;;+ /redwood/lib/*) [ -f "root/lib/${library#/redwood/lib/}" ] && echo "$library $names" ;;+ *) echo "$library $names" ;;+ esacdone > root/etc/inject.confcp $IOS/var.skel root/etc/var.skel # the directories init creates in /private/var# time zones: the template's links go in a circle until the time zone daemon has run (/usr/share/zoneinfodiff --git a/userland/rc.jobs.pl b/userland/rc.jobs.plnew file mode 100644index 0000000..11b7442--- /dev/null+++ b/userland/rc.jobs.pl@@ -0,0 +1,13 @@+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).+# preferenceloader, the package nearly every tweak with a settings pane depends on+# (RC_JOBS=rc.jobs.pl): it wants "mobilesubstrate", which ElleKit Provides, so apt can take it at+# all now; then Settings is opened, and the tweak loader says whether PreferenceLoader went into it.+/redwood/bin/sound_test springboard+/var/jb/bin/sh /var/jb/prep.sh+/redwood/bin/net_test waitaddr en0 60 resolve apt.procurs.us+/var/jb/usr/bin/apt-get update+/var/jb/usr/bin/apt-get install -y preferenceloader+/var/jb/usr/bin/dpkg-query -W -f${binary:Package}_${Version}_${Status}\n preferenceloader ellekit+/var/jb/bin/ls -la /var/jb/usr/lib/TweakInject/+/var/jb/usr/bin/uiopen prefs:+/redwood/bin/rwtouch wait 20diff --git a/userland/src/dlopen_test.c b/userland/src/dlopen_test.cnew file mode 100644index 0000000..f911590--- /dev/null+++ b/userland/src/dlopen_test.c@@ -0,0 +1,46 @@+/*+ * dlopen_test PATH...: open each library the way dyld would and say what happened -- what the path+ * really leads to, and dlerror() when it will not open. For finding out why a library the kernel+ * inserts into a process (etc/inject.conf) is not there in the end.+ */+#include <dlfcn.h>+#include <stdio.h>+#include <stdlib.h>+#include <sys/stat.h>+#include <unistd.h>+#include <errno.h>+#include <string.h>++int+main(int argc, char **argv)+{+ {+ const char *inserted = getenv("DYLD_INSERT_LIBRARIES");++ printf("dlopen_test: DYLD_INSERT_LIBRARIES=%s\n", inserted != NULL ? inserted : "(not set)");+ }+ for (int i = 1; i < argc; i++) {+ char real[1024];+ struct stat st;+ void *h;++ printf("dlopen_test: %s\n", argv[i]);+ if (realpath(argv[i], real) != NULL) {+ printf(" leads to %s\n", real);+ } else {+ printf(" realpath: no (%s)\n", strerror(errno));+ }+ if (stat(argv[i], &st) == 0) {+ printf(" %lld bytes, mode %o, uid %d\n", (long long)st.st_size, st.st_mode & 07777, st.st_uid);+ } else {+ printf(" stat: %s\n", strerror(errno));+ }+ h = dlopen(argv[i], RTLD_NOW | RTLD_GLOBAL);+ if (h != NULL) {+ printf(" OPENED\n");+ } else {+ printf(" dlopen failed: %s\n", dlerror());+ }+ }+ return 0;+}diff --git a/userland/src/inject_sb.c b/userland/src/inject_sb.cindex 86f5072..d28cb7a 100644--- a/userland/src/inject_sb.c+++ b/userland/src/inject_sb.c@@ -33,12 +33,14 @@#include <CoreFoundation/CoreFoundation.h>#include <IOKit/IOKitLib.h>#include <dispatch/dispatch.h>+#include <dlfcn.h>#include <fcntl.h>#include <mach/mach_time.h>#include <notify.h>#include <objc/message.h>#include <objc/runtime.h>#include <stdarg.h>+#include <sys/stat.h>#include <stdbool.h>#include <stdio.h>#include <stdlib.h>@@ -842,6 +844,27 @@ control_center_fades(const char *arguments)method_setImplementation(add, (IMP)add_animation_cc);}+/*+ * Open the tweak loader of the package manager's prefix, when there is one. Nothing here depends on+ * it: without a prefix (or without ElleKit in it) the file is not there and this does nothing.+ */+#define REDWOOD_TWEAK_LOADER "/redwood/lib/libredwood-tweaks.dylib"++static void+load_tweaks(void)+{+ struct stat st;++ if (stat("/var/jb/usr/lib/TweakInject", &st) != 0) {+ return; /* no package manager's prefix here, so no tweaks */+ }+ if (dlopen(REDWOOD_TWEAK_LOADER, RTLD_NOW | RTLD_GLOBAL) == NULL) {+ say("redwood: the tweak loader %s: %s\n", REDWOOD_TWEAK_LOADER, dlerror());+ } else {+ say("redwood: tweaks: %s opened\n", REDWOOD_TWEAK_LOADER);+ }+}+__attribute__((constructor))static voidredwood_springboard_init(void)@@ -868,6 +891,14 @@ redwood_springboard_init(void)});return;}+ /*+ * The tweaks of the package manager's prefix, if one is installed: ElleKit's loader+ * (/var/jb/usr/lib/TweakLoader.dylib) reads /var/jb/usr/lib/TweakInject and opens the tweaks+ * whose Filter names this process. The kernel could insert it as well (etc/inject.conf), but+ * only one inserted library per program is reliable here, and SpringBoard's is this one; so+ * this one opens it, the way a jailbreak's own hook does.+ */+ load_tweaks();slide = (uintptr_t)_dyld_get_shared_cache_range(&length) - CACHE_BASE;activation = replace_method("SBLockdownManager", "_setupActivationState", 0x1c4bc8dfcUL, (IMP)setup_activation_state);original_set_setup_required_reason = (void (*)(id, SEL, long))replace_method("SBSetupManager",diff --git a/userland/src/inject_tweaks.c b/userland/src/inject_tweaks.cnew file mode 100644index 0000000..94dd90a--- /dev/null+++ b/userland/src/inject_tweaks.c@@ -0,0 +1,256 @@+/*+ * libredwood-tweaks.dylib: the tweak loader of this port.+ *+ * A tweak is a dylib that a package manager put in /var/jb/usr/lib/TweakInject, beside a property+ * list of the same name whose Filter says which programs it belongs in. Something has to be inside+ * every program to read that directory and open the tweaks that name it; on a jailbroken phone the+ * jailbreak's own hook does it, and ElleKit ships one (ellekit/libinjector.dylib). That one opens+ * here but loads nothing and says nothing about why, so this is ours, which says what it does.+ *+ * ElleKit is still what does the work a tweak asks for: the tweaks link against+ * /var/jb/usr/lib/libsubstrate.dylib (libhooker, CydiaSubstrate), which is ElleKit's libellekit,+ * and that is the engine that replaces functions and methods. Only the finding and opening is ours.+ *+ * It goes into a program either as the one library the kernel inserts (etc/inject.conf, see+ * src/init.c) or because a library of ours that is already there opens it (src/inject_sb.c does,+ * for SpringBoard). Loading it twice is harmless: the second time does nothing.+ *+ * A Filter says any of:+ * Bundles the identifier of the program's main bundle (com.apple.springboard)+ * Executables the file name of the program (SpringBoard)+ * Classes an Objective-C class the program has+ * and takes the tweak if any of them matches, unless Mode is "Any", where all must. A list with no+ * Filter at all belongs in every program, as it does on a phone.+ */+#include <CoreFoundation/CoreFoundation.h>+#include <dirent.h>+#include <dlfcn.h>+#include <fcntl.h>+#include <mach-o/dyld.h>+#include <objc/runtime.h>+#include <stdarg.h>+#include <stdbool.h>+#include <stdio.h>+#include <string.h>+#include <sys/stat.h>+#include <unistd.h>++#define TWEAK_DIR "/var/jb/usr/lib/TweakInject"++static void+say(const char *format, ...)+{+ char line[512];+ va_list args;+ int n, fd;++ va_start(args, format);+ n = vsnprintf(line, sizeof(line), format, args);+ va_end(args);+ if (n <= 0) {+ return;+ }+ fd = open("/dev/console", O_WRONLY | O_NOCTTY);+ if (fd >= 0) {+ (void)write(fd, line, (size_t)(n < (int)sizeof(line) ? n : (int)sizeof(line) - 1));+ close(fd);+ }+}++/* The name this program was started as, and its bundle identifier when it has one yet. */+static const char *+executable_name(void)+{+ static char path[1024];+ uint32_t size = sizeof(path);+ char *slash;++ if (_NSGetExecutablePath(path, &size) != 0) {+ return NULL;+ }+ slash = strrchr(path, '/');+ return slash != NULL ? slash + 1 : path;+}++static bool+bundle_identifier(char *out, size_t size)+{+ CFBundleRef bundle = CFBundleGetMainBundle();+ CFStringRef identifier;++ if (bundle == NULL) {+ return false;+ }+ identifier = CFBundleGetIdentifier(bundle);+ return identifier != NULL && CFStringGetCString(identifier, out, (CFIndex)size, kCFStringEncodingUTF8);+}++/* Does this array of strings hold `want`? */+static bool+array_has(CFArrayRef array, const char *want)+{+ CFIndex count = array != NULL ? CFArrayGetCount(array) : 0;++ for (CFIndex i = 0; i < count; i++) {+ CFStringRef string = CFArrayGetValueAtIndex(array, i);+ char text[256];++ if (CFGetTypeID(string) == CFStringGetTypeID() &&+ CFStringGetCString(string, text, sizeof(text), kCFStringEncodingUTF8) && strcmp(text, want) == 0) {+ return true;+ }+ }+ return false;+}++static bool+array_has_class(CFArrayRef array)+{+ CFIndex count = array != NULL ? CFArrayGetCount(array) : 0;++ for (CFIndex i = 0; i < count; i++) {+ CFStringRef string = CFArrayGetValueAtIndex(array, i);+ char text[256];++ if (CFGetTypeID(string) == CFStringGetTypeID() &&+ CFStringGetCString(string, text, sizeof(text), kCFStringEncodingUTF8) && objc_getClass(text) != NULL) {+ return true;+ }+ }+ return false;+}++static CFArrayRef+filter_array(CFDictionaryRef filter, const char *key)+{+ CFStringRef name = CFStringCreateWithCString(NULL, key, kCFStringEncodingUTF8);+ CFArrayRef array = NULL;++ if (name != NULL) {+ CFTypeRef value = CFDictionaryGetValue(filter, name);++ if (value != NULL && CFGetTypeID(value) == CFArrayGetTypeID()) {+ array = value;+ }+ CFRelease(name);+ }+ return array;+}++/* Whether the list at `path` names this program. */+static bool+wanted_here(const char *path, const char *name, const char *identifier)+{+ CFDictionaryRef plist, filter;+ CFArrayRef bundles, executables, classes;+ CFStringRef mode_key, filter_key;+ CFDataRef data;+ CFTypeRef mode;+ bool all, hits[3], wanted;+ int fd, used = 0;+ uint8_t buffer[65536];+ ssize_t n;++ fd = open(path, O_RDONLY);+ n = fd >= 0 ? read(fd, buffer, sizeof(buffer)) : -1;+ if (fd >= 0) {+ close(fd);+ }+ if (n <= 0) {+ return false;+ }+ data = CFDataCreate(NULL, buffer, (CFIndex)n);+ if (data == NULL) {+ return false;+ }+ plist = (CFDictionaryRef)CFPropertyListCreateWithData(NULL, data, kCFPropertyListImmutable, NULL, NULL);+ CFRelease(data);+ if (plist == NULL || CFGetTypeID(plist) != CFDictionaryGetTypeID()) {+ if (plist != NULL) {+ CFRelease(plist);+ }+ say("redwood: tweaks: %s is not a property list\n", path);+ return false;+ }+ filter_key = CFSTR("Filter");+ filter = CFDictionaryGetValue(plist, filter_key);+ if (filter == NULL || CFGetTypeID(filter) != CFDictionaryGetTypeID()) {+ CFRelease(plist); /* no Filter: it belongs everywhere */+ return true;+ }+ bundles = filter_array(filter, "Bundles");+ executables = filter_array(filter, "Executables");+ classes = filter_array(filter, "Classes");+ hits[0] = bundles != NULL && identifier != NULL && array_has(bundles, identifier);+ hits[1] = executables != NULL && name != NULL && array_has(executables, name);+ hits[2] = classes != NULL && array_has_class(classes);+ /* Mode "Any" means every kind that is given has to match; by default one is enough. */+ mode_key = CFSTR("Mode");+ mode = CFDictionaryGetValue(filter, mode_key);+ all = mode != NULL && CFGetTypeID(mode) == CFStringGetTypeID() &&+ CFStringCompare(mode, CFSTR("Any"), 0) == kCFCompareEqualTo;+ used = (bundles != NULL) + (executables != NULL) + (classes != NULL);+ if (used == 0) {+ wanted = true;+ } else if (all) {+ wanted = (bundles == NULL || hits[0]) && (executables == NULL || hits[1]) && (classes == NULL || hits[2]);+ } else {+ wanted = hits[0] || hits[1] || hits[2];+ }+ CFRelease(plist);+ return wanted;+}++static void+load_the_tweaks(void)+{+ char identifier[256];+ const char *name = executable_name();+ const char *who = bundle_identifier(identifier, sizeof(identifier)) ? identifier : NULL;+ struct dirent *entry;+ int opened = 0, tried = 0;+ DIR *dir;++ dir = opendir(TWEAK_DIR);+ if (dir == NULL) {+ return; /* no prefix, or no tweaks in it */+ }+ while ((entry = readdir(dir)) != NULL) {+ char plist[1024], dylib[1024];+ size_t length = strlen(entry->d_name);++ if (length < 7 || strcmp(entry->d_name + length - 6, ".plist") != 0) {+ continue;+ }+ snprintf(plist, sizeof(plist), "%s/%s", TWEAK_DIR, entry->d_name);+ snprintf(dylib, sizeof(dylib), "%s/%.*s.dylib", TWEAK_DIR, (int)(length - 6), entry->d_name);+ if (!wanted_here(plist, name, who)) {+ continue;+ }+ tried++;+ if (dlopen(dylib, RTLD_NOW | RTLD_GLOBAL) != NULL) {+ opened++;+ say("redwood: tweaks: %s\n", dylib);+ } else {+ say("redwood: tweaks: %s: %s\n", dylib, dlerror());+ }+ }+ closedir(dir);+ if (tried > 0) {+ say("redwood: tweaks: %s (%s): %d of %d opened\n", name != NULL ? name : "?",+ who != NULL ? who : "no bundle", opened, tried);+ }+}++__attribute__((constructor))+static void+redwood_tweaks_init(void)+{+ static bool done;++ if (done) {+ return;+ }+ done = true;+ load_the_tweaks();+}diff --git a/userland/src/tweaks/redbare.c b/userland/src/tweaks/redbare.cnew file mode 100644index 0000000..6c12cae--- /dev/null+++ b/userland/src/tweaks/redbare.c@@ -0,0 +1,15 @@+/*+ * The same as redtest.c, but with nothing linked against it: if this one loads and redtest does not,+ * what fails is the hooking library (ElleKit is Swift and wants /usr/lib/swift from the shared cache),+ * not the loader that found and opened the tweak.+ */+#include <fcntl.h>+#include <string.h>+#include <unistd.h>++__attribute__((constructor))+static void redbare_init(void) {+ int fd = open("/dev/console", O_WRONLY);+ const char *s = "redwood: bare tweak loaded (no libsubstrate)\n";+ if (fd >= 0) { (void)write(fd, s, strlen(s)); close(fd); }+}diff --git a/userland/src/tweaks/redtest.c b/userland/src/tweaks/redtest.cnew file mode 100644index 0000000..7101c9b--- /dev/null+++ b/userland/src/tweaks/redtest.c@@ -0,0 +1,40 @@+/*+ * A tiny tweak, to prove the port loads tweaks: injected into a process by ElleKit's loader+ * (/var/jb/usr/lib/TweakLoader.dylib, which the kernel inserts into the processes named in+ * etc/inject.conf), it says on the console that it is there, and hooks one SpringBoard method+ * with MSHookMessageEx (from libsubstrate -> libellekit) to show that the hooking engine works.+ */+#include <fcntl.h>+#include <string.h>+#include <unistd.h>+#include <objc/message.h>+#include <objc/runtime.h>++extern void MSHookMessageEx(Class cls, SEL sel, IMP imp, IMP *result);++static void say(const char *s) {+ int fd = open("/dev/console", O_WRONLY);+ if (fd >= 0) { (void)write(fd, s, strlen(s)); close(fd); }+}++/* -[SpringBoard applicationDidFinishLaunching:] has already fired by the time we load, so hook+ * something that keeps being called: -[SBIconView setIcon:] runs whenever an icon is laid out. */+static void (*orig_setIcon)(id, SEL, id);+static int hit;+static void my_setIcon(id self, SEL _cmd, id icon) {+ if (!hit) { hit = 1; say("redwood: tweak hook fired: -[SBIconView setIcon:]\n"); }+ orig_setIcon(self, _cmd, icon);+}++__attribute__((constructor))+static void redtest_init(void) {+ say("redwood: test tweak loaded\n");+ Class c = objc_getClass("SBIconView");+ SEL s = sel_registerName("setIcon:");+ if (c && class_getInstanceMethod(c, s)) {+ MSHookMessageEx(c, s, (IMP)my_setIcon, (IMP *)&orig_setIcon);+ say("redwood: tweak hooked -[SBIconView setIcon:]\n");+ } else {+ say("redwood: tweak: no SBIconView setIcon:\n");+ }+}diff --git a/xnu/redwood.patch b/xnu/redwood.patchindex a135db7..73b773f 100644--- a/xnu/redwood.patch+++ b/xnu/redwood.patch@@ -372,7 +372,7 @@ index 95192dd62449..b00da803b5b4 100644/*diff --git a/bsd/kern/kern_exec.c b/bsd/kern/kern_exec.c-index 1d71c66629fb..fd94fd8a588a 100644+index 1d71c66629fb..3e1737c21b86 100644--- a/bsd/kern/kern_exec.c+++ b/bsd/kern/kern_exec.c@@ -1157,6 +1157,9 @@ SYSCTL_PROC(_vm, OID_AUTO, shared_region_control,@@ -565,7 +565,7 @@ index 1d71c66629fb..fd94fd8a588a 100644ret = persona_proc_adopt(p, persona, cred);kauth_cred_unref(&cred);-@@ -5586,10 +5685,61 @@ bad:+@@ -5586,10 +5685,75 @@ bad:* Note: The argument and environment vectors are user space pointers* to arrays of user space pointers.*/@@ -573,9 +573,11 @@ index 1d71c66629fb..fd94fd8a588a 100644+ * Bring-up aid: the sysctl kern.exec_inject holds "LIBRARY NAME NAME ..."; every program+ * whose file name is one of the NAMEs is started with DYLD_INSERT_LIBRARIES=LIBRARY in its+ * environment, so that dyld loads our library into it. Several such groups may follow each-+ * other, separated by ";". launchd refuses to pass DYLD_ variables from a job description,-+ * which is why the kernel adds this one. Set once by our init (from etc/inject.conf) before-+ * it hands over to launchd.++ * other, separated by ";". A program named in more than one group gets every one of those++ * libraries, joined with ":" in the one DYLD_INSERT_LIBRARIES, so SpringBoard can carry both++ * our window-server library and the tweak loader. launchd refuses to pass DYLD_ variables++ * from a job description, which is why the kernel adds this one. Set once by our init (from++ * etc/inject.conf) before it hands over to launchd.+ */+static char exec_inject_conf[512];+SYSCTL_STRING(_kern, OID_AUTO, exec_inject, CTLFLAG_RW | CTLFLAG_LOCKED, exec_inject_conf, sizeof(exec_inject_conf),@@ -585,16 +587,19 @@ index 1d71c66629fb..fd94fd8a588a 100644+exec_inject_env(struct image_params *imgp, char *env, size_t size)+{+ const char *name, *p = exec_inject_conf;-+ size_t namelen;++ size_t namelen, used;++ bool any = false;++ if (*p == 0 || imgp->ip_ndp == NULL) {+ return false;+ }+ name = imgp->ip_ndp->ni_cnd.cn_nameptr;+ namelen = imgp->ip_ndp->ni_cnd.cn_namelen;++ used = strlcpy(env, "DYLD_INSERT_LIBRARIES=", size);+ while (*p != 0) {+ const char *library;+ size_t liblen;++ bool matched = false;++ while (*p == ' ' || *p == ';') {+ p++;@@ -610,24 +615,33 @@ index 1d71c66629fb..fd94fd8a588a 100644+ while (*p != 0 && *p != ' ' && *p != ';') {+ p++;+ }-+ if ((size_t)(p - word) == namelen && namelen != 0 && liblen != 0 && strncmp(word, name, namelen) == 0) {-+ snprintf(env, size, "DYLD_INSERT_LIBRARIES=%.*s", (int)liblen, library);-+ return true;++ if (!matched && (size_t)(p - word) == namelen && namelen != 0 && liblen != 0 &&++ strncmp(word, name, namelen) == 0) {++ matched = true; /* this group names this program; its library is added once */+ }+ }++ if (matched && liblen != 0 && used + 1 + liblen < size) {++ if (any) {++ env[used++] = ':'; /* dyld takes several libraries separated by ":" */++ }++ memcpy(env + used, library, liblen);++ used += liblen;++ env[used] = 0;++ any = true;++ }+ }-+ return false;++ return any;+}+__attribute__((noinline))static intexec_extract_strings(struct image_params *imgp){-+ char inject_env[300];++ char inject_env[512];int error = 0;int ptr_size = (imgp->ip_flags & IMGPF_WAS_64BIT_ADDR) ? 8 : 4;int new_ptr_size = (imgp->ip_flags & IMGPF_IS_64BIT_ADDR) ? 8 : 4;-@@ -5759,6 +5909,33 @@ exec_extract_strings(struct image_params *imgp)+@@ -5759,6 +5923,33 @@ exec_extract_strings(struct image_params *imgp)imgp->ip_envc++;}@@ -661,7 +675,7 @@ index 1d71c66629fb..fd94fd8a588a 100644/* Save space for envv[] NULL terminator */if (imgp->ip_argspace < new_ptr_size) {error = E2BIG;-@@ -6636,7 +6813,23 @@ handle_mac_transition:+@@ -6636,7 +6827,23 @@ handle_mac_transition:}/* Update the process' identity version and set the security token */@@ -686,7 +700,7 @@ index 1d71c66629fb..fd94fd8a588a 100644task_set_uniqueid(task);set_security_token_task_internal(p, task);-@@ -7538,6 +7731,11 @@ process_signature(proc_t p, struct image_params *imgp)+@@ -7538,6 +7745,11 @@ process_signature(proc_t p, struct image_params *imgp)}done: