glibc/sysdeps/nptl
Jason A. Donenfeld eaad4f9e8f arc4random: simplify design for better safety
Rather than buffering 16 MiB of entropy in userspace (by way of
chacha20), simply call getrandom() every time.

This approach is doubtlessly slower, for now, but trying to prematurely
optimize arc4random appears to be leading toward all sorts of nasty
properties and gotchas. Instead, this patch takes a much more
conservative approach. The interface is added as a basic loop wrapper
around getrandom(), and then later, the kernel and libc together can
work together on optimizing that.

This prevents numerous issues in which userspace is unaware of when it
really must throw away its buffer, since we avoid buffering all
together. Future improvements may include userspace learning more from
the kernel about when to do that, which might make these sorts of
chacha20-based optimizations more possible. The current heuristic of 16
MiB is meaningless garbage that doesn't correspond to anything the
kernel might know about. So for now, let's just do something
conservative that we know is correct and won't lead to cryptographic
issues for users of this function.

This patch might be considered along the lines of, "optimization is the
root of all evil," in that the much more complex implementation it
replaces moves too fast without considering security implications,
whereas the incremental approach done here is a much safer way of going
about things. Once this lands, we can take our time in optimizing this
properly using new interplay between the kernel and userspace.

getrandom(0) is used, since that's the one that ensures the bytes
returned are cryptographically secure. But on systems without it, we
fallback to using /dev/urandom. This is unfortunate because it means
opening a file descriptor, but there's not much of a choice. Secondly,
as part of the fallback, in order to get more or less the same
properties of getrandom(0), we poll on /dev/random, and if the poll
succeeds at least once, then we assume the RNG is initialized. This is a
rough approximation, as the ancient "non-blocking pool" initialized
after the "blocking pool", not before, and it may not port back to all
ancient kernels, though it does to all kernels supported by glibc
(≥3.2), so generally it's the best approximation we can do.

The motivation for including arc4random, in the first place, is to have
source-level compatibility with existing code. That means this patch
doesn't attempt to litigate the interface itself. It does, however,
choose a conservative approach for implementing it.

Cc: Adhemerval Zanella Netto <adhemerval.zanella@linaro.org>
Cc: Florian Weimer <fweimer@redhat.com>
Cc: Cristian Rodríguez <crrodriguez@opensuse.org>
Cc: Paul Eggert <eggert@cs.ucla.edu>
Cc: Mark Harris <mark.hsj@gmail.com>
Cc: Eric Biggers <ebiggers@kernel.org>
Cc: linux-crypto@vger.kernel.org
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>

Reviewed-by: Adhemerval Zanella  <adhemerval.zanella@linaro.org>
2022-07-27 08:58:27 -03:00
..
bits Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
sys Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
Implies
Makeconfig Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
Makefile Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
Subdirs
_Fork.c arc4random: simplify design for better safety 2022-07-27 08:58:27 -03:00
aio_misc.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
dl-mutex.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
dl-thread_gscope_wait.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
dl-tls_init_tp.c nptl: Handle spurious EINTR when thread cancellation is disabled (BZ#29029) 2022-04-14 12:48:31 -03:00
dl-tunables.list Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
fork.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
futex-internal.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
gai_misc.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
internaltypes.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
jmp-unwind.c Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
libc-lock.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
libc-lockP.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
libc_start_call_main.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
lowlevellock-futex.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
lowlevellock.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
malloc-machine.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
proc_service.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
pthread-offsets.h nptl: update default pthread-offsets.h 2020-02-10 17:01:21 +01:00
pthread.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
pthreadP.h nptl: Add backoff mechanism to spinlock loop 2022-05-09 14:38:40 -07:00
pthread_atfork_compat.h Consolidate pthread_atfork 2021-06-24 10:04:41 -03:00
pthread_early_init.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
pthread_mutex_backoff.h nptl: Add backoff mechanism to spinlock loop 2022-05-09 14:38:40 -07:00
pthread_mutex_conf.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
setxid.h Replace __libc_multiple_threads with __libc_single_threaded 2022-07-05 10:14:47 -03:00
stdio-lock.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
tcb-access.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
tcb-offsets.h
thread_db.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
thread_pointer.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
timer_routines.h Update copyright dates with scripts/update-copyrights 2022-01-01 11:40:24 -08:00
tst-mqueue8x.c