Files
reverse-engineering-anything/src/application/BinaryTargetExecutableProbes.ts
T
morluto 782c274200 refactor(elf): decouple structural admission from lane policies and type MIPS limitations (#14356)
Decouple fatal ELF structural/framing checks from optional trailing
section-table range observations via an explicit sectionRanges policy
("strict" | "observe") on validateElfDeclaredRanges, keeping native
section-directory and non-MIPS32 validation strict while making
readMipsElfMetadata reachable for carved MIPS32 ELFs.

Replace untyped string[] limitations on MipsElfMetadata and
MipsElfInspection with the MipsElfLimitation discriminated union
("truncated_section_table" | "unexamined_declarations") so
GhidraMipsProfile switches on typed kinds rather than prose prefixes,
admitting carved recordless legacy MIPS32 ET_EXEC images while keeping
declared-ABI, non-ET_EXEC, PSP, and non-section-truncation limitations
fail-closed.

Closes #14343
2026-11-24 14:07:27 +00:00

561 lines
18 KiB
TypeScript

import type { FileHandle } from "node:fs/promises";
import { cancelledBy } from "../domain/analysisErrorCore.js";
import {
hasJvmClassHeader,
isJvmClassFile,
MAX_JVM_CLASS_IDENTITY_BYTES,
} from "../domain/apple/jvmClassFile.js";
import {
classifyFatContainerIdentity,
readFatContainerHeader,
readFatJvmIdentityLimitIssue,
readFatSliceHeaders,
type FatSliceHeaderEvidence,
} from "../domain/apple/machoContainer.js";
import {
readMachoLoadCommandTableHeader,
validateMachoLoadCommandFraming,
} from "../domain/apple/machoLoadCommands.js";
import {
MAX_PE_OPTIONAL_HEADER_BYTES,
PE_RECORD_HEADER_BYTES,
parseCodeViewData,
parseExecutableHeader,
parseMzExecutableMetadata,
parsePeRecordFacts,
peCodeViewDirectoryEntries,
peDebugDirectoryEntry,
peRvaToFileOffset,
type ExecutableMetadata,
} from "../domain/binaryTarget.js";
import {
isRecordlessLegacyMipsExecHeader,
PE_CODEVIEW_PREFIX_BYTES,
type PeCodeViewRecord,
type UniversalMachOSlice,
} from "../domain/binaryTargetTypes.js";
import {
hasDosMzMagic,
parseMzHeaderEvidence,
withMzWindowsHeaderSignature,
type DosMzHeader,
type MzHeaderEvidence,
type MzWindowsHeaderObservation,
} from "../domain/dosMz.js";
import {
validateElfDeclaredRanges,
type ElfDeclarationRead,
} from "../domain/elfDeclaredRanges.js";
import type { JsonValue } from "../domain/jsonValue.js";
import { readPeCoffHeader } from "../domain/peCoffHeader.js";
import { peOptionalHeaderReadBytes } from "../domain/peOptionalHeader.js";
import { err, ok, type Result } from "../domain/result.js";
import { readRiscvIsaDeclaration } from "../domain/riscvIsaDeclaration.js";
import { readFileHandleRange } from "../filesystem/readFileHandleRange.js";
import type { StableRegularFileDescriptor } from "../filesystem/RegularFile.js";
import { readMipsElfMetadata } from "./MipsElfMetadata.js";
export type ExecutableMetadataFailure =
| string
| {
readonly reason: string;
readonly diagnostics: Readonly<Record<string, JsonValue>>;
};
export const throwIfTargetResolutionCancelled = (
signal?: AbortSignal,
): void => {
if (signal?.aborted === true)
throw cancelledBy("resolve_binary_target", signal);
};
/** Fill one bounded range, honoring cancellation; a short result means end of file. */
export const readHandleRange = async (
handle: FileHandle,
position: number,
length: number,
signal?: AbortSignal,
): Promise<Buffer> => {
const bytes = await readFileHandleRange(handle, position, length, () =>
throwIfTargetResolutionCancelled(signal),
);
throwIfTargetResolutionCancelled(signal);
return bytes;
};
/** Probe native executable headers (MZ/PE, ELF, thin/FAT Mach-O) over a stable descriptor. */
export const readExecutableMetadata = async (
source: StableRegularFileDescriptor,
hostArchitecture: NodeJS.Architecture,
signal?: AbortSignal,
): Promise<Result<ExecutableMetadata, ExecutableMetadataFailure>> => {
const { handle, initial } = source;
throwIfTargetResolutionCancelled(signal);
const bytes = await readHandleRange(handle, 0, 4096, signal);
// Header commitments such as Mach-O load commands and FAT slices are
// checked against the whole file, not the probed prefix.
const fileSize = initial.size;
if (hasDosMzMagic(bytes)) {
let evidence = parseMzHeaderEvidence(bytes, fileSize);
let signatureEvidence:
| { readonly offset: number; readonly bytes: Buffer }
| undefined;
if (evidence.windowsHeader.state === "signature-needed") {
const { offset } = evidence.windowsHeader;
signatureEvidence = {
offset,
bytes: await readHandleRange(
handle,
offset,
Math.min(4, fileSize - offset),
signal,
),
};
evidence = withMzWindowsHeaderSignature(
bytes,
fileSize,
evidence,
signatureEvidence,
);
}
const { windowsHeader } = evidence;
if (windowsHeader.state === "declared") {
return readPeMetadata(handle, windowsHeader.offset, {
mzPrefix: bytes,
fileSize,
mzEvidence: evidence,
signal,
});
}
if (windowsHeader.state === "candidate") {
// Extend the bounded prefix only for unresolved Windows-header candidates
// so diagnostics can report accurate DOS relocation structure.
const { relocationTableRead } = evidence;
if (relocationTableRead !== undefined) {
const observedBytes = await readHandleRange(
handle,
relocationTableRead.offset,
relocationTableRead.byteLength,
signal,
);
evidence = parseMzHeaderEvidence(
observedBytes,
fileSize,
signatureEvidence,
);
if (evidence.windowsHeader.state === "candidate")
return err(
unresolvedMzHeaderFailure(evidence.windowsHeader, evidence.dos),
);
return parseMzExecutableMetadata(observedBytes, evidence);
}
return err(unresolvedMzHeaderFailure(windowsHeader, evidence.dos));
}
return parseMzExecutableMetadata(bytes, evidence);
}
if (bytes.length >= 4 && bytes.toString("hex", 0, 4) === "7f454c46")
return readElfMetadata(handle, bytes, {
fileSize,
hostArchitecture,
signal,
});
const thin = await readThinMachoMetadata(handle, bytes, {
fileSize,
hostArchitecture,
signal,
});
if (thin !== null) return thin;
const mayBeJvmClass = hasJvmClassHeader(bytes);
if (
mayBeJvmClass &&
fileSize > bytes.length &&
fileSize <= MAX_JVM_CLASS_IDENTITY_BYTES
) {
const complete = await readHandleRange(handle, 0, fileSize, signal);
if (complete.byteLength === fileSize && isJvmClassFile(complete))
return err("unsupported binary format");
}
const fatEvidence = await readFatSliceHeaderEvidence(
handle,
bytes,
fileSize,
signal,
);
if (
mayBeJvmClass &&
fileSize > MAX_JVM_CLASS_IDENTITY_BYTES &&
fatEvidence !== undefined
) {
const { header, sliceHeadersByOffset } = fatEvidence;
const identity = classifyFatContainerIdentity(
header,
(offset) => sliceHeadersByOffset.get(offset)?.magic,
{ ignoreAlignmentIssues: true },
);
const limitIssue = await readFatJvmIdentityLimitIssue(
header,
identity,
(offset, length) => readHandleRange(handle, offset, length, signal),
);
if (limitIssue !== null) return err(limitIssue);
}
const parsed = parseExecutableHeader(
bytes,
hostArchitecture,
fileSize,
undefined,
fatEvidence,
);
if (
parsed.ok &&
parsed.value.format === "mach-o-universal" &&
fatEvidence !== undefined
) {
const framed = await readMachoSliceCommandFrames(
handle,
parsed.value.universalSlice,
signal,
);
if (!framed.ok) return framed;
}
return parsed;
};
/** ELF structure and family declarations share one descriptor before provider selection. */
const readElfMetadata = async (
handle: FileHandle,
bytes: Buffer,
options: {
readonly fileSize: number;
readonly hostArchitecture: NodeJS.Architecture;
readonly signal?: AbortSignal | undefined;
},
): Promise<Result<ExecutableMetadata, ExecutableMetadataFailure>> => {
const { fileSize, hostArchitecture, signal } = options;
const parsed = parseExecutableHeader(bytes, hostArchitecture, fileSize);
if (!parsed.ok || parsed.value.format !== "elf") return parsed;
const isMips32 =
parsed.value.architecture === "mips" && parsed.value.mips.elfClass === 32;
const ranges = await readElfDeclarations(
handle,
bytes,
validateElfDeclaredRanges(bytes, fileSize, {
sectionRanges: isMips32 ? "observe" : "strict",
}),
signal,
);
if (!ranges.ok) return ranges;
if (parsed.value.architecture === "riscv")
return ok({
...parsed.value,
riscv: {
...parsed.value.riscv,
...(ranges.value.hasInterpreter ? { hasInterpreter: true } : {}),
isa: await readElfDeclarations(
handle,
bytes,
readRiscvIsaDeclaration(bytes, fileSize),
signal,
),
},
});
if (!isMips32) return parsed;
const { sectionTruncation } = ranges.value;
if (sectionTruncation?.kind === "truncated_section_payload")
return err(sectionTruncation.reason);
const declarations = await readMipsElfMetadata(
handle,
parsed.value.mips,
() => throwIfTargetResolutionCancelled(signal),
);
if (!declarations.ok) return declarations;
if (sectionTruncation !== undefined) {
const admitsTruncatedSectionTable =
sectionTruncation.kind === "truncated_section_table" &&
isRecordlessLegacyMipsExecHeader(parsed.value.mips) &&
declarations.value.abiFlags === null;
if (!admitsTruncatedSectionTable) return err(sectionTruncation.reason);
}
return ok({
...parsed.value,
mips: {
...parsed.value.mips,
abiFlags: declarations.value.abiFlags,
gnuAttributes: declarations.value.gnuAttributes,
...(ranges.value.hasInterpreter ? { hasInterpreter: true } : {}),
...(declarations.value.sharedLoad === undefined
? {}
: { sharedLoad: declarations.value.sharedLoad }),
...(declarations.value.limitations.length === 0
? {}
: { limitations: declarations.value.limitations }),
},
});
};
/**
* Satisfy one ELF declaration reader from the admitted descriptor. Every
* request is at most one 64 KiB chunk, and the reader consumes each chunk
* before its next request reuses the window.
*/
const readElfDeclarations = async <T>(
handle: FileHandle,
prefix: Buffer,
steps: Generator<ElfDeclarationRead, T, Buffer>,
signal?: AbortSignal,
): Promise<T> => {
let next = steps.next();
let window: Buffer | undefined;
while (!next.done) {
throwIfTargetResolutionCancelled(signal);
const { offset, size } = next.value;
let bytes: Buffer;
if (offset + size <= prefix.length)
bytes = prefix.subarray(offset, offset + size);
else {
window ??= Buffer.allocUnsafe(64 * 1024);
bytes = window.subarray(0, size);
let length = 0;
while (length < size) {
const read = await handle.read(
bytes,
length,
size - length,
offset + length,
);
throwIfTargetResolutionCancelled(signal);
if (read.bytesRead === 0) break;
length += read.bytesRead;
}
bytes = bytes.subarray(0, length);
}
next = steps.next(bytes);
}
return next.value;
};
const unresolvedMzHeaderFailure = (
windowsHeader: Extract<MzWindowsHeaderObservation, { state: "candidate" }>,
dos: Result<DosMzHeader, string>,
): Exclude<ExecutableMetadataFailure, string> => {
return {
reason: "MZ Windows new-header candidate is unresolved",
diagnostics: {
mz_windows_header: {
state: windowsHeader.state,
offset: windowsHeader.offset,
signature: windowsHeader.signature,
},
mz_dos: dos.ok
? {
state: "valid",
header_bytes: dos.value.headerBytes,
image_bytes: dos.value.imageBytes,
module_bytes: dos.value.moduleBytes,
overlay_bytes: dos.value.overlayBytes,
relocation_count: dos.value.relocationCount,
relocation_table_offset: dos.value.relocationTableOffset,
entry_segment: dos.value.entrySegment,
entry_offset: dos.value.entryOffset,
}
: { state: "invalid", reason: dos.error },
},
};
};
/** Establish a complete thin-command framing proof before provider admission. */
const readThinMachoMetadata = async (
handle: FileHandle,
prefix: Buffer,
input: {
readonly fileSize: number;
readonly hostArchitecture: NodeJS.Architecture;
readonly signal?: AbortSignal | undefined;
},
): Promise<Result<ExecutableMetadata, string> | null> => {
const table = readMachoLoadCommandTableHeader(prefix, input.fileSize);
if (!table.ok) return table;
if (table.value === null) return null;
const parsed = parseExecutableHeader(
prefix.subarray(0, table.value.headerSize),
input.hostArchitecture,
input.fileSize,
);
if (!parsed.ok) return parsed;
const framed = await validateMachoLoadCommandFraming(table.value, {
prefix,
fileSize: input.fileSize,
readWindow: (offset, length) =>
readHandleRange(handle, offset, length, input.signal),
});
return framed.ok ? parsed : framed;
};
/** Validate the selected FAT slice's load-command framing after container admission succeeds. */
const readMachoSliceCommandFrames = async (
handle: FileHandle,
slice: UniversalMachOSlice,
signal?: AbortSignal,
): Promise<Result<null, string>> => {
const bytes = await readHandleRange(
handle,
slice.offset,
Math.min(4096, slice.size),
signal,
);
const table = readMachoLoadCommandTableHeader(bytes, slice.size);
if (!table.ok) return table;
if (table.value === null)
return err("FAT slice does not identify a thin Mach-O image");
return validateMachoLoadCommandFraming(table.value, {
prefix: bytes,
fileSize: slice.size,
readWindow: (offset, length) =>
readHandleRange(handle, slice.offset + offset, length, signal),
});
};
/** Read the validated FAT table and bounded thin-header identity prefixes. */
const readFatSliceHeaderEvidence = async (
handle: FileHandle,
prefix: Buffer,
fileSize: number,
signal?: AbortSignal,
): Promise<FatSliceHeaderEvidence | undefined> => {
let header = readFatContainerHeader(prefix, fileSize);
if (header.state === "needs-data") {
// readFatContainerHeader enforces the 128-entry format bound before this
// allocation; this is only the table, never a synthesized file image.
const table = await readHandleRange(
handle,
0,
header.requiredBytes,
signal,
);
header = readFatContainerHeader(table, fileSize);
}
if (header.state !== "parsed") return undefined;
return readFatSliceHeaders(
header,
(offset, length) => readHandleRange(handle, offset, length, signal),
{ includeMisaligned: true },
);
};
const readPeMetadata = async (
handle: FileHandle,
offset: number,
{
mzPrefix,
fileSize,
mzEvidence,
signal,
}: {
readonly mzPrefix: Buffer;
readonly fileSize: number;
readonly mzEvidence: MzHeaderEvidence;
readonly signal: AbortSignal | undefined;
},
): Promise<Result<ExecutableMetadata, string>> => {
throwIfTargetResolutionCancelled(signal);
const fileHeader = await readHandleRange(handle, offset, 24, signal);
if (fileHeader.length !== 24) return err("invalid or truncated PE header");
const { windowsHeader } = withMzWindowsHeaderSignature(
mzPrefix,
fileSize,
mzEvidence,
{ offset, bytes: fileHeader.subarray(0, 4) },
);
if (windowsHeader.state !== "declared")
return err("invalid Windows new-header signature in MZ image");
if (["NE", "LE", "LX"].includes(windowsHeader.signature))
return err(`unsupported ${windowsHeader.signature} executable in MZ image`);
const coff = readPeCoffHeader(fileHeader.subarray(4));
if (!coff.ok) return err("invalid or truncated PE header");
const optionalHeaderSize = coff.value.optionalHeaderSize;
const numberOfSections = coff.value.sectionCount;
const optionalOffset = offset + PE_RECORD_HEADER_BYTES;
const optionalAvailable = Math.max(0, fileSize - optionalOffset);
const optionalReadLength = Math.min(
peOptionalHeaderReadBytes(optionalHeaderSize, optionalAvailable),
MAX_PE_OPTIONAL_HEADER_BYTES,
);
const record = await readHandleRange(
handle,
offset,
PE_RECORD_HEADER_BYTES + optionalReadLength,
signal,
);
if (record.length < PE_RECORD_HEADER_BYTES)
return err("invalid or truncated PE header");
const codeView = await readPeCodeView(
handle,
offset,
numberOfSections,
record.subarray(PE_RECORD_HEADER_BYTES),
optionalHeaderSize,
signal,
);
const parsed = parsePeRecordFacts(record, codeView);
return parsed.ok ? ok(parsed.value.metadata) : parsed;
};
const readPeCodeView = async (
handle: FileHandle,
peOffset: number,
numberOfSections: number,
optionalHeader: Buffer,
optionalHeaderSize: number,
signal?: AbortSignal,
): Promise<PeCodeViewRecord | undefined> => {
throwIfTargetResolutionCancelled(signal);
const debugDir = peDebugDirectoryEntry(optionalHeader);
if (debugDir === undefined || numberOfSections > 96) return undefined;
const sectionTableOffset = peOffset + 24 + optionalHeaderSize;
const sectionTableSize = numberOfSections * 40;
const sectionTable = await readHandleRange(
handle,
sectionTableOffset,
sectionTableSize,
signal,
);
if (sectionTable.length !== sectionTableSize) return undefined;
const debugFileOffset = peRvaToFileOffset(
debugDir.debugRva,
sectionTable,
numberOfSections,
debugDir.sizeOfHeaders,
Math.min(debugDir.debugSize, 4096),
);
if (debugFileOffset === undefined) return undefined;
const maxDebugSize = Math.min(debugDir.debugSize, 4096);
const debugDirectoryBuffer = await readHandleRange(
handle,
debugFileOffset,
maxDebugSize,
signal,
);
for (const {
fileOffset: pointerToRawData,
declaredSize: sizeOfData,
} of peCodeViewDirectoryEntries(
debugDirectoryBuffer,
sectionTable,
numberOfSections,
debugDir.sizeOfHeaders,
)) {
if (pointerToRawData === undefined) continue;
const maxCodeViewRead = Math.min(sizeOfData, PE_CODEVIEW_PREFIX_BYTES);
const codeViewBuffer = await readHandleRange(
handle,
pointerToRawData,
maxCodeViewRead,
signal,
);
const parsed = parseCodeViewData(codeViewBuffer, sizeOfData);
if (parsed !== undefined) return parsed;
}
return undefined;
};