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

189 lines · 8939 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# qemu_security_suite.sh: TRUST-1 Gate 4 Security Test Suite in QEMU AArch64
18# Includes:
19# 1. Software TPM 2.0 (swtpm) socket attachment
20# 2. Soak test (configurable iterations, default 5)
21# 3. Fault injection / tampered image rejection test
22# 4. Strict exit code propagation and receipt output
23# Zero Disk Secrets and Unslop compliant.
24
25set -euo pipefail
26
27repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
28cd "${repo_root}"
29
30# judge_corrupt_rejection LOG: Subtest 3 verdict from the console log of the
31# corrupted-image boot. PASS needs positive evidence that the firmware ran and
32# the console was captured (the AAVMF banner), and no "kernel: alive". A missing
33# or empty log, or a log without the banner, is FAIL: QEMU that never started
34# must not read as "the firmware rejected the image".
35judge_corrupt_rejection() {
36 local log="$1"
37 if [[ ! -f "${log}" ]]; then
38 echo "FAIL Corrupted-image boot left no console log: nothing shows the firmware ran"; return 1
39 fi
40 if ! grep -q "UEFI firmware (version" "${log}"; then
41 echo "FAIL Corrupted-image console log has no firmware banner: QEMU or the firmware did not run, so no rejection was observed"; return 1
42 fi
43 if grep -q "kernel: alive" "${log}"; then
44 echo "FAIL Corrupted binary unexpectedly reached kernel alive"; return 1
45 fi
46 echo "PASS Corrupted binary cleanly rejected by firmware without reaching kernel alive (firmware banner present)"
47}
48
49# --self-test: negative controls for judge_corrupt_rejection. No QEMU, no build.
50if [[ "${1:-}" == --self-test ]]; then
51 st=0; t="$(mktemp -d)"; trap 'rm -rf "${t}"' EXIT
52 expect_rc() { # name want_rc log
53 local rc=0; judge_corrupt_rejection "$3" >/dev/null || rc=$?
54 if [[ "${rc}" == "$2" ]]; then echo "PASS $1 -> exit ${rc}"; else echo "FAIL $1: wanted ${2}, got ${rc}"; st=1; fi
55 }
56 printf 'UEFI firmware (version 2024.02 built at 12:48:22 on Jun 2 2026)\nBdsDxe: loading Boot0001 "UEFI Misc Device"\nBdsDxe: failed to load Boot0001\n' >"${t}/rejected.txt"
57 expect_rc "firmware ran, image rejected (no kernel: alive)" 0 "${t}/rejected.txt"
58 expect_rc "mutant: no log file at all (QEMU never started)" 1 "${t}/absent.txt"
59 : >"${t}/empty.txt"; expect_rc "mutant: empty console log" 1 "${t}/empty.txt"
60 printf 'qemu-system-aarch64: invalid option\n' >"${t}/nobanner.txt"; expect_rc "mutant: log without the firmware banner" 1 "${t}/nobanner.txt"
61 { cat "${t}/rejected.txt"; echo "kernel: alive"; } >"${t}/booted.txt"; expect_rc "mutant: corrupted image reached kernel: alive" 1 "${t}/booted.txt"
62 if [[ ${st} == 0 ]]; then echo "QEMU_SECURITY_SUITE_SELF_TEST: PASS"; exit 0; fi
63 echo "QEMU_SECURITY_SUITE_SELF_TEST: FAIL"; exit 1
64fi
65
66code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}"
67vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}"
68ITERATIONS="${GATE4_SOAK_RUNS:-5}"
69
70# Issue #61: MTTCG hung in 1/40 boots with -smp 1 and 1/40 with cortex-a76;
71# single-threaded TCG completed 120/120 boots (40 initial plus 80 confirmation).
72qemu_accel=(-accel tcg,thread=single)
73
74command -v qemu-system-aarch64 >/dev/null || { echo "Error: qemu-system-aarch64 not installed" >&2; exit 2; }
75command -v swtpm >/dev/null || { echo "Error: swtpm not installed" >&2; exit 2; }
76[[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "Error: AAVMF firmware not found" >&2; exit 2; }
77
78commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
79echo "Building aienos-handoff binary for Gate 4 suite..."
80AIENOS_COMMIT="${commit}" AIENOS_RESTART_SECS=1 cargo build --quiet --release \
81 -p aienos-boot --target aarch64-unknown-uefi --features handoff --bin aienos-handoff
82
83work="$(mktemp -d)"
84
85cleanup() {
86 if [[ -n "${SWTPM_PID:-}" ]] && kill -0 "${SWTPM_PID}" 2>/dev/null; then
87 kill "${SWTPM_PID}" 2>/dev/null || true
88 fi
89 rm -rf "${work}"
90}
91trap cleanup EXIT
92
93echo "=== Subtest 1: Software TPM 2.0 Integration & Live Boot ==="
94tpm_dir="${work}/tpm"
95mkdir -p "${tpm_dir}" "${work}/subtest1/esp/EFI/BOOT" "${work}/subtest1/esp/EFI/AIENOS"
96touch "${work}/subtest1/esp/EFI/AIENOS/BOOTREPORT.TXT"
97cp target/aarch64-unknown-uefi/release/aienos-handoff.efi "${work}/subtest1/esp/EFI/BOOT/BOOTAA64.EFI"
98
99swtpm socket --tpm2 \
100 --tpmstate dir="${tpm_dir}" \
101 --ctrl type=unixio,path="${tpm_dir}/swtpm-sock" \
102 --flags not-need-init,startup-clear &
103SWTPM_PID=$!
104sleep 1
105
106cp "${vars_fd}" "${work}/subtest1/vars.fd"
107log="${work}/subtest1/serial_tpm.log"
108
109set +e
110timeout ${AIENOS_QEMU_TIMEOUT:-120} qemu-system-aarch64 \
111 -M virt,virtualization=on,gic-version=3 "${qemu_accel[@]}" -cpu max -smp 4 -m 2048 \
112 -chardev socket,id=chrtpm,path="${tpm_dir}/swtpm-sock" \
113 -tpmdev emulator,id=tpm0,chardev=chrtpm \
114 -device tpm-tis-device,tpmdev=tpm0 \
115 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
116 -drive if=pflash,format=raw,file="${work}/subtest1/vars.fd" \
117 -drive if=none,id=esp,format=raw,file=fat:rw:"${work}/subtest1/esp" \
118 -device virtio-blk-pci,drive=esp \
119 -device ramfb -display none -nic none \
120 -serial file:"${log}" -no-reboot
121tpm_status=$?
122set -e
123
124tr -d "\r" <"${log}" >"${work}/subtest1/serial_tpm.txt"
125[[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${work}/subtest1/serial_tpm.txt" "${AIENOS_LOG_DIR}/gate4_tpm_serial.log"
126if grep -q "kernel: alive" "${work}/subtest1/serial_tpm.txt" && grep -q "report_kind: final" "${work}/subtest1/serial_tpm.txt"; then
127 echo "PASS swTPM live boot reached kernel alive and final report"
128else
129 echo "FAIL swTPM live boot did not complete cleanly"
130 cat "${work}/subtest1/serial_tpm.txt"
131 exit 1
132fi
133
134echo "=== Subtest 2: Soak Testing (${ITERATIONS} Sequential Boots) ==="
135for i in $(seq 1 "${ITERATIONS}"); do
136 soak_work="${work}/soak_${i}"
137 mkdir -p "${soak_work}/esp/EFI/BOOT" "${soak_work}/esp/EFI/AIENOS"
138 touch "${soak_work}/esp/EFI/AIENOS/BOOTREPORT.TXT"
139 cp target/aarch64-unknown-uefi/release/aienos-handoff.efi "${soak_work}/esp/EFI/BOOT/BOOTAA64.EFI"
140 cp "${vars_fd}" "${soak_work}/vars.fd"
141 soak_log="${soak_work}/serial_soak.log"
142 set +e
143 timeout ${AIENOS_QEMU_TIMEOUT:-120} qemu-system-aarch64 \
144 -M virt,virtualization=on,gic-version=3 "${qemu_accel[@]}" -cpu max -smp 4 -m 2048 \
145 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
146 -drive if=pflash,format=raw,file="${soak_work}/vars.fd" \
147 -drive if=none,id=esp,format=raw,file=fat:rw:"${soak_work}/esp" \
148 -device virtio-blk-pci,drive=esp \
149 -device ramfb -display none -nic none \
150 -serial file:"${soak_log}" -no-reboot
151 soak_status=$?
152 set -e
153 tr -d "\r" <"${soak_log}" >"${soak_work}/serial_soak.txt"
154 [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${soak_work}/serial_soak.txt" "${AIENOS_LOG_DIR}/gate4_soak_${i}_serial.log"
155 if grep -q "kernel: alive" "${soak_work}/serial_soak.txt" && grep -q "report_kind: final" "${soak_work}/serial_soak.txt"; then
156 echo "PASS Soak run ${i}/${ITERATIONS}: clean boot & reset"
157 else
158 echo "FAIL Soak run ${i}/${ITERATIONS} failed"
159 cat "${soak_work}/serial_soak.txt"
160 exit 1
161 fi
162done
163
164echo "=== Subtest 3: Fault Injection (Corrupted EFI Image) ==="
165corrupt_work="${work}/corrupt"
166mkdir -p "${corrupt_work}/esp/EFI/BOOT"
167head -c 1024 /dev/urandom > "${corrupt_work}/esp/EFI/BOOT/BOOTAA64.EFI"
168cp "${vars_fd}" "${corrupt_work}/vars.fd"
169corrupt_log="${corrupt_work}/serial_corrupt.log"
170
171set +e
172timeout 30 qemu-system-aarch64 \
173 -M virt,virtualization=on,gic-version=3 "${qemu_accel[@]}" -cpu max -smp 4 -m 2048 \
174 -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \
175 -drive if=pflash,format=raw,file="${corrupt_work}/vars.fd" \
176 -drive if=none,id=esp,format=raw,file=fat:rw:"${corrupt_work}/esp" \
177 -device virtio-blk-pci,drive=esp \
178 -device ramfb -display none -nic none \
179 -serial file:"${corrupt_log}" -no-reboot
180corrupt_status=$?
181set -e
182
183if [[ -f "${corrupt_log}" ]]; then
184 tr -d "\r" <"${corrupt_log}" >"${corrupt_work}/serial_corrupt.txt"
185 [[ -z "${AIENOS_LOG_DIR:-}" ]] || cp "${corrupt_work}/serial_corrupt.txt" "${AIENOS_LOG_DIR}/gate4_corrupt_serial.log"
186fi
187judge_corrupt_rejection "${corrupt_work}/serial_corrupt.txt" || exit 1
188
189echo "TRUST-1 Gate 4 Security Suite: ALL TESTS PASS"