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
Check · Review · Land · Live done — Landed and live in production.
scout/forge:scout/16963-hosted-child-forges-on-a-host-with-landl → main 0 commits fork 2 threads · 2 unresolved
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. | ||
| 28 | 28 | every sibling along the way to the hidden path instead of `/`; a hidden path's parent cannot be |
| 29 | 29 | listed, a link is never granted, and hidden paths are planned by their real name (one that cannot |
| 30 | 30 | 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. | |
| 35 | 37 | - **Never silent.** The kernel's ABI is probed; rights it lacks are dropped and named. UDP is never |
| 36 | 38 | covered before ABI 10 and every record says so. |
| 37 | 39 | - `FORGE_LANDLOCK=off` declines the floor on purpose. |
scripts/spawn-argv-allow.json +13 −7
| @@ -56,7 +56,7 @@ | ||
| 56 | 56 | { |
| 57 | 57 | "file": "src/cli/commands/open.ts", |
| 58 | 58 | "kind": "spawn", |
| 59 | "code": "[opener, …]", | |
| 59 | "code": "[opener, \u2026]", | |
| 60 | 60 | "why": "opener is \"open\" or \"xdg-open\" by platform, decided on the line above; the URL is one argument" |
| 61 | 61 | }, |
| 62 | 62 | { |
| @@ -74,13 +74,13 @@ | ||
| 74 | 74 | { |
| 75 | 75 | "file": "src/crew/claude.ts", |
| 76 | 76 | "kind": "spawn", |
| 77 | "code": "[binary, …]", | |
| 77 | "code": "[binary, \u2026]", | |
| 78 | 78 | "why": "the operator's configured agent runner binary (FORGE_AGENT_RUNNER, default \"claude\"), asked its version" |
| 79 | 79 | }, |
| 80 | 80 | { |
| 81 | 81 | "file": "src/crew/claude_usage.ts", |
| 82 | 82 | "kind": "spawn", |
| 83 | "code": "[input.binary, …]", | |
| 83 | "code": "[input.binary, \u2026]", | |
| 84 | 84 | "why": "the operator's configured agent runner binary, asked its usage" |
| 85 | 85 | }, |
| 86 | 86 | { |
| @@ -92,7 +92,7 @@ | ||
| 92 | 92 | { |
| 93 | 93 | "file": "src/crew/plan.ts", |
| 94 | 94 | "kind": "spawn", |
| 95 | "code": "[options.binary, …]", | |
| 95 | "code": "[options.binary, \u2026]", | |
| 96 | 96 | "why": "the operator's configured agent runner binary; every other element is a literal, the prompt one argument" |
| 97 | 97 | }, |
| 98 | 98 | { |
| @@ -140,19 +140,25 @@ | ||
| 140 | 140 | { |
| 141 | 141 | "file": "src/tui/screens/external.ts", |
| 142 | 142 | "kind": "spawn", |
| 143 | "code": "[bin, …]", | |
| 143 | "code": "[bin, \u2026]", | |
| 144 | 144 | "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" |
| 145 | 145 | }, |
| 146 | 146 | { |
| 147 | 147 | "file": "src/tui/term/terminal.ts", |
| 148 | 148 | "kind": "spawn", |
| 149 | "code": "[opener, …]", | |
| 149 | "code": "[opener, \u2026]", | |
| 150 | 150 | "why": "opener is \"open\" or \"xdg-open\" by platform, decided on the line above; the URL is one argument" |
| 151 | 151 | }, |
| 152 | 152 | { |
| 153 | 153 | "file": "src/git/read.ts", |
| 154 | 154 | "kind": "spawn", |
| 155 | "code": "[...this.command, …]", | |
| 155 | "code": "[...this.command, \u2026]", | |
| 156 | 156 | "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" | |
| 157 | 163 | } |
| 158 | 164 | ] |
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"; | ||
| 17 | 17 | import { BODY_ID } from "./archive.ts"; |
| 18 | 18 | import { CHILD_NAME } from "./placement.ts"; |
| 19 | 19 | 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"; | |
| 20 | 24 | |
| 21 | 25 | /** What a machine carries when it beats (#16860): forges awake, memory in MiB, busy cores. */ |
| 22 | 26 | export interface MachineLoad { |
| @@ -65,6 +69,18 @@ async function freePort(): Promise<number> { | ||
| 65 | 69 | |
| 66 | 70 | const PROBE_TIMEOUT_MS = 2_000; |
| 67 | 71 | |
| 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 | ||
| 68 | 84 | /** `probe` answers whether the forge on `port` is up; tests hand in theirs. */ |
| 69 | 85 | export function processMachine( |
| 70 | 86 | name: string, |
| @@ -72,6 +88,7 @@ export function processMachine( | ||
| 72 | 88 | launch: Launch = forgeLaunch, |
| 73 | 89 | 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, |
| 74 | 90 | waitMs = 30_000, |
| 91 | hold: MachineHold = {}, | |
| 75 | 92 | ): Machine { |
| 76 | 93 | const running = new Map<string, { pid: number; exited: Promise<number> }>(); |
| 77 | 94 | const pidFile = (forge: string): string => join(root, "pids", `${forge}.pid`); |
| @@ -100,8 +117,22 @@ export function processMachine( | ||
| 100 | 117 | async start(forge, bodyId, url) { |
| 101 | 118 | const port = await freePort(); |
| 102 | 119 | 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}`); | |
| 133 | } | |
| 103 | 134 | // 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" }); | |
| 105 | 136 | running.set(forge, { pid: proc.pid, exited: proc.exited }); |
| 106 | 137 | mkdirSync(join(root, "pids"), { recursive: true }); |
| 107 | 138 | writeFileSync(pidFile(forge), String(proc.pid), { mode: 0o600 }); |
| @@ -179,7 +210,7 @@ export function machinesOf(app: App, env: Record<string, string | undefined> = p | ||
| 179 | 210 | const at = part.indexOf("="); |
| 180 | 211 | const name = part.slice(0, at).trim(); |
| 181 | 212 | 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) })); | |
| 183 | 214 | } |
| 184 | 215 | return [...known.values()]; |
| 185 | 216 | } |
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 | }); |
new above
offline
Unrelated rewrite of
…to\u2026across the file. Revert it and keep only the new entry.