b2a9b0
commit 4dddd7e9cbecad4aa03ee5a9b9edb596e3d4e909
b2a9b0
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
b2a9b0
Date:   Tue Sep 29 08:56:07 2020 -0300
b2a9b0
b2a9b0
    posix: Sync tempname with gnulib [BZ #26648]
b2a9b0
b2a9b0
    It syncs with gnulib commit b1268f22f443e8e4b9e.  The try_tempname_len
b2a9b0
    now uses getrandom on each iteration to get entropy and only uses the
b2a9b0
    clock plus ASLR as source of entropy if getrandom fails.
b2a9b0
b2a9b0
    Checked on x86_64-linux-gnu and i686-linux-gnu.
b2a9b0
b2a9b0
Conflicts:
b2a9b0
	sysdeps/posix/tempname.c
b2a9b0
	  (Missing tree-wide __gettimeofday to clock_gettime change,
b2a9b0
	  commit 4a39c34c4f85de57fb4e648cfa1e774437d69680 upstream.
b2a9b0
	  File was rebased to the upstream version.)
b2a9b0
b2a9b0
diff --git a/sysdeps/posix/tempname.c b/sysdeps/posix/tempname.c
b2a9b0
index a7b404cf4410cb00..f199b25a7a227751 100644
b2a9b0
--- a/sysdeps/posix/tempname.c
b2a9b0
+++ b/sysdeps/posix/tempname.c
b2a9b0
@@ -1,4 +1,4 @@
b2a9b0
-/* Copyright (C) 1991-2018 Free Software Foundation, Inc.
b2a9b0
+/* Copyright (C) 1991-2021 Free Software Foundation, Inc.
b2a9b0
    This file is part of the GNU C Library.
b2a9b0
 
b2a9b0
    The GNU C Library is free software; you can redistribute it and/or
b2a9b0
@@ -13,10 +13,10 @@
b2a9b0
 
b2a9b0
    You should have received a copy of the GNU Lesser General Public
b2a9b0
    License along with the GNU C Library; if not, see
b2a9b0
-   <http://www.gnu.org/licenses/>.  */
b2a9b0
+   <https://www.gnu.org/licenses/>.  */
b2a9b0
 
b2a9b0
 #if !_LIBC
b2a9b0
-# include <config.h>
b2a9b0
+# include <libc-config.h>
b2a9b0
 # include "tempname.h"
b2a9b0
 #endif
b2a9b0
 
b2a9b0
@@ -24,9 +24,6 @@
b2a9b0
 #include <assert.h>
b2a9b0
 
b2a9b0
 #include <errno.h>
b2a9b0
-#ifndef __set_errno
b2a9b0
-# define __set_errno(Val) errno = (Val)
b2a9b0
-#endif
b2a9b0
 
b2a9b0
 #include <stdio.h>
b2a9b0
 #ifndef P_tmpdir
b2a9b0
@@ -36,12 +33,12 @@
b2a9b0
 # define TMP_MAX 238328
b2a9b0
 #endif
b2a9b0
 #ifndef __GT_FILE
b2a9b0
-# define __GT_FILE	0
b2a9b0
-# define __GT_DIR	1
b2a9b0
-# define __GT_NOCREATE	2
b2a9b0
+# define __GT_FILE      0
b2a9b0
+# define __GT_DIR       1
b2a9b0
+# define __GT_NOCREATE  2
b2a9b0
 #endif
b2a9b0
-#if !_LIBC && (GT_FILE != __GT_FILE || GT_DIR != __GT_DIR	\
b2a9b0
-	       || GT_NOCREATE != __GT_NOCREATE)
b2a9b0
+#if !_LIBC && (GT_FILE != __GT_FILE || GT_DIR != __GT_DIR       \
b2a9b0
+               || GT_NOCREATE != __GT_NOCREATE)
b2a9b0
 # error report this to bug-gnulib@gnu.org
b2a9b0
 #endif
b2a9b0
 
b2a9b0
@@ -50,11 +47,11 @@
b2a9b0
 #include <string.h>
b2a9b0
 
b2a9b0
 #include <fcntl.h>
b2a9b0
-#include <sys/time.h>
b2a9b0
+#include <stdalign.h>
b2a9b0
 #include <stdint.h>
b2a9b0
-#include <unistd.h>
b2a9b0
-
b2a9b0
+#include <sys/random.h>
b2a9b0
 #include <sys/stat.h>
b2a9b0
+#include <time.h>
b2a9b0
 
b2a9b0
 #if _LIBC
b2a9b0
 # define struct_stat64 struct stat64
b2a9b0
@@ -62,33 +59,38 @@
b2a9b0
 #else
b2a9b0
 # define struct_stat64 struct stat
b2a9b0
 # define __gen_tempname gen_tempname
b2a9b0
-# define __getpid getpid
b2a9b0
-# define __gettimeofday gettimeofday
b2a9b0
 # define __mkdir mkdir
b2a9b0
 # define __open open
b2a9b0
-# define __secure_getenv secure_getenv
b2a9b0
+# define __lstat64(file, buf) lstat (file, buf)
b2a9b0
+# define __stat64(file, buf) stat (file, buf)
b2a9b0
+# define __getrandom getrandom
b2a9b0
+# define __clock_gettime64 clock_gettime
b2a9b0
+# define __timespec64 timespec
b2a9b0
 #endif
b2a9b0
 
b2a9b0
-#ifdef _LIBC
b2a9b0
-# include <random-bits.h>
b2a9b0
-# define RANDOM_BITS(Var) ((Var) = random_bits ())
b2a9b0
-# else
b2a9b0
-# define RANDOM_BITS(Var) \
b2a9b0
-    {                                                                         \
b2a9b0
-      struct timeval tv;                                                      \
b2a9b0
-      __gettimeofday (&tv, NULL);                                             \
b2a9b0
-      (Var) = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;                      \
b2a9b0
-    }
b2a9b0
-#endif
b2a9b0
+/* Use getrandom if it works, falling back on a 64-bit linear
b2a9b0
+   congruential generator that starts with Var's value
b2a9b0
+   mixed in with a clock's low-order bits if available.  */
b2a9b0
+typedef uint_fast64_t random_value;
b2a9b0
+#define RANDOM_VALUE_MAX UINT_FAST64_MAX
b2a9b0
+#define BASE_62_DIGITS 10 /* 62**10 < UINT_FAST64_MAX */
b2a9b0
+#define BASE_62_POWER (62LL * 62 * 62 * 62 * 62 * 62 * 62 * 62 * 62 * 62)
b2a9b0
 
b2a9b0
-/* Use the widest available unsigned type if uint64_t is not
b2a9b0
-   available.  The algorithm below extracts a number less than 62**6
b2a9b0
-   (approximately 2**35.725) from uint64_t, so ancient hosts where
b2a9b0
-   uintmax_t is only 32 bits lose about 3.725 bits of randomness,
b2a9b0
-   which is better than not having mkstemp at all.  */
b2a9b0
-#if !defined UINT64_MAX && !defined uint64_t
b2a9b0
-# define uint64_t uintmax_t
b2a9b0
+static random_value
b2a9b0
+random_bits (random_value var)
b2a9b0
+{
b2a9b0
+  random_value r;
b2a9b0
+  /* Without GRND_NONBLOCK it can be blocked for minutes on some systems.  */
b2a9b0
+  if (__getrandom (&r, sizeof r, GRND_NONBLOCK) == sizeof r)
b2a9b0
+    return r;
b2a9b0
+#if _LIBC || (defined CLOCK_MONOTONIC && HAVE_CLOCK_GETTIME)
b2a9b0
+  /* Add entropy if getrandom did not work.  */
b2a9b0
+  struct __timespec64 tv;
b2a9b0
+  __clock_gettime64 (CLOCK_MONOTONIC, &tv;;
b2a9b0
+  var ^= tv.tv_nsec;
b2a9b0
 #endif
b2a9b0
+  return 2862933555777941757 * var + 3037000493;
b2a9b0
+}
b2a9b0
 
b2a9b0
 #if _LIBC
b2a9b0
 /* Return nonzero if DIR is an existent directory.  */
b2a9b0
@@ -107,7 +109,7 @@ direxists (const char *dir)
b2a9b0
    enough space in TMPL. */
b2a9b0
 int
b2a9b0
 __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
b2a9b0
-	       int try_tmpdir)
b2a9b0
+               int try_tmpdir)
b2a9b0
 {
b2a9b0
   const char *d;
b2a9b0
   size_t dlen, plen;
b2a9b0
@@ -121,35 +123,35 @@ __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
b2a9b0
     {
b2a9b0
       plen = strlen (pfx);
b2a9b0
       if (plen > 5)
b2a9b0
-	plen = 5;
b2a9b0
+        plen = 5;
b2a9b0
     }
b2a9b0
 
b2a9b0
   if (try_tmpdir)
b2a9b0
     {
b2a9b0
       d = __secure_getenv ("TMPDIR");
b2a9b0
       if (d != NULL && direxists (d))
b2a9b0
-	dir = d;
b2a9b0
+        dir = d;
b2a9b0
       else if (dir != NULL && direxists (dir))
b2a9b0
-	/* nothing */ ;
b2a9b0
+        /* nothing */ ;
b2a9b0
       else
b2a9b0
-	dir = NULL;
b2a9b0
+        dir = NULL;
b2a9b0
     }
b2a9b0
   if (dir == NULL)
b2a9b0
     {
b2a9b0
       if (direxists (P_tmpdir))
b2a9b0
-	dir = P_tmpdir;
b2a9b0
+        dir = P_tmpdir;
b2a9b0
       else if (strcmp (P_tmpdir, "/tmp") != 0 && direxists ("/tmp"))
b2a9b0
-	dir = "/tmp";
b2a9b0
+        dir = "/tmp";
b2a9b0
       else
b2a9b0
-	{
b2a9b0
-	  __set_errno (ENOENT);
b2a9b0
-	  return -1;
b2a9b0
-	}
b2a9b0
+        {
b2a9b0
+          __set_errno (ENOENT);
b2a9b0
+          return -1;
b2a9b0
+        }
b2a9b0
     }
b2a9b0
 
b2a9b0
   dlen = strlen (dir);
b2a9b0
   while (dlen > 1 && dir[dlen - 1] == '/')
b2a9b0
-    dlen--;			/* remove trailing slashes */
b2a9b0
+    dlen--;                     /* remove trailing slashes */
b2a9b0
 
b2a9b0
   /* check we have room for "${dir}/${pfx}XXXXXX\0" */
b2a9b0
   if (tmpl_len < dlen + 1 + plen + 6 + 1)
b2a9b0
@@ -163,39 +165,91 @@ __path_search (char *tmpl, size_t tmpl_len, const char *dir, const char *pfx,
b2a9b0
 }
b2a9b0
 #endif /* _LIBC */
b2a9b0
 
b2a9b0
+#if _LIBC
b2a9b0
+static int try_tempname_len (char *, int, void *, int (*) (char *, void *),
b2a9b0
+                             size_t);
b2a9b0
+#endif
b2a9b0
+
b2a9b0
+static int
b2a9b0
+try_file (char *tmpl, void *flags)
b2a9b0
+{
b2a9b0
+  int *openflags = flags;
b2a9b0
+  return __open (tmpl,
b2a9b0
+                 (*openflags & ~O_ACCMODE)
b2a9b0
+                 | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
b2a9b0
+}
b2a9b0
+
b2a9b0
+static int
b2a9b0
+try_dir (char *tmpl, void *flags _GL_UNUSED)
b2a9b0
+{
b2a9b0
+  return __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
b2a9b0
+}
b2a9b0
+
b2a9b0
+static int
b2a9b0
+try_nocreate (char *tmpl, void *flags _GL_UNUSED)
b2a9b0
+{
b2a9b0
+  struct_stat64 st;
b2a9b0
+
b2a9b0
+  if (__lstat64 (tmpl, &st) == 0 || errno == EOVERFLOW)
b2a9b0
+    __set_errno (EEXIST);
b2a9b0
+  return errno == ENOENT ? 0 : -1;
b2a9b0
+}
b2a9b0
+
b2a9b0
 /* These are the characters used in temporary file names.  */
b2a9b0
 static const char letters[] =
b2a9b0
 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
b2a9b0
 
b2a9b0
 /* Generate a temporary file name based on TMPL.  TMPL must match the
b2a9b0
-   rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix).
b2a9b0
+   rules for mk[s]temp (i.e., end in at least X_SUFFIX_LEN "X"s,
b2a9b0
+   possibly with a suffix).
b2a9b0
    The name constructed does not exist at the time of the call to
b2a9b0
-   __gen_tempname.  TMPL is overwritten with the result.
b2a9b0
+   this function.  TMPL is overwritten with the result.
b2a9b0
 
b2a9b0
    KIND may be one of:
b2a9b0
-   __GT_NOCREATE:	simply verify that the name does not exist
b2a9b0
-			at the time of the call.
b2a9b0
-   __GT_FILE:		create the file using open(O_CREAT|O_EXCL)
b2a9b0
-			and return a read-write fd.  The file is mode 0600.
b2a9b0
-   __GT_DIR:		create a directory, which will be mode 0700.
b2a9b0
+   __GT_NOCREATE:       simply verify that the name does not exist
b2a9b0
+                        at the time of the call.
b2a9b0
+   __GT_FILE:           create the file using open(O_CREAT|O_EXCL)
b2a9b0
+                        and return a read-write fd.  The file is mode 0600.
b2a9b0
+   __GT_DIR:            create a directory, which will be mode 0700.
b2a9b0
 
b2a9b0
    We use a clever algorithm to get hard-to-predict names. */
b2a9b0
+#ifdef _LIBC
b2a9b0
+static
b2a9b0
+#endif
b2a9b0
 int
b2a9b0
-__gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
b2a9b0
+gen_tempname_len (char *tmpl, int suffixlen, int flags, int kind,
b2a9b0
+                  size_t x_suffix_len)
b2a9b0
 {
b2a9b0
-  int len;
b2a9b0
+  static int (*const tryfunc[]) (char *, void *) =
b2a9b0
+    {
b2a9b0
+      [__GT_FILE] = try_file,
b2a9b0
+      [__GT_DIR] = try_dir,
b2a9b0
+      [__GT_NOCREATE] = try_nocreate
b2a9b0
+    };
b2a9b0
+  return try_tempname_len (tmpl, suffixlen, &flags, tryfunc[kind],
b2a9b0
+                           x_suffix_len);
b2a9b0
+}
b2a9b0
+
b2a9b0
+#ifdef _LIBC
b2a9b0
+static
b2a9b0
+#endif
b2a9b0
+int
b2a9b0
+try_tempname_len (char *tmpl, int suffixlen, void *args,
b2a9b0
+                  int (*tryfunc) (char *, void *), size_t x_suffix_len)
b2a9b0
+{
b2a9b0
+  size_t len;
b2a9b0
   char *XXXXXX;
b2a9b0
   unsigned int count;
b2a9b0
   int fd = -1;
b2a9b0
   int save_errno = errno;
b2a9b0
-  struct_stat64 st;
b2a9b0
 
b2a9b0
   /* A lower bound on the number of temporary files to attempt to
b2a9b0
      generate.  The maximum total number of temporary file names that
b2a9b0
      can exist for a given template is 62**6.  It should never be
b2a9b0
      necessary to try all of these combinations.  Instead if a reasonable
b2a9b0
      number of names is tried (we define reasonable as 62**3) fail to
b2a9b0
-     give the system administrator the chance to remove the problems.  */
b2a9b0
+     give the system administrator the chance to remove the problems.
b2a9b0
+     This value requires that X_SUFFIX_LEN be at least 3.  */
b2a9b0
 #define ATTEMPTS_MIN (62 * 62 * 62)
b2a9b0
 
b2a9b0
   /* The number of times to attempt to generate a temporary file.  To
b2a9b0
@@ -206,82 +260,75 @@ __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
b2a9b0
   unsigned int attempts = ATTEMPTS_MIN;
b2a9b0
 #endif
b2a9b0
 
b2a9b0
+  /* A random variable.  The initial value is used only the for fallback path
b2a9b0
+     on 'random_bits' on 'getrandom' failure.  Its initial value tries to use
b2a9b0
+     some entropy from the ASLR and ignore possible bits from the stack
b2a9b0
+     alignment.  */
b2a9b0
+  random_value v = ((uintptr_t) &v) / alignof (max_align_t);
b2a9b0
+
b2a9b0
+  /* How many random base-62 digits can currently be extracted from V.  */
b2a9b0
+  int vdigits = 0;
b2a9b0
+
b2a9b0
+  /* Least unfair value for V.  If V is less than this, V can generate
b2a9b0
+     BASE_62_DIGITS digits fairly.  Otherwise it might be biased.  */
b2a9b0
+  random_value const unfair_min
b2a9b0
+    = RANDOM_VALUE_MAX - RANDOM_VALUE_MAX % BASE_62_POWER;
b2a9b0
+
b2a9b0
   len = strlen (tmpl);
b2a9b0
-  if (len < 6 + suffixlen || memcmp (&tmpl[len - 6 - suffixlen], "XXXXXX", 6))
b2a9b0
+  if (len < x_suffix_len + suffixlen
b2a9b0
+      || strspn (&tmpl[len - x_suffix_len - suffixlen], "X") < x_suffix_len)
b2a9b0
     {
b2a9b0
       __set_errno (EINVAL);
b2a9b0
       return -1;
b2a9b0
     }
b2a9b0
 
b2a9b0
   /* This is where the Xs start.  */
b2a9b0
-  XXXXXX = &tmpl[len - 6 - suffixlen];
b2a9b0
+  XXXXXX = &tmpl[len - x_suffix_len - suffixlen];
b2a9b0
 
b2a9b0
-  uint64_t pid = (uint64_t) __getpid () << 32;
b2a9b0
   for (count = 0; count < attempts; ++count)
b2a9b0
     {
b2a9b0
-      uint64_t v;
b2a9b0
-      /* Get some more or less random data.  */
b2a9b0
-      RANDOM_BITS (v);
b2a9b0
-      v ^= pid;
b2a9b0
-
b2a9b0
-      /* Fill in the random bits.  */
b2a9b0
-      XXXXXX[0] = letters[v % 62];
b2a9b0
-      v /= 62;
b2a9b0
-      XXXXXX[1] = letters[v % 62];
b2a9b0
-      v /= 62;
b2a9b0
-      XXXXXX[2] = letters[v % 62];
b2a9b0
-      v /= 62;
b2a9b0
-      XXXXXX[3] = letters[v % 62];
b2a9b0
-      v /= 62;
b2a9b0
-      XXXXXX[4] = letters[v % 62];
b2a9b0
-      v /= 62;
b2a9b0
-      XXXXXX[5] = letters[v % 62];
b2a9b0
-
b2a9b0
-      switch (kind)
b2a9b0
-	{
b2a9b0
-	case __GT_FILE:
b2a9b0
-	  fd = __open (tmpl,
b2a9b0
-		       (flags & ~O_ACCMODE)
b2a9b0
-		       | O_RDWR | O_CREAT | O_EXCL, S_IRUSR | S_IWUSR);
b2a9b0
-	  break;
b2a9b0
-
b2a9b0
-	case __GT_DIR:
b2a9b0
-	  fd = __mkdir (tmpl, S_IRUSR | S_IWUSR | S_IXUSR);
b2a9b0
-	  break;
b2a9b0
-
b2a9b0
-	case __GT_NOCREATE:
b2a9b0
-	  /* This case is backward from the other three.  __gen_tempname
b2a9b0
-	     succeeds if lstat fails because the name does not exist.
b2a9b0
-	     Note the continue to bypass the common logic at the bottom
b2a9b0
-	     of the loop.  */
b2a9b0
-	  if (__lstat64 (tmpl, &st) < 0)
b2a9b0
-	    {
b2a9b0
-	      if (errno == ENOENT)
b2a9b0
-		{
b2a9b0
-		  __set_errno (save_errno);
b2a9b0
-		  return 0;
b2a9b0
-		}
b2a9b0
-	      else
b2a9b0
-		/* Give up now. */
b2a9b0
-		return -1;
b2a9b0
-	    }
b2a9b0
-	  continue;
b2a9b0
-
b2a9b0
-	default:
b2a9b0
-	  assert (! "invalid KIND in __gen_tempname");
b2a9b0
-	  abort ();
b2a9b0
-	}
b2a9b0
-
b2a9b0
+      for (size_t i = 0; i < x_suffix_len; i++)
b2a9b0
+        {
b2a9b0
+          if (vdigits == 0)
b2a9b0
+            {
b2a9b0
+              do
b2a9b0
+                v = random_bits (v);
b2a9b0
+              while (unfair_min <= v);
b2a9b0
+
b2a9b0
+              vdigits = BASE_62_DIGITS;
b2a9b0
+            }
b2a9b0
+
b2a9b0
+          XXXXXX[i] = letters[v % 62];
b2a9b0
+          v /= 62;
b2a9b0
+          vdigits--;
b2a9b0
+        }
b2a9b0
+
b2a9b0
+      fd = tryfunc (tmpl, args);
b2a9b0
       if (fd >= 0)
b2a9b0
-	{
b2a9b0
-	  __set_errno (save_errno);
b2a9b0
-	  return fd;
b2a9b0
-	}
b2a9b0
+        {
b2a9b0
+          __set_errno (save_errno);
b2a9b0
+          return fd;
b2a9b0
+        }
b2a9b0
       else if (errno != EEXIST)
b2a9b0
-	return -1;
b2a9b0
+        return -1;
b2a9b0
     }
b2a9b0
 
b2a9b0
   /* We got out of the loop because we ran out of combinations to try.  */
b2a9b0
   __set_errno (EEXIST);
b2a9b0
   return -1;
b2a9b0
 }
b2a9b0
+
b2a9b0
+int
b2a9b0
+__gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
b2a9b0
+{
b2a9b0
+  return gen_tempname_len (tmpl, suffixlen, flags, kind, 6);
b2a9b0
+}
b2a9b0
+
b2a9b0
+#if !_LIBC
b2a9b0
+int
b2a9b0
+try_tempname (char *tmpl, int suffixlen, void *args,
b2a9b0
+              int (*tryfunc) (char *, void *))
b2a9b0
+{
b2a9b0
+  return try_tempname_len (tmpl, suffixlen, args, tryfunc, 6);
b2a9b0
+}
b2a9b0
+#endif