Skip to content

[crypto] TLS handshake parses wire lengths unclamped (cert/hs_len vs record bounds) #1062

Closed

Description

@eas4ai

Summary

tls_recv_server_handshake (lib/std/tls.w:536-619) decodes
wire-controlled lengths and uses them without clamping to the
received record, so a malicious server drives out-of-bounds reads
over the 16640-byte record stack buffer (same escape class as the
executed #1056, one layer up).

Evidence (complete read at 450733e; each site inspected

end-to-end, no clamp on any path between decode and use)

  • first_cert_len from wire bytes (:584-587) is passed
    unclamped to x509_parse(first_cert_data, first_cert_len)
    (:588-590); nothing checks 6 + first_cert_len <= hs_len.
  • hs_len (3-byte wire length, :561) is never checked against
    the record: tls_hs_hash_update(conn, &rec_buf[rp], 4 + hs_len)
    (:564) hashes past the buffer, and every body parse
    (tls_parse_server_hello(conn, body, hs_len) :569, cert copy
    :576, SKE reads :606-612) reads up to hs_len past body.
    The only guard (rp + 4 > rec_len, :555) covers the header,
    not the body.
  • *cert_len = hs_len (:580) is stored unclamped while the copy
    loop caps at 8192 (:576), so cert_len can exceed the
    cert_buf (tls_handshake:8192, :796) it describes.

Wire-path execution is HELD (requires a peer; no stub harness in
repo), but the consequence shape is already executed next door:
#1056 proved the same parser turns overlong lengths into escaping
offsets with rc=1.

Notes (not applied; audit is read-only)

Clamp hs_len to the record (rp + 4 + hs_len <= rec_len,
fail closed), clamp first_cert_len to hs_len - 6, store the
clamped copy count in cert_len. Hardening notes (internal
callers currently pass safe sizes, recorded not separately
filed): tls_send/tls_send_encrypted trust data_len against
the 16640 ct buffer; ClientHello trusts hostname_len against
the 512 buf; PRF trusts label/seed against seed_full[256].
Suggested regression: stub-peer record with oversized hs_len /
first_cert_len must fail closed with no escaping offsets.

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced

    Verdict. Established by full source reading: tls_recv_server_handshake decodes wire-controlled handshake and certificate lengths and uses them without clamping to the received record, driving out-of-bounds reads over the 16640-byte record stack buffer and a stored cert_len that can exceed the 8192-byte buffer it describes. The only bounds guard on the parse loop covers the 4-byte header, not the body. This is the same "trust the wire length" class as #1056, which I executed; here it is one layer up, in the record parser. Wire-path execution is held (no TLS peer/stub in-repo), but the missing clamps are provable from source and the downstream escape is already demonstrated in #1056.

    Tested. main @ 5809ac1 (2026-09-14); lib/std/tls.w unchanged since the reported revision 450733e. Source read on with v0.15.2.1 (staging worktree), macOS arm64.

    Source facts (5809ac1, all in tls_recv_server_handshake, tls.w:536-619).

    • hs_len is a 3-byte wire length (:561) never checked against rec_len. The loop's only guard is rp + 4 > rec_len (:555), which validates the header only.
    • tls_hs_hash_update(conn, &rec_buf[rp], 4 + hs_len) (:564) hashes 4 + hs_len bytes; an oversized hs_len reads past the 16640-byte rec_buf.
    • tls_parse_server_hello(conn, body, hs_len) (:569) and the SKE reads consume up to hs_len past body.
    • Cert copy loop caps at 8192 (:576) but *cert_len = hs_len is stored unclamped (:580); the caller's cert_buf is [u8; 8192] (:796), so cert_len can describe more bytes than exist, and any later consumer trusting cert_len reads past the buffer.
    • first_cert_len is decoded from wire bytes (:587) and passed straight to x509_parse(first_cert_data, first_cert_len) (:590) with no 6 + first_cert_len <= hs_len check. Given [crypto] x509_parse missing content-bounds validation: truncated cert yields escaping offsets, OOB reads downstream #1056 (x509 DER content bounds unvalidated), an oversized first_cert_len yields escaping offsets, and the EC-point extraction at :603-612 then reads first_cert_data + (pt_start + ...) out of bounds.

    Root cause. Missing length validation against the received record at every wire-length decode site, same class as #1056. No compiler involvement.

    Severity. High/security: a malicious or MITM server (and the auth gap is #1061) drives OOB reads over stack buffers during the handshake, before any authentication. Worst case is a crash; whether bytes leak depends on what the hash/parse does with them.

    Related. #1056 (executed proof of the x509 escape this feeds), #1061 (no TLS authentication at all). No open PR.

    Recommendation — champion. Clamp at each decode: reject unless rp + 4 + hs_len <= rec_len (fail closed), clamp first_cert_len to hs_len - 6, and store the clamped copy count in cert_len rather than raw hs_len. Acceptance criteria:

    • A stub-peer handshake record with hs_len or first_cert_len larger than the record fails closed with no read past rec_buf and no escaping offsets.
    • cert_len never exceeds the bytes actually copied into cert_buf.
    • A regression harness (local tampering TLS stub) covers oversized hs_len, oversized first_cert_len, and a truncated record.

    Recorded, not separately filed (per the report). Internal callers currently pass safe sizes, but tls_send/tls_send_encrypted trust data_len against the 16640 ct buffer, ClientHello trusts hostname_len against the 512 buffer, and the PRF trusts label/seed against seed_full[256]; these should get the same defensive clamps.

    Blockers / not tested. No TLS peer/stub harness in-repo, so the wire path was not executed; the missing clamps are provable from source and the downstream OOB shape is executed in #1056. Linux/Windows not exercised.

  2. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Triage 2026-10-25, with v0.15.3.0 (main a261b65): reproduced in source: tls_recv_server_handshake (lib/std/tls.w:557-563) still decodes hs_len from the wire and passes 4 + hs_len to tls_hs_hash_update without clamping it to the received record.

    pre-release: a malicious server drives out-of-bounds reads; clamping each decoded length to the record is small.

  3. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Fixed on main by e3b0ad0 (PR #2667, stack #2668; battery green on the stack top 9f92dbb). The commit message names the root-cause line and the regression test.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions