util/nvmutil: fix entropy issue

the time difference used here could go negative, which
would overflow in the xor op on mix, leading to a biased
entropy pool. we want to ensure that they numbers do
not overflow, because here they are cast to unsigned
which would then produce very large numbers.

Signed-off-by: Leah Rowe <leah@libreboot.org>
This commit is contained in:
Leah Rowe
2026-03-13 14:10:10 +00:00
parent 475c50932e
commit 0c9bcaaba8
+10 -1
View File
@@ -1172,6 +1172,7 @@ entropy_jitter(void)
{
struct timeval a, b;
unsigned long mix = 0;
long mix_diff;
int i;
for (i = 0; i < 8; i++) {
@@ -1179,7 +1180,15 @@ entropy_jitter(void)
getpid();
gettimeofday(&b, NULL);
mix ^= (unsigned long)(b.tv_usec - a.tv_usec);
/*
* prevent negative numbers to prevent overflow,
* which would bias rand to large numbers
*/
mix_diff = (long)(b.tv_usec - a.tv_usec);
if (mix_diff < 0)
mix_diff = -mix_diff;
mix ^= (unsigned long)(mix_diff);
mix ^= (unsigned long)&mix;
}