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

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

 



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@xxxxxxxxxx>
Signed-off-by: Bjoern Walk <bwalk@xxxxxxxxxxxxx>
---
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

--
libvir-list mailing list
libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list




[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux