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 / capture_gate1_receipt.sh

219 lines · 10613 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# FLAG(sovereignty): the tool list below names /bin/tpm2_pcrread (outside dep) in the recovery-image receipt; follows the in-house C replacement.
18# capture_gate1_receipt.sh: write the TRUST-1 Gate 1 (standalone RAM recovery
19# media) receipt from checks that actually ran. Nothing in the receipt is
20# assumed: every field is either measured here or marked NOT_RUN.
21#
22# What it checks:
23# host The recovery image (a gzip'd cpio archive) opens, has /init, and
24# carries every recovery tool the build script packages.
25# QEMU Only with --boot (boots THIS image in QEMU with zero disks attached
26# and requires the image's own completion line) or --serial-log FILE
27# (reads the console log of an earlier zero-disk boot; the receipt
28# then says the log is not bound to this image's hash).
29# hardware Never checked here. Internal NVMe unmounted and the ATLAS_RECOV
30# stick protected are facts about Machine 1, so they stay NOT_RUN
31# until the attended exercise records them.
32#
33# Usage: capture_gate1_receipt.sh [--boot | --serial-log FILE]
34# Env: AIENOS_GATE1_INITRD image to check (default /tmp/aienos-recovery-standalone-initrd.img,
35# built if missing)
36# AIENOS_GATE1_OUT_DIR where the receipt goes (default evidence/); the
37# file is named by its own sha256 and never overwritten
38# AIENOS_GATE1_KERNEL kernel for --boot (default newest /boot/vmlinuz-*)
39# Last line: GATE1_RECEIPT: PASS | FAIL | NOT_RUN <receipt path>. PASS needs host
40# PASS, a bound QEMU boot (--boot) and a clean tree; anything less is NOT_RUN.
41# Exit: 1 on FAIL, 2 on usage or setup errors, 0 otherwise (read the marker).
42# Invariant: NO PLAINTEXT SECRETS IN REPOSITORY OR BUILD ARTIFACTS.
43
44set -euo pipefail
45
46REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
47cd "${REPO_ROOT}"
48
49mode="none"
50serial_log=""
51case "${1:-}" in
52 "") ;;
53 --boot) mode="boot" ;;
54 --serial-log) mode="log"; serial_log="${2:-}"; [[ -n "${serial_log}" ]] || { echo "usage: $0 [--boot | --serial-log FILE]" >&2; exit 2; } ;;
55 *) echo "usage: $0 [--boot | --serial-log FILE]" >&2; exit 2 ;;
56esac
57
58INITRD_IMG="${AIENOS_GATE1_INITRD:-/tmp/aienos-recovery-standalone-initrd.img}"
59OUT_DIR="${AIENOS_GATE1_OUT_DIR:-${REPO_ROOT}/evidence}"
60WORK_DIR=$(mktemp -d)
61trap 'rm -rf "${WORK_DIR}"' EXIT
62
63json_str() { printf '"%s"' "$(printf '%s' "$1" | sed -e 's/\\/\\\\/g' -e 's/"/\\"/g')"; }
64
65# ---- Source identity -------------------------------------------------------
66commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)"
67if [[ "${commit}" == unknown ]]; then tree_dirty="unknown"
68elif [[ -n "$(git status --porcelain 2>/dev/null)" ]]; then tree_dirty="true"
69else tree_dirty="false"; fi
70
71# ---- Host checks on the image ----------------------------------------------
72if [[ ! -f "${INITRD_IMG}" && -z "${AIENOS_GATE1_INITRD:-}" ]]; then
73 bash "${REPO_ROOT}/scripts/build_standalone_recovery_initrd.sh" "${INITRD_IMG}"
74fi
75
76host_status="PASS"
77host_notes=()
78host_fail() { host_status="FAIL"; host_notes+=("$1"); echo "FAIL host: $1"; }
79
80initrd_sha="null"; initrd_size="null"; init_status="FAIL"
81# Every tool build_standalone_recovery_initrd.sh copies into bin/.
82EXPECTED=(busybox sh bash lsblk blkid mount umount mkdir cat grep sed sha256sum age
83 sbverify fusermount3 tpm2_pcrread efibootmgr findmnt cryptsetup fsck.vfat
84 mkfs.vfat fsck.ext4 chroot gocryptfs)
85present=(); missing=()
86if [[ ! -f "${INITRD_IMG}" ]]; then
87 host_fail "image not found: ${INITRD_IMG}"
88 missing=("${EXPECTED[@]}")
89else
90 initrd_sha=$(json_str "$(sha256sum "${INITRD_IMG}" | awk '{print $1}')")
91 initrd_size=$(stat -c%s "${INITRD_IMG}")
92 if ! gzip -t "${INITRD_IMG}" 2>/dev/null; then
93 host_fail "image is not a valid gzip stream"
94 elif ! gzip -dc "${INITRD_IMG}" | cpio -t --quiet > "${WORK_DIR}/list" 2>/dev/null; then
95 host_fail "image is not a readable cpio archive"
96 fi
97 sed -e 's|^\./||' "${WORK_DIR}/list" 2>/dev/null > "${WORK_DIR}/names" || : > "${WORK_DIR}/names"
98 init_status="PASS"
99 grep -qx 'init' "${WORK_DIR}/names" || { init_status="FAIL"; host_fail "image has no /init (rdinit=/init would not start)"; }
100 for t in "${EXPECTED[@]}"; do
101 if grep -qx "bin/${t}" "${WORK_DIR}/names"; then present+=("/bin/${t}"); else missing+=("/bin/${t}"); fi
102 done
103 [[ ${#missing[@]} -eq 0 ]] || host_fail "image lacks ${#missing[@]} recovery tool(s): ${missing[*]}"
104fi
105[[ "${host_status}" == FAIL ]] || echo "PASS host: image opens, has /init and all ${#EXPECTED[@]} recovery tools"
106
107# ---- QEMU zero-disk boot ----------------------------------------------------
108qemu_status="NOT_RUN"; qemu_note="not requested (pass --boot or --serial-log FILE)"
109qemu_bound="false"; kernel_path="null"; kernel_sha="null"; log_sha="null"
110markers=("AIENOS Standalone Hardware Recovery Core"
111 "TEST MODE DETECTED: automated recovery verification complete")
112
113if [[ "${mode}" == boot ]]; then
114 quiet="${AIENOS_QUIET_FLAG:-${HOME}/workspace/.spark-quiet}"
115 kernel="${AIENOS_GATE1_KERNEL:-$(ls -1t /boot/vmlinuz-* 2>/dev/null | head -n 1 || true)}"
116 if [[ -e "${quiet}" ]]; then
117 qemu_note="quiet flag ${quiet} is held; boot skipped"
118 elif ! command -v qemu-system-aarch64 >/dev/null; then
119 qemu_note="qemu-system-aarch64 not installed"
120 elif [[ "$(uname -m)" != aarch64 && -z "${AIENOS_GATE1_KERNEL:-}" ]]; then
121 qemu_note="host is $(uname -m); set AIENOS_GATE1_KERNEL to an AArch64 kernel"
122 elif [[ "${host_status}" != PASS ]]; then
123 qemu_note="host checks failed; image not booted"
124 elif [[ -z "${kernel}" ]] || ! { cp "${kernel}" "${WORK_DIR}/vmlinuz" 2>/dev/null || sudo -n cp "${kernel}" "${WORK_DIR}/vmlinuz" 2>/dev/null; }; then
125 qemu_note="kernel not readable: ${kernel:-none found}"
126 else
127 kernel_path=$(json_str "${kernel}")
128 kernel_sha=$(json_str "$(sha256sum "${WORK_DIR}/vmlinuz" | awk '{print $1}')")
129 serial_log="${WORK_DIR}/serial.log"
130 set +e
131 timeout "${AIENOS_QEMU_TIMEOUT:-60}" qemu-system-aarch64 \
132 -M virt -cpu max -smp 2 -m 1024 \
133 -kernel "${WORK_DIR}/vmlinuz" -initrd "${INITRD_IMG}" \
134 -append "rdinit=/init aienos.test=1 console=ttyAMA0 panic=1 quiet" \
135 -display none -nic none -serial file:"${serial_log}" -no-reboot
136 set -e
137 qemu_bound="true"
138 fi
139elif [[ "${mode}" == log ]]; then
140 [[ -f "${serial_log}" ]] || { echo "serial log not found: ${serial_log}" >&2; exit 2; }
141 qemu_note="console log supplied; the image that produced it is not bound to this receipt's image hash"
142fi
143
144if [[ -n "${serial_log}" && -f "${serial_log}" ]]; then
145 log_sha=$(json_str "$(sha256sum "${serial_log}" | awk '{print $1}')")
146 tr -d '\r' < "${serial_log}" > "${WORK_DIR}/serial.txt"
147 qemu_status="PASS"
148 for m in "${markers[@]}"; do
149 if ! grep -qF -- "${m}" "${WORK_DIR}/serial.txt"; then
150 qemu_status="FAIL"; echo "FAIL QEMU: console never printed: ${m}"
151 fi
152 done
153 if [[ "${qemu_status}" == PASS ]]; then
154 echo "PASS QEMU: zero-disk boot reached the recovery completion line"
155 [[ "${qemu_bound}" != true ]] || qemu_note="booted this image with zero disks attached"
156 else
157 qemu_note="zero-disk boot did not reach the recovery completion line"
158 fi
159fi
160echo "${qemu_status} QEMU: ${qemu_note}"
161
162# ---- Verdict ----------------------------------------------------------------
163if [[ "${host_status}" == FAIL || "${qemu_status}" == FAIL ]]; then status="FAIL"
164elif [[ "${qemu_status}" == PASS && "${qemu_bound}" == true && "${tree_dirty}" == false ]]; then status="PASS"
165else status="NOT_RUN"; fi
166
167json_list() { local first=1; printf '['; for x in "$@"; do [[ $first == 1 ]] || printf ', '; first=0; json_str "$x"; done; printf ']'; }
168notes_json=$(json_list "${host_notes[@]+"${host_notes[@]}"}")
169
170mkdir -p "${OUT_DIR}"
171tmp_receipt="${WORK_DIR}/receipt.json"
172cat > "${tmp_receipt}" << JSON_EOF
173{
174 "gate": "TRUST-1 Gate 1",
175 "name": "standalone_ram_recovery_media",
176 "status": "${status}",
177 "status_rule": "PASS needs host PASS, a QEMU zero-disk boot of this exact image and a clean tree at a known commit; any FAIL is FAIL; otherwise NOT_RUN",
178 "evidence_levels": {
179 "host": "${host_status}",
180 "qemu": "${qemu_status}",
181 "hardware": "NOT_RUN"
182 },
183 "commit": $(json_str "${commit}"),
184 "tree_dirty": $( [[ "${tree_dirty}" == unknown ]] && echo '"unknown"' || echo "${tree_dirty}"),
185 "captured_utc": "$(date -u +"%Y-%m-%dT%H:%M:%SZ")",
186 "host": $(json_str "$(uname -n)"),
187 "arch": "$(uname -m)",
188 "artifacts": {
189 "standalone_initrd": {
190 "path": $(json_str "${INITRD_IMG}"),
191 "size_bytes": ${initrd_size},
192 "sha256": ${initrd_sha},
193 "host_check_failures": ${notes_json}
194 },
195 "qemu_kernel": { "path": ${kernel_path}, "sha256": ${kernel_sha} },
196 "qemu_serial_log_sha256": ${log_sha}
197 },
198 "checks": {
199 "init_entrypoint_present": "${init_status}",
200 "recovery_tools_packaged": "$( [[ ${#missing[@]} -eq 0 ]] && echo PASS || echo FAIL)",
201 "qemu_zero_disk_boot": "${qemu_status}",
202 "qemu_boot_bound_to_image_hash": ${qemu_bound},
203 "qemu_note": $(json_str "${qemu_note}"),
204 "internal_nvme_unmounted": "NOT_RUN",
205 "atlas_recov_protected": "NOT_RUN",
206 "hardware_note": "Machine 1 facts; only the attended exercise can record them",
207 "secrets_policy": "NO PLAINTEXT SECRETS IN REPOSITORY OR BUILD ARTIFACTS"
208 },
209 "utilities_packaged": $(json_list "${present[@]+"${present[@]}"}"),
210 "utilities_missing": $(json_list "${missing[@]+"${missing[@]}"}")
211}
212JSON_EOF
213
214receipt_sha=$(sha256sum "${tmp_receipt}" | awk '{print $1}')
215RECEIPT_FILE="${OUT_DIR}/gate1_recovery_receipt_${receipt_sha}.json"
216cp "${tmp_receipt}" "${RECEIPT_FILE}"
217echo "GATE1_RECEIPT: ${status} ${RECEIPT_FILE}"
218[[ "${status}" != FAIL ]] || exit 1
219exit 0