From 401356511c5de3b5a88fdd3ac4977e19b82babf5 Mon Sep 17 00:00:00 2001 From: Denys Vlasenko Date: Wed, 27 Apr 2022 17:20:43 +0200 Subject: [PATCH] seedrng: remove unnecessary zero-filling of local variables function old new delta seedrng_main 1292 1273 -19 Signed-off-by: Denys Vlasenko --- util-linux/seedrng.c | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/util-linux/seedrng.c b/util-linux/seedrng.c index c762e9ecd..dd082ea90 100644 --- a/util-linux/seedrng.c +++ b/util-linux/seedrng.c @@ -54,20 +54,23 @@ #define CREDITABLE_SEED_NAME "seed.credit" #define NON_CREDITABLE_SEED_NAME "seed.no-credit" -enum seedrng_lengths { +enum { MIN_SEED_LEN = SHA256_OUTSIZE, MAX_SEED_LEN = 512 }; static size_t determine_optimal_seed_len(void) { - char poolsize_str[11] = { 0 }; - unsigned long poolsize; + char poolsize_str[12]; + unsigned poolsize; + int n; - if (open_read_close("/proc/sys/kernel/random/poolsize", poolsize_str, sizeof(poolsize_str) - 1) < 0) { + n = open_read_close("/proc/sys/kernel/random/poolsize", poolsize_str, sizeof(poolsize_str) - 1); + if (n < 0) { bb_perror_msg("unable to determine pool size, assuming %u bits", MIN_SEED_LEN * 8); return MIN_SEED_LEN; } + poolsize_str[n] = '\0'; poolsize = (bb_strtoul(poolsize_str, NULL, 10) + 7) / 8; return MAX(MIN(poolsize, MAX_SEED_LEN), MIN_SEED_LEN); } @@ -159,7 +162,7 @@ static int seed_from_file_if_exists(const char *filename, int dfd, bool credit, int seedrng_main(int argc, char *argv[]) MAIN_EXTERNALLY_VISIBLE; int seedrng_main(int argc UNUSED_PARAM, char *argv[]) { - const char *seed_dir = DEFAULT_SEED_DIR, *creditable_seed, *non_creditable_seed; + const char *seed_dir, *creditable_seed, *non_creditable_seed; int fd, dfd, program_ret = 0; uint8_t new_seed[MAX_SEED_LEN]; size_t new_seed_len; @@ -178,10 +181,8 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[]) ; #endif + seed_dir = DEFAULT_SEED_DIR; skip_credit = getopt32long(argv, "d:n", longopts, &seed_dir) & OPT_n; - creditable_seed = concat_path_file(seed_dir, CREDITABLE_SEED_NAME); - non_creditable_seed = concat_path_file(seed_dir, NON_CREDITABLE_SEED_NAME); - umask(0077); if (getuid()) bb_simple_error_msg_and_die(bb_msg_you_must_be_root); @@ -200,6 +201,8 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[]) clock_gettime(CLOCK_BOOTTIME, ×tamp); sha256_hash(&hash, ×tamp, sizeof(timestamp)); + creditable_seed = concat_path_file(seed_dir, CREDITABLE_SEED_NAME); + non_creditable_seed = concat_path_file(seed_dir, NON_CREDITABLE_SEED_NAME); for (int i = 1; i < 3; ++i) { if (seed_from_file_if_exists(i == 1 ? non_creditable_seed : creditable_seed, dfd, i == 1 ? false : !skip_credit, &hash) < 0)