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
iOS with its root on the phone's flash: the root container read only as /, an ordinary session
vcvkk4711a63a0988
diff --git a/userland/build.sh b/userland/build.shindex efbd2ce..d69efb5 100755--- a/userland/build.sh+++ b/userland/build.sh@@ -264,8 +264,42 @@ if [ -n "$ADSP" ] && [ -f ../fw-audio/adsp.mdt ]; thenmkdir -p root/fwinstall -m 0644 ../fw-audio/adsp.mdt ../fw-audio/adsp.b0[2-9] ../fw-audio/adsp.b[1-9][0-9] root/fw/fi+# When the iOS volume on the phone's flash is to become the root (boot-arg root_apfs=ufs_darwin,+# with flashboot.packfs resident; src/init.c, flash_root): an empty directory to hide iOS's+# NanoLaunchDaemons and LaunchAngels with,+# xpc without launchd.plist (launchd would read that list of all of iOS's jobs instead of+# LaunchDaemons) and the audio server's plug-in directory with only our device in it.+mkdir -p root/empty root/xpc root/hal+for f in extensions.plist extensions.plist.sig; do+ [ -f $IOS/rootfs/System/Library/xpc/$f ] && cp $IOS/rootfs/System/Library/xpc/$f root/xpc/+done+[ -d root/lib/RedwoodAudio.driver ] && ln -s /redwood/lib/RedwoodAudio.driver root/hal/RedwoodAudio.driver../mkpackfs.py -o root.packfs root+# --- the RAM part of a boot off the flash: FLASHBOOT=1 -> flashboot.packfs ------------------------------+# With the iOS volume on the phone's flash as the root, all that has to come from RAM is what is not+# on it -- the OS cryptex with the dyld shared cache -- and what starts the boot. This image is the+# root the kernel starts from (rd=md0, the resident image): stage0 as /sbin/launchd mounts the small+# image on /redwood and runs its init, which with boot-arg root_apfs=DEV mounts the volume on /flash,+# makes it the root and moves this image to the volume's /private/preboot -- where the volume's own+# /System/Cryptexes/OS points, and where the kernel lets a shared cache come from (src/init.c,+# flash_root). Upload it with `xl ramdisk userland/flashboot.packfs`.+if [ -n "$FLASHBOOT" ] && [ -f $CACHE/dyld_shared_cache_arm64 ]; then+ rm -rf flashboot+ mkdir -p flashboot/sbin flashboot/redwood flashboot/dev flashboot/flash flashboot/Cryptexes+ cp obj/stage0 flashboot/sbin/launchd+ if [ -d $IOS/cryptex ]; then cp -al $IOS/cryptex flashboot/Cryptexes/OS; else mkdir -p flashboot/Cryptexes/OS; fi+ D=flashboot/Cryptexes/OS/System/Library/Caches/com.apple.dyld+ mkdir -p $D+ for f in $CACHE/dyld_shared_cache_arm64*; do+ case "$f" in *.symbols) continue ;; esac+ ln "$f" $D/+ done+ if [ -n "$PLAIN" ]; then pack="-b 4096 -a 16384"; elif [ -n "$ZSTD" ]; then pack="--zstd"; else pack="-z"; fi+ ../mkpackfs.py $pack -o flashboot.packfs flashboot+ rm -rf flashboot+fi+# --- the system image -----------------------------------------------------------------------------if [ -f $IOS/system.manifest ] && [ -f $CACHE/dyld_shared_cache_arm64 ] &&{ [ ! -f system.packfs ] || [ obj/stage0 -nt system.packfs ] || [ $IOS/system.manifest -nt system.packfs ] ||diff --git a/userland/rc.jobs.flashroot b/userland/rc.jobs.flashrootnew file mode 100644index 0000000..b03bcd3--- /dev/null+++ b/userland/rc.jobs.flashroot@@ -0,0 +1,18 @@+# Commands run by the launchd job com.redwood.demo (see com.redwood.demo.plist).+# The iOS root volume on the phone's flash as the root (flashboot.packfs resident,+# root_apfs=ufs_darwin ufs=2 bootcpu=7; src/init.c, flash_root), seen from user space: what+# is mounted where, the root's own files read through read(2) and through a mapping.+# The host's hashes: SystemVersion.plist 47687daabd864929, usr/lib/dyld 602fba9751ad6aa7,+# usr/sbin/mediaserverd 895037cd97f96801.+/sbin/mount+/bin/df+/redwood/bin/vol_ls fs /+/redwood/bin/vol_ls fs /private/preboot+/redwood/bin/vol_ls ls /+/redwood/bin/vol_ls ls /Applications+/redwood/bin/vol_ls ls /System/Cryptexes/OS/System/Library/Caches/com.apple.dyld /redwood+/redwood/bin/vol_ls cat /System/Library/CoreServices/SystemVersion.plist+/redwood/bin/vol_ls hash /System/Library/CoreServices/SystemVersion.plist /usr/lib/dyld /usr/sbin/mediaserverd+/redwood/bin/apfs_mmap_test mmap /usr/sbin+/redwood/bin/apfs_mmap_test read /usr/sbin 65536+/bin/ps -Adiff --git a/userland/src/init.c b/userland/src/init.cindex 6111abf..6c83a7f 100644--- a/userland/src/init.c+++ b/userland/src/init.c@@ -34,6 +34,177 @@ in_image(const char *path)return buf;}+/* ---- the iOS root volume on the phone's flash as the root (boot-arg root_apfs=DEV) ---- */++/* the root is the iOS volume: its /sbin/launchd is Apple's, there is no launchd.ios */+static int flash_root_mode;++static void check(int ok, const char *what);++/* dst = a + b + c (each call's result only until the next) */+static const char *+join(const char *a, const char *b, const char *c)+{+ static char buf[1200];+ size_t la = strlen(a), lb = strlen(b), lc = strlen(c);++ if (la + lb + lc >= sizeof(buf)) {+ buf[0] = 0;+ return buf;+ }+ memcpy(buf, a, la);+ memcpy(buf + la, b, lb);+ memcpy(buf + la + lb, c, lc + 1);+ return buf;+}++/* The value of boot-arg NAME on the kernel's command line, or NULL. */+static const char *+boot_arg(const char *name)+{+ static char args[1024], value[128];+ size_t len = sizeof(args) - 1, n = strlen(name);++ if (sysctlbyname("kern.bootargs", args, &len, NULL, 0) != 0) {+ return NULL;+ }+ args[len < sizeof(args) ? len : sizeof(args) - 1] = 0;+ for (char *p = args; *p;) {+ while (*p == ' ') {+ p++;+ }+ if (strncmp(p, name, n) == 0 && p[n] == '=') {+ size_t i = 0;++ for (p += n + 1; *p && *p != ' ' && i < sizeof(value) - 1; p++) {+ value[i++] = *p;+ }+ value[i] = 0;+ return value;+ }+ while (*p && *p != ' ') {+ p++;+ }+ }+ return NULL;+}++/* A directory of this image (or anything else) shown in place of one of the volume's: bindfs. */+static int+bind_dir(const char *from, const char *to)+{+ int ok = mount("bindfs", to, MNT_RDONLY, (void *)from) == 0;++ if (!ok) {+ printf(" bindfs %s on %s: %s\n", from, to, strerror(errno));+ }+ return ok;+}++/* An empty directory that is not on the volume, for a mount point (sysctl kern.apfs_synthetic). */+static int+synthetic_dir(const char *path)+{+ int ok = sysctlbyname("kern.apfs_synthetic", NULL, NULL, (void *)path, strlen(path)) == 0;++ if (!ok) {+ printf(" synthetic directory %s: %s\n", path, strerror(errno));+ }+ return ok;+}++/*+ * root_apfs=DEV: the root is to be the iOS root volume on /dev/DEV -- the phone's flash, read+ * only. The kernel has started off flashboot.packfs (the resident image, md0: stage0, the OS+ * cryptex with the dyld shared cache, mount points) and stage0 has mounted this image on+ * /redwood. What the big system image changes in iOS (userland/build.sh) is put in place on+ * the volume instead, without writing to it:+ * - our choice of launchd jobs instead of iOS's 500 (this image's LaunchDaemons, and+ * nothing for NanoLaunchDaemons, LaunchAngels, nor xpc's launchd.plist, which launchd+ * would read instead of the directory);+ * - only our audio device among the audio server's plug-ins;+ * - the applications iOS installs on its first boot (private/var/staged_system_apps)+ * among the system's own in /Applications: another name for each there (a synthetic+ * entry of the volume -- not a mount, which whatever registers the system's+ * applications passed over), made before /private/var is covered by a memory+ * filesystem.+ * Then the volume becomes the root (sysctl kern.redwood_switch_root) and flashboot.packfs+ * goes to its /private/preboot: the volume's /System/Cryptexes/OS points to+ * /private/preboot/Cryptexes/OS, and the kernel takes a shared cache from there. This+ * image goes along with it, to /private/preboot/redwood, and is bound back on /redwood, a+ * synthetic directory of the volume. The boot-args stay those of a RAM disk (rd=md0),+ * which is what launchd reads to leave out the boot tasks of a real device.+ */+static void+flash_root(const char *dev)+{+ static char buf[8192];+ char devpath[96], name[256], from[800], to[400];+ off_t pos = 0;+ ssize_t n;+ int fd, ok, apps = 0, bound = 0;++ printf("\n-- the iOS root volume on /dev/%s as the root --\n", dev);+ strcpy(devpath, join("/dev/", dev, ""));+ ok = mount("apfs", "/flash", MNT_RDONLY, devpath) == 0;+ if (!ok) {+ printf(" mount %s: %s\n", devpath, strerror(errno));+ }+ check(ok, "mount the iOS volume read only");+ if (!ok) {+ return;+ }+ check(synthetic_dir("/flash/redwood"), "a synthetic /redwood on it, for this image");+ check(bind_dir(in_image("/LaunchDaemons"), "/flash/System/Library/LaunchDaemons") &&+ bind_dir(in_image("/empty"), "/flash/System/Library/NanoLaunchDaemons") &&+ bind_dir(in_image("/empty"), "/flash/System/Library/LaunchAngels") &&+ bind_dir(in_image("/xpc"), "/flash/System/Library/xpc"), "our launchd jobs, not iOS's");+ check(bind_dir(in_image("/hal"), "/flash/System/Library/Audio/Plug-Ins/HAL"),+ "our audio device, not Apple's plug-ins");++ fd = open("/flash/private/var/staged_system_apps", O_RDONLY | O_DIRECTORY, 0);+ while (fd >= 0 && (n = getdirentries(fd, buf, sizeof(buf), &pos)) > 0) {+ for (char *p = buf; p < buf + n;) {+ struct dirent *de = (struct dirent *)(void *)p;+ size_t len = de->d_namlen;++ if (de->d_reclen == 0) {+ break;+ }+ p += de->d_reclen;+ if (len < 5 || len >= sizeof(name) || strncmp(de->d_name + len - 4, ".app", 4) != 0) {+ continue;+ }+ memcpy(name, de->d_name, len);+ name[len] = 0;+ apps++;+ /* another name for it in /Applications, on the same volume: no mount point to skip */+ strcpy(to, join("/flash/Applications/", name, "="));+ strcpy(from, join(to, "/flash/private/var/staged_system_apps/", name));+ if (synthetic_dir(from)) {+ bound++;+ }+ }+ }+ if (fd >= 0) {+ close(fd);+ }+ printf(" %d applications waiting to be installed, %d of them in /Applications\n", apps, bound);+ check(apps > 0 && bound == apps, "the first boot's applications in /Applications");++ ok = sysctlbyname("kern.redwood_switch_root", NULL, NULL, "/flash private/preboot", 22) == 0;+ if (!ok) {+ printf(" switching the root: %s\n", strerror(errno));+ }+ check(ok, "the iOS volume is the root, the RAM part of the boot on /private/preboot");+ if (!ok) {+ return;+ }+ chdir("/");+ check(bind_dir("/private/preboot/redwood", "/redwood"), "this image back on /redwood");+ flash_root_mode = 1;+}+static voidcheck(int ok, const char *what){@@ -606,6 +777,9 @@ run_command(char *line)/* "exec program args": become that program, keeping process number 1 */if (strcmp(argv[0], "exec") == 0 && argc > 1) {+ if (flash_root_mode && strcmp(argv[1], "/sbin/launchd.ios") == 0) {+ argv[1] = "/sbin/launchd"; /* the root is iOS's own volume: its launchd is that */+ }execve(argv[1], argv + 1, envp);printf("%s: %s\n", argv[1], strerror(errno));return;@@ -799,6 +973,13 @@ main(int argc, char **argv, char **envp)}printf("\n=== init: process %d, started as %s ===\n", getpid(), argc > 0 ? argv[0] : "?");+ {+ const char *dev = boot_arg("root_apfs");++ if (dev != NULL) {+ flash_root(dev);+ }+ }show_system();mount_writable();diff --git a/xnu/files/bsd/dev/redwood_apfs_check.c b/xnu/files/bsd/dev/redwood_apfs_check.cindex 25f8d21..3dc638a 100644--- a/xnu/files/bsd/dev/redwood_apfs_check.c+++ b/xnu/files/bsd/dev/redwood_apfs_check.c@@ -77,6 +77,7 @@#include <sys/vnode_if.h>#include <sys/vnode_internal.h>#include <sys/redwood_input.h>+#include <sys/sysctl.h>#include <pexpert/pexpert.h>#include <libkern/OSAtomic.h>#include <sys/redwood_apfs_check.h>@@ -945,6 +946,58 @@ apfs_check_runner(void *arg, wait_result_t wr)apfs_check_finished = 1;}+/*+ * sysctl kern.redwood_switch_root="INCOMING OUTGOING": the volume mounted on INCOMING+ * becomes the root, and what was the root until now is mounted on OUTGOING inside it (a+ * path without a leading slash, which must be a directory there -- on the iOS volume, a+ * synthetic one). devfs stays on /dev (vfs_switch_root() keeps it there). This is how+ * the iOS root volume on the phone's flash becomes the root: the kernel starts from the+ * small image, whose init mounts the volume, puts in place what the system needs on it+ * and then asks for this (userland/src/init.c, boot-arg root_apfs=). Process 1 only, once.+ */+static int redwood_root_switched;++static int+redwood_sysctl_switch_root SYSCTL_HANDLER_ARGS+{+#pragma unused(oidp, arg1, arg2)+ char buf[512], *outgoing;+ int error;++ if (req->newptr == USER_ADDR_NULL) {+ return SYSCTL_OUT(req, redwood_root_switched ? "1" : "0", 2);+ }+ if (proc_getpid(current_proc()) != 1 || suser(kauth_cred_get(), NULL) != 0) {+ return EPERM;+ }+ if (redwood_root_switched) {+ return EBUSY;+ }+ if (req->newlen == 0 || req->newlen >= sizeof(buf)) {+ return EINVAL;+ }+ error = SYSCTL_IN(req, buf, req->newlen);+ if (error) {+ return error;+ }+ buf[req->newlen] = '\0';+ outgoing = strchr(buf, ' ');+ if (outgoing == NULL || buf[0] != '/') {+ return EINVAL;+ }+ *outgoing++ = '\0';+ error = vfs_switch_root(buf, outgoing, 0);+ printf("redwood: %s is the root now, the one before it on /%s: %d\n", buf, outgoing, error);+ if (error == 0) {+ redwood_root_switched = 1;+ }+ return error;+}++SYSCTL_PROC(_kern, OID_AUTO, redwood_switch_root, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_LOCKED,+ NULL, 0, redwood_sysctl_switch_root, "A",+ "write \"INCOMING OUTGOING\": the volume on INCOMING becomes the root (process 1, once)");+/** apfs_volume=DEV: a volume for user space in an ordinary boot. Mounted read only (and* nosuid, nodev) on apfs_volume_at -- by default /System/Volumes/iOS, which the system imagediff --git a/xnu/files/bsd/miscfs/apfs/apfs.h b/xnu/files/bsd/miscfs/apfs/apfs.hindex 740d5d4..d102904 100644--- a/xnu/files/bsd/miscfs/apfs/apfs.h+++ b/xnu/files/bsd/miscfs/apfs/apfs.h@@ -43,6 +43,21 @@ struct apfs_node;#define APFS_ROUND_SHARED (128 * 1024)#define APFS_IOBUF_KEEP 8+#define APFS_SYNTH_MAX 64+#define APFS_SYNTH_NAMELEN 96+/*+ * Object ids from here up are synthetic: the last few of the 60 bits an id has. (Not+ * 0x0FFFFFFF00000000: the iOS root volume numbers its own objects from there.)+ */+#define APFS_SYNTH_BASE 0x0FFFFFFFFFFFF000ULL++struct apfs_synth {+ uint64_t parent; /* the directory it is in (a real one, or synthetic) */+ uint64_t id; /* its own, or the directory it is another name for */+ bool alias; /* another name for a directory of the volume */+ char name[APFS_SYNTH_NAMELEN];+};+struct apfs_mount {mount_t am_mp;vnode_t am_devvp;@@ -75,6 +90,16 @@ struct apfs_mount {void *am_iobuf[APFS_IOBUF_KEEP];uint32_t am_iobuf_kept;uint32_t am_iobuf_size;+ /*+ * Synthetic directories: empty directories that are not on the volume, for mount+ * points on a volume nobody can add a directory to -- what macOS's synthetic.conf is+ * for its sealed system volume. Added through sysctl kern.apfs_synthetic; seen by+ * lookup and readdir of their parent, empty, owned by root. Or another name for a+ * directory of the same volume ("PATH=TARGET"): the directory itself, with PATH's+ * parent for its "..". Under am_lock.+ */+ struct apfs_synth *am_synth; /* APFS_SYNTH_MAX of them, made at the first */+ uint32_t am_nsynth;};/* The private part of a vnode: which object it is. */@@ -104,6 +129,9 @@ struct apfs_node {};#define APFS_MP(mp) ((struct apfs_mount *)vfs_fsprivate(mp))+const struct apfs_synth *apfs_synth_find(struct apfs_mount *am, uint64_t parent,+ const char *name, size_t len); /* with am_lock held */+#define APFS_VP(vp) ((struct apfs_node *)vnode_fsnode(vp))#define APFS_VP_MOUNT(vp) (APFS_VP(vp)->an_mount)diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.cindex e47d95d..0a60fa7 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_vfsops.c@@ -309,13 +309,42 @@ apfs_node_vnode(struct apfs_mount *am, uint64_t obj_id, vnode_t dvp,}}- error = apfs_inode_get(&am->am_vol, obj_id, &ino);- if (error == 0) {- error = apfs_inode_size(&am->am_vol, &ino, &size);- if (error) {- size = ino.size; /* the stream's size is still better than nothing */+ if (obj_id >= APFS_SYNTH_BASE) {+ /* a synthetic directory: an empty one, root's, made up here */+ const struct apfs_synth *sy = NULL;+ uint32_t i;++ for (i = 0; i < am->am_nsynth; i++) {+ if (am->am_synth[i].id == obj_id) {+ sy = &am->am_synth[i];+ }+ }+ bzero(&ino, sizeof(ino));+ if (sy == NULL) {+ error = ENOENT;+ } else {+ ino.obj_id = obj_id;+ ino.parent_id = sy->parent;+ ino.mode = APFS_S_IFDIR | 0755;error = 0;}+ } else {+ error = apfs_inode_get(&am->am_vol, obj_id, &ino);+ if (error == 0) {+ uint32_t i;++ error = apfs_inode_size(&am->am_vol, &ino, &size);+ if (error) {+ size = ino.size; /* the stream's size is still better than nothing */+ error = 0;+ }+ /* a directory with another name: ".." is where that name is */+ for (i = 0; i < am->am_nsynth; i++) {+ if (am->am_synth[i].alias && am->am_synth[i].id == obj_id) {+ ino.parent_id = am->am_synth[i].parent;+ }+ }+ }}lck_mtx_unlock(&am->am_lock);if (error) {@@ -377,6 +406,146 @@ apfs_node_vnode(struct apfs_mount *am, uint64_t obj_id, vnode_t dvp,return 0;}+/* A synthetic directory by where it is and its name; with am_lock held. */+const struct apfs_synth *+apfs_synth_find(struct apfs_mount *am, uint64_t parent, const char *name, size_t len)+{+ uint32_t i;++ for (i = 0; i < am->am_nsynth; i++) {+ if (am->am_synth[i].parent == parent && strlen(am->am_synth[i].name) == len &&+ memcmp(am->am_synth[i].name, name, len) == 0) {+ return &am->am_synth[i];+ }+ }+ return NULL;+}++/*+ * sysctl kern.apfs_synthetic=PATH: a synthetic directory at PATH, whose parent must be a+ * directory of an APFS volume (real or synthetic) and which must not exist yet.+ * PATH=TARGET: at PATH, another name for the directory TARGET of the same volume. Root+ * only; nothing ever takes one away again but unmounting.+ */+static int+apfs_sysctl_synthetic SYSCTL_HANDLER_ARGS+{+#pragma unused(oidp, arg1, arg2)+ char path[MAXPATHLEN], name[APFS_SYNTH_NAMELEN], *target;+ vnode_t dvp = NULLVP, vp = NULLVP, tvp = NULLVP;+ struct apfs_mount *am;+ struct apfs_node *dn;+ uint64_t target_id = 0;+ const char *slash;+ size_t len;+ int error;++ if (req->newptr == USER_ADDR_NULL) {+ return SYSCTL_OUT(req, "", 1); /* reading it says nothing */+ }+ if ((error = suser(kauth_cred_get(), NULL)) != 0) {+ return error;+ }+ if (req->newlen == 0 || req->newlen >= sizeof(path)) {+ return EINVAL;+ }+ error = SYSCTL_IN(req, path, req->newlen);+ if (error) {+ return error;+ }+ path[req->newlen] = '\0';+ target = strchr(path, '=');+ if (target != NULL) {+ *target++ = '\0';+ error = vnode_lookup(target, 0, &tvp, vfs_context_current());+ if (error) {+ return error;+ }+ if (vnode_vtype(tvp) != VDIR || vnode_mount(tvp) == NULL ||+ strcmp(vfs_statfs(vnode_mount(tvp))->f_fstypename, "apfs") != 0) {+ vnode_put(tvp);+ return ENOTDIR;+ }+ target_id = APFS_VP(tvp)->an_id;+ }+ while ((len = strlen(path)) > 1 && path[len - 1] == '/') {+ path[len - 1] = '\0';+ }+ slash = strrchr(path, '/');+ if (slash == NULL || slash[1] == '\0') {+ error = EINVAL;+ goto out;+ }+ len = strlen(slash + 1);+ if (len >= sizeof(name)) {+ error = EINVAL;+ goto out;+ }+ strlcpy(name, slash + 1, sizeof(name)); /* before the path is cut to its parent */+ if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) {+ error = EINVAL;+ goto out;+ }+ if (vnode_lookup(path, 0, &vp, vfs_context_current()) == 0) {+ vnode_put(vp);+ error = EEXIST;+ goto out;+ }+ path[slash - path == 0 ? 1 : slash - path] = '\0'; /* the parent ("/" for "/name") */+ error = vnode_lookup(path, 0, &dvp, vfs_context_current());+ if (error) {+ goto out;+ }+ if (vnode_vtype(dvp) != VDIR || vnode_mount(dvp) == NULL ||+ strcmp(vfs_statfs(vnode_mount(dvp))->f_fstypename, "apfs") != 0 ||+ (tvp != NULLVP && vnode_mount(tvp) != vnode_mount(dvp))) {+ vnode_put(dvp);+ error = ENOTSUP;+ goto out;+ }+ dn = APFS_VP(dvp);+ am = dn->an_mount;+ lck_mtx_lock(&am->am_lock);+ if (am->am_synth == NULL) {+ am->am_synth = kalloc_data(APFS_SYNTH_MAX * sizeof(*am->am_synth), Z_WAITOK | Z_ZERO);+ }+ if (am->am_synth == NULL) {+ error = ENOMEM;+ } else if (apfs_synth_find(am, dn->an_id, name, len) != NULL) {+ error = EEXIST;+ } else if (am->am_nsynth >= APFS_SYNTH_MAX) {+ error = ENOSPC;+ } else {+ struct apfs_synth *sy = &am->am_synth[am->am_nsynth];+ struct apfs_inode ino;++ sy->parent = dn->an_id;+ sy->alias = (tvp != NULLVP);+ sy->id = sy->alias ? target_id : APFS_SYNTH_BASE + am->am_nsynth;+ strlcpy(sy->name, name, sizeof(sy->name));+ /* an id the volume does not use itself, or nothing */+ if (!sy->alias && apfs_inode_get(&am->am_vol, sy->id, &ino) != ENOENT) {+ error = EEXIST;+ } else {+ am->am_nsynth++;+ error = 0;+ }+ }+ lck_mtx_unlock(&am->am_lock);+ /* the parent's entries changed: what the name cache has of it is stale */+ cache_purge(dvp);+ vnode_put(dvp);+out:+ if (tvp != NULLVP) {+ vnode_put(tvp);+ }+ return error;+}++SYSCTL_PROC(_kern, OID_AUTO, apfs_synthetic, CTLTYPE_STRING | CTLFLAG_RW | CTLFLAG_LOCKED,+ NULL, 0, apfs_sysctl_synthetic, "A",+ "write PATH: an empty synthetic directory on an APFS volume (a mount point)");+/* Take a node out of the hash; called from vnop_reclaim. */voidapfs_node_detach(struct apfs_node *an)@@ -569,6 +738,9 @@ apfs_pool_start(void)static voidapfs_free_mount(struct apfs_mount *am){+ if (am->am_synth != NULL) {+ kfree_data(am->am_synth, APFS_SYNTH_MAX * sizeof(*am->am_synth));+ }while (am->am_iobuf_kept > 0) {kfree_data(am->am_iobuf[--am->am_iobuf_kept], am->am_iobuf_size);}diff --git a/xnu/files/bsd/miscfs/apfs/apfs_vnops.c b/xnu/files/bsd/miscfs/apfs/apfs_vnops.cindex 654aa2a..53509fb 100644--- a/xnu/files/bsd/miscfs/apfs/apfs_vnops.c+++ b/xnu/files/bsd/miscfs/apfs/apfs_vnops.c@@ -69,10 +69,6 @@ apfs_vnop_lookup(struct vnop_lookup_args *ap)if (!vnode_isdir(dvp)) {return ENOTDIR;}- /* read only: anything that would change the directory is refused up front */- if (cnp->cn_nameiop != LOOKUP) {- return EROFS;- }if (cnp->cn_namelen == 1 && cnp->cn_nameptr[0] == '.') {error = vnode_get(dvp);if (error == 0) {@@ -85,9 +81,32 @@ apfs_vnop_lookup(struct vnop_lookup_args *ap)}apfs_lock(am);- error = apfs_lookup(&am->am_vol, dn->an_id, cnp->cn_nameptr,- (size_t)cnp->cn_namelen, &id, &dt);+ if (dn->an_id >= APFS_SYNTH_BASE) {+ error = ENOENT; /* nothing of a synthetic directory is on the volume */+ } else {+ error = apfs_lookup(&am->am_vol, dn->an_id, cnp->cn_nameptr,+ (size_t)cnp->cn_namelen, &id, &dt);+ }+ if (error == ENOENT && am->am_nsynth > 0) {+ const struct apfs_synth *sy = apfs_synth_find(am, dn->an_id, cnp->cn_nameptr,+ (size_t)cnp->cn_namelen);++ if (sy != NULL) {+ id = sy->id;+ error = 0;+ }+ }apfs_unlock(am);+ /*+ * What is there is found whatever the caller means to do with it -- VFS refuses the+ * change itself on a read-only mount, and mkdir of a directory that exists is to fail+ * with EEXIST, not EROFS (xpcproxy gives up on the latter). Only a name that is not+ * there and is to be made is refused here, as packfs does.+ */+ if (error == ENOENT && (cnp->cn_flags & ISLASTCN) &&+ (cnp->cn_nameiop == CREATE || cnp->cn_nameiop == RENAME)) {+ return EROFS;+ }if (error) {return error;}@@ -413,8 +432,19 @@ apfs_vnop_readdir(struct vnop_readdir_args *ap)error = apfs_vnop_readdir_one(&rc, "..", 2, an->an_parent, DT_DIR);}if (error == 0) {+ uint32_t i;+apfs_lock(am);- error = apfs_readdir(&am->am_vol, an->an_id, apfs_readdir_cb, &rc);+ if (an->an_id < APFS_SYNTH_BASE) {+ error = apfs_readdir(&am->am_vol, an->an_id, apfs_readdir_cb, &rc);+ }+ /* the synthetic directories in this one, after what is on the volume */+ for (i = 0; error == 0 && i < am->am_nsynth; i++) {+ if (am->am_synth[i].parent == an->an_id) {+ error = apfs_vnop_readdir_one(&rc, am->am_synth[i].name,+ strlen(am->am_synth[i].name), am->am_synth[i].id, DT_DIR);+ }+ }apfs_unlock(am);}if (error == EEXIST) {@@ -650,6 +680,17 @@ apfs_vnop_reclaim(struct vnop_reclaim_args *ap)return 0;}+/*+ * No ioctl or fsctl means anything here. ENOTTY is what callers take for "this filesystem+ * does not have that"; the default, ENOTSUP, is not -- the iOS dyld, probing the root+ * volume for APFS features, gives up on it (see packfs_ioctl).+ */+static int+apfs_vnop_ioctl(__unused struct vnop_ioctl_args *ap)+{+ return ENOTTY;+}+static intapfs_vnop_pathconf(struct vnop_pathconf_args *ap){@@ -689,6 +730,7 @@ apfs_vnop_pathconf(struct vnop_pathconf_args *ap)int(**apfs_vnodeop_p)(void *);const struct vnodeopv_entry_desc apfs_vnodeop_entries[] = {{ .opve_op = &vnop_default_desc, .opve_impl = (VOPFUNC)vn_default_error },+ { .opve_op = &vnop_ioctl_desc, .opve_impl = (VOPFUNC)apfs_vnop_ioctl },{ .opve_op = &vnop_lookup_desc, .opve_impl = (VOPFUNC)apfs_vnop_lookup },{ .opve_op = &vnop_open_desc, .opve_impl = (VOPFUNC)apfs_vnop_open },{ .opve_op = &vnop_close_desc, .opve_impl = (VOPFUNC)apfs_vnop_close },diff --git a/xnu/redwood.patch b/xnu/redwood.patchindex 58e2c8c..76dea0f 100644--- a/xnu/redwood.patch+++ b/xnu/redwood.patch@@ -779,6 +779,25 @@ index dce802fc36f1..e2e6b3a3f3db 100644size_t uiolen;user_ssize_t rlen;off_t pgoff;+diff --git a/bsd/miscfs/bindfs/bind_vfsops.c b/bsd/miscfs/bindfs/bind_vfsops.c+index 412f9ecfc22c..f6595c0401a3 100644+--- a/bsd/miscfs/bindfs/bind_vfsops.c++++ b/bsd/miscfs/bindfs/bind_vfsops.c+@@ -139,8 +139,12 @@ bindfs_mount(struct mount * mp, __unused vnode_t devvp, user_addr_t user_data, v+ return ENOTSUP;+ }++- /* check entitlement */+- if (!IOCurrentTaskHasEntitlement(BINDFS_ENTITLEMENT)) {++ /*++ * check entitlement -- redwood: or process 1, the port's own init, unsigned, which binds++ * its choice of launchd jobs and the like onto the read-only iOS root volume before it++ * hands over to launchd (userland/src/init.c, root_apfs=)++ */++ if (!IOCurrentTaskHasEntitlement(BINDFS_ENTITLEMENT) && proc_getpid(current_proc()) != 1) {+ return EPERM;+ }+diff --git a/bsd/miscfs/mockfs/mockfs.h b/bsd/miscfs/mockfs/mockfs.hindex 07c91ffaf686..5ce41d043901 100644--- a/bsd/miscfs/mockfs/mockfs.h