1 /* $OpenBSD: getentropy_solaris.c,v 1.5 2014/07/13 08:24:20 beck Exp $ */ 2 3 /* 4 * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org> 5 * Copyright (c) 2014 Bob Beck <beck@obtuse.com> 6 * 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. 10 * 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. 18 */ 19 20 #include <sys/types.h> 21 #include <sys/param.h> 22 #include <sys/ioctl.h> 23 #include <sys/resource.h> 24 #include <sys/syscall.h> 25 #include <sys/statvfs.h> 26 #include <sys/socket.h> 27 #include <sys/mount.h> 28 #include <sys/mman.h> 29 #include <sys/stat.h> 30 #include <sys/time.h> 31 #include <stdlib.h> 32 #include <stdint.h> 33 #include <stdio.h> 34 #include <termios.h> 35 #include <fcntl.h> 36 #include <signal.h> 37 #include <string.h> 38 #include <errno.h> 39 #include <unistd.h> 40 #include <time.h> 41 #include <sys/sha2.h> 42 #define SHA512_Init SHA512Init 43 #define SHA512_Update SHA512Update 44 #define SHA512_Final SHA512Final 45 46 #include <sys/vfs.h> 47 #include <sys/statfs.h> 48 #include <sys/loadavg.h> 49 50 #define REPEAT 5 51 #define min(a, b) (((a) < (b)) ? (a) : (b)) 52 53 #define HX(a, b) \ 54 do { \ 55 if ((a)) \ 56 HD(errno); \ 57 else \ 58 HD(b); \ 59 } while (0) 60 61 #define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l))) 62 #define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x))) 63 #define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*))) 64 65 int getentropy(void *buf, size_t len); 66 67 #if 0 68 extern int main(int, char *argv[]); 69 #endif 70 static int gotdata(char *buf, size_t len); 71 static int getentropy_urandom(void *buf, size_t len, const char *path, 72 int devfscheck); 73 static int getentropy_fallback(void *buf, size_t len); 74 75 int 76 getentropy(void *buf, size_t len) 77 { 78 int ret = -1; 79 80 if (len > 256) { 81 errno = EIO; 82 return -1; 83 } 84 85 /* 86 * Try to get entropy with /dev/urandom 87 * 88 * Solaris provides /dev/urandom as a symbolic link to 89 * /devices/pseudo/random@0:urandom which is provided by 90 * a devfs filesystem. Best practice is to use O_NOFOLLOW, 91 * so we must try the unpublished name directly. 92 * 93 * This can fail if the process is inside a chroot which lacks 94 * the devfs mount, or if file descriptors are exhausted. 95 */ 96 ret = getentropy_urandom(buf, len, 97 "/devices/pseudo/random@0:urandom", 1); 98 if (ret != -1) 99 return (ret); 100 101 /* 102 * Unfortunately, chroot spaces on Solaris are sometimes setup 103 * with direct device node of the well-known /dev/urandom name 104 * (perhaps to avoid dragging all of devfs into the space). 105 * 106 * This can fail if the process is inside a chroot or if file 107 * descriptors are exhausted. 108 */ 109 ret = getentropy_urandom(buf, len, "/dev/urandom", 0); 110 if (ret != -1) 111 return (ret); 112 113 /* 114 * Entropy collection via /dev/urandom has failed. 115 * 116 * No other API exists for collecting entropy, and we have 117 * no failsafe way to get it on Solaris that is not sensitive 118 * to resource exhaustion. 119 * 120 * We have very few options: 121 * - Even syslog_r is unsafe to call at this low level, so 122 * there is no way to alert the user or program. 123 * - Cannot call abort() because some systems have unsafe 124 * corefiles. 125 * - Could raise(SIGKILL) resulting in silent program termination. 126 * - Return EIO, to hint that arc4random's stir function 127 * should raise(SIGKILL) 128 * - Do the best under the circumstances.... 129 * 130 * This code path exists to bring light to the issue that Solaris 131 * does not provide a failsafe API for entropy collection. 132 * 133 * We hope this demonstrates that Solaris should consider 134 * providing a new failsafe API which works in a chroot or 135 * when file descriptors are exhausted. 136 */ 137 #undef FAIL_INSTEAD_OF_TRYING_FALLBACK 138 #ifdef FAIL_INSTEAD_OF_TRYING_FALLBACK 139 raise(SIGKILL); 140 #endif 141 ret = getentropy_fallback(buf, len); 142 if (ret != -1) 143 return (ret); 144 145 errno = EIO; 146 return (ret); 147 } 148 149 /* 150 * Basic sanity checking; wish we could do better. 151 */ 152 static int 153 gotdata(char *buf, size_t len) 154 { 155 char any_set = 0; 156 size_t i; 157 158 for (i = 0; i < len; ++i) 159 any_set |= buf[i]; 160 if (any_set == 0) 161 return -1; 162 return 0; 163 } 164 165 static int 166 getentropy_urandom(void *buf, size_t len, const char *path, int devfscheck) 167 { 168 struct stat st; 169 size_t i; 170 int fd, flags; 171 int save_errno = errno; 172 173 start: 174 175 flags = O_RDONLY; 176 #ifdef O_NOFOLLOW 177 flags |= O_NOFOLLOW; 178 #endif 179 #ifdef O_CLOEXEC 180 flags |= O_CLOEXEC; 181 #endif 182 fd = open(path, flags, 0); 183 if (fd == -1) { 184 if (errno == EINTR) 185 goto start; 186 goto nodevrandom; 187 } 188 #ifndef O_CLOEXEC 189 fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); 190 #endif 191 192 /* Lightly verify that the device node looks sane */ 193 if (fstat(fd, &st) == -1 || !S_ISCHR(st.st_mode) || 194 (devfscheck && (strcmp(st.st_fstype, "devfs") != 0))) { 195 close(fd); 196 goto nodevrandom; 197 } 198 for (i = 0; i < len; ) { 199 size_t wanted = len - i; 200 ssize_t ret = read(fd, (char *)buf + i, wanted); 201 202 if (ret == -1) { 203 if (errno == EAGAIN || errno == EINTR) 204 continue; 205 close(fd); 206 goto nodevrandom; 207 } 208 i += ret; 209 } 210 close(fd); 211 if (gotdata(buf, len) == 0) { 212 errno = save_errno; 213 return 0; /* satisfied */ 214 } 215 nodevrandom: 216 errno = EIO; 217 return -1; 218 } 219 220 static const int cl[] = { 221 CLOCK_REALTIME, 222 #ifdef CLOCK_MONOTONIC 223 CLOCK_MONOTONIC, 224 #endif 225 #ifdef CLOCK_MONOTONIC_RAW 226 CLOCK_MONOTONIC_RAW, 227 #endif 228 #ifdef CLOCK_TAI 229 CLOCK_TAI, 230 #endif 231 #ifdef CLOCK_VIRTUAL 232 CLOCK_VIRTUAL, 233 #endif 234 #ifdef CLOCK_UPTIME 235 CLOCK_UPTIME, 236 #endif 237 #ifdef CLOCK_PROCESS_CPUTIME_ID 238 CLOCK_PROCESS_CPUTIME_ID, 239 #endif 240 #ifdef CLOCK_THREAD_CPUTIME_ID 241 CLOCK_THREAD_CPUTIME_ID, 242 #endif 243 }; 244 245 static int 246 getentropy_fallback(void *buf, size_t len) 247 { 248 uint8_t results[SHA512_DIGEST_LENGTH]; 249 int save_errno = errno, e, pgs = getpagesize(), faster = 0, repeat; 250 static int cnt; 251 struct timespec ts; 252 struct timeval tv; 253 double loadavg[3]; 254 struct rusage ru; 255 sigset_t sigset; 256 struct stat st; 257 SHA512_CTX ctx; 258 static pid_t lastpid; 259 pid_t pid; 260 size_t i, ii, m; 261 char *p; 262 263 pid = getpid(); 264 if (lastpid == pid) { 265 faster = 1; 266 repeat = 2; 267 } else { 268 faster = 0; 269 lastpid = pid; 270 repeat = REPEAT; 271 } 272 for (i = 0; i < len; ) { 273 int j; 274 SHA512_Init(&ctx); 275 for (j = 0; j < repeat; j++) { 276 HX((e = gettimeofday(&tv, NULL)) == -1, tv); 277 if (e != -1) { 278 cnt += (int)tv.tv_sec; 279 cnt += (int)tv.tv_usec; 280 } 281 282 for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]); ii++) 283 HX(clock_gettime(cl[ii], &ts) == -1, ts); 284 285 HX((pid = getpid()) == -1, pid); 286 HX((pid = getsid(pid)) == -1, pid); 287 HX((pid = getppid()) == -1, pid); 288 HX((pid = getpgid(0)) == -1, pid); 289 HX((e = getpriority(0, 0)) == -1, e); 290 HX((getloadavg(loadavg, 3) == -1), loadavg); 291 292 if (!faster) { 293 ts.tv_sec = 0; 294 ts.tv_nsec = 1; 295 (void) nanosleep(&ts, NULL); 296 } 297 298 HX(sigpending(&sigset) == -1, sigset); 299 HX(sigprocmask(SIG_BLOCK, NULL, &sigset) == -1, 300 sigset); 301 302 #if 0 303 HF(main); /* an addr in program */ 304 #endif 305 HF(getentropy); /* an addr in this library */ 306 HF(printf); /* an addr in libc */ 307 p = (char *)&p; 308 HD(p); /* an addr on stack */ 309 p = (char *)&errno; 310 HD(p); /* the addr of errno */ 311 312 if (i == 0) { 313 struct sockaddr_storage ss; 314 struct statvfs stvfs; 315 struct termios tios; 316 socklen_t ssl; 317 off_t off; 318 319 /* 320 * Prime-sized mappings encourage fragmentation; 321 * thus exposing some address entropy. 322 */ 323 struct mm { 324 size_t npg; 325 void *p; 326 } mm[] = { 327 { 17, MAP_FAILED }, { 3, MAP_FAILED }, 328 { 11, MAP_FAILED }, { 2, MAP_FAILED }, 329 { 5, MAP_FAILED }, { 3, MAP_FAILED }, 330 { 7, MAP_FAILED }, { 1, MAP_FAILED }, 331 { 57, MAP_FAILED }, { 3, MAP_FAILED }, 332 { 131, MAP_FAILED }, { 1, MAP_FAILED }, 333 }; 334 335 for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) { 336 HX(mm[m].p = mmap(NULL, 337 mm[m].npg * pgs, 338 PROT_READ|PROT_WRITE, 339 MAP_PRIVATE|MAP_ANON, -1, 340 (off_t)0), mm[m].p); 341 if (mm[m].p != MAP_FAILED) { 342 size_t mo; 343 344 /* Touch some memory... */ 345 p = mm[m].p; 346 mo = cnt % 347 (mm[m].npg * pgs - 1); 348 p[mo] = 1; 349 cnt += (int)((long)(mm[m].p) 350 / pgs); 351 } 352 353 /* Check cnts and times... */ 354 for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]); 355 ii++) { 356 HX((e = clock_gettime(cl[ii], 357 &ts)) == -1, ts); 358 if (e != -1) 359 cnt += (int)ts.tv_nsec; 360 } 361 362 HX((e = getrusage(RUSAGE_SELF, 363 &ru)) == -1, ru); 364 if (e != -1) { 365 cnt += (int)ru.ru_utime.tv_sec; 366 cnt += (int)ru.ru_utime.tv_usec; 367 } 368 } 369 370 for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) { 371 if (mm[m].p != MAP_FAILED) 372 munmap(mm[m].p, mm[m].npg * pgs); 373 mm[m].p = MAP_FAILED; 374 } 375 376 HX(stat(".", &st) == -1, st); 377 HX(statvfs(".", &stvfs) == -1, stvfs); 378 379 HX(stat("/", &st) == -1, st); 380 HX(statvfs("/", &stvfs) == -1, stvfs); 381 382 HX((e = fstat(0, &st)) == -1, st); 383 if (e == -1) { 384 if (S_ISREG(st.st_mode) || 385 S_ISFIFO(st.st_mode) || 386 S_ISSOCK(st.st_mode)) { 387 HX(fstatvfs(0, &stvfs) == -1, 388 stvfs); 389 HX((off = lseek(0, (off_t)0, 390 SEEK_CUR)) < 0, off); 391 } 392 if (S_ISCHR(st.st_mode)) { 393 HX(tcgetattr(0, &tios) == -1, 394 tios); 395 } else if (S_ISSOCK(st.st_mode)) { 396 memset(&ss, 0, sizeof ss); 397 ssl = sizeof(ss); 398 HX(getpeername(0, 399 (void *)&ss, &ssl) == -1, 400 ss); 401 } 402 } 403 404 HX((e = getrusage(RUSAGE_CHILDREN, 405 &ru)) == -1, ru); 406 if (e != -1) { 407 cnt += (int)ru.ru_utime.tv_sec; 408 cnt += (int)ru.ru_utime.tv_usec; 409 } 410 } else { 411 /* Subsequent hashes absorb previous result */ 412 HD(results); 413 } 414 415 HX((e = gettimeofday(&tv, NULL)) == -1, tv); 416 if (e != -1) { 417 cnt += (int)tv.tv_sec; 418 cnt += (int)tv.tv_usec; 419 } 420 421 HD(cnt); 422 } 423 SHA512_Final(results, &ctx); 424 memcpy((char *)buf + i, results, min(sizeof(results), len - i)); 425 i += min(sizeof(results), len - i); 426 } 427 memset(results, 0, sizeof results); 428 if (gotdata(buf, len) == 0) { 429 errno = save_errno; 430 return 0; /* satisfied */ 431 } 432 errno = EIO; 433 return -1; 434 } 435