Skip to content

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

C

tests: host checks, the init bring-up and the corecrypto kext's tests fail when they should; corecrypto tests in CI

Fred Nixd450182ca6fc
diff --git a/.github/workflows/corecrypto.yml b/.github/workflows/corecrypto.yml
new file mode 100644
index 0000000..9ddaae2
--- /dev/null
+++ b/.github/workflows/corecrypto.yml
@@ -0,0 +1,35 @@
+# SPDX-License-Identifier: GPL-2.0-only
+# The corecrypto kext's tests (kexts/free/corecrypto/test/run.sh): its sources in user space, through the interface's
+# calls against OpenSSL, with AddressSanitizer and UndefinedBehaviorSanitizer. The arm64 runners test the code the hosted
+# kernel runs (Mbed TLS's AES and SHA-256 with the CPU's instructions only: mbedtls_config_xnulinux.h), x86_64 the
+# portable C.
+name: corecrypto
+on:
+ push:
+ paths:
+ - .github/workflows/corecrypto.yml
+ - kexts/free/corecrypto/**
+ - xnu-patches/corecrypto/**
+ pull_request:
+ paths:
+ - .github/workflows/corecrypto.yml
+ - kexts/free/corecrypto/**
+ - xnu-patches/corecrypto/**
+jobs:
+ test:
+ strategy:
+ fail-fast: false
+ matrix:
+ os: [ubuntu-24.04-arm, ubuntu-24.04, macos-latest]
+ runs-on: ${{ matrix.os }}
+ steps:
+ - uses: actions/checkout@v4
+ - name: Compiler and OpenSSL's headers
+ if: runner.os == 'Linux'
+ run: |
+ sudo apt-get update -qq
+ sudo apt-get install -y -qq clang libssl-dev >/dev/null
+ - name: Host
+ run: uname -sm; clang --version | head -1; openssl version
+ - name: Tests
+ run: kexts/free/corecrypto/test/run.sh
diff --git a/kexts/free/corecrypto/test/run.sh b/kexts/free/corecrypto/test/run.sh
index 70dc18a..de46460 100755
--- a/kexts/free/corecrypto/test/run.sh
+++ b/kexts/free/corecrypto/test/run.sh
@@ -1,7 +1,9 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-only
# Build the corecrypto kext's sources for user space with Mbed TLS (MBEDTLS: a checkout of the pinned release; fetched
-# when not given) and run test.c against OpenSSL, with AddressSanitizer and UndefinedBehaviorSanitizer.
+# when not given) and run test.c against OpenSSL, with AddressSanitizer and UndefinedBehaviorSanitizer. On Linux, or on a
+# Mac (OPENSSL: OpenSSL's prefix when the compiler does not find it by itself; Homebrew's on a Mac). On arm64 this is the
+# code the hosted kernel runs (AES and SHA-256 with the CPU's instructions only: mbedtls_config_xnulinux.h).
set -euo pipefail
D=$(cd "$(dirname "$0")" && pwd); K=$D/..; I=$(cd "$K/../../../xnu-patches/corecrypto/include" && pwd)
MBEDTLS_REV=$(sed -n 's/^MBEDTLS_REV=//p' "$K/mbedtls.rev")
@@ -15,8 +17,10 @@ HEAD=$(git -C "$M" rev-parse HEAD 2>/dev/null || echo "(not a git checkout)")
[ "$HEAD" = "$MBEDTLS_REV" ] || { echo "Mbed TLS at $M is $HEAD, mbedtls.rev pins $MBEDTLS_REV"; exit 1; }
LIB=$(sed -n 's/^MBEDTLS_SOURCES=//p' "$K/mbedtls.rev")
CC=${CC:-clang}
+OPENSSL=${OPENSSL:-$([ "$(uname)" != Darwin ] || brew --prefix openssl@3 2>/dev/null || true)}
$CC -std=c11 -O1 -g -fsanitize=address,undefined -fno-sanitize-recover=undefined -Wall -Wextra -Wno-unused-parameter \
-DCC_TEST_USERSPACE=1 -D_GNU_SOURCE -DMBEDTLS_CONFIG_FILE='"mbedtls_config_xnulinux.h"' -I"$I" -I"$K" -I"$M/include" -I"$M/library" \
+ ${OPENSSL:+-I"$OPENSSL/include" -L"$OPENSSL/lib"} \
"$D/test.c" "$K"/cc_digest.c "$K"/cc_aes.c "$K"/cc_des.c "$K"/cc_chachapoly.c "$K"/cc_rsa.c "$K"/cc_platform.c \
"$K"/cc_selftest.c \
$(for f in $LIB; do echo "$M/library/$f"; done) -lcrypto -o "$W/cctest" > "$W/build.log" 2>&1 ||
diff --git a/kexts/free/corecrypto/test/test.c b/kexts/free/corecrypto/test/test.c
index 8ee7cce..0720e23 100644
--- a/kexts/free/corecrypto/test/test.c
+++ b/kexts/free/corecrypto/test/test.c
@@ -37,8 +37,11 @@ void
xc_random(void *buf, size_t n)
{
host_calls++;
- if (getrandom(buf, n, 0) != (ssize_t)n) {
- abort();
+ for (size_t k; n; buf = (uint8_t *)buf + k, n -= k) { /* getentropy: Linux's and macOS's alike, 256 bytes a call */
+ k = n < 256 ? n : 256;
+ if (getentropy(buf, k) != 0) {
+ abort();
+ }
}
}
diff --git a/tests/host/run.sh b/tests/host/run.sh
index b954762..49c73d9 100755
--- a/tests/host/run.sh
+++ b/tests/host/run.sh
@@ -1,13 +1,15 @@
#!/bin/bash
# SPDX-License-Identifier: GPL-2.0-only
# In the VM, with a hosted system running: checks that the hosted kernel leaves Linux alone. 1 min.
-# fpcheck: a task on the hosted kernel's CPU (0) keeps its FP/SIMD registers across being switched out
+# fpcheck: a task on the hosted kernel's first CPU (0, or the first of fki's cpu_list) keeps its FP/SIMD registers across
+# being switched out
# memcheck: 4 GB of Linux memory keep their contents (nothing writes over Linux's pages)
set -e
cd "$(dirname "$0")"
O=${TMPDIR:-/tmp}
-cc -O2 -o $O/fpcheck fpcheck.c && cc -O2 -o $O/memcheck memcheck.c
-$O/fpcheck 0 25 & fp_pid=$!
+cc -O2 -o $O/fpcheck fpcheck.c; cc -O2 -o $O/memcheck memcheck.c # (not &&: set -e would pass over a failed first compile)
+XCPU0=$( (tr -d '\n' 2>/dev/null < /sys/module/fki/parameters/cpu_list || true) | cut -d, -f1) # as xnulinux-boot.sh has it
+$O/fpcheck ${XCPU0:-0} 25 & fp_pid=$!
m=0; sudo $O/memcheck 4 25 || m=$?
f=0; wait "$fp_pid" || f=$?
[ $f = 0 ] && [ $m = 0 ] && echo "host tests: pass" || { echo "host tests: FAIL (fpcheck $f, memcheck $m)"; exit 1; }
diff --git a/tools/init/start.s b/tools/init/start.s
index dfa78ac..164621e 100644
--- a/tools/init/start.s
+++ b/tools/init/start.s
@@ -3,7 +3,8 @@
// open("/dev/console", O_WRONLY), write a line, fork; the child writes its own line and exits 7,
// the child then execve()s /sbin/hello (which writes its line and exits 7); the parent wait4()s for it, writes a closing line and exits 42.
// Darwin reports a failed syscall with the carry flag set and errno in x0. Exit codes on failure:
-// open 64+errno, write 128+errno, short write 3, fork 4, wait4 5, child status not 7 or 0: 16+status (8: execve failed, 9: /sbin/hello failed).
+// open 64+errno, write 128+errno, short write 3, fork 4, wait4 5, child killed by a signal 6, child status not 7 or 0: 16+status
+// (8: execve failed, 9: /sbin/hello failed).
.text
.globl _start
.p2align 2
@@ -38,6 +39,8 @@ Lopen_again:
svc #0x80
b.cs Lwait_failed
ldr w0, [sp]
+ tst w0, #0x7f // WTERMSIG: killed (by AMFI, dyld, a fault). Its exit status byte is 0, as for a child that exited well
+ b.ne Lsignaled
lsr w0, w0, #8 // WEXITSTATUS
and w0, w0, #0xff
mov x23, x0
@@ -111,6 +114,9 @@ Lfork_failed:
Lwait_failed:
mov x0, #5
b Lexit
+Lsignaled:
+ mov x0, #6
+ b Lexit
Lbad_status:
add x0, x23, #16 // 16 + the child's exit status
b Lexit
diff --git a/tools/init/test/run.sh b/tools/init/test/run.sh
new file mode 100755
index 0000000..d3aaa17
--- /dev/null
+++ b/tools/init/test/run.sh
@@ -0,0 +1,25 @@
+#!/bin/bash
+# SPDX-License-Identifier: GPL-2.0-only
+# On an arm64 Mac: how ../start.s (the test root's /sbin/launchd) judges the child it execs. Its raw BSD system calls
+# (svc #0x80: open, write, fork, execve, wait4, exit) are the same in a macOS process as under the hosted XNU, so the
+# source is assembled as it is and run here, with two of its strings replaced: "/dev/console" by a file, and the script
+# it gives /bin/sh -c by what the child is to do. It is linked as a dynamic executable with _start as its entry (arm64
+# macOS runs no static ones) and started with fds 0-2 closed, so that its three opens are 0, 1 and 2, as for pid 1.
+set -u
+[ "$(uname -sm)" = "Darwin arm64" ] || { echo "skip: tools/init/start.s is Darwin arm64 code, this is $(uname -sm)"; exit 0; }
+D=$(cd "$(dirname "$0")/.." && pwd); W=$(mktemp -d) && W=$(cd "$W" && pwd -P) || exit 1; trap 'rm -rf "$W"' EXIT; failed=0
+check() { # check 'what the child does' STATUS: the status start.s must exit with (42: its "child exited with 7 or 0")
+ sed -e "s|\"/dev/console\"|\"$W/console\"|" -e '/^Lsharg2:/{' -e n -e "s|\".*\"|\"$1\"|" -e '}' "$D/start.s" > "$W/start.s"
+ grep -q "\"$W/console\"" "$W/start.s" && grep -qF "\"$1\"" "$W/start.s" || { echo "FAIL start.s has no \"/dev/console\" or no Lsharg2 string to replace"; exit 1; }
+ clang -arch arm64 -nostdlib -lSystem -e _start "$W/start.s" -o "$W/init" || exit 1
+ : > "$W/console"; "$W/init" <&- >&- 2>&-; local s=$? said=no
+ grep -q '^parent: child exited with 7 or 0$' "$W/console" && said=yes
+ if [ "$s $said" = "$2 $([ "$2" = 42 ] && echo yes || echo no)" ]; then echo "ok child: $1: exit status $s"
+ else echo "FAIL child: $1: exit status $s (expected $2), \"parent: child exited with 7 or 0\": $said"; failed=1; fi
+}
+check 'exit 0' 42
+check 'exit 7' 42
+check 'exit 3' 19 # 16 + its status
+check 'kill -9 $$' 6 # SIGKILL: what AMFI or the code-signing check gives a binary it refuses
+check 'kill -11 $$' 6 # a fault
+exit $failed