XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)
C
Hosted macOS 27.2.1 logs in: the OS cryptex's libraries on the system volume; sealed registers after AppleKeyStore
Heyang Zhou751df96987e6
diff --git a/scripts/mk-macvm-disk.sh b/scripts/mk-macvm-disk.shindex c579f00..dba0074 100755--- a/scripts/mk-macvm-disk.sh+++ b/scripts/mk-macvm-disk.sh@@ -28,7 +28,7 @@ donemkdir -p "$W"; rm -f "$IMG"; cp -c "$SRC" "$IMG" # an APFS clone of the installed disk: the installed one stays pristineDEV=$(hdiutil attach -imagekey diskimage-class=CRawDiskImage -nomount "$IMG" 2>/dev/null | awk '/GUID_partition_scheme/{print $1}')sleep 3 # the containers' synthesized disks appear after the attach returns-cleanup() { for m in "$W/data" "$W/sys" "$W/preboot"; do diskutil unmount force "$m" >/dev/null 2>&1 || true; done; hdiutil detach "$DEV" -force >/dev/null 2>&1 || true; }+cleanup() { for m in "$W/cx" "$W/sysrw" "$W/data" "$W/sys" "$W/preboot"; do diskutil unmount force "$m" >/dev/null 2>&1 || true; done; hdiutil detach "$DEV" -force >/dev/null 2>&1 || true; }trap cleanup EXITvol() { # the APFS volume named $1 in the main container of this image (the names the installer gives)for v in $(diskutil list | awk '/APFS Volume/ {print $NF}'); do@@ -78,6 +78,35 @@ for c in OS:os App:app; dohdiutil detach "$W/cx" >/dev/nullecho "cryptex ${c%%:*}: $(sudo -n du -sh "$W/preboot/Cryptexes/${c%%:*}" | cut -f1)"done+# ... and on the system volume too: dyld looks for a library outside the shared cache at its install name (e.g.+# /usr/lib/libLaunchServicesSupport.dylib, which loginwindow and the Dock link) and adds the OS cryptex's prefix only+# when the cryptex is grafted. The cryptex's files are merged into the system volume (nothing of the system's own is+# replaced), as the libraries sat before macOS moved them into the cryptex. This unseals the system volume; the hosted+# kernel mounts an unsealed root when SIP allows it (lp-sip0 0x800).+if [ "${MERGE_OS_CRYPTEX:-1}" = 1 ]; then+ diskutil unmount "$W/sys" >/dev/null+ mkdir -p "$W/sysrw" "$W/cx"+ if sudo -n mount -t apfs -o rw,nobrowse "/dev/$SYS" "$W/sysrw"; then+ hdiutil attach -readonly -nobrowse -mountpoint "$W/cx" "$CX/os.dmg" >/dev/null+ for d in usr/lib usr/libexec usr/bin usr/sbin usr/share System/iOSSupport; do+ [ -d "$W/cx/$d" ] && sudo -n mkdir -p "$W/sysrw/$d" && sudo -n cp -Rpn "$W/cx/$d/." "$W/sysrw/$d/" 2>/dev/null || true # cp -n exits 1 when it skips files+ done+ for e in "$W"/cx/System/Library/*; do+ n=$(basename "$e"); [ "$n" = dyld ] && continue+ if [ -d "$e" ]; then+ sudo -n mkdir -p "$W/sysrw/System/Library/$n"; sudo -n cp -Rpn "$e/." "$W/sysrw/System/Library/$n/" 2>/dev/null || true+ else+ sudo -n cp -pn "$e" "$W/sysrw/System/Library/" 2>/dev/null || true+ fi+ done+ hdiutil detach "$W/cx" >/dev/null+ echo "OS cryptex merged into the system volume: $(ls "$W/sysrw/usr/lib/libLaunchServicesSupport.dylib" 2>&1)"+ sync; sudo -n umount "$W/sysrw"+ else+ echo "cannot mount the system volume read-write"+ fi+ diskutil mount readOnly -mountPoint "$W/sys" "$SYS" >/dev/null+fisudo -n python3 scripts/extract-trustcache.py "$W/trustcache.bin" "$W/preboot"grep -h -A1 ProductVersion "$W/sys/System/Library/CoreServices/SystemVersion.plist" | tr -d '\t\n'; echocleanup; trap - EXITdiff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patchindex 55268f9..6842ed7 100644--- a/xnu-patches/0001-linux64-stage-hook.patch+++ b/xnu-patches/0001-linux64-stage-hook.patch@@ -267,7 +267,7 @@ index ec19ee7..6baf626 100644}}diff --git a/bsd/kern/bsd_init.c b/bsd/kern/bsd_init.c-index 5362b32..6d03460 100644+index 5362b32..88414be 100644--- a/bsd/kern/bsd_init.c+++ b/bsd/kern/bsd_init.c@@ -585,7 +585,7 @@ bsd_init(void)@@ -309,7 +309,7 @@ index 5362b32..6d03460 100644if (require_rootauth) {/* enforce sealedness */int autherr = VNOP_IOCTL(rootvnode, FSIOC_KERNEL_ROOTAUTH, NULL, 0, vfs_context_kernel());-@@ -1177,6 +1193,25 @@ bsd_init(void)+@@ -1177,6 +1193,42 @@ bsd_init(void)}#endif /* DEVFS */@@ -328,6 +328,23 @@ index 5362b32..6d03460 100644+ int perr = kernel_mount("apfs", NULLVP, NULLVP, prebootpath, rootvnode->v_mount, 0, 0,+ KERNEL_MOUNT_PREBOOTVOL, vfs_context_kernel());+ printf("LINUX64: Preboot volume at %s: %d\n", prebootpath, perr);++ /*++ * The cryptexes' graft points on the system volume, where dyld looks for their libraries (e.g.++ * /usr/lib/libLaunchServicesSupport.dylib lives only in the OS cryptex) and /Applications/Safari.app points:++ * bound to the cryptexes' trees in Preboot, read-only, as the grafts would show them.++ */++ static const char *const grafts[][2] = {++ { "/System/Cryptexes/OS", "/System/Volumes/Preboot/Cryptexes/OS" },++ { "/System/Cryptexes/App", "/System/Volumes/Preboot/Cryptexes/App" },++ };++ for (int g = 0; perr == 0 && g < 2; g++) {++ char at[64], from[64];++ strlcpy(at, grafts[g][0], sizeof(at));++ strlcpy(from, grafts[g][1], sizeof(from));++ int berr = kernel_mount("bindfs", NULLVP, NULLVP, at, (void *)from, 0, MNT_RDONLY | MNT_DONTBROWSE,++ 0, vfs_context_kernel());++ printf("LINUX64: %s bound to %s: %d\n", at, from, berr);++ }+ }+ }+#endif@@ -789,6 +806,34 @@ index c08a74c..1e26f75 100644353 AUE_GETAUID ALL { int getauid(au_id_t *auid); }354 AUE_SETAUID ALL { int setauid(au_id_t *auid); }355 AUE_NULL ALL { int nosys(void); } { old getaudit }+diff --git a/bsd/miscfs/bindfs/bind_vfsops.c b/bsd/miscfs/bindfs/bind_vfsops.c+index 4d50604..dc49a67 100644+--- a/bsd/miscfs/bindfs/bind_vfsops.c++++ b/bsd/miscfs/bindfs/bind_vfsops.c+@@ -137,14 +137,21 @@ bindfs_mount(struct mount * mp, __unused vnode_t devvp, user_addr_t user_data, v+ }++ /* check entitlement */+- if (!IOCurrentTaskHasEntitlement(BINDFS_ENTITLEMENT)) {++ /* LINUX64 (xnulinux): the kernel's own bind mounts (bsd_init, a hosted APFS boot) pass the path from kernel memory */++ boolean_t kernel_mount_path = vfs_iskernelmount(mp);++ if (!kernel_mount_path && !IOCurrentTaskHasEntitlement(BINDFS_ENTITLEMENT)) {+ return EPERM;+ }++ /*+ * Get argument+ */+- error = copyinstr(user_data, data, MAXPATHLEN - 1, &count);++ if (kernel_mount_path) {++ count = strlcpy(data, (const char *)user_data, MAXPATHLEN - 1) + 1;++ error = 0;++ } else {++ error = copyinstr(user_data, data, MAXPATHLEN - 1, &count);++ }+ if (error) {+ BINDFSERROR("error copying data from user %d\n", error);+ goto error;diff --git a/bsd/net/if_linux64.c b/bsd/net/if_linux64.cnew file mode 100644index 0000000..29cf3df