Repository navigation
Fix integer-overflow crash in qrng_range32/qrng_range64 - #5
RichardHoekstra wants to merge 1 commit into
Conversation
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.)
|
Thank you, @RichardHoekstra — this is a correct and well-reasoned fix, and the analysis (signed-overflow UB at the full span, the compiler proving The repository has since been rebuilt (v3, now the default 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 Closing this PR since the fix now lives on |
…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.
Summary
qrng_range32computes the spanmax - min + 1in signedint32. At the full span (min = INT32_MIN, max = INT32_MAX) that is signed-overflow UB; at-O2the compiler then provesrange != 0and eliminates theif (range == 0)guard, so the subsequentr % rangedivides by zero → SIGFPE.qrng_range64has the sibling bug: on the full 2⁶⁴ span the range wraps to0(defined, so the guard survives) and it returns the constantUINT64_MAXinstead of a uniform draw.This is the standalone-library copy of the same core shipped in Quantum-RNG-API, where the
qrng_range32bug is reachable as an unauthenticated crash via the/v1/qrng/rangeendpoint (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 whenmin < 0.qrng_range64: returnqrng_uint64(ctx)on the full-span (range == 0) case.Verification
Built and tested with the real generator (
qrng_init+qrng_range*) at-O2:qrng_range32(ctx, INT32_MIN, INT32_MAX)→SIGFPE(exit 136); post-fix → no crash, output spans negatives + positives over 3M draws;qrng_range64(ctx, 0, UINT64_MAX)→ constant18446744073709551615; post-fix → draws vary;[-3..6]all buckets hit,[1..6]unaffected,range64 [100..109]in range;make test(statistical + stress) andedge_cases_test("Range Edge Cases: PASS").Standalone reproducer available on request.