* 'master' of https://github.com/RAOF/util-linux:
  lib/randutils.c: More paranoia in getrandom() call.
  lib/randutils.c: Fall back gracefully when kernel doesn't support getrandom(2).
This commit is contained in:
Karel Zak 2017-08-10 10:26:21 +02:00
commit 959b9250d8
1 changed files with 29 additions and 21 deletions

View File

@ -99,32 +99,40 @@ void random_get_bytes(void *buf, size_t nbytes)
unsigned char *cp = (unsigned char *)buf;
#ifdef HAVE_GETRANDOM
while (getrandom(buf, nbytes, 0) < 0) {
errno = 0;
while (getrandom(buf, nbytes, 0) != (ssize_t)nbytes) {
if (errno == EINTR)
continue;
break;
}
#else
size_t n = nbytes;
int fd = random_get_fd();
int lose_counter = 0;
if (fd >= 0) {
while (n > 0) {
ssize_t x = read(fd, cp, n);
if (x <= 0) {
if (lose_counter++ > 16)
break;
continue;
}
n -= x;
cp += x;
lose_counter = 0;
}
close(fd);
}
if (errno == ENOSYS)
/*
* We've been built against headers that support getrandom,
* but the running kernel does not.
* Fallback to reading from /dev/{u,}random as before
*/
#endif
{
size_t n = nbytes;
int fd = random_get_fd();
int lose_counter = 0;
if (fd >= 0) {
while (n > 0) {
ssize_t x = read(fd, cp, n);
if (x <= 0) {
if (lose_counter++ > 16)
break;
continue;
}
n -= x;
cp += x;
lose_counter = 0;
}
close(fd);
}
}
/*
* We do this all the time, but this is the only source of
* randomness if /dev/random/urandom is out to lunch.