aien-architecture-mv
Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.
Brought by drakestapleton
aien-architecture-mv / scripts / pipeline-proofs / 23-hostile-records.sh
| 1 | #!/usr/bin/env bash |
| 2 | # 23: hostile coordination-store files get a bounded, named refusal (aien-architecture#234, synapse#58). On a clone: |
| 3 | # a FIFO record, a record symlinked to /dev/zero, an oversize record, a directory planted as .lock and a symlinked |
| 4 | # directory under dirs/ are each refused (exit 4, REFUSED_RECORD) without hanging, without unbounded reads and |
| 5 | # without replacing the planted file; moving the file aside restores normal operation. Every crumb run here is under |
| 6 | # a watchdog (background kill after W seconds, default 10) and a 1 GiB address-space limit. Records refusal latency. |
| 7 | here="$(cd "$(dirname "$0")" && pwd)"; . "$here/lib.sh" |
| 8 | unset CRUMB_EXPECT_STORE_ID CRUMB_STRICT_STORE_ID |
| 9 | W=${W:-10} |
| 10 | proof_begin 23 "hostile store files are refused, bounded, never replaced" "${1:-}" |
| 11 | clone_repo "$TMP/a" |
| 12 | L=$(leaf_crumb_dir "$TMP/a") || L=docs |
| 13 | STORE=$(git -C "$TMP/a" rev-parse --absolute-git-dir)/crumb/v1 |
| 14 | num "target=$L"; num "watchdog=${W}s" |
| 15 | CR=$(command -v "$CRUMB" || echo "$CRUMB") |
| 16 | bounded() { # bounded <label> <crumb args...> -> like measure, plus HUNG=1 if the watchdog killed it |
| 17 | local l=$1; shift |
| 18 | local t0 t1 pid wd o="$TMP/bounded.out"; rm -f "$o.hung"; t0=$(now_ms) |
| 19 | ( cd "$TMP/a" && ulimit -v 1048576 && exec "$CR" "$@" ) > "$o" 2>&1 < /dev/null & |
| 20 | pid=$! |
| 21 | ( n=0; while kill -0 "$pid" 2>/dev/null; do n=$((n + 1)); if [ "$n" -gt $((W * 10)) ]; then : > "$o.hung"; kill -9 "$pid" 2>/dev/null; exit 0; fi; sleep 0.1; done ) & |
| 22 | wd=$! |
| 23 | wait "$pid"; RC=$?; t1=$(now_ms); wait "$wd" 2>/dev/null |
| 24 | MS=$((t1 - t0)); HUNG=0; [ -e "$o.hung" ] && HUNG=1; OUT=$(cat "$o") |
| 25 | echo " MEASURE $l: ${MS} ms (rc=$RC hung=$HUNG)"; NUMBERS="$NUMBERS $l=${MS}ms" |
| 26 | } |
| 27 | |
| 28 | cd "$TMP/a" || exit 1 |
| 29 | $CRUMB claim agent-A . "$L" "proof 23: seed the store" 600 >/dev/null 2>&1 |
| 30 | REC=$(find "$STORE/dirs" -name "*.json" -type f | sort | head -1); note "record: ${REC#$STORE/}" |
| 31 | mv "$REC" "$TMP/aside.json"; mkfifo "$REC" |
| 32 | bounded fifo_sniff sniff agent-B . |
| 33 | check "FIFO record: sniff refused (exit 4), no hang" [ "$HUNG" -eq 0 -a "$RC" -eq 4 ] |
| 34 | check "refusal names REFUSED_RECORD and the fifo" grep -q 'REFUSED_RECORD: .* is a fifo' <<<"$OUT"; note "$(echo "$OUT" | grep REFUSED | head -1 | cut -c1-140)" |
| 35 | bounded fifo_claim claim agent-B . "$L" "proof 23" 600 |
| 36 | check "FIFO record: claim refused (exit 4), no hang" [ "$HUNG" -eq 0 -a "$RC" -eq 4 ] |
| 37 | check "FIFO left in place" [ -p "$REC" ] |
| 38 | rm -f "$REC"; ln -s /dev/zero "$REC" |
| 39 | bounded zero_sniff sniff agent-B . |
| 40 | check "record symlinked to /dev/zero: refused (exit 4), no unbounded read" [ "$HUNG" -eq 0 -a "$RC" -eq 4 ] |
| 41 | check "refusal names the symbolic link" grep -q 'is a symbolic link' <<<"$OUT" |
| 42 | rm -f "$REC"; { printf '{"schema_version": "1.0.0", "directory": "x", "pad": "'; head -c 1100000 /dev/zero | tr '\0' x; printf '"}\n'; } > "$REC" |
| 43 | s0=$(cksum < "$REC") |
| 44 | bounded oversize_sniff sniff agent-B . |
| 45 | check "oversize record (> 1 MiB): refused (exit 4)" [ "$HUNG" -eq 0 -a "$RC" -eq 4 ] |
| 46 | check "refusal names the 1 MiB cap" grep -q 'is larger than 1048576 bytes' <<<"$OUT" |
| 47 | check_eq "oversize record byte-identical after the refusal" "$(cksum < "$REC")" "$s0" |
| 48 | rm -f "$REC"; mv "$TMP/aside.json" "$REC" |
| 49 | rm -f "$STORE/.lock"; mkdir "$STORE/.lock" |
| 50 | bounded lockdir_claim claim agent-B . README-proof23 "proof 23" 600 |
| 51 | check "directory planted as .lock: named refusal (exit 4)" [ "$HUNG" -eq 0 -a "$RC" -eq 4 ] |
| 52 | check "refusal names .lock as a directory" grep -q '\.lock is a directory' <<<"$OUT" |
| 53 | rmdir "$STORE/.lock" |
| 54 | mkdir -p "$TMP/outside"; ln -s "$TMP/outside" "$STORE/dirs/evil" |
| 55 | bounded symdir_claim claim agent-B . evil/sub/f.c "proof 23" 600 |
| 56 | check "record path through a symlinked directory: refused (exit 4)" [ "$HUNG" -eq 0 -a "$RC" -eq 4 ] |
| 57 | check_eq "nothing written outside the store" "$(ls -A "$TMP/outside" | wc -l)" 0 |
| 58 | rm -f "$STORE/dirs/evil" |
| 59 | bounded restored_claim claim agent-B . README-proof23 "proof 23: after moving files aside" 600 |
| 60 | check "after moving the hostile files aside the store works again (rc 0)" [ "$HUNG" -eq 0 -a "$RC" -eq 0 ] |
| 61 | bounded restored_sniff sniff agent-B . |
| 62 | check "sniff shows agent-A's original lock again" grep -q "held by agent-A" <<<"$OUT" |
| 63 | $CRUMB close agent-A . "$L" modify "proof 23 done" >/dev/null 2>&1; $CRUMB close agent-B . README-proof23 modify done >/dev/null 2>&1 |
| 64 | proof_end |