Skip to content

[SignTool] Open PE files read-only with shared access during verification - #7274

Merged
ellahathaway merged 1 commit into
dotnet:mainfrom
mmitche:signtool-fix-file-sharing-violation
Jun 17, 2026
Merged

ellahathaway merged 1 commit into
dotnet:mainfrom
mmitche:signtool-fix-file-sharing-violation

Conversation

@mmitche

@mmitche mmitche commented Jun 17, 2026

Copy link
Copy Markdown
Member

Problem

VMR Windows signing intermittently fails with a sharing violation while SignTool inventories files to sign. Example from dotnet-unified-build build 1464414 (failed on both Windows_x64 and Windows_x86, across retries), in the cecil repo's Sign step:

Sign.proj(74,5): error MSB4018: The "Microsoft.DotNet.SignTool.SignToolTask" task failed unexpectedly.
System.IO.IOException: The process cannot access the file
'...\src\cecil\artifacts\bin\Mono.Cecil\Release\netstandard2.0\Mono.Cecil.dll'
because it is being used by another process.
  at System.IO.FileStream..ctor(String path, FileMode mode)
  at Microsoft.DotNet.SignTool.VerifySignatures.IsSignedPE(String filePath)
  at Microsoft.DotNet.SignTool.Configuration.ExtractSignInfo(...)
  at Microsoft.DotNet.SignTool.Configuration.GenerateListOfFiles()

Root cause

VerifySignatures.IsSignedPE (and the sibling read-only PE inspectors ContentUtil.IsManaged / IsCrossgened / GetExecutableType) opened the file with new FileStream(path, FileMode.Open). That constructor defaults to FileAccess.ReadWrite and FileShare.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 with ERROR_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.Read and FileShare.ReadWrite. Read access is all the verification needs and is compatible with a concurrent scanner's read share; offering FileShare.ReadWrite tolerates other holders.

  • VerifySignatures.IsSignedPE
  • ContentUtil.IsManaged
  • ContentUtil.IsCrossgened
  • ContentUtil.GetExecutableType (already FileAccess.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.

Note: this changes mirrored Arcade sources under src/arcade/. It should also be ported to dotnet/arcade so it isn't reverted by the next code flow.

…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]>
Copilot AI review requested due to automatic review settings June 17, 2026 16:26
@dotnet-policy-service
dotnet-policy-service Bot requested a review from a team June 17, 2026 16:26

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 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.Read instead of the FileStream(path, FileMode.Open) default ReadWrite in signature verification and PE inspection helpers.
  • Standardize PE inspection helpers on FileShare.ReadWrite to 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
ellahathaway merged commit c2b3bc4 into dotnet:main Jun 17, 2026
11 checks passed
@dotnet-policy-service
dotnet-policy-service Bot requested a review from a team June 17, 2026 21:14
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.

3 participants