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_native_rollback.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_native_rollback.sh |
| 18 | # Host-side automated verifier for AIENOS native-boot rollback observations. |
| 19 | # |
| 20 | # Consumes pre-boot and post-return evidence captures and determines whether |
| 21 | # rollback invariants held. |
| 22 | # |
| 23 | # Stable result verdicts: |
| 24 | # PASS - All rollback invariants satisfied. |
| 25 | # FAIL - One or more invariants violated (e.g. BootOrder changed, root modified, BootNext stuck). |
| 26 | # BLOCKED - Hardware qualification blocked by trust chain / Secure Boot. |
| 27 | # INCOMP - Incomplete evidence or missing required capture files. |
| 28 | |
| 29 | set -euo pipefail |
| 30 | |
| 31 | usage() { |
| 32 | cat <<EOF |
| 33 | Usage: |
| 34 | Capture pre-boot baseline: |
| 35 | $0 --capture-pre <OUT_JSON> |
| 36 | |
| 37 | Capture post-return evidence: |
| 38 | $0 --capture-post <OUT_JSON> |
| 39 | |
| 40 | Verify pre/post observations: |
| 41 | $0 --verify --pre <PRE_JSON> --post <POST_JSON> [--candidate <IMAGE_EFI>] |
| 42 | EOF |
| 43 | exit 2 |
| 44 | } |
| 45 | |
| 46 | capture_state() { |
| 47 | local out_file="$1" |
| 48 | local sb_state="unknown" |
| 49 | local secure_boot_var="/sys/firmware/efi/efivars/SecureBoot-8be4df61-93ca-11d2-aa0d-00e098032b8c" |
| 50 | |
| 51 | if [[ -r "${secure_boot_var}" ]]; then |
| 52 | local b |
| 53 | b="$(od -An -t u1 -j 4 -N 1 "${secure_boot_var}" 2>/dev/null | tr -d ' ' || echo "")" |
| 54 | if [[ "${b}" == "1" ]]; then |
| 55 | sb_state="enabled" |
| 56 | elif [[ "${b}" == "0" ]]; then |
| 57 | sb_state="disabled" |
| 58 | fi |
| 59 | fi |
| 60 | |
| 61 | if [[ "${sb_state}" == "unknown" ]] && command -v mokutil >/dev/null 2>&1; then |
| 62 | if mokutil --sb-state 2>/dev/null | grep -qi "enabled"; then |
| 63 | sb_state="enabled" |
| 64 | else |
| 65 | sb_state="disabled" |
| 66 | fi |
| 67 | fi |
| 68 | |
| 69 | local boot_current="" |
| 70 | local boot_order="" |
| 71 | local boot_next="" |
| 72 | if command -v efibootmgr >/dev/null 2>&1; then |
| 73 | boot_current="$(efibootmgr 2>/dev/null | sed -n 's/^BootCurrent: //p' || echo "")" |
| 74 | boot_order="$(efibootmgr 2>/dev/null | sed -n 's/^BootOrder: //p' || echo "")" |
| 75 | boot_next="$(efibootmgr 2>/dev/null | sed -n 's/^BootNext: //p' || echo "")" |
| 76 | fi |
| 77 | |
| 78 | local root_dev root_uuid root_opts |
| 79 | root_dev="$(findmnt -no SOURCE / 2>/dev/null || echo "")" |
| 80 | root_uuid="$(findmnt -no UUID / 2>/dev/null || echo "")" |
| 81 | root_opts="$(findmnt -no OPTIONS / 2>/dev/null || echo "")" |
| 82 | |
| 83 | local esp_dev esp_uuid esp_opts |
| 84 | esp_dev="$(findmnt -no SOURCE /boot/efi 2>/dev/null || echo "")" |
| 85 | esp_uuid="$(findmnt -no UUID /boot/efi 2>/dev/null || echo "")" |
| 86 | esp_opts="$(findmnt -no OPTIONS /boot/efi 2>/dev/null || echo "")" |
| 87 | |
| 88 | local commit="" |
| 89 | commit="$(git rev-parse HEAD 2>/dev/null || echo "unknown")" |
| 90 | |
| 91 | cat >"${out_file}" <<EOF |
| 92 | { |
| 93 | "captured_utc": "$(date -u +%Y-%m-%dT%H:%M:%SZ)", |
| 94 | "host": "$(uname -n)", |
| 95 | "kernel": "$(uname -r)", |
| 96 | "git_commit": "${commit}", |
| 97 | "secure_boot": "${sb_state}", |
| 98 | "boot_current": "${boot_current}", |
| 99 | "boot_order": "${boot_order}", |
| 100 | "boot_next": "${boot_next}", |
| 101 | "root": { |
| 102 | "device": "${root_dev}", |
| 103 | "uuid": "${root_uuid}", |
| 104 | "options": "${root_opts}" |
| 105 | }, |
| 106 | "esp": { |
| 107 | "device": "${esp_dev}", |
| 108 | "uuid": "${esp_uuid}", |
| 109 | "options": "${esp_opts}" |
| 110 | } |
| 111 | } |
| 112 | EOF |
| 113 | echo "Captured state to ${out_file}" |
| 114 | } |
| 115 | |
| 116 | verify_observations() { |
| 117 | local pre_file="$1" |
| 118 | local post_file="$2" |
| 119 | # $3 (--candidate) is accepted for the record; the verdict does not use it. |
| 120 | |
| 121 | if [[ ! -f "${pre_file}" || ! -f "${post_file}" ]]; then |
| 122 | echo "ERROR: Capture file(s) missing or unreadable" |
| 123 | echo "VERDICT: INCOMP" |
| 124 | exit 1 |
| 125 | fi |
| 126 | |
| 127 | # The comparison, report and verdict live in Rust: `aienos-evidence |
| 128 | # verify-rollback` exits 0 PASS, 1 FAIL, 3 BLOCKED, 2 unparseable capture. |
| 129 | local repo_root evidence_bin |
| 130 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 131 | evidence_bin="${repo_root}/target/release/aienos-evidence" |
| 132 | if [[ "${EUID}" -eq 0 && -n "${SUDO_USER:-}" ]]; then |
| 133 | # Under sudo, build as the invoking user: their toolchain is on their |
| 134 | # PATH (not sudo's secure_path) and target/ stays owned by them. |
| 135 | sudo -u "${SUDO_USER}" -H bash -lc \ |
| 136 | 'cd "$1" && cargo build --quiet --release -p aienos-evidence' _ "${repo_root}" \ |
| 137 | || { echo "ERROR: Could not build aienos-evidence"; echo "VERDICT: INCOMP"; exit 1; } |
| 138 | else |
| 139 | (cd "${repo_root}" && cargo build --quiet --release -p aienos-evidence) \ |
| 140 | || { echo "ERROR: Could not build aienos-evidence"; echo "VERDICT: INCOMP"; exit 1; } |
| 141 | fi |
| 142 | |
| 143 | exec "${evidence_bin}" verify-rollback "${pre_file}" "${post_file}" |
| 144 | } |
| 145 | |
| 146 | action="" |
| 147 | pre_file="" |
| 148 | post_file="" |
| 149 | candidate="" |
| 150 | |
| 151 | while [[ $# -gt 0 ]]; do |
| 152 | case "$1" in |
| 153 | --capture-pre) |
| 154 | action="capture" |
| 155 | pre_file="${2:-}" |
| 156 | [[ -n "${pre_file}" ]] || usage |
| 157 | shift 2 |
| 158 | ;; |
| 159 | --capture-post) |
| 160 | action="capture" |
| 161 | post_file="${2:-}" |
| 162 | [[ -n "${post_file}" ]] || usage |
| 163 | shift 2 |
| 164 | ;; |
| 165 | --verify) |
| 166 | action="verify" |
| 167 | shift |
| 168 | ;; |
| 169 | --pre) |
| 170 | pre_file="${2:-}" |
| 171 | shift 2 |
| 172 | ;; |
| 173 | --post) |
| 174 | post_file="${2:-}" |
| 175 | shift 2 |
| 176 | ;; |
| 177 | --candidate) |
| 178 | candidate="${2:-}" |
| 179 | shift 2 |
| 180 | ;; |
| 181 | -h|--help) |
| 182 | usage |
| 183 | ;; |
| 184 | *) |
| 185 | echo "Unknown argument: $1" |
| 186 | usage |
| 187 | ;; |
| 188 | esac |
| 189 | done |
| 190 | |
| 191 | if [[ "${action}" == "capture" ]]; then |
| 192 | target="${pre_file:-${post_file}}" |
| 193 | capture_state "${target}" |
| 194 | elif [[ "${action}" == "verify" ]]; then |
| 195 | [[ -n "${pre_file}" && -n "${post_file}" ]] || usage |
| 196 | verify_observations "${pre_file}" "${post_file}" "${candidate}" |
| 197 | else |
| 198 | usage |
| 199 | fi |