linux/drivers/char
Jason A. Donenfeld 4ad10a5f5f random: introduce generic vDSO getrandom() implementation
Provide a generic C vDSO getrandom() implementation, which operates on
an opaque state returned by vgetrandom_alloc() and produces random bytes
the same way as getrandom(). This has the following API signature:

  ssize_t vgetrandom(void *buffer, size_t len, unsigned int flags,
                     void *opaque_state, size_t opaque_len);

The return value and the first three arguments are the same as ordinary
getrandom(), while the last two arguments are a pointer to the opaque
allocated state and its size. Were all five arguments passed to the
getrandom() syscall, nothing different would happen, and the functions
would have the exact same behavior.

The actual vDSO RNG algorithm implemented is the same one implemented by
drivers/char/random.c, using the same fast-erasure techniques as that.
Should the in-kernel implementation change, so too will the vDSO one.

It requires an implementation of ChaCha20 that does not use any stack,
in order to maintain forward secrecy if a multi-threaded program forks
(though this does not account for a similar issue with SA_SIGINFO
copying registers to the stack), so this is left as an
architecture-specific fill-in. Stack-less ChaCha20 is an easy algorithm
to implement on a variety of architectures, so this shouldn't be too
onerous.

Initially, the state is keyless, and so the first call makes a
getrandom() syscall to generate that key, and then uses it for
subsequent calls. By keeping track of a generation counter, it knows
when its key is invalidated and it should fetch a new one using the
syscall. Later, more than just a generation counter might be used.

Since MADV_WIPEONFORK is set on the opaque state, the key and related
state is wiped during a fork(), so secrets don't roll over into new
processes, and the same state doesn't accidentally generate the same
random stream. The generation counter, as well, is always >0, so that
the 0 counter is a useful indication of a fork() or otherwise
uninitialized state.

If the kernel RNG is not yet initialized, then the vDSO always calls the
syscall, because that behavior cannot be emulated in userspace, but
fortunately that state is short lived and only during early boot. If it
has been initialized, then there is no need to inspect the `flags`
argument, because the behavior does not change post-initialization
regardless of the `flags` value.

Since the opaque state passed to it is mutated, vDSO getrandom() is not
reentrant, when used with the same opaque state, which libc should be
mindful of.

The function works over an opaque per-thread state of a particular size,
which must be marked VM_WIPEONFORK, VM_DONTDUMP, VM_NORESERVE, and
VM_DROPPABLE for proper operation. Over time, the nuances of these
allocations may change or grow or even differ based on architectural
features.

The opaque state passed to vDSO getrandom() must be allocated using the
mmap_flags and mmap_prot parameters provided by the vgetrandom_opaque_params
struct, which also contains the size of each state. That struct can be
obtained with a call to vgetrandom(NULL, 0, 0, &params, ~0UL). Then,
libc can call mmap(2) and slice up the returned array into a state per
each thread, while ensuring that no single state straddles a page
boundary. Libc is expected to allocate a chunk of these on first use,
and then dole them out to threads as they're created, allocating more
when needed.

vDSO getrandom() provides the ability for userspace to generate random
bytes quickly and safely, and is intended to be integrated into libc's
thread management. As an illustrative example, the introduced code in
the vdso_test_getrandom self test later in this series might be used to
do the same outside of libc. In a libc the various pthread-isms are
expected to be elided into libc internals.

Reviewed-by: Thomas Gleixner <tglx@linutronix.de>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
2024-07-19 20:22:12 +02:00
..
agp alpha: don't make functions public without a reason 2024-05-03 22:09:21 +02:00
hw_random This push fixes a new run-time warning triggered by tpm. 2024-05-29 09:12:58 -07:00
ipmi ipmi: kcs_bmc_npcm7xx: Convert to platform remove callback returning void 2024-04-17 14:55:04 -05:00
mwave char/mwave: Adjust io port register size 2021-12-03 14:27:06 +01:00
tpm tpm: Address !chip->auth in tpm_buf_append_hmac_session*() 2024-07-05 02:12:27 +03:00
xilinx_hwicap char: xilinx_hwicap: Fix NULL vs IS_ERR() bug 2024-03-07 21:48:38 +00:00
xillybus char: xillybus: Convert to platform remove callback returning void 2024-03-07 21:49:30 +00:00
adi.c
apm-emulation.c apm-emulation: drop unexpected word "the" in the comments 2022-06-27 16:15:27 +02:00
applicom.c applicom: Fix PCI device refcount leak in applicom_init() 2023-01-20 13:05:39 +01:00
applicom.h
bsr.c char: Explicitly include correct DT includes 2023-07-30 18:15:27 +02:00
ds1620.c
dsp56k.c dsp56k: make dsp56k_class a static const structure 2023-06-23 10:27:08 +02:00
dtlk.c
hangcheck-timer.c
hpet.c hpet: remove hpets::hp_clocksource 2024-03-07 21:48:00 +00:00
Kconfig arch: Remove Itanium (IA-64) architecture 2023-09-11 08:13:17 +00:00
lp.c char: lp: make lp_class a static const structure 2023-06-23 10:27:11 +02:00
Makefile arch: Remove Itanium (IA-64) architecture 2023-09-11 08:13:17 +00:00
mem.c Char/Misc and other driver subsystem changes for 6.10-rc1 2024-05-22 12:26:46 -07:00
misc.c char: misc: make misc_class a static const structure 2023-06-23 10:27:15 +02:00
nsc_gpio.c
nvram.c
nwbutton.c
nwbutton.h
nwflash.c
pc8736x_gpio.c
powernv-op-panel.c powerpc/powernv: Convert to platform remove callback returning void 2024-04-11 15:26:59 +02:00
ppdev.c ppdev: Add an error check in register_device 2024-05-04 18:59:38 +02:00
ps3flash.c powerpc/ps3: make system bus's remove and shutdown callbacks return void 2020-12-04 01:01:22 +11:00
random.c random: introduce generic vDSO getrandom() implementation 2024-07-19 20:22:12 +02:00
scx200_gpio.c
sonypi.c sonypi: Convert to platform remove callback returning void 2024-04-11 15:27:02 +02:00
tlclk.c
toshiba.c module: remove never implemented MODULE_SUPPORTED_DEVICE 2021-03-17 13:16:18 -07:00
ttyprintk.c tty: ttyprintk: convert to u8 and size_t 2023-12-08 12:02:38 +01:00
uv_mmtimer.c
virtio_console.c virtio_console: drop owner assignment 2024-05-22 08:31:16 -04:00