#!/usr/bin/env bash # ───────────────────────────────────────────────────────────────────────────── # selftest-harness.sh — adversarial self-test for check.sh Phase 0c. # # Phase 0c pins the scripts and policy files the button itself runs on. The # attack it exists to stop is the cheapest one in the estate: don't touch the # proofs at all, edit the checker. Round-5 review of the companion SLH-DSA # repository stubbed the compiler wrapper alone and got ALL GREEN in 3.6 # seconds over deliberately destroyed proofs. # # Cases, each asserting a SPECIFIC diagnostic: # 0 positive control: untouched tree passes # 1 a pinned harness file edited by one byte → does not match its pin # 2 a NEW executable appears, unpinned → set mismatch # 3 an entry DELETED from HARNESS.sha256 → set mismatch, NOT a # silent un-pin (this is the shape of the defect SLH-DSA round-6 found: # dropping a key un-pinned two files with no diagnostic at all) # 4 HARNESS.sha256 itself removed → fail-closed # # Phase 0c is lifted out of check.sh at run time, so the tested logic IS the # shipping logic. Cheap: no Lean, runs in about a second. # ───────────────────────────────────────────────────────────────────────────── set -uo pipefail HERE="$(cd "$(dirname "$0")" && pwd)" FAILURES=0 STASH="$(mktemp -d)" NEWEXE="$HERE/zz-selftest-helper.sh" cleanup() { [ -f "$STASH/HARNESS.sha256" ] && cp "$STASH/HARNESS.sha256" "$HERE/HARNESS.sha256" [ -f "$STASH/victim" ] && cp "$STASH/victim" "$HERE/$VICTIM" rm -f "$NEWEXE" rm -rf "$STASH" } trap cleanup EXIT INT TERM cp "$HERE/HARNESS.sha256" "$STASH/HARNESS.sha256" # Lift Phase 0c. The two repo families end the phase differently, so accept # either terminator rather than hardcoding one and silently lifting nothing. DRIVER="$STASH/phase0c.sh" PAYLOAD="$STASH/payload.sh" awk '/^# ── Phase 0c/{f=1} f{print} /^# ── Phase 1|^echo "=== Phase 1/{if(f && !/Phase 0c/) exit}' "$HERE/check.sh" \ | sed '/^# ── Phase 1/d; /^echo "=== Phase 1/d' > "$PAYLOAD" { echo 'set -uo pipefail' echo "HERE=\"$HERE\"" cat "$PAYLOAD" } > "$DRIVER" if [ "$(grep -c . "$PAYLOAD")" -lt 20 ]; then echo "FATAL: could not lift Phase 0c out of check.sh — the phase markers moved." echo "This self-test must attack the shipping gate; refusing to run against nothing." exit 1 fi "$HERE/lift-guard.sh" "$PAYLOAD" "$DRIVER" "check.sh Phase 0c" || exit 1 expect() { # expect