diff --git a/tests/system/ci-runner/ci-artifact-contract.test.ts b/tests/system/ci-runner/ci-artifact-contract.test.ts index 09611290..ec92a221 100644 --- a/tests/system/ci-runner/ci-artifact-contract.test.ts +++ b/tests/system/ci-runner/ci-artifact-contract.test.ts @@ -42,6 +42,11 @@ import { providerVerificationArtifactSchema, } from "../../../scripts/lib/provider-evidence.ts"; import { localEvidenceAssessmentArtifactSchema } from "../../../scripts/contracts/release-artifacts.ts"; +import { + PROVIDER_PROCESS_TRACE_VARIABLE, + type ProviderProcessRole, + type ProviderProcessTraceEntry, +} from "../../../scripts/lib/provider-process-trace.ts"; import { readProviderTrust } from "../../../scripts/lib/provider-trust.ts"; import { createReleaseCandidateManifest, @@ -756,23 +761,26 @@ describe("candidate archive and provider upload boundaries", () => { `process.stdout.write(${JSON.stringify(credential)});`, `process.stderr.write(${JSON.stringify(credential)});`, providerV2WriterSource(), + // No launch stage can record the provider itself, so it checks its own + // scope membership; exit 8 fails the run below. + "if (!readFileSync('/proc/self/cgroup', 'utf8').includes('/ca-provider-vulnerability-')) process.exit(8);", "setTimeout(() => process.exit(0), 1500);", ].join("\n")); const command = `node ${JSON.stringify(providerScript)}`; + // Outside the workspace, so the sandboxed provider cannot read it. + const tracePath = path.join(await temporaryRoot("provider-process-trace-"), "trace.jsonl"); const execution = startProviderSupervisor(fixture, { command, sealedPath: path.join(fixture.root, "provider-evidence/vulnerability-report.json"), environment: { PROVIDER_SUPERVISOR_TIMEOUT_MS: "4000", + [PROVIDER_PROCESS_TRACE_VARIABLE]: tracePath, VULNERABILITY_PROVIDER_SECRET: credential, }, }); const completionStarted = Date.now(); - // Start recording before anything is asserted: the provider's whole life is - // shorter than one `systemctl show`, so the tree has to be sampled, not - // sampled once at whatever moment the assertions happen to arrive. - const tree = recordProviderProcessTree(execution.child.pid!); - const unit = await waitForProviderUnit("vulnerability", execution.child.pid); + const supervisorPid = execution.child.pid!; + const unit = await waitForProviderUnit("vulnerability", supervisorPid); const properties = showProviderUnit(unit); expect(properties).toMatchObject({ ActiveState: "active", @@ -792,47 +800,55 @@ describe("candidate archive and provider upload boundaries", () => { expect(readProviderUnitMetadata(unit)).not.toMatch(new RegExp(`${command}|${credential}`, "u")); const reportIdentity = await lstat(fixture.reportPath); const result = await execution.completion; - tree.stop(); expect(result.code, result.stderr).toBe(0); expect(result.stdout).not.toContain(credential); expect(result.stderr).not.toContain(credential); - // Everything below reads the recording of the whole run rather than a live - // snapshot. The sandbox exists for a few hundred milliseconds; asserting - // while it runs meant racing it, and the assertions are about what the run - // contained, not about what a particular instant looked like. - const cgroupPids = tree.cgroupPids(); - const processArguments = tree.cgroupArguments(); - const directChildPids = tree.directChildPids(); - const directChildArguments = tree.directChildArguments(); - // The supervisor's argv comes from the recording too: once `completion` - // resolves Node has reaped it, so a lookup by PID here can only fail. - const supervisorArguments = tree.supervisorArguments(); - expect(supervisorArguments.join("\n"), "supervisor never observed").toContain("run-and-validate-provider.ts"); - expect(supervisorArguments.join("\n")).not.toMatch(new RegExp(`${command}|${credential}`, "u")); - const observedArguments = [ - ...supervisorArguments, - ...directChildArguments, - ...processArguments, - ]; - expect(observedArguments.join("\n")).not.toContain(credential); - expect(directChildArguments.filter((arguments_) => arguments_.includes("/usr/bin/systemd-run"))).toHaveLength(1); - expect(directChildArguments.filter((arguments_) => arguments_.includes("provider-raw-guardian.ts"))).toHaveLength(1); - expect(processArguments.filter((arguments_) => arguments_.includes("provider-scope-wrapper.ts"))).toHaveLength(1); - expect( - processArguments.filter((arguments_) => /\/usr\/bin\/(?:bwrap|prlimit)/u.test(arguments_)).length, - `sandbox never observed in the scope; recorded: ${processArguments.join(" | ")}`, - ).toBeGreaterThan(0); - const infrastructureArguments = [...directChildArguments, ...processArguments].filter((arguments_) => - /provider-(?:scope-wrapper|raw-guardian)|systemd-run|bwrap|prlimit/u.test(arguments_), - ); - expect(infrastructureArguments.join("\n")).not.toContain(command); - expect(processArguments.filter((arguments_) => arguments_.includes(providerScript))).toHaveLength(1); + // Which processes ran, with which argv, comes from the launch stages + // themselves (scripts/lib/provider-process-trace.ts). Sampling /proc lost + // a race it could not win: systemd-run execs into the wrapper on the same + // PID, and a fresh child shows its parent's argv until it execs (11-01). + const traceText = await readFile(tracePath, "utf8"); + // The trace is one more place argv lands; it must not become the leak. + expect(traceText).not.toContain(credential); + const trace = traceText.trim().split("\n") + .map((line) => JSON.parse(line) as ProviderProcessTraceEntry); + const only = (role: ProviderProcessRole): ProviderProcessTraceEntry => { + const entries = trace.filter((entry) => entry.role === role); + expect(entries, `${role} records: ${traceText}`).toHaveLength(1); + return entries[0]!; + }; + const supervisor = only("supervisor"); + const systemdRun = only("systemd-run"); + const guardian = only("guardian"); + const wrapper = only("scope-wrapper"); + const sandbox = only("bwrap"); + expect(supervisor.pid).toBe(supervisorPid); + expect(supervisor.argv.join(" ")).toContain("run-and-validate-provider.ts"); + // The supervisor's direct children: one systemd-run, one guardian. + expect(systemdRun.ppid).toBe(supervisorPid); + expect(systemdRun.argv[0]).toBe("/usr/bin/systemd-run"); + expect(guardian.ppid).toBe(supervisorPid); + expect(guardian.argv.join(" ")).toContain("provider-raw-guardian.ts"); + // `systemd-run --scope` moves itself into the unit and execs the wrapper, + // so the wrapper is that same PID, observed from inside the scope. + expect(wrapper.pid).toBe(systemdRun.pid); + expect(wrapper.cgroup).toBe(`0::${properties.ControlGroup}`); + expect(wrapper.argv.join(" ")).toContain("provider-scope-wrapper.ts"); + // Forked by the in-scope wrapper, so the sandbox inherits the scope. + expect(sandbox.ppid).toBe(wrapper.pid); + expect(sandbox.argv[0]).toBe("/usr/bin/bwrap"); + expect(sandbox.argv).toContain("/usr/bin/prlimit"); + // Every recorded argv is infrastructure: the command and the credential + // reach the sandbox through the args file's environment, never argv. + const recordedArguments = trace.flatMap((entry) => entry.argv).join("\n"); + expect(recordedArguments).not.toContain(command); + expect(recordedArguments).not.toContain(providerScript); + expect(recordedArguments).not.toContain(credential); expect(Date.now() - completionStarted).toBeLessThan(4_000); await expectProviderUnitGone(unit); await expect(lstat(cgroupRoot)).rejects.toMatchObject({ code: "ENOENT" }); - expect(cgroupPids.every((pid) => !processExists(pid))).toBe(true); - expect(directChildPids.every((pid) => !processExists(pid))).toBe(true); + expect(trace.every((entry) => !processExists(entry.pid))).toBe(true); await expect(lstat(fixture.reportPath)).rejects.toMatchObject({ code: "ENOENT" }); await expect(findProviderRawReferences(fixture.reportPath, reportIdentity)).resolves.toEqual([]); }, PROCESS_HEAVY_TIMEOUT_MS); @@ -1882,7 +1898,7 @@ function showProcessArguments(pid: number | undefined): string { * Spawning `ps` per pid costs milliseconds each, and the provider sandbox now * completes a whole run in well under a second — the observation was losing a * race against the thing it was observing. Returns null for a process that is - * already gone, which the sampler treats as "nothing more to record". + * already gone. */ function readProcessArguments(pid: number): string | null { try { @@ -1896,89 +1912,6 @@ function readProcessArguments(pid: number): string | null { } } -/** - * Records the provider's process tree for the whole life of the run. - * - * The assertions below are about what the sandbox looked like while it was - * running, and a single snapshot taken afterwards can only ever be a guess at - * that. Sampling from the moment the supervisor starts turns "did we look at - * the right instant?" into "what did this run actually contain?". - */ -function recordProviderProcessTree(supervisorPid: number) { - // Every distinct value, not just the first: a process can rewrite its own - // argv (`process.title`) mid-run, and the supervisor is the process whose - // argv a later lookup can no longer see. - const supervisorArguments = new Set(); - const directChildren = new Map(); - const cgroupMembers = new Map(); - let stopped = false; - - const remember = (into: Map, pid: number): void => { - if (into.has(pid)) return; - const argv = readProcessArguments(pid); - if (argv !== null && argv.length > 0) into.set(pid, argv); - }; - const childrenOf = (pid: number): number[] => { - try { - return readFileSync(`/proc/${pid}/task/${pid}/children`, "utf8") - .trim() - .split(/\s+/u) - .filter(Boolean) - .map(Number); - } catch { - return []; - } - }; - /** - * Scope membership is read from the process itself rather than from - * `systemctl show`. Waiting for the unit to be described before watching its - * cgroup meant bubblewrap had usually already exited by the time the first - * sample was taken — the recording missed exactly the process the assertions - * are about. - */ - const inProviderScope = (pid: number): boolean => { - try { - return readFileSync(`/proc/${pid}/cgroup`, "utf8").includes("ca-provider-"); - } catch { - return false; - } - }; - const sample = (): void => { - if (stopped) return; - const own = readProcessArguments(supervisorPid); - if (own) supervisorArguments.add(own); - const direct = childrenOf(supervisorPid); - for (const pid of direct) remember(directChildren, pid); - // Walk the whole subtree: the scope wrapper, bubblewrap and the provider - // itself sit below systemd-run, not beside it. - const pending = [...direct]; - const seen = new Set(direct); - while (pending.length > 0 && seen.size < 512) { - const pid = pending.pop()!; - if (inProviderScope(pid)) remember(cgroupMembers, pid); - for (const child of childrenOf(pid)) { - if (seen.has(child)) continue; - seen.add(child); - pending.push(child); - } - } - }; - const timer = setInterval(sample, 5); - timer.unref(); - sample(); - return Object.freeze({ - stop() { - stopped = true; - clearInterval(timer); - }, - supervisorArguments: () => [...supervisorArguments], - directChildPids: () => [...directChildren.keys()], - directChildArguments: () => [...directChildren.values()], - cgroupPids: () => [...cgroupMembers.keys()], - cgroupArguments: () => [...cgroupMembers.values()], - }); -} - async function startLoopbackCanary(root: string): Promise