diff --git a/packages/conversation/README.md b/packages/conversation/README.md index ce3c914df..433debc46 100644 --- a/packages/conversation/README.md +++ b/packages/conversation/README.md @@ -328,8 +328,9 @@ can disagree with it. repository's `.bin`, then this process's PATH, as `sessionEnv` builds it), and it looks inside the fit check's sandbox, which shows only `/usr`, `/etc`, the host's `/bin`, `/lib`, `/lib64` and `/sbin`, and this Node's - own file at its path unless that path is under `/tmp` (the sandbox's fresh - `/tmp` covers it). Up to the first `node`, every entry must read the + own file at its path unless that path is under `/tmp` (a session's sandbox + mounts its private `/tmp` over it, and the fit check stays undecided for a + node there). Up to the first `node`, every entry must read the same in the sandbox as on the host: absolute (env reads an empty or relative one against the fit check's directory), already in plain form (a `..` after a link goes where the link points), and not in `/proc` or diff --git a/packages/conversation/src/controller.mjs b/packages/conversation/src/controller.mjs index e1cc36068..a5f0fd060 100644 --- a/packages/conversation/src/controller.mjs +++ b/packages/conversation/src/controller.mjs @@ -174,10 +174,12 @@ const shownDir = (p) => SHOWN_DIRS.some((d) => p === d || p.startsWith(d + "/")) // The sandbox makes its own /proc and /dev, so a path there names something // else inside it than on the host. Its /tmp is another directory too, but one // that starts empty (pi-fit.mjs makes it fresh for each run), so an entry -// there holds no node in the sandbox, and a node the host finds there is one -// the sandbox can't show. That holds for this Node's own file too: pi-fit.mjs -// puts the fresh /tmp in place after it mounts that file, so an engine whose -// node sits under /tmp is hidden in the sandbox. +// there holds no node in the sandbox, and a node the host finds there counts +// as one the sandbox can't show. That holds for this Node's own file too. The +// fit sandbox now mounts that file over its fresh /tmp (pi-fit-tmp's +// mountOrder), but a session's sandbox mounts its private /tmp last and +// covers it, and the fit check stays undecided for a node under /tmp, so an +// engine whose node sits under /tmp still can't run. const OWN_COPY_DIRS = ["/proc", "/dev"]; const underTmp = (p) => p === "/tmp" || p.startsWith("/tmp/"); function shownInFit(p, hops = 0) { diff --git a/packages/harness/src/pi-fit.mjs b/packages/harness/src/pi-fit.mjs index cbe7f9c3f..b28d4fb17 100644 --- a/packages/harness/src/pi-fit.mjs +++ b/packages/harness/src/pi-fit.mjs @@ -125,7 +125,7 @@ import { spawn } from "node:child_process"; import { createHash } from "node:crypto"; import { existsSync, lstatSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, readlinkSync, realpathSync, rmSync, symlinkSync, writeFileSync } from "node:fs"; import { tmpdir } from "node:os"; -import { dirname, join, relative } from "node:path"; +import { dirname, join, posix, relative } from "node:path"; import { fileURLToPath } from "node:url"; import { BWRAP, RUNNER_FILES, SETPRIV, UNSHARE, checkArgv, outerArgv, sessionSetenv } from "../../seat/src/sandbox.mjs"; import { isProviderVariable, sessionEnv } from "../../seat/src/session.mjs"; @@ -351,6 +351,12 @@ export function piFit(found, { levels = [], bwrap = BWRAP, unshare = UNSHARE, no } catch { return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: its files can't be read`, digest: null, undecided: true }); } + // pi-fit-tmp review P3-1: the fit sandbox orders its binds so nothing under /tmp is hidden, but + // a session's sandbox (binds()) mounts its private /tmp last and covers whatever lies under it. + // A pi mounted there, or a node there, would pass the check and then fail every session, so the + // check stays undecided for it, as it was before the fit sandbox's binds were ordered. + const covered = sessionCovers(found.dest ?? found.root) ? "the pi's package" : sessionCovers(node) ? "node" : null; + if (covered) return Promise.resolve({ ok: false, message: `${who(null)} can't be checked: ${covered} sits under the system temp directory, which a session's sandbox covers`, digest: JSON.parse(key)[0], undecided: true }); if (passed.has(key)) return Promise.resolve(passed.get(key)); if (running.has(key)) return share(running.get(key), signal, who, key); const cancel = new AbortController(); @@ -752,6 +758,10 @@ function spawned(argv, { input, timeout, signal }) { // pi-fit-tmp: how a run ended, for a line that must say enough to diagnose: why the check killed // it, else the signal that ended it, else its exit status. Never its output, which can name a host // path. +// A path a session's private /tmp covers: /tmp itself or under it, by its spelling (what the +// session's bind names as its target). +const sessionCovers = (p) => typeof p === "string" && /^\/+tmp(\/|$)/.test(posix.normalize(p)); + function ended(r) { if (r.stopped) return r.stopped; if (typeof r.signal === "string") return `ended by ${r.signal}`; diff --git a/packages/harness/tests/pi-fit.test.mjs b/packages/harness/tests/pi-fit.test.mjs index 14f31d2d7..f2429b0ed 100644 --- a/packages/harness/tests/pi-fit.test.mjs +++ b/packages/harness/tests/pi-fit.test.mjs @@ -1063,6 +1063,15 @@ test("pi-fit-tmp: mountOrder puts a bind before every bind whose target lies und assert.deepEqual(mountOrder([ro("/a/b/c"), ro("/a/b"), ro("/a")]), [ro("/a"), ro("/a/b"), ro("/a/b/c")]); // A bind at / goes before everything under it. assert.deepEqual(mountOrder([ro("/usr"), ro("/r", "/")]), [ro("/r", "/"), ro("/usr")]); + // Targets compare in plain form: a trailing slash or a "." still names the same directory. + assert.deepEqual(mountOrder([ro("/a/x"), ro("/b", "/a/")]), [ro("/b", "/a/"), ro("/a/x")]); + assert.deepEqual(mountOrder([ro("/a/x"), ro("/b", "/a/./")]), [ro("/b", "/a/./"), ro("/a/x")]); + assert.deepEqual(mountOrder([ro("/t/a"), rw("/x", "//t")]), [rw("/x", "//t"), ro("/t/a")]); + // An entry with no target, or one that isn't absolute, stays where it is and moves nothing. + const odd = [ro("/a/x"), { kind: "none" }, ro("rel"), ro("/c")]; + assert.deepEqual(mountOrder(odd), odd); + // The limit, by design: no bind stays hidden under a later one. /c at /a no longer covers /a/x. + assert.deepEqual(mountOrder([ro("/b", "/a"), ro("/a/x"), ro("/c", "/a")]), [ro("/b", "/a"), ro("/c", "/a"), ro("/a/x")]); // In the fit check's own argv the /tmp bind comes before the scratch's. const argv = checkArgv([rw(scratch), rw(`${scratch}/tmp`, "/tmp")], ["/usr/bin/true"], { chdir: scratch }); const at = (src, dest) => argv.findIndex((a, i) => a === "--bind" && argv[i + 1] === src && argv[i + 2] === dest); @@ -1086,6 +1095,34 @@ exec '${BWRAP}' "$@" } }); +// pi-fit-tmp review P3-1: the fit sandbox now shows what lies under /tmp, but a session's sandbox +// mounts its private /tmp last and covers it. A pi mounted under /tmp, or a node there, stays +// undecided, with a line that names no path, and no run starts (the bwrap stand-in would record one). +test("pi-fit-tmp: a pi mounted under /tmp, or a node there, can't be checked, since a session's sandbox covers it", SKIP, async t => { + const s = standIn(t, {}); + const bwrap = join(s.root, "record-bwrap"); + const ran = join(s.root, "ran"); + writeFileSync(bwrap, `#!/bin/sh\necho x >> '${ran}'\nexec '${BWRAP}' "$@"\n`, { mode: 0o755 }); + const found = locate(s.env); + const cases = [ + [{ ...found, dest: "/tmp/mosaic-fit-dest" }, {}, "the pi's package"], + [{ ...found, dest: "//tmp" }, {}, "the pi's package"], + [found, { node: "/tmp/mosaic-fit-node" }, "node"], + ]; + for (const [f, extra, what] of cases) { + const r = await piFit(f, { levels: LEVELS, bwrap, ...extra }); + assert.deepEqual([r.ok, r.undecided], [false, true], r.message); + assert.equal(r.message, `pi (MOSAIC_PI_BIN) can't be checked: ${what} sits under the system temp directory, which a session's sandbox covers`); + assert.match(r.digest, /^[0-9a-f]{64}$/); + noPath(r.message, [...s.paths, bwrap, "/tmp"]); + } + assert.equal(existsSync(ran), false, "a run started"); + // A name that only starts with tmp is not under it, and the check runs as usual. + const r = await piFit({ ...found, dest: "/tmpx/mosaic-fit-dest" }, { levels: LEVELS, bwrap }); + assert.doesNotMatch(r.message ?? "", /system temp directory/); + assert.equal(existsSync(ran), true, "no run started for /tmpx"); +}); + test("pi-unpin S3: the adapter passes --no-mcp only when the module asks for it", SKIP, async t => { const session = { provider: "fixture", model: "m", thinking: "low", toolSocket: "/run/tools.sock" }; const run = async (s) => { diff --git a/packages/seat/src/sandbox.mjs b/packages/seat/src/sandbox.mjs index 4b8a37c0d..4b278b37f 100644 --- a/packages/seat/src/sandbox.mjs +++ b/packages/seat/src/sandbox.mjs @@ -35,7 +35,7 @@ import { spawnSync } from "node:child_process"; import { existsSync, lstatSync, mkdirSync, readlinkSync, realpathSync, statSync } from "node:fs"; -import { dirname, isAbsolute, join, relative, resolve } from "node:path"; +import { dirname, isAbsolute, join, posix, relative, resolve } from "node:path"; import { fileURLToPath } from "node:url"; import { ID_PATTERN } from "../../organization/src/vocabulary.mjs"; @@ -248,10 +248,18 @@ export function checkArgv(list, command, { env = {}, chdir, node = process.execP // pi-fit-tmp: list in an order bwrap can mount without one bind hiding another: each bind moves // ahead of the first bind whose target lies strictly under its own. Binds with no such relation // keep their order, and so do binds with the same target, so a later one still covers an earlier -// one as before. Only the order changes: the same binds, each with its own source and target. +// one at that same target. Only the order changes: the same binds, each with its own source and +// target. Targets compare in plain form ("/a/" and "/a/./" are "/a"); an entry with no target +// stays where it is. Limits, by design: no bind is left hidden under a later one, so a caller that +// counts on a later mount covering what an earlier one put under it (a tmpfs over a directory, +// or [/b at /a, /a/x, /c at /a], where /c no longer covers /a/x) must not use it. Only checkArgv +// does; a session's binds() keep their own order. export function mountOrder(list) { - const target = (b) => b.dest ?? b.src; - const under = (path, dir) => dir === "/" ? path !== "/" : path.startsWith(`${dir}/`); + const target = (b) => { + const t = b.dest ?? b.src; + return typeof t === "string" && t.startsWith("/") ? posix.normalize(t).replace(/(.)\/+$/, "$1") : null; + }; + const under = (path, dir) => path !== null && dir !== null && (dir === "/" ? path !== "/" : path.startsWith(`${dir}/`)); const out = []; for (const b of list) { const at = out.findIndex((o) => under(target(o), target(b)));