fix(runner): do not confine on Linux when instance state is in a shared temporary directory

Landlock leaves a hidden path's ancestors without create rights, which would
stop confined processes creating files in /tmp. Fall back in auto mode and
refuse by name in required mode.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DottaandClaude Opus 5.5 committed 2026-11-17 02:31:04 -06:00
1 parent 6e8a8110c0
commit 8388d55ca9
3 files changed
+36 -4

No files matched your search

+5
View File
@@ -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
@@ -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();
});
@@ -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<string>();
/**
@@ -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()!;