Skip to content

armgit / rea

Public Imported from GitHub
0

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

fix(pwntools): preserve worker ownership across launcher exec

Ysopwnable781d5751ad18
diff --git a/src/native/pwntools/PwntoolsDecoder.ts b/src/native/pwntools/PwntoolsDecoder.ts
index 6aa9a3a96..1f407d147 100644
--- a/src/native/pwntools/PwntoolsDecoder.ts
+++ b/src/native/pwntools/PwntoolsDecoder.ts
@@ -299,9 +299,6 @@ export class PwntoolsDecoder<
requestPath,
],
cwd: root.path,
- // Caller-selected Python launchers can exec through prlimit or a
- // shell wrapper. Keep parent/group/run-token ownership across exec.
- expectedCommand: null,
runId: `rea-artifact-decoder-${randomUUID()}`,
hostEnvironment: {
...this.environment,
@@ -478,7 +475,13 @@ export class PwntoolsDecoder<
try {
outcome = ok(
await runOwnedCommand(
- spawn,
+ {
+ ...spawn,
+ // Every decoder stage can replace its launcher through exec,
+ // including the optional Python-to-GDB handoff. Preserve explicit
+ // pins; otherwise keep parent/group/run-token proof across exec.
+ expectedCommand: spawn.expectedCommand ?? null,
+ },
{
timeoutMs: PWNTOOLS_LIMITS.timeoutMs,
diagnosticBytes: PWNTOOLS_LIMITS.diagnosticBytes,
diff --git a/tests/boundary/binaryDiagnostics/recordedCrashProvider.test.ts b/tests/boundary/binaryDiagnostics/recordedCrashProvider.test.ts
index 16c355789..d80277226 100644
--- a/tests/boundary/binaryDiagnostics/recordedCrashProvider.test.ts
+++ b/tests/boundary/binaryDiagnostics/recordedCrashProvider.test.ts
@@ -4,6 +4,7 @@ import type {
RecordedCrashArchitecture,
} from "../../../src/domain/native/recordedCrash.js";
import { access, mkdir, readFile, writeFile } from "node:fs/promises";
+import { createHash } from "node:crypto";
import { join } from "node:path";
import { expect, it, onTestFinished } from "vitest";
import { z } from "zod";
@@ -15,10 +16,14 @@ import { PwntoolsRecordedCrashProvider } from "../../../src/native/pwntools/Pwnt
import { PWNTOOLS_PROVIDER_IDENTITY } from "../../../src/native/pwntools/PwntoolsRelease.js";
import {
spawnOwnedProviderProcess,
+ type OwnedProviderProcessSpawnOptions,
type SpawnedOwnedProviderProcess,
} from "../../../src/process/ProviderProcess.js";
import { cleanupOwnedProcessGroup } from "../../../src/process/ProcessOwnership.js";
-import { waitForProviderProcessReady } from "../../fixtures/providerProcess.js";
+import {
+ stopProviderProcessFixture,
+ waitForProviderProcessReady,
+} from "../../fixtures/providerProcess.js";
import {
recordedCrashFixture,
recordedCrashDebuggerFixture,
@@ -47,6 +52,49 @@ const withCleanupGate = (
},
});
+const spawnDebuggerFixture = (
+ spawn: OwnedProviderProcessSpawnOptions,
+ scenario: string,
+): Promise<SpawnedOwnedProviderProcess> => {
+ const script =
+ scenario === "process-failure"
+ ? "process.stderr.write('debugger failed',()=>{process.exitCode=9;})"
+ : scenario.startsWith("cancelled")
+ ? "process.stdout.write('ready\\n'); setInterval(() => {}, 1000)"
+ : "process.stdout.write('debugger stage diagnostic')";
+ const exec = scenario.startsWith("cancelled-exec");
+ return spawnOwnedProviderProcess({
+ ...spawn,
+ command: exec ? "/bin/sh" : process.execPath,
+ arguments: exec
+ ? [
+ "-c",
+ scenario === "cancelled-exec-foreign"
+ ? 'REA_PROCESS_RUN_ID=source-owned-foreign exec "$1" -e "$2"'
+ : 'exec "$1" -e "$2"',
+ "recorded-debugger-launcher",
+ process.execPath,
+ script,
+ ]
+ : ["-e", script],
+ });
+};
+
+const expectForeignDebuggerPreserved = async (
+ child: SpawnedOwnedProviderProcess["process"] | undefined,
+): Promise<void> => {
+ if (child === undefined || child.pid === undefined)
+ throw new Error("Expected an owned fixture PID");
+ const environment = await readFile(`/proc/${child.pid}/environ`);
+ expect(
+ environment.includes(
+ Buffer.from("REA_PROCESS_RUN_ID=source-owned-foreign\0"),
+ ),
+ ).toBe(true);
+ expect(child.exitCode).toBeNull();
+ expect(child.signalCode).toBeNull();
+};
+
it
.runIf(host)
.each([
@@ -157,6 +205,8 @@ it
"process-failure",
"invalid-context",
"cancelled",
+ "cancelled-exec",
+ "cancelled-exec-foreign",
"cleanup-failure",
"decoder-cleanup-failure",
] as const)(
@@ -168,6 +218,7 @@ it
let snapshotPath = "";
let cleanupAllowed = false;
let launches = 0;
+ let debuggerProcess: SpawnedOwnedProviderProcess["process"] | undefined;
const venv = join(root, "venv");
const plugin = join(root, "gdbinit.py");
await mkdir(venv);
@@ -224,19 +275,9 @@ it
request.reply_path,
JSON.stringify({ ok: true, value: context }),
);
- const launched = await spawnOwnedProviderProcess({
- ...spawn,
- command: process.execPath,
- arguments: [
- "-e",
- scenario === "process-failure"
- ? "process.stderr.write('debugger failed',()=>{process.exitCode=9;})"
- : scenario === "cancelled"
- ? "process.stdout.write('ready\\n'); setInterval(() => {}, 1000)"
- : "process.stdout.write('debugger stage diagnostic')",
- ],
- });
- if (scenario === "cancelled") {
+ const launched = await spawnDebuggerFixture(spawn, scenario);
+ debuggerProcess = launched.process;
+ if (scenario.startsWith("cancelled")) {
await waitForProviderProcessReady(launched.process);
setImmediate(() => controller.abort());
}
@@ -247,6 +288,10 @@ it
);
onTestFinished(async () => {
cleanupAllowed = true;
+ // The test retains its creation handle even when the provider must refuse
+ // an exec'd worker with a foreign token, or a regression leaves it live.
+ if (debuggerProcess !== undefined)
+ await stopProviderProcessFixture(debuggerProcess);
await provider.close();
});
const result = await provider.inspect(
@@ -265,12 +310,23 @@ it
});
} else {
if (result.ok) throw new Error("Expected optional debugger failure");
- expectOptionalContextFailure(scenario, result.error);
+ expectOptionalContextFailure(scenario, result.error, value);
+ if (scenario === "cancelled-exec-foreign")
+ await expectForeignDebuggerPreserved(debuggerProcess);
+ }
+ if (scenario === "cancelled" || scenario === "cancelled-exec") {
+ if (debuggerProcess === undefined)
+ throw new Error("Expected an owned debugger process");
+ expect(
+ debuggerProcess.exitCode !== null ||
+ debuggerProcess.signalCode !== null,
+ ).toBe(true);
}
expect(await readFile(path)).toEqual(bytes);
if (
scenario === "cleanup-failure" ||
- scenario === "decoder-cleanup-failure"
+ scenario === "decoder-cleanup-failure" ||
+ scenario === "cancelled-exec-foreign"
) {
await expect(access(owned)).resolves.toBeUndefined();
expect(await readFile(snapshotPath)).toEqual(bytes);
@@ -287,6 +343,11 @@ it
});
await expect(access(owned)).resolves.toBeUndefined();
cleanupAllowed = true;
+ if (
+ scenario === "cancelled-exec-foreign" &&
+ debuggerProcess !== undefined
+ )
+ await stopProviderProcessFixture(debuggerProcess);
await provider.close();
}
await expect(access(owned)).rejects.toMatchObject({ code: "ENOENT" });
@@ -296,8 +357,11 @@ it
const expectOptionalContextFailure = (
scenario: string,
error: AnalysisError,
+ value: RecordedCrash,
) => {
const projected = projectAnalysisError(error);
+ if (scenario === "cancelled-exec-foreign")
+ expect(projected.code).toBe("cleanup_incomplete");
if (scenario === "unconfigured")
expect(projected).toMatchObject({
details: { captured_output: { stdout: "basic stage diagnostic" } },
@@ -311,18 +375,29 @@ const expectOptionalContextFailure = (
});
else if (
scenario === "invalid-context" ||
- scenario === "cancelled" ||
+ scenario.startsWith("cancelled") ||
scenario === "cleanup-failure"
) {
const serialized = JSON.stringify(projected);
expect(serialized).toContain("basic stage diagnostic");
expect(serialized).toContain(
- scenario === "cancelled" ? "ready" : "debugger stage diagnostic",
+ scenario.startsWith("cancelled") ? "ready" : "debugger stage diagnostic",
);
+ expect(error.partialObservation).toMatchObject({
+ artifact: {
+ ...value.artifact,
+ sha256: createHash("sha256")
+ .update(recordedCrashFixtureBytes(value))
+ .digest("hex"),
+ },
+ threads: value.threads,
+ notes: value.notes,
+ });
expect(error._tag).toBe(
scenario === "invalid-context"
? "AnalysisOutputError"
- : scenario === "cancelled"
+ : scenario.startsWith("cancelled") &&
+ scenario !== "cancelled-exec-foreign"
? "AnalysisCancelledError"
: "ProviderAdapterError",
);