Skip to content

[crypto] x509_parse missing content-bounds validation: truncated cert yields escaping offsets, OOB reads downstream #1056

Closed

Description

@eas4ai

Summary

x509_parse (lib/std/crypto/x509.w) never validates claimed DER content lengths against buf_len, so a truncated certificate whose trailing element header fits returns success with offsets/lengths escaping the buffer. Downstream consumers then perform raw unbounded reads over the escaped ranges. Reachable from the TLS peer-cert path (lib/std/tls.w:589-590).

Evidence (executed at 450733e)

Vendored-parse probe (.audit/probes/x509/parse_escape.w) with an openssl-generated 771-byte RSA-2048 cert (full parse verified: rc=1, n=149/256, e=407/3, sig=515/256 — matches openssl anatomy):

  • buf_len=520 (cut inside signatureValue content; TLV header fits): returns 1 with sig_start=515, sig_len=256 — the range 515..771 escapes the 520-byte bound by 251 bytes.
  • buf_len=200 (mid-modulus cut): returns 0 (fail-closed). Only trailing-element truncation escapes with rc=1.

der_read_tl checks only header fit; der_skip and all cs + cl propagations are unclamped, and no stored start+len is validated. Escaped-range consumers (all raw reads): sha256_hash over the tbs range (:367), rsa_pkcs1_sha256_verify n/e/sig ranges (:372-377), oid_match (:75-83, which takes no length at all), ECDSA r/s/point reads. Adversarial cs + cl i32 wraparound collapses into the same missing-clamp class.

Notes (not applied; audit is read-only)

Clamp every cs + cl and every stored start+len to buf_len, failing closed with 0. Suggested regression: trailing-truncated cert must return 0 and never emit escaping offsets.

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced

    Verdict. Established. der_read_tl validates only that the tag and length header fit within buf_len; it never checks that content_start + content_len fits. der_skip returns cs + cl unclamped, and x509_parse stores element start/len without a final bounds check, so a certificate truncated inside a trailing element (whose TLV header still fits) parses as success with offsets that escape the buffer. x509_verify_signature then performs raw reads over those ranges (sha256_hash on the tbs range, rsa_pkcs1_sha256_verify on the n/e/sig ranges), i.e. out-of-bounds reads driven by attacker-controlled length fields. Reachable from the TLS peer-certificate path.

    Tested.

    Source main @ 5809ac1 (2026-09-14); x509.w unchanged since the reported revision 450733e
    Compilers with v0.15.2.1 built from 5809ac1 (staging worktree); a compiler built from 450733e with the nightly-20260903 seed. Same result
    Platform macOS Darwin 25.6.0 arm64

    Repro of the primitive (der_read_tl inlined verbatim from x509.w:28-60, fed a 4-byte buffer [0x04, 0x82, 0x01, 0x00] — tag OCTET STRING, long-form length 0x0100 = 256):

    content_start = 4      (>= 0, i.e. SUCCESS; not the -1 fail sentinel)
    content_len   = 256
    content_start + content_len = 260   -- escapes buf_len = 4 by 256 bytes
    

    Both compilers. The header-fit guard (p + num_len_bytes > buf_len) passes because 2 + 2 == 4, and there is no content_start + content_len <= buf_len check, so a range 256 bytes past the buffer is returned as valid. This is the exact primitive behind the report's full-cert numbers (buf_len 520 → sig_start 515, sig_len 256, escaping by 251).

    Source pins (5809ac1).

    • der_read_tl: only header bounds checked (x509.w:29,36,50); the two *(out_content_start) = p; *(out_content_len) = length stores never validate p + length <= buf_len.
    • der_skip returns cs + cl unclamped (x509.w:72).
    • x509_parse stores sig_start/sig_len, tbs_start/tbs_len, key ranges from these outputs with no start + len <= buf_len gate (x509.w:190-300).
    • Escaped-range consumers, all raw reads: sha256_hash(cert_buf + cert.tbs_start, cert.tbs_len, ...) and rsa_pkcs1_sha256_verify(..., cert_buf + cert.sig_start, cert.sig_len, ...) (x509.w:367,372-377), plus oid_match which takes no length at all (:75-83).

    Additional hazard. content_len is assembled from up to 4 length bytes into an i32 (x509.w:56-58), so a crafted 0x84 7F FF FF FF length is negative or near INT_MAX; cs + cl can then wrap. Any fix must clamp before, not after, the addition. Same missing-clamp class as the report notes.

    Root cause. A DER length-validation omission: header bounds are checked, content bounds are not. No compiler defect; established by the primitive repro plus the consumer trace. I did not drive the full x509_parse end to end because it and x509_verify_signature are private unsafe fn and need a real DER cert; the report did that with an openssl-generated 771-byte RSA-2048 cert and got rc=1 with escaping sig offsets, consistent with the primitive above.

    Severity. High and security-relevant: out-of-bounds reads over attacker-supplied certificate content, reachable from TLS peer-cert verification (lib/std/tls.w:589-590 per the report). The read escapes are bounded only by where the process's mapping ends, so worst case is a crash (info-leak potential depends on what the hash/verify does with the bytes).

    Validator gap. No test feeds a truncated certificate; combined with #1053/#1055 (crypto suites don't compile / not wired in), the whole x509 path has no negative-input coverage.

    Related. #1052/#1051/#1054/#1055 are other crypto-path defects; this one is the DER parser. No open PR.

    Recommendation — champion. Add content-bounds validation: in der_read_tl, after computing content_start/content_len, fail closed (content_start = -1) unless content_len >= 0 and content_start + content_len <= buf_len (compute the comparison in a way that cannot overflow, e.g. content_len > buf_len - content_start). Clamp/validate der_skip's return and every stored start + len in x509_parse the same way. Acceptance criteria:

    Blockers / not tested. Full x509_parse/TLS path not driven end to end (private, needs a DER fixture); the OOB read itself was not observed under a sanitizer here. The primitive-level repro plus the consumer trace establish the defect. 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: der_read_tl (lib/std/crypto/x509.w:28) still checks that the tag and length header fit in buf_len (:29, :36, :50) and never that content_start + content_len does.

    pre-release: a truncated peer certificate drives out-of-bounds reads through std.tls; the bound check is one comparison.

  3. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Fixed on main by 3f96513 (PR #2671, stack #2678; battery green on the stack top 0008bf2). 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