aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / scripts / qemu_ck_artifact_card_test.sh
| 1 | #!/usr/bin/env bash |
| 2 | # SPDX-License-Identifier: Apache-2.0 |
| 3 | # Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 4 | # |
| 5 | # Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | # you may not use this file except in compliance with the License. |
| 7 | # You may obtain a copy of the License at |
| 8 | # |
| 9 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | # |
| 11 | # Unless required by applicable law or agreed to in writing, software |
| 12 | # distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | # See the License for the specific language governing permissions and |
| 15 | # limitations under the License. |
| 16 | |
| 17 | # QEMU twin of the Raspberry Pi 5 EL0 artifact qualification image (PI5-7 cut 3, native/kernel/core/artifact_card.h). |
| 18 | # Builds the SAME code the Pi image runs (make CK_ARTIFACT_CARD=1 CK_SEED0B_TEST_ANCHOR=1, the core image with the card |
| 19 | # bundle source; the Pi image adds only CK_PI5) and boots it in QEMU AArch64 + UEFI (AAVMF) with the signed artifacts in ONE |
| 20 | # file on the ESP, \EFI\AIENOS\ARTIFACT.BND. QEMU is not hardware: a PASS here qualifies nothing physical; it is the red- |
| 21 | # before-green run that must pass before a Pi run is spent. |
| 22 | # |
| 23 | # TEST keys only: every artifact is signed by the in-tree C tool with the public RFC 8032 TEST 1 key (SEED-0B, TEST ONLY). |
| 24 | # Boots (all with the card image): |
| 25 | # 1. layout: the bundle holds P25EXEC only, to read the kernel's own layout lines; the two kernel-address probes are then |
| 26 | # assembled for exactly those addresses (fixtures/p2_7/assemble.sh); |
| 27 | # 2. qualification: P25EXEC (good, exits 0 through SYS_EXIT after a capability read), P27KREAD (EL0 load from kernel memory), |
| 28 | # P27KWRITE (EL0 store to kernel memory), P27PRIV (EL0 read of a kernel control register), P26SEED (good, runs AFTER the |
| 29 | # three faults: the kernel kept running), P25TAMP (one payload byte flipped after signing): every verdict, the fault |
| 30 | # class/ESR/FAR, the receipts (host-verified), frame accounting, TEST-ONLY labels; |
| 31 | # 3. negative controls: no ARTIFACT.BND on the card (no candidates), and a bundle with one trailing byte (refused whole). |
| 32 | # Last line: AIENOS_CK_ARTIFACT_CARD: PASS|FAIL|NOT_RUN. |
| 33 | set -euo pipefail |
| 34 | |
| 35 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 36 | cd "${repo_root}" |
| 37 | verdict=AIENOS_CK_ARTIFACT_CARD |
| 38 | |
| 39 | code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" |
| 40 | vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" |
| 41 | command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${verdict}: NOT_RUN"; exit 2; } |
| 42 | [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${verdict}: NOT_RUN"; exit 2; } |
| 43 | |
| 44 | . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" |
| 45 | if ! gh_quiet_check || ! gh_lock_take "qemu_ck_artifact_card" "${AIENOS_GATE_MINUTES:-60}"; then |
| 46 | echo "NOT_RUN ${gh_why}" |
| 47 | echo "${verdict}: NOT_RUN" |
| 48 | exit 3 |
| 49 | fi |
| 50 | work="$(mktemp -d)" |
| 51 | cleanup() { rm -rf "${work}"; gh_lock_release; } |
| 52 | trap cleanup EXIT |
| 53 | failed=0 |
| 54 | pass() { echo "PASS $1"; } |
| 55 | fail() { echo "FAIL $1"; failed=1; } |
| 56 | |
| 57 | cross="" |
| 58 | if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi |
| 59 | commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" |
| 60 | out="${repo_root}/target/native-kernel-artifact-card" |
| 61 | make -s -C native/kernel CROSS="${cross}" AIENOS_COMMIT="${commit}" OUT="${out}" CK_ARTIFACT_CARD=1 CK_SEED0B_TEST_ANCHOR=1 >/dev/null |
| 62 | make -s -C native/kernel CROSS="${cross}" OUT="${out}" tool >/dev/null |
| 63 | tool="${out}/host/ck_artifact_tool" |
| 64 | efi="${out}/BOOTAA64.EFI" |
| 65 | fx="crates/aienos-artifact-tool/fixtures" |
| 66 | |
| 67 | # ---- static: the image is TEST-ONLY labelled and carries no fw_cfg artifact code ---- |
| 68 | if grep -aqF "artifact_trust: seed0b-test qualification build" "${efi}"; then pass "card image carries the TEST ONLY trust label"; else fail "card image lacks the TEST ONLY trust label"; fi |
| 69 | if grep -aqE "opt/aienos/artifacts|fw_cfg|QEMU0002" "${efi}"; then fail "card image carries fw_cfg artifact code"; else pass "card image has no fw_cfg artifact code"; fi |
| 70 | for bad in "CK_ARTIFACT_CARD=1" "CK_ARTIFACT_CARD=1 CK_SEED0B_TEST_ANCHOR=1 CK_HARDWARE_STAGING=1" "CK_PI5_ARTIFACT=1 CK_PI5_STORE=1" "CK_ARTIFACT_CARD=1 CK_SEED0B_TEST_ANCHOR=1 full"; do |
| 71 | # shellcheck disable=SC2086 |
| 72 | if make -n -C native/kernel ${bad} >/dev/null 2>&1; then fail "Makefile accepts ${bad}"; else pass "Makefile refuses ${bad}"; fi |
| 73 | done |
| 74 | |
| 75 | boot() { # bundle-or-empty, serial text output |
| 76 | local esp="${work}/esp" log="${work}/serial.log" status |
| 77 | rm -rf "${esp}" |
| 78 | mkdir -p "${esp}/EFI/BOOT" "${esp}/EFI/AIENOS" |
| 79 | cp "${efi}" "${esp}/EFI/BOOT/BOOTAA64.EFI" |
| 80 | if [[ -n "$1" ]]; then cp "$1" "${esp}/EFI/AIENOS/ARTIFACT.BND"; fi |
| 81 | cp "${vars_fd}" "${work}/vars.fd" |
| 82 | set +e |
| 83 | nice -n 10 timeout "${AIENOS_QEMU_TIMEOUT:-300}" qemu-system-aarch64 \ |
| 84 | -M virt,virtualization=on,gic-version=3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ |
| 85 | -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ |
| 86 | -drive if=pflash,format=raw,file="${work}/vars.fd" \ |
| 87 | -drive if=none,id=esp,format=raw,file=fat:rw:"${esp}" \ |
| 88 | -device virtio-blk-pci,drive=esp \ |
| 89 | -device ramfb -display none -nic none \ |
| 90 | -serial file:"${log}" -no-reboot 2>/dev/null |
| 91 | status=$? |
| 92 | set -e |
| 93 | tr -d '\r' <"${log}" >"$2" |
| 94 | [[ "${status}" == 0 ]] || fail "QEMU exit status ${status} (124 = timeout)" |
| 95 | } |
| 96 | has() { grep -qE -- "$2" "$1"; } |
| 97 | check() { if has "$1" "$3"; then pass "$2"; else fail "$2"; fi; } |
| 98 | |
| 99 | # ---- inputs: signed artifacts (TEST key) ---- |
| 100 | art="${work}/art" |
| 101 | mkdir -p "${art}" |
| 102 | "${fx}/p2_5/pack.sh" "${tool}" "${art}" >/dev/null |
| 103 | "${fx}/p2_6/pack.sh" "${tool}" "${art}" >/dev/null |
| 104 | |
| 105 | # ---- boot 1: layout ---- |
| 106 | "${tool}" bundle "${work}/layout.bnd" "${art}/P25EXEC.AIEN" >/dev/null |
| 107 | boot "${work}/layout.bnd" "${work}/layout.txt" |
| 108 | root=$(sed -nE 's/^mmu_switch: .* aienos_root=(0x[0-9a-f]+) .*/\1/p' "${work}/layout.txt" | head -1) |
| 109 | heap_lo=$(sed -nE 's/^memory: .* heap=(0x[0-9a-f]+)-(0x[0-9a-f]+) .*/\1/p' "${work}/layout.txt" | head -1) |
| 110 | heap_hi=$(sed -nE 's/^memory: .* heap=(0x[0-9a-f]+)-(0x[0-9a-f]+) .*/\2/p' "${work}/layout.txt" | head -1) |
| 111 | if [[ -n "${root}" && -n "${heap_lo}" && -n "${heap_hi}" ]]; then |
| 112 | kw=$(printf '0x%x' $(( (heap_lo + heap_hi) / 2 & ~0xfff ))) |
| 113 | pass "kernel layout read: translation root ${root}, heap ${heap_lo}-${heap_hi}; probes target ${root} (read) and ${kw} (write)" |
| 114 | else |
| 115 | fail "kernel layout lines (root='${root}' heap='${heap_lo}-${heap_hi}')"; root=0x40000000; kw=0x4a000000 |
| 116 | fi |
| 117 | KREAD_ADDR="${root}" KWRITE_ADDR="${kw}" "${fx}/p2_7/assemble.sh" "${work}/p27" >/dev/null |
| 118 | "${fx}/p2_7/pack.sh" "${tool}" "${art}" "${work}/p27" >/dev/null |
| 119 | # the committed Pi variant must still be exactly what probe.S assembles to (default addresses) |
| 120 | "${fx}/p2_7/assemble.sh" "${work}/p27pi" >/dev/null |
| 121 | pi_ok=1 |
| 122 | for p in p27kread p27kwrite p27priv; do cmp -s "${work}/p27pi/${p}/code.bin" "${fx}/p2_7/${p}/code.bin" || pi_ok=0; done |
| 123 | if [[ "${pi_ok}" == 1 ]]; then pass "committed Pi probe code.bin files equal probe.S assembled"; else fail "committed Pi probe code.bin differs from probe.S"; fi |
| 124 | |
| 125 | # ---- boot 2: qualification ---- |
| 126 | order=(P25EXEC P27KREAD P27KWRITE P27PRIV P26SEED P25TAMP) |
| 127 | files=() |
| 128 | for n in "${order[@]}"; do files+=("${art}/${n}.AIEN"); done |
| 129 | "${tool}" bundle "${work}/qual.bnd" "${files[@]}" >/dev/null |
| 130 | qual="${work}/qual.txt" |
| 131 | boot "${work}/qual.bnd" "${qual}" |
| 132 | check "${qual}" "kernel alive, M3 EL0 isolation proof unchanged" "el0: ok write=granted forged=denied fault=contained exit=0" |
| 133 | check "${qual}" "stub found a well-formed bundle on the card" "^artifact_card: found a well-formed bundle \(QUALIFICATION ONLY, TEST keys, 6 entries, $(stat -c %s "${work}/qual.bnd") bytes\)$" |
| 134 | check "${qual}" "TEST ONLY trust label" "^artifact_trust: seed0b-test qualification build — TEST ONLY$" |
| 135 | check "${qual}" "card_file source with six candidates" "^artifact_source: card_file candidates=6 " |
| 136 | if has "${qual}" "artifact_source: nvme_store|fw_cfg"; then fail "wrong artifact source label"; fi |
| 137 | check "${qual}" "bundle SHA-256 as the kernel read it equals the host file" "^artifact_card: .* sha256=$(sha256sum "${work}/qual.bnd" | cut -d' ' -f1) " |
| 138 | ok=1 |
| 139 | for n in "${order[@]}"; do |
| 140 | has "${qual}" "^artifact_card: ${n}\.AIEN bytes=$(stat -c %s "${art}/${n}.AIEN") sha256=$(sha256sum "${art}/${n}.AIEN" | cut -d' ' -f1)$" || { ok=0; echo " kernel-side SHA-256 differs: ${n}"; } |
| 141 | done |
| 142 | if [[ "${ok}" == 1 ]]; then pass "kernel-side SHA-256 of all six artifacts equals the host files"; else fail "kernel-side artifact SHA-256"; fi |
| 143 | adm() { echo "^artifact: $1\.AIEN admitted id=[0-9a-f]{16} tier=seed0b-test exec=$2 bytes=identified=verified=admitted=mapped=executed wx=enforced caps=$3 revoked=yes reclaimed=yes frames=[0-9]+ "; } |
| 144 | check "${qual}" "(a) P25EXEC ran at EL0 and returned exit 0 through SYS_EXIT, capability read granted, forged handle denied" "$(adm P25EXEC exited:0x0 1)syscalls=3 reads=1 denials=1$" |
| 145 | check "${qual}" "(a) P26SEED ran after the faults, exit 0, 3 granted reads, 5 denials" "$(adm P26SEED exited:0x0 1)syscalls=9 reads=3 denials=5$" |
| 146 | check "${qual}" "(b) P25TAMP (one payload byte flipped) refused before running" "^artifact: P25TAMP\.AIEN rejected stage=verified reason=BadSignature reclaimed=yes$" |
| 147 | check "${qual}" "(c) P27KREAD: EL0 load from kernel memory faults" "$(adm P27KREAD fault:outside-read 0)syscalls=0 reads=0 denials=0$" |
| 148 | check "${qual}" "(c) P27KWRITE: EL0 store to kernel memory faults" "$(adm P27KWRITE fault:outside-write 0)syscalls=0 reads=0 denials=0$" |
| 149 | check "${qual}" "(c) P27PRIV: EL0 read of a kernel control register faults" "$(adm P27PRIV fault:privileged-insn 0)syscalls=0 reads=0 denials=0$" |
| 150 | check "${qual}" "(c) P27KREAD fault: data abort from EL0 (EC 0x24), read, FAR = the kernel root" "^artifact_fault: P27KREAD\.AIEN class=outside-read esr=0x[0-9a-f]+ ec=0x24 far=${root} elr=0x[0-9a-f]+ contained=yes$" |
| 151 | check "${qual}" "(c) P27KWRITE fault: data abort from EL0 (EC 0x24), write, FAR = the kernel heap address" "^artifact_fault: P27KWRITE\.AIEN class=outside-write esr=0x[0-9a-f]+ ec=0x24 far=${kw} elr=0x[0-9a-f]+ contained=yes$" |
| 152 | check "${qual}" "(c) P27PRIV fault: unknown-reason/trapped system instruction (EC 0x00 or 0x18), FAR not claimed" "^artifact_fault: P27PRIV\.AIEN class=privileged-insn esr=0x[0-9a-f]+ ec=0x(0|18) far=n/a elr=0x[0-9a-f]+ contained=yes$" |
| 153 | if has "${qual}" "exec=exited:0x57"; then fail "a probe reached its violation exit 0x57 (isolation broken)"; else pass "no probe reached its violation exit"; fi |
| 154 | check "${qual}" "kernel kept running: final report after all candidates" "^artifacts: candidates=6 admitted=5 rejected=1$" |
| 155 | if has "${qual}" "report_kind: (panic|fault)"; then fail "panic or fault reported"; else pass "no kernel panic or fault report"; fi |
| 156 | before=$(grep -oE "artifact_frames_free_before: [0-9]+" "${qual}" | awk '{print $2}' | tail -1) |
| 157 | after=$(grep -oE "artifact_frames_free_after: [0-9]+" "${qual}" | awk '{print $2}' | tail -1) |
| 158 | if [[ -n "${before}" && "${before}" == "${after}" ]]; then pass "every frame returned after all candidates (${before} free before and after)"; else fail "frame accounting (before=${before:-?} after=${after:-?})"; fi |
| 159 | check "${qual}" "bundle buffer released after the loader" "^artifact_card: released bundle bytes=$(stat -c %s "${work}/qual.bnd") entries=6$" |
| 160 | # receipts: decoded, bound to the supplied artifact, digest recomputed on the host, TEST ONLY signature verifies |
| 161 | receipt_check() { # name pattern |
| 162 | local line record kdigest o file="${work}/$1.receipt" |
| 163 | line=$(grep -E "^receipt: $1\.AIEN seq=[0-9]+ digest=[0-9a-f]{64} record=[0-9a-f]{1024}$" "${qual}" | tail -1 || true) |
| 164 | if [[ -z "${line}" ]]; then fail "$1 receipt emitted"; return; fi |
| 165 | record=${line##*record=} |
| 166 | kdigest=$(sed -E 's/.* digest=([0-9a-f]{64}) .*/\1/' <<<"${line}") |
| 167 | "${tool}" receipt from-hex "${record}" "${file}" >/dev/null 2>&1 || { fail "$1 receipt decodes"; return; } |
| 168 | o=$("${tool}" receipt check "${file}" "${art}/$1.AIEN" 2>&1) || { fail "$1 receipt binds the artifact (${o})"; return; } |
| 169 | [[ "${o}" == *"digest=${kdigest} "* ]] || { fail "$1 receipt digest recomputed by the host"; return; } |
| 170 | grep -qE -- "$2" <<<"${o}" || { fail "$1 receipt records the observed outcome (${o})"; return; } |
| 171 | "${tool}" receipt sign-test "${file}" "${file}.signed" >/dev/null 2>&1 || { fail "$1 receipt TEST ONLY signing"; return; } |
| 172 | if "${tool}" receipt verify "${file}.signed" | grep -q "RECEIPT_VERIFY: PASS digest=${kdigest} "; then pass "$1 receipt: bound, host digest match, outcome recorded, TEST ONLY signature verifies"; else fail "$1 signed receipt verifies"; fi |
| 173 | } |
| 174 | receipt_check P25EXEC "decision=Admitted .* status=Exited exit=0 " |
| 175 | receipt_check P26SEED "decision=Admitted .* status=Exited exit=0 " |
| 176 | receipt_check P25TAMP "decision=Rejected .* stage=3 reason=0x12 status=NotRun " |
| 177 | for n in P27KREAD P27KWRITE P27PRIV; do receipt_check "${n}" "decision=Admitted .* status=Fault exit=0 syscalls=0 "; done |
| 178 | |
| 179 | # ---- boot 3: no bundle on the card ---- |
| 180 | none="${work}/none.txt" |
| 181 | boot "" "${none}" |
| 182 | check "${none}" "no bundle: loader reports no source" "^artifact_source: none \(no card artifact bundle \(file absent\)\)$" |
| 183 | check "${none}" "no bundle: zero candidates" "^artifacts: candidates=0 admitted=0 rejected=0$" |
| 184 | |
| 185 | # ---- boot 4: a bundle with one trailing byte is refused whole ---- |
| 186 | cp "${work}/qual.bnd" "${work}/trail.bnd" |
| 187 | printf '\000' >>"${work}/trail.bnd" |
| 188 | trail="${work}/trail.txt" |
| 189 | boot "${work}/trail.bnd" "${trail}" |
| 190 | check "${trail}" "trailing byte: bundle refused by the stub" "^artifact_card: refused \(bytes after the last entry\)$" |
| 191 | check "${trail}" "trailing byte: no artifact reaches the loader" "^artifacts: candidates=0 admitted=0 rejected=0$" |
| 192 | if has "${trail}" "^artifact: [A-Z0-9-]+\.AIEN "; then fail "an artifact of a refused bundle was named"; else pass "no artifact of the refused bundle was named or run"; fi |
| 193 | |
| 194 | if [[ -n "${AIENOS_LOG_DIR:-}" ]]; then |
| 195 | cp "${qual}" "${AIENOS_LOG_DIR}/qemu_ck_artifact_card_serial.log" |
| 196 | fi |
| 197 | if [[ "${failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then |
| 198 | echo "---- qualification serial (artifact lines) ----" |
| 199 | grep -E "^(artifact|grant|kernel|report_kind|panic|fault|exception|esr|far)" "${qual}" | head -80 || true |
| 200 | fi |
| 201 | if [[ "${failed}" == 0 ]]; then echo "${verdict}: PASS"; else echo "${verdict}: FAIL"; exit 1; fi |