Repository navigation
Hold a SafeHandle reference while formatting a channel binding - #136584
Merged
Merged
Conversation
SafeFreeContextBufferChannelBinding.ToString() read the raw handle and called Marshal.Copy on it without keeping the SafeHandle alive. After the handle field is loaded nothing uses the object any more, so the finalizer (or a concurrent Dispose) could run ReleaseHandle, which calls FreeContextBuffer, while the native buffer was still being copied. Every call to TransportContext.GetChannelBinding creates a new wrapper that the caller usually does not hold on to, so this is easily reachable. Take a DangerousAddRef reference around the copy, as is done for the other raw-handle uses of SafeHandles in this file, and return null for a closed handle instead of reading freed memory. Co-authored-by: Copilot <[email protected]> Copilot-Session: a13c23e0-b8e7-47bd-9714-cdc27528b527
|
Azure Pipelines: Successfully started running 3 pipeline(s). 13 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @bartonjs, @vcsjones, @dotnet/area-system-security |
Co-authored-by: Copilot <[email protected]> Copilot-Session: f86ef291-922b-46a2-bed8-98845b7c4e0e
vcsjones
reviewed
Oct 28, 2026
GC.KeepAlive only protects against finalization. Return null instead of throwing if the handle is closed before the reference is taken. Co-authored-by: Copilot <[email protected]> Copilot-Session: f86ef291-922b-46a2-bed8-98845b7c4e0e
vcsjones
approved these changes
Oct 28, 2026
Avoid Marshal.Copy, the temporary array and the Replace by writing the space-separated hex string with string.Create and HexConverter. Co-authored-by: Copilot <[email protected]> Copilot-Session: f86ef291-922b-46a2-bed8-98845b7c4e0e
Co-authored-by: Copilot <[email protected]> Copilot-Session: f86ef291-922b-46a2-bed8-98845b7c4e0e
Co-authored-by: Copilot <[email protected]> Copilot-Session: f86ef291-922b-46a2-bed8-98845b7c4e0e
jkoritzinsky
approved these changes
Oct 28, 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.
SafeFreeContextBufferChannelBinding.ToString()read the raw handle and calledMarshal.Copyon it without keeping theSafeHandlealive. After the handle field is loaded nothing uses the object any more, so the finalizer (or a concurrent Dispose) could run ReleaseHandle, which calls FreeContextBuffer, while the native buffer was still being copied. Every call to TransportContext.GetChannelBinding creates a new wrapper that the caller usually does not hold on to, so this is easily reachable.Take a DangerousAddRef reference around the copy, as is done for the other raw-handle uses of SafeHandles in this file, and return null for a closed handle instead of reading freed memory.