문제: tests/system의 cgroup 테스트는 provider 실행 중 /proc을 5 ms마다 표본으로 읽어 어떤 프로세스가 어떤 argv로 돌았는지 판정한다. systemd-run --scope는 scope로 옮긴 뒤 같은 PID에서 sh 게이트와 scope wrapper로 exec하고, 갓 fork된 자식은 exec 전까지 부모 argv를 보인다. 표본은 PID마다 처음 본 argv만 남겨 이 짧은 단계를 자주 놓친다. 운영 코드에는 테스트가 결정적으로 읽을 실행 증거가 없다. 고른 방법: scripts/lib/provider-process-trace.ts를 추가한다. PROVIDER_PROCESS_TRACE_PATH가 있을 때만 각 단계가 JSON 한 줄을 덧붙인다. - 자기 기록: supervisor와 scope wrapper가 /proc/self/cmdline과 /proc/self/cgroup을 남긴다(커널이 본 값). - 자식 기록: supervisor가 systemd-run, guardian client가 guardian, wrapper가 bwrap을 spawn에 넘긴 argv 배열 그대로 남긴다. spawn과 기록이 같은 배열을 쓴다. - 필드는 role, pid, ppid, argv, (자기 기록만) cgroup. 환경변수는 담지 않는다. credential과 provider 명령이 지나가는 곳이기 때문이다. - systemd-run 환경은 허용 목록으로 다시 만들어지므로 providerProcessTraceEnvironment()로 기록 경로만 넘긴다. bwrap은 --clearenv라 sandbox 안의 provider는 경로를 받지 않는다. guardian은 빈 환경을 그대로 두고 client가 spawn 지점에서 기록한다. 테스트용 opt-in으로 두고 운영 감사 증거로 쓰지 않는다. 운영에서 켜면 argv가 두 번째로 남는 곳이 생긴다. 나중에 credential이나 provider 명령이 argv에 들어가는 회귀가 생기면, 지금은 /proc에 잠깐 보이는 값이 파일로 남아 CI 산출물이 될 수 있다. 감사 증거로 쓰려면 봉인 보고서와 같은 경로 안전·크기 상한·스키마·보존 정책이 필요하고 이번 범위가 아니다. CI 워크플로는 이 변수를 설정하지 않는다. 버린 대안: - /proc 표본 보강(부모와 같은 argv 무시, PID별 argv 이력 보존): 리뷰 README 13.1에서 시도하고 되돌렸다. 이력 보존은 prlimit -> sh로 바뀌는 PID 이력에 provider 명령이 섞여 "인프라 argv에 명령 없음" 단언의 뜻이 바뀐다. - spawn 직후 /proc/<pid>/cmdline 읽기: libuv는 exec 뒤에 반환하지만 systemd-run은 곧 wrapper로 다시 exec하므로 경쟁이 남는다. - guardian 자기 기록: guardian이 빈 환경으로 뜨는 것이 보장이라 기록 경로를 넘기지 않는다. 위험·한계: - 기록은 각 단계가 스스로 남긴다. 새 spawn 지점이 기록을 빠뜨리면 테스트에서 보이지 않는다. 모듈 주석에 적었다. - 기록 실패는 run 결과를 바꾸지 않고 stderr에 한 줄 남긴다. wrapper의 stderr는 provider 출력으로 버려지므로 빠진 줄은 기록을 읽는 쪽에서 드러난다. - 변수가 없으면 spawn 인자와 환경은 이전과 같다. 검증: - node scripts/run-vitest.ts run tests/unit/provider-process-trace.test.ts 2건 통과. 자식 기록에 env 필드를 넣는 변이를 넣으면 실패한다. - 관련 테스트 6파일 44건 통과(provider-process-trace, task3-selective- integration, provider-output-limiter, security-provider-evidence, provider-guardian-transaction, provider-process-group). - pnpm 없는 하네스(운영 encoder로 systemd-run -> sh -> wrapper -> bwrap -> prlimit -> provider 실행)에서 실행 기록 20회 중 20회 완전. - tsc(tsconfig.node.json, tsconfig.test.json), eslint 통과.
70 lines
2.7 KiB
TypeScript
70 lines
2.7 KiB
TypeScript
import { existsSync } from "node:fs";
|
|
import { mkdtemp, readFile, rm } from "node:fs/promises";
|
|
import { tmpdir } from "node:os";
|
|
import path from "node:path";
|
|
|
|
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
|
|
import {
|
|
PROVIDER_PROCESS_TRACE_VARIABLE,
|
|
providerProcessTraceEnvironment,
|
|
traceOwnProviderProcess,
|
|
traceSpawnedProviderProcess,
|
|
} from "../../scripts/lib/provider-process-trace.ts";
|
|
|
|
const roots: string[] = [];
|
|
|
|
afterEach(async () => {
|
|
vi.unstubAllEnvs();
|
|
vi.restoreAllMocks();
|
|
await Promise.all(roots.splice(0).map((root) => rm(root, { recursive: true, force: true })));
|
|
});
|
|
|
|
describe("provider process trace", () => {
|
|
it("records role, PIDs, argv and own cgroup, never the environment", async () => {
|
|
const root = await mkdtemp(path.join(tmpdir(), "provider-process-trace-"));
|
|
roots.push(root);
|
|
const target = path.join(root, "trace.jsonl");
|
|
const credential = "trace-must-not-carry-this-credential";
|
|
vi.stubEnv(PROVIDER_PROCESS_TRACE_VARIABLE, target);
|
|
vi.stubEnv("VULNERABILITY_PROVIDER_SECRET", credential);
|
|
|
|
traceOwnProviderProcess("supervisor");
|
|
traceSpawnedProviderProcess("bwrap", 4242, ["/usr/bin/bwrap", "--args", "0"]);
|
|
|
|
const text = await readFile(target, "utf8");
|
|
expect(text).not.toContain(credential);
|
|
const [own, spawned, ...rest] = text.trim().split("\n").map((line) => JSON.parse(line) as unknown);
|
|
expect(rest).toEqual([]);
|
|
// Exact key sets: a field added later (environment, say) fails here.
|
|
expect(own).toEqual({
|
|
role: "supervisor",
|
|
pid: process.pid,
|
|
ppid: process.ppid,
|
|
argv: expect.arrayContaining([expect.any(String)]),
|
|
cgroup: expect.stringMatching(/^0::\//u),
|
|
});
|
|
expect(spawned).toEqual({
|
|
role: "bwrap",
|
|
pid: 4242,
|
|
ppid: process.pid,
|
|
argv: ["/usr/bin/bwrap", "--args", "0"],
|
|
});
|
|
expect(providerProcessTraceEnvironment()).toEqual({ [PROVIDER_PROCESS_TRACE_VARIABLE]: target });
|
|
});
|
|
|
|
it("is a no-op when unset and never fails the run on an unusable path", () => {
|
|
vi.stubEnv(PROVIDER_PROCESS_TRACE_VARIABLE, undefined);
|
|
const stderr = vi.spyOn(process.stderr, "write").mockImplementation(() => true);
|
|
traceOwnProviderProcess("supervisor");
|
|
expect(providerProcessTraceEnvironment()).toEqual({});
|
|
expect(stderr).not.toHaveBeenCalled();
|
|
|
|
const relative = `provider-process-trace-${process.pid}.jsonl`;
|
|
vi.stubEnv(PROVIDER_PROCESS_TRACE_VARIABLE, relative);
|
|
expect(() => traceSpawnedProviderProcess("guardian", 4242, ["/usr/bin/node"])).not.toThrow();
|
|
expect(stderr).toHaveBeenCalledWith(expect.stringMatching(/must be absolute/u));
|
|
expect(existsSync(relative)).toBe(false);
|
|
});
|
|
});
|