fix(conversation): pi-unpin S4 re-check: a first line must end within the 256 bytes the kernel reads and carry no CR, and an absolute node in it must resolve to the engine's own node, so a package's own program named node or a long first line can't make the fit check run another program than the launch (P1); the lookup is run again on the answer's bin and must give the same root, dest, view and pkg, so a hand-built answer can't mount one package for the fit check and launch another (P2); S4g checks each field alone (P3); test S4h (#1873, lane pi-unpin-s4)

This commit is contained in:
jason.woltje committed 2026-11-20 05:12:49 -06:00
1 parent 28a5d38349
commit 3f39ed3f11
3 files changed
+99 -24

No files matched your search

+9 -6
View File
@@ -321,11 +321,14 @@ can disagree with it.
- **Same file, same interpreter.** The fit check runs the bin as a program,
so its first line picks what runs it, while the launch runs the bin's real
file with this Node. A pi whose first line isn't `#!/usr/bin/env node` or
`#!` and an absolute path to a `node` refuses `engine-feature-missing`.
The lookup's answer must also still hold together at each check: its bin
and the view the fit check runs resolve to the same file, inside the package
the lookup found. A package link moved since the lookup, or an answer built
by hand with another bin, refuses `unsealed-engine`.
`#!` and an absolute path that resolves to this Node refuses
`engine-feature-missing`. So does a first line that doesn't end within the
256 bytes the kernel reads, or one with a CR before its newline.
The lookup's answer must also still be the lookup's at each check: the
lookup run again on its bin gives the same `root`, `dest`, `view` and `pkg`,
so the fit check mounts, runs and digests the package the launch runs. A
package link moved since the lookup, or an answer changed by hand, refuses
`unsealed-engine`.
- **Engine environment.** The engine gets `ENGINE_ENV` (`pi-seal.mjs`):
`PATH`, `HOME`, `USER`, `LOGNAME`, `SHELL`, `LANG`, `LC_ALL`,
`LC_CTYPE`, `TZ`, `TERM`, `TMPDIR`, `PI_CODING_AGENT_DIR`, `PI_OFFLINE`,
@@ -622,7 +625,7 @@ no prompt:
| Suite | Covers |
|---|---|
| `claim.test.mjs` | W1–W17, W20, G1–G3, S4a–S4g: the writer claim, crash barriers, the guard, the engine fit, pins and seal, the node-script check and the lookup answer check |
| `claim.test.mjs` | W1–W17, W20, G1–G3, S4a–S4h: the writer claim, crash barriers, the guard, the engine fit, pins and seal, the node-script check and the lookup answer check |
| `races.test.mjs` | H1–H4, H9–H23: takeover, Interrupt and force stop, retries, incarnations |
| `turns.test.mjs` | N1–N25, N24b, N24c: the turn tracker against the fake engine's Pi behaviors; the engine seal, `--no-mcp` and environment |
| `cohort.test.mjs` | K1–K19, R1–R3, R5–R13, E1–E6: scopes, force stop, proofs, recovery, eligibility, the scope's environment, scope release and its retry after a crash, the engine-exit stop (needs a systemd user manager) |
+26 -16
View File
@@ -157,7 +157,8 @@ export function sessionTools(parsed) {
// A first line that runs node: #!/usr/bin/env node, or #! and an absolute path
// whose last part is node, with nothing after it.
const NODE_SHEBANG = /^#![ \t]*(?:\/usr\/bin\/env[ \t]+node|\/[^\s]*\/node)[ \t]*\r?$/;
const NODE_SHEBANG = /^#![ \t]*(?:\/usr\/bin\/env[ \t]+node|(\/[^\s]*\/node))[ \t]*$/;
const ANSWER_KEYS = ["root", "dest", "view", "pkg"];
export class Controller {
constructor(opts = {}) {
@@ -269,10 +270,10 @@ export class Controller {
// The real file of the pi's bin, read now. No answer from the lookup, or a
// bin that no longer resolves, refuses: the lookup's own line names the
// step, never a path. The answer must still hold together: the bin and the
// view the fit check runs resolve to the same file, inside the package the
// lookup found. A stale answer (a link moved since the lookup) or one built
// by hand with another bin refuses unsealed-engine.
// step, never a path. The answer must still be the lookup's: the lookup run
// again on its bin gives the same root, dest, view and pkg. A stale
// answer (a link moved since the lookup) or one changed by hand refuses
// unsealed-engine.
#piFile() {
const pi = this.pi;
if (!pi || typeof pi !== "object" || typeof pi.bin !== "string" || !pi.bin) {
@@ -286,14 +287,11 @@ export class Controller {
} catch {
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label} no longer resolves to a file`);
}
let view = null, pkg = null;
try {
view = typeof pi.view === "string" ? realpathSync(pi.view) : null;
pkg = typeof pi.pkg === "string" ? realpathSync(pi.pkg) : null;
} catch {
view = null;
}
if (view !== real || pkg === null || !real.startsWith(pkg + "/")) {
// The lookup run again on the same bin must give the same answer: what the
// fit check mounts (root at dest), runs (view) and digests (pkg) is then
// the package the launch runs.
const again = locate({ MOSAIC_PI_BIN: pi.bin, ...(typeof process.env.HOME === "string" ? { HOME: process.env.HOME } : {}) });
if (again.bin === null || ANSWER_KEYS.some((k) => (again[k] ?? null) !== (pi[k] ?? null))) {
throw new ControlRefusal(UNSEALED_ENGINE, `${label} no longer matches the lookup's answer; look the pi up again`);
}
return real;
@@ -302,7 +300,7 @@ export class Controller {
// #1873 S4: the launch is this Node running the bin's real file, while the
// fit check runs the bin as a program, so its first line picks what runs it.
// Only a file whose first line runs node (#!/usr/bin/env node, or an
// absolute path to a node) runs the same way in both.
// absolute path to this same node) runs the same way in both.
#nodeScript(file) {
let head = "";
let fd = null;
@@ -315,8 +313,20 @@ export class Controller {
} finally {
if (fd !== null) try { closeSync(fd); } catch {}
}
if (!NODE_SHEBANG.test(head.split("\n")[0])) {
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${this.pi.label ?? "the pi"} isn't a node script, and the engine runs pi with node`);
const label = this.pi.label ?? "the pi";
// The kernel reads at most 256 bytes for the first line; one that doesn't
// end inside them isn't run as a #! line at all.
const nl = head.indexOf("\n");
const m = nl < 0 ? null : NODE_SHEBANG.exec(head.slice(0, nl));
if (!m) throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label} isn't a node script, and the engine runs pi with node`);
// An absolute node must be this one: a file named node that is something
// else would let the fit check run another program than the launch does.
if (m[1] !== undefined) {
let interp = null;
try { interp = realpathSync(m[1]); } catch {}
if (interp === null || interp !== realpathSync(process.execPath)) {
throw new ControlRefusal(ENGINE_FEATURE_MISSING, `${label}'s first line runs another node than the engine's`);
}
}
}
+64 -2
View File
@@ -7,7 +7,7 @@ import { test, after } from "node:test";
import assert from "node:assert/strict";
import { appendFileSync, copyFileSync, linkSync, mkdirSync, statSync, mkdtempSync, readFileSync, readdirSync, realpathSync, rmSync, symlinkSync, unlinkSync, writeFileSync, lstatSync } from "node:fs";
import { homedir, tmpdir } from "node:os";
import { join } from "node:path";
import { dirname, join } from "node:path";
import { spawnSync } from "node:child_process";
import { createHash } from "node:crypto";
import { ALREADY_ACTIVE, ClaimStore, FOREIGN_HOST, UNSAFE_REPLACEMENT, held, machineId } from "../src/claim.mjs";
@@ -763,7 +763,7 @@ test("S4f: a pi whose first line doesn't run node is refused engine-feature-miss
}
});
test("S4g: a lookup answer that no longer holds together refuses unsealed-engine: a package link moved since the lookup, or a hand-built answer with another bin", async () => {
test("S4g: a lookup answer that is no longer the lookup's refuses unsealed-engine: a package link moved since the lookup, a hand-built answer with another bin, or one field changed by hand", async () => {
const fx = fixture();
const good = standInPi(join(fx.base, "pi-good"));
const lacking = standInPi(join(fx.base, "pi-lacking"), { modes: ["text", "json"] });
@@ -798,6 +798,68 @@ test("S4g: a lookup answer that no longer holds together refuses unsealed-engine
// An answer with a bin swapped in by hand.
assert.throws(() => make({ ...good.found, bin: lacking.bin }), (e) => e.code === UNSEALED_ENGINE);
assert.throws(() => make({ bin: good.bin, label: "MOSAIC_PI_BIN" }), (e) => e.code === UNSEALED_ENGINE);
// One field changed by hand at a time: the fit check would mount, run or
// digest another package than the launch runs.
for (const k of ["root", "dest", "view", "pkg"]) {
const value = k === "dest" ? lacking.found.root : lacking.found[k];
assert.throws(() => make({ ...good.found, [k]: value }), (e) => e.code === UNSEALED_ENGINE, k);
}
// The lacking pi's answer with the good package mounted over its path.
assert.throws(() => make({ ...lacking.found, root: good.found.root, dest: lacking.found.root }), (e) => e.code === UNSEALED_ENGINE);
// The answer as the lookup gives it still builds.
await make(piAt(good.bin)).close();
});
test("S4h: a first line the kernel and node read differently refuses engine-feature-missing: an absolute node that isn't the engine's, a first line past 256 bytes, a CR before the newline; a link to the engine's node launches", async () => {
const fx = fixture();
const refused = async (pi, message) => {
const { ctrl, launcher } = controllerFor(fx, { pi });
try {
await assert.rejects(ctrl.start(), (e) => {
assert.equal(e.code, ENGINE_FEATURE_MISSING);
assert.equal(e.message, message);
return true;
});
assert.equal(launcher.launches.length, 0);
} finally {
await ctrl.close().catch(() => {});
}
};
const withFirstLine = (name, line) => {
const pi = standInPi(join(fx.base, name));
const text = readFileSync(pi.cli, "utf8");
writeFileSync(pi.cli, line + text.slice(text.indexOf("\n")));
return pi;
};
// A program named node in the package that runs another program: here the
// real node, so the fit check passes it.
const shim = standInPi(join(fx.base, "pi-shim"));
const fakeNode = join(dirname(shim.cli), "node");
writeFileSync(fakeNode, `#!/bin/sh\nexec ${process.execPath} "$@"\n`, { mode: 0o755 });
const shimText = readFileSync(shim.cli, "utf8");
writeFileSync(shim.cli, `#!${fakeNode}` + shimText.slice(shimText.indexOf("\n")));
const fit = await piFit(shim.found, { levels: levels() });
assert.equal(fit.ok, true, fit.message);
await refused(shim.found, "MOSAIC_PI_BIN's first line runs another node than the engine's");
// The kernel reads 256 bytes for the first line; this one ends past them.
const long = withFirstLine("pi-long", "#!/usr/bin/env node" + " ".repeat(260));
await refused(long.found, "MOSAIC_PI_BIN isn't a node script, and the engine runs pi with node");
// The kernel looks for a program named with the CR.
const cr = withFirstLine("pi-cr", "#!/usr/bin/env node\r");
await refused(cr.found, "MOSAIC_PI_BIN isn't a node script, and the engine runs pi with node");
// A link to the engine's own node is the same file.
const linked = standInPi(join(fx.base, "pi-linked"));
const nodeLink = join(dirname(linked.cli), "node");
symlinkSync(process.execPath, nodeLink);
const linkedText = readFileSync(linked.cli, "utf8");
writeFileSync(linked.cli, `#!${nodeLink}` + linkedText.slice(linkedText.indexOf("\n")));
const h = await started({ fx, pi: linked.found });
try {
assert.equal(h.launcher.launches.length, 1);
assert.equal(h.launcher.launches[0].args[0], realpathSync(linked.cli));
} finally {
await h.close();
}
});
// ---- W11, W16, W17 ----------------------------------------------------------