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

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@redhat.com> --- 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

On Mon, Oct 12, 2009 at 10:32:14AM -0400, Cole Robinson wrote:
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.
I don't much like this as an approach - it would be nicer to put it into the tests/testutil.c file where we already call the virThreadInitialize & virErrorInitialize functions Daniel -- |: Red Hat, Engineering, London -o- http://people.redhat.com/berrange/ :| |: http://libvirt.org -o- http://virt-manager.org -o- http://ovirt.org :| |: http://autobuild.org -o- http://search.cpan.org/~danberr/ :| |: GnuPG: 7D3B9505 -o- F3C9 553F A1DA 4AC2 5648 23C1 B3DF F742 7D3B 9505 :|

Daniel P. Berrange wrote:
On Mon, Oct 12, 2009 at 10:32:14AM -0400, Cole Robinson wrote:
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.
I don't much like this as an approach - it would be nicer to put it into the tests/testutil.c file where we already call the virThreadInitialize & virErrorInitialize functions
I've sent a new patch to address this. Thanks, Cole
participants (2)
-
Cole Robinson
-
Daniel P. Berrange