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

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

Parked opened this
Closed without merging

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

  • Closed without merging. Nothing was applied to main.

Review

Sign in to review this change.
4 files changed +234 −67
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.
src/kernel/why_now.ts deleted −37
@@ -1,37 +0,0 @@
1 /**
2 * Retired 2026-11-09: the direction no longer puts the pipeline first —
3 * order is set by the pillars and the captain's ranking — so the gate no
4 * longer asks (`scripts/gate.ts`). A finished, approved pull closed unmerged
5 * on this rule (#10317); nothing reads it now. Kept with its test until the
6 * next sweep of unused modules.
7 *
8 * The direction issue put the pipeline first (#8839), so a new feature
9 * said why it went ahead of it. A feature is a file a pull adds, at any
10 * depth, under an action, a page, an API route, an MCP tool or a terminal
11 * screen; the pull's commits must then say either `Pipeline: #N` (it is
12 * pipeline work) or `Why now: <reason>` (it goes ahead, and why). The check
13 * cannot read the pull description, so the answer lives in a commit
14 * message. It is a nudge, not a proof: it checks a reason was written, not
15 * that it is a good one.
16 */
17
18 import type { Change } from "./pin_edits.ts";
19
20 const FEATURE = /^src\/(actions|web\/pages|http\/api|http\/mcp\/tools|tui)\/.+\.ts$/;
21 const PIPELINE = /^Pipeline:\s*#\d+/im;
22 const WHY_NOW = /^Why now:\s*\S[^\n]{14,}/im;
23
24 /** The files a pull adds that make a new feature. */
25 export function featuresAdded(changes: readonly Change[]): string[] {
26 return changes.filter((c) => c.status.startsWith("A") && FEATURE.test(c.path)).map((c) => c.path);
27 }
28
29 /** One line per problem: none when no feature is added or a commit answers the direction. */
30 export function whyNowProblems(changes: readonly Change[], messages: readonly string[]): string[] {
31 const added = featuresAdded(changes);
32 if (added.length === 0) return [];
33 if (messages.some((m) => PIPELINE.test(m) || WHY_NOW.test(m))) return [];
34 return [
35 `rule "say why it goes ahead of the pipeline": this pull adds ${added.join(", ")} but no commit message has a "Why now: <reason>" line (15+ characters) or a "Pipeline: #N" line`,
36 ];
37 }
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/why-now.test.ts deleted −28
@@ -1,28 +0,0 @@
1 /** Retired 2026-11-09 with the pipeline-first direction: the gate no longer asks (`scripts/gate.ts`); the pure rule keeps its tests until the module goes. */
2 import { expect, test } from "bun:test";
3 import { featuresAdded, whyNowProblems } from "../src/kernel/why_now.ts";
4
5 const add = (path: string) => ({ status: "A", path });
6
7 test("a new action, page or route is a feature; edits, tests and other files are not", () => {
8 expect(featuresAdded([add("src/actions/x.ts"), add("src/web/pages/y.ts"), add("src/http/api/z.ts")])).toHaveLength(3);
9 expect(featuresAdded([add("src/http/mcp/tools/x.ts"), add("src/tui/app/screen.ts"), add("src/http/api/v1/y.ts")])).toHaveLength(3);
10 expect(featuresAdded([{ status: "M", path: "src/actions/x.ts" }, add("test/x.test.ts"), add("src/core/x.ts")])).toEqual([]);
11 });
12
13 test("a feature with no answer to the direction is refused, naming the rule and the file", () => {
14 const [p] = whyNowProblems([add("src/actions/x.ts")], ["Add x\n\nIt does x."]);
15 expect(p).toContain("say why it goes ahead of the pipeline");
16 expect(p).toContain("src/actions/x.ts");
17 });
18
19 test("a Why now line or a Pipeline line answers it; a thin reason does not", () => {
20 const changes = [add("src/actions/x.ts")];
21 expect(whyNowProblems(changes, ["Add x\n\nWhy now: the owner asked for it in #1234 this week"])).toEqual([]);
22 expect(whyNowProblems(changes, ["Add x\n\nPipeline: #6534"])).toEqual([]);
23 expect(whyNowProblems(changes, ["Add x\n\nWhy now: soon"])).toHaveLength(1);
24 });
25
26 test("a pull that adds no feature is never asked", () => {
27 expect(whyNowProblems([add("test/a.test.ts")], ["Fix a"])).toEqual([]);
28 });

↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs