seedrng: use predefined strings where possible

- Use predefined strings where possible.
- Open /dev/random with O_RDONLY for ioctl().

function                                             old     new   delta
seed_from_file_if_exists                             413     410      -3
.rodata                                           108407  108350     -57
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-60)             Total: -60 bytes
   text	   data	    bss	    dec	    hex	filename
 975979	   4227	   1816	 982022	  efc06	busybox_old
 975919	   4227	   1816	 981962	  efbca	busybox_unstripped

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Bernhard Reutner-Fischer <rep.dot.nop@gmail.com>
This commit is contained in:
Jason A. Donenfeld 2022-04-20 15:36:22 +02:00 committed by Bernhard Reutner-Fischer
parent 398bb3861a
commit ce9a345632

View File

@ -79,7 +79,7 @@ static size_t determine_optimal_seed_len(void)
unsigned long poolsize; unsigned long poolsize;
if (open_read_close("/proc/sys/kernel/random/poolsize", poolsize_str, sizeof(poolsize_str) - 1) < 0) { if (open_read_close("/proc/sys/kernel/random/poolsize", poolsize_str, sizeof(poolsize_str) - 1) < 0) {
bb_perror_msg("unable to determine pool size, falling back to %u bits", MIN_SEED_LEN * 8); bb_perror_msg("unable to determine pool size, assuming %u bits", MIN_SEED_LEN * 8);
return MIN_SEED_LEN; return MIN_SEED_LEN;
} }
poolsize = (bb_strtoul(poolsize_str, NULL, 10) + 7) / 8; poolsize = (bb_strtoul(poolsize_str, NULL, 10) + 7) / 8;
@ -129,7 +129,7 @@ static int seed_rng(uint8_t *seed, size_t len, bool credit)
} }
memcpy(req.buffer, seed, len); memcpy(req.buffer, seed, len);
random_fd = open("/dev/random", O_RDWR); random_fd = open("/dev/random", O_RDONLY);
if (random_fd < 0) if (random_fd < 0)
return -1; return -1;
ret = ioctl(random_fd, RNDADDENTROPY, &req); ret = ioctl(random_fd, RNDADDENTROPY, &req);
@ -154,7 +154,7 @@ static int seed_from_file_if_exists(const char *filename, int dfd, bool credit,
return -1; return -1;
} }
if ((unlink(filename) < 0 || fsync(dfd) < 0) && seed_len) { if ((unlink(filename) < 0 || fsync(dfd) < 0) && seed_len) {
bb_simple_perror_msg("unable to remove seed after reading, so not seeding"); bb_simple_perror_msg("unable to remove seed, so not seeding");
return -1; return -1;
} else if (!seed_len) } else if (!seed_len)
return 0; return 0;
@ -205,14 +205,14 @@ int seedrng_main(int argc UNUSED_PARAM, char *argv[])
umask(0077); umask(0077);
if (getuid()) if (getuid())
bb_simple_error_msg_and_die("this program requires root"); bb_simple_error_msg_and_die(bb_msg_you_must_be_root);
if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST) if (mkdir(seed_dir, 0700) < 0 && errno != EEXIST)
bb_simple_perror_msg_and_die("unable to create seed directory"); bb_simple_perror_msg_and_die("unable to create seed directory");
dfd = open(seed_dir, O_DIRECTORY | O_RDONLY); dfd = open(seed_dir, O_DIRECTORY | O_RDONLY);
if (dfd < 0 || flock(dfd, LOCK_EX) < 0) { if (dfd < 0 || flock(dfd, LOCK_EX) < 0) {
bb_simple_perror_msg("unable to open and lock seed directory"); bb_simple_perror_msg("unable to lock seed directory");
program_ret = 1; program_ret = 1;
goto out; goto out;
} }