Files
reverse-engineering-anything/src/native/pe/PeLoadConfigProvider.ts
T
morluto f060967789 feat(native): carry the value of each fixed structural decoding allowance (#11651)
* feat(native): carry the value of each fixed structural decoding allowance

BinaryResourceFailure and relocation-index refusals pass their maximum as limit and unit; the PE certificate decoder allowances drop the value from their names and pass it with a per-payload unit; the ELF note parser names its text ceiling with the enforced value. Kernel-module and unwind providers no longer claim a fixed text or structural allowance for a memory limit that names no ceiling (no parser produces one).

* fix(native): report a binary resource ceiling in the unit its producer counts

BinaryResourceFailure carries an optional unit; the ELF hardening entry budget says entries and the structural byte budgets say bytes. binaryResourceCeiling reports the unit only when the producer named it, so an entry count is no longer labelled as bytes. The ELF note test takes its expected ceiling from the fixture length instead of the message.
2026-11-15 21:25:13 -05:00

121 lines
3.7 KiB
TypeScript

import { projectPeBinaryFailure } from "./PeBinaryFailure.js";
import {
projectArtifactCloseFailure,
imageFileSizeRemediation,
type StableArtifactSnapshot,
readStableArtifact,
assertArtifactReadActive,
ArtifactResourceScope,
} from "../../resources/index.js";
import type { PeLoadConfigPort } from "../../ports/binaryDiagnostics/PeLoadConfigPort.js";
import type { ExecutionOptions } from "../../ports/AnalysisProvider.js";
import type { AnalysisError } from "../../domain/analysisErrorBase.js";
import type { ResourceConstraintRecovery } from "../../domain/analysisErrorCore.js";
import {
fixedAllowanceOptions,
type FixedCeilingFacts,
} from "../../domain/fixedAllowanceRemediation.js";
import {
MAX_INSPECT_PE_LOAD_CONFIG_FILE_BYTES,
MAX_INSPECT_PE_LOAD_CONFIG_CALLBACKS,
type InspectPeLoadConfigInput,
} from "../../domain/native/peLoadConfig.js";
import { err, ok } from "../../domain/result.js";
import { PE_LOAD_CONFIG_PROVIDER } from "../../domain/investigationProviders.js";
import {
parsePeLoadConfig,
PE_LOAD_CONFIG_LIMITS,
type PeLoadConfigLimit,
} from "./PeLoadConfigParser.js";
const OPERATION = "inspect_pe_load_config";
/** Portable TLS/load-config reader over one bounded, identity-checked regular file. */
export class PeLoadConfigProvider implements PeLoadConfigPort {
readonly identity = PE_LOAD_CONFIG_PROVIDER;
readonly #resources = new ArtifactResourceScope();
async close(): Promise<void> {
try {
await this.#resources.close();
} catch (cause: unknown) {
throw projectArtifactCloseFailure(
cause,
PE_LOAD_CONFIG_PROVIDER.id,
OPERATION,
);
}
}
async inspect(input: InspectPeLoadConfigInput, 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 parsePeLoadConfig(
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(peLoadConfigFailure(cause, input, snapshot));
}
}
}
const limitRecovery = (
limit: PeLoadConfigLimit | null,
input: InspectPeLoadConfigInput,
ceiling: FixedCeilingFacts,
): ResourceConstraintRecovery =>
limit === "callbacks"
? {
retryable: false,
remediationAction:
input.max_callbacks >= MAX_INSPECT_PE_LOAD_CONFIG_CALLBACKS
? `The image exceeds the ${String(MAX_INSPECT_PE_LOAD_CONFIG_CALLBACKS)}-callback ceiling; select a smaller artifact.`
: `Raise max_callbacks above ${String(input.max_callbacks)} or select a smaller artifact.`,
}
: fixedAllowanceOptions(
"The PE structural decoding allowance",
"parser",
ceiling,
);
const peLoadConfigFailure = (
cause: unknown,
input: InspectPeLoadConfigInput,
snapshot?: StableArtifactSnapshot,
): AnalysisError =>
projectPeBinaryFailure<PeLoadConfigLimit>(
cause,
input,
{
operation: OPERATION,
providerId: PE_LOAD_CONFIG_PROVIDER.id,
fileSizeRemediation: () =>
imageFileSizeRemediation(
input.max_file_bytes,
MAX_INSPECT_PE_LOAD_CONFIG_FILE_BYTES,
),
limits: PE_LOAD_CONFIG_LIMITS,
limitRecovery: (limit, ceiling) => limitRecovery(limit, input, ceiling),
},
snapshot,
);