Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(electron): share executable architecture facts for fuse inspection

morlutobf54c4626490
diff --git a/src/application/MipsElfMetadata.ts b/src/application/MipsElfMetadata.ts
index ae3a6ae91..0cf8efdfe 100644
--- a/src/application/MipsElfMetadata.ts
+++ b/src/application/MipsElfMetadata.ts
@@ -83,7 +83,7 @@ export const readMipsElfMetadata = async (
if (
fixedHeader.value.elfClass !== 32 ||
fixedHeader.value.byteOrder !== metadata.byteOrder ||
- u16(bytes, 18) !== 8 ||
+ fixedHeader.value.machine !== 8 ||
u16(bytes, 16) !== metadata.type ||
u32(bytes, 36) !== metadata.flags
)
diff --git a/src/domain/binaryTarget.ts b/src/domain/binaryTarget.ts
index 77787b3c5..e8d778081 100644
--- a/src/domain/binaryTarget.ts
+++ b/src/domain/binaryTarget.ts
@@ -3,7 +3,6 @@ import type {
ElfFamilyMetadata,
HeaderArchitecture,
MipsElfMetadata,
- RawOnlyArchitecture,
PeCodeViewRecord,
RiscvElfMetadata,
PowerPcElfMetadata,
@@ -206,11 +205,9 @@ const parseElf = (
): Result<ExecutableMetadata, string> => {
const fixedHeader = readElfFixedHeader(bytes, fileSize);
if (!fixedHeader.ok) return fixedHeader;
- const { elfClass, byteOrder } = fixedHeader.value;
+ const { elfClass, byteOrder, architecture } = fixedHeader.value;
const little = byteOrder === "little";
- const machine = little ? bytes.readUInt16LE(18) : bytes.readUInt16BE(18);
- const architecture = elfArchitecture(machine);
- if (architecture === undefined) return err("unsupported ELF architecture");
+ if (architecture === null) return err("unsupported ELF architecture");
if (
architecture === "mips" ||
architecture === "riscv" ||
@@ -543,25 +540,3 @@ const machArchitecture = (cpu: number): HeaderArchitecture | undefined => {
}
return undefined;
};
-
-const elfArchitecture = (
- machine: number,
-): Exclude<BinaryArchitecture, RawOnlyArchitecture> | undefined => {
- switch (machine) {
- case 3:
- return "x86";
- case 62:
- return "x86_64";
- case 8: // EM_MIPS: ISA, ABI and byte order remain separate ELF facts.
- return "mips";
- case 20: // EM_PPC; byte order and processor variant remain ELF declarations.
- return "powerpc";
- case 40:
- return "arm";
- case 183:
- return "arm64";
- case 243: // EM_RISCV: XLEN, byte order and e_flags remain separate ELF facts.
- return "riscv";
- }
- return undefined;
-};
diff --git a/src/domain/elfFixedHeader.test.ts b/src/domain/elfFixedHeader.test.ts
index b902635ac..dedc77ce2 100644
--- a/src/domain/elfFixedHeader.test.ts
+++ b/src/domain/elfFixedHeader.test.ts
@@ -17,6 +17,8 @@ it.each([
elfClass: cls === 1 ? 32 : 64,
byteOrder: data === 1 ? "little" : "big",
headerSize: cls === 1 ? 52 : 64,
+ machine: cls === 1 ? 3 : 62,
+ architecture: cls === 1 ? "x86" : "x86_64",
},
});
},
@@ -59,3 +61,10 @@ it("checks declared header size against the whole file, not the prefix", () => {
expect(readElfFixedHeader(bytes, 128)).toMatchObject({ ok: true });
expect(readElfFixedHeader(bytes, 127)).toMatchObject({ ok: false });
});
+
+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 },
+ });
+});
diff --git a/src/domain/elfFixedHeader.ts b/src/domain/elfFixedHeader.ts
index 3b98976c8..ea5943d69 100644
--- a/src/domain/elfFixedHeader.ts
+++ b/src/domain/elfFixedHeader.ts
@@ -1,9 +1,18 @@
+import type {
+ BinaryArchitecture,
+ RawOnlyArchitecture,
+} from "./binaryTargetTypes.js";
import { err, ok, type Result } from "./result.js";
export interface ElfFixedHeader {
readonly elfClass: 32 | 64;
readonly byteOrder: "little" | "big";
readonly headerSize: number;
+ readonly machine: number;
+ readonly architecture: Exclude<
+ BinaryArchitecture,
+ RawOnlyArchitecture
+ > | null;
}
/**
@@ -47,9 +56,34 @@ export const readElfFixedHeader = (
const headerSize = readU16(elfClass === 32 ? 40 : 52);
if (headerSize < minimumSize || headerSize > totalBytes)
return err("invalid ELF header size");
+ const machine = readU16(18);
return ok({
elfClass,
byteOrder: littleEndian ? "little" : "big",
headerSize,
+ machine,
+ architecture: elfArchitecture(machine) ?? null,
});
};
+
+const elfArchitecture = (
+ machine: number,
+): Exclude<BinaryArchitecture, RawOnlyArchitecture> | undefined => {
+ switch (machine) {
+ case 3:
+ return "x86";
+ case 62:
+ return "x86_64";
+ case 8: // EM_MIPS: ISA, ABI and byte order remain separate ELF facts.
+ return "mips";
+ case 20: // EM_PPC; byte order and processor variant remain ELF declarations.
+ return "powerpc";
+ case 40:
+ return "arm";
+ case 183:
+ return "arm64";
+ case 243: // EM_RISCV: XLEN, byte order and e_flags remain separate ELF facts.
+ return "riscv";
+ }
+ return undefined;
+};
diff --git a/src/electron/ElectronFuseArchitecture.test.ts b/src/electron/ElectronFuseArchitecture.test.ts
new file mode 100644
index 000000000..aeafce7c3
--- /dev/null
+++ b/src/electron/ElectronFuseArchitecture.test.ts
@@ -0,0 +1,118 @@
+import { createHash } from "node:crypto";
+import { readFile, writeFile } from "node:fs/promises";
+import { join } from "node:path";
+import { expect, it } from "vitest";
+import { ElectronFuseService } from "../application/electron/ElectronFuseService.js";
+import { electronFuseReportSchema } from "../domain/electron/electronFuses.js";
+import { ElectronFuseProvider } from "./ElectronFuseProvider.js";
+import { createTestTempDirectory } from "../../tests/fixtures/temporaryDirectory.js";
+import {
+ elfHeaderFuseImage,
+ peHeaderFuseImage,
+ OBSIDIAN_FUSE_BYTES,
+} from "../../tests/fixtures/electron/fuseImage.js";
+
+it.each([
+ [3, 32, "x86"],
+ [62, 64, "x86_64"],
+ [40, 32, "arm"],
+ [183, 64, "arm64"],
+ [8, 32, "mips"],
+ [8, 64, "mips"],
+ [20, 32, "powerpc"],
+ [243, 64, "riscv"],
+ [0xfffe, 64, null],
+] as const)(
+ "preserves ELF machine %i (%i-bit) and its fuse bytes in both byte orders",
+ async (machine, bits, architecture) => {
+ for (const byteOrder of ["little", "big"] as const) {
+ const image = elfHeaderFuseImage(machine, OBSIDIAN_FUSE_BYTES, {
+ bits,
+ byteOrder,
+ });
+ await assertArchitecture(image, architecture);
+ }
+ },
+);
+
+it.each([
+ [0x14c, "x86"],
+ [0x8664, "x86_64"],
+ [0x1c0, "arm"],
+ [0x1c2, "arm"],
+ [0x1c4, "arm"],
+ [0xaa64, "arm64"],
+ [0x01f0, null],
+ [0x8664 ^ 0x7b79, null],
+] as const)(
+ "preserves PE machine %i without assuming managed OS overrides",
+ async (machine, architecture) => {
+ await assertArchitecture(
+ peHeaderFuseImage(machine, OBSIDIAN_FUSE_BYTES),
+ architecture,
+ );
+ },
+);
+
+it("keeps an invalid ELF fixed header unknown without dropping fuse observations", async () => {
+ const image = elfHeaderFuseImage(62, OBSIDIAN_FUSE_BYTES);
+ image.bytes[6] = 0;
+ await assertArchitecture(image, null);
+});
+
+it("keeps a truncated COFF header unknown without refusing a recognized PE container", async () => {
+ const image = peHeaderFuseImage(0x8664, OBSIDIAN_FUSE_BYTES);
+ const bytes = image.bytes.subarray(0, 0x80 + 6);
+ const root = await createTestTempDirectory("rea-fuse-coff-truncated-");
+ const path = join(root, "selected");
+ await writeFile(path, bytes);
+ const result = await new ElectronFuseService(
+ new ElectronFuseProvider(),
+ ).inspect({ path });
+ expect(result).toMatchObject({
+ ok: true,
+ value: {
+ subject: { architecture: null },
+ normalized_result: {
+ slices: [{ architecture: null, sentinel_count: 0 }],
+ },
+ },
+ });
+});
+
+it("reads PE machine facts independently of optional-header validity", async () => {
+ const image = peHeaderFuseImage(0x8664, OBSIDIAN_FUSE_BYTES);
+ image.bytes.writeUInt16LE(0, 0x80 + 24);
+ await assertArchitecture(image, "x86_64");
+});
+
+const assertArchitecture = async (
+ image: { readonly bytes: Buffer; readonly sentinelOffset: number },
+ architecture: string | null,
+): Promise<void> => {
+ const root = await createTestTempDirectory("rea-fuse-architecture-");
+ const path = join(root, "selected");
+ await writeFile(path, image.bytes);
+ const result = await new ElectronFuseService(
+ new ElectronFuseProvider(),
+ ).inspect({ path });
+ if (!result.ok) throw result.error;
+ expect(result.value.subject?.architecture).toBe(architecture);
+ expect(result.value.subject?.digest.sha256).toBe(
+ createHash("sha256").update(image.bytes).digest("hex"),
+ );
+ expect(result.value.normalized_result).toMatchObject({
+ slices: [
+ {
+ architecture,
+ sentinel_count: 1,
+ sentinels: [{ sentinel_offset: image.sentinelOffset, complete: true }],
+ },
+ ],
+ });
+ const report = electronFuseReportSchema.parse(result.value.normalized_result);
+ expect(
+ report.slices[0]?.sentinels[0]?.fuses.map((fuse) => fuse.raw_byte),
+ ).toEqual(OBSIDIAN_FUSE_BYTES);
+ expect(await readFile(path)).toEqual(image.bytes);
+};
diff --git a/src/electron/ElectronFuseReader.ts b/src/electron/ElectronFuseReader.ts
index dc135c78c..c7b764091 100644
--- a/src/electron/ElectronFuseReader.ts
+++ b/src/electron/ElectronFuseReader.ts
@@ -1,3 +1,5 @@
+import { readElfFixedHeader } from "../domain/elfFixedHeader.js";
+import { decodePeMachine } from "../domain/peMachine.js";
import type { ResolvedElectronFuseFile } from "./ElectronFusePath.js";
import { createHash } from "node:crypto";
import { lstat, type FileHandle } from "node:fs/promises";
@@ -211,15 +213,28 @@ const identifyContainer = async (
}> => {
const head = await readAt(0, Math.min(size, 8));
if (hasMachoMagic(head)) return identifyMacho(readAt, size, head, path);
- if (isElf(head))
+ if (isElf(head)) {
+ const header = await readAt(0, 64);
+ const parsed = readElfFixedHeader(Buffer.from(header), size);
return {
format: "elf",
- slices: [{ index: 0, fileOffset: 0, size, architecture: null }],
+ slices: [
+ {
+ index: 0,
+ fileOffset: 0,
+ size,
+ architecture: parsed.ok ? parsed.value.architecture : null,
+ },
+ ],
};
- if (await isPe(readAt, size))
+ }
+ const pe = await identifyPe(readAt, size);
+ if (pe !== undefined)
return {
format: "pe",
- slices: [{ index: 0, fileOffset: 0, size, architecture: null }],
+ slices: [
+ { index: 0, fileOffset: 0, size, architecture: pe.architecture },
+ ],
};
throw new ElectronFuseFormatFailure(
"unsupported",
@@ -458,11 +473,14 @@ const isFat = (head: Uint8Array): boolean => {
return FAT_MAGICS.has(magic);
};
-const isPe = async (readAt: ReadAt, size: number): Promise<boolean> => {
- if (size < 64) return false;
+const identifyPe = async (
+ readAt: ReadAt,
+ size: number,
+): Promise<{ readonly architecture: string | null } | undefined> => {
+ if (size < 64) return undefined;
const header = await readAt(0, 64);
if (header.length < 64 || header[0] !== 0x4d || header[1] !== 0x5a)
- return false;
+ return undefined;
const offset = Buffer.from(
header.buffer,
header.byteOffset,
@@ -474,15 +492,18 @@ const isPe = async (readAt: ReadAt, size: number): Promise<boolean> => {
offset > ELECTRON_FUSE_LIMITS.maximumPeHeaderOffset ||
offset > size - 4
)
- return false;
- const signature = await readAt(offset, 4);
- return (
- signature.length === 4 &&
- signature[0] === 0x50 &&
- signature[1] === 0x45 &&
- signature[2] === 0 &&
- signature[3] === 0
- );
+ return undefined;
+ const bytes = Buffer.from(await readAt(offset, 24));
+ if (bytes.length < 4 || bytes.readUInt32LE(0) !== 0x00004550)
+ return undefined;
+ // A signature establishes the existing scan boundary; only a complete COFF
+ // header establishes its machine. Managed OS overrides need separate proof.
+ const machine =
+ bytes.length === 24 ? decodePeMachine(bytes.readUInt16LE(4)) : undefined;
+ return {
+ architecture:
+ machine?.nativeOsOverride === false ? machine.architecture : null,
+ };
};
const isMissingPath = (cause: unknown): boolean =>
diff --git a/src/go/GoElfImage.ts b/src/go/GoElfImage.ts
index 1ba0c3058..7dfb0aaa9 100644
--- a/src/go/GoElfImage.ts
+++ b/src/go/GoElfImage.ts
@@ -217,10 +217,9 @@ export const readGoElfImage = (bytes: Buffer): GoBinaryContainer => {
}
}
}
- const machine = reader.u16(18);
return {
format: "elf",
- architecture: elfArchitecture(machine),
+ architecture: goArchitectureForElfMachine(fixedHeader.value.machine),
bits,
byte_order: reader.little ? "little" : "big",
mappings,
@@ -324,7 +323,8 @@ const elfImageSection = ({
const SHT_NOBITS = 8;
-const elfArchitecture = (machine: number): string => {
+/** Go build-info vocabulary, including families outside REA target admission. */
+const goArchitectureForElfMachine = (machine: number): string => {
const names = new Map([
[3, "386"],
[8, "mips"],
diff --git a/tests/acceptance/electronFusesCli.test.ts b/tests/acceptance/electronFusesCli.test.ts
index e1e86f276..a43b39b53 100644
--- a/tests/acceptance/electronFusesCli.test.ts
+++ b/tests/acceptance/electronFusesCli.test.ts
@@ -5,7 +5,9 @@ import { expect } from "vitest";
import { toolContract } from "../../src/contracts/toolContracts.js";
import {
+ elfHeaderFuseImage,
OBSIDIAN_FUSE_BYTES,
+ peHeaderFuseImage,
sentinelFreeMacho,
thinFuseImage,
universalFuseImage,
@@ -213,6 +215,58 @@ cliTest(
},
);
+cliTest.for([
+ { label: "ELF x86", format: "elf", machine: 3, architecture: "x86" },
+ { label: "ELF x86-64", format: "elf", machine: 62, architecture: "x86_64" },
+ { label: "ELF AArch64", format: "elf", machine: 183, architecture: "arm64" },
+ { label: "ELF unknown", format: "elf", machine: 0xfffe, architecture: null },
+ { label: "PE x86", format: "pe", machine: 0x14c, architecture: "x86" },
+ { label: "PE AMD64", format: "pe", machine: 0x8664, architecture: "x86_64" },
+ { label: "PE ARM64", format: "pe", machine: 0xaa64, architecture: "arm64" },
+ { label: "PE unknown", format: "pe", machine: 0x01f0, architecture: null },
+] as const)(
+ "reports the $label header architecture beside unchanged fuse wires",
+ async ({ format, machine, architecture }, { cli }) => {
+ const root = await createTestTempDirectory("rea-electron-fuses-cli-arch-");
+ const image =
+ format === "elf"
+ ? elfHeaderFuseImage(machine, OBSIDIAN_FUSE_BYTES, {
+ bits: architecture === "x86" ? 32 : 64,
+ byteOrder: "little",
+ })
+ : peHeaderFuseImage(machine, OBSIDIAN_FUSE_BYTES);
+ const path = join(root, "electron");
+ await writeFile(path, image.bytes);
+ const result = await cli.run({
+ arguments: ["inspect-electron-fuses", path, "--json"],
+ });
+ expect(result.exitCode, result.stderr).toBe(0);
+ const evidence = toolContract("inspect_electron_fuses").outputSchema.parse(
+ result.json,
+ );
+ expect(evidence.normalized_result.artifact.format).toBe(format);
+ expect(evidence.subject?.architecture).toBe(architecture);
+ expect(evidence.normalized_result.slices).toMatchObject([
+ {
+ architecture,
+ sentinel_count: 1,
+ sentinels: [
+ {
+ sentinel_offset: image.sentinelOffset,
+ wire_version: 1,
+ complete: true,
+ },
+ ],
+ },
+ ]);
+ expect(
+ evidence.normalized_result.slices[0]?.sentinels[0]?.fuses.map(
+ (fuse) => fuse.raw_byte,
+ ),
+ ).toEqual(OBSIDIAN_FUSE_BYTES);
+ },
+);
+
cliTest(
"reports a missing sentinel as unknown fuse state through the public CLI",
async ({ cli }) => {
diff --git a/tests/boundary/mcp/electronFusesMcp.test.ts b/tests/boundary/mcp/electronFusesMcp.test.ts
index db398cd0f..5ade4b474 100644
--- a/tests/boundary/mcp/electronFusesMcp.test.ts
+++ b/tests/boundary/mcp/electronFusesMcp.test.ts
@@ -11,6 +11,9 @@ import { createServer } from "../../../src/server/createServer.js";
import { ELECTRON_FUSE_STATE_BYTES } from "../../../src/domain/electron/electronFuses.js";
import {
elfFuseImage,
+ elfHeaderFuseImage,
+ peHeaderFuseImage,
+ OBSIDIAN_FUSE_BYTES,
truncatedFuseImage,
twoSentinelElf,
} from "../../fixtures/electron/fuseImage.js";
@@ -178,3 +181,49 @@ fuseMcpTest(
}
},
);
+
+fuseMcpTest.for([
+ { format: "elf", machine: 3, architecture: "x86" },
+ { format: "elf", machine: 62, architecture: "x86_64" },
+ { format: "elf", machine: 183, architecture: "arm64" },
+ { format: "elf", machine: 0xfffe, architecture: null },
+ { format: "pe", machine: 0x14c, architecture: "x86" },
+ { format: "pe", machine: 0x8664, architecture: "x86_64" },
+ { format: "pe", machine: 0xaa64, architecture: "arm64" },
+ { format: "pe", machine: 0x01f0, architecture: null },
+] as const)(
+ "preserves $format machine $machine architecture in MCP Evidence",
+ async ({ format, machine, architecture }, { fuse: { client } }) => {
+ const root = await createTestTempDirectory("rea-fuse-mcp-architecture-");
+ const path = join(root, "selected");
+ const image =
+ format === "elf"
+ ? elfHeaderFuseImage(machine, OBSIDIAN_FUSE_BYTES, {
+ bits: architecture === "x86" ? 32 : 64,
+ byteOrder: "little",
+ })
+ : peHeaderFuseImage(machine, OBSIDIAN_FUSE_BYTES);
+ await writeFile(path, image.bytes);
+ const response = await client.callTool({
+ name: "inspect_electron_fuses",
+ arguments: { path },
+ });
+ expect(response.isError, JSON.stringify(response.content)).not.toBe(true);
+ const evidence = toolContract("inspect_electron_fuses").outputSchema.parse(
+ response.structuredContent,
+ );
+ expect(evidence.subject?.architecture).toBe(architecture);
+ expect(evidence.normalized_result.slices).toMatchObject([
+ {
+ architecture,
+ sentinel_count: 1,
+ sentinels: [{ sentinel_offset: image.sentinelOffset, complete: true }],
+ },
+ ]);
+ expect(
+ evidence.normalized_result.slices[0]?.sentinels[0]?.fuses.map(
+ (fuse) => fuse.raw_byte,
+ ),
+ ).toEqual(OBSIDIAN_FUSE_BYTES);
+ },
+);
diff --git a/tests/fixtures/electron/fuseImage.ts b/tests/fixtures/electron/fuseImage.ts
index e887f83ec..d618a375f 100644
--- a/tests/fixtures/electron/fuseImage.ts
+++ b/tests/fixtures/electron/fuseImage.ts
@@ -144,3 +144,59 @@ export const twoSentinelElf = (): {
export const sentinelFreeMacho = (): Buffer =>
thinMacho(0x0100000c, Buffer.from("no fuse wire in this image"));
+
+/** Fixed ELF header declaring `machine`, followed by one fuse wire. */
+export const elfHeaderFuseImage = (
+ machine: number,
+ fuses: readonly number[],
+ layout: { readonly bits: 32 | 64; readonly byteOrder: "little" | "big" } = {
+ bits: 64,
+ byteOrder: "little",
+ },
+): { readonly bytes: Buffer; readonly sentinelOffset: number } => {
+ const header = Buffer.alloc(layout.bits === 32 ? 52 : 64);
+ const little = layout.byteOrder === "little";
+ header.set(
+ [0x7f, 0x45, 0x4c, 0x46, layout.bits === 32 ? 1 : 2, little ? 1 : 2, 1],
+ 0,
+ );
+ if (little) {
+ header.writeUInt16LE(3, 16);
+ header.writeUInt16LE(machine, 18);
+ header.writeUInt32LE(1, 20);
+ header.writeUInt16LE(header.length, layout.bits === 32 ? 40 : 52);
+ } else {
+ header.writeUInt16BE(3, 16);
+ header.writeUInt16BE(machine, 18);
+ header.writeUInt32BE(1, 20);
+ header.writeUInt16BE(header.length, layout.bits === 32 ? 40 : 52);
+ }
+ return {
+ bytes: Buffer.concat([header, FUSE_SENTINEL, fuseWire(1, fuses)]),
+ sentinelOffset: header.length,
+ };
+};
+
+/** PE32/PE32+ application header declaring `machine`, followed by one fuse wire. */
+export const peHeaderFuseImage = (
+ machine: number,
+ fuses: readonly number[],
+): { readonly bytes: Buffer; readonly sentinelOffset: number } => {
+ const dos = Buffer.alloc(0x80);
+ dos.write("MZ", 0, "ascii");
+ dos.writeUInt32LE(dos.length, 0x3c);
+ const pe32 = [0x14c, 0x1c0, 0x1c2, 0x1c4].includes(machine);
+ const optional = Buffer.alloc(pe32 ? 0xe0 : 0xf0);
+ optional.writeUInt16LE(pe32 ? 0x10b : 0x20b, 0);
+ optional.writeUInt32LE(16, pe32 ? 92 : 108);
+ const coff = Buffer.alloc(24);
+ coff.write("PE\0\0", 0, "binary");
+ coff.writeUInt16LE(machine, 4);
+ coff.writeUInt16LE(optional.length, 20);
+ coff.writeUInt16LE(0x0022, 22);
+ const header = Buffer.concat([dos, coff, optional]);
+ return {
+ bytes: Buffer.concat([header, FUSE_SENTINEL, fuseWire(1, fuses)]),
+ sentinelOffset: header.length,
+ };
+};