Files
reverse-engineering-anything/src/native/initializers/NativeInitializersProvider.ts
T
morluto e540cbdafd feat(native)!: give the native structural decoding allowance a per-call max_structure_bytes (#14626)
* feat(native)!: give the native structural decoding allowance a per-call max_structure_bytes

The 16 MiB structural budget on native header, section, load-command and
ELF dynamic-directory table reads, and on decoded section names, was a
fixed ceiling with no override. inspect_elf_unwind_ranges,
inspect_kernel_module, inspect_elf_debug_link, inspect_dwarf_units,
inspect_binary_hardening and inspect_native_initializers now accept
max_structure_bytes (64 bytes to 512 MiB, default 16 MiB) and report
details.limit when it stops them.

binaryContainer owns the default, the limit name and the failure
(nativeStructureFailure); the directory readers take a
NativeDirectoryBudgets object in place of the positional decoded-name
budget, and Mach-O, the ELF dynamic directory and the ELF pointer
relocation pass take the same budget. Other allowances that shared the
"structure" limit name now carry their own: the PE per-name decode, the
retained DWARF section bound (derived from the admitted image), and the
hardening table-entry count. The ELF dependency provider's structural
projection was unreachable and is removed.

Part of #14235.

BREAKING CHANGE: refusals at the structural budget report
reported_limits.boundary "max_structure_bytes" instead of "structure".

* fix(native): apply max_structure_bytes to the ELF hardening dynamic table and scope its help

inspect_binary_hardening read PT_DYNAMIC under its entry count only, so a sectionless ELF could exceed the selected max_structure_bytes; the dynamic table is now checked first. The shared help no longer names tables a reader does not bound, and the limits reference states hardening's Mach-O and PE coverage. The initializer CLI recovery test asserts a parsed successful report.

* fix(native): refuse a partial ELF hardening dynamic table as malformed before its budget
2026-11-25 22:04:06 -05:00

245 lines
7.6 KiB
TypeScript

import {
isParseLimit,
parseLimitConstraint,
} from "../../domain/parseFailureAnalysisError.js";
import {
imageFileSizeRemediation,
closeArtifactResources,
projectStableArtifactFailure,
ArtifactReaderFailure,
readStableArtifact,
artifactFilePrimaryFacts,
assertArtifactReadActive,
type StableArtifactSnapshot,
ArtifactResourceScope,
} from "../../resources/index.js";
import {
hasElfMagic,
BinaryFormatFailure,
isNativeStructureFailure,
} from "../../domain/binaryContainer.js";
import { nativeStructureConstraint } from "../nativeStructureConstraint.js";
import type { NativeInitializersPort } from "../../ports/binaryDiagnostics/NativeInitializersPort.js";
import type { ExecutionOptions } from "../../ports/AnalysisProvider.js";
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
import { AnalysisResourceConstraintError } from "../../domain/analysisErrorCore.js";
import { classifyProviderFailure } from "../../providers/classifyProviderFailure.js";
import { offlineArtifactFailureDialect } from "../../providers/offlineArtifactFailure.js";
import { fixedAllowanceOptions } from "../../domain/fixedAllowanceRemediation.js";
import {
MAX_INSPECT_NATIVE_INITIALIZERS_FILE_BYTES,
type InspectNativeInitializersInput,
type NativeInitializerInspection,
type NativeInitializers,
type NativeInitializerSlice,
} from "../../domain/native/nativeInitializers.js";
import { err, ok } from "../../domain/result.js";
import { NATIVE_INITIALIZERS_PROVIDER } from "../../domain/investigationProviders.js";
import {
ELF_POINTER_RELOCATION_FORMAT,
ElfPointerRelocationFailure,
} from "../../domain/native/elfPointerRelocations.js";
import { hasMachoMagic } from "../../domain/apple/machoContainer.js";
import { requireSelectedNativeImage } from "../../domain/apple/machoSliceSelection.js";
import { readElfInitializers } from "./ElfInitializers.js";
import { readMachoInitializers } from "./MachoInitializers.js";
const OPERATION = "inspect_native_initializers";
/** Portable ELF and Mach-O initializer reader over one stable regular file. */
export class NativeInitializersProvider implements NativeInitializersPort {
readonly identity = NATIVE_INITIALIZERS_PROVIDER;
readonly #resources = new ArtifactResourceScope();
close(): Promise<void> {
return closeArtifactResources(
() => this.#resources.close(),
NATIVE_INITIALIZERS_PROVIDER,
OPERATION,
);
}
async inspect(
input: InspectNativeInitializersInput,
options?: ExecutionOptions,
) {
let snapshot: StableArtifactSnapshot | undefined;
try {
return await this.#resources.run(async () => {
snapshot = await readStableArtifact(
input.path,
input.max_file_bytes,
options?.signal,
undefined,
this.#resources,
);
assertArtifactReadActive(options?.signal);
const report = await readNativeInitializers(
snapshot.bytes,
{
path: input.path,
sha256: snapshot.sha256,
bytes: snapshot.bytes.length,
},
input,
options?.signal,
);
assertArtifactReadActive(options?.signal);
return ok(report);
});
} catch (cause) {
return err(nativeInitializersFailure(cause, input, snapshot));
}
}
}
/** Decode one stable buffer. ELF is read whole; Mach-O uses the selected slice. */
export const readNativeInitializers = async (
bytes: Buffer,
artifact: NativeInitializers["artifact"],
input: Pick<
InspectNativeInitializersInput,
"offset" | "limit" | "architecture" | "path" | "max_structure_bytes"
>,
signal?: AbortSignal,
): Promise<NativeInitializers> => {
assertArtifactReadActive(signal);
const selected = requireSelectedNativeImage(
OPERATION,
input.path,
bytes,
input.architecture,
);
const page = { offset: input.offset, limit: input.limit };
const inspection = await inspectSelected(
bytes,
selected,
page,
signal,
input.max_structure_bytes,
);
return {
artifact,
...inspection,
limitations: [...inspection.limitations, ...selected.limitations],
};
};
const inspectSelected = async (
container: Buffer,
selected: ReturnType<typeof requireSelectedNativeImage>,
page: { readonly offset: number; readonly limit: number },
signal: AbortSignal | undefined,
structureBytes: number | undefined,
): Promise<NativeInitializerInspection> => {
if (hasElfMagic(selected.bytes)) {
if (selected.selection !== null)
throw new BinaryFormatFailure(
"malformed",
"ELF bytes are not a Mach-O slice",
);
return readElfInitializers(selected.bytes, page, signal, structureBytes);
}
if (!hasMachoMagic(container) && !hasMachoMagic(selected.bytes))
throw new BinaryFormatFailure(
"malformed",
"Selected file is not an ELF or Mach-O image",
);
const selection = selected.selection;
const slice: NativeInitializerSlice | null =
selection === null
? null
: {
file_offset: selection.offset,
size: selection.byte_length,
cpu_type: selection.cpu_type,
cpu_subtype: selection.cpu_subtype,
architecture: architectureName(selection.cpu_type),
};
return readMachoInitializers(
{ bytes: selected.bytes, fileBase: selected.fileOffset, slice },
page,
signal,
structureBytes,
);
};
const architectureName = (cpuType: number): string => {
switch (cpuType >>> 0) {
case 7:
return "x86";
case 0x01000007:
return "x86_64";
case 12:
return "arm";
case 0x0100000c:
return "arm64";
default:
return `cpu-0x${(cpuType >>> 0).toString(16)}`;
}
};
const nativeInitializersFailure = (
cause: unknown,
input: InspectNativeInitializersInput,
snapshot?: StableArtifactSnapshot,
): AnalysisError => {
const facts = artifactFilePrimaryFacts(cause);
const primary = classifyProviderFailure(
facts.cause,
offlineArtifactFailureDialect(facts, {
providerId: NATIVE_INITIALIZERS_PROVIDER.id,
operation: OPERATION,
path: input.path,
description: "image",
recognise: (failure, origin) => {
if (
failure instanceof ElfPointerRelocationFailure &&
isParseLimit(failure, ELF_POINTER_RELOCATION_FORMAT)
)
return parseLimitConstraint(OPERATION, "capacity", failure, {
path: input.path,
...fixedAllowanceOptions(
"The native initializer structural allowance",
"parser",
{
limit: failure.failure.limit.maximum,
...(failure.unit === undefined ? {} : { unit: failure.unit }),
},
),
});
if (isNativeStructureFailure(failure))
return nativeStructureConstraint(OPERATION, failure, input.path);
if (
origin === "owned-or-classified" &&
failure instanceof ArtifactReaderFailure &&
failure.reason === "limit"
)
return new AnalysisResourceConstraintError(
OPERATION,
"file-size",
failure.message,
{ max_file_bytes: input.max_file_bytes },
{
cause: failure,
retryable: false,
remediationAction: imageFileSizeRemediation(
input.max_file_bytes,
MAX_INSPECT_NATIVE_INITIALIZERS_FILE_BYTES,
),
},
);
return undefined;
},
}),
);
return projectStableArtifactFailure(
cause,
primary,
input.path,
NATIVE_INITIALIZERS_PROVIDER.id,
OPERATION,
snapshot,
);
};