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

203 lines · 9382 bytes Raw Blame History
1#!/bin/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# collect_recovery_boot_evidence.sh: Record evidence that the AIENOS recovery
18# USB actually booted Machine 1 into a rescue environment that can mount the
19# NVMe root, repair /boot/efi, and restore boot entries (issue #17).
20#
21# Run this on Machine 1 (the DGX Spark), booted from the recovery USB, under:
22# aien-proof hold --resource machine-1 --job recovery-boot-evidence -- \
23# bash scripts/collect_recovery_boot_evidence.sh /mnt/root
24#
25# Optional first argument: where the NVMe root is already mounted (default
26# /mnt/root). This script is READ-ONLY: it never formats, never deletes,
27# never writes to the ESP or the NVMe root, and never changes BootOrder. It
28# only inspects mount state, filesystem presence, and firmware variables
29# that are already exposed read-only by the kernel.
30# Gate 1 items 11-12 (recovery unlock) run only when the operator sets
31# AIENOS_UNLOCK_CIPHER, AIENOS_UNLOCK_SPARE and AIENOS_UNLOCK_IDENTITY for
32# that run; the store is then opened read-only and the passfile lives only
33# in RAM (/tmp) and is deleted straight after the mount.
34set -uo pipefail
35
36ROOT_MNT="${1:-/mnt/root}"
37ESP_MNT="${AIENOS_ESP_MNT:-${ROOT_MNT}/boot/efi}"
38FAILED=0
39
40check() { # name, pass(0/1), detail
41 if [[ "$2" == 1 ]]; then
42 echo "PASS $1: $3"
43 else
44 echo "FAIL $1: $3"
45 FAILED=1
46 fi
47}
48
49echo "== recovery environment identity"
50echo "captured_utc: $(date -u +%Y-%m-%dT%H:%M:%SZ 2>/dev/null || echo unknown)"
51echo "kernel: $(uname -r 2>/dev/null || echo unknown)"
52echo "cmdline: $(cat /proc/cmdline 2>/dev/null || echo unknown)"
53echo "hostname: $(cat /proc/sys/kernel/hostname 2>/dev/null || echo unknown)"
54
55echo "== media identity (this boot's root)"
56root_src="$(findmnt -no SOURCE / 2>/dev/null || echo unknown)"
57echo "root_source: ${root_src}"
58check "booted_from_removable_media" \
59 "$(findmnt -no SOURCE / 2>/dev/null | grep -qE 'sd|usb|mapper|/dev/ram|rootfs|tmpfs' && echo 1 || echo 0)" \
60 "root is ${root_src} (not the internal NVMe)"
61
62echo "== secure boot state"
63sb="unknown"
64sb_var="/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c"
65if [[ -r "${sb_var}" ]]; then
66 sb="$(od -An -t u1 -j 4 -N 1 "${sb_var}" 2>/dev/null | tr -d ' ')"
67fi
68echo "secure_boot_byte: ${sb}"
69check "secure_boot_state_recorded" "$([[ "${sb}" == "0" || "${sb}" == "1" ]] && echo 1 || echo 0)" \
70 "byte=${sb} (1=enabled)"
71
72echo "== NVMe root mount"
73check "nvme_root_mounted" "$(findmnt "${ROOT_MNT}" >/dev/null 2>&1 && echo 1 || echo 0)" \
74 "findmnt ${ROOT_MNT}"
75root_dev="$(findmnt -no SOURCE "${ROOT_MNT}" 2>/dev/null || echo unknown)"
76echo "nvme_root_device: ${root_dev}"
77check "nvme_root_is_internal" \
78 "$(echo "${root_dev}" | grep -qE 'nvme|mapper' && echo 1 || echo 0)" \
79 "source ${root_dev}"
80root_opts="$(findmnt -no OPTIONS "${ROOT_MNT}" 2>/dev/null || echo unknown)"
81echo "nvme_root_mount_options: ${root_opts}"
82check "nvme_root_mounted_readonly" \
83 "$(case ",${root_opts}," in *,ro,*) echo 1 ;; *) echo 0 ;; esac)" \
84 "mount options: ${root_opts}"
85
86echo "== /boot/efi access (read-only inspection; nothing is written here)"
87esp_ok=0
88if findmnt "${ESP_MNT}" >/dev/null 2>&1 || mountpoint -q "${ESP_MNT}" 2>/dev/null; then
89 esp_ok=1
90fi
91check "esp_mounted" "${esp_ok}" "findmnt ${ESP_MNT}"
92
93esp_fallback_present=0
94if [[ "${esp_ok}" == 1 && -f "${ESP_MNT}/EFI/BOOT/BOOTAA64.EFI" ]]; then
95 esp_fallback_present=1
96fi
97check "esp_fallback_bootloader_present" "${esp_fallback_present}" \
98 "${ESP_MNT}/EFI/BOOT/BOOTAA64.EFI"
99
100esp_opts="$(findmnt -no OPTIONS "${ESP_MNT}" 2>/dev/null || echo unknown)"
101echo "esp_mount_options: ${esp_opts}"
102check "esp_mounted_readonly" \
103 "$(case ",${esp_opts}," in *,ro,*) echo 1 ;; *) echo 0 ;; esac)" \
104 "mount options: ${esp_opts}"
105
106echo "== boot entry restore capability"
107efi_vars=0
108[[ -d /sys/firmware/efi/efivars ]] && efi_vars=1
109check "efi_variables_present" "${efi_vars}" "/sys/firmware/efi/efivars"
110entries="$(efibootmgr 2>/dev/null || true)"
111echo "${entries}" | sed -n '1,6p'
112check "efibootmgr_readable" "$(grep -q '^Boot' <<<"${entries}" && echo 1 || echo 0)" \
113 "efibootmgr lists boot entries"
114
115echo "== TRUST-1 Gate 1 item 6: disks and encrypted stores (read-only)"
116lsblk -o NAME,SIZE,TYPE,FSTYPE,LABEL 2>/dev/null || echo "lsblk unavailable"
117# The Spark's encrypted stores are gocryptfs folders on the NVMe root.
118# Default: the two known store locations, matched for any home folder on the root (AIENOS_CIPHER_DIRS overrides).
119if [[ -n "${AIENOS_CIPHER_DIRS:-}" ]]; then
120 CIPHER_DIRS="${AIENOS_CIPHER_DIRS}"
121else
122 CIPHER_DIRS=""
123 for d in "${ROOT_MNT}"/home/*/atlas-runtime-setup-*/private-cipher "${ROOT_MNT}"/home/*/workspace/atlas-forgejo/cipher; do
124 [[ -d "${d}" ]] && CIPHER_DIRS="${CIPHER_DIRS:+${CIPHER_DIRS} }${d#"${ROOT_MNT}"}"
125 done
126 [[ -n "${CIPHER_DIRS}" ]] || echo "gocryptfs_store: no store at the default locations under ${ROOT_MNT}"
127fi
128stores_seen=0
129for c in ${CIPHER_DIRS}; do
130 conf="${ROOT_MNT}${c}/gocryptfs.conf"
131 if [[ -f "${conf}" ]]; then
132 echo "gocryptfs_store: ${c} conf_sha256=$(sha256sum "${conf}" | cut -d' ' -f1)"
133 stores_seen=$((stores_seen + 1))
134 else
135 echo "gocryptfs_store: ${c} NOT FOUND under ${ROOT_MNT}"
136 fi
137done
138check "encrypted_stores_identified" "$([[ ${stores_seen} -gt 0 ]] && echo 1 || echo 0)" \
139 "${stores_seen} gocryptfs store(s) found on the NVMe root"
140
141echo "== TRUST-1 Gate 1 item 8: PCRs and event log (read-only)"
142export TPM2TOOLS_TCTI="${TPM2TOOLS_TCTI:-device:/dev/tpmrm0}"
143# FLAG(sovereignty): tpm2_pcrread run inside the recovery image; replace with in-house C PCR reader.
144pcrs="$(tpm2_pcrread sha256 2>/dev/null || true)"
145echo "${pcrs}" | sed -n '1,26p'
146check "pcrs_read" "$(grep -qE '^ +7 *:' <<<"${pcrs}" && echo 1 || echo 0)" "tpm2_pcrread sha256"
147evlog=/sys/kernel/security/tpm0/binary_bios_measurements
148if [[ -r "${evlog}" ]]; then
149 echo "event_log_sha256: $(sha256sum "${evlog}" | cut -d' ' -f1)"
150 echo "event_log_bytes: $(wc -c <"${evlog}")"
151fi
152check "event_log_recorded" "$([[ -r "${evlog}" ]] && echo 1 || echo 0)" "${evlog}"
153
154echo "== TRUST-1 Gate 1 items 9-10: AIENOS slots, loader hashes and signatures (read-only)"
155if [[ "${esp_ok}" == 1 ]]; then
156 ls -la "${ESP_MNT}/EFI" 2>/dev/null
157 ls -laR "${ESP_MNT}"/EFI/AIENOS* 2>/dev/null || echo "aienos_slots: none on this ESP yet (expected before Gate 6)"
158 loaders=0
159 while IFS= read -r -d '' efi; do
160 echo "efi_image: ${efi#"${ESP_MNT}"} sha256=$(sha256sum "${efi}" | cut -d' ' -f1)"
161 sbverify --list "${efi}" 2>/dev/null | sed -n 's/^ *\(subject\|issuer\):/ \1:/p' | head -n 4
162 loaders=$((loaders + 1))
163 done < <(find "${ESP_MNT}" -type f -iname '*.efi' -print0 2>/dev/null)
164 check "esp_loaders_hashed" "$([[ ${loaders} -gt 0 ]] && echo 1 || echo 0)" "${loaders} EFI image(s) hashed"
165else
166 check "esp_loaders_hashed" 0 "ESP not mounted"
167fi
168
169echo "== TRUST-1 Gate 1 items 11-12: recovery unlock (only when explicitly authorized)"
170# Runs ONLY when the operator sets all three variables for this run. Opens
171# ONE store read-only with the offline spare: the passfile is decrypted into
172# RAM (/tmp), used once and deleted; nothing is written to the NVMe.
173if [[ -n "${AIENOS_UNLOCK_CIPHER:-}" && -n "${AIENOS_UNLOCK_SPARE:-}" && -n "${AIENOS_UNLOCK_IDENTITY:-}" ]]; then
174 plain="${AIENOS_UNLOCK_MNT:-/mnt/plain}"
175 mkdir -p "${plain}"
176 unlock_ok=0
177 if age -d -i "${AIENOS_UNLOCK_IDENTITY}" -o /tmp/aienos-unlock-pass "${AIENOS_UNLOCK_SPARE}" \
178 && gocryptfs -q -nosyslog -ro -passfile /tmp/aienos-unlock-pass "${AIENOS_UNLOCK_CIPHER}" "${plain}"; then
179 unlock_ok=1
180 fi
181 rm -f /tmp/aienos-unlock-pass
182 check "recovery_unlock_readonly" "${unlock_ok}" "store ${AIENOS_UNLOCK_CIPHER} opened read-only with the offline spare"
183 if [[ "${unlock_ok}" == 1 && -n "${AIENOS_UNLOCK_EXPECT:-}" ]]; then
184 # AIENOS_UNLOCK_EXPECT="relative/path sha256" of the harmless test file.
185 exp_path="${AIENOS_UNLOCK_EXPECT% *}"
186 exp_sha="${AIENOS_UNLOCK_EXPECT##* }"
187 got_sha="$(sha256sum "${plain}/${exp_path}" 2>/dev/null | cut -d' ' -f1)"
188 echo "test_artifact: ${exp_path} sha256=${got_sha:-unreadable}"
189 check "test_artifact_round_trip" "$([[ "${got_sha}" == "${exp_sha}" ]] && echo 1 || echo 0)" \
190 "expected ${exp_sha}"
191 fi
192 [[ "${unlock_ok}" == 1 ]] && { umount "${plain}" 2>/dev/null || fusermount3 -u "${plain}"; }
193else
194 echo "recovery_unlock: not authorized for this run (set AIENOS_UNLOCK_CIPHER, AIENOS_UNLOCK_SPARE, AIENOS_UNLOCK_IDENTITY)"
195fi
196
197echo "== verdict"
198if [[ "${FAILED}" == 0 ]]; then
199 echo "RECOVERY_BOOT_GATE: PASS"
200else
201 echo "RECOVERY_BOOT_GATE: FAIL"
202 exit 1
203fi