1 /* $OpenBSD: getentropy_solaris.c,v 1.13 2018/11/20 08:04:28 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.
19 * Emulation of getentropy(2) as documented at:
20 * http://man.openbsd.org/getentropy.2
24 #include <sys/types.h>
25 #include <sys/param.h>
26 #include <sys/ioctl.h>
27 #include <sys/resource.h>
28 #include <sys/syscall.h>
29 #include <sys/statvfs.h>
30 #include <sys/socket.h>
31 #include <sys/mount.h>
48 #ifdef HAVE_SYS_SHA2_H
50 #define SHA512_Init SHA512Init
51 #define SHA512_Update SHA512Update
52 #define SHA512_Final SHA512Final
54 #include "openssl/sha.h"
58 #include <sys/statfs.h>
59 #include <sys/loadavg.h>
62 #define min(a, b) (((a) < (b)) ? (a) : (b))
72 #define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
73 #define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
74 #define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
76 int getentropy(void *buf, size_t len);
78 static int getentropy_urandom(void *buf, size_t len, const char *path,
80 static int getentropy_fallback(void *buf, size_t len);
81 static int getentropy_phdr(struct dl_phdr_info *info, size_t size, void *data);
84 getentropy(void *buf, size_t len)
94 * Try to get entropy with /dev/urandom
96 * Solaris provides /dev/urandom as a symbolic link to
97 * /devices/pseudo/random@0:urandom which is provided by
98 * a devfs filesystem. Best practice is to use O_NOFOLLOW,
99 * so we must try the unpublished name directly.
101 * This can fail if the process is inside a chroot which lacks
102 * the devfs mount, or if file descriptors are exhausted.
104 ret = getentropy_urandom(buf, len,
105 "/devices/pseudo/random@0:urandom", 1);
110 * Unfortunately, chroot spaces on Solaris are sometimes setup
111 * with direct device node of the well-known /dev/urandom name
112 * (perhaps to avoid dragging all of devfs into the space).
114 * This can fail if the process is inside a chroot or if file
115 * descriptors are exhausted.
117 ret = getentropy_urandom(buf, len, "/dev/urandom", 0);
122 * Entropy collection via /dev/urandom has failed.
124 * No other API exists for collecting entropy, and we have
125 * no failsafe way to get it on Solaris that is not sensitive
126 * to resource exhaustion.
128 * We have very few options:
129 * - Even syslog_r is unsafe to call at this low level, so
130 * there is no way to alert the user or program.
131 * - Cannot call abort() because some systems have unsafe
133 * - Could raise(SIGKILL) resulting in silent program termination.
134 * - Return EIO, to hint that arc4random's stir function
135 * should raise(SIGKILL)
136 * - Do the best under the circumstances....
138 * This code path exists to bring light to the issue that Solaris
139 * does not provide a failsafe API for entropy collection.
141 * We hope this demonstrates that Solaris should consider
142 * providing a new failsafe API which works in a chroot or
143 * when file descriptors are exhausted.
145 #undef FAIL_INSTEAD_OF_TRYING_FALLBACK
146 #ifdef FAIL_INSTEAD_OF_TRYING_FALLBACK
149 ret = getentropy_fallback(buf, len);
158 getentropy_urandom(void *buf, size_t len, const char *path, int devfscheck)
163 int save_errno = errno;
174 fd = open(path, flags, 0);
181 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
184 /* Lightly verify that the device node looks sane */
185 if (fstat(fd, &st) == -1 || !S_ISCHR(st.st_mode) ||
186 (devfscheck && (strcmp(st.st_fstype, "devfs") != 0))) {
190 for (i = 0; i < len; ) {
191 size_t wanted = len - i;
192 ssize_t ret = read(fd, (char *)buf + i, wanted);
195 if (errno == EAGAIN || errno == EINTR)
204 return (0); /* satisfied */
210 static const int cl[] = {
212 #ifdef CLOCK_MONOTONIC
215 #ifdef CLOCK_MONOTONIC_RAW
227 #ifdef CLOCK_PROCESS_CPUTIME_ID
228 CLOCK_PROCESS_CPUTIME_ID,
230 #ifdef CLOCK_THREAD_CPUTIME_ID
231 CLOCK_THREAD_CPUTIME_ID,
236 getentropy_phdr(struct dl_phdr_info *info, size_t size, void *data)
238 SHA512_CTX *ctx = data;
240 SHA512_Update(ctx, &info->dlpi_addr, sizeof (info->dlpi_addr));
245 getentropy_fallback(void *buf, size_t len)
247 uint8_t results[SHA512_DIGEST_LENGTH];
248 int save_errno = errno, e, pgs = getpagesize(), faster = 0, repeat;
257 static pid_t lastpid;
263 if (lastpid == pid) {
271 for (i = 0; i < len; ) {
274 for (j = 0; j < repeat; j++) {
275 HX((e = gettimeofday(&tv, NULL)) == -1, tv);
277 cnt += (int)tv.tv_sec;
278 cnt += (int)tv.tv_usec;
281 dl_iterate_phdr(getentropy_phdr, &ctx);
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 HF(getentropy); /* an addr in this library */
304 HF(printf); /* an addr in libc */
306 HD(p); /* an addr on stack */
308 HD(p); /* the addr of errno */
311 struct sockaddr_storage ss;
312 struct statvfs stvfs;
318 * Prime-sized mappings encourage fragmentation;
319 * thus exposing some address entropy.
325 { 17, MAP_FAILED }, { 3, MAP_FAILED },
326 { 11, MAP_FAILED }, { 2, MAP_FAILED },
327 { 5, MAP_FAILED }, { 3, MAP_FAILED },
328 { 7, MAP_FAILED }, { 1, MAP_FAILED },
329 { 57, MAP_FAILED }, { 3, MAP_FAILED },
330 { 131, MAP_FAILED }, { 1, MAP_FAILED },
333 for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) {
334 HX(mm[m].p = mmap(NULL,
336 PROT_READ|PROT_WRITE,
337 MAP_PRIVATE|MAP_ANON, -1,
339 if (mm[m].p != MAP_FAILED) {
342 /* Touch some memory... */
345 (mm[m].npg * pgs - 1);
347 cnt += (int)((long)(mm[m].p)
351 /* Check cnts and times... */
352 for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]);
354 HX((e = clock_gettime(cl[ii],
357 cnt += (int)ts.tv_nsec;
360 HX((e = getrusage(RUSAGE_SELF,
363 cnt += (int)ru.ru_utime.tv_sec;
364 cnt += (int)ru.ru_utime.tv_usec;
368 for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) {
369 if (mm[m].p != MAP_FAILED)
370 munmap(mm[m].p, mm[m].npg * pgs);
371 mm[m].p = MAP_FAILED;
374 HX(stat(".", &st) == -1, st);
375 HX(statvfs(".", &stvfs) == -1, stvfs);
377 HX(stat("/", &st) == -1, st);
378 HX(statvfs("/", &stvfs) == -1, stvfs);
380 HX((e = fstat(0, &st)) == -1, st);
382 if (S_ISREG(st.st_mode) ||
383 S_ISFIFO(st.st_mode) ||
384 S_ISSOCK(st.st_mode)) {
385 HX(fstatvfs(0, &stvfs) == -1,
387 HX((off = lseek(0, (off_t)0,
388 SEEK_CUR)) < 0, off);
390 if (S_ISCHR(st.st_mode)) {
391 HX(tcgetattr(0, &tios) == -1,
393 } else if (S_ISSOCK(st.st_mode)) {
394 memset(&ss, 0, sizeof ss);
397 (void *)&ss, &ssl) == -1,
402 HX((e = getrusage(RUSAGE_CHILDREN,
405 cnt += (int)ru.ru_utime.tv_sec;
406 cnt += (int)ru.ru_utime.tv_usec;
409 /* Subsequent hashes absorb previous result */
413 HX((e = gettimeofday(&tv, NULL)) == -1, tv);
415 cnt += (int)tv.tv_sec;
416 cnt += (int)tv.tv_usec;
421 SHA512_Final(results, &ctx);
422 memcpy((char *)buf + i, results, min(sizeof(results), len - i));
423 i += min(sizeof(results), len - i);
425 explicit_bzero(&ctx, sizeof ctx);
426 explicit_bzero(results, sizeof results);
428 return (0); /* satisfied */