1 /* $OpenBSD: getentropy_aix.c,v 1.5 2016/08/07 03:27:21 tb Exp $ */
4 * Copyright (c) 2015 Michael Felt <aixtools@gmail.com>
5 * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org>
6 * Copyright (c) 2014 Bob Beck <beck@obtuse.com>
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.
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.
20 * Emulation of getentropy(2) as documented at:
21 * http://man.openbsd.org/getentropy.2
24 * -lperfstat is needed for the psuedo entropy data
28 #include <sys/procfs.h>
29 #include <sys/protosw.h>
30 #include <sys/resource.h>
31 #include <sys/socket.h>
33 #include <sys/statvfs.h>
34 #include <sys/timers.h>
42 #include <openssl/sha.h>
44 #include <libperfstat.h>
47 #define min(a, b) (((a) < (b)) ? (a) : (b))
57 #define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
58 #define HD(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
59 #define HF(x) (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
61 int getentropy(void *buf
, size_t len
);
63 static int gotdata(char *buf
, size_t len
);
64 static int getentropy_urandom(void *buf
, size_t len
, const char *path
,
66 static int getentropy_fallback(void *buf
, size_t len
);
69 getentropy(void *buf
, size_t len
)
79 * Try to get entropy with /dev/urandom
81 ret
= getentropy_urandom(buf
, len
, "/dev/urandom", 0);
86 * Entropy collection via /dev/urandom has failed.
88 * No other API exists for collecting entropy, and we have
89 * no failsafe way to get it on AIX that is not sensitive
90 * to resource exhaustion.
92 * We have very few options:
93 * - Even syslog_r is unsafe to call at this low level, so
94 * there is no way to alert the user or program.
95 * - Cannot call abort() because some systems have unsafe
97 * - Could raise(SIGKILL) resulting in silent program termination.
98 * - Return EIO, to hint that arc4random's stir function
99 * should raise(SIGKILL)
100 * - Do the best under the circumstances....
102 * This code path exists to bring light to the issue that AIX
103 * does not provide a failsafe API for entropy collection.
105 * We hope this demonstrates that AIX should consider
106 * providing a new failsafe API which works in a chroot or
107 * when file descriptors are exhausted.
109 #undef FAIL_INSTEAD_OF_TRYING_FALLBACK
110 #ifdef FAIL_INSTEAD_OF_TRYING_FALLBACK
113 ret
= getentropy_fallback(buf
, len
);
122 * Basic sanity checking; wish we could do better.
125 gotdata(char *buf
, size_t len
)
130 for (i
= 0; i
< len
; ++i
)
138 getentropy_urandom(void *buf
, size_t len
, const char *path
, int devfscheck
)
143 int save_errno
= errno
;
154 fd
= open(path
, flags
, 0);
161 fcntl(fd
, F_SETFD
, fcntl(fd
, F_GETFD
) | FD_CLOEXEC
);
164 /* Lightly verify that the device node looks sane */
165 if (fstat(fd
, &st
) == -1 || !S_ISCHR(st
.st_mode
)) {
169 for (i
= 0; i
< len
; ) {
170 size_t wanted
= len
- i
;
171 ssize_t ret
= read(fd
, (char *)buf
+ i
, wanted
);
174 if (errno
== EAGAIN
|| errno
== EINTR
)
182 if (gotdata(buf
, len
) == 0) {
184 return (0); /* satisfied */
191 static const int cl
[] = {
193 #ifdef CLOCK_MONOTONIC
196 #ifdef CLOCK_MONOTONIC_RAW
208 #ifdef CLOCK_PROCESS_CPUTIME_ID
209 CLOCK_PROCESS_CPUTIME_ID
,
211 #ifdef CLOCK_THREAD_CPUTIME_ID
212 CLOCK_THREAD_CPUTIME_ID
,
217 getentropy_fallback(void *buf
, size_t len
)
219 uint8_t results
[SHA512_DIGEST_LENGTH
];
220 int save_errno
= errno
, e
, pgs
= sysconf(_SC_PAGESIZE
), faster
= 0, repeat
;
224 perfstat_cpu_total_t cpustats
;
226 perfstat_cpu_total_wpar_t cpustats_wpar
;
228 perfstat_partition_total_t lparstats
;
229 perfstat_disk_total_t diskinfo
;
230 perfstat_netinterface_total_t netinfo
;
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(perfstat_cpu_total(NULL
, &cpustats
,
260 sizeof(cpustats
), 1) == -1, cpustats
);
263 HX(perfstat_cpu_total_wpar(NULL
, &cpustats_wpar
,
264 sizeof(cpustats_wpar
), 1) == -1, cpustats_wpar
);
267 HX(perfstat_partition_total(NULL
, &lparstats
,
268 sizeof(lparstats
), 1) == -1, lparstats
);
270 HX(perfstat_disk_total(NULL
, &diskinfo
,
271 sizeof(diskinfo
), 1) == -1, diskinfo
);
273 HX(perfstat_netinterface_total(NULL
, &netinfo
,
274 sizeof(netinfo
), 1) == -1, netinfo
);
276 for (ii
= 0; ii
< sizeof(cl
)/sizeof(cl
[0]); ii
++)
277 HX(clock_gettime(cl
[ii
], &ts
) == -1, ts
);
279 HX((pid
= getpid()) == -1, pid
);
280 HX((pid
= getsid(pid
)) == -1, pid
);
281 HX((pid
= getppid()) == -1, pid
);
282 HX((pid
= getpgid(0)) == -1, pid
);
283 HX((e
= getpriority(0, 0)) == -1, e
);
288 (void) nanosleep(&ts
, NULL
);
291 HX(sigpending(&sigset
) == -1, sigset
);
292 HX(sigprocmask(SIG_BLOCK
, NULL
, &sigset
) == -1,
295 HF(getentropy
); /* an addr in this library */
296 HF(printf
); /* an addr in libc */
298 HD(p
); /* an addr on stack */
300 HD(p
); /* the addr of errno */
303 struct sockaddr_storage ss
;
304 struct statvfs stvfs
;
310 * Prime-sized mappings encourage fragmentation;
311 * thus exposing some address entropy.
317 { 17, MAP_FAILED
}, { 3, MAP_FAILED
},
318 { 11, MAP_FAILED
}, { 2, MAP_FAILED
},
319 { 5, MAP_FAILED
}, { 3, MAP_FAILED
},
320 { 7, MAP_FAILED
}, { 1, MAP_FAILED
},
321 { 57, MAP_FAILED
}, { 3, MAP_FAILED
},
322 { 131, MAP_FAILED
}, { 1, MAP_FAILED
},
325 for (m
= 0; m
< sizeof mm
/sizeof(mm
[0]); m
++) {
326 HX(mm
[m
].p
= mmap(NULL
,
328 PROT_READ
|PROT_WRITE
,
329 MAP_PRIVATE
|MAP_ANON
, -1,
331 if (mm
[m
].p
!= MAP_FAILED
) {
334 /* Touch some memory... */
337 (mm
[m
].npg
* pgs
- 1);
339 cnt
+= (int)((long)(mm
[m
].p
)
343 /* Check cnts and times... */
344 for (ii
= 0; ii
< sizeof(cl
)/sizeof(cl
[0]);
346 HX((e
= clock_gettime(cl
[ii
],
349 cnt
+= (int)ts
.tv_nsec
;
352 HX((e
= getrusage(RUSAGE_SELF
,
355 cnt
+= (int)ru
.ru_utime
.tv_sec
;
356 cnt
+= (int)ru
.ru_utime
.tv_usec
;
360 for (m
= 0; m
< sizeof mm
/sizeof(mm
[0]); m
++) {
361 if (mm
[m
].p
!= MAP_FAILED
)
362 munmap(mm
[m
].p
, mm
[m
].npg
* pgs
);
363 mm
[m
].p
= MAP_FAILED
;
366 HX(stat(".", &st
) == -1, st
);
367 HX(statvfs(".", &stvfs
) == -1, stvfs
);
369 HX(stat("/", &st
) == -1, st
);
370 HX(statvfs("/", &stvfs
) == -1, stvfs
);
372 HX((e
= fstat(0, &st
)) == -1, st
);
374 if (S_ISREG(st
.st_mode
) ||
375 S_ISFIFO(st
.st_mode
) ||
376 S_ISSOCK(st
.st_mode
)) {
377 HX(fstatvfs(0, &stvfs
) == -1,
379 HX((off
= lseek(0, (off_t
)0,
380 SEEK_CUR
)) < 0, off
);
382 if (S_ISCHR(st
.st_mode
)) {
383 HX(tcgetattr(0, &tios
) == -1,
385 } else if (S_ISSOCK(st
.st_mode
)) {
386 memset(&ss
, 0, sizeof ss
);
389 (void *)&ss
, &ssl
) == -1,
394 HX((e
= getrusage(RUSAGE_CHILDREN
,
397 cnt
+= (int)ru
.ru_utime
.tv_sec
;
398 cnt
+= (int)ru
.ru_utime
.tv_usec
;
401 /* Subsequent hashes absorb previous result */
405 HX((e
= gettimeofday(&tv
, NULL
)) == -1, tv
);
407 cnt
+= (int)tv
.tv_sec
;
408 cnt
+= (int)tv
.tv_usec
;
413 SHA512_Final(results
, &ctx
);
414 memcpy((char *)buf
+ i
, results
, min(sizeof(results
), len
- i
));
415 i
+= min(sizeof(results
), len
- i
);
417 explicit_bzero(&ctx
, sizeof ctx
);
418 explicit_bzero(results
, sizeof results
);
419 if (gotdata(buf
, len
) == 0) {
421 return (0); /* satisfied */