From 54bf23151f1730cfc69cd06bec4589c768ab3d67 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 6 Nov 2018 12:08:07 +0100 Subject: [PATCH 1/2] random-util: we don't intend to write to auxv, hence make it const --- src/basic/random-util.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/basic/random-util.c b/src/basic/random-util.c index aa04cc23185..23951c01811 100644 --- a/src/basic/random-util.c +++ b/src/basic/random-util.c @@ -142,7 +142,7 @@ void initialize_srand(void) { static bool srand_called = false; unsigned x; #if HAVE_SYS_AUXV_H - void *auxv; + const void *auxv; #endif if (srand_called) @@ -153,7 +153,7 @@ void initialize_srand(void) { * try to make use of that to seed the pseudo-random generator. It's * better than nothing... */ - auxv = (void*) getauxval(AT_RANDOM); + auxv = (const void*) getauxval(AT_RANDOM); if (auxv) { assert_cc(sizeof(x) <= 16); memcpy(&x, auxv, sizeof(x)); From 92025e8f521c55c9360ea749d139d796f62efb96 Mon Sep 17 00:00:00 2001 From: Lennart Poettering Date: Tue, 6 Nov 2018 12:08:26 +0100 Subject: [PATCH 2/2] random-util: initialize srand() from RDRAND It's cheap to get RDRAND and given that srand() is anyway not really useful for trusted randomness let's use RDRAND for it, after all we have all the hard work for that already in place. --- src/basic/random-util.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/basic/random-util.c b/src/basic/random-util.c index 23951c01811..071a41f1863 100644 --- a/src/basic/random-util.c +++ b/src/basic/random-util.c @@ -144,6 +144,7 @@ void initialize_srand(void) { #if HAVE_SYS_AUXV_H const void *auxv; #endif + uint64_t k; if (srand_called) return; @@ -164,6 +165,9 @@ void initialize_srand(void) { x ^= (unsigned) now(CLOCK_REALTIME); x ^= (unsigned) gettid(); + if (rdrand64(&k) >= 0) + x ^= (unsigned) k; + srand(x); srand_called = true; }