[libvirt] [PATCH] util: Make sure random data is initialized when in virRandom

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



When writing some tests, I mistakenly attempted to auto-generate a UUID,
which caused a segfault: virRandom was being used without calling
virRandomInitialize. Make sure this case can't happen.

Signed-off-by: Cole Robinson <crobinso@xxxxxxxxxx>
---
 src/util/util.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)

diff --git a/src/util/util.c b/src/util/util.c
index e5135fc..1554097 100644
--- a/src/util/util.c
+++ b/src/util/util.c
@@ -1910,6 +1910,7 @@ int virKillProcess(pid_t pid, int sig)
 }
 
 
+static int random_initialized = 0;
 static char randomState[128];
 static struct random_data randomData;
 static virMutex randomLock;
@@ -1925,6 +1926,7 @@ int virRandomInitialize(unsigned int seed)
                     &randomData) < 0)
         return -1;
 
+    random_initialized = 1;
     return 0;
 }
 
@@ -1932,6 +1934,12 @@ int virRandom(int max)
 {
     int32_t ret;
 
+    if (!random_initialized) {
+        /* This can error, but what's worse? Unnoticed bogus random data or
+        *  a segfault? */
+        virRandomInitialize(time(NULL) ^ getpid());
+    }
+
     virMutexLock(&randomLock);
     random_r(&randomData, &ret);
     virMutexUnlock(&randomLock);
-- 
1.6.5.rc2

--
Libvir-list mailing list
Libvir-list@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list

[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]