Align PE certificate tables before portable signing - #36
Open
nurfed1 wants to merge 2 commits into
Open
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Problem
The PE attribute certificate table must start on an 8-byte boundary. The portable signing paths currently compute the Authenticode digest over the original PE and then append the WIN_CERTIFICATE record directly at EOF.
When an unsigned PE ends at an unaligned offset, the resulting security-directory file offset is also unaligned. psign can parse and verify its own output, but Windows rejects that signature even though the embedded digest is otherwise internally consistent.
This is observable with valid PE inputs carrying an overlay or other trailing data whose total file length is not divisible by eight.
Microsoft documents the attribute certificate table layout and alignment requirements in the PE format specification:
https://learn.microsoft.com/en-us/windows/win32/debug/pe-format#the-attribute-certificate-table-image-only
Change
For a PE without an existing certificate table, the signer now appends zero padding to the next 8-byte boundary before hashing. Because that padding is added before digest computation, it remains part of the Authenticode-hashed region and the subsequently embedded PKCS#7 digest matches the final file layout.
The low-level append helper rejects an unaligned unsigned image rather than silently adding unhashed bytes. Existing certificate tables are accepted only when both their offset and size are aligned; misaligned tables are rejected instead of being relocated and invalidating an existing signature.
The same preparation is used by all PE signing entry points. Verification also rejects misaligned certificate-table metadata explicitly.
Tests
Rust tests cover:
The Windows parity test builds the repro input at runtime from the existing CI PE fixture, signs it through portable sign-pe, and requires native signtool verify /pa to succeed. No signed product binary is committed as a fixture.
The fork branch passed formatting, clippy, the portable test matrix, packaging builds, and the Windows native verification workflow: