mirror of
https://github.com/morluto/rea.git
synced 2026-11-23 23:15:58 +00:00
fix(ghidra): name invalid installed language definition files (#13401)
This commit is contained in:
1 parent
103e1a71f2
commit
1d85f680f9
4 files changed
+73
-50
No files matched your search
@@ -18,6 +18,7 @@ const GHIDRA_ADMISSION_CODES = [
|
||||
"elf_image_base_format",
|
||||
"language_override_dos",
|
||||
"language_override_raw_binary",
|
||||
"language_definition_invalid",
|
||||
"raw_binary_dos",
|
||||
"invalid_environment_entry",
|
||||
"invalid_jvm_argument",
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
import { readdir } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { malformedInput, ParseFailureError } from "../domain/parseFailure.js";
|
||||
import { GhidraAdmissionError } from "./GhidraAdmissionError.js";
|
||||
import { parseXmlDocument } from "../domain/xmlDocument.js";
|
||||
import { decodeXmlPlistText } from "../domain/propertyListXmlText.js";
|
||||
|
||||
@@ -29,7 +31,7 @@ const LANGUAGE_ROOTS = ["Ghidra/Processors", "Ghidra/Extensions"] as const;
|
||||
const CATALOG_CONCURRENCY = 8;
|
||||
|
||||
/** Parse the `<language>` elements of one `.ldefs` document. */
|
||||
export const parseGhidraLanguageDefinitions = (
|
||||
const parseGhidraLanguageDefinitions = (
|
||||
text: string,
|
||||
): readonly GhidraLanguageDefinition[] => {
|
||||
const definitions: GhidraLanguageDefinition[] = [];
|
||||
@@ -41,7 +43,10 @@ export const parseGhidraLanguageDefinitions = (
|
||||
const document = parsed.value;
|
||||
const root = document.documentElement;
|
||||
if (root?.tagName !== "language_definitions")
|
||||
throw new SyntaxError("Expected Ghidra language_definitions XML root");
|
||||
throw malformedInput(
|
||||
"ghidra-language-definition",
|
||||
"Expected Ghidra language_definitions XML root",
|
||||
);
|
||||
for (const language of Array.from(root.getElementsByTagName("language"))) {
|
||||
if (language.parentNode !== root) continue;
|
||||
const id = language.getAttribute("id");
|
||||
@@ -101,9 +106,17 @@ const readLanguageDefinitionFile = (
|
||||
});
|
||||
// The XML byte owner validates encoding and declaration before syntax
|
||||
// parsing; a literal U+FFFD cannot diagnose invalid source bytes.
|
||||
return bytes === undefined
|
||||
? []
|
||||
: parseGhidraLanguageDefinitions(decodeXmlPlistText(bytes));
|
||||
if (bytes === undefined) return [];
|
||||
try {
|
||||
return parseGhidraLanguageDefinitions(decodeXmlPlistText(bytes));
|
||||
} catch (cause: unknown) {
|
||||
if (!(cause instanceof ParseFailureError)) throw cause;
|
||||
throw new GhidraAdmissionError(
|
||||
"language_definition_invalid",
|
||||
{ path, parse_kind: cause.kind, parse_message: cause.message },
|
||||
{ cause },
|
||||
);
|
||||
}
|
||||
},
|
||||
"follow",
|
||||
);
|
||||
|
||||
@@ -16,10 +16,7 @@ import {
|
||||
ghidraProfileInstallationHost as host,
|
||||
} from "./GhidraInstallation.fixture.js";
|
||||
import { SUPPORTED_GHIDRA_VERSION } from "./GhidraReleasePolicy.js";
|
||||
import {
|
||||
parseGhidraLanguageDefinitions,
|
||||
readGhidraLanguageCatalog,
|
||||
} from "./GhidraLanguageCatalog.js";
|
||||
import { readGhidraLanguageCatalog } from "./GhidraLanguageCatalog.js";
|
||||
import { GhidraProvider } from "./GhidraProvider.js";
|
||||
import { ArtifactResourceScope } from "../resources/ArtifactResourceScope.js";
|
||||
|
||||
@@ -214,39 +211,16 @@ describe("Ghidra language override configuration", () => {
|
||||
});
|
||||
|
||||
describe("Ghidra language catalog", () => {
|
||||
it("reads quoted and escaped XML attributes without admitting comments or CDATA", () => {
|
||||
const catalog = parseGhidraLanguageDefinitions(`<language_definitions>
|
||||
<!-- <language id="removed"><compiler id="removed"/></language> -->
|
||||
<language id='x86:LE:32:default'>
|
||||
<description><![CDATA[<compiler id="not-a-compiler"/>]]></description>
|
||||
<!-- <compiler id="removed"/> -->
|
||||
<compiler id='borlandcpp' name='Borland & C++'/>
|
||||
</language>
|
||||
</language_definitions>`);
|
||||
expect(catalog).toEqual([
|
||||
{ id: "x86:LE:32:default", compilerSpecIds: ["borlandcpp"] },
|
||||
]);
|
||||
});
|
||||
|
||||
it.each([
|
||||
'<language_definitions><language id="x86:LE:32:default"></language_definitions>',
|
||||
"<language_definitions><language id=x86:LE:32:default/></language_definitions>",
|
||||
'<other><language id="x86:LE:32:default"/></other>',
|
||||
])("rejects malformed language-definition documents", (text) => {
|
||||
expect(() => parseGhidraLanguageDefinitions(text)).toThrow();
|
||||
});
|
||||
|
||||
it("parses language IDs and their compiler specs", async () => {
|
||||
expect(parseGhidraLanguageDefinitions(X86_LDEFS)).toEqual([
|
||||
expect(
|
||||
await readGhidraLanguageCatalog(installDir, new ArtifactResourceScope()),
|
||||
).toEqual([
|
||||
{
|
||||
id: "x86:LE:32:default",
|
||||
compilerSpecIds: ["windows", "gcc", "borlandcpp"],
|
||||
},
|
||||
{ id: "x86:LE:16:Real Mode", compilerSpecIds: ["default"] },
|
||||
]);
|
||||
expect(
|
||||
await readGhidraLanguageCatalog(installDir, new ArtifactResourceScope()),
|
||||
).toHaveLength(2);
|
||||
expect(
|
||||
await readGhidraLanguageCatalog(
|
||||
join(installDir, "absent"),
|
||||
@@ -254,32 +228,48 @@ describe("Ghidra language catalog", () => {
|
||||
),
|
||||
).toEqual([]);
|
||||
});
|
||||
});
|
||||
|
||||
describe("Ghidra language override profile", () => {
|
||||
it("returns a provider failure for a malformed installed language definition", async () => {
|
||||
it("identifies the malformed installed definition beside a valid selected language", async () => {
|
||||
const path = join(
|
||||
installDir,
|
||||
"Ghidra/Processors/x86/data/languages/broken.ldefs",
|
||||
);
|
||||
const configured = provider({
|
||||
REA_GHIDRA_LANGUAGE_ID: "x86:LE:32:default",
|
||||
});
|
||||
await writeFile(
|
||||
path,
|
||||
"<language_definitions><language></language_definitions>",
|
||||
);
|
||||
try {
|
||||
const resolved = await provider({
|
||||
REA_GHIDRA_LANGUAGE_ID: "x86:LE:32:default",
|
||||
}).resolveAnalysisProfile(pe);
|
||||
const resolved = await configured.resolveAnalysisProfile(pe);
|
||||
if (resolved.ok) throw new Error("Expected a language catalog failure");
|
||||
expect(resolved.error).toMatchObject({
|
||||
providerId: "ghidra",
|
||||
operation: "resolve_analysis_profile",
|
||||
expect(projectAnalysisError(resolved.error)).toMatchObject({
|
||||
details: {
|
||||
provider_id: "ghidra",
|
||||
operation: "resolve_analysis_profile",
|
||||
diagnostics: {
|
||||
setting: "REA_GHIDRA_LANGUAGE_ID",
|
||||
code: "language_definition_invalid",
|
||||
path,
|
||||
parse_kind: "malformed",
|
||||
parse_message: expect.stringContaining("XML is not well-formed"),
|
||||
},
|
||||
},
|
||||
});
|
||||
await writeFile(path, "<language_definitions/>");
|
||||
const repaired = await configured.resolveAnalysisProfile(pe);
|
||||
if (!repaired.ok) throw repaired.error;
|
||||
expect(repaired.value.profile?.parameters.language_id).toBe(
|
||||
"x86:LE:32:default",
|
||||
);
|
||||
} finally {
|
||||
await rm(path);
|
||||
}
|
||||
});
|
||||
});
|
||||
|
||||
describe("Ghidra language override profile", () => {
|
||||
it("accepts the caller-selected language on the legacy MIPS lane", async () => {
|
||||
const mipsLanguages = join(
|
||||
installDir,
|
||||
|
||||
@@ -5,8 +5,14 @@ import { ArtifactResourceScope } from "../../../src/resources/ArtifactResourceSc
|
||||
import { readGhidraLanguageCatalog } from "../../../src/ghidra/GhidraLanguageCatalog.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
const xml =
|
||||
'<language_definitions><language id="toy:LE:32:default" description="�"><compiler id="default"/></language></language_definitions>';
|
||||
const xml = `<language_definitions>
|
||||
<!-- <language id="commented"><compiler id="commented"/></language> -->
|
||||
<language id='toy:LE:32:default' description="�">
|
||||
<description><![CDATA[<compiler id="not-a-compiler"/>]]></description>
|
||||
<!-- <compiler id="commented"/> -->
|
||||
<compiler id='default' name='Default & Standard'/>
|
||||
</language>
|
||||
</language_definitions>`;
|
||||
const languageDirectory = async () => {
|
||||
const root = await createTestTempDirectory("rea-ghidra-xml-encoding-");
|
||||
const directory = join(root, "Ghidra/Processors/toy/data/languages");
|
||||
@@ -19,7 +25,7 @@ it("reads independent unsigned, UTF-8 BOM and UTF-16 BOM definitions together",
|
||||
const named = (id: string) =>
|
||||
xml.replace("toy:LE:32:default", `${id}:LE:32:default`);
|
||||
await writeFile(
|
||||
join(directory, "ascii.ldefs"),
|
||||
join(directory, "plain.ldefs"),
|
||||
Buffer.from(named("unsigned")),
|
||||
);
|
||||
await writeFile(
|
||||
@@ -67,16 +73,29 @@ it.each([
|
||||
},
|
||||
{
|
||||
name: "malformed XML alongside U+FFFD",
|
||||
bytes: Buffer.from(xml.replace('id="toy:LE:32:default"', "id=unquoted")),
|
||||
bytes: Buffer.from(xml.replace("id='toy:LE:32:default'", "id=unquoted")),
|
||||
expectedError: { _tag: "ParseFailureError", kind: "malformed" },
|
||||
},
|
||||
{
|
||||
name: "a valid XML document with the wrong root",
|
||||
bytes: Buffer.from('<other><language id="toy:LE:32:default"/></other>'),
|
||||
expectedError: {
|
||||
kind: "malformed",
|
||||
message: "Expected Ghidra language_definitions XML root",
|
||||
},
|
||||
},
|
||||
])(
|
||||
"preserves the failure for $name at the language-file byte boundary",
|
||||
async ({ bytes, expectedError }) => {
|
||||
const { root, directory } = await languageDirectory();
|
||||
await writeFile(join(directory, "toy.ldefs"), bytes);
|
||||
const path = join(directory, "toy.ldefs");
|
||||
await writeFile(path, bytes);
|
||||
await expect(
|
||||
readGhidraLanguageCatalog(root, new ArtifactResourceScope()),
|
||||
).rejects.toMatchObject(expectedError);
|
||||
).rejects.toMatchObject({
|
||||
code: "language_definition_invalid",
|
||||
detail: { path },
|
||||
cause: expectedError,
|
||||
});
|
||||
},
|
||||
);
|
||||
Reference in new issue
Block a user