[prev in list] [next in list] [prev in thread] [next in thread] 

List:       busybox-cvs
Subject:    [git commit] seedrng: prepare read_new_seed() to not need a "success" retval
From:       Denys Vlasenko <vda.linux () googlemail ! com>
Date:       2022-04-30 13:25:55
Message-ID: 20220430133023.786B8842E4 () busybox ! osuosl ! org
[Download RAW message or body]

commit: https://git.busybox.net/busybox/commit/?id=282b61a64921775e5d167df942347a8a3cf984e7
branch: https://git.busybox.net/busybox/commit/?id=refs/heads/master

We do not expect /dev/[u]random to be not openable/readable.
If they are, just bail out (something is obviously very wrong).

function                                             old     new   delta
seedrng_main                                        1077    1076      -1
.rodata                                           104939  104929     -10

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
---
 util-linux/seedrng.c | 22 ++++++++++++----------
 1 file changed, 12 insertions(+), 10 deletions(-)

diff --git a/util-linux/seedrng.c b/util-linux/seedrng.c
index 82c69b72b..3f4c5c0c8 100644
--- a/util-linux/seedrng.c
+++ b/util-linux/seedrng.c
@@ -79,25 +79,27 @@ static int read_new_seed(uint8_t *seed, size_t len, bool *is_creditable)
 {
 	ssize_t ret;
 
-	*is_creditable = false;
 	ret = getrandom(seed, len, GRND_NONBLOCK);
 	if (ret == (ssize_t)len) {
 		*is_creditable = true;
 		return 0;
-	} else if (ret < 0 && errno == ENOSYS) {
+	}
+	if (ret < 0 && errno == ENOSYS) {
 		struct pollfd random_fd = {
-			.fd = open("/dev/random", O_RDONLY),
+			.fd = xopen("/dev/random", O_RDONLY),
 			.events = POLLIN
 		};
-		if (random_fd.fd < 0)
-			return -1;
 		*is_creditable = poll(&random_fd, 1, 0) == 1;
 		close(random_fd.fd);
-	} else if (getrandom(seed, len, GRND_INSECURE) == (ssize_t)len)
-		return 0;
-	if (open_read_close("/dev/urandom", seed, len) == (ssize_t)len)
-		return 0;
-	return -1;
+	} else {
+		*is_creditable = false;
+		if (getrandom(seed, len, GRND_INSECURE) == (ssize_t)len)
+			return 0;
+	}
+	errno = 0;
+	if (open_read_close("/dev/urandom", seed, len) != (ssize_t)len)
+		bb_perror_msg_and_die("can't read '%s'", "/dev/urandom");
+	return 0;
 }
 
 static void seed_rng(uint8_t *seed, size_t len, bool credit)
_______________________________________________
busybox-cvs mailing list
busybox-cvs@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox-cvs
[prev in list] [next in list] [prev in thread] [next in thread] 

Configure | About | News | Add a list | Sponsored by KoreLogic