]> Savannah Git Hosting - gnulib.git/commitdiff
tempname: don't lose entropy in seed
authorPaul Eggert <eggert@cs.ucla.edu>
Mon, 22 Aug 2022 20:43:18 +0000 (15:43 -0500)
committerBruno Haible <bruno@clisp.org>
Wed, 31 Aug 2022 23:29:06 +0000 (01:29 +0200)
* lib/tempname.c (random_bits): Don't lose entropy in S
in the rare case where where the template has more than 10 Xs.
From a suggestion by Bruno Haible in:
https://bugs.gnu.org/57129#149

ChangeLog
lib/tempname.c

index 4120b4da57398206385617cd8e9f7c09008cb8c5..0e0029227db79a5d028f624acd32b8a5b6320882 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2022-08-22  Paul Eggert  <eggert@cs.ucla.edu>
 
+       tempname: don't lose entropy in seed
+       * lib/tempname.c (random_bits): Don't lose entropy in S
+       in the rare case where where the template has more than 10 Xs.
+       From a suggestion by Bruno Haible in:
+       https://bugs.gnu.org/57129#149
+
        tempname: fix multithreading, ASLR leak etc.
        Fix problems with tempname and multithreading, entropy loss,
        and missing clock data (this last on non-GNU platforms).
@@ -23,6 +29,7 @@
        Do not try to use a static var as that has issues if multithreaded.
        Instead, simply generate new random bits.
        Worry about bias only with high-quality random bits.
+
        * modules/tempname (Depends-on): Do not depend on stdalign.
 
        tempname: merge 64-bit time_t fix from glibc
index bc1f7c14dd72cfb391e759b60a862ac44416898d..0e2f29f5de00d5945d708aa22ffb9e4f7ac72687 100644 (file)
@@ -114,7 +114,7 @@ random_bits (random_value *r, random_value s)
 
      Of course we are in a state of sin here.  */
 
-  random_value v = 0;
+  random_value v = s;
 
 #if _LIBC || (defined CLOCK_REALTIME && HAVE_CLOCK_GETTIME)
   struct __timespec64 tv;
@@ -298,7 +298,9 @@ try_tempname_len (char *tmpl, int suffixlen, void *args,
   /* A random variable.  */
   random_value v = 0;
 
-  /* How many random base-62 digits can currently be extracted from V.  */
+  /* A value derived from the random variable, and how many random
+     base-62 digits can currently be extracted from VDIGBUF.  */
+  random_value vdigbuf;
   int vdigits = 0;
 
   /* Least biased value for V.  If V is less than this, V can generate
@@ -327,11 +329,12 @@ try_tempname_len (char *tmpl, int suffixlen, void *args,
               while (random_bits (&v, v) && biased_min <= v)
                 continue;
 
+              vdigbuf = v;
               vdigits = BASE_62_DIGITS;
             }
 
-          XXXXXX[i] = letters[v % 62];
-          v /= 62;
+          XXXXXX[i] = letters[vdigbuf % 62];
+          vdigbuf /= 62;
           vdigits--;
         }