From: Sascha Wildner Date: Tue, 25 Feb 2014 22:50:13 +0000 (+0100) Subject: rand(3): Fix up sranddev(3) a bit better. X-Git-Tag: v3.9.0~457 X-Git-Url: https://gitweb.dragonflybsd.org/~tuxillo/dragonfly.git/commitdiff_plain/7918d779d8aeb069b5be1d6901d96574ed6dd83c rand(3): Fix up sranddev(3) a bit better. In case we can't read /dev/random for the seed, try kern.random. If that fails, use the getpid()/gettimeofday() xor. For the latter, remove the usage of an uninitialized variable. We should probably just abort() when kern.random can't be read. Discussed-with: dillon --- diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c index aaf73d4ee9..8b36e0988c 100644 --- a/lib/libc/stdlib/rand.c +++ b/lib/libc/stdlib/rand.c @@ -35,6 +35,7 @@ #include "namespace.h" #include /* for sranddev() */ #include +#include #include /* for sranddev() */ #include #include /* for sranddev() */ @@ -115,12 +116,18 @@ sranddev(void) _close(fd); } + if (!done) { + size_t len = sizeof(next); + + if (sysctlbyname("kern.random", &next, &len, NULL, 0) == 0) + done = 1; + } + if (!done) { struct timeval tv; - unsigned long junk; /* XXX left uninitialized on purpose */ gettimeofday(&tv, NULL); - srand((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec ^ junk); + srand((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec); } }