diff --git a/src/domain/binaryContainer.ts b/src/domain/binaryContainer.ts index fd2d78ccf1..66be48c997 100644 --- a/src/domain/binaryContainer.ts +++ b/src/domain/binaryContainer.ts @@ -1,3 +1,4 @@ +import type { FixedCeilingFacts } from "./fixedAllowanceRemediation.js"; import { resolveMappedFileOffset, type FileMapping, @@ -56,11 +57,17 @@ export class BinaryMappingFailure extends BinaryFormatFailure { * `LimitRef`; it defaults to the boundary when a decoder has only one. */ export class BinaryResourceFailure extends ParseFailureError<"limit"> { + /** + * @param maximum_bytes The exceeded maximum, in `unit`. Despite its name, + * some producers count entries or values; `unit` says which, and is + * undefined when the producer has not named it. + */ constructor( readonly boundary: "build-info" | "structure" | "search", readonly maximum_bytes: number, message: string, allowance: string = boundary, + readonly unit?: "bytes" | "entries", ) { super({ kind: "limit", @@ -72,6 +79,14 @@ export class BinaryResourceFailure extends ParseFailureError<"limit"> { } } +/** The exceeded maximum as fixed-ceiling facts; the unit only when its producer named it. */ +export const binaryResourceCeiling = ( + failure: BinaryResourceFailure, +): FixedCeilingFacts => ({ + limit: failure.maximum_bytes, + ...(failure.unit === undefined ? {} : { unit: failure.unit }), +}); + /** The failure's allowance when it is one of `allowances`, else `null`. */ export const binaryResourceAllowance = ( failure: BinaryResourceFailure, @@ -309,6 +324,8 @@ export class BinaryReader { "structure", 16 * 1024 * 1024, `${label} exceeds the 16 MiB structural decoding budget`, + "structure", + "bytes", ); return this.fileRange(offset, size, label).offset; } diff --git a/src/domain/native/nativeSectionDirectory.ts b/src/domain/native/nativeSectionDirectory.ts index 2a60f3bd7f..73fdd331fa 100644 --- a/src/domain/native/nativeSectionDirectory.ts +++ b/src/domain/native/nativeSectionDirectory.ts @@ -121,6 +121,8 @@ const request = ( "structure", STRUCTURE_BYTES, "Native directory exceeds the 16777216-byte (16 MiB) structural decoding budget. This release has no runtime override.", + "structure", + "bytes", ); return { offset: exact(offset), size: exact(size) }; }; @@ -237,6 +239,8 @@ export function* readElfSectionDirectory( "structure", STRUCTURE_BYTES, `Decoded ELF section names require ${String(decoded.requiredBytes)} bytes, above the ${String(decoded.maximumBytes)}-byte remaining structural budget`, + "structure", + "bytes", ); if (decoded.state === "unsupported") section_names_limitation = `ELF section names are unavailable because compression type ${String(decoded.compressionType)} is unsupported`; @@ -272,6 +276,8 @@ export function* readElfSectionDirectory( "structure", STRUCTURE_BYTES, "Decoded ELF section names exceed the 16 MiB structural budget", + "structure", + "bytes", ); decoded = { name: elfStringTableText(observed), diff --git a/src/go/GoBinaryProvider.ts b/src/go/GoBinaryProvider.ts index eea7515ed4..6d5fe68946 100644 --- a/src/go/GoBinaryProvider.ts +++ b/src/go/GoBinaryProvider.ts @@ -38,7 +38,10 @@ import { fixedAllowanceOptions } from "../domain/fixedAllowanceRemediation.js"; import { err, ok, type Result } from "../domain/result.js"; import { readGoBinaryImage, type GoBinaryImage } from "./GoBinaryImage.js"; import { requireSelectedNativeImage } from "../domain/apple/machoSliceSelection.js"; -import { BinaryResourceFailure } from "../domain/binaryContainer.js"; +import { + BinaryResourceFailure, + binaryResourceCeiling, +} from "../domain/binaryContainer.js"; import { errorMessage } from "../kernel/errorMessage.js"; const OPERATION = "inspect_go_binary"; @@ -107,7 +110,7 @@ const failureForPrimary = ( ? fixedAllowanceOptions( "The Go structural decoding allowance", "parser", - { limit: cause.maximum_bytes, unit: "bytes" }, + binaryResourceCeiling(cause), ) : { retryable: false, diff --git a/src/native/elf/ElfNoteParser.ts b/src/native/elf/ElfNoteParser.ts index 4a0a1ad6d8..ebf3096af3 100644 --- a/src/native/elf/ElfNoteParser.ts +++ b/src/native/elf/ElfNoteParser.ts @@ -1,3 +1,4 @@ +import type { FixedAllowance } from "../../domain/fixedAllowanceRemediation.js"; import { observeElfStringTableEntry, elfStringTableText, @@ -43,6 +44,17 @@ const HEX_DESCRIPTOR_LIMIT = 64; const isLinuxCoreProfile = (header: ElfFixedHeader): boolean => (header.osAbi === 0 || header.osAbi === 3) && header.abiVersion === 0; +/** + * The note text, hash, and scan ceiling. It equals the admitted image length + * plus decoded section names, so host memory cannot raise it. + */ +const noteTextAllowance = (budget: TextBudget): FixedAllowance => ({ + name: "The ELF note text, hash, and scan allowance (derived from the image length and section names)", + kind: "parser", + limit: budget.limit, + unit: "bytes", +}); + interface TextBudget { limit: number; used: number; @@ -113,7 +125,7 @@ const chargeText = ( throw new ArtifactReaderFailure( "limit", `ELF note text exceeds the ${String(budget.limit)}-byte budget.`, - { boundary: "memory" }, + { boundary: "memory", fixedAllowance: noteTextAllowance(budget) }, ); budget[field] += bytes; }; @@ -453,7 +465,7 @@ const decodeCoreFile = ( throw new ArtifactReaderFailure( "limit", `ELF note text exceeds the ${String(text.limit)}-byte budget.`, - { boundary: "memory" }, + { boundary: "memory", fixedAllowance: noteTextAllowance(text) }, ); } diff --git a/src/native/elf/ElfNotesProvider.test.ts b/src/native/elf/ElfNotesProvider.test.ts index e810250b0e..0d7a459e72 100644 --- a/src/native/elf/ElfNotesProvider.test.ts +++ b/src/native/elf/ElfNotesProvider.test.ts @@ -8,6 +8,7 @@ import { AnalysisUnsupportedTargetError, } from "../../domain/analysisErrorCore.js"; import { projectAnalysisError } from "../../domain/analysisErrorProjection.js"; +import { analysisRefusalRemediation } from "../../domain/analysisRefusal.js"; import { elfNotesSchema } from "../../domain/native/elfNotes.js"; import { createTestTempDirectory } from "../../../tests/fixtures/temporaryDirectory.js"; import { @@ -85,13 +86,11 @@ describe("ELF note provider failures", () => { it("names the note text budget instead of host memory", async () => { const root = await createTestTempDirectory("rea-elf-note-text-budget-"); const path = join(root, "program"); - await writeFile( - path, - elfNoteFixture({ - ownership: "program", - notes: elfNote("GNU", 3, Buffer.alloc(4096, 1)), - }), - ); + const bytes = elfNoteFixture({ + ownership: "program", + notes: elfNote("GNU", 3, Buffer.alloc(4096, 1)), + }); + await writeFile(path, bytes); const result = await inspect(path); if (result.ok) throw new Error("Expected a text budget refusal"); const error = result.error as AnalysisResourceConstraintError; @@ -99,12 +98,22 @@ describe("ELF note provider failures", () => { expect(error.message).toMatch(/byte budget/u); const projected = projectAnalysisError(error); expect(projected.retryable).toBe(false); - expect(projected.remediation.action).toContain( - "ELF note text, hash, and scan budget", + // The ceiling is the image length plus decoded section names; this + // program-only fixture has no section names. + expect(projected.details?.refusal).toMatchObject({ + kind: "fixed_ceiling", + allowance_kind: "parser", + limit: bytes.length, + unit: "bytes", + }); + expect(error.fixedAllowance).toBeDefined(); + expect(projected.remediation.action).toBe( + analysisRefusalRemediation({ + kind: "fixed_ceiling", + operation: "inspect_elf_notes", + ceiling: error.fixedAllowance!, + }), ); - expect(projected.remediation.action).toMatch(/admitted image length/u); - expect(projected.remediation.action).toMatch(/do not raise it/u); - expect(projected.remediation.action).not.toMatch(/available host memory/u); }); it("keeps a caller file-size budget distinct from host memory", async () => { diff --git a/src/native/elf/ElfNotesProvider.ts b/src/native/elf/ElfNotesProvider.ts index 100765311e..053cacaeb3 100644 --- a/src/native/elf/ElfNotesProvider.ts +++ b/src/native/elf/ElfNotesProvider.ts @@ -74,9 +74,6 @@ export class ElfNotesProvider implements ElfNotesPort { } } -const NOTE_TEXT_BUDGET = - "The ELF note text, hash, and scan budget fixed by the admitted image length plus decoded section names"; - const inputBudgetRemediation = ( boundary: "file-size" | "capacity", input: InspectElfNotesInput, @@ -130,9 +127,8 @@ const elfNotesFailurePrimary = ( cause instanceof ArtifactReaderFailure ) { if (cause.reason === "limit") { - // Caller-selected read and entry budgets name the bound to raise. - // The note text, hash, and scan ceiling equals the admitted image - // length plus decoded section names, so host memory cannot raise it. + // Caller-selected read and entry budgets name the bound to raise; a + // memory limit keeps the ceiling its parser named (note text). const noteTextBudget = cause.boundary === "memory"; return new AnalysisResourceConstraintError( OPERATION, @@ -147,7 +143,12 @@ const elfNotesFailurePrimary = ( ...(cause.boundary === undefined ? {} : noteTextBudget - ? fixedAllowanceOptions(NOTE_TEXT_BUDGET, "parser") + ? cause.fixedAllowance === undefined + ? {} + : { + retryable: false as const, + fixedAllowance: cause.fixedAllowance, + } : { retryable: false as const, remediationAction: inputBudgetRemediation( diff --git a/src/native/elf/ElfUnwindRangesProvider.ts b/src/native/elf/ElfUnwindRangesProvider.ts index bcf8330621..0b7ac82929 100644 --- a/src/native/elf/ElfUnwindRangesProvider.ts +++ b/src/native/elf/ElfUnwindRangesProvider.ts @@ -23,7 +23,10 @@ import { } from "../../domain/native/elfUnwindRanges.js"; import { err, ok } from "../../domain/result.js"; import { ELF_UNWIND_RANGES_PROVIDER } from "../../domain/investigationProviders.js"; -import { BinaryResourceFailure } from "../../domain/binaryContainer.js"; +import { + BinaryResourceFailure, + binaryResourceCeiling, +} from "../../domain/binaryContainer.js"; import { ElfUnwindSymbolTextFailure, parseElfUnwindRanges, @@ -119,8 +122,9 @@ const elfUnwindFailurePrimary = ( { cause, ...fixedAllowanceOptions( - "The 16 MiB ELF structural decoding budget", + "The ELF structural decoding allowance", "parser", + binaryResourceCeiling(cause), ), }, ); @@ -151,10 +155,12 @@ const elfUnwindFailurePrimary = ( ...(cause.boundary === undefined ? {} : structural - ? fixedAllowanceOptions( - "The ELF unwind structural decoding budget", - "parser", - ) + ? cause.fixedAllowance === undefined + ? {} + : { + retryable: false as const, + fixedAllowance: cause.fixedAllowance, + } : { retryable: false as const, remediationAction: inputBudgetRemediation( diff --git a/src/native/elf/KernelModuleProvider.ts b/src/native/elf/KernelModuleProvider.ts index d6df3e4149..208769e7cf 100644 --- a/src/native/elf/KernelModuleProvider.ts +++ b/src/native/elf/KernelModuleProvider.ts @@ -24,7 +24,10 @@ import { } from "../../domain/native/kernelModule.js"; import { err, ok } from "../../domain/result.js"; import { KERNEL_MODULE_PROVIDER } from "../../domain/investigationProviders.js"; -import { BinaryResourceFailure } from "../../domain/binaryContainer.js"; +import { + BinaryResourceFailure, + binaryResourceCeiling, +} from "../../domain/binaryContainer.js"; import { parseKernelModule } from "./KernelModuleParser.js"; const OPERATION = "inspect_kernel_module"; @@ -75,9 +78,6 @@ export class KernelModuleProvider implements KernelModulePort { } } -const TEXT_BUDGET = - "The kernel-module entry text and hash budget fixed by the admitted image length"; - const inputBudgetRemediation = ( boundary: "file-size" | "capacity", input: InspectKernelModuleInput, @@ -106,6 +106,7 @@ const kernelModuleFailurePrimary = ( ...fixedAllowanceOptions( "The ELF structural decoding allowance", "parser", + binaryResourceCeiling(cause), ), }, ); @@ -122,9 +123,8 @@ const kernelModuleFailurePrimary = ( cause instanceof ArtifactReaderFailure ) { if (cause.reason === "limit") { - // Caller-selected read and entry budgets name the bound to raise. - // Entry text and hashes are bounded by the admitted image, so host - // memory cannot raise that ceiling. + // Caller-selected read and entry budgets name the bound to raise; a + // memory limit keeps only a ceiling its reader named. const textBudget = cause.boundary === "memory"; return new AnalysisResourceConstraintError( OPERATION, @@ -139,7 +139,12 @@ const kernelModuleFailurePrimary = ( ...(cause.boundary === undefined ? {} : textBudget - ? fixedAllowanceOptions(TEXT_BUDGET, "parser") + ? cause.fixedAllowance === undefined + ? {} + : { + retryable: false as const, + fixedAllowance: cause.fixedAllowance, + } : { retryable: false as const, remediationAction: inputBudgetRemediation( diff --git a/src/native/exports/NativeExportsProvider.ts b/src/native/exports/NativeExportsProvider.ts index 2cc1180216..e3121771f6 100644 --- a/src/native/exports/NativeExportsProvider.ts +++ b/src/native/exports/NativeExportsProvider.ts @@ -29,6 +29,7 @@ import { err, ok } from "../../domain/result.js"; import { NATIVE_EXPORTS_PROVIDER } from "../../domain/investigationProviders.js"; import { BinaryResourceFailure, + binaryResourceCeiling, binaryResourceAllowance, } from "../../domain/binaryContainer.js"; import { @@ -86,8 +87,9 @@ export class NativeExportsProvider implements NativeExportsPort { /** Recovery for one exhausted allowance, chosen from the parser's structured tag. */ const limitRecovery = ( limit: NativeExportLimit | null, - maximum: number, + cause: BinaryResourceFailure, ): ResourceConstraintRecovery => { + const maximum = cause.maximum_bytes; switch (limit) { case "rows": return fixedAllowanceOptions( @@ -105,6 +107,7 @@ const limitRecovery = ( return fixedAllowanceOptions( "The native export structural decoding allowance", "parser", + binaryResourceCeiling(cause), ); } }; @@ -132,7 +135,7 @@ const nativeExportsFailurePrimary = ( cause, ...limitRecovery( binaryResourceAllowance(cause, NATIVE_EXPORT_LIMITS), - cause.maximum_bytes, + cause, ), }, ); diff --git a/src/native/hardening/BinaryHardeningParsers.test.ts b/src/native/hardening/BinaryHardeningParsers.test.ts index b9bf545d63..f44b6c530d 100644 --- a/src/native/hardening/BinaryHardeningParsers.test.ts +++ b/src/native/hardening/BinaryHardeningParsers.test.ts @@ -180,6 +180,21 @@ it("rejects truncated, duplicate and over-budget ELF tables", () => { expect(() => readElfHardening(elfHardeningFixture({ duplicateStack: true })), ).toThrow(expect.objectContaining({ kind: "malformed" })); + // A file-backed PT_DYNAMIC of 65,537 entries exceeds the entry allowance, + // which counts entries, not bytes. + const fixture = elfHardeningFixture(); + const wide = Buffer.concat([fixture, Buffer.alloc(65_537 * 16)]); + const programs = Number(wide.readBigUInt64LE(32)); + const dynamic = Array.from( + { length: wide.readUInt16LE(56) }, + (_, index) => programs + index * wide.readUInt16LE(54), + ).find((at) => wide.readUInt32LE(at) === 2); + if (dynamic === undefined) throw new Error("Fixture has no PT_DYNAMIC"); + wide.writeBigUInt64LE(BigInt(fixture.length), dynamic + 8); + wide.writeBigUInt64LE(65_537n * 16n, dynamic + 32); + expect(() => readElfHardening(wide)).toThrow( + expect.objectContaining({ maximum_bytes: 65_536, unit: "entries" }), + ); const overBudget = readElfHardening( elfHardeningFixture({ symbolBudget: true }), ); diff --git a/src/native/hardening/BinaryHardeningProvider.ts b/src/native/hardening/BinaryHardeningProvider.ts index 33769c8d4f..2bc3967de6 100644 --- a/src/native/hardening/BinaryHardeningProvider.ts +++ b/src/native/hardening/BinaryHardeningProvider.ts @@ -28,7 +28,10 @@ import { import { ProviderAdapterError } from "../../domain/providerAdapterError.js"; import { err, ok } from "../../domain/result.js"; import { BINARY_HARDENING_PROVIDER } from "../../domain/investigationProviders.js"; -import { BinaryResourceFailure } from "../../domain/binaryContainer.js"; +import { + BinaryResourceFailure, + binaryResourceCeiling, +} from "../../domain/binaryContainer.js"; import { readElfHardening } from "./ElfHardening.js"; import { readMachoHardening } from "./MachoHardening.js"; import { readPeHardening } from "./PeHardening.js"; @@ -145,6 +148,7 @@ const binaryHardeningFailurePrimary = ( ...fixedAllowanceOptions( "The binary hardening structural allowance", "parser", + binaryResourceCeiling(cause), ), }, ); diff --git a/src/native/hardening/ElfHardening.ts b/src/native/hardening/ElfHardening.ts index b7291ab085..6871ca7a55 100644 --- a/src/native/hardening/ElfHardening.ts +++ b/src/native/hardening/ElfHardening.ts @@ -124,6 +124,8 @@ const budgetFailure = (count: number, label: string): BinaryResourceFailure => "structure", MAX_BINARY_HARDENING_TABLE_ENTRIES, `${label} has ${String(count)} entries, above the ${String(MAX_BINARY_HARDENING_TABLE_ENTRIES)}-entry structural budget. This release has no runtime override.`, + "structure", + "entries", ); const budget = (count: number, label: string): void => { diff --git a/src/native/initializers/ElfInitializers.ts b/src/native/initializers/ElfInitializers.ts index 5ab5927f58..c7adbeb8fa 100644 --- a/src/native/initializers/ElfInitializers.ts +++ b/src/native/initializers/ElfInitializers.ts @@ -158,6 +158,8 @@ const readDynamicTags = ( "structure", 16 * 1024 * 1024, "ELF dynamic directory exceeds the 16 MiB structural allowance", + "structure", + "bytes", ); if ( size < 0 || diff --git a/src/native/initializers/NativeInitializersProvider.ts b/src/native/initializers/NativeInitializersProvider.ts index d958d5860b..90becd3aaf 100644 --- a/src/native/initializers/NativeInitializersProvider.ts +++ b/src/native/initializers/NativeInitializersProvider.ts @@ -31,6 +31,7 @@ import { NATIVE_INITIALIZERS_PROVIDER } from "../../domain/investigationProvider import { BinaryFormatFailure, BinaryResourceFailure, + binaryResourceCeiling, } from "../../domain/binaryContainer.js"; import { ElfPointerRelocationFailure } from "../../domain/native/elfPointerRelocations.js"; import { requireSelectedNativeImage } from "../../domain/apple/machoSliceSelection.js"; @@ -210,6 +211,10 @@ const failurePrimary = ( ...fixedAllowanceOptions( "The native initializer structural allowance", "parser", + { + limit: cause.failure.limit.maximum, + ...(cause.unit === undefined ? {} : { unit: cause.unit }), + }, ), }, ); @@ -224,6 +229,7 @@ const failurePrimary = ( ...fixedAllowanceOptions( "The native initializer structural allowance", "parser", + binaryResourceCeiling(cause), ), }, ); diff --git a/src/native/pe/PeBinaryFailure.ts b/src/native/pe/PeBinaryFailure.ts index edf929bdac..bae3f8597c 100644 --- a/src/native/pe/PeBinaryFailure.ts +++ b/src/native/pe/PeBinaryFailure.ts @@ -1,3 +1,4 @@ +import type { FixedCeilingFacts } from "../../domain/fixedAllowanceRemediation.js"; import { parseFailureAnalysisError } from "../../domain/parseFailureAnalysisError.js"; import { projectSelectedArtifactFileFailure, @@ -20,6 +21,7 @@ import { import { ProviderAdapterError } from "../../domain/providerAdapterError.js"; import { BinaryResourceFailure, + binaryResourceCeiling, binaryResourceAllowance, } from "../../domain/binaryContainer.js"; import { errorMessage } from "../../kernel/errorMessage.js"; @@ -34,7 +36,11 @@ interface PeBinaryFailureOptions { readonly fileSizeRemediation: () => string; /** The allowances the parser names in its `BinaryResourceFailure`. */ readonly limits: readonly Limit[]; - readonly limitRecovery: (limit: Limit | null) => ResourceConstraintRecovery; + /** Recovery for the named allowance, or the structural allowance (`null`) at `ceiling`. */ + readonly limitRecovery: ( + limit: Limit | null, + ceiling: FixedCeilingFacts, + ) => ResourceConstraintRecovery; } const peBinaryFailurePrimary = ( @@ -68,7 +74,7 @@ const peBinaryFailurePrimary = ( "capacity", `Selected artifact ${input.path}: ${cause.message}`, { boundary: cause.boundary, maximum: cause.maximum_bytes, limit }, - { cause, ...options.limitRecovery(limit) }, + { cause, ...options.limitRecovery(limit, binaryResourceCeiling(cause)) }, ); } const formatFailure = parseFailureAnalysisError(operation, input.path, cause); diff --git a/src/native/pe/PeCertificatesProvider.test.ts b/src/native/pe/PeCertificatesProvider.test.ts index 385777f943..1b7f245fbb 100644 --- a/src/native/pe/PeCertificatesProvider.test.ts +++ b/src/native/pe/PeCertificatesProvider.test.ts @@ -52,23 +52,23 @@ it.each([ name: "depth", payload: () => encapsulated(40), limits: { max_asn1_depth: 32 }, - description: "32 nesting levels", + ceiling: { limit: 32, unit: "nesting levels per PKCS#7 payload" }, }, { name: "nodes", payload: () => repeatedCertificateSignedData(600), limits: { max_asn1_values: 16_384 }, - description: "16384 values", + ceiling: { limit: 16_384, unit: "values per PKCS#7 payload" }, }, { name: "text", payload: () => repeatedCertificateSignedData(350), limits: { max_decoded_text_bytes: 65_536 }, - description: "65536 bytes", + ceiling: { limit: 65_536, unit: "bytes per PKCS#7 payload" }, }, ])( "reports the fixed $name allowance with its own resource and recovery", - async ({ payload, limits, description }) => { + async ({ payload, limits, ceiling }) => { const result = await inspect(payload()); expect(result.ok).toBe(false); if (result.ok) throw new Error("Expected decoder refusal"); @@ -77,8 +77,10 @@ it.each([ throw result.error; expect(result.error.reportedLimits).toEqual(limits); expect(result.error.retryable).toBe(false); - expect(result.error.fixedAllowance?.name).toContain(description); - expect(result.error.fixedAllowance?.name).toContain("no runtime override"); + expect(result.error.fixedAllowance).toMatchObject({ + kind: "parser", + ...ceiling, + }); }, ); @@ -95,15 +97,17 @@ it.each([ name: "bytes", content: Buffer.from([0x2a, ...Array(256).fill(0)]), limits: { max_oid_bytes: 256 }, + ceiling: { limit: 256, unit: "bytes per OID" }, }, { name: "arc bytes", content: Buffer.from([0x2a, 0x81, ...Array(31).fill(0x80), 0]), limits: { max_oid_arc_bytes: 32 }, + ceiling: { limit: 32, unit: "bytes per base-128 arc" }, }, ])( "preserves the canonical OID $name allowance in a valid CMS algorithm", - async ({ content, limits }) => { + async ({ content, limits, ceiling }) => { const algorithm = tlv(0x06, content); const result = await inspect( Buffer.from( @@ -127,6 +131,6 @@ it.each([ expect(result.error.reportedLimits).toEqual(limits); expect(result.error.retryable).toBe(false); expect(result.error.fixedAllowance?.name).toContain("OBJECT IDENTIFIER"); - expect(result.error.fixedAllowance?.name).toContain("no runtime override"); + expect(result.error.fixedAllowance).toMatchObject(ceiling); }, ); diff --git a/src/native/pe/PeCertificatesProvider.ts b/src/native/pe/PeCertificatesProvider.ts index 4c5cb74e91..7b82777834 100644 --- a/src/native/pe/PeCertificatesProvider.ts +++ b/src/native/pe/PeCertificatesProvider.ts @@ -102,23 +102,28 @@ const decoderAllowanceFailure = ( const selected = { "asn1-nodes": { limits: { max_asn1_values: limit }, - description: `The PE certificate ASN.1 allowance of ${String(limit)} values per PKCS#7 payload (this release has no runtime override)`, + name: "The PE certificate ASN.1 value allowance", + unit: "values per PKCS#7 payload", }, "asn1-depth": { limits: { max_asn1_depth: limit }, - description: `The PE certificate ASN.1 allowance of ${String(limit)} nesting levels per PKCS#7 payload (this release has no runtime override)`, + name: "The PE certificate ASN.1 nesting allowance", + unit: "nesting levels per PKCS#7 payload", }, "decoded-text": { limits: { max_decoded_text_bytes: limit }, - description: `The PE certificate decoded-text allowance of ${String(limit)} bytes per PKCS#7 payload (this release has no runtime override)`, + name: "The PE certificate decoded-text allowance", + unit: "bytes per PKCS#7 payload", }, "oid-bytes": { limits: { max_oid_bytes: limit }, - description: `The PE certificate OBJECT IDENTIFIER allowance of ${String(limit)} bytes per OID (this release has no runtime override)`, + name: "The PE certificate OBJECT IDENTIFIER allowance", + unit: "bytes per OID", }, "oid-arc-bytes": { limits: { max_oid_arc_bytes: limit }, - description: `The PE certificate OBJECT IDENTIFIER allowance of ${String(limit)} bytes per base-128 arc (this release has no runtime override)`, + name: "The PE certificate OBJECT IDENTIFIER arc allowance", + unit: "bytes per base-128 arc", }, }[allowance]; return new AnalysisResourceConstraintError( @@ -128,7 +133,10 @@ const decoderAllowanceFailure = ( selected.limits, { cause, - ...fixedAllowanceOptions(selected.description, "parser"), + ...fixedAllowanceOptions(selected.name, "parser", { + limit, + unit: selected.unit, + }), }, ); }; diff --git a/src/native/pe/PeImportsProvider.ts b/src/native/pe/PeImportsProvider.ts index 08d2d07d67..ca3297fd72 100644 --- a/src/native/pe/PeImportsProvider.ts +++ b/src/native/pe/PeImportsProvider.ts @@ -11,7 +11,10 @@ import type { PeImportsPort } from "../../ports/binaryDiagnostics/PeImportsPort. import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; import type { ResourceConstraintRecovery } from "../../domain/analysisErrorCore.js"; -import { fixedAllowanceOptions } from "../../domain/fixedAllowanceRemediation.js"; +import { + fixedAllowanceOptions, + type FixedCeilingFacts, +} from "../../domain/fixedAllowanceRemediation.js"; import { MAX_INSPECT_PE_IMPORT_DLLS, MAX_INSPECT_PE_IMPORT_FILE_BYTES, @@ -91,6 +94,7 @@ const functionRemediation = (input: InspectPeImportsInput): string => const limitRecovery = ( limit: PeImportLimit | null, input: InspectPeImportsInput, + ceiling: FixedCeilingFacts, ): ResourceConstraintRecovery => { switch (limit) { case "dlls": @@ -110,6 +114,7 @@ const limitRecovery = ( return fixedAllowanceOptions( "The PE structural decoding allowance", "parser", + ceiling, ); } }; @@ -131,7 +136,7 @@ const peImportsFailure = ( MAX_INSPECT_PE_IMPORT_FILE_BYTES, ), limits: PE_IMPORT_LIMITS, - limitRecovery: (limit) => limitRecovery(limit, input), + limitRecovery: (limit, ceiling) => limitRecovery(limit, input, ceiling), }, snapshot, ); diff --git a/src/native/pe/PeLoadConfigProvider.ts b/src/native/pe/PeLoadConfigProvider.ts index e07eb441c7..ca14fcd43b 100644 --- a/src/native/pe/PeLoadConfigProvider.ts +++ b/src/native/pe/PeLoadConfigProvider.ts @@ -11,7 +11,10 @@ import type { PeLoadConfigPort } from "../../ports/binaryDiagnostics/PeLoadConfi import type { ExecutionOptions } from "../../ports/AnalysisProvider.js"; import type { AnalysisError } from "../../domain/analysisErrorBase.js"; import type { ResourceConstraintRecovery } from "../../domain/analysisErrorCore.js"; -import { fixedAllowanceOptions } from "../../domain/fixedAllowanceRemediation.js"; +import { + fixedAllowanceOptions, + type FixedCeilingFacts, +} from "../../domain/fixedAllowanceRemediation.js"; import { MAX_INSPECT_PE_LOAD_CONFIG_FILE_BYTES, MAX_INSPECT_PE_LOAD_CONFIG_CALLBACKS, @@ -78,6 +81,7 @@ export class PeLoadConfigProvider implements PeLoadConfigPort { const limitRecovery = ( limit: PeLoadConfigLimit | null, input: InspectPeLoadConfigInput, + ceiling: FixedCeilingFacts, ): ResourceConstraintRecovery => limit === "callbacks" ? { @@ -87,7 +91,11 @@ const limitRecovery = ( ? `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"); + : fixedAllowanceOptions( + "The PE structural decoding allowance", + "parser", + ceiling, + ); const peLoadConfigFailure = ( cause: unknown, @@ -106,7 +114,7 @@ const peLoadConfigFailure = ( MAX_INSPECT_PE_LOAD_CONFIG_FILE_BYTES, ), limits: PE_LOAD_CONFIG_LIMITS, - limitRecovery: (limit) => limitRecovery(limit, input), + limitRecovery: (limit, ceiling) => limitRecovery(limit, input, ceiling), }, snapshot, ); diff --git a/tests/boundary/filesystem/binaryHardening.test.ts b/tests/boundary/filesystem/binaryHardening.test.ts index dd88a7d6b1..ea1ce36ff8 100644 --- a/tests/boundary/filesystem/binaryHardening.test.ts +++ b/tests/boundary/filesystem/binaryHardening.test.ts @@ -69,4 +69,32 @@ describe("binary hardening stable filesystem boundary", () => { await service.close(); } }); + + it("reports the dynamic-table allowance in entries, not bytes", async () => { + const root = await createTestTempDirectory("rea-binary-hardening-entries-"); + const path = join(root, "app"); + // A file-backed PT_DYNAMIC of 65,537 entries (16 bytes each in ELF64). + const fixture = elfHardeningFixture(); + const bytes = Buffer.concat([fixture, Buffer.alloc(65_537 * 16)]); + const programs = Number(bytes.readBigUInt64LE(32)); + const dynamic = Array.from( + { length: bytes.readUInt16LE(56) }, + (_, index) => programs + index * bytes.readUInt16LE(54), + ).find((at) => bytes.readUInt32LE(at) === 2); + if (dynamic === undefined) throw new Error("Fixture has no PT_DYNAMIC"); + bytes.writeBigUInt64LE(BigInt(fixture.length), dynamic + 8); + bytes.writeBigUInt64LE(65_537n * 16n, dynamic + 32); + await writeFile(path, bytes); + const service = createBinaryHardeningService(); + try { + const result = await service.inspect({ path }); + if (result.ok) throw new Error("Expected the entry allowance refusal"); + expect(result.error).toMatchObject({ + _tag: "AnalysisResourceConstraintError", + fixedAllowance: { limit: 65_536, unit: "entries" }, + }); + } finally { + await service.close(); + } + }); });