aboutsummaryrefslogtreecommitdiff
path: root/lib/libc/stdlib/rand.c
diff options
context:
space:
mode:
authorXin LI <delphij@FreeBSD.org>2013-04-02 23:41:20 +0000
committerXin LI <delphij@FreeBSD.org>2013-04-02 23:41:20 +0000
commit12a6865090ad2f88c671256c53a8fed4d5424424 (patch)
treeef793ecd9ffc2cadc85db8ff811a6c2015c943cd /lib/libc/stdlib/rand.c
parentc6a453a430c67cc9d2d796721c1cc34b1d619dca (diff)
downloadsrc-12a6865090ad2f88c671256c53a8fed4d5424424.tar.gz
src-12a6865090ad2f88c671256c53a8fed4d5424424.zip
Replace access to /dev/random with the kernel pseudo-random number
source sysctl(KERN_ARND) and remove the fallback code. Obtained from: OpenBSD Reviewed by: secteam MFC after: 1 month
Notes
Notes: svn path=/head/; revision=249035
Diffstat (limited to 'lib/libc/stdlib/rand.c')
-rw-r--r--lib/libc/stdlib/rand.c33
1 files changed, 12 insertions, 21 deletions
diff --git a/lib/libc/stdlib/rand.c b/lib/libc/stdlib/rand.c
index 704181890bb7..0cbd94883125 100644
--- a/lib/libc/stdlib/rand.c
+++ b/lib/libc/stdlib/rand.c
@@ -36,11 +36,10 @@ static char sccsid[] = "@(#)rand.c 8.1 (Berkeley) 6/14/93";
__FBSDID("$FreeBSD$");
#include "namespace.h"
-#include <sys/time.h> /* for sranddev() */
+#include <sys/param.h>
+#include <sys/sysctl.h>
#include <sys/types.h>
-#include <fcntl.h> /* for sranddev() */
#include <stdlib.h>
-#include <unistd.h> /* for sranddev() */
#include "un-namespace.h"
#ifdef TEST
@@ -112,28 +111,20 @@ u_int seed;
* sranddev:
*
* Many programs choose the seed value in a totally predictable manner.
- * This often causes problems. We seed the generator using the much more
- * secure random(4) interface.
+ * This often causes problems. We seed the generator using pseudo-random
+ * data from the kernel.
*/
void
sranddev()
{
- int fd, done;
-
- done = 0;
- fd = _open("/dev/random", O_RDONLY | O_CLOEXEC, 0);
- if (fd >= 0) {
- if (_read(fd, (void *) &next, sizeof(next)) == sizeof(next))
- done = 1;
- _close(fd);
- }
-
- if (!done) {
- struct timeval tv;
-
- gettimeofday(&tv, NULL);
- srand((getpid() << 16) ^ tv.tv_sec ^ tv.tv_usec);
- }
+ int mib[2];
+ size_t len;
+
+ len = sizeof(next);
+
+ mib[0] = CTL_KERN;
+ mib[1] = KERN_ARND;
+ sysctl(mib, 2, (void *)&next, &len, NULL, 0);
}