chore: merge main into feat/dyld-shared-cache

This commit is contained in:
morluto
2026-10-08 17:02:53 +08:00
85 changed files with 5854 additions and 1121 deletions
+24
View File
@@ -0,0 +1,24 @@
#include <stdint.h>
#include <pthread.h>
#include <stdatomic.h>
#include <unistd.h>
static atomic_int ready;
static void *recorded_thread(void *unused) {
(void)unused;
__asm__ volatile("movabs $0xfedcba9876543210, %%r12" ::: "r12");
atomic_store(&ready, 1);
for (;;) pause();
return 0;
}
__attribute__((noinline)) static void fixture_crash(uint64_t value) {
volatile uint64_t *pointer = (volatile uint64_t *)(uintptr_t)0x123;
*pointer = value;
}
int main(void) {
pthread_t thread;
pthread_attr_t attr;
if (pthread_attr_init(&attr) || pthread_attr_setstacksize(&attr, 65536) || pthread_create(&thread, &attr, recorded_thread, 0)) return 1;
while (!atomic_load(&ready)) usleep(1000);
fixture_crash(UINT64_C(0x1122334455667788));
return 0;
}
+94
View File
@@ -0,0 +1,94 @@
"""Source-owned mutations built with the same unchanged upstream ELF/ABI structures."""
import ctypes
import io
import json
from pathlib import Path
import sys
from elftools.elf.elffile import ELFFile
from elftools.construct import Container
from pwnlib.elf.datatypes import elf_prstatus_amd64
source, directory, sentinel = Path(sys.argv[1]), Path(sys.argv[2]), int(sys.argv[3])
original = source.read_bytes()
image = ELFFile(io.BytesIO(original))
segments = list(image.iter_segments())
notes = [note for segment in segments if segment.header.p_type == "PT_NOTE" for note in segment.iter_notes()]
status = next(note for note in notes if note.n_name == "CORE" and note.n_type == "NT_PRSTATUS")
descriptor_start = lambda note: note.n_offset + image.structs.Elf_Nhdr.sizeof() + ((note.n_namesz + 3) // 4) * 4
files = {}
def save(name, content):
path = directory / (name + ".core")
path.write_bytes(content)
files[name] = str(path)
def note_header(content, note, **changes):
header = dict(note)
header.update(changes)
encoded = image.structs.Elf_Nhdr.build(Container(**header))
content[note.n_offset:note.n_offset + len(encoded)] = encoded
save("truncated-header", original[:32])
for name, osabi, version in [("linux-osabi", 3, 0), ("solaris-osabi", 6, 0), ("freebsd-osabi", 9, 0), ("unsupported-abi-version", 0, 1)]:
content = bytearray(original)
content[7:9] = bytes([osabi, version])
save(name, content)
content = bytearray(original)
section = Container(sh_name=0, sh_type="SHT_NULL", sh_flags=0, sh_addr=0, sh_offset=0, sh_size=0, sh_link=0, sh_info=len(segments), sh_addralign=0, sh_entsize=0)
content.extend(image.structs.Elf_Shdr.build(section))
header = Container(**dict(image.header, e_phnum=0xffff, e_shoff=len(original), e_shentsize=image.structs.Elf_Shdr.sizeof(), e_shnum=1, e_shstrndx=0))
content[:image.structs.Elf_Ehdr.sizeof()] = image.structs.Elf_Ehdr.build(header)
save("extended-program-count", content)
for index, segment in enumerate(segments):
if segment.header.p_type != "PT_NOTE":
continue
content = bytearray(original)
raw_notes = original[segment.header.p_offset:segment.header.p_offset + segment.header.p_filesz]
content.extend(raw_notes + b"\0\0\0")
header = Container(**dict(segment.header, p_offset=len(original), p_filesz=len(raw_notes) + 3))
start = image.header.e_phoff + index * image.header.e_phentsize
content[start:start + image.structs.Elf_Phdr.sizeof()] = image.structs.Elf_Phdr.build(header)
save("zero-note-tail", content)
content[-1] = 1
save("nonzero-note-tail", content)
break
content = bytearray(original)
note_header(content, status, n_descsz=1)
save("truncated-status", content)
content = bytearray(original)
note_header(content, status, n_namesz=len(original))
save("truncated-owner", content)
content = bytearray(original)
content[status.n_offset + image.structs.Elf_Nhdr.sizeof()] = 255
save("opaque-owner", content)
content = bytearray(original)
for note in notes:
if note.n_type == "NT_SIGINFO":
note_header(content, note, n_type=0x7ffeff)
save("unknown-signals", content)
content = bytearray(original)
for index, segment in enumerate(segments):
if segment.header.p_type == "PT_NOTE":
header = dict(segment.header, p_type="PT_NULL")
encoded = image.structs.Elf_Phdr.build(Container(**header))
start = image.header.e_phoff + index * image.header.e_phentsize
content[start:start + len(encoded)] = encoded
save("missing-notes", content)
content = bytearray(original)
start = descriptor_start(status) + elf_prstatus_amd64.pr_pid.offset
content[start:start + ctypes.sizeof(ctypes.c_uint32)] = (0xfffffffe).to_bytes(4, "little")
save("negative-pid", content)
content = bytearray(original)
content[start:start + ctypes.sizeof(ctypes.c_uint32)] = sentinel.to_bytes(4, "little")
for note in notes:
if note.n_name == "CORE" and note.n_type == "NT_PRPSINFO":
# Build with unchanged pyelftools; no hard-coded PID offset or host file resolution.
raw = dict(note.n_desc, pr_pid=sentinel)
encoded = image.structs.Elf_Prpsinfo.build(Container(**raw))
offset = descriptor_start(note)
content[offset:offset + len(encoded)] = encoded
save("historical-pid-collision", content)
print(json.dumps(files))
+2 -2
View File
@@ -1032,8 +1032,8 @@ try {
"invalid_input",
],
["undersized-program-header", undersizedProgramHeader, "invalid_input"],
["arm64", unsupported, "unsupported_provider"],
["core", core, "unsupported_provider"],
["arm64", unsupported, "unsupported_target"],
["core", core, "unsupported_target"],
]) {
const path = join(root.path, name);
await writeFile(path, bytes);
+318
View File
@@ -0,0 +1,318 @@
#!/usr/bin/env node
import assert from "node:assert/strict";
import { writeFile, readFile, copyFile } from "node:fs/promises";
import { isAbsolute, join } from "node:path";
import { fileURLToPath } from "node:url";
import { PrivateRuntimeRoot } from "../dist/process/PrivateRuntimeRoot.js";
import { createVerifierRun, completeVerifierRun } from "./lib/verifier-run.mjs";
import { verifyCompleteToolCatalog } from "./lib/verify-package-core.mjs";
import { connectRecordedCrash } from "./verify/recorded-crash/public.mjs";
import {
createRecordedCore,
fixtureCommand,
startCoreSentinel,
} from "./verify/recorded-crash/fixtures.mjs";
import { verifyCoreInspectionTrace } from "./verify/recorded-crash/trace.mjs";
const environment = Object.fromEntries(
Object.entries(process.env).filter(([, value]) => typeof value === "string"),
);
environment.REA_PWNDBG_GDB ??= "/usr/bin/gdb";
for (const key of [
"REA_PWNTOOLS_PYTHON",
"REA_PWNDBG_GDB",
"REA_PWNDBG_GDBINIT",
"REA_PWNDBG_VENV_PATH",
"REA_VERIFY_STRACE_COMMAND",
])
if (!isAbsolute(environment[key] ?? ""))
throw new Error(
`verify:recorded:crash requires absolute ${key}; see docs/recorded-crashes.md. No dependencies are installed by this verifier.`,
);
if (process.platform !== "linux" || process.arch !== "x64")
throw new Error("verify:recorded:crash requires Linux x64.");
const run = createVerifierRun();
environment.REA_PROCESS_RUN_ID = run.run_id;
const root = await PrivateRuntimeRoot.create({ prefix: "rea-core-verifier-" });
const options = { root: root.path, environment, runId: run.run_id };
const entrypoint =
process.argv[2] ?? fileURLToPath(new URL("./rea.mjs", import.meta.url));
const failures = [];
const sessions = [];
let sentinel;
let cases = 0;
let trace;
let catalogTools;
try {
for (const [command, args] of [
["gcc", ["--version"]],
[environment.REA_PWNDBG_GDB, ["--version"]],
[environment.REA_VERIFY_STRACE_COMMAND, ["-V"]],
]) {
try {
await fixtureCommand(command, args, options);
} catch (cause) {
throw new Error(
`verify:recorded:crash prerequisite unavailable: ${command}`,
{ cause },
);
}
}
const core = await createRecordedCore(options);
sentinel = await startCoreSentinel(options);
sentinel.assertAlive();
const fixtures = JSON.parse(
await fixtureCommand(
environment.REA_PWNTOOLS_PYTHON,
[
"-I",
fileURLToPath(
new URL("./lib/recorded-core-fixtures.py", import.meta.url),
),
core,
root.path,
String(sentinel.pid),
],
options,
),
);
const spaced = join(root.path, "recording with spaces.core");
await copyFile(core, spaced);
const basic = await connectRecordedCrash({ entrypoint, environment });
sessions.push(basic);
const advertised = (await basic.client.listTools()).tools;
assert.equal(
advertised.filter((tool) => tool.name === "inspect_recorded_crash").length,
1,
);
const catalog = await verifyCompleteToolCatalog(basic.client, {
timeout: 90_000,
});
for (const name of [
"inspect_binary_layout",
"inspect_recorded_crash",
"inspect_evm_interface",
])
assert.ok(catalog.includes(name));
catalogTools = catalog.length;
for (const mode of ["cli", "mcp"]) {
const report = await basic.inspect(mode, spaced);
assert.equal(report.threads.length, 2);
assert.ok(
report.threads.some((thread) =>
thread.registers.some(
(register) =>
register.name === "rdi" && register.value === "0x1122334455667788",
),
),
);
assert.ok(
report.threads.some((thread) =>
thread.registers.some(
(register) =>
register.name === "r12" && register.value === "0xfedcba9876543210",
),
),
);
assert.ok(
report.signals.some(
(signal) =>
signal.number === 11 &&
signal.code === 1 &&
signal.fault_address === "0x123",
),
);
assert.ok(
report.signals.some(
(signal) => signal.code === -6 && signal.fault_address === null,
),
);
cases++;
const enriched = await basic.inspect(mode, spaced, {
debuggerContext: true,
});
assert.deepEqual(enriched.threads, report.threads);
assert.deepEqual(enriched.notes, report.notes);
assert.ok(enriched.debugger.pwndbg_version.startsWith("2026.09.15"));
assert.equal(enriched.debugger.connection, "core");
assert.equal(enriched.debugger.executable, null);
assert.ok(enriched.debugger.maps.length > 0);
assert.ok(
enriched.debugger.maps.every(
(map) => map.reported_flags !== 0 || map.permissions === null,
),
);
assert.ok(
enriched.debugger.maps.every(
(map) => map.current_file_identity === "unknown",
),
);
assert.ok(
enriched.debugger.reported_limits.address_space_bytes <= 3 * 1024 ** 3,
);
assert.ok(enriched.debugger.reported_limits.cpu_seconds <= 30);
assert.ok(
enriched.debugger.reported_limits.file_size_bytes <= 64 * 1024 ** 2,
);
assert.ok(enriched.diagnostics.stdout.includes("[core decoder]"));
assert.equal(enriched.decoder_diagnostics.truncated, false);
cases++;
for (const name of [
"truncated-header",
"truncated-status",
"truncated-owner",
"nonzero-note-tail",
]) {
await basic.inspect(mode, fixtures[name], { category: "invalid_input" });
cases++;
}
const opaque = await basic.inspect(mode, fixtures["opaque-owner"]);
assert.ok(
opaque.notes.some(
(note) => Buffer.from(note.owner_bytes_base64, "base64")[0] === 255,
),
);
assert.equal(opaque.threads.length, 1);
cases++;
const noSignals = await basic.inspect(mode, fixtures["unknown-signals"]);
assert.equal(noSignals.signals.length, 0);
assert.equal(noSignals.threads.length, 2);
cases++;
const noNotes = await basic.inspect(mode, fixtures["missing-notes"]);
assert.equal(noNotes.notes.length, 0);
assert.equal(noNotes.threads.length, 0);
assert.equal(noNotes.note_interpretation_completeness, "unknown");
cases++;
const negative = await basic.inspect(mode, fixtures["negative-pid"]);
assert.ok(negative.threads.some((thread) => thread.historical_pid === -2));
cases++;
const linux = await basic.inspect(mode, fixtures["linux-osabi"]);
assert.deepEqual(linux.threads, report.threads);
assert.deepEqual(linux.signals, report.signals);
cases++;
const extended = await basic.inspect(
mode,
fixtures["extended-program-count"],
);
assert.deepEqual(extended.threads, report.threads);
assert.deepEqual(extended.signals, report.signals);
cases++;
const padded = await basic.inspect(mode, fixtures["zero-note-tail"]);
assert.equal(padded.threads.length, 2);
assert.ok(
padded.note_padding.some((padding) => padding.bytes_base64 === "AAAA"),
);
cases++;
for (const name of [
"solaris-osabi",
"freebsd-osabi",
"unsupported-abi-version",
]) {
await basic.inspect(mode, fixtures[name], {
category: "unsupported_target",
});
cases++;
}
const collision = await basic.inspect(
mode,
fixtures["historical-pid-collision"],
{ debuggerContext: true },
);
assert.ok(
collision.threads.some(
(thread) => thread.historical_pid === sentinel.pid,
),
);
sentinel.assertAlive();
cases++;
}
for (const [name, changes] of [
["missing", { REA_PWNDBG_GDBINIT: join(root.path, "missing-plugin.py") }],
[
"wrong-profile",
{ REA_PWNDBG_GDBINIT: join(root.path, "wrong-profile.py") },
],
]) {
if (name === "wrong-profile") {
// Source-owned failure injection changes only the in-memory reported version.
// The selected upstream checkout and its installed files remain unchanged.
await writeFile(
changes.REA_PWNDBG_GDBINIT,
`from pathlib import Path\nentry = ${JSON.stringify(environment.REA_PWNDBG_GDBINIT)}\nexec(compile(Path(entry).read_bytes(), entry, 'exec'), {'__name__': '__main__', '__file__': entry})\nimport pwndbg.lib.version\npwndbg.lib.version.__version__ = '0.0.0-source-owned'\n`,
);
}
const session = await connectRecordedCrash({
entrypoint,
environment: { ...environment, ...changes },
});
sessions.push(session);
for (const mode of ["cli", "mcp"]) {
const error = await session.inspect(mode, core, {
debuggerContext: true,
category: "unsupported_provider",
});
assert.ok(
JSON.stringify(error).includes(
name === "missing" ? "REA_PWNDBG_GDBINIT" : "source-owned",
),
);
cases++;
}
}
const executableCarrier = join(root.path, "executable-as-core");
const executableBytes = Buffer.from(await readFile(core));
executableBytes.writeUInt16LE(2, 16);
await writeFile(executableCarrier, executableBytes);
for (const mode of ["cli", "mcp"]) {
const error = await basic.inspect(mode, executableCarrier, {
category: "unsupported_target",
});
assert.ok(JSON.stringify(error).includes("ET_CORE"));
cases++;
}
trace = await verifyCoreInspectionTrace({
...options,
entrypoint,
core: fixtures["historical-pid-collision"],
sentinel,
});
cases += trace.length;
assert.ok((await readFile(core)).length > 0);
} catch (cause) {
failures.push(cause);
} finally {
for (const close of [
...sessions.map((session) => () => session.close()),
...(sentinel ? [() => sentinel.close()] : []),
() => root.close(),
]) {
try {
await close();
} catch (cause) {
failures.push(cause);
}
}
}
const verifier = await completeVerifierRun(run);
try {
assert.equal(verifier.process_lineage.status, "verified");
assert.deepEqual(verifier.process_lineage.descendants, []);
} catch (cause) {
failures.push(cause);
}
const result = {
status: failures.length === 0 ? "passed" : "failed",
public_cases: cases,
catalog_tools: catalogTools,
profile:
"Linux x64; pwntools4.15.0/pyelftools0.33/Unicorn2.1.2; core-only BYO GDB/pwndbg2026.09.15",
fixture_generation: "owned test program executed",
inspection_trace: trace,
verifier,
};
console.log(JSON.stringify(result, null, 2));
if (failures.length > 0)
throw new AggregateError(
failures,
"Recorded crash verification failed; cleanup failures are retained.",
);
+142
View File
@@ -0,0 +1,142 @@
import assert from "node:assert/strict";
import { fileURLToPath } from "node:url";
import { join } from "node:path";
import { runOwnedCommand } from "../../../dist/process/OwnedCommand.js";
import {
spawnOwnedProviderProcess,
ProviderProcessSupervisor,
} from "../../../dist/process/ProviderProcess.js";
import { cleanupOwnedProcessGroup } from "../../../dist/process/ProcessOwnership.js";
/** Execute fixture tooling under an owned deadline; fixture generation deliberately runs its own test program. */
export async function fixtureCommand(
command,
arguments_,
{ root, environment, runId },
) {
const result = await runOwnedCommand(
{
command,
arguments: arguments_,
cwd: root,
runId,
hostEnvironment: environment,
expectedCommand: null,
},
{ timeoutMs: 45_000, diagnosticBytes: 1024 * 1024 },
);
return result.stdout.text;
}
/** Build and record a source-owned two-thread crash without changing kernel core settings. */
export async function createRecordedCore(options) {
const executable = join(options.root, "fixture-program");
const core = join(options.root, "recording.core");
await fixtureCommand(
"gcc",
[
"-g",
"-O0",
"-pthread",
"-fno-pie",
"-no-pie",
fileURLToPath(
new URL("../../fixtures/recorded-crash.c", import.meta.url),
),
"-o",
executable,
],
options,
);
// Fixed relative arguments avoid putting selected filesystem paths in GDB command syntax.
await fixtureCommand(
options.environment.REA_PWNDBG_GDB,
[
"-nx",
"-nh",
"--batch",
"--quiet",
"-iex",
"set auto-load off",
"-iex",
"set debuginfod enabled off",
"-ex",
"set startup-with-shell off",
"-ex",
"set disable-randomization off",
"-ex",
"file ./fixture-program",
"-ex",
"run",
"-ex",
"generate-core-file ./recording.core",
],
options,
);
return core;
}
/** Retain authority over an unrelated live process used only as a historical-PID collision oracle. */
export async function startCoreSentinel(options) {
const launch = await spawnOwnedProviderProcess({
command: process.execPath,
arguments: [
"-e",
"process.stdout.write('ready\\n'); setInterval(() => {}, 1000)",
],
cwd: options.root,
hostEnvironment: options.environment,
runId: options.runId,
});
const supervisor = new ProviderProcessSupervisor({
...launch,
ownsProcessLifetime: true,
cleanup: () => cleanupOwnedProcessGroup(launch.ownership),
});
try {
await new Promise((resolve, reject) => {
const timer = setTimeout(
() => finish(new Error("Sentinel readiness timed out")),
10_000,
);
const onData = () => {
if (supervisor.snapshot().stdout.text.includes("ready\n")) finish();
};
const onExit = () =>
finish(new Error("Sentinel exited before readiness"));
function finish(error) {
clearTimeout(timer);
launch.process.stdout.off("data", onData);
launch.process.off("exit", onExit);
if (error) reject(error);
else resolve();
}
launch.process.stdout.on("data", onData);
launch.process.once("exit", onExit);
onData();
});
} catch (cause) {
try {
await supervisor.stop();
} finally {
supervisor.dispose();
}
throw cause;
}
return {
pid: launch.process.pid,
assertAlive() {
assert.equal(launch.process.exitCode, null);
assert.equal(launch.process.signalCode, null);
process.kill(launch.process.pid, 0);
},
async close() {
try {
const stopped = await supervisor.stop();
assert.notEqual(stopped.status, "incomplete", JSON.stringify(stopped));
} finally {
supervisor.dispose();
}
},
};
}
+133
View File
@@ -0,0 +1,133 @@
import assert from "node:assert/strict";
import { execFile } from "node:child_process";
import { createHash } from "node:crypto";
import { readFile } from "node:fs/promises";
import { promisify } from "node:util";
import { Client } from "@modelcontextprotocol/client";
import { StdioClientTransport } from "@modelcontextprotocol/client/stdio";
import { parseEvidence } from "../../../dist/domain/evidence.js";
import { recordedCrashSchema } from "../../../dist/domain/native/recordedCrash.js";
import { mcpTextValue } from "../../lib/mcp-verifier-results.mjs";
const execute = promisify(execFile);
/** Exercise the public installed CLI and SDK transport with the same selected environment. */
export async function connectRecordedCrash({ entrypoint, environment }) {
const client = new Client({ name: "recorded-crash-verifier", version: "1" });
const transport = new StdioClientTransport({
command: process.execPath,
args: [entrypoint, "mcp"],
env: environment,
stderr: "pipe",
});
try {
await client.connect(transport);
} catch (cause) {
await transport.close();
throw cause;
}
return {
client,
async inspect(mode, path, { debuggerContext = false, category } = {}) {
const original = await readFile(path);
let evidence;
if (mode === "mcp") {
const response = await client.callTool(
{
name: "inspect_recorded_crash",
arguments: { path, include_debugger_context: debuggerContext },
},
{ timeout: 90_000 },
);
const value = JSON.parse(mcpTextValue(response));
if (category !== undefined) {
assert.equal(response.isError, true);
assert.equal(value.error.category, category, JSON.stringify(value));
assert.deepEqual(await readFile(path), original);
return value.error;
}
assert.notEqual(response.isError, true, JSON.stringify(value));
evidence = parseEvidence(value.evidence);
assert.deepEqual(value.result, evidence.normalized_result);
} else {
assert.equal(mode, "cli");
let response;
try {
response = await execute(
process.execPath,
[
entrypoint,
"inspect-recorded-crash",
path,
...(debuggerContext ? ["--debugger-context"] : []),
"--json",
],
{ env: environment, timeout: 90_000, maxBuffer: 32 * 1024 * 1024 },
);
} catch (cause) {
if (category === undefined || typeof cause.code !== "number")
throw cause;
const error = JSON.parse(cause.stdout);
assert.equal(error.category, category, JSON.stringify(error));
assert.deepEqual(await readFile(path), original);
return error;
}
assert.equal(category, undefined, "Expected an explicit failure");
evidence = parseEvidence(JSON.parse(response.stdout));
}
const report = recordedCrashSchema.parse(evidence.normalized_result);
assert.equal(
evidence.confidence,
debuggerContext ? "derived" : "observed",
);
assert.deepEqual(evidence.raw_result, report);
assert.equal(evidence.subject.local_path, path);
assert.equal(report.artifact.path, path);
assert.equal(
report.artifact.sha256,
createHash("sha256").update(original).digest("hex"),
);
assert.equal(report.artifact.bytes, original.length);
assert.equal(
evidence.provider.version,
"pwntools@4.15.0;pyelftools@0.33;unicorn@2.1.2",
);
assert.equal(report.target_execution, "not-performed");
assert.equal(report.live_process_identity, "unknown");
assert.equal(report.diagnostics.truncated, false);
assert.equal(
report.debugger.status,
debuggerContext ? "available" : "not-requested",
);
for (const note of report.notes) {
for (const [encoded, range] of [
[note.owner_bytes_base64, note.owner_location],
[note.descriptor_bytes_base64, note.descriptor_location],
]) {
const start = Number(BigInt(range.offset));
assert.deepEqual(
Buffer.from(encoded, "base64"),
original.subarray(start, start + Number(BigInt(range.bytes))),
);
}
}
for (const thread of report.threads) {
assert.equal(thread.registers.length, 27);
for (const register of thread.registers)
assert.equal(
BigInt(register.value),
original.readBigUInt64LE(Number(BigInt(register.location.offset))),
);
}
assert.deepEqual(await readFile(path), original);
return report;
},
async close() {
try {
await client.close();
} finally {
await transport.close();
}
},
};
}
@@ -0,0 +1,11 @@
import { connectRecordedCrash } from "./public.mjs";
const session = await connectRecordedCrash({
entrypoint: process.argv[2],
environment: process.env,
});
try {
await session.inspect("mcp", process.argv[3], { debuggerContext: true });
} finally {
await session.close();
}
+173
View File
@@ -0,0 +1,173 @@
import assert from "node:assert/strict";
import { readdir, readFile, realpath } from "node:fs/promises";
import { fileURLToPath } from "node:url";
import { isAbsolute, join, resolve } from "node:path";
import { runOwnedCommand } from "../../../dist/process/OwnedCommand.js";
/** Check attempted syscalls, including failed attempts, on a recorded PID that collides with an owned live sentinel. */
export async function verifyCoreInspectionTrace({
root,
environment,
entrypoint,
core,
sentinel,
runId,
}) {
const gdb = await realpath(environment.REA_PWNDBG_GDB);
const python = await realpath(environment.REA_PWNTOOLS_PYTHON);
const plugin = resolve(environment.REA_PWNDBG_GDBINIT, "..");
const helperPaths = (name) =>
new Set([
join(environment.REA_PWNDBG_VENV_PATH, "bin", name),
...(environment.PATH ?? "")
.split(":")
.filter(isAbsolute)
.map((directory) => join(directory, name)),
]);
const psPaths = helperPaths("ps");
const iconvPaths = helperPaths("iconv");
const gitPaths = helperPaths("git");
const observations = [];
for (const mode of ["cli", "mcp"]) {
sentinel.assertAlive();
const prefix = join(root, `inspection-${mode}.trace`);
const invocation =
mode === "cli"
? [
entrypoint,
"inspect-recorded-crash",
core,
"--debugger-context",
"--json",
]
: [
fileURLToPath(new URL("./trace-client.mjs", import.meta.url)),
entrypoint,
core,
];
await runOwnedCommand(
{
command: environment.REA_VERIFY_STRACE_COMMAND,
arguments: [
"-ff",
"-qq",
"-s",
"8192",
"-e",
"trace=%process,%network,%file,ptrace,process_vm_readv,process_vm_writev",
"-o",
prefix,
process.execPath,
...invocation,
],
cwd: root,
hostEnvironment: environment,
expectedCommand: null,
runId,
},
{ timeoutMs: 90_000, diagnosticBytes: 1024 * 1024 },
);
const executions = [];
const ownershipReads = [];
let gdbObserved = false;
for (const file of await readdir(root)) {
if (!file.startsWith(`inspection-${mode}.trace.`)) continue;
const text = await readFile(join(root, file), "utf8");
const ownershipTrace = text
.split("\n")
.some(
(line) =>
/^execve\("\/(?:usr\/)?bin\/ps",/u.test(line) &&
line.includes(
'["ps", "-axo", "pid=,ppid=,pgid=,stat=,command="]',
) &&
line.endsWith(" = 0"),
);
const pidReads = text
.split("\n")
.filter((line) => line.includes(`/proc/${sentinel.pid}/`));
if (ownershipTrace) {
for (const line of pidReads) {
assert.ok(
new RegExp(
`/proc/${sentinel.pid}/(?:stat|status|cmdline|wchan|environ)"`,
"u",
).test(line),
`Unexpected ownership observation of the sentinel: ${line}`,
);
ownershipReads.push(line);
}
} else
assert.deepEqual(
pidReads,
[],
`Historical PID caused a provider lookup in ${file}: ${pidReads.join("\n")}`,
);
assert.ok(
!/\bptrace\(/u.test(text),
`Inspection attempted ptrace: ${file}`,
);
assert.ok(
!/\bprocess_vm_(?:readv|writev)\(/u.test(text),
`Inspection attempted process memory access: ${file}`,
);
assert.ok(
!/\b(?:socket|connect|sendto|sendmsg)\([^\n]*(?:AF_INET|AF_INET6)/u.test(
text,
),
`Inspection attempted an Internet socket: ${file}`,
);
for (const line of text.split("\n")) {
if (!/execve(?:at)?\(/u.test(line)) continue;
const match = /^execve\("([^"]+)"/u.exec(line);
assert.notEqual(match, null, `Unresolved executable identity: ${line}`);
assert.ok(
/ = (?:0|-1 [A-Z]+)/u.test(line),
`Incomplete execution observation: ${line}`,
);
const executable = await realpath(match[1]).catch(() => match[1]);
const ownership =
psPaths.has(match[1]) &&
line.includes('["ps", "-axo", "pid=,ppid=,pgid=,stat=,command="]');
const iconv =
iconvPaths.has(match[1]) && line.includes('["iconv", "-l"]');
const version =
gitPaths.has(match[1]) &&
line.includes(
"[" +
[
"git",
"--git-dir",
join(plugin, ".git"),
"rev-parse",
"--short",
"HEAD",
]
.map((argument) => JSON.stringify(argument))
.join(", ") +
"]",
);
assert.ok(
[process.execPath, python, gdb].includes(executable) ||
ownership ||
iconv ||
version,
`Unexpected attempted host execution: ${line}`,
);
if (executable === gdb) gdbObserved = true;
executions.push(executable);
}
}
assert.ok(gdbObserved, "Trace must include the actual core-only GDB stage");
assert.ok(executions.includes(python));
sentinel.assertAlive();
observations.push({
mode,
executions,
historical_pid: sentinel.pid,
sentinel: "still-alive",
ownership_process_table_reads: ownershipReads,
});
}
return observations;
}