Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

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
  • Loading branch information...
commit 7918d779d8aeb069b5be1d6901d96574ed6dd83c 1 parent 72e717d
Sascha Wildner authored
Showing with 9 additions and 2 deletions.
  1. +9 −2 lib/libc/stdlib/rand.c
View
11 lib/libc/stdlib/rand.c
@@ -35,6 +35,7 @@
#include "namespace.h"
#include <sys/time.h> /* for sranddev() */
#include <sys/types.h>
+#include <sys/sysctl.h>
#include <fcntl.h> /* for sranddev() */
#include <stdlib.h>
#include <unistd.h> /* for sranddev() */
@@ -116,11 +117,17 @@ sranddev(void)
}
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);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.