Fail hard if /dev/urandom and /dev/random is not available

CVE-2013-4442

Addresses-Debian-Bug: #767008
Addresses-Launchpad-Bug: #1183213

Signed-off-by: Theodore Ts'o <tytso@mit.edu>
This commit is contained in:
Theodore Ts'o 2014-10-27 21:38:19 -04:00
parent 8a2172c8d4
commit ccda6f21c6
2 changed files with 5 additions and 23 deletions

View File

@ -6,6 +6,6 @@ AC_PATH_PROG(MV, mv, mv)
AC_PATH_PROG(RM, rm, rm)
AC_PATH_PROG(SED, sed, sed)
AC_PATH_PROG(PERL, perl, perl)
AC_CHECK_FUNCS(drand48 getopt_long)
AC_CHECK_FUNCS(getopt_long)
AC_CHECK_HEADERS(getopt.h)
AC_OUTPUT(Makefile)

View File

@ -7,6 +7,7 @@
* License.
*/
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
@ -28,29 +29,13 @@ static int get_random_fd()
{
struct timeval tv;
static int fd = -2;
int i;
if (fd == -2) {
gettimeofday(&tv, 0);
fd = open("/dev/urandom", O_RDONLY);
if (fd == -1)
fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
#ifdef HAVE_DRAND48
srand48((tv.tv_sec<<9) ^ (getpgrp()<<15) ^
(getpid()) ^ (tv.tv_usec>>11));
#else
srandom((getpid() << 16) ^ (getpgrp() << 8) ^ getuid()
^ tv.tv_sec ^ tv.tv_usec);
#endif
}
/* Crank the random number generator a few times */
gettimeofday(&tv, 0);
for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
#ifdef HAVE_DRAND48
drand48();
#else
random();
#endif
return fd;
}
@ -85,11 +70,8 @@ int pw_random_number(max_num)
if (nbytes == 0)
return (rand_num % max_num);
/* OK, we weren't able to use /dev/random, fall back to rand/rand48 */
/* We weren't able to use /dev/random, fail hard */
#ifdef HAVE_DRAND48
return ((int) ((drand48() * max_num)));
#else
return ((int) (random() / ((float) RAND_MAX) * max_num));
#endif
fprintf(stderr, "No entropy available!\n");
exit(1);
}