Files
RRFZ 2c07552b64 fix(limits): report details.limit for KernelModuleProvider refusals (#14306)
* fix(limits): report details.limit for ELF dependency, unwind, and kernel reader refusals

Attach structured `details.limit` metadata to per-call budget refusals across
the remaining `src/native/elf/` providers (`ElfDependenciesProvider`,
`KernelModuleProvider`, `KernelBuildConfigProvider`, and
`ElfUnwindRangesProvider`), following the `#14243` shared-budget pattern:

- Generalize `elfReadBudgetLimit` in `src/domain/native/elfReadBudgets.ts` so
  ELF and kernel readers share one parameter-limit builder.
- Export `ELF_DEPENDENCIES_READ_BUDGETS`, `KERNEL_MODULE_READ_BUDGETS`,
  `KERNEL_BUILD_CONFIG_READ_BUDGETS`, and `ELF_UNWIND_RANGES_READ_BUDGETS` from
  their domain schemas so Zod input bounds and projected `details.limit`
  metadata have a single owner.
- Report `details.limit` for caller-selected file, entry, inflation, and record
  budgets, including `compressed_candidate_bytes` (`max_config_bytes`), while
  moving `KernelBuildConfigProvider` file-size handling onto
  `offlineArtifactFailureDialect.fileSize` and removing its unreachable
  `capacity` branch.
- Report `ElfUnwindRangesProvider`'s fixed 16 MiB `symbol_name_text_bytes`
  allowance through `fixedAllowanceOptions` with narrowing guidance and
  `related: { parameters: ["limit", "offset"] }`, registered in
  `scripts/verify/fixed-ceiling-registry.json` under `#14235`, rather than
  reporting an inverted `details.limit`.
- Remove `ElfDependenciesProvider.ts`, `ElfUnwindRangesProvider.ts`, and
  `KernelBuildConfigProvider.ts` from
  `scripts/verify/untyped-budget-refusal-baseline.json`, clearing all
  `src/native/elf/` entries from the baseline, and update
  `docs/reference/rea-limit-inventory.md` and
  `docs/reference/limits-and-recovery.md`.

Part of #14234

* test(elf): assert CLI details.limit and flags for kernel module budgets

* fix(elf): report required_value for KernelModuleProvider max_file_bytes and add MCP recovery test
2026-11-28 22:58:31 +08:00

202 lines
7.3 KiB
TypeScript

import { createHash } from "node:crypto";
import { fileIdentity } from "../../fixtures/evidenceSubject.js";
import { symlink, writeFile } from "node:fs/promises";
import { join } from "node:path";
import { describe, expect, it } from "vitest";
import { createKernelModuleService } from "../../../src/composition/binaryDiagnostics/kernelModule.js";
import { projectAnalysisError } from "../../../src/domain/analysisErrorProjection.js";
import { kernelModuleSchema } from "../../../src/domain/native/kernelModule.js";
import {
kernelModuleFixture,
moduleSignatureTrailer,
} from "../../../src/native/elf/KernelModule.fixture.js";
import { createTestTempDirectory } from "../../fixtures/temporaryDirectory.js";
import { expectFileSelectionErrors } from "../../support/expectFileSelectionErrors.js";
describe("kernel module stable filesystem boundary", () => {
it("names max_structure_bytes when its section table exceeds it", async () => {
const root = await createTestTempDirectory("rea-kernel-module-structure-");
const path = join(root, "demo.ko");
await writeFile(path, kernelModuleFixture());
const service = createKernelModuleService();
try {
const refused = await service.inspect({ path, max_structure_bytes: 64 });
if (refused.ok) throw new Error("Expected a max_structure_bytes refusal");
expect(refused.error).toMatchObject({
_tag: "AnalysisResourceConstraintError",
limit: {
control_kind: "parameter",
parameter: "max_structure_bytes",
effective_value: 64,
applies_at: "next_call",
},
});
const raised = await service.inspect({
path,
max_structure_bytes: 65536,
});
if (!raised.ok) throw raised.error;
} finally {
await service.close();
}
});
it("retains trailer fields but withholds an empty PKCS#7 range", async () => {
const root = await createTestTempDirectory("rea-kernel-module-empty-sig-");
const path = join(root, "demo.ko");
await writeFile(
path,
kernelModuleFixture({
signature: moduleSignatureTrailer(Buffer.alloc(0)),
}),
);
const service = createKernelModuleService();
try {
const result = await service.inspect({ path });
if (!result.ok) throw result.error;
expect(result.value.normalized_result).toMatchObject({
coverage: { status: "partial" },
modinfo: {
entries: expect.arrayContaining([
expect.objectContaining({ key: "license", value: "GPL" }),
]),
},
signature: {
status: "malformed",
reason: "signature_length_inconsistent",
magic: { bytes: 28 },
info: { sig_len: 0, id_type: 2 },
pkcs7: null,
},
});
} finally {
await service.close();
}
});
it("reads module metadata and signature beyond the retained prefix", async () => {
const root = await createTestTempDirectory("rea-kernel-module-positional-");
const path = join(root, "demo.ko");
// Enough section headers to place .modinfo and the signature beyond 4 KiB.
const pkcs7 = Buffer.from("module signature outside prefix");
const trailer = moduleSignatureTrailer(pkcs7);
const bytes = kernelModuleFixture({
sharedNameSections: { name: ".padding", count: 70 },
signature: trailer,
});
const firstSection = Number(bytes.readBigUInt64LE(40)) + 64;
const modinfoOffset = Number(bytes.readBigUInt64LE(firstSection + 24));
const versionsOffset = Number(
bytes.readBigUInt64LE(firstSection + 64 + 24),
);
expect(modinfoOffset).toBeGreaterThan(4096);
expect(versionsOffset).toBeGreaterThan(4096);
await writeFile(path, bytes);
const service = createKernelModuleService();
try {
const result = await service.inspect({ path });
if (!result.ok) throw result.error;
expect(fileIdentity(result.value.subject)).toMatchObject({
format: "elf",
architecture: "x86_64",
});
expect(result.value.confidence).toBe("observed");
const report = kernelModuleSchema.parse(result.value.normalized_result);
expect(report.modinfo?.entries[0]).toMatchObject({
key: "license",
value: "GPL",
location: { offset: modinfoOffset, bytes: 12 },
});
expect(report.versions?.location).toEqual({
offset: versionsOffset,
bytes: 128,
});
expect(report.versions?.entries[0]).toMatchObject({
crc_hex: "0000000012345678",
name: "printk",
location: { offset: versionsOffset, bytes: 64 },
});
expect(report.signature).toMatchObject({
status: "present",
pkcs7: {
location: {
offset: bytes.length - trailer.length,
bytes: pkcs7.length,
},
sha256: createHash("sha256").update(pkcs7).digest("hex"),
},
});
} finally {
await service.close();
}
});
it("reports non-file and missing selections as caller input errors", async () => {
const root = await createTestTempDirectory("rea-kernel-module-files-");
const path = join(root, "demo.ko");
const alias = join(root, "alias");
await writeFile(path, kernelModuleFixture());
await symlink(path, alias);
const service = createKernelModuleService();
try {
await expectFileSelectionErrors(service, "inspect_kernel_module", {
root,
alias,
path,
});
} finally {
await service.close();
}
});
it("returns typed resource and cancellation failures", async () => {
const root = await createTestTempDirectory("rea-kernel-module-budget-");
const path = join(root, "demo.ko");
const bytes = kernelModuleFixture();
await writeFile(path, bytes);
const service = createKernelModuleService();
try {
const tooSmall = await service.inspect({ path, max_file_bytes: 64 });
if (tooSmall.ok) throw new Error("Expected resource budget failure");
expect(tooSmall.error._tag).toBe("AnalysisResourceConstraintError");
expect(projectAnalysisError(tooSmall.error).details).toMatchObject({
limit: {
control_kind: "parameter",
parameter: "max_file_bytes",
effective_value: 64,
unit: "bytes",
minimum: 64,
maximum: 536870912,
default_value: 67108864,
applies_at: "next_call",
required_value: bytes.length,
},
});
const tooMany = await service.inspect({ path, max_entries: 1 });
if (tooMany.ok) throw new Error("Expected entry budget failure");
expect(tooMany.error._tag).toBe("AnalysisResourceConstraintError");
expect(projectAnalysisError(tooMany.error).details).toMatchObject({
limit: {
control_kind: "parameter",
parameter: "max_entries",
effective_value: 1,
unit: "count",
minimum: 1,
maximum: 65536,
default_value: 8192,
applies_at: "next_call",
},
});
expect((await service.inspect({ path, max_entries: 16 })).ok).toBe(true);
const cancelled = await service.inspect(
{ path },
{ signal: AbortSignal.abort() },
);
if (cancelled.ok) throw new Error("Expected cancellation");
expect(cancelled.error._tag).toBe("AnalysisCancelledError");
} finally {
await service.close();
}
});
});