interplane.Get Omega
Read in your language

aienux

Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.

Brought by drakestapleton

branch main Branches Tags History

aienux / scripts / qemu_recovery_test.sh

214 lines · 9513 bytes Raw Blame History
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# Recovery Core qualification in QEMU (ADR 0006): deterministic entry and
18# read-only inspection, operator-authorised repair and provisioning bound to
19# the exact on-disk state, and no action that mints an identity after
20# identity loss. No model, no network.
21#
22# The operator key is TEST ONLY (public in this tree); the real credential
23# scheme is not decided (ADR 0006 / M5). 4096-byte LBA, native NVMe driver.
24# Emits RECOVERY_CORE_QEMU: PASS only when every step passes.
25set -uo pipefail
26
27repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
28cd "${repo_root}"
29
30code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
31vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
32command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; exit 2; }
33[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; exit 2; }
34
35boot_timeout="${AIENOS_QEMU_TIMEOUT:-180}"
36img_bytes=67108864
37lba_bytes=4096
38cfg_offset=$(( 32 * lba_bytes ))
39store_offset=$(( 64 * lba_bytes ))
40region_units=256
41machine="virt,virtualization=on,gic-version=3,iommu=smmuv3"
42target_dir="target/qemu-recovery"
43# TEST ONLY operator key (crates/aienos-boot/src/store_qual.rs).
44test_key=$(printf '0f%.0s' $(seq 32))
45wrong_key=$(printf '0e%.0s' $(seq 32))
46zero=$(printf '00%.0s' $(seq 32))
47
48echo "=== Building aienos-handoff (recovery-qual) and aienos-store-tool ==="
49commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
50git diff --quiet HEAD 2>/dev/null || commit="${commit}-dirty"
51AIENOS_COMMIT="${commit}" AIENOS_RESTART_SECS=1 cargo build --quiet --release \
52 -p aienos-boot --target aarch64-unknown-uefi --features recovery-qual --bin aienos-handoff \
53 --target-dir "${target_dir}" || { echo "build failed"; exit 2; }
54cargo build --quiet --release -p aienos-store-tool || { echo "store tool build failed"; exit 2; }
55tool="target/release/aienos-store-tool"
56
57work="$(mktemp -d)"
58qemu_pid=""
59cleanup() { [[ -n "${qemu_pid}" ]] && kill -9 "${qemu_pid}" 2>/dev/null || true; rm -rf "${work}"; }
60trap cleanup EXIT
61mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS"
62touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT"
63cp "${target_dir}/aarch64-unknown-uefi/release/aienos-handoff.efi" "${work}/esp/EFI/BOOT/BOOTAA64.EFI"
64
65image="${work}/nvme.img"
66serial="${work}/serial.log"
67fail=0
68
69fresh_image() { rm -f "${image}"; truncate -s "${img_bytes}" "${image}"; }
70digest() { sha256sum "${image}" | cut -d' ' -f1; }
71pass() { echo "PASS $1"; }
72bad() { echo "FAIL $1"; fail=1; }
73
74run_qemu() {
75 cp -f "${vars_fd}" "${work}/vars.fd"
76 rm -f "${serial}"
77 timeout "${boot_timeout}" qemu-system-aarch64 \
78 -M "${machine}" -accel tcg,thread=single -cpu max -smp 4 -m 2048 \
79 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
80 -drive if=pflash,format=raw,file="${work}/vars.fd" \
81 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/esp" \
82 -device virtio-blk-pci,drive=esp \
83 -drive if=none,id=nvme0,format=raw,file="${image}" \
84 -device nvme,drive=nvme0,serial=aienos-recovery,logical_block_size=${lba_bytes},physical_block_size=${lba_bytes} \
85 -display none -nic none -serial file:"${serial}" -no-reboot || true
86}
87
88# boot MODE [RESPONSE_HEX]: write the control block, then one cold boot.
89# before/after digests bracket only the guest's writes.
90boot() {
91 "${tool}" cfg "${image}" "${cfg_offset}" "$1" 0 ${2:+"$2"} >/dev/null || { echo "cfg write failed"; exit 2; }
92 before=$(digest)
93 run_qemu
94 after=$(digest)
95}
96out() { tr -d '\r' <"${serial}" | grep -E "$1" | tail -1; }
97challenge_for() { out "^RECOVERY_CHALLENGE: action=$1 " | sed -nE 's/.*challenge=([0-9a-f]{64}).*/\1/p'; }
98respond() { "${tool}" operator-respond "$1" "$2"; }
99agent_of() { sed -nE 's/.*agent=([0-9a-f]{64}).*/\1/p' <<<"$1"; }
100unchanged() { [[ "${before}" == "${after}" ]]; }
101
102echo "=== 1. Degraded store: operator-authorised repair ==="
103fresh_image
104boot 5; agent=$(agent_of "$(out '^CONTINUITY: PROVISIONED')")
105boot 7; memory=$(out '^CONTINUITY: REMEMBERED' | sed -nE 's/.*memory=([0-9a-f]+).*/\1/p')
106[[ ${#agent} == 64 && -n "${memory}" ]] || bad "could not build a provisioned store with memory"
107"${tool}" inject "${image}" "${store_offset}" inactive seeded_garbage >/dev/null || bad "inject failed"
108
109boot 9
110if [[ -n "$(out '^RECOVERY_OPERATOR_KEY: TEST-ONLY$')" && \
111 -n "$(out '^RECOVERY_CORE: ENTERED reason=Degraded\(Malformed\)$')" && \
112 "$(agent_of "$(out '^RECOVERY_RECORD:')")" == "${agent}" ]] && unchanged; then
113 pass "inspection enters the Recovery Core (Degraded(Malformed)), shows the same agent, writes nothing"
114else
115 bad "inspection: $(tr -d '\r' <"${serial}" | grep -E '^RECOVERY_' | tr '\n' '|')"
116fi
117c_repair=$(challenge_for repair-degraded-peer)
118[[ ${#c_repair} == 64 ]] || bad "no repair challenge offered"
119
120for case in zero wrong_key wrong_action; do
121 case "${case}" in
122 zero) r="${zero}" ;;
123 wrong_key) r=$(respond "${wrong_key}" "${c_repair}") ;;
124 wrong_action) r=$(respond "${test_key}" "$(printf 'ff%.0s' $(seq 32))") ;;
125 esac
126 boot 10 "${r}"
127 if [[ -n "$(out '^RECOVERY_REFUSED \(Unauthorised\)$')" ]] && unchanged; then
128 pass "repair with ${case} response refused, nothing written"
129 else
130 bad "repair with ${case}: $(out '^RECOVERY_(ACTION|REFUSED)')"
131 fi
132done
133
134good=$(respond "${test_key}" "${c_repair}")
135boot 10 "${good}"
136if [[ -n "$(out '^RECOVERY_ACTION: repair-degraded-peer DONE$')" ]] && ! unchanged; then
137 pass "authorised repair done"
138else
139 bad "authorised repair: $(out '^RECOVERY_(ACTION|REFUSED)')"
140fi
141boot 6
142r=$(out '^CONTINUITY: RESUMED ')
143if [[ "$(agent_of "${r}")" == "${agent}" && "${r}" == *"memory=${memory}"* ]]; then
144 pass "after repair a cold boot resumes writable with the same agent and memory"
145else
146 bad "resume after repair: '${r:-$(out '^CONTINUITY:')}'"
147fi
148boot 10 "${good}"
149if [[ -n "$(out '^RECOVERY_REFUSED \(NotApplicable\)$')" ]] && unchanged; then
150 pass "replaying the repair authorisation on the healthy store does nothing"
151else
152 bad "replay: $(out '^RECOVERY_(ACTION|REFUSED)')"
153fi
154
155echo "=== 2. Unprovisioned store: operator-authorised provisioning ==="
156fresh_image
157"${tool}" cfg "${image}" "${cfg_offset}" 1 0 >/dev/null; run_qemu # formatted store with data, no identity
158boot 9
159c_prov=$(challenge_for provision-identity)
160if [[ -n "$(out '^RECOVERY_CORE: ENTERED reason=Unprovisioned$')" && ${#c_prov} == 64 ]] && unchanged; then
161 pass "unprovisioned store enters the Recovery Core and offers only provisioning"
162else
163 bad "unprovisioned inspection: $(tr -d '\r' <"${serial}" | grep -E '^RECOVERY_' | tr '\n' '|')"
164fi
165boot 11 "$(respond "${wrong_key}" "${c_prov}")"
166if [[ -n "$(out '^RECOVERY_REFUSED \(Unauthorised\)$')" ]] && unchanged; then
167 pass "provisioning with a wrong key refused, nothing written"
168else
169 bad "wrong-key provisioning: $(out '^RECOVERY_(ACTION|REFUSED)')"
170fi
171boot 11 "$(respond "${test_key}" "${c_prov}")"
172new_agent=$(agent_of "$(out '^RECOVERY_ACTION: provision-identity DONE')")
173boot 6
174if [[ ${#new_agent} == 64 && "$(agent_of "$(out '^CONTINUITY: RESUMED ')")" == "${new_agent}" ]]; then
175 pass "operator provisioning created one identity and a cold boot resumes it"
176else
177 bad "operator provisioning: agent='${new_agent}' resume='$(out '^CONTINUITY:')'"
178fi
179
180echo "=== 3. Identity loss: nothing may mint ==="
181fresh_image
182boot 5; lost=$(agent_of "$(out '^CONTINUITY: PROVISIONED')")
183"${tool}" corrupt-kind "${image}" "${store_offset}" "${region_units}" 16 >/dev/null || bad "corrupt-kind failed"
184boot 9
185if [[ -n "$(out '^RECOVERY_CORE: ENTERED reason=')" && -z "$(out '^RECOVERY_CHALLENGE:')" ]] && unchanged; then
186 pass "corrupted agent root: Recovery Core entered, no action offered, nothing written ($(out '^RECOVERY_CORE:' | sed 's/.*reason=//'))"
187else
188 bad "identity loss inspection: $(tr -d '\r' <"${serial}" | grep -E '^RECOVERY_' | tr '\n' '|')"
189fi
190forged=$(respond "${test_key}" "$(printf 'aa%.0s' $(seq 32))")
191for m in 10 11; do
192 boot "${m}" "${forged}"
193 if [[ -n "$(out '^RECOVERY_REFUSED \(')" && -z "$(out '^RECOVERY_ACTION:')" ]] && unchanged; then
194 pass "mode ${m} after identity loss refused, nothing written"
195 else
196 bad "mode ${m} after identity loss: $(out '^RECOVERY_(ACTION|REFUSED)')"
197 fi
198done
199boot 6
200if [[ -z "$(out '^CONTINUITY: (RESUMED|PROVISIONED)')" ]] && unchanged; then
201 pass "a normal boot after identity loss does not resume or mint (${lost:0:16}... is not replaced)"
202else
203 bad "normal boot after identity loss: $(out '^CONTINUITY:')"
204fi
205
206echo
207echo "=== Summary (commit ${commit}) ==="
208if [[ "${fail}" == 0 ]]; then
209 echo "RECOVERY_CORE_QEMU: PASS"
210else
211 echo "RECOVERY_CORE_QEMU: FAIL"
212 exit 1
213fi
214echo "RECOVERY_CORE_OPERATOR_CREDENTIAL: TEST-ONLY (real scheme undecided, ADR 0006 / M5)"