Blob Blame History Raw
From 0a774bbd6c55f95a9cd106a0c79881a52b9cbdbc Mon Sep 17 00:00:00 2001
Message-Id: <0a774bbd6c55f95a9cd106a0c79881a52b9cbdbc@dist-git>
From: Michal Privoznik <mprivozn@redhat.com>
Date: Mon, 3 Dec 2018 08:46:19 -0500
Subject: [PATCH] virrandom: Avoid undefined behaviour in virRandomBits
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

https://bugzilla.redhat.com/show_bug.cgi?id=1655586 [RHEL8]
https://bugzilla.redhat.com/show_bug.cgi?id=1652894 [RHEL7]

If nbits is 64 (or greater) then shifting 1ULL left is undefined.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
(cherry picked from commit 0a5a6f0d019996b015bb0acbe30efa8f2fbbb351)
Signed-off-by: John Ferlan <jferlan@redhat.com>
---
 src/util/virrandom.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/util/virrandom.c b/src/util/virrandom.c
index 3c011a8615..7915f6531e 100644
--- a/src/util/virrandom.c
+++ b/src/util/virrandom.c
@@ -68,7 +68,9 @@ uint64_t virRandomBits(int nbits)
         return 0;
     }
 
-    ret &= (1ULL << nbits) - 1;
+    if (nbits < 64)
+        ret &= (1ULL << nbits) - 1;
+
     return ret;
 }
 
-- 
2.20.1