libmisc/salt.c: Use secure system ressources to obtain random bytes.

In a previous commit we introduced /dev/urandom as a source to obtain
random bytes from.  This may not be available on all systems, or when
operating inside of a chroot.

Almost all systems provide functions to obtain random bytes from
secure system ressources.  Thus we should prefer to use these, and
fall back to /dev/urandom, if there is no such function present, as
a last resort.

Signed-off-by: Björn Esser <besser82@fedoraproject.org>
This commit is contained in:
Björn Esser
2021-07-04 12:10:11 +02:00
parent 9eb191edc4
commit c82ed0c15e
2 changed files with 45 additions and 14 deletions

View File

@ -15,6 +15,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_SYS_RANDOM_H
#include <sys/random.h>
#endif
#include "prototypes.h"
#include "defines.h"
#include "getdef.h"
@ -128,19 +131,46 @@ static /*@observer@*/char *l64a (long value)
static long read_random_bytes (void)
{
long randval = 0;
#ifdef HAVE_ARC4RANDOM_BUF
/* arc4random_buf, if it exists, can never fail. */
arc4random_buf (&randval, sizeof (randval));
goto end;
#elif defined(HAVE_GETENTROPY)
/* getentropy may exist but lack kernel support. */
if (getentropy (&randval, sizeof (randval))) {
goto fail;
}
goto end;
#elif defined(HAVE_GETRANDOM)
/* Likewise getrandom. */
if ((size_t) getrandom (&randval, sizeof (randval), 0) != sizeof (randval)) {
goto fail;
}
goto end;
#else
FILE *f = fopen ("/dev/urandom", "r");
if (fread (&randval, sizeof (randval), 1, f) != sizeof (randval))
{
fprintf (shadow_logfd,
_("Unable to read from /dev/urandom.\n"));
if (fread (&randval, sizeof (randval), 1, f) != sizeof (randval)) {
fclose(f);
exit (1);
goto fail;
}
fclose(f);
goto end;
#endif
fail:
fprintf (shadow_logfd,
_("Unable to obtain random bytes.\n"));
exit (1);
end:
return randval;
}