Skip to content

Fix SignCheck signature verification and interop issues - #16667

Merged
ViktorHofer merged 3 commits into
mainfrom
signcheck-verification-fixes
Apr 2, 2026
Merged

ViktorHofer merged 3 commits into
mainfrom
signcheck-verification-fixes

Conversation

@ViktorHofer

@ViktorHofer ViktorHofer commented Apr 2, 2026 •

Copy link
Copy Markdown
Member

Summary

Addresses feedback from dotnet/dotnet#5834 and fixes several bugs found during code review.

Changes

StgOpenStorageEx migration (PR feedback from @teo-tsirpanis)

  • Replaced StgOpenStorage with StgOpenStorageEx in Ole32.cs, OleStorageSecurityInfoProvider.cs, and StructuredStorage.cs per Microsoft recommendation.

WinVerifyTrust file integrity check (new)

  • Added WinVerifyTrust with WTD_HASH_ONLY_FLAG after CMS blob validation to detect files modified after signing. Previously, AuthentiCode.IsSigned only validated the CMS signature was well-formed—a file with a valid signature blob but tampered content would pass.

WinTrustFileInfo Unicode interop fix (bug)

  • Updated WinTrustFileInfo struct to use CharSet.Unicode and UnmanagedType.LPWStr for pcwszFilePath instead of LPTStr. This ensures the path is always marshaled as a Unicode (LPCWSTR) string as required by WinVerifyTrust, preventing corruption of non-ASCII paths.

StructuredStorage COM object lifecycle fixes (bug)

  • Fixed double Marshal.ReleaseComObject on rootStorage that threw InvalidComObjectException on every MSP patch verification, crashing nested verification and leaking temp directories.
  • Preserved intentional lock release before new Database() to avoid STGM_SHARE_EXCLUSIVE sharing violations.
  • Fixed rootStorage COM leak on non-patch compound files (only released inside IsPatch branch).
  • Fixed incorrect null-check on COM enumerator: changed if (enumStg != null) to if (rootStorageEnum != null) so the guard correctly protects Marshal.ReleaseComObject(rootStorageEnum) when EnumElements fails.

MsiVerifier error propagation (bug)

  • Both catch blocks now add error details to SignatureVerificationResult via AddDetail, not just Log.WriteError. Callers consuming results programmatically now see extraction failures instead of silent passes.

WinTrustFileInfo string leak fix (bug)

  • Added Marshal.DestroyStructure before FreeHGlobal to free the inner unmanaged string allocated by StructureToPtr for pcwszFilePath.

To double check:

- Migrate StgOpenStorage to StgOpenStorageEx per Microsoft recommendation
- Add WinVerifyTrust file integrity check (WTD_HASH_ONLY_FLAG) to detect
  post-signing file modifications that CMS-only validation would miss
- Fix double Marshal.ReleaseComObject on rootStorage in StructuredStorage
  that crashed MSP patch verification; preserve intentional lock release
  before Database open to avoid STGM_SHARE_EXCLUSIVE sharing violations
- Fix rootStorage COM leak on non-patch compound files
- Propagate recursive MSI/Binary-table extraction errors to
  SignatureVerificationResult instead of only logging them
- Fix unmanaged string leak in WinTrustFileInfo marshaling via
  DestroyStructure before FreeHGlobal

Co-authored-by: Copilot <[email protected]>

Copilot AI left a comment

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.

Pull request overview

This PR improves SignCheck’s Windows Authenticode/structured-storage verification by modernizing OLE storage interop, tightening signature integrity validation, and ensuring extraction errors propagate to callers.

Changes:

  • Migrates OLE compound storage opening from StgOpenStorage to StgOpenStorageEx and fixes COM object lifecycle/release behavior.
  • Adds a Windows-only WinVerifyTrust integrity check (hash validation) after CMS validation to detect post-signing tampering.
  • Improves MSI nested verification error reporting by adding extraction failures to SignatureVerificationResult.

Reviewed changes

Copilot reviewed 5 out of 5 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
src/SignCheck/Microsoft.SignCheck/Verification/OleStorageSecurityInfoProvider.cs Switches OLE storage open to StgOpenStorageEx for reading the \x05DigitalSignature stream.
src/SignCheck/Microsoft.SignCheck/Verification/MsiVerifier.cs Propagates extraction exceptions into SignatureVerificationResult details.
src/SignCheck/Microsoft.SignCheck/Verification/AuthentiCodeVerifier.cs Adds WinVerifyTrust-based file integrity validation (hash-only) after CMS validation on Windows.
src/SignCheck/Microsoft.SignCheck/Interop/StructuredStorage.cs Uses StgOpenStorageEx and fixes IStorage release timing/leaks for MSP extraction scenarios.
src/SignCheck/Microsoft.SignCheck/Interop/Ole32.cs Replaces StgOpenStorage P/Invoke with StgOpenStorageEx and adds STGFMT_STORAGE.

@ViktorHofer

Copy link
Copy Markdown
Member Author

@copilot address feedback

Copilot AI commented Apr 2, 2026

Copy link
Copy Markdown
Contributor

@copilot address feedback

Addressed both review comments in c532e67:

  1. WinTrustFileInfo (WinTrustFileInfo.cs): Changed StructLayout to CharSet.Unicode and the marshal attribute from UnmanagedType.LPTStr to UnmanagedType.LPWStr, ensuring pcwszFilePath is always passed as a Unicode string as WinVerifyTrust expects.

  2. COM enumerator null-check (StructuredStorage.cs): Changed if (enumStg != null) to if (rootStorageEnum != null) so the guard correctly protects the Marshal.ReleaseComObject(rootStorageEnum) call.

@ViktorHofer
ViktorHofer enabled auto-merge (squash) April 2, 2026 12:54
@ViktorHofer
ViktorHofer disabled auto-merge April 2, 2026 13:07
…gnature

The C# \x hex escape greedily consumes up to 4 hex digits, so
\x05DigitalSignature was parsed as \x05D (U+005D = ']') followed
by 'igitalSignature' instead of U+0005 followed by 'DigitalSignature'.

This caused IStorage.OpenStream to search for the wrong stream name,
making MSI signature verification always report files as unsigned.

Fix: use \u0005 which always takes exactly 4 hex digits.

Co-authored-by: Copilot <[email protected]>
@ViktorHofer

Copy link
Copy Markdown
Member Author

Tested E2E locally on the sdk .exe bundle that contains the workload msis.

@ViktorHofer
ViktorHofer merged commit 9a4e05f into main Apr 2, 2026
9 checks passed
@ViktorHofer
ViktorHofer deleted the signcheck-verification-fixes branch April 2, 2026 15:42
akoeplinger added a commit to akoeplinger/arcade that referenced this pull request Aug 20, 2026
PR dotnet#16667 added WinVerifyTrust-based content-integrity validation to SignCheck. Unlike the other Authenticode SIPs, the legacy Windows Script Host SIP cannot locate embedded JavaScript signatures when invoked from an MTA thread and returns TRUST_E_NOSIGNATURE.

Run JavaScript WinVerifyTrust verification on an STA thread so signed scripts are recognized correctly. Add signed, tampered, and unsigned JavaScript coverage.

Fixes regression from dotnet#16667

Co-authored-by: Copilot <[email protected]>
akoeplinger added a commit to akoeplinger/arcade that referenced this pull request Aug 20, 2026
PR dotnet#16667 added WinVerifyTrust-based content-integrity validation to SignCheck. Unlike the other Authenticode SIPs, the legacy Windows Script Host SIP cannot locate embedded JavaScript signatures when invoked from an MTA thread and returns TRUST_E_NOSIGNATURE.

Run JavaScript WinVerifyTrust verification on an STA thread so signed scripts are recognized correctly. Add signed, tampered, and unsigned JavaScript coverage.

Fixes regression from dotnet#16667

Co-authored-by: Copilot <[email protected]>
akoeplinger added a commit to akoeplinger/arcade that referenced this pull request Aug 20, 2026
PR dotnet#16667 added WinVerifyTrust-based content-integrity validation to SignCheck. Unlike the other Authenticode SIPs, the legacy Windows Script Host SIP cannot locate embedded JavaScript signatures when invoked from an MTA thread and returns TRUST_E_NOSIGNATURE.

Run JavaScript WinVerifyTrust verification on an STA thread so signed scripts are recognized correctly. Add signed, tampered, and unsigned JavaScript coverage.

Fixes regression from dotnet#16667

Co-authored-by: Copilot <[email protected]>
akoeplinger added a commit to akoeplinger/arcade that referenced this pull request Aug 20, 2026
PR dotnet#16667 added WinVerifyTrust-based content-integrity validation to SignCheck. Unlike the other Authenticode SIPs, the legacy Windows Script Host SIP cannot locate embedded JavaScript signatures when invoked from an MTA thread and returns TRUST_E_NOSIGNATURE.

Run JavaScript WinVerifyTrust verification on an STA thread so signed scripts are recognized correctly. Add signed, tampered, and unsigned JavaScript coverage.

Fixes regression from dotnet#16667

Co-authored-by: Copilot <[email protected]>
akoeplinger added a commit to akoeplinger/arcade that referenced this pull request Aug 20, 2026
PR dotnet#16667 added WinVerifyTrust-based content-integrity validation to SignCheck. Unlike the other Authenticode SIPs, the legacy Windows Script Host SIP cannot locate embedded JavaScript signatures when invoked from an MTA thread and returns TRUST_E_NOSIGNATURE.

Run JavaScript WinVerifyTrust verification on an STA thread so signed scripts are recognized correctly. Add signed, tampered, and unsigned JavaScript coverage.

Fixes regression from dotnet#16667

Co-authored-by: Copilot <[email protected]>
akoeplinger added a commit to akoeplinger/arcade that referenced this pull request Aug 20, 2026
PR dotnet#16667 added WinVerifyTrust-based content-integrity validation to SignCheck. Unlike the other Authenticode SIPs, the legacy Windows Script Host SIP cannot locate embedded JavaScript signatures when invoked from an MTA thread and returns TRUST_E_NOSIGNATURE.

Run JavaScript WinVerifyTrust verification on an STA thread so signed scripts are recognized correctly. Add signed, tampered, and unsigned JavaScript coverage.

Fixes regression from dotnet#16667

Co-authored-by: Copilot <[email protected]>
akoeplinger added a commit that referenced this pull request Aug 20, 2026
PR #16667 added WinVerifyTrust-based content-integrity validation to SignCheck. Unlike the other Authenticode SIPs, the legacy Windows Script Host SIP cannot locate embedded JavaScript signatures when invoked from an MTA thread and returns TRUST_E_NOSIGNATURE.

Run JavaScript WinVerifyTrust verification on an STA thread so signed scripts are recognized correctly. Add signed, tampered, and unsigned JavaScript coverage.

Fixes regression from #16667

Co-authored-by: Copilot <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants