Skip to content

Conversation

@github-actions
Copy link
Contributor

@github-actions github-actions bot commented Sep 4, 2025

Backport of #119362 to release/10.0

/cc @vcsjones

Customer Impact

  • Customer reported
  • Found internally

Customers occasionally use an X509Certificate2 in a way that results in concurrent use and Dispose. A change was recently made that cause this to have an access violation or corruption and take down the process.

Regression

  • Yes
  • No

Introduced by PR #117907.

Testing

We have a smoke test for this, RaceDisposeAndKeyAccess that attempts to use and dispose a certificate concurrently. Since the race window is fairly narrow, the test does not hit the conditions required to create a crash reliably. However this test did start failing in CI which identified the issue.

The fix was verified by running the same test with significantly more attempts and not crashing in 60 minutes, whereas a crash would previously happen in less than 5.

Risk

Low. The change is straightforward and uses a SafeHandle to ensure the native interop does not use a freed handle. There is significant test coverage in this area to test that existing scenarios continue to work as expected.

CertDuplicateCertificateContext does not ensure the CERT_CONTEXT pointer it is incrementing has not been freed.
If the duplicate and dispose race, duplicating a disposed handle will lead to unspecified behavior.

For .NET 10, we can use the SafeHandle around the certificate before we duplicate the handle. For OOB, we don't have
access to the internals, so we will continue to use the IntPtr Handle property.
@vcsjones vcsjones requested a review from bartonjs September 4, 2025 22:13
@vcsjones vcsjones added this to the 10.0.0 milestone Sep 4, 2025
@dotnet-policy-service
Copy link
Contributor

Tagging subscribers to this area: @dotnet/area-system-security, @bartonjs, @vcsjones
See info in area-owners.md if you want to be subscribed.

@bartonjs bartonjs requested a review from artl93 September 4, 2025 22:18
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

area-System.Security Servicing-approved Approved for servicing release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants