From 359653aaacad463d916323f03c0ac3c47405aafa Mon Sep 17 00:00:00 2001 From: Adhemerval Zanella Date: Wed, 16 Jan 2019 18:10:56 +0000 Subject: Do not use HP_TIMING_NOW for random bits This patch removes the HP_TIMING_BITS usage for fast random bits and replace with clock_gettime (CLOCK_MONOTONIC). It has unspecified starting time and nano-second accuracy, so its randomness is significantly better than gettimeofday. Althoug it should incur in more overhead (specially for architecture that support hp-timing), the symbol is also common implemented as a vDSO. Checked on aarch64-linux-gnu, x86_64-linux-gnu, and i686-linux-gnu. I also checked on a i686-gnu build. * include/random-bits.h: New file. * resolv/res_mkquery.c [HP_TIMING_AVAIL] (RANDOM_BITS, (__res_context_mkquery): Remove usage hp-timing usage and replace with random_bits. * resolv/res_send.c [HP_TIMING_AVAIL] (nameserver_offset): Likewise. * sysdeps/posix/tempname.c [HP_TIMING_AVAIL] (__gen_tempname): Likewise. --- sysdeps/posix/tempname.c | 40 ++++++++++++---------------------------- 1 file changed, 12 insertions(+), 28 deletions(-) (limited to 'sysdeps') diff --git a/sysdeps/posix/tempname.c b/sysdeps/posix/tempname.c index 2ed39d1a42..de346949b2 100644 --- a/sysdeps/posix/tempname.c +++ b/sysdeps/posix/tempname.c @@ -71,22 +71,15 @@ #endif #ifdef _LIBC -# include -# if HP_TIMING_AVAIL -# define RANDOM_BITS(Var) \ - if (__glibc_unlikely (value == UINT64_C (0))) \ - { \ - /* If this is the first time this function is used initialize \ - the variable we accumulate the value in to some somewhat \ - random value. If we'd not do this programs at startup time \ - might have a reduced set of possible names, at least on slow \ - machines. */ \ - struct timeval tv; \ - __gettimeofday (&tv, NULL); \ - value = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; \ - } \ - HP_TIMING_NOW (Var) -# endif +# include +# define RANDOM_BITS(Var) ((Var) = random_bits ()) +# else +# define RANDOM_BITS(Var) \ + { \ + struct timeval tv; \ + __gettimeofday (&tv, NULL); \ + (Var) = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; \ + } #endif /* Use the widest available unsigned type if uint64_t is not @@ -193,8 +186,7 @@ __gen_tempname (char *tmpl, int suffixlen, int flags, int kind) { int len; char *XXXXXX; - static uint64_t value; - uint64_t random_time_bits; + uint64_t value; unsigned int count; int fd = -1; int save_errno = errno; @@ -227,16 +219,8 @@ __gen_tempname (char *tmpl, int suffixlen, int flags, int kind) XXXXXX = &tmpl[len - 6 - suffixlen]; /* Get some more or less random data. */ -#ifdef RANDOM_BITS - RANDOM_BITS (random_time_bits); -#else - { - struct timeval tv; - __gettimeofday (&tv, NULL); - random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; - } -#endif - value += random_time_bits ^ __getpid (); + RANDOM_BITS (value); + value ^= (uint64_t)__getpid () << 32; for (count = 0; count < attempts; value += 7777, ++count) { -- cgit 1.4.1