]> git.ipfire.org Git - people/ms/ipfire-3.x.git/blob - glibc/patches/glibc-2.11.1-mktemp_urandom.patch
Change file layout of the makefiles.
[people/ms/ipfire-3.x.git] / glibc / patches / glibc-2.11.1-mktemp_urandom.patch
1 Submitted By: Robert Connolly <robert at linuxfromscratch dot org> (ashes)
2 Date: 2010-02-19
3 Initial Package Version: 2.11.1
4 Upstream Status: Not Submitted
5 Origin: Based on http://www.uclibc.org/cgi-bin/viewcvs.cgi/trunk/uClibc/libc/
6 misc/internals/tempname.c?rev=8887&r1=5747&r2=8887
7 Description:
8 Use /dev/urandom exclusively with __gen_tempname(), for the mktemp/tmpnam
9 family, instead of hp-timing, gettimeofday(), or getpid(). return -1 if
10 /dev/urandom does not open.
11
12 diff -Naur glibc-2.11.1.orig/sysdeps/posix/tempname.c glibc-2.11.1/sysdeps/posix/tempname.c
13 --- glibc-2.11.1.orig/sysdeps/posix/tempname.c 2009-12-08 20:10:20.000000000 +0000
14 +++ glibc-2.11.1/sysdeps/posix/tempname.c 2010-02-19 17:36:44.000000000 +0000
15 @@ -51,10 +51,6 @@
16 # include <fcntl.h>
17 #endif
18
19 -#if HAVE_SYS_TIME_H || _LIBC
20 -# include <sys/time.h>
21 -#endif
22 -
23 #if HAVE_STDINT_H || _LIBC
24 # include <stdint.h>
25 #endif
26 @@ -93,11 +89,11 @@
27 # define struct_stat64 struct stat64
28 #else
29 # define struct_stat64 struct stat
30 -# define __getpid getpid
31 -# define __gettimeofday gettimeofday
32 # define __mkdir mkdir
33 # define __open open
34 # define __open64 open
35 +# define __close close
36 +# define __read read
37 # define __lxstat64(version, path, buf) lstat (path, buf)
38 # define __xstat64(version, path, buf) stat (path, buf)
39 #endif
40 @@ -106,25 +102,6 @@
41 # define __secure_getenv getenv
42 #endif
43
44 -#ifdef _LIBC
45 -# include <hp-timing.h>
46 -# if HP_TIMING_AVAIL
47 -# define RANDOM_BITS(Var) \
48 - if (__builtin_expect (value == UINT64_C (0), 0)) \
49 - { \
50 - /* If this is the first time this function is used initialize \
51 - the variable we accumulate the value in to some somewhat \
52 - random value. If we'd not do this programs at startup time \
53 - might have a reduced set of possible names, at least on slow \
54 - machines. */ \
55 - struct timeval tv; \
56 - __gettimeofday (&tv, NULL); \
57 - value = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec; \
58 - } \
59 - HP_TIMING_NOW (Var)
60 -# endif
61 -#endif
62 -
63 /* Use the widest available unsigned type if uint64_t is not
64 available. The algorithm below extracts a number less than 62**6
65 (approximately 2**35.725) from uint64_t, so ancient hosts where
66 @@ -209,6 +186,19 @@
67 static const char letters[] =
68 "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
69
70 +static unsigned int fillrand(unsigned char *buf, unsigned int len)
71 +{
72 + int fd;
73 + unsigned int result = -1;
74 + fd = __open("/dev/urandom", O_RDONLY|O_NONBLOCK|O_NOCTTY);
75 + if (fd >= 0)
76 + {
77 + result = __read(fd, buf, len);
78 + __close(fd);
79 + }
80 + return result;
81 +}
82 +
83 /* Generate a temporary file name based on TMPL. TMPL must match the
84 rules for mk[s]temp (i.e. end in "XXXXXX", possibly with a suffix).
85 The name constructed does not exist at the time of the call to
86 @@ -219,13 +209,12 @@
87 at the time of the call.
88 __GT_FILE: create the file using open(O_CREAT|O_EXCL)
89 and return a read-write fd. The file is mode 0600.
90 - __GT_DIR: create a directory, which will be mode 0700.
91 + __GT_DIR: create a directory, which will be mode 0700. */
92
93 - We use a clever algorithm to get hard-to-predict names. */
94 int
95 __gen_tempname (char *tmpl, int suffixlen, int flags, int kind)
96 {
97 - int len;
98 + int len, i;
99 char *XXXXXX;
100 static uint64_t value;
101 uint64_t random_time_bits;
102 @@ -233,6 +222,8 @@
103 int fd = -1;
104 int save_errno = errno;
105 struct_stat64 st;
106 + unsigned char randomness[6];
107 + unsigned int k;
108
109 /* A lower bound on the number of temporary files to attempt to
110 generate. The maximum total number of temporary file names that
111 @@ -260,39 +251,20 @@
112 /* This is where the Xs start. */
113 XXXXXX = &tmpl[len - 6 - suffixlen];
114
115 - /* Get some more or less random data. */
116 -#ifdef RANDOM_BITS
117 - RANDOM_BITS (random_time_bits);
118 -#else
119 -# if HAVE_GETTIMEOFDAY || _LIBC
120 - {
121 - struct timeval tv;
122 - __gettimeofday (&tv, NULL);
123 - random_time_bits = ((uint64_t) tv.tv_usec << 16) ^ tv.tv_sec;
124 - }
125 -# else
126 - random_time_bits = time (NULL);
127 -# endif
128 -#endif
129 - value += random_time_bits ^ __getpid ();
130 + /* Get some random data, and die otherwise. */
131 + if (fillrand(randomness, sizeof(randomness)) != sizeof(randomness))
132 + {
133 + __set_errno (ENODEV);
134 + return -1;
135 + }
136 + for (i = 0 ; i < sizeof(randomness) ; i++)
137 + {
138 + k = ((randomness[i]) % 62);
139 + XXXXXX[i] = letters[k];
140 + }
141
142 for (count = 0; count < attempts; value += 7777, ++count)
143 {
144 - uint64_t v = value;
145 -
146 - /* Fill in the random bits. */
147 - XXXXXX[0] = letters[v % 62];
148 - v /= 62;
149 - XXXXXX[1] = letters[v % 62];
150 - v /= 62;
151 - XXXXXX[2] = letters[v % 62];
152 - v /= 62;
153 - XXXXXX[3] = letters[v % 62];
154 - v /= 62;
155 - XXXXXX[4] = letters[v % 62];
156 - v /= 62;
157 - XXXXXX[5] = letters[v % 62];
158 -
159 switch (kind)
160 {
161 case __GT_FILE: