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

217 lines · 12469 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# ck_owner_keys_check.sh -- build-side proof that the C kernel hardware
18# staging image never embeds the TEST Store keys, the TEST store uuid or the
19# TEST machine id (native/kernel/README.md, "Owner provisioning").
20#
21# bash scripts/ck_owner_keys_check.sh
22#
23# Checks, each PASS or FAIL:
24# 1. make full CK_HARDWARE_STAGING=1 with no owner files is refused with the
25# expected message (also with only one of the two files); owner files
26# without CK_HARDWARE_STAGING are refused.
27# 2. tools/ck_owner_gen.c refuses every known TEST value and every malformed
28# input (wrong length, not hex, duplicate, unknown name, missing name,
29# all-equal bytes) and writes no header.
30# 3. With the labelled TEST-fixture owner files
31# (native/kernel/tests/fixtures/owner/, public bytes only, distinct from
32# every old TEST constant) the hardware staging image builds; its ELF and
33# EFI carry no TEST Store label, TEST uuid, TEST Store print line, fixture
34# label, or TEST key symbol; they do carry the fixture machine id bytes
35# and the BLOCKED_OPERATOR Store refusal line.
36# 4. Control: the default full image still carries the labelled TEST Store
37# label (the grep is not vacuous).
38# 5. The default image carries the "argus: TEST machine id 0xA1" banner (control); the
39# Makefile owner_check pattern matches a blob holding it (counterexample).
40# Build-only: nothing is booted. Like the qemu_ck_* scripts it only reads the
41# Spark quiet flag and takes the QEMU gate lock (scripts/lib_gate_hold.sh);
42# NOT_RUN, exit 3, while either is held by another run. No Python.
43set -euo pipefail
44
45repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
46kdir="${repo_root}/native/kernel"
47fix="${kdir}/tests/fixtures/owner"
48out="${CK_OWNER_CHECK_OUT:-${repo_root}/target/ck-owner-keys-check}"
49cross=""
50if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi
51
52# Machine quiet flag (read only) and the QEMU gate lock (one gate at a time):
53# scripts/lib_gate_hold.sh (aienos#278).
54. "${repo_root}/scripts/lib_gate_hold.sh"
55if ! gh_quiet_check || ! gh_lock_take ck_owner_keys_check "${AIENOS_GATE_MINUTES:-60}"; then
56 echo "NOT_RUN ${gh_why}"
57 echo "CK_OWNER_KEYS_CHECK: NOT_RUN"
58 exit 3
59fi
60tmp="$(mktemp -d)"
61cleanup() {
62 rm -rf "${tmp}"
63 gh_lock_release
64}
65trap cleanup EXIT
66
67fails=0
68ok() { echo "PASS $*"; }
69bad() { echo "FAIL $*"; fails=$((fails + 1)); }
70mk() { make -s -C "${kdir}" CROSS="${cross}" OUT="${out}" "$@"; }
71
72own="${fix}/TEST-FIXTURE-owner-pubkeys.txt"
73mach="${fix}/TEST-FIXTURE-machine-id.txt"
74refusal="CK_HARDWARE_STAGING refuses the TEST Store keys and TEST machine id"
75
76# ---- 1. build-time refusals ----------------------------------------------
77expect_make_refused() { # label, expected text, make args...
78 local label="$1" want="$2"; shift 2
79 local log="${tmp}/make.log"
80 if mk "$@" >"${log}" 2>&1; then
81 bad "${label}: build was not refused"
82 elif grep -qF -- "${want}" "${log}"; then
83 ok "${label}: refused ($(grep -oF -- "${want}" "${log}" | head -1))"
84 else
85 bad "${label}: refused without the expected message: $(tail -c 300 "${log}")"
86 fi
87}
88expect_make_refused "hardware staging, no owner files" "${refusal}" full CK_HARDWARE_STAGING=1
89expect_make_refused "hardware staging, owner keys only" "${refusal}" full CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS="${own}"
90expect_make_refused "hardware staging, machine id only" "${refusal}" full CK_HARDWARE_STAGING=1 CK_MACHINE_ID="${mach}"
91expect_make_refused "owner files without hardware staging" "only used with CK_HARDWARE_STAGING=1" \
92 full CK_OWNER_PUBKEYS="${own}" CK_MACHINE_ID="${mach}"
93expect_make_refused "hardware staging + TEST artifact anchor" "cannot be combined with CK_HARDWARE_STAGING" \
94 full CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS="${own}" CK_MACHINE_ID="${mach}" CK_SEED0B_TEST_ANCHOR=1
95
96# ---- 2. generator refusals -----------------------------------------------
97mk owner-gen
98gen="${out}/host/ck_owner_gen"
99good_pk="owner_root_ed25519 $(awk '$1=="owner_root_ed25519"{print $2}' "${own}")"
100good_mid="machine_id $(awk '$1=="machine_id"{print $2}' "${mach}")"
101good_uu="store_uuid $(awk '$1=="store_uuid"{print $2}' "${mach}")"
102expect_gen_refused() { # label, expected text, owner file text, machine file text
103 local label="$1" want="$2"
104 printf '%s\n' "$3" >"${tmp}/o.txt"
105 printf '%s\n' "$4" >"${tmp}/m.txt"
106 rm -f "${tmp}/h.h"
107 local res rc=0
108 res="$("${gen}" "${tmp}/o.txt" "${tmp}/m.txt" "${tmp}/h.h" 2>&1)" || rc=$?
109 if [[ "${rc}" == 1 && "${res}" == *"CK_OWNER_GEN: REFUSED"* && "${res}" == *"${want}"* && ! -e "${tmp}/h.h" ]]; then
110 ok "generator refuses ${label} (${res#CK_OWNER_GEN: REFUSED })"
111 else
112 bad "generator ${label}: rc=${rc} out=${res}"
113 fi
114}
115z64="$(printf '0%.0s' {1..64})"
116expect_gen_refused "RFC 8032 TEST 1 owner key" "RFC 8032 TEST public key" \
117 "owner_root_ed25519 d75a980182b10ab7d54bfed3c964073a0ee172f3daa62325af021a68f707511a" "${good_mid}"$'\n'"${good_uu}"
118expect_gen_refused "RFC 8032 TEST 2 owner key" "RFC 8032 TEST public key" \
119 "owner_root_ed25519 3d4017c3e843895a92b70aa74d1b7ebc9c982ccf2ec4968cc0cd55f12af4660c" "${good_mid}"$'\n'"${good_uu}"
120expect_gen_refused "RFC 8032 TEST 3 owner key" "RFC 8032 TEST public key" \
121 "owner_root_ed25519 fc51cd8e6218a1a38da47ed00230f0580816ed13ba3303ac5deb911548908025" "${good_mid}"$'\n'"${good_uu}"
122expect_gen_refused "TEST machine id 0xA1" "TEST machine id" \
123 "${good_pk}" "machine_id a1${z64:2}"$'\n'"${good_uu}"
124expect_gen_refused "TEST store uuid" "TEST store uuid" \
125 "${good_pk}" "${good_mid}"$'\n'"store_uuid 4149454e2d544553542d424f4f543031"
126expect_gen_refused "all-zero owner key" "all bytes equal" "owner_root_ed25519 ${z64}" "${good_mid}"$'\n'"${good_uu}"
127expect_gen_refused "all-zero machine id" "all bytes equal" "${good_pk}" "machine_id ${z64}"$'\n'"${good_uu}"
128expect_gen_refused "short owner key" "need exactly 64 hex digits" "owner_root_ed25519 abcd" "${good_mid}"$'\n'"${good_uu}"
129expect_gen_refused "long store uuid" "need exactly 32 hex digits" \
130 "${good_pk}" "${good_mid}"$'\n'"store_uuid ${z64:0:30}0102"
131expect_gen_refused "non-hex owner key" "not hex" "owner_root_ed25519 zz${z64:2}" "${good_mid}"$'\n'"${good_uu}"
132expect_gen_refused "duplicate machine id" "given twice" "${good_pk}" "${good_mid}"$'\n'"${good_mid}"$'\n'"${good_uu}"
133expect_gen_refused "unknown name" "unknown name" "${good_pk}"$'\n'"k_vol ${z64}" "${good_mid}"$'\n'"${good_uu}"
134expect_gen_refused "missing store uuid" "missing" "${good_pk}" "${good_mid}"
135expect_gen_refused "machine id equal to owner key" "equals the owner root public key" \
136 "${good_pk}" "machine_id ${good_pk#owner_root_ed25519 }"$'\n'"${good_uu}"
137
138# ---- 3. hardware staging image with the TEST-fixture owner files ---------
139hw="${out}/full-hardware-staging"
140if mk full CK_HARDWARE_STAGING=1 CK_OWNER_PUBKEYS="${own}" CK_MACHINE_ID="${mach}" >"${tmp}/hw.log" 2>&1 \
141 && [[ -s "${hw}/BOOTAA64.EFI" && -s "${hw}/aienos-ck.elf" ]]; then
142 ok "hardware staging image built with the TEST-fixture owner files ($(grep -c . "${tmp}/hw.log") build lines)"
143 grep -qF "no TEST Store label, uuid or key code" "${tmp}/hw.log" \
144 && ok "Makefile owner_check ran on the hardware staging ELF" || bad "Makefile owner_check line missing"
145else
146 bad "hardware staging image with fixture owner files did not build: $(tail -c 400 "${tmp}/hw.log")"
147fi
148banned='AIENOS-LANE18-TEST-KVOL-NOT-SECRET|AIEN-TEST-BOOT01|TEST identity, TEST keys|formatted TEST store|TEST machine id 0xA1|TEST-FIXTURE|TEST FIXTURE'
149for f in "${hw}/aienos-ck.elf" "${hw}/BOOTAA64.EFI"; do
150 [[ -s "${f}" ]] || { bad "missing ${f}"; continue; }
151 s="$(grep -aoE "${banned}" "${f}" | sort -u | tr '\n' ' ' || true)"
152 [[ -z "${s}" ]] && ok "no TEST Store label/uuid/print line or fixture label in $(basename "${f}")" \
153 || bad "$(basename "${f}") carries: ${s}"
154 hex="$(od -An -tx1 -v "${f}" | tr -d ' \n')"
155 mid_hex="$(awk '$1=="machine_id"{print $2}' "${mach}")"
156 [[ "${hex}" == *"${mid_hex}"* ]] && ok "$(basename "${f}") carries the provisioned machine id bytes" \
157 || bad "$(basename "${f}") lacks the provisioned machine id bytes"
158 [[ "${hex}" == *"4149454e2d544553542d424f4f543031"* ]] && bad "$(basename "${f}") carries the TEST uuid bytes" \
159 || ok "no TEST uuid bytes in $(basename "${f}")"
160 grep -aqF 'production Store key source' "${f}" && ok "$(basename "${f}") carries the BLOCKED_OPERATOR Store refusal" \
161 || bad "$(basename "${f}") lacks the BLOCKED_OPERATOR Store refusal"
162done
163if [[ -s "${hw}/aienos-ck.elf" ]]; then
164 nmtool="${cross}nm"
165 # Capture nm fully first: `nm | grep -q` lets grep exit early, nm can die of SIGPIPE, and
166 # pipefail then turns a real match into a FAIL (CK-4 light log 181624).
167 nmout="$("${nmtool}" "${hw}/aienos-ck.elf")"
168 y="$(grep -E ' (ck_store_test_keys|ck_store_test_uuid)$' <<<"${nmout}" || true)"
169 [[ -z "${y}" ]] && ok "no TEST Store key symbols in the hardware staging ELF" || bad "TEST symbols: ${y}"
170 grep -qE ' ck_store_production_keys$' <<<"${nmout}" \
171 && ok "ck_store_production_keys (BLOCKED_OPERATOR seam) is linked" || bad "ck_store_production_keys not linked"
172 # Control: the same match must FAIL on nm text without the symbol and PASS with it.
173 ctl_absent=$'0000 T ck_main\n0001 D ck_other_keys'
174 ctl_present="${ctl_absent}"$'\n0002 D ck_store_production_keys'
175 if grep -qE ' ck_store_production_keys$' <<<"${ctl_absent}"; then bad "control: absent symbol matched"; else ok "control: symbol check fails when the symbol is absent"; fi
176 if grep -qE ' ck_store_production_keys$' <<<"${ctl_present}"; then ok "control: symbol check passes when the symbol is present"; else bad "control: present symbol not matched"; fi
177fi
178
179# ---- 4. control: the default image keeps the labelled TEST keys -----------
180if mk full >"${tmp}/def.log" 2>&1 && grep -aqF 'AIENOS-LANE18-TEST-KVOL-NOT-SECRET' "${out}/full/aienos-ck.elf"; then
181 ok "control: default full image still carries the labelled TEST Store label"
182else
183 bad "control: default full image lacks the TEST Store label or did not build"
184fi
185
186# ---- 5. TEST machine id banner: control + counterexample ------------------
187if grep -aqF 'argus: TEST machine id 0xA1' "${out}/full/aienos-ck.elf" 2>/dev/null; then
188 ok "control: default full image carries the 'argus: TEST machine id 0xA1' banner"
189else
190 bad "control: default full image lacks the 'argus: TEST machine id 0xA1' banner"
191fi
192# Counterexample: the Makefile owner_check pattern must catch a blob that holds
193# the banner. The pattern is read from the Makefile itself, not copied here.
194oc_pat="$(grep -oE 'grep -aoE "[^"]*TEST machine id 0xA1[^"]*"' "${kdir}/Makefile" | head -1 | sed -e 's/^grep -aoE "//' -e 's/"$//' || true)"
195if [[ -z "${oc_pat}" ]]; then
196 bad "Makefile owner_check pattern for the TEST machine id banner not found"
197else
198 printf 'padding\0argus: TEST machine id 0xA1 (fixed label)\n\0padding' > "${tmp}/bad-blob.bin"
199 printf 'padding\0argus: ok narrow_revoke=1\n\0padding' > "${tmp}/clean-blob.bin"
200 if [[ -n "$(grep -aoE "${oc_pat}" "${tmp}/bad-blob.bin" || true)" ]]; then
201 ok "counterexample: owner_check pattern refuses a blob carrying the TEST machine id banner"
202 else
203 bad "counterexample: owner_check pattern did NOT match a blob carrying the TEST machine id banner"
204 fi
205 if [[ -z "$(grep -aoE "${oc_pat}" "${tmp}/clean-blob.bin" || true)" ]]; then
206 ok "owner_check pattern passes a clean blob"
207 else
208 bad "owner_check pattern matched a clean blob"
209 fi
210fi
211
212if [[ "${fails}" == 0 ]]; then
213 echo "CK_OWNER_KEYS_CHECK: PASS"
214else
215 echo "CK_OWNER_KEYS_CHECK: FAIL failures=${fails}"
216 exit 1
217fi