fix(elf): centralize fixed header validation across consumers

Validate ELF identification/header versions and declared header bounds at the shared owner. Reuse parsed facts in target selection, Go, Dart and MIPS readers while preserving their stricter format policies and x32 support.

Repair synthetic ELF fixtures and remove a superseded Go error-text assertion. Validation: 74 source tests, 202 filesystem/Go/MIPS boundary tests, typecheck and lint. No new real-provider claims.
This commit is contained in:
morluto
2026-10-13 08:07:58 +08:00
parent f09bb8a997
commit 34a4bb6508
12 changed files with 205 additions and 59 deletions
+10 -3
View File
@@ -5,9 +5,16 @@ export const elf = (
machine: number,
): Buffer => {
const bytes = Buffer.alloc(class_ === 1 ? 52 : 64);
bytes.set([0x7f, 0x45, 0x4c, 0x46, class_, endian]);
if (endian === 1) bytes.writeUInt16LE(machine, 18);
else bytes.writeUInt16BE(machine, 18);
bytes.set([0x7f, 0x45, 0x4c, 0x46, class_, endian, 1]);
if (endian === 1) {
bytes.writeUInt16LE(machine, 18);
bytes.writeUInt32LE(1, 20);
bytes.writeUInt16LE(class_ === 1 ? 52 : 64, class_ === 1 ? 40 : 52);
} else {
bytes.writeUInt16BE(machine, 18);
bytes.writeUInt32BE(1, 20);
bytes.writeUInt16BE(class_ === 1 ? 52 : 64, class_ === 1 ? 40 : 52);
}
return bytes;
};
+4
View File
@@ -7,10 +7,14 @@ const header = (little: boolean, bits: 32 | 64 = 32): Buffer => {
if (little) {
bytes.writeUInt16LE(2, 16);
bytes.writeUInt16LE(8, 18);
bytes.writeUInt32LE(1, 20);
bytes.writeUInt16LE(bits === 32 ? 52 : 64, bits === 32 ? 40 : 52);
bytes.writeUInt32LE(0x70001001, bits === 32 ? 36 : 48);
} else {
bytes.writeUInt16BE(2, 16);
bytes.writeUInt16BE(8, 18);
bytes.writeUInt32BE(1, 20);
bytes.writeUInt16BE(bits === 32 ? 52 : 64, bits === 32 ? 40 : 52);
bytes.writeUInt32BE(0x70001001, bits === 32 ? 36 : 48);
}
return bytes;
+4
View File
@@ -13,10 +13,14 @@ const header = (
if (little) {
bytes.writeUInt16LE(type, 16);
bytes.writeUInt16LE(machine, 18);
bytes.writeUInt32LE(1, 20);
bytes.writeUInt16LE(bits === 32 ? 52 : 64, bits === 32 ? 40 : 52);
bytes.writeUInt32LE(flags, bits === 32 ? 36 : 48);
} else {
bytes.writeUInt16BE(type, 16);
bytes.writeUInt16BE(machine, 18);
bytes.writeUInt32BE(1, 20);
bytes.writeUInt16BE(bits === 32 ? 52 : 64, bits === 32 ? 40 : 52);
bytes.writeUInt32BE(flags, bits === 32 ? 36 : 48);
}
return bytes;
+7
View File
@@ -26,6 +26,13 @@ describe("executable header parsing", () => {
expect(result.ok && result.value).toMatchObject({ format, architecture });
});
it("retains ELF32 x86-64 identity for the x32 ABI", () => {
expect(parseExecutableHeader(elf(1, 1, 62), "x64")).toMatchObject({
ok: true,
value: { format: "elf", architecture: "x86_64" },
});
});
it("distinguishes native PE applications, DLLs, and managed assemblies", () => {
const application = parseExecutableHeader(pe(0x8664), "x64");
const library = parseExecutableHeader(pe(0x8664, 64, 0x2002), "x64");
+5 -10
View File
@@ -7,6 +7,7 @@ import type {
UniversalMachOSlice,
} from "./binaryTargetTypes.js";
import { err, ok, type Result } from "./result.js";
import { readElfFixedHeader } from "./elfFixedHeader.js";
import { observeMzWindowsHeader, parseDosMzHeader } from "./dosMz.js";
import { decodePeMachine } from "./peMachine.js";
import {
@@ -191,20 +192,14 @@ const parseElf = (
bytes: Buffer,
fileSize: number,
): Result<ExecutableMetadata, string> => {
if (bytes.length < 6) return err("truncated ELF header");
if (bytes[4] !== 1 && bytes[4] !== 2) return err("unsupported ELF class");
const little = bytes[5] === 1;
if (!little && bytes[5] !== 2) return err("unsupported ELF endianness");
const headerSize = bytes[4] === 1 ? 52 : 64;
if (bytes.length < headerSize)
return err(
`truncated ELF header: an ELF${bytes[4] === 1 ? 32 : 64} header needs ${headerSize} bytes; the file has ${fileSize}`,
);
const fixedHeader = readElfFixedHeader(bytes, fileSize);
if (!fixedHeader.ok) return fixedHeader;
const { elfClass, byteOrder } = 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 === "mips" || architecture === "riscv") {
const elfClass = bytes[4] === 1 ? 32 : 64;
const flagsOffset = elfClass === 32 ? 36 : 48;
const metadata: ElfFamilyMetadata = {
elfClass,
+61
View File
@@ -0,0 +1,61 @@
import { expect, it } from "vitest";
import { readElfFixedHeader } 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);
expect(readElfFixedHeader(bytes)).toEqual({
ok: true,
value: {
elfClass: cls === 1 ? 32 : 64,
byteOrder: data === 1 ? "little" : "big",
headerSize: cls === 1 ? 52 : 64,
},
});
},
);
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 });
});
+55
View File
@@ -0,0 +1,55 @@
import { err, ok, type Result } from "./result.js";
export interface ElfFixedHeader {
readonly elfClass: 32 | 64;
readonly byteOrder: "little" | "big";
readonly headerSize: number;
}
/**
* Validate the common, fixed ELF header commitments.
*
* `bytes` may be a bounded prefix. `totalBytes` is the containing file size,
* so the declared header extent is checked against the file without requiring
* the whole declared extent to be present in this prefix.
*/
export const readElfFixedHeader = (
bytes: Buffer,
totalBytes = bytes.length,
): Result<ElfFixedHeader, string> => {
if (bytes.length >= 4 && bytes.toString("hex", 0, 4) !== "7f454c46")
return err("not an ELF image");
if (bytes.length < 16) return err("ELF identification is truncated");
const classByte = bytes[4];
if (classByte !== 1 && classByte !== 2) return err("unsupported ELF class");
const dataByte = bytes[5];
if (dataByte !== 1 && dataByte !== 2)
return err("unsupported ELF endianness");
if (bytes[6] !== 1) return err("unsupported ELF identification version");
const elfClass = classByte === 1 ? 32 : 64;
const minimumSize = elfClass === 32 ? 52 : 64;
if (
!Number.isSafeInteger(totalBytes) ||
totalBytes < bytes.length ||
totalBytes < minimumSize ||
bytes.length < minimumSize
)
return err(
`truncated ELF header: an ELF${elfClass} header needs ${minimumSize} bytes; the file has ${totalBytes}`,
);
const littleEndian = dataByte === 1;
const readU16 = (offset: number): number =>
littleEndian ? bytes.readUInt16LE(offset) : bytes.readUInt16BE(offset);
const readU32 = (offset: number): number =>
littleEndian ? bytes.readUInt32LE(offset) : bytes.readUInt32BE(offset);
if (readU32(20) !== 1) return err("unsupported ELF header version");
const headerSize = readU16(elfClass === 32 ? 40 : 52);
if (headerSize < minimumSize || headerSize > totalBytes)
return err("invalid ELF header size");
return ok({
elfClass,
byteOrder: littleEndian ? "little" : "big",
headerSize,
});
};