From 17ad9c14ff84f41d13d2b5cedf02a19fdd49401b Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 09:22:26 -0500 Subject: [PATCH] fix(pe): validate resource name extents before capacity (#12368) --- scripts/verify/baselines/raw-buffer-read.json | 2 +- src/domain/pe/peResourceInventory.ts | 2 +- src/domain/pe/peResourceTreeFacts.ts | 2 +- .../applications/peResources.test.ts | 73 +++++++++++++++++++ 4 files changed, 76 insertions(+), 3 deletions(-) diff --git a/scripts/verify/baselines/raw-buffer-read.json b/scripts/verify/baselines/raw-buffer-read.json index 772e9b88de..e56db522ca 100644 --- a/scripts/verify/baselines/raw-buffer-read.json +++ b/scripts/verify/baselines/raw-buffer-read.json @@ -33,7 +33,7 @@ "src/domain/native/nativeAotPe.ts": 37, "src/domain/native/readBinaryContainer.ts": 1, "src/domain/pe/peResourceInventory.ts": 7, - "src/domain/pe/peResourceTreeFacts.ts": 9, + "src/domain/pe/peResourceTreeFacts.ts": 8, "src/domain/peSectionHeader.ts": 5, "src/domain/pngVisualDiff.ts": 2, "src/domain/riscvIsaDeclaration.ts": 1, diff --git a/src/domain/pe/peResourceInventory.ts b/src/domain/pe/peResourceInventory.ts index d1881d0fd7..067bc05103 100644 --- a/src/domain/pe/peResourceInventory.ts +++ b/src/domain/pe/peResourceInventory.ts @@ -134,8 +134,8 @@ export const parsePeResources = async ( const { utf16Units, payloadBytes: length } = readPeResourceNameFacts( bytes.subarray(start, start + PE_RESOURCE_NAME_PREFIX_BYTES), ); - reserveDecodedName(utf16Units); relative(at, PE_RESOURCE_NAME_PREFIX_BYTES + length); + reserveDecodedName(utf16Units); const raw = bytes.subarray( start + PE_RESOURCE_NAME_PREFIX_BYTES, start + PE_RESOURCE_NAME_PREFIX_BYTES + length, diff --git a/src/domain/pe/peResourceTreeFacts.ts b/src/domain/pe/peResourceTreeFacts.ts index 3307c20c4e..b76736f191 100644 --- a/src/domain/pe/peResourceTreeFacts.ts +++ b/src/domain/pe/peResourceTreeFacts.ts @@ -47,7 +47,7 @@ export const readPeResourceEntryFacts = (bytes: Buffer) => { /** Observe the length only; callers admit decoded-name costs before reading or decoding payload. */ export const readPeResourceNameFacts = (bytes: Buffer) => { - const utf16Units = bytes.readUInt16LE(0); + const utf16Units = new BinaryReader(bytes, true, "pe").u16(0); return { utf16Units, payloadBytes: utf16Units * 2 }; }; diff --git a/tests/acceptance/applications/peResources.test.ts b/tests/acceptance/applications/peResources.test.ts index b30c2f5a66..b5bfd66c34 100644 --- a/tests/acceptance/applications/peResources.test.ts +++ b/tests/acceptance/applications/peResources.test.ts @@ -96,6 +96,78 @@ const expectPeInputFailuresAcrossAdapters = async (input: { const contract = toolContract("inspect_pe_resources"); +const expectPeResourceNameExtentsAcrossAdapters = async ( + cli: TestCli, + call: PeResourceCall, + root: string, +): Promise => { + const payload = Buffer.from("text"); + const retainedName = "Evidence".repeat(5_000); + for (const [priorLeaves, backed] of [ + [36, false], + [38, false], + [38, true], + ] as const) { + const fixture = peResourceFixture( + false, + [ + ...Array.from({ length: priorLeaves }, (_, index) => ({ + type: 10, + name: retainedName, + language: index + 1, + payload, + })), + { + type: 11, + name: backed ? "S".repeat(65_535) : "SECOND", + language: 1033, + payload, + }, + ], + 256 * 1024, + ); + if (!backed) { + const resourceRoot = fixture.directoryOffsets[0]; + const nameDirectory = fixture.directoryOffsets.at(-2); + if (resourceRoot === undefined || nameDirectory === undefined) + throw new Error("Missing fixture resource directory"); + const nameOffset = + fixture.bytes.readUInt32LE(nameDirectory + 16) & 0x7fff_ffff; + // Only the length changes. The declared name cannot fit in the resource interval. + fixture.bytes.writeUInt16LE(65_535, resourceRoot + nameOffset); + } + const path = join(root, `resource-name-${priorLeaves}-${backed}.exe`); + await writeFile(path, fixture.bytes); + if (!backed) { + await expectPeInputFailureAcrossAdapters({ + cli, + call, + path, + reason: "invalid_format", + }); + continue; + } + const result = await cli.run({ + arguments: ["inspect-pe-resources", path, "--json"], + }); + expect(result.exitCode).toBe(1); + expect(result.json).toMatchObject({ + code: "resource_constraint", + details: { + resource: "memory", + reported_limits: { + boundary: "pe_resource_metadata_bytes", + maximum: 8 * 1024 * 1024, + maximum_metadata_bytes: 8 * 1024 * 1024, + }, + }, + }); + const response = await call({ path }); + expect(response.isError).toBe(true); + expect(parseMcpToolError(response)).toEqual({ error: result.json }); + } +}; + const expectPeWordAlignedRecordsAcrossAdapters = async (input: { readonly cli: TestCli; readonly call: PeResourceCall; @@ -467,6 +539,7 @@ cliTest( root, }); await expectPeMappingOwnershipAcrossAdapters(cli, call, root); + await expectPeResourceNameExtentsAcrossAdapters(cli, call, root); await expectPePaddingFailureAcrossAdapters({ cli, call,