Repository navigation
[SignTool] Open PE files read-only with shared access during verification - #7274
Merged
ellahathaway merged 1 commit intoJun 17, 2026
Merged
Conversation
…tion
SignTool's read-only PE inspection (IsSignedPE, IsManaged, IsCrossgened,
GetExecutableType) opened files with FileStream(path, FileMode.Open), which
defaults to FileAccess.ReadWrite and FileShare.Read. Requesting write access
for a read-only verification causes an ERROR_SHARING_VIOLATION ("The process
cannot access the file ... because it is being used by another process")
whenever another process - typically an antimalware scanner - momentarily
holds the freshly-built binary open.
Open these files with FileAccess.Read and FileShare.ReadWrite instead, which
is all verification needs and is tolerant of concurrent readers.
Co-authored-by: Copilot <[email protected]>
Contributor
There was a problem hiding this comment.
Pull request overview
This PR hardens Windows signing verification in the VMR by changing several PE-inspection code paths in Microsoft.DotNet.SignTool to open binaries read-only (and with permissive sharing) to avoid intermittent ERROR_SHARING_VIOLATION failures during file inventory/verification.
Changes:
- Open PE files using
FileAccess.Readinstead of theFileStream(path, FileMode.Open)defaultReadWritein signature verification and PE inspection helpers. - Standardize PE inspection helpers on
FileShare.ReadWriteto reduce contention with concurrent file holders (e.g., antimalware scanning).
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| src/arcade/src/Microsoft.DotNet.SignTool/src/VerifySignatures.cs | Adjusts PE signature verification to open files read-only with shared access and adds rationale comment. |
| src/arcade/src/Microsoft.DotNet.SignTool/src/ContentUtil.cs | Updates PE header inspection helpers to open files read-only with shared access for consistency and reduced contention. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
ellahathaway
approved these changes
Jun 17, 2026
This was referenced Jul 7, 2026
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.
Problem
VMR Windows signing intermittently fails with a sharing violation while SignTool inventories files to sign. Example from
dotnet-unified-buildbuild 1464414 (failed on bothWindows_x64andWindows_x86, across retries), in thececilrepo's Sign step:Root cause
VerifySignatures.IsSignedPE(and the sibling read-only PE inspectorsContentUtil.IsManaged/IsCrossgened/GetExecutableType) opened the file withnew FileStream(path, FileMode.Open). That constructor defaults toFileAccess.ReadWriteandFileShare.Read.These code paths only read PE headers, so requesting write access is unnecessary — and harmful. When another process (typically Microsoft Defender / antimalware real-time scanning the just-built binary) momentarily holds the file open with
FileShare.Read, a new open that requests write access is denied withERROR_SHARING_VIOLATION, surfacing as "being used by another process." The failure happens in the sequential file-inventory phase (GenerateListOfFiles), so SignTool is not holding the file itself — the unnecessary write request is what makes a transient external read-share collide.Fix
Open these read-only verification streams with
FileAccess.ReadandFileShare.ReadWrite. Read access is all the verification needs and is compatible with a concurrent scanner's read share; offeringFileShare.ReadWritetolerates other holders.VerifySignatures.IsSignedPEContentUtil.IsManagedContentUtil.IsCrossgenedContentUtil.GetExecutableType(alreadyFileAccess.Read; widened the share mode for consistency)This makes signature verification robust to antimalware/indexer file holds and removes a recurring source of flaky Windows signing failures.