chiark / gitweb /
basic: getauxval(AT_RANDOM) is apparently not necessarily aligned
authorLennart Poettering <lennart@poettering.net>
Thu, 28 Jan 2016 16:00:38 +0000 (17:00 +0100)
committerSven Eden <yamakuzure@gmx.net>
Wed, 17 May 2017 13:22:15 +0000 (15:22 +0200)
Let's make sure we read it in a way compatible with non-aligned memory.

Fixes: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=812928
src/basic/random-util.c

index 2f5c16e2afbdc7fc3230d7111007960ce8e7b996..34cc7cbcee32fc3008c1a3fcba5ed1055ca8520c 100644 (file)
@@ -94,17 +94,18 @@ void initialize_srand(void) {
         if (srand_called)
                 return;
 
-        x = 0;
-
 #ifdef HAVE_SYS_AUXV_H
-        /* The kernel provides us with a bit of entropy in auxv, so
-         * let's try to make use of that to seed the pseudo-random
-         * generator. It's better than nothing... */
+        /* The kernel provides us with 16 bytes of entropy in auxv, so let's try to make use of that to seed the
+         * pseudo-random generator. It's better than nothing... */
 
         auxv = (void*) getauxval(AT_RANDOM);
-        if (auxv)
-                x ^= *(unsigned*) auxv;
+        if (auxv) {
+                assert_cc(sizeof(x) < 16);
+                memcpy(&x, auxv, sizeof(x));
+        } else
 #endif
+                x = 0;
+
 
         x ^= (unsigned) now(CLOCK_REALTIME);
         x ^= (unsigned) gettid();