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 / check_receipt_labels.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 | # check_receipt_labels.sh: lint for evidence receipts that carry hardcoded |
| 18 | # invariant labels (a literal `true` inside an "invariants" block). |
| 19 | # |
| 20 | # A receipt with such a block is only acceptable if an addendum in the same |
| 21 | # folder amends it: the addendum names the receipt's path and carries the |
| 22 | # receipt's current sha256. Without that, the literal true reads as a PASS that |
| 23 | # nothing checked. |
| 24 | # |
| 25 | # Usage: scripts/check_receipt_labels.sh [EVIDENCE_DIR] (default: ../evidence) |
| 26 | # scripts/check_receipt_labels.sh --self-test |
| 27 | # Exit: 0 = every such receipt is amended, 1 = at least one is not. |
| 28 | set -uo pipefail |
| 29 | |
| 30 | lint_dir() { # DIR -> prints PASS/FAIL lines, returns 1 on any FAIL |
| 31 | local dir="$1" rc=0 f base h found |
| 32 | shopt -s nullglob |
| 33 | for f in "${dir}"/*.json; do |
| 34 | base="$(basename "${f}")" |
| 35 | case "${base}" in *addendum*) continue ;; esac |
| 36 | # a literal true inside an "invariants" object (up to the closing brace) |
| 37 | if sed -n '/"invariants"[[:space:]]*:[[:space:]]*{/,/}/p' "${f}" | grep -Eq ':[[:space:]]*true[[:space:]]*,?[[:space:]]*$'; then |
| 38 | h="$(sha256sum "${f}" | cut -d' ' -f1)" |
| 39 | found=0 |
| 40 | for a in "${dir}"/*addendum*.json; do |
| 41 | if grep -Eq "\"path\"[[:space:]]*:[[:space:]]*\"evidence/${base}\"" "${a}" && grep -Eq "\"sha256\"[[:space:]]*:[[:space:]]*\"${h}\"" "${a}"; then found=1; break; fi |
| 42 | done |
| 43 | if [[ ${found} == 1 ]]; then echo "PASS ${base}: hardcoded invariants are amended by an addendum bound to sha256 ${h:0:8}" |
| 44 | else echo "FAIL ${base}: literal true invariants with no addendum bound to its current sha256 ${h:0:8}"; rc=1; fi |
| 45 | fi |
| 46 | done |
| 47 | return ${rc} |
| 48 | } |
| 49 | |
| 50 | if [[ "${1:-}" == --self-test ]]; then |
| 51 | st=0; t="$(mktemp -d)"; trap 'rm -rf "${t}"' EXIT |
| 52 | expect() { # name want dir |
| 53 | local rc=0; lint_dir "$3" >"${t}/out.txt" 2>&1 || rc=$? |
| 54 | if [[ "${rc}" == "$2" ]]; then echo "PASS ${1} -> exit ${rc}"; else echo "FAIL ${1}: wanted ${2}, got ${rc}"; cat "${t}/out.txt"; st=1; fi |
| 55 | } |
| 56 | mk() { mkdir -p "${t}/$1"; printf '{\n "invariants": {\n "a_check": true,\n "policy": "x"\n }\n}\n' >"${t}/$1/r.json"; } |
| 57 | mk bare; expect "mutant: literal true, no addendum -> FAIL" 1 "${t}/bare" |
| 58 | mk good; h="$(sha256sum "${t}/good/r.json" | cut -d' ' -f1)" |
| 59 | printf '{"amends_receipt":{"path":"evidence/r.json","sha256":"%s"}}\n' "${h}" >"${t}/good/r_addendum.json" |
| 60 | expect "amended by an addendum bound to the hash -> PASS" 0 "${t}/good" |
| 61 | mk stale; printf '{"amends_receipt":{"path":"evidence/r.json","sha256":"%064d"}}\n' 0 >"${t}/stale/r_addendum.json" |
| 62 | expect "mutant: addendum bound to a different hash -> FAIL" 1 "${t}/stale" |
| 63 | mkdir -p "${t}/nr"; printf '{\n "invariants": {\n "a_check": "NOT_RUN"\n }\n}\n' >"${t}/nr/r.json" |
| 64 | expect "NOT_RUN labels need no addendum -> PASS" 0 "${t}/nr" |
| 65 | mkdir -p "${t}/other"; printf '{\n "ok": true\n}\n' >"${t}/other/r.json" |
| 66 | expect "receipt with no invariants block -> PASS" 0 "${t}/other" |
| 67 | if [[ ${st} == 0 ]]; then echo "CHECK_RECEIPT_LABELS_SELF_TEST: PASS"; exit 0; fi |
| 68 | echo "CHECK_RECEIPT_LABELS_SELF_TEST: FAIL"; exit 1 |
| 69 | fi |
| 70 | |
| 71 | dir="${1:-$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)/evidence}" |
| 72 | if lint_dir "${dir}"; then echo "RECEIPT_LABELS: PASS"; exit 0; fi |
| 73 | echo "RECEIPT_LABELS: FAIL"; exit 1 |