feat(ghidra): admit verified MIPS16 shared ELF images
Admit the verified ELF32 o32 MIPS32r2 MIPS16 shared-image profile after complete source load and relocation inspection. Preserve caller-selected bytes, pin the endian-matched language, and report per-address decoder context. Refuse unverified relocation shapes with explicit reasons. Verified on Linux x64 with Ghidra 12.1.4: eight source-owned public CLI/MCP controls, original-artifact instruction/pseudocode/export replay, snapshot restore and owned cleanup. 152 integrated focused tests, static checks and documentation checks passed. Fixes #1851 Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com> Co-authored-by: Kaoru0822 <208281322+kaoru0822-kitauji@users.noreply.github.com>
This commit is contained in:
co-authored by
factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
Kaoru0822
parent
6395e8d300
commit
e9fe6437a7
@@ -7,7 +7,7 @@ import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver
|
||||
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 { readMipsElfMetadata } from "../../../src/application/MipsElfMetadata.js";
|
||||
import { ok } from "../../../src/domain/result.js";
|
||||
import {
|
||||
ghidraMipsProfileParameters,
|
||||
@@ -85,7 +85,7 @@ const inspect = async (
|
||||
const path = await writeFixture(bytes);
|
||||
const handle = await open(path, "r");
|
||||
try {
|
||||
const result = await readMipsElfAbiFlags(
|
||||
const result = await readMipsElfMetadata(
|
||||
handle,
|
||||
metadata(little),
|
||||
checkCancelled,
|
||||
|
||||
@@ -0,0 +1,260 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { createHash } from "node:crypto";
|
||||
import { open, readFile, writeFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { it } from "vitest";
|
||||
import { parseBinaryTarget } from "../../../src/application/BinaryTargetResolver.js";
|
||||
import { readMipsElfMetadata } from "../../../src/application/MipsElfMetadata.js";
|
||||
import { AnalysisCancelledError } from "../../../src/domain/analysisErrorCore.js";
|
||||
import {
|
||||
ghidraMipsProfileParameters,
|
||||
ghidraMipsUnsupportedReason,
|
||||
} from "../../../src/ghidra/GhidraMipsProfile.js";
|
||||
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
|
||||
|
||||
const relOffset = 4096;
|
||||
const fixture = (little: boolean, count = 2): Buffer => {
|
||||
const bytes = Buffer.alloc(relOffset + count * 8 + 16);
|
||||
bytes.set([0x7f, 0x45, 0x4c, 0x46, 1, little ? 1 : 2, 1]);
|
||||
const u16 = (value: number, offset: number) =>
|
||||
little
|
||||
? bytes.writeUInt16LE(value, offset)
|
||||
: bytes.writeUInt16BE(value, offset);
|
||||
const u32 = (value: number, offset: number) =>
|
||||
little
|
||||
? bytes.writeUInt32LE(value, offset)
|
||||
: bytes.writeUInt32BE(value, offset);
|
||||
u16(3, 16);
|
||||
u16(8, 18);
|
||||
u32(1, 20);
|
||||
u32(52, 28);
|
||||
u32(0x74001005, 36);
|
||||
u16(52, 40);
|
||||
u16(32, 42);
|
||||
u16(2, 44);
|
||||
u32(1, 52);
|
||||
u32(bytes.length, 68);
|
||||
u32(bytes.length, 72);
|
||||
u32(2, 84);
|
||||
u32(256, 88);
|
||||
u32(256, 92);
|
||||
u32(32, 100);
|
||||
u32(32, 104);
|
||||
u32(17, 256);
|
||||
u32(relOffset, 260);
|
||||
u32(18, 264);
|
||||
u32(count * 8, 268);
|
||||
u32(19, 272);
|
||||
u32(8, 276);
|
||||
for (let i = 1; i < count; i += 1) {
|
||||
u32(512, relOffset + 8 * i);
|
||||
u32(3, relOffset + 8 * i + 4);
|
||||
}
|
||||
return bytes;
|
||||
};
|
||||
const resolve = async (bytes: Buffer) => {
|
||||
const directory = await createTestTempDirectory("rea-mips-shared-test-");
|
||||
const path = join(directory, "shared.elf");
|
||||
await writeFile(path, bytes);
|
||||
const result = await parseBinaryTarget(path);
|
||||
assert.ok(result.ok);
|
||||
assert.equal(result.value.kind, "executable");
|
||||
assert.equal(result.value.architecture, "mips");
|
||||
if (
|
||||
result.value.kind !== "executable" ||
|
||||
result.value.architecture !== "mips"
|
||||
)
|
||||
throw new Error("expected MIPS ELF");
|
||||
assert.equal(
|
||||
result.value.sha256,
|
||||
createHash("sha256").update(bytes).digest("hex"),
|
||||
);
|
||||
assert.deepEqual(await readFile(path), bytes);
|
||||
return result.value;
|
||||
};
|
||||
|
||||
for (const little of [true, false]) {
|
||||
it(`retains complete dynamic declarations beyond the initial probe (${little ? "LE" : "BE"})`, async () => {
|
||||
const target = await resolve(fixture(little));
|
||||
assert.equal(target.mips.abiFlags, null);
|
||||
assert.deepEqual(target.mips.sharedLoad, {
|
||||
status: "complete",
|
||||
relocationCount: 2,
|
||||
relocationTypes: [0, 3],
|
||||
symbolRelativeCount: 0,
|
||||
});
|
||||
assert.equal(ghidraMipsUnsupportedReason(target), null);
|
||||
assert.deepEqual(ghidraMipsProfileParameters(target).mips_elf, {
|
||||
elf_class: 32,
|
||||
byte_order: little ? "little" : "big",
|
||||
type: 3,
|
||||
flags: 0x74001005,
|
||||
abi_flags: null,
|
||||
shared_load: {
|
||||
status: "complete",
|
||||
relocation_count: 2,
|
||||
relocation_types: [0, 3],
|
||||
symbol_relative_count: 0,
|
||||
},
|
||||
});
|
||||
});
|
||||
it(`retains and refuses symbol-bearing REL32 without repairing the mode tag (${little ? "LE" : "BE"})`, async () => {
|
||||
const bytes = fixture(little);
|
||||
if (little) bytes.writeUInt32LE(0x103, relOffset + 12);
|
||||
else bytes.writeUInt32BE(0x103, relOffset + 12);
|
||||
const target = await resolve(bytes);
|
||||
assert.equal(target.mips.sharedLoad?.status, "complete");
|
||||
assert.match(
|
||||
ghidraMipsUnsupportedReason(target) ?? "",
|
||||
/Symbol-bearing R_MIPS_REL32/u,
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
it("examines relocation records beyond a read chunk before deciding completeness", async () => {
|
||||
const count = 8194;
|
||||
const bytes = fixture(true, count);
|
||||
bytes.writeUInt32LE(0x103, relOffset + (count - 1) * 8 + 4);
|
||||
const target = await resolve(bytes);
|
||||
assert.deepEqual(target.mips.sharedLoad, {
|
||||
status: "complete",
|
||||
relocationCount: count,
|
||||
relocationTypes: [0, 3],
|
||||
symbolRelativeCount: 1,
|
||||
});
|
||||
assert.match(ghidraMipsUnsupportedReason(target) ?? "", /mode tag/u);
|
||||
});
|
||||
|
||||
it("does not treat uncaptured ABI declaration headers as an inspected absence", async () => {
|
||||
const bytes = fixture(true);
|
||||
bytes.writeUInt32LE(bytes.length + 1, 32);
|
||||
bytes.writeUInt16LE(40, 46);
|
||||
bytes.writeUInt16LE(1, 48);
|
||||
const target = await resolve(bytes);
|
||||
assert.equal(target.mips.abiFlags, null);
|
||||
assert.equal(target.mips.sharedLoad?.status, "unavailable");
|
||||
assert.match(
|
||||
ghidraMipsUnsupportedReason(target) ?? "",
|
||||
/unavailable headers remain unknown/u,
|
||||
);
|
||||
});
|
||||
|
||||
const malformed: [string, (bytes: Buffer) => void, RegExp][] = [
|
||||
[
|
||||
"overlapping load ranges",
|
||||
(b) => {
|
||||
b.writeUInt16LE(3, 44);
|
||||
b.writeUInt32LE(1, 116);
|
||||
b.writeUInt32LE(256, 124);
|
||||
b.writeUInt32LE(16, 132);
|
||||
b.writeUInt32LE(16, 136);
|
||||
},
|
||||
/overlapping MIPS PT_LOAD/u,
|
||||
],
|
||||
[
|
||||
"inconsistent dynamic address",
|
||||
(b) => b.writeUInt32LE(0x10000, 92),
|
||||
/matching file-backed PT_LOAD/u,
|
||||
],
|
||||
["missing dynamic table", (b) => b.writeUInt32LE(0, 84), /no PT_DYNAMIC/u],
|
||||
["repeated dynamic tables", (b) => b.writeUInt32LE(2, 52), /duplicate/u],
|
||||
[
|
||||
"declared dynamic tail outside file",
|
||||
(b) => b.writeUInt32LE(b.length, 100),
|
||||
/beyond the file/u,
|
||||
],
|
||||
[
|
||||
"unavailable load bytes",
|
||||
(b) => b.writeUInt32LE(b.length + 1, 68),
|
||||
/PT_LOAD range/u,
|
||||
],
|
||||
["missing terminator", (b) => b.writeUInt32LE(1, 280), /DT_NULL/u],
|
||||
[
|
||||
"repeated scalar tags",
|
||||
(b) => b.writeUInt32LE(17, 272),
|
||||
/duplicate MIPS dynamic tag/u,
|
||||
],
|
||||
["invalid REL entry size", (b) => b.writeUInt32LE(16, 276), /DT_RELENT/u],
|
||||
["truncated REL record", (b) => b.writeUInt32LE(17, 268), /DT_RELENT/u],
|
||||
[
|
||||
"unmapped REL table",
|
||||
(b) => b.writeUInt32LE(0x10000, 260),
|
||||
/PT_LOAD mapping/u,
|
||||
],
|
||||
[
|
||||
"unmapped REL destination",
|
||||
(b) => b.writeUInt32LE(0x10000, relOffset + 8),
|
||||
/PT_LOAD memory range/u,
|
||||
],
|
||||
[
|
||||
"RELA declaration",
|
||||
(b) => {
|
||||
b.writeUInt32LE(7, 272);
|
||||
b.writeUInt32LE(4096, 276);
|
||||
},
|
||||
/RELA or PLT/u,
|
||||
],
|
||||
];
|
||||
it.each(malformed)(
|
||||
"preserves useful partial header facts for %s",
|
||||
async (_name, mutate, reason) => {
|
||||
const bytes = fixture(true);
|
||||
mutate(bytes);
|
||||
const target = await resolve(bytes);
|
||||
assert.equal(target.mips.flags, 0x74001005);
|
||||
assert.equal(target.mips.abiFlags, null);
|
||||
assert.equal(target.mips.sharedLoad?.status, "unavailable");
|
||||
assert.match(ghidraMipsUnsupportedReason(target) ?? "", reason);
|
||||
},
|
||||
);
|
||||
|
||||
it("retains unverified relocation type instead of claiming every examined record is supported", async () => {
|
||||
const bytes = fixture(true);
|
||||
bytes.writeUInt32LE(5, relOffset + 12);
|
||||
const target = await resolve(bytes);
|
||||
assert.deepEqual(target.mips.sharedLoad, {
|
||||
status: "complete",
|
||||
relocationCount: 2,
|
||||
relocationTypes: [0, 5],
|
||||
symbolRelativeCount: 0,
|
||||
});
|
||||
assert.match(
|
||||
ghidraMipsUnsupportedReason(target) ?? "",
|
||||
/other dynamic relocation types/u,
|
||||
);
|
||||
});
|
||||
|
||||
it("does not equate complete dynamic REL coverage with coverage of another section relocation table", async () => {
|
||||
const bytes = fixture(true);
|
||||
bytes.writeUInt32LE(1024, 32);
|
||||
bytes.writeUInt16LE(40, 46);
|
||||
bytes.writeUInt16LE(1, 48);
|
||||
bytes.writeUInt32LE(9, 1028);
|
||||
bytes.writeUInt32LE(2048, 1040);
|
||||
bytes.writeUInt32LE(8, 1044);
|
||||
bytes.writeUInt32LE(8, 1060);
|
||||
const target = await resolve(bytes);
|
||||
assert.equal(target.mips.sharedLoad?.status, "unavailable");
|
||||
assert.match(
|
||||
ghidraMipsUnsupportedReason(target) ?? "",
|
||||
/section relocation declarations/u,
|
||||
);
|
||||
});
|
||||
|
||||
it("preserves the caller cancellation error during a bounded relocation scan", async () => {
|
||||
const target = await resolve(fixture(true, 8194));
|
||||
const handle = await open(target.path, "r");
|
||||
const cancellation = new AnalysisCancelledError("open_binary");
|
||||
let checks = 0;
|
||||
try {
|
||||
await assert.rejects(
|
||||
readMipsElfMetadata(handle, target.mips, () => {
|
||||
checks += 1;
|
||||
if (checks === 100) throw cancellation;
|
||||
}),
|
||||
(error) => error === cancellation,
|
||||
);
|
||||
} finally {
|
||||
await handle.close();
|
||||
}
|
||||
});
|
||||
@@ -0,0 +1,22 @@
|
||||
/* Build an older-style shared image from source without ABI declarations. */
|
||||
PHDRS {
|
||||
headers PT_PHDR PHDRS;
|
||||
text PT_LOAD FILEHDR PHDRS FLAGS(5);
|
||||
data PT_LOAD FLAGS(6);
|
||||
dynamic PT_DYNAMIC FLAGS(6);
|
||||
}
|
||||
SECTIONS {
|
||||
. = SIZEOF_HEADERS;
|
||||
.hash : { *(.hash) } :text
|
||||
.gnu.hash : { *(.gnu.hash) } :text
|
||||
.dynsym : { *(.dynsym) } :text
|
||||
.dynstr : { *(.dynstr) } :text
|
||||
.rel.dyn : { *(.rel.*) } :text
|
||||
.text : { *(.text*) } :text
|
||||
. = ALIGN(0x10000);
|
||||
.dynamic : { *(.dynamic) } :data :dynamic
|
||||
.got : { *(.got*) } :data
|
||||
.data : { *(.data*) } :data
|
||||
.bss : { *(.bss*) *(COMMON) } :data
|
||||
/DISCARD/ : { *(.MIPS.abiflags) *(.reginfo) *(.pdr) *(.mdebug.*) *(.comment) *(.note.*) }
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
import assert from "node:assert/strict";
|
||||
import { execFile } from "node:child_process";
|
||||
import { createHash } from "node:crypto";
|
||||
import { readFile } from "node:fs/promises";
|
||||
import { join } from "node:path";
|
||||
import { fileURLToPath } from "node:url";
|
||||
import { promisify } from "node:util";
|
||||
|
||||
const execute = promisify(execFile);
|
||||
const source = fileURLToPath(new URL("./mips16-shared.S", import.meta.url));
|
||||
const linkerScript = fileURLToPath(
|
||||
new URL("./mips16-recordless.ld", import.meta.url),
|
||||
);
|
||||
const hash = (bytes) => createHash("sha256").update(bytes).digest("hex");
|
||||
|
||||
/** BYO GNU cross-binutils; generated controls are never executed or byte-patched. */
|
||||
export async function buildMips16SharedFixture(directory, byteOrder, variant) {
|
||||
assert(["little", "big"].includes(byteOrder));
|
||||
assert(
|
||||
[
|
||||
"declared",
|
||||
"recordless",
|
||||
"symbol-relative",
|
||||
"section-relocations",
|
||||
].includes(variant),
|
||||
);
|
||||
const assembler = process.env.REA_MIPS_AS ?? "mips-linux-gnu-as";
|
||||
const linker = process.env.REA_MIPS_LD ?? "mips-linux-gnu-ld";
|
||||
const endian = byteOrder === "little" ? "-EL" : "-EB";
|
||||
const prefix = join(directory, `${byteOrder}-${variant}`);
|
||||
const object = `${prefix}.o`;
|
||||
const path = `${prefix}.so`;
|
||||
const assemblerVersion = (
|
||||
await execute(assembler, ["--version"], { timeout: 10000 })
|
||||
).stdout;
|
||||
const linkerVersion = (
|
||||
await execute(linker, ["--version"], { timeout: 10000 })
|
||||
).stdout;
|
||||
assert(
|
||||
assemblerVersion.includes("GNU assembler"),
|
||||
"Use GNU's MIPS assembler: other assemblers have different compressed-instruction behavior.",
|
||||
);
|
||||
assert(linkerVersion.includes("GNU ld"));
|
||||
await execute(
|
||||
assembler,
|
||||
[
|
||||
"-32",
|
||||
"-mips32r2",
|
||||
endian,
|
||||
...(variant === "symbol-relative"
|
||||
? ["--defsym", "REA_MIPS_SYMBOL_RELATIVE=1"]
|
||||
: []),
|
||||
source,
|
||||
"-o",
|
||||
object,
|
||||
],
|
||||
{ timeout: 60000 },
|
||||
);
|
||||
await execute(
|
||||
linker,
|
||||
[
|
||||
endian,
|
||||
"-shared",
|
||||
...(variant === "section-relocations" ? ["--emit-relocs"] : []),
|
||||
...(variant === "recordless" ? ["-T", linkerScript] : []),
|
||||
object,
|
||||
"-o",
|
||||
path,
|
||||
],
|
||||
{ timeout: 60000 },
|
||||
);
|
||||
const bytes = await readFile(path);
|
||||
const little = byteOrder === "little";
|
||||
const u16 = (offset) =>
|
||||
little ? bytes.readUInt16LE(offset) : bytes.readUInt16BE(offset);
|
||||
const u32 = (offset) =>
|
||||
little ? bytes.readUInt32LE(offset) : bytes.readUInt32BE(offset);
|
||||
assert.equal(
|
||||
bytes.subarray(0, 6).toString("hex"),
|
||||
little ? "7f454c460101" : "7f454c460102",
|
||||
);
|
||||
assert.equal(u16(16), 3);
|
||||
assert.equal(u16(18), 8);
|
||||
assert.equal(u32(36), 0x74001007); // noreorder/PIC/CPIC plus o32/MIPS16/MIPS32r2.
|
||||
const headers = Array.from(
|
||||
{ length: u16(44) },
|
||||
(_, i) => u32(28) + i * u16(42),
|
||||
);
|
||||
const fileOffset = (address, size) => {
|
||||
const mappings = headers.filter(
|
||||
(offset) =>
|
||||
u32(offset) === 1 &&
|
||||
address >= u32(offset + 8) &&
|
||||
address - u32(offset + 8) <= u32(offset + 16) - size,
|
||||
);
|
||||
assert.equal(mappings.length, 1);
|
||||
return u32(mappings[0] + 4) + address - u32(mappings[0] + 8);
|
||||
};
|
||||
const readelf = await execute(
|
||||
process.env.REA_MIPS_READELF ?? "readelf",
|
||||
["-W", "-h", "-l", "-s", "-r", path],
|
||||
{ timeout: 10000 },
|
||||
);
|
||||
const symbols = {};
|
||||
for (const name of [
|
||||
"rea_mips16_probe",
|
||||
"rea_mips32_probe",
|
||||
"rea_mips16_pointer",
|
||||
]) {
|
||||
const addresses = new Set(
|
||||
readelf.stdout
|
||||
.split("\n")
|
||||
.map((line) => line.trim().split(/\s+/u))
|
||||
.filter(
|
||||
(fields) =>
|
||||
fields[0]?.endsWith(":") === true &&
|
||||
["FUNC", "OBJECT"].includes(fields[3]) &&
|
||||
fields.at(-1) === name,
|
||||
)
|
||||
.map((fields) => Number.parseInt(fields[1], 16)),
|
||||
);
|
||||
assert.equal(addresses.size, 1, `Missing or conflicting ${name}`);
|
||||
symbols[name] = [...addresses][0];
|
||||
}
|
||||
assert.equal(symbols.rea_mips16_probe & 1, 1);
|
||||
assert.equal(symbols.rea_mips32_probe & 1, 0);
|
||||
const compressedOffset = fileOffset(symbols.rea_mips16_probe - 1, 8);
|
||||
assert.equal(
|
||||
bytes.subarray(compressedOffset, compressedOffset + 8).toString("hex"),
|
||||
little ? "5a6a8eea074aa0e8" : "6a5aea8e4a07e8a0",
|
||||
);
|
||||
const standardOffset = fileOffset(symbols.rea_mips32_probe, 12);
|
||||
assert.equal(
|
||||
bytes.subarray(standardOffset, standardOffset + 12).toString("hex"),
|
||||
little ? "030082240800e00300000000" : "2482000303e0000800000000",
|
||||
);
|
||||
const pointerOffset = fileOffset(symbols.rea_mips16_pointer, 4);
|
||||
const rawPointer = u32(pointerOffset);
|
||||
if (variant !== "symbol-relative")
|
||||
assert.equal(rawPointer, symbols.rea_mips16_probe);
|
||||
return {
|
||||
path,
|
||||
sha256: hash(bytes),
|
||||
byte_order: byteOrder,
|
||||
variant,
|
||||
elf_flags: u32(36),
|
||||
symbols,
|
||||
raw_pointer: rawPointer,
|
||||
source_sha256: hash(await readFile(source)),
|
||||
linker_script_sha256:
|
||||
variant === "recordless" ? hash(await readFile(linkerScript)) : null,
|
||||
assembler_version: assemblerVersion.split("\n")[0],
|
||||
linker_version: linkerVersion.split("\n")[0],
|
||||
readelf_output: readelf.stdout,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
/* Source-owned mixed-instruction shared-image control. */
|
||||
.abicalls
|
||||
.module fp=xx
|
||||
.module nooddspreg
|
||||
.text
|
||||
.set noreorder
|
||||
.set nomicromips
|
||||
.set mips16
|
||||
.globl rea_mips16_probe
|
||||
.type rea_mips16_probe, @function
|
||||
.ent rea_mips16_probe
|
||||
rea_mips16_probe:
|
||||
li $2, 90
|
||||
xor $2, $4
|
||||
addiu $2, 7
|
||||
jrc $31
|
||||
.end rea_mips16_probe
|
||||
.size rea_mips16_probe, . - rea_mips16_probe
|
||||
.ifndef REA_MIPS_SYMBOL_RELATIVE
|
||||
.hidden rea_mips16_probe
|
||||
.endif
|
||||
|
||||
.set nomips16
|
||||
.globl rea_mips32_probe
|
||||
.type rea_mips32_probe, @function
|
||||
.ent rea_mips32_probe
|
||||
rea_mips32_probe:
|
||||
addiu $2, $4, 3
|
||||
jr $31
|
||||
nop
|
||||
.end rea_mips32_probe
|
||||
.size rea_mips32_probe, . - rea_mips32_probe
|
||||
|
||||
.data
|
||||
.balign 4
|
||||
.globl rea_mips16_pointer
|
||||
.type rea_mips16_pointer, @object
|
||||
.size rea_mips16_pointer, 4
|
||||
rea_mips16_pointer:
|
||||
.word rea_mips16_probe
|
||||
.section .note.GNU-stack, "", @progbits
|
||||
Reference in New Issue
Block a user