From 3892cd565f835c129f8ad47a5068be57bccfe40d Mon Sep 17 00:00:00 2001 From: Richard Hoekstra Date: Tue, 7 Jul 2026 16:59:28 +0200 Subject: [PATCH] Fix integer-overflow crash in qrng_range32/qrng_range64 qrng_range32 computed 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 subsequent r % range divides by zero and the process takes SIGFPE. Because /v1/qrng/range passes caller-supplied min/max straight through, a single request at the full span crashes the server. 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 (return a raw draw, no modulo). Unsigned add on the return avoids the 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. --- src/quantum_rng/quantum_rng.c | 30 ++++++++++++++++++++---------- 1 file changed, 20 insertions(+), 10 deletions(-) diff --git a/src/quantum_rng/quantum_rng.c b/src/quantum_rng/quantum_rng.c index d711a9e..cf5096f 100644 --- a/src/quantum_rng/quantum_rng.c +++ b/src/quantum_rng/quantum_rng.c @@ -401,20 +401,28 @@ int32_t qrng_range32(qrng_ctx *ctx, int32_t min, int32_t max) { if (!ctx || min > max) { return max; } - - uint32_t range = (uint32_t)(max - min + 1); - if (range == 0) { - return max; + + /* Span in [1, 2^32]. Compute in 64-bit signed so (max - min + 1) can + never overflow: the original int32 form is signed-overflow UB at the + full span, and at -O2 the compiler then PROVES range != 0 and deletes + the (range == 0) guard below -- leaving r % 0 (SIGFPE). */ + uint64_t span = (uint64_t)((int64_t)max - (int64_t)min) + 1; + + if (span > 0xFFFFFFFFULL) { + /* Full 32-bit span: every uint32 is valid, no rejection or modulo. */ + return (int32_t)((uint32_t)min + (uint32_t)qrng_uint64(ctx)); } - - uint32_t threshold = (uint32_t)-range % range; + + uint32_t range = (uint32_t)span; /* guaranteed nonzero */ + uint32_t threshold = (0u - range) % range; /* == 2^32 % range, unbiased */ uint32_t r; - + do { r = (uint32_t)qrng_uint64(ctx); } while (r < threshold); - - return min + (r % range); + + /* Unsigned add avoids signed-overflow UB when min<0 and result near max. */ + return (int32_t)((uint32_t)min + (r % range)); } uint64_t qrng_range64(qrng_ctx *ctx, uint64_t min, uint64_t max) { @@ -428,7 +436,9 @@ uint64_t qrng_range64(qrng_ctx *ctx, uint64_t min, uint64_t max) { uint64_t range = max - min + 1; if (range == 0) { - return max; + /* range wrapped => full 2^64 span (min==0, max==UINT64_MAX): every + uint64 is valid. Return a raw draw, not the constant max. */ + return qrng_uint64(ctx); } uint64_t threshold = -range % range;