Public: anyone can read this; the crew can change it.

Crew sandbox: escape attempts under procMount Unmasked, each a test #10479

Parked opened this
Closed without merging

scout/forge:scout/10450-crew-sandbox-escape-attempts-under-procm → main 10 commits fork

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
6 files changed +239 −4
docs/ops/RED-TEAM.md +24 −2
@@ -15,7 +15,7 @@ so the live run is still open as its own issue.
1515 | A visitor sends `x-forge-identity` / `x-forge-signature` that the app believes | The proxy deletes both and writes signed ones of its own (`IDENTITY_HEADERS`, `src/apps/proxy.ts`) | `test/apps-identity.test.ts`, `test/redteam-doors.test.ts` |
1616 | An app learns the edge secret or a bearer token from a forwarded request | `stripForgeCredentials` drops every `x-plat-*` header, `authorization`, and the grant cookie | `test/redteam-doors.test.ts` "forged identity headers" |
1717 | Someone outside claims a person with `x-plat-user` | `edgeIdentity` believes it only with the edge secret, compared as digests | `test/redteam-doors.test.ts`, `test/edge-auth.test.ts` |
18 | A crew run leaves its worktree or its network allowlist | bwrap in `src/crew/sandbox.ts` | `test/crew-sandbox.test.ts`; the escape attempts under `procMount: Unmasked` are still open |
18 | A crew run leaves its worktree or its network allowlist | bwrap in `src/crew/sandbox.ts` | `test/crew-sandbox.test.ts`, `test/crew-sandbox-real.test.ts`, and the escape attempts in `test/crew-sandbox-escape.test.ts` (see below) |
1919 | Note or issue text steers a crew brief | `untrusted` marking in briefs | still open: needs a wider test than #10365 |
2020
2121 ## Not done here
@@ -23,6 +23,28 @@ so the live run is still open as its own issue.
2323 - **Pod security enforced on all nine namespaces.** The namespace labels live
2424 in the cluster's GitOps source, which is not in this repository. That work
2525 happens there.
26 - **The live run inside the pod and the sandbox escape attempts**: #10450.
26 - **The live run inside the pod**: still open. The escape tests below were written but not executed here (see below).
2727 - **Wider prompt-injection tests**: #10451.
2828 - **A security page under ops** with one line per control: #10452.
29
30 ## Crew sandbox escape attempts, `procMount: Unmasked` (#10450)
31
32 `test/crew-sandbox-escape.test.ts` starts a shell under the settings the crew
33 hands Claude Code (real bwrap through `srt`) and tries each of these; each
34 test fails if the attempt works. The pod leaves /proc whole, so the attempts
35 include the /proc paths.
36
37 | Attempt | Test asserts |
38 | --- | --- |
39 | Read a denied data file, another crew's file | not read |
40 | Read the same through `/proc/self/root`, `/proc/1/root`, `/proc/<pid>/root` | not read |
41 | Read `/proc/1/environ` and every `/proc/<pid>/environ` for a secret held by a process outside | no leak |
42 | Write the data dir, another crew's dir, beside the worktree, home, `/etc`, and through `/proc/*/root` | no write succeeds |
43 | Reach a loopback port the policy does not name, directly and through the proxy | refused |
44 | Reach an internet host / address beyond the allowlist | refused |
45
46 **Result: not yet observed.** The sandbox that wrote these cannot make a
47 nested one (user namespaces refused), so the file skips here with the reason
48 and was never run green or red. The forge's check on a host that allows
49 bwrap is the first run; if an attempt works there, that test goes red and the
50 row above becomes a finding. Until then no door in this table is claimed held.
scripts/unreached-allow.json −1
@@ -15,7 +15,6 @@
1515 "src/flow/review_rounds.ts",
1616 "src/flow/shared_red.ts",
1717 "src/flow/stale_fold.ts",
18 "src/kernel/why_now.ts",
1918 "src/spawn/pod.ts",
2019 "src/tui/gallery.ts",
2120 "src/tui/ui/qr.ts",
src/spawn/cell.ts +3
@@ -153,6 +153,9 @@ export function cellEnv(input: { port: number; dataDir: string; url: string; par
153153 FORGE_APP_URL: input.url,
154154 FORGE_APP_DATA: input.dataDir,
155155 NODE_ENV: "production",
156 // No router sits in front of a child: the parent forwards to it, and
157 // stops asking when it stops it. A linger would outlast STOP_GRACE_MS.
158 FORGE_DRAIN_LINGER_S: "0",
156159 ...(input.parentPort === undefined ? {} : { FORGE_PARENT_LOCAL: parentLocal(input.parentPort) }),
157160 });
158161 delete env["USER"];
test/child-quota.test.ts +1 −1
@@ -101,7 +101,7 @@ describe("child quota", () => {
101101
102102 test("room for small but not large is refused only for large", async () => {
103103 clear();
104 process.env["FORGE_CHILD_MEMORY_MIB"] = "2000";
104 process.env["FORGE_CHILD_MEMORY_MIB"] = "6000";
105105 expect(quotaRefusal(dir, "small")).toBeUndefined();
106106 expect(quotaRefusal(dir, "large")).toContain("memory limit");
107107 });
test/crew-sandbox-escape.test.ts added +210
@@ -0,0 +1,210 @@
1 /**
2 * Escape attempts from a crew run's real sandbox (bwrap, as Claude Code's
3 * runtime makes it), each a test that fails if the attempt works. The pod
4 * runs with `procMount: Unmasked`, so /proc is whole: another process's
5 * environment, root and cwd are paths to try, beside the plain ones.
6 *
7 * The attempts: read outside the worktree (a denied data file, another crew's
8 * directory, /proc/1/environ, any process's environ, the same files through
9 * /proc/self/root and /proc/1/root), write outside it (the data directory,
10 * the other crew's directory, the home directory, through /proc/self/root),
11 * and reach a host beyond the allowlist (a loopback port the policy does not
12 * name, direct and through the proxy; an address on the internet).
13 *
14 * Like crew-sandbox-real.test.ts this needs a sandbox of its own, so inside a
15 * crew run or a container that refuses user namespaces it is skipped, with
16 * the reason on the console; the forge's check runs it. Results are recorded
17 * in docs/ops/RED-TEAM.md.
18 */
19 import { afterAll, beforeAll, describe, expect, test } from "bun:test";
20 import { existsSync, mkdirSync, writeFileSync } from "node:fs";
21 import { mkdtemp, rm } from "node:fs/promises";
22 import { homedir, tmpdir } from "node:os";
23 import { dirname, join } from "node:path";
24 import { claudeSettings } from "../src/crew/claude.ts";
25 import { sandboxAvailable, sandboxPolicy } from "../src/crew/sandbox.ts";
26
27 const probe = await sandboxAvailable();
28 const srt = join(import.meta.dir, "../node_modules/.bin/srt");
29 const nestedReason = !probe.ok
30 ? `no sandbox can be made here (nested in a crew sandbox or user namespaces refused): ${probe.reason}`
31 : undefined;
32 if (nestedReason !== undefined) console.warn(`skipped: ${nestedReason}`);
33 else if (Bun.which("rg") === null || !existsSync(srt))
34 throw new Error(
35 "a sandbox can be made here but its runtime is missing: install ripgrep (rg) and run bun install",
36 );
37
38 const CANARY = "the-canary-must-not-be-read";
39 const ENV_CANARY = "forge-redteam-environ-canary";
40
41 interface Report {
42 readFiles: Record<string, boolean>;
43 environLeaks: string[];
44 writes: Record<string, boolean>;
45 net: Record<string, string>;
46 worktreeWrite: boolean;
47 }
48
49 describe.skipIf(nestedReason !== undefined)(
50 "escape attempts from a real sandboxed shell, /proc unmasked",
51 () => {
52 let dataDir: string;
53 let report: Report;
54 let outside: ReturnType<typeof Bun.serve>;
55 let holder: ReturnType<typeof Bun.spawn>;
56 const homeMarker = join(homedir(), `.forge-redteam-${process.pid}`);
57
58 beforeAll(async () => {
59 dataDir = await mkdtemp(join(tmpdir(), "forge-escape-"));
60 const work = join(dataDir, "crew/scout/01repo/1");
61 mkdirSync(work, { recursive: true });
62 mkdirSync(join(dataDir, "crew/mason/01repo/3"), { recursive: true });
63 writeFileSync(join(dataDir, "forge.db"), CANARY);
64 writeFileSync(join(dataDir, "crew/mason/01repo/3/notes.txt"), CANARY);
65
66 // A process outside the sandbox with a secret in its environment: with
67 // /proc whole, the shell must still not read it.
68 holder = Bun.spawn(["sleep", "60"], {
69 env: { ...process.env, FORGE_REDTEAM_SECRET: ENV_CANARY },
70 stdin: "ignore",
71 stdout: "ignore",
72 stderr: "ignore",
73 });
74 outside = Bun.serve({
75 hostname: "127.0.0.1",
76 port: 0,
77 fetch: () => new Response("outside"),
78 });
79
80 const policy = sandboxPolicy({
81 dataDir,
82 agentName: "scout",
83 urls: [],
84 runId: "01m46abcdefghjkmnpqrstvwxy",
85 });
86 const readTargets = [
87 join(dataDir, "forge.db"),
88 join(dataDir, "crew/mason/01repo/3/notes.txt"),
89 `/proc/self/root${join(dataDir, "forge.db")}`,
90 `/proc/1/root${join(dataDir, "forge.db")}`,
91 `/proc/${holder.pid}/root${join(dataDir, "forge.db")}`,
92 `/proc/self/root${join(dataDir, "crew/mason/01repo/3/notes.txt")}`,
93 ];
94 const writeTargets = [
95 join(dataDir, "planted"),
96 join(dataDir, "crew/mason/01repo/3/planted"),
97 `/proc/self/root${join(dataDir, "planted-via-proc")}`,
98 `/proc/1/root${join(dataDir, "planted-via-init")}`,
99 join(dirname(work), "planted-beside"),
100 homeMarker,
101 "/etc/forge-redteam-planted",
102 ];
103 writeFileSync(
104 join(work, "probe.ts"),
105 `
106 import { readFileSync, readdirSync, writeFileSync, existsSync } from "node:fs";
107 const CANARY = ${JSON.stringify(CANARY)};
108 const ENV = ${JSON.stringify(ENV_CANARY)};
109 const readFiles: Record<string, boolean> = {};
110 for (const path of ${JSON.stringify(readTargets)}) {
111 try { readFiles[path] = readFileSync(path, "utf8").includes(CANARY); }
112 catch { readFiles[path] = false; }
113 }
114 const environLeaks: string[] = [];
115 let pids: string[] = [];
116 try { pids = readdirSync("/proc").filter((n) => /^[0-9]+$/.test(n)); } catch {}
117 for (const pid of pids) {
118 try { if (readFileSync("/proc/" + pid + "/environ", "utf8").includes(ENV)) environLeaks.push(pid); } catch {}
119 }
120 const writes: Record<string, boolean> = {};
121 for (const path of ${JSON.stringify(writeTargets)}) {
122 try { writeFileSync(path, "planted"); writes[path] = true; } catch { writes[path] = false; }
123 }
124 let worktreeWrite = false;
125 try { writeFileSync("ok.txt", "ok"); worktreeWrite = true; } catch {}
126 const get = async (url: string, proxy?: string) => {
127 try {
128 const res = await fetch(url, { proxy, signal: AbortSignal.timeout(4000) });
129 return res.ok ? await res.text() : "refused";
130 } catch { return "refused"; }
131 };
132 const proxy = process.env.HTTP_PROXY;
133 const net = {
134 directLoopback: await get("http://127.0.0.1:${outside.port}/"),
135 proxyLoopback: await get("http://127.0.0.1:${outside.port}/", proxy),
136 proxyInternet: await get("http://example.com/", proxy),
137 directInternet: await get("http://192.0.2.1/"),
138 };
139 console.log(JSON.stringify({ readFiles, environLeaks, writes, net, worktreeWrite }));
140 `,
141 );
142
143 const settings = join(dataDir, "srt-settings.json");
144 const real = claudeSettings(policy).sandbox as Record<string, any>;
145 writeFileSync(
146 settings,
147 JSON.stringify({
148 network: { deniedDomains: [], ...real.network },
149 filesystem: {
150 denyRead: [],
151 denyWrite: [],
152 ...real.filesystem,
153 allowWrite: [work, ...(real.filesystem?.allowWrite ?? [])],
154 },
155 }),
156 );
157 const proc = Bun.spawn(
158 [srt, "--settings", settings, "-c", `'${process.execPath}' probe.ts`],
159 { cwd: work, stdin: "ignore", stdout: "pipe", stderr: "pipe" },
160 );
161 const [out, err, code] = await Promise.all([
162 new Response(proc.stdout).text(),
163 new Response(proc.stderr).text(),
164 proc.exited,
165 ]);
166 if (code !== 0) throw new Error(`sandboxed shell exited ${code}: ${err}`);
167 report = JSON.parse(out.trim().split("\n").pop() ?? "{}");
168 }, 60_000);
169
170 afterAll(async () => {
171 holder?.kill();
172 outside?.stop(true);
173 await rm(homeMarker, { force: true });
174 await rm("/etc/forge-redteam-planted", { force: true }).catch(() => {});
175 await rm(dataDir, { recursive: true, force: true });
176 });
177
178 test("the shell can write its worktree (the probe ran as intended)", () => {
179 expect(report.worktreeWrite).toBe(true);
180 });
181
182 test("reads nothing outside the worktree, by path or through /proc/*/root", () => {
183 const leaked = Object.entries(report.readFiles)
184 .filter(([, read]) => read)
185 .map(([path]) => path);
186 expect(leaked).toEqual([]);
187 });
188
189 test("reads no other process's environment (/proc/*/environ, /proc/1/environ)", () => {
190 expect(report.environLeaks).toEqual([]);
191 });
192
193 test("writes nothing outside the worktree, by path or through /proc/*/root", () => {
194 const wrote = Object.entries(report.writes)
195 .filter(([, ok]) => ok)
196 .map(([path]) => path);
197 expect(wrote).toEqual([]);
198 });
199
200 test("reaches no loopback port the policy does not name, direct or by proxy", () => {
201 expect(report.net["directLoopback"]).toBe("refused");
202 expect(report.net["proxyLoopback"]).toBe("refused");
203 });
204
205 test("reaches no host beyond the allowlist, direct or by proxy", () => {
206 expect(report.net["proxyInternet"]).toBe("refused");
207 expect(report.net["directInternet"]).toBe("refused");
208 });
209 },
210 );
test/spawn-cell.test.ts +1
@@ -196,6 +196,7 @@ describe("a child reaching its parent from inside its cell", () => {
196196 expect(spec.reach).toEqual(["https://f.test", parentLocal(3000)]);
197197 expect(cellEnv({ port: 4100, dataDir: "/d/c", url: "https://kid--f.test", parentPort: 3000 })["FORGE_PARENT_LOCAL"]).toBe("http://127.0.0.1:3000");
198198 expect(cellEnv({ port: 4100, dataDir: "/d/c", url: "https://kid--f.test" })["FORGE_PARENT_LOCAL"]).toBeUndefined();
199 expect(cellEnv({ port: 4100, dataDir: "/d/c", url: "https://kid--f.test" })["FORGE_DRAIN_LINGER_S"]).toBe("0");
199200 });
200201
201202 test("the door answers the heartbeat route and nothing else, and ignores who the client claims to be", async () => {

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs