Skip to content

Commit 51c03bc

Browse files
committed
TlsSession: address PR dotnet#130366 review feedback
Code fixes (all in TlsSession.cs): - Remove debug 'CAPTURE-DEBUG' InvalidOperationException that was leaking a debug marker to consumers; silently skip ClientHello capture when the parse succeeds but the frame length doesn't fit (already benign). - Fix ArrayPool leak in HandshakeSocketCore / ReadSocketCore: replace Array.Resize on the pool-rented _socketInBuf with a proper Rent/Copy/ Return via new GrowSocketInBuf() helper. Prevents leaking the original pooled buffer and prevents returning a non-pooled resized array back to the pool on Dispose. - Fix SetClientCertificateContext race with concurrent sessions: instead of disposing/nulling the shared TlsContext.CredentialsHandle, acquire a session-local credentials handle (mirroring the pattern in SetContext). ActiveCredentialsRef() already routes subsequent PAL calls through the session-local handle. Acquire eagerly so failures surface here. Squash-regression restores: - System.Net.Security.csproj: restore $(NetCoreAppCurrent)-openbsd TFM that was dropped from TargetFrameworks. - System.Net.Security.csproj: fix stale ReadWriteAdapter.cs path — the file lives at $(CommonPath)System/ (not System/Net/). - LocalAppContextSwitches.cs: restore IsOpenBsd flag used by NegotiateAuthenticationPal.ManagedSpnego on OpenBSD. - docs/project/list-of-diagnostics.md: restore SYSLIB0065 row and update the SYSLIB5007 row to .NET 11 with the wording proposed by @vcsjones.
1 parent ada2c85 commit 51c03bc

4 files changed

Lines changed: 45 additions & 14 deletions

File tree

‎docs/project/list-of-diagnostics.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ The PR that reveals the implementation of the `<IncludeInternalObsoleteAttribute
119119
| __`SYSLIB0062`__ | XSLT Script blocks are not supported. |
120120
| __`SYSLIB0063`__ | This constructor has been deprecated and argument bool isConnected does not have any effect. Use NamedPipeClientStream(PipeDirection direction, bool isAsync, SafePipeHandle safePipeHandle) instead. |
121121
| __`SYSLIB0064`__ | RSACryptoServiceProvider.Encrypt and RSACryptoServiceProvider.Decrypt methods that take a Boolean are obsolete. Use the overload that accepts RSAEncryptionPadding instead. |
122+
| __`SYSLIB0065`__ | Setting AsnEncodedData.RawData is obsolete. Use CopyFrom instead. |
122123

123124
## Analyzer Warnings
124125

@@ -319,4 +320,4 @@ Diagnostic id values for experimental APIs must not be recycled, as that could s
319320
| __`SYSLIB5004`__ | .NET 9 | TBD | `X86Base.DivRem` is experimental since performance is not as optimized as `T.DivRem` |
320321
| __`SYSLIB5005`__ | .NET 9 | .NET 10 | `System.Formats.Nrbf` is experimental |
321322
| __`SYSLIB5006`__ | .NET 10 | TBD | Types for Post-Quantum Cryptography (PQC) are experimental. |
322-
| __`SYSLIB5007`__ | .NET 10 | TBD | Low-level TLS engine types (`TlsContext`, `TlsSession`) in `System.Net.Security` are experimental. |
323+
| __`SYSLIB5007`__ | .NET 11 | TBD | Low-level TLS engine types (`TlsContext`, `TlsSession`) in `System.Net.Security` are experimental. |

‎src/libraries/System.Net.Security/src/System.Net.Security.csproj‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
4-
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-freebsd;$(NetCoreAppCurrent)-haiku;$(NetCoreAppCurrent)-android;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos;$(NetCoreAppCurrent)</TargetFrameworks>
4+
<TargetFrameworks>$(NetCoreAppCurrent)-windows;$(NetCoreAppCurrent)-linux;$(NetCoreAppCurrent)-freebsd;$(NetCoreAppCurrent)-openbsd;$(NetCoreAppCurrent)-haiku;$(NetCoreAppCurrent)-android;$(NetCoreAppCurrent)-osx;$(NetCoreAppCurrent)-ios;$(NetCoreAppCurrent)-tvos;$(NetCoreAppCurrent)</TargetFrameworks>
55
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
66
<!-- This is needed so that code for TlsCipherSuite will have no namespace (causes compile errors) when used within T4 template -->
77
<DefineConstants>$(DefineConstants);PRODUCT</DefineConstants>
@@ -127,8 +127,8 @@
127127
<Compile Include="$(CommonPath)System\Net\ExceptionCheck.cs"
128128
Link="Common\System\Net\ExceptionCheck.cs" />
129129
<!-- Common -->
130-
<Compile Include="$(CommonPath)\System\Net\ReadWriteAdapter.cs"
131-
Link="Common\System\Net\ReadWriteAdapter.cs" />
130+
<Compile Include="$(CommonPath)System\ReadWriteAdapter.cs"
131+
Link="Common\System\ReadWriteAdapter.cs" />
132132
<Compile Include="$(CommonPath)System\NotImplemented.cs"
133133
Link="Common\System\NotImplemented.cs" />
134134
<Compile Include="$(CommonPath)System\Net\Security\TlsAlertMessage.cs"

‎src/libraries/System.Net.Security/src/System/Net/Security/LocalAppContextSwitches.cs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@ namespace System
99
{
1010
internal static partial class LocalAppContextSwitches
1111
{
12+
// OpenBSD's GSS-API (Heimdal) cannot drive password-based NTLM and reports a
13+
// missing Kerberos TGT as a missing credential, so managed NTLM is used there.
14+
internal static readonly bool IsOpenBsd = RuntimeInformation.IsOSPlatform(OSPlatform.Create("OPENBSD"));
15+
1216
private static int s_disableTlsResume;
1317
internal static bool DisableTlsResume
1418
{

‎src/libraries/System.Net.Security/src/System/Net/Security/TlsSession.cs‎

Lines changed: 36 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -575,10 +575,15 @@ public void SetClientCertificateContext(SslStreamCertificateContext? context)
575575
}
576576
_options.CertificateContext = context;
577577

578-
// Drop the cached credentials handle (acquired without a client cert) so the
579-
// next Handshake re-acquires with the supplied context.
580-
_context!.CredentialsHandle?.Dispose();
581-
_context!.CredentialsHandle = null;
578+
// Acquire a session-local credentials handle so we don't touch the shared
579+
// TlsContext.CredentialsHandle, which is used by any concurrent session on
580+
// the same context (racing/disposing it can cause handshake failures or
581+
// deliver the wrong certificate on SChannel). ActiveCredentialsRef() will
582+
// return this session-local handle for subsequent PAL calls. Acquire eagerly
583+
// so any AcquireCredentialsHandle failure surfaces here, not from an opaque
584+
// PAL call downstream.
585+
_sessionCredentialsHandle?.Dispose();
586+
_sessionCredentialsHandle = SslStreamPal.AcquireCredentialsHandle(_options, false);
582587
_resumeAfterCredentials = true;
583588
}
584589

@@ -785,10 +790,9 @@ private protected TlsOperationStatus HandshakeBufferedCore(
785790
{
786791
_clientHelloBytesBuffered = input.Slice(0, frameLength).ToArray();
787792
}
788-
else
789-
{
790-
throw new InvalidOperationException($"CAPTURE-DEBUG: fL={frameLength} iL={input.Length}");
791-
}
793+
// If frameLength is out of range (shouldn't happen after a successful
794+
// parse), silently skip capture; the session continues to work,
795+
// GetClientHelloLength just reports 0.
792796
}
793797
else if (_securityContext is null)
794798
{
@@ -1800,6 +1804,28 @@ private bool TryDrainPendingToSocket(out SocketError lastError)
18001804
return true;
18011805
}
18021806

1807+
// Grows the pool-rented _socketInBuf to at least newMinCapacity, preserving the
1808+
// first _socketInUsed bytes. Rents a new array, copies the used prefix, and
1809+
// returns the old buffer to the shared pool.
1810+
private void GrowSocketInBuf(int newMinCapacity)
1811+
{
1812+
byte[] oldBuf = _socketInBuf!;
1813+
int newSize = oldBuf.Length;
1814+
do
1815+
{
1816+
newSize *= 2;
1817+
}
1818+
while (newSize < newMinCapacity);
1819+
1820+
byte[] newBuf = ArrayPool<byte>.Shared.Rent(newSize);
1821+
if (_socketInUsed > 0)
1822+
{
1823+
Buffer.BlockCopy(oldBuf, 0, newBuf, 0, _socketInUsed);
1824+
}
1825+
_socketInBuf = newBuf;
1826+
ArrayPool<byte>.Shared.Return(oldBuf);
1827+
}
1828+
18031829
private protected TlsOperationStatus HandshakeSocketCore()
18041830
{
18051831
ThrowIfDisposed();
@@ -1890,7 +1916,7 @@ private protected TlsOperationStatus HandshakeSocketCore()
18901916
if (_socketInUsed >= _socketInBuf.Length)
18911917
{
18921918
// Should not happen with conservative scratch sizing, but guard.
1893-
Array.Resize(ref _socketInBuf, _socketInBuf.Length * 2);
1919+
GrowSocketInBuf(_socketInUsed + 1);
18941920
}
18951921
int received = _socket!.Receive(
18961922
_socketInBuf.AsSpan(_socketInUsed),
@@ -1990,7 +2016,7 @@ private protected TlsOperationStatus ReadSocketCore(Span<byte> buffer, out int b
19902016

19912017
if (_socketInUsed >= _socketInBuf.Length)
19922018
{
1993-
Array.Resize(ref _socketInBuf, _socketInBuf.Length * 2);
2019+
GrowSocketInBuf(_socketInUsed + 1);
19942020
}
19952021
int received = _socket!.Receive(
19962022
_socketInBuf.AsSpan(_socketInUsed),

0 commit comments

Comments
 (0)