seedrng: incorporate various improvements

There have been a number of subtle improvements and cleanups to seedrng,
including using openat and locking the directory fd instead of a
separate lock file. Also various stylistic cleanups.

This fixes #519.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
This commit is contained in:
Jason A. Donenfeld 2022-04-20 02:44:21 +02:00 committed by William Hubbs
parent c3545b54f3
commit 9b16bfe0e7
3 changed files with 148 additions and 126 deletions

View File

@ -1,17 +1,14 @@
# The convention in this file is to show the default settings # The convention in this file is to show the default settings commented out.
# commented out.
# #
# Sometimes you want to have urandom start before "localmount" # Sometimes you want to have urandom start before "localmount" (say for crypt
# (say for crypt swap), so you will need to customize this # swap), so you will need to customize this behavior. If you have /var on a
# behavior. If you have /var on a separate partition, make sure paths # separate partition, make sure paths under /var are adjusted to point to
# under /var are adjusted to point to locations on your # locations on your root device.
# root device.
# #
# Set skip_credit to yes or true if you do not want seed files to # Set skip_credit to yes or true if you do not want seed files to actually
# actually credit the random number generator. # credit the random number generator. For example, you should set this if you
# For example, you should set this if you plan to replicate the file # plan to replicate the file system image without removing the contents of
# system image without removing the contents of ${seed_dir}. # ${seed_dir}.
# #
# lock_file=/var/run/seedrng.lock
# seed_dir=/var/lib/seedrng # seed_dir=/var/lib/seedrng
# skip_credit=no # skip_credit=no

View File

@ -21,8 +21,6 @@ depend()
seedrng_options() seedrng_options()
{ {
[ -n "${lock_file}" ] &&
echo "--lock-file \"${lock_file}\""
[ -n "${seed_dir}" ] && [ -n "${seed_dir}" ] &&
echo "--seed-dir \"${seed_dir}\"" echo "--seed-dir \"${seed_dir}\""
yesno "${skip_credit}" && yesno "${skip_credit}" &&

View File

@ -44,6 +44,10 @@
#define GRND_INSECURE 0x0004 /* Apparently some headers don't ship with this yet. */ #define GRND_INSECURE 0x0004 /* Apparently some headers don't ship with this yet. */
#endif #endif
#define DEFAULT_SEED_DIR "/var/lib/seedrng"
#define CREDITABLE_SEED "seed.credit"
#define NON_CREDITABLE_SEED "seed.no-credit"
/* Use long option value that is out of range for 8 bit getopt values. /* Use long option value that is out of range for 8 bit getopt values.
* The exact enum value is internal and can freely change, so we keep the * The exact enum value is internal and can freely change, so we keep the
* options sorted. * options sorted.
@ -51,7 +55,6 @@
enum long_opts { enum long_opts {
/* This has to come first so following values stay in the 0x100+ range. */ /* This has to come first so following values stay in the 0x100+ range. */
LONGOPT_BASE = 0x100, LONGOPT_BASE = 0x100,
LONGOPT_LOCK_FILE,
LONGOPT_SEED_DIR, LONGOPT_SEED_DIR,
LONGOPT_SKIP_CREDIT, LONGOPT_SKIP_CREDIT,
}; };
@ -60,24 +63,17 @@ const char *applet = NULL;
const char *extraopts = NULL; const char *extraopts = NULL;
const char getoptstring[] = getoptstring_COMMON; const char getoptstring[] = getoptstring_COMMON;
const struct option longopts[] = { const struct option longopts[] = {
{ "lock-file", 1, NULL, LONGOPT_LOCK_FILE },
{ "seed-dir", 1, NULL, LONGOPT_SEED_DIR }, { "seed-dir", 1, NULL, LONGOPT_SEED_DIR },
{ "skip-credit", 0, NULL, LONGOPT_SKIP_CREDIT }, { "skip-credit", 0, NULL, LONGOPT_SKIP_CREDIT },
longopts_COMMON longopts_COMMON
}; };
const char * const longopts_help[] = { const char * const longopts_help[] = {
"lock file", "Directory for seed files (default: " DEFAULT_SEED_DIR ")",
"directory for seed files", "Skip crediting entropy of seeds",
"skip credit",
longopts_help_COMMON longopts_help_COMMON
}; };
const char *usagestring = NULL; const char *usagestring = NULL;
static char *lock_file = NULL;
static char *seed_dir = NULL;
static char *creditable_seed = NULL;
static char *non_creditable_seed = NULL;
enum blake2s_lengths { enum blake2s_lengths {
BLAKE2S_BLOCK_LEN = 64, BLAKE2S_BLOCK_LEN = 64,
BLAKE2S_HASH_LEN = 32, BLAKE2S_HASH_LEN = 32,
@ -269,13 +265,69 @@ static void blake2s_final(struct blake2s_state *state, uint8_t *out)
memcpy(out, state->h, state->outlen); memcpy(out, state->h, state->outlen);
} }
static ssize_t getrandom_full(void *buf, size_t count, unsigned int flags)
{
ssize_t ret, total = 0;
uint8_t *p = buf;
do {
ret = getrandom(p, count, flags);
if (ret < 0 && errno == EINTR)
continue;
else if (ret < 0)
return ret;
total += ret;
p += ret;
count -= ret;
} while (count);
return total;
}
static ssize_t read_full(int fd, void *buf, size_t count)
{
ssize_t ret, total = 0;
uint8_t *p = buf;
do {
ret = read(fd, p, count);
if (ret < 0 && errno == EINTR)
continue;
else if (ret < 0)
return ret;
else if (ret == 0)
break;
total += ret;
p += ret;
count -= ret;
} while (count);
return total;
}
static ssize_t write_full(int fd, const void *buf, size_t count)
{
ssize_t ret, total = 0;
const uint8_t *p = buf;
do {
ret = write(fd, p, count);
if (ret < 0 && errno == EINTR)
continue;
else if (ret < 0)
return ret;
total += ret;
p += ret;
count -= ret;
} while (count);
return total;
}
static size_t determine_optimal_seed_len(void) static size_t determine_optimal_seed_len(void)
{ {
size_t ret = 0; size_t ret = 0;
char poolsize_str[11] = { 0 }; char poolsize_str[11] = { 0 };
int fd = open("/proc/sys/kernel/random/poolsize", O_RDONLY); int fd = open("/proc/sys/kernel/random/poolsize", O_RDONLY);
if (fd < 0 || read(fd, poolsize_str, sizeof(poolsize_str) - 1) < 0) { if (fd < 0 || read_full(fd, poolsize_str, sizeof(poolsize_str) - 1) < 0) {
ewarn("%s: Unable to determine pool size, falling back to %u bits: %s", ewarn("%s: Unable to determine pool size, falling back to %u bits: %s",
applet, MIN_SEED_LEN * 8, strerror(errno)); applet, MIN_SEED_LEN * 8, strerror(errno));
ret = MIN_SEED_LEN; ret = MIN_SEED_LEN;
@ -296,12 +348,11 @@ static int read_new_seed(uint8_t *seed, size_t len, bool *is_creditable)
int urandom_fd; int urandom_fd;
*is_creditable = false; *is_creditable = false;
ret = getrandom(seed, len, GRND_NONBLOCK); ret = getrandom_full(seed, len, GRND_NONBLOCK);
if (ret == (ssize_t)len) { if (ret == (ssize_t)len) {
*is_creditable = true; *is_creditable = true;
return 0; return 0;
} } else if (ret < 0 && errno == ENOSYS) {
if (ret == -1 && errno == ENOSYS) {
struct pollfd random_fd = { struct pollfd random_fd = {
.fd = open("/dev/random", O_RDONLY), .fd = open("/dev/random", O_RDONLY),
.events = POLLIN .events = POLLIN
@ -310,18 +361,19 @@ static int read_new_seed(uint8_t *seed, size_t len, bool *is_creditable)
return -errno; return -errno;
*is_creditable = poll(&random_fd, 1, 0) == 1; *is_creditable = poll(&random_fd, 1, 0) == 1;
close(random_fd.fd); close(random_fd.fd);
} else if (getrandom(seed, len, GRND_INSECURE) == (ssize_t)len) } else if (getrandom_full(seed, len, GRND_INSECURE) == (ssize_t)len)
return 0; return 0;
urandom_fd = open("/dev/urandom", O_RDONLY); urandom_fd = open("/dev/urandom", O_RDONLY);
if (urandom_fd < 0) if (urandom_fd < 0)
return -errno; return -1;
ret = read(urandom_fd, seed, len); ret = read_full(urandom_fd, seed, len);
if (ret == (ssize_t)len) if (ret == (ssize_t)len)
ret = 0; ret = 0;
else else
ret = -errno ? -errno : -EIO; ret = -errno ? -errno : -EIO;
close(urandom_fd); close(urandom_fd);
return ret; errno = -ret;
return ret ? -1 : 0;
} }
static int seed_rng(uint8_t *seed, size_t len, bool credit) static int seed_rng(uint8_t *seed, size_t len, bool credit)
@ -336,96 +388,74 @@ static int seed_rng(uint8_t *seed, size_t len, bool credit)
}; };
int random_fd, ret; int random_fd, ret;
if (len > sizeof(req.buffer)) if (len > sizeof(req.buffer)) {
return -EFBIG; errno = EFBIG;
return -1;
}
memcpy(req.buffer, seed, len); memcpy(req.buffer, seed, len);
random_fd = open("/dev/random", O_RDWR); random_fd = open("/dev/urandom", O_RDONLY);
if (random_fd < 0) if (random_fd < 0)
return -errno; return -1;
ret = ioctl(random_fd, RNDADDENTROPY, &req); ret = ioctl(random_fd, RNDADDENTROPY, &req);
if (ret) if (ret)
ret = -errno ? -errno : -EIO; ret = -errno ? -errno : -EIO;
close(random_fd); close(random_fd);
return ret; errno = -ret;
return ret ? -1 : 0;
} }
static int seed_from_file_if_exists(const char *filename, bool credit, struct blake2s_state *hash) static int seed_from_file_if_exists(const char *filename, int dfd, bool credit, struct blake2s_state *hash)
{ {
uint8_t seed[MAX_SEED_LEN]; uint8_t seed[MAX_SEED_LEN];
ssize_t seed_len; ssize_t seed_len;
int fd, dfd, ret = 0; int fd = -1, ret = 0;
fd = open(filename, O_RDONLY); fd = openat(dfd, filename, O_RDONLY);
if (fd < 0 && errno == ENOENT) if (fd < 0 && errno == ENOENT)
return 0; return 0;
else if (fd < 0) { else if (fd < 0) {
ret = -errno; ret = -errno;
eerror("%s: Unable to open seed file: %s", applet, strerror(errno)); eerror("%s: Unable to open seed file: %s", applet, strerror(errno));
return ret; goto out;
} }
dfd = open(seed_dir, O_DIRECTORY | O_RDONLY); seed_len = read_full(fd, seed, sizeof(seed));
if (dfd < 0) {
ret = -errno;
close(fd);
eerror("%s: Unable to open seed directory: %s", applet, strerror(errno));
return ret;
}
seed_len = read(fd, seed, sizeof(seed));
if (seed_len < 0) { if (seed_len < 0) {
ret = -errno; ret = -errno;
eerror("%s: Unable to read seed file: %s", applet, strerror(errno)); eerror("%s: Unable to read seed file: %s", applet, strerror(errno));
goto out;
} }
close(fd); if ((unlinkat(dfd, filename, 0) < 0 || fsync(dfd) < 0) && seed_len) {
if (ret) {
close(dfd);
return ret;
}
if ((unlink(filename) < 0 || fsync(dfd) < 0) && seed_len) {
ret = -errno; ret = -errno;
eerror("%s: Unable to remove seed after reading, so not seeding: %s", eerror("%s: Unable to remove seed after reading, so not seeding: %s",
applet, strerror(errno)); applet, strerror(errno));
goto out;
} }
close(dfd);
if (ret)
return ret;
if (!seed_len) if (!seed_len)
return 0; goto out;
blake2s_update(hash, &seed_len, sizeof(seed_len)); blake2s_update(hash, &seed_len, sizeof(seed_len));
blake2s_update(hash, seed, seed_len); blake2s_update(hash, seed, seed_len);
einfo("Seeding %zd bits %s crediting", seed_len * 8, credit ? "and" : "without"); einfo("Seeding %zd bits %s crediting", seed_len * 8, credit ? "and" : "without");
ret = seed_rng(seed, seed_len, credit); if (seed_rng(seed, seed_len, credit) < 0) {
if (ret < 0) ret = -errno;
eerror("%s: Unable to seed: %s", applet, strerror(-ret)); eerror("%s: Unable to seed: %s", applet, strerror(errno));
return ret;
} }
static void populate_global_paths(void) out:
{ if (fd >= 0)
if (!lock_file) close(fd);
lock_file = xstrdup("/var/run/seedrng.lock"); errno = -ret;
if (!seed_dir) return ret ? -1 : 0;
seed_dir = xstrdup("/var/lib/seedrng");
xasprintf(&creditable_seed, "%s/seed.credit", seed_dir);
xasprintf(&non_creditable_seed, "%s/seed.no-credit", seed_dir);
}
static void cleanup(void)
{
free(lock_file);
free(seed_dir);
free(creditable_seed);
free(non_creditable_seed);
} }
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
int opt;
static const char seedrng_prefix[] = "SeedRNG v1 Old+New Prefix"; static const char seedrng_prefix[] = "SeedRNG v1 Old+New Prefix";
static const char seedrng_failure[] = "SeedRNG v1 No New Seed Failure"; static const char seedrng_failure[] = "SeedRNG v1 No New Seed Failure";
int ret, fd = -1, lock, program_ret = 0; int opt, fd = -1, dfd = -1, program_ret = 0;
char *seed_dir = NULL;
uint8_t new_seed[MAX_SEED_LEN]; uint8_t new_seed[MAX_SEED_LEN];
size_t new_seed_len; size_t new_seed_len;
bool new_seed_creditable; bool new_seed_creditable;
@ -433,20 +463,11 @@ int main(int argc, char **argv)
struct blake2s_state hash; struct blake2s_state hash;
bool skip_credit = false; bool skip_credit = false;
atexit(cleanup);
applet = basename_c(argv[0]); applet = basename_c(argv[0]);
if (geteuid())
eerrorx("%s: superuser access is required", applet);
umask(0077);
while ((opt = getopt_long(argc, argv, getoptstring, while ((opt = getopt_long(argc, argv, getoptstring, longopts, (int *) 0)) != -1)
longopts, (int *) 0)) != -1)
{ {
switch (opt) { switch (opt) {
case LONGOPT_LOCK_FILE:
if (!lock_file)
lock_file = xstrdup(optarg);
break;
case LONGOPT_SEED_DIR: case LONGOPT_SEED_DIR:
if (!seed_dir) if (!seed_dir)
seed_dir = xstrdup(optarg); seed_dir = xstrdup(optarg);
@ -457,7 +478,15 @@ int main(int argc, char **argv)
case_RC_COMMON_GETOPT case_RC_COMMON_GETOPT
} }
} }
populate_global_paths(); if (!seed_dir)
seed_dir = xstrdup(DEFAULT_SEED_DIR);
if (getuid()) {
eerror("%s: superuser access is required", applet);
program_ret = 1;
goto out;
}
umask(0077);
blake2s_init(&hash, BLAKE2S_HASH_LEN); blake2s_init(&hash, BLAKE2S_HASH_LEN);
blake2s_update(&hash, seedrng_prefix, strlen(seedrng_prefix)); blake2s_update(&hash, seedrng_prefix, strlen(seedrng_prefix));
clock_gettime(CLOCK_REALTIME, &realtime); clock_gettime(CLOCK_REALTIME, &realtime);
@ -465,28 +494,27 @@ int main(int argc, char **argv)
blake2s_update(&hash, &realtime, sizeof(realtime)); blake2s_update(&hash, &realtime, sizeof(realtime));
blake2s_update(&hash, &boottime, sizeof(boottime)); blake2s_update(&hash, &boottime, sizeof(boottime));
if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST) if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST) {
eerrorx("%s: Unable to create \"%s\" directory: %s", eerror("%s: Unable to create seed directory: %s", applet, strerror(errno));
applet, seed_dir, strerror(errno));
lock = open(lock_file, O_WRONLY | O_CREAT, 0000);
if (lock < 0 || flock(lock, LOCK_EX) < 0) {
eerror("%s: Unable to open lock file: %s", applet, strerror(errno));
program_ret = 1; program_ret = 1;
goto out; goto out;
} }
ret = seed_from_file_if_exists(non_creditable_seed, false, &hash); dfd = open(seed_dir, O_DIRECTORY | O_RDONLY);
if (ret < 0) if (dfd < 0 || flock(dfd, LOCK_EX) < 0) {
eerror("%s: Unable to lock seed directory: %s", applet, strerror(errno));
program_ret = 1;
goto out;
}
if (seed_from_file_if_exists(NON_CREDITABLE_SEED, dfd, false, &hash) < 0)
program_ret |= 1 << 1; program_ret |= 1 << 1;
ret = seed_from_file_if_exists(creditable_seed, !skip_credit, &hash); if (seed_from_file_if_exists(CREDITABLE_SEED, dfd, !skip_credit, &hash) < 0)
if (ret < 0)
program_ret |= 1 << 2; program_ret |= 1 << 2;
new_seed_len = determine_optimal_seed_len(); new_seed_len = determine_optimal_seed_len();
ret = read_new_seed(new_seed, new_seed_len, &new_seed_creditable); if (read_new_seed(new_seed, new_seed_len, &new_seed_creditable) < 0) {
if (ret < 0) { eerror("%s: Unable to read new seed: %s", applet, strerror(errno));
eerror("%s: Unable to read new seed: %s", applet, strerror(-ret));
new_seed_len = BLAKE2S_HASH_LEN; new_seed_len = BLAKE2S_HASH_LEN;
strncpy((char *)new_seed, seedrng_failure, new_seed_len); strncpy((char *)new_seed, seedrng_failure, new_seed_len);
program_ret |= 1 << 3; program_ret |= 1 << 3;
@ -496,27 +524,26 @@ int main(int argc, char **argv)
blake2s_final(&hash, new_seed + new_seed_len - BLAKE2S_HASH_LEN); blake2s_final(&hash, new_seed + new_seed_len - BLAKE2S_HASH_LEN);
einfo("Saving %zu bits of %s seed for next boot", new_seed_len * 8, new_seed_creditable ? "creditable" : "non-creditable"); einfo("Saving %zu bits of %s seed for next boot", new_seed_len * 8, new_seed_creditable ? "creditable" : "non-creditable");
fd = open(non_creditable_seed, O_WRONLY | O_CREAT | O_TRUNC, 0400); fd = openat(dfd, NON_CREDITABLE_SEED, O_WRONLY | O_CREAT | O_TRUNC, 0400);
if (fd < 0) { if (fd < 0) {
eerror("%s: Unable to open seed file for writing: %s", eerror("%s: Unable to open seed file for writing: %s", applet, strerror(errno));
applet, strerror(errno));
program_ret |= 1 << 4; program_ret |= 1 << 4;
goto out; goto out;
} }
if (write(fd, new_seed, new_seed_len) != (ssize_t)new_seed_len || fsync(fd) < 0) { if (write_full(fd, new_seed, new_seed_len) != (ssize_t)new_seed_len || fsync(fd) < 0) {
eerror("%s: Unable to write seed file: %s", applet, strerror(errno)); eerror("%s: Unable to write seed file: %s", applet, strerror(errno));
program_ret |= 1 << 5; program_ret |= 1 << 5;
goto out; goto out;
} }
if (new_seed_creditable && rename(non_creditable_seed, creditable_seed) < 0) { if (new_seed_creditable && renameat(dfd, NON_CREDITABLE_SEED, dfd, CREDITABLE_SEED) < 0) {
ewarn("%s: Unable to make new seed creditable: %s", ewarn("%s: Unable to make new seed creditable: %s", applet, strerror(errno));
applet, strerror(errno));
program_ret |= 1 << 6; program_ret |= 1 << 6;
} }
out: out:
if (fd >= 0) if (fd >= 0)
close(fd); close(fd);
if (lock >= 0) if (dfd >= 0)
close(lock); close(dfd);
free(seed_dir);
return program_ret; return program_ret;
} }