cuda-crack
I broke CUDA: GB10 sm_121 machine code without the CUDA toolkit - method, encoder tables, receipts
Brought by drakestapleton
cuda-crack / tests / agent-gate.sh
| 1 | #!/bin/sh |
| 2 | # tests/agent-gate.sh: checks the --agent command gate and the --turns check of replicate.sh. |
| 3 | # No model, no network, no GPU: it runs the gate code of agent.py on fixed command strings. |
| 4 | # sh tests/agent-gate.sh [path/to/replicate.sh] (default: replicate.sh next to tests/) |
| 5 | # Prints one line per case and "agent-gate: N passed, M failed". Exit 0 only when every case passes. |
| 6 | set -u |
| 7 | here=$(cd "$(dirname "$0")" && pwd) |
| 8 | script=${1:-$here/../replicate.sh} |
| 9 | tmp=$(mktemp -d) |
| 10 | trap 'rm -rf "$tmp"' EXIT INT TERM |
| 11 | awk '/<<.AGENT_PY_EOF.$/ {f=1; next} /^AGENT_PY_EOF$/ {f=0} f' "$script" > "$tmp/agent.py" |
| 12 | [ -s "$tmp/agent.py" ] || { echo "agent-gate: no agent.py in $script"; exit 2; } |
| 13 | |
| 14 | fail=0 pass=0 |
| 15 | python3 - "$tmp/agent.py" > "$tmp/py.out" <<'PY_EOF' |
| 16 | import os, sys |
| 17 | src = open(sys.argv[1], encoding="utf-8").read() |
| 18 | src = src[:src.index("\nprompt = open(")] # the gate code; the rest needs a model |
| 19 | |
| 20 | def load(yes, chip): |
| 21 | os.environ.update(WS="/nonexistent", AGENT_URL="http://127.0.0.1:9", AGENT_MODEL="none", |
| 22 | AGENT_YES="1" if yes else "0", AGENT_CHIP="1" if chip else "0", AGENT_TTY="") |
| 23 | ns = {} |
| 24 | exec(src, ns) |
| 25 | return ns |
| 26 | |
| 27 | def decide(ns, cmd): |
| 28 | if "gate" in ns: |
| 29 | return ns["gate"](cmd)[0] |
| 30 | # replicate.sh before gate() existed: the decision its main loop made |
| 31 | if ns["FORBID"].search(cmd): |
| 32 | return "refuse" |
| 33 | chip = bool(ns["CHIPCMD"].search(cmd)) |
| 34 | if chip and not ns["CHIP"]: |
| 35 | return "refuse" |
| 36 | return "auto" if ns["YES"] and not chip else "ask" |
| 37 | |
| 38 | NOT_AUTO = ("refuse", "ask") |
| 39 | cases = [ |
| 40 | # (--yes, --chip, command, allowed decisions, what it checks) |
| 41 | (1, 0, r's\udo id', ("refuse",), "bypass: backslash in sudo"), |
| 42 | (1, 0, 'sud""o id', ("refuse",), "bypass: empty quotes in sudo"), |
| 43 | (1, 0, 'curl http://127.0.0.1:1""8091/v1', ("refuse",), "bypass: quotes in port 18091"), |
| 44 | (1, 0, 'printf c3VkbyBpZAo= | base64 -d | sh', NOT_AUTO, "bypass: base64 piped to sh"), |
| 45 | (1, 0, 'sh -c "make doc""tor"', ("refuse",), "bypass: chip step via sh -c and quotes, no --chip"), |
| 46 | (1, 1, 'sh -c "make doc""tor"', ("ask",), "chip step with --chip still needs a human y"), |
| 47 | (1, 0, 'nvidia-smi --reset-gpu-clocks', ("refuse",), "nvidia-smi --reset-gpu-clocks"), |
| 48 | (1, 0, 'nvidia-smi --reset-memory-clocks', ("refuse",), "nvidia-smi --reset-memory-clocks"), |
| 49 | (1, 0, 'nvidia-smi -rac', ("refuse",), "nvidia-smi -rac"), |
| 50 | (1, 0, 'nvidia-smi -r', ("refuse",), "nvidia-smi -r"), |
| 51 | (1, 0, 'nvidia-smi --gpu-reset', ("refuse",), "nvidia-smi --gpu-reset"), |
| 52 | (1, 0, 'sudo id', ("refuse",), "plain sudo"), |
| 53 | (1, 0, 'make doctor', ("refuse",), "plain chip step without --chip"), |
| 54 | (1, 0, 'nvidia-smi -pl 100', ("refuse",), "plain power limit"), |
| 55 | (1, 0, 'echo "$(id)"', NOT_AUTO, "quotes and $( ) go to the human"), |
| 56 | # RevCrackRepl's second round on 5c2635a9: variables, heredocs and interpreters |
| 57 | (1, 0, 'a=su; b=do; $a$b id', NOT_AUTO, "bypass: sudo from two variables"), |
| 58 | (1, 0, 't=doctor\ncd $R/CUDApp && make $t', NOT_AUTO, "bypass: chip step from a variable, no --chip"), |
| 59 | (1, 0, 't=doctor; make $t', NOT_AUTO, "bypass: chip step from a variable, one line"), |
| 60 | (1, 0, 'p=18\nq=091\ncurl http://127.0.0.1:$p$q/v1/models', NOT_AUTO, "bypass: port 18091 from variables"), |
| 61 | (1, 0, 'python3 - <<EOF\nimport os\nos.system(chr(115)+chr(117)+chr(100)+chr(111)+chr(32)+chr(105)+chr(100))\nEOF', |
| 62 | NOT_AUTO, "bypass: python3 heredoc"), |
| 63 | (1, 0, 'perl -e system(chr(115).chr(117).chr(100).chr(111))', NOT_AUTO, "bypass: perl -e"), |
| 64 | (1, 0, 'awk BEGIN{system(id)}', NOT_AUTO, "awk system()"), |
| 65 | (1, 0, 'cat $R/../../../etc/passwd', NOT_AUTO, "path escaping the workspace with .."), |
| 66 | (1, 0, 'cat /etc/passwd', NOT_AUTO, "absolute path outside the workspace"), |
| 67 | (1, 0, 'head -n 5 /nonexistent/../etc/passwd', NOT_AUTO, "absolute path with .."), |
| 68 | (1, 0, 'git -C $R/CUDApp log --output=/tmp/x', NOT_AUTO, "git log --output=FILE"), |
| 69 | (1, 0, 'git -C $R/CUDApp log --output /tmp/x', NOT_AUTO, "git log --output FILE"), |
| 70 | (1, 0, 'grep -n -f /etc/passwd README.md', NOT_AUTO, "grep option outside the list"), |
| 71 | (1, 0, 'ls -la; id', NOT_AUTO, "; after an allowed command"), |
| 72 | (1, 0, 'make hello > /tmp/x', NOT_AUTO, "redirection"), |
| 73 | (1, 0, 'make clean', NOT_AUTO, "make target outside the list"), |
| 74 | (1, 0, 'cd $R/../x && make hello', NOT_AUTO, "cd outside the fixed repositories"), |
| 75 | (1, 0, 'cd $R/CUDApp && rm -rf .', NOT_AUTO, "cd prefix with a command outside the list"), |
| 76 | (1, 0, 'make hello & id', NOT_AUTO, "background &"), |
| 77 | (1, 1, 'cd $R/CUDApp && make doctor', ("ask",), "chip step with --chip and --yes still asks"), |
| 78 | # controls: the allow-list |
| 79 | (1, 0, 'make hello', ("auto",), "control: make hello"), |
| 80 | (1, 0, 'make test-hello-sim', ("auto",), "control: make test-hello-sim"), |
| 81 | (1, 0, 'cd $R/CUDApp && make test-hello-sim', ("auto",), "control: cd $R/CUDApp && make"), |
| 82 | (1, 0, 'cd "$R/omc-f4df8239" && sh seed/omega-from-seed.sh', ("auto",), "control: quoted cd, seed script"), |
| 83 | (1, 0, 'cd $R/cuda-crack/receipts && sha256sum -c --quiet SHA256SUMS', ("auto",), "control: sha256sum -c"), |
| 84 | (1, 0, 'git -C $R/CUDApp rev-parse HEAD', ("auto",), "control: git rev-parse"), |
| 85 | (1, 0, 'git -C replica/omega-mv log --oneline -5', ("auto",), "control: git log"), |
| 86 | (1, 0, 'ls -la $R', ("auto",), "control: ls"), |
| 87 | (1, 0, 'cat $R/cuda-crack/README.md', ("auto",), "control: cat"), |
| 88 | (1, 0, 'cat /nonexistent/replica/cuda-crack/README.md', ("auto",), "control: absolute path inside the workspace"), |
| 89 | (1, 0, 'head -n 20 AGENT-PROMPT.md', ("auto",), "control: head"), |
| 90 | (1, 0, 'wc -l $R/cuda-crack/receipts/SHA256SUMS', ("auto",), "control: wc"), |
| 91 | (1, 0, 'grep -n PASS $R/cuda-crack/README.md', ("auto",), "control: grep -n"), |
| 92 | (1, 0, 'uname -sm', ("auto",), "control: uname -sm"), |
| 93 | (1, 0, 'nproc', ("auto",), "control: nproc"), |
| 94 | (1, 0, 'free -h', ("auto",), "control: free"), |
| 95 | (0, 0, 'make hello', ("ask",), "control: without --yes every command asks"), |
| 96 | ] |
| 97 | for yes, chip, cmd, ok, what in cases: |
| 98 | d = decide(load(yes, chip), cmd) |
| 99 | shown = cmd.replace(chr(10), " " + chr(92) + "n ") |
| 100 | print(("PASS" if d in ok else "FAIL") + f" {d:6} yes={yes} chip={chip} {shown} ({what})") |
| 101 | PY_EOF |
| 102 | rc=$? |
| 103 | cat "$tmp/py.out" |
| 104 | [ "$rc" = 0 ] || { echo "agent-gate: python3 failed (rc $rc)"; exit 2; } |
| 105 | pass=$(grep -c '^PASS' "$tmp/py.out") |
| 106 | fail=$(grep -c '^FAIL' "$tmp/py.out") |
| 107 | |
| 108 | # --turns: an integer from 1 to 200, checked before anything is cloned. "--bogus" after it stops the run |
| 109 | # either way, so nothing is fetched. |
| 110 | mkdir "$tmp/home" |
| 111 | for t in abc 0 201 99999999999999999999 -5 ''; do |
| 112 | out=$(env -i HOME="$tmp/home" PATH=/usr/bin:/bin sh "$script" --turns "$t" --bogus 2>&1) |
| 113 | case $out in |
| 114 | *"--turns needs an integer from 1 to 200"*) echo "PASS --turns '$t' refused"; pass=$((pass + 1)) ;; |
| 115 | *) echo "FAIL --turns '$t' accepted"; fail=$((fail + 1)) ;; |
| 116 | esac |
| 117 | done |
| 118 | out=$(env -i HOME="$tmp/home" PATH=/usr/bin:/bin sh "$script" --turns 200 --bogus 2>&1) |
| 119 | case $out in |
| 120 | *"unknown option --bogus"*) echo "PASS --turns 200 accepted (control)"; pass=$((pass + 1)) ;; |
| 121 | *) echo "FAIL --turns 200 not accepted"; fail=$((fail + 1)) ;; |
| 122 | esac |
| 123 | |
| 124 | echo "agent-gate: $pass passed, $fail failed" |
| 125 | [ "$fail" = 0 ] |