Skip to content

fix(pe): accept WIN_CERTIFICATE padding counted in dwLength - #6711

Closed
Deepak-Kambala wants to merge 1 commit into
morluto:mainfrom
Deepak-Kambala:fix/pe-certificate-dwlength-padding
Closed

Deepak-Kambala wants to merge 1 commit into
morluto:mainfrom
Deepak-Kambala:fix/pe-certificate-dwlength-padding

Conversation

@Deepak-Kambala

@Deepak-Kambala Deepak-Kambala commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

inspect-pe-certificates rejected valid PE32 and PE32+ certificate entries when WIN_CERTIFICATE.dwLength included the required zero alignment padding. Those bytes were passed to the CMS parser as encoded payload, which rejected them as trailing ASN.1 data.

Use the ASN.1 root end offset to bound the encoded CMS payload. Accept trailing bytes only when they are zero-filled and exactly the padding needed to align the full certificate entry. Preserve the original artifact bytes and digest, dwLength, and entry range; report the CMS payload range and hash without alignment padding.

Regression coverage checks counted-padding PE32 and PE32+ fixtures, existing controls, invalid trailing bytes, malformed CMS, truncated entries, and length boundaries. It also checks artifact identity and entry/payload ranges and hashes.

Fixes #6694

Verification

  • Parser tests: 30 passed.
  • Related PE tests: 76 passed.
  • Focused CLI/filesystem/MCP/contract tests: 17 passed, 4 skipped.
  • npm run check:fast: passed.
  • git diff --check: passed.

Provider and artifact coverage

Tests use synthetic PE fixtures; no external provider or real-world PE artifact was verified.

Author readiness

Human owner:

  • [yes] I reviewed the final diff and relevant existing code, understand the change and its edge cases, and take responsibility for the complete PR, including AI-assisted work.
  • [yes] I checked the evidence above, confirmed it supports the current change, and recorded meaningful validation gaps.
  • [yes] I declare this PR ready for initial maintainer review.

@Deepak-Kambala
Deepak-Kambala marked this pull request as ready for review October 23, 2026 12:41
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Oct 23, 2026 •

Copy link
Copy Markdown

Codex Review Summary

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-10-23T12:46:25.212087Z fb2db30 Draft marked ready
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@greptile-apps

greptile-apps Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

RetriggerView in GreptileConfidence Score: 4/5

[Medium impact] The change reaches the PE certificate-inspection workflow.

The PR appears safe to merge, with a non-blocking opportunity to avoid reading each ASN.1 tree twice.

Findings

  1. P2 Certificate trees get read twice ▶
Fix with agent prompt
### Issue 1
src/native/pe/PeCertificateParser.ts:120-130
`readAsn1(entryPayload)` builds the full ASN.1 tree, then `parsePkcs7SignedData(payload)` immediately builds it again. This doubles the tree-reading work for every PKCS#7 entry, including entries without counted padding. Reuse the decoded root through a shared helper, while keeping the strict trailing-data check for standalone callers.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Summary

Accepts PE certificate entries whose dwLength includes the required zero padding.

  • PE certificate inspection separates CMS data from counted alignment padding.

Review was based on source inspection; tests were not rerun. Whether this PR is superseded could not be checked because GitHub access was unavailable.

Reviews (1) · Last reviewed commit: "fix(pe): accept WIN_CERTIFICATE padding ..." · Reviewed by Greptile

Comment on lines +120 to 130
const root = readAsn1(entryPayload);
const paddingBytes = entryPayload.length - root.end;
const alignedLength = align8(8 + root.end);
if (
paddingBytes !== 0 &&
(8 + entryPayload.length !== alignedLength ||
!entryPayload.subarray(root.end).every((byte) => byte === 0))
)
fail("WIN_CERTIFICATE has invalid PKCS#7 alignment padding.");
payload = entryPayload.subarray(0, root.end);
const parsed = parsePkcs7SignedData(payload);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Certificate trees get read twice

readAsn1(entryPayload) builds the full ASN.1 tree, then parsePkcs7SignedData(payload) immediately builds it again. This doubles the tree-reading work for every PKCS#7 entry, including entries without counted padding. Reuse the decoded root through a shared helper, while keeping the strict trailing-data check for standalone callers.

Prompt To Fix With AI
This is a comment left during a code review.
Path: src/native/pe/PeCertificateParser.ts
Line: 120-130

Comment:
**Certificate trees get read twice**

`readAsn1(entryPayload)` builds the full ASN.1 tree, then `parsePkcs7SignedData(payload)` immediately builds it again. This doubles the tree-reading work for every PKCS#7 entry, including entries without counted padding. Reuse the decoded root through a shared helper, while keeping the strict trailing-data check for standalone callers.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Note: If this suggestion doesn't match your team's coding style, reply to this and let me know. I'll remember it for next time!

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: fb2db3021f

ℹ️ About Codex in GitHub

Codex has been enabled to automatically review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

When you sign up for Codex through ChatGPT, Codex can also answer questions or update the PR, like "@codex address that feedback".

// WIN_CERTIFICATE to eight bytes. Bound CMS to its encoded ASN.1 root;
// tolerate only the corresponding alignment bytes, never arbitrary
// trailing data.
const root = readAsn1(entryPayload);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P1 Badge Parse each PKCS#7 payload only once

For every valid type-2 entry, this call builds the entire BER node tree just to discover root.end, and the immediately following parsePkcs7SignedData(payload) builds the same tree again via readAsn1 in PeCertificateAsn1.ts:562. That doubles ASN.1 traversal and node allocation for normal signatures (up to the 16,384-node budget per entry); pass the decoded root/end into the SignedData decoder or have one API return both parsed data and the consumed length instead.

AGENTS.md reference: AGENTS.md:L27-L29

Useful? React with 👍 / 👎.

@morluto morluto added area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working labels Nov 23, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area: native Native analysis and decompilation, including Ghidra, IDA, and Hopper analysis behavior bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] inspect-pe-certificates rejects WIN_CERTIFICATE padding counted in dwLength

2 participants