libreboot-utils: replace rlong() with rset()

now you can send an arbitrary number of bytes
with random numbers

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-25 18:01:38 +00:00
parent 00b56c0278
commit a70ede850d
4 changed files with 9 additions and 26 deletions
+1 -1
View File
@@ -384,7 +384,7 @@ int dcat(const char *s, size_t n,
*/ */
unsigned short hextonum(char ch_s); unsigned short hextonum(char ch_s);
size_t rlong(void); void rset(void *buf, size_t n);
/* Helper functions for command: dump /* Helper functions for command: dump
*/ */
+1 -12
View File
@@ -635,12 +635,6 @@ mkhtemp(int *fd,
p, xc, fd, st, type); p, xc, fd, st, type);
if (r == 0) { if (r == 0) {
if (retries >= MKHTEMP_SPIN_THRESHOLD) {
/* usleep can return EINTR */
close_errno = errno;
usleep((useconds_t)rlong() & 0x3FF);
errno = close_errno;
}
continue; continue;
} }
if (r < 0) if (r < 0)
@@ -903,12 +897,7 @@ mkhtemp_fill_random(char *p, size_t xc)
for (chx = 0; chx < xc; chx++) { for (chx = 0; chx < xc; chx++) {
retry_rand: retry_rand:
/* /dev/urandom if enabled, OR: rset(&r, sizeof(r));
* on bsd: uses arc4random
* on linux: uses getrandom
NOTE: *aborts* on error, regardless of method
*/
r = rlong(); /* always *returns* successfully */
if (r >= limit) if (r >= limit)
goto retry_rand; goto retry_rand;
+1 -1
View File
@@ -84,7 +84,7 @@ hextonum(char ch_s)
if (ch == '?' || ch == 'x') { if (ch == '?' || ch == 'x') {
rval = rlong(); rset(&rval, sizeof(rval));
if (errno > 0) if (errno > 0)
goto err_hextonum; goto err_hextonum;
+6 -12
View File
@@ -29,23 +29,21 @@
#include "../include/common.h" #include "../include/common.h"
size_t void
rlong(void) rset(void *buf, size_t n)
{ {
int saved_errno = errno; int saved_errno = errno;
size_t len = sizeof(size_t);
#if (defined(__OpenBSD__) || defined(__FreeBSD__) || \ #if (defined(__OpenBSD__) || defined(__FreeBSD__) || \
defined(__NetBSD__) || defined(__APPLE__) || \ defined(__NetBSD__) || defined(__APPLE__) || \
defined(__DragonFly__)) && !(defined(USE_URANDOM) && \ defined(__DragonFly__)) && !(defined(USE_URANDOM) && \
((USE_URANDOM) > 0)) ((USE_URANDOM) > 0))
arc4random_buf(&rval, len); arc4random_buf(buf, n);
goto out; goto out;
#else #else
size_t off = errno = 0; size_t off = errno = 0;
ssize_t rc = 0; ssize_t rc = 0;
size_t rval;
#if defined(USE_URANDOM) && \ #if defined(USE_URANDOM) && \
((USE_URANDOM) > 0) ((USE_URANDOM) > 0)
@@ -54,11 +52,11 @@ rlong(void)
if ((fd = open("/dev/urandom", O_RDONLY)) < 0) if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
goto err; goto err;
retry_rand: retry_rand:
if ((rc = read(fd, &rval, len)) < 0) { if ((rc = read(fd, buf, n)) < 0) {
#elif defined(__linux__) #elif defined(__linux__)
retry_rand: retry_rand:
if ((rc = (ssize_t)syscall(SYS_getrandom, if ((rc = (ssize_t)syscall(SYS_getrandom,
(char *)&rval + off, len - off, 0)) < 0) { buf + off, n - off, 0)) < 0) {
#else #else
#error Unsupported operating system (possibly unsecure randomisation) #error Unsupported operating system (possibly unsecure randomisation)
#endif #endif
@@ -72,21 +70,17 @@ retry_rand:
goto err; /* possibly unsupported by kernel */ goto err; /* possibly unsupported by kernel */
} }
if ((off += (size_t)rc) < len) if ((off += (size_t)rc) < n)
goto retry_rand; goto retry_rand;
goto out; goto out;
err: err:
err_no_cleanup(1, ECANCELED, err_no_cleanup(1, ECANCELED,
"Randomisation failure, possibly unsupported in your kernel."); "Randomisation failure, possibly unsupported in your kernel.");
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
return 0;
#endif #endif
out: out:
errno = saved_errno; errno = saved_errno;
return rval;
} }
#endif #endif