Security #9167
closedtls: evasion on fragmented handshake header across records
bea218e7b1dd8ffb09f250e62c096c6b3da749b4
Description
Splitting the TLS handshake header across records disables all TLS inspection for the connection¶
TLDR¶
The vulnerability is that Suricata's TLS parser in src/app-layer-ssl.c treats a Handshake record that is too small to hold the whole 4-byte handshake header as a fatal error instead of waiting for the rest. When a client puts the first 1-3 bytes of its ClientHello in one small TLS record and the rest in later records, the parser returns a hard error, and Suricata reacts by turning off all application-layer parsing for that TCP flow in both directions. Because splitting a handshake message across record boundaries is explicitly allowed by the TLS specification and is accepted by normal TLS servers, an attacker who controls one end of a connection can complete a perfectly normal TLS session while Suricata extracts nothing from it. Every tls. rule (tls.sni, ja3/ja4, tls.cert_, tls.version, etc.) is silently bypassed for the connection; only a single generic anomaly event is raised. The root cause is on line 2601 (and its sibling on line 1779) of src/app-layer-ssl.c.
Root cause analysis¶
When Suricata has a complete TLS record whose content type is Handshake, it refuses to process it if the record body is shorter than a 4-byte handshake header, and returns a decoder error:
if (ssl_state->curr_connp->record_length < 4) {
SSLParserReset(ssl_state);
SSLSetEvent(ssl_state, TLS_DECODER_EVENT_INVALID_SSL_RECORD);
SCLogDebug("record len < 4 => %u", ssl_state->curr_connp->record_length);
return SSL_DECODER_ERROR(-1);
}
The problem is that a Handshake record carrying only 1-3 bytes is not invalid. In TLS the handshake is a byte stream that may be fragmented into records at any boundary (RFC 5246 §6.2.1, RFC 8446 §5.1), so the 4-byte handshake header itself may be split across two records. A compliant server simply buffers the first bytes and waits for the rest. Suricata instead treats "this one record does not yet contain a full header" as "this traffic is malformed" and aborts.
The parser already knows how to buffer a fragmented handshake message body across records: once the 4 header bytes have been read, if the announced message length exceeds the current record it allocates hs_buffer and resumes on the next record (https://github.com/OISF/suricata/blob/bc48b4f44c7d3bdd06bf670405a2f016bb1157a1/src/app-layer-ssl.c#L1815). But that logic only begins after the 4-byte header has been read, so a header that is itself split across records is never reassembled. The same gap produces a sibling hard error when a record ends 1-3 bytes into the next message's header, at https://github.com/OISF/suricata/blob/bc48b4f44c7d3bdd06bf670405a2f016bb1157a1/src/app-layer-ssl.c#L1779-L1782.
A single SSL_DECODER_ERROR(-1) does not just drop one record; it disables TLS inspection for the whole flow. The dataflow from the small record to that outcome is:
- The attacker's client sends a Handshake record whose body is the first 2 bytes of the ClientHello, e.g. the 7 bytes
16 03 01 00 02 01 00, then sends the remaining handshake bytes in later records. - Protocol detection matches the
|16 03 01|pattern and hands the flow to the TLS parser (SSLRegisterPatternsForProtocolDetection, https://github.com/OISF/suricata/blob/bc48b4f44c7d3bdd06bf670405a2f016bb1157a1/src/app-layer-ssl.c#L3050-L3052). SSLv3Decode(https://github.com/OISF/suricata/blob/bc48b4f44c7d3bdd06bf670405a2f016bb1157a1/src/app-layer-ssl.c#L2397) parses the 5-byte record header, sees a complete record of length 2, so the "record body incomplete" buffering does not apply, and it enters the Handshake branch.- The
record_length < 4check (https://github.com/OISF/suricata/blob/bc48b4f44c7d3bdd06bf670405a2f016bb1157a1/src/app-layer-ssl.c#L2601) fires and returnsSSL_DECODER_ERROR(-1). SSLDecodeconverts that intoAPP_LAYER_ERROR(https://github.com/OISF/suricata/blob/bc48b4f44c7d3bdd06bf670405a2f016bb1157a1/src/app-layer-ssl.c#L2780).AppLayerParserParsesees the negative status and jumps to its error handler (https://github.com/OISF/suricata/blob/bc48b4f44c7d3bdd06bf670405a2f016bb1157a1/src/app-layer-parser.c#L1652).- The error handler calls
StreamTcpDisableAppLayer(f)(https://github.com/OISF/suricata/blob/bc48b4f44c7d3bdd06bf670405a2f016bb1157a1/src/app-layer-parser.c#L1754), which marks app-layer detection complete and stops reassembly for both directions of the flow (https://github.com/OISF/suricata/blob/bc48b4f44c7d3bdd06bf670405a2f016bb1157a1/src/stream-tcp-reassemble.c#L445-L458). - From then on Suricata parses no more TLS for the connection, so the real ClientHello/ServerHello/certificate that follow are never inspected, even though the bytes are present in the stream.
I confirmed this against a build of the parser at commit bc48b4f4. Feeding an intact ClientHello (SNI example.attacker.test) makes a tls.sni rule match. Feeding the identical ClientHello with its handshake header split so the first record is the 7 bytes 16 03 01 00 02 01 00 makes the tls.sni match disappear; only a raw TCP content rule (matching the same bytes still sitting in the stream) still fires, alongside the single TLS_DECODER_EVENT_INVALID_SSL_RECORD anomaly. Sending a fully valid ClientHello in a later record on the same flow also produces no match, confirming that the one small record disables TLS parsing for the entire flow rather than skipping a single record.
Suggested fix¶
Do not treat a short handshake record as fatal. Extend the existing hs_buffer reassembly so it also buffers a partial (1-3 byte) handshake header across record boundaries, the same way it already buffers a partial message body. Concretely, at the record_length < 4 site (line 2601) and at the residual-bytes site (line 1779), instead of raising TLS_DECODER_EVENT_INVALID_SSL_RECORD and returning SSL_DECODER_ERROR(-1), stash the available header bytes in a small buffer and return the "incomplete / need more data" result so the parser resumes when the next record arrives; only once at least 4 header bytes have been accumulated should the message length be read and the existing body-buffering path take over. This keeps genuinely malformed traffic detectable (for example via a length sanity cap) while no longer aborting on RFC-compliant fragmentation.
Reproducing with suricata-verify¶
Attached is the suricata-verify test tls-fragmented-handshake-header, a directory containing test.yaml, test.rules and input.pcap. Its flow carries the SNI example.attacker.test with the ClientHello's handshake header split so the first Handshake record is the 7 bytes 16 03 01 00 02 01 00. The test contains two rules: sid:1 matches on tls.sni (does the parser extract the SNI?) and sid:2 matches the same bytes as raw TCP content (a built-in control confirming the bytes reached the flow regardless of the TLS parser).
To run it against a build of Suricata:
- Build Suricata as usual; the test runs against
./src/suricatain your source tree. - Get suricata-verify:
git clone https://github.com/OISF/suricata-verify - Unpack the attached artifacts into suricata-verify's
tests/directory, so thatsuricata-verify/tests/tls-fragmented-handshake-header/test.yamlexists. - From the Suricata source/build directory, run the test by name:
On current code the test reports FAILED: the raw-bytes control sub-check (sid:2) passes, but the tls.sni sub-check (sid:1) gets 0 matches and one INVALID_SSL_RECORD anomaly is raised. That failure is the evasion: the SNI bytes reached the flow but the TLS parser extracted nothing. Once the parser buffers partial handshake headers across records, the test passes, so it also serves as the regression test.
If Suricata aborts at startup with "failed to setup/expand stream session pool. Expand stream.memcap?", that is an environment limit on hosts with a very high CPU count (many worker threads), not a test failure; add --runmode single to the args: list in test.yaml, or raise stream.memcap, and re-run.
Credit¶
Please attribute Anthropic and Ada Logics (Adam Korczynski). This issue was found by Anthropic by using agents to study security of open source projects; Ada Logics validated the finding, created the report and communicated it.
Files