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_recovery_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 | # 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. |
| 25 | set -uo pipefail |
| 26 | |
| 27 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 28 | cd "${repo_root}" |
| 29 | |
| 30 | code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" |
| 31 | vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" |
| 32 | command -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 | |
| 35 | boot_timeout="${AIENOS_QEMU_TIMEOUT:-180}" |
| 36 | img_bytes=67108864 |
| 37 | lba_bytes=4096 |
| 38 | cfg_offset=$(( 32 * lba_bytes )) |
| 39 | store_offset=$(( 64 * lba_bytes )) |
| 40 | region_units=256 |
| 41 | machine="virt,virtualization=on,gic-version=3,iommu=smmuv3" |
| 42 | target_dir="target/qemu-recovery" |
| 43 | # TEST ONLY operator key (crates/aienos-boot/src/store_qual.rs). |
| 44 | test_key=$(printf '0f%.0s' $(seq 32)) |
| 45 | wrong_key=$(printf '0e%.0s' $(seq 32)) |
| 46 | zero=$(printf '00%.0s' $(seq 32)) |
| 47 | |
| 48 | echo "=== Building aienos-handoff (recovery-qual) and aienos-store-tool ===" |
| 49 | commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" |
| 50 | git diff --quiet HEAD 2>/dev/null || commit="${commit}-dirty" |
| 51 | AIENOS_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; } |
| 54 | cargo build --quiet --release -p aienos-store-tool || { echo "store tool build failed"; exit 2; } |
| 55 | tool="target/release/aienos-store-tool" |
| 56 | |
| 57 | work="$(mktemp -d)" |
| 58 | qemu_pid="" |
| 59 | cleanup() { [[ -n "${qemu_pid}" ]] && kill -9 "${qemu_pid}" 2>/dev/null || true; rm -rf "${work}"; } |
| 60 | trap cleanup EXIT |
| 61 | mkdir -p "${work}/esp/EFI/BOOT" "${work}/esp/EFI/AIENOS" |
| 62 | touch "${work}/esp/EFI/AIENOS/BOOTREPORT.TXT" |
| 63 | cp "${target_dir}/aarch64-unknown-uefi/release/aienos-handoff.efi" "${work}/esp/EFI/BOOT/BOOTAA64.EFI" |
| 64 | |
| 65 | image="${work}/nvme.img" |
| 66 | serial="${work}/serial.log" |
| 67 | fail=0 |
| 68 | |
| 69 | fresh_image() { rm -f "${image}"; truncate -s "${img_bytes}" "${image}"; } |
| 70 | digest() { sha256sum "${image}" | cut -d' ' -f1; } |
| 71 | pass() { echo "PASS $1"; } |
| 72 | bad() { echo "FAIL $1"; fail=1; } |
| 73 | |
| 74 | run_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. |
| 90 | boot() { |
| 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 | } |
| 96 | out() { tr -d '\r' <"${serial}" | grep -E "$1" | tail -1; } |
| 97 | challenge_for() { out "^RECOVERY_CHALLENGE: action=$1 " | sed -nE 's/.*challenge=([0-9a-f]{64}).*/\1/p'; } |
| 98 | respond() { "${tool}" operator-respond "$1" "$2"; } |
| 99 | agent_of() { sed -nE 's/.*agent=([0-9a-f]{64}).*/\1/p' <<<"$1"; } |
| 100 | unchanged() { [[ "${before}" == "${after}" ]]; } |
| 101 | |
| 102 | echo "=== 1. Degraded store: operator-authorised repair ===" |
| 103 | fresh_image |
| 104 | boot 5; agent=$(agent_of "$(out '^CONTINUITY: PROVISIONED')") |
| 105 | boot 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 | |
| 109 | boot 9 |
| 110 | if [[ -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" |
| 114 | else |
| 115 | bad "inspection: $(tr -d '\r' <"${serial}" | grep -E '^RECOVERY_' | tr '\n' '|')" |
| 116 | fi |
| 117 | c_repair=$(challenge_for repair-degraded-peer) |
| 118 | [[ ${#c_repair} == 64 ]] || bad "no repair challenge offered" |
| 119 | |
| 120 | for 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 |
| 132 | done |
| 133 | |
| 134 | good=$(respond "${test_key}" "${c_repair}") |
| 135 | boot 10 "${good}" |
| 136 | if [[ -n "$(out '^RECOVERY_ACTION: repair-degraded-peer DONE$')" ]] && ! unchanged; then |
| 137 | pass "authorised repair done" |
| 138 | else |
| 139 | bad "authorised repair: $(out '^RECOVERY_(ACTION|REFUSED)')" |
| 140 | fi |
| 141 | boot 6 |
| 142 | r=$(out '^CONTINUITY: RESUMED ') |
| 143 | if [[ "$(agent_of "${r}")" == "${agent}" && "${r}" == *"memory=${memory}"* ]]; then |
| 144 | pass "after repair a cold boot resumes writable with the same agent and memory" |
| 145 | else |
| 146 | bad "resume after repair: '${r:-$(out '^CONTINUITY:')}'" |
| 147 | fi |
| 148 | boot 10 "${good}" |
| 149 | if [[ -n "$(out '^RECOVERY_REFUSED \(NotApplicable\)$')" ]] && unchanged; then |
| 150 | pass "replaying the repair authorisation on the healthy store does nothing" |
| 151 | else |
| 152 | bad "replay: $(out '^RECOVERY_(ACTION|REFUSED)')" |
| 153 | fi |
| 154 | |
| 155 | echo "=== 2. Unprovisioned store: operator-authorised provisioning ===" |
| 156 | fresh_image |
| 157 | "${tool}" cfg "${image}" "${cfg_offset}" 1 0 >/dev/null; run_qemu # formatted store with data, no identity |
| 158 | boot 9 |
| 159 | c_prov=$(challenge_for provision-identity) |
| 160 | if [[ -n "$(out '^RECOVERY_CORE: ENTERED reason=Unprovisioned$')" && ${#c_prov} == 64 ]] && unchanged; then |
| 161 | pass "unprovisioned store enters the Recovery Core and offers only provisioning" |
| 162 | else |
| 163 | bad "unprovisioned inspection: $(tr -d '\r' <"${serial}" | grep -E '^RECOVERY_' | tr '\n' '|')" |
| 164 | fi |
| 165 | boot 11 "$(respond "${wrong_key}" "${c_prov}")" |
| 166 | if [[ -n "$(out '^RECOVERY_REFUSED \(Unauthorised\)$')" ]] && unchanged; then |
| 167 | pass "provisioning with a wrong key refused, nothing written" |
| 168 | else |
| 169 | bad "wrong-key provisioning: $(out '^RECOVERY_(ACTION|REFUSED)')" |
| 170 | fi |
| 171 | boot 11 "$(respond "${test_key}" "${c_prov}")" |
| 172 | new_agent=$(agent_of "$(out '^RECOVERY_ACTION: provision-identity DONE')") |
| 173 | boot 6 |
| 174 | if [[ ${#new_agent} == 64 && "$(agent_of "$(out '^CONTINUITY: RESUMED ')")" == "${new_agent}" ]]; then |
| 175 | pass "operator provisioning created one identity and a cold boot resumes it" |
| 176 | else |
| 177 | bad "operator provisioning: agent='${new_agent}' resume='$(out '^CONTINUITY:')'" |
| 178 | fi |
| 179 | |
| 180 | echo "=== 3. Identity loss: nothing may mint ===" |
| 181 | fresh_image |
| 182 | boot 5; lost=$(agent_of "$(out '^CONTINUITY: PROVISIONED')") |
| 183 | "${tool}" corrupt-kind "${image}" "${store_offset}" "${region_units}" 16 >/dev/null || bad "corrupt-kind failed" |
| 184 | boot 9 |
| 185 | if [[ -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=//'))" |
| 187 | else |
| 188 | bad "identity loss inspection: $(tr -d '\r' <"${serial}" | grep -E '^RECOVERY_' | tr '\n' '|')" |
| 189 | fi |
| 190 | forged=$(respond "${test_key}" "$(printf 'aa%.0s' $(seq 32))") |
| 191 | for 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 |
| 198 | done |
| 199 | boot 6 |
| 200 | if [[ -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)" |
| 202 | else |
| 203 | bad "normal boot after identity loss: $(out '^CONTINUITY:')" |
| 204 | fi |
| 205 | |
| 206 | echo |
| 207 | echo "=== Summary (commit ${commit}) ===" |
| 208 | if [[ "${fail}" == 0 ]]; then |
| 209 | echo "RECOVERY_CORE_QEMU: PASS" |
| 210 | else |
| 211 | echo "RECOVERY_CORE_QEMU: FAIL" |
| 212 | exit 1 |
| 213 | fi |
| 214 | echo "RECOVERY_CORE_OPERATOR_CREDENTIAL: TEST-ONLY (real scheme undecided, ADR 0006 / M5)" |