]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
random-util: initialize srand() from RDRAND 10654/head
authorLennart Poettering <lennart@poettering.net>
Tue, 6 Nov 2018 11:08:26 +0000 (12:08 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 6 Nov 2018 11:12:18 +0000 (12:12 +0100)
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

index 23951c01811cb1bca22023ec7ffc8ced8138149c..071a41f1863a9bf77e50c0eafc495950f3af4054 100644 (file)
@@ -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;
 }