Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
fix(ghidra): validate JVM platform overrides
  • Loading branch information
morluto committed Oct 13, 2026
commit 62e422953d7ab7dabfd9b2a93dbf3b21f9966be1
22 changes: 18 additions & 4 deletions src/ghidra/GhidraJavaLaunch.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { execFileOutput } from "../process/ExecFileOutput.js";
export const ghidraJavaLaunch = async (options: {
readonly platform: NodeJS.Platform;
readonly analyzeHeadlessPath: string;
readonly javaHome: string;
readonly javaHome?: string;
readonly javaVmName?: string;
readonly homeRoot: string;
readonly tempRoot: string;
Expand All @@ -21,7 +21,17 @@ export const ghidraJavaLaunch = async (options: {
}> => {
const supportRoot = dirname(options.analyzeHeadlessPath);
const installRoot = dirname(supportRoot);
const command = join(options.javaHome, "bin", "java");
const selectedJavaHome = options.javaHome ?? options.environment.JAVA_HOME;
const command =
selectedJavaHome === undefined
? options.platform === "win32"
? "java.exe"
: "java"
: join(
selectedJavaHome,
"bin",
options.platform === "win32" ? "java.exe" : "java",
);
const isolatedProperties = [
`-Duser.home=${options.homeRoot}`,
`-Djava.io.tmpdir=${options.tempRoot}`,
Expand Down Expand Up @@ -83,9 +93,13 @@ export const ghidraJavaLaunch = async (options: {
)
)
throw new Error("Invalid Ghidra LaunchSupport JVM argument");
if (vmArguments.some((argument) => /^-Dos\.arch(?:=|$)/u.test(argument)))
if (
vmArguments.some((argument) =>
/^-Dos\.(?:arch|name)(?:=|$)/u.test(argument),
)
Comment thread
morluto marked this conversation as resolved.
Resolved
)
throw new Error(
"Ghidra LaunchSupport may not override os.arch; the native decompiler was validated for the inspected JVM architecture.",
"Ghidra LaunchSupport may not override os.arch or os.name; the native decompiler was validated for the inspected JVM platform.",
);
return {
command,
Expand Down
73 changes: 37 additions & 36 deletions src/ghidra/GhidraLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -303,43 +303,44 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
throw new GhidraLaunchError(
"POSIX Ghidra requires its inspected JDK home",
);
const inspectedJavaLaunch = await ghidraJavaLaunch({
Comment thread
morluto marked this conversation as resolved.
Resolved
Comment thread
morluto marked this conversation as resolved.
Resolved
platform,
analyzeHeadlessPath: this.options.analyzeHeadlessPath,
...(this.options.javaHome === undefined
? {}
: { javaHome: this.options.javaHome }),
Comment thread
morluto marked this conversation as resolved.
Resolved
...(this.options.javaVmName === undefined
? {}
: { javaVmName: this.options.javaVmName }),
homeRoot: paths.homeRoot,
tempRoot: paths.tempRoot,
arguments: headlessArguments,
environment,
onStderr: (mode, stderr) => {
if (stderr === "") return;
const bytes = Buffer.byteLength(stderr);
const redacted = redactCapturedTransportCredential(
{ text: stderr, bytes, observedBytes: bytes },
session.token,
"[REDACTED]",
);
const bounded = boundedUtf8Text(
redacted,
GHIDRA_LAUNCH_COMMAND_OUTPUT_BYTES,
);
launchSupport.push({
mode,
stderr: bounded.text,
stderr_bytes: bytes,
stderr_truncated: bounded.truncated,
});
},
...(options.signal === undefined ? {} : { signal: options.signal }),
});
const command =
platform !== "win32" && this.options.javaHome !== undefined
? await ghidraJavaLaunch({
platform,
analyzeHeadlessPath: this.options.analyzeHeadlessPath,
javaHome: this.options.javaHome,
...(this.options.javaVmName === undefined
? {}
: { javaVmName: this.options.javaVmName }),
homeRoot: paths.homeRoot,
tempRoot: paths.tempRoot,
arguments: headlessArguments,
environment,
onStderr: (mode, stderr) => {
if (stderr === "") return;
const bytes = Buffer.byteLength(stderr);
const redacted = redactCapturedTransportCredential(
{ text: stderr, bytes, observedBytes: bytes },
session.token,
"[REDACTED]",
);
const bounded = boundedUtf8Text(
redacted,
GHIDRA_LAUNCH_COMMAND_OUTPUT_BYTES,
);
launchSupport.push({
mode,
stderr: bounded.text,
stderr_bytes: bytes,
stderr_truncated: bounded.truncated,
});
},
...(options.signal === undefined
? {}
: { signal: options.signal }),
})
: { ...scriptCommand, environment };
platform === "win32"
? { ...scriptCommand, environment }
: inspectedJavaLaunch;
started = await (this.options.spawnProcess ?? spawnOwnedProviderProcess)({
command: command.command,
arguments: command.arguments,
Expand Down
30 changes: 19 additions & 11 deletions tests/boundary/providers/ghidra/ghidraJavaLaunch.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,9 @@ if (mode === "env-failure" || (probe === "-vmargs" && mode === "vm-failure")) {
? "invalid JVM argument\\n"
: mode === "os-arch-override"
? "-Dos.arch=x86_64\\n"
: "-Dfile.encoding=UTF8\\n";
: mode === "os-name-override"
? "-Dos.name=Windows\\n"
: "-Dfile.encoding=UTF8\\n";
process.stdout.write(vmArguments);
}
`,
Expand Down Expand Up @@ -220,16 +222,22 @@ describe.skipIf(process.platform === "win32")(
},
);

it("rejects LaunchSupport attempts to override the inspected JVM architecture", async () => {
const { session, launcher, analysisArguments } =
await fixture("os-arch-override");
const launched = await launcher.launch(session);
expect(launched.ok).toBe(false);
expect(analysisArguments).toEqual([]);
if (launched.ok)
throw new Error("Expected an architecture override failure");
expect(launched.error.message).toContain("may not override os.arch");
});
it.each(["os-arch-override", "os-name-override"])(
"rejects LaunchSupport %s before starting analysis",
async (mode) => {
const { session, launcher, analysisArguments } = await fixture(mode);
const launched = await launcher.launch(session);
expect(launched.ok).toBe(false);
expect(analysisArguments).toEqual([]);
if (launched.ok)
throw new Error("Expected a JVM platform override failure");
expect(launched.error.cause).toMatchObject({
message: expect.stringContaining(
"may not override os.arch or os.name",
),
});
},
);

it("bounds retained UTF-8 warnings and reports original byte counts", async () => {
const { session, launcher } = await fixture("large");
Expand Down
Loading