From 6db9514c956c74afa171cca165d207a3ec502af0 Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Wed, 25 Mar 2026 11:23:49 +0000 Subject: libreboot-utils: tidy up the rand code Signed-off-by: Leah Rowe --- util/libreboot-utils/lib/rand.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) (limited to 'util/libreboot-utils/lib/rand.c') diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c index 2f88b420..0b156e51 100644 --- a/util/libreboot-utils/lib/rand.c +++ b/util/libreboot-utils/lib/rand.c @@ -59,6 +59,7 @@ rlong(void) { size_t rval; int saved_errno = errno; + errno = 0; #if (defined(__OpenBSD__) && (OpenBSD) >= 201) || \ defined(__FreeBSD__) || \ @@ -71,30 +72,23 @@ rlong(void) size_t off = 0; size_t len = sizeof(rval); - ssize_t rc; - if (!len) - goto err; - - while (off < len) { - - rc = (ssize_t)syscall(SYS_getrandom, - (char *)&rval + off, len - off, 0); - - if (rc < 0) { - if (errno == EINTR || errno == EAGAIN) - continue; +retry_rand: + rc = (ssize_t)syscall(SYS_getrandom, + (char *)&rval + off, len - off, 0); - goto err; /* possibly unsupported by kernel */ - } + if (rc < 0) { + if (errno == EINTR || errno == EAGAIN) + goto retry_rand; - off += (size_t)rc; + goto err; /* possibly unsupported by kernel */ } - goto out; + if ((off += (size_t)rc) < len) + goto retry_rand; - return rval; + goto out; err: /* * getrandom can return with error, butt arc4random @@ -103,8 +97,9 @@ err: * BSD. So a rand failure is to be interpreted as * a major systems failure, and we act accordingly. */ - err_no_cleanup(1, ECANCELED, "Randomisation failure"); - exit(1); + err_no_cleanup(1, ECANCELED, + "Randomisation failure, possibly unsupported in your kernel."); + exit(EXIT_FAILURE); #else #error Unsupported operating system (possibly unsecure randomisation) -- cgit v1.2.1