Skip to content

Fix bounds check bugs in TlsFrameHelper and harden SslStream server handshake - #126352

Merged
rzikm merged 5 commits into
dotnet:mainfrom
rzikm:client-hello-fuzz
Apr 1, 2026
Merged

rzikm merged 5 commits into
dotnet:mainfrom
rzikm:client-hello-fuzz

Conversation

@rzikm

@rzikm rzikm commented Mar 31, 2026

Copy link
Copy Markdown
Member

Note

This PR description was AI/Copilot-generated.

Summary

Fix multiple out-of-bounds access bugs in \TlsFrameHelper\ discovered by the new \SslStreamClientHelloFuzzer, and add an early validation check in \SslStream\ to reject non-ClientHello frames during the initial server handshake.

Changes

TlsFrameHelper bounds check fixes

  • **\TryParseHelloFrame**: Added \helloLength < ProtocolVersionSize\ check to reject hello messages too small to contain protocol version bytes. Previously, a crafted TLS record with \helloLength=0\ caused an \IndexOutOfRangeException\ when accessing \helloData[0].

  • **\TryParseClientHello\ / \TryParseServerHello**: Added \p.Length < sizeof(ushort)\ check before calling \ReadUInt16BigEndian\ for the extension list length. A malformed ClientHello with truncated data after compression methods could leave only 1 byte remaining, causing \ArgumentOutOfRangeException.

  • **\GetSniFromHostNameStruct**: Added \hostNameStruct.Length < 2\ check before \ReadUInt16BigEndian.

  • **\TryGetSupportedVersionsFromExtension**: Added \�xtensionData.IsEmpty\ check before accessing \�xtensionData[0].

SslStream server handshake hardening

  • **\ReceiveHandshakeFrameAsync**: Added early check that the first frame a server receives is a ClientHello (TLS protocol invariant). Previously, non-ClientHello data (wrong content type, or non-ClientHello handshake messages) would bypass the \ServerOptionDelegate\ callback, leaving no certificate configured, causing a confusing \NotSupportedException\ from \AcquireServerCredentials. Now throws \AuthenticationException\ with a clear message.

Fuzzer

  • Added \SslStreamClientHelloFuzzer\ that exercises \SslStream.AuthenticateAsServer\ with corrupted ClientHello inputs.

Validation

  • All 13 fuzzer crash inputs now handled gracefully (throw \AuthenticationException\ or \IOException)
  • 500 random inputs (up to 200 bytes) pass with 0 failures
  • All 4,059 existing \TlsFrameHelper\ tests pass
  • All 790 \SslStream\ tests pass

rzikm and others added 4 commits March 31, 2026 13:15
…malformed ClientHello

In TryParseHelloFrame, when a crafted TLS record declares a hello body
length of 0 (or less than ProtocolVersionSize), the code would try to
access helloData[0] on an empty span, causing IndexOutOfRangeException.

Add a minimum length check (helloLength < ProtocolVersionSize) to reject
hello messages too small to contain the required protocol version fields.

Also update the fuzzer to accept NotSupportedException, which is the
expected behavior when non-ClientHello data bypasses the server options
callback leaving no certificate configured.

Co-authored-by: Copilot <[email protected]>
Per TLS spec, the first message a server receives must be ClientHello.
Previously, non-ClientHello frames (wrong content type, or handshake
messages like ServerHello/Finished) would bypass the ServerOptionDelegate
callback and fall through to AcquireServerCredentials, which threw a
confusing NotSupportedException about missing certificates.

Add an early check in ReceiveHandshakeFrameAsync: when the server has not
yet initialized its security context, verify that the incoming frame is a
Handshake with ClientHello type. Otherwise throw AuthenticationException
with a clear 'unexpected packet format' message.

This also removes the need for the fuzzer to catch NotSupportedException
since all corrupted inputs now produce AuthenticationException or
IOException as expected.

Co-authored-by: Copilot <[email protected]>
Fix ArgumentOutOfRangeException crashes when parsing malformed TLS
hello messages with truncated data:

- TryParseClientHello: check p.Length >= 2 before reading extension
  list length with ReadUInt16BigEndian
- TryParseServerHello: same fix for the identical pattern
- GetSniFromHostNameStruct: check hostNameStruct.Length >= 2 before
  reading hostname length
- TryGetSupportedVersionsFromExtension: check extensionData is not
  empty before accessing the version list length byte

Co-authored-by: Copilot <[email protected]>
@dotnet-policy-service

Copy link
Copy Markdown
Contributor

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

@rzikm
rzikm marked this pull request as ready for review March 31, 2026 14:15
@rzikm
rzikm requested a review from a team March 31, 2026 14:15

@wfurt wfurt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. good findings.

@rzikm
rzikm merged commit cac7b4d into dotnet:main Apr 1, 2026
110 of 115 checks passed
@github-actions github-actions Bot locked and limited conversation to collaborators May 1, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants