From eb7d89c742e537312b5c736350ac156b89b4cd30 Mon Sep 17 00:00:00 2001 From: DongHyeonka Date: Fri, 13 Nov 2026 18:45:58 +0900 Subject: [PATCH] =?UTF-8?q?fix(system-test):=20=EC=82=AC=EC=A0=84=20?= =?UTF-8?q?=EC=A0=90=EA=B2=80=EC=9D=B4=20provider=EC=99=80=20=EA=B0=99?= =?UTF-8?q?=EC=9D=80=20user=20manager=C2=B7scope=20=ED=98=B8=EC=B6=9C?= =?UTF-8?q?=EC=9D=84=20=ED=95=9C=EB=8B=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 문제: `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 --- docs/operations/ci-quality-gates.md | 9 ++ scripts/check-system-test-prerequisites.ts | 96 ++++++++++++--- scripts/lib/provider-cgroup.ts | 53 ++++++-- scripts/run-and-validate-provider.ts | 5 +- tests/unit/system-test-prerequisites.test.ts | 122 +++++++++++++++++-- 5 files changed, 250 insertions(+), 35 deletions(-) diff --git a/docs/operations/ci-quality-gates.md b/docs/operations/ci-quality-gates.md index ae54a31d..785c38ba 100644 --- a/docs/operations/ci-quality-gates.md +++ b/docs/operations/ci-quality-gates.md @@ -221,6 +221,15 @@ namespace 점검을 돌아, 이 조건이 없는 runner에서 이름과 host 설 stderr, 위 sysctl 값, 고치는 방법을 적고 exit 1로 멈춘다. 전에는 이 검사가 PASS를 낸 뒤 provider 테스트가 bwrap 원문 오류로 실패했다(2026-11-05 desktop에서 114건 중 18건). +- 같은 검사가 provider의 cgroup 준비도 provider와 같은 호출로 해 본다. + `/usr/bin/prlimit`·`/usr/bin/systemd-run`이 실행 가능한지 보고, + `systemctl --user show --property=Version --value`로 user manager와 버전(254 + 이상)을 확인한 뒤, provider scope와 같은 속성·같은 cgroup 강제 확인으로 + `systemd-run --user --scope`에서 `/bin/true`를 한 번 띄운다 + (`scripts/lib/provider-cgroup.ts`). user bus가 없거나, 버전이 낮거나, memory·pids·cpu + controller가 위임되지 않은 호스트는 그 원인을 이름으로 대며 exit 1이다. 전에는 + system manager bus만 보고 PASS를 낸 뒤 provider 테스트가 하나씩 + `provider cgroup unavailable: ...`로 실패했다. - 고치는 방법은 `scripts/check-system-test-prerequisites.ts`가 실패할 때 출력한다. 호스트의 보안 설정을 바꾸는 일이므로 호스트 소유자가 고른다. 호스트를 바꾸는 설정 파일과 명령은 runner를 만드는 쪽의 인프라 repo가 갖는다. diff --git a/scripts/check-system-test-prerequisites.ts b/scripts/check-system-test-prerequisites.ts index f3aa5ec9..1619ea71 100644 --- a/scripts/check-system-test-prerequisites.ts +++ b/scripts/check-system-test-prerequisites.ts @@ -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(); 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 `) 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): 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),", diff --git a/scripts/lib/provider-cgroup.ts b/scripts/lib/provider-cgroup.ts index 74ae58d4..770f26b3 100644 --- a/scripts/lib/provider-cgroup.ts +++ b/scripts/lib/provider-cgroup.ts @@ -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), ]; } diff --git a/scripts/run-and-validate-provider.ts b/scripts/run-and-validate-provider.ts index a662ca3f..0be35287 100644 --- a/scripts/run-and-validate-provider.ts +++ b/scripts/run-and-validate-provider.ts @@ -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 { 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()); diff --git a/tests/unit/system-test-prerequisites.test.ts b/tests/unit/system-test-prerequisites.test.ts index 8821bc42..9957b620 100644 --- a/tests/unit/system-test-prerequisites.test.ts +++ b/tests/unit/system-test-prerequisites.test.ts @@ -9,33 +9,52 @@ import { NAMESPACE_PROBE_ARGUMENTS, type SystemTestHost, } from "../../scripts/check-system-test-prerequisites.ts"; +import { + systemdRunPrerequisiteProbeArguments, + systemdRunProviderArguments, +} from "../../scripts/lib/provider-cgroup.ts"; import { PROVIDER_SANDBOX_NAMESPACE_ARGUMENTS } from "../../scripts/lib/provider-sandbox-namespaces.ts"; type SpawnCall = Readonly<{ command: string; args: readonly string[] }>; type SpawnResult = ReturnType; const RTM_NEWADDR = "bwrap: loopback: Failed RTM_NEWADDR: Operation not permitted"; +const PROBE_UNIT = "ca-provider-prerequisite-4242-0123456789abcdef01234567.scope"; -/** A host that has every prerequisite, except what `bwrap` and `files` say. */ +type HostOverrides = Readonly<{ + files?: Readonly>; + missing?: readonly string[]; + userManager?: SpawnResult; + systemdRun?: SpawnResult; +}>; + +/** A host that has every prerequisite, except what `bwrap` and the overrides say. */ function fakeHost( bwrap: SpawnResult, - files: Readonly> = {}, + options: HostOverrides = {}, ): { host: SystemTestHost; calls: SpawnCall[] } { const calls: SpawnCall[] = []; const host: SystemTestHost = { - access: async () => undefined, + access: async (target) => { + if (options.missing?.includes(target)) throw new Error(`EACCES: ${target}`); + }, readFile: async (target) => { if (target === "/sys/fs/cgroup/cgroup.controllers") return "cpuset cpu io memory pids\n"; - const content = files[target]; + const content = options.files?.[target]; if (content === undefined) throw new Error(`ENOENT: ${target}`); return content; }, spawn: (command, args) => { calls.push({ command, args }); - if (command === "/usr/bin/systemctl") return { status: 0, stderr: "" }; + if (command === "/usr/bin/systemctl" && args[0] === "--user") { + return options.userManager ?? { status: 0, stdout: "255\n", stderr: "" }; + } + if (command === "/usr/bin/systemctl") return { status: 0, stdout: "", stderr: "" }; + if (command === "/usr/bin/systemd-run") return options.systemdRun ?? { status: 0, stdout: "", stderr: "" }; if (command === "/usr/bin/bwrap") return bwrap; throw new Error(`unexpected spawn: ${command}`); }, + probeUnitName: () => PROBE_UNIT, }; return { host, calls }; } @@ -74,7 +93,7 @@ describe("system test prerequisites (1105/12-01)", () => { it("fails with the namespace restriction, bwrap's stderr and what to do when bwrap cannot set up loopback", async () => { const { host } = fakeHost( { status: 1, stderr: `${RTM_NEWADDR}\n` }, - { "/proc/sys/kernel/apparmor_restrict_unprivileged_userns": "1\n" }, + { files: { "/proc/sys/kernel/apparmor_restrict_unprivileged_userns": "1\n" } }, ); const failures = await findSystemTestPrerequisiteFailures(host); @@ -103,7 +122,7 @@ describe("system test prerequisites (1105/12-01)", () => { expect(failures[0]).toContain("did not finish: spawnSync /usr/bin/bwrap ETIMEDOUT"); }); - it("keeps the executable, cgroup v2 and systemd bus checks, and probes bwrap only when it is there", async () => { + it("keeps the executable, cgroup v2 and systemd checks, and probes bwrap only when it is there", async () => { const calls: SpawnCall[] = []; const host: SystemTestHost = { access: async (target) => { @@ -116,15 +135,98 @@ describe("system test prerequisites (1105/12-01)", () => { }, }; - expect(await findSystemTestPrerequisiteFailures(host)).toEqual([ + const failures = await findSystemTestPrerequisiteFailures(host); + expect(failures.slice(0, 2)).toEqual([ "bubblewrap is required at /usr/bin/bwrap", "cgroup v2 controllers are unavailable", - "a reachable systemd manager bus is required", ]); + expect(failures[2]).toMatch(/^a reachable systemd user manager is required/u); + expect(failures).toHaveLength(3); expect(calls.map(({ command }) => command)).toEqual(["/usr/bin/systemctl"]); }); }); +// 1112/08-03. The check looked at the system manager bus while the provider +// asks the user manager for its version and starts a `systemd-run --user +// --scope`, so a host without a user manager, with an old one, or without +// prlimit, systemd-run or delegated controllers printed PASS and then failed +// each provider test with `provider cgroup unavailable: ...`. +describe("system test prerequisites match the provider's own calls (1112/08-03)", () => { + it("fails by name when the system manager answers but the user manager does not", async () => { + const { host, calls } = fakeHost( + { status: 0, stderr: "" }, + { userManager: { status: 1, stdout: "", stderr: "Failed to connect to bus: No medium found\n" } }, + ); + + const failures = await findSystemTestPrerequisiteFailures(host); + + expect(failures).toHaveLength(1); + expect(failures[0]).toMatch(/^a reachable systemd user manager is required/u); + expect(failures[0]).toContain("/usr/bin/systemctl --user show --property=Version --value exited 1"); + expect(failures[0]).toContain("systemctl stderr: Failed to connect to bus: No medium found"); + expect(calls.some(({ command }) => command === "/usr/bin/systemd-run")).toBe(false); + }); + + it("fails when the user manager is older than the provider needs", async () => { + const { host } = fakeHost({ status: 0, stderr: "" }, { userManager: { status: 0, stdout: "253\n", stderr: "" } }); + + expect(await findSystemTestPrerequisiteFailures(host)).toEqual([ + 'systemd user manager 254 or newer is required (/usr/bin/systemctl --user show --property=Version --value reported "253")', + ]); + }); + + it("requires the prlimit and systemd-run executables the provider runs", async () => { + const { host } = fakeHost({ status: 0, stderr: "" }, { missing: ["/usr/bin/prlimit", "/usr/bin/systemd-run"] }); + + expect(await findSystemTestPrerequisiteFailures(host)).toEqual([ + "prlimit is required at /usr/bin/prlimit", + "systemd-run is required at /usr/bin/systemd-run", + ]); + }); + + it("starts the provider's own scope once and names a scope that cannot enforce its limits", async () => { + const { host, calls } = fakeHost( + { status: 0, stderr: "" }, + { systemdRun: { status: 125, stdout: "", stderr: "provider cgroup enforcement failed: cannot read memory.max\n" } }, + ); + + const failures = await findSystemTestPrerequisiteFailures(host); + + expect(failures).toHaveLength(1); + expect(failures[0]).toMatch(/^systemd-run could not start the provider's cgroup scope/u); + expect(failures[0]).toContain("exited 125"); + expect(failures[0]).toContain("systemd-run stderr: provider cgroup enforcement failed: cannot read memory.max"); + expect(failures[0]).toMatch(/memory, pids and cpu controllers delegated/u); + + const scopes = calls.filter(({ command }) => command === "/usr/bin/systemd-run"); + expect(scopes).toHaveLength(1); + expect(scopes[0]?.args).toEqual(systemdRunPrerequisiteProbeArguments(PROBE_UNIT)); + expect(scopes[0]?.args.at(-1)).toBe("/bin/true"); + }); + + it("probes with the provider scope's properties and enforcement gate, not a copy", () => { + const probe = systemdRunPrerequisiteProbeArguments(PROBE_UNIT); + const provider = systemdRunProviderArguments( + "ca-provider-vulnerability-4242-0123456789abcdef01234567.scope", + 60_000, + 60, + "/usr/bin/node", + "/opt/wrapper.ts", + "/tmp/report.json", + 1, + 1, + ); + const options = (args: readonly string[]) => + args.filter((arg) => arg.startsWith("--") && !arg.startsWith("--unit=") && !arg.startsWith("--property=RuntimeMaxSec=")); + expect(options(probe)).toEqual(options(provider)); + const gate = (args: readonly string[]) => args[args.indexOf("-c") + 1]; + expect(gate(probe)).toBe(gate(provider)); + expect(gate(probe)).toContain("require_cgroup_value memory.max"); + expect(() => systemdRunPrerequisiteProbeArguments("ca-provider-vulnerability-1-0123456789abcdef01234567.scope")) + .toThrow(/unit is invalid/u); + }); +}); + // 1101/13-03. The release provider jobs run the same sandbox on whatever // runner picks them up. The supervisor checked only that the executables // exist, so a host that refuses the namespaces failed later with bwrap's raw @@ -140,7 +242,7 @@ describe("provider sandbox namespace check (1101/13-03)", () => { it("names the restriction, bwrap's stderr and the host settings on a host that refuses them", async () => { const { host } = fakeHost( { status: 1, stderr: `${RTM_NEWADDR}\n` }, - { "/proc/sys/kernel/apparmor_restrict_unprivileged_userns": "1\n" }, + { files: { "/proc/sys/kernel/apparmor_restrict_unprivileged_userns": "1\n" } }, ); const failure = (await findProviderSandboxNamespaceFailure(host)) ?? "";