feat(ghidra): admit RISC-V position-independent executables (#4487) (#4811)

* feat(ghidra): admit RISC-V position-independent executables (#4487)

Admit little-endian RISC-V ELF32 and ELF64 position-independent executables
(ET_DYN with PT_INTERP) into the pinned generic Ghidra lane.

- Collect `PT_INTERP` during the existing validated ELF table walk in
  `validateElfDeclaredRanges` and record `hasInterpreter` in target metadata,
  supporting arbitrary table offsets and extended program counts without duplicate scanning.
- Admit ET_DYN images when `hasInterpreter` is present under lane identities
  `elf32-pie-le-double-float-generic-v1` and `elf64-pie-le-double-float-generic-v1`.
- Preserve refusal for non-PIE ET_DYN targets (shared libraries without PT_INTERP)
  explaining that shared libraries require separate load verification.
- Record load model facts (`load_model: "pie"`, `expected_default_image_base: "0x10000"`
  or `"0x100000"`, `has_interpreter: true`) in `riscv_elf` analysis profile parameters.
- Record default image base analysis coordinates limitation for PIE targets.
- Update `docs/ghidra-riscv.md` with PIE admission boundary and evidence semantics.
- Add unit tests for PT_INTERP collection, high-offset program headers, and PIE profile resolution.
- Add PIE fixture generation capability to `tests/conformance/ghidra/riscv-fixture.mjs` and
  pass `-Wl,-pie` to ensure bare-metal drivers generate ET_DYN.
- Extend `scripts/verify-real-ghidra-riscv.mjs` to test both ET_EXEC and ET_DYN.

Fixes #4487

* fix(ghidra): check structured facts and rethrow typed errors in PIE tests

* style: format GhidraRiscvProfile.test.ts

* fix(ghidra): restrict PIE admission to verified double-float RISC-V lanes
This commit is contained in:
Abhay Tiwari authored and GitHub committed 2026-11-18 19:44:40 -05:00
1 parent 3e69b37327
commit 8d226251a7
10 files changed
+455 -153

No files matched your search

+34 -25
View File
@@ -15,17 +15,18 @@ supported. Existing PE, managed and Mach-O architecture sets are unchanged.
Headerless inputs use the separate [raw-binary admission
boundary](raw-binary.md); raw RISC-V images are not supported.
The Ghidra lane admits little-endian `ET_EXEC` images with no `EF_RISCV_RVE`,
`EF_RISCV_TSO`, or any bit outside `EF_RISCV_RVC | EF_RISCV_FLOAT_ABI`, in one
of three lanes:
The Ghidra lane admits little-endian `ET_EXEC` images and position-independent
executables (`ET_DYN` with `PT_INTERP`) with no `EF_RISCV_RVE`, `EF_RISCV_TSO`,
or any bit outside `EF_RISCV_RVC | EF_RISCV_FLOAT_ABI`, in one of the supported
lanes:
- ELF64 or ELF32 images that declare `EF_RISCV_FLOAT_ABI_DOUBLE` use Ghidra's
stock double-float model unchanged. Their `.riscv.attributes` is not read for
admission.
- ELF32 images that declare the soft-float ABI (`e_flags` float-ABI bits `0`,
ilp32) use REA's [ilp32 calling-convention policy](#ilp32). They must also
declare an RV32I base and only [decoded extensions](#isa-declarations) in
`Tag_RISCV_arch`.
- ELF32 `ET_EXEC` images that declare the soft-float ABI (`e_flags` float-ABI
bits `0`, ilp32) use REA's [ilp32 calling-convention policy](#ilp32). They
must also declare an RV32I base and only [decoded extensions](#isa-declarations)
in `Tag_RISCV_arch`. Soft-float PIE images remain refused.
These are header declarations, not proof that every instruction conforms to
that ISA.
@@ -33,24 +34,29 @@ that ISA.
Provider selection returns the specific unsupported constraint rather than
guessing a compatible profile:
| Declaration | Outcome |
| -------------------------------------------------------------------------- | -------------------------------------------------------------------------------------------------------------------- |
| Little-endian ELF64 `ET_EXEC`, double-float ABI | admitted, `RISCV:LE:64:default` + `gcc`, lane `elf64-exec-le-double-float-generic-v1` |
| Little-endian ELF32 `ET_EXEC`, double-float ABI | admitted, `RISCV:LE:32:default` + `gcc`, lane `elf32-exec-le-double-float-generic-v1` |
| Little-endian ELF32 `ET_EXEC`, soft-float ABI, decoded extensions only | admitted, `RISCV:LE:32:default` + `gcc` with the `__rea_ilp32` prototype, lane `elf32-exec-le-soft-float-generic-v1` |
| ELF32 soft-float ABI without `Tag_RISCV_arch`, or with any other extension | refused; the reason names the extensions outside the decoded set |
| ELF32 single-float ABI (ilp32f) | refused until its [written policy](#ilp32f) is implemented and verified |
| ELF64 soft- or single-float ABI (lp64, lp64f), or the quad-float ABI | refused; no verified calling-convention policy |
| Big-endian | refused; Ghidra 12.1.4 ships no big-endian RISC-V language |
| `ET_REL`, `ET_DYN`, other `e_type` | refused with the declared value |
| `EF_RISCV_RVE` | refused; the embedded 16-register profile (x0–x15) needs its own processor specification |
| `EF_RISCV_TSO` | refused; the total-store-order memory model is unverified here |
| Reserved or newer `e_flags` bits | refused with the offending bits named |
| Declaration | Outcome |
| -------------------------------------------------------------------------- | ------------------------------------------------------------------------------------------------------------------------------- |
| Little-endian ELF64 `ET_EXEC` or PIE (`ET_DYN`), double-float ABI | admitted, `RISCV:LE:64:default` + `gcc`, lane `elf64-exec-le-double-float-generic-v1` or `elf64-pie-le-double-float-generic-v1` |
| Little-endian ELF32 `ET_EXEC` or PIE (`ET_DYN`), double-float ABI | admitted, `RISCV:LE:32:default` + `gcc`, lane `elf32-exec-le-double-float-generic-v1` or `elf32-pie-le-double-float-generic-v1` |
| Little-endian ELF32 `ET_EXEC`, soft-float ABI, decoded extensions only | admitted, `RISCV:LE:32:default` + `gcc` with the `__rea_ilp32` prototype, lane `elf32-exec-le-soft-float-generic-v1` |
| ELF32 soft-float PIE (`ET_DYN`) | refused; soft-float position-independent executables need separate load verification |
| ELF32 soft-float ABI without `Tag_RISCV_arch`, or with any other extension | refused; the reason names the extensions outside the decoded set |
| ELF32 single-float ABI (ilp32f) | refused until its [written policy](#ilp32f) is implemented and verified |
| ELF64 soft- or single-float ABI (lp64, lp64f), or the quad-float ABI | refused; no verified calling-convention policy |
| Big-endian | refused; Ghidra 12.1.4 ships no big-endian RISC-V language |
| `ET_DYN` without `PT_INTERP` (shared library) | refused; shared libraries without PT_INTERP need separate load verification |
| `ET_REL`, other `e_type` | refused with the declared value |
| `EF_RISCV_RVE` | refused; the embedded 16-register profile (x0–x15) needs its own processor specification |
| `EF_RISCV_TSO` | refused; the total-store-order memory model is unverified here |
| Reserved or newer `e_flags` bits | refused with the offending bits named |
`e_type` is the only load-shape declaration this lane reads. A fixed-address
`ET_EXEC` declaration does **not** establish static linkage: `PT_INTERP` and
dynamic dependencies are not inspected, and the session records that boundary as
a limitation rather than inferring a load model.
`e_type` and program headers decide the load model this lane reads. A
fixed-address `ET_EXEC` declaration does **not** establish static linkage:
dynamic dependencies are not inspected, and the session records that boundary
as a limitation rather than inferring a load model. Position-independent
executables (`ET_DYN` with `PT_INTERP`) are admitted using Ghidra's default
analysis base (`0x10000` for RV32, `0x100000` for RV64); runtime load addresses
and dynamic linker relocations are not executed or verified.
## Why the language is pinned
@@ -253,7 +259,10 @@ Those base strings are admitted. Builds that add `_zba_zbb_zbs`,
The lane identity is one of the lane ids above, and `riscv_elf` retains
`elf_class`, `byte_order`, `type`, and the raw `flags` alongside the read-only
booleans `float_abi_double`, `rvc_declared`, `rve_declared`, and
`tso_declared`. Soft-float profiles also retain `float_abi: soft`, the declared
`tso_declared`. For PIE images, `riscv_elf` also records
`has_interpreter: true`, `load_model: "pie"`, and the expected default image
base (`expected_default_image_base: "0x10000"` for RV32, `"0x100000"` for RV64).
Soft-float profiles also retain `float_abi: soft`, the declared
`arch` string, and `riscv_calling_convention: ilp32-prototype-v1`. Those fields
report declarations the header and attributes make, and the policy REA applied;
none of them is evidence of the original compiler or of instruction
+154 -97
View File
@@ -103,19 +103,28 @@ transport.stderr?.on("data", (chunk) => {
/** The profile a fixture's lane must commit, from independent declarations. */
const expectedLane = (fixture) => {
const soft = fixture.riscv.flags === 1;
const pie = fixture.riscv.type === 3;
return {
lane: `elf${fixture.riscv.elfClass}-exec-le-${soft ? "soft" : "double"}-float-generic-v1`,
lane: `elf${fixture.riscv.elfClass}-${pie ? "pie" : "exec"}-le-${soft ? "soft" : "double"}-float-generic-v1`,
callingConvention: soft ? "ilp32-prototype-v1" : undefined,
riscvElf: {
elf_class: fixture.riscv.elfClass,
byte_order: "little",
type: 2,
type: pie ? 3 : 2,
flags: fixture.riscv.flags,
float_abi_double: !soft,
...(soft ? { float_abi: "soft", arch: fixture.riscv.isa.arch } : {}),
rvc_declared: true,
rve_declared: false,
tso_declared: false,
...(pie
? {
has_interpreter: true,
load_model: "pie",
expected_default_image_base:
fixture.riscv.elfClass === 32 ? "0x10000" : "0x100000",
}
: {}),
},
};
};
@@ -152,12 +161,15 @@ try {
const fixtures = [
await buildRiscvFixture(workspace, 32),
await buildRiscvFixture(workspace, 64),
await buildRiscvFixture(workspace, 32, { pie: true }),
await buildRiscvFixture(workspace, 64, { pie: true }),
typedSoft,
await stripRiscvFixture(workspace, typedSoft),
];
for (const fixture of fixtures) {
const bits = fixture.riscv.elfClass;
const soft = fixture.riscv.flags === 1;
const pie = fixture.riscv.type === 3;
const stripped = fixture.stripped === true;
const expected = expectedLane(fixture);
const resolved = await parseBinaryTarget(fixture.path);
@@ -233,6 +245,17 @@ try {
assert.equal(source.original_sha256, fixture.sha256);
assert.equal(source.modified_sha256, fixture.sha256);
}
const expectedBase = pie ? (bits === 32 ? 0x10000n : 0x100000n) : 0n;
if (pie) {
assert.equal(
image.observations.image_base,
`0x${expectedBase.toString(16)}`,
);
}
const addressFor = (name) => {
const symbolAddress = BigInt(fixture.symbols[name]);
return `0x${(pie ? expectedBase + symbolAddress : symbolAddress).toString(16)}`;
};
const procedures = await call("list_procedures");
const names = soft
? [
@@ -257,21 +280,21 @@ try {
// A stripped image has no names; the typed twin's addresses locate it.
const procedure = procedures.find((value) =>
stripped
? BigInt(value.address) === BigInt(fixture.symbols[name])
? BigInt(value.address) === BigInt(addressFor(name))
: value.value === name,
);
assert.ok(procedure, `${name} missing from Ghidra inventory`);
assert.equal(BigInt(procedure.address), BigInt(fixture.symbols[name]));
assert.equal(BigInt(procedure.address), BigInt(addressFor(name)));
}
const probe = await call("inspect_native_instruction", {
address: fixture.symbols.rea_riscv_probe,
address: addressFor("rea_riscv_probe"),
});
assert.equal(probe.status, "decoded");
assert.equal(probe.architecture, expectedLanguage);
assert.equal(probe.length, 4);
assert.equal(probe.bytes, "13051501"); // addi a0, a0, 17
const mapping = await call("address_to_file_offset", {
address: fixture.symbols.rea_riscv_probe,
address: addressFor("rea_riscv_probe"),
});
const sourceBytes = await readFile(fixture.path);
assert.equal(
@@ -288,7 +311,7 @@ try {
[4n, "f32200b0", /^csrr/iu],
]) {
const instruction = await call("inspect_native_instruction", {
address: `0x${(BigInt(fixture.symbols.rea_riscv_isa_probe) + offset).toString(16)}`,
address: `0x${(BigInt(addressFor("rea_riscv_isa_probe")) + offset).toString(16)}`,
});
assert.equal(instruction.status, "decoded");
assert.equal(instruction.bytes, bytes);
@@ -297,33 +320,30 @@ try {
}
}
const global = await call("read_bytes", {
address: fixture.symbols.rea_riscv_global,
address: addressFor("rea_riscv_global"),
length: 4,
});
assert.equal(global.bytes_hex, "07000000");
const callees = await call("procedure_callees", {
procedure: fixture.symbols.rea_riscv_entry,
procedure: addressFor("rea_riscv_entry"),
});
for (const name of soft
? ["rea_riscv_abi_caller", "rea_riscv_isa_probe", "rea_riscv_probe"]
: ["rea_riscv_leaf", "rea_riscv_probe"])
assert.ok(
callees.some(
(address) => BigInt(address) === BigInt(fixture.symbols[name]),
),
callees.some((address) => BigInt(address) === BigInt(addressFor(name))),
`${name} missing from the entry's callees`,
);
const dossier = functionDossierSchema.parse(
await call("analyze_function", {
procedure: fixture.symbols.rea_riscv_entry,
procedure: addressFor("rea_riscv_entry"),
detail: "complete",
}),
);
assert.ok(dossier.pseudocode.trim().length > 0);
if (!stripped) {
const floatCode = await call("procedure_pseudo_code", {
procedure:
fixture.symbols[soft ? "rea_riscv_abi_wide" : "rea_riscv_float"],
procedure: addressFor(soft ? "rea_riscv_abi_wide" : "rea_riscv_float"),
});
assert.ok(typeof floatCode === "string" && floatCode.trim().length > 0);
assert.match(floatCode, /\bdouble\b/u);
@@ -333,12 +353,12 @@ try {
const boundaries = {};
for (const name of Object.keys(RISCV_ILP32_ABI))
boundaries[name] = (
await call("inspect_native_api", { procedure: fixture.symbols[name] })
await call("inspect_native_api", { procedure: addressFor(name) })
).boundary;
assertRiscvIlp32Storage(boundaries, { stripped });
const caller = functionDossierSchema.parse(
await call("analyze_function", {
procedure: fixture.symbols.rea_riscv_abi_caller,
procedure: addressFor("rea_riscv_abi_caller"),
detail: "complete",
}),
);
@@ -362,7 +382,7 @@ try {
const snapshotPath = join(
workspace,
`${fixture.path.split("/").at(-1)}.snapshot.json`,
`${fixture.path.split(/[\/\\]/).at(-1)}.snapshot.json`,
);
const saved = await call("close_binary", { snapshot_path: snapshotPath });
assert.ok(saved.evidence_records > 0);
@@ -399,7 +419,7 @@ try {
...(stripped ? [] : [["search", ["rea_riscv_", "--kind", "procedures"]]]),
[
"function",
[stripped ? fixture.symbols.rea_riscv_entry : "rea_riscv_entry"],
[stripped ? addressFor("rea_riscv_entry") : "rea_riscv_entry"],
],
]) {
const cli = await execute(
@@ -431,90 +451,127 @@ try {
}
}
const headers = [];
// Header-mutated copies test admission; they are not new compiler claims.
for (const [flags, outcome] of stripped
? []
: soft
? [
[0x3, "single-float ABI (ilp32f)"],
[0x80000001, "(0x80000000)"],
]
: bits === 32
? [
[0x1, "admitted"],
[0x80000005, "(0x80000000)"],
]
: [
[0x1, "soft-float ABI (lp64)"],
[0x80000005, "(0x80000000)"],
]) {
const mutatedPath = join(
workspace,
`rv${bits}${soft ? "-soft" : ""}-flags-${flags.toString(16)}.elf`,
);
const mutatedBytes = Buffer.from(sourceBytes);
mutatedBytes.writeUInt32LE(flags, bits === 32 ? 36 : 48);
await writeFile(mutatedPath, mutatedBytes);
if (pie) {
// Mutate program headers to clear PT_INTERP and verify shared library refusal.
const is64 = bits === 64;
const phoff = is64
? Number(sourceBytes.readBigUInt64LE(32))
: sourceBytes.readUInt32LE(28);
const phentsize = sourceBytes.readUInt16LE(is64 ? 54 : 42);
const phnum = sourceBytes.readUInt16LE(is64 ? 56 : 44);
const sharedBytes = Buffer.from(sourceBytes);
for (let i = 0; i < phnum; i += 1) {
const offset = phoff + i * phentsize;
if (sharedBytes.readUInt32LE(offset) === 3) {
sharedBytes.writeUInt32LE(0, offset); // PT_NULL
}
}
const sharedPath = join(workspace, `rv${bits}-shared.so`);
await writeFile(sharedPath, sharedBytes);
const reply = await client.callTool(
{
name: "open_binary",
arguments: { path: mutatedPath, provider_id: "ghidra" },
arguments: { path: sharedPath, provider_id: "ghidra" },
},
{ timeout: 240000 },
{ timeout: 60000 },
);
const cliArguments = [
entrypoint,
"function",
mutatedPath,
"rea_riscv_entry",
"--provider",
"ghidra",
"--json",
];
const cliOptions = { env, timeout: 240000, maxBuffer: 16 * 1024 * 1024 };
if (outcome === "admitted") {
// The double-float image now declares ilp32: refused before, admitted
// now through the soft-float lane with the image's own ISA string.
assert.equal(
requireMcpLifecycleResult(reply, "open_binary").sha256,
createHash("sha256").update(mutatedBytes).digest("hex"),
);
requireMcpLifecycleResult(
await client.callTool(
{ name: "close_binary", arguments: {} },
{ timeout: 240000 },
),
"close_binary",
);
const want = {
lane: "elf32-exec-le-soft-float-generic-v1",
callingConvention: "ilp32-prototype-v1",
riscvElf: {
...expected.riscvElf,
flags,
float_abi_double: false,
float_abi: "soft",
arch: fixture.riscv.isa.arch,
},
};
const cli = await execute(process.execPath, cliArguments, cliOptions);
assertProfile(
parseEvidence(JSON.parse(cli.stdout)).analysis_profile,
want,
);
headers.push({ flags, outcome, lane: want.lane });
continue;
}
const error = requireMcpToolError(reply);
assert.ok(JSON.stringify(error).includes(outcome), JSON.stringify(error));
await assert.rejects(
execute(process.execPath, cliArguments, cliOptions),
(cause) =>
Number.isInteger(cause.code) &&
cause.code !== 0 &&
`${cause.stdout}\n${cause.stderr}`.includes(outcome),
);
headers.push({ flags, outcome: "refused", reason: outcome });
const reason = "shared libraries without PT_INTERP";
assert.ok(JSON.stringify(error).includes(reason), JSON.stringify(error));
headers.push({ flags: "shared", outcome: "refused", reason });
} else {
// Header-mutated copies test admission; they are not new compiler claims.
for (const [flags, outcome] of stripped
? []
: soft
? [
[0x3, "single-float ABI (ilp32f)"],
[0x80000001, "(0x80000000)"],
]
: bits === 32
? [
[0x1, "admitted"],
[0x80000005, "(0x80000000)"],
]
: [
[0x1, "soft-float ABI (lp64)"],
[0x80000005, "(0x80000000)"],
]) {
const mutatedPath = join(
workspace,
`rv${bits}${soft ? "-soft" : ""}-flags-${flags.toString(16)}.elf`,
);
const mutatedBytes = Buffer.from(sourceBytes);
mutatedBytes.writeUInt32LE(flags, bits === 32 ? 36 : 48);
await writeFile(mutatedPath, mutatedBytes);
const reply = await client.callTool(
{
name: "open_binary",
arguments: { path: mutatedPath, provider_id: "ghidra" },
},
{ timeout: 240000 },
);
const cliArguments = [
entrypoint,
"function",
mutatedPath,
"rea_riscv_entry",
"--provider",
"ghidra",
"--json",
];
const cliOptions = {
env,
timeout: 240000,
maxBuffer: 16 * 1024 * 1024,
};
if (outcome === "admitted") {
// The double-float image now declares ilp32: refused before, admitted
// now through the soft-float lane with the image's own ISA string.
assert.equal(
requireMcpLifecycleResult(reply, "open_binary").sha256,
createHash("sha256").update(mutatedBytes).digest("hex"),
);
requireMcpLifecycleResult(
await client.callTool(
{ name: "close_binary", arguments: {} },
{ timeout: 240000 },
),
"close_binary",
);
const want = {
lane: "elf32-exec-le-soft-float-generic-v1",
callingConvention: "ilp32-prototype-v1",
riscvElf: {
...expected.riscvElf,
flags,
float_abi_double: false,
float_abi: "soft",
arch: fixture.riscv.isa.arch,
},
};
const cli = await execute(process.execPath, cliArguments, cliOptions);
assertProfile(
parseEvidence(JSON.parse(cli.stdout)).analysis_profile,
want,
);
headers.push({ flags, outcome, lane: want.lane });
continue;
}
const error = requireMcpToolError(reply);
assert.ok(
JSON.stringify(error).includes(outcome),
JSON.stringify(error),
);
await assert.rejects(
execute(process.execPath, cliArguments, cliOptions),
(cause) =>
Number.isInteger(cause.code) &&
cause.code !== 0 &&
`${cause.stdout}\n${cause.stderr}`.includes(outcome),
);
headers.push({ flags, outcome: "refused", reason: outcome });
}
}
assert.equal(
createHash("sha256")
+1
View File
@@ -1088,6 +1088,7 @@ const readElfMetadata = async (
...parsed.value,
riscv: {
...parsed.value.riscv,
...(ranges.value.hasInterpreter ? { hasInterpreter: true } : {}),
isa: await readElfDeclarations(
handle,
bytes,
+1
View File
@@ -41,6 +41,7 @@ export interface ElfFamilyMetadata {
readonly byteOrder: "little" | "big";
readonly type: number;
readonly flags: number;
readonly hasInterpreter?: boolean;
}
/** Referenced CodeView debug symbols from a PE debug directory. */
+25 -5
View File
@@ -1,9 +1,13 @@
import { readElfSectionHeader } from "./elfSectionHeader.js";
import { readElfProgramHeader } from "./elfProgramHeader.js";
import { readElfProgramHeader, PT_INTERP } from "./elfProgramHeader.js";
import { readElfRawTableDeclarations } from "./elfRawTableDeclarations.js";
import { readElfFixedHeader, type ElfFixedHeader } from "./elfFixedHeader.js";
import { err, ok, type Result } from "./result.js";
export interface ElfDeclaredRangeFacts {
readonly hasInterpreter: boolean;
}
/** Positional source requests; the caller owns the descriptor and cancellation. */
export interface ElfDeclarationRead {
readonly offset: number;
@@ -203,7 +207,12 @@ function* inspectTableRanges(
table: ElfDeclaredTable,
header: ElfFixedHeader,
fileSize: number,
): Generator<ElfDeclarationRead, Result<null, string>, Buffer> {
): Generator<
ElfDeclarationRead,
Result<{ readonly hasInterpreter: boolean }, string>,
Buffer
> {
let hasInterpreter = false;
const perRead = Math.max(1, Math.floor((64 * 1024) / table.width));
for (let index = 0; index < table.count; index += perRead) {
const count = Math.min(perRead, table.count - index);
@@ -229,6 +238,9 @@ function* inspectTableRanges(
);
if (!parsed.ok) return err(parsed.error.message);
const facts = parsed.value;
if (kind === "program" && facts.type === PT_INTERP) {
hasInterpreter = true;
}
if (facts.type === 0 || (kind === "section" && facts.type === 8))
continue;
const offset = facts.offset;
@@ -242,17 +254,22 @@ function* inspectTableRanges(
if (failure !== undefined) return err(failure);
}
}
return ok(null);
return ok({ hasInterpreter });
}
/** Physical extent admission, independent of section names, CPU lanes and decoder budgets. */
export function* validateElfDeclaredRanges(
prefix: Buffer,
fileSize: number,
): Generator<ElfDeclarationRead, Result<null, string>, Buffer> {
): Generator<
ElfDeclarationRead,
Result<ElfDeclaredRangeFacts, string>,
Buffer
> {
const declarations = yield* readElfTableDeclarations(prefix, fileSize);
if (!declarations.ok) return declarations;
const { header, programs, sections } = declarations.value;
let hasInterpreter = false;
for (const [kind, table] of [
["program", programs],
["section", sections],
@@ -260,6 +277,9 @@ export function* validateElfDeclaredRanges(
if (table.count === 0) continue;
const inspected = yield* inspectTableRanges(kind, table, header, fileSize);
if (!inspected.ok) return inspected;
if (kind === "program") {
hasInterpreter = inspected.value.hasInterpreter;
}
}
return ok(null);
return ok({ hasInterpreter });
}
+2
View File
@@ -1,5 +1,7 @@
import { err, ok } from "./result.js";
export const PT_INTERP = 3;
export type ElfProgramHeader = Extract<
ReturnType<typeof readElfProgramHeader>,
{ readonly ok: true }
+144 -4
View File
@@ -12,6 +12,7 @@ import { silentLogger } from "../logger.js";
import { HopperProvider } from "../hopper/HopperProvider.js";
import { hopperLoaderArgsForTarget } from "../hopper/HopperAnalysisProfile.js";
import { IdaProvider } from "../ida/IdaProvider.js";
import { parseBinaryTarget } from "../application/BinaryTargetResolver.js";
import { GhidraProvider } from "./GhidraProvider.js";
import {
GHIDRA_RISCV32_DECODED_EXTENSIONS,
@@ -226,12 +227,124 @@ const registerRiscvLanguageCommitmentCases = () => {
});
};
const registerRiscvPieAdmissionCases = () => {
it("admits little-endian position-independent executables (ET_DYN with PT_INTERP)", async () => {
const provider = ghidra();
const pie64 = target({ type: 3, hasInterpreter: true });
const pie32 = target({
elfClass: 32,
flags: 0x4,
type: 3,
hasInterpreter: true,
});
expect(provider.inspectTargetSupport(pie64).status).toBe("supported");
expect(provider.inspectTargetSupport(pie32).status).toBe("supported");
expect(ghidraPinnedLanguage(pie64)).toEqual({
languageId: "RISCV:LE:64:default",
compilerSpecId: "gcc",
});
expect(ghidraPinnedLanguage(pie32)).toEqual({
languageId: "RISCV:LE:32:default",
compilerSpecId: "gcc",
});
const wide = await provider.resolveAnalysisProfile(pie64);
const narrow = await provider.resolveAnalysisProfile(pie32);
if (!wide.ok) throw wide.error;
if (!narrow.ok) throw narrow.error;
expect(wide.value.profile?.parameters.riscv_elf).toEqual({
elf_class: 64,
byte_order: "little",
type: 3,
flags: 5,
float_abi_double: true,
rvc_declared: true,
rve_declared: false,
tso_declared: false,
has_interpreter: true,
load_model: "pie",
expected_default_image_base: "0x100000",
});
expect(wide.value.profile?.parameters.riscv_support_lane).toBe(
"elf64-pie-le-double-float-generic-v1",
);
expect(narrow.value.profile?.parameters.riscv_support_lane).toBe(
"elf32-pie-le-double-float-generic-v1",
);
expect(narrow.value.profile?.parameters.riscv_elf).toEqual({
elf_class: 32,
byte_order: "little",
type: 3,
flags: 4,
float_abi_double: true,
rvc_declared: false,
rve_declared: false,
tso_declared: false,
has_interpreter: true,
load_model: "pie",
expected_default_image_base: "0x10000",
});
});
it("refuses soft-float position-independent executables (PIE)", () => {
const pieSoft = ilp32(C3_ARCH, { type: 3, hasInterpreter: true });
const support = ghidra().inspectTargetSupport(pieSoft);
expect(support).toMatchObject({
status: "unsupported",
code: "architecture_unsupported",
reason:
"This Ghidra RISC-V soft-float lane supports fixed-address ET_EXEC only; position-independent executables (PIE) need separate load verification.",
});
expect(ghidraRiscvCallingConvention(pieSoft)).toBeUndefined();
expect(ghidraPinnedLanguage(pieSoft)).toBeUndefined();
});
it("admits a valid RISC-V PIE whose program headers start beyond 4096 and end past 65536", async () => {
const testFile = join(installDir, "pie-high-phoff.elf");
const fileSize = 75000;
const phoff = 70000;
const phentsize = 56;
const phnum = 2;
const buffer = Buffer.alloc(fileSize);
buffer.set([0x7f, 0x45, 0x4c, 0x46, 2, 1, 1]); // ELF64, little-endian, version 1
buffer.writeUInt16LE(3, 16); // ET_DYN
buffer.writeUInt16LE(243, 18); // EM_RISCV
buffer.writeUInt32LE(1, 20); // EV_CURRENT
buffer.writeBigUInt64LE(BigInt(phoff), 32); // e_phoff
buffer.writeUInt32LE(0x5, 48); // e_flags: double-float ABI | RVC
buffer.writeUInt16LE(64, 52); // e_ehsize
buffer.writeUInt16LE(phentsize, 54); // e_phentsize
buffer.writeUInt16LE(phnum, 56); // e_phnum
// Program headers at phoff:
// Entry 0: PT_LOAD (1)
buffer.writeUInt32LE(1, phoff);
// Entry 1: PT_INTERP (3)
buffer.writeUInt32LE(3, phoff + phentsize);
await writeFile(testFile, buffer);
const resolved = await parseBinaryTarget(testFile);
if (!resolved.ok) throw resolved.error;
expect(resolved.value.kind).toBe("executable");
expect(resolved.value.architecture).toBe("riscv");
if (resolved.value.architecture === "riscv") {
expect(resolved.value.riscv.hasInterpreter).toBe(true);
}
const support = ghidra().inspectTargetSupport(resolved.value);
expect(support.status).toBe("supported");
});
};
const registerRiscvAdmissionAndCapabilityCases = () => {
it.each([
[{ byteOrder: "big" }, "little-endian"],
[{ type: 3 }, "fixed-address ET_EXEC only"],
[{ type: 1 }, "fixed-address ET_EXEC only"],
[{ type: 3 }, "shared libraries without PT_INTERP"],
[{ type: 3, hasInterpreter: false }, "shared libraries without PT_INTERP"],
[{ type: 1 }, "relocatable"],
[{ type: 0xffa0 }, "e_type 65440"],
[
{ elfClass: 32, flags: 0x1, type: 3, hasInterpreter: true },
"soft-float lane supports fixed-address ET_EXEC only",
],
[{ flags: 0xc }, "EF_RISCV_RVE"],
[{ flags: 0x14 }, "EF_RISCV_TSO"],
[{ flags: 0x1 }, "soft-float ABI (lp64) declared by e_flags"],
@@ -323,6 +436,14 @@ const registerRiscvAdmissionAndCapabilityCases = () => {
expect(notes).toContainEqual(
expect.stringContaining("does not establish static linkage"),
);
const pieNotes = ghidraRiscvLimitations(
target({ type: 3, hasInterpreter: true }),
);
expect(pieNotes).toContainEqual(expect.stringContaining("0x100000"));
expect(pieNotes).toContainEqual(
expect.stringContaining("Position-independent executable"),
);
expect(
ghidraRiscvLimitations({
path: "/other.elf",
@@ -364,6 +485,19 @@ describe("RISC-V lane and calling-convention tables", () => {
"elf32-exec-le-double-float-generic-v1",
"elf32-exec-le-soft-float-generic-v1",
]);
expect(
GHIDRA_RISCV_LANES.flatMap((lane) =>
lane.loadModels.map((loadModel) =>
ghidraRiscvSupportLane({ ...lane, loadModel }),
),
),
).toEqual([
"elf64-exec-le-double-float-generic-v1",
"elf64-pie-le-double-float-generic-v1",
"elf32-exec-le-double-float-generic-v1",
"elf32-pie-le-double-float-generic-v1",
"elf32-exec-le-soft-float-generic-v1",
]);
});
it("gives every non-double lane one policy and every policy one lane", () => {
@@ -388,8 +522,13 @@ describe("RISC-V lane and calling-convention tables", () => {
new URL("../../docs/providers/ghidra-riscv.md", import.meta.url),
"utf8",
);
for (const lane of GHIDRA_RISCV_LANES)
expect(docs).toContain(`\`${ghidraRiscvSupportLane(lane)}\``);
for (const lane of GHIDRA_RISCV_LANES) {
for (const loadModel of lane.loadModels) {
expect(docs).toContain(
`\`${ghidraRiscvSupportLane({ ...lane, loadModel })}\``,
);
}
}
for (const id of GHIDRA_RISCV_CALLING_CONVENTION_IDS) {
const { abi, prototype } = GHIDRA_RISCV_CALLING_CONVENTIONS[id];
expect(docs).toContain(`Policy \`${id}\`, prototype \`${prototype}\``);
@@ -409,6 +548,7 @@ describe("RISC-V lane and calling-convention tables", () => {
describe("bounded Ghidra RISC-V admission", () => {
registerRiscvLanguageCommitmentCases();
registerRiscvPieAdmissionCases();
registerRiscvAdmissionAndCapabilityCases();
});
+41 -14
View File
@@ -33,24 +33,37 @@ export const GHIDRA_RISCV_CALLING_CONVENTIONS: Readonly<
"ilp32-prototype-v1": { abi: "ilp32", prototype: "__rea_ilp32" },
};
/** Admitted load models for each lane. */
export type GhidraRiscvLoadModel = "exec" | "pie";
/** One admitted ELF class and declared float ABI. */
export interface GhidraRiscvLane {
readonly elfClass: 32 | 64;
readonly floatAbi: RiscvFloatAbi;
readonly loadModels: readonly GhidraRiscvLoadModel[];
/** Absent: Ghidra's stock double-float model applies unchanged. */
readonly callingConvention?: GhidraRiscvCallingConvention;
}
/** Every class and float ABI pair the Ghidra lane admits; others are refused. */
export const GHIDRA_RISCV_LANES: readonly GhidraRiscvLane[] = [
{ elfClass: 64, floatAbi: "double" },
{ elfClass: 32, floatAbi: "double" },
{ elfClass: 32, floatAbi: "soft", callingConvention: "ilp32-prototype-v1" },
{ elfClass: 64, floatAbi: "double", loadModels: ["exec", "pie"] },
{ elfClass: 32, floatAbi: "double", loadModels: ["exec", "pie"] },
{
elfClass: 32,
floatAbi: "soft",
loadModels: ["exec"],
callingConvention: "ilp32-prototype-v1",
},
];
/** Lane ids are built from their fields, so a new ABI or load model adds a value. */
export const ghidraRiscvSupportLane = (lane: GhidraRiscvLane): string =>
`elf${lane.elfClass}-exec-le-${lane.floatAbi}-float-generic-v1`;
export const ghidraRiscvSupportLane = (lane: {
readonly elfClass: 32 | 64;
readonly floatAbi: RiscvFloatAbi;
readonly loadModel?: GhidraRiscvLoadModel;
}): string =>
`elf${lane.elfClass}-${lane.loadModel ?? "exec"}-le-${lane.floatAbi}-float-generic-v1`;
/**
* Extensions Ghidra 12.1.4's generic RV32 language implements with p-code, by
@@ -168,16 +181,18 @@ export const ghidraRiscvUnsupportedReason = (
return "EF_RISCV_RVE declares the embedded 16-register ABI (x0-x15), which needs its own processor specification and compiler verification.";
if ((metadata.flags & EF_RISCV_TSO) !== 0)
return "EF_RISCV_TSO declares the total-store-order memory model; this lane verifies the baseline model only.";
if (metadata.type !== ET_EXEC)
return `This Ghidra RISC-V lane supports fixed-address ET_EXEC only; ${
metadata.type === ET_REL
? "relocatable"
: metadata.type === ET_DYN
? "shared and position-independent"
: `e_type ${metadata.type}`
} images need separate load verification. ET_EXEC alone does not establish static linkage, which this lane does not inspect.`;
if (metadata.type === ET_DYN && !metadata.hasInterpreter)
return "This Ghidra RISC-V lane supports fixed-address ET_EXEC and position-independent executables (PIE) only; shared libraries without PT_INTERP need separate load verification.";
if (metadata.type !== ET_EXEC && metadata.type !== ET_DYN)
return `This Ghidra RISC-V lane supports fixed-address ET_EXEC and position-independent executables (PIE) only; ${
metadata.type === ET_REL ? "relocatable" : `e_type ${metadata.type}`
} images need separate load verification.`;
const lane = ghidraRiscvLane(metadata);
if (lane === undefined) return ghidraRiscvFloatAbiRefusal(metadata);
const loadModel: GhidraRiscvLoadModel =
metadata.type === ET_DYN ? "pie" : "exec";
if (!lane.loadModels.includes(loadModel))
return `This Ghidra RISC-V ${lane.floatAbi}-float lane supports fixed-address ET_EXEC only; position-independent executables (PIE) need separate load verification.`;
const unsupportedFlags = (metadata.flags & ~EF_RISCV_DECLARED_BITS) >>> 0;
if (unsupportedFlags !== 0)
return `RISC-V e_flags declares bits outside the verified lane (0x${unsupportedFlags.toString(16)}); reserved or newer declarations are not treated as compatible with this profile.`;
@@ -223,6 +238,7 @@ export const ghidraRiscvProfileParameters = (
target.riscv === undefined
)
return {};
const isPie = target.riscv.type === ET_DYN;
const floatAbi = riscvFloatAbi(target.riscv.flags);
const convention = ghidraRiscvCallingConvention(target);
const isa = target.riscv.isa;
@@ -239,6 +255,14 @@ export const ghidraRiscvProfileParameters = (
...(convention === undefined || isa?.status !== "declared"
? {}
: { float_abi: floatAbi, arch: isa.arch }),
...(isPie
? {
has_interpreter: true,
load_model: "pie",
expected_default_image_base:
target.riscv.elfClass === 32 ? "0x10000" : "0x100000",
}
: {}),
},
...(convention === undefined
? {}
@@ -246,6 +270,7 @@ export const ghidraRiscvProfileParameters = (
riscv_support_lane: ghidraRiscvSupportLane({
elfClass: target.riscv.elfClass,
floatAbi,
...(isPie ? { loadModel: "pie" } : {}),
}),
};
};
@@ -273,7 +298,9 @@ export const ghidraRiscvLimitations = (
`\`.riscv.attributes\` declares \`${isa.arch}\`. Every declared extension is in the set this lane decodes; the declaration does not prove that every instruction belongs to it.`,
]
: []),
"Admission reads `e_type` only: a fixed-address ET_EXEC declaration does not establish static linkage, and program headers (`PT_INTERP`, dynamic dependencies) are not inspected by this lane.",
target.riscv.type === ET_DYN
? `Position-independent executable (PIE): Ghidra assigns analysis coordinates from its default image base (${target.riscv.elfClass === 32 ? "0x10000" : "0x100000"}). Runtime load addresses and dynamic linker relocation are not executed or verified by this lane.`
: "Admission reads `e_type` only: a fixed-address ET_EXEC declaration does not establish static linkage, and program headers (`PT_INTERP`, dynamic dependencies) are not inspected by this lane.",
];
};
@@ -197,3 +197,25 @@ it.each(["sections", "programs", "section names"] as const)(
);
},
);
it("detects PT_INTERP in program headers and records hasInterpreter for RISC-V", async () => {
const fixture = image(64, true);
fixture.u16(18, 243); // EM_RISCV
fixture.u16(16, 3); // ET_DYN
const withoutInterp = await admit(fixture.bytes);
expect(withoutInterp.ok).toBe(true);
if (!withoutInterp.ok) throw withoutInterp.error;
expect(withoutInterp.value.architecture).toBe("riscv");
if (withoutInterp.value.architecture === "riscv") {
expect(withoutInterp.value.riscv.hasInterpreter).toBeUndefined();
}
fixture.u32(fixture.program, 3); // PT_INTERP
const withInterp = await admit(fixture.bytes);
expect(withInterp.ok).toBe(true);
if (!withInterp.ok) throw withInterp.error;
expect(withInterp.value.architecture).toBe("riscv");
if (withInterp.value.architecture === "riscv") {
expect(withInterp.value.riscv.hasInterpreter).toBe(true);
}
});
+31 -8
View File
@@ -36,11 +36,11 @@ const readerOptions = {
maxBuffer: 1024 * 1024,
};
/** Independent reading of LLVM readelf's `-h -S -s -A` report. */
/** Independent reading of LLVM readelf's `-h -l -S -s -A` report. */
const readRiscvElf = async (reader, path, floatAbi) => {
const { stdout, stderr } = await execute(
reader,
["-h", "-S", "-s", "-A", "-W", path],
["-h", "-l", "-S", "-s", "-A", "-W", path],
readerOptions,
);
assert.equal(stderr.trim(), "", "Unexpected ELF reader diagnostics");
@@ -92,7 +92,11 @@ const sha256 = async (path) =>
.digest("hex");
/** Build and independently inspect a source-owned ELF; never execute it. */
export async function buildRiscvFixture(directory, bits, floatAbi = "double") {
export async function buildRiscvFixture(directory, bits, buildOptions = {}) {
const { floatAbi = "double", pie = false } =
typeof buildOptions === "string"
? { floatAbi: buildOptions }
: buildOptions;
assert.ok(bits === 32 || bits === 64);
const build = BUILDS[floatAbi];
assert.ok(build !== undefined, `No ${floatAbi}-float RISC-V fixture`);
@@ -105,16 +109,21 @@ export async function buildRiscvFixture(directory, bits, floatAbi = "double") {
);
const path = join(
directory,
`rv${bits}${floatAbi === "double" ? "" : `-${floatAbi}`}.elf`,
`rv${bits}${floatAbi === "double" ? "" : `-${floatAbi}`}${pie ? "-pie" : ""}.elf`,
);
const interpreter =
bits === 32
? "/lib/ld-linux-riscv32-ilp32d.so.1"
: "/lib/ld-linux-riscv64-lp64d.so.1";
const args = [
`--target=riscv${bits}-unknown-elf`,
`-march=${build.march(bits)}`,
`-mabi=${build.mabi(bits)}`,
`--ld-path=${linker}`,
"-nostdlib",
"-static",
"-fno-pic",
...(pie
? ["-fPIE", "-Wl,-pie", `-Wl,--dynamic-linker=${interpreter}`]
: ["-static", "-fno-pic"]),
"-O0",
"-g",
"-Wl,-e,rea_riscv_entry",
@@ -131,7 +140,20 @@ export async function buildRiscvFixture(directory, bits, floatAbi = "double") {
const elf = await readRiscvElf(reader, path, floatAbi);
assert.equal(elf.class, `ELF${bits}`);
assert.match(elf.data, /little endian/u);
assert.match(elf.type, /^EXEC\b/u);
assert.match(elf.type, pie ? /^DYN\b/u : /^EXEC\b/u);
if (pie) {
assert.match(
elf.stdout,
/^\s*INTERP\b/mu,
"Expected PT_INTERP program header in PIE fixture",
);
} else {
assert.doesNotMatch(
elf.stdout,
/^\s*INTERP\b/mu,
"Unexpected PT_INTERP program header in static ET_EXEC fixture",
);
}
assert.match(elf.machine, /^RISC-V\b/u);
assert.equal(
elf.flags,
@@ -153,9 +175,10 @@ export async function buildRiscvFixture(directory, bits, floatAbi = "double") {
riscv: {
elfClass: bits,
byteOrder: "little",
type: 2,
type: pie ? 3 : 2,
flags: elf.flags,
isa: elf.isa,
...(pie ? { hasInterpreter: true } : {}),
},
symbols,
sections: elf.sections,