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

217 lines · 13415 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# AIENOS_CK_OSC_UNIT gate: OSC unit admission (OSC_UNIT_ARTIFACT v1, aien-protocols
18# PR #17) in the C kernel, booted in QEMU AArch64 + UEFI (AAVMF).
19#
20# Label: QEMU (aarch64 virt), TEST signer, not physical. Nothing here ran on a
21# Spark or any real machine. The only key is the spec's THROWAWAY TEST key; no
22# OWNER anchor exists (TRUST-1 has not provisioned one). Admission only: no
23# unit is mapped, launched or scheduled.
24#
25# Five conformance vectors (tests/fixtures/osc_unit/vectors) are written into
26# the sealed C Store of a GPT boot disk image at build time and read back by the
27# kernel's store stage, the same path as the P2 artifacts (svc/artifact_store.h):
28# a01_valid_min.unit TEST-signed, valid
29# a02_valid_caps_kernel.unit vector a02_valid_caps_kernel_domain.unit, staged
30# under a shorter Store name (names are at most 32
31# bytes, CK_ART_NAME_MAX; same bytes, checked).
32# TEST-signed, valid, one domain-1 request
33# pinning generation 7 (object 1)
34# a04_valid_owner_class.unit OWNER class, valid signature, no OWNER anchor
35# r14_bad_signature.unit TEST-signed, signature byte flipped
36# r37_code_svc.unit code contains an SVC word
37# Two boots of the full image, each with QEMU iommu=smmuv3 and the NVMe image:
38# 1. qualification build (CK_SEED0B_TEST_ANCHOR=1, TEST ONLY): a01 ACCEPT with
39# the UnitDigest and program id of the frozen expected.txt; a04
40# UNTRUSTED_SIGNER; r14 BAD_SIGNATURE; r37 CODE_INSTRUCTION; a02
41# CAP_GENERATION_STALE (29): the kernel's own generation lookup knows no
42# resource, so every nonzero pin is refused (ADR 0014 amendment point 5;
43# the host test checks the same rule only with a test lookup).
44# 2. ordinary (release) build, no anchors: a01 and a02 TEST_SIGNER_IN_RELEASE; a04
45# UNTRUSTED_SIGNER; r14 TEST_SIGNER_IN_RELEASE (the class check precedes the
46# signature); r37 CODE_INSTRUCTION; nothing accepted.
47# Static: the release image carries the TEST public key nowhere.
48# Last line: AIENOS_CK_OSC_UNIT: PASS|FAIL|NOT_RUN.
49# Needs qemu-system-aarch64 and AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64).
50set -euo pipefail
51
52repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
53cd "${repo_root}"
54verdict=AIENOS_CK_OSC_UNIT
55
56code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
57vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
58command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${verdict}: NOT_RUN"; exit 2; }
59[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${verdict}: NOT_RUN"; exit 2; }
60
61# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time):
62# scripts/lib_gate_hold.sh (aienos#278).
63. "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh"
64if ! gh_quiet_check || ! gh_lock_take "qemu_ck_osc_unit_test" "${AIENOS_GATE_MINUTES:-60}"; then
65 echo "NOT_RUN ${gh_why}"
66 echo "${verdict}: NOT_RUN"
67 exit 3
68fi
69# Release only this run's own gate lock record, at most once.
70release_flag() { gh_lock_release; }
71work="$(mktemp -d)"
72cleanup() { rm -rf "${work}"; release_flag; }
73trap cleanup EXIT
74failed=0
75pass() { echo "PASS $1"; }
76fail() { echo "FAIL $1"; failed=1; }
77
78cross=""
79if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi
80commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
81out_prod="${AIENOS_CK_OUT_PROD:-${repo_root}/target/native-kernel}"
82out_qual="${AIENOS_CK_OUT_QUAL:-${repo_root}/target/native-kernel-seed0b-test}"
83mk() { make -s -C native/kernel CROSS="${cross}" AIENOS_COMMIT="${commit}" "$@" >/dev/null; }
84mk OUT="${out_prod}" full
85mk OUT="${out_qual}" CK_SEED0B_TEST_ANCHOR=1 full
86make -s -C native/kernel OUT="${out_prod}" store-image gpt-image >/dev/null
87simg="${out_prod}/host/ck_store_image"
88
89fix="native/kernel/tests/fixtures/osc_unit"
90units=(a01_valid_min.unit a02_valid_caps_kernel.unit a04_valid_owner_class.unit r14_bad_signature.unit r37_code_svc.unit)
91# Store names are at most 32 bytes (CK_ART_NAME_MAX); the a02 vector file name is 33, so a02 is
92# staged under a shorter name. The bytes are the vector's, checked here.
93stage="${work}/stage"
94mkdir -p "${stage}"
95cp "${fix}/vectors/a02_valid_caps_kernel_domain.unit" "${stage}/a02_valid_caps_kernel.unit"
96cmp -s "${fix}/vectors/a02_valid_caps_kernel_domain.unit" "${stage}/a02_valid_caps_kernel.unit" \
97 && pass "a02 staged as a02_valid_caps_kernel.unit, bytes identical to the vector" || fail "a02 staged copy differs from the vector"
98files=()
99for u in "${units[@]}"; do
100 if [[ -f "${stage}/${u}" ]]; then files+=("${stage}/${u}"); else files+=("${fix}/vectors/${u}"); fi
101done
102a01_line=$(grep -E '^a01_valid_min\.unit ' "${fix}/vectors/expected.txt")
103a01_digest=$(sed -E 's/.* unit_digest=([0-9a-f]{64}) .*/\1/' <<<"${a01_line}")
104a01_prog=$(sed -E 's/.* program_id=([0-9a-f]{64})$/\1/' <<<"${a01_line}")
105[[ ${#a01_digest} == 64 && ${#a01_prog} == 64 ]] || { fail "expected UnitDigest read from the frozen expected.txt"; }
106
107# ---- static: the release image does not carry the TEST public key ----------
108key_hex=$(tr -d '\n' <"${fix}/keys/test1.pub")
109key_bytes=$(printf "$(sed 's/../\\x&/g' <<<"${key_hex}")")
110for d in "${out_prod}/full"; do
111 if LC_ALL=C grep -aqF -- "${key_bytes}" "${d}/BOOTAA64.EFI"; then fail "release image ${d#"${repo_root}"/} carries the OSC TEST public key"
112 else pass "release image ${d#"${repo_root}"/} does not carry the OSC TEST public key"; fi
113done
114if LC_ALL=C grep -aqF -- "${key_bytes}" "${out_qual}/full/BOOTAA64.EFI"; then
115 pass "the check sees the TEST key in the TEST-anchor qualification image (never counted as release)"
116else
117 fail "the TEST key is not found in the qualification image, so the release check proves nothing"
118fi
119
120# ---- boot disk: the vectors in the sealed Store ------------------------------
121disk="${work}/nvme.img"
122"${out_prod}/host/ck_gpt_image" create "${disk}" 512 64 aienos-middle >/dev/null || fail "GPT boot disk image created"
123if "${simg}" build "${disk}" 512 "${files[@]}" >"${work}/build.txt" 2>&1; then pass "boot disk built: $(head -1 "${work}/build.txt")"
124else fail "boot disk build: $(head -3 "${work}/build.txt" | tr '\n' ' ')"; fi
125"${simg}" list "${disk}" 512 >"${work}/list.txt" 2>&1 || fail "host read-back of the boot disk Store"
126for f in "${files[@]}"; do
127 grep -qxF "$(basename "${f}") bytes=$(stat -c %s "${f}") sha256=$(sha256sum "${f}" | cut -d' ' -f1)" "${work}/list.txt" \
128 || fail "host read-back of $(basename "${f}")"
129done
130
131boot() { # image, serial text output, disk image
132 local esp="${work}/esp" log="${work}/serial.log" started status
133 rm -rf "${esp}"
134 mkdir -p "${esp}/EFI/BOOT" "${esp}/EFI/AIENOS"
135 touch "${esp}/EFI/AIENOS/BOOTREPORT.TXT"
136 cp "$1" "${esp}/EFI/BOOT/BOOTAA64.EFI"
137 cp "${vars_fd}" "${work}/vars.fd"
138 started=$(date +%s)
139 set +e
140 nice -n 10 timeout "${AIENOS_QEMU_TIMEOUT:-300}" qemu-system-aarch64 \
141 -M virt,virtualization=on,gic-version=3,iommu=smmuv3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \
142 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
143 -drive if=pflash,format=raw,file="${work}/vars.fd" \
144 -drive if=none,id=esp,format=raw,file=fat:rw:"${esp}" \
145 -device virtio-blk-pci,drive=esp \
146 -drive if=none,id=nvme0,format=raw,file="$3" \
147 -device nvme,drive=nvme0,serial=aienos-nvme-test \
148 -device ramfb -display none -nic none \
149 -serial file:"${log}" -no-reboot
150 status=$?
151 set -e
152 tr -d '\r' <"${log}" >"$2"
153 echo "qemu exit ${status} after $(( $(date +%s) - started )) s (commit ${commit:0:12})"
154 [[ "${status}" == 0 ]] || fail "QEMU exit status ${status} (124 = timeout)"
155}
156has() { grep -qE -- "$2" "$1"; }
157check() { if has "$1" "$3"; then pass "$2"; else fail "$2"; fi; }
158common_checks() {
159 check "$1" "left firmware and entered the kernel" "kernel: alive"
160 check "$1" "kernel read all ${#units[@]} candidates from the boot disk Store" "^artifact_candidates: ${#units[@]}$"
161 check "$1" "artifact_source=nvme_store" "^artifact_source: nvme_store generation=[0-9]+ candidates=${#units[@]} "
162 if has "$1" "^report-truncated:"; then fail "kernel report lines were truncated"; else pass "no kernel report line was truncated"; fi
163 check "$1" "final report reached the console" "report_kind: final"
164 if has "$1" "report_kind: (panic|fault)"; then fail "panic or fault reported"; fi
165 if has "$1" "^artifact_bundle: malformed"; then fail "candidate list malformed"; fi
166 if has "$1" "TEST-ONLY QEMU fw_cfg|^artifact_source: fw_cfg"; then fail "fw_cfg source in a counted boot"; fi
167 if has "$1" "^artifact: "; then fail "an OSC unit was handed to the Binary Artifact v0 path"; else pass "no OSC unit went through the Binary Artifact v0 path"; fi
168 check "$1" "one osc_unit line per candidate" "^osc_units: seen=${#units[@]} "
169 check "$1" "the summary says admission only, nothing launched" "^osc_units: .*\(admission only, nothing launched\)$"
170}
171unit_line() { # serial name expected-regex-tail
172 if has "$1" "^osc_unit: ${2//./\\.} $3\$"; then pass "$2 -> $4"; else fail "$2 expected: $4 (got: $(grep -E "^osc_unit: ${2//./\\.} " "$1" | head -1 || echo none))"; fi
173}
174
175# ---- boot 1: qualification build (TEST ONLY anchor) -------------------------
176qual="${work}/qualification.txt"
177cp "${disk}" "${work}/disk-qual.img"
178boot "${out_qual}/full/BOOTAA64.EFI" "${qual}" "${work}/disk-qual.img"
179common_checks "${qual}"
180check "${qual}" "policy line: qualification mode, one TEST anchor, no OWNER anchor, TEST ONLY, QEMU not physical" \
181 "^osc_policy: mode=qualification test_anchors=1 \(throwaway TEST key, TEST ONLY\) owner_anchors=0 domains=1 gen_width=32; QEMU, not physical$"
182unit_line "${qual}" a01_valid_min.unit \
183 "ACCEPT unit_digest=${a01_digest} program_id=${a01_prog} funcs=2 caps=0 signer=TEST" \
184 "ACCEPT, UnitDigest and program id equal the frozen expected.txt"
185unit_line "${qual}" a02_valid_caps_kernel.unit "REFUSED code=29 name=CAP_GENERATION_STALE" \
186 "REFUSED CAP_GENERATION_STALE (pinned generation 7; the kernel lookup knows no resource)"
187unit_line "${qual}" a04_valid_owner_class.unit "REFUSED code=25 name=UNTRUSTED_SIGNER" "REFUSED UNTRUSTED_SIGNER (OWNER, no OWNER anchor)"
188unit_line "${qual}" r14_bad_signature.unit "REFUSED code=26 name=BAD_SIGNATURE" "REFUSED BAD_SIGNATURE"
189unit_line "${qual}" r37_code_svc.unit "REFUSED code=34 name=CODE_INSTRUCTION" "REFUSED CODE_INSTRUCTION (SVC word)"
190check "${qual}" "qualification totals: 1 accepted, 4 refused" "^osc_units: seen=5 accepted=1 refused=4 "
191
192# ---- boot 2: ordinary (release) build, no anchors ----------------------------
193prod="${work}/release.txt"
194cp "${disk}" "${work}/disk-prod.img"
195boot "${out_prod}/full/BOOTAA64.EFI" "${prod}" "${work}/disk-prod.img"
196release_flag
197common_checks "${prod}"
198check "${prod}" "policy line: release mode, no anchors at all" \
199 "^osc_policy: mode=release test_anchors=0 owner_anchors=0 \(none provisioned\) domains=1 gen_width=32; QEMU, not physical$"
200if has "${prod}" "seed0b-test qualification build"; then fail "release build carries the qualification label"; fi
201unit_line "${prod}" a01_valid_min.unit "REFUSED code=24 name=TEST_SIGNER_IN_RELEASE" "REFUSED TEST_SIGNER_IN_RELEASE (a valid TEST-signed unit)"
202unit_line "${prod}" a02_valid_caps_kernel.unit "REFUSED code=24 name=TEST_SIGNER_IN_RELEASE" "REFUSED TEST_SIGNER_IN_RELEASE (class is checked before capability policy)"
203unit_line "${prod}" a04_valid_owner_class.unit "REFUSED code=25 name=UNTRUSTED_SIGNER" "REFUSED UNTRUSTED_SIGNER (OWNER, no anchor)"
204unit_line "${prod}" r14_bad_signature.unit "REFUSED code=24 name=TEST_SIGNER_IN_RELEASE" "REFUSED TEST_SIGNER_IN_RELEASE (class is checked before the signature)"
205unit_line "${prod}" r37_code_svc.unit "REFUSED code=34 name=CODE_INSTRUCTION" "REFUSED CODE_INSTRUCTION"
206check "${prod}" "release totals: nothing accepted" "^osc_units: seen=5 accepted=0 refused=5 "
207
208if [[ -n "${AIENOS_LOG_DIR:-}" ]]; then
209 cp "${qual}" "${AIENOS_LOG_DIR}/qemu_ck_osc_unit_qualification_serial.log"
210 cp "${prod}" "${AIENOS_LOG_DIR}/qemu_ck_osc_unit_release_serial.log"
211fi
212if [[ "${failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then
213 echo "---- qualification serial (osc lines) ----"; grep -E "^(osc_|artifact|kernel|report_kind|panic|fault)" "${qual}" | head -60 || true
214 echo "---- release serial (osc lines) ----"; grep -E "^(osc_|artifact|kernel|report_kind|panic|fault)" "${prod}" | head -60 || true
215fi
216echo "label: QEMU (aarch64 virt), TEST signer, not physical"
217if [[ "${failed}" == 0 ]]; then echo "${verdict}: PASS"; else echo "${verdict}: FAIL"; exit 1; fi