xref: /netbsd-src/external/bsd/unbound/dist/compat/getentropy_linux.c (revision 01049ae6d55a7fce6c6379cd1e0c997c04dc0061)
1*01049ae6Schristos /*	$OpenBSD: getentropy_linux.c,v 1.46 2018/11/20 08:04:28 deraadt Exp $	*/
23b6c3722Schristos 
33b6c3722Schristos /*
43b6c3722Schristos  * Copyright (c) 2014 Theo de Raadt <deraadt@openbsd.org>
53b6c3722Schristos  * Copyright (c) 2014 Bob Beck <beck@obtuse.com>
63b6c3722Schristos  *
73b6c3722Schristos  * Permission to use, copy, modify, and distribute this software for any
83b6c3722Schristos  * purpose with or without fee is hereby granted, provided that the above
93b6c3722Schristos  * copyright notice and this permission notice appear in all copies.
103b6c3722Schristos  *
113b6c3722Schristos  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
123b6c3722Schristos  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
133b6c3722Schristos  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
143b6c3722Schristos  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
153b6c3722Schristos  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
163b6c3722Schristos  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
173b6c3722Schristos  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
18*01049ae6Schristos  *
19*01049ae6Schristos  * Emulation of getentropy(2) as documented at:
20*01049ae6Schristos  * http://man.openbsd.org/getentropy.2
213b6c3722Schristos  */
223b6c3722Schristos 
23*01049ae6Schristos #include "config.h"
243b6c3722Schristos /*
253b6c3722Schristos #define	_POSIX_C_SOURCE	199309L
263b6c3722Schristos #define	_GNU_SOURCE	1
273b6c3722Schristos */
283b6c3722Schristos #include <sys/types.h>
293b6c3722Schristos #include <sys/param.h>
303b6c3722Schristos #include <sys/ioctl.h>
313b6c3722Schristos #include <sys/resource.h>
323b6c3722Schristos #include <sys/syscall.h>
33*01049ae6Schristos #ifdef SYS__sysctl
34*01049ae6Schristos #include <linux/sysctl.h>
353b6c3722Schristos #endif
363b6c3722Schristos #include <sys/statvfs.h>
373b6c3722Schristos #include <sys/socket.h>
383b6c3722Schristos #include <sys/mount.h>
393b6c3722Schristos #include <sys/mman.h>
403b6c3722Schristos #include <sys/stat.h>
413b6c3722Schristos #include <sys/time.h>
423b6c3722Schristos #include <stdlib.h>
433b6c3722Schristos #include <stdint.h>
443b6c3722Schristos #include <stdio.h>
45*01049ae6Schristos #include <link.h>
463b6c3722Schristos #include <termios.h>
473b6c3722Schristos #include <fcntl.h>
483b6c3722Schristos #include <signal.h>
493b6c3722Schristos #include <string.h>
503b6c3722Schristos #include <errno.h>
513b6c3722Schristos #include <unistd.h>
523b6c3722Schristos #include <time.h>
53*01049ae6Schristos #ifndef HAVE_NETTLE
543b6c3722Schristos #include <openssl/sha.h>
55*01049ae6Schristos #else
563b6c3722Schristos #include <nettle/sha.h>
57*01049ae6Schristos #define SHA512_CTX		struct sha512_ctx
58*01049ae6Schristos #define SHA512_Init(x)		sha512_init(x)
59*01049ae6Schristos #define SHA512_Update(x, b, s)	sha512_update(x, s, b)
60*01049ae6Schristos #define SHA512_Final(r, c)	sha512_digest(c, SHA512_DIGEST_SIZE, r)
613b6c3722Schristos #endif
623b6c3722Schristos 
633b6c3722Schristos #include <linux/types.h>
643b6c3722Schristos #include <linux/random.h>
653b6c3722Schristos #ifdef HAVE_GETAUXVAL
663b6c3722Schristos #include <sys/auxv.h>
673b6c3722Schristos #endif
683b6c3722Schristos #include <sys/vfs.h>
690cd9f4ecSchristos #ifndef MAP_ANON
700cd9f4ecSchristos #define MAP_ANON MAP_ANONYMOUS
710cd9f4ecSchristos #endif
723b6c3722Schristos 
733b6c3722Schristos #define REPEAT 5
743b6c3722Schristos #define min(a, b) (((a) < (b)) ? (a) : (b))
753b6c3722Schristos 
763b6c3722Schristos #define HX(a, b) \
773b6c3722Schristos 	do { \
783b6c3722Schristos 		if ((a)) \
793b6c3722Schristos 			HD(errno); \
803b6c3722Schristos 		else \
813b6c3722Schristos 			HD(b); \
823b6c3722Schristos 	} while (0)
833b6c3722Schristos 
843b6c3722Schristos #define HR(x, l) (SHA512_Update(&ctx, (char *)(x), (l)))
853b6c3722Schristos #define HD(x)	 (SHA512_Update(&ctx, (char *)&(x), sizeof (x)))
863b6c3722Schristos #define HF(x)    (SHA512_Update(&ctx, (char *)&(x), sizeof (void*)))
873b6c3722Schristos 
883b6c3722Schristos int	getentropy(void *buf, size_t len);
893b6c3722Schristos 
90*01049ae6Schristos #if defined(SYS_getrandom) && defined(GRND_NONBLOCK)
913b6c3722Schristos static int getentropy_getrandom(void *buf, size_t len);
923b6c3722Schristos #endif
933b6c3722Schristos static int getentropy_urandom(void *buf, size_t len);
943b6c3722Schristos #ifdef SYS__sysctl
953b6c3722Schristos static int getentropy_sysctl(void *buf, size_t len);
963b6c3722Schristos #endif
973b6c3722Schristos static int getentropy_fallback(void *buf, size_t len);
98*01049ae6Schristos static int getentropy_phdr(struct dl_phdr_info *info, size_t size, void *data);
993b6c3722Schristos 
1003b6c3722Schristos int
getentropy(void * buf,size_t len)1013b6c3722Schristos getentropy(void *buf, size_t len)
1023b6c3722Schristos {
1033b6c3722Schristos 	int ret = -1;
1043b6c3722Schristos 
1053b6c3722Schristos 	if (len > 256) {
1063b6c3722Schristos 		errno = EIO;
107*01049ae6Schristos 		return (-1);
1083b6c3722Schristos 	}
1093b6c3722Schristos 
110*01049ae6Schristos #if defined(SYS_getrandom) && defined(GRND_NONBLOCK)
1113b6c3722Schristos 	/*
112*01049ae6Schristos 	 * Try descriptor-less getrandom(), in non-blocking mode.
113*01049ae6Schristos 	 *
114*01049ae6Schristos 	 * The design of Linux getrandom is broken.  It has an
115*01049ae6Schristos 	 * uninitialized phase coupled with blocking behaviour, which
116*01049ae6Schristos 	 * is unacceptable from within a library at boot time without
117*01049ae6Schristos 	 * possible recovery. See http://bugs.python.org/issue26839#msg267745
1183b6c3722Schristos 	 */
1193b6c3722Schristos 	ret = getentropy_getrandom(buf, len);
1203b6c3722Schristos 	if (ret != -1)
1213b6c3722Schristos 		return (ret);
1223b6c3722Schristos #endif
1233b6c3722Schristos 
1243b6c3722Schristos 	/*
1253b6c3722Schristos 	 * Try to get entropy with /dev/urandom
1263b6c3722Schristos 	 *
1273b6c3722Schristos 	 * This can fail if the process is inside a chroot or if file
1283b6c3722Schristos 	 * descriptors are exhausted.
1293b6c3722Schristos 	 */
1303b6c3722Schristos 	ret = getentropy_urandom(buf, len);
1313b6c3722Schristos 	if (ret != -1)
1323b6c3722Schristos 		return (ret);
1333b6c3722Schristos 
1343b6c3722Schristos #ifdef SYS__sysctl
1353b6c3722Schristos 	/*
1363b6c3722Schristos 	 * Try to use sysctl CTL_KERN, KERN_RANDOM, RANDOM_UUID.
1373b6c3722Schristos 	 * sysctl is a failsafe API, so it guarantees a result.  This
1383b6c3722Schristos 	 * should work inside a chroot, or when file descriptors are
1393b6c3722Schristos 	 * exhausted.
1403b6c3722Schristos 	 *
1413b6c3722Schristos 	 * However this can fail if the Linux kernel removes support
1423b6c3722Schristos 	 * for sysctl.  Starting in 2007, there have been efforts to
1433b6c3722Schristos 	 * deprecate the sysctl API/ABI, and push callers towards use
1443b6c3722Schristos 	 * of the chroot-unavailable fd-using /proc mechanism --
1453b6c3722Schristos 	 * essentially the same problems as /dev/urandom.
1463b6c3722Schristos 	 *
1473b6c3722Schristos 	 * Numerous setbacks have been encountered in their deprecation
1483b6c3722Schristos 	 * schedule, so as of June 2014 the kernel ABI still exists on
1493b6c3722Schristos 	 * most Linux architectures. The sysctl() stub in libc is missing
1503b6c3722Schristos 	 * on some systems.  There are also reports that some kernels
1513b6c3722Schristos 	 * spew messages to the console.
1523b6c3722Schristos 	 */
1533b6c3722Schristos 	ret = getentropy_sysctl(buf, len);
1543b6c3722Schristos 	if (ret != -1)
1553b6c3722Schristos 		return (ret);
1563b6c3722Schristos #endif /* SYS__sysctl */
1573b6c3722Schristos 
1583b6c3722Schristos 	/*
1593b6c3722Schristos 	 * Entropy collection via /dev/urandom and sysctl have failed.
1603b6c3722Schristos 	 *
1613b6c3722Schristos 	 * No other API exists for collecting entropy.  See the large
1623b6c3722Schristos 	 * comment block above.
1633b6c3722Schristos 	 *
1643b6c3722Schristos 	 * We have very few options:
1653b6c3722Schristos 	 *     - Even syslog_r is unsafe to call at this low level, so
1663b6c3722Schristos 	 *	 there is no way to alert the user or program.
1673b6c3722Schristos 	 *     - Cannot call abort() because some systems have unsafe
1683b6c3722Schristos 	 *	 corefiles.
1693b6c3722Schristos 	 *     - Could raise(SIGKILL) resulting in silent program termination.
1703b6c3722Schristos 	 *     - Return EIO, to hint that arc4random's stir function
1713b6c3722Schristos 	 *       should raise(SIGKILL)
1723b6c3722Schristos 	 *     - Do the best under the circumstances....
1733b6c3722Schristos 	 *
1743b6c3722Schristos 	 * This code path exists to bring light to the issue that Linux
175*01049ae6Schristos 	 * still does not provide a failsafe API for entropy collection.
1763b6c3722Schristos 	 *
1773b6c3722Schristos 	 * We hope this demonstrates that Linux should either retain their
1783b6c3722Schristos 	 * sysctl ABI, or consider providing a new failsafe API which
1793b6c3722Schristos 	 * works in a chroot or when file descriptors are exhausted.
1803b6c3722Schristos 	 */
1813b6c3722Schristos #undef FAIL_INSTEAD_OF_TRYING_FALLBACK
1823b6c3722Schristos #ifdef FAIL_INSTEAD_OF_TRYING_FALLBACK
1833b6c3722Schristos 	raise(SIGKILL);
1843b6c3722Schristos #endif
1853b6c3722Schristos 	ret = getentropy_fallback(buf, len);
1863b6c3722Schristos 	if (ret != -1)
1873b6c3722Schristos 		return (ret);
1883b6c3722Schristos 
1893b6c3722Schristos 	errno = EIO;
1903b6c3722Schristos 	return (ret);
1913b6c3722Schristos }
1923b6c3722Schristos 
193*01049ae6Schristos #if defined(SYS_getrandom) && defined(GRND_NONBLOCK)
1943b6c3722Schristos static int
getentropy_getrandom(void * buf,size_t len)1953b6c3722Schristos getentropy_getrandom(void *buf, size_t len)
1963b6c3722Schristos {
1973b6c3722Schristos 	int pre_errno = errno;
1983b6c3722Schristos 	int ret;
1993b6c3722Schristos 	if (len > 256)
2003b6c3722Schristos 		return (-1);
2013b6c3722Schristos 	do {
202*01049ae6Schristos 		ret = syscall(SYS_getrandom, buf, len, GRND_NONBLOCK);
2033b6c3722Schristos 	} while (ret == -1 && errno == EINTR);
2043b6c3722Schristos 
2053b6c3722Schristos 	if (ret != (int)len)
2063b6c3722Schristos 		return (-1);
2073b6c3722Schristos 	errno = pre_errno;
2083b6c3722Schristos 	return (0);
2093b6c3722Schristos }
2103b6c3722Schristos #endif
2113b6c3722Schristos 
2123b6c3722Schristos static int
getentropy_urandom(void * buf,size_t len)2133b6c3722Schristos getentropy_urandom(void *buf, size_t len)
2143b6c3722Schristos {
2153b6c3722Schristos 	struct stat st;
2163b6c3722Schristos 	size_t i;
2173b6c3722Schristos 	int fd, cnt, flags;
2183b6c3722Schristos 	int save_errno = errno;
2193b6c3722Schristos 
2203b6c3722Schristos start:
2213b6c3722Schristos 
2223b6c3722Schristos 	flags = O_RDONLY;
2233b6c3722Schristos #ifdef O_NOFOLLOW
2243b6c3722Schristos 	flags |= O_NOFOLLOW;
2253b6c3722Schristos #endif
2263b6c3722Schristos #ifdef O_CLOEXEC
2273b6c3722Schristos 	flags |= O_CLOEXEC;
2283b6c3722Schristos #endif
2293b6c3722Schristos 	fd = open("/dev/urandom", flags, 0);
2303b6c3722Schristos 	if (fd == -1) {
2313b6c3722Schristos 		if (errno == EINTR)
2323b6c3722Schristos 			goto start;
2333b6c3722Schristos 		goto nodevrandom;
2343b6c3722Schristos 	}
2353b6c3722Schristos #ifndef O_CLOEXEC
2363b6c3722Schristos 	fcntl(fd, F_SETFD, fcntl(fd, F_GETFD) | FD_CLOEXEC);
2373b6c3722Schristos #endif
2383b6c3722Schristos 
2393b6c3722Schristos 	/* Lightly verify that the device node looks sane */
2403b6c3722Schristos 	if (fstat(fd, &st) == -1 || !S_ISCHR(st.st_mode)) {
2413b6c3722Schristos 		close(fd);
2423b6c3722Schristos 		goto nodevrandom;
2433b6c3722Schristos 	}
2443b6c3722Schristos 	if (ioctl(fd, RNDGETENTCNT, &cnt) == -1) {
2453b6c3722Schristos 		close(fd);
2463b6c3722Schristos 		goto nodevrandom;
2473b6c3722Schristos 	}
2483b6c3722Schristos 	for (i = 0; i < len; ) {
2493b6c3722Schristos 		size_t wanted = len - i;
2503b6c3722Schristos 		ssize_t ret = read(fd, (char *)buf + i, wanted);
2513b6c3722Schristos 
2523b6c3722Schristos 		if (ret == -1) {
2533b6c3722Schristos 			if (errno == EAGAIN || errno == EINTR)
2543b6c3722Schristos 				continue;
2553b6c3722Schristos 			close(fd);
2563b6c3722Schristos 			goto nodevrandom;
2573b6c3722Schristos 		}
2583b6c3722Schristos 		i += ret;
2593b6c3722Schristos 	}
2603b6c3722Schristos 	close(fd);
2613b6c3722Schristos 	errno = save_errno;
262*01049ae6Schristos 	return (0);		/* satisfied */
2633b6c3722Schristos nodevrandom:
2643b6c3722Schristos 	errno = EIO;
265*01049ae6Schristos 	return (-1);
2663b6c3722Schristos }
2673b6c3722Schristos 
2683b6c3722Schristos #ifdef SYS__sysctl
2693b6c3722Schristos static int
getentropy_sysctl(void * buf,size_t len)2703b6c3722Schristos getentropy_sysctl(void *buf, size_t len)
2713b6c3722Schristos {
2723b6c3722Schristos 	static int mib[] = { CTL_KERN, KERN_RANDOM, RANDOM_UUID };
2733b6c3722Schristos 	size_t i;
2743b6c3722Schristos 	int save_errno = errno;
2753b6c3722Schristos 
2763b6c3722Schristos 	for (i = 0; i < len; ) {
2773b6c3722Schristos 		size_t chunk = min(len - i, 16);
2783b6c3722Schristos 
2793b6c3722Schristos 		/* SYS__sysctl because some systems already removed sysctl() */
2803b6c3722Schristos 		struct __sysctl_args args = {
2813b6c3722Schristos 			.name = mib,
2823b6c3722Schristos 			.nlen = 3,
2833b6c3722Schristos 			.oldval = (char *)buf + i,
2843b6c3722Schristos 			.oldlenp = &chunk,
2853b6c3722Schristos 		};
2863b6c3722Schristos 		if (syscall(SYS__sysctl, &args) != 0)
2873b6c3722Schristos 			goto sysctlfailed;
2883b6c3722Schristos 		i += chunk;
2893b6c3722Schristos 	}
2903b6c3722Schristos 	errno = save_errno;
2913b6c3722Schristos 	return (0);			/* satisfied */
2923b6c3722Schristos sysctlfailed:
2933b6c3722Schristos 	errno = EIO;
294*01049ae6Schristos 	return (-1);
2953b6c3722Schristos }
2963b6c3722Schristos #endif /* SYS__sysctl */
2973b6c3722Schristos 
298*01049ae6Schristos static const int cl[] = {
2993b6c3722Schristos 	CLOCK_REALTIME,
3003b6c3722Schristos #ifdef CLOCK_MONOTONIC
3013b6c3722Schristos 	CLOCK_MONOTONIC,
3023b6c3722Schristos #endif
3033b6c3722Schristos #ifdef CLOCK_MONOTONIC_RAW
3043b6c3722Schristos 	CLOCK_MONOTONIC_RAW,
3053b6c3722Schristos #endif
3063b6c3722Schristos #ifdef CLOCK_TAI
3073b6c3722Schristos 	CLOCK_TAI,
3083b6c3722Schristos #endif
3093b6c3722Schristos #ifdef CLOCK_VIRTUAL
3103b6c3722Schristos 	CLOCK_VIRTUAL,
3113b6c3722Schristos #endif
3123b6c3722Schristos #ifdef CLOCK_UPTIME
3133b6c3722Schristos 	CLOCK_UPTIME,
3143b6c3722Schristos #endif
3153b6c3722Schristos #ifdef CLOCK_PROCESS_CPUTIME_ID
3163b6c3722Schristos 	CLOCK_PROCESS_CPUTIME_ID,
3173b6c3722Schristos #endif
3183b6c3722Schristos #ifdef CLOCK_THREAD_CPUTIME_ID
3193b6c3722Schristos 	CLOCK_THREAD_CPUTIME_ID,
3203b6c3722Schristos #endif
3213b6c3722Schristos };
3223b6c3722Schristos 
3233b6c3722Schristos static int
getentropy_phdr(struct dl_phdr_info * info,size_t ATTR_UNUSED (size),void * data)324*01049ae6Schristos getentropy_phdr(struct dl_phdr_info *info, size_t ATTR_UNUSED(size), void *data)
325*01049ae6Schristos {
326*01049ae6Schristos 	SHA512_CTX *ctx = data;
327*01049ae6Schristos 
328*01049ae6Schristos 	SHA512_Update(ctx, &info->dlpi_addr, sizeof (info->dlpi_addr));
329*01049ae6Schristos 	return (0);
330*01049ae6Schristos }
331*01049ae6Schristos 
332*01049ae6Schristos static int
getentropy_fallback(void * buf,size_t len)3333b6c3722Schristos getentropy_fallback(void *buf, size_t len)
3343b6c3722Schristos {
3353b6c3722Schristos 	uint8_t results[SHA512_DIGEST_LENGTH];
3363b6c3722Schristos 	int save_errno = errno, e, pgs = getpagesize(), faster = 0, repeat;
3373b6c3722Schristos 	static int cnt;
3383b6c3722Schristos 	struct timespec ts;
3393b6c3722Schristos 	struct timeval tv;
3403b6c3722Schristos 	struct rusage ru;
3413b6c3722Schristos 	sigset_t sigset;
3423b6c3722Schristos 	struct stat st;
343*01049ae6Schristos 	SHA512_CTX ctx;
3443b6c3722Schristos 	static pid_t lastpid;
3453b6c3722Schristos 	pid_t pid;
3463b6c3722Schristos 	size_t i, ii, m;
3473b6c3722Schristos 	char *p;
3483b6c3722Schristos 
3493b6c3722Schristos 	pid = getpid();
3503b6c3722Schristos 	if (lastpid == pid) {
3513b6c3722Schristos 		faster = 1;
3523b6c3722Schristos 		repeat = 2;
3533b6c3722Schristos 	} else {
3543b6c3722Schristos 		faster = 0;
3553b6c3722Schristos 		lastpid = pid;
3563b6c3722Schristos 		repeat = REPEAT;
3573b6c3722Schristos 	}
3583b6c3722Schristos 	for (i = 0; i < len; ) {
3593b6c3722Schristos 		int j;
360*01049ae6Schristos 		SHA512_Init(&ctx);
3613b6c3722Schristos 		for (j = 0; j < repeat; j++) {
3623b6c3722Schristos 			HX((e = gettimeofday(&tv, NULL)) == -1, tv);
3633b6c3722Schristos 			if (e != -1) {
3643b6c3722Schristos 				cnt += (int)tv.tv_sec;
3653b6c3722Schristos 				cnt += (int)tv.tv_usec;
3663b6c3722Schristos 			}
3673b6c3722Schristos 
368*01049ae6Schristos 			dl_iterate_phdr(getentropy_phdr, &ctx);
369*01049ae6Schristos 
3703b6c3722Schristos 			for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]); ii++)
3713b6c3722Schristos 				HX(clock_gettime(cl[ii], &ts) == -1, ts);
3723b6c3722Schristos 
3733b6c3722Schristos 			HX((pid = getpid()) == -1, pid);
3743b6c3722Schristos 			HX((pid = getsid(pid)) == -1, pid);
3753b6c3722Schristos 			HX((pid = getppid()) == -1, pid);
3763b6c3722Schristos 			HX((pid = getpgid(0)) == -1, pid);
3773b6c3722Schristos 			HX((e = getpriority(0, 0)) == -1, e);
3783b6c3722Schristos 
3793b6c3722Schristos 			if (!faster) {
3803b6c3722Schristos 				ts.tv_sec = 0;
3813b6c3722Schristos 				ts.tv_nsec = 1;
3823b6c3722Schristos 				(void) nanosleep(&ts, NULL);
3833b6c3722Schristos 			}
3843b6c3722Schristos 
3853b6c3722Schristos 			HX(sigpending(&sigset) == -1, sigset);
3863b6c3722Schristos 			HX(sigprocmask(SIG_BLOCK, NULL, &sigset) == -1,
3873b6c3722Schristos 			    sigset);
3883b6c3722Schristos 
3893b6c3722Schristos 			HF(getentropy);	/* an addr in this library */
3903b6c3722Schristos 			HF(printf);		/* an addr in libc */
3913b6c3722Schristos 			p = (char *)&p;
3923b6c3722Schristos 			HD(p);		/* an addr on stack */
3933b6c3722Schristos 			p = (char *)&errno;
3943b6c3722Schristos 			HD(p);		/* the addr of errno */
3953b6c3722Schristos 
3963b6c3722Schristos 			if (i == 0) {
3973b6c3722Schristos 				struct sockaddr_storage ss;
3983b6c3722Schristos 				struct statvfs stvfs;
3993b6c3722Schristos 				struct termios tios;
4003b6c3722Schristos 				struct statfs stfs;
4013b6c3722Schristos 				socklen_t ssl;
4023b6c3722Schristos 				off_t off;
4033b6c3722Schristos 
4043b6c3722Schristos 				/*
4053b6c3722Schristos 				 * Prime-sized mappings encourage fragmentation;
4063b6c3722Schristos 				 * thus exposing some address entropy.
4073b6c3722Schristos 				 */
4083b6c3722Schristos 				struct mm {
4093b6c3722Schristos 					size_t	npg;
4103b6c3722Schristos 					void	*p;
4113b6c3722Schristos 				} mm[] =	 {
4123b6c3722Schristos 					{ 17, MAP_FAILED }, { 3, MAP_FAILED },
4133b6c3722Schristos 					{ 11, MAP_FAILED }, { 2, MAP_FAILED },
4143b6c3722Schristos 					{ 5, MAP_FAILED }, { 3, MAP_FAILED },
4153b6c3722Schristos 					{ 7, MAP_FAILED }, { 1, MAP_FAILED },
4163b6c3722Schristos 					{ 57, MAP_FAILED }, { 3, MAP_FAILED },
4173b6c3722Schristos 					{ 131, MAP_FAILED }, { 1, MAP_FAILED },
4183b6c3722Schristos 				};
4193b6c3722Schristos 
4203b6c3722Schristos 				for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) {
4213b6c3722Schristos 					HX(mm[m].p = mmap(NULL,
4223b6c3722Schristos 					    mm[m].npg * pgs,
4233b6c3722Schristos 					    PROT_READ|PROT_WRITE,
4243b6c3722Schristos 					    MAP_PRIVATE|MAP_ANON, -1,
4253b6c3722Schristos 					    (off_t)0), mm[m].p);
4263b6c3722Schristos 					if (mm[m].p != MAP_FAILED) {
4273b6c3722Schristos 						size_t mo;
4283b6c3722Schristos 
4293b6c3722Schristos 						/* Touch some memory... */
4303b6c3722Schristos 						p = mm[m].p;
4313b6c3722Schristos 						mo = cnt %
4323b6c3722Schristos 						    (mm[m].npg * pgs - 1);
4333b6c3722Schristos 						p[mo] = 1;
4343b6c3722Schristos 						cnt += (int)((long)(mm[m].p)
4353b6c3722Schristos 						    / pgs);
4363b6c3722Schristos 					}
4373b6c3722Schristos 
4383b6c3722Schristos 					/* Check cnts and times... */
4393b6c3722Schristos 					for (ii = 0; ii < sizeof(cl)/sizeof(cl[0]);
4403b6c3722Schristos 					    ii++) {
4413b6c3722Schristos 						HX((e = clock_gettime(cl[ii],
4423b6c3722Schristos 						    &ts)) == -1, ts);
4433b6c3722Schristos 						if (e != -1)
4443b6c3722Schristos 							cnt += (int)ts.tv_nsec;
4453b6c3722Schristos 					}
4463b6c3722Schristos 
4473b6c3722Schristos 					HX((e = getrusage(RUSAGE_SELF,
4483b6c3722Schristos 					    &ru)) == -1, ru);
4493b6c3722Schristos 					if (e != -1) {
4503b6c3722Schristos 						cnt += (int)ru.ru_utime.tv_sec;
4513b6c3722Schristos 						cnt += (int)ru.ru_utime.tv_usec;
4523b6c3722Schristos 					}
4533b6c3722Schristos 				}
4543b6c3722Schristos 
4553b6c3722Schristos 				for (m = 0; m < sizeof mm/sizeof(mm[0]); m++) {
4563b6c3722Schristos 					if (mm[m].p != MAP_FAILED)
4573b6c3722Schristos 						munmap(mm[m].p, mm[m].npg * pgs);
4583b6c3722Schristos 					mm[m].p = MAP_FAILED;
4593b6c3722Schristos 				}
4603b6c3722Schristos 
4613b6c3722Schristos 				HX(stat(".", &st) == -1, st);
4623b6c3722Schristos 				HX(statvfs(".", &stvfs) == -1, stvfs);
4633b6c3722Schristos 				HX(statfs(".", &stfs) == -1, stfs);
4643b6c3722Schristos 
4653b6c3722Schristos 				HX(stat("/", &st) == -1, st);
4663b6c3722Schristos 				HX(statvfs("/", &stvfs) == -1, stvfs);
4673b6c3722Schristos 				HX(statfs("/", &stfs) == -1, stfs);
4683b6c3722Schristos 
4693b6c3722Schristos 				HX((e = fstat(0, &st)) == -1, st);
4703b6c3722Schristos 				if (e == -1) {
4713b6c3722Schristos 					if (S_ISREG(st.st_mode) ||
4723b6c3722Schristos 					    S_ISFIFO(st.st_mode) ||
4733b6c3722Schristos 					    S_ISSOCK(st.st_mode)) {
4743b6c3722Schristos 						HX(fstatvfs(0, &stvfs) == -1,
4753b6c3722Schristos 						    stvfs);
4763b6c3722Schristos 						HX(fstatfs(0, &stfs) == -1,
4773b6c3722Schristos 						    stfs);
4783b6c3722Schristos 						HX((off = lseek(0, (off_t)0,
4793b6c3722Schristos 						    SEEK_CUR)) < 0, off);
4803b6c3722Schristos 					}
4813b6c3722Schristos 					if (S_ISCHR(st.st_mode)) {
4823b6c3722Schristos 						HX(tcgetattr(0, &tios) == -1,
4833b6c3722Schristos 						    tios);
4843b6c3722Schristos 					} else if (S_ISSOCK(st.st_mode)) {
4853b6c3722Schristos 						memset(&ss, 0, sizeof ss);
4863b6c3722Schristos 						ssl = sizeof(ss);
4873b6c3722Schristos 						HX(getpeername(0,
4883b6c3722Schristos 						    (void *)&ss, &ssl) == -1,
4893b6c3722Schristos 						    ss);
4903b6c3722Schristos 					}
4913b6c3722Schristos 				}
4923b6c3722Schristos 
4933b6c3722Schristos 				HX((e = getrusage(RUSAGE_CHILDREN,
4943b6c3722Schristos 				    &ru)) == -1, ru);
4953b6c3722Schristos 				if (e != -1) {
4963b6c3722Schristos 					cnt += (int)ru.ru_utime.tv_sec;
4973b6c3722Schristos 					cnt += (int)ru.ru_utime.tv_usec;
4983b6c3722Schristos 				}
4993b6c3722Schristos 			} else {
5003b6c3722Schristos 				/* Subsequent hashes absorb previous result */
5013b6c3722Schristos 				HD(results);
5023b6c3722Schristos 			}
5033b6c3722Schristos 
5043b6c3722Schristos 			HX((e = gettimeofday(&tv, NULL)) == -1, tv);
5053b6c3722Schristos 			if (e != -1) {
5063b6c3722Schristos 				cnt += (int)tv.tv_sec;
5073b6c3722Schristos 				cnt += (int)tv.tv_usec;
5083b6c3722Schristos 			}
5093b6c3722Schristos 
5103b6c3722Schristos 			HD(cnt);
5113b6c3722Schristos 		}
5123b6c3722Schristos #ifdef HAVE_GETAUXVAL
5133b6c3722Schristos #ifdef AT_RANDOM
5143b6c3722Schristos 		/* Not as random as you think but we take what we are given */
5153b6c3722Schristos 		p = (char *) getauxval(AT_RANDOM);
5163b6c3722Schristos 		if (p)
5173b6c3722Schristos 			HR(p, 16);
5183b6c3722Schristos #endif
5193b6c3722Schristos #ifdef AT_SYSINFO_EHDR
5203b6c3722Schristos 		p = (char *) getauxval(AT_SYSINFO_EHDR);
5213b6c3722Schristos 		if (p)
5223b6c3722Schristos 			HR(p, pgs);
5233b6c3722Schristos #endif
5243b6c3722Schristos #ifdef AT_BASE
5253b6c3722Schristos 		p = (char *) getauxval(AT_BASE);
5263b6c3722Schristos 		if (p)
5273b6c3722Schristos 			HD(p);
5283b6c3722Schristos #endif
529*01049ae6Schristos #endif
5303b6c3722Schristos 
531*01049ae6Schristos 		SHA512_Final(results, &ctx);
5323b6c3722Schristos 		memcpy((char *)buf + i, results, min(sizeof(results), len - i));
5333b6c3722Schristos 		i += min(sizeof(results), len - i);
5343b6c3722Schristos 	}
535*01049ae6Schristos 	explicit_bzero(&ctx, sizeof ctx);
536*01049ae6Schristos 	explicit_bzero(results, sizeof results);
5373b6c3722Schristos 	errno = save_errno;
538*01049ae6Schristos 	return (0);		/* satisfied */
5393b6c3722Schristos }
540