Skip to content

[crypto] bigint i31_from_monty fixed [u32;80] temps overflow for moduli above 2449 bits (RSA-3072/4096 admitted by callers) #1052

Closed

Description

@eas4ai

Summary

i31_from_monty in lib/std/crypto/bigint.w uses fixed var one/d: [u32; 80] temps, but i31_montmul writes d[0..mlen] and the copy-back reads d[0..mlen] with mlen = (bitlen+30)/31. Moduli above 2449 bits (mlen >= 80: RSA-3072 needs 100 words, RSA-4096 needs 134) write/read past the arrays. In-repo callers admit such keys: lib/std/crypto/rsa.w:108-109 allows n_len up to 512 bytes (4096 bits), and lib/std/crypto/x509.w:359-377 forwards arbitrary issuer moduli.

Evidence (executed at 450733e)

Self-contained probes inlining the module's i31 code verbatim:

Unaffected: P-256 paths (ec.w, ecdsa.w, 12-word buffers for 9-word values) and RSA <= 2048 (68 words). Landed intent (613be15 "RSA-1024 test vector") sized the temp for <= 2048-bit keys; larger keys were never covered.

Notes (not applied; audit is read-only)

Needs a source-level decision, independent of #1049/#1051: size the temps for the max admitted key (134 words for RSA-4096), reject oversize moduli loudly at the rsa/x509 boundary, or thread caller-sized temps. Whichever is chosen, the tests below must cover it.

Activity

  1. ehartford commented on Sep 16, 2026

    @ehartford
    Contributor

    Review: reproduced

    Verdict. Established, and it is a genuine source-level defect independent of #1049/#1051 (it survives fixing them). i31_from_monty allocates fixed var one: [u32; 80] and var d: [u32; 80] temps, but indexes them by mlen = (bitlen + 30) / 31, which reaches 100 for RSA-3072 and ~134 for RSA-4096. For any modulus with mlen >= 80 (bit length >= 2450), i31_montmul writes past d[79] through raw pointers (silent stack corruption), and the indexed copy-back d[ci] at bigint.w:224 then panics index out of bounds. In-repo callers admit such keys.

    Tested.

    Source main @ 5809ac1 (2026-09-14); bigint.w/rsa.w/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

    Faithful reproduction of the copy-back at bigint.w:224 (indexed d[ci], d sized [u32; 80]), varying mlen:

    var d: [u32; 80] = [0u32; 80]
    var x: [u32; 200] = [0u32; 200]
    var ci = 0
    while ci <= MLEN:
        x[ci] = d[ci]
        ci = ci + 1
    
    MLEN modulus bits result
    79 2419..2449 completes (in-bounds; d[0..79])
    80 2450..2480 panic: index out of bounds
    100 RSA-3072 panic: index out of bounds

    The boundary is exactly mlen = 80 on both compilers, i.e. bitlen >= 2450, matching the report's 2450-bit and 2480-bit panics and its 2449-bit (mlen 79) non-panic. Before the bounds-checked copy-back panics, i31_montmul's *(d + j - 1) / *(d + mlen) writes (bigint.w:191-194) have already run past d[79] unchecked, so the true fault is a silent stack overwrite; the panic is the first visible symptom.

    Source pins (5809ac1).

    • Undersized temps and mlen-indexed copy-back: lib/std/crypto/bigint.w:213-225 (var one/d: [u32; 80]; while ci <= mlen: *(x + ci) = d[ci]).
    • mlen formula: bigint.w:10-12 ((bitlen + 30) / 31).
    • Callers admit oversize moduli: rsa_pkcs1_sha256_verify allows n_len up to 512 bytes = 4096 bits (rsa.w:108), and x509_verify_signature forwards the issuer's key_n/key_n_len unchanged into it (x509.w:372-373).

    Note on scope. The modpow scratch buffers rsa.w allocates (m/x/t1/t2 at [u32; 140]) are correctly sized for RSA-4096; the defect is only in i31_from_monty's own [u32; 80] temps. So the outer path is ready for 4096-bit keys but this one function is not. This is the sub-bug I flagged in my #1051 review, filed here as its own issue as the report intends.

    Root cause. Fixed temp size chosen for <= 2048-bit keys (per 613be15's RSA-1024 vector) while callers permit up to 4096-bit. No debugger needed; the array size and the mlen index are both in source and the panic boundary matches the arithmetic.

    Validator gap. No crypto self-test exercises a modulus above 2048 bits, so this and #1051 are both green in CI while RSA-3072/4096 verification faults.

    Related. #1049/#1050 (codegen) and #1051 (the mlen>=20 wrongness) are separate; this overflow is orthogonal and would remain after all of them are fixed. No open PR.

    Recommendation — champion. It is a memory-safety defect (unchecked raw writes past a stack array) reachable from certificate verification with an oversize issuer key, so it should not be left as a panic-if-lucky. Pick one, per the report:

    • Size the temps to the maximum admitted key (134 words for RSA-4096), ideally from a shared MAX_LIMBS constant that rsa.w's [u32; 140] also derives from; or
    • Reject mlen > SUPPORTED_MAX loudly at the rsa/x509 boundary before calling into i31_*; or
    • Thread caller-sized scratch into i31_from_monty instead of stack temps.

    Acceptance criteria:

    • RSA-3072 and RSA-4096 verification either succeed against oracle vectors or fail with an explicit "unsupported key size" error, never a silent overwrite or a raw panic.
    • A test/ case verifies a 3072-bit and a 4096-bit signature (or asserts the loud rejection).
    • The [u32; 80] temps and the [u32; 140] caller buffers derive from one constant so they cannot drift.

    Blockers / not tested. i31_from_monty, rsa_pkcs1_sha256_verify, and x509_verify_signature are private unsafe fn; I reproduced the copy-back overflow faithfully at the array level but did not drive a full RSA-3072 verification from a public entry point (the report inlined the module to do so). 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: i31_from_monty (lib/std/crypto/bigint.w:215, :219) still declares var one: [u32; 80] and var d: [u32; 80] and indexes them up to mlen = (bitlen + 30) / 31, which is 100 for RSA-3072 and 134 for RSA-4096; rsa.w admits keys up to 512 bytes.

    pre-release: ordinary RSA-3072/4096 keys hit an out-of-bounds panic or a stack overrun in std; sizing the temps from mlen is small.

  3. ehartford commented on Oct 25, 2026

    @ehartford
    Contributor

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