| 1 | //go:build linux |
| 2 | |
| 3 | package containment |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | "fmt" |
| 9 | "io" |
| 10 | "strings" |
| 11 | "testing" |
| 12 | |
| 13 | "amadan.net/oficina/equipo/internal/runner/provider" |
| 14 | ) |
| 15 | |
| 16 | // toolSandbox is the bubblewrap command line the pinned Claude Code 2.1.312 |
| 17 | // builds for every Bash tool command on Linux (its sandbox's R1 builder), |
| 18 | // with the filesystem and network binds reduced to a read-only root: a |
| 19 | // new session, a new user, PID and network namespace, and a fresh /proc. |
| 20 | // The CLI mounts the fresh /proc unless sandbox.enableWeakerNestedSandbox |
| 21 | // is true, when it binds the inherited one instead; the launcher refuses |
| 22 | // settings that set it (PolicyInputs in provider/claude). |
| 23 | var toolSandbox = []string{"/usr/bin/bwrap", "--new-session", "--die-with-parent", "--unshare-net", "--ro-bind", "/", "/", "--dev", "/dev", "--unshare-pid", "--unshare-user", "--proc", "/proc", "--unsetenv", "SRT_ALLOW_NESTED_USERNS", "--"} |
| 24 | |
| 25 | // procProbe is what a model's Bash command can try against the process |
| 26 | // that holds the token (argument 1, its PID in the namespace the CLI runs |
| 27 | // in; argument 2, its start time, which no PID namespace changes). It |
| 28 | // reads every environ it can name: the PID itself, its parent, every |
| 29 | // visible process and thread, and the aliases through /proc/self/.., |
| 30 | // task/<tid> and another process's root. It prints one "exposed" line |
| 31 | // per read that shows the token's variable, a "visible" line where it |
| 32 | // can see the token holder itself, and the number of paths it tried. |
| 33 | const procProbe = `hp=$1; hstart=$2; tried=0 |
| 34 | try() { |
| 35 | tried=$((tried+1)) |
| 36 | if tr '\0' '\n' <"$1" 2>/dev/null | grep -q '^CLAUDE_CODE_OAUTH_TOKEN='; then echo "exposed $1"; fi |
| 37 | } |
| 38 | pids="$hp $PPID 1 2 $(ls /proc | grep -E '^[0-9]+$')" |
| 39 | for p in $pids; do |
| 40 | try "/proc/$p/environ" |
| 41 | try "/proc/self/../$p/environ" |
| 42 | try "/proc/$p/root/proc/$hp/environ" |
| 43 | for task in /proc/$p/task/*; do try "$task/environ"; done |
| 44 | stat=$(cat /proc/$p/stat 2>/dev/null) || continue |
| 45 | fields=(${stat##*) }) |
| 46 | if [ "${fields[19]}" = "$hstart" ]; then echo "visible $p"; fi |
| 47 | done |
| 48 | try "/proc/$hp/task/$hp/environ" |
| 49 | try "/proc/self/root/proc/$hp/environ" |
| 50 | try "/proc/1/root/proc/$hp/environ" |
| 51 | try "/proc/$PPID/root/proc/$hp/environ" |
| 52 | echo "tried $tried" |
| 53 | ` |
| 54 | |
| 55 | // The harness stands in for the claude CLI: it holds the token in its |
| 56 | // environment, as LaunchSecretEnv delivers it, and starts the probe as |
| 57 | // the CLI starts a Bash tool command: without the token in the child's |
| 58 | // environment (CLAUDE_CODE_SUBPROCESS_ENV_SCRUB), once directly and once |
| 59 | // in the CLI's tool sandbox. |
| 60 | const procHarness = `import json,os,subprocess,sys |
| 61 | assert os.environ.get("CLAUDE_CODE_OAUTH_TOKEN") |
| 62 | env={k:v for k,v in os.environ.items() if k!="CLAUDE_CODE_OAUTH_TOKEN"} |
| 63 | sandbox=json.loads(%q); probe=json.loads(%q) |
| 64 | me=str(os.getpid()); start=open("/proc/self/stat").read().rsplit(")",1)[1].split()[19] |
| 65 | out={"pid":me} |
| 66 | for name,prefix in [("direct",[]),("sandboxed",sandbox)]: |
| 67 | r=subprocess.run(prefix+["/usr/bin/bash","-c",probe,"probe",me,start],env=env,stdin=subprocess.DEVNULL,capture_output=True,text=True,timeout=20) |
| 68 | out[name]={"code":r.returncode,"out":r.stdout,"err":r.stderr[-400:]} |
| 69 | print(json.dumps(out),flush=True) |
| 70 | ` |
| 71 | |
| 72 | type probeRun struct { |
| 73 | Code int |
| 74 | Out, Err string |
| 75 | } |
| 76 | |
| 77 | // The pasted token is in the CLI's environment. A command the model runs |
| 78 | // through the Bash tool must not read it from /proc: the tool sandbox's |
| 79 | // fresh PID namespace and /proc hide the CLI and every ancestor, under |
| 80 | // every alias. The direct run is the control: without the namespace the |
| 81 | // same probe reads the token, so the probe is not vacuous. |
| 82 | func TestToolCannotReadCLIEnvironment(t *testing.T) { |
| 83 | m, l := fixture(t) |
| 84 | m.secretEnv = func(provider.Launch) (map[string]string, error) { |
| 85 | return map[string]string{provider.OAuthTokenEnv: canary, provider.ScrubEnv: "1"}, nil |
| 86 | } |
| 87 | sandbox, _ := json.Marshal(toolSandbox) |
| 88 | probe, _ := json.Marshal(procProbe) |
| 89 | p := launch(t, m, l, fmt.Sprintf(procHarness, sandbox, probe)) |
| 90 | raw, err := io.ReadAll(p.Stdout()) |
| 91 | if err != nil { |
| 92 | t.Fatal(err) |
| 93 | } |
| 94 | if exit, err := p.Wait(context.Background()); err != nil || exit.Code != 0 { |
| 95 | errout, _ := io.ReadAll(p.Stderr()) |
| 96 | t.Fatalf("harness: %+v %v %s", exit, err, errout) |
| 97 | } |
| 98 | if strings.Contains(string(raw), canary) { |
| 99 | t.Fatal("the token's value reached the probe's output") |
| 100 | } |
| 101 | var out struct { |
| 102 | PID string |
| 103 | Direct, Sandboxed probeRun |
| 104 | } |
| 105 | if err = json.Unmarshal(raw, &out); err != nil { |
| 106 | t.Fatalf("harness output %q: %v", raw, err) |
| 107 | } |
| 108 | // Control: run directly, the probe reads the CLI's environment. |
| 109 | if out.Direct.Code != 0 || !strings.Contains(out.Direct.Out, "exposed /proc/"+out.PID+"/environ\n") || !strings.Contains(out.Direct.Out, "visible "+out.PID+"\n") { |
| 110 | t.Fatalf("control probe found nothing, so it proves nothing: %+v", out.Direct) |
| 111 | } |
| 112 | // The namespace helper (PID 1) holds the token too, but is not |
| 113 | // dumpable: not even a direct child of the CLI reads its environment. |
| 114 | if strings.Contains(out.Direct.Out, "exposed /proc/1/") { |
| 115 | t.Fatalf("the namespace helper's environment is readable: %s", out.Direct.Out) |
| 116 | } |
| 117 | run := out.Sandboxed |
| 118 | if run.Code != 0 || !strings.HasPrefix(lastLine(run.Out), "tried ") { |
| 119 | t.Fatalf("tool sandbox did not start inside the namespace: %+v", run) |
| 120 | } |
| 121 | var tried int |
| 122 | if _, err = fmt.Sscanf(lastLine(run.Out), "tried %d", &tried); err != nil || tried < 12 { |
| 123 | t.Fatalf("probe tried %d paths", tried) |
| 124 | } |
| 125 | t.Logf("control: %d reads exposed the token; sandboxed: %d paths tried", strings.Count(out.Direct.Out, "exposed "), tried) |
| 126 | for _, line := range strings.Split(strings.TrimSpace(run.Out), "\n") { |
| 127 | if strings.HasPrefix(line, "exposed ") || strings.HasPrefix(line, "visible ") { |
| 128 | t.Errorf("from the tool sandbox: %s", line) |
| 129 | } |
| 130 | } |
| 131 | } |
| 132 | |
| 133 | func lastLine(s string) string { |
| 134 | lines := strings.Split(strings.TrimSpace(s), "\n") |
| 135 | return lines[len(lines)-1] |
| 136 | } |
| 137 | |