fix(system-test): 사전 점검이 provider와 같은 user manager·scope 호출을 한다
문제: `test:system` 사전 점검은 system manager bus(`systemctl show-environment`)와 실행 파일 셋만 보고 PASS를 냈다. provider 본 단계는 `/usr/bin/prlimit`·`/usr/bin/systemd-run`을 쓰고, user manager 버전(254 이상)을 묻고, `systemd-run --user --scope`로 cgroup 한도를 강제한다. 그래서 user bus가 없거나 버전이 낮거나 controller가 위임되지 않은 호스트는 PASS 뒤 provider 시험마다 `provider cgroup unavailable: ...`로 떨어졌다. 고친 것: 점검이 prlimit·systemd-run 실행 가능 여부를 보고, provider와 같은 인자(`USER_MANAGER_VERSION_ARGUMENTS`)로 user manager 버전을 묻고, provider scope와 같은 속성·같은 cgroup 강제 확인으로 `/bin/true`를 한 번 띄운다. scope 인자와 최소 버전은 `scripts/lib/provider-cgroup.ts` 한 곳에 두고 provider와 점검이 같이 쓴다. system bus 점검은 provider가 쓰지 않아 user manager 점검으로 바꿨다. 위임 controller 값은 scope 안의 강제 확인(memory.max·pids.max·cpu.max)이 실제로 읽어 보므로 UNVERIFIED로 남길 몫이 없다. 위험·한계: 점검이 user scope 하나를 띄웠다 지운다(`--collect`, 5초 상한). 시스템 컨테이너 안(root)에서도 provider와 같은 호출이라 같은 결과를 낸다고 보고, FE-GATE-013으로 확인한다. 검증: desktop `scripts/verify.sh exec`로 `node scripts/run-vitest.ts run tests/unit/system-test-prerequisites.test.ts` 등 7파일 134건 통과. 점검 코드를 기준 커밋으로 되돌리면 새 시험 6건이 실패한다(RED). Refs: 1112/08-03
This commit is contained in:
@@ -1,9 +1,16 @@
|
||||
import { spawnSync } from "node:child_process";
|
||||
import { randomBytes } from "node:crypto";
|
||||
import { constants } from "node:fs";
|
||||
import { access, readFile } from "node:fs/promises";
|
||||
import { spawnSync } from "node:child_process";
|
||||
import path from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
|
||||
import {
|
||||
formatProviderPrerequisiteUnitName,
|
||||
MINIMUM_PROVIDER_SYSTEMD_VERSION,
|
||||
systemdRunPrerequisiteProbeArguments,
|
||||
USER_MANAGER_VERSION_ARGUMENTS,
|
||||
} from "./lib/provider-cgroup.ts";
|
||||
import { PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS } from "./lib/provider-sandbox-namespaces.ts";
|
||||
|
||||
/** What the check reads from the host; a test passes a fake. */
|
||||
@@ -15,7 +22,9 @@ export type SystemTestHost = Readonly<{
|
||||
command: string,
|
||||
args: readonly string[],
|
||||
timeoutMs: number,
|
||||
): Readonly<{ status: number | null; stderr: string; error?: Error | undefined }>;
|
||||
): Readonly<{ status: number | null; stdout?: string; stderr: string; error?: Error | undefined }>;
|
||||
/** Names the provider-shaped scope the probe starts; a test passes a fixed one. */
|
||||
probeUnitName?: () => string;
|
||||
}>;
|
||||
|
||||
export const nodeSystemTestHost: SystemTestHost = Object.freeze({
|
||||
@@ -23,12 +32,18 @@ export const nodeSystemTestHost: SystemTestHost = Object.freeze({
|
||||
readFile: async (target: string) => readFile(target, "utf8"),
|
||||
spawn: (command: string, args: readonly string[], timeoutMs: number) => {
|
||||
const result = spawnSync(command, [...args], { encoding: "utf8", timeout: timeoutMs });
|
||||
return { status: result.status, stderr: result.stderr ?? "", error: result.error };
|
||||
return { status: result.status, stdout: result.stdout ?? "", stderr: result.stderr ?? "", error: result.error };
|
||||
},
|
||||
probeUnitName: defaultProbeUnitName,
|
||||
});
|
||||
|
||||
function defaultProbeUnitName(): string {
|
||||
return formatProviderPrerequisiteUnitName(process.pid, randomBytes(12).toString("hex"));
|
||||
}
|
||||
|
||||
const BWRAP = "/usr/bin/bwrap";
|
||||
const SYSTEMCTL = "/usr/bin/systemctl";
|
||||
const SYSTEMD_RUN = "/usr/bin/systemd-run";
|
||||
|
||||
/**
|
||||
* 1105/12-01. The provider sandbox's own namespace options on /bin/true. The
|
||||
@@ -50,6 +65,7 @@ const USER_NAMESPACE_SETTINGS = Object.freeze([
|
||||
] as const);
|
||||
|
||||
const PROBE_TIMEOUT_MS = 5_000;
|
||||
const SCOPE_PROBE_TIMEOUT_MS = 15_000;
|
||||
const MAX_REPORTED_STDERR_LINES = 4;
|
||||
|
||||
export async function findSystemTestPrerequisiteFailures(
|
||||
@@ -59,6 +75,8 @@ export async function findSystemTestPrerequisiteFailures(
|
||||
const executable = new Map<string, boolean>();
|
||||
for (const [target, label] of [
|
||||
[BWRAP, "bubblewrap"],
|
||||
["/usr/bin/prlimit", "prlimit"],
|
||||
[SYSTEMD_RUN, "systemd-run"],
|
||||
[SYSTEMCTL, "systemctl"],
|
||||
["/usr/bin/tar", "tar"],
|
||||
] as const) {
|
||||
@@ -80,10 +98,18 @@ export async function findSystemTestPrerequisiteFailures(
|
||||
failures.push("cgroup v2 is required at /sys/fs/cgroup/cgroup.controllers");
|
||||
}
|
||||
|
||||
// 1112/08-03. The provider asks the user manager (not the system manager)
|
||||
// for its version and then starts its sandbox in a `systemd-run --user
|
||||
// --scope` (scripts/run-and-validate-provider.ts). The check makes the same
|
||||
// calls, so a host without a user bus, with an old manager, or without
|
||||
// delegated controllers fails here by name instead of in each provider test.
|
||||
if (executable.get(SYSTEMCTL)) {
|
||||
const probe = host.spawn(SYSTEMCTL, ["show-environment"], 3_000);
|
||||
if (probe.error || probe.status !== 0) {
|
||||
failures.push("a reachable systemd manager bus is required");
|
||||
const userManager = findUserManagerFailure(host);
|
||||
if (userManager !== undefined) {
|
||||
failures.push(userManager);
|
||||
} else if (executable.get(SYSTEMD_RUN)) {
|
||||
const scope = findProviderScopeFailure(host);
|
||||
if (scope !== undefined) failures.push(scope);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -94,6 +120,52 @@ export async function findSystemTestPrerequisiteFailures(
|
||||
return failures;
|
||||
}
|
||||
|
||||
function findUserManagerFailure(host: SystemTestHost): string | undefined {
|
||||
const probe = host.spawn(SYSTEMCTL, USER_MANAGER_VERSION_ARGUMENTS, 3_000);
|
||||
const command = `${SYSTEMCTL} ${USER_MANAGER_VERSION_ARGUMENTS.join(" ")}`;
|
||||
if (probe.error || probe.status !== 0) {
|
||||
return [
|
||||
`a reachable systemd user manager is required (${command} ${outcomeOf(probe)}).`,
|
||||
...stderrLines(probe.stderr, "systemctl"),
|
||||
" The provider runs in a `systemd-run --user --scope`; give the runner user a user",
|
||||
" manager (for example `loginctl enable-linger <user>`) and a user bus in XDG_RUNTIME_DIR.",
|
||||
].join("\n");
|
||||
}
|
||||
const reported = (probe.stdout ?? "").trim();
|
||||
const match = /^([0-9]+)/u.exec(reported);
|
||||
const version = match ? Number(match[1]) : Number.NaN;
|
||||
if (!Number.isSafeInteger(version) || version < MINIMUM_PROVIDER_SYSTEMD_VERSION) {
|
||||
return `systemd user manager ${MINIMUM_PROVIDER_SYSTEMD_VERSION} or newer is required (${command} reported ${JSON.stringify(reported)})`;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function findProviderScopeFailure(host: SystemTestHost): string | undefined {
|
||||
const args = systemdRunPrerequisiteProbeArguments((host.probeUnitName ?? defaultProbeUnitName)());
|
||||
const probe = host.spawn(SYSTEMD_RUN, args, SCOPE_PROBE_TIMEOUT_MS);
|
||||
if (!probe.error && probe.status === 0) return undefined;
|
||||
return [
|
||||
"systemd-run could not start the provider's cgroup scope around /bin/true " +
|
||||
`(${SYSTEMD_RUN} --user --scope ... ${outcomeOf(probe)}).`,
|
||||
...stderrLines(probe.stderr, "systemd-run"),
|
||||
" The scope needs the memory, pids and cpu controllers delegated to the user manager",
|
||||
" (Delegate= in user@.service) and cgroup v2. Runner requirements: docs/operations/ci-quality-gates.md.",
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
function outcomeOf(probe: ReturnType<SystemTestHost["spawn"]>): string {
|
||||
return probe.error ? `did not finish: ${probe.error.message}` : `exited ${String(probe.status)}`;
|
||||
}
|
||||
|
||||
function stderrLines(stderr: string, label: string): string[] {
|
||||
const lines = stderr
|
||||
.split("\n")
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line.length > 0)
|
||||
.slice(0, MAX_REPORTED_STDERR_LINES);
|
||||
return lines.length > 0 ? lines.map((line) => ` ${label} stderr: ${line}`) : [` ${label} stderr: (empty)`];
|
||||
}
|
||||
|
||||
/**
|
||||
* 1101/13-03. Whether this host lets the provider sandbox create its
|
||||
* namespaces, and if not, why and what to do. The release provider jobs run
|
||||
@@ -118,19 +190,11 @@ async function namespaceFailure(
|
||||
// Absent on this kernel; the others still say what the host allows.
|
||||
}
|
||||
}
|
||||
const stderr = probe.stderr
|
||||
.split("\n")
|
||||
.map((line) => line.trim())
|
||||
.filter((line) => line.length > 0)
|
||||
.slice(0, MAX_REPORTED_STDERR_LINES);
|
||||
const outcome = probe.error
|
||||
? `did not finish: ${probe.error.message}`
|
||||
: `exited ${String(probe.status)}`;
|
||||
return [
|
||||
"this host does not allow unprivileged user and network namespaces, which the provider " +
|
||||
`sandbox needs (bwrap ${PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS.join(" ")}).`,
|
||||
` probe: ${BWRAP} ${NAMESPACE_PROBE_ARGUMENTS.join(" ")} ${outcome}`,
|
||||
...(stderr.length > 0 ? stderr.map((line) => ` bwrap stderr: ${line}`) : [" bwrap stderr: (empty)"]),
|
||||
` probe: ${BWRAP} ${NAMESPACE_PROBE_ARGUMENTS.join(" ")} ${outcomeOf(probe)}`,
|
||||
...stderrLines(probe.stderr, "bwrap"),
|
||||
...(settings.length > 0 ? [` host settings: ${settings.join(", ")}`] : []),
|
||||
" To fix, do one of these:",
|
||||
" - give /usr/bin/bwrap an AppArmor profile that allows `userns` (Ubuntu 23.10 and later),",
|
||||
|
||||
@@ -1,10 +1,24 @@
|
||||
export type ProviderKind = "vulnerability" | "provenance";
|
||||
|
||||
/** The oldest systemd user manager whose `--scope` properties the provider relies on. */
|
||||
export const MINIMUM_PROVIDER_SYSTEMD_VERSION = 254;
|
||||
|
||||
/** `systemctl` arguments that ask the user manager its version; the provider and the prerequisite check both use them. */
|
||||
export const USER_MANAGER_VERSION_ARGUMENTS: readonly string[] = Object.freeze([
|
||||
"--user",
|
||||
"show",
|
||||
"--property=Version",
|
||||
"--value",
|
||||
]);
|
||||
|
||||
const MEMORY_MAX = 1_073_741_824;
|
||||
const TASKS_MAX = 64;
|
||||
const STOP_TIMEOUT_MS = 5_000;
|
||||
const RUNTIME_GRACE_MS = 10_000;
|
||||
const UNIT_NAME = /^ca-provider-(?:vulnerability|provenance)-[1-9][0-9]*-[0-9a-f]{24}\.scope$/u;
|
||||
const PROBE_UNIT_NAME = /^ca-provider-prerequisite-[1-9][0-9]*-[0-9a-f]{24}\.scope$/u;
|
||||
/** How long the prerequisite probe's scope may live; it runs /bin/true. */
|
||||
const PROBE_RUNTIME_MS = 5_000;
|
||||
const UNIT_NONCE = /^[0-9a-f]{24}$/u;
|
||||
const ENVIRONMENT_NAME = /^[A-Za-z_][A-Za-z0-9_]*$/u;
|
||||
const ENFORCEMENT_GATE = [
|
||||
@@ -79,6 +93,37 @@ export function systemdRunProviderArguments(
|
||||
) {
|
||||
throw new TypeError("provider scope wrapper path is invalid");
|
||||
}
|
||||
return [
|
||||
...scopeArguments(unit, timeoutMs + RUNTIME_GRACE_MS),
|
||||
nodeExecutable,
|
||||
wrapperScript,
|
||||
String(cpuSeconds),
|
||||
reportPath,
|
||||
String(reportDev),
|
||||
String(reportIno),
|
||||
];
|
||||
}
|
||||
|
||||
export function formatProviderPrerequisiteUnitName(supervisorPid: number, nonce: string): string {
|
||||
if (!Number.isSafeInteger(supervisorPid) || supervisorPid <= 0 || !UNIT_NONCE.test(nonce)) {
|
||||
throw new TypeError("provider cgroup unit identity is invalid");
|
||||
}
|
||||
return `ca-provider-prerequisite-${supervisorPid}-${nonce}.scope`;
|
||||
}
|
||||
|
||||
/**
|
||||
* 1112/08-03. The provider's own scope — same properties, same check that the
|
||||
* unit's cgroup enforces them — around /bin/true. A host whose user manager
|
||||
* cannot start the scope, or does not delegate the memory, pids and cpu
|
||||
* controllers, fails here instead of in each provider test.
|
||||
*/
|
||||
export function systemdRunPrerequisiteProbeArguments(unit: string): string[] {
|
||||
if (!PROBE_UNIT_NAME.test(unit)) throw new TypeError("provider cgroup unit is invalid");
|
||||
return [...scopeArguments(unit, PROBE_RUNTIME_MS), "/bin/true"];
|
||||
}
|
||||
|
||||
/** `systemd-run` options and the enforcement gate up to its `$0`; the caller appends what the gate execs. */
|
||||
function scopeArguments(unit: string, runtimeMaxMs: number): string[] {
|
||||
return [
|
||||
"--user",
|
||||
"--scope",
|
||||
@@ -94,19 +139,13 @@ export function systemdRunProviderArguments(
|
||||
"--property=KillMode=control-group",
|
||||
"--property=SendSIGKILL=yes",
|
||||
`--property=TimeoutStopSec=${STOP_TIMEOUT_MS}ms`,
|
||||
`--property=RuntimeMaxSec=${timeoutMs + RUNTIME_GRACE_MS}ms`,
|
||||
`--property=RuntimeMaxSec=${runtimeMaxMs}ms`,
|
||||
"--",
|
||||
"/bin/sh",
|
||||
"-eu",
|
||||
"-c",
|
||||
ENFORCEMENT_GATE,
|
||||
unit,
|
||||
nodeExecutable,
|
||||
wrapperScript,
|
||||
String(cpuSeconds),
|
||||
reportPath,
|
||||
String(reportDev),
|
||||
String(reportIno),
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -20,8 +20,10 @@ import {
|
||||
encodeProviderBwrapInput,
|
||||
encodeProviderScopeFrame,
|
||||
formatProviderCgroupUnitName,
|
||||
MINIMUM_PROVIDER_SYSTEMD_VERSION,
|
||||
systemctlKillProviderArguments,
|
||||
systemdRunProviderArguments,
|
||||
USER_MANAGER_VERSION_ARGUMENTS,
|
||||
} from "./lib/provider-cgroup.ts";
|
||||
import {
|
||||
assertProviderGuardianLeasePaths,
|
||||
@@ -49,7 +51,6 @@ 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);
|
||||
@@ -501,7 +502,7 @@ async function waitForProviderClose(
|
||||
|
||||
async function assertProviderCgroupManagerAvailable(): Promise<void> {
|
||||
const output = await runBoundedSystemctl(
|
||||
["--user", "show", "--property=Version", "--value"],
|
||||
USER_MANAGER_VERSION_ARGUMENTS,
|
||||
"provider cgroup user manager probe",
|
||||
);
|
||||
const match = /^([0-9]+)/u.exec(output.trim());
|
||||
|
||||
Reference in New Issue
Block a user