Files
rea/src/pcap/PcapCommand.ts
T
morluto 44fedc8fdd fix(pcap): report a refused tool launch as an unavailable tool (#10553)
* fix(pcap): report a refused tool launch as an unavailable tool

When a capture read's tool launch was refused with EPERM, pcap reported it
as unreadable tool output (AnalysisProtocolError) instead of an unavailable
tool. Pcap also never told "not executable" apart from "missing".

Add classifyLaunchFailure in src/process as the one owner of which spawn
errnos mean missing (ENOENT, ENOTDIR) and which mean present but not
executable (EACCES, EPERM, ENOEXEC, EISDIR). Pcap, apktool and gdb used three
different errno lists; all three now use it.

* refactor(process): classify Java and adb launch refusals with the shared owner (#10598)

The Ghidra and JADX Java probes and adb's version probe now take
missing-versus-not-executable from classifyLaunchFailure instead of their
own errno checks. ELOOP and ENAMETOOLONG join the owner's missing set, so
adb keeps its existing classification and both Java probes now treat a
non-directory path component as a missing runtime.

* fix(pcap): preserve command launch attribution

* test(pcap): expect an unknown failing stage when a limited capture tool exits unsuccessfully

* fix(pcap): attribute a limited capture's exit through the prlimit wrapper

* fix(process): identify the refused executable for every launcher shape

Name the executable on errnos Node throws synchronously from spawn
(ENOEXEC, ENOTDIR, ...) and read the Windows native launcher's
executable-launch constraint, so pcap attributes those refusals to the
selected tool or limiter instead of an unknown stage.

* test(pcap): prove synchronous launch naming with ENOTDIR on every POSIX host
2026-11-14 21:11:59 -05:00

405 lines
14 KiB
TypeScript

import { analysisErrorWithCleanupFailure } from "../domain/analysisErrorCleanup.js";
import { ProviderCleanupError } from "../domain/providerCleanupError.js";
import {
assertArtifactCommandPath,
type ArtifactCommandPath,
} from "../process/ArtifactCommandPath.js";
import { ArtifactReaderFailure } from "../artifacts/ArtifactReader.js";
import { artifactFileCancellationCause } from "../artifacts/ArtifactFileOperation.js";
import { randomUUID } from "node:crypto";
import { fixedAllowanceOptions } from "../domain/fixedAllowanceRemediation.js";
import {
AnalysisError,
type AnalysisCapturedOutput,
} from "../domain/analysisErrorBase.js";
import { createPcapDiagnosticProjection } from "./PcapDiagnosticOutput.js";
import {
AnalysisCancelledError,
AnalysisCapabilityUnavailableError,
AnalysisProtocolError,
AnalysisResourceConstraintError,
AnalysisTimeoutError,
AnalysisUnsupportedTargetError,
} from "../domain/analysisErrorCore.js";
import {
OwnedCommandFailure,
ownedCommandResources,
prepareOwnedCommand,
runOwnedCommand,
} from "../process/OwnedCommand.js";
import type {
ProviderProcessSupervisor,
ProviderProcessSnapshot,
} from "../process/ProviderProcess.js";
import {
isWindowsBatchFile,
windowsBatchCommand,
WindowsBatchCommandError,
} from "../process/WindowsBatchCommand.js";
import {
classifyLaunchFailure,
executableLaunchRefusalOf,
} from "../process/launchFailure.js";
import { pcapToolUnavailable } from "./PcapConfiguration.js";
import {
attributePcapLimitedExit,
PCAP_LIMITER_REMEDIATION,
} from "./PcapNativeBound.js";
export type PcapCommandLauncher = NonNullable<
Parameters<typeof runOwnedCommand>[2]
>["launcher"];
export interface PcapCommandCleanup {
readonly reason: string;
readonly resources: readonly string[];
readonly capturedOutput: AnalysisCapturedOutput;
}
export interface PcapCommandOutput {
/** Lazy safe diagnostics; raw stdout remains owned by the successful parser. */
readonly capturedOutput: AnalysisCapturedOutput;
readonly stdout: string;
readonly stderr: string;
readonly invalidUtf8Streams: readonly ("stdout" | "stderr")[];
readonly cleanup?: PcapCommandCleanup;
}
export interface PcapCommandLimits {
readonly timeoutMs: number;
readonly maxOutputBytes: number;
}
/** Owned process execution shared by version probes and capture reads. */
export const runPcapCommand = async (options: {
readonly operation: string;
readonly command: string;
readonly arguments: readonly string[];
readonly limits: PcapCommandLimits;
readonly environment: Readonly<Record<string, string | undefined>>;
readonly signal: AbortSignal | undefined;
readonly inputPath?: string;
readonly redactHttpTransportCredentials?: boolean;
readonly nativeBound?: {
readonly limiter?: {
command: ArtifactCommandPath;
arguments: readonly string[];
};
readonly memoryLimitBytes?: number;
};
readonly launcher?: PcapCommandLauncher;
readonly retain: (
owner: ProviderProcessSupervisor,
resources: readonly string[],
reason: string,
capturedOutput: PcapCommandCleanup["capturedOutput"],
) => void;
}): Promise<PcapCommandOutput> => {
const { operation, command, limits, signal } = options;
const projectDiagnostic = createPcapDiagnosticProjection(
options.redactHttpTransportCredentials === true,
);
const captureDiagnostic = (
snapshot: ProviderProcessSnapshot,
): AnalysisCapturedOutput =>
projectDiagnostic({
stdout: snapshot.stdout.text,
stderr: snapshot.stderr.text,
stdout_bytes: snapshot.stdout.bytes,
stderr_bytes: snapshot.stderr.bytes,
exit_code: snapshot.exitCode ?? null,
signal: snapshot.signal ?? null,
truncated:
snapshot.diagnosticTruncated === true ||
snapshot.outputIncomplete === true,
});
if (signal?.aborted)
throw new AnalysisCancelledError(operation, { cause: signal.reason });
const hostEnvironment = {
...options.environment,
LC_ALL: "C",
LANG: "C",
};
let cleanup: PcapCommandCleanup | undefined;
let commandAttempted = false;
try {
const batch = process.platform === "win32" && isWindowsBatchFile(command);
const invocation = batch
? windowsBatchCommand({
environment: hostEnvironment,
batchPath: command,
arguments: options.arguments,
})
: { command, arguments: options.arguments };
const boundedInvocation =
options.nativeBound?.limiter === undefined
? invocation
: {
command: options.nativeBound.limiter.command.invocationPath,
arguments: [
...options.nativeBound.limiter.arguments,
invocation.command,
...invocation.arguments,
],
};
// Native ownership preparation has its own bounded compiler lifecycle;
// do not spend the tool's execution deadline rebuilding a cold SDK cache.
await prepareOwnedCommand(signal);
const selectedLimiter = options.nativeBound?.limiter?.command;
if (selectedLimiter !== undefined)
await assertArtifactCommandPath(selectedLimiter, operation, { signal });
commandAttempted = true;
const output = await runOwnedCommand(
{
...boundedInvocation,
runId: randomUUID(),
hostEnvironment,
windowsVerbatimArguments: batch,
expectedCommand: null,
...(options.nativeBound?.memoryLimitBytes === undefined
? {}
: { memoryLimitBytes: options.nativeBound.memoryLimitBytes }),
},
{ timeoutMs: limits.timeoutMs, diagnosticBytes: limits.maxOutputBytes },
{
...(signal === undefined ? {} : { signal }),
...(options.launcher === undefined
? {}
: { launcher: options.launcher }),
retainCleanup: (owner, stopped) => {
const resources =
owner.launch.ownership === undefined
? ["owned-process"]
: ownedCommandResources(owner.launch.ownership);
const snapshot = owner.snapshot();
const capturedOutput = captureDiagnostic(snapshot);
options.retain(owner, resources, stopped.reason, capturedOutput);
cleanup = { reason: stopped.reason, resources, capturedOutput };
},
},
);
if (selectedLimiter !== undefined)
await assertArtifactCommandPath(selectedLimiter, operation, {
get capturedOutput() {
return captureDiagnostic(output);
},
});
return {
get capturedOutput() {
return captureDiagnostic(output);
},
stdout: output.stdout.text,
stderr: output.stderr.text,
invalidUtf8Streams: [
...(output.stdout.invalidUtf8 === true ? (["stdout"] as const) : []),
...(output.stderr.invalidUtf8 === true ? (["stderr"] as const) : []),
],
...(cleanup === undefined ? {} : { cleanup }),
};
} catch (cause) {
if (cause instanceof AnalysisError) {
if (cleanup !== undefined) {
const pending = new ProviderCleanupError(
"pcap",
cleanup.resources,
{
reason: cleanup.reason,
},
{
operation,
capturedOutput: cleanup.capturedOutput,
cleanup: { reason: cleanup.reason, resources: cleanup.resources },
},
);
throw analysisErrorWithCleanupFailure(cause, pending, operation);
}
throw cause;
}
if (cause instanceof ArtifactReaderFailure && cause.reason === "cancelled")
throw new AnalysisCancelledError(operation, {
cause: artifactFileCancellationCause(cause),
});
if (
cause instanceof OwnedCommandFailure &&
cause.cleanupOwner !== undefined
)
options.retain(
cause.cleanupOwner,
cause.resources,
cause.cleanupFailure ?? cause.message,
cause.snapshot === null
? { stdout: "", stderr: "", truncated: true }
: captureDiagnostic(cause.snapshot),
);
const details = {
cause,
...(cause instanceof OwnedCommandFailure && cause.snapshot !== null
? {
capturedOutput: captureDiagnostic(cause.snapshot),
}
: {}),
...(cleanup !== undefined
? { cleanup }
: cause instanceof OwnedCommandFailure && cause.cleanupFailure !== null
? {
cleanup: {
reason: cause.cleanupFailure,
resources: cause.resources,
},
}
: {}),
};
if (cause instanceof OwnedCommandFailure && cause.reason === "cancelled")
throw new AnalysisCancelledError(operation, {
...details,
cause: cause.cancellation?.reason,
});
if (
cause instanceof OwnedCommandFailure &&
cause.snapshot?.memoryLimitReached === true
)
throw new AnalysisResourceConstraintError(
operation,
"memory",
"The owned Windows tool tree reached its enforced committed-memory limit.",
{
process_memory_limit_bytes:
options.nativeBound?.memoryLimitBytes ?? null,
},
details,
);
if (
cause instanceof OwnedCommandFailure &&
(cause.snapshot?.signal === "SIGXCPU" ||
cause.snapshot?.signal === "SIGXFSZ")
)
throw new AnalysisResourceConstraintError(
operation,
cause.snapshot.signal === "SIGXCPU" ? "cpu" : "file-size",
`Producer terminated with ${cause.snapshot.signal}; its exact cause is not independently established.`,
null,
details,
);
if (cause instanceof OwnedCommandFailure && cause.reason === "timeout")
throw new AnalysisTimeoutError(operation, limits.timeoutMs, details);
if (cause instanceof OwnedCommandFailure && cause.reason === "output-limit")
throw new AnalysisResourceConstraintError(
operation,
"memory",
"Tool output exceeded its complete-output budget",
{ max_output_bytes: limits.maxOutputBytes },
{
...details,
...fixedAllowanceOptions(
"The packet-tool output capture allowance",
"capture",
),
},
);
if (cause instanceof WindowsBatchCommandError)
throw pcapToolUnavailable(
operation,
`Cannot run selected tool ${command}: ${cause.message}`,
details,
);
const selectedLimiter =
options.nativeBound?.limiter?.command.invocationPath;
const launchedPath = selectedLimiter ?? command;
const launchRefusal = executableLaunchRefusalOf(cause);
const spawnRefusal =
launchRefusal?.executable === launchedPath ? launchRefusal : undefined;
if (spawnRefusal !== undefined && selectedLimiter !== undefined) {
const reason = `Selected capture limiter ${selectedLimiter} could not be launched (${spawnRefusal.code}).`;
throw new AnalysisCapabilityUnavailableError("pcap", operation, reason, {
...details,
userMessage: `${reason} ${PCAP_LIMITER_REMEDIATION}`,
remediationAction: PCAP_LIMITER_REMEDIATION,
});
}
if (spawnRefusal !== undefined)
throw pcapToolUnavailable(
operation,
spawnRefusal.kind === "missing"
? `Selected tool ${command} was not found (${spawnRefusal.code})`
: `Selected tool ${command} exists but could not be executed (${spawnRefusal.code})`,
details,
);
const diagnostic =
cause instanceof OwnedCommandFailure
? cause.snapshot?.stderr.text.trim() || cause.message
: cause instanceof Error
? cause.message
: String(cause);
if (!commandAttempted)
throw new AnalysisProtocolError(
`Preparing the owned packet command failed: ${diagnostic}`,
details,
);
if (classifyLaunchFailure(cause) !== undefined)
throw new AnalysisProtocolError(
`The packet command could not start; the failing stage is unknown: ${diagnostic}`,
details,
);
if (
selectedLimiter !== undefined &&
cause instanceof OwnedCommandFailure &&
cause.reason === "process"
) {
if (cause.collectionFailure || cause.snapshot === null)
throw new AnalysisProtocolError(
`The bounded packet command failed before its exit was observed; whether the limiter or selected tool failed is unknown: ${diagnostic}`,
details,
);
const attributed = attributePcapLimitedExit(selectedLimiter, {
exitCode: cause.snapshot.exitCode,
signal: cause.snapshot.signal,
stderr: cause.snapshot.stderr.text,
});
if (attributed.stage === "limiter") {
const reason = `Selected capture limiter ${selectedLimiter} failed before starting ${command}: ${diagnostic}`;
throw new AnalysisCapabilityUnavailableError(
"pcap",
operation,
reason,
{
...details,
userMessage: `${reason} ${PCAP_LIMITER_REMEDIATION}`,
remediationAction: PCAP_LIMITER_REMEDIATION,
},
);
}
if (attributed.stage === "tool-launch")
throw pcapToolUnavailable(
operation,
attributed.refusal === "missing"
? `Selected tool ${command} was not found: ${diagnostic}`
: `Selected tool ${command} exists but could not be executed: ${diagnostic}`,
details,
);
// Otherwise prlimit started the tool; its exit and output are the tool's.
}
if (options.inputPath === undefined)
throw pcapToolUnavailable(
operation,
`The selected tool ${command} cannot report its version: ${diagnostic}`,
details,
);
if (
options.inputPath !== undefined &&
/not a capture file|file.*isn't a capture file|file format.*not supported/iu.test(
diagnostic,
)
)
throw new AnalysisUnsupportedTargetError(
operation,
options.inputPath,
diagnostic,
details,
);
throw new AnalysisProtocolError(
`Packet capture tool ${command} failed: ${diagnostic}`,
details,
);
}
};