seedrng: do not try to continue on unexpected errors (just exit)

function                                             old     new   delta
.rodata                                           104946  104938      -8
seedrng_main                                        1225    1077    -148
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-156)           Total: -156 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
This commit is contained in:
Denys Vlasenko 2022-04-29 18:37:42 +02:00
parent 002d6ee46d
commit 0fa16fc7a2

View File

@ -100,63 +100,43 @@ static int read_new_seed(uint8_t *seed, size_t len, bool *is_creditable)
return -1; return -1;
} }
static int seed_rng(uint8_t *seed, size_t len, bool credit) static void seed_rng(uint8_t *seed, size_t len, bool credit)
{ {
struct { struct {
int entropy_count; int entropy_count;
int buf_size; int buf_size;
uint8_t buffer[MAX_SEED_LEN]; uint8_t buffer[MAX_SEED_LEN];
} req; } req;
int random_fd, ret; int random_fd;
if (len > sizeof(req.buffer)) {
errno = EFBIG;
return -1;
}
req.entropy_count = credit ? len * 8 : 0; req.entropy_count = credit ? len * 8 : 0;
req.buf_size = len; req.buf_size = len;
memcpy(req.buffer, seed, len); memcpy(req.buffer, seed, len);
random_fd = open("/dev/urandom", O_RDONLY); random_fd = xopen("/dev/urandom", O_RDONLY);
if (random_fd < 0) xioctl(random_fd, RNDADDENTROPY, &req);
return -1;
ret = ioctl(random_fd, RNDADDENTROPY, &req);
if (ret)
ret = -errno ? -errno : -EIO;
if (ENABLE_FEATURE_CLEAN_UP) if (ENABLE_FEATURE_CLEAN_UP)
close(random_fd); close(random_fd);
errno = -ret;
return ret ? -1 : 0;
} }
static int seed_from_file_if_exists(const char *filename, int dfd, bool credit, sha256_ctx_t *hash) static void seed_from_file_if_exists(const char *filename, bool credit, sha256_ctx_t *hash)
{ {
uint8_t seed[MAX_SEED_LEN]; uint8_t seed[MAX_SEED_LEN];
ssize_t seed_len; ssize_t seed_len;
seed_len = open_read_close(filename, seed, sizeof(seed)); seed_len = open_read_close(filename, seed, sizeof(seed));
if (seed_len < 0) { if (seed_len < 0) {
if (errno == ENOENT) if (errno != ENOENT)
return 0; bb_perror_msg_and_die("can't%s seed", " read");
bb_perror_msg("can't%s seed", " read"); return;
return -1;
} }
if ((unlink(filename) < 0 || fsync(dfd) < 0) && seed_len) { xunlink(filename);
bb_perror_msg("can't%s seed", " remove"); if (seed_len != 0) {
return -1;
} else if (!seed_len)
return 0;
sha256_hash(hash, &seed_len, sizeof(seed_len)); sha256_hash(hash, &seed_len, sizeof(seed_len));
sha256_hash(hash, seed, seed_len); sha256_hash(hash, seed, seed_len);
printf("Seeding %u bits %s crediting\n", (unsigned)seed_len * 8, credit ? "and" : "without"); printf("Seeding %u bits %s crediting\n", (unsigned)seed_len * 8, credit ? "and" : "without");
if (seed_rng(seed, seed_len, credit) < 0) { seed_rng(seed, seed_len, credit);
bb_perror_msg("can't%s seed", "");
return -1;
} }
return 0;
} }
int seedrng_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE; int seedrng_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE;
@ -202,11 +182,9 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
sha256_hash(&hash, &timestamp, sizeof(timestamp)); sha256_hash(&hash, &timestamp, sizeof(timestamp));
for (int i = 1; i < 3; ++i) { for (int i = 1; i < 3; ++i) {
if (seed_from_file_if_exists(i == 1 ? NON_CREDITABLE_SEED_NAME : CREDITABLE_SEED_NAME, seed_from_file_if_exists(i == 1 ? NON_CREDITABLE_SEED_NAME : CREDITABLE_SEED_NAME,
dfd,
i == 1 ? false : !skip_credit, i == 1 ? false : !skip_credit,
&hash) < 0) &hash);
program_ret |= 1 << i;
} }
new_seed_len = determine_optimal_seed_len(); new_seed_len = determine_optimal_seed_len();