]> git.ipfire.org Git - thirdparty/e2fsprogs.git/blob - lib/uuid/gen_uuid.c
Set FD_CLOEXEC on the /dev/random file descriptor used by libuuid
[thirdparty/e2fsprogs.git] / lib / uuid / gen_uuid.c
1 /*
2 * gen_uuid.c --- generate a DCE-compatible uuid
3 *
4 * Copyright (C) 1996, 1997, 1998, 1999 Theodore Ts'o.
5 *
6 * %Begin-Header%
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, and the entire permission notice in its entirety,
12 * including the disclaimer of warranties.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. The name of the author may not be used to endorse or promote
17 * products derived from this software without specific prior
18 * written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
21 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF
23 * WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE
24 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
26 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 * USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH
31 * DAMAGE.
32 * %End-Header%
33 */
34
35 /*
36 * Force inclusion of SVID stuff since we need it if we're compiling in
37 * gcc-wall wall mode
38 */
39 #define _SVID_SOURCE
40
41 #ifdef HAVE_UNISTD_H
42 #include <unistd.h>
43 #endif
44 #ifdef HAVE_STDLIB_H
45 #include <stdlib.h>
46 #endif
47 #include <string.h>
48 #include <fcntl.h>
49 #include <errno.h>
50 #include <sys/types.h>
51 #include <sys/time.h>
52 #include <sys/stat.h>
53 #include <sys/file.h>
54 #ifdef HAVE_SYS_IOCTL_H
55 #include <sys/ioctl.h>
56 #endif
57 #ifdef HAVE_SYS_SOCKET_H
58 #include <sys/socket.h>
59 #endif
60 #ifdef HAVE_SYS_SOCKIO_H
61 #include <sys/sockio.h>
62 #endif
63 #ifdef HAVE_NET_IF_H
64 #include <net/if.h>
65 #endif
66 #ifdef HAVE_NETINET_IN_H
67 #include <netinet/in.h>
68 #endif
69 #ifdef HAVE_NET_IF_DL_H
70 #include <net/if_dl.h>
71 #endif
72
73 #include "uuidP.h"
74
75 #ifdef HAVE_SRANDOM
76 #define srand(x) srandom(x)
77 #define rand() random()
78 #endif
79
80 static int get_random_fd(void)
81 {
82 struct timeval tv;
83 static int fd = -2;
84 int i;
85
86 if (fd == -2) {
87 gettimeofday(&tv, 0);
88 fd = open("/dev/urandom", O_RDONLY);
89 if (fd == -1)
90 fd = open("/dev/random", O_RDONLY | O_NONBLOCK);
91 if (fd >= 0) {
92 i = fcntl(fd, F_GETFD);
93 if (i >= 0)
94 fcntl(fd, F_SETFD, i | FD_CLOEXEC);
95 }
96 srand((getpid() << 16) ^ getuid() ^ tv.tv_sec ^ tv.tv_usec);
97 }
98 /* Crank the random number generator a few times */
99 gettimeofday(&tv, 0);
100 for (i = (tv.tv_sec ^ tv.tv_usec) & 0x1F; i > 0; i--)
101 rand();
102 return fd;
103 }
104
105
106 /*
107 * Generate a series of random bytes. Use /dev/urandom if possible,
108 * and if not, use srandom/random.
109 */
110 static void get_random_bytes(void *buf, int nbytes)
111 {
112 int i, n = nbytes, fd = get_random_fd();
113 int lose_counter = 0;
114 unsigned char *cp = (unsigned char *) buf;
115
116 if (fd >= 0) {
117 while (n > 0) {
118 i = read(fd, cp, n);
119 if (i <= 0) {
120 if (lose_counter++ > 16)
121 break;
122 continue;
123 }
124 n -= i;
125 cp += i;
126 lose_counter = 0;
127 }
128 }
129
130 /*
131 * We do this all the time, but this is the only source of
132 * randomness if /dev/random/urandom is out to lunch.
133 */
134 for (cp = buf, i = 0; i < nbytes; i++)
135 *cp++ ^= (rand() >> 7) & 0xFF;
136 return;
137 }
138
139 /*
140 * Get the ethernet hardware address, if we can find it...
141 */
142 static int get_node_id(unsigned char *node_id)
143 {
144 #ifdef HAVE_NET_IF_H
145 int sd;
146 struct ifreq ifr, *ifrp;
147 struct ifconf ifc;
148 char buf[1024];
149 int n, i;
150 unsigned char *a;
151 #ifdef HAVE_NET_IF_DL_H
152 struct sockaddr_dl *sdlp;
153 #endif
154
155 /*
156 * BSD 4.4 defines the size of an ifreq to be
157 * max(sizeof(ifreq), sizeof(ifreq.ifr_name)+ifreq.ifr_addr.sa_len
158 * However, under earlier systems, sa_len isn't present, so the size is
159 * just sizeof(struct ifreq)
160 */
161 #ifdef HAVE_SA_LEN
162 #ifndef max
163 #define max(a,b) ((a) > (b) ? (a) : (b))
164 #endif
165 #define ifreq_size(i) max(sizeof(struct ifreq),\
166 sizeof((i).ifr_name)+(i).ifr_addr.sa_len)
167 #else
168 #define ifreq_size(i) sizeof(struct ifreq)
169 #endif /* HAVE_SA_LEN*/
170
171 sd = socket(AF_INET, SOCK_DGRAM, IPPROTO_IP);
172 if (sd < 0) {
173 return -1;
174 }
175 memset(buf, 0, sizeof(buf));
176 ifc.ifc_len = sizeof(buf);
177 ifc.ifc_buf = buf;
178 if (ioctl (sd, SIOCGIFCONF, (char *)&ifc) < 0) {
179 close(sd);
180 return -1;
181 }
182 n = ifc.ifc_len;
183 for (i = 0; i < n; i+= ifreq_size(*ifrp) ) {
184 ifrp = (struct ifreq *)((char *) ifc.ifc_buf+i);
185 strncpy(ifr.ifr_name, ifrp->ifr_name, IFNAMSIZ);
186 #ifdef SIOCGIFHWADDR
187 if (ioctl(sd, SIOCGIFHWADDR, &ifr) < 0)
188 continue;
189 a = (unsigned char *) &ifr.ifr_hwaddr.sa_data;
190 #else
191 #ifdef SIOCGENADDR
192 if (ioctl(sd, SIOCGENADDR, &ifr) < 0)
193 continue;
194 a = (unsigned char *) ifr.ifr_enaddr;
195 #else
196 #ifdef HAVE_NET_IF_DL_H
197 sdlp = (struct sockaddr_dl *) &ifrp->ifr_addr;
198 if ((sdlp->sdl_family != AF_LINK) || (sdlp->sdl_alen != 6))
199 continue;
200 a = (unsigned char *) &sdlp->sdl_data[sdlp->sdl_nlen];
201 #else
202 /*
203 * XXX we don't have a way of getting the hardware
204 * address
205 */
206 close(sd);
207 return 0;
208 #endif /* HAVE_NET_IF_DL_H */
209 #endif /* SIOCGENADDR */
210 #endif /* SIOCGIFHWADDR */
211 if (!a[0] && !a[1] && !a[2] && !a[3] && !a[4] && !a[5])
212 continue;
213 if (node_id) {
214 memcpy(node_id, a, 6);
215 close(sd);
216 return 1;
217 }
218 }
219 close(sd);
220 #endif
221 return 0;
222 }
223
224 /* Assume that the gettimeofday() has microsecond granularity */
225 #define MAX_ADJUSTMENT 10
226
227 static int get_clock(uint32_t *clock_high, uint32_t *clock_low, uint16_t *ret_clock_seq)
228 {
229 static int adjustment = 0;
230 static struct timeval last = {0, 0};
231 static uint16_t clock_seq;
232 struct timeval tv;
233 unsigned long long clock_reg;
234
235 try_again:
236 gettimeofday(&tv, 0);
237 if ((last.tv_sec == 0) && (last.tv_usec == 0)) {
238 get_random_bytes(&clock_seq, sizeof(clock_seq));
239 clock_seq &= 0x3FFF;
240 last = tv;
241 last.tv_sec--;
242 }
243 if ((tv.tv_sec < last.tv_sec) ||
244 ((tv.tv_sec == last.tv_sec) &&
245 (tv.tv_usec < last.tv_usec))) {
246 clock_seq = (clock_seq+1) & 0x3FFF;
247 adjustment = 0;
248 last = tv;
249 } else if ((tv.tv_sec == last.tv_sec) &&
250 (tv.tv_usec == last.tv_usec)) {
251 if (adjustment >= MAX_ADJUSTMENT)
252 goto try_again;
253 adjustment++;
254 } else {
255 adjustment = 0;
256 last = tv;
257 }
258
259 clock_reg = tv.tv_usec*10 + adjustment;
260 clock_reg += ((unsigned long long) tv.tv_sec)*10000000;
261 clock_reg += (((unsigned long long) 0x01B21DD2) << 32) + 0x13814000;
262
263 *clock_high = clock_reg >> 32;
264 *clock_low = clock_reg;
265 *ret_clock_seq = clock_seq;
266 return 0;
267 }
268
269 void uuid_generate_time(uuid_t out)
270 {
271 static unsigned char node_id[6];
272 static int has_init = 0;
273 struct uuid uu;
274 uint32_t clock_mid;
275
276 if (!has_init) {
277 if (get_node_id(node_id) <= 0) {
278 get_random_bytes(node_id, 6);
279 /*
280 * Set multicast bit, to prevent conflicts
281 * with IEEE 802 addresses obtained from
282 * network cards
283 */
284 node_id[0] |= 0x01;
285 }
286 has_init = 1;
287 }
288 get_clock(&clock_mid, &uu.time_low, &uu.clock_seq);
289 uu.clock_seq |= 0x8000;
290 uu.time_mid = (uint16_t) clock_mid;
291 uu.time_hi_and_version = ((clock_mid >> 16) & 0x0FFF) | 0x1000;
292 memcpy(uu.node, node_id, 6);
293 uuid_pack(&uu, out);
294 }
295
296 void uuid_generate_random(uuid_t out)
297 {
298 uuid_t buf;
299 struct uuid uu;
300
301 get_random_bytes(buf, sizeof(buf));
302 uuid_unpack(buf, &uu);
303
304 uu.clock_seq = (uu.clock_seq & 0x3FFF) | 0x8000;
305 uu.time_hi_and_version = (uu.time_hi_and_version & 0x0FFF) | 0x4000;
306 uuid_pack(&uu, out);
307 }
308
309 /*
310 * This is the generic front-end to uuid_generate_random and
311 * uuid_generate_time. It uses uuid_generate_random only if
312 * /dev/urandom is available, since otherwise we won't have
313 * high-quality randomness.
314 */
315 void uuid_generate(uuid_t out)
316 {
317 if (get_random_fd() >= 0)
318 uuid_generate_random(out);
319 else
320 uuid_generate_time(out);
321 }