Skip to content

Conversation

@vcsjones
Copy link
Member

@vcsjones vcsjones commented Sep 4, 2025

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.

Contributes to #119313

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 added this to the 10.0.0 milestone Sep 4, 2025
@vcsjones vcsjones requested a review from bartonjs September 4, 2025 19:47
@vcsjones vcsjones self-assigned this Sep 4, 2025
Copilot AI review requested due to automatic review settings September 4, 2025 19:47
Copy link
Contributor

Copilot AI left a comment

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 addresses a race condition in certificate context duplication by using SafeHandle to ensure thread safety. The changes prevent potential crashes when CertDuplicateCertificateContext is called on a certificate handle that has been freed.

  • Uses SafeHandle for certificate context duplication instead of raw IntPtr
  • Implements conditional compilation to support both .NET 10 (with SafeHandle access) and OOB packages (with IntPtr fallback)
  • Adds proper reference counting around the duplication operation to prevent use-after-free

Reviewed Changes

Copilot reviewed 4 out of 4 changed files in this pull request and generated 1 comment.

File Description
CertificatePal.Windows.cs Adds SafeHandle property to expose the internal certificate context handle
System.Security.Cryptography CertificateHelpers.Windows.cs Implements SafeHandle-based certificate duplication with proper reference counting
Microsoft.Bcl.Cryptography CertificateHelpers.Windows.cs Provides fallback implementation using IntPtr for OOB packages
Common CertificateHelpers.Windows.cs Adds partial method declaration and updates GetPrivateKey to use new duplication method

@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.

@vcsjones vcsjones modified the milestones: 10.0.0, 11.0.0 Sep 4, 2025
@vcsjones vcsjones merged commit 638f0a5 into dotnet:main Sep 4, 2025
91 checks passed
@vcsjones
Copy link
Member Author

vcsjones commented Sep 4, 2025

/backport to release/10.0

@github-actions
Copy link
Contributor

github-actions bot commented Sep 4, 2025

Started backporting to release/10.0: https://github.com/dotnet/runtime/actions/runs/17477676496

@vcsjones vcsjones deleted the fix-cert-lifetime branch September 4, 2025 22:26
@github-actions github-actions bot locked and limited conversation to collaborators Oct 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants