Fix thread safety in SafeEvpPKeyHandle.DuplicateHandle#124485
Conversation
Use DangerousAddRef/DangerousRelease to prevent the source handle from being disposed by another thread while DuplicateHandle reads the handle value. Capture handle and ExtraHandle values into local variables while the ref count is elevated, so a concurrent Dispose cannot zero them out before they are copied to the new SafeEvpPKeyHandle. Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
...s/System.Security.Cryptography/src/System/Security/Cryptography/SafeEvpPKeyHandle.OpenSsl.cs
Outdated
Show resolved
Hide resolved
...s/System.Security.Cryptography/src/System/Security/Cryptography/SafeEvpPKeyHandle.OpenSsl.cs
Show resolved
Hide resolved
...s/System.Security.Cryptography/src/System/Security/Cryptography/SafeEvpPKeyHandle.OpenSsl.cs
Show resolved
Hide resolved
…ut catch Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
There was a problem hiding this comment.
Pull request overview
This PR fixes a thread-safety race condition in SafeEvpPKeyHandle.DuplicateHandle() where concurrent calls to Dispose() could zero the handle field between the UpRef operation and copying the handle to the new SafeHandle, resulting in an undisposed handle with an invalid IntPtr.Zero value.
Changes:
- Wrap DuplicateHandle operation in DangerousAddRef/DangerousRelease to prevent concurrent Dispose from zeroing handle fields mid-operation
- Add regression test exercising concurrent DuplicateHandle/Dispose calls
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/SafeEvpPKeyHandle.OpenSsl.cs |
Added DangerousAddRef/DangerousRelease protection around UpRefEvpPkey and handle copy operations in DuplicateHandle method |
src/libraries/System.Security.Cryptography.OpenSsl/tests/SafeEvpPKeyHandleTests.cs |
Added test that runs 1000 iterations of concurrent DuplicateHandle/Dispose to verify the race condition is fixed |
src/libraries/System.Security.Cryptography.OpenSsl/tests/SafeEvpPKeyHandleTests.cs
Show resolved
Hide resolved
src/libraries/System.Security.Cryptography.OpenSsl/tests/SafeEvpPKeyHandleTests.cs
Outdated
Show resolved
Hide resolved
…t formatting Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
...s/System.Security.Cryptography/src/System/Security/Cryptography/SafeEvpPKeyHandle.OpenSsl.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
|
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security |
...s/System.Security.Cryptography/src/System/Security/Cryptography/SafeEvpPKeyHandle.OpenSsl.cs
Outdated
Show resolved
Hide resolved
Co-authored-by: vcsjones <361677+vcsjones@users.noreply.github.com>
|
/backport to release/8.0-staging |
|
Started backporting to |
|
/backport to release/9.0-staging |
|
/backport to release/10.0 |
|
Started backporting to |
|
Started backporting to |
|
@bartonjs backporting to git am output$ git am --3way --empty=keep --ignore-whitespace --keep-non-patch changes.patch
Creating an empty commit: Initial plan
Applying: Fix thread safety in SafeEvpPKeyHandle.DuplicateHandle and add test
Using index info to reconstruct a base tree...
M src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/SafeEvpPKeyHandle.OpenSsl.cs
Falling back to patching base and 3-way merge...
Auto-merging src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/SafeEvpPKeyHandle.OpenSsl.cs
CONFLICT (content): Merge conflict in src/libraries/System.Security.Cryptography/src/System/Security/Cryptography/SafeEvpPKeyHandle.OpenSsl.cs
error: Failed to merge in the changes.
hint: Use 'git am --show-current-patch=diff' to see the failed patch
hint: When you have resolved this problem, run "git am --continue".
hint: If you prefer to skip this patch, run "git am --skip" instead.
hint: To restore the original branch and stop patching, run "git am --abort".
hint: Disable this message with "git config set advice.mergeConflict false"
Patch failed at 0002 Fix thread safety in SafeEvpPKeyHandle.DuplicateHandle and add test
Error: The process '/usr/bin/git' failed with exit code 128 |
SafeEvpPKeyHandle.DuplicateHandleDuplicateHandleusingDangerousAddRef/DangerousReleaseto prevent concurrentDisposefrom zeroing handle fieldsDuplicateHandle/DisposeDangerousAddRef/DangerousReleaseapproachkeyHandle.Dispose()(the dispose thread already handles it)Original prompt
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.