Blob Blame History Raw
From cdd5c75a34654abb2516476b851c6730b4bcb11b Mon Sep 17 00:00:00 2001
Message-Id: <cdd5c75a34654abb2516476b851c6730b4bcb11b@dist-git>
From: Michal Privoznik <mprivozn@redhat.com>
Date: Mon, 21 Jan 2019 09:04:11 -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=1667329 (RHEL 7.6.z)
https://bugzilla.redhat.com/show_bug.cgi?id=1652894 (RHEL 7.7)

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>
Message-Id: <20190121140412.27804-3-jferlan@redhat.com>
Reviewed-by: Erik Skultety <eskultet@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