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 / qemu_ck_osc_receipt_test.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 | # AIENOS_CK_OSC_RECEIPT gate (aienos#383): the C kernel refuses to launch an OSC unit unless a valid signed Omega |
| 18 | # realization receipt (omega spec/one-layer-receipts.md, receipt v1) comes with it. Booted in QEMU AArch64 + UEFI |
| 19 | # (AAVMF). |
| 20 | # |
| 21 | # Label: QEMU (aarch64 virt), TEST signer, not physical. Nothing here ran on a Spark or any real machine. The |
| 22 | # receipts are minted by omega checker2 with its TEST key (tests/fixtures/osc_receipt/PROVENANCE.txt), except |
| 23 | # a01.owner.rcpt, signed with the owner key on the owner YubiKey (tests/fixtures/osc_receipt_owner/PROVENANCE.txt, |
| 24 | # aienos#387); the units are signed with the container spec's THROWAWAY TEST key. |
| 25 | # |
| 26 | # One unit (a01_valid_min.unit) and fourteen receipts are written into the sealed C Store of a GPT boot disk |
| 27 | # image. One boot of the qualification image built with CK_SEED0B_TEST_ANCHOR=1 CK_OSC_LAUNCH_TEST=1 |
| 28 | # CK_OSC_RECEIPT_TEST=1 launches a01 add(a, b) nineteen times, each time with one receipt or none, and prints one |
| 29 | # "osc_rcpt_launch:" line each. This gate checks: |
| 30 | # with the valid receipt the unit RETURNS the exact value (first and last launch), and with the owner receipt in |
| 31 | # release mode (aienos#387); |
| 32 | # no receipt, an altered, stale, revoked, expired, not-yet-valid, wrong-target, wrong-firmware, wrong-program, |
| 33 | # wrong-executable, wrong-length, wrong-dependencies or untrusted-checker receipt, the TEST key revoked, and a |
| 34 | # TEST receipt under the release policy are each REFUSED with its own named reason, and nothing is mapped; |
| 35 | # the receipt id printed is the SHA-256 of the receipt file. |
| 36 | # Last line: AIENOS_CK_OSC_RECEIPT: PASS|FAIL|NOT_RUN. |
| 37 | # Needs qemu-system-aarch64 and AAVMF (Ubuntu: qemu-system-arm qemu-efi-aarch64). |
| 38 | set -euo pipefail |
| 39 | |
| 40 | repo_root="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)" |
| 41 | cd "${repo_root}" |
| 42 | verdict=AIENOS_CK_OSC_RECEIPT |
| 43 | code_fd="${AAVMF_CODE:-/usr/share/AAVMF/AAVMF_CODE.no-secboot.fd}" |
| 44 | vars_fd="${AAVMF_VARS:-/usr/share/AAVMF/AAVMF_VARS.fd}" |
| 45 | command -v qemu-system-aarch64 >/dev/null || { echo "qemu-system-aarch64 not installed"; echo "${verdict}: NOT_RUN"; exit 2; } |
| 46 | [[ -r "${code_fd}" && -r "${vars_fd}" ]] || { echo "AAVMF firmware not found"; echo "${verdict}: NOT_RUN"; exit 2; } |
| 47 | |
| 48 | # Machine quiet flag (read only) and the QEMU gate lock (one gate at a time): |
| 49 | # scripts/lib_gate_hold.sh (aienos#278). |
| 50 | . "$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)/lib_gate_hold.sh" |
| 51 | if ! gh_quiet_check || ! gh_lock_take "qemu_ck_osc_receipt_test" "${AIENOS_GATE_MINUTES:-60}"; then |
| 52 | echo "NOT_RUN ${gh_why}" |
| 53 | echo "${verdict}: NOT_RUN" |
| 54 | exit 3 |
| 55 | fi |
| 56 | # Release only this run's own gate lock record, at most once. |
| 57 | release_flag() { gh_lock_release; } |
| 58 | work="$(mktemp -d)" |
| 59 | cleanup() { rm -rf "${work}"; release_flag; } |
| 60 | trap cleanup EXIT |
| 61 | failed=0 |
| 62 | pass() { echo "PASS $1"; } |
| 63 | fail() { echo "FAIL $1"; failed=1; } |
| 64 | |
| 65 | cross="" |
| 66 | if [ "$(uname -m)" != "aarch64" ]; then cross="aarch64-linux-gnu-"; fi |
| 67 | commit="$(git rev-parse HEAD 2>/dev/null || echo unknown)" |
| 68 | out_prod="${AIENOS_CK_OUT_PROD:-${repo_root}/target/native-kernel}" |
| 69 | out_rcpt="${AIENOS_CK_OUT_RECEIPT:-${repo_root}/target/native-kernel-osc-receipt-test}" |
| 70 | mk() { make -s -C native/kernel CROSS="${cross}" AIENOS_COMMIT="${commit}" "$@" >/dev/null; } |
| 71 | mk OUT="${out_rcpt}" CK_SEED0B_TEST_ANCHOR=1 CK_OSC_LAUNCH_TEST=1 CK_OSC_RECEIPT_TEST=1 full |
| 72 | make -s -C native/kernel OUT="${out_prod}" store-image gpt-image >/dev/null |
| 73 | simg="${out_prod}/host/ck_store_image" |
| 74 | |
| 75 | fix="native/kernel/tests/fixtures/osc_unit" |
| 76 | rfix="native/kernel/tests/fixtures/osc_receipt" |
| 77 | ofix="native/kernel/tests/fixtures/osc_receipt_owner" |
| 78 | rcpts=(a01 l01 n_altered n_stale n_revoked_id n_expired n_future n_target n_program n_length n_firmware n_deps n_checker) |
| 79 | files=("${fix}/vectors/a01_valid_min.unit") |
| 80 | for r in "${rcpts[@]}"; do files+=("${rfix}/${r}.rcpt"); done |
| 81 | files+=("${ofix}/a01.owner.rcpt") |
| 82 | disk="${work}/nvme.img" |
| 83 | "${out_prod}/host/ck_gpt_image" create "${disk}" 512 64 aienos-middle >/dev/null || fail "GPT boot disk image created" |
| 84 | if "${simg}" build "${disk}" 512 "${files[@]}" >"${work}/build.txt" 2>&1; then pass "boot disk built: $(head -1 "${work}/build.txt")" |
| 85 | else fail "boot disk build: $(head -3 "${work}/build.txt" | tr '\n' ' ')"; fi |
| 86 | "${simg}" list "${disk}" 512 >"${work}/list.txt" 2>&1 || fail "host read-back of the boot disk Store" |
| 87 | for f in "${files[@]}"; do |
| 88 | grep -qxF "$(basename "${f}") bytes=$(stat -c %s "${f}") sha256=$(sha256sum "${f}" | cut -d' ' -f1)" "${work}/list.txt" \ |
| 89 | || fail "host read-back of $(basename "${f}")" |
| 90 | done |
| 91 | |
| 92 | boot() { # image, serial text output, disk image |
| 93 | local esp="${work}/esp" log="${work}/serial.log" started status |
| 94 | rm -rf "${esp}" |
| 95 | mkdir -p "${esp}/EFI/BOOT" "${esp}/EFI/AIENOS" |
| 96 | touch "${esp}/EFI/AIENOS/BOOTREPORT.TXT" |
| 97 | cp "$1" "${esp}/EFI/BOOT/BOOTAA64.EFI" |
| 98 | cp "${vars_fd}" "${work}/vars.fd" |
| 99 | started=$(date +%s) |
| 100 | set +e |
| 101 | nice -n 10 timeout "${AIENOS_QEMU_TIMEOUT:-300}" qemu-system-aarch64 \ |
| 102 | -M virt,virtualization=on,gic-version=3,iommu=smmuv3 -accel tcg,thread=single -cpu max -smp 4 -m 2048 \ |
| 103 | -drive if=pflash,format=raw,readonly=on,file="${code_fd}" \ |
| 104 | -drive if=pflash,format=raw,file="${work}/vars.fd" \ |
| 105 | -drive if=none,id=esp,format=raw,file=fat:rw:"${esp}" \ |
| 106 | -device virtio-blk-pci,drive=esp \ |
| 107 | -drive if=none,id=nvme0,format=raw,file="$3" \ |
| 108 | -device nvme,drive=nvme0,serial=aienos-nvme-test \ |
| 109 | -device ramfb -display none -nic none \ |
| 110 | -serial file:"${log}" -no-reboot |
| 111 | status=$? |
| 112 | set -e |
| 113 | tr -d '\r' <"${log}" >"$2" |
| 114 | echo "qemu exit ${status} after $(( $(date +%s) - started )) s (commit ${commit:0:12})" |
| 115 | [[ "${status}" == 0 ]] || fail "QEMU exit status ${status} (124 = timeout)" |
| 116 | } |
| 117 | has() { grep -qE -- "$2" "$1"; } |
| 118 | check() { if has "$1" "$3"; then pass "$2"; else fail "$2"; fi; } |
| 119 | |
| 120 | serial="${work}/serial.txt" |
| 121 | cp "${disk}" "${work}/disk-run.img" |
| 122 | boot "${out_rcpt}/full/BOOTAA64.EFI" "${serial}" "${work}/disk-run.img" |
| 123 | release_flag |
| 124 | |
| 125 | check "${serial}" "left firmware and entered the kernel" "kernel: alive" |
| 126 | check "${serial}" "kernel read all fifteen candidates (one unit, fourteen receipts) from the boot disk Store" "^artifact_candidates: 15$" |
| 127 | if has "${serial}" "^report-truncated:"; then fail "kernel report lines were truncated"; else pass "no kernel report line was truncated"; fi |
| 128 | check "${serial}" "final report reached the console" "report_kind: final" |
| 129 | if has "${serial}" "report_kind: (panic|fault)"; then fail "panic or fault reported"; else pass "no kernel panic or fault report"; fi |
| 130 | check "${serial}" "a01 admitted (the container check is unchanged)" "^osc_unit: a01_valid_min\\.unit ACCEPT " |
| 131 | check "${serial}" "receipt policy line: qualification, TEST receipt anchor, the owner root as owner receipt anchor, launch requires a receipt" \ |
| 132 | "^osc_receipt_policy: mode=qualification receipt_test_anchors=1 receipt_owner_anchors=1 \\(the owner root\\) checkers=1 target=aienos-ck-aarch64-el0 epoch_floor=10 epoch_now=20 revoked_ids=1 revoked_keys=0 firmware=none dependencies=none producer=none; launch requires a valid receipt; QEMU, not physical$" |
| 133 | for rf in "${files[@]:1}"; do |
| 134 | r=$(basename "${rf}" .rcpt); b=$(stat -c %s "${rf}"); id=- |
| 135 | [[ "${b}" == 416 ]] && id=$(sha256sum "${rf}" | cut -c1-64) |
| 136 | check "${serial}" "${r}.rcpt stored from the Store (bytes=${b})" "^osc_receipt: ${r//./\\.}\\.rcpt STORED bytes=${b} id=${id} " |
| 137 | done |
| 138 | check "${serial}" "receipt totals: 14 seen, 14 stored" "^osc_receipts: seen=14 stored=14 dropped=0 " |
| 139 | check "${serial}" "self-test announces 19 launches" "^osc_rcpt_selftest: launches=19 unit=a01_valid_min\\.unit; " |
| 140 | |
| 141 | label='QEMU \(aarch64 virt\), TEST signer, not physical' |
| 142 | a=a01_valid_min.unit |
| 143 | # rline TEXT ARGS RECEIPT POLICY RESULT REVOKED [run]: one launch line. A refusal ran nothing: no tick, no page. |
| 144 | rline() { |
| 145 | local file="${rfix}/$3" id='-' t |
| 146 | [[ -e "${ofix}/$3" ]] && file="${ofix}/$3" |
| 147 | [[ "$3" != none && $(stat -c %s "${file}") == 416 ]] && id=$(sha256sum "${file}" | cut -c1-16) |
| 148 | t="ticks=0 pages_mapped=0 page_tables_zeroed=1 slot_free=1" |
| 149 | [[ "${7:-}" == run ]] && t="ticks=[0-9]+ pages_mapped=[1-9][0-9]* page_tables_zeroed=1 slot_free=1" |
| 150 | check "${serial}" "$1" "^osc_rcpt_launch: ${a} add\\($2\\) receipt=${3//./\\.} policy=$4 -> $5 receipt_id=${id} revoked=$6 ${t}; ${label}\$" |
| 151 | } |
| 152 | ref() { echo "REFUSED_AT_ADMISSION code=$1 name=RECEIPT_$2"; } |
| 153 | rline "valid receipt: the unit runs, add(1,2) RETURNED 3" '1,2' a01.rcpt qualification 'RETURNED value=3' - run |
| 154 | rline "no receipt: refused RECEIPT_MISSING (64)" '2,3' none qualification "$(ref 64 MISSING)" - |
| 155 | rline "altered receipt (one byte of the executable digest flipped): RECEIPT_BAD_SIGNATURE (75)" '3,4' n_altered.rcpt qualification "$(ref 75 BAD_SIGNATURE)" - |
| 156 | rline "replayed receipt below the epoch floor: RECEIPT_STALE_EPOCH (77)" '4,5' n_stale.rcpt qualification "$(ref 77 STALE_EPOCH)" - |
| 157 | rline "revoked receipt id: RECEIPT_REVOKED (76), revoked=receipt_id" '5,6' n_revoked_id.rcpt qualification "$(ref 76 REVOKED)" receipt_id |
| 158 | rline "revoked signer key (the TEST key fingerprint revoked): RECEIPT_REVOKED (76), revoked=signer_key" '6,7' a01.rcpt revoked_test_key "$(ref 76 REVOKED)" signer_key |
| 159 | rline "expired receipt: RECEIPT_EXPIRED (78)" '7,8' n_expired.rcpt qualification "$(ref 78 EXPIRED)" - |
| 160 | rline "receipt not yet valid: RECEIPT_NOT_YET_VALID (79)" '8,9' n_future.rcpt qualification "$(ref 79 NOT_YET_VALID)" - |
| 161 | rline "receipt for another target: RECEIPT_WRONG_TARGET (80)" '9,10' n_target.rcpt qualification "$(ref 80 WRONG_TARGET)" - |
| 162 | rline "receipt for another executable (l01's): RECEIPT_EXECUTABLE_MISMATCH (83)" '10,11' l01.rcpt qualification "$(ref 83 EXECUTABLE_MISMATCH)" - |
| 163 | rline "receipt for another program: RECEIPT_PROGRAM_MISMATCH (82)" '11,12' n_program.rcpt qualification "$(ref 82 PROGRAM_MISMATCH)" - |
| 164 | rline "receipt of the wrong length (417 bytes): RECEIPT_SIZE (65)" '12,13' n_length.rcpt qualification "$(ref 65 SIZE)" - |
| 165 | rline "TEST receipt under the release policy: RECEIPT_TEST_SIGNER_IN_PRODUCTION (73)" '13,14' a01.rcpt release "$(ref 73 TEST_SIGNER_IN_PRODUCTION)" - |
| 166 | rline "receipt naming firmware: RECEIPT_WRONG_FIRMWARE (81)" '14,15' n_firmware.rcpt qualification "$(ref 81 WRONG_FIRMWARE)" - |
| 167 | rline "receipt with a dependency: RECEIPT_DEPENDENCIES_MISMATCH (88)" '15,16' n_deps.rcpt qualification "$(ref 88 DEPENDENCIES_MISMATCH)" - |
| 168 | rline "receipt from an unlisted checker: RECEIPT_UNTRUSTED_CHECKER (86)" '16,17' n_checker.rcpt qualification "$(ref 86 UNTRUSTED_CHECKER)" - |
| 169 | rline "owner receipt (signed on the owner YubiKey) in release mode: the unit runs, add(17,18) RETURNED 35" '17,18' a01.owner.rcpt release 'RETURNED value=35' - run |
| 170 | rline "valid receipt with code changed after admission: CODE_HASH_MISMATCH (16), nothing mapped or executed" '18,19' a01.rcpt qualification 'REFUSED_AT_ADMISSION code=16 name=CODE_HASH_MISMATCH' - |
| 171 | rline "after every refusal, the valid receipt still runs the unit: add(20,22) RETURNED 42" '20,22' a01.rcpt qualification 'RETURNED value=42' - run |
| 172 | check "${serial}" "summary: 3 returned, 16 refused" "^osc_rcpt_launches: returned=3 trapped=0 refused=16 unknown=0; ${label}\$" |
| 173 | nl=$(grep -c '^osc_rcpt_launch: ' "${serial}" || true) |
| 174 | names=$(grep -oE '^osc_rcpt_launch: .* name=RECEIPT_[A-Z_]+' "${serial}" | sed -E 's/.*name=//' | sort | uniq -d | tr '\n' ' ') |
| 175 | [[ "${nl}" == 19 ]] && pass "exactly 19 receipt launch lines" || fail "receipt launch lines (${nl} of 19)" |
| 176 | # REVOKED appears twice on purpose (receipt id, signer key), told apart by the revoked= column |
| 177 | [[ "${names}" == "RECEIPT_REVOKED " ]] && pass "every other refusal names a reason of its own" || fail "refusal reasons repeated: ${names}" |
| 178 | if grep -E '^osc_launch: ' "${serial}" | grep -q .; then fail "a C3-3a launch line appeared in the receipt self-test"; else pass "only the receipt self-test ran"; fi |
| 179 | |
| 180 | if [[ -n "${AIENOS_LOG_DIR:-}" ]]; then cp "${serial}" "${AIENOS_LOG_DIR}/qemu_ck_osc_receipt_serial.log"; fi |
| 181 | if [[ "${failed}" != 0 || -n "${AIENOS_QEMU_VERBOSE:-}" ]]; then |
| 182 | echo "---- serial (osc lines) ----"; grep -E "^(osc_|artifact|kernel|report_kind|panic|fault)" "${serial}" | head -80 || true |
| 183 | fi |
| 184 | echo "label: QEMU (aarch64 virt), TEST signer, not physical" |
| 185 | if [[ "${failed}" == 0 ]]; then echo "${verdict}: PASS"; else echo "${verdict}: FAIL"; exit 1; fi |