diff --git a/doc/DEPLOYMENT-MODES.md b/doc/DEPLOYMENT-MODES.md index 136178ba868..5cd6f67d0c6 100644 --- a/doc/DEPLOYMENT-MODES.md +++ b/doc/DEPLOYMENT-MODES.md @@ -146,6 +146,11 @@ or run set-user-ID programs such as `ps`; use `pgrep` instead. recovery does not retry it. - `off`: never confine. +On Linux, a hidden path's parent directories accept no new entries inside the +scope. A server whose instance state lies in a shared temporary directory +(`/tmp`, `/var/tmp` or the OS temporary directory) therefore does not confine +sessions on Linux. `auto` logs a warning once, and `required` refuses. + Other providers start their own operating-system sandboxes, which cannot nest inside this scope. They stay unconfined until they are qualified. Sessions that were already running when the server upgraded keep running unconfined until diff --git a/packages/paperclip-runner/src/control-plane/runner-confinement.test.ts b/packages/paperclip-runner/src/control-plane/runner-confinement.test.ts index e23930fb8d5..510212e8a2e 100644 --- a/packages/paperclip-runner/src/control-plane/runner-confinement.test.ts +++ b/packages/paperclip-runner/src/control-plane/runner-confinement.test.ts @@ -1,7 +1,6 @@ import { execFileSync } from "node:child_process"; import { chmodSync, mkdirSync, mkdtempSync, readFileSync, realpathSync, rmSync, writeFileSync } from "node:fs"; import { createServer, type Server } from "node:net"; -import { tmpdir } from "node:os"; import { join } from "node:path"; import { afterEach, beforeEach, describe, expect, it } from "vitest"; import { spawnRunner, waitForProcess } from "./durable-prp-control-plane.js"; @@ -9,6 +8,7 @@ import { RUNNER_CONFINEMENT_UNAVAILABLE, confineRunnerLaunch, instanceSessionStatePaths, + landlockTemporaryConflict, resetRunnerConfinementAvailability, runnerConfinementScope, runnerConfinementUnavailableReason, @@ -23,7 +23,8 @@ const OPENCODE_LAUNCH_PROFILE = { let root: string; beforeEach(() => { - root = realpathSync(mkdtempSync(join(tmpdir(), "runner-confinement-"))); + // Outside the shared temporary directory: Linux does not confine instance state there. + root = realpathSync(mkdtempSync(join(process.cwd(), ".runner-confinement-test-"))); resetRunnerConfinementAvailability(); }); afterEach(() => rmSync(root, { recursive: true, force: true })); @@ -103,6 +104,16 @@ describe("confineRunnerLaunch", () => { .toThrow(expect.objectContaining({ code: RUNNER_CONFINEMENT_UNAVAILABLE })); }); + it("does not confine on Linux when instance state lies in a shared temporary directory", () => { + const launch = (mode: "auto" | "required") => confineRunnerLaunch({ + policy: { mode, instanceRoot: join(root, "instance") }, command: "/bin/x", args: [], environment: {}, + platform: "linux", sharedTemporaryDirectories: [root], + }); + expect(launch("auto")).toBeNull(); + expect(() => launch("required")).toThrow(expect.objectContaining({ code: RUNNER_CONFINEMENT_UNAVAILABLE })); + expect(landlockTemporaryConflict("darwin", runnerConfinementScope({ mode: "auto", instanceRoot: join(root, "i") }, [], {}), [root])).toBeNull(); + }); + it("falls back to an unconfined launch in auto mode when unavailable", () => { expect(confineRunnerLaunch({ policy: { mode: "auto", instanceRoot: root }, command: "/bin/x", args: [], environment: {}, platform: "win32" })).toBeNull(); }); diff --git a/packages/paperclip-runner/src/control-plane/runner-confinement.ts b/packages/paperclip-runner/src/control-plane/runner-confinement.ts index 1776e0efa06..43c38f90b47 100644 --- a/packages/paperclip-runner/src/control-plane/runner-confinement.ts +++ b/packages/paperclip-runner/src/control-plane/runner-confinement.ts @@ -385,6 +385,19 @@ export function resetRunnerConfinementAvailability() { availability.clear(); } +/** + * Landlock hides a path by leaving its ancestors ungranted, and an ungranted + * directory accepts no new entries. Instance state inside a shared temporary + * directory would stop every confined process creating files there (mktemp, + * runnerd's activity pipe), so such a host does not confine on Linux. + */ +export function landlockTemporaryConflict(platform: NodeJS.Platform, scope: RunnerConfinementScope, sharedTemporaryDirectories?: readonly string[]) { + if (platform !== "linux") return null; + const shared = [...new Set((sharedTemporaryDirectories ?? [tmpdir(), "/tmp", "/var/tmp"]).map(canonicalPath))]; + const conflict = scope.hidden.map(canonicalPath).find((hidden) => shared.some((temporary) => hidden !== temporary && within(hidden, temporary))); + return conflict ? "instance state lies in a shared temporary directory" : null; +} + const warned = new Set(); /** @@ -398,11 +411,15 @@ export function confineRunnerLaunch(input: { args: readonly string[]; environment: NodeJS.ProcessEnv; platform?: NodeJS.Platform; + /** Test seam: the shared temporary directories (default: the OS temporary directory, /tmp and /var/tmp). */ + sharedTemporaryDirectories?: readonly string[]; }): { command: string; args: string[]; scope: RunnerConfinementScope } | null { const { policy } = input; if (policy.mode === "off") return null; const platform = input.platform ?? process.platform; - const reason = runnerConfinementUnavailableReason(platform); + const scope = runnerConfinementScope(policy, input.args, input.environment); + const reason = landlockTemporaryConflict(platform, scope, input.sharedTemporaryDirectories) + ?? runnerConfinementUnavailableReason(platform); if (reason !== null) { if (policy.mode === "required") throw new RunnerConfinementUnavailableError(reason); if (!warned.has(reason)) { @@ -411,7 +428,6 @@ export function confineRunnerLaunch(input: { } return null; } - const scope = runnerConfinementScope(policy, input.args, input.environment); prepareRunnerConfinementScope(scope); if (platform === "darwin") return { ...seatbeltInvocation(scope, input.command, input.args), scope }; const python = pythonExecutable()!;