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

126 lines · 5543 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# test_recovery_unlock_chroot.sh: TRUST-1 Gate 1 items 11-12, host rehearsal.
18#
19# Proves the recovery image's OWN binaries can do the recovery unlock path:
20# age decrypts a passfile spare, and gocryptfs mounts a gocryptfs store with
21# it (read-only), exactly as the rescue shell would. Everything is a
22# throwaway made here in a private temp folder: a test age identity, a test
23# gocryptfs store, a test file. No owner key, no real store, no hardware,
24# no USB stick. The real Spark stores and spares are never read.
25#
26# Runs the image's binaries inside a chroot of the extracted initrd, which
27# needs root (sudo). Without passwordless sudo it SKIPs (exit 0). MANUAL
28# ONLY: not part of verify_all, because it needs root.
29#
30# Usage: test_recovery_unlock_chroot.sh [INITRD_IMG]
31# Exit 0 = PASS or SKIP, 1 = FAIL, 2 = missing tools.
32
33set -euo pipefail
34
35repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
36
37for t in age age-keygen gocryptfs gzip cpio; do
38 command -v "${t}" >/dev/null || { echo "Error: ${t} not installed" >&2; exit 2; }
39done
40if ! sudo -n true 2>/dev/null; then
41 echo "SKIP recovery unlock rehearsal needs passwordless sudo for chroot"
42 exit 0
43fi
44
45work="$(mktemp -d)"
46root="${work}/root"
47cleanup() {
48 # SAFETY: never delete across a mount. Unmount everything under the
49 # temp folder (lazy detach as a fallback), then refuse to delete at all
50 # if any mount is still there. rm also stays on one filesystem.
51 local m
52 for m in "${root}/mnt/plain" "${root}/proc" "${root}/dev"; do
53 if findmnt "${m}" >/dev/null 2>&1; then
54 sudo -n umount "${m}" 2>/dev/null || sudo -n umount -l "${m}" 2>/dev/null || true
55 fi
56 done
57 if findmnt -rn -o TARGET | grep -qF "${work}"; then
58 echo "WARNING: mounts remain under ${work}; NOT deleting it" >&2
59 return
60 fi
61 sudo -n rm -rf --one-file-system "${work}"
62}
63trap cleanup EXIT
64
65img="${1:-${work}/initrd.img}"
66if [[ $# -lt 1 ]]; then
67 bash "${repo_root}/scripts/build_standalone_recovery_initrd.sh" "${img}" >/dev/null
68fi
69[[ -f "${img}" ]] || { echo "FAIL initrd not found: ${img}"; exit 1; }
70mkdir -p "${root}"
71gzip -dc "${img}" | (cd "${root}" && cpio -idm --quiet)
72
73# Throwaway material, built with the HOST tools (stands in for the spares
74# made on the Spark): a test identity, a test store, an age-wrapped passfile.
75age-keygen -o "${work}/test-identity.txt" 2>/dev/null
76recipient="$(age-keygen -y "${work}/test-identity.txt")"
77head -c 32 /dev/urandom | od -An -tx1 | tr -d ' \n' >"${work}/pass"
78mkdir -p "${work}/cipher" "${work}/plain-host"
79gocryptfs -q -init -passfile "${work}/pass" "${work}/cipher"
80gocryptfs -q -passfile "${work}/pass" "${work}/cipher" "${work}/plain-host"
81head -c 4096 /dev/urandom >"${work}/plain-host/known-file"
82want="$(sha256sum "${work}/plain-host/known-file" | cut -d' ' -f1)"
83fusermount3 -u "${work}/plain-host"
84age -r "${recipient}" -o "${work}/pass.age" "${work}/pass"
85rm -f "${work}/pass"
86
87# Stage into the image root as the operator would bring them on the
88# second medium: the store, the spare, and the identity.
89mkdir -p "${root}/media/test"
90cp -a "${work}/cipher" "${root}/mnt/cipher/store"
91cp "${work}/pass.age" "${work}/test-identity.txt" "${root}/media/test/"
92
93# A private, throwaway /dev for the chroot (never a bind of the host /dev):
94# only the nodes the recipe needs.
95sudo -n mount -t tmpfs -o mode=0755,size=1m aienos-test-dev "${root}/dev"
96sudo -n mknod -m 0666 "${root}/dev/null" c 1 3
97sudo -n mknod -m 0666 "${root}/dev/zero" c 1 5
98sudo -n mknod -m 0666 "${root}/dev/urandom" c 1 9
99sudo -n mknod -m 0666 "${root}/dev/random" c 1 8
100sudo -n mknod -m 0666 "${root}/dev/fuse" c 10 229
101sudo -n mount -t proc proc "${root}/proc"
102
103fail() { echo "FAIL $*"; exit 1; }
104
105# The rescue-shell recipe, run only with the image's binaries. The passfile
106# is decrypted into RAM (tmpfs in the real shell) and removed after mount.
107sudo -n chroot "${root}" /bin/sh -c '
108 set -e
109 /bin/busybox --install -s /bin
110 /bin/age -d -i /media/test/test-identity.txt -o /tmp/pass /media/test/pass.age
111 /bin/gocryptfs -q -nosyslog -ro -passfile /tmp/pass /mnt/cipher/store /mnt/plain
112 rm -f /tmp/pass
113' || fail "image age + gocryptfs could not open the test store"
114
115got="$(sudo -n chroot "${root}" /bin/sha256sum /mnt/plain/known-file | cut -d' ' -f1)"
116[[ "${got}" == "${want}" ]] || fail "known file digest differs (${got} != ${want})"
117echo "PASS image age decrypted the spare passfile; image gocryptfs mounted the store read-only"
118echo "PASS known test file read back intact (sha256 ${want})"
119
120if sudo -n chroot "${root}" /bin/sh -c 'echo x > /mnt/plain/write-probe' 2>/dev/null; then
121 fail "store was writable; recovery mount must be read-only"
122fi
123echo "PASS recovery mount refuses writes"
124
125sudo -n umount "${root}/mnt/plain"
126echo "TRUST-1 Gate 1 recovery unlock rehearsal (throwaway keys, chroot): ALL PASS"