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_media_security.sh

155 lines · 9235 bytes Raw Blame History
1#!/usr/bin/env bash
2# SPDX-License-Identifier: Apache-2.0
3# Offline SB-02/SB-05 regression: scratch images and throwaway signing keys only.
4set -euo pipefail
5root="$(cd "$(dirname "$0")/.." && pwd)"
6for tool in mkfs.ext4 mkfs.vfat blkid grub-mkstandalone openssl sbsign sbverify strings; do
7 command -v "$tool" >/dev/null || { echo "RECOVERY_SECURITY: NOT_RUN ($tool unavailable)"; exit 3; }
8done
9t="$(mktemp -d)"
10trap 'rm -rf "$t"' EXIT
11fail() { echo "RECOVERY_SECURITY: FAIL ($1)" >&2; exit 1; }
12
13# The original root write followed a symlink on a Unix-formatted USB.
14mkdir -p "$t/media/EFI/BOOT"
15printf 'host sentinel\n' > "$t/host"
16ln -s "$t/host" "$t/media/EFI/BOOT/grub.cfg"
17printf 'injected menu\n' | tee "$t/media/EFI/BOOT/grub.cfg" >/dev/null
18[[ "$(cat "$t/host")" == 'injected menu' ]] || fail 'symlink overwrite primitive did not reproduce'
19echo 'PASS pre-fix symlink overwrite reproduced in scratch files'
20
21truncate -s 8M "$t/ext4.img" "$t/fat.img"
22mkfs.ext4 -F -q "$t/ext4.img"
23mkfs.vfat -n AIENOSRECOV "$t/fat.img" >/dev/null
24if bash "$root/scripts/build_recovery_media.sh" --check-fstype "$t/ext4.img" >"$t/ext4.log" 2>&1; then
25 fail 'Unix filesystem admitted'
26fi
27if ! bash "$root/scripts/build_recovery_media.sh" --check-fstype "$t/fat.img" >"$t/fat.log" 2>&1; then
28 fail 'FAT filesystem refused'
29fi
30[[ "$(cat "$t/host")" == 'injected menu' ]] || fail 'filesystem probe wrote to host sentinel'
31echo 'PASS ext4 refused and FAT accepted before any mount or write (type-only probe)'
32
33# An alias must be evaluated against the canonical device name, not the
34# apparently removable-looking link supplied by the caller.
35: > "$t/nvme0n1p2"
36ln -s "$t/nvme0n1p2" "$t/innocent-usb"
37if bash "$root/scripts/build_recovery_media.sh" --check-target-path "$t/innocent-usb" >"$t/alias.log" 2>&1; then
38 fail 'alias to internal NVMe name admitted'
39fi
40ln -s "$t/fat.img" "$t/valid-usb"
41if ! bash "$root/scripts/build_recovery_media.sh" --check-target-path "$t/valid-usb" >"$t/valid-alias.log" 2>&1; then
42 fail 'alias to FAT scratch path refused'
43fi
44echo 'PASS symlink alias to NVMe name refused; benign alias resolved'
45
46# Model the exact mkstandalone/sbsign/sbverify sequence with tiny, throwaway
47# payloads. A single changed initrd byte must invalidate the signed PE.
48printf 'TEST_INITRD_MARKER\n' > "$t/initrd.img"
49printf 'TEST_KERNEL_MARKER\n' > "$t/vmlinuz"
50printf 'linux (memdisk)/boot/vmlinuz rdinit=/init\ninitrd (memdisk)/boot/initrd.img\n' > "$t/grub.cfg"
51printf 'sbat,1,SBAT Version,sbat,1,https://github.com/rhboot/shim/blob/main/SBAT.md\ngrub,2,AIENOS Recovery GRUB,aien-dev,1,https://github.com/aien-dev/aienux\n' > "$t/sbat.csv"
52grub-mkstandalone -O arm64-efi --fonts='' --locales='' --themes='' \
53 --modules='linux normal' --compress=no --sbat="$t/sbat.csv" -o "$t/unsigned.efi" \
54 "/boot/grub/grub.cfg=$t/grub.cfg" "/boot/vmlinuz=$t/vmlinuz" \
55 "/boot/initrd.img=$t/initrd.img" >/dev/null
56openssl req -x509 -newkey rsa:2048 -nodes -keyout "$t/key.pem" \
57 -out "$t/cert.pem" -subj '/CN=throwaway recovery test/' -days 1 >/dev/null 2>&1
58sbsign --key "$t/key.pem" --cert "$t/cert.pem" --output "$t/signed.efi" "$t/unsigned.efi" >/dev/null 2>&1
59sbverify --cert "$t/cert.pem" "$t/signed.efi" >/dev/null 2>&1 || fail 'signed PE refused'
60if sbverify --cert "$t/cert.pem" "$t/unsigned.efi" >/dev/null 2>&1; then
61 fail 'unsigned PE accepted'
62fi
63
64# Pre-fix trust-boundary control: signing GRUB while it names an external
65# initrd leaves that file entirely outside the signature. This is the old
66# builder's `initrd /aienos-recovery/initrd.img` pattern, not a boot test.
67printf 'initrd /aienos-recovery/initrd.img\n' > "$t/external.cfg"
68grub-mkstandalone -O arm64-efi --fonts='' --locales='' --themes='' \
69 --modules='linux normal' --compress=no --sbat="$t/sbat.csv" \
70 -o "$t/external-unsigned.efi" "/boot/grub/grub.cfg=$t/external.cfg" >/dev/null
71sbsign --key "$t/key.pem" --cert "$t/cert.pem" \
72 --output "$t/external-signed.efi" "$t/external-unsigned.efi" >/dev/null 2>&1
73printf 'original initrd\n' > "$t/external-initrd.img"
74sbverify --cert "$t/cert.pem" "$t/external-signed.efi" >/dev/null 2>&1 ||
75 fail 'external-initrd control PE did not verify'
76printf 'attacker initrd\n' > "$t/external-initrd.img"
77sbverify --cert "$t/cert.pem" "$t/external-signed.efi" >/dev/null 2>&1 ||
78 fail 'external-initrd control unexpectedly bound the external file'
79echo 'PASS pre-fix external initrd mutation leaves signed GRUB unchanged'
80marker_offset=''
81while read -r offset text; do
82 if [[ "$text" == TEST_INITRD_MARKER ]]; then marker_offset="$offset"; break; fi
83done < <(strings -t d "$t/signed.efi")
84[[ "$marker_offset" =~ ^[0-9]+$ ]] || fail 'initrd marker not embedded in signed PE'
85cp "$t/signed.efi" "$t/tampered.efi"
86printf X | dd of="$t/tampered.efi" bs=1 seek="$marker_offset" conv=notrunc status=none
87if sbverify --cert "$t/cert.pem" "$t/tampered.efi" >/dev/null 2>&1; then
88 fail 'changed embedded initrd accepted'
89fi
90# The production builder must use the same memdisk paths and must not write
91# an independent menu or initrd to the FAT volume.
92if ! [[ "$(cat "$root/scripts/build_recovery_media.sh")" == *'initrd (memdisk)/boot/initrd.img'* ]] ||
93 [[ "$(cat "$root/scripts/build_recovery_media.sh")" == *'initrd /aienos-recovery/initrd.img'* ]]; then
94 fail 'builder no longer binds the initrd inside signed PE'
95fi
96echo 'PASS signed PE accepted, unsigned and changed-initrd PE refused'
97
98# Optional firmware-level control: AAVMF's published snakeoil TEST db accepts
99# a signed standalone GRUB with its embedded config, then rejects that exact
100# EFI after a one-byte mutation in its embedded initrd. Ubuntu shim/MOK is
101# not exercised here; it requires separate attended qualification.
102if [[ "${AIENOS_RECOVERY_QEMU_EMBED:-0}" == 1 ]]; then
103 code="${AAVMF_SB_CODE:-/usr/share/AAVMF/AAVMF_CODE.snakeoil.fd}"
104 vars="${AAVMF_SB_VARS:-/usr/share/AAVMF/AAVMF_VARS.snakeoil.fd}"
105 key="${SNAKEOIL_KEY:-/usr/share/qemu-efi-aarch64/PkKek-1-snakeoil.key}"
106 cert="${SNAKEOIL_CERT:-/usr/share/qemu-efi-aarch64/PkKek-1-snakeoil.pem}"
107 command -v qemu-system-aarch64 >/dev/null &&
108 [[ -r "$code" && -r "$vars" && -r "$key" && -r "$cert" ]] ||
109 { echo 'RECOVERY_SECURITY_QEMU: NOT_RUN (AAVMF snakeoil firmware/key unavailable)'; exit 3; }
110 openssl pkey -in "$key" -passin pass:snakeoil -out "$t/snakeoil.key" 2>/dev/null
111 printf 'serial --unit=0 --speed=115200\nterminal_output serial\necho EMBEDDED_ONLY\nsleep 2\nreboot\n' > "$t/grub.cfg"
112 grub-mkstandalone -O arm64-efi --fonts='' --locales='' --themes='' \
113 --modules='linux normal serial' --compress=no --sbat="$t/sbat.csv" \
114 -o "$t/qemu-unsigned.efi" "/boot/grub/grub.cfg=$t/grub.cfg" \
115 "/boot/vmlinuz=$t/vmlinuz" "/boot/initrd.img=$t/initrd.img" >/dev/null
116 sbsign --key "$t/snakeoil.key" --cert "$cert" --output "$t/qemu-signed.efi" \
117 "$t/qemu-unsigned.efi" >/dev/null 2>&1
118 sbverify --cert "$cert" "$t/qemu-signed.efi" >/dev/null 2>&1 ||
119 fail 'AAVMF-test image signature invalid'
120 qemu_offset=''
121 while read -r offset text; do
122 if [[ "$text" == TEST_INITRD_MARKER ]]; then qemu_offset="$offset"; break; fi
123 done < <(strings -t d "$t/qemu-signed.efi")
124 [[ "$qemu_offset" =~ ^[0-9]+$ ]] || fail 'AAVMF-test initrd marker not embedded'
125 cp "$t/qemu-signed.efi" "$t/qemu-tampered.efi"
126 printf X | dd of="$t/qemu-tampered.efi" bs=1 seek="$qemu_offset" conv=notrunc status=none
127 mkdir -p "$t/esp/EFI/BOOT" "$t/esp/EFI/ubuntu" "$t/esp/aienos-recovery"
128 printf 'serial --unit=0\nterminal_output serial\necho EXTERNAL_OVERRIDE\nhalt\n' > "$t/esp/EFI/BOOT/grub.cfg"
129 cp "$t/esp/EFI/BOOT/grub.cfg" "$t/esp/EFI/ubuntu/grub.cfg"
130 printf 'ATTACKER_EXTERNAL_INITRD\n' > "$t/esp/aienos-recovery/initrd.img"
131 boot_qemu() {
132 local variant="$1" image="$2" result
133 cp "$image" "$t/esp/EFI/BOOT/BOOTAA64.EFI"
134 cp "$vars" "$t/vars-$variant.fd"
135 timeout 60 qemu-system-aarch64 -M virt,virtualization=on,gic-version=3 \
136 -accel tcg,thread=single -cpu max -smp 2 -m 1024 \
137 -drive if=pflash,format=raw,readonly=on,file="$code" \
138 -drive if=pflash,format=raw,file="$t/vars-$variant.fd" \
139 -drive if=none,id=esp,format=raw,file=fat:rw:"$t/esp" \
140 -device virtio-blk-pci,drive=esp -display none -nic none \
141 -serial file:"$t/$variant.log" -no-reboot >/dev/null 2>&1 || true
142 result="$(cat "$t/$variant.log")"
143 if [[ "$variant" == signed ]]; then
144 [[ "$result" == *EMBEDDED_ONLY* && "$result" != *EXTERNAL_OVERRIDE* ]] ||
145 fail 'firmware signed control did not execute embedded-only config'
146 else
147 [[ "$result" == *'Access Denied'* && "$result" != *EMBEDDED_ONLY* ]] ||
148 fail 'firmware did not refuse changed-initrd PE with Access Denied'
149 fi
150 }
151 boot_qemu signed "$t/qemu-signed.efi"
152 boot_qemu tampered "$t/qemu-tampered.efi"
153 echo 'RECOVERY_SECURITY_QEMU: PASS (direct AAVMF Secure Boot, external config ignored; shim/MOK NOT_RUN)'
154fi
155echo 'RECOVERY_SECURITY: PASS (scratch host checks; optional direct AAVMF; owner shim/MOK physical boot NOT_RUN)'