diff options
author | bcook <> | 2015-01-06 21:08:11 +0000 |
---|---|---|
committer | bcook <> | 2015-01-06 21:08:11 +0000 |
commit | 7ab5dcfb9695ddd10bc3141625c1a17be5b67bde (patch) | |
tree | af19259100728f885a25228ac4fa4e371670ec6b /src/lib | |
parent | a6e31cd9243757b5df0b259fe6a94f654b53fcc4 (diff) | |
download | openbsd-7ab5dcfb9695ddd10bc3141625c1a17be5b67bde.tar.gz openbsd-7ab5dcfb9695ddd10bc3141625c1a17be5b67bde.tar.bz2 openbsd-7ab5dcfb9695ddd10bc3141625c1a17be5b67bde.zip |
add initial HP-UX getentropy/arc4random support.
patch from Kinichiro Inoguchi, tested on HP-UX 11.31
ok deraadt@
Diffstat (limited to 'src/lib')
-rw-r--r-- | src/lib/libcrypto/arc4random/arc4random_hpux.h | 79 | ||||
-rw-r--r-- | src/lib/libcrypto/arc4random/getentropy_hpux.c | 417 | ||||
-rw-r--r-- | src/lib/libcrypto/crypto/arc4random_hpux.h | 79 | ||||
-rw-r--r-- | src/lib/libcrypto/crypto/getentropy_hpux.c | 417 |
4 files changed, 992 insertions, 0 deletions
diff --git a/src/lib/libcrypto/arc4random/arc4random_hpux.h b/src/lib/libcrypto/arc4random/arc4random_hpux.h new file mode 100644 index 0000000000..b6841f583c --- /dev/null +++ b/src/lib/libcrypto/arc4random/arc4random_hpux.h | |||
@@ -0,0 +1,79 @@ | |||
1 | /* $OpenBSD: arc4random_hpux.h,v 1.1 2015/01/06 21:08:11 bcook Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> | ||
5 | * Copyright (c) 2008, Damien Miller <djm@openbsd.org> | ||
6 | * Copyright (c) 2013, Markus Friedl <markus@openbsd.org> | ||
7 | * | ||
8 | * Permission to use, copy, modify, and distribute this software for any | ||
9 | * purpose with or without fee is hereby granted, provided that the above | ||
10 | * copyright notice and this permission notice appear in all copies. | ||
11 | * | ||
12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Stub functions for portability. | ||
23 | */ | ||
24 | |||
25 | #include <sys/mman.h> | ||
26 | |||
27 | #include <pthread.h> | ||
28 | #include <signal.h> | ||
29 | |||
30 | static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER; | ||
31 | #define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx) | ||
32 | #define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx) | ||
33 | |||
34 | #define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f)) | ||
35 | |||
36 | static inline void | ||
37 | _getentropy_fail(void) | ||
38 | { | ||
39 | raise(SIGKILL); | ||
40 | } | ||
41 | |||
42 | static volatile sig_atomic_t _rs_forked; | ||
43 | |||
44 | static inline void | ||
45 | _rs_forkhandler(void) | ||
46 | { | ||
47 | _rs_forked = 1; | ||
48 | } | ||
49 | |||
50 | static inline void | ||
51 | _rs_forkdetect(void) | ||
52 | { | ||
53 | static pid_t _rs_pid = 0; | ||
54 | pid_t pid = getpid(); | ||
55 | |||
56 | if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) { | ||
57 | _rs_pid = pid; | ||
58 | _rs_forked = 0; | ||
59 | if (rs) | ||
60 | memset(rs, 0, sizeof(*rs)); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | static inline int | ||
65 | _rs_allocate(struct _rs **rsp, struct _rsx **rsxp) | ||
66 | { | ||
67 | if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE, | ||
68 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | ||
69 | return (-1); | ||
70 | |||
71 | if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE, | ||
72 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) { | ||
73 | munmap(*rsp, sizeof(**rsp)); | ||
74 | return (-1); | ||
75 | } | ||
76 | |||
77 | _ARC4_ATFORK(_rs_forkhandler); | ||
78 | return (0); | ||
79 | } | ||
diff --git a/src/lib/libcrypto/arc4random/getentropy_hpux.c b/src/lib/libcrypto/arc4random/getentropy_hpux.c new file mode 100644 index 0000000000..46ab8f54d8 --- /dev/null +++ b/src/lib/libcrypto/arc4random/getentropy_hpux.c | |||
@@ -0,0 +1,417 @@ | |||
1 | /* $OpenBSD: getentropy_hpux.c,v 1.1 2015/01/06 21:08:11 bcook 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 | * Emulation of getentropy(2) as documented at: | ||
20 | * http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man2/getentropy.2 | ||
21 | */ | ||
22 | |||
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> | ||
31 | #include <sys/mman.h> | ||
32 | #include <sys/stat.h> | ||
33 | #include <sys/time.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <stdint.h> | ||
36 | #include <stdio.h> | ||
37 | #include <termios.h> | ||
38 | #include <fcntl.h> | ||
39 | #include <signal.h> | ||
40 | #include <string.h> | ||
41 | #include <errno.h> | ||
42 | #include <unistd.h> | ||
43 | #include <time.h> | ||
44 | #include <openssl/sha.h> | ||
45 | |||
46 | #include <sys/vfs.h> | ||
47 | |||
48 | #include <sys/pstat.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 | static int gotdata(char *buf, size_t len); | ||
68 | static int getentropy_urandom(void *buf, size_t len, const char *path, | ||
69 | int devfscheck); | ||
70 | static int getentropy_fallback(void *buf, size_t len); | ||
71 | |||
72 | int | ||
73 | getentropy(void *buf, size_t len) | ||
74 | { | ||
75 | int ret = -1; | ||
76 | |||
77 | if (len > 256) { | ||
78 | errno = EIO; | ||
79 | return -1; | ||
80 | } | ||
81 | |||
82 | /* | ||
83 | * Try to get entropy with /dev/urandom | ||
84 | */ | ||
85 | ret = getentropy_urandom(buf, len, "/dev/urandom", 0); | ||
86 | if (ret != -1) | ||
87 | return (ret); | ||
88 | |||
89 | /* | ||
90 | * Entropy collection via /dev/urandom has failed. | ||
91 | * | ||
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. | ||
95 | * | ||
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 | ||
100 | * corefiles. | ||
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.... | ||
105 | * | ||
106 | * This code path exists to bring light to the issue that hpux | ||
107 | * does not provide a failsafe API for entropy collection. | ||
108 | * | ||
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. | ||
112 | */ | ||
113 | #undef FAIL_INSTEAD_OF_TRYING_FALLBACK | ||
114 | #ifdef FAIL_INSTEAD_OF_TRYING_FALLBACK | ||
115 | raise(SIGKILL); | ||
116 | #endif | ||
117 | ret = getentropy_fallback(buf, len); | ||
118 | if (ret != -1) | ||
119 | return (ret); | ||
120 | |||
121 | errno = EIO; | ||
122 | return (ret); | ||
123 | } | ||
124 | |||
125 | /* | ||
126 | * Basic sanity checking; wish we could do better. | ||
127 | */ | ||
128 | static int | ||
129 | gotdata(char *buf, size_t len) | ||
130 | { | ||
131 | char any_set = 0; | ||
132 | size_t i; | ||
133 | |||
134 | for (i = 0; i < len; ++i) | ||
135 | any_set |= buf[i]; | ||
136 | if (any_set == 0) | ||
137 | return -1; | ||
138 | return 0; | ||
139 | } | ||
140 | |||
141 | static int | ||
142 | getentropy_urandom(void *buf, size_t len, const char *path, int devfscheck) | ||
143 | { | ||
144 | struct stat st; | ||
145 | size_t i; | ||
146 | int fd, flags; | ||
147 | int save_errno = errno; | ||
148 | |||
149 | start: | ||
150 | |||
151 | flags = O_RDONLY; | ||
152 | #ifdef O_NOFOLLOW | ||
153 | flags |= O_NOFOLLOW; | ||
154 | #endif | ||
155 | #ifdef O_CLOEXEC | ||
156 | flags |= O_CLOEXEC; | ||
157 | #endif | ||
158 | fd = open(path, flags, 0); | ||
159 | if (fd == -1) { | ||
160 | if (errno == EINTR) | ||
161 | goto start; | ||
162 | goto nodevrandom; | ||
163 | } | ||
164 | #ifndef O_CLOEXEC | ||
165 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); | ||
166 | #endif | ||
167 | |||
168 | /* Lightly verify that the device node looks sane */ | ||
169 | if (fstat(fd, &st) == -1 || !S_ISCHR(st.st_mode)) { | ||
170 | close(fd); | ||
171 | goto nodevrandom; | ||
172 | } | ||
173 | for (i = 0; i < len; ) { | ||
174 | size_t wanted = len - i; | ||
175 | ssize_t ret = read(fd, (char *)buf + i, wanted); | ||
176 | |||
177 | if (ret == -1) { | ||
178 | if (errno == EAGAIN || errno == EINTR) | ||
179 | continue; | ||
180 | close(fd); | ||
181 | goto nodevrandom; | ||
182 | } | ||
183 | i += ret; | ||
184 | } | ||
185 | close(fd); | ||
186 | if (gotdata(buf, len) == 0) { | ||
187 | errno = save_errno; | ||
188 | return 0; /* satisfied */ | ||
189 | } | ||
190 | nodevrandom: | ||
191 | errno = EIO; | ||
192 | return -1; | ||
193 | } | ||
194 | |||
195 | static const int cl[] = { | ||
196 | CLOCK_REALTIME, | ||
197 | #ifdef CLOCK_MONOTONIC | ||
198 | CLOCK_MONOTONIC, | ||
199 | #endif | ||
200 | #ifdef CLOCK_MONOTONIC_RAW | ||
201 | CLOCK_MONOTONIC_RAW, | ||
202 | #endif | ||
203 | #ifdef CLOCK_TAI | ||
204 | CLOCK_TAI, | ||
205 | #endif | ||
206 | #ifdef CLOCK_VIRTUAL | ||
207 | CLOCK_VIRTUAL, | ||
208 | #endif | ||
209 | #ifdef CLOCK_UPTIME | ||
210 | CLOCK_UPTIME, | ||
211 | #endif | ||
212 | #ifdef CLOCK_PROCESS_CPUTIME_ID | ||
213 | CLOCK_PROCESS_CPUTIME_ID, | ||
214 | #endif | ||
215 | #ifdef CLOCK_THREAD_CPUTIME_ID | ||
216 | CLOCK_THREAD_CPUTIME_ID, | ||
217 | #endif | ||
218 | }; | ||
219 | |||
220 | static int | ||
221 | getentropy_fallback(void *buf, size_t len) | ||
222 | { | ||
223 | uint8_t results[SHA512_DIGEST_LENGTH]; | ||
224 | int save_errno = errno, e, pgs = sysconf(_SC_PAGESIZE), faster = 0, repeat; | ||
225 | static int cnt; | ||
226 | struct timespec ts; | ||
227 | struct timeval tv; | ||
228 | struct pst_vm_status pvs; | ||
229 | struct pst_dynamic pdy; | ||
230 | struct rusage ru; | ||
231 | sigset_t sigset; | ||
232 | struct stat st; | ||
233 | SHA512_CTX ctx; | ||
234 | static pid_t lastpid; | ||
235 | pid_t pid; | ||
236 | size_t i, ii, m; | ||
237 | char *p; | ||
238 | |||
239 | pid = getpid(); | ||
240 | if (lastpid == pid) { | ||
241 | faster = 1; | ||
242 | repeat = 2; | ||
243 | } else { | ||
244 | faster = 0; | ||
245 | lastpid = pid; | ||
246 | repeat = REPEAT; | ||
247 | } | ||
248 | for (i = 0; i < len; ) { | ||
249 | int j; | ||
250 | SHA512_Init(&ctx); | ||
251 | for (j = 0; j < repeat; j++) { | ||
252 | HX((e = gettimeofday(&tv, NULL)) == -1, tv); | ||
253 | if (e != -1) { | ||
254 | cnt += (int)tv.tv_sec; | ||
255 | cnt += (int)tv.tv_usec; | ||
256 | } | ||
257 | |||
258 | HX(pstat_getprocvm(&pvs, sizeof(pvs), 0, 0) != 1, pvs.pst_space); | ||
259 | |||
260 | for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]); ii++) | ||
261 | HX(clock_gettime(cl[ii], &ts) == -1, ts); | ||
262 | |||
263 | HX((pid = getpid()) == -1, pid); | ||
264 | HX((pid = getsid(pid)) == -1, pid); | ||
265 | HX((pid = getppid()) == -1, pid); | ||
266 | HX((pid = getpgid(0)) == -1, pid); | ||
267 | HX((e = getpriority(0, 0)) == -1, e); | ||
268 | |||
269 | if(pstat_getdynamic(&pdy, sizeof(pdy), 1, 0) != 1) { | ||
270 | HD(errno); | ||
271 | } else { | ||
272 | HD(pdy.psd_avg_1_min); | ||
273 | HD(pdy.psd_avg_5_min); | ||
274 | HD(pdy.psd_avg_15_min); | ||
275 | } | ||
276 | |||
277 | if (!faster) { | ||
278 | ts.tv_sec = 0; | ||
279 | ts.tv_nsec = 1; | ||
280 | (void) nanosleep(&ts, NULL); | ||
281 | } | ||
282 | |||
283 | HX(sigpending(&sigset) == -1, sigset); | ||
284 | HX(sigprocmask(SIG_BLOCK, NULL, &sigset) == -1, | ||
285 | sigset); | ||
286 | |||
287 | HF(getentropy); /* an addr in this library */ | ||
288 | HF(printf); /* an addr in libc */ | ||
289 | p = (char *)&p; | ||
290 | HD(p); /* an addr on stack */ | ||
291 | p = (char *)&errno; | ||
292 | HD(p); /* the addr of errno */ | ||
293 | |||
294 | if (i == 0) { | ||
295 | struct sockaddr_storage ss; | ||
296 | struct statvfs stvfs; | ||
297 | struct termios tios; | ||
298 | socklen_t ssl; | ||
299 | off_t off; | ||
300 | |||
301 | /* | ||
302 | * Prime-sized mappings encourage fragmentation; | ||
303 | * thus exposing some address entropy. | ||
304 | */ | ||
305 | struct mm { | ||
306 | size_t npg; | ||
307 | void *p; | ||
308 | } mm[] = { | ||
309 | { 17, MAP_FAILED }, { 3, MAP_FAILED }, | ||
310 | { 11, MAP_FAILED }, { 2, MAP_FAILED }, | ||
311 | { 5, MAP_FAILED }, { 3, MAP_FAILED }, | ||
312 | { 7, MAP_FAILED }, { 1, MAP_FAILED }, | ||
313 | { 57, MAP_FAILED }, { 3, MAP_FAILED }, | ||
314 | { 131, MAP_FAILED }, { 1, MAP_FAILED }, | ||
315 | }; | ||
316 | |||
317 | for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) { | ||
318 | HX(mm[m].p = mmap(NULL, | ||
319 | mm[m].npg * pgs, | ||
320 | PROT_READ|PROT_WRITE, | ||
321 | MAP_PRIVATE|MAP_ANON, -1, | ||
322 | (off_t)0), mm[m].p); | ||
323 | if (mm[m].p != MAP_FAILED) { | ||
324 | size_t mo; | ||
325 | |||
326 | /* Touch some memory... */ | ||
327 | p = mm[m].p; | ||
328 | mo = cnt % | ||
329 | (mm[m].npg * pgs - 1); | ||
330 | p[mo] = 1; | ||
331 | cnt += (int)((long)(mm[m].p) | ||
332 | / pgs); | ||
333 | } | ||
334 | |||
335 | /* Check cnts and times... */ | ||
336 | for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]); | ||
337 | ii++) { | ||
338 | HX((e = clock_gettime(cl[ii], | ||
339 | &ts)) == -1, ts); | ||
340 | if (e != -1) | ||
341 | cnt += (int)ts.tv_nsec; | ||
342 | } | ||
343 | |||
344 | HX((e = getrusage(RUSAGE_SELF, | ||
345 | &ru)) == -1, ru); | ||
346 | if (e != -1) { | ||
347 | cnt += (int)ru.ru_utime.tv_sec; | ||
348 | cnt += (int)ru.ru_utime.tv_usec; | ||
349 | } | ||
350 | } | ||
351 | |||
352 | for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) { | ||
353 | if (mm[m].p != MAP_FAILED) | ||
354 | munmap(mm[m].p, mm[m].npg * pgs); | ||
355 | mm[m].p = MAP_FAILED; | ||
356 | } | ||
357 | |||
358 | HX(stat(".", &st) == -1, st); | ||
359 | HX(statvfs(".", &stvfs) == -1, stvfs); | ||
360 | |||
361 | HX(stat("/", &st) == -1, st); | ||
362 | HX(statvfs("/", &stvfs) == -1, stvfs); | ||
363 | |||
364 | HX((e = fstat(0, &st)) == -1, st); | ||
365 | if (e == -1) { | ||
366 | if (S_ISREG(st.st_mode) || | ||
367 | S_ISFIFO(st.st_mode) || | ||
368 | S_ISSOCK(st.st_mode)) { | ||
369 | HX(fstatvfs(0, &stvfs) == -1, | ||
370 | stvfs); | ||
371 | HX((off = lseek(0, (off_t)0, | ||
372 | SEEK_CUR)) < 0, off); | ||
373 | } | ||
374 | if (S_ISCHR(st.st_mode)) { | ||
375 | HX(tcgetattr(0, &tios) == -1, | ||
376 | tios); | ||
377 | } else if (S_ISSOCK(st.st_mode)) { | ||
378 | memset(&ss, 0, sizeof ss); | ||
379 | ssl = sizeof(ss); | ||
380 | HX(getpeername(0, | ||
381 | (void *)&ss, &ssl) == -1, | ||
382 | ss); | ||
383 | } | ||
384 | } | ||
385 | |||
386 | HX((e = getrusage(RUSAGE_CHILDREN, | ||
387 | &ru)) == -1, ru); | ||
388 | if (e != -1) { | ||
389 | cnt += (int)ru.ru_utime.tv_sec; | ||
390 | cnt += (int)ru.ru_utime.tv_usec; | ||
391 | } | ||
392 | } else { | ||
393 | /* Subsequent hashes absorb previous result */ | ||
394 | HD(results); | ||
395 | } | ||
396 | |||
397 | HX((e = gettimeofday(&tv, NULL)) == -1, tv); | ||
398 | if (e != -1) { | ||
399 | cnt += (int)tv.tv_sec; | ||
400 | cnt += (int)tv.tv_usec; | ||
401 | } | ||
402 | |||
403 | HD(cnt); | ||
404 | } | ||
405 | SHA512_Final(results, &ctx); | ||
406 | memcpy((char *)buf + i, results, min(sizeof(results), len - i)); | ||
407 | i += min(sizeof(results), len - i); | ||
408 | } | ||
409 | explicit_bzero(&ctx, sizeof ctx); | ||
410 | explicit_bzero(results, sizeof results); | ||
411 | if (gotdata(buf, len) == 0) { | ||
412 | errno = save_errno; | ||
413 | return 0; /* satisfied */ | ||
414 | } | ||
415 | errno = EIO; | ||
416 | return -1; | ||
417 | } | ||
diff --git a/src/lib/libcrypto/crypto/arc4random_hpux.h b/src/lib/libcrypto/crypto/arc4random_hpux.h new file mode 100644 index 0000000000..b6841f583c --- /dev/null +++ b/src/lib/libcrypto/crypto/arc4random_hpux.h | |||
@@ -0,0 +1,79 @@ | |||
1 | /* $OpenBSD: arc4random_hpux.h,v 1.1 2015/01/06 21:08:11 bcook Exp $ */ | ||
2 | |||
3 | /* | ||
4 | * Copyright (c) 1996, David Mazieres <dm@uun.org> | ||
5 | * Copyright (c) 2008, Damien Miller <djm@openbsd.org> | ||
6 | * Copyright (c) 2013, Markus Friedl <markus@openbsd.org> | ||
7 | * | ||
8 | * Permission to use, copy, modify, and distribute this software for any | ||
9 | * purpose with or without fee is hereby granted, provided that the above | ||
10 | * copyright notice and this permission notice appear in all copies. | ||
11 | * | ||
12 | * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES | ||
13 | * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF | ||
14 | * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR | ||
15 | * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES | ||
16 | * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN | ||
17 | * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF | ||
18 | * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. | ||
19 | */ | ||
20 | |||
21 | /* | ||
22 | * Stub functions for portability. | ||
23 | */ | ||
24 | |||
25 | #include <sys/mman.h> | ||
26 | |||
27 | #include <pthread.h> | ||
28 | #include <signal.h> | ||
29 | |||
30 | static pthread_mutex_t arc4random_mtx = PTHREAD_MUTEX_INITIALIZER; | ||
31 | #define _ARC4_LOCK() pthread_mutex_lock(&arc4random_mtx) | ||
32 | #define _ARC4_UNLOCK() pthread_mutex_unlock(&arc4random_mtx) | ||
33 | |||
34 | #define _ARC4_ATFORK(f) pthread_atfork(NULL, NULL, (f)) | ||
35 | |||
36 | static inline void | ||
37 | _getentropy_fail(void) | ||
38 | { | ||
39 | raise(SIGKILL); | ||
40 | } | ||
41 | |||
42 | static volatile sig_atomic_t _rs_forked; | ||
43 | |||
44 | static inline void | ||
45 | _rs_forkhandler(void) | ||
46 | { | ||
47 | _rs_forked = 1; | ||
48 | } | ||
49 | |||
50 | static inline void | ||
51 | _rs_forkdetect(void) | ||
52 | { | ||
53 | static pid_t _rs_pid = 0; | ||
54 | pid_t pid = getpid(); | ||
55 | |||
56 | if (_rs_pid == 0 || _rs_pid != pid || _rs_forked) { | ||
57 | _rs_pid = pid; | ||
58 | _rs_forked = 0; | ||
59 | if (rs) | ||
60 | memset(rs, 0, sizeof(*rs)); | ||
61 | } | ||
62 | } | ||
63 | |||
64 | static inline int | ||
65 | _rs_allocate(struct _rs **rsp, struct _rsx **rsxp) | ||
66 | { | ||
67 | if ((*rsp = mmap(NULL, sizeof(**rsp), PROT_READ|PROT_WRITE, | ||
68 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) | ||
69 | return (-1); | ||
70 | |||
71 | if ((*rsxp = mmap(NULL, sizeof(**rsxp), PROT_READ|PROT_WRITE, | ||
72 | MAP_ANON|MAP_PRIVATE, -1, 0)) == MAP_FAILED) { | ||
73 | munmap(*rsp, sizeof(**rsp)); | ||
74 | return (-1); | ||
75 | } | ||
76 | |||
77 | _ARC4_ATFORK(_rs_forkhandler); | ||
78 | return (0); | ||
79 | } | ||
diff --git a/src/lib/libcrypto/crypto/getentropy_hpux.c b/src/lib/libcrypto/crypto/getentropy_hpux.c new file mode 100644 index 0000000000..46ab8f54d8 --- /dev/null +++ b/src/lib/libcrypto/crypto/getentropy_hpux.c | |||
@@ -0,0 +1,417 @@ | |||
1 | /* $OpenBSD: getentropy_hpux.c,v 1.1 2015/01/06 21:08:11 bcook 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 | * Emulation of getentropy(2) as documented at: | ||
20 | * http://www.openbsd.org/cgi-bin/man.cgi/OpenBSD-current/man2/getentropy.2 | ||
21 | */ | ||
22 | |||
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> | ||
31 | #include <sys/mman.h> | ||
32 | #include <sys/stat.h> | ||
33 | #include <sys/time.h> | ||
34 | #include <stdlib.h> | ||
35 | #include <stdint.h> | ||
36 | #include <stdio.h> | ||
37 | #include <termios.h> | ||
38 | #include <fcntl.h> | ||
39 | #include <signal.h> | ||
40 | #include <string.h> | ||
41 | #include <errno.h> | ||
42 | #include <unistd.h> | ||
43 | #include <time.h> | ||
44 | #include <openssl/sha.h> | ||
45 | |||
46 | #include <sys/vfs.h> | ||
47 | |||
48 | #include <sys/pstat.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 | static int gotdata(char *buf, size_t len); | ||
68 | static int getentropy_urandom(void *buf, size_t len, const char *path, | ||
69 | int devfscheck); | ||
70 | static int getentropy_fallback(void *buf, size_t len); | ||
71 | |||
72 | int | ||
73 | getentropy(void *buf, size_t len) | ||
74 | { | ||
75 | int ret = -1; | ||
76 | |||
77 | if (len > 256) { | ||
78 | errno = EIO; | ||
79 | return -1; | ||
80 | } | ||
81 | |||
82 | /* | ||
83 | * Try to get entropy with /dev/urandom | ||
84 | */ | ||
85 | ret = getentropy_urandom(buf, len, "/dev/urandom", 0); | ||
86 | if (ret != -1) | ||
87 | return (ret); | ||
88 | |||
89 | /* | ||
90 | * Entropy collection via /dev/urandom has failed. | ||
91 | * | ||
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. | ||
95 | * | ||
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 | ||
100 | * corefiles. | ||
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.... | ||
105 | * | ||
106 | * This code path exists to bring light to the issue that hpux | ||
107 | * does not provide a failsafe API for entropy collection. | ||
108 | * | ||
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. | ||
112 | */ | ||
113 | #undef FAIL_INSTEAD_OF_TRYING_FALLBACK | ||
114 | #ifdef FAIL_INSTEAD_OF_TRYING_FALLBACK | ||
115 | raise(SIGKILL); | ||
116 | #endif | ||
117 | ret = getentropy_fallback(buf, len); | ||
118 | if (ret != -1) | ||
119 | return (ret); | ||
120 | |||
121 | errno = EIO; | ||
122 | return (ret); | ||
123 | } | ||
124 | |||
125 | /* | ||
126 | * Basic sanity checking; wish we could do better. | ||
127 | */ | ||
128 | static int | ||
129 | gotdata(char *buf, size_t len) | ||
130 | { | ||
131 | char any_set = 0; | ||
132 | size_t i; | ||
133 | |||
134 | for (i = 0; i < len; ++i) | ||
135 | any_set |= buf[i]; | ||
136 | if (any_set == 0) | ||
137 | return -1; | ||
138 | return 0; | ||
139 | } | ||
140 | |||
141 | static int | ||
142 | getentropy_urandom(void *buf, size_t len, const char *path, int devfscheck) | ||
143 | { | ||
144 | struct stat st; | ||
145 | size_t i; | ||
146 | int fd, flags; | ||
147 | int save_errno = errno; | ||
148 | |||
149 | start: | ||
150 | |||
151 | flags = O_RDONLY; | ||
152 | #ifdef O_NOFOLLOW | ||
153 | flags |= O_NOFOLLOW; | ||
154 | #endif | ||
155 | #ifdef O_CLOEXEC | ||
156 | flags |= O_CLOEXEC; | ||
157 | #endif | ||
158 | fd = open(path, flags, 0); | ||
159 | if (fd == -1) { | ||
160 | if (errno == EINTR) | ||
161 | goto start; | ||
162 | goto nodevrandom; | ||
163 | } | ||
164 | #ifndef O_CLOEXEC | ||
165 | fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC); | ||
166 | #endif | ||
167 | |||
168 | /* Lightly verify that the device node looks sane */ | ||
169 | if (fstat(fd, &st) == -1 || !S_ISCHR(st.st_mode)) { | ||
170 | close(fd); | ||
171 | goto nodevrandom; | ||
172 | } | ||
173 | for (i = 0; i < len; ) { | ||
174 | size_t wanted = len - i; | ||
175 | ssize_t ret = read(fd, (char *)buf + i, wanted); | ||
176 | |||
177 | if (ret == -1) { | ||
178 | if (errno == EAGAIN || errno == EINTR) | ||
179 | continue; | ||
180 | close(fd); | ||
181 | goto nodevrandom; | ||
182 | } | ||
183 | i += ret; | ||
184 | } | ||
185 | close(fd); | ||
186 | if (gotdata(buf, len) == 0) { | ||
187 | errno = save_errno; | ||
188 | return 0; /* satisfied */ | ||
189 | } | ||
190 | nodevrandom: | ||
191 | errno = EIO; | ||
192 | return -1; | ||
193 | } | ||
194 | |||
195 | static const int cl[] = { | ||
196 | CLOCK_REALTIME, | ||
197 | #ifdef CLOCK_MONOTONIC | ||
198 | CLOCK_MONOTONIC, | ||
199 | #endif | ||
200 | #ifdef CLOCK_MONOTONIC_RAW | ||
201 | CLOCK_MONOTONIC_RAW, | ||
202 | #endif | ||
203 | #ifdef CLOCK_TAI | ||
204 | CLOCK_TAI, | ||
205 | #endif | ||
206 | #ifdef CLOCK_VIRTUAL | ||
207 | CLOCK_VIRTUAL, | ||
208 | #endif | ||
209 | #ifdef CLOCK_UPTIME | ||
210 | CLOCK_UPTIME, | ||
211 | #endif | ||
212 | #ifdef CLOCK_PROCESS_CPUTIME_ID | ||
213 | CLOCK_PROCESS_CPUTIME_ID, | ||
214 | #endif | ||
215 | #ifdef CLOCK_THREAD_CPUTIME_ID | ||
216 | CLOCK_THREAD_CPUTIME_ID, | ||
217 | #endif | ||
218 | }; | ||
219 | |||
220 | static int | ||
221 | getentropy_fallback(void *buf, size_t len) | ||
222 | { | ||
223 | uint8_t results[SHA512_DIGEST_LENGTH]; | ||
224 | int save_errno = errno, e, pgs = sysconf(_SC_PAGESIZE), faster = 0, repeat; | ||
225 | static int cnt; | ||
226 | struct timespec ts; | ||
227 | struct timeval tv; | ||
228 | struct pst_vm_status pvs; | ||
229 | struct pst_dynamic pdy; | ||
230 | struct rusage ru; | ||
231 | sigset_t sigset; | ||
232 | struct stat st; | ||
233 | SHA512_CTX ctx; | ||
234 | static pid_t lastpid; | ||
235 | pid_t pid; | ||
236 | size_t i, ii, m; | ||
237 | char *p; | ||
238 | |||
239 | pid = getpid(); | ||
240 | if (lastpid == pid) { | ||
241 | faster = 1; | ||
242 | repeat = 2; | ||
243 | } else { | ||
244 | faster = 0; | ||
245 | lastpid = pid; | ||
246 | repeat = REPEAT; | ||
247 | } | ||
248 | for (i = 0; i < len; ) { | ||
249 | int j; | ||
250 | SHA512_Init(&ctx); | ||
251 | for (j = 0; j < repeat; j++) { | ||
252 | HX((e = gettimeofday(&tv, NULL)) == -1, tv); | ||
253 | if (e != -1) { | ||
254 | cnt += (int)tv.tv_sec; | ||
255 | cnt += (int)tv.tv_usec; | ||
256 | } | ||
257 | |||
258 | HX(pstat_getprocvm(&pvs, sizeof(pvs), 0, 0) != 1, pvs.pst_space); | ||
259 | |||
260 | for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]); ii++) | ||
261 | HX(clock_gettime(cl[ii], &ts) == -1, ts); | ||
262 | |||
263 | HX((pid = getpid()) == -1, pid); | ||
264 | HX((pid = getsid(pid)) == -1, pid); | ||
265 | HX((pid = getppid()) == -1, pid); | ||
266 | HX((pid = getpgid(0)) == -1, pid); | ||
267 | HX((e = getpriority(0, 0)) == -1, e); | ||
268 | |||
269 | if(pstat_getdynamic(&pdy, sizeof(pdy), 1, 0) != 1) { | ||
270 | HD(errno); | ||
271 | } else { | ||
272 | HD(pdy.psd_avg_1_min); | ||
273 | HD(pdy.psd_avg_5_min); | ||
274 | HD(pdy.psd_avg_15_min); | ||
275 | } | ||
276 | |||
277 | if (!faster) { | ||
278 | ts.tv_sec = 0; | ||
279 | ts.tv_nsec = 1; | ||
280 | (void) nanosleep(&ts, NULL); | ||
281 | } | ||
282 | |||
283 | HX(sigpending(&sigset) == -1, sigset); | ||
284 | HX(sigprocmask(SIG_BLOCK, NULL, &sigset) == -1, | ||
285 | sigset); | ||
286 | |||
287 | HF(getentropy); /* an addr in this library */ | ||
288 | HF(printf); /* an addr in libc */ | ||
289 | p = (char *)&p; | ||
290 | HD(p); /* an addr on stack */ | ||
291 | p = (char *)&errno; | ||
292 | HD(p); /* the addr of errno */ | ||
293 | |||
294 | if (i == 0) { | ||
295 | struct sockaddr_storage ss; | ||
296 | struct statvfs stvfs; | ||
297 | struct termios tios; | ||
298 | socklen_t ssl; | ||
299 | off_t off; | ||
300 | |||
301 | /* | ||
302 | * Prime-sized mappings encourage fragmentation; | ||
303 | * thus exposing some address entropy. | ||
304 | */ | ||
305 | struct mm { | ||
306 | size_t npg; | ||
307 | void *p; | ||
308 | } mm[] = { | ||
309 | { 17, MAP_FAILED }, { 3, MAP_FAILED }, | ||
310 | { 11, MAP_FAILED }, { 2, MAP_FAILED }, | ||
311 | { 5, MAP_FAILED }, { 3, MAP_FAILED }, | ||
312 | { 7, MAP_FAILED }, { 1, MAP_FAILED }, | ||
313 | { 57, MAP_FAILED }, { 3, MAP_FAILED }, | ||
314 | { 131, MAP_FAILED }, { 1, MAP_FAILED }, | ||
315 | }; | ||
316 | |||
317 | for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) { | ||
318 | HX(mm[m].p = mmap(NULL, | ||
319 | mm[m].npg * pgs, | ||
320 | PROT_READ|PROT_WRITE, | ||
321 | MAP_PRIVATE|MAP_ANON, -1, | ||
322 | (off_t)0), mm[m].p); | ||
323 | if (mm[m].p != MAP_FAILED) { | ||
324 | size_t mo; | ||
325 | |||
326 | /* Touch some memory... */ | ||
327 | p = mm[m].p; | ||
328 | mo = cnt % | ||
329 | (mm[m].npg * pgs - 1); | ||
330 | p[mo] = 1; | ||
331 | cnt += (int)((long)(mm[m].p) | ||
332 | / pgs); | ||
333 | } | ||
334 | |||
335 | /* Check cnts and times... */ | ||
336 | for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]); | ||
337 | ii++) { | ||
338 | HX((e = clock_gettime(cl[ii], | ||
339 | &ts)) == -1, ts); | ||
340 | if (e != -1) | ||
341 | cnt += (int)ts.tv_nsec; | ||
342 | } | ||
343 | |||
344 | HX((e = getrusage(RUSAGE_SELF, | ||
345 | &ru)) == -1, ru); | ||
346 | if (e != -1) { | ||
347 | cnt += (int)ru.ru_utime.tv_sec; | ||
348 | cnt += (int)ru.ru_utime.tv_usec; | ||
349 | } | ||
350 | } | ||
351 | |||
352 | for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) { | ||
353 | if (mm[m].p != MAP_FAILED) | ||
354 | munmap(mm[m].p, mm[m].npg * pgs); | ||
355 | mm[m].p = MAP_FAILED; | ||
356 | } | ||
357 | |||
358 | HX(stat(".", &st) == -1, st); | ||
359 | HX(statvfs(".", &stvfs) == -1, stvfs); | ||
360 | |||
361 | HX(stat("/", &st) == -1, st); | ||
362 | HX(statvfs("/", &stvfs) == -1, stvfs); | ||
363 | |||
364 | HX((e = fstat(0, &st)) == -1, st); | ||
365 | if (e == -1) { | ||
366 | if (S_ISREG(st.st_mode) || | ||
367 | S_ISFIFO(st.st_mode) || | ||
368 | S_ISSOCK(st.st_mode)) { | ||
369 | HX(fstatvfs(0, &stvfs) == -1, | ||
370 | stvfs); | ||
371 | HX((off = lseek(0, (off_t)0, | ||
372 | SEEK_CUR)) < 0, off); | ||
373 | } | ||
374 | if (S_ISCHR(st.st_mode)) { | ||
375 | HX(tcgetattr(0, &tios) == -1, | ||
376 | tios); | ||
377 | } else if (S_ISSOCK(st.st_mode)) { | ||
378 | memset(&ss, 0, sizeof ss); | ||
379 | ssl = sizeof(ss); | ||
380 | HX(getpeername(0, | ||
381 | (void *)&ss, &ssl) == -1, | ||
382 | ss); | ||
383 | } | ||
384 | } | ||
385 | |||
386 | HX((e = getrusage(RUSAGE_CHILDREN, | ||
387 | &ru)) == -1, ru); | ||
388 | if (e != -1) { | ||
389 | cnt += (int)ru.ru_utime.tv_sec; | ||
390 | cnt += (int)ru.ru_utime.tv_usec; | ||
391 | } | ||
392 | } else { | ||
393 | /* Subsequent hashes absorb previous result */ | ||
394 | HD(results); | ||
395 | } | ||
396 | |||
397 | HX((e = gettimeofday(&tv, NULL)) == -1, tv); | ||
398 | if (e != -1) { | ||
399 | cnt += (int)tv.tv_sec; | ||
400 | cnt += (int)tv.tv_usec; | ||
401 | } | ||
402 | |||
403 | HD(cnt); | ||
404 | } | ||
405 | SHA512_Final(results, &ctx); | ||
406 | memcpy((char *)buf + i, results, min(sizeof(results), len - i)); | ||
407 | i += min(sizeof(results), len - i); | ||
408 | } | ||
409 | explicit_bzero(&ctx, sizeof ctx); | ||
410 | explicit_bzero(results, sizeof results); | ||
411 | if (gotdata(buf, len) == 0) { | ||
412 | errno = save_errno; | ||
413 | return 0; /* satisfied */ | ||
414 | } | ||
415 | errno = EIO; | ||
416 | return -1; | ||
417 | } | ||