Repository navigation
Accept moving write buffers for socket-bound TLS sessions - #131759
Conversation
In fd-mode, TlsSession hands SSL_write a span over GC-tracked memory. When the socket send buffer is full, SSL_write reports WANT_WRITE (surfaced as DestinationTooSmall) and OpenSSL remembers the address of the plaintext buffer as the pending write. A managed caller retrying the write has no way to keep that memory pinned across two separate calls, so the GC may have relocated it; OpenSSL then rejects the retry with SSL_R_BAD_WRITE_RETRY, which the session surfaces as an AuthenticationException. Enable SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER on socket-bound SSL objects so the address comparison is skipped. The length check still enforces the real contract, and the pending record is flushed from OpenSSL's internal write buffer, so the retry buffer's contents are not re-read. SslStream's BIO path is unaffected: its managed-span BIO grows on demand and never reports WANT_WRITE. Co-authored-by: Copilot <[email protected]>
|
Azure Pipelines: Successfully started running 4 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
|
Tagging subscribers to this area: @dotnet/ncl, @bartonjs, @vcsjones |
There was a problem hiding this comment.
Pull request overview
This PR updates the OpenSSL-backed socket-bound TLS session path to tolerate write retries where the managed plaintext buffer’s address changes between SSL_write attempts (GC relocation), by enabling OpenSSL’s SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER for fd-backed SSL objects. It also adds a regression test to validate that a DestinationTooSmall/WANT_WRITE retry succeeds even when the retry buffer is at a different address.
Changes:
- Add a new native CryptoNative shim to enable
SSL_MODE_ACCEPT_MOVING_WRITE_BUFFERon anSSL*. - Call the new shim during
SafeSslHandle.Createwhen creating socket-bound (useFd) sessions. - Add a functional regression test that induces
WANT_WRITEand verifies retry success with both a cloned buffer and a GC-relocated buffer.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| src/native/libs/System.Security.Cryptography.Native/pal_ssl.h | Declares the new exported shim API for enabling the OpenSSL mode. |
| src/native/libs/System.Security.Cryptography.Native/pal_ssl.c | Implements the shim by calling SSL_set_mode(..., SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER). |
| src/native/libs/System.Security.Cryptography.Native/entrypoints.c | Exposes the new shim via the CryptoNative entrypoint table. |
| src/libraries/Common/src/Interop/Unix/System.Security.Cryptography.Native/Interop.Ssl.cs | Adds a LibraryImport and invokes it for useFd SafeSslHandle creation. |
| src/libraries/System.Net.Security/tests/FunctionalTests/TlsSessionTests.cs | Adds a regression test covering retry semantics with a moved write buffer. |
Co-authored-by: Copilot Autofix powered by AI <[email protected]>
There was a problem hiding this comment.
Pull request overview
Copilot reviewed 5 out of 5 changed files in this pull request and generated no new comments.
Suppressed comments (1)
src/libraries/System.Net.Security/tests/FunctionalTests/TlsSessionTests.cs:3158
drainis awaited without a timeout. IfSslStream.ReadAsync(..., drainCts.Token)fails to observe cancellation promptly (or hangs for any reason), this test can hang the entire run. Most other waits in this file are bounded viaWaitAsync(TimeSpan...).
finally
{
drainCts.Cancel();
await drain;
}
Problem
A socket-bound
TlsSocketSessioncan fail a perfectly valid write retry with:In fd-mode,
TryFastWritepassesref MemoryMarshal.GetReference(buffer)— a GC-heap address that is pinned only for the duration of the P/Invoke. When the socket send buffer is full,SSL_writereportsWANT_WRITE(surfaced asDestinationTooSmall) and OpenSSL stores that address inwpend_buf. Before the caller retries, the GC can relocate the array, sossl3_write_bytessees a different pointer and fails withSSL_R_BAD_WRITE_RETRY.This is not something the caller can work around: a
ReadOnlySpan<byte>cannot be kept pinned across two separate calls, so retrying with the exact samebyte[]is already the correct thing to do — and it still fails intermittently, depending on GC timing.Not reachable through
SslStream: its managed-span BIO grows on demand and never reportsWANT_WRITE.Fix
Enable
SSL_MODE_ACCEPT_MOVING_WRITE_BUFFER— the mode OpenSSL provides precisely for relocating heaps — on socket-boundSSLobjects only (useFdinSafeSslHandle.Create). This skips the address comparison. Thewpend_tot > lencheck still enforces the real contract, and the pending record is flushed from OpenSSL's internal write buffer, so the retry buffer's contents are never re-read.SslStream's BIO path is untouched.pal_ssl.h/pal_ssl.c/entrypoints.c: newCryptoNative_SslSetAcceptMovingWriteBuffershim.Interop.Ssl.cs:LibraryImportand the call site inSafeSslHandle.Create.Testing
New
TlsSessionTests.SocketBoundSession_WriteRetryWithRelocatedBuffer_Succeedstheory fills the socket send buffer to induceWANT_WRITE, then retries and drives the write toCompletewhile a client drains. Two cases:Verified locally on Linux/x64:
bad write retryin each).TlsSessionTests: 47/47 passed.System.Net.Security.Tests: 5034 passed, 0 failed, 8 skipped.Note
This pull request description was generated with the assistance of GitHub Copilot.