mirror of
https://github.com/morluto/rea.git
synced 2026-11-19 21:23:45 +00:00
* feat(domain): classify EM_PPC64 ELF in the powerpc family
An ELF with e_machine 21 was refused as "unsupported ELF architecture" before any provider was consulted, so every entry point answered a 64-bit PowerPC file with the generic file-format refusal.
The header reader now places EM_PPC64 in the powerpc family and keeps its class, byte order, e_type and e_flags as it does for EM_PPC. No provider lane admits these files: the Ghidra PowerPC gate already refuses ELF64 by naming PPC64, and Hopper, IDA and Kuna decline the family. Static ELF readers keep the name ppc64 for the machine.
Refs morluto/rea#12085.
* feat(domain): map the ppc64 reader name to the powerpc Evidence subject
Static readers report a 64-bit PowerPC ELF as ppc64, and the Evidence subject of those reports had no architecture because the name mapped to no family. With EM_PPC64 classified as powerpc, the subject now carries that family, as it does for ppc.
The Go verifier and the Go and Rust guides state the new mapping, and the boundary test keeps an unknown machine (s390) to show that unrecognized names still map to no family.
Refs morluto/rea#12085.
* revert(domain): keep ppc64 reader reports without a subject family
This reverts commit bbd7f7f661.
Evidence saved before the mapping carries no subject architecture for a ppc64 report. Views over those records compare the saved subject with the family the reader name maps to, so the mapping made them refuse records they accepted before. Nothing in this branch produces Evidence that needs the family, so the mapping can wait for a provider lane.
Refs morluto/rea#12085.
* fix(domain): classify EM_PPC64 only for an ELF64 header
Classifying machine 21 by number alone had two effects beyond target resolution. An ELF32 big-endian ET_EXEC header that claims EM_PPC64 became a powerpc target with class 32, which the 32-bit Ghidra lane admits because it sees only class, byte order, type and flags. And every static reader that maps a machine to a family started to put powerpc in its Evidence subject, which changes what views expect from records saved earlier.
elfArchitecture now takes the ELF class and names the family for machine 21 only when the caller commits ELF64. Header admission passes the class. Readers that pass only a machine keep the machine unclassified, so their reports, reader names and Evidence subjects are as they were.
Refs morluto/rea#12085.
180 lines
4.9 KiB
TypeScript
180 lines
4.9 KiB
TypeScript
import { expect, it } from "vitest";
|
|
import {
|
|
elfArchitecture,
|
|
readElfFixedHeader,
|
|
readElfHeaderFacts,
|
|
} from "./elfFixedHeader.js";
|
|
import { elf } from "./binaryTarget.fixture.js";
|
|
|
|
it.each([
|
|
["ELF32 little endian", 1, 1],
|
|
["ELF32 big endian", 1, 2],
|
|
["ELF64 little endian", 2, 1],
|
|
["ELF64 big endian", 2, 2],
|
|
] as const)(
|
|
"reads common fixed header commitments for %s",
|
|
(_name, cls, data) => {
|
|
const bytes = elf(cls, data, cls === 1 ? 3 : 62);
|
|
const flagsOffset = cls === 1 ? 36 : 48;
|
|
if (data === 1) {
|
|
bytes.writeUInt16LE(0xfe01, 16);
|
|
bytes.writeUInt32LE(0x89abcdef, flagsOffset);
|
|
} else {
|
|
bytes.writeUInt16BE(0xfe01, 16);
|
|
bytes.writeUInt32BE(0x89abcdef, flagsOffset);
|
|
}
|
|
expect(readElfFixedHeader(bytes)).toEqual({
|
|
ok: true,
|
|
value: {
|
|
elfClass: cls === 1 ? 32 : 64,
|
|
byteOrder: data === 1 ? "little" : "big",
|
|
headerSize: cls === 1 ? 52 : 64,
|
|
machine: cls === 1 ? 3 : 62,
|
|
type: 0xfe01,
|
|
flags: 0x89abcdef,
|
|
osAbi: 0,
|
|
abiVersion: 0,
|
|
architecture: cls === 1 ? "x86" : "x86_64",
|
|
},
|
|
});
|
|
},
|
|
);
|
|
|
|
it("retains the declared ABI version without admitting a consumer-specific ABI", () => {
|
|
const bytes = elf(2, 1, 62);
|
|
bytes[7] = 6;
|
|
bytes[8] = 7;
|
|
expect(readElfFixedHeader(bytes)).toMatchObject({
|
|
ok: true,
|
|
value: { osAbi: 6, abiVersion: 7 },
|
|
});
|
|
});
|
|
|
|
it.each([
|
|
[
|
|
"identification version",
|
|
(bytes: Buffer) => {
|
|
bytes[6] = 0;
|
|
},
|
|
],
|
|
[
|
|
"header version",
|
|
(bytes: Buffer) => {
|
|
bytes.writeUInt32LE(0, 20);
|
|
},
|
|
],
|
|
[
|
|
"undersized declared header",
|
|
(bytes: Buffer) => {
|
|
bytes.writeUInt16LE(51, 40);
|
|
},
|
|
],
|
|
[
|
|
"declared header beyond file",
|
|
(bytes: Buffer) => {
|
|
bytes.writeUInt16LE(53, 40);
|
|
},
|
|
],
|
|
])("rejects an invalid %s", (_name, corrupt) => {
|
|
const bytes = elf(1, 1, 3);
|
|
corrupt(bytes);
|
|
expect(readElfFixedHeader(bytes)).toMatchObject({ ok: false });
|
|
});
|
|
|
|
it("checks declared header size against the whole file, not the prefix", () => {
|
|
const bytes = elf(2, 1, 62);
|
|
bytes.writeUInt16LE(128, 52);
|
|
expect(readElfFixedHeader(bytes, 128)).toMatchObject({ ok: true });
|
|
expect(readElfFixedHeader(bytes, 127)).toMatchObject({ ok: false });
|
|
});
|
|
|
|
it("classifies EM_PPC64 only for a caller that commits ELF64", () => {
|
|
expect(elfArchitecture(21, 64)).toBe("powerpc");
|
|
expect(elfArchitecture(21, 32)).toBeUndefined();
|
|
// Readers that pass only the machine keep their reports as they were.
|
|
expect(elfArchitecture(21)).toBeUndefined();
|
|
expect(elfArchitecture(20)).toBe("powerpc");
|
|
});
|
|
|
|
it("preserves an unknown ELF machine as a fact without rejecting the header", () => {
|
|
expect(readElfFixedHeader(elf(2, 2, 0xfffe))).toMatchObject({
|
|
ok: true,
|
|
value: { machine: 0xfffe, architecture: null },
|
|
});
|
|
});
|
|
|
|
it("accepts NVIDIA's vendor ELF version only for the CUDA machine and OS ABI", () => {
|
|
const bytes = elf(2, 1, 190);
|
|
bytes.writeUInt32LE(0x81, 20);
|
|
expect(readElfFixedHeader(bytes).ok).toBe(false);
|
|
bytes[7] = 0x33;
|
|
expect(readElfFixedHeader(bytes)).toMatchObject({
|
|
ok: true,
|
|
value: { machine: 190 },
|
|
});
|
|
bytes.writeUInt16LE(62, 18);
|
|
expect(readElfFixedHeader(bytes).ok).toBe(false);
|
|
});
|
|
|
|
it.each([
|
|
{ cls: 1, data: 1 },
|
|
{ cls: 1, data: 2 },
|
|
{ cls: 2, data: 1 },
|
|
{ cls: 2, data: 2 },
|
|
] as const)(
|
|
"preserves raw version, ABI and declared size facts ($cls/$data)",
|
|
({ cls, data }) => {
|
|
const original = elf(cls, data, 0xfffe);
|
|
const bytes = Buffer.concat([Buffer.alloc(7), original]).subarray(7);
|
|
bytes[8] = 0xff;
|
|
if (data === 1) {
|
|
bytes.writeUInt32LE(0x89abcdef, 20);
|
|
bytes.writeUInt16LE(0, cls === 1 ? 40 : 52);
|
|
} else {
|
|
bytes.writeUInt32BE(0x89abcdef, 20);
|
|
bytes.writeUInt16BE(0, cls === 1 ? 40 : 52);
|
|
}
|
|
expect(readElfHeaderFacts(bytes)).toMatchObject({
|
|
ok: true,
|
|
value: {
|
|
elfClass: cls === 1 ? 32 : 64,
|
|
byteOrder: data === 1 ? "little" : "big",
|
|
machine: 0xfffe,
|
|
version: 0x89abcdef,
|
|
abiVersion: 0xff,
|
|
headerSize: 0,
|
|
},
|
|
});
|
|
expect(readElfFixedHeader(bytes)).toEqual({
|
|
ok: false,
|
|
error: "unsupported ELF header version",
|
|
});
|
|
},
|
|
);
|
|
|
|
it("keeps framing failures ahead of version and declared-size admission", () => {
|
|
const bytes = elf(2, 1, 62);
|
|
bytes[6] = 0;
|
|
bytes.writeUInt32LE(0, 20);
|
|
bytes.writeUInt16LE(0, 52);
|
|
expect(readElfFixedHeader(bytes.subarray(0, 16), 16)).toEqual({
|
|
ok: false,
|
|
error: "unsupported ELF identification version",
|
|
});
|
|
bytes[6] = 1;
|
|
const truncated = readElfFixedHeader(bytes, 63);
|
|
expect(truncated).toMatchObject({
|
|
ok: false,
|
|
error: expect.stringContaining("truncated ELF header"),
|
|
});
|
|
expect(readElfFixedHeader(bytes)).toEqual({
|
|
ok: false,
|
|
error: "unsupported ELF header version",
|
|
});
|
|
bytes.writeUInt32LE(1, 20);
|
|
expect(readElfFixedHeader(bytes)).toEqual({
|
|
ok: false,
|
|
error: "invalid ELF header size",
|
|
});
|
|
});
|