fix(ghidra): admit carved MIPS ELF with incomplete section tables (#1629)
* fix(ghidra): inspect carved MIPS ABI declarations without missing sections * test: share filesystem fixtures and cleanup assertions
This commit is contained in:
@@ -56,6 +56,41 @@ const waitForNoMatchingDescriptor = async (identity: Stats): Promise<void> => {
|
||||
expect(await matchingDescriptorCount(identity)).toBe(0);
|
||||
};
|
||||
|
||||
const createUnpackedFixture = async () => {
|
||||
const root = await createTestTempDirectory("rea-asar-unpacked-");
|
||||
const source = join(root, "source");
|
||||
const archive = join(root, "fixture.asar");
|
||||
await mkdir(source);
|
||||
await writeFile(join(source, "small.js"), "module.exports = 1;\n");
|
||||
await createPackageWithOptions(source, archive, { unpack: "small.js" });
|
||||
const unpackedPath = join(`${archive}.unpacked`, "small.js");
|
||||
return {
|
||||
archive,
|
||||
unpackedPath,
|
||||
identity: await lstat(unpackedPath),
|
||||
};
|
||||
};
|
||||
|
||||
const unpackedMember = async (reader: AsarArtifactReader) => {
|
||||
const entries = [];
|
||||
for await (const entry of reader.entries()) entries.push(entry);
|
||||
const member = entries.find(({ path }) => path === "small.js");
|
||||
if (member === undefined) throw new Error("Expected unpacked ASAR member");
|
||||
return member;
|
||||
};
|
||||
|
||||
const expectRetainedHandleRecovery = async (
|
||||
reader: AsarArtifactReader,
|
||||
identity: Stats,
|
||||
closeCount: () => number,
|
||||
): Promise<void> => {
|
||||
expect(closeCount()).toBe(1);
|
||||
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
||||
await reader.close();
|
||||
expect(closeCount()).toBe(2);
|
||||
await waitForNoMatchingDescriptor(identity);
|
||||
};
|
||||
|
||||
describe("ASAR entry streaming", () => {
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"closes a packed member handle when its unopened output stream is destroyed",
|
||||
@@ -292,14 +327,7 @@ describe("ASAR read failures", () => {
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"closes the opened unpacked file and retains ASAR context when handle stat fails",
|
||||
async () => {
|
||||
const root = await createTestTempDirectory("rea-asar-unpacked-stat-");
|
||||
const source = join(root, "source");
|
||||
const archive = join(root, "fixture.asar");
|
||||
await mkdir(source);
|
||||
await writeFile(join(source, "small.js"), "module.exports = 1;\n");
|
||||
await createPackageWithOptions(source, archive, { unpack: "small.js" });
|
||||
const unpackedPath = join(`${archive}.unpacked`, "small.js");
|
||||
const identity = await lstat(unpackedPath);
|
||||
const { archive, unpackedPath, identity } = await createUnpackedFixture();
|
||||
const ioError = Object.assign(new Error("device stat failed"), {
|
||||
code: "EIO",
|
||||
errno: -5,
|
||||
@@ -328,11 +356,7 @@ describe("ASAR read failures", () => {
|
||||
});
|
||||
|
||||
try {
|
||||
const entries = [];
|
||||
for await (const entry of reader.entries()) entries.push(entry);
|
||||
const entry = entries.find(({ path }) => path === "small.js");
|
||||
if (entry === undefined)
|
||||
throw new Error("Expected unpacked ASAR member");
|
||||
const entry = await unpackedMember(reader);
|
||||
|
||||
await expect(reader.open(entry)).rejects.toMatchObject({
|
||||
name: "ArtifactReaderFailure",
|
||||
@@ -349,11 +373,7 @@ describe("ASAR read failures", () => {
|
||||
reason: "device close failed",
|
||||
},
|
||||
});
|
||||
expect(closeCount).toBe(1);
|
||||
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
||||
await reader.close();
|
||||
expect(closeCount).toBe(2);
|
||||
await waitForNoMatchingDescriptor(identity);
|
||||
await expectRetainedHandleRecovery(reader, identity, () => closeCount);
|
||||
} finally {
|
||||
await reader.close();
|
||||
}
|
||||
@@ -364,14 +384,7 @@ describe("ASAR read failures", () => {
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"retries cleanup when cancellation arrives immediately after unpacked stat",
|
||||
async () => {
|
||||
const root = await createTestTempDirectory("rea-asar-unpacked-abort-stat-");
|
||||
const source = join(root, "source");
|
||||
const archive = join(root, "fixture.asar");
|
||||
await mkdir(source);
|
||||
await writeFile(join(source, "small.js"), "module.exports = 1;\n");
|
||||
await createPackageWithOptions(source, archive, { unpack: "small.js" });
|
||||
const unpackedPath = join(`${archive}.unpacked`, "small.js");
|
||||
const identity = await lstat(unpackedPath);
|
||||
const { archive, unpackedPath, identity } = await createUnpackedFixture();
|
||||
const controller = new AbortController();
|
||||
let closeCount = 0;
|
||||
const reader = new AsarArtifactReader(archive, async (path, flags) => {
|
||||
@@ -392,10 +405,7 @@ it.skipIf(process.platform === "win32")(
|
||||
});
|
||||
|
||||
try {
|
||||
const entries = [];
|
||||
for await (const entry of reader.entries()) entries.push(entry);
|
||||
const entry = entries.find(({ path }) => path === "small.js");
|
||||
if (entry === undefined) throw new Error("Expected unpacked ASAR member");
|
||||
const entry = await unpackedMember(reader);
|
||||
|
||||
await expect(reader.open(entry, controller.signal)).rejects.toMatchObject(
|
||||
{
|
||||
@@ -406,11 +416,7 @@ it.skipIf(process.platform === "win32")(
|
||||
},
|
||||
},
|
||||
);
|
||||
expect(closeCount).toBe(1);
|
||||
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
||||
await reader.close();
|
||||
expect(closeCount).toBe(2);
|
||||
await waitForNoMatchingDescriptor(identity);
|
||||
await expectRetainedHandleRecovery(reader, identity, () => closeCount);
|
||||
} finally {
|
||||
await reader.close();
|
||||
}
|
||||
@@ -420,14 +426,7 @@ it.skipIf(process.platform === "win32")(
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"preserves an unpacked identity failure when closing its handle fails",
|
||||
async () => {
|
||||
const root = await createTestTempDirectory("rea-asar-unpacked-identity-");
|
||||
const source = join(root, "source");
|
||||
const archive = join(root, "fixture.asar");
|
||||
await mkdir(source);
|
||||
await writeFile(join(source, "small.js"), "module.exports = 1;\n");
|
||||
await createPackageWithOptions(source, archive, { unpack: "small.js" });
|
||||
const unpackedPath = join(`${archive}.unpacked`, "small.js");
|
||||
const identity = await lstat(unpackedPath);
|
||||
const { archive, unpackedPath, identity } = await createUnpackedFixture();
|
||||
const closeError = new Error("first close failed");
|
||||
let closeCount = 0;
|
||||
const reader = new AsarArtifactReader(archive, async (path, flags) => {
|
||||
@@ -449,10 +448,7 @@ it.skipIf(process.platform === "win32")(
|
||||
});
|
||||
|
||||
try {
|
||||
const entries = [];
|
||||
for await (const entry of reader.entries()) entries.push(entry);
|
||||
const entry = entries.find(({ path }) => path === "small.js");
|
||||
if (entry === undefined) throw new Error("Expected unpacked ASAR member");
|
||||
const entry = await unpackedMember(reader);
|
||||
|
||||
await expect(reader.open(entry)).rejects.toMatchObject({
|
||||
name: "ArtifactReaderFailure",
|
||||
@@ -465,11 +461,7 @@ it.skipIf(process.platform === "win32")(
|
||||
reason: "first close failed",
|
||||
},
|
||||
});
|
||||
expect(closeCount).toBe(1);
|
||||
expect(await matchingDescriptorCount(identity)).toBeGreaterThan(0);
|
||||
await reader.close();
|
||||
expect(closeCount).toBe(2);
|
||||
await waitForNoMatchingDescriptor(identity);
|
||||
await expectRetainedHandleRecovery(reader, identity, () => closeCount);
|
||||
} finally {
|
||||
await reader.close();
|
||||
}
|
||||
@@ -479,14 +471,7 @@ it.skipIf(process.platform === "win32")(
|
||||
it.skipIf(process.platform === "win32")(
|
||||
"retains unpacked handles and snapshots across combined cleanup failures",
|
||||
async () => {
|
||||
const root = await createTestTempDirectory("rea-asar-combined-cleanup-");
|
||||
const source = join(root, "source");
|
||||
const archive = join(root, "fixture.asar");
|
||||
await mkdir(source);
|
||||
await writeFile(join(source, "small.js"), "module.exports = 1;\n");
|
||||
await createPackageWithOptions(source, archive, { unpack: "small.js" });
|
||||
const unpackedPath = join(`${archive}.unpacked`, "small.js");
|
||||
const identity = await lstat(unpackedPath);
|
||||
const { archive, unpackedPath, identity } = await createUnpackedFixture();
|
||||
const closeError = new Error("unpacked close denied");
|
||||
const snapshotError = new Error("snapshot removal denied");
|
||||
let closeCount = 0;
|
||||
@@ -519,10 +504,7 @@ it.skipIf(process.platform === "win32")(
|
||||
);
|
||||
|
||||
try {
|
||||
const entries = [];
|
||||
for await (const entry of reader.entries()) entries.push(entry);
|
||||
const entry = entries.find(({ path }) => path === "small.js");
|
||||
if (entry === undefined) throw new Error("Expected unpacked ASAR member");
|
||||
const entry = await unpackedMember(reader);
|
||||
|
||||
await expect(reader.open(entry)).rejects.toMatchObject({
|
||||
reason: "io",
|
||||
|
||||
@@ -8,6 +8,7 @@ import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
import type { MipsElfMetadata } from "../../../src/domain/binaryTargetTypes.js";
|
||||
import { readMipsElfAbiFlags } from "../../../src/application/MipsElfAbiFlags.js";
|
||||
import { ok } from "../../../src/domain/result.js";
|
||||
import {
|
||||
ghidraMipsProfileParameters,
|
||||
ghidraMipsUnsupportedReason,
|
||||
@@ -52,17 +53,44 @@ const fixture = (little = true): Buffer => {
|
||||
bytes.set([0, 0, 32, 2, 1, 1, 0, 5], abiOffset);
|
||||
return bytes;
|
||||
};
|
||||
const writeFixture = async (bytes: Buffer): Promise<string> => {
|
||||
const directory = await createTestTempDirectory("rea-mips-abi-test-");
|
||||
const path = join(directory, "fixture.elf");
|
||||
await writeFile(path, bytes);
|
||||
return path;
|
||||
};
|
||||
const resolveFixture = async (bytes: Buffer) => {
|
||||
const path = await writeFixture(bytes);
|
||||
const result = await parseBinaryTarget(path);
|
||||
if (!result.ok) throw result.error;
|
||||
if (
|
||||
result.value.kind !== "executable" ||
|
||||
result.value.architecture !== "mips"
|
||||
)
|
||||
throw new Error("Expected a resolved MIPS executable");
|
||||
assert.equal(result.value.format, "elf");
|
||||
assert.equal(result.value.path, path);
|
||||
assert.equal(
|
||||
result.value.sha256,
|
||||
createHash("sha256").update(bytes).digest("hex"),
|
||||
);
|
||||
assert.deepEqual(await readFile(path), bytes);
|
||||
return result.value;
|
||||
};
|
||||
const inspect = async (
|
||||
bytes: Buffer,
|
||||
little = true,
|
||||
checkCancelled: () => void = () => {},
|
||||
) => {
|
||||
const directory = await createTestTempDirectory("rea-mips-abi-test-");
|
||||
const path = join(directory, "fixture.elf");
|
||||
await writeFile(path, bytes);
|
||||
const path = await writeFixture(bytes);
|
||||
const handle = await open(path, "r");
|
||||
try {
|
||||
return await readMipsElfAbiFlags(handle, metadata(little), checkCancelled);
|
||||
const result = await readMipsElfAbiFlags(
|
||||
handle,
|
||||
metadata(little),
|
||||
checkCancelled,
|
||||
);
|
||||
return result.ok ? ok(result.value.abiFlags) : result;
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
@@ -116,6 +144,62 @@ it("supports stripped section tables using PT_MIPS_ABIFLAGS", async () => {
|
||||
assert.ok(result.ok);
|
||||
assert.equal(result.value?.fpAbi, 5);
|
||||
});
|
||||
for (const little of [true, false]) {
|
||||
for (const availableBytes of [0, 7, 47]) {
|
||||
it(`resolves a carved ${little ? "LE" : "BE"} ELF with ${availableBytes} captured section-table bytes`, async () => {
|
||||
const original = fixture(little);
|
||||
const bytes = Buffer.concat([original, Buffer.alloc(availableBytes)]);
|
||||
const sectionOffset = original.length + (availableBytes === 0 ? 1 : 0);
|
||||
if (little) bytes.writeUInt32LE(sectionOffset, 32);
|
||||
else bytes.writeUInt32BE(sectionOffset, 32);
|
||||
const resolved = await resolveFixture(bytes);
|
||||
assert.equal(resolved.mips.abiFlags?.fpAbi, 5);
|
||||
assert.equal(ghidraMipsUnsupportedReason(resolved), null);
|
||||
assert.match(
|
||||
resolved.mips.limitations?.join(" ") ?? "",
|
||||
/section header table/u,
|
||||
);
|
||||
});
|
||||
}
|
||||
}
|
||||
it("keeps declarations in uncaptured section headers unknown when program ABI data is absent", async () => {
|
||||
const bytes = fixture();
|
||||
bytes.writeUInt32LE(bytes.length + 1, 32);
|
||||
bytes.writeUInt32LE(0, 52);
|
||||
const resolved = await resolveFixture(bytes);
|
||||
assert.equal(resolved.mips.abiFlags, null);
|
||||
assert.match(
|
||||
ghidraMipsUnsupportedReason(resolved) ?? "",
|
||||
/inspected ABI.*unavailable headers remain unknown/u,
|
||||
);
|
||||
});
|
||||
for (const failure of ["conflicting", "outside", "duplicate"]) {
|
||||
it(`retains ${failure} ABI errors in a partly captured section table`, async () => {
|
||||
const original = fixture();
|
||||
const count = failure === "duplicate" ? 2 : 1;
|
||||
const bytes = Buffer.concat([original, Buffer.alloc(count * 40 + 7)]);
|
||||
bytes.writeUInt32LE(original.length, 32);
|
||||
bytes.writeUInt16LE(count + 1, 48);
|
||||
original.copy(bytes, abiOffset + 24, abiOffset, abiOffset + 24);
|
||||
bytes[abiOffset + 24 + 7] = 6;
|
||||
for (let i = 0; i < count; i += 1) {
|
||||
const position = original.length + i * 40;
|
||||
bytes.writeUInt32LE(0x7000002a, position + 4);
|
||||
bytes.writeUInt32LE(
|
||||
failure === "outside"
|
||||
? 0xfffffff0
|
||||
: failure === "conflicting"
|
||||
? abiOffset + 24
|
||||
: abiOffset,
|
||||
position + 16,
|
||||
);
|
||||
bytes.writeUInt32LE(24, position + 20);
|
||||
}
|
||||
const result = await inspect(bytes);
|
||||
assert.ok(!result.ok);
|
||||
assert.match(result.error, new RegExp(failure, "u"));
|
||||
});
|
||||
}
|
||||
it("supports a section-only ABI record without assuming its name", async () => {
|
||||
const bytes = fixture();
|
||||
bytes.writeUInt32LE(0, 28);
|
||||
@@ -239,49 +323,21 @@ it("preserves the caller's cancellation error during table inspection", async ()
|
||||
|
||||
for (const little of [true, false]) {
|
||||
it(`resolves full-file MIPS ABI identity before provider selection (${little ? "LE" : "BE"})`, async () => {
|
||||
const directory = await createTestTempDirectory("rea-mips-resolution-");
|
||||
const path = join(directory, "fixture.elf");
|
||||
const bytes = fixture(little);
|
||||
await writeFile(path, bytes);
|
||||
const result = await parseBinaryTarget(path);
|
||||
if (!result.ok) throw result.error;
|
||||
assert.equal(result.value.architecture, "mips");
|
||||
assert.equal(result.value.format, "elf");
|
||||
assert.equal(
|
||||
result.value.sha256,
|
||||
createHash("sha256").update(bytes).digest("hex"),
|
||||
);
|
||||
if (
|
||||
result.value.kind !== "executable" ||
|
||||
result.value.architecture !== "mips"
|
||||
)
|
||||
throw new Error("Expected a resolved MIPS executable");
|
||||
assert.equal(result.value.mips.byteOrder, little ? "little" : "big");
|
||||
assert.equal(result.value.mips.abiFlags?.fpAbi, 5);
|
||||
assert.equal(ghidraMipsUnsupportedReason(result.value), null);
|
||||
assert.deepEqual(await readFile(path), bytes);
|
||||
const resolved = await resolveFixture(bytes);
|
||||
assert.equal(resolved.mips.byteOrder, little ? "little" : "big");
|
||||
assert.equal(resolved.mips.abiFlags?.fpAbi, 5);
|
||||
assert.equal(ghidraMipsUnsupportedReason(resolved), null);
|
||||
});
|
||||
}
|
||||
|
||||
it("keeps missing ABI data unknown through the public target resolver", async () => {
|
||||
const directory = await createTestTempDirectory("rea-mips-resolution-");
|
||||
const path = join(directory, "fixture.elf");
|
||||
const bytes = fixture();
|
||||
bytes.writeUInt32LE(0, 52);
|
||||
bytes.writeUInt32LE(0, 172);
|
||||
await writeFile(path, bytes);
|
||||
const result = await parseBinaryTarget(path);
|
||||
if (!result.ok) throw result.error;
|
||||
if (
|
||||
result.value.kind !== "executable" ||
|
||||
result.value.architecture !== "mips"
|
||||
)
|
||||
throw new Error("Expected a resolved MIPS executable");
|
||||
assert.equal(result.value.mips.abiFlags, null);
|
||||
assert.match(
|
||||
ghidraMipsUnsupportedReason(result.value) ?? "",
|
||||
/inspected ABI/u,
|
||||
);
|
||||
const resolved = await resolveFixture(bytes);
|
||||
assert.equal(resolved.mips.abiFlags, null);
|
||||
assert.match(ghidraMipsUnsupportedReason(resolved) ?? "", /inspected ABI/u);
|
||||
});
|
||||
|
||||
it("projects a malformed out-of-probe ABI record as a target error", async () => {
|
||||
|
||||
Reference in New Issue
Block a user