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_ck_recovery_test.sh

125 lines · 6733 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# CK gate M4_RECOVERY: C kernel Recovery Core (inspect / repair-degraded-peer /
18# provision-identity, operator-authorised) under QEMU, one cold boot each.
19# Oracle: scripts/qemu_recovery_test.sh (Rust). Contract section 6.2.
20# Mode selection: contract K-7. The operator key is TEST-ONLY (0x0f x32).
21# QEMU is not hardware. Run only through the heavy forge. Usage: [--mutant NAME]
22set -uo pipefail
23cd "$(dirname "${BASH_SOURCE[0]}")/.."
24repo_root="$(pwd)"
25gate_name=M4_RECOVERY
26script_tag=qemu_ck_recovery
27# shellcheck source=scripts/lib_ck_cont_qemu.sh
28source scripts/lib_ck_cont_qemu.sh
29cq_init "$@"
30cq_build
31[[ "${cq_mode}" == mutant ]] || cq_default_image_checks
32cq_quiet_and_trap
33[[ "${cq_mode}" == mutant || "${hw_ok}" == 1 ]] || fail=1
34new_disk
35
36test_key=$(printf '0f%.0s' $(seq 32))
37wrong_key=$(printf '0e%.0s' $(seq 32))
38zero=$(printf '00%.0s' $(seq 32))
39respond() { "${cont_tool}" respond "$1" "$2"; }
40challenge_for() { sed -nE "s/^RECOVERY_CHALLENGE: action=$1 challenge=([0-9a-f]{64})\$/\1/p" "${work}/serial.txt" | tail -1; }
41refused_unauth() { [[ -n "$(out '^RECOVERY_REFUSED \(Unauthorised\)$')" ]] && unchanged; }
42b() { plan "$@"; boot; } # plan + boot
43
44# Setup: provisioned store with memory, then a malformed inactive peer.
45fresh_image; b 5
46l=$(out '^CONTINUITY: PROVISIONED '); agent=$(agent_of "${l}")
47[[ -n "${agent}" ]] || bad "setup: provision failed"
48b 7
49lm=$(out '^CONTINUITY: REMEMBERED '); memory=$(field memory "${lm}")
50[[ -n "${memory}" ]] || bad "setup: remember failed"
51good_image="${top}/good.img"; cp "${image}" "${good_image}"
52"${cont_tool}" inject-peer "${image}" 4096 >/dev/null || bad "setup: inject-peer failed"
53
54echo "=== 1. Degraded store ==="
55b 9
56if [[ -n "$(out '^RECOVERY_OPERATOR_KEY: TEST-ONLY$')" && \
57 -n "$(out '^RECOVERY_CORE: ENTERED reason=Degraded\(Malformed\)$')" && \
58 "$(agent_of "$(out '^RECOVERY_RECORD:')")" == "${agent}" ]] && unchanged; then
59 pass "88 inspection enters the Recovery Core (Degraded(Malformed)), same agent, writes nothing"
60else bad "88 inspection: $(grep -E '^RECOVERY_' "${work}/serial.txt" | tr '\n' '|')"; fi
61c_repair=$(challenge_for repair-degraded-peer)
62[[ ${#c_repair} == 64 ]] || bad "88 no repair challenge offered"
63good=$(respond "${test_key}" "${c_repair}")
64flip="${good:0:63}$([[ "${good:63:1}" == 0 ]] && echo 1 || echo 0)"
65i=0
66for c in zero wrong_key wrong_challenge flipped_last_byte; do
67 case "${c}" in
68 zero) r="${zero}" ;;
69 wrong_key) r=$(respond "${wrong_key}" "${c_repair}") ;;
70 wrong_challenge) r=$(respond "${test_key}" "$(printf 'ff%.0s' $(seq 32))") ;;
71 flipped_last_byte) r="${flip}" ;;
72 esac
73 plan 10 "response=${r}"; boot
74 i=$(( i + 1 ))
75 if refused_unauth; then pass "88.${i} repair with ${c} response refused (Unauthorised), image unchanged"
76 else bad "88.${i} repair with ${c}: $(out '^RECOVERY_(ACTION|REFUSED)')"; fi
77done
78plan 10 "response=${good}"; boot
79if [[ -n "$(out '^RECOVERY_ACTION: repair-degraded-peer DONE$')" ]] && ! unchanged; then pass "89 authorised repair done, image changed"
80else bad "89 authorised repair: $(out '^RECOVERY_(ACTION|REFUSED)')"; fi
81b 6; r=$(out '^CONTINUITY: RESUMED ')
82if [[ "$(agent_of "${r}")" == "${agent}" && "$(field memory "${r}")" == "${memory}" ]]; then pass "89a after repair a cold boot resumes writable, same agent and memory"
83else bad "89a resume after repair: '${r:-$(out '^CONTINUITY:')}'"; fi
84plan 10 "response=${good}"; boot
85if [[ -n "$(out '^RECOVERY_REFUSED \(NotApplicable\)$')" ]] && unchanged; then pass "89b replaying the repair authorisation on the healthy store: NotApplicable, image unchanged"
86else bad "89b replay: $(out '^RECOVERY_(ACTION|REFUSED)')"; fi
87
88echo "=== 2. Unprovisioned store ==="
89fresh_image; b 1; b 9
90if [[ -n "$(out '^RECOVERY_CORE: ENTERED reason=Unprovisioned$')" ]] && unchanged; then pass "90 unprovisioned store enters the Recovery Core"
91else bad "90 inspection: $(grep -E '^RECOVERY_' "${work}/serial.txt" | tr '\n' '|')"; fi
92c_prov=$(challenge_for provision-identity)
93[[ ${#c_prov} == 64 ]] || bad "90 no provision challenge offered"
94plan 11 "response=$(respond "${wrong_key}" "${c_prov}")"; boot
95if refused_unauth; then pass "90a provisioning with the wrong key refused, image unchanged"
96else bad "90a wrong key: $(out '^RECOVERY_(ACTION|REFUSED)')"; fi
97plan 11 "response=$(respond "${test_key}" "${c_prov}")"; boot
98da=$(out '^RECOVERY_ACTION: provision-identity DONE agent=[0-9a-f]{64}$'); pa=$(agent_of "${da}")
99b 6; r=$(out '^CONTINUITY: RESUMED ')
100if [[ -n "${pa}" && "$(agent_of "${r}")" == "${pa}" ]]; then pass "90b authorised provisioning done; a cold resume returns that agent"
101else bad "90b provisioning: '${da}' resume '${r}'"; fi
102
103echo "=== 3. Corrupted agent root (identity lost) ==="
104cp "${good_image}" "${image}"
105"${cont_tool}" corrupt-root "${image}" 4096 >/dev/null || bad "91 corrupt-root failed"
106b 9
107if [[ -n "$(out '^RECOVERY_CORE: ENTERED ')" && -z "$(out '^RECOVERY_CHALLENGE')" ]] && unchanged; then pass "91 corrupted root: Recovery Core entered, no challenge, image unchanged"
108else bad "91 corrupt root: $(grep -E '^RECOVERY_' "${work}/serial.txt" | tr '\n' '|')"; fi
109for m in 10 11; do
110 plan "${m}" "response=${zero}"; boot
111 if [[ -n "$(out '^RECOVERY_REFUSED ')" && -z "$(out '^RECOVERY_ACTION')" ]] && unchanged; then pass "91.m${m} mode ${m} with a forged response refused, nothing done, image unchanged"
112 else bad "91.m${m} forged response: $(out '^RECOVERY_(ACTION|REFUSED)')"; fi
113done
114b 6
115if [[ -z "$(out '^CONTINUITY: (RESUMED|PROVISIONED)')" && -n "$(out '^CONTINUITY: ')" ]] && unchanged; then pass "91c normal boot after identity loss: no RESUMED/PROVISIONED ($(out '^CONTINUITY: ')), image unchanged"
116else bad "91c identity loss resume: '$(out '^CONTINUITY: ')'"; fi
117
118echo "rows ${rows_ok}/${rows}; M1 failures: ${m1_fail}"
119if [[ "${cq_mode}" == mutant ]]; then
120 release_flag
121 if [[ "${fail}" == 0 ]]; then verdict SURVIVED; else verdict "KILLED (gate failed)"; fi
122 exit 0
123fi
124[[ "${hw_ok:-1}" == 1 && "${fail}" == 0 ]] && verdict PASS || verdict FAIL
125[[ "${fail}" == 0 ]]