문제: FE-GATE-013의 provider sandbox는 bwrap을 --unshare-pid --unshare-ipc --unshare-uts --unshare-net으로 띄운다. bwrap이 setuid가 아니므로 비특권 user namespace가 있어야 한다. 사전 점검 (scripts/check-system-test-prerequisites.ts)은 실행 파일·cgroup·systemd bus만 보고 bwrap을 한 번도 실행하지 않는다. 그래서 이 제한이 있는 호스트 (Ubuntu 24.04, kernel.apparmor_restrict_unprivileged_userns=1)에서 PASS를 낸 뒤 provider 테스트 114건 중 18건이 "bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted" 또는 그 provider를 기다린 시간 초과로 실패한다. 이 조건은 runner 요구 목록, gate 정의, workflow 어디에도 없고 운영 원장의 일회성 메모에만 있다. 고른 방법: - 사전 점검이 provider와 같은 namespace 옵션으로 bwrap에서 /bin/true를 한 번 띄운다. 실패하면 "this host does not allow unprivileged user and network namespaces", bwrap stderr(최대 4줄), user namespace sysctl 값, 고치는 방법 셋(AppArmor profile, sysctl, 다른 runner)을 적고 exit 1. 기존 검사(실행 파일 3개, cgroup v2, systemd bus)는 그대로 둔다. - 옵션 목록을 scripts/lib/provider-sandbox-namespaces.ts로 옮기고 run-and-validate-provider.ts와 사전 점검이 같은 상수를 쓴다. 둘이 갈라지면 점검이 다른 것을 시험하게 된다. - 점검을 findSystemTestPrerequisiteFailures(host)로 내보내고 access· readFile·spawn을 주입받는다. CLI 동작(출력·exit 코드)은 같다. - config/ci/gates.json의 FE-GATE-013에 hostRequirements 두 줄을 둔다. gate schema에 선택 필드를 더하고, generator가 그 gate를 도는 job의 runs-on 바로 위에 "# FE-GATE-013 runner requirement: ..." 주석으로 그린다. runner를 고르는 사람이 보는 자리다. 줄바꿈은 기존 nonEmpty 규칙이 막으므로 주석 밖으로 나갈 수 없다. 이 필드는 gate의 명령·증거가 아니라 runner 설명이라 기준선 gate shape 해시에는 넣지 않았다(기준선 재기록 불필요). - 요구 목록 세 곳(docs/operations/ci-quality-gates.md, docs/security/supply-chain.md, docs/testing/taxonomy.md)과 README merge 게이트 절에 조건을 적었다. ci-quality-gates.md에 AppArmor profile 예시를 두었다. 버린 대안: - namespace가 없으면 provider 테스트를 skip: 거짓 초록이다. - FE-GATE-013을 release 단계로 옮김: PR마다 sandbox 검사를 잃는다. - probe를 systemd-run --user --scope 안에서 돌림: 리뷰 11-R10에서 scope 안과 밖의 결과가 같았다. 직접 실행이 더 단순하다. - workflow에 요구 사항을 runs-on label로 표현: runner label 체계가 아직 없다. 주석이 지금 가능한 가장 작은 표시다. 위험·한계: - probe는 bwrap에 --ro-bind / /만 준다. --size 같은 provider의 다른 옵션 지원은 여기서 보지 않는다(기존과 같다). probe가 다른 이유로 실패해도 namespace 문구가 나오지만 bwrap stderr를 같이 적는다. - 호스트를 고칠지는 호스트 소유자가 정한다. 이 커밋은 조건을 적고 알리기만 한다. 이 desktop에서 FE-GATE-013은 여전히 실패하고, 이제 첫 단계에서 이유와 함께 실패한다. - gate schema에 선택 필드 hostRequirements가 생겼다. 기존 gates.json과 제품 overlay는 그대로 통과한다. 검증: desktop batch에서 확인 - node scripts/run-vitest.ts run tests/unit/system-test-prerequisites.test.ts tests/unit/ci-workflow-generation.test.ts - corepack pnpm check:ci, check:types, lint, verify:documentation - desktop에서 node scripts/check-system-test-prerequisites.ts가 exit 1과 namespace 문구를 내는지, FE-GATE-013이 그 단계에서 멈추는지 Refs: 1105/12-01, 1105/11-02
664 lines
25 KiB
TypeScript
664 lines
25 KiB
TypeScript
import { spawn } from "node:child_process";
|
|
import { randomBytes } from "node:crypto";
|
|
import { constants } from "node:fs";
|
|
import { access, appendFile, lstat, stat } from "node:fs/promises";
|
|
import path from "node:path";
|
|
|
|
import {
|
|
provenanceProviderAttestationSchema,
|
|
vulnerabilityProviderReportSchema,
|
|
} from "./lib/provider-evidence.ts";
|
|
import { readBoundedRegularFile } from "./lib/ci-artifact-validator.ts";
|
|
import { readProviderTrust } from "./lib/provider-trust.ts";
|
|
import { superviseProviderEvidence } from "./lib/provider-supervisor.ts";
|
|
import { serializeValidatedJsonArtifact } from "./lib/validated-json-artifact.ts";
|
|
import {
|
|
assertSafePublishLeaf,
|
|
ensureSafePublishDirectory,
|
|
} from "./lib/ci-gate-log.ts";
|
|
import {
|
|
encodeProviderBwrapInput,
|
|
encodeProviderScopeFrame,
|
|
formatProviderCgroupUnitName,
|
|
systemctlKillProviderArguments,
|
|
systemdRunProviderArguments,
|
|
} from "./lib/provider-cgroup.ts";
|
|
import {
|
|
assertProviderGuardianLeasePaths,
|
|
createProviderScopeGuardianLatch,
|
|
startProviderGuardian,
|
|
type ProviderGuardianLease,
|
|
} from "./lib/provider-guardian-client.ts";
|
|
import { createProviderOutputLimiter } from "./lib/provider-output-limiter.ts";
|
|
import { PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS } from "./lib/provider-sandbox-namespaces.ts";
|
|
import {
|
|
providerProcessTraceEnvironment,
|
|
traceOwnProviderProcess,
|
|
traceSpawnedProviderProcess,
|
|
} from "./lib/provider-process-trace.ts";
|
|
|
|
const kind = process.argv[process.argv.indexOf("--kind") + 1];
|
|
const PROVIDER_TMP_BYTES = 16_777_216;
|
|
const PROVIDER_MASK_BYTES = 1_048_576;
|
|
const PROVIDER_MAX_OUTPUT_BYTES = 1_048_576;
|
|
const DEFAULT_PROVIDER_CPU_SECONDS = 1_200;
|
|
const DEFAULT_PROVIDER_TIMEOUT_MS = 30 * 60 * 1_000;
|
|
const PROVIDER_POSTPROCESS_TIMEOUT_MS = 10 * 60 * 1_000;
|
|
const PROVIDER_REAP_TIMEOUT_MS = 5_000;
|
|
const PROVIDER_CONTROL_TIMEOUT_MS = 5_000;
|
|
const PROVIDER_CONTROL_MAX_OUTPUT_BYTES = 65_536;
|
|
const PROVIDER_CONTROL_POLL_MS = 25;
|
|
const MINIMUM_PROVIDER_SYSTEMD_VERSION = 254;
|
|
if (kind !== "vulnerability" && kind !== "provenance") {
|
|
process.stderr.write("Usage: run-and-validate-provider --kind vulnerability|provenance\n");
|
|
process.exit(2);
|
|
}
|
|
traceOwnProviderProcess("supervisor");
|
|
const command =
|
|
kind === "vulnerability"
|
|
? process.env.VULNERABILITY_PROVIDER_COMMAND
|
|
: process.env.PROVENANCE_PROVIDER_COMMAND;
|
|
const reportPath =
|
|
kind === "vulnerability"
|
|
? process.env.VULNERABILITY_REPORT_PATH
|
|
: process.env.PROVENANCE_ATTESTATION_PATH;
|
|
const sealedPath = process.env.VALIDATED_PROVIDER_REPORT_PATH;
|
|
const archivePath = process.env.CANDIDATE_ARCHIVE_PATH;
|
|
const archiveSha256 = process.env.CANDIDATE_ARCHIVE_SHA256;
|
|
const publicKeyPath = kind === "vulnerability"
|
|
? process.env.VULNERABILITY_PUBLIC_KEY_PATH
|
|
: process.env.PROVENANCE_PUBLIC_KEY_PATH;
|
|
const keyId = kind === "vulnerability"
|
|
? process.env.VULNERABILITY_KEY_ID
|
|
: process.env.PROVENANCE_KEY_ID;
|
|
const runId = process.env.GITEA_RUN_ID ?? process.env.GITHUB_RUN_ID ?? process.env.CI_RUN_ID;
|
|
const runAttemptSource = process.env.GITEA_RUN_ATTEMPT ??
|
|
process.env.GITHUB_RUN_ATTEMPT ?? process.env.CI_RUN_ATTEMPT;
|
|
const sourceRevision = process.env.EXPECTED_SOURCE_REVISION ?? process.env.VITE_COMMIT_SHA;
|
|
if (
|
|
!command ||
|
|
!reportPath ||
|
|
!sealedPath ||
|
|
!archivePath ||
|
|
!archiveSha256 ||
|
|
!publicKeyPath ||
|
|
!keyId ||
|
|
!runId ||
|
|
!runAttemptSource ||
|
|
!sourceRevision
|
|
) {
|
|
process.stderr.write("Provider supervisor environment is incomplete\n");
|
|
process.exit(2);
|
|
}
|
|
const runAttempt = Number(runAttemptSource);
|
|
if (!Number.isInteger(runAttempt) || runAttempt < 1 || runAttempt > 1_000) {
|
|
throw new TypeError("provider supervisor run attempt is invalid");
|
|
}
|
|
|
|
const workspaceRoot = process.cwd();
|
|
const reportAbsolute = path.resolve(reportPath);
|
|
const rawDirectory = path.dirname(reportAbsolute);
|
|
const sealedAbsolute = path.resolve(sealedPath);
|
|
const expectedRawLeaf = kind === "vulnerability"
|
|
? "vulnerability-report.json"
|
|
: "provenance-attestation.json";
|
|
const expectedRawDirectory = path.resolve(workspaceRoot, "provider-evidence/untrusted");
|
|
if (
|
|
reportAbsolute !== path.join(expectedRawDirectory, expectedRawLeaf) ||
|
|
path.basename(rawDirectory) !== "untrusted" ||
|
|
path.dirname(rawDirectory) !== path.dirname(sealedAbsolute) ||
|
|
reportAbsolute === sealedAbsolute
|
|
) {
|
|
throw new TypeError("provider raw and sealed evidence paths are not isolated");
|
|
}
|
|
await prepareMissingProviderOutput(workspaceRoot, reportAbsolute, reportPath, "raw provider report");
|
|
await prepareMissingProviderOutput(workspaceRoot, sealedAbsolute, sealedPath, "sealed provider report");
|
|
await access("/usr/bin/bwrap", constants.X_OK).catch(() => {
|
|
throw new Error("provider sandbox unavailable: /usr/bin/bwrap is required");
|
|
});
|
|
await access("/usr/bin/prlimit", constants.X_OK).catch(() => {
|
|
throw new Error("provider sandbox unavailable: /usr/bin/prlimit is required");
|
|
});
|
|
await access("/usr/bin/systemd-run", constants.X_OK).catch(() => {
|
|
throw new Error("provider cgroup unavailable: /usr/bin/systemd-run is required");
|
|
});
|
|
await access("/usr/bin/systemctl", constants.X_OK).catch(() => {
|
|
throw new Error("provider cgroup unavailable: /usr/bin/systemctl is required");
|
|
});
|
|
await assertProviderCgroupManagerAvailable();
|
|
const trust = await readProviderTrust(workspaceRoot, publicKeyPath, keyId);
|
|
if (!trust) throw new TypeError("provider supervisor trust key is invalid");
|
|
let guardianLease: ProviderGuardianLease | undefined;
|
|
let guardianTerminalStarted = false;
|
|
try {
|
|
const providerWallTimeoutMs = providerTimeoutMs();
|
|
const supervised = await superviseProviderEvidence({
|
|
kind,
|
|
archivePath,
|
|
expectedArchiveSha256: archiveSha256,
|
|
expectedRun: { id: runId, attempt: runAttempt, sourceRevision },
|
|
trust,
|
|
executeProvider: async ({ candidateRoot, environment }) => {
|
|
guardianLease = await startProviderGuardian({
|
|
kind,
|
|
workspaceRoot,
|
|
leaseMs: providerWallTimeoutMs + PROVIDER_POSTPROCESS_TIMEOUT_MS,
|
|
guardianScript: path.join(workspaceRoot, "scripts/lib/provider-raw-guardian.ts"),
|
|
});
|
|
const activeGuardian = guardianLease;
|
|
assertProviderGuardianLeasePaths(activeGuardian, {
|
|
rawPath: reportAbsolute,
|
|
sealedPath: sealedAbsolute,
|
|
});
|
|
const childEnvironment = createProviderEnvironment(kind, reportPath, environment);
|
|
await runProviderInSandbox(
|
|
kind,
|
|
command,
|
|
childEnvironment,
|
|
activeGuardian.rawPath,
|
|
activeGuardian.rawIdentity,
|
|
workspaceRoot,
|
|
candidateRoot,
|
|
providerWallTimeoutMs,
|
|
activeGuardian.prematureExit,
|
|
);
|
|
await assertOwnedProviderOutput(activeGuardian.rawPath, activeGuardian.rawIdentity);
|
|
},
|
|
captureReport: async () => {
|
|
if (!guardianLease) throw new Error("provider raw guardian lease was not established");
|
|
await assertOwnedProviderOutput(guardianLease.rawPath, guardianLease.rawIdentity);
|
|
return readBoundedRegularFile({
|
|
root: workspaceRoot,
|
|
relativePath: path.relative(workspaceRoot, guardianLease.rawPath).replaceAll(path.sep, "/"),
|
|
maxBytes: 8_388_608,
|
|
});
|
|
},
|
|
});
|
|
if (!guardianLease) throw new Error("provider raw guardian lease was not established");
|
|
await assertSafePublishLeaf(sealedAbsolute, sealedPath);
|
|
await guardianLease.publish(serializeValidatedJsonArtifact({
|
|
path: sealedPath,
|
|
schema:
|
|
kind === "vulnerability"
|
|
? vulnerabilityProviderReportSchema
|
|
: provenanceProviderAttestationSchema,
|
|
value: supervised.evidence,
|
|
}));
|
|
if (process.env.GITHUB_OUTPUT) {
|
|
await appendFile(
|
|
process.env.GITHUB_OUTPUT,
|
|
`invocation_nonce=${supervised.invocationNonce}\n`,
|
|
"utf8",
|
|
);
|
|
}
|
|
guardianTerminalStarted = true;
|
|
await guardianLease.commit();
|
|
} catch (providerError) {
|
|
const failures = [toError(providerError)];
|
|
if (guardianLease && !guardianTerminalStarted) {
|
|
try {
|
|
await guardianLease.abort();
|
|
} catch (guardianError) {
|
|
failures.push(toError(guardianError));
|
|
}
|
|
}
|
|
if (failures.length > 1) {
|
|
throw new AggregateError(failures, "provider lifecycle and owned output cleanup failed", {
|
|
cause: providerError,
|
|
});
|
|
}
|
|
throw providerError;
|
|
}
|
|
process.stdout.write(`${kind} provider supervised validation: PASS\n`);
|
|
|
|
function createProviderEnvironment(
|
|
providerKind: "vulnerability" | "provenance",
|
|
rawReportPath: string,
|
|
bindings: Readonly<Record<string, string>>,
|
|
): NodeJS.ProcessEnv {
|
|
const environment: NodeJS.ProcessEnv = {
|
|
PATH: process.env.PATH ?? "/usr/local/bin:/usr/bin:/bin",
|
|
HOME: "/tmp/provider-home",
|
|
TMPDIR: "/tmp",
|
|
CI: "true",
|
|
GITHUB_ENV: "/tmp/github-env",
|
|
GITHUB_PATH: "/tmp/github-path",
|
|
...bindings,
|
|
...(providerKind === "vulnerability"
|
|
? { VULNERABILITY_REPORT_PATH: rawReportPath }
|
|
: { PROVENANCE_ATTESTATION_PATH: rawReportPath }),
|
|
};
|
|
for (const name of ["LANG", "LC_ALL"] as const) {
|
|
if (process.env[name]) environment[name] = process.env[name];
|
|
}
|
|
const credentialPrefix = `${providerKind.toUpperCase()}_PROVIDER_`;
|
|
for (const [name, value] of Object.entries(process.env)) {
|
|
if (name.startsWith(credentialPrefix) && !name.endsWith("_COMMAND") && value) {
|
|
environment[name] = value;
|
|
}
|
|
}
|
|
return environment;
|
|
}
|
|
|
|
async function runProviderInSandbox(
|
|
providerKind: "vulnerability" | "provenance",
|
|
command: string,
|
|
environment: NodeJS.ProcessEnv,
|
|
reportAbsolute: string,
|
|
reportIdentity: Readonly<{ dev: number; ino: number }>,
|
|
workspaceRoot: string,
|
|
candidateRoot: string,
|
|
timeoutMs: number,
|
|
guardianExit: Promise<Error>,
|
|
): Promise<void> {
|
|
const cpuSeconds = providerCpuSeconds();
|
|
const bwrapArguments = [
|
|
"--die-with-parent", "--new-session", "--as-pid-1",
|
|
...PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS,
|
|
"--dev", "/dev", "--remount-ro", "/dev",
|
|
"--proc", "/proc", "--remount-ro", "/proc",
|
|
"--size", String(PROVIDER_TMP_BYTES), "--tmpfs", "/tmp",
|
|
"--dir", "/tmp/provider-home",
|
|
"--size", String(PROVIDER_MASK_BYTES), "--tmpfs", "/etc",
|
|
];
|
|
for (const source of ["/usr", "/bin", "/lib", "/lib64"]) {
|
|
if (await exists(source)) bwrapArguments.push("--ro-bind", source, source);
|
|
}
|
|
bwrapArguments.push("--ro-bind", process.execPath, "/tmp/node");
|
|
for (const source of [
|
|
"/etc/ca-certificates", "/etc/ssl", "/etc/nsswitch.conf", "/etc/passwd", "/etc/group",
|
|
]) {
|
|
if (await exists(source)) bwrapArguments.push("--ro-bind", source, source);
|
|
}
|
|
bwrapArguments.push("--remount-ro", "/etc");
|
|
for (const directory of missingDestinationAncestors(workspaceRoot)) {
|
|
bwrapArguments.push("--dir", directory);
|
|
}
|
|
bwrapArguments.push("--ro-bind", workspaceRoot, workspaceRoot);
|
|
const repositoryMetadata = path.join(workspaceRoot, ".git");
|
|
const metadataKind = await entryKind(repositoryMetadata);
|
|
if (metadataKind === "directory") {
|
|
bwrapArguments.push(
|
|
"--size", String(PROVIDER_MASK_BYTES),
|
|
"--tmpfs", repositoryMetadata,
|
|
"--remount-ro", repositoryMetadata,
|
|
);
|
|
} else if (metadataKind === "other") {
|
|
// 1103/11-04. A linked worktree or a submodule checkout has a one-line
|
|
// `gitdir:` file here, and a tmpfs cannot be mounted on a file. /dev/null
|
|
// is bound over it instead. bubblewrap mounts binds nodev, so the provider
|
|
// can neither read the line nor write the path; the directory it names
|
|
// lies outside the workspace and is never bound into the sandbox.
|
|
bwrapArguments.push("--ro-bind", "/dev/null", repositoryMetadata);
|
|
}
|
|
bwrapArguments.push(
|
|
"--ro-bind", candidateRoot, "/candidate",
|
|
"--remount-ro", "/tmp",
|
|
"--remount-ro", "/",
|
|
"--bind", reportAbsolute, reportAbsolute,
|
|
"--chdir", workspaceRoot,
|
|
);
|
|
/**
|
|
* Everything above is a bubblewrap *option* and travels in the args file, so
|
|
* host paths never reach `/proc/<pid>/cmdline`. The command below cannot: an
|
|
* args file's option stream ends at the first non-option and bubblewrap drops
|
|
* the remainder, so a command written there is never executed. It stays on
|
|
* real argv, and it is safe there because the provider command and its
|
|
* credentials are passed as `--setenv PROVIDER_COMMAND` inside the args file
|
|
* and only expanded by the innermost shell.
|
|
*/
|
|
const bwrapCommand = [
|
|
"/usr/bin/prlimit",
|
|
"--core=0:0",
|
|
"--fsize=8388607:8388607",
|
|
"--nofile=64:64",
|
|
`--cpu=${cpuSeconds}:${cpuSeconds}`,
|
|
"--", "/bin/sh", "-eu", "-c",
|
|
'exec /bin/sh -eu -c "$PROVIDER_COMMAND"',
|
|
];
|
|
const unitName = formatProviderCgroupUnitName(
|
|
providerKind,
|
|
process.pid,
|
|
randomBytes(12).toString("hex"),
|
|
);
|
|
const bwrapInput = encodeProviderBwrapInput(bwrapArguments, {
|
|
...environment,
|
|
PATH: `/tmp:${environment.PATH ?? ""}`,
|
|
PROVIDER_COMMAND: command,
|
|
});
|
|
const scopeFrame = encodeProviderScopeFrame({
|
|
bwrapInput,
|
|
bwrapCommand,
|
|
reportPath: reportAbsolute,
|
|
reportDev: reportIdentity.dev,
|
|
reportIno: reportIdentity.ino,
|
|
});
|
|
const systemdRunArguments = systemdRunProviderArguments(
|
|
unitName,
|
|
timeoutMs,
|
|
cpuSeconds,
|
|
process.execPath,
|
|
path.join(workspaceRoot, "scripts/lib/provider-scope-wrapper.ts"),
|
|
reportAbsolute,
|
|
reportIdentity.dev,
|
|
reportIdentity.ino,
|
|
);
|
|
const systemdRun = spawn("/usr/bin/systemd-run", systemdRunArguments, {
|
|
env: { ...providerCgroupClientEnvironment(), ...providerProcessTraceEnvironment() },
|
|
stdio: ["pipe", "pipe", "pipe"],
|
|
});
|
|
traceSpawnedProviderProcess("systemd-run", systemdRun.pid, ["/usr/bin/systemd-run", ...systemdRunArguments]);
|
|
await waitForProvider(
|
|
systemdRun,
|
|
timeoutMs,
|
|
unitName,
|
|
scopeFrame,
|
|
guardianExit,
|
|
);
|
|
}
|
|
|
|
async function waitForProvider(
|
|
child: ReturnType<typeof spawn>,
|
|
timeoutMs: number,
|
|
unitName: string,
|
|
scopeFrame: Buffer,
|
|
guardianExit: Promise<Error>,
|
|
): Promise<void> {
|
|
let termination: "guardian" | "timeout" | "output" | undefined;
|
|
let guardianError: Error | undefined;
|
|
let signalTermination!: () => void;
|
|
let kill: Promise<void> | undefined;
|
|
const terminationStarted = new Promise<void>((resolve) => { signalTermination = resolve; });
|
|
const close = new Promise<Readonly<{ code: number | null; error?: Error; signal: NodeJS.Signals | null }>>((resolve) => {
|
|
child.once("error", (error) => resolve({ code: null, error, signal: null }));
|
|
child.once("close", (code, signal) => resolve({ code, signal }));
|
|
});
|
|
let inputError: Error | undefined;
|
|
child.stdin?.once("error", (error) => { inputError = error; });
|
|
if (child.stdin) child.stdin.write(scopeFrame);
|
|
else inputError = new Error("systemd-run provider argument pipe is unavailable");
|
|
const terminate = (reason: "guardian" | "timeout" | "output"): void => {
|
|
if (termination) return;
|
|
termination = reason;
|
|
kill = killProviderUnit(unitName);
|
|
signalTermination();
|
|
};
|
|
const guardianLatch = createProviderScopeGuardianLatch(guardianExit);
|
|
void guardianLatch.activeFailure.then((error) => {
|
|
guardianError = error;
|
|
terminate("guardian");
|
|
});
|
|
const outputLimiter = createProviderOutputLimiter(
|
|
PROVIDER_MAX_OUTPUT_BYTES,
|
|
() => terminate("output"),
|
|
);
|
|
/**
|
|
* Lines the sandbox tooling itself emits, kept so a launch failure can say
|
|
* why. Everything else the child writes is provider output and may carry
|
|
* credentials, so it is counted and discarded as before.
|
|
*
|
|
* Without this a sandbox that never started reported only `exit=1`, and the
|
|
* actual cause — `bwrap: loopback: Failed RTM_NEWADDR: Operation not
|
|
* permitted` on a host with `kernel.apparmor_restrict_unprivileged_userns=1`
|
|
* — was invisible. That turned a host restriction into an unexplained
|
|
* product failure.
|
|
*/
|
|
const SANDBOX_DIAGNOSTIC = /^(?:bwrap|prlimit|systemd-run|systemctl):\s.*$/gmu;
|
|
const sandboxDiagnostics: string[] = [];
|
|
const capture = (chunk: Buffer | string): void => {
|
|
for (const line of String(chunk).matchAll(SANDBOX_DIAGNOSTIC)) {
|
|
if (sandboxDiagnostics.length < 8 && !sandboxDiagnostics.includes(line[0])) {
|
|
sandboxDiagnostics.push(line[0]);
|
|
}
|
|
}
|
|
if (termination) return;
|
|
outputLimiter.consume(chunk);
|
|
};
|
|
child.stdout?.on("data", capture);
|
|
child.stderr?.on("data", capture);
|
|
const timeout = setTimeout(() => terminate("timeout"), timeoutMs);
|
|
try {
|
|
const first = await Promise.race([
|
|
close.then((result) => ({ type: "close" as const, result })),
|
|
terminationStarted.then(() => ({ type: "termination" as const })),
|
|
]);
|
|
let collection: Promise<void> | undefined;
|
|
if (first.type === "close" && !termination) {
|
|
collection = waitForProviderUnitCollected(unitName);
|
|
const scopeOutcome = await Promise.race([
|
|
collection.then(() => "collected" as const),
|
|
terminationStarted.then(() => "termination" as const),
|
|
]);
|
|
if (scopeOutcome === "collected") {
|
|
await guardianLatch.close();
|
|
const boundaryFailure = guardianLatch.failure();
|
|
if (boundaryFailure && !termination) {
|
|
guardianError = boundaryFailure;
|
|
terminate("guardian");
|
|
}
|
|
}
|
|
}
|
|
if (termination) {
|
|
let killError: Error | undefined;
|
|
try { await kill; } catch (error) { killError = toError(error); child.kill("SIGKILL"); }
|
|
const closed = await waitForProviderClose(close);
|
|
if (!closed) child.kill("SIGKILL");
|
|
let collectionError: Error | undefined;
|
|
try {
|
|
await (collection ?? waitForProviderUnitCollected(unitName));
|
|
} catch (error) {
|
|
collectionError = toError(error);
|
|
}
|
|
await guardianLatch.close();
|
|
const reason = termination === "timeout"
|
|
? "sandboxed external provider command timed out"
|
|
: termination === "output"
|
|
? `sandboxed external provider output exceeded the ${PROVIDER_MAX_OUTPUT_BYTES}-byte aggregate limit`
|
|
: `provider raw guardian failed${guardianError ? `: ${guardianError.message}` : ""}`;
|
|
if (!closed) throw new Error(`${reason} and systemd-run did not close within the reap bound`);
|
|
if (collectionError) throw new Error(`${reason}; provider cgroup collection failed: ${collectionError.message}`);
|
|
if (killError) throw new Error(`${reason}; provider cgroup kill failed: ${killError.message}`);
|
|
throw new Error(reason);
|
|
}
|
|
const result = first.type === "close" ? first.result : await close;
|
|
await collection;
|
|
if (result.error) throw result.error;
|
|
if (result.code !== 0 || result.signal !== null) {
|
|
throw new Error(
|
|
`sandboxed external provider failed: exit=${result.code ?? "none"}, ` +
|
|
`signal=${result.signal ?? "none"}` +
|
|
(sandboxDiagnostics.length > 0
|
|
? `; sandbox reported: ${sandboxDiagnostics.join("; ")}`
|
|
: ""),
|
|
);
|
|
}
|
|
if (inputError) throw inputError;
|
|
} finally {
|
|
clearTimeout(timeout);
|
|
await guardianLatch.close();
|
|
}
|
|
}
|
|
|
|
async function waitForProviderClose(
|
|
close: Promise<Readonly<{ code: number | null; error?: Error; signal: NodeJS.Signals | null }>>,
|
|
): Promise<boolean> {
|
|
let timer: NodeJS.Timeout | undefined;
|
|
const closed = await Promise.race([
|
|
close.then(() => true),
|
|
new Promise<false>((resolve) => { timer = setTimeout(() => resolve(false), PROVIDER_REAP_TIMEOUT_MS); }),
|
|
]);
|
|
if (timer) clearTimeout(timer);
|
|
return closed;
|
|
}
|
|
|
|
async function assertProviderCgroupManagerAvailable(): Promise<void> {
|
|
const output = await runBoundedSystemctl(
|
|
["--user", "show", "--property=Version", "--value"],
|
|
"provider cgroup user manager probe",
|
|
);
|
|
const match = /^([0-9]+)/u.exec(output.trim());
|
|
const version = match ? Number(match[1]) : Number.NaN;
|
|
if (!Number.isSafeInteger(version) || version < MINIMUM_PROVIDER_SYSTEMD_VERSION) {
|
|
throw new Error(`provider cgroup unavailable: systemd ${MINIMUM_PROVIDER_SYSTEMD_VERSION} or newer is required`);
|
|
}
|
|
}
|
|
|
|
async function killProviderUnit(unitName: string): Promise<void> {
|
|
await runBoundedSystemctl(systemctlKillProviderArguments(unitName), "provider cgroup unit kill");
|
|
}
|
|
|
|
async function waitForProviderUnitCollected(unitName: string): Promise<void> {
|
|
const deadline = Date.now() + PROVIDER_CONTROL_TIMEOUT_MS;
|
|
let loadState = "unknown";
|
|
while (Date.now() <= deadline) {
|
|
loadState = (await runBoundedSystemctl(
|
|
["--user", "show", unitName, "--property=LoadState", "--value"],
|
|
"provider cgroup collection probe",
|
|
)).trim();
|
|
if (loadState === "not-found") return;
|
|
await new Promise<void>((resolve) => setTimeout(resolve, PROVIDER_CONTROL_POLL_MS));
|
|
}
|
|
throw new Error(`provider unit remained loaded with state ${loadState || "unknown"}`);
|
|
}
|
|
|
|
async function runBoundedSystemctl(arguments_: readonly string[], label: string): Promise<string> {
|
|
return await new Promise<string>((resolve, reject) => {
|
|
const child = spawn("/usr/bin/systemctl", arguments_, {
|
|
env: providerCgroupClientEnvironment(),
|
|
stdio: ["ignore", "pipe", "pipe"],
|
|
});
|
|
let bytes = 0;
|
|
const chunks: Buffer[] = [];
|
|
let settled = false;
|
|
let termination: "output" | "timeout" | undefined;
|
|
let reap: NodeJS.Timeout | undefined;
|
|
const finish = (error?: Error): void => {
|
|
if (settled) return;
|
|
settled = true;
|
|
clearTimeout(timeout);
|
|
if (reap) clearTimeout(reap);
|
|
error ? reject(error) : resolve(Buffer.concat(chunks).toString("utf8"));
|
|
};
|
|
const terminate = (reason: "output" | "timeout"): void => {
|
|
if (termination) return;
|
|
termination = reason;
|
|
child.kill("SIGKILL");
|
|
reap = setTimeout(() => finish(new Error(`${label} ${reason} bound was exceeded and systemctl did not close`)), PROVIDER_REAP_TIMEOUT_MS);
|
|
};
|
|
const capture = (chunk: Buffer | string): void => {
|
|
if (termination) return;
|
|
const value = Buffer.isBuffer(chunk) ? chunk : Buffer.from(chunk);
|
|
const remaining = PROVIDER_CONTROL_MAX_OUTPUT_BYTES - bytes;
|
|
if (remaining > 0) { chunks.push(value.subarray(0, remaining)); bytes += Math.min(value.length, remaining); }
|
|
if (value.length > remaining) terminate("output");
|
|
};
|
|
child.stdout?.on("data", capture);
|
|
child.stderr?.on("data", capture);
|
|
const timeout = setTimeout(() => terminate("timeout"), PROVIDER_CONTROL_TIMEOUT_MS);
|
|
child.once("error", (error) => finish(error));
|
|
child.once("close", (code, signal) => {
|
|
if (termination) return finish(new Error(`${label} ${termination} bound was exceeded`));
|
|
if (code === 0 && signal === null) return finish();
|
|
const detail = Buffer.concat(chunks).toString("utf8").trim();
|
|
finish(new Error(`${label} failed: exit=${code ?? "none"}, signal=${signal ?? "none"}${detail ? `, output=${detail}` : ""}`));
|
|
});
|
|
});
|
|
}
|
|
|
|
function providerCgroupClientEnvironment(): NodeJS.ProcessEnv {
|
|
const environment: NodeJS.ProcessEnv = { PATH: "/usr/bin:/bin" };
|
|
for (const name of ["DBUS_SESSION_BUS_ADDRESS", "HOME", "LANG", "LC_ALL", "LOGNAME", "USER", "XDG_RUNTIME_DIR"] as const) {
|
|
if (process.env[name]) environment[name] = process.env[name];
|
|
}
|
|
return environment;
|
|
}
|
|
|
|
function providerCpuSeconds(): number {
|
|
const value = process.env.PROVIDER_SUPERVISOR_CPU_SECONDS;
|
|
if (!value) return DEFAULT_PROVIDER_CPU_SECONDS;
|
|
const parsed = Number(value);
|
|
if (!Number.isSafeInteger(parsed) || parsed <= 0 || parsed > DEFAULT_PROVIDER_CPU_SECONDS) {
|
|
throw new TypeError("PROVIDER_SUPERVISOR_CPU_SECONDS must be a positive integer no greater than 1200");
|
|
}
|
|
return parsed;
|
|
}
|
|
|
|
function providerTimeoutMs(): number {
|
|
const value = process.env.PROVIDER_SUPERVISOR_TIMEOUT_MS;
|
|
if (!value) return DEFAULT_PROVIDER_TIMEOUT_MS;
|
|
const parsed = Number(value);
|
|
if (!Number.isSafeInteger(parsed) || parsed <= 0 || parsed > DEFAULT_PROVIDER_TIMEOUT_MS) {
|
|
throw new TypeError("PROVIDER_SUPERVISOR_TIMEOUT_MS must be a positive integer no greater than 1800000");
|
|
}
|
|
return parsed;
|
|
}
|
|
|
|
function toError(error: unknown): Error {
|
|
return error instanceof Error ? error : new Error(String(error));
|
|
}
|
|
|
|
function missingDestinationAncestors(target: string): string[] {
|
|
const ancestors: string[] = [];
|
|
let current = path.dirname(path.resolve(target));
|
|
while (current !== path.parse(current).root && !["/usr", "/bin", "/lib", "/lib64", "/tmp"].includes(current)) {
|
|
ancestors.push(current);
|
|
current = path.dirname(current);
|
|
}
|
|
return ancestors.reverse();
|
|
}
|
|
|
|
/** What `target` is once symlinks are followed, as bubblewrap will see it. */
|
|
async function entryKind(target: string): Promise<"directory" | "other" | null> {
|
|
try {
|
|
return (await stat(target)).isDirectory() ? "directory" : "other";
|
|
} catch (error) {
|
|
if (hasErrorCode(error, "ENOENT")) return null;
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function exists(target: string): Promise<boolean> {
|
|
try {
|
|
await lstat(target);
|
|
return true;
|
|
} catch (error) {
|
|
if (hasErrorCode(error, "ENOENT")) return false;
|
|
throw error;
|
|
}
|
|
}
|
|
|
|
async function prepareMissingProviderOutput(
|
|
root: string,
|
|
absolutePath: string,
|
|
configuredPath: string,
|
|
label: string,
|
|
): Promise<void> {
|
|
await ensureSafePublishDirectory(root, path.dirname(absolutePath));
|
|
await assertSafePublishLeaf(absolutePath, configuredPath);
|
|
try {
|
|
await lstat(absolutePath);
|
|
throw new Error(`${label} already exists: ${configuredPath}`);
|
|
} catch (error) {
|
|
if (!hasErrorCode(error, "ENOENT")) throw error;
|
|
}
|
|
}
|
|
|
|
async function assertOwnedProviderOutput(
|
|
absolutePath: string,
|
|
identity: Readonly<{ dev: number; ino: number }>,
|
|
): Promise<void> {
|
|
const metadata = await lstat(absolutePath);
|
|
if (
|
|
metadata.isSymbolicLink() ||
|
|
!metadata.isFile() ||
|
|
metadata.dev !== identity.dev ||
|
|
metadata.ino !== identity.ino
|
|
) {
|
|
throw new Error("provider raw output identity changed");
|
|
}
|
|
}
|
|
|
|
function hasErrorCode(error: unknown, code: string): boolean {
|
|
return Boolean(error && typeof error === "object" && "code" in error && error.code === code);
|
|
}
|