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 / native / kernel / tests / fixtures / osc_receipt / make-receipts.sh

167 lines · 9512 bytes Raw Blame History
1#!/bin/bash
2# make-receipts.sh: build the Omega realization receipt fixtures (receipt v1, omega spec/one-layer-receipts.md)
3# for OSC unit admission (aienos#383) with omega's own checker2. Usage:
4# make-receipts.sh OMEGA_DIR [OUT_DIR] write the fixtures to OUT_DIR (default: this directory)
5# make-receipts.sh --check OMEGA_DIR rebuild into a temporary directory and require every committed
6# fixture and expected.txt to be byte-identical (Ed25519 is
7# deterministic, so the same checker2 gives the same bytes)
8# OMEGA_DIR is a checkout of aien-dev/omega with src/checker2 (omega#448). checker2 and checker2_other are built
9# from it into a temporary OUT_DIR (omega's own build/ is not touched). Every receipt is minted by
10# `checker2 receipt issue` with the checker2 TEST key (class TEST); nothing here signs anything itself.
11# Altered bytes (n_altered: one flipped byte; n_length: one extra byte) are copies of a minted receipt.
12# Needs bash, make, cc, od, dd, sha256sum, cmp. No Python.
13set -eu
14export LC_ALL=C
15mydir=$(cd "$(dirname "$0")" && pwd)
16check=0
17if [ "${1:-}" = --check ]; then check=1; shift; fi
18OMEGA=$(cd "${1:?OMEGA_DIR}" && pwd)
19OUT=${2:-$mydir}
20work=$(mktemp -d)
21trap 'rm -rf "$work"' EXIT
22if [ "$check" = 1 ]; then OUT=$work/out; fi
23mkdir -p "$OUT"
24
25fix=$(cd "$mydir/.." && pwd)
26TARGET=aienos-ck-aarch64-el0 # the kernel's target name (osc_receipt.h OSC_RCPT_TARGET_NAME)
27FLOOR=10 # kernel qualification policy (core/osc_admit.c): epoch floor 10, current epoch 20
28NOW=20
29
30make -s -C "$OMEGA" OUT_DIR="$work/c2" "$work/c2/checker2/checker2" "$work/c2/checker2/checker2_other" >/dev/null
31C2=$work/c2/checker2/checker2
32C2O=$work/c2/checker2/checker2_other
33omega_commit=$(git -C "$OMEGA" rev-parse HEAD 2>/dev/null || echo unknown)
34omega_dirty=$(git -C "$OMEGA" status --porcelain -- src/checker2 mk/checker2.mk 2>/dev/null | head -1)
35[ -z "$omega_dirty" ] || { echo "omega checker2 sources are modified; refusing (fixtures must come from a commit)" >&2; exit 1; }
36checker_id=$("$C2" version | sed -n 's/^checker_id=//p')
37other_id=$("$C2O" version | sed -n 's/^checker_id=//p')
38
39# Section 1 (IR) and section 2 (code) of an OSCUNIT container: section table at 128, 48 bytes per entry,
40# offset u32 at +4, length u32 at +8, SHA-256 at +16 (OSC_UNIT_ARTIFACT.md section 4). The extracted bytes must
41# hash to the table's hash, so a wrong offset cannot go unnoticed.
42u32() { od -An -tu4 -j "$2" -N4 "$1" | tr -d ' '; }
43hexat() { od -An -tx1 -j "$2" -N "$3" "$1" | tr -d ' \n'; }
44extract() { # unit-file short-name
45 local u=$1 b=$2 k off len ent
46 for k in 1 2; do
47 ent=$((128 + (k - 1) * 48))
48 off=$(u32 "$u" $((ent + 4)))
49 len=$(u32 "$u" $((ent + 8)))
50 dd if="$u" of="$work/$b.s$k" bs=1 skip="$off" count="$len" status=none
51 [ "$(sha256sum "$work/$b.s$k" | cut -c1-64)" = "$(hexat "$u" $((ent + 16)) 32)" ] \
52 || { echo "$u: section $k does not hash to its table entry" >&2; exit 1; }
53 done
54 mv "$work/$b.s1" "$work/$b.ir"
55 mv "$work/$b.s2" "$work/$b.code"
56}
57extract "$fix/osc_unit/vectors/a01_valid_min.unit" a01
58extract "$fix/osc_unit/launch/l01_launch_fns.unit" l01
59extract "$fix/osh/osh_lex.unit" osh_lex
60extract "$fix/osh/osh_parse.unit" osh_parse
61extract "$fix/osh/osh_expand.unit" osh_expand
62printf 'not a firmware image (receipt fixture)\n' >"$work/firmware.bin"
63printf 'stand-in producer identity (receipt fixture)\n' >"$work/producer.bin"
64
65# issue NAME CHECKER IR CODE EPOCH EXPIRY [extra issue options]
66issue() {
67 local name=$1 c=$2 ir=$3 code=$4 ep=$5 ex=$6 line path
68 shift 6
69 rm -rf "$work/store"; mkdir -p "$work/store"
70 line=$("$c" receipt issue "$work/$ir.ir" "$work/$code.code" --target "${TGT:-$TARGET}" --epoch "$ep" --expiry "$ex" \
71 --out-dir "$work/store" "$@")
72 path=${line#receipt=}
73 [ -f "$path" ] || { echo "issue $name: $line" >&2; exit 1; }
74 cat "$path" >"$OUT/$name.rcpt"
75}
76id_of() { sha256sum "$OUT/$1.rcpt" | cut -c1-64; }
77
78# valid receipts, one per launched program+code pair. A receipt binds the IR and the code, not the container
79# manifest: l02_stack_over.unit has l01's IR and code (l01.rcpt covers it) and a06_valid_limits_at_max.unit has
80# a01's (a01.rcpt covers it; checker2 gives byte-identical receipts for them).
81issue a01 "$C2" a01 a01 10 1000
82issue l01 "$C2" l01 l01 10 1000
83issue osh_lex "$C2" osh_lex osh_lex 10 1000
84issue osh_parse "$C2" osh_parse osh_parse 10 1000
85issue osh_expand "$C2" osh_expand osh_expand 10 1000
86# negative receipts, all for a01_valid_min.unit
87issue n_stale "$C2" a01 a01 5 1000 # issued under epoch 5, below the floor 10: replay
88issue n_revoked_id "$C2" a01 a01 11 1000 # valid except that its id is on the kernel's revoked list
89issue n_expired "$C2" a01 a01 10 15 # last fresh epoch 15, kernel is at 20
90issue n_future "$C2" a01 a01 30 1000 # issued under epoch 30, kernel is at 20
91TGT=aarch64-linux-host issue n_target "$C2" a01 a01 10 1000
92issue n_program "$C2" osh_lex a01 10 1000 # another (checker2-accepted) program with a01's code
93issue n_firmware "$C2" a01 a01 10 1000 --firmware "$work/firmware.bin"
94issue n_producer "$C2" a01 a01 10 1000 --producer "$work/producer.bin"
95issue n_deps "$C2" a01 a01 10 1000 --dep "$(id_of a01)"
96issue n_checker "$C2O" a01 a01 10 1000 # minted by a checker2 build with another identity
97# copies with altered bytes
98cat "$OUT/a01.rcpt" >"$OUT/n_altered.rcpt"
99b=$(od -An -tu1 -j80 -N1 "$OUT/a01.rcpt" | tr -d ' ')
100printf "\\$(printf '%03o' $((b ^ 1)))" | dd of="$OUT/n_altered.rcpt" bs=1 seek=80 conv=notrunc status=none
101{ cat "$OUT/a01.rcpt"; printf '\0'; } >"$OUT/n_length.rcpt"
102
103test_fp=$(hexat "$OUT/a01.rcpt" 320 32)
104revoked_id=$(id_of n_revoked_id)
105# The same policy for checker2 verify (the oracle) as the kernel's qualification policy: TEST key allowed,
106# floor 10, epoch 20, target aienos-ck-aarch64-el0, no firmware, no dependencies, revoked id n_revoked_id.
107# Both checker2 and the kernel compare the producer (none = all zero unless the policy names one).
108qual=(--allow-test-key --floor "$FLOOR" --epoch "$NOW" --target "$TARGET" --revoked-id "$revoked_id")
109# oracle NAME UNIT VARIANT KERNEL-VERDICT [extra verify options]
110oracle() {
111 local name=$1 unit=$2 variant=$3 kv=$4 v opts
112 shift 4
113 case $variant in
114 qual) opts=("${qual[@]}") ;;
115 release) opts=(--production --floor "$FLOOR" --epoch "$NOW" --target "$TARGET" --revoked-id "$revoked_id") ;;
116 revoke_key) opts=("${qual[@]}" --revoked-key "$test_fp") ;;
117 producer) opts=("${qual[@]}" --producer "$work/producer.bin") ;;
118 esac
119 v=$("$C2" receipt verify "$OUT/$name.rcpt" "$work/$unit.ir" "$work/$unit.code" "${opts[@]}" "$@" 2>&1 || true)
120 v=${v#receipt=}
121 echo "$name.rcpt unit=$unit variant=$variant checker2=$v kernel=$kv id=$(id_of "$name")"
122}
123{
124 echo "# Receipt fixtures for aienos#383, minted by omega checker2 (TEST key). Generated by make-receipts.sh."
125 echo "# omega=$omega_commit checker_id=$checker_id other_checker_id=$other_id"
126 echo "# test_key_fingerprint=$test_fp target=$TARGET floor=$FLOOR epoch=$NOW revoked_id=$revoked_id"
127 echo "# columns: receipt unit variant checker2-verify-verdict kernel-verdict receipt-id"
128 echo "# variant: qual = kernel qualification policy; release = the same with release=1 (TEST refused);"
129 echo "# revoke_key = qual plus the TEST key fingerprint on the revoked-key list;"
130 echo "# producer = qual plus an expected producer (qual expects none = all zero)"
131 oracle a01 a01 qual VALID
132 oracle l01 l01 qual VALID
133 oracle osh_lex osh_lex qual VALID
134 oracle osh_parse osh_parse qual VALID
135 oracle osh_expand osh_expand qual VALID
136 oracle a01 a01 release TEST_SIGNER_IN_PRODUCTION
137 oracle a01 a01 revoke_key REVOKED
138 oracle n_altered a01 qual BAD_SIGNATURE
139 oracle n_length a01 qual SIZE
140 oracle n_stale a01 qual STALE_EPOCH
141 oracle n_revoked_id a01 qual REVOKED
142 oracle n_expired a01 qual EXPIRED
143 oracle n_future a01 qual NOT_YET_VALID
144 oracle n_target a01 qual WRONG_TARGET
145 oracle n_firmware a01 qual WRONG_FIRMWARE
146 oracle n_program a01 qual PROGRAM_MISMATCH
147 oracle l01 a01 qual EXECUTABLE_MISMATCH
148 oracle n_deps a01 qual DEPENDENCIES_MISMATCH
149 oracle n_checker a01 qual UNTRUSTED_CHECKER
150 oracle n_producer a01 qual WRONG_PRODUCER
151 oracle n_producer a01 producer VALID
152 oracle a01 a01 producer WRONG_PRODUCER
153} >"$OUT/expected.txt"
154# The oracle must agree with the kernel column everywhere except where the kernel deliberately does not check:
155# n_producer under qual (checker2 compares an all-zero producer; the kernel policy has no producer).
156bad=$(awk '!/^#/ { split($4, c, "="); split($5, k, "="); if (c[2] != k[2] && !($1 == "n_producer.rcpt" && $3 == "variant=qual")) print }' "$OUT/expected.txt")
157[ -z "$bad" ] || { echo "checker2 and the expected kernel verdict disagree:" >&2; echo "$bad" >&2; exit 1; }
158
159if [ "$check" = 1 ]; then
160 rc=0
161 for f in "$OUT"/*; do
162 cmp -s "$f" "$mydir/$(basename "$f")" || { echo "differs from the committed fixture: $(basename "$f")" >&2; rc=1; }
163 done
164 [ "$rc" = 0 ] && echo "receipt fixtures reproduced byte for byte ($(ls "$OUT" | wc -l) files, omega $omega_commit)"
165 exit "$rc"
166fi
167echo "wrote $(ls "$OUT"/*.rcpt | wc -l) receipts and expected.txt to $OUT (omega $omega_commit, checker_id $checker_id)"