Skip to content

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

Sileo with the iOS volume on the flash as the root, and what it needed in RAM too

vcvkk5383e2454e5b
diff --git a/tools/mk-jb.sh b/tools/mk-jb.sh
index de975f4..04a2f40 100755
--- a/tools/mk-jb.sh
+++ b/tools/mk-jb.sh
@@ -139,6 +139,25 @@ cat > "$OUT/root/var/jb/prep.sh" <<'PREP'
#!/var/jb/bin/sh
# Run once per boot, after the prefix has been grafted onto /var/jb (rc.jobs.sileo does it).
/var/jb/usr/libexec/firmware
+# The prefix's applications: in the system image they are in /Applications already (userland/build.sh),
+# where LaunchServices finds them by itself; with the iOS volume on the flash as the root they are not,
+# and are registered from the prefix as a rootless jailbreak does it (uikittools' uicache) -- by where the
+# bundle really is, as there: in /var/jb, grafted, its files are links into the boot volume
+# (/private/preboot/jb), and LaunchServices refuses a bundle made of links ("Unable to register").
+# Asked again every five seconds, two minutes at most, aside: right after SpringBoard it may be busy.
+for app in /var/jb/Applications/*.app; do
+ [ -d "/Applications/${app##*/}" ] && continue
+ real=$(/var/jb/usr/bin/readlink "$app/Info.plist") && app=${real%/Info.plist}
+ (
+ for try in 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24; do
+ case "$(/var/jb/usr/bin/uicache -p "$app" 2>&1)" in
+ *Error*) sleep 5 ;;
+ *) echo "prep.sh: ${app##*/} registered (try $try)"; exit 0 ;;
+ esac
+ done
+ echo "prep.sh: ${app##*/} could not be registered"
+ ) &
+done
/var/jb/usr/sbin/pwd_mkdb -p /var/jb/etc/master.passwd 2>/dev/null
for package in debianutils apt dash bash zsh vi; do
[ -x "/var/jb/Library/dpkg/info/$package.postinst" ] &&
diff --git a/userland/build.sh b/userland/build.sh
index 9089c92..e534e49 100755
--- a/userland/build.sh
+++ b/userland/build.sh
@@ -78,6 +78,9 @@ IOS_JOBS=${JOBS-"
# The programs (file names) started with a library of ours inside them: "LIBRARY NAME..." per line
# (src/inject.c is libredwood.dylib, src/inject_sb.c is libredwood-sb.dylib, src/inject_media.c is
# libredwood-media.dylib).
+# 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"
INJECT=${INJECT-"/redwood/lib/libredwood.dylib backboardd containermanagerd containermanagerd_system
/redwood/lib/libredwood-sb.dylib SpringBoard
/redwood/lib/libredwood-media.dylib mediaserverd"}
@@ -226,6 +229,11 @@ if [ -d $IOS/rootfs/usr/lib ]; then
case $f in mobile/*) owner=501 ;; *) owner=0 ;; esac
printf 'f\t644\t%s\t%s\t/private/var/%s\t/var/%s\n' $owner $owner "$f" "$f"
done >> root/etc/var.skel
+ # mobile's own temporary directory, $HOME/tmp, which iOS's template does not have: an application of
+ # mobile's without a data container of its own keeps its temporary files there -- Sileo, registered from
+ # the prefix with the root on the flash, downloads every package and index there, and without it each
+ # download ended in "No such file or directory"
+ printf 'd\t700\t501\t501\t/private/var/mobile/tmp\n' >> root/etc/var.skel
cp ${RC:-rc.launchd} root/etc/rc
cp ${RC_JOBS:-rc.jobs} root/etc/rc.jobs
# QUIET=1: launchd's own standard error goes nowhere (src/init.c). It says a few thousand
@@ -327,6 +335,19 @@ if [ -n "$FLASHBOOT" ] && [ -f $CACHE/dyld_shared_cache_arm64 ]; then
mkdir -p flashboot/fw
cp ../fw-audio/aw882xx_acf.bin flashboot/fw/
fi
+ # the package manager's prefix, when tools/mk-jb.sh has fetched one: /jb of the boot volume, which is
+ # /private/preboot/jb once the iOS volume is the root -- the volume where a rootless jailbreak keeps its
+ # Procursus on an iPhone as well; init grafts it onto /var/jb (src/init.c, mount_writable)
+ if [ -d ../jb/root/var/jb ]; then
+ cp -a ../jb/root/var/jb flashboot/jb
+ mkdir -p flashboot/jb-rw # where init mounts the prefix's writable copy (src/init.c)
+ # and what Sileo runs from /bin, for init to put over the iOS volume's /bin (see the system image below)
+ mkdir -p flashboot/jb-bin
+ for c in $SILEO_BIN; do
+ ln -s /var/jb/usr/bin/$c flashboot/jb-bin/$c
+ done
+ echo " the package manager's prefix: $(du -sh flashboot/jb | cut -f1) as /jb of the boot volume"
+ fi
D=flashboot.os/System/Library/Caches/com.apple.dyld
mkdir -p $D
for f in $CACHE/dyld_shared_cache_arm64*; do
@@ -403,6 +424,13 @@ if [ -f $IOS/system.manifest ] && [ -f $CACHE/dyld_shared_cache_arm64 ] &&
for app in sysroot/jb/Applications/*.app; do
[ -d "$app" ] && cp -a "$app" sysroot/Applications/
done
+ # Sileo moves, copies and removes files as root with /bin/mv, /bin/cp, /bin/rm, /bin/mkdir and
+ # /bin/chmod -- where a jailbreak with a writable root keeps them -- and iOS's /bin has df and ps
+ # alone: every one of those spawns failed with nothing said, and the package it had downloaded never
+ # reached apt's archives ("Unable to fetch some archives"). The prefix's own programs, by those names.
+ for c in $SILEO_BIN; do
+ ln -sf /var/jb/usr/bin/$c sysroot/bin/$c
+ done
echo " the package manager's prefix: $(du -sh sysroot/jb | cut -f1) as /jb (init puts it at /var/jb)"
fi
# our audio device, where the audio server looks for such plug-ins (and for no others: Apple's are left out)
diff --git a/userland/libmini/mini.h b/userland/libmini/mini.h
index 39f0d54..4650e10 100644
--- a/userland/libmini/mini.h
+++ b/userland/libmini/mini.h
@@ -67,6 +67,7 @@ struct sockaddr_un {
#define MAP_FAILED ((void *)-1)
#define MNT_RDONLY 1
+#define MNT_UNION 0x00000020 /* what is not in the mounted directory is found in the one below */
#define S_IFMT 0170000
#define S_IFDIR 0040000
diff --git a/userland/rc.jobs.jb b/userland/rc.jobs.jb
new file mode 100644
index 0000000..02cc92d
--- /dev/null
+++ b/userland/rc.jobs.jb
@@ -0,0 +1,9 @@
+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
+# The package manager for a person to use (RC_JOBS=rc.jobs.jb): nothing pressed by a script -- the
+# preparation the bootstrap wants once per boot, the network, a fresh index, and the phone is yours
+# for the session (XL_SECS). What is installed lives in memory, as all of /private/var: gone at the
+# next boot.
+/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
diff --git a/userland/rc.jobs.jbreg b/userland/rc.jobs.jbreg
new file mode 100644
index 0000000..0087943
--- /dev/null
+++ b/userland/rc.jobs.jbreg
@@ -0,0 +1,15 @@
+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
+# Whether LaunchServices registers an application that a package puts in /var/jb/Applications -- the
+# memory filesystem the package manager's prefix is grafted on -- with the root on the flash
+# (RC_JOBS=rc.jobs.jbreg, FLASHBOOT=1 CRYPTEXES=flash, EXTRA=<a package with an application>.deb; with
+# systrace_name=lsd and OSLOG with com.apple.lsd for LaunchServices' side): the package installed by
+# dpkg, and its application registered by uicache, as Sileo does it after an install.
+/redwood/bin/sound_test springboard
+/var/jb/bin/sh /var/jb/prep.sh
+/redwood/bin/rwtouch wait 40
+/var/jb/usr/bin/dpkg -i /redwood/bin/trollstorehelper.deb
+/var/jb/usr/bin/ls -la /var/jb/Applications/TrollStorePersistenceHelper.app
+/var/jb/usr/bin/uicache -p /var/jb/Applications/TrollStorePersistenceHelper.app
+/redwood/bin/rwtouch wait 5
+/var/jb/usr/bin/uicache -p /var/jb/Applications/TrollStorePersistenceHelper.app
+/redwood/bin/rwtouch wait 5
diff --git a/userland/rc.jobs.sileo b/userland/rc.jobs.sileo
index 60ca8fa..7a3c20d 100644
--- a/userland/rc.jobs.sileo
+++ b/userland/rc.jobs.sileo
@@ -1,12 +1,15 @@
# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).
# Sileo, the package manager (RC_JOBS=rc.jobs.sileo): the prefix is grafted onto /var/jb by init
-# (tools/mk-jb.sh puts it in the image). The preparation the bootstrap wants once per boot, a fresh
-# index, and then the whole way a person goes: the package's own page is opened by URL (no typing),
+# (tools/mk-jb.sh puts it in the image). The preparation the bootstrap wants once per boot, the
+# network (Wi-Fi has an address and a name resolves: before that, apt finds no repository), a fresh
+# index, the half minute LaunchServices wants before it registers the prefix's applications (with the root
+# on the flash: prep.sh, uicache), and then the whole way a person goes: the package's own page is opened by URL (no typing),
# GET puts it in the queue, Confirm installs it. At the end, what dpkg has to say about 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
-/redwood/bin/rwtouch wait 4 swipe 0.5 0.985 0.5 0.3 200 wait 6
+/redwood/bin/rwtouch wait 25 swipe 0.5 0.985 0.5 0.3 200 wait 6
/var/jb/usr/bin/uiopen sileo://package/tree
/redwood/bin/rwtouch wait 14 tap 0.32 0.59 wait 3 tap 0.5 0.826 wait 6
/redwood/bin/rwtouch tap 0.86 0.303 wait 5 tap 0.2 0.862 wait 6 tap 0.5 0.85 wait 60
diff --git a/userland/src/init.c b/userland/src/init.c
index 97a4479..3673c8d 100644
--- a/userland/src/init.c
+++ b/userland/src/init.c
@@ -202,6 +202,19 @@ flash_root(const char *dev)
/* the audio DSP's firmware and the speakers' configuration, from the RAM image as in a RAM boot */
check(synthetic_dir("/flash/fw") && bind_dir("/fw", "/flash/fw"), "the firmware on /fw (adsp=1, sound=N)");
}
+ if (stat("/jb-bin", &fw_st) == 0) {
+ /*
+ * The package manager's prefix is in the boot volume (/jb), and Sileo runs mv, cp, rm, mkdir
+ * and chmod from /bin, where iOS has df and ps alone (userland/build.sh, SILEO_BIN): the boot
+ * volume's jb-bin -- those five, links into the prefix -- over the volume's /bin as a union
+ * mount, through which what it does not have is found in the /bin below.
+ */
+ ok = mount("bindfs", "/flash/bin", MNT_RDONLY | MNT_UNION, (void *)"/jb-bin") == 0;
+ if (!ok) {
+ printf(" bindfs /jb-bin on /flash/bin, union: %s\n", strerror(errno));
+ }
+ check(ok, "Sileo's programs in /bin, over the volume's own");
+ }
fd = open("/flash/private/var/staged_system_apps", O_RDONLY | O_DIRECTORY, 0);
while (fd >= 0 && (n = getdirentries(fd, buf, sizeof(buf), &pos)) > 0) {
@@ -628,8 +641,9 @@ own_tree(const char *path, int uid)
/*
* The rootless prefix of a package manager, /var/jb: the Procursus bootstrap with Sileo in it
- * (tools/mk-jb.sh) rides in the system image as /jb, which is a packfs image and cannot be written
- * to, while dpkg and apt must be able to write anywhere in their own prefix.
+ * (tools/mk-jb.sh) rides in the system image as /jb (or in the boot volume, /private/preboot/jb,
+ * with the root on the flash), an APFS image that cannot be written to, while dpkg and apt must be
+ * able to write anywhere in their own prefix.
*
* Copying all 124 MB of it into the memory filesystem is more than this machine has to spare (the
* system image is already 2.8 GB of its 4.3 GB), so the prefix is grafted instead: every directory
@@ -772,6 +786,7 @@ mount_writable(void)
static const char *const markers[] = { "/private/var/db/diagnostics/.migrated",
"/private/var/db/diagnostics/timesync/.migrated", "/private/var/db/uuidtext/.migrated" };
int ok, fd, made = 0, files = 0;
+ const char *jb;
struct stat st;
ssize_t n;
@@ -831,9 +846,34 @@ mount_writable(void)
printf(" %d directories, %d files\n", made, files);
check(ok && made > 50, "create the directories of the data volume");
- /* and the package manager's prefix, when the image was built with one (tools/mk-jb.sh) */
- if (ok && stat("/jb", &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR) {
- int dirs = 0, entries = 0, grafted = graft_tree("/jb", "/private/var/jb", 1, &dirs, &entries);
+ /*
+ * and the package manager's prefix, when the image was built with one (tools/mk-jb.sh): /jb of
+ * the system image in RAM, or, with the iOS volume on the flash as the root, /private/preboot/jb
+ * -- the boot volume, where a rootless jailbreak on an iPhone keeps its Procursus too
+ * (/var/jb leads into /private/preboot there)
+ */
+ jb = stat("/jb", &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR ? "/jb" :
+ stat("/private/preboot/jb", &st) == 0 && (st.st_mode & S_IFMT) == S_IFDIR ? "/private/preboot/jb" : NULL;
+ if (ok && jb != NULL) {
+ const char *to = "/private/var/jb";
+ int dirs = 0, entries = 0, grafted;
+
+ /*
+ * With the root on the flash, the prefix's writable copy goes into a memory filesystem of its own
+ * on the boot volume's jb-rw, as procursus, and /var/jb is a link to it: where /var/jb leads on
+ * an iPhone with a rootless jailbreak, into /private/preboot. LaunchServices registers an
+ * application from there, and from /private/var not at all -- everything a package put in
+ * /var/jb/Applications was "Unable to register", with nothing on the files even looked at.
+ */
+ if (strcmp(jb, "/private/preboot/jb") == 0 && stat("/private/preboot/jb-rw", &st) == 0) {
+ if (mount("ramfs", "/private/preboot/jb-rw", 0, NULL) == 0 &&
+ symlink("/private/preboot/jb-rw/procursus", "/private/var/jb") == 0) {
+ to = "/private/preboot/jb-rw/procursus";
+ } else {
+ printf(" the prefix on /private/preboot/jb-rw: %s\n", strerror(errno));
+ }
+ }
+ grafted = graft_tree(jb, to, 1, &dirs, &entries);
/*
* All of it belongs to root, as it does on a phone, but for the user's corner of it: an
@@ -856,7 +896,7 @@ mount_writable(void)
grafted = chown(mobiles[i], 501, 501) == 0 && grafted;
}
grafted = own_tree("/private/var/jb/var/mobile", 501) && grafted;
- printf(" /var/jb: %d directories, %d files and links\n", dirs, entries);
+ printf(" /var/jb from %s, in %s: %d directories, %d files and links\n", jb, to, dirs, entries);
check(grafted, "graft the package manager's prefix onto the data volume");
}
}
diff --git a/userland/src/inject_app.c b/userland/src/inject_app.c
index facc0fe..08960e3 100644
--- a/userland/src/inject_app.c
+++ b/userland/src/inject_app.c
@@ -134,6 +134,68 @@ keep_to_the_repositories(void)
}
}
+/*
+ * apt-get install, not its simulation (-s...): the one that ends in "Unable to fetch some archives"
+ * when Sileo runs it and installs the package when a terminal runs the same command.
+ */
+static int
+apt_install(char *const argv[])
+{
+ int install = 0;
+
+ if (argv == NULL || argv[0] == NULL || strcmp(argv[0], "apt-get") != 0) {
+ return 0;
+ }
+ for (int i = 1; argv[i] != NULL; i++) {
+ if (argv[i][0] == '-' && argv[i][1] == 's') {
+ return 0;
+ }
+ install |= strcmp(argv[i], "install") == 0;
+ }
+ return install;
+}
+
+/*
+ * What apt says goes to Sileo, which shows a line of it at most. Run the same apt-get, with the same
+ * descriptors, credential and environment, under a shell that first says on the console who it runs
+ * as, what lies in apt's archives and what Sileo's index says of the package, and afterwards how apt
+ * ended and what the archives hold then. This is how "Unable to fetch some archives" was found: the
+ * archives were empty, Sileo's /bin/mv having never run.
+ */
+static int
+spawn_apt_said(pid_t *pid, const posix_spawn_file_actions_t *actions, const posix_spawnattr_t *attr,
+ char *const argv[], char *const envp[])
+{
+ static char script[] =
+ "exec 7>/dev/console; J=/var/jb\n"
+ "echo \"redwood: apt-get runs as $($J/usr/bin/id), in $(pwd)\" >&7\n"
+ "$J/bin/ls -ln $J/var/cache/apt/archives $J/var/lib/apt/sileolists >&7 2>&1\n"
+ "for a in \"$@\"; do case $a in -*|install) ;; *) p=${a%%=*}\n"
+ " echo \"redwood: $p in sileolists:\" >&7\n"
+ " $J/usr/bin/grep -h -A30 \"^Package: $p\\$\" $J/var/lib/apt/sileolists/*Packages 2>&1 | $J/usr/bin/head -40 >&7 ;; esac; done\n"
+ "$J/usr/bin/apt-get \"$@\"\n"
+ "s=$?; echo \"redwood: apt-get exits $s; its archives after:\" >&7\n"
+ "$J/bin/ls -ln $J/var/cache/apt/archives >&7 2>&1; exit $s\n";
+ char *wrapped[96];
+ int k = 0;
+
+ for (int i = 0; envp != NULL && envp[i] != NULL; i++) {
+ char line[600];
+ int n = snprintf(line, sizeof(line), "redwood: %s: apt-get's environment: %s\n", who, envp[i]);
+
+ say(line, (size_t)(n < (int)sizeof(line) ? n : (int)sizeof(line) - 1));
+ }
+ wrapped[k++] = "sh";
+ wrapped[k++] = "-c";
+ wrapped[k++] = script;
+ wrapped[k++] = "apt-get";
+ for (int i = 1; argv[i] != NULL && k < 95; i++) {
+ wrapped[k++] = argv[i];
+ }
+ wrapped[k] = NULL;
+ return posix_spawn(pid, "/var/jb/bin/sh", actions, attr, wrapped, envp);
+}
+
/*
* Every program the application starts, with its arguments. Sileo does everything to the system by
* spawning its set-user-ID helper, which execs apt or dpkg; the arguments are the whole question
@@ -167,7 +229,89 @@ watched_spawn(pid_t *pid, const char *path, const posix_spawn_file_actions_t *ac
}
n += snprintf(line + n, sizeof(line) - (size_t)n, "\n");
say(line, (size_t)n);
- return posix_spawn(pid, path, actions, attr, argv, envp);
+ if (apt_install(argv)) {
+ return spawn_apt_said(pid, actions, attr, argv, envp);
+ }
+ n = posix_spawn(pid, path, actions, attr, argv, envp);
+ if (n != 0) {
+ char said[300];
+ int k = snprintf(said, sizeof(said), "redwood: %s: ... and that spawn failed: %s\n", who, strerror(n));
+
+ say(said, (size_t)(k < (int)sizeof(said) ? k : (int)sizeof(said) - 1));
+ }
+ return n;
+}
+
+/*
+ * A file the application moves or copies itself (NSFileManager, under Swift's FileManager), when it
+ * fails: from where, to where, and what the error says. Sileo moves each download into place this
+ * way, and says only "The operation couldn't be completed. No such file or directory".
+ */
+static const char *
+text_of(id object)
+{
+ id (*get)(id, SEL) = (id (*)(id, SEL))objc_msgSend;
+ const char *(*utf8)(id, SEL) = (const char *(*)(id, SEL))objc_msgSend;
+ id text;
+
+ if (object == NULL) {
+ return "(nil)";
+ }
+ text = (class_respondsToSelector(object_getClass(object), sel_registerName("path")) ?
+ get(object, sel_registerName("path")) : get(object, sel_registerName("description")));
+ return text != NULL ? utf8(text, sel_registerName("UTF8String")) : "?";
+}
+
+static signed char (*move_url)(id, SEL, id, id, id *), (*copy_url)(id, SEL, id, id, id *);
+static signed char (*move_path)(id, SEL, id, id, id *), (*copy_path)(id, SEL, id, id, id *);
+
+static signed char
+file_said(const char *what, signed char ok, id from, id to, id error)
+{
+ if (!ok) {
+ char line[1400];
+ int n = snprintf(line, sizeof(line), "redwood: %s: %s %s -> %s failed: %s\n", who, what, text_of(from),
+ text_of(to), error != NULL ? text_of(error) : "(no error given)");
+
+ say(line, (size_t)(n < (int)sizeof(line) ? n : (int)sizeof(line) - 1));
+ }
+ return ok;
+}
+
+static signed char
+watched_move_url(id self, SEL cmd, id from, id to, id *error)
+{
+ id mine = NULL;
+ signed char ok = move_url(self, cmd, from, to, error != NULL ? error : &mine);
+
+ return file_said("moveItemAtURL", ok, from, to, error != NULL ? *error : mine);
+}
+
+static signed char
+watched_copy_url(id self, SEL cmd, id from, id to, id *error)
+{
+ id mine = NULL;
+ signed char ok = copy_url(self, cmd, from, to, error != NULL ? error : &mine);
+
+ return file_said("copyItemAtURL", ok, from, to, error != NULL ? *error : mine);
+}
+
+static signed char
+watched_move_path(id self, SEL cmd, id from, id to, id *error)
+{
+ id mine = NULL;
+ signed char ok = move_path(self, cmd, from, to, error != NULL ? error : &mine);
+
+ return file_said("moveItemAtPath", ok, from, to, error != NULL ? *error : mine);
+}
+
+static signed char
+watched_copy_path(id self, SEL cmd, id from, id to, id *error)
+{
+ id mine = NULL;
+ signed char ok = copy_path(self, cmd, from, to, error != NULL ? error : &mine);
+
+ return file_said("copyItemAtPath", ok, from, to, error != NULL ? *error : mine);
}
__attribute__((used)) static struct {
@@ -227,6 +371,10 @@ redwood_app_init(void)
* would leave ours as the "original" of itself -- a call that never returns.
*/
watch("NSURLSessionTask", "resume", (IMP)watched_resume_task, &resume_task);
+ watch("NSFileManager", "moveItemAtURL:toURL:error:", (IMP)watched_move_url, &move_url);
+ watch("NSFileManager", "copyItemAtURL:toURL:error:", (IMP)watched_copy_url, &copy_url);
+ watch("NSFileManager", "moveItemAtPath:toPath:error:", (IMP)watched_move_path, &move_path);
+ watch("NSFileManager", "copyItemAtPath:toPath:error:", (IMP)watched_copy_path, &copy_path);
n = snprintf(line, sizeof(line), "redwood: %s: started as uid %d (effective %d), watching for what kills it\n",
name, (int)getuid(), (int)geteuid());
say(line, (size_t)(n > 0 ? n : 0));