[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH] util: virrandom: make virRandomBits endian-safe



Make the generation of random bits in virRandomBits independent of the
endianness of the running architecture.

This also solves problems with the mocked random byte generation on
big-endian machines.

Suggested-by: Daniel P. Berrangé <berrange redhat com>
Signed-off-by: Bjoern Walk <bwalk linux ibm com>
---
This goes on top of Michal's fix: https://www.redhat.com/archives/libvir-list/2018-August/msg00080.html

 src/util/virrandom.c | 6 +++++-
 1 file changed, 5 insertions(+), 1 deletion(-)

diff --git a/src/util/virrandom.c b/src/util/virrandom.c
index 7915f653..26ff68f5 100644
--- a/src/util/virrandom.c
+++ b/src/util/virrandom.c
@@ -34,6 +34,7 @@
 # include <gnutls/crypto.h>
 #endif
 
+#include "virendian.h"
 #include "virrandom.h"
 #include "virthread.h"
 #include "count-one-bits.h"
@@ -61,13 +62,16 @@ VIR_LOG_INIT("util.random");
 uint64_t virRandomBits(int nbits)
 {
     uint64_t ret = 0;
+    uint8_t val[8];
 
-    if (virRandomBytes((unsigned char *) &ret, sizeof(ret)) < 0) {
+    if (virRandomBytes((unsigned char *) &val, sizeof(val)) < 0) {
         /* You're already hosed, so this particular non-random value
          * isn't any worse.  */
         return 0;
     }
 
+    ret = virReadBufInt64LE(val);
+
     if (nbits < 64)
         ret &= (1ULL << nbits) - 1;
 
-- 
2.17.0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]