From cec9a25c2acadb6d62d25d9a43c8641b6078bd7d Mon Sep 17 00:00:00 2001 From: Leah Rowe Date: Sat, 28 Mar 2026 03:10:46 +0000 Subject: nvmutil: clamp rand (rejection sampling) clamp rand to eliminate modulo sampling; high values on the randomisation will bias the result. not really critical for mac addresses, but there's no reason not to have this. this patches reduces the chance that two libreboot users will generate the same mac addresses! Signed-off-by: Leah Rowe --- util/libreboot-utils/lib/num.c | 6 ++---- util/libreboot-utils/lib/rand.c | 1 + 2 files changed, 3 insertions(+), 4 deletions(-) (limited to 'util') diff --git a/util/libreboot-utils/lib/num.c b/util/libreboot-utils/lib/num.c index d297ab86..f53f0cee 100644 --- a/util/libreboot-utils/lib/num.c +++ b/util/libreboot-utils/lib/num.c @@ -41,10 +41,8 @@ hextonum(char ch_s) if ((unsigned int)(ch - 'a') <= 5) return ch - 'a' + 10; - if (ch == '?' || ch == 'x') { - rset(&rval, sizeof(rval)); - return rval & 0xf; - } + if (ch == '?' || ch == 'x') + return rsize(16); /* <-- with rejection sampling! */ return 16; } diff --git a/util/libreboot-utils/lib/rand.c b/util/libreboot-utils/lib/rand.c index 9304d83a..10831e44 100644 --- a/util/libreboot-utils/lib/rand.c +++ b/util/libreboot-utils/lib/rand.c @@ -86,6 +86,7 @@ rsize(size_t n) if (!n) err_no_cleanup(0, EFAULT, "rsize: division by zero"); + /* rejection sampling (clamp rand to eliminate modulo bias) */ for (; rval >= SIZE_MAX - (SIZE_MAX % n); rset(&rval, sizeof(rval))); return rval % n; -- cgit v1.2.1