From 15150b017a1012ecc3bbd9e992f5086457a527c6 Mon Sep 17 00:00:00 2001 From: morluto <76467478+morluto@users.noreply.github.com> Date: Thu, 19 Nov 2026 07:57:45 -0500 Subject: [PATCH] fix(pe): validate certificate entry framing before capacity (#12336) fix(pe): frame certificate entries before count admission --- docs/pe-certificates.md | 2 + scripts/verify/baselines/raw-buffer-read.json | 1 - src/native/pe/PeCertificateParser.ts | 26 ++++---- tests/acceptance/peCertificatesCli.test.ts | 64 +++++++++++++++++++ 4 files changed, 80 insertions(+), 13 deletions(-) diff --git a/docs/pe-certificates.md b/docs/pe-certificates.md index 5b73bdc508..f8f4dacc45 100644 --- a/docs/pe-certificates.md +++ b/docs/pe-certificates.md @@ -78,6 +78,8 @@ next eight-byte boundary, or leaves fewer than eight header bytes including zeros, is truncated and fails the same way. The [PE format specification](https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#the-attribute-certificate-table-image-only) requires the sum of the rounded entry lengths to equal the directory size. +The reader checks the next entry's header and rounded extent before applying +`max_entries`. Only a framed entry can exhaust the count budget. SignedData must contain at least one `SignerInfo`, and each `SignerInfo` must carry a non-empty signature value. Each embedded X.509 certificate must include `subjectPublicKeyInfo` and `signatureValue`. diff --git a/scripts/verify/baselines/raw-buffer-read.json b/scripts/verify/baselines/raw-buffer-read.json index d7e6879aae..2d493094e5 100644 --- a/scripts/verify/baselines/raw-buffer-read.json +++ b/scripts/verify/baselines/raw-buffer-read.json @@ -80,7 +80,6 @@ "src/native/initializers/MachoInitializers.ts": 22, "src/native/initializers/NativeInitializersProvider.ts": 1, "src/native/pe/PeCertificateAsn1.ts": 2, - "src/native/pe/PeCertificateParser.ts": 3, "src/native/pe/PeLoadConfigParser.ts": 7, "src/native/pwndbg/CoreFileMappingAuthority.ts": 1, "src/native/pwntools/PwntoolsRecordedCrashProvider.ts": 1, diff --git a/src/native/pe/PeCertificateParser.ts b/src/native/pe/PeCertificateParser.ts index a0ddb70dba..2cf1365ad3 100644 --- a/src/native/pe/PeCertificateParser.ts +++ b/src/native/pe/PeCertificateParser.ts @@ -7,6 +7,7 @@ import type { PeCertificates, } from "../../domain/native/peCertificates.js"; import { peCertificateTypeName } from "../../domain/native/peCertificates.js"; +import { BinaryReader } from "../../domain/binaryContainer.js"; import { oidName, parsePkcs7SignedData, @@ -94,6 +95,7 @@ export const parsePeCertificates = ( const table = bytes.subarray(tableOffset, tableOffset + tableSize); const entries: PeCertificates["entries"] = []; let at = 0; + const reader = new BinaryReader(table, true, "pe-certificate-table"); let examined = 0; let teletexStringCompatibilityUsed = false; let unsignedAttributesPresent = false; @@ -102,6 +104,15 @@ export const parsePeCertificates = ( // Bytes that remain after that boundary are a short header, including // zeros at the start of the table. if (table.length - at < 8) fail("WIN_CERTIFICATE header is truncated."); + const length = reader.u32(at); + if (length < 8) + fail("WIN_CERTIFICATE dwLength is shorter than its header."); + const entryEnd = at + length; + if (entryEnd > table.length) + fail("WIN_CERTIFICATE dwLength exceeds the certificate table."); + const next = align8(entryEnd); + if (next > table.length) + fail("WIN_CERTIFICATE alignment padding is truncated."); examined += 1; if (examined > input.max_entries) throw new ArtifactReaderFailure( @@ -109,18 +120,9 @@ export const parsePeCertificates = ( `PE certificate table exceeds the ${String(input.max_entries)}-entry budget.`, { boundary: "capacity" }, ); - const length = table.readUInt32LE(at); - const revision = table.readUInt16LE(at + 4); - const certificateType = table.readUInt16LE(at + 6); - if (length < 8) - fail("WIN_CERTIFICATE dwLength is shorter than its header."); - if (at + length > table.length) - fail("WIN_CERTIFICATE dwLength exceeds the certificate table."); - const next = align8(at + length); - if (next <= at) fail("WIN_CERTIFICATE alignment did not advance."); - if (next > table.length) - fail("WIN_CERTIFICATE alignment padding is truncated."); - const entryPayload = table.subarray(at + 8, at + length); + const revision = reader.u16(at + 4); + const certificateType = reader.u16(at + 6); + const entryPayload = table.subarray(at + 8, entryEnd); let payload = entryPayload; let pkcs7: PeCertificates["entries"][number]["pkcs7"] = null; if (peCertificateTypeName(certificateType) === "pkcs7") { diff --git a/tests/acceptance/peCertificatesCli.test.ts b/tests/acceptance/peCertificatesCli.test.ts index e76a99de3c..398f3778ed 100644 --- a/tests/acceptance/peCertificatesCli.test.ts +++ b/tests/acceptance/peCertificatesCli.test.ts @@ -20,6 +20,70 @@ const contract = BINARY_DIAGNOSTICS_TOOL_CONTRACTS.find( if (contract === undefined) throw new Error("Missing inspect_pe_certificates contract"); +cliTest( + "checks the next PE certificate frame before reporting the entry limit through CLI and MCP", + async ({ cli }) => { + const root = await createTestTempDirectory("rea-pe-cert-limit-framing-"); + const path = join(root, "table.exe"); + const single = peCertificateFixture(false, Buffer.from([0x61]), 0x0001); + const table = single.subarray(0x200); + const tableSizeAt = 0x98 + 96 + 4 * 8 + 4; + const valid = Buffer.concat([single, table]); + valid.writeUInt32LE(table.length * 2, tableSizeAt); + const badLength = Buffer.from(valid); + badLength.writeUInt32LE(table.length + 1, single.length); + const badAlignment = Buffer.from(valid.subarray(0, valid.length - 1)); + badAlignment.writeUInt32LE(table.length * 2 - 1, tableSizeAt); + const call = await cli.sharedMcpCall!(); + for (const { bytes, message } of [ + { bytes: valid }, + { bytes: badLength, message: "dwLength exceeds" }, + { bytes: badAlignment, message: "alignment padding is truncated" }, + ]) { + const expectedError = + message === undefined + ? { + code: "resource_constraint", + details: { + resource: "capacity", + reported_limits: { max_entries: 1 }, + }, + } + : { + code: "invalid_request", + details: { + issues: [ + expect.objectContaining({ + reason: "invalid_format", + message: expect.stringContaining(message), + }), + ], + }, + }; + await writeFile(path, bytes); + const inspected = await cli.run({ + arguments: [ + "inspect-pe-certificates", + path, + "--max-entries", + "1", + "--json", + ], + }); + expect(inspected.exitCode, inspected.stdout).toBe(1); + expect.soft(inspected.json).toMatchObject(expectedError); + const rejected = await call("inspect_pe_certificates", { + path, + max_entries: 1, + }); + expect(rejected.isError, JSON.stringify(rejected)).toBe(true); + expect + .soft(parseMcpToolError(rejected).error) + .toMatchObject(expectedError); + } + }, +); + cliTest.for([false, true])( "rejects incomplete certificate alignment and recovers through CLI/MCP (PE32+ = %s)", async (plus, { cli }) => {