Skip to content

Fix integer-overflow crash in qrng_range32/qrng_range64 - #5

Closed
RichardHoekstra wants to merge 1 commit into
tsotchke:mainfrom
RichardHoekstra:fix/qrng-range-overflow-sigfpe
Closed

RichardHoekstra wants to merge 1 commit into
tsotchke:mainfrom
RichardHoekstra:fix/qrng-range-overflow-sigfpe

Conversation

@RichardHoekstra

Copy link
Copy Markdown

Summary

qrng_range32 computes the span max - min + 1 in signed int32. At the full span (min = INT32_MIN, max = INT32_MAX) that is signed-overflow UB; at -O2 the compiler then proves range != 0 and eliminates the if (range == 0) guard, so the subsequent r % range divides by zero → SIGFPE.

qrng_range64 has the sibling bug: on the full 2⁶⁴ span the range wraps to 0 (defined, so the guard survives) and it returns the constant UINT64_MAX instead of a uniform draw.

This is the standalone-library copy of the same core shipped in Quantum-RNG-API, where the qrng_range32 bug is reachable as an unauthenticated crash via the /v1/qrng/range endpoint (Tsotchke-Corporation/Quantum-RNG-API#1). Same fix, so it can land here without a separate port.

Fix

  • qrng_range32: compute the span in 64-bit ((uint64_t)((int64_t)max - (int64_t)min) + 1), which cannot overflow; branch out the full-span case (raw draw, no modulo); unsigned-add on return to avoid residual signed overflow when min < 0.
  • qrng_range64: return qrng_uint64(ctx) on the full-span (range == 0) case.

Verification

Built and tested with the real generator (qrng_init + qrng_range*) at -O2:

  • pre-fix, qrng_range32(ctx, INT32_MIN, INT32_MAX) → SIGFPE (exit 136); post-fix → no crash, output spans negatives + positives over 3M draws;
  • pre-fix, qrng_range64(ctx, 0, UINT64_MAX) → constant 18446744073709551615; post-fix → draws vary;
  • bounded [-3..6] all buckets hit, [1..6] unaffected, range64 [100..109] in range;
  • the repo's own suite passes on the patched tree: make test (statistical + stress) and edge_cases_test ("Range Edge Cases: PASS").

Standalone reproducer available on request.

qrng_range32 computes the span (max - min + 1) in signed int32. At the
full span (min=INT32_MIN, max=INT32_MAX) this is signed-overflow UB; at
-O2 the compiler then proves range != 0 and eliminates the (range == 0)
guard, so the following r % range divides by zero -> SIGFPE.

Fix: compute the span in 64-bit (uint64_t)((int64_t)max-(int64_t)min)+1,
which cannot overflow, and branch out the full-span case (raw draw, no
modulo). Unsigned add on the return avoids residual signed overflow when
min<0.

qrng_range64 had the sibling bug: on the full 2^64 span the range wraps
to 0 (defined, so the guard survived) and it returned the constant max
instead of a uniform draw. Now returns qrng_uint64(ctx).

Verified at -O2: no crash on the full span, output covers negatives and
positives, all buckets hit, range64 full-span now varies.

(This is the standalone-library copy of the same core shipped in
Quantum-RNG-API, where the bug is reachable via /v1/qrng/range.)
@tsotchke

Copy link
Copy Markdown
Owner

Thank you, @RichardHoekstra — this is a correct and well-reasoned fix, and the analysis (signed-overflow UB at the full span, the compiler proving range != 0 at -O2 and deleting the guard → r % 0 SIGFPE, plus the qrng_range64 wrap-to-constant sibling) is exactly right.

The repository has since been rebuilt (v3, now the default master branch): a genuine 8-qubit state-vector QRNG with a live CHSH Bell test, hardware entropy, NIST SP 800-90B health tests, and a production secure_rng layer. Because that rebuild replaced src/quantum_rng/quantum_rng.c wholesale, this PR (based on main) no longer merges cleanly — but the underlying bug was carried into the v3 core, so your fix was still needed.

I've applied your exact approach (compute the span in 64-bit, branch out the full-span case as a raw draw, unsigned add to avoid residual signed-overflow UB) to master in commit c91b7d1, crediting you as co-author. Verified: qrng_range32(INT32_MIN, INT32_MAX) no longer crashes and returns uniform values, and qrng_range64(0, UINT64_MAX) now returns a real draw instead of the constant UINT64_MAX. make verify_all is green across macOS, Linux, and Windows.

Closing this PR since the fix now lives on master; main remains only as the legacy v1 branch. Really appreciate the careful report and the standalone reproducer offer. 🙏

@tsotchke tsotchke closed this Jul 10, 2026
waefrebeorn pushed a commit to waefrebeorn/quantum_rng_audit that referenced this pull request Jul 28, 2026
…hke#5 overflow fix is already present

- PR tsotchke#6 (our audit) was closed (not merged) by tsotchke: wrong target
  branch (main vs master) + stale vs his tsotchke#7 production refactor. We accept
  the close and will resubmit reconciled against master after tsotchke#7.
- PR tsotchke#5 range-overflow fix (RichardHoekstra, praised by owner) is ALREADY
  in this fork: qrng_range32 uses uint64 span + full-span guard; qrng_range64
  handles range==0. No port needed.
- Note tsotchke's real verify_all counts (26/26 health, 23/23 secure-RNG,
  8/8 thread, 18/18 v3) -- a gap in OUR evidence, not his code.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants