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);
size_t rlong(void);
void rset(void *buf, size_t n);
/* Helper functions for command: dump
*/
+1 -12
View File
@@ -635,12 +635,6 @@ mkhtemp(int *fd,
p, xc, fd, st, type);
if (r == 0) {
if (retries >= MKHTEMP_SPIN_THRESHOLD) {
/* usleep can return EINTR */
close_errno = errno;
usleep((useconds_t)rlong() & 0x3FF);
errno = close_errno;
}
continue;
}
if (r < 0)
@@ -903,12 +897,7 @@ mkhtemp_fill_random(char *p, size_t xc)
for (chx = 0; chx < xc; chx++) {
retry_rand:
/* /dev/urandom if enabled, OR:
* on bsd: uses arc4random
* on linux: uses getrandom
NOTE: *aborts* on error, regardless of method
*/
r = rlong(); /* always *returns* successfully */
rset(&r, sizeof(r));
if (r >= limit)
goto retry_rand;
+1 -1
View File
@@ -84,7 +84,7 @@ hextonum(char ch_s)
if (ch == '?' || ch == 'x') {
rval = rlong();
rset(&rval, sizeof(rval));
if (errno > 0)
goto err_hextonum;
+6 -12
View File
@@ -29,23 +29,21 @@
#include "../include/common.h"
size_t
rlong(void)
void
rset(void *buf, size_t n)
{
int saved_errno = errno;
size_t len = sizeof(size_t);
#if (defined(__OpenBSD__) || defined(__FreeBSD__) || \
defined(__NetBSD__) || defined(__APPLE__) || \
defined(__DragonFly__)) && !(defined(USE_URANDOM) && \
((USE_URANDOM) > 0))
arc4random_buf(&rval, len);
arc4random_buf(buf, n);
goto out;
#else
size_t off = errno = 0;
ssize_t rc = 0;
size_t rval;
#if defined(USE_URANDOM) && \
((USE_URANDOM) > 0)
@@ -54,11 +52,11 @@ rlong(void)
if ((fd = open("/dev/urandom", O_RDONLY)) < 0)
goto err;
retry_rand:
if ((rc = read(fd, &rval, len)) < 0) {
if ((rc = read(fd, buf, n)) < 0) {
#elif defined(__linux__)
retry_rand:
if ((rc = (ssize_t)syscall(SYS_getrandom,
(char *)&rval + off, len - off, 0)) < 0) {
buf + off, n - off, 0)) < 0) {
#else
#error Unsupported operating system (possibly unsecure randomisation)
#endif
@@ -72,21 +70,17 @@ retry_rand:
goto err; /* possibly unsupported by kernel */
}
if ((off += (size_t)rc) < len)
if ((off += (size_t)rc) < n)
goto retry_rand;
goto out;
err:
err_no_cleanup(1, ECANCELED,
"Randomisation failure, possibly unsupported in your kernel.");
exit(EXIT_FAILURE);
return 0;
#endif
out:
errno = saved_errno;
return rval;
}
#endif