Skip to content

XNU as a loadable kernel module inside Linux (macOS personality for arm64 Linux)

C

Hosted XNU: arm64 (not arm64e) processes run with all four PAC keys off (#15)

George Shostakovych6abac0565cc0
diff --git a/xnu-ko/xnu_main.c b/xnu-ko/xnu_main.c
index ed712b9..e2ceea1 100644
--- a/xnu-ko/xnu_main.c
+++ b/xnu-ko/xnu_main.c
@@ -642,8 +642,8 @@ static void derive_user_pac_keys(u64_t rop_pid, u64_t jop_pid, int djop,
k.ga[0] ^= jop_pid; k.ga[1] ^= jmix;
k.ib[0] ^= rop_pid; k.ib[1] ^= rmix;
k.db[0] ^= rop_pid; k.db[1] ^= rmix;
- if (djop)
- k.sctlr_en &= ~((1ULL << 31) | (1ULL << 27));
+ if (djop) /* arm64 (not arm64e): all four keys off (EnIA, EnIB, EnDA, EnDB), as linux64_el0_run does at EL1 */
+ k.sctlr_en &= ~((1ULL << 31) | (1ULL << 30) | (1ULL << 27) | (1ULL << 13));
*out = k;
}
diff --git a/xnu-patches/0001-linux64-machine-layer.patch b/xnu-patches/0001-linux64-machine-layer.patch
index 4e11f88..a48f6ac 100644
--- a/xnu-patches/0001-linux64-machine-layer.patch
+++ b/xnu-patches/0001-linux64-machine-layer.patch
@@ -4811,7 +4811,7 @@ index a191eb3..ff1f9fc 100644
LOAD_KERN_STACK_TOP dst=x1, src=x0, tmp=x2 // Get the top of the kernel stack
load_general_registers x1, 2
set_process_dependent_keys_and_sync_context x0, x1, x2, x3, w4
-@@ -444,3 +442,216 @@ LEXT(machine_set_current_thread)
+@@ -444,3 +442,221 @@ LEXT(machine_set_current_thread)
/* vim: set ts=4: */
@@ -4895,12 +4895,17 @@ index a191eb3..ff1f9fc 100644
+ orr x10, x10, x9
+ msr TPIDR_EL0, x10
+ /*
-+ * x1: JOP off for this thread (arm64, not arm64e): EnIA (31) and EnDA (27) off at EL0. Only after the last
-+ * kernel pointer authentication (LOAD_KERN_STACK_TOP authenticates; with EnDA off it would trap to brk).
++ * x1: JOP off for this thread (arm64, not arm64e): all four keys, EnIA (31), EnIB (30), EnDA (27) and EnDB (13),
++ * off at EL0, as Apple hardware runs such a process. Its return addresses are unsigned, and the arm64e system
++ * unwinder authenticates each frame's with the IB key: with EnIB on, an exception thrown in an arm64 process
++ * ends in EXC_ARM_PAC_FAIL. Only after the last kernel pointer authentication (LOAD_KERN_STACK_TOP
++ * authenticates; with EnDA off it would trap to brk).
+ */
+ cbz x1, 1f
+ mrs x10, SCTLR_EL1
+ mov x11, #((1 << 31) | (1 << 27))
++ orr x11, x11, #(1 << 30)
++ orr x11, x11, #(1 << 13)
+ bic x10, x10, x11
+ msr SCTLR_EL1, x10
+1:
@@ -5021,7 +5026,7 @@ index a191eb3..ff1f9fc 100644
+ ldr x10, [x9, #16]
+ msr VBAR_EL1, x10
+ ldr x18, [x9, #24] // the host's x18 again (user code owns x18 at EL0)
-+ ldr x10, [x9, #32] // SCTLR_EL1 as the kernels expect it (EnIA/EnDA back on)
++ ldr x10, [x9, #32] // SCTLR_EL1 as the kernels expect it (the keys back on)
+ msr SCTLR_EL1, x10
+ isb
+ mov x0, sp
@@ -5930,7 +5935,7 @@ index d8f1b19..c257ecf 100644
return true;
}
-@@ -3646,3 +3655,463 @@ sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,
+@@ -3646,3 +3655,464 @@ sleh_panic_lockdown_should_initiate_el1_sp0_sync(uint64_t esr, uint64_t elr,
}
__SECURITY_STACK_DISALLOWED_POP
#endif /* CONFIG_SPTM */
@@ -6136,8 +6141,9 @@ index d8f1b19..c257ecf 100644
+ }
+ /*
+ * An arm64 (not arm64e) process runs the arm64e system libraries with JOP off: Apple hardware disables the user
-+ * IA/DA keys for it; hosted, user code runs with SCTLR_EL1.EnIA/EnDA cleared (linux64_el0_run puts them back on
-+ * the next exception), or a blraaz on one of its unsigned function pointers fails (EXC_ARM_PAC_FAIL).
++ * keys for it; hosted, user code runs with SCTLR_EL1.EnIA/EnIB/EnDA/EnDB cleared (linux64_el0_run puts them back
++ * on the next exception), or a blraaz on one of its unsigned function pointers, or the unwinder's autib of one of
++ * its unsigned return addresses, fails (EXC_ARM_PAC_FAIL).
+ */
+#if PRECISE_USER_KERNEL_TIME
+ recount_enter_user(); /* user time vs system time, as locore's return to user space does */