Skip to content

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

C

XNU patch: PoC random fallback when no corecrypto provider is registered

Heyang Zhou826dbb6a0e6b
diff --git a/xnu-patches/0001-linux64-stage-hook.patch b/xnu-patches/0001-linux64-stage-hook.patch
index 4ba9431..e7ba4bd 100644
--- a/xnu-patches/0001-linux64-stage-hook.patch
+++ b/xnu-patches/0001-linux64-stage-hook.patch
@@ -1,3 +1,103 @@
+diff --git a/libkern/crypto/corecrypto_rand.c b/libkern/crypto/corecrypto_rand.c
+index d46815b..ccd2d84 100644
+--- a/libkern/crypto/corecrypto_rand.c
++++ b/libkern/crypto/corecrypto_rand.c
+@@ -28,8 +28,42 @@
+
+ #include <libkern/crypto/crypto_internal.h>
+ #include <corecrypto/ccrng.h>
++#include <string.h>
+ #include <libkern/crypto/rand.h>
+
++/*
++ * LINUX64 (xnulinux) PoC: without the corecrypto kext, g_crypto_funcs stays NULL. Until the hosted
++ * kernel has a crypto provider, fall back to a non-cryptographic xorshift generator seeded from
++ * early_random(). NOT FOR PRODUCTION.
++ */
++extern uint64_t early_random(void);
++static uint64_t linux64_rng_state[2];
++static uint64_t
++linux64_xorshift(uint64_t *st)
++{
++ if (st[0] == 0 && st[1] == 0) {
++ st[0] = early_random() | 1;
++ st[1] = early_random();
++ }
++ uint64_t s1 = st[0], s0 = st[1];
++ st[0] = s0;
++ s1 ^= s1 << 23;
++ st[1] = s1 ^ s0 ^ (s1 >> 17) ^ (s0 >> 26);
++ return st[1] + s0;
++}
++static void
++linux64_fill(uint64_t *st, void *out, size_t len)
++{
++ uint8_t *p = out;
++ while (len) {
++ uint64_t v = linux64_xorshift(st);
++ size_t n = len < sizeof(v) ? len : sizeof(v);
++ memcpy(p, &v, n);
++ p += n;
++ len -= n;
++ }
++}
++
+ int
+ cc_rand_generate(void *out, size_t outlen)
+ {
+@@ -41,6 +75,9 @@ cc_rand_generate(void *out, size_t outlen)
+ if (rng_state != NULL) {
+ error = ccrng_generate(rng_state, outlen, out);
+ }
++ } else {
++ linux64_fill(linux64_rng_state, out, outlen);
++ error = 0;
+ }
+
+ return error;
+@@ -58,6 +95,10 @@ crypto_random_generate(
+ void *random,
+ size_t random_size)
+ {
++ if (!g_crypto_funcs) {
++ linux64_fill((uint64_t *)ctx, random, random_size);
++ return;
++ }
+ g_crypto_funcs->random_generate_fn(ctx, random, random_size);
+ }
+
+@@ -67,12 +108,19 @@ crypto_random_uniform(
+ uint64_t bound,
+ uint64_t *random)
+ {
++ if (!g_crypto_funcs) {
++ *random = bound ? linux64_xorshift((uint64_t *)ctx) % bound : 0;
++ return;
++ }
+ g_crypto_funcs->random_uniform_fn(ctx, bound, random);
+ }
+
+ size_t
+ crypto_random_kmem_ctx_size(void)
+ {
++ if (!g_crypto_funcs) {
++ return 2 * sizeof(uint64_t);
++ }
+ return g_crypto_funcs->random_kmem_ctx_size_fn();
+ }
+
+@@ -80,5 +128,11 @@ void
+ crypto_random_kmem_init(
+ crypto_random_ctx_t ctx)
+ {
++ if (!g_crypto_funcs) {
++ uint64_t *st = (uint64_t *)ctx;
++ st[0] = early_random() | 1;
++ st[1] = early_random();
++ return;
++ }
+ g_crypto_funcs->random_kmem_init_fn(ctx);
+ }
diff --git a/osfmk/arm/arm_init.c b/osfmk/arm/arm_init.c
index ac1713e..96b959a 100644
--- a/osfmk/arm/arm_init.c