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

Hosted child forges on a host with Landlock and no bubblewrap are not held by the floor #17133

Merged opened this

Check · Review · Land · Live done — Landed and live in production.

Merged

scout/forge:scout/16963-hosted-child-forges-on-a-host-with-landl → main 0 commits fork 2 threads · 2 unresolved

  • Merged. scout/16963-hosted-child-forges-on-a-host-with-landl was merged into main as a164ff4.

Review

Sign in to review this change.
5 files changed +141 −13
docs/features/landlock-floor.md +6 −4
@@ -28,10 +28,12 @@ holds the work instead, with no root and no namespace.
2828 every sibling along the way to the hidden path instead of `/`; a hidden path's parent cannot be
2929 listed, a link is never granted, and hidden paths are planned by their real name (one that cannot
3030 be resolved fails the plan). No other process's `/proc/<pid>` is readable (its environment and
31 open files are the forge's own); the step's own is. A check can write `/dev/null`. This holds
32 checks only: hosted child forges on a host with Landlock and no bubblewrap are not yet held this
33 way (`hide` and `keep` exist in the plan for it); under bubblewrap they are covered by the
34 mounts and cgroups.
31 open files are the forge's own); the step's own is. A check can write `/dev/null`. A forge hosted on a machine
32 (`FORGE_MACHINES`, `src/spawn/machine_floor.ts`) where no cell can be made is held the same way
33 (#16963): its own directory under the machine's root is its to write and read (with a `.home` and
34 `.tmp` inside it for `HOME` and `TMPDIR`), and the parent's data and everything else under the
35 machine's root, every sibling forge included, are not readable; its tcp stays open, and the log
36 says so. Under bubblewrap, forges in cells are covered by the mounts and cgroups.
3537 - **Never silent.** The kernel's ABI is probed; rights it lacks are dropped and named. UDP is never
3638 covered before ABI 10 and every record says so.
3739 - `FORGE_LANDLOCK=off` declines the floor on purpose.
scripts/spawn-argv-allow.json +13 −7
@@ -56,7 +56,7 @@
5656 {
5757 "file": "src/cli/commands/open.ts",
5858 "kind": "spawn",
59 "code": "[opener, …]",
59 "code": "[opener, \u2026]",

Unrelated rewrite of … to \u2026 across the file. Revert it and keep only the new entry.

6060 "why": "opener is \"open\" or \"xdg-open\" by platform, decided on the line above; the URL is one argument"
6161 },
6262 {
@@ -74,13 +74,13 @@
7474 {
7575 "file": "src/crew/claude.ts",
7676 "kind": "spawn",
77 "code": "[binary, …]",
77 "code": "[binary, \u2026]",
7878 "why": "the operator's configured agent runner binary (FORGE_AGENT_RUNNER, default \"claude\"), asked its version"
7979 },
8080 {
8181 "file": "src/crew/claude_usage.ts",
8282 "kind": "spawn",
83 "code": "[input.binary, …]",
83 "code": "[input.binary, \u2026]",
8484 "why": "the operator's configured agent runner binary, asked its usage"
8585 },
8686 {
@@ -92,7 +92,7 @@
9292 {
9393 "file": "src/crew/plan.ts",
9494 "kind": "spawn",
95 "code": "[options.binary, …]",
95 "code": "[options.binary, \u2026]",
9696 "why": "the operator's configured agent runner binary; every other element is a literal, the prompt one argument"
9797 },
9898 {
@@ -140,19 +140,25 @@
140140 {
141141 "file": "src/tui/screens/external.ts",
142142 "kind": "spawn",
143 "code": "[bin, …]",
143 "code": "[bin, \u2026]",
144144 "why": "bin is the person's own $EDITOR resolved by Bun.which, refused when it holds a space, in their own terminal; the file is one argument"
145145 },
146146 {
147147 "file": "src/tui/term/terminal.ts",
148148 "kind": "spawn",
149 "code": "[opener, …]",
149 "code": "[opener, \u2026]",
150150 "why": "opener is \"open\" or \"xdg-open\" by platform, decided on the line above; the URL is one argument"
151151 },
152152 {
153153 "file": "src/git/read.ts",
154154 "kind": "spawn",
155 "code": "[...this.command, …]",
155 "code": "[...this.command, \u2026]",
156156 "why": "command is a constructor seam whose default is [\"git\", \"cat-file\"]; only tests pass another, and no request text reaches it; the mode is one fixed flag"
157 },
158 {
159 "file": "src/spawn/machines.ts",
160 "kind": "spawn",
161 "code": "argv",
162 "why": "a forge on a machine: this process's bun and the forge's own entry, behind the Landlock helper where no cell can be made (landlockArgv: bun, a fixed helper, a plan, then --); no request text is in it"
157163 }
158164 ]
src/spawn/machine_floor.ts added +25
@@ -0,0 +1,25 @@
1 /**
2 * What a forge run on a machine's root is held by where the host has Landlock and no bubblewrap
3 * (#16963): the plan `landlockPlan` takes, as plain data. The forge's own directory is its to
4 * write and read; the parent's data and every sibling forge's directory (`<root>/children`, the
5 * machine's spawn records and pids) are not readable at all.
6 */
7 import { join } from "node:path";
8 import type { LandlockWant } from "../sandbox/landlock.ts";
9
10 export function machineFloor(root: string, bodyId: string, script: string, dataDir?: string): Pick<LandlockWant, "writable" | "hide" | "keep"> {
11 const own = join(root, "children", bodyId);
12 // The code it runs is read from where it lies, even when that is under a hidden path.
13 const code = join(script, "..", "..");
14 return {
15 writable: [own, "/dev/null"],
16 hide: [...(dataDir === undefined ? [] : [dataDir]), root],
17 keep: [own, code],
18 };
19 }
20
21 /** The directories the floor's forge uses for `HOME` and temp files, inside its own directory. */
22 export const floorHome = (root: string, bodyId: string): { home: string; tmp: string } => ({
23 home: join(root, "children", bodyId, ".home"),
24 tmp: join(root, "children", bodyId, ".tmp"),
25 });
src/spawn/machines.ts +33 −2
@@ -17,6 +17,10 @@ import { cellEnv } from "./cell.ts";
1717 import { BODY_ID } from "./archive.ts";
1818 import { CHILD_NAME } from "./placement.ts";
1919 import { clearRoute } from "./routes.ts";
20 import { landlockArgv, landlockPlan, READS_PARTLY } from "../sandbox/landlock.ts";
21 import { probeLandlock, type LandlockProbe } from "../sandbox/landlock_probe.ts";
22 import { cellsAvailable } from "../apps/contain.ts";
23 import { floorHome, machineFloor } from "./machine_floor.ts";
2024
2125 /** What a machine carries when it beats (#16860): forges awake, memory in MiB, busy cores. */
2226 export interface MachineLoad {
@@ -65,6 +69,18 @@ async function freePort(): Promise<number> {
6569
6670 const PROBE_TIMEOUT_MS = 2_000;
6771
72 /**
73 * What holds a forge started on a machine: where bubblewrap cannot make a cell (`cells`) and the
74 * kernel has Landlock (`landlock`), the floor (`machineFloor`) does; `dataDir` is the parent's,
75 * which that forge may not read. Both are the host's own when unset.
76 */
77 interface MachineHold {
78 readonly dataDir?: string;
79 readonly cells?: () => Promise<{ ok: boolean }>;
80 readonly landlock?: () => Promise<LandlockProbe>;
81 readonly log?: (line: string) => void;
82 }
83
6884 /** `probe` answers whether the forge on `port` is up; tests hand in theirs. */
6985 export function processMachine(
7086 name: string,
@@ -72,6 +88,7 @@ export function processMachine(
7288 launch: Launch = forgeLaunch,
7389 probe: (port: number) => Promise<boolean> = async (port) => (await fetch(`http://127.0.0.1:${port}/healthz`, { signal: AbortSignal.timeout(PROBE_TIMEOUT_MS) }).catch(() => undefined))?.ok === true,
7490 waitMs = 30_000,
91 hold: MachineHold = {},
7592 ): Machine {
7693 const running = new Map<string, { pid: number; exited: Promise<number> }>();
7794 const pidFile = (forge: string): string => join(root, "pids", `${forge}.pid`);
@@ -100,8 +117,22 @@ export function processMachine(
100117 async start(forge, bodyId, url) {
101118 const port = await freePort();
102119 const how = launch({ name: forge, bodyId, root, port, url });
120 let argv = [process.execPath, how.script];
121 let env = how.env;
122 if (!(await (hold.cells ?? cellsAvailable)()).ok) {
123 const held = await (hold.landlock ?? probeLandlock)();
124 if (held.ok) {
125 const where = floorHome(root, bodyId);
126 const want = machineFloor(root, bodyId, how.script, hold.dataDir);
127 mkdirSync(where.home, { recursive: true });
128 mkdirSync(where.tmp, { recursive: true });
129 argv = landlockArgv(landlockPlan(held.abi, want), argv);
130 env = { ...env, HOME: where.home, TMPDIR: where.tmp };
131 (hold.log ?? (() => {}))(`forge ${forge} on machine ${name}: no cell here; held by ${held.note}; ${READS_PARTLY}`);
132 } else (hold.log ?? (() => {}))(`forge ${forge} on machine ${name} runs uncontained: ${held.reason}`);

Resolve const log = hold.log ?? (() => {}) once instead of in both branches. When Landlock is missing the forge runs unconfined, so consider warn level and say so plainly.

133 }
103134 // Its own process group (`detached`), so one signal reaches the tree it grows.
104 const proc = Bun.spawn([process.execPath, how.script], { detached: true, env: how.env, stdin: "ignore", stdout: "ignore", stderr: "ignore" });
135 const proc = Bun.spawn(argv, { detached: true, env, stdin: "ignore", stdout: "ignore", stderr: "ignore" });
105136 running.set(forge, { pid: proc.pid, exited: proc.exited });
106137 mkdirSync(join(root, "pids"), { recursive: true });
107138 writeFileSync(pidFile(forge), String(proc.pid), { mode: 0o600 });
@@ -179,7 +210,7 @@ export function machinesOf(app: App, env: Record<string, string | undefined> = p
179210 const at = part.indexOf("=");
180211 const name = part.slice(0, at).trim();
181212 const root = part.slice(at + 1).trim();
182 if (at > 0 && CHILD_NAME.test(name) && name !== "here" && root.startsWith("/") && !known.has(name)) known.set(name, processMachine(name, root));
213 if (at > 0 && CHILD_NAME.test(name) && name !== "here" && root.startsWith("/") && !known.has(name)) known.set(name, processMachine(name, root, undefined, undefined, undefined, { dataDir: app.config.dataDir, log: (line) => app.log.warn(line) }));
183214 }
184215 return [...known.values()];
185216 }
test/machine-floor.test.ts added +64
@@ -0,0 +1,64 @@
1 import { afterAll, expect, test } from "bun:test";
2 import { existsSync, mkdirSync, mkdtempSync, readFileSync, rmSync, writeFileSync } from "node:fs";
3 import { tmpdir } from "node:os";
4 import { join } from "node:path";
5 import { machineFloor } from "../src/spawn/machine_floor.ts";
6 import { landlockPlan } from "../src/sandbox/landlock.ts";
7 import { probeLandlock } from "../src/sandbox/landlock_probe.ts";
8 import { processMachine } from "../src/spawn/machines.ts";
9
10 const kernel = process.platform === "linux" ? await probeLandlock({}) : { ok: false as const, reason: "not linux" };
11 const tmp = mkdtempSync(join(tmpdir(), "machine-floor-"));
12 afterAll(() => rmSync(tmp, { recursive: true, force: true }));
13
14 test("a forge on a machine keeps its own directory and hides the parent's data and its siblings (#16963)", () => {
15 const f = machineFloor("/m", "c1", "/code/src/main.ts", "/d");
16 expect(f.keep).toEqual(["/m/children/c1", "/code"]);
17 expect(f.hide).toEqual(["/d", "/m"]);
18 expect(f.writable).toEqual(["/m/children/c1", "/dev/null"]);
19 });
20
21 test.skipIf(!kernel.ok)("a started forge reads its own files, not a sibling's nor the parent's, where no cell can be made", async () => {
22 if (!kernel.ok) return;
23 const data = join(tmp, "parent");
24 const root = join(tmp, "m");
25 mkdirSync(data, { recursive: true });
26 mkdirSync(join(root, "children", "other"), { recursive: true });
27 writeFileSync(join(data, "secret"), "parent");
28 writeFileSync(join(root, "children", "other", "secret"), "sibling");
29 const code = join(tmp, "code", "src");
30 mkdirSync(code, { recursive: true });
31 writeFileSync(join(code, "main.ts"), `
32 const { readFileSync, writeFileSync } = require("node:fs");
33 const out = [];
34 for (const p of [${JSON.stringify(join(data, "secret"))}, ${JSON.stringify(join(root, "children", "other", "secret"))}]) { try { readFileSync(p); out.push("read"); } catch { out.push("denied"); } }
35 writeFileSync(process.env.FORGE_APP_DATA + "/report", out.join(","));
36 Bun.serve({ port: Number(process.env.PORT), hostname: "127.0.0.1", fetch: () => new Response("ok") });
37 `);
38 const lines: string[] = [];
39 const m = processMachine(
40 "m",
41 root,
42 ({ bodyId, port, root: r }) => ({ script: join(code, "main.ts"), env: { PATH: process.env["PATH"] ?? "", PORT: String(port), FORGE_APP_DATA: join(r, "children", bodyId) } }),
43 undefined,
44 15_000,
45 { dataDir: data, cells: async () => ({ ok: false }), landlock: async () => kernel, log: (l) => lines.push(l) },
46 );
47 try {
48 expect(await m.start("c1", "c1", "https://c1.test")).toBeGreaterThan(0);
49 const report = join(root, "children", "c1", "report");
50 expect(existsSync(report)).toBe(true);
51 expect(readFileSync(report, "utf8")).toBe("denied,denied");
52 expect(lines.join("\n")).toContain("landlock ABI");
53 } finally {
54 await m.stop("c1");
55 }
56 });
57
58 test("the plan it makes grants no read on the hidden places", () => {
59 const root = join(tmp, "p");
60 mkdirSync(join(root, "children", "c1"), { recursive: true });
61 const plan = landlockPlan(6, machineFloor(root, "c1", join(tmp, "code", "src", "main.ts"), tmp));
62 expect(plan.readRoots).toContain(join(root, "children", "c1"));
63 expect(plan.readRoots).not.toContain(root);
64 });

Search
↑↓ navigate ↵ open ⌘↵ its app esc close

Keyboard shortcuts

Docs