Skip to content

Commit

Permalink
lib/randutils: Do not block on getrandom()
Browse files Browse the repository at this point in the history
In Endless we have hit a problem when using 'sfdisk' on the really first
boot to automatically expand the rootfs partition. On this platform
'sfdisk' is blocking on getrandom() because not enough random bytes are
available. This is an ARM platform without a hwrng.

We fix this passing GRND_NONBLOCK to getrandom(). 'sfdisk' will use the
best entropy it has available and fallback only as necessary.

Signed-off-by: Carlo Caione <[email protected]>
  • Loading branch information
Carlo Caione authored and karelzak committed Mar 19, 2018
1 parent 655776a commit a9cf659
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion lib/randutils.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,8 @@

#if !defined(HAVE_GETRANDOM) && defined(SYS_getrandom)
/* libc without function, but we have syscal */
#define GRND_NONBLOCK 0x01
#define GRND_RANDOM 0x02
static int getrandom(void *buf, size_t buflen, unsigned int flags)
{
return (syscall(SYS_getrandom, buf, buflen, flags));
Expand Down Expand Up @@ -104,13 +106,15 @@ void random_get_bytes(void *buf, size_t nbytes)
int x;

errno = 0;
x = getrandom(cp, n, 0);
x = getrandom(cp, n, GRND_NONBLOCK);
if (x > 0) { /* success */
n -= x;
cp += x;
lose_counter = 0;
} else if (errno == ENOSYS) /* kernel without getrandom() */
break;
else if (errno == EAGAIN)
break;
else if (lose_counter++ > 16) /* entropy problem? */
break;
}
Expand Down

0 comments on commit a9cf659

Please sign in to comment.