atlas
aien-dev/atlas
Brought by drakestapleton
atlas / scripts / test-host-security.sh
| 1 | #!/bin/sh |
| 2 | # Offline host-only controls for m1tool and the KAT gate. No QEMU or receipt writes. |
| 3 | set -eu |
| 4 | ROOT=$(cd "$(dirname "$0")/.." && pwd -P) |
| 5 | T=$(mktemp -d "${TMPDIR:-/tmp}/atlas-host-security.XXXXXXXX") |
| 6 | trap 'rm -rf "$T"' EXIT |
| 7 | trap 'exit 1' INT TERM |
| 8 | : "${CC:=gcc}" |
| 9 | "$CC" -O1 -g -fsanitize=address,undefined -fno-omit-frame-pointer -std=c11 -Wall -Wextra -Werror \ |
| 10 | -o "$T/m1tool" "$ROOT/tools/m1tool.c" "$ROOT/sha256_clean.c" |
| 11 | export ASAN_OPTIONS=detect_leaks=0 |
| 12 | |
| 13 | audit() { |
| 14 | "$T/m1tool" audit-verify "$ROOT/atlas.bin" "$ROOT/atlas.sha256" \ |
| 15 | "$ROOT/atlas.manifest" "$ROOT/machine_contract.json" "$ROOT/atlas.audit" "$1" |
| 16 | } |
| 17 | audit "$ROOT/atlas.decode" > "$T/golden.log" |
| 18 | for operand in 'x19, 160' 'x0, 138'; do |
| 19 | case $operand in x19*) f=target ;; *) f=stack ;; esac |
| 20 | sed "s/ldr[[:space:]]$operand/ldr\t${operand%%,*}, fffffffffffffffc/" \ |
| 21 | "$ROOT/atlas.decode" > "$T/$f.decode" |
| 22 | grep -q fffffffffffffffc "$T/$f.decode" |
| 23 | if audit "$T/$f.decode" > "$T/$f.log" 2>&1; then |
| 24 | echo "unexpected PASS: malformed $f offset" >&2; exit 1 |
| 25 | fi |
| 26 | grep -q 'FAIL:' "$T/$f.log" |
| 27 | if grep -q AddressSanitizer "$T/$f.log"; then |
| 28 | echo "sanitizer abort: $f offset" >&2; exit 1 |
| 29 | fi |
| 30 | done |
| 31 | marker="$T/shell-marker" |
| 32 | if "$T/m1tool" gen-audit "$ROOT/atlas.bin" \ |
| 33 | "missing; touch $marker; #" "$T/audit" "$T/decode" > "$T/injection.log" 2>&1; then |
| 34 | echo 'unexpected PASS: nonexistent ELF' >&2; exit 1 |
| 35 | fi |
| 36 | [ ! -e "$marker" ] || { echo 'shell interpreted artifact path' >&2; exit 1; } |
| 37 | |
| 38 | # A deterministic inert QEMU stand-in exercises the complete mutation path |
| 39 | # without asserting anything about an actual emulated boot. |
| 40 | cat > "$T/qemu-system-aarch64" <<'EOF' |
| 41 | #!/bin/sh |
| 42 | case "$*" in |
| 43 | *atlas_sweep_*) echo 'ATLAS: REFUSE' ;; |
| 44 | *) echo 'ATLAS: HANDOFF'; echo 'PHYSICS-0: AUTHORIZED' ;; |
| 45 | esac |
| 46 | EOF |
| 47 | chmod 700 "$T/qemu-system-aarch64" |
| 48 | printf KEEP_ME > "$T/victim" |
| 49 | PATH="$T:$PATH" sh -c ' |
| 50 | printf "%s" "$$" > "$5" |
| 51 | ln -s "$1" "/tmp/atlas_sweep_$$.bin" |
| 52 | exec "$2" qemu-matrix "$3" "$4" |
| 53 | ' sh "$T/victim" "$T/m1tool" "$ROOT/atlas.bin" "$ROOT/physics-0.bin" "$T/pid" > "$T/matrix.log" |
| 54 | rm -f "/tmp/atlas_sweep_$(cat "$T/pid").bin" |
| 55 | [ "$(cat "$T/victim")" = KEEP_ME ] || { echo 'predictable temp clobbered victim' >&2; exit 1; } |
| 56 | grep -q 'SEAM 2 RESULTS: ALL EXECUTION GATES PASSED' "$T/matrix.log" |
| 57 | |
| 58 | # Replace only the freshly built ignored KAT executable with a failing |
| 59 | # surrogate; the runner must stop before printing any gate PASS summary. |
| 60 | REAL_CC=$(command -v gcc) |
| 61 | export REAL_CC ROOT |
| 62 | cat > "$T/gcc" <<'EOF' |
| 63 | #!/bin/sh |
| 64 | "$REAL_CC" "$@" || exit $? |
| 65 | case " $* " in |
| 66 | *test_sha256_kat*) printf '#!/bin/sh\necho "[PASS] misleading KAT output"\nexit 17\n' > "$ROOT/test_sha256_kat"; chmod 700 "$ROOT/test_sha256_kat" ;; |
| 67 | esac |
| 68 | EOF |
| 69 | chmod 700 "$T/gcc" |
| 70 | if PATH="$T:$PATH" "$ROOT/run_m1_gates.sh" > "$T/kat.log" 2>&1; then |
| 71 | echo 'unexpected PASS: failed SHA-256 KAT' >&2; exit 1 |
| 72 | else |
| 73 | rc=$? |
| 74 | fi |
| 75 | [ "$rc" -eq 17 ] || { echo "wrong KAT refusal status: $rc" >&2; exit 1; } |
| 76 | grep -q 'known-answer test exited 17' "$T/kat.log" |
| 77 | if grep -q 'Overall Result: MILESTONE 1 QUALIFIED' "$T/kat.log"; then |
| 78 | echo 'qualification summary printed after KAT failure' >&2; exit 1 |
| 79 | fi |
| 80 | printf 'ATLAS-HOST-SECURITY PASS (sanitizer bounds, argv, private temp, failed KAT)\n' |