You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[crypto] bigint i31_from_monty fixed [u32;80] temps overflow for moduli above 2449 bits (RSA-3072/4096 admitted by callers) #1052
Self-contained probes inlining the module's i31 code verbatim:
2480-bit and 2450-bit modpow probes die with panic: index out of bounds (the indexed d[ci] copy-back at :224 fires only after raw-pointer writes have already run past the buffer).
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.
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.
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.
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.
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.
Activity
ehartford commentedon Sep 16, 2026
Review: reproduced
Verdict. Established, and it is a genuine source-level defect independent of #1049/#1051 (it survives fixing them).
i31_from_montyallocates fixedvar one: [u32; 80]andvar d: [u32; 80]temps, but indexes them bymlen = (bitlen + 30) / 31, which reaches 100 for RSA-3072 and ~134 for RSA-4096. For any modulus withmlen >= 80(bit length >= 2450),i31_montmulwrites pastd[79]through raw pointers (silent stack corruption), and the indexed copy-backd[ci]atbigint.w:224then panicsindex out of bounds. In-repo callers admit such keys.Tested.
main@ 5809ac1 (2026-09-14);bigint.w/rsa.w/x509.wunchanged since the reported revision 450733ewith v0.15.2.1built from 5809ac1 (staging worktree); a compiler built from 450733e with the nightly-20260903 seed. Same panicFaithful reproduction of the copy-back at
bigint.w:224(indexedd[ci],dsized[u32; 80]), varyingmlen:d[0..79])panic: index out of boundspanic: index out of boundsThe boundary is exactly
mlen = 80on 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 pastd[79]unchecked, so the true fault is a silent stack overwrite; the panic is the first visible symptom.Source pins (5809ac1).
lib/std/crypto/bigint.w:213-225(var one/d: [u32; 80];while ci <= mlen: *(x + ci) = d[ci]).mlenformula:bigint.w:10-12((bitlen + 30) / 31).rsa_pkcs1_sha256_verifyallowsn_lenup to 512 bytes = 4096 bits (rsa.w:108), andx509_verify_signatureforwards the issuer'skey_n/key_n_lenunchanged into it (x509.w:372-373).Note on scope. The modpow scratch buffers
rsa.wallocates (m/x/t1/t2at[u32; 140]) are correctly sized for RSA-4096; the defect is only ini31_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:
MAX_LIMBSconstant thatrsa.w's[u32; 140]also derives from; ormlen > SUPPORTED_MAXloudly at thersa/x509boundary before calling intoi31_*; ori31_from_montyinstead of stack temps.Acceptance criteria:
test/case verifies a 3072-bit and a 4096-bit signature (or asserts the loud rejection).[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, andx509_verify_signatureare privateunsafe 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.ehartford commentedon Oct 25, 2026
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 declaresvar one: [u32; 80]andvar d: [u32; 80]and indexes them up tomlen = (bitlen + 30) / 31, which is 100 for RSA-3072 and 134 for RSA-4096;rsa.wadmits 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
mlenis small.std.crypto: every i31 temp holds a 4096-bit modulus, and RSA-4096 has…
ehartford commentedon Oct 25, 2026
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.