[libvirt] [PATCH for v4.6.0 0/2] Fix some random problems

Bjoern Walk bwalk at linux.ibm.com
Wed Aug 1 12:57:07 UTC 2018


And here's the fix for the viriscsitest on big-endian machine like
Daniel suggested.

Bjoern

-- 
IBM Systems
Linux on Z & Virtualization Development
------------------------------------------------------------------------
IBM Deutschland Research & Development GmbH
Schönaicher Str. 220, 71032 Böblingen
Phone: +49 7031 16 1819
------------------------------------------------------------------------
Vorsitzende des Aufsichtsrats: Martina Koederitz
Geschäftsführung: Dirk Wittkopp
Sitz der Gesellschaft: Böblingen
Registergericht: Amtsgericht Stuttgart, HRB 243294 
-------------- next part --------------
From d59b254294a90c5a9ca0fb6ad29465cd0950bb61 Mon Sep 17 00:00:00 2001
From: Bjoern Walk <bwalk at linux.ibm.com>
Date: Wed, 1 Aug 2018 14:48:47 +0200
Subject: [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 at redhat.com>
Signed-off-by: Bjoern Walk <bwalk at linux.ibm.com>
---
 src/util/virrandom.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/src/util/virrandom.c b/src/util/virrandom.c
index 01cc82a0..a58ee97a 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"
@@ -60,16 +61,15 @@ VIR_LOG_INIT("util.random");
  */
 uint64_t virRandomBits(int nbits)
 {
-    uint64_t ret = 0;
+    uint8_t ret[8];
 
-    if (virRandomBytes((unsigned char *) &ret, sizeof(ret)) < 0) {
+    if (virRandomBytes(ret, sizeof(ret)) < 0) {
         /* You're already hosed, so this particular non-random value
          * isn't any worse.  */
         return 0;
     }
 
-    ret &= (1ULL << nbits) - 1;
-    return ret;
+    return virReadBufInt64LE(ret) & ((1ULL << nbits) - 1);
 }
 
 
-- 
2.17.0

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 902 bytes
Desc: not available
URL: <http://listman.redhat.com/archives/libvir-list/attachments/20180801/a3b20e42/attachment-0001.sig>


More information about the libvir-list mailing list