From: Mark Wooding Date: Thu, 26 May 2016 08:26:09 +0000 (+0100) Subject: rand/noise.c (noise_devrandom): Handle Linux's broken `/dev/urandom'. X-Git-Tag: 2.2.3~1^2~7 X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~mdw/git/catacomb/commitdiff_plain/ecc296ebb74bb17cfb9998972a4bc28b91e82da0 rand/noise.c (noise_devrandom): Handle Linux's broken `/dev/urandom'. On Linux, try to open `/dev/random' and make sure it's readable before proceeding to `/dev/urandom'. Generally we want to be reading `/dev/urandom', but not if it hasn't been initialized properly. --- diff --git a/rand/noise.c b/rand/noise.c index 5421bc10..6458f92d 100644 --- a/rand/noise.c +++ b/rand/noise.c @@ -162,6 +162,21 @@ int noise_devrandom(rand_pool *r) ssize_t len; size_t n = 0; int ret = 0; +#ifdef __linux__ + fd_set infd; + struct timeval tv = { 0, 0 }; +#endif + +#ifdef __linux__ + /* --- Don't take from `/dev/urandom' if `/dev/random' would block --- */ + + if ((fd = open("/dev/random", O_RDONLY | O_NONBLOCK)) < 0) goto done; + FD_ZERO(&infd); + FD_SET(fd, &infd); + if (select(fd + 1, &infd, 0, 0, &tv) < 0 || !FD_ISSET(fd, &infd)) + goto done; + close(fd); fd = -1; +#endif /* --- Be nice to other clients of the random device --- * *