X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=elogind.git;a=blobdiff_plain;f=src%2Fbasic%2Frandom-util.c;h=b216be579d28c0fe2d250b263dfe0f49de2ef5fb;hp=b230044f50994a12070c3a842c18563148211438;hb=9742b1e43855b1599bd52ff95af995d9a9d35eac;hpb=1cfc78c91965df340cdde100ad6cb3ed50b28927 diff --git a/src/basic/random-util.c b/src/basic/random-util.c index b230044f5..b216be579 100644 --- a/src/basic/random-util.c +++ b/src/basic/random-util.c @@ -17,21 +17,30 @@ along with systemd; If not, see . ***/ -#include +#include #include -#include -#include #include -#include +#include +#include +#include +#include +#include + #ifdef HAVE_SYS_AUXV_H -#include +# include #endif -#include +#ifdef USE_SYS_RANDOM_H +# include +#else +# include +#endif + +#include "fd-util.h" +#include "io-util.h" +#include "missing.h" #include "random-util.h" #include "time-util.h" -#include "missing.h" -#include "util.h" int dev_urandom(void *p, size_t n) { static int have_syscall = -1; @@ -43,7 +52,7 @@ int dev_urandom(void *p, size_t n) { * never block, and will always return some data from the * kernel, regardless if the random pool is fully initialized * or not. It thus makes no guarantee for the quality of the - * returned entropy, but is good enough for or usual usecases + * returned entropy, but is good enough for our usual usecases * of seeding the hash functions for hashtable */ /* Use the getrandom() syscall unless we know we don't have @@ -92,17 +101,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();