[uClibc-cvs] svn commit: trunk/uClibc/libc/misc/internals

vapier at uclibc.org vapier at uclibc.org
Sat Apr 16 03:59:42 UTC 2005


Author: vapier
Date: 2005-04-15 21:59:42 -0600 (Fri, 15 Apr 2005)
New Revision: 10108

Log:
In Bug 116, dicksnippe writes:

uClibc's mkstemp/mktemp tries to read /dev/urandom (or /dev/random) to 
generate random contents for the .XXXXXX part of its argument.  In a 
chrooted environment /dev/[u]random might not be available.  Thus the 
mkstemp call fails.

Add back in the braindamaged gettimeofday/getpid code, but only as a 
fallback for when reading /dev/[u]random fail for whatever reasons.


Modified:
   trunk/uClibc/libc/misc/internals/tempname.c


Changeset:
Modified: trunk/uClibc/libc/misc/internals/tempname.c
===================================================================
--- trunk/uClibc/libc/misc/internals/tempname.c	2005-04-16 03:02:25 UTC (rev 10107)
+++ trunk/uClibc/libc/misc/internals/tempname.c	2005-04-16 03:59:42 UTC (rev 10108)
@@ -26,6 +26,11 @@
  * Don't use brain damaged getpid() based randomness.
  */
 
+/* April 15, 2005     Mike Frysinger
+ *
+ * Use brain damaged getpid() if real random fails.
+ */
+
 #include <stddef.h>
 #include <stdint.h>
 #include <stdio.h>
@@ -132,6 +137,30 @@
     return result;
 }
 
+static void brain_damaged_fillrand(unsigned char *buf, unsigned int len)
+{
+	int i, k;
+	struct timeval tv;
+	uint32_t high, low, rh;
+	static uint64_t value;
+	gettimeofday(&tv, NULL);
+	value += ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec ^ getpid();
+	low = value & UINT32_MAX;
+	high = value >> 32;
+	for (i = 0; i < len; ++i) {
+		rh = high % 62;
+		high /= 62;
+#define L ((UINT32_MAX % 62 + 1) % 62)
+		k = (low % 62) + (L * rh);
+#undef L
+#define H ((UINT32_MAX / 62) + ((UINT32_MAX % 62 + 1) / 62))
+		low = (low / 62) + (H * rh) + (k / 62);
+#undef H
+		k %= 62;
+		buf[i] = letters[k];
+	}
+}
+
 /* Generate a temporary file name based on TMPL.  TMPL must match the
    rules for mk[s]temp (i.e. end in "XXXXXX").  The name constructed
    does not exist at the time of the call to __gen_tempname.  TMPL is
@@ -164,8 +193,9 @@
     XXXXXX = &tmpl[len - 6];
 
     /* Get some random data.  */
-    if (fillrand(randomness,  sizeof(randomness)) != sizeof(randomness)) {
-	goto all_done;
+	if (fillrand(randomness, sizeof(randomness)) != sizeof(randomness)) {
+		/* if random device nodes failed us, lets use the braindamaged ver */
+		brain_damaged_fillrand(randomness, sizeof(randomness));
     }
     for (i = 0 ; i < sizeof(randomness) ; i++) {
 	k = ((randomness[i]) % 62);
@@ -219,7 +249,6 @@
     }
 
     /* We got out of the loop because we ran out of combinations to try.  */
-all_done:
     __set_errno (EEXIST);
     return -1;
 }




More information about the uClibc-cvs mailing list