interplane.Get Omega
Read in your language

aien-architecture-mv

Canonical architecture for the AIEN ecosystem: flows, boundaries, ADRs, and the map from design to implementation repositories.

Brought by drakestapleton

branch main Branches Tags History

aien-architecture-mv / scripts / pipeline-proofs / 23-hostile-records.sh

64 lines · 4263 bytes Raw Blame History
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.
7here="$(cd "$(dirname "$0")" && pwd)"; . "$here/lib.sh"
8unset CRUMB_EXPECT_STORE_ID CRUMB_STRICT_STORE_ID
9W=${W:-10}
10proof_begin 23 "hostile store files are refused, bounded, never replaced" "${1:-}"
11clone_repo "$TMP/a"
12L=$(leaf_crumb_dir "$TMP/a") || L=docs
13STORE=$(git -C "$TMP/a" rev-parse --absolute-git-dir)/crumb/v1
14num "target=$L"; num "watchdog=${W}s"
15CR=$(command -v "$CRUMB" || echo "$CRUMB")
16bounded() { # 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
28cd "$TMP/a" || exit 1
29$CRUMB claim agent-A . "$L" "proof 23: seed the store" 600 >/dev/null 2>&1
30REC=$(find "$STORE/dirs" -name "*.json" -type f | sort | head -1); note "record: ${REC#$STORE/}"
31mv "$REC" "$TMP/aside.json"; mkfifo "$REC"
32bounded fifo_sniff sniff agent-B .
33check "FIFO record: sniff refused (exit 4), no hang" [ "$HUNG" -eq 0 -a "$RC" -eq 4 ]
34check "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)"
35bounded fifo_claim claim agent-B . "$L" "proof 23" 600
36check "FIFO record: claim refused (exit 4), no hang" [ "$HUNG" -eq 0 -a "$RC" -eq 4 ]
37check "FIFO left in place" [ -p "$REC" ]
38rm -f "$REC"; ln -s /dev/zero "$REC"
39bounded zero_sniff sniff agent-B .
40check "record symlinked to /dev/zero: refused (exit 4), no unbounded read" [ "$HUNG" -eq 0 -a "$RC" -eq 4 ]
41check "refusal names the symbolic link" grep -q 'is a symbolic link' <<<"$OUT"
42rm -f "$REC"; { printf '{"schema_version": "1.0.0", "directory": "x", "pad": "'; head -c 1100000 /dev/zero | tr '\0' x; printf '"}\n'; } > "$REC"
43s0=$(cksum < "$REC")
44bounded oversize_sniff sniff agent-B .
45check "oversize record (> 1 MiB): refused (exit 4)" [ "$HUNG" -eq 0 -a "$RC" -eq 4 ]
46check "refusal names the 1 MiB cap" grep -q 'is larger than 1048576 bytes' <<<"$OUT"
47check_eq "oversize record byte-identical after the refusal" "$(cksum < "$REC")" "$s0"
48rm -f "$REC"; mv "$TMP/aside.json" "$REC"
49rm -f "$STORE/.lock"; mkdir "$STORE/.lock"
50bounded lockdir_claim claim agent-B . README-proof23 "proof 23" 600
51check "directory planted as .lock: named refusal (exit 4)" [ "$HUNG" -eq 0 -a "$RC" -eq 4 ]
52check "refusal names .lock as a directory" grep -q '\.lock is a directory' <<<"$OUT"
53rmdir "$STORE/.lock"
54mkdir -p "$TMP/outside"; ln -s "$TMP/outside" "$STORE/dirs/evil"
55bounded symdir_claim claim agent-B . evil/sub/f.c "proof 23" 600
56check "record path through a symlinked directory: refused (exit 4)" [ "$HUNG" -eq 0 -a "$RC" -eq 4 ]
57check_eq "nothing written outside the store" "$(ls -A "$TMP/outside" | wc -l)" 0
58rm -f "$STORE/dirs/evil"
59bounded restored_claim claim agent-B . README-proof23 "proof 23: after moving files aside" 600
60check "after moving the hostile files aside the store works again (rc 0)" [ "$HUNG" -eq 0 -a "$RC" -eq 0 ]
61bounded restored_sniff sniff agent-B .
62check "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
64proof_end