]>
git.saurik.com Git - apple/network_cmds.git/blob - unbound/compat/getentropy_solaris.c
1 /* $OpenBSD: getentropy_solaris.c,v 1.3 2014/07/12 14:46:31 deraadt Exp $ */
4 * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org>
5 * Copyright (c) 2014 Bob Beck <beck@obtuse.com>
7 * Permission to use, copy, modify, and distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice appear in all copies.
11 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
12 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
13 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
14 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
15 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
16 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
17 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
21 #include <sys/types.h>
22 #include <sys/param.h>
23 #include <sys/ioctl.h>
24 #include <sys/resource.h>
25 #include <sys/syscall.h>
26 #include <sys/statvfs.h>
27 #include <sys/socket.h>
28 #include <sys/mount.h>
43 #define SHA512_Init SHA512Init
44 #define SHA512_Update SHA512Update
45 #define SHA512_Final SHA512Final
48 #include <sys/statfs.h>
49 #include <sys/loadavg.h>
52 #define min(a, b) (((a) < (b)) ? (a) : (b))
62 #define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
63 #define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
64 #define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
66 int getentropy(void *buf
, size_t len
);
68 #ifdef CAN_REFERENCE_MAIN
69 extern int main(int, char *argv
[]);
71 static int gotdata(char *buf
, size_t len
);
72 static int getentropy_urandom(void *buf
, size_t len
, const char *path
,
74 static int getentropy_fallback(void *buf
, size_t len
);
77 getentropy(void *buf
, size_t len
)
87 * Try to get entropy with /dev/urandom
89 * Solaris provides /dev/urandom as a symbolic link to
90 * /devices/pseudo/random@0:urandom which is provided by
91 * a devfs filesystem. Best practice is to use O_NOFOLLOW,
92 * so we must try the unpublished name directly.
94 * This can fail if the process is inside a chroot which lacks
95 * the devfs mount, or if file descriptors are exhausted.
97 ret
= getentropy_urandom(buf
, len
,
98 "/devices/pseudo/random@0:urandom", 1);
103 * Unfortunately, chroot spaces on Solaris are sometimes setup
104 * with direct device node of the well-known /dev/urandom name
105 * (perhaps to avoid dragging all of devfs into the space).
107 * This can fail if the process is inside a chroot or if file
108 * descriptors are exhausted.
110 ret
= getentropy_urandom(buf
, len
, "/dev/urandom", 0);
115 * Entropy collection via /dev/urandom has failed.
117 * No other API exists for collecting entropy, and we have
118 * no failsafe way to get it on Solaris that is not sensitive
119 * to resource exhaustion.
121 * We have very few options:
122 * - Even syslog_r is unsafe to call at this low level, so
123 * there is no way to alert the user or program.
124 * - Cannot call abort() because some systems have unsafe
126 * - Could raise(SIGKILL) resulting in silent program termination.
127 * - Return EIO, to hint that arc4random's stir function
128 * should raise(SIGKILL)
129 * - Do the best under the circumstances....
131 * This code path exists to bring light to the issue that Solaris
132 * does not provide a failsafe API for entropy collection.
134 * We hope this demonstrates that Solaris should consider
135 * providing a new failsafe API which works in a chroot or
136 * when file descriptors are exhausted.
138 #undef FAIL_INSTEAD_OF_TRYING_FALLBACK
139 #ifdef FAIL_INSTEAD_OF_TRYING_FALLBACK
142 ret
= getentropy_fallback(buf
, len
);
151 * Basic sanity checking; wish we could do better.
154 gotdata(char *buf
, size_t len
)
159 for (i
= 0; i
< len
; ++i
)
167 getentropy_urandom(void *buf
, size_t len
, const char *path
, int devfscheck
)
172 int save_errno
= errno
;
183 fd
= open(path
, flags
, 0);
190 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
193 /* Lightly verify that the device node looks sane */
194 if (fstat(fd
, &st
) == -1 || !S_ISCHR(st
.st_mode
) ||
195 (devfscheck
&& (strcmp(st
.st_fstype
, "devfs") != 0))) {
199 for (i
= 0; i
< len
; ) {
200 size_t wanted
= len
- i
;
201 ssize_t ret
= read(fd
, (char*)buf
+ i
, wanted
);
204 if (errno
== EAGAIN
|| errno
== EINTR
)
212 if (gotdata(buf
, len
) == 0) {
214 return 0; /* satisfied */
221 static const int cl
[] = {
223 #ifdef CLOCK_MONOTONIC
226 #ifdef CLOCK_MONOTONIC_RAW
238 #ifdef CLOCK_PROCESS_CPUTIME_ID
239 CLOCK_PROCESS_CPUTIME_ID
,
241 #ifdef CLOCK_THREAD_CPUTIME_ID
242 CLOCK_THREAD_CPUTIME_ID
,
247 getentropy_fallback(void *buf
, size_t len
)
249 uint8_t results
[SHA512_DIGEST_LENGTH
];
250 int save_errno
= errno
, e
, pgs
= getpagesize(), faster
= 0, repeat
;
259 static pid_t lastpid
;
265 if (lastpid
== pid
) {
273 for (i
= 0; i
< len
; ) {
276 for (j
= 0; j
< repeat
; j
++) {
277 HX((e
= gettimeofday(&tv
, NULL
)) == -1, tv
);
279 cnt
+= (int)tv
.tv_sec
;
280 cnt
+= (int)tv
.tv_usec
;
283 for (ii
= 0; ii
< sizeof(cl
)/sizeof(cl
[0]); ii
++)
284 HX(clock_gettime(cl
[ii
], &ts
) == -1, ts
);
286 HX((pid
= getpid()) == -1, pid
);
287 HX((pid
= getsid(pid
)) == -1, pid
);
288 HX((pid
= getppid()) == -1, pid
);
289 HX((pid
= getpgid(0)) == -1, pid
);
290 HX((e
= getpriority(0, 0)) == -1, e
);
291 HX((getloadavg(loadavg
, 3) == -1), loadavg
);
296 (void) nanosleep(&ts
, NULL
);
299 HX(sigpending(&sigset
) == -1, sigset
);
300 HX(sigprocmask(SIG_BLOCK
, NULL
, &sigset
) == -1,
303 #ifdef CAN_REFERENCE_MAIN
304 HF(main
); /* an addr in program */
306 HF(getentropy
); /* an addr in this library */
307 HF(printf
); /* an addr in libc */
309 HD(p
); /* an addr on stack */
311 HD(p
); /* the addr of errno */
314 struct sockaddr_storage ss
;
315 struct statvfs stvfs
;
321 * Prime-sized mappings encourage fragmentation;
322 * thus exposing some address entropy.
328 { 17, MAP_FAILED
}, { 3, MAP_FAILED
},
329 { 11, MAP_FAILED
}, { 2, MAP_FAILED
},
330 { 5, MAP_FAILED
}, { 3, MAP_FAILED
},
331 { 7, MAP_FAILED
}, { 1, MAP_FAILED
},
332 { 57, MAP_FAILED
}, { 3, MAP_FAILED
},
333 { 131, MAP_FAILED
}, { 1, MAP_FAILED
},
336 for (m
= 0; m
< sizeof mm
/sizeof(mm
[0]); m
++) {
337 HX(mm
[m
].p
= mmap(NULL
,
339 PROT_READ
|PROT_WRITE
,
340 MAP_PRIVATE
|MAP_ANON
, -1,
342 if (mm
[m
].p
!= MAP_FAILED
) {
345 /* Touch some memory... */
348 (mm
[m
].npg
* pgs
- 1);
350 cnt
+= (int)((long)(mm
[m
].p
)
354 /* Check cnts and times... */
355 for (ii
= 0; ii
< sizeof(cl
)/sizeof(cl
[0]);
357 HX((e
= clock_gettime(cl
[ii
],
360 cnt
+= (int)ts
.tv_nsec
;
363 HX((e
= getrusage(RUSAGE_SELF
,
366 cnt
+= (int)ru
.ru_utime
.tv_sec
;
367 cnt
+= (int)ru
.ru_utime
.tv_usec
;
371 for (m
= 0; m
< sizeof mm
/sizeof(mm
[0]); m
++) {
372 if (mm
[m
].p
!= MAP_FAILED
)
373 munmap(mm
[m
].p
, mm
[m
].npg
* pgs
);
374 mm
[m
].p
= MAP_FAILED
;
377 HX(stat(".", &st
) == -1, st
);
378 HX(statvfs(".", &stvfs
) == -1, stvfs
);
380 HX(stat("/", &st
) == -1, st
);
381 HX(statvfs("/", &stvfs
) == -1, stvfs
);
383 HX((e
= fstat(0, &st
)) == -1, st
);
385 if (S_ISREG(st
.st_mode
) ||
386 S_ISFIFO(st
.st_mode
) ||
387 S_ISSOCK(st
.st_mode
)) {
388 HX(fstatvfs(0, &stvfs
) == -1,
390 HX((off
= lseek(0, (off_t
)0,
391 SEEK_CUR
)) < 0, off
);
393 if (S_ISCHR(st
.st_mode
)) {
394 HX(tcgetattr(0, &tios
) == -1,
396 } else if (S_ISSOCK(st
.st_mode
)) {
397 memset(&ss
, 0, sizeof ss
);
400 (void *)&ss
, &ssl
) == -1,
405 HX((e
= getrusage(RUSAGE_CHILDREN
,
408 cnt
+= (int)ru
.ru_utime
.tv_sec
;
409 cnt
+= (int)ru
.ru_utime
.tv_usec
;
412 /* Subsequent hashes absorb previous result */
416 HX((e
= gettimeofday(&tv
, NULL
)) == -1, tv
);
418 cnt
+= (int)tv
.tv_sec
;
419 cnt
+= (int)tv
.tv_usec
;
424 SHA512_Final(results
, &ctx
);
425 memcpy((char*)buf
+ i
, results
, min(sizeof(results
), len
- i
));
426 i
+= min(sizeof(results
), len
- i
);
428 memset(results
, 0, sizeof results
);
429 if (gotdata(buf
, len
) == 0) {
431 return 0; /* satisfied */