Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(process): restrict cleanup to captured group leaders

N0zoM1z040c6f60ac9a4
diff --git a/src/application/ProcessCaptureLifecycle.ts b/src/application/ProcessCaptureLifecycle.ts
index afe98cc5b..17839556e 100644
--- a/src/application/ProcessCaptureLifecycle.ts
+++ b/src/application/ProcessCaptureLifecycle.ts
@@ -285,7 +285,7 @@ export const releaseProcessResources = async (options: {
readonly checkpoints: ProcessCheckpoints | undefined;
readonly runId: string;
readonly temporaryRoot: string;
- readonly sampledProcessGroupIds: readonly number[];
+ readonly capturedProcessGroupIds: readonly number[];
}): Promise<string | undefined> => {
for (const timer of options.timers) clearTimeout(timer);
let failure: string | undefined;
@@ -310,11 +310,7 @@ export const releaseProcessResources = async (options: {
failure ??= "filesystem checkpoint cleanup failed";
}
if (options.terminal !== undefined && process.platform !== "win32") {
- const processGroupIds = new Set([
- options.terminal.pid,
- ...options.sampledProcessGroupIds,
- ]);
- for (const processGroupId of processGroupIds) {
+ for (const processGroupId of new Set(options.capturedProcessGroupIds)) {
const cleaned = await cleanupOwnedProcessGroup({
runId: options.runId,
leaderPid: processGroupId,
diff --git a/src/application/ProcessHarness.ts b/src/application/ProcessHarness.ts
index 7cc48b33e..d16502f8b 100644
--- a/src/application/ProcessHarness.ts
+++ b/src/application/ProcessHarness.ts
@@ -25,6 +25,7 @@ import {
type CommandShimReplay,
} from "./CommandShimReplay.js";
import { normalizeProcessText } from "./ProcessNormalization.js";
+import { selectCapturedProcessGroupIds } from "./ProcessOwnership.js";
import {
awaitTerminalExit,
buildCaptureResult,
@@ -157,7 +158,8 @@ const cleanupFailedStartup = async (options: {
options.terminal?.kill("SIGKILL");
const cleanupFailure = await releaseProcessResources({
...options,
- sampledProcessGroupIds: [],
+ capturedProcessGroupIds:
+ options.terminal === undefined ? [] : [options.terminal.pid],
});
if (cleanupFailure !== undefined)
throw new ProcessCaptureError(cleanupFailure, {
@@ -319,9 +321,13 @@ const finishProcessRun = async (options: {
checkpoints: options.runtime?.checkpoints,
runId: options.runId,
temporaryRoot: options.temporaryRoot,
- sampledProcessGroupIds: options.samples.flatMap(({ process_group_id }) =>
- process_group_id === null ? [] : [process_group_id],
- ),
+ capturedProcessGroupIds:
+ options.runtime === undefined
+ ? []
+ : selectCapturedProcessGroupIds(
+ options.runtime.terminal.pid,
+ options.samples,
+ ),
});
let { capture, executionFailure } = options;
if (capture !== undefined) {
@@ -365,12 +371,7 @@ const completeCapture = async (options: {
if (reason === "cancelled") throw processCaptureCancelled();
const settlement = await observeSettlement(
options.runId,
- [
- runtime.terminal.pid,
- ...options.samples.flatMap(({ process_group_id }) =>
- process_group_id === null ? [] : [process_group_id],
- ),
- ],
+ selectCapturedProcessGroupIds(runtime.terminal.pid, options.samples),
scenario.settle_ms,
);
runtime.checkpoints.trigger("settled");
diff --git a/src/application/ProcessOwnership.ts b/src/application/ProcessOwnership.ts
index 9302d0d2f..d2c1e1e21 100644
--- a/src/application/ProcessOwnership.ts
+++ b/src/application/ProcessOwnership.ts
@@ -1,6 +1,7 @@
import { execFile } from "node:child_process";
import { readFile } from "node:fs/promises";
import { promisify } from "node:util";
+import type { ProcessSample } from "../domain/processCapture.js";
const execFileAsync = promisify(execFile);
@@ -40,6 +41,22 @@ export type ProcessGroupObservation =
| { readonly state: "alive" }
| { readonly state: "unverifiable"; readonly reason: string };
+/** Select the PTY root group and groups led by an observed captured process. */
+export const selectCapturedProcessGroupIds = (
+ rootPid: number,
+ samples: readonly Pick<ProcessSample, "pid" | "process_group_id">[],
+): readonly number[] => {
+ const processGroupIds = new Set<number>([rootPid]);
+ for (const sample of samples) {
+ // A sampled member may transiently inherit an unrelated group. POSIX group
+ // leaders have pid === pgid, so only that observation establishes that the
+ // group leader itself belonged to the captured tree. Token checks below
+ // remain the final authority immediately before observation or signaling.
+ if (sample.pid === sample.process_group_id) processGroupIds.add(sample.pid);
+ }
+ return [...processGroupIds];
+};
+
/** Parse the NUL-delimited Linux process environment without nameless keys. */
export const parseProcessEnvironment = (
value: string,
diff --git a/tests/processOwnership.test.ts b/tests/processOwnership.test.ts
index 09f0bd985..cae1559eb 100644
--- a/tests/processOwnership.test.ts
+++ b/tests/processOwnership.test.ts
@@ -2,6 +2,7 @@ import { describe, expect, it, vi } from "vitest";
import {
cleanupOwnedProcessGroup,
parseProcessEnvironment,
+ selectCapturedProcessGroupIds,
type ProcessOwnershipHost,
} from "../src/application/ProcessOwnership.js";
@@ -37,6 +38,18 @@ const host = (
};
describe("owned process-group cleanup", () => {
+ it("excludes sampled groups whose leader was outside the captured tree", () => {
+ expect(
+ selectCapturedProcessGroupIds(100, [
+ { pid: 100, process_group_id: 42 },
+ { pid: 100, process_group_id: 100 },
+ { pid: 101, process_group_id: 42 },
+ { pid: 102, process_group_id: 102 },
+ { pid: 103, process_group_id: 102 },
+ ]),
+ ).toEqual([100, 102]);
+ });
+
it("drops nameless Linux environment entries", () => {
expect(
parseProcessEnvironment("=ignored\0REA_PROCESS_RUN_ID=owned\0EMPTY=\0"),