8d8641
From cdd5c75a34654abb2516476b851c6730b4bcb11b Mon Sep 17 00:00:00 2001
8d8641
Message-Id: <cdd5c75a34654abb2516476b851c6730b4bcb11b@dist-git>
8d8641
From: Michal Privoznik <mprivozn@redhat.com>
8d8641
Date: Mon, 21 Jan 2019 09:04:11 -0500
8d8641
Subject: [PATCH] virrandom: Avoid undefined behaviour in virRandomBits
8d8641
MIME-Version: 1.0
8d8641
Content-Type: text/plain; charset=UTF-8
8d8641
Content-Transfer-Encoding: 8bit
8d8641
8d8641
https://bugzilla.redhat.com/show_bug.cgi?id=1667329 (RHEL 7.6.z)
8d8641
https://bugzilla.redhat.com/show_bug.cgi?id=1652894 (RHEL 7.7)
8d8641
8d8641
If nbits is 64 (or greater) then shifting 1ULL left is undefined.
8d8641
8d8641
Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
8d8641
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
8d8641
(cherry picked from commit 0a5a6f0d019996b015bb0acbe30efa8f2fbbb351)
8d8641
Signed-off-by: John Ferlan <jferlan@redhat.com>
8d8641
Message-Id: <20190121140412.27804-3-jferlan@redhat.com>
8d8641
Reviewed-by: Erik Skultety <eskultet@redhat.com>
8d8641
---
8d8641
 src/util/virrandom.c | 4 +++-
8d8641
 1 file changed, 3 insertions(+), 1 deletion(-)
8d8641
8d8641
diff --git a/src/util/virrandom.c b/src/util/virrandom.c
8d8641
index 3c011a8615..7915f6531e 100644
8d8641
--- a/src/util/virrandom.c
8d8641
+++ b/src/util/virrandom.c
8d8641
@@ -68,7 +68,9 @@ uint64_t virRandomBits(int nbits)
8d8641
         return 0;
8d8641
     }
8d8641
 
8d8641
-    ret &= (1ULL << nbits) - 1;
8d8641
+    if (nbits < 64)
8d8641
+        ret &= (1ULL << nbits) - 1;
8d8641
+
8d8641
     return ret;
8d8641
 }
8d8641
 
8d8641
-- 
8d8641
2.20.1
8d8641