Skip to content

[crypto] ecdsa_verify_der_sig panics (DoS) on over-long DER signature INTEGERs instead of rejecting #1057

Closed

Description

@eas4ai

Summary

ecdsa_verify_der_sig (lib/std/crypto/x509.w:447-471, same shape :400-405) aborts the process on a malformed ECDSA signature whose r/s INTEGER exceeds 32 bytes (post zero-strip): let r_off = 32 - r_len goes negative while the copy loop still indexes r_bytes[r_off + ri], producing panic: index out of bounds instead of returning 0. Peer-controlled signature bytes (via the x509/TLS path) become a crash/DoS vector.

Evidence (executed at 450733e)

Vendored probe (.audit/probes/x509/sig_oversize.w, ecdsa_p256_verify stubbed): DER SEQ{INT(40 bytes), INT(1)} → panic: index out of bounds. Control (.audit/probes/x509/sig_legit33.w): SEQ{INT(0x00 + 32 bytes), INT(1)} — the legitimate P-256 zero-padded form, strips to 32 — returns 0 cleanly. Boundary exactly r/s_len > 32.

Notes (not applied; audit is read-only)

Reject r_len/s_len > 32 with 0 up front. Same file also carries dead code at :408-413 (a bogus s-position computation whose result is discarded with "re-parse from scratch" commentary) worth cleaning in the same pass.

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced

    Verdict. Established. In ecdsa_verify_der_sig, let r_off = 32 - r_len goes negative when an r (or s) INTEGER exceeds 32 bytes after the leading-zero strip, and the copy loop still evaluates r_bytes[r_off + ri], indexing the 32-byte array at a negative offset — a bounds panic that aborts the process instead of returning 0. Because the signature bytes are peer-controlled through the x509/TLS verification path, this is a remote crash/DoS. The dead-code observation is also correct.

    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 panic
    Platform macOS Darwin 25.6.0 arm64

    Repro of the exact r-copy loop from x509.w:447-453:

    var src: [u8; 40] = [7u8; 40]
    let r_len = 40                 // over-long INTEGER (attacker-controlled)
    var r_bytes: [u8; 32] = [0u8; 32]
    let r_off = 32 - r_len         // = -8
    var ri = 0
    while ri < r_len and ri < 32:
        r_bytes[r_off + ri] = src[ri]   // r_bytes[-8 + ri] -> OOB
        ri = ri + 1
    
    r_len result
    40 panic: index out of bounds
    32 (legitimate P-256) completes cleanly

    Both compilers. The ri < 32 cap in the loop guard does not help: the index r_off + ri is already negative at ri = 0, so the first store panics. The report's full-DER probe (SEQ{INT(40 bytes), INT(1)} → panic; SEQ{INT(0x00 + 32 bytes), INT(1)} → returns 0) is consistent, and the boundary is exactly len > 32.

    Source pins (5809ac1). r path: x509.w:447-453 (let r_off = 32 - r_len; r_bytes[r_off + ri] = rv). s path is the same shape at :461-471. Neither rejects len > 32 before computing the offset. Reachable from certificate/TLS verification.

    Dead code confirmed. x509.w:400-414 is an abandoned first attempt at the r/s parse: it computes a bogus s position, comments "Hmm, need to track position properly ... Let me re-parse from scratch," and ends in a recursive return ecdsa_verify_der_sig(...). The r_bytes fill there has the same 32 - r_len shape. It should be removed in the same pass.

    Root cause. Missing input validation: the INTEGER length is trusted to be <= 32 before deriving a right-justification offset. No compiler defect; the bounds panic is the runtime correctly catching a negative index the code created. Established by the loop repro plus the source.

    Severity. High/security: a malformed peer signature aborts the verifying process. A panic is better than the silent OOB read of #1056, but for a network-facing TLS handshake it is still a denial-of-service on attacker input.

    Validator gap. No test feeds an over-long ECDSA signature; with #1053/#1055 the ecdsa suite does not run at all.

    Related. #1056 (x509 DER content-bounds, same file, same "trust the length" class), #1055 (ecdsa test drift). No open PR.

    Recommendation — champion. Reject r_len > 32 and s_len > 32 with return 0 before computing r_off/s_off (after the zero-strip). Acceptance criteria:

    • The r_len=40 case returns 0, not a panic; the legitimate 32-byte and zero-padded-33-byte forms still verify.
    • A negative-input test (SEQ{INT(40 bytes), INT(1)}) asserts a 0 return, under test/ once the suite is repaired.
    • The dead block at :400-414 is removed.

    Blockers / not tested. Full ecdsa_verify_der_sig/TLS path not driven end to end (private unsafe fn, needs a DER fixture); the report exercised it with a stubbed ecdsa_p256_verify. The loop-level repro establishes the negative-index panic exactly. 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: ecdsa_verify_der_sig (lib/std/crypto/x509.w:448, :466) still computes let r_off = 32 - r_len (and s_off) with no r_len > 32 rejection, so a long INTEGER indexes r_bytes[r_off + ri] below zero and panics.

    pre-release: a peer-controlled signature crashes the process; rejecting > 32 is one line.

  3. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

    Fixed on main by 3f96513 (PR #2670, 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