feat(ghidra): configurable import language and analysis seeds (#1667)
* feat(ghidra): configurable import language and analysis seeds REA_GHIDRA_LANGUAGE_ID and REA_GHIDRA_COMPILER_SPEC_ID set the import language when the header cannot identify the compiler (for example, Borland C++ PE images). REA_GHIDRA_SEED_FILE seeds functions, code and labels before auto-analysis, for code reached only through computed jumps or data. Both are validated before launch, recorded in the analysis profile and verified in the bridge handshake. * fix(ghidra): parse language XML and preserve seed ordering * test(ghidra): use explicit platform paths for seed launch checks * fix(javascript): retain cleanup failure reason in error causes --------- Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
This commit is contained in:
@@ -9,6 +9,15 @@ import {
|
||||
} from "../application/AnalysisProvider.js";
|
||||
import type { AppConfig } from "../config/types.js";
|
||||
import type { JsonValue } from "../domain/jsonValue.js";
|
||||
import {
|
||||
ghidraSeedCommitmentSchema,
|
||||
ghidraSeedFailure,
|
||||
ghidraSeedLimitations,
|
||||
} from "./GhidraAnalysisSeeds.js";
|
||||
import {
|
||||
ghidraProfileLanguageOverride,
|
||||
sameGhidraLanguageOverride,
|
||||
} from "./GhidraAnalysisProfile.js";
|
||||
import type { AnalysisProfileCommitment } from "../domain/analysisProfile.js";
|
||||
import type { BinaryTarget } from "../domain/binaryTargetTypes.js";
|
||||
import {
|
||||
@@ -105,6 +114,36 @@ export const createGhidraProviderClient = (input: {
|
||||
}),
|
||||
);
|
||||
const extensions = extensionProfile.data;
|
||||
const seedProfile = ghidraSeedCommitmentSchema
|
||||
.optional()
|
||||
.safeParse(committedProfile.parameters.analysis_seeds);
|
||||
if (
|
||||
!seedProfile.success ||
|
||||
(config.ghidraSeedFile === undefined) !== (seedProfile.data === undefined)
|
||||
)
|
||||
return unavailableClient(
|
||||
new ProviderAdapterError("ghidra", "open_binary", {
|
||||
diagnostics: {
|
||||
reason:
|
||||
"Resolve REA_GHIDRA_SEED_FILE into an analysis profile before opening the session.",
|
||||
},
|
||||
}),
|
||||
);
|
||||
const seeds = seedProfile.data;
|
||||
const languageOverride = ghidraProfileLanguageOverride(
|
||||
committedProfile.parameters,
|
||||
);
|
||||
if (
|
||||
!sameGhidraLanguageOverride(languageOverride, config.ghidraLanguageOverride)
|
||||
)
|
||||
return unavailableClient(
|
||||
new ProviderAdapterError("ghidra", "open_binary", {
|
||||
diagnostics: {
|
||||
reason:
|
||||
"The committed analysis profile and REA_GHIDRA_LANGUAGE_ID/REA_GHIDRA_COMPILER_SPEC_ID differ; reopen the target to resolve a new profile.",
|
||||
},
|
||||
}),
|
||||
);
|
||||
const invalidProfile = validateGhidraExtensionProfile(
|
||||
extensions,
|
||||
config,
|
||||
@@ -118,6 +157,8 @@ export const createGhidraProviderClient = (input: {
|
||||
}),
|
||||
);
|
||||
let extensionFailure: AnalysisError | undefined;
|
||||
// Replaced by the reported outcome once the session handshake is checked.
|
||||
const seedLimitations: string[] = [...ghidraSeedLimitations(seeds)];
|
||||
const targetLimitations = [
|
||||
...(target.format === "dos-mz"
|
||||
? [
|
||||
@@ -149,6 +190,8 @@ export const createGhidraProviderClient = (input: {
|
||||
),
|
||||
...(target.format === "dos-mz" ? { dosMz: true } : {}),
|
||||
...(target.format === "dos-com" ? { dosCom: true } : {}),
|
||||
...(languageOverride === undefined ? {} : { languageOverride }),
|
||||
...(seeds === undefined ? {} : { analysisSeeds: seeds }),
|
||||
platform: installation.platform,
|
||||
...(extensions.length === 0 ? {} : { analysisExtensions: extensions }),
|
||||
}),
|
||||
@@ -168,7 +211,14 @@ export const createGhidraProviderClient = (input: {
|
||||
expectedLanguageId: "x86:LE:16:Real Mode",
|
||||
expectedCompilerSpecId: "default",
|
||||
}
|
||||
: {}),
|
||||
: languageOverride === undefined
|
||||
? {}
|
||||
: {
|
||||
expectedLanguageId: languageOverride.languageId,
|
||||
...(languageOverride.compilerSpecId === undefined
|
||||
? {}
|
||||
: { expectedCompilerSpecId: languageOverride.compilerSpecId }),
|
||||
}),
|
||||
...(context === undefined ? {} : { runId: context.runId }),
|
||||
logger: logger.child({ layer: "ghidra-bridge" }),
|
||||
});
|
||||
@@ -176,11 +226,18 @@ export const createGhidraProviderClient = (input: {
|
||||
operation: AnalysisOperation,
|
||||
info: GhidraSessionInfo,
|
||||
): Promise<AnalysisError | undefined> => {
|
||||
extensionFailure = ghidraExtensionFailure(
|
||||
extensions,
|
||||
info.analysis_extensions ?? [],
|
||||
operation,
|
||||
);
|
||||
extensionFailure =
|
||||
ghidraExtensionFailure(
|
||||
extensions,
|
||||
info.analysis_extensions ?? [],
|
||||
operation,
|
||||
) ?? ghidraSeedFailure(seeds, info.analysis_seeds, operation);
|
||||
if (extensionFailure === undefined && info.analysis_seeds !== undefined)
|
||||
seedLimitations.splice(
|
||||
0,
|
||||
seedLimitations.length,
|
||||
...ghidraSeedLimitations(seeds, info.analysis_seeds),
|
||||
);
|
||||
if (extensionFailure === undefined) return undefined;
|
||||
const closed = await client.close();
|
||||
return closed.ok
|
||||
@@ -194,10 +251,11 @@ export const createGhidraProviderClient = (input: {
|
||||
const releaseLimitation = unverifiedGhidraBuildLimitation(
|
||||
prerequisites.value.providerVersion,
|
||||
);
|
||||
const sessionLimitations = [
|
||||
const sessionLimitations = (): readonly string[] => [
|
||||
...providerLimitations,
|
||||
...targetLimitations,
|
||||
...ghidraExtensionLimitations(extensions),
|
||||
...seedLimitations,
|
||||
...(releaseLimitation === undefined ? [] : [releaseLimitation]),
|
||||
];
|
||||
return {
|
||||
@@ -230,7 +288,7 @@ export const createGhidraProviderClient = (input: {
|
||||
return ok(
|
||||
createAnalysisExecution(started.value, committedProfile.provider, {
|
||||
analysisProfile: committedProfile,
|
||||
limitations: [...healthLimitations, ...sessionLimitations],
|
||||
limitations: [...healthLimitations, ...sessionLimitations()],
|
||||
}),
|
||||
);
|
||||
}
|
||||
@@ -238,7 +296,7 @@ export const createGhidraProviderClient = (input: {
|
||||
? parseGhidraFunctionInput(operation, parameters)
|
||||
: parseGhidraInventoryInput(operation, parameters);
|
||||
if (!input.ok) return input;
|
||||
if (extensions.length > 0) {
|
||||
if (extensions.length > 0 || seeds !== undefined) {
|
||||
const started = await client.start(options?.signal);
|
||||
if (!started.ok)
|
||||
return err(
|
||||
@@ -299,7 +357,7 @@ export const createGhidraProviderClient = (input: {
|
||||
limitations: [
|
||||
...limitationsFor(operation),
|
||||
...observationLimitations,
|
||||
...sessionLimitations,
|
||||
...sessionLimitations(),
|
||||
],
|
||||
}),
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user