1 /* $OpenBSD: getentropy_hpux.c,v 1.5 2016/08/07 03:27:21 tb 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
23 #include <sys/types.h>
24 #include <sys/param.h>
25 #include <sys/ioctl.h>
26 #include <sys/resource.h>
27 #include <sys/syscall.h>
28 #include <sys/statvfs.h>
29 #include <sys/socket.h>
30 #include <sys/mount.h>
44 #include <openssl/sha.h>
48 #include <sys/pstat.h>
51 #define min(a, b) (((a) < (b)) ? (a) : (b))
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*)))
65 int getentropy(void *buf
, size_t len
);
67 static int gotdata(char *buf
, size_t len
);
68 static int getentropy_urandom(void *buf
, size_t len
, const char *path
,
70 static int getentropy_fallback(void *buf
, size_t len
);
73 getentropy(void *buf
, size_t len
)
83 * Try to get entropy with /dev/urandom
85 ret
= getentropy_urandom(buf
, len
, "/dev/urandom", 0);
90 * Entropy collection via /dev/urandom has failed.
92 * No other API exists for collecting entropy, and we have
93 * no failsafe way to get it on hpux that is not sensitive
94 * to resource exhaustion.
96 * We have very few options:
97 * - Even syslog_r is unsafe to call at this low level, so
98 * there is no way to alert the user or program.
99 * - Cannot call abort() because some systems have unsafe
101 * - Could raise(SIGKILL) resulting in silent program termination.
102 * - Return EIO, to hint that arc4random's stir function
103 * should raise(SIGKILL)
104 * - Do the best under the circumstances....
106 * This code path exists to bring light to the issue that hpux
107 * does not provide a failsafe API for entropy collection.
109 * We hope this demonstrates that hpux should consider
110 * providing a new failsafe API which works in a chroot or
111 * when file descriptors are exhausted.
113 #undef FAIL_INSTEAD_OF_TRYING_FALLBACK
114 #ifdef FAIL_INSTEAD_OF_TRYING_FALLBACK
117 ret
= getentropy_fallback(buf
, len
);
126 * Basic sanity checking; wish we could do better.
129 gotdata(char *buf
, size_t len
)
134 for (i
= 0; i
< len
; ++i
)
142 getentropy_urandom(void *buf
, size_t len
, const char *path
, int devfscheck
)
147 int save_errno
= errno
;
158 fd
= open(path
, flags
, 0);
165 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
168 /* Lightly verify that the device node looks sane */
169 if (fstat(fd
, &st
) == -1 || !S_ISCHR(st
.st_mode
)) {
173 for (i
= 0; i
< len
; ) {
174 size_t wanted
= len
- i
;
175 ssize_t ret
= read(fd
, (char *)buf
+ i
, wanted
);
178 if (errno
== EAGAIN
|| errno
== EINTR
)
186 if (gotdata(buf
, len
) == 0) {
188 return (0); /* satisfied */
195 static const int cl
[] = {
197 #ifdef CLOCK_MONOTONIC
200 #ifdef CLOCK_MONOTONIC_RAW
212 #ifdef CLOCK_PROCESS_CPUTIME_ID
213 CLOCK_PROCESS_CPUTIME_ID
,
215 #ifdef CLOCK_THREAD_CPUTIME_ID
216 CLOCK_THREAD_CPUTIME_ID
,
221 getentropy_fallback(void *buf
, size_t len
)
223 uint8_t results
[SHA512_DIGEST_LENGTH
];
224 int save_errno
= errno
, e
, pgs
= sysconf(_SC_PAGESIZE
), faster
= 0, repeat
;
228 struct pst_vminfo pvi
;
229 struct pst_vm_status pvs
;
230 struct pst_dynamic pdy
;
235 static pid_t lastpid
;
241 if (lastpid
== pid
) {
249 for (i
= 0; i
< len
; ) {
252 for (j
= 0; j
< repeat
; j
++) {
253 HX((e
= gettimeofday(&tv
, NULL
)) == -1, tv
);
255 cnt
+= (int)tv
.tv_sec
;
256 cnt
+= (int)tv
.tv_usec
;
259 HX(pstat_getvminfo(&pvi
, sizeof(pvi
), 1, 0) != 1, pvi
);
260 HX(pstat_getprocvm(&pvs
, sizeof(pvs
), 0, 0) != 1, pvs
);
262 for (ii
= 0; ii
< sizeof(cl
)/sizeof(cl
[0]); ii
++)
263 HX(clock_gettime(cl
[ii
], &ts
) == -1, ts
);
265 HX((pid
= getpid()) == -1, pid
);
266 HX((pid
= getsid(pid
)) == -1, pid
);
267 HX((pid
= getppid()) == -1, pid
);
268 HX((pid
= getpgid(0)) == -1, pid
);
269 HX((e
= getpriority(0, 0)) == -1, e
);
271 if(pstat_getdynamic(&pdy
, sizeof(pdy
), 1, 0) != 1) {
274 HD(pdy
.psd_avg_1_min
);
275 HD(pdy
.psd_avg_5_min
);
276 HD(pdy
.psd_avg_15_min
);
282 (void) nanosleep(&ts
, NULL
);
285 HX(sigpending(&sigset
) == -1, sigset
);
286 HX(sigprocmask(SIG_BLOCK
, NULL
, &sigset
) == -1,
289 HF(getentropy
); /* an addr in this library */
290 HF(printf
); /* an addr in libc */
292 HD(p
); /* an addr on stack */
294 HD(p
); /* the addr of errno */
297 struct sockaddr_storage ss
;
298 struct statvfs stvfs
;
304 * Prime-sized mappings encourage fragmentation;
305 * thus exposing some address entropy.
311 { 17, MAP_FAILED
}, { 3, MAP_FAILED
},
312 { 11, MAP_FAILED
}, { 2, MAP_FAILED
},
313 { 5, MAP_FAILED
}, { 3, MAP_FAILED
},
314 { 7, MAP_FAILED
}, { 1, MAP_FAILED
},
315 { 57, MAP_FAILED
}, { 3, MAP_FAILED
},
316 { 131, MAP_FAILED
}, { 1, MAP_FAILED
},
319 for (m
= 0; m
< sizeof mm
/sizeof(mm
[0]); m
++) {
320 HX(mm
[m
].p
= mmap(NULL
,
322 PROT_READ
|PROT_WRITE
,
323 MAP_PRIVATE
|MAP_ANON
, -1,
325 if (mm
[m
].p
!= MAP_FAILED
) {
328 /* Touch some memory... */
331 (mm
[m
].npg
* pgs
- 1);
333 cnt
+= (int)((long)(mm
[m
].p
)
337 /* Check cnts and times... */
338 for (ii
= 0; ii
< sizeof(cl
)/sizeof(cl
[0]);
340 HX((e
= clock_gettime(cl
[ii
],
343 cnt
+= (int)ts
.tv_nsec
;
346 HX((e
= getrusage(RUSAGE_SELF
,
349 cnt
+= (int)ru
.ru_utime
.tv_sec
;
350 cnt
+= (int)ru
.ru_utime
.tv_usec
;
354 for (m
= 0; m
< sizeof mm
/sizeof(mm
[0]); m
++) {
355 if (mm
[m
].p
!= MAP_FAILED
)
356 munmap(mm
[m
].p
, mm
[m
].npg
* pgs
);
357 mm
[m
].p
= MAP_FAILED
;
360 HX(stat(".", &st
) == -1, st
);
361 HX(statvfs(".", &stvfs
) == -1, stvfs
);
363 HX(stat("/", &st
) == -1, st
);
364 HX(statvfs("/", &stvfs
) == -1, stvfs
);
366 HX((e
= fstat(0, &st
)) == -1, st
);
368 if (S_ISREG(st
.st_mode
) ||
369 S_ISFIFO(st
.st_mode
) ||
370 S_ISSOCK(st
.st_mode
)) {
371 HX(fstatvfs(0, &stvfs
) == -1,
373 HX((off
= lseek(0, (off_t
)0,
374 SEEK_CUR
)) < 0, off
);
376 if (S_ISCHR(st
.st_mode
)) {
377 HX(tcgetattr(0, &tios
) == -1,
379 } else if (S_ISSOCK(st
.st_mode
)) {
380 memset(&ss
, 0, sizeof ss
);
383 (void *)&ss
, &ssl
) == -1,
388 HX((e
= getrusage(RUSAGE_CHILDREN
,
391 cnt
+= (int)ru
.ru_utime
.tv_sec
;
392 cnt
+= (int)ru
.ru_utime
.tv_usec
;
395 /* Subsequent hashes absorb previous result */
399 HX((e
= gettimeofday(&tv
, NULL
)) == -1, tv
);
401 cnt
+= (int)tv
.tv_sec
;
402 cnt
+= (int)tv
.tv_usec
;
407 SHA512_Final(results
, &ctx
);
408 memcpy((char *)buf
+ i
, results
, min(sizeof(results
), len
- i
));
409 i
+= min(sizeof(results
), len
- i
);
411 explicit_bzero(&ctx
, sizeof ctx
);
412 explicit_bzero(results
, sizeof results
);
413 if (gotdata(buf
, len
) == 0) {
415 return (0); /* satisfied */