test(system): cgroup 테스트가 /proc 표본 대신 실행 기록을 읽는다 (11-01)
문제: "applies effective aggregate cgroup limits ..." 테스트는 recordProviderProcessTree로 /proc을 5 ms마다 읽고 PID마다 처음 본 argv만 남긴다. systemd-run이 같은 PID에서 wrapper로 exec하고, 갓 fork된 자식이 exec 전까지 부모 argv를 보여서 "systemd-run 1개", "guardian 1개" 단언이 간헐적으로 실패한다(리뷰 README 13.1: 부하 약 3에서 10회 중 4회). 고른 방법: 표본을 지우고 앞 커밋의 실행 기록을 읽는다. 기록 파일은 workspace 밖 임시 폴더에 둔다. sandbox가 workspace를 ro-bind하므로 그 안에 두면 provider가 읽을 수 있다. 단언 대응: - 직접 자식 systemd-run 1회: systemd-run 기록 1개, ppid가 supervisor, argv[0]이 /usr/bin/systemd-run - guardian 1회: guardian 기록 1개, ppid가 supervisor - scope wrapper 1회: wrapper 기록 1개, pid가 systemd-run과 같고 cgroup이 `0::<ControlGroup>`과 정확히 같다 - sandbox 관찰: bwrap 기록 1개, ppid가 wrapper, argv에 /usr/bin/prlimit - 인프라 argv에 명령 없음: 모든 기록 argv에 command가 없다. 추가로 providerScript 경로도 없다 - provider 1회: bwrap 실행 1회 + provider가 자기 /proc/self/cgroup에 /ca-provider-vulnerability-가 없으면 exit 8(run 실패) - credential 없음: 기록 파일 원문과 모든 argv에 없다. stdout/stderr와 unit 메타데이터 단언은 그대로 - 종료 뒤 기록된 PID가 모두 없고 cgroup 디렉터리가 없다(그대로) 버린 대안: provider만 host 표본으로 계속 보는 혼합안. provider PID도 bwrap -> prlimit -> sh -> node로 바뀌어 같은 경쟁이 남는다. 위험·한계: - provider의 scope 소속은 host 관찰이 아니라 provider 자신의 확인(exit code)으로 본다. bwrap의 소속은 관찰된 wrapper cgroup에서 fork 상속으로 판단한다. - 기록에 남지 않은 spawn은 보이지 않는다(앞 커밋 한계와 같다). 검증: - 이 테스트는 createProviderFixture에서 corepack pnpm build:release-candidate를 부른다. 작업 규칙상 worktree에서 pnpm을 띄우지 않으므로 실제 테스트의 RED/GREEN은 실행하지 않았다. - 대신 pnpm 없는 하네스로 같은 실행 체인(운영 encoder, systemd-run -> sh -> wrapper -> bwrap -> prlimit -> provider, guardian 대역 포함)을 돌렸다. 옛 표본 판정: 10회 중 4회 실패(systemd-run=0, wrapper=2). 실행 기록 판정: 20회 중 20회 통과. provider 자기 확인은 scope 안에서 통과, host에서 exit 8. - tsc(tsconfig.test.json), eslint 통과.
This commit is contained in:
@@ -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<string>();
|
||||
const directChildren = new Map<number, string>();
|
||||
const cgroupMembers = new Map<number, string>();
|
||||
let stopped = false;
|
||||
|
||||
const remember = (into: Map<number, string>, 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<Readonly<{
|
||||
child: ChildProcess;
|
||||
marker: string;
|
||||
|
||||
Reference in New Issue
Block a user