fix(pe): validate resource name extents before capacity (#12368)

This commit is contained in:
morluto authored and GitHub committed 2026-11-19 14:22:26 +00:00
1 parent 5f258086b0
commit 17ad9c14ff
4 files changed
+76 -3

No files matched your search

@@ -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,
+1 -1
View File
@@ -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,
+1 -1
View File
@@ -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 };
};
@@ -96,6 +96,78 @@ const expectPeInputFailuresAcrossAdapters = async (input: {
const contract = toolContract("inspect_pe_resources");
const expectPeResourceNameExtentsAcrossAdapters = async (
cli: TestCli,
call: PeResourceCall,
root: string,
): Promise<void> => {
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,