]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: Don't overflow in virRandomBits
authorMichal Privoznik <mprivozn@redhat.com>
Wed, 1 Aug 2018 11:26:46 +0000 (13:26 +0200)
committerMichal Privoznik <mprivozn@redhat.com>
Wed, 1 Aug 2018 13:07:19 +0000 (15:07 +0200)
The function is supposed to return up to 64bit long integer. In
order to do that it calls virRandomBytes() to fill the integer
with random bytes and then masks out everything but requested
bits. However, when doing that it shifts 1U and not 1ULL. So
effectively, requesting 32 random bis or more always return 0
which is not random enough.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
Reviewed-by: Pino Toscano <ptoscano@redhat.com>
src/util/virrandom.c

index 01cc82a0528e63487040411b3fb2f7df8c59287e..3c011a861501397172d495a0ebdc453f1bfbd55c 100644 (file)
@@ -68,7 +68,7 @@ uint64_t virRandomBits(int nbits)
         return 0;
     }
 
-    ret &= (1U << nbits) - 1;
+    ret &= (1ULL << nbits) - 1;
     return ret;
 }