Skip to content

Hold a SafeHandle reference while formatting a channel binding - #136584

Merged
EgorBo merged 6 commits into
dotnet:mainfrom
EgorBo:fix-channelbinding-tostring-addref
Oct 28, 2026
Merged

EgorBo merged 6 commits into
dotnet:mainfrom
EgorBo:fix-channelbinding-tostring-addref

Conversation

@EgorBo

@EgorBo EgorBo commented Oct 28, 2026

Copy link
Copy Markdown
Member

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.

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

Copy link
Copy Markdown
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.

@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

Co-authored-by: Copilot <[email protected]>
Copilot-Session: f86ef291-922b-46a2-bed8-98845b7c4e0e
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
EgorBo and others added 3 commits October 28, 2026 19:19
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
@EgorBo
EgorBo merged commit e414a3a into dotnet:main Oct 28, 2026
88 checks passed
@EgorBo
EgorBo deleted the fix-channelbinding-tostring-addref branch October 28, 2026 23:50
@dotnet-milestone-bot dotnet-milestone-bot Bot added this to the 12.0-preview1 milestone Oct 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants