9c6c51
From f341ec3a1077c2d876ece271e5f58aa8d3ef795a Mon Sep 17 00:00:00 2001
9c6c51
Message-Id: <f341ec3a1077c2d876ece271e5f58aa8d3ef795a@dist-git>
9c6c51
From: Michal Privoznik <mprivozn@redhat.com>
9c6c51
Date: Mon, 3 Dec 2018 08:46:18 -0500
9c6c51
Subject: [PATCH] util: Don't overflow in virRandomBits
9c6c51
MIME-Version: 1.0
9c6c51
Content-Type: text/plain; charset=UTF-8
9c6c51
Content-Transfer-Encoding: 8bit
9c6c51
9c6c51
https://bugzilla.redhat.com/show_bug.cgi?id=1655586 [RHEL8]
9c6c51
https://bugzilla.redhat.com/show_bug.cgi?id=1652894 [RHEL7]
9c6c51
9c6c51
The function is supposed to return up to 64bit long integer. In
9c6c51
order to do that it calls virRandomBytes() to fill the integer
9c6c51
with random bytes and then masks out everything but requested
9c6c51
bits. However, when doing that it shifts 1U and not 1ULL. So
9c6c51
effectively, requesting 32 random bis or more always return 0
9c6c51
which is not random enough.
9c6c51
9c6c51
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
9c6c51
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
9c6c51
Reviewed-by: Pino Toscano <ptoscano@redhat.com>
9c6c51
(cherry picked from commit 78c47a92ecb450c9f8bcabd35da7006dc2547882)
9c6c51
Signed-off-by: John Ferlan <jferlan@redhat.com>
9c6c51
---
9c6c51
 src/util/virrandom.c | 2 +-
9c6c51
 1 file changed, 1 insertion(+), 1 deletion(-)
9c6c51
9c6c51
diff --git a/src/util/virrandom.c b/src/util/virrandom.c
9c6c51
index 01cc82a052..3c011a8615 100644
9c6c51
--- a/src/util/virrandom.c
9c6c51
+++ b/src/util/virrandom.c
9c6c51
@@ -68,7 +68,7 @@ uint64_t virRandomBits(int nbits)
9c6c51
         return 0;
9c6c51
     }
9c6c51
 
9c6c51
-    ret &= (1U << nbits) - 1;
9c6c51
+    ret &= (1ULL << nbits) - 1;
9c6c51
     return ret;
9c6c51
 }
9c6c51
 
9c6c51
-- 
9c6c51
2.20.1
9c6c51