Skip to content

Commit 62e4229

Browse files
committed
fix(ghidra): validate JVM platform overrides
1 parent 5730da1 commit 62e4229

3 files changed

Lines changed: 74 additions & 51 deletions

File tree

‎src/ghidra/GhidraJavaLaunch.ts‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { execFileOutput } from "../process/ExecFileOutput.js";
66
export const ghidraJavaLaunch = async (options: {
77
readonly platform: NodeJS.Platform;
88
readonly analyzeHeadlessPath: string;
9-
readonly javaHome: string;
9+
readonly javaHome?: string;
1010
readonly javaVmName?: string;
1111
readonly homeRoot: string;
1212
readonly tempRoot: string;
@@ -21,7 +21,17 @@ export const ghidraJavaLaunch = async (options: {
2121
}> => {
2222
const supportRoot = dirname(options.analyzeHeadlessPath);
2323
const installRoot = dirname(supportRoot);
24-
const command = join(options.javaHome, "bin", "java");
24+
const selectedJavaHome = options.javaHome ?? options.environment.JAVA_HOME;
25+
const command =
26+
selectedJavaHome === undefined
27+
? options.platform === "win32"
28+
? "java.exe"
29+
: "java"
30+
: join(
31+
selectedJavaHome,
32+
"bin",
33+
options.platform === "win32" ? "java.exe" : "java",
34+
);
2535
const isolatedProperties = [
2636
`-Duser.home=${options.homeRoot}`,
2737
`-Djava.io.tmpdir=${options.tempRoot}`,
@@ -83,9 +93,13 @@ export const ghidraJavaLaunch = async (options: {
8393
)
8494
)
8595
throw new Error("Invalid Ghidra LaunchSupport JVM argument");
86-
if (vmArguments.some((argument) => /^-Dos\.arch(?:=|$)/u.test(argument)))
96+
if (
97+
vmArguments.some((argument) =>
98+
/^-Dos\.(?:arch|name)(?:=|$)/u.test(argument),
99+
)
100+
)
87101
throw new Error(
88-
"Ghidra LaunchSupport may not override os.arch; the native decompiler was validated for the inspected JVM architecture.",
102+
"Ghidra LaunchSupport may not override os.arch or os.name; the native decompiler was validated for the inspected JVM platform.",
89103
);
90104
return {
91105
command,

‎src/ghidra/GhidraLauncher.ts‎

Lines changed: 37 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -303,43 +303,44 @@ export class GhidraHeadlessLauncher implements GhidraLauncher {
303303
throw new GhidraLaunchError(
304304
"POSIX Ghidra requires its inspected JDK home",
305305
);
306+
const inspectedJavaLaunch = await ghidraJavaLaunch({
307+
platform,
308+
analyzeHeadlessPath: this.options.analyzeHeadlessPath,
309+
...(this.options.javaHome === undefined
310+
? {}
311+
: { javaHome: this.options.javaHome }),
312+
...(this.options.javaVmName === undefined
313+
? {}
314+
: { javaVmName: this.options.javaVmName }),
315+
homeRoot: paths.homeRoot,
316+
tempRoot: paths.tempRoot,
317+
arguments: headlessArguments,
318+
environment,
319+
onStderr: (mode, stderr) => {
320+
if (stderr === "") return;
321+
const bytes = Buffer.byteLength(stderr);
322+
const redacted = redactCapturedTransportCredential(
323+
{ text: stderr, bytes, observedBytes: bytes },
324+
session.token,
325+
"[REDACTED]",
326+
);
327+
const bounded = boundedUtf8Text(
328+
redacted,
329+
GHIDRA_LAUNCH_COMMAND_OUTPUT_BYTES,
330+
);
331+
launchSupport.push({
332+
mode,
333+
stderr: bounded.text,
334+
stderr_bytes: bytes,
335+
stderr_truncated: bounded.truncated,
336+
});
337+
},
338+
...(options.signal === undefined ? {} : { signal: options.signal }),
339+
});
306340
const command =
307-
platform !== "win32" && this.options.javaHome !== undefined
308-
? await ghidraJavaLaunch({
309-
platform,
310-
analyzeHeadlessPath: this.options.analyzeHeadlessPath,
311-
javaHome: this.options.javaHome,
312-
...(this.options.javaVmName === undefined
313-
? {}
314-
: { javaVmName: this.options.javaVmName }),
315-
homeRoot: paths.homeRoot,
316-
tempRoot: paths.tempRoot,
317-
arguments: headlessArguments,
318-
environment,
319-
onStderr: (mode, stderr) => {
320-
if (stderr === "") return;
321-
const bytes = Buffer.byteLength(stderr);
322-
const redacted = redactCapturedTransportCredential(
323-
{ text: stderr, bytes, observedBytes: bytes },
324-
session.token,
325-
"[REDACTED]",
326-
);
327-
const bounded = boundedUtf8Text(
328-
redacted,
329-
GHIDRA_LAUNCH_COMMAND_OUTPUT_BYTES,
330-
);
331-
launchSupport.push({
332-
mode,
333-
stderr: bounded.text,
334-
stderr_bytes: bytes,
335-
stderr_truncated: bounded.truncated,
336-
});
337-
},
338-
...(options.signal === undefined
339-
? {}
340-
: { signal: options.signal }),
341-
})
342-
: { ...scriptCommand, environment };
341+
platform === "win32"
342+
? { ...scriptCommand, environment }
343+
: inspectedJavaLaunch;
343344
started = await (this.options.spawnProcess ?? spawnOwnedProviderProcess)({
344345
command: command.command,
345346
arguments: command.arguments,

‎tests/boundary/providers/ghidra/ghidraJavaLaunch.test.ts‎

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ if (mode === "env-failure" || (probe === "-vmargs" && mode === "vm-failure")) {
5656
? "invalid JVM argument\\n"
5757
: mode === "os-arch-override"
5858
? "-Dos.arch=x86_64\\n"
59-
: "-Dfile.encoding=UTF8\\n";
59+
: mode === "os-name-override"
60+
? "-Dos.name=Windows\\n"
61+
: "-Dfile.encoding=UTF8\\n";
6062
process.stdout.write(vmArguments);
6163
}
6264
`,
@@ -220,16 +222,22 @@ describe.skipIf(process.platform === "win32")(
220222
},
221223
);
222224

223-
it("rejects LaunchSupport attempts to override the inspected JVM architecture", async () => {
224-
const { session, launcher, analysisArguments } =
225-
await fixture("os-arch-override");
226-
const launched = await launcher.launch(session);
227-
expect(launched.ok).toBe(false);
228-
expect(analysisArguments).toEqual([]);
229-
if (launched.ok)
230-
throw new Error("Expected an architecture override failure");
231-
expect(launched.error.message).toContain("may not override os.arch");
232-
});
225+
it.each(["os-arch-override", "os-name-override"])(
226+
"rejects LaunchSupport %s before starting analysis",
227+
async (mode) => {
228+
const { session, launcher, analysisArguments } = await fixture(mode);
229+
const launched = await launcher.launch(session);
230+
expect(launched.ok).toBe(false);
231+
expect(analysisArguments).toEqual([]);
232+
if (launched.ok)
233+
throw new Error("Expected a JVM platform override failure");
234+
expect(launched.error.cause).toMatchObject({
235+
message: expect.stringContaining(
236+
"may not override os.arch or os.name",
237+
),
238+
});
239+
},
240+
);
233241

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

0 commit comments

Comments
 (0)