feat(ghidra): add authenticated Windows loopback transport
Keep private Unix sockets on supported POSIX hosts and add a Windows P0 transport that binds the Java bridge to IPv4 loopback on an ephemeral port. Publish only a strict, token-free endpoint record through an atomic rename, validate it before connecting, and retain the same authenticated request protocol, bounded queue, handshake, cancellation, and shutdown semantics across both transports.
This commit is contained in:
+29
-11
@@ -1,6 +1,6 @@
|
||||
import { randomBytes, randomUUID } from "node:crypto";
|
||||
import { access } from "node:fs/promises";
|
||||
import type { Socket } from "node:net";
|
||||
import { tmpdir } from "node:os";
|
||||
import { join } from "node:path";
|
||||
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
@@ -46,9 +46,13 @@ import {
|
||||
attachGhidraSocket,
|
||||
connectGhidraSocketOnce,
|
||||
} from "./GhidraSocketConnection.js";
|
||||
import {
|
||||
observeGhidraEndpoint,
|
||||
type GhidraEndpoint,
|
||||
} from "./GhidraTransport.js";
|
||||
|
||||
const SHUTDOWN_TIMEOUT_MS = 10_000;
|
||||
const SESSION_ROOT = "/tmp";
|
||||
const SESSION_ROOT = tmpdir();
|
||||
|
||||
export type {
|
||||
GhidraClientOptions,
|
||||
@@ -65,7 +69,10 @@ export type GhidraOperation =
|
||||
/** Owns one authenticated, private, read-only Ghidra headless session. */
|
||||
export class GhidraClient {
|
||||
readonly #options: Required<
|
||||
Pick<GhidraClientOptions, "requestTimeoutMs" | "startupTimeoutMs">
|
||||
Pick<
|
||||
GhidraClientOptions,
|
||||
"requestTimeoutMs" | "startupTimeoutMs" | "transport"
|
||||
>
|
||||
> &
|
||||
GhidraClientOptions;
|
||||
readonly #pending = new PendingOperations<
|
||||
@@ -120,6 +127,7 @@ export class GhidraClient {
|
||||
...options,
|
||||
requestTimeoutMs: options.requestTimeoutMs ?? GHIDRA_REQUEST_TIMEOUT_MS,
|
||||
startupTimeoutMs: options.startupTimeoutMs ?? GHIDRA_STARTUP_TIMEOUT_MS,
|
||||
transport: options.transport ?? "unix-socket",
|
||||
};
|
||||
this.#requestQueue = new GhidraRequestQueue(
|
||||
GHIDRA_MAX_QUEUED_REQUESTS,
|
||||
@@ -233,7 +241,15 @@ export class GhidraClient {
|
||||
);
|
||||
}
|
||||
if (deadline.signal.aborted) return this.#startupInterrupted(deadline);
|
||||
const socketPath = join(this.#runtimeRoot.path, "bridge.sock");
|
||||
const endpoint: GhidraEndpoint = {
|
||||
transport: this.#options.transport,
|
||||
path: join(
|
||||
this.#runtimeRoot.path,
|
||||
this.#options.transport === "unix-socket"
|
||||
? "bridge.sock"
|
||||
: "bridge-endpoint.json",
|
||||
),
|
||||
};
|
||||
try {
|
||||
const snapshot = await createGhidraTargetSnapshot(
|
||||
this.#options.targetPath,
|
||||
@@ -257,7 +273,8 @@ export class GhidraClient {
|
||||
.launch(
|
||||
{
|
||||
runtimeRoot: this.#runtimeRoot.path,
|
||||
socketPath,
|
||||
transport: endpoint.transport,
|
||||
endpointPath: endpoint.path,
|
||||
token: this.#token,
|
||||
runId: this.#runId,
|
||||
targetPath: this.#snapshotPath,
|
||||
@@ -283,7 +300,7 @@ export class GhidraClient {
|
||||
this.#process = new ProviderProcessSupervisor(launched.value, {
|
||||
onDiagnostic: (event) => this.#onProcessDiagnostic(event),
|
||||
});
|
||||
const connected = await this.#connect(socketPath, deadline);
|
||||
const connected = await this.#connect(endpoint, deadline);
|
||||
if (!connected.ok) {
|
||||
const failure = connected.error;
|
||||
await this.#cleanup();
|
||||
@@ -319,7 +336,7 @@ export class GhidraClient {
|
||||
}
|
||||
|
||||
async #connect(
|
||||
socketPath: string,
|
||||
endpoint: GhidraEndpoint,
|
||||
deadline: ProviderStartupDeadline,
|
||||
): Promise<Result<undefined, GhidraSessionError>> {
|
||||
while (deadline.remainingMs() > 0) {
|
||||
@@ -331,15 +348,15 @@ export class GhidraClient {
|
||||
return err(
|
||||
this.#failure("process", "Ghidra exited before bridge startup"),
|
||||
);
|
||||
try {
|
||||
await access(socketPath);
|
||||
} catch {
|
||||
const observed = await observeGhidraEndpoint(endpoint);
|
||||
if (!observed.ok) return observed;
|
||||
if (observed.value === null) {
|
||||
if ((await deadline.wait(50)) === "aborted")
|
||||
return err(this.#interruptionFailure(deadline));
|
||||
continue;
|
||||
}
|
||||
const connected = await connectGhidraSocketOnce(
|
||||
socketPath,
|
||||
observed.value,
|
||||
deadline.signal,
|
||||
);
|
||||
if (connected.ok) {
|
||||
@@ -565,6 +582,7 @@ export class GhidraClient {
|
||||
return createGhidraDiagnostics({
|
||||
targetPath: this.#options.targetPath,
|
||||
targetSha256: this.#options.targetSha256,
|
||||
transport: this.#options.transport,
|
||||
providerVersion: this.#options.providerVersion,
|
||||
profileDigest: this.#options.profileDigest,
|
||||
...(this.#runtimeRoot === undefined
|
||||
|
||||
@@ -3,6 +3,7 @@ import type { Result } from "../domain/result.js";
|
||||
import type { GhidraLauncher } from "./GhidraLauncher.js";
|
||||
import type { GhidraSessionError } from "./GhidraSessionError.js";
|
||||
import type { GhidraSessionInfo } from "./GhidraSessionValues.js";
|
||||
import type { GhidraTransportKind } from "./GhidraTransport.js";
|
||||
|
||||
/** Result of opening and authenticating one headless Ghidra session. */
|
||||
export type GhidraStartResult = Result<GhidraSessionInfo, GhidraSessionError>;
|
||||
@@ -34,6 +35,7 @@ export interface GhidraClientOptions {
|
||||
readonly launcher: GhidraLauncher;
|
||||
readonly targetPath: string;
|
||||
readonly targetSha256: string;
|
||||
readonly transport?: GhidraTransportKind;
|
||||
readonly providerVersion: string;
|
||||
readonly profileDigest: string;
|
||||
readonly requestTimeoutMs?: number;
|
||||
|
||||
@@ -1,11 +1,13 @@
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import type { ProviderProcessSnapshot } from "../process/ProviderProcess.js";
|
||||
import type { GhidraLaunch } from "./GhidraLauncher.js";
|
||||
import type { GhidraTransportKind } from "./GhidraTransport.js";
|
||||
|
||||
/** Inputs needed to project bounded diagnostics for one Ghidra runtime. */
|
||||
export interface GhidraDiagnosticsOptions {
|
||||
readonly targetPath: string;
|
||||
readonly targetSha256: string;
|
||||
readonly transport: GhidraTransportKind;
|
||||
readonly providerVersion: string;
|
||||
readonly profileDigest: string;
|
||||
readonly runtimeRoot?: string;
|
||||
@@ -32,13 +34,18 @@ export const createGhidraDiagnostics = (
|
||||
return {
|
||||
target_path: options.targetPath,
|
||||
target_sha256: options.targetSha256,
|
||||
transport: options.transport,
|
||||
provider_version: options.providerVersion,
|
||||
profile_digest: options.profileDigest,
|
||||
...(options.runtimeRoot === undefined
|
||||
? {}
|
||||
: {
|
||||
runtime_root: options.runtimeRoot,
|
||||
socket_path: `${options.runtimeRoot}/bridge.sock`,
|
||||
endpoint_path: `${options.runtimeRoot}/${
|
||||
options.transport === "unix-socket"
|
||||
? "bridge.sock"
|
||||
: "bridge-endpoint.json"
|
||||
}`,
|
||||
}),
|
||||
...(options.launch === undefined
|
||||
? {}
|
||||
|
||||
@@ -17,11 +17,13 @@ import {
|
||||
GHIDRA_MAX_HEAP,
|
||||
} from "./GhidraDefaults.js";
|
||||
import { ghidraJavaEnvironment } from "./GhidraInstallation.js";
|
||||
import type { GhidraTransportKind } from "./GhidraTransport.js";
|
||||
|
||||
/** Private paths and identity material for one headless Ghidra import. */
|
||||
export interface GhidraLaunchSession {
|
||||
readonly runtimeRoot: string;
|
||||
readonly socketPath: string;
|
||||
readonly transport: GhidraTransportKind;
|
||||
readonly endpointPath: string;
|
||||
readonly token: string;
|
||||
readonly runId: string;
|
||||
readonly targetPath: string;
|
||||
@@ -86,8 +88,9 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
|
||||
await writeFileAtomic(
|
||||
paths.descriptorPath,
|
||||
`${JSON.stringify({
|
||||
schema_version: 1,
|
||||
socket_path: session.socketPath,
|
||||
schema_version: 2,
|
||||
transport: session.transport,
|
||||
endpoint_path: session.endpointPath,
|
||||
token: session.token,
|
||||
run_id: session.runId,
|
||||
target_sha256: session.targetSha256,
|
||||
|
||||
@@ -357,6 +357,10 @@ export class GhidraProvider implements AnalysisProviderCandidate {
|
||||
}),
|
||||
targetPath: target.path,
|
||||
targetSha256: target.sha256,
|
||||
transport:
|
||||
installation.platform === "win32"
|
||||
? "authenticated-loopback-tcp"
|
||||
: "unix-socket",
|
||||
providerVersion: prerequisites.value.providerVersion,
|
||||
profileDigest: committedProfile.digest,
|
||||
logger: this.logger.child({ layer: "ghidra-bridge" }),
|
||||
|
||||
@@ -2,6 +2,7 @@ import { createConnection, type Socket } from "node:net";
|
||||
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import { GhidraSessionError } from "./GhidraSessionError.js";
|
||||
import type { GhidraConnectTarget } from "./GhidraTransport.js";
|
||||
|
||||
/** Stable listeners attached for the lifetime of one bridge socket. */
|
||||
export interface GhidraSocketHandlers {
|
||||
@@ -26,13 +27,16 @@ export const attachGhidraSocket = (
|
||||
};
|
||||
};
|
||||
|
||||
/** Open one abort-aware Ghidra Unix-socket connection attempt. */
|
||||
/** Open one abort-aware connection to an observed local Ghidra endpoint. */
|
||||
export const connectGhidraSocketOnce = (
|
||||
socketPath: string,
|
||||
target: GhidraConnectTarget,
|
||||
signal: AbortSignal,
|
||||
): Promise<Result<Socket, GhidraSessionError>> =>
|
||||
new Promise((resolve) => {
|
||||
const socket = createConnection(socketPath);
|
||||
const socket =
|
||||
"path" in target
|
||||
? createConnection(target.path)
|
||||
: createConnection({ host: target.host, port: target.port });
|
||||
const detach = (): void => {
|
||||
socket.off("connect", onConnect);
|
||||
socket.off("error", onError);
|
||||
@@ -48,7 +52,7 @@ export const connectGhidraSocketOnce = (
|
||||
resolve(
|
||||
err(
|
||||
new GhidraSessionError("start", "Ghidra bridge connection failed", {
|
||||
socket_path: socketPath,
|
||||
endpoint: target,
|
||||
error: cause.message,
|
||||
}),
|
||||
),
|
||||
@@ -62,7 +66,7 @@ export const connectGhidraSocketOnce = (
|
||||
new GhidraSessionError(
|
||||
"cancelled",
|
||||
"Ghidra bridge connection was cancelled",
|
||||
{ socket_path: socketPath },
|
||||
{ endpoint: target },
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
@@ -0,0 +1,129 @@
|
||||
import { access, open } from "node:fs/promises";
|
||||
|
||||
import { err, ok, type Result } from "../domain/result.js";
|
||||
import { GhidraSessionError } from "./GhidraSessionError.js";
|
||||
|
||||
/** Local bridge transports admitted by the versioned session descriptor. */
|
||||
export type GhidraTransportKind = "unix-socket" | "authenticated-loopback-tcp";
|
||||
|
||||
/** Private runtime coordinate used to discover one bridge listener. */
|
||||
export interface GhidraEndpoint {
|
||||
readonly transport: GhidraTransportKind;
|
||||
readonly path: string;
|
||||
}
|
||||
|
||||
/** Exact Node connection target after endpoint discovery. */
|
||||
export type GhidraConnectTarget =
|
||||
| { readonly path: string }
|
||||
| { readonly host: "127.0.0.1"; readonly port: number };
|
||||
|
||||
const MAX_ENDPOINT_BYTES = 1024;
|
||||
|
||||
/** Observe a ready endpoint, returning null while its bridge is not listening. */
|
||||
export const observeGhidraEndpoint = async (
|
||||
endpoint: GhidraEndpoint,
|
||||
): Promise<Result<GhidraConnectTarget | null, GhidraSessionError>> => {
|
||||
if (endpoint.transport === "unix-socket") {
|
||||
try {
|
||||
await access(endpoint.path);
|
||||
return ok({ path: endpoint.path });
|
||||
} catch (cause: unknown) {
|
||||
return isMissing(cause)
|
||||
? ok(null)
|
||||
: err(
|
||||
endpointFailure(
|
||||
endpoint,
|
||||
"Ghidra socket observation failed",
|
||||
cause,
|
||||
),
|
||||
);
|
||||
}
|
||||
}
|
||||
let handle;
|
||||
try {
|
||||
handle = await open(endpoint.path, "r");
|
||||
} catch (cause: unknown) {
|
||||
return isMissing(cause)
|
||||
? ok(null)
|
||||
: err(
|
||||
endpointFailure(
|
||||
endpoint,
|
||||
"Ghidra endpoint observation failed",
|
||||
cause,
|
||||
),
|
||||
);
|
||||
}
|
||||
try {
|
||||
const bytes = Buffer.alloc(MAX_ENDPOINT_BYTES + 1);
|
||||
const observed = await handle.read(bytes, 0, bytes.length, 0);
|
||||
if (observed.bytesRead === 0 || observed.bytesRead > MAX_ENDPOINT_BYTES)
|
||||
return err(endpointFailure(endpoint, "Ghidra TCP endpoint is invalid"));
|
||||
return parseTcpEndpoint(
|
||||
endpoint,
|
||||
bytes.subarray(0, observed.bytesRead).toString("utf8"),
|
||||
);
|
||||
} catch (cause: unknown) {
|
||||
return err(endpointFailure(endpoint, "Ghidra endpoint read failed", cause));
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
};
|
||||
|
||||
const parseTcpEndpoint = (
|
||||
endpoint: GhidraEndpoint,
|
||||
encoded: string,
|
||||
): Result<GhidraConnectTarget, GhidraSessionError> => {
|
||||
try {
|
||||
const value: unknown = JSON.parse(encoded);
|
||||
if (
|
||||
typeof value !== "object" ||
|
||||
value === null ||
|
||||
Array.isArray(value) ||
|
||||
!hasExactKeys(value, ["host", "port", "schema_version"]) ||
|
||||
value.schema_version !== 1 ||
|
||||
value.host !== "127.0.0.1" ||
|
||||
!Number.isSafeInteger(value.port) ||
|
||||
typeof value.port !== "number" ||
|
||||
value.port < 1 ||
|
||||
value.port > 65_535
|
||||
)
|
||||
return err(endpointFailure(endpoint, "Ghidra TCP endpoint is invalid"));
|
||||
return ok({ host: "127.0.0.1", port: value.port });
|
||||
} catch (cause: unknown) {
|
||||
return err(
|
||||
endpointFailure(endpoint, "Ghidra TCP endpoint is invalid", cause),
|
||||
);
|
||||
}
|
||||
};
|
||||
|
||||
const hasExactKeys = (
|
||||
value: object,
|
||||
expected: readonly string[],
|
||||
): value is Record<string, unknown> => {
|
||||
const keys = Object.keys(value).sort((left, right) =>
|
||||
left.localeCompare(right),
|
||||
);
|
||||
return (
|
||||
keys.length === expected.length &&
|
||||
keys.every((key, index) => key === expected[index])
|
||||
);
|
||||
};
|
||||
|
||||
const endpointFailure = (
|
||||
endpoint: GhidraEndpoint,
|
||||
message: string,
|
||||
cause?: unknown,
|
||||
): GhidraSessionError =>
|
||||
new GhidraSessionError(
|
||||
"start",
|
||||
message,
|
||||
{
|
||||
transport: endpoint.transport,
|
||||
endpoint_path: endpoint.path,
|
||||
...(cause instanceof Error ? { error: cause.message } : {}),
|
||||
},
|
||||
cause instanceof Error ? { cause } : undefined,
|
||||
);
|
||||
|
||||
const isMissing = (cause: unknown): boolean =>
|
||||
cause instanceof Error && "code" in cause && cause.code === "ENOENT";
|
||||
Reference in New Issue
Block a user