From 5c1ea1fcd01c9be610c82e8f554edbc2456493e1 Mon Sep 17 00:00:00 2001 From: Ed Maste Date: Mon, 3 Oct 2016 13:12:44 +0000 Subject: [PATCH] libc arc4_stir: use only kern.arandom sysctl The sysctl cannot fail. If it does fail on some FreeBSD derivative or after some future change, just abort() so that the problem will be found and fixed. It's preferable to provide an arc4random() function that cannot fail and cannot return poor quality random data. While abort() is not normally suitable for a library, it makes sense here. Reviewed by: ed, jonathan, markm MFC after: 1 month Sponsored by: The FreeBSD Foundation Differential Revision: https://reviews.freebsd.org/D8077 --- lib/libc/gen/arc4random.c | 28 +++++----------------------- 1 file changed, 5 insertions(+), 23 deletions(-) diff --git a/lib/libc/gen/arc4random.c b/lib/libc/gen/arc4random.c index a22e290006f5..0fa3dff75cc9 100644 --- a/lib/libc/gen/arc4random.c +++ b/lib/libc/gen/arc4random.c @@ -137,35 +137,17 @@ arc4_sysctl(u_char *buf, size_t size) static void arc4_stir(void) { - int done, fd, i; - struct { - struct timeval tv; - pid_t pid; - u_char rnd[KEYSIZE]; - } rdat; + u_char rdat[KEYSIZE]; + int i; if (!rs_initialized) { arc4_init(); rs_initialized = 1; } - done = 0; - if (arc4_sysctl((u_char *)&rdat, KEYSIZE) == KEYSIZE) - done = 1; - if (!done) { - fd = _open(RANDOMDEV, O_RDONLY | O_CLOEXEC, 0); - if (fd >= 0) { - if (_read(fd, &rdat, KEYSIZE) == KEYSIZE) - done = 1; - (void)_close(fd); - } - } - if (!done) { - (void)gettimeofday(&rdat.tv, NULL); - rdat.pid = getpid(); - /* We'll just take whatever was on the stack too... */ - } + if (arc4_sysctl(rdat, KEYSIZE) != KEYSIZE) + abort(); /* Random sysctl cannot fail. */ - arc4_addrandom((u_char *)&rdat, KEYSIZE); + arc4_addrandom(rdat, KEYSIZE); /* * Discard early keystream, as per recommendations in: