lib/randutils: don't break on EAGAIN, use usleep()
The current code uses lose_counter to make more attempts to read random numbers. It seems better to wait a moment between attempts to avoid busy loop (we do the same in all-io.h). The worst case is 1 second delay for all random_get_bytes() on systems with uninitialized entropy pool -- for example you call sfdisk (MBR Id or GPT UUIDs) on very first boot, etc. In this case it will use libc rand() as a fallback solution. Note that we do not use random numbers for security sensitive things like keys or so. It's used for random based UUIDs etc. Addresses: https://github.com/karelzak/util-linux/pull/603 Signed-off-by: Karel Zak <kzak@redhat.com>
This commit is contained in:
parent
6ffdfda846
commit
edc1c90cb9
|
@ -95,6 +95,9 @@ int random_get_fd(void)
|
||||||
* Use /dev/urandom if possible, and if not,
|
* Use /dev/urandom if possible, and if not,
|
||||||
* use glibc pseudo-random functions.
|
* use glibc pseudo-random functions.
|
||||||
*/
|
*/
|
||||||
|
#define UL_RAND_READ_ATTEMPTS 8
|
||||||
|
#define UL_RAND_READ_DELAY 125000 /* microseconds */
|
||||||
|
|
||||||
void random_get_bytes(void *buf, size_t nbytes)
|
void random_get_bytes(void *buf, size_t nbytes)
|
||||||
{
|
{
|
||||||
unsigned char *cp = (unsigned char *)buf;
|
unsigned char *cp = (unsigned char *)buf;
|
||||||
|
@ -111,11 +114,14 @@ void random_get_bytes(void *buf, size_t nbytes)
|
||||||
n -= x;
|
n -= x;
|
||||||
cp += x;
|
cp += x;
|
||||||
lose_counter = 0;
|
lose_counter = 0;
|
||||||
} else if (errno == ENOSYS) /* kernel without getrandom() */
|
|
||||||
|
} else if (errno == ENOSYS) { /* kernel without getrandom() */
|
||||||
break;
|
break;
|
||||||
else if (errno == EAGAIN)
|
|
||||||
break;
|
} else if (errno == EAGAIN && lose_counter < UL_RAND_READ_ATTEMPTS) {
|
||||||
else if (lose_counter++ > 16) /* entropy problem? */
|
xusleep(UL_RAND_READ_DELAY); /* no etropy, wait and try again */
|
||||||
|
lose_counter++;
|
||||||
|
} else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -134,8 +140,9 @@ void random_get_bytes(void *buf, size_t nbytes)
|
||||||
while (n > 0) {
|
while (n > 0) {
|
||||||
ssize_t x = read(fd, cp, n);
|
ssize_t x = read(fd, cp, n);
|
||||||
if (x <= 0) {
|
if (x <= 0) {
|
||||||
if (lose_counter++ > 16)
|
if (lose_counter++ > UL_RAND_READ_ATTEMPTS)
|
||||||
break;
|
break;
|
||||||
|
xusleep(UL_RAND_READ_DELAY);
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
n -= x;
|
n -= x;
|
||||||
|
|
Loading…
Reference in New Issue