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

211 lines · 9956 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# trust1_gate7_preflight.sh: TRUST-1 Gate 7 pre-flight check (read-only).
18#
19# Checks, before the attended Gate 7 steps, everything that can be checked
20# without the operator's reboot measurements: the tools the stick and the
21# snapshot/compare steps need, the offline spare and the encrypted storage
22# folder, the TPM, Secure Boot, and (if the stick is plugged in and open, or
23# an image path is given) the tools inside the recovery image.
24#
25# Safety rules (after the 2026-09-30 /dev deletion incident):
26# - changes nothing on the machine: no sudo, no mount, no chroot, no efivar
27# or boot-entry writes, no TPM writes;
28# - never creates, removes or binds anything under /dev (it only looks at
29# /dev entries with `test`, and reads PCRs through the TPM like
30# tpm_measurement_campaign.sh does);
31# - writes only inside one private temp folder, which it removes with
32# `rm --one-file-system` and only if nothing is mounted under it.
33#
34# Usage: bash scripts/trust1_gate7_preflight.sh [INITRD_IMAGE]
35# INITRD_IMAGE optional: a recovery initrd to inspect. Without it, the
36# image on a mounted AIENOSRECOV stick is used; if the stick
37# is not open, a private copy is built in a temp folder.
38# Exit: 0 = no FAIL, 1 = at least one FAIL.
39
40set -uo pipefail
41
42REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
43SPARE_DIR="${AIENOS_SPARE_DIR:-${HOME}/.config/atlas/offline-spares}"
44SPARE="${SPARE_DIR}/atlas-private-storage.passfile.age"
45CIPHER_DIR="${AIENOS_CIPHER_DIR:-/home/atlas/atlas-runtime-setup-20260905/private-cipher}"
46EVENTLOG="${AIENOS_EVENTLOG:-/sys/kernel/security/tpm0/binary_bios_measurements}"
47EFIVARS="${AIENOS_EFIVARS_DIR:-/sys/firmware/efi/efivars}"
48SB_VAR="${EFIVARS}/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c"
49
50TMP="$(mktemp -d /tmp/trust1-gate7-preflight.XXXXXX)" || { echo "FAIL could not create a temp folder"; exit 1; }
51cleanup() {
52 # Refuse to delete if anything is mounted inside the temp folder.
53 if findmnt -rn -o TARGET 2>/dev/null | grep -q "^${TMP}\(/\|$\)"; then
54 echo "WARNING: something is mounted under ${TMP}; leaving it in place" >&2
55 return
56 fi
57 rm -rf --one-file-system -- "${TMP}"
58}
59trap cleanup EXIT
60
61PASSES=0; FAILS=0; SKIPS=0
62pass() { echo "PASS $1"; PASSES=$((PASSES + 1)); }
63fail() { echo "FAIL $1"; FAILS=$((FAILS + 1)); }
64skip() { echo "SKIP $1"; SKIPS=$((SKIPS + 1)); }
65check() { if [[ "$2" == 1 ]]; then pass "$1"; else fail "$1"; fi; }
66yes_if() { if "$@" >/dev/null 2>&1; then echo 1; else echo 0; fi; }
67
68echo "TRUST-1 Gate 7 pre-flight (read-only; changes nothing)"
69echo ""
70
71echo "-- 1. The Spark's device list looks healthy (after the 2026-09-30 incident) --"
72check "/dev/null is a real device (not a plain file)" "$(yes_if test -c /dev/null)"
73check "the TPM device /dev/tpmrm0 exists" "$(yes_if test -c /dev/tpmrm0)"
74
75echo ""
76echo "-- 2. The TPM can be read --"
77check "you are in the tss group (allowed to read the TPM)" \
78 "$(id -nG | tr ' ' '\n' | grep -qx tss && echo 1 || echo 0)"
79check "the TPM device is readable by you" "$(yes_if test -r /dev/tpmrm0)"
80# FLAG(sovereignty): tpm2_pcrread (outside dep) on Linux host; replace with in-house C PCR reader.
81if command -v tpm2_pcrread >/dev/null 2>&1; then
82 check "the TPM answers a read of startup measurement 7 (Secure Boot state)" \
83 "$(yes_if tpm2_pcrread sha256:7)"
84else
85 fail "tpm2_pcrread is installed (needed to read the TPM)"
86fi
87check "the firmware's startup measurement log is readable" "$(yes_if test -r "${EVENTLOG}")"
88
89echo ""
90echo "-- 3. Secure Boot state can be read --"
91if command -v mokutil >/dev/null 2>&1; then
92 sb="$(mokutil --sb-state 2>/dev/null || true)"
93 check "Secure Boot reports ON (mokutil says: ${sb:-nothing})" \
94 "$([[ "${sb}" == *"SecureBoot enabled"* ]] && echo 1 || echo 0)"
95else
96 fail "mokutil is installed (reads the Secure Boot state)"
97fi
98if [[ -r "${SB_VAR}" ]]; then
99 # efivarfs: 4 attribute bytes, then the 1-byte value.
100 sb_byte="$(od -An -tu1 -j4 -N1 "${SB_VAR}" 2>/dev/null | tr -d ' ')"
101 check "the firmware's own Secure Boot setting reads 1 (ON), got '${sb_byte}'" \
102 "$([[ "${sb_byte}" == 1 ]] && echo 1 || echo 0)"
103else
104 fail "the firmware's Secure Boot setting is readable"
105fi
106
107echo ""
108echo "-- 4. Tools the stick build copies from Ubuntu are all present --"
109mapfile -t stick_bins < <(sed -n '/^BINARIES=(/,/^)/p' "${REPO_ROOT}/scripts/build_standalone_recovery_initrd.sh" \
110 | sed -n 's/^[[:space:]]*\(\/[^[:space:]]*\).*/\1/p')
111if [[ ${#stick_bins[@]} -eq 0 ]]; then
112 fail "read the list of stick tools from build_standalone_recovery_initrd.sh"
113else
114 missing=()
115 for b in "${stick_bins[@]}"; do [[ -e "${b}" ]] || missing+=("${b}"); done
116 check "all ${#stick_bins[@]} programs the stick build copies exist on Ubuntu${missing[*]:+ (missing: ${missing[*]})}" \
117 "$([[ ${#missing[@]} -eq 0 ]] && echo 1 || echo 0)"
118fi
119gocryptfs_bin="${AIENOS_GOCRYPTFS:-$(command -v gocryptfs 2>/dev/null || echo /home/atlas/atlas-forgejo-setup-20260904/runtime/usr/bin/gocryptfs)}"
120check "gocryptfs (opens the private storage) is where the stick build looks: ${gocryptfs_bin}" \
121 "$(yes_if test -f "${gocryptfs_bin}")"
122
123echo ""
124echo "-- 5. Snapshot, compare and stick scripts are present --"
125for s in tpm_measurement_campaign.sh build_recovery_media.sh build_standalone_recovery_initrd.sh \
126 verify_recovery_tools.sh collect_recovery_boot_evidence.sh; do
127 check "scripts/${s} is present" "$(yes_if test -f "${REPO_ROOT}/scripts/${s}")"
128done
129# FLAG(sovereignty): tpm2_pcrread/tpm2_eventlog required on Linux host; replace with in-house C tool.
130for t in tpm2_pcrread tpm2_eventlog mokutil efibootmgr sha256sum aien-proof; do
131 check "${t} is installed on Ubuntu" "$(yes_if command -v "${t}")"
132done
133
134echo ""
135echo "-- 6. Key ciphertext is where the docs say, and no secret key sits beside it --"
136if [[ -s "${SPARE}" ]]; then
137 hdr="$(head -n 1 "${SPARE}" 2>/dev/null)"
138 check "the offline spare is present and locked (age format): ${SPARE}" \
139 "$([[ "${hdr}" == "age-encryption.org/v1" ]] && echo 1 || echo 0)"
140else
141 fail "the offline spare is present and not empty: ${SPARE}"
142fi
143if [[ -d "${SPARE_DIR}" ]]; then
144 if grep -rlq 'AGE-SECRET-KEY-' "${SPARE_DIR}" 2>/dev/null; then
145 fail "no unlocking identity (secret key) is stored next to the spares on the Spark"
146 else
147 pass "no unlocking identity (secret key) is stored next to the spares on the Spark"
148 fi
149fi
150if [[ -d "${CIPHER_DIR}" ]]; then
151 if [[ -r "${CIPHER_DIR}/gocryptfs.conf" ]]; then
152 pass "the encrypted private storage folder and its gocryptfs.conf are present: ${CIPHER_DIR}"
153 elif [[ -e "${CIPHER_DIR}/gocryptfs.conf" ]]; then
154 skip "the encrypted storage's gocryptfs.conf exists but you may not read it (not a fault)"
155 else
156 fail "the encrypted private storage has its gocryptfs.conf: ${CIPHER_DIR}"
157 fi
158elif [[ -e "${CIPHER_DIR}" ]]; then
159 skip "the encrypted private storage folder exists but you may not look inside (not a fault)"
160else
161 fail "the encrypted private storage folder exists: ${CIPHER_DIR}"
162fi
163
164echo ""
165echo "-- 7. Tools inside the recovery image --"
166image=""; image_note=""
167if [[ $# -ge 1 ]]; then
168 image="$1"; image_note="the image you named"
169else
170 stick_mnt="$(findmnt -rn -o TARGET -S LABEL=AIENOSRECOV 2>/dev/null | head -n 1)"
171 if [[ -n "${stick_mnt}" ]]; then
172 image="${stick_mnt}/aienos-recovery/initrd.img"; image_note="the image on the plugged-in stick"
173 fi
174fi
175log="${TMP}/verify_recovery_tools.log"
176if [[ -n "${image}" ]]; then
177 if [[ -f "${image}" ]]; then
178 TMPDIR="${TMP}" bash "${REPO_ROOT}/scripts/verify_recovery_tools.sh" "${image}" >"${log}" 2>&1
179 rc=$?
180 else
181 echo "image not found: ${image}" >"${log}"; rc=1
182 fi
183else
184 image_note="a fresh private copy built now (stick not open; this is what Step 4 would write)"
185 TMPDIR="${TMP}" bash "${REPO_ROOT}/scripts/verify_recovery_tools.sh" >"${log}" 2>&1
186 rc=$?
187fi
188grep -E '^(FAIL|SKIP)|^image not found' "${log}" | sed 's/^/ /'
189if [[ ${rc} -eq 3 ]]; then
190 # verify_recovery_tools.sh exits 3 when a check class was skipped (NOT_RUN).
191 fail "every recovery tool is inside ${image_note}: NOT CHECKED in full (verify_recovery_tools.sh skipped a check class and reported NOT_RUN)"
192else
193 check "every recovery tool is inside ${image_note}" "$([[ ${rc} -eq 0 ]] && echo 1 || echo 0)"
194fi
195
196echo ""
197echo "-- Not checked here (need Drake's reboots or later gates) --"
198echo "NOT CHECKED startup measurements match across normal boots (Gate 2 reboot snapshots)"
199echo "NOT CHECKED owner key fingerprints and backups (Gate 3 key ceremony not run yet)"
200echo "NOT CHECKED owner-signed loader and its fingerprint (Gate 4)"
201echo "NOT CHECKED Secure Boot variables archived, Gates 0-6 PASS, operator approval"
202echo "NOT CHECKED the stick actually boots and unlocks with Secure Boot ON (Gate 1, Step 6)"
203echo "NOT CHECKED every Gate 7 checkpoint A-E (attended hardware boot)"
204
205echo ""
206echo "Summary: ${PASSES} passed, ${FAILS} failed, ${SKIPS} skipped"
207if [[ ${FAILS} -ne 0 ]]; then
208 echo "GATE7_PREFLIGHT: FAIL"
209 exit 1
210fi
211echo "GATE7_PREFLIGHT: PASS (only covers what can be checked before the reboots)"