aienux
Aienux, the AienOS kernel: boot, scheduling, IPC and capability boundaries. Pre-alpha; host, emulator and physical evidence are labelled separately.
Brought by drakestapleton
aienux / scripts / verify_recovery_tools.sh
| 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 | # verify_recovery_tools.sh: Verify that the standalone recovery initrd ships |
| 18 | # the tooling issue #17 requires: mount the NVMe root, repair /boot/efi, and |
| 19 | # restore boot entries. Host-side, static check only: extracts the built |
| 20 | # initrd and greps for files and init hooks. No real devices, no root, no |
| 21 | # QEMU boot (qemu_verify_recovery_media.sh already covers the boot itself). |
| 22 | # Verdict: PASS only when every check ran and passed; FAIL on any failed check |
| 23 | # (exit 1); NOT_RUN (exit 3) when a check class was skipped (readelf missing, or |
| 24 | # the test-only no-gocryptfs allowance in use). A skip is never a PASS. |
| 25 | # bash scripts/verify_recovery_tools.sh --self-test negative controls, no build. |
| 26 | # Zero Disk Secrets and Unslop compliant. |
| 27 | |
| 28 | set -euo pipefail |
| 29 | |
| 30 | REPO_ROOT="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 31 | cd "${REPO_ROOT}" |
| 32 | |
| 33 | # --self-test: negative controls on a synthetic image. No build, no QEMU, no |
| 34 | # root. Proves the verdict follows the checks: a missing tool is FAIL, a |
| 35 | # skipped check class (no gocryptfs allowed, no readelf) is NOT_RUN (exit 3), |
| 36 | # and only a complete, unskipped image is PASS. |
| 37 | if [[ "${1:-}" == --self-test ]]; then |
| 38 | self="${REPO_ROOT}/scripts/verify_recovery_tools.sh" |
| 39 | command -v readelf >/dev/null || { echo "NOT_RUN self-test needs readelf on this host"; echo "RECOVERY_TOOLS_SELF_TEST: NOT_RUN"; exit 3; } |
| 40 | t="$(mktemp -d)"; trap 'rm -rf "${t}"' EXIT |
| 41 | st=0 |
| 42 | mk_root() { # dir: a complete synthetic recovery root |
| 43 | local d="$1" f |
| 44 | mkdir -p "${d}/bin" "${d}/etc" "${d}/usr/local/sbin" "${d}/lib/aarch64-linux-gnu" |
| 45 | for f in busybox mount lsblk blkid cryptsetup fsck.vfat mkfs.vfat fsck.ext4 efibootmgr chroot findmnt \ |
| 46 | tpm2_pcrread sbverify age gocryptfs fusermount3; do |
| 47 | : >"${d}/bin/${f}"; chmod +x "${d}/bin/${f}" |
| 48 | done |
| 49 | : >"${d}/usr/local/sbin/collect_recovery_boot_evidence" |
| 50 | : >"${d}/lib/aarch64-linux-gnu/libtss2-tcti-device.so.0" |
| 51 | printf 'nvme.ko\ndm-crypt.ko\n' >"${d}/etc/aienos-modules.order" |
| 52 | printf '#!/bin/sh\n# aienos.test=1\n# efibootmgr\nexec /bin/sh\n' >"${d}/init"; chmod +x "${d}/init" |
| 53 | } |
| 54 | mk_img() { (cd "$1" && find . | cpio -o -H newc --quiet 2>/dev/null) | gzip -c >"$2"; } |
| 55 | # expect NAME RC MARKER IMAGE [PATH_OVERRIDE] [ALLOW_NO_GOCRYPTFS] |
| 56 | expect() { |
| 57 | local name="$1" want_rc="$2" want="$3" img="$4" path="${5:-${PATH}}" allow="${6:-0}" rc=0 out |
| 58 | out="$(PATH="${path}" AIENOS_RECOVERY_ALLOW_NO_GOCRYPTFS="${allow}" /bin/bash "${self}" "${img}" 2>&1)" || rc=$? |
| 59 | if [[ "${rc}" == "${want_rc}" ]] && grep -q "^RECOVERY_TOOLS: ${want}" <<<"${out}"; then |
| 60 | echo "PASS ${name} -> ${want} (exit ${rc})" |
| 61 | else |
| 62 | echo "FAIL ${name}: want ${want} exit ${want_rc}, got exit ${rc}: $(grep '^RECOVERY_TOOLS:' <<<"${out}" || echo 'no verdict line')"; st=1 |
| 63 | fi |
| 64 | } |
| 65 | mk_root "${t}/full"; mk_img "${t}/full" "${t}/full.img" |
| 66 | expect "complete image" 0 PASS "${t}/full.img" |
| 67 | mk_root "${t}/noage"; rm "${t}/noage/bin/age"; mk_img "${t}/noage" "${t}/noage.img" |
| 68 | expect "mutant: age deleted from the image" 1 FAIL "${t}/noage.img" |
| 69 | mk_root "${t}/nogo"; rm "${t}/nogo/bin/gocryptfs"; mk_img "${t}/nogo" "${t}/nogo.img" |
| 70 | expect "mutant: gocryptfs missing, not allowed" 1 FAIL "${t}/nogo.img" |
| 71 | expect "gocryptfs missing, test-only allowance -> check skipped" 3 NOT_RUN "${t}/nogo.img" "${PATH}" 1 |
| 72 | # A PATH with the tools the script needs but no readelf: the closure check cannot run. |
| 73 | mkdir -p "${t}/nopath" |
| 74 | for c in gzip cpio sed grep mktemp rm mkdir readlink head cat dirname basename sort wc tr; do |
| 75 | ln -s "$(command -v "${c}")" "${t}/nopath/${c}" |
| 76 | done |
| 77 | expect "readelf absent -> library closure checks skipped" 3 NOT_RUN "${t}/full.img" "${t}/nopath" |
| 78 | if [[ ${st} == 0 ]]; then echo "RECOVERY_TOOLS_SELF_TEST: PASS"; exit 0; fi |
| 79 | echo "RECOVERY_TOOLS_SELF_TEST: FAIL"; exit 1 |
| 80 | fi |
| 81 | |
| 82 | WORK_DIR=$(mktemp -d) |
| 83 | trap 'rm -rf "${WORK_DIR}"' EXIT |
| 84 | |
| 85 | # With an argument: inspect exactly that image, and fail if it is missing |
| 86 | # (never substitute a freshly built one). Without: build a private copy in |
| 87 | # WORK_DIR, so parallel runs never share a fixed /tmp path and no stale |
| 88 | # user-owned file is left where a root build would later collide with it. |
| 89 | if [[ $# -ge 1 ]]; then |
| 90 | INITRD_IMG="$1" |
| 91 | if [[ ! -f "${INITRD_IMG}" ]]; then |
| 92 | echo "FAIL initrd image not found: ${INITRD_IMG}" >&2 |
| 93 | exit 1 |
| 94 | fi |
| 95 | else |
| 96 | INITRD_IMG="${WORK_DIR}/aienos-recovery-standalone-initrd.img" |
| 97 | # Repo checks run on hosts without gocryptfs (CI, laptops). Only there, and |
| 98 | # visibly, allow a test image without it. The real stick build |
| 99 | # (build_recovery_media.sh) never sets this. |
| 100 | if ! command -v gocryptfs >/dev/null 2>&1 && [[ ! -f /home/atlas/atlas-forgejo-setup-20260904/runtime/usr/bin/gocryptfs && -z "${AIENOS_GOCRYPTFS:-}" ]]; then |
| 101 | echo "SKIP gocryptfs not on this host; building a TEST-ONLY image without it" |
| 102 | export AIENOS_RECOVERY_ALLOW_NO_GOCRYPTFS=1 |
| 103 | fi |
| 104 | |
| 105 | echo "Building standalone recovery initrd..." |
| 106 | bash "${REPO_ROOT}/scripts/build_standalone_recovery_initrd.sh" "${INITRD_IMG}" |
| 107 | fi |
| 108 | |
| 109 | ROOT_DIR="${WORK_DIR}/root" |
| 110 | mkdir -p "${ROOT_DIR}" |
| 111 | |
| 112 | echo "Extracting ${INITRD_IMG}..." |
| 113 | gzip -dc "${INITRD_IMG}" | (cd "${ROOT_DIR}" && cpio -idm --quiet) |
| 114 | |
| 115 | FAILED=0 |
| 116 | SKIPS=() |
| 117 | check() { |
| 118 | if [[ "$2" == 1 ]]; then |
| 119 | echo "PASS $1" |
| 120 | else |
| 121 | echo "FAIL $1" |
| 122 | FAILED=1 |
| 123 | fi |
| 124 | } |
| 125 | |
| 126 | have_file() { [[ -f "${ROOT_DIR}/$1" || -x "${ROOT_DIR}/$1" || -L "${ROOT_DIR}/$1" ]] && echo 1 || echo 0; } |
| 127 | init_has() { grep -q -- "$1" "${ROOT_DIR}/init" 2>/dev/null && echo 1 || echo 0; } |
| 128 | module_packaged() { grep -qxF "$1.ko" "${ROOT_DIR}/etc/aienos-modules.order" 2>/dev/null && echo 1 || echo 0; } |
| 129 | |
| 130 | echo "" |
| 131 | echo "-- required tools for NVMe root mount --" |
| 132 | check "busybox present" "$(have_file bin/busybox)" |
| 133 | check "mount present" "$(have_file bin/mount)" |
| 134 | check "lsblk present" "$(have_file bin/lsblk)" |
| 135 | check "blkid present" "$(have_file bin/blkid)" |
| 136 | check "cryptsetup present" "$(have_file bin/cryptsetup)" |
| 137 | check "nvme kernel module packaged" "$(module_packaged nvme)" |
| 138 | check "dm-crypt kernel module packaged" "$(module_packaged dm-crypt)" |
| 139 | |
| 140 | echo "" |
| 141 | echo "-- required tools for /boot/efi repair --" |
| 142 | check "fsck.vfat present" "$(have_file bin/fsck.vfat)" |
| 143 | check "mkfs.vfat present" "$(have_file bin/mkfs.vfat)" |
| 144 | check "fsck.ext4 present" "$(have_file bin/fsck.ext4)" |
| 145 | check "efibootmgr present" "$(have_file bin/efibootmgr)" |
| 146 | |
| 147 | echo "" |
| 148 | echo "-- required tools for boot-entry restore --" |
| 149 | check "efibootmgr present (boot entries)" "$(have_file bin/efibootmgr)" |
| 150 | check "chroot present" "$(have_file bin/chroot)" |
| 151 | check "findmnt present for attended evidence collector" "$(have_file bin/findmnt)" |
| 152 | check "attended evidence collector packaged on media" "$(have_file usr/local/sbin/collect_recovery_boot_evidence)" |
| 153 | |
| 154 | echo "" |
| 155 | echo "-- TRUST-1 Gate 1 items 8, 10, 11-12: TPM, signatures, recovery unlock --" |
| 156 | # FLAG(sovereignty): tpm2_pcrread expected in the recovery image (outside dep); replace with in-house C PCR reader. |
| 157 | check "tpm2_pcrread present" "$(have_file bin/tpm2_pcrread)" |
| 158 | check "tpm2 device transport library packaged (dlopen, not seen by ldd)" \ |
| 159 | "$(compgen -G "${ROOT_DIR}/lib/*-linux-gnu/libtss2-tcti-device.so.0*" >/dev/null && echo 1 || echo 0)" |
| 160 | check "sbverify present" "$(have_file bin/sbverify)" |
| 161 | check "age present" "$(have_file bin/age)" |
| 162 | if [[ "${AIENOS_RECOVERY_ALLOW_NO_GOCRYPTFS:-0}" == 1 ]]; then |
| 163 | echo "SKIP gocryptfs present (TEST-ONLY image built on a host without gocryptfs)"; SKIPS+=("gocryptfs present (TEST-ONLY image without gocryptfs)") |
| 164 | else |
| 165 | check "gocryptfs present" "$(have_file bin/gocryptfs)" |
| 166 | fi |
| 167 | check "fusermount3 present" "$(have_file bin/fusermount3)" |
| 168 | |
| 169 | # Every shared library each recovery tool needs (and what those need in |
| 170 | # turn) must be inside the image itself, or the tool fails only later, in the |
| 171 | # rescue shell. Static check with readelf: no root, no execution. |
| 172 | lib_in_image() { |
| 173 | local d |
| 174 | for d in lib lib64 usr/lib lib/aarch64-linux-gnu usr/lib/aarch64-linux-gnu lib/x86_64-linux-gnu usr/lib/x86_64-linux-gnu; do |
| 175 | [[ -e "${ROOT_DIR}/${d}/$1" ]] && { echo "${ROOT_DIR}/${d}/$1"; return 0; } |
| 176 | done |
| 177 | return 1 |
| 178 | } |
| 179 | closure_ok() { # binary path inside image |
| 180 | local queue=("$1") seen=" " f lib p |
| 181 | while [[ ${#queue[@]} -gt 0 ]]; do |
| 182 | f="${queue[0]}"; queue=("${queue[@]:1}") |
| 183 | while read -r lib; do |
| 184 | [[ "${seen}" == *" ${lib} "* ]] && continue |
| 185 | seen+="${lib} " |
| 186 | p="$(lib_in_image "${lib}")" || { echo "missing ${lib} (needed by ${f##*/})" >&2; echo 0; return; } |
| 187 | queue+=("$(readlink -f "${p}")") |
| 188 | done < <(readelf -d "${f}" 2>/dev/null | sed -n 's/.*(NEEDED).*\[\(.*\)\]/\1/p') |
| 189 | done |
| 190 | echo 1 |
| 191 | } |
| 192 | if command -v readelf >/dev/null; then |
| 193 | # FLAG(sovereignty): tpm2_pcrread expected in the recovery image; follows the in-house C replacement. |
| 194 | for t in gocryptfs age sbverify tpm2_pcrread cryptsetup fusermount3 efibootmgr; do |
| 195 | [[ -e "${ROOT_DIR}/bin/${t}" ]] || continue |
| 196 | check "${t}: every shared library it needs is inside the image" "$(closure_ok "${ROOT_DIR}/bin/${t}")" |
| 197 | done |
| 198 | tcti="$(compgen -G "${ROOT_DIR}/lib/*-linux-gnu/libtss2-tcti-device.so.0" | head -n 1 || true)" |
| 199 | [[ -z "${tcti}" ]] || check "tpm2 device transport: its libraries are inside the image" "$(closure_ok "$(readlink -f "${tcti}")")" |
| 200 | else |
| 201 | echo "SKIP library closure checks (readelf not installed)"; SKIPS+=("library closure checks (readelf not installed)") |
| 202 | fi |
| 203 | |
| 204 | echo "" |
| 205 | echo "-- init hooks --" |
| 206 | check "init is executable" "$([[ -x "${ROOT_DIR}/init" ]] && echo 1 || echo 0)" |
| 207 | check "init keeps zero-disk test mode (aienos.test=1)" "$(init_has 'aienos.test=1')" |
| 208 | check "init reports EFI boot entries (efibootmgr)" "$(init_has 'efibootmgr')" |
| 209 | check "init drops into an interactive maintenance shell" "$(init_has 'exec /bin/sh')" |
| 210 | |
| 211 | echo "" |
| 212 | if [[ "${FAILED}" != 0 ]]; then |
| 213 | echo "RECOVERY_TOOLS: FAIL" |
| 214 | exit 1 |
| 215 | fi |
| 216 | # A skipped check is not a passed check: say NOT_RUN and exit 3, never PASS. |
| 217 | if [[ ${#SKIPS[@]} -gt 0 ]]; then |
| 218 | printf 'NOT_RUN skipped check: %s\n' "${SKIPS[@]}" |
| 219 | echo "RECOVERY_TOOLS: NOT_RUN (${#SKIPS[@]} check(s) skipped; every other check passed)" |
| 220 | exit 3 |
| 221 | fi |
| 222 | echo "RECOVERY_TOOLS: PASS (initrd ships mount, EFI repair, and boot-entry restore capability)" |