freebsd-dev/sys/libkern
Conrad Meyer 13774e8228 random(4): Block read_random(9) on initial seeding
read_random() is/was used, mostly without error checking, in a lot of
very sensitive places in the kernel -- including seeding the widely used
arc4random(9).

Most uses, especially arc4random(9), should block until the device is seeded
rather than proceeding with a bogus or empty seed.  I did not spy any
obvious kernel consumers where blocking would be inappropriate (in the
sense that lack of entropy would be ok -- I did not investigate locking
angle thoroughly).  In many instances, arc4random_buf(9) or that family
of APIs would be more appropriate anyway; that work was done in r345865.

A minor cleanup was made to the implementation of the READ_RANDOM function:
instead of using a variable-length array on the stack to temporarily store
all full random blocks sufficient to satisfy the requested 'len', only store
a single block on the stack.  This has some benefit in terms of reducing
stack usage, reducing memcpy overhead and reducing devrandom output leakage
via the stack.  Additionally, the stack block is now safely zeroed if it was
used.

One caveat of this change is that the kern.arandom sysctl no longer returns
zero bytes immediately if the random device is not seeded.  This means that
FreeBSD-specific userspace applications which attempted to handle an
unseeded random device may be broken by this change.  If such behavior is
needed, it can be replaced by the more portable getrandom(2) GRND_NONBLOCK
option.

On any typical FreeBSD system, entropy is persisted on read/write media and
used to seed the random device very early in boot, and blocking is never a
problem.

This change primarily impacts the behavior of /dev/random on embedded
systems with read-only media that do not configure "nodevice random".  We
toggle the default from 'charge on blindly with no entropy' to 'block
indefinitely.'  This default is safer, but may cause frustration.  Embedded
system designers using FreeBSD have several options.  The most obvious is to
plan to have a small writable NVRAM or NAND to persist entropy, like larger
systems.  Early entropy can be fed from any loader, or by writing directly
to /dev/random during boot.  Some embedded SoCs now provide a fast hardware
entropy source; this would also work for quickly seeding Fortuna.  A 3rd
option would be creating an embedded-specific, more simplistic random
module, like that designed by DJB in [1] (this design still requires a small
rewritable media for forward secrecy).  Finally, the least preferred option
might be "nodevice random", although I plan to remove this in a subsequent
revision.

To help developers emulate the behavior of these embedded systems on
ordinary workstations, the tunable kern.random.block_seeded_status was
added.  When set to 1, it blocks the random device.

I attempted to document this change in random.4 and random.9 and ran into a
bunch of out-of-date or irrelevant or inaccurate content and ended up
rototilling those documents more than I intended to.  Sorry.  I think
they're in a better state now.

PR:		230875
Reviewed by:	delphij, markm (earlier version)
Approved by:	secteam(delphij), devrandom(markm)
Relnotes:	yes
Differential Revision:	https://reviews.freebsd.org/D19744
2019-04-15 18:40:36 +00:00
..
arm sys/kern: adoption of SPDX licensing ID tags. 2017-11-27 15:20:12 +00:00
arm64 arm64: add ".arch armv8-a+crc" to allow use of crc instructions 2017-06-08 20:06:09 +00:00
x86 x86/crc32_sse42.c: quiet unused function warning 2017-08-11 17:05:31 +00:00
arc4random.c random(4): Block read_random(9) on initial seeding 2019-04-15 18:40:36 +00:00
ashldi3.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
ashrdi3.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
asprintf.c Implement asprintf in libkern 2015-03-01 00:22:16 +00:00
bcd.c Use time_t for intermediate values to avoid overflow in clock_ts_to_ct 2017-01-24 18:05:29 +00:00
bcmp.c Use __builtin for various mem* and b* (e.g. bzero) routines. 2018-06-02 18:03:35 +00:00
bcopy.c Simplify things a little 2018-05-10 02:31:48 +00:00
bsearch.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
cmpdi2.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
crc32.c Continuing efforts to provide hardening of FFS, this change adds a 2017-09-22 12:45:15 +00:00
divdi3.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
explicit_bzero.c
ffs.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
ffsl.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
ffsll.c libkern: ffs, fls: s/4/3/ the 3rd BSD clause 2015-10-22 21:04:47 +00:00
fls.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
flsl.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
flsll.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
fnmatch.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
iconv_converter_if.m
iconv_ucs.c sys/kern: adoption of SPDX licensing ID tags. 2017-11-27 15:20:12 +00:00
iconv_xlat16.c sys/kern: adoption of SPDX licensing ID tags. 2017-11-27 15:20:12 +00:00
iconv_xlat.c sys/kern: adoption of SPDX licensing ID tags. 2017-11-27 15:20:12 +00:00
iconv.c iconv uses strlen directly on user supplied memory 2018-02-26 18:23:36 +00:00
inet_aton.c sys/kern: adoption of SPDX licensing ID tags. 2017-11-27 15:20:12 +00:00
inet_ntoa.c Remove inet_ntoa() from the kernel 2017-02-16 20:50:01 +00:00
inet_ntop.c
inet_pton.c
jenkins_hash.c
lshrdi3.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
mcount.c mcount: tidy up ANSIfication 2018-10-20 22:39:35 +00:00
memcchr.c sys/kern: adoption of SPDX licensing ID tags. 2017-11-27 15:20:12 +00:00
memchr.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
memcmp.c Use __builtin for various mem* and b* (e.g. bzero) routines. 2018-06-02 18:03:35 +00:00
memmem.c libkern: Remove obsolete 'register' keyword 2017-01-12 17:02:29 +00:00
memset.c Use __builtin for various mem* and b* (e.g. bzero) routines. 2018-06-02 18:03:35 +00:00
moddi3.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
murmur3_32.c
qdivrem.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
qsort_r.c
qsort.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
quad.h sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
random.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
scanc.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
strcasecmp.c spdx: initial adoption of licensing ID tags. 2017-11-18 14:26:50 +00:00
strcat.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
strchr.c str(r)chr: Replace union abuse with __DECONST 2018-06-04 18:47:14 +00:00
strcmp.c libkern: use nul for terminating char rather than 0 2018-02-13 19:17:48 +00:00
strcpy.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
strcspn.c sys/kern: adoption of SPDX licensing ID tags. 2017-11-27 15:20:12 +00:00
strdup.c Add non-sleepable strdup variant strdup_flags 2019-02-20 20:48:10 +00:00
strlcat.c sys/kern: adoption of SPDX licensing ID tags. 2017-11-27 15:20:12 +00:00
strlcpy.c Sync strlcpy with userland version, again 2018-06-21 17:35:13 +00:00
strlen.c Remove "All rights reserved" from my files. 2018-05-10 06:41:08 +00:00
strncat.c libkern: use nul for terminating char rather than 0 2018-02-13 19:17:48 +00:00
strncmp.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
strncpy.c libkern: use nul for terminating char rather than 0 2018-02-13 19:17:48 +00:00
strndup.c
strnlen.c sys/kern: adoption of SPDX licensing ID tags. 2017-11-27 15:20:12 +00:00
strrchr.c str(r)chr: Replace union abuse with __DECONST 2018-06-04 18:47:14 +00:00
strsep.c libkern: use nul for terminating char rather than 0 2018-02-13 19:17:48 +00:00
strspn.c sys/kern: adoption of SPDX licensing ID tags. 2017-11-27 15:20:12 +00:00
strstr.c libkern: use nul for terminating char rather than 0 2018-02-13 19:17:48 +00:00
strtol.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
strtoq.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
strtoul.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
strtouq.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
strvalid.c sys/kern: adoption of SPDX licensing ID tags. 2017-11-27 15:20:12 +00:00
timingsafe_bcmp.c
ucmpdi2.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
udivdi3.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
umoddi3.c sys: further adoption of SPDX licensing ID tags. 2017-11-20 19:43:44 +00:00
zlib.c Remove register keyword from sys/ and ANSIfy prototypes 2017-05-17 00:34:34 +00:00