4.19.249-rc1 review
Subsystems (5)
Greg Kroah-Hartman
maintainerPATCH 1/234 9p: missing chunk of "fs/9p: Dont update file type when updating file attributes"
In reply to Greg Kroah-HartmanFrom: Al Viro <[email protected]>
commit b577d0cd2104fdfcf0ded3707540a12be8ddd8b0 upstream.
In commit 45089142b149 Aneesh had missed one (admittedly, very unlikely to hit) case in v9fs_stat2inode_dotl(). However, the same considerations apply there as well - we have no business whatsoever to change ->i_rdev or the file type.
--- fs/9p/vfs_inode_dotl.c | 10 +++------- 1 file changed, 3 insertions(+), 7 deletions(-)
fs/9p/vfs_inode_dotl.c +3 -7
@@ -656,14 +656,10 @@ v9fs_stat2inode_dotl(struct p9_stat_dotlif (stat->st_result_mask &P9_STATS_NLINK )set_nlink (inode, stat->st_nlink);if (stat->st_result_mask &P9_STATS_MODE ) { - inode->i_mode = stat->st_mode; -if ((S_ISBLK (inode->i_mode)) || - (S_ISCHR (inode->i_mode))) -init_special_inode (inode, inode->i_mode, - inode->i_rdev); + mode = stat->st_mode &S_IALLUGO ; + mode |= inode->i_mode & ~S_IALLUGO ; + inode->i_mode = mode; } -if (stat->st_result_mask &P9_STATS_RDEV ) - inode->i_rdev =new_decode_dev (stat->st_rdev);if (!(flags &V9FS_STAT2INODE_KEEP_ISIZE ) && stat->st_result_mask &P9_STATS_SIZE )v9fs_i_size_write (inode, stat->st_size);
Greg Kroah-Hartman
maintainerPATCH 10/234 random: Support freezable kthreads in add_hwgenerator_randomness()
In reply to Greg Kroah-HartmanFrom: Stephen Boyd <[email protected]>
commit ff296293b3538d19278a7f7cd1f3aa600ad9164c upstream.
The kthread calling this function is freezable after commit 03a3bb7ae631 ("hwrng: core - Freeze khwrng thread during suspend") is applied. Unfortunately, this function uses wait_event_interruptible() but doesn't check for the kthread being woken up by the fake freezer signal. When a user suspends the system, this kthread will wake up and if it fails the entropy size check it will immediately go back to sleep and not go into the freezer. Eventually, suspend will fail because the task never froze and a warning message like this may appear:
PM: suspend entry (deep)
Filesystems sync: 0.000 seconds
Freezing user space processes ... (elapsed 0.001 seconds) done.
OOM killer disabled.
Freezing remaining freezable tasks ...
Freezing of tasks failed after 20.003 seconds (1 tasks refusing to freeze, wq_busy=0):
hwrng R running task 0 289 2 0x00000020
[<c08c64c4>] (__schedule) from [<c08c6a10>] (schedule+0x3c/0xc0)
[<c08c6a10>] (schedule) from [<c05dbd8c>] (add_hwgenerator_randomness+0xb0/0x100) [<c05dbd8c>] (add_hwgenerator_randomness) from [<bf1803c8>] (hwrng_fillfn+0xc0/0x14c [rng_core]) [<bf1803c8>] (hwrng_fillfn [rng_core]) from [<c015abec>] (kthread+0x134/0x148) [<c015abec>] (kthread) from [<c01010e8>] (ret_from_fork+0x14/0x2c)
Check for a freezer signal here and skip adding any randomness if the task wakes up because it was frozen. This should make the kthread freeze properly and suspend work again.
--- drivers/char/random.c | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-)
drivers/char/random.c +7 -3
@@ -2483,6 +2483,7 @@ void add_hwgenerator_randomness(const ch size_t entropy) {struct entropy_store *poolp = &input_pool; +bool frozen = false;if (unlikely (crng_init ==0 )) {crng_fast_load (buffer, count);
@@ -2493,9 +2494,12 @@ void add_hwgenerator_randomness(const ch* We'll be woken up again once below random_write_wakeup_thresh, * or when the calling thread is about to terminate. */ -wait_event_interruptible (random_write_wait,kthread_should_stop () || +wait_event_interruptible (random_write_wait, +kthread_freezable_should_stop (&frozen) ||ENTROPY_BITS (&input_pool) <= random_write_wakeup_bits); -mix_pool_bytes (poolp, buffer, count); -credit_entropy_bits (poolp, entropy); +if (!frozen) { +mix_pool_bytes (poolp, buffer, count); +credit_entropy_bits (poolp, entropy); + } }EXPORT_SYMBOL_GPL (add_hwgenerator_randomness);
Greg Kroah-Hartman
maintainerPATCH 14/234 Revert "hwrng: core - Freeze khwrng thread during suspend"
In reply to Greg Kroah-HartmanFrom: Herbert Xu <[email protected]>
commit 08e97aec700aeff54c4847f170e566cbd7e14e81 upstream.
This reverts commit 03a3bb7ae631 ("hwrng: core - Freeze khwrng thread during suspend"), ff296293b353 ("random: Support freezable kthreads in add_hwgenerator_randomness()") and 59b569480dc8 ("random:
Use wait_event_freezable() in add_hwgenerator_randomness()").
These patches introduced regressions and we need more time to get them ready for mainline.
--- drivers/char/random.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/char/random.c +1 -3
@@ -327,7 +327,6 @@#include <linux/percpu.h> #include <linux/cryptohash.h> #include <linux/fips.h> -#include <linux/freezer.h> #include <linux/ptrace.h> #include <linux/workqueue.h> #include <linux/irq.h>
@@ -2494,8 +2493,7 @@ void add_hwgenerator_randomness(const ch* We'll be woken up again once below random_write_wakeup_thresh, * or when the calling thread is about to terminate. */ -wait_event_freezable (random_write_wait, -kthread_should_stop () || +wait_event_interruptible (random_write_wait,kthread_should_stop () ||ENTROPY_BITS (&input_pool) <= random_write_wakeup_bits);mix_pool_bytes (poolp, buffer, count);credit_entropy_bits (poolp, entropy);
Greg Kroah-Hartman
maintainerPATCH 15/234 crypto: blake2s - generic C library implementation and selftest
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 66d7fb94e4ffe5acc589e0b2b4710aecc1f07a28 upstream.
The C implementation was originally based on Samuel Neves' public domain reference implementation but has since been heavily modified for the kernel. We're able to do compile-time optimizations by moving some scaffolding around the final function into the header file.
Information: https://blake2.net/
[ardb: - move from lib/zinc to lib/crypto
- remove simd handling
- rewrote selftest for better coverage
- use fixed digest length for blake2s_hmac() and rename to blake2s256_hmac() ]
[Jason: for stable, skip kconfig and wire up directly, and skip the arch
hooks; optimized implementations need not be backported.]
--- include/crypto/blake2s.h | 106 ++++++ include/crypto/internal/blake2s.h | 19 + lib/Makefile | 2 lib/crypto/Makefile | 7 lib/crypto/blake2s-generic.c | 111 ++++++ lib/crypto/blake2s-selftest.c | 622 ++++++++++++++++++++++++++++++++++++++ lib/crypto/blake2s.c | 115 +++++++ 7 files changed, 982 insertions(+) create mode 100644 include/crypto/blake2s.h create mode 100644 include/crypto/internal/blake2s.h create mode 100644 lib/crypto/Makefile create mode 100644 lib/crypto/blake2s-generic.c create mode 100644 lib/crypto/blake2s-selftest.c create mode 100644 lib/crypto/blake2s.c
include/crypto/blake2s.h new file +106 -0
@@ -0,0 +1,106 @@ +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ +/* +* Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved. +*/ + +#ifndef BLAKE2S_H +#define BLAKE2S_H + +#include <linux/types.h> +#include <linux/kernel.h> +#include <linux/string.h> + +#include <asm/bug.h> + +enum blake2s_lengths { +BLAKE2S_BLOCK_SIZE =64 , +BLAKE2S_HASH_SIZE =32 , +BLAKE2S_KEY_SIZE =32 , + +BLAKE2S_128_HASH_SIZE =16 , +BLAKE2S_160_HASH_SIZE =20 , +BLAKE2S_224_HASH_SIZE =28 , +BLAKE2S_256_HASH_SIZE =32 , +}; + +struct blake2s_state { +u32 h[8 ]; +u32 t[2 ]; +u32 f[2 ]; +u8 buf[BLAKE2S_BLOCK_SIZE ]; +unsigned int buflen; +unsigned int outlen; +}; + +enum blake2s_iv { +BLAKE2S_IV0 =0x6A09E667UL , +BLAKE2S_IV1 =0xBB67AE85UL , +BLAKE2S_IV2 =0x3C6EF372UL , +BLAKE2S_IV3 =0xA54FF53AUL , +BLAKE2S_IV4 =0x510E527FUL , +BLAKE2S_IV5 =0x9B05688CUL , +BLAKE2S_IV6 =0x1F83D9ABUL , +BLAKE2S_IV7 =0x5BE0CD19UL , +}; + +void blake2s_update (struct blake2s_state *state,const u8 *in,size_t inlen); +void blake2s_final (struct blake2s_state *state,u8 *out); + +static inline void blake2s_init_param (struct blake2s_state *state, +const u32 param) +{ + *state = (struct blake2s_state ){{ +BLAKE2S_IV0 ^ param, +BLAKE2S_IV1 , +BLAKE2S_IV2 , +BLAKE2S_IV3 , +BLAKE2S_IV4 , +BLAKE2S_IV5 , +BLAKE2S_IV6 , +BLAKE2S_IV7 , + }}; +} + +static inline void blake2s_init (struct blake2s_state *state, +const size_t outlen) +{ +blake2s_init_param (state,0x01010000 | outlen); + state->outlen = outlen; +} + +static inline void blake2s_init_key (struct blake2s_state *state, +const size_t outlen,const void *key, +const size_t keylen) +{ +WARN_ON (IS_ENABLED (DEBUG ) && (!outlen || outlen >BLAKE2S_HASH_SIZE || + !key || !keylen || keylen >BLAKE2S_KEY_SIZE )); + +blake2s_init_param (state,0x01010000 | keylen <<8 | outlen); +memcpy (state->buf, key, keylen); + state->buflen =BLAKE2S_BLOCK_SIZE ; + state->outlen = outlen; +} + +static inline void blake2s (u8 *out,const u8 *in,const u8 *key, +const size_t outlen,const size_t inlen, +const size_t keylen) +{ +struct blake2s_state state; + +WARN_ON (IS_ENABLED (DEBUG ) && ((!in && inlen >0 ) || !out || !outlen || + outlen >BLAKE2S_HASH_SIZE || keylen >BLAKE2S_KEY_SIZE || + (!key && keylen))); + +if (keylen) +blake2s_init_key (&state, outlen, key, keylen); +else +blake2s_init (&state, outlen); + +blake2s_update (&state, in, inlen); +blake2s_final (&state, out); +} + +void blake2s256_hmac (u8 *out,const u8 *in,const u8 *key,const size_t inlen, +const size_t keylen); + +#endif /* BLAKE2S_H */
include/crypto/internal/blake2s.h new file +19 -0
@@ -0,0 +1,19 @@ +/* SPDX-License-Identifier: GPL-2.0 OR MIT */ + +#ifndef BLAKE2S_INTERNAL_H +#define BLAKE2S_INTERNAL_H + +#include <crypto/blake2s.h> + +void blake2s_compress_generic (struct blake2s_state *state,const u8 *block, +size_t nblocks,const u32 inc); + +void blake2s_compress_arch (struct blake2s_state *state,const u8 *block, +size_t nblocks,const u32 inc); + +static inline void blake2s_set_lastblock (struct blake2s_state *state) +{ + state->f[0 ] =-1 ; +} + +#endif /* BLAKE2S_INTERNAL_H */
lib/Makefile +2 -0
@@ -283,3 +283,5 @@ obj-$(CONFIG_GENERIC_LIB_LSHRDI3) += lsh obj-$(CONFIG_GENERIC_LIB_MULDI3) += muldi3.o obj-$(CONFIG_GENERIC_LIB_CMPDI2) += cmpdi2.o obj-$(CONFIG_GENERIC_LIB_UCMPDI2) += ucmpdi2.o + +obj-y += crypto/
lib/crypto/Makefile new file +7 -0
@@ -0,0 +1,7 @@ +# SPDX-License-Identifier: GPL-2.0 + +obj-y += libblake2s.o +libblake2s-y += blake2s.o blake2s-generic.o +ifneq ($(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS),y) +libblake2s-y += blake2s-selftest.o +endif
lib/crypto/blake2s-generic.c new file +111 -0
111 lines not on this page · raw message
lib/crypto/blake2s-selftest.c new file +622 -0
622 lines not on this page · raw message
lib/crypto/blake2s.c new file +115 -0
115 lines not on this page · raw message
Greg Kroah-Hartman
maintainerPATCH 4/234 drivers/char/random.c: make primary_crng static
In reply to Greg Kroah-HartmanFrom: Rasmus Villemoes <[email protected]>
commit 764ed189c82090c1d85f0e30636156736d8f09a8 upstream.
Since the definition of struct crng_state is private to random.c, and primary_crng is neither declared or used elsewhere, there's no reason for that symbol to have external linkage.
--- drivers/char/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
@@ -415,7 +415,7 @@ struct crng_state {spinlock_t lock; }; -struct crng_state primary_crng = { +static struct crng_state primary_crng = { .lock =__SPIN_LOCK_UNLOCKED (primary_crng.lock), };
Greg Kroah-Hartman
maintainerPATCH 11/234 fdt: add support for rng-seed
In reply to Greg Kroah-HartmanFrom: Hsin-Yi Wang <[email protected]>
commit 428826f5358c922dc378830a1717b682c0823160 upstream.
Introducing a chosen node, rng-seed, which is an entropy that can be passed to kernel called very early to increase initial device randomness. Bootloader should provide this entropy and the value is read from /chosen/rng-seed in DT.
Obtain of_fdt_crc32 for CRC check after early_init_dt_scan_nodes(), since early_init_dt_scan_chosen() would modify fdt to erase rng-seed.
Add a new interface add_bootloader_randomness() for rng-seed use case. Depends on whether the seed is trustworthy, rng seed would be passed to add_hwgenerator_randomness(). Otherwise it would be passed to add_device_randomness(). Decision is controlled by kernel config RANDOM_TRUST_BOOTLOADER.
--- drivers/char/Kconfig | 9 +++++++++ drivers/char/random.c | 14 ++++++++++++++ drivers/of/fdt.c | 14 ++++++++++++-- include/linux/random.h | 1 + 4 files changed, 36 insertions(+), 2 deletions(-)
drivers/char/Kconfig +9 -0
@@ -568,3 +568,12 @@ config RANDOM_TRUST_CPU has not installed a hidden back door to compromise the CPU's random number generation facilities. This can also be configured at boot with "random.trust_cpu=on/off". + +config RANDOM_TRUST_BOOTLOADER + bool "Trust the bootloader to initialize Linux's CRNG" + help + Some bootloaders can provide entropy to increase the kernel's initial + device randomness. Say Y here to assume the entropy provided by the + booloader is trustworthy so it will be added to the kernel's entropy + pool. Otherwise, say N here so it will be regarded as device input that + only mixes the entropy pool.
drivers/char/random.c +14 -0
@@ -2503,3 +2503,17 @@ void add_hwgenerator_randomness(const ch } }EXPORT_SYMBOL_GPL (add_hwgenerator_randomness); + +/* Handle random seed passed by bootloader. +* If the seed is trustworthy, it would be regarded as hardware RNGs. Otherwise +* it would be regarded as device data. +* The decision is controlled by CONFIG_RANDOM_TRUST_BOOTLOADER. +*/ +void add_bootloader_randomness (const void *buf,unsigned int size) +{ +if (IS_ENABLED (CONFIG_RANDOM_TRUST_BOOTLOADER )) +add_hwgenerator_randomness (buf, size, size *8 ); +else +add_device_randomness (buf, size); +} +EXPORT_SYMBOL_GPL (add_bootloader_randomness);
drivers/of/fdt.c +12 -2
@@ -25,6 +25,7 @@#include <linux/debugfs.h> #include <linux/serial_core.h> #include <linux/sysfs.h> +#include <linux/random.h> #include <asm/setup.h> /* for COMMAND_LINE_SIZE */ #include <asm/page.h>
@@ -1077,6 +1078,7 @@ int __init early_init_dt_scan_chosen(uns {int l;const char *p; +const void *rng_seed;pr_debug ("search \"chosen\", depth: %d, uname: %s\n" , depth, uname);
@@ -1111,6 +1113,14 @@ int __init early_init_dt_scan_chosen(unspr_debug ("Command line is: %s\n" , (char *)data); + rng_seed =of_get_flat_dt_prop (node,"rng-seed" , &l); +if (rng_seed && l >0 ) { +add_bootloader_randomness (rng_seed, l); + +/* try to clear seed so it won't be found. */ +fdt_nop_property (initial_boot_params, node,"rng-seed" ); + } +/* break now */ return 1 ; }
@@ -1213,8 +1223,6 @@ bool __init early_init_dt_verify(void *p/* Setup flat device-tree pointer */ initial_boot_params = params; - of_fdt_crc32 =crc32_be (~0 , initial_boot_params, -fdt_totalsize (initial_boot_params));return true; }
@@ -1240,6 +1248,8 @@ bool __init early_init_dt_scan(void *parreturn false;early_init_dt_scan_nodes (); + of_fdt_crc32 =crc32_be (~0 , initial_boot_params, +fdt_totalsize (initial_boot_params));return true; }
include/linux/random.h +1 -0
@@ -19,6 +19,7 @@ struct random_ready_callback { };extern void add_device_randomness (const void *,unsigned int ); +extern void add_bootloader_randomness (const void *,unsigned int );#if defined(LATENT_ENTROPY_PLUGIN ) && !defined(__CHECKER__)static inline void add_latent_entropy (void )
Greg Kroah-Hartman
maintainerPATCH 23/234 char/random: silence a lockdep splat with printk()
In reply to Greg Kroah-HartmanFrom: Sergey Senozhatsky <[email protected]>
[ Upstream commit 1b710b1b10eff9d46666064ea25f079f70bc67a8 ]
Sergey didn't like the locking order,
uart_port->lock -> tty_port->lock
uart_write (uart_port->lock)
__uart_start
pl011_start_tx
pl011_tx_chars
uart_write_wakeup
tty_port_tty_wakeup
tty_port_default
tty_port_tty_get (tty_port->lock)but those code is so old, and I have no clue how to de-couple it after checking other locks in the splat. There is an onging effort to make all printk() as deferred, so until that happens, workaround it for now as a short-term fix.
LTP: starting iogen01 (export LTPROOT; rwtest -N iogen01 -i 120s -s read,write -Da -Dv -n 2 500b:$TMPDIR/doio.f1.$$ 1000b:$TMPDIR/doio.f2.$$)
WARNING: possible circular locking dependency detected
------------------------------------------------------
doio/49441 is trying to acquire lock:ffff008b7cff7290 (&(&zone->lock)->rlock){..-.}, at: rmqueue+0x138/0x2050
but task is already holding lock:60ff000822352818 (&pool->lock/1){-.-.}, at: start_flush_work+0xd8/0x3f0
which lock already depends on the new lock.
the existing dependency chain (in reverse order) is:
-> #4 (&pool->lock/1){-.-.}:
lock_acquire+0x320/0x360
_raw_spin_lock+0x64/0x80
__queue_work+0x4b4/0xa10
queue_work_on+0xac/0x11c
tty_schedule_flip+0x84/0xbc
tty_flip_buffer_push+0x1c/0x28
pty_write+0x98/0xd0
n_tty_write+0x450/0x60c
tty_write+0x338/0x474
__vfs_write+0x88/0x214
vfs_write+0x12c/0x1a4
redirected_tty_write+0x90/0xdc
do_loop_readv_writev+0x140/0x180
do_iter_write+0xe0/0x10c
vfs_writev+0x134/0x1cc
do_writev+0xbc/0x130
__arm64_sys_writev+0x58/0x8c
el0_svc_handler+0x170/0x240
el0_sync_handler+0x150/0x250
el0_sync+0x164/0x180-> #3 (&(&port->lock)->rlock){-.-.}:
lock_acquire+0x320/0x360
_raw_spin_lock_irqsave+0x7c/0x9c
tty_port_tty_get+0x24/0x60
tty_port_default_wakeup+0x1c/0x3c
tty_port_tty_wakeup+0x34/0x40
uart_write_wakeup+0x28/0x44
pl011_tx_chars+0x1b8/0x270
pl011_start_tx+0x24/0x70
__uart_start+0x5c/0x68
uart_write+0x164/0x1c8
do_output_char+0x33c/0x348
n_tty_write+0x4bc/0x60c
tty_write+0x338/0x474
redirected_tty_write+0xc0/0xdc
do_loop_readv_writev+0x140/0x180
do_iter_write+0xe0/0x10c
vfs_writev+0x134/0x1cc
do_writev+0xbc/0x130
__arm64_sys_writev+0x58/0x8c
el0_svc_handler+0x170/0x240
el0_sync_handler+0x150/0x250
el0_sync+0x164/0x180-> #2 (&port_lock_key){-.-.}:
lock_acquire+0x320/0x360
_raw_spin_lock+0x64/0x80
pl011_console_write+0xec/0x2cc
console_unlock+0x794/0x96c
vprintk_emit+0x260/0x31c
vprintk_default+0x54/0x7c
vprintk_func+0x218/0x254
printk+0x7c/0xa4
register_console+0x734/0x7b0
uart_add_one_port+0x734/0x834
pl011_register_port+0x6c/0xac
sbsa_uart_probe+0x234/0x2ec
platform_drv_probe+0xd4/0x124
really_probe+0x250/0x71c
driver_probe_device+0xb4/0x200
__device_attach_driver+0xd8/0x188
bus_for_each_drv+0xbc/0x110
__device_attach+0x120/0x220
device_initial_probe+0x20/0x2c
bus_probe_device+0x54/0x100
device_add+0xae8/0xc2c
platform_device_add+0x278/0x3b8
platform_device_register_full+0x238/0x2ac
acpi_create_platform_device+0x2dc/0x3a8
acpi_bus_attach+0x390/0x3cc
acpi_bus_attach+0x108/0x3cc
acpi_bus_attach+0x108/0x3cc
acpi_bus_attach+0x108/0x3cc
acpi_bus_scan+0x7c/0xb0
acpi_scan_init+0xe4/0x304
acpi_init+0x100/0x114
do_one_initcall+0x348/0x6a0
do_initcall_level+0x190/0x1fc
do_basic_setup+0x34/0x4c
kernel_init_freeable+0x19c/0x260
kernel_init+0x18/0x338
ret_from_fork+0x10/0x18-> #1 (console_owner){-...}:
lock_acquire+0x320/0x360
console_lock_spinning_enable+0x6c/0x7c
console_unlock+0x4f8/0x96c
vprintk_emit+0x260/0x31c
vprintk_default+0x54/0x7c
vprintk_func+0x218/0x254
printk+0x7c/0xa4
get_random_u64+0x1c4/0x1dc
shuffle_pick_tail+0x40/0xac
__free_one_page+0x424/0x710
free_one_page+0x70/0x120
__free_pages_ok+0x61c/0xa94
__free_pages_core+0x1bc/0x294
memblock_free_pages+0x38/0x48
__free_pages_memory+0xcc/0xfc
__free_memory_core+0x70/0x78
free_low_memory_core_early+0x148/0x18c
memblock_free_all+0x18/0x54
mem_init+0xb4/0x17c
mm_init+0x14/0x38
start_kernel+0x19c/0x530-> #0 (&(&zone->lock)->rlock){..-.}:
validate_chain+0xf6c/0x2e2c
__lock_acquire+0x868/0xc2c
lock_acquire+0x320/0x360
_raw_spin_lock+0x64/0x80
rmqueue+0x138/0x2050
get_page_from_freelist+0x474/0x688
__alloc_pages_nodemask+0x3b4/0x18dc
alloc_pages_current+0xd0/0xe0
alloc_slab_page+0x2b4/0x5e0
new_slab+0xc8/0x6bc
___slab_alloc+0x3b8/0x640
kmem_cache_alloc+0x4b4/0x588
__debug_object_init+0x778/0x8b4
debug_object_init_on_stack+0x40/0x50
start_flush_work+0x16c/0x3f0
__flush_work+0xb8/0x124
flush_work+0x20/0x30
xlog_cil_force_lsn+0x88/0x204 [xfs]
xfs_log_force_lsn+0x128/0x1b8 [xfs]
xfs_file_fsync+0x3c4/0x488 [xfs]
vfs_fsync_range+0xb0/0xd0
generic_write_sync+0x80/0xa0 [xfs]
xfs_file_buffered_aio_write+0x66c/0x6e4 [xfs]
xfs_file_write_iter+0x1a0/0x218 [xfs]
__vfs_write+0x1cc/0x214
vfs_write+0x12c/0x1a4
ksys_write+0xb0/0x120
__arm64_sys_write+0x54/0x88
el0_svc_handler+0x170/0x240
el0_sync_handler+0x150/0x250
el0_sync+0x164/0x180other info that might help us debug this:
Chain exists of: &(&zone->lock)->rlock --> &(&port->lock)->rlock --> &pool->lock/1 Possible unsafe locking scenario:
CPU0 CPU1
---- ----
lock(&pool->lock/1);
lock(&(&port->lock)->rlock);
lock(&pool->lock/1);lock(&(&zone->lock)->rlock);
*** DEADLOCK ***
4 locks held by doio/49441:
#0: a0ff00886fc27408 (sb_writers#8){.+.+}, at: vfs_write+0x118/0x1a4 #1: 8fff00080810dfe0 (&xfs_nondir_ilock_class){++++}, at:
xfs_ilock+0x2a8/0x300 [xfs]
#2: ffff9000129f2390 (rcu_read_lock){....}, at:
rcu_lock_acquire+0x8/0x38
#3: 60ff000822352818 (&pool->lock/1){-.-.}, at:
start_flush_work+0xd8/0x3f0
stack backtrace:
CPU: 48 PID: 49441 Comm: doio Tainted: G W
Hardware name: HPE Apollo 70 /C01_APACHE_MB , BIOS
L50_5.13_1.11 06/18/2019
Call trace:
dump_backtrace+0x0/0x248
show_stack+0x20/0x2c
dump_stack+0xe8/0x150
print_circular_bug+0x368/0x380
check_noncircular+0x28c/0x294
validate_chain+0xf6c/0x2e2c
__lock_acquire+0x868/0xc2c
lock_acquire+0x320/0x360
_raw_spin_lock+0x64/0x80
rmqueue+0x138/0x2050
get_page_from_freelist+0x474/0x688
__alloc_pages_nodemask+0x3b4/0x18dc
alloc_pages_current+0xd0/0xe0
alloc_slab_page+0x2b4/0x5e0
new_slab+0xc8/0x6bc
___slab_alloc+0x3b8/0x640
kmem_cache_alloc+0x4b4/0x588
__debug_object_init+0x778/0x8b4
debug_object_init_on_stack+0x40/0x50
start_flush_work+0x16c/0x3f0
__flush_work+0xb8/0x124
flush_work+0x20/0x30
xlog_cil_force_lsn+0x88/0x204 [xfs]
xfs_log_force_lsn+0x128/0x1b8 [xfs]
xfs_file_fsync+0x3c4/0x488 [xfs]
vfs_fsync_range+0xb0/0xd0
generic_write_sync+0x80/0xa0 [xfs]
xfs_file_buffered_aio_write+0x66c/0x6e4 [xfs]
xfs_file_write_iter+0x1a0/0x218 [xfs]
__vfs_write+0x1cc/0x214
vfs_write+0x12c/0x1a4
ksys_write+0xb0/0x120
__arm64_sys_write+0x54/0x88
el0_svc_handler+0x170/0x240
el0_sync_handler+0x150/0x250
el0_sync+0x164/0x180--- drivers/char/random.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
drivers/char/random.c +3 -2
@@ -1690,8 +1690,9 @@ static void _warn_unseeded_randomness(co print_once = true;#endif if (__ratelimit (&unseeded_warning)) -pr_notice ("random: %s called from %pS with crng_init=%d\n" , - func_name, caller, crng_init); +printk_deferred (KERN_NOTICE "random: %s called from %pS " +"with crng_init=%d\n" , func_name, caller, + crng_init); }/*
Greg Kroah-Hartman
maintainerPATCH 33/234 random: split primary/secondary crng init paths
In reply to Greg Kroah-HartmanFrom: Mark Rutland <[email protected]>
commit 5cbe0f13b51ac2fb2fd55902cff8d0077fc084c0 upstream.
Currently crng_initialize() is used for both the primary CRNG and secondary CRNGs. While we wish to share common logic, we need to do a number of additional things for the primary CRNG, and this would be easier to deal with were these handled in separate functions.
This patch splits crng_initialize() into crng_initialize_primary() and crng_initialize_secondary(), with common logic factored out into a crng_init_try_arch() helper.
There should be no functional change as a result of this patch.
--- drivers/char/random.c | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-)
drivers/char/random.c +22 -12
@@ -783,27 +783,37 @@ static int __init parse_trust_cpu(char * }early_param ("random.trust_cpu" , parse_trust_cpu); -static void crng_initialize (struct crng_state *crng) +static bool crng_init_try_arch (struct crng_state *crng) {int i; -int arch_init =1 ; +bool arch_init = true;unsigned long rv; -memcpy (&crng->state[0 ],"expand 32-byte k" ,16 ); -if (crng == &primary_crng) -_extract_entropy (&input_pool, &crng->state[4 ], -sizeof (__u32) *12 ,0 ); -else -_get_random_bytes (&crng->state[4 ],sizeof (__u32) *12 );for (i =4 ; i <16 ; i++) {if (!arch_get_random_seed_long (&rv) && !arch_get_random_long (&rv)) { rv =random_get_entropy (); - arch_init =0 ; + arch_init = false; } crng->state[i] ^= rv; } -if (trust_cpu && arch_init && crng == &primary_crng) { + +return arch_init; +} + +static void crng_initialize_secondary (struct crng_state *crng) +{ +memcpy (&crng->state[0 ],"expand 32-byte k" ,16 ); +_get_random_bytes (&crng->state[4 ],sizeof (__u32) *12 ); +crng_init_try_arch (crng); + crng->init_time = jiffies -CRNG_RESEED_INTERVAL -1 ; +} + +static void __init crng_initialize_primary (struct crng_state *crng) +{ +memcpy (&crng->state[0 ],"expand 32-byte k" ,16 ); +_extract_entropy (&input_pool, &crng->state[4 ],sizeof (__u32) *12 ,0 ); +if (crng_init_try_arch (crng) && trust_cpu) {invalidate_batched_entropy ();numa_crng_init (); crng_init =2 ;
@@ -854,7 +864,7 @@ static void do_numa_crng_init(struct wor crng =kmalloc_node (sizeof (struct crng_state ),GFP_KERNEL | __GFP_NOFAIL, i);spin_lock_init (&crng->lock); -crng_initialize (crng); +crng_initialize_secondary (crng); pool[i] = crng; }/* pairs with READ_ONCE() in select_crng() */
@@ -1792,7 +1802,7 @@ int __init rand_initialize(void)init_std_data (&input_pool);if (crng_need_final_init)crng_finalize_init (&primary_crng); -crng_initialize (&primary_crng); +crng_initialize_primary (&primary_crng); crng_global_init_time = jiffies;if (ratelimit_disable) { urandom_warning.interval =0 ;
Greg Kroah-Hartman
maintainerPATCH 44/234 random: remove dead code left over from blocking pool
In reply to Greg Kroah-HartmanFrom: Eric Biggers <[email protected]>
commit 118a4417e14348b2e46f5e467da8444ec4757a45 upstream.
Remove some dead code that was left over following commit 90ea1c6436d2 ("random: remove the blocking pool").
--- drivers/char/random.c | 17 +------- include/trace/events/random.h | 83 ------------------------------------------ 2 files changed, 3 insertions(+), 97 deletions(-)
drivers/char/random.c +3 -14
@@ -502,7 +502,6 @@ struct entropy_store {unsigned short add_ptr;unsigned short input_rotate;int entropy_count; - unsigned int initialized:1; unsigned int last_data_init:1;__u8 last_data[EXTRACT_SIZE ]; };
@@ -662,7 +661,7 @@ static void process_random_ready_list(vo*/ static void credit_entropy_bits (struct entropy_store *r,int nbits) { -int entropy_count, orig, has_initialized =0 ; +int entropy_count, orig;const int pool_size = r->poolinfo->poolfracbits;int nfrac = nbits <<ENTROPY_SHIFT ;
@@ -719,23 +718,14 @@ retry:if (cmpxchg (&r->entropy_count, orig, entropy_count) != orig) goto retry; -if (has_initialized) { - r->initialized =1 ; -kill_fasync (&fasync,SIGIO ,POLL_IN ); - } -trace_credit_entropy_bits (r->name, nbits, entropy_count >>ENTROPY_SHIFT , _RET_IP_);if (r == &input_pool) {int entropy_bits = entropy_count >>ENTROPY_SHIFT ; -if (crng_init <2 ) { -if (entropy_bits <128 ) -return ; +if (crng_init <2 && entropy_bits >=128 )crng_reseed (&primary_crng, r); - entropy_bits =ENTROPY_BITS (r); - } } }
@@ -1391,8 +1381,7 @@ retry: }/* -* This function does the actual extraction for extract_entropy and -* extract_entropy_user. +* This function does the actual extraction for extract_entropy. * * Note: we assume that .poolwords is a multiple of 16 words. */
include/trace/events/random.h +0 -83
101 lines not on this page · raw message
Greg Kroah-Hartman
maintainerPATCH 12/234 random: Use wait_event_freezable() in add_hwgenerator_randomness()
In reply to Greg Kroah-HartmanFrom: Stephen Boyd <[email protected]>
commit 59b569480dc8bb9dce57cdff133853a842dfd805 upstream.
Sebastian reports that after commit ff296293b353 ("random: Support freezable kthreads in add_hwgenerator_randomness()") we can call might_sleep() when the task state is TASK_INTERRUPTIBLE (state=1). This leads to the following warning.
do not call blocking ops when !TASK_RUNNING; state=1 set at [<00000000349d1489>] prepare_to_wait_event+0x5a/0x180
WARNING: CPU: 0 PID: 828 at kernel/sched/core.c:6741 __might_sleep+0x6f/0x80 Modules linked in: CPU: 0 PID: 828 Comm: hwrng Not tainted 5.3.0-rc7-next-20190903+ #46 RIP: 0010:__might_sleep+0x6f/0x80 Call Trace: kthread_freezable_should_stop+0x1b/0x60 add_hwgenerator_randomness+0xdd/0x130 hwrng_fillfn+0xbf/0x120 kthread+0x10c/0x140 ret_from_fork+0x27/0x50
We shouldn't call kthread_freezable_should_stop() from deep within the wait_event code because the task state is still set as TASK_INTERRUPTIBLE instead of TASK_RUNNING and kthread_freezable_should_stop() will try to call into the freezer with the task in the wrong state. Use wait_event_freezable() instead so that it calls schedule() in the right place and tries to enter the freezer when the task state is TASK_RUNNING instead.
--- drivers/char/random.c | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-)
drivers/char/random.c +5 -7
@@ -327,6 +327,7 @@#include <linux/percpu.h> #include <linux/cryptohash.h> #include <linux/fips.h> +#include <linux/freezer.h> #include <linux/ptrace.h> #include <linux/workqueue.h> #include <linux/irq.h>
@@ -2483,7 +2484,6 @@ void add_hwgenerator_randomness(const ch size_t entropy) {struct entropy_store *poolp = &input_pool; -bool frozen = false;if (unlikely (crng_init ==0 )) {crng_fast_load (buffer, count);
@@ -2494,13 +2494,11 @@ void add_hwgenerator_randomness(const ch* We'll be woken up again once below random_write_wakeup_thresh, * or when the calling thread is about to terminate. */ -wait_event_interruptible (random_write_wait, -kthread_freezable_should_stop (&frozen) || +wait_event_freezable (random_write_wait, +kthread_should_stop () ||ENTROPY_BITS (&input_pool) <= random_write_wakeup_bits); -if (!frozen) { -mix_pool_bytes (poolp, buffer, count); -credit_entropy_bits (poolp, entropy); - } +mix_pool_bytes (poolp, buffer, count); +credit_entropy_bits (poolp, entropy); }EXPORT_SYMBOL_GPL (add_hwgenerator_randomness);
Greg Kroah-Hartman
maintainerPATCH 18/234 random: Dont wake crng_init_wait when crng_init == 1
In reply to Greg Kroah-HartmanFrom: Andy Lutomirski <[email protected]>
commit 4c8d062186d9923c09488716b2fb1b829b5b8006 upstream.
crng_init_wait is only used to wayt for crng_init to be set to 2, so there's no point to waking it when crng_init is set to 1. Remove the unnecessary wake_up_interruptible() call.
--- drivers/char/random.c | 1 - 1 file changed, 1 deletion(-)
drivers/char/random.c +0 -1
@@ -965,7 +965,6 @@ static int crng_fast_load(const char *cpif (crng_init_cnt >=CRNG_INIT_CNT_THRESH ) {invalidate_batched_entropy (); crng_init =1 ; -wake_up_interruptible (&crng_init_wait);pr_notice ("random: fast init done\n" ); }return 1 ;
Greg Kroah-Hartman
maintainerPATCH 28/234 random: remove unnecessary unlikely()
In reply to Greg Kroah-HartmanFrom: Yangtao Li <[email protected]>
commit 870e05b1b18814911cb2703a977f447cb974f0f9 upstream.
WARN_ON() already contains an unlikely(), so it's not necessary to use unlikely.
--- drivers/char/random.c | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-)
drivers/char/random.c +2 -4
@@ -738,10 +738,9 @@ retry: }while (unlikely (entropy_count < pool_size-2 && pnfrac)); } -if (unlikely (entropy_count <0 )) { +if (WARN_ON (entropy_count <0 )) {pr_warn ("random: negative entropy/overflow: pool %s count %d\n" , r->name, entropy_count); -WARN_ON (1 ); entropy_count =0 ; }else if (entropy_count > pool_size) entropy_count = pool_size;
@@ -1383,10 +1382,9 @@ retry:if (ibytes < min) ibytes =0 ; -if (unlikely (entropy_count <0 )) { +if (WARN_ON (entropy_count <0 )) {pr_warn ("random: negative entropy count: pool %s count %d\n" , r->name, entropy_count); -WARN_ON (1 ); entropy_count =0 ; } nfrac = ibytes << (ENTROPY_SHIFT +3 );
Greg Kroah-Hartman
maintainerPATCH 3/234 drivers/char/random.c: remove unused stuct poolinfo::poolbits
In reply to Greg Kroah-HartmanFrom: Rasmus Villemoes <[email protected]>
commit 3bd0b5bf7dc3ea02070fcbcd682ecf628269e8ef upstream.
This field is never used, might as well remove it.
--- drivers/char/random.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/char/random.c +3 -3
@@ -295,7 +295,7 @@* To allow fractional bits to be tracked, the entropy_count field is * denominated in units of 1/8th bits. * -* 2*(ENTROPY_SHIFT + log2(poolbits)) must <= 31, or the multiply in +* 2*(ENTROPY_SHIFT + poolbitshift) must <= 31, or the multiply in * credit_entropy_bits() needs to be 64 bits wide. */ #define ENTROPY_SHIFT 3
@@ -360,8 +360,8 @@ static int random_write_wakeup_bits = 28* irreducible, which we have made here. */ static const struct poolinfo { -int poolbitshift, poolwords, poolbytes, poolbits, poolfracbits; -#define S (x) ilog2(x)+5, (x), (x)*4, (x)*32, (x) << (ENTROPY_SHIFT+5) +int poolbitshift, poolwords, poolbytes, poolfracbits; +#define S (x) ilog2(x)+5, (x), (x)*4, (x) << (ENTROPY_SHIFT+5)int tap1, tap2, tap3, tap4, tap5; } poolinfo_table[] = {/* was: x^128 + x^103 + x^76 + x^51 +x^25 + x + 1 */
Greg Kroah-Hartman
maintainerPATCH 20/234 random: add GRND_INSECURE to return best-effort non-cryptographic bytes
In reply to Greg Kroah-HartmanFrom: Andy Lutomirski <[email protected]>
commit 75551dbf112c992bc6c99a972990b3f272247e23 upstream.
--- drivers/char/random.c | 11 +++++++++-- include/uapi/linux/random.h | 2 ++ 2 files changed, 11 insertions(+), 2 deletions(-)
drivers/char/random.c +9 -2
@@ -2197,7 +2197,14 @@ SYSCALL_DEFINE3(getrandom, char __user * {int ret; -if (flags & ~(GRND_NONBLOCK |GRND_RANDOM )) +if (flags & ~(GRND_NONBLOCK |GRND_RANDOM |GRND_INSECURE )) +return -EINVAL ; + +/* +* Requesting insecure and blocking randomness at the same time makes +* no sense. +*/ +if ((flags & (GRND_INSECURE |GRND_RANDOM )) == (GRND_INSECURE |GRND_RANDOM ))return -EINVAL ;if (count >INT_MAX )
@@ -2206,7 +2213,7 @@ SYSCALL_DEFINE3(getrandom, char __user *if (flags &GRND_RANDOM )return _random_read (flags &GRND_NONBLOCK , buf, count); -if (!crng_ready ()) { +if (!(flags &GRND_INSECURE ) && !crng_ready ()) {if (flags &GRND_NONBLOCK )return -EAGAIN ; ret =wait_for_random_bytes ();
include/uapi/linux/random.h +2 -0
@@ -49,8 +49,10 @@ struct rand_pool_info {* * GRND_NONBLOCK Don't block and return EAGAIN instead * GRND_RANDOM Use the /dev/random pool instead of /dev/urandom +* GRND_INSECURE Return non-cryptographic random bytes */ #define GRND_NONBLOCK 0x0001#define GRND_RANDOM 0x0002 +#define GRND_INSECURE 0x0004#endif /* _UAPI_LINUX_RANDOM_H */
Greg Kroah-Hartman
maintainerPATCH 6/234 random: move rand_initialize() earlier
In reply to Greg Kroah-HartmanFrom: Kees Cook <[email protected]>
commit d55535232c3dbde9a523a9d10d68670f5fe5dec3 upstream.
Right now rand_initialize() is run as an early_initcall(), but it only depends on timekeeping_init() (for mixing ktime_get_real() into the pools). However, the call to boot_init_stack_canary() for stack canary initialization runs earlier, which triggers a warning at boot:
random: get_random_bytes called from start_kernel+0x357/0x548 with crng_init=0
Instead, this moves rand_initialize() to after timekeeping_init(), and moves canary initialization here as well.
Note that this warning may still remain for machines that do not have UEFI RNG support (which initializes the RNG pools during setup_arch()), or for x86 machines without RDRAND (or booting without "random.trust=on" or CONFIG_RANDOM_TRUST_CPU=y).
--- drivers/char/random.c | 5 ++--- include/linux/random.h | 1 + init/main.c | 21 ++++++++++++++------- 3 files changed, 17 insertions(+), 10 deletions(-)
drivers/char/random.c +2 -3
@@ -1852,7 +1852,7 @@ EXPORT_SYMBOL(get_random_bytes_arch);* data into the pool to prepare it for use. The pool is not cleared * as that can only decrease the entropy in the pool. */ -static void init_std_data (struct entropy_store *r) +static void __init init_std_data (struct entropy_store *r) {int i;ktime_t now =ktime_get_real ();
@@ -1879,7 +1879,7 @@ static void init_std_data(struct entropy* take care not to overwrite the precious per platform data * we were given. */ -static int rand_initialize (void ) +int __init rand_initialize (void ) {init_std_data (&input_pool);init_std_data (&blocking_pool);
@@ -1891,7 +1891,6 @@ static int rand_initialize(void) }return 0 ; } -early_initcall (rand_initialize);#ifdef CONFIG_BLOCK void rand_initialize_disk (struct gendisk *disk)
include/linux/random.h +1 -0
@@ -36,6 +36,7 @@ extern void add_interrupt_randomness(intextern void get_random_bytes (void *buf,int nbytes);extern int wait_for_random_bytes (void ); +extern int __init rand_initialize (void );extern bool rng_is_initialized (void );extern int add_random_ready_callback (struct random_ready_callback *rdy);extern void del_random_ready_callback (struct random_ready_callback *rdy);
init/main.c +14 -7
@@ -549,13 +549,6 @@ asmlinkage __visible void __init start_kpage_address_init ();pr_notice ("%s" , linux_banner);setup_arch (&command_line); -/* -* Set up the the initial canary and entropy after arch -* and after adding latent and command line entropy. -*/ -add_latent_entropy (); -add_device_randomness (command_line,strlen (command_line)); -boot_init_stack_canary ();mm_init_cpumask (&init_mm);setup_command_line (command_line);setup_nr_cpu_ids ();
@@ -640,6 +633,20 @@ asmlinkage __visible void __init start_khrtimers_init ();softirq_init ();timekeeping_init (); + +/* +* For best initial stack canary entropy, prepare it after: +* - setup_arch() for any UEFI RNG entropy and boot cmdline access +* - timekeeping_init() for ktime entropy used in rand_initialize() +* - rand_initialize() to get any arch-specific entropy like RDRAND +* - add_latent_entropy() to get any latent entropy +* - adding command line entropy +*/ +rand_initialize (); +add_latent_entropy (); +add_device_randomness (command_line,strlen (command_line)); +boot_init_stack_canary (); +time_init ();perf_event_init ();profile_init ();
Greg Kroah-Hartman
maintainerPATCH 13/234 char/random: Add a newline at the end of the file
In reply to Greg Kroah-HartmanFrom: Borislav Petkov <[email protected]>
commit 3fd57e7a9e66b9a8bcbf0560ff09e84d0b8de1bd upstream.
Quoted 8 lines · Linus Torvalds
The previous state of the file didn't have that
0xaat the end, so you get that-EXPORT_SYMBOL_GPL(add_bootloader_randomness); \ No newline at end of file +EXPORT_SYMBOL_GPL(add_bootloader_randomness);which is "the '-' line doesn't have a newline, the '+' line does" marker.
Aaha, that makes total sense, thanks for explaining. Oh well, let's fix it then so that people don't scratch heads like me.
--- drivers/char/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
@@ -2514,4 +2514,4 @@ void add_bootloader_randomness(const voielse add_device_randomness (buf, size); } -EXPORT_SYMBOL_GPL (add_bootloader_randomness); +EXPORT_SYMBOL_GPL (add_bootloader_randomness);
Greg Kroah-Hartman
maintainerPATCH 17/234 lib/crypto: sha1: re-roll loops to reduce code size
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 9a1536b093bb5bf60689021275fd24d513bb8db0 upstream.
With SHA-1 no longer being used for anything performance oriented, and also soon to be phased out entirely, we can make up for the space added by unrolled BLAKE2s by simply re-rolling SHA-1. Since SHA-1 is so much more complex, re-rolling it more or less takes care of the code size added by BLAKE2s. And eventually, hopefully we'll see SHA-1 removed entirely from most small kernel builds.
--- lib/sha1.c | 95 ++++++++----------------------------------------------------- 1 file changed, 14 insertions(+), 81 deletions(-)
lib/sha1.c +14 -81
@@ -10,6 +10,7 @@#include <linux/export.h> #include <linux/bitops.h> #include <linux/cryptohash.h> +#include <linux/string.h> #include <asm/unaligned.h> /*
@@ -55,7 +56,8 @@#define SHA_ROUND (t, input, fn, constant,A ,B ,C ,D ,E ) do { \ __u32 TEMP = input(t); setW(t, TEMP); \ E += TEMP + rol32(A,5) + (fn) + (constant); \ - B = ror32(B, 2); } while (0) + B = ror32(B, 2); \ + TEMP = E; E = D; D = C; C = B; B = A; A = TEMP; } while (0)#define T_0_15 (t,A ,B ,C ,D ,E ) SHA_ROUND(t, SHA_SRC, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )#define T_16_19 (t,A ,B ,C ,D ,E ) SHA_ROUND(t, SHA_MIX, (((C^D)&B)^D) , 0x5a827999, A, B, C, D, E )
@@ -82,6 +84,7 @@void sha_transform (__u32 *digest,const char *data,__u32 *array) {__u32 A ,B ,C ,D ,E ; +unsigned int i =0 ;A = digest[0 ];B = digest[1 ];
@@ -90,94 +93,24 @@ void sha_transform(__u32 *digest, constE = digest[4 ];/* Round 1 - iterations 0-16 take their input from 'data' */ -T_0_15 (0 ,A ,B ,C ,D ,E ); -T_0_15 (1 ,E ,A ,B ,C ,D ); -T_0_15 (2 ,D ,E ,A ,B ,C ); -T_0_15 (3 ,C ,D ,E ,A ,B ); -T_0_15 (4 ,B ,C ,D ,E ,A ); -T_0_15 (5 ,A ,B ,C ,D ,E ); -T_0_15 (6 ,E ,A ,B ,C ,D ); -T_0_15 (7 ,D ,E ,A ,B ,C ); -T_0_15 (8 ,C ,D ,E ,A ,B ); -T_0_15 (9 ,B ,C ,D ,E ,A ); -T_0_15 (10 ,A ,B ,C ,D ,E ); -T_0_15 (11 ,E ,A ,B ,C ,D ); -T_0_15 (12 ,D ,E ,A ,B ,C ); -T_0_15 (13 ,C ,D ,E ,A ,B ); -T_0_15 (14 ,B ,C ,D ,E ,A ); -T_0_15 (15 ,A ,B ,C ,D ,E ); +for (; i <16 ; ++i) +T_0_15 (i,A ,B ,C ,D ,E );/* Round 1 - tail. Input from 512-bit mixing array */ -T_16_19 (16 ,E ,A ,B ,C ,D ); -T_16_19 (17 ,D ,E ,A ,B ,C ); -T_16_19 (18 ,C ,D ,E ,A ,B ); -T_16_19 (19 ,B ,C ,D ,E ,A ); +for (; i <20 ; ++i) +T_16_19 (i,A ,B ,C ,D ,E );/* Round 2 */ -T_20_39 (20 ,A ,B ,C ,D ,E ); -T_20_39 (21 ,E ,A ,B ,C ,D ); -T_20_39 (22 ,D ,E ,A ,B ,C ); -T_20_39 (23 ,C ,D ,E ,A ,B ); -T_20_39 (24 ,B ,C ,D ,E ,A ); -T_20_39 (25 ,A ,B ,C ,D ,E ); -T_20_39 (26 ,E ,A ,B ,C ,D ); -T_20_39 (27 ,D ,E ,A ,B ,C ); -T_20_39 (28 ,C ,D ,E ,A ,B ); -T_20_39 (29 ,B ,C ,D ,E ,A ); -T_20_39 (30 ,A ,B ,C ,D ,E ); -T_20_39 (31 ,E ,A ,B ,C ,D ); -T_20_39 (32 ,D ,E ,A ,B ,C ); -T_20_39 (33 ,C ,D ,E ,A ,B ); -T_20_39 (34 ,B ,C ,D ,E ,A ); -T_20_39 (35 ,A ,B ,C ,D ,E ); -T_20_39 (36 ,E ,A ,B ,C ,D ); -T_20_39 (37 ,D ,E ,A ,B ,C ); -T_20_39 (38 ,C ,D ,E ,A ,B ); -T_20_39 (39 ,B ,C ,D ,E ,A ); +for (; i <40 ; ++i) +T_20_39 (i,A ,B ,C ,D ,E );/* Round 3 */ -T_40_59 (40 ,A ,B ,C ,D ,E ); -T_40_59 (41 ,E ,A ,B ,C ,D ); -T_40_59 (42 ,D ,E ,A ,B ,C ); -T_40_59 (43 ,C ,D ,E ,A ,B ); -T_40_59 (44 ,B ,C ,D ,E ,A ); -T_40_59 (45 ,A ,B ,C ,D ,E ); -T_40_59 (46 ,E ,A ,B ,C ,D ); -T_40_59 (47 ,D ,E ,A ,B ,C ); -T_40_59 (48 ,C ,D ,E ,A ,B ); -T_40_59 (49 ,B ,C ,D ,E ,A ); -T_40_59 (50 ,A ,B ,C ,D ,E ); -T_40_59 (51 ,E ,A ,B ,C ,D ); -T_40_59 (52 ,D ,E ,A ,B ,C ); -T_40_59 (53 ,C ,D ,E ,A ,B ); -T_40_59 (54 ,B ,C ,D ,E ,A ); -T_40_59 (55 ,A ,B ,C ,D ,E ); -T_40_59 (56 ,E ,A ,B ,C ,D ); -T_40_59 (57 ,D ,E ,A ,B ,C ); -T_40_59 (58 ,C ,D ,E ,A ,B ); -T_40_59 (59 ,B ,C ,D ,E ,A ); +for (; i <60 ; ++i) +T_40_59 (i,A ,B ,C ,D ,E );/* Round 4 */ -T_60_79 (60 ,A ,B ,C ,D ,E ); -T_60_79 (61 ,E ,A ,B ,C ,D ); -T_60_79 (62 ,D ,E ,A ,B ,C ); -T_60_79 (63 ,C ,D ,E ,A ,B ); -T_60_79 (64 ,B ,C ,D ,E ,A ); -T_60_79 (65 ,A ,B ,C ,D ,E ); -T_60_79 (66 ,E ,A ,B ,C ,D ); -T_60_79 (67 ,D ,E ,A ,B ,C ); -T_60_79 (68 ,C ,D ,E ,A ,B ); -T_60_79 (69 ,B ,C ,D ,E ,A ); -T_60_79 (70 ,A ,B ,C ,D ,E ); -T_60_79 (71 ,E ,A ,B ,C ,D ); -T_60_79 (72 ,D ,E ,A ,B ,C ); -T_60_79 (73 ,C ,D ,E ,A ,B ); -T_60_79 (74 ,B ,C ,D ,E ,A ); -T_60_79 (75 ,A ,B ,C ,D ,E ); -T_60_79 (76 ,E ,A ,B ,C ,D ); -T_60_79 (77 ,D ,E ,A ,B ,C ); -T_60_79 (78 ,C ,D ,E ,A ,B ); -T_60_79 (79 ,B ,C ,D ,E ,A ); +for (; i <80 ; ++i) +T_60_79 (i,A ,B ,C ,D ,E ); digest[0 ] +=A ; digest[1 ] +=B ;
Greg Kroah-Hartman
maintainerPATCH 25/234 random: remove the blocking pool
In reply to Greg Kroah-HartmanFrom: Andy Lutomirski <[email protected]>
commit 90ea1c6436d26e62496616fb5891e00819ff4849 upstream.
There is no longer any interface to read data from the blocking pool, so remove it.
This enables quite a bit of code deletion, much of which will be done in subsequent patches.
--- drivers/char/random.c | 106 -------------------------------------------------- 1 file changed, 106 deletions(-)
drivers/char/random.c +0 -106
@@ -470,7 +470,6 @@ static const struct poolinfo {/* * Static global variables */ -static DECLARE_WAIT_QUEUE_HEAD (random_read_wait);static DECLARE_WAIT_QUEUE_HEAD (random_write_wait);static struct fasync_struct *fasync;
@@ -532,7 +531,6 @@ struct entropy_store {__u32 *pool;const char *name;struct entropy_store *pull; -struct work_struct push_work;/* read-write data: */ unsigned long last_pulled;
@@ -551,9 +549,7 @@ static ssize_t _extract_entropy(struct e size_t nbytes, int fips);static void crng_reseed (struct crng_state *crng,struct entropy_store *r); -static void push_to_pool (struct work_struct *work); static __u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy; -static __u32 blocking_pool_data[OUTPUT_POOL_WORDS] __latent_entropy;static struct entropy_store input_pool = { .poolinfo = &poolinfo_table[0 ],
@@ -562,16 +558,6 @@ static struct entropy_store input_pool = .pool = input_pool_data }; -static struct entropy_store blocking_pool = { - .poolinfo = &poolinfo_table[1 ], - .name ="blocking" , - .pull = &input_pool, - .lock =__SPIN_LOCK_UNLOCKED (blocking_pool.lock), - .pool = blocking_pool_data, - .push_work =__WORK_INITIALIZER (blocking_pool.push_work, - push_to_pool), -}; -static __u32 const twist_table[8 ] = {0x00000000 ,0x3b6e20c8 ,0x76dc4190 ,0x4db26158 ,0xedb88320 ,0xd6d6a3e8 ,0x9b64c2b0 ,0xa00ae278 };
@@ -767,15 +753,11 @@ retry: entropy_count =0 ; }else if (entropy_count > pool_size) entropy_count = pool_size; -if ((r == &blocking_pool) && !r->initialized && - (entropy_count >>ENTROPY_SHIFT ) >128 ) - has_initialized =1 ;if (cmpxchg (&r->entropy_count, orig, entropy_count) != orig) goto retry;if (has_initialized) { r->initialized =1 ; -wake_up_interruptible (&random_read_wait);kill_fasync (&fasync,SIGIO ,POLL_IN ); }
@@ -784,7 +766,6 @@ retry:if (r == &input_pool) {int entropy_bits = entropy_count >>ENTROPY_SHIFT ; -struct entropy_store *other = &blocking_pool;if (crng_init <2 ) {if (entropy_bits <128 )
@@ -792,27 +773,6 @@ retry:crng_reseed (&primary_crng, r); entropy_bits = r->entropy_count >>ENTROPY_SHIFT ; } - -/* initialize the blocking pool if necessary */ -if (entropy_bits >= random_read_wakeup_bits && - !other->initialized) { -schedule_work (&other->push_work); -return ; - } - -/* should we wake readers? */ -if (entropy_bits >= random_read_wakeup_bits && -wq_has_sleeper (&random_read_wait)) { -wake_up_interruptible (&random_read_wait); - } -/* If the input pool is getting full, and the blocking -* pool has room, send some entropy to the blocking -* pool. -*/ -if (!work_pending (&other->push_work) && - (ENTROPY_BITS (r) >6 * r->poolinfo->poolbytes) && - (ENTROPY_BITS (other) <=6 * other->poolinfo->poolbytes)) -schedule_work (&other->push_work); } }
@@ -1442,22 +1402,6 @@ static void _xfer_secondary_pool(struct }/* -* Used as a workqueue function so that when the input pool is getting -* full, we can "spill over" some entropy to the output pools. That -* way the output pools can store some of the excess entropy instead -* of letting it go to waste. -*/ -static void push_to_pool (struct work_struct *work) -{ - struct entropy_store *r = container_of(work, struct entropy_store, - push_work); -BUG_ON (!r); -_xfer_secondary_pool (r, random_read_wakeup_bits/8 ); -trace_push_to_pool (r->name, r->entropy_count >>ENTROPY_SHIFT , - r->pull->entropy_count >>ENTROPY_SHIFT ); -} - -/* * This function decides how many bytes to actually take from the * given pool, and also debits the entropy count accordingly. */
@@ -1635,54 +1579,6 @@ static ssize_t extract_entropy(struct enreturn _extract_entropy (r, buf, nbytes, fips_enabled); } -/* -* This function extracts randomness from the "entropy pool", and -* returns it in a userspace buffer. -*/ -static ssize_t extract_entropy_user (struct entropy_store *r,void __user *buf, -size_t nbytes) -{ -ssize_t ret =0 , i; -__u8 tmp[EXTRACT_SIZE ]; -int large_request = (nbytes >256 ); - -trace_extract_entropy_user (r->name, nbytes,ENTROPY_BITS (r), _RET_IP_); -if (!r->initialized && r->pull) { -xfer_secondary_pool (r,ENTROPY_BITS (r->pull)/8 ); -if (!r->initialized) -return 0 ; - } -xfer_secondary_pool (r, nbytes); - nbytes =account (r, nbytes,0 ,0 ); - -while (nbytes) { -if (large_request &&need_resched ()) { -if (signal_pending (current)) { -if (ret ==0 ) - ret = -ERESTARTSYS ; -break ; - } -schedule (); - } - -extract_buf (r, tmp); - i =min_t (int, nbytes,EXTRACT_SIZE ); -if (copy_to_user (buf, tmp, i)) { - ret = -EFAULT ; -break ; - } - - nbytes -= i; - buf += i; - ret += i; - } - -/* Wipe data just returned from memory */ -memzero_explicit (tmp,sizeof (tmp)); - -return ret; -} -#define warn_unseeded_randomness (previous) \ _warn_unseeded_randomness(__func__, (void *) _RET_IP_, (previous))
@@ -1973,7 +1869,6 @@ static void __init init_std_data(structint __init rand_initialize (void ) {init_std_data (&input_pool); -init_std_data (&blocking_pool);if (crng_need_final_init)crng_finalize_init (&primary_crng);crng_initialize (&primary_crng);
@@ -2144,7 +2039,6 @@ static long random_ioctl(struct file *f,if (!capable (CAP_SYS_ADMIN ))return -EPERM ; input_pool.entropy_count =0 ; - blocking_pool.entropy_count =0 ;return 0 ;case RNDRESEEDCRNG :if (!capable (CAP_SYS_ADMIN ))
Greg Kroah-Hartman
maintainerPATCH 49/234 random: remove unused irq_flags argument from add_interrupt_randomness()
In reply to Greg Kroah-HartmanFrom: Sebastian Andrzej Siewior <[email protected]>
commit 703f7066f40599c290babdb79dd61319264987e9 upstream.
Since commit
ee3e00e9e7101 ("random: use registers from interrupted code for CPU's w/o a cycle counter")
the irq_flags argument is no longer used.
Remove unused irq_flags.
--- drivers/char/random.c | 4 ++-- drivers/hv/hv.c | 2 +- drivers/hv/vmbus_drv.c | 2 +- include/linux/random.h | 2 +- kernel/irq/handle.c | 2 +- 5 files changed, 6 insertions(+), 6 deletions(-)
drivers/char/random.c +2 -2
@@ -200,7 +200,7 @@* void add_device_randomness(const void *buf, unsigned int size); * void add_input_randomness(unsigned int type, unsigned int code, * unsigned int value); -* void add_interrupt_randomness(int irq, int irq_flags); +* void add_interrupt_randomness(int irq); * void add_disk_randomness(struct gendisk *disk); * void add_hwgenerator_randomness(const char *buffer, size_t count, * size_t entropy);
@@ -1272,7 +1272,7 @@ static __u32 get_reg(struct fast_pool *freturn *ptr; } -void add_interrupt_randomness (int irq,int irq_flags) +void add_interrupt_randomness (int irq) {struct entropy_store *r;struct fast_pool *fast_pool =this_cpu_ptr (&irq_randomness);
drivers/hv/hv.c +1 -1
@@ -115,7 +115,7 @@ static void hv_stimer0_isr(void) hv_cpu =this_cpu_ptr (hv_context.cpu_context); hv_cpu->clk_evt->event_handler (hv_cpu->clk_evt); -add_interrupt_randomness (stimer0_vector,0 ); +add_interrupt_randomness (stimer0_vector); } static int hv_ce_set_next_event(unsigned long delta,
drivers/hv/vmbus_drv.c +1 -1
@@ -1146,7 +1146,7 @@ static void vmbus_isr(void)tasklet_schedule (&hv_cpu->msg_dpc); } -add_interrupt_randomness (HYPERVISOR_CALLBACK_VECTOR ,0 ); +add_interrupt_randomness (HYPERVISOR_CALLBACK_VECTOR ); }/*
include/linux/random.h +1 -1
@@ -35,7 +35,7 @@ static inline void add_latent_entropy(vo extern void add_input_randomness(unsigned int type, unsigned int code, unsigned int value) __latent_entropy; -extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy; +extern void add_interrupt_randomness(int irq) __latent_entropy;extern void get_random_bytes (void *buf,int nbytes);extern int wait_for_random_bytes (void );
kernel/irq/handle.c +1 -1
@@ -188,7 +188,7 @@ irqreturn_t handle_irq_event_percpu(stru retval =__handle_irq_event_percpu (desc, &flags); -add_interrupt_randomness (desc->irq_data.irq, flags); +add_interrupt_randomness (desc->irq_data.irq);if (!noirqdebug)note_interrupt (desc, retval);
Greg Kroah-Hartman
maintainerPATCH 5/234 random: only read from /dev/random after its pool has received 128 bits
In reply to Greg Kroah-HartmanFrom: Theodore Ts'o <[email protected]>
commit eb9d1bf079bb438d1a066d72337092935fc770f6 upstream.
Immediately after boot, we allow reads from /dev/random before its entropy pool has been fully initialized. Fix this so that we don't allow this until the blocking pool has received 128 bits.
We do this by repurposing the initialized flag in the entropy pool struct, and use the initialized flag in the blocking pool to indicate whether it is safe to pull from the blocking pool.
To do this, we needed to rework when we decide to push entropy from the input pool to the blocking pool, since the initialized flag for the input pool was used for this purpose. To simplify things, we no longer use the initialized flag for that purpose, nor do we use the entropy_total field any more.
--- drivers/char/random.c | 44 +++++++++++++++++++++--------------------- include/trace/events/random.h | 13 ++++-------- 2 files changed, 27 insertions(+), 30 deletions(-)
drivers/char/random.c +22 -22
@@ -471,7 +471,6 @@ struct entropy_store {unsigned short add_ptr;unsigned short input_rotate;int entropy_count; -int entropy_total; unsigned int initialized:1; unsigned int last_data_init:1;__u8 last_data[EXTRACT_SIZE ];
@@ -644,7 +643,7 @@ static void process_random_ready_list(vo*/ static void credit_entropy_bits (struct entropy_store *r,int nbits) { -int entropy_count, orig; +int entropy_count, orig, has_initialized =0 ;const int pool_size = r->poolinfo->poolfracbits;int nfrac = nbits <<ENTROPY_SHIFT ;
@@ -699,23 +698,25 @@ retry: entropy_count =0 ; }else if (entropy_count > pool_size) entropy_count = pool_size; +if ((r == &blocking_pool) && !r->initialized && + (entropy_count >>ENTROPY_SHIFT ) >128 ) + has_initialized =1 ;if (cmpxchg (&r->entropy_count, orig, entropy_count) != orig) goto retry; - r->entropy_total += nbits; -if (!r->initialized && r->entropy_total >128 ) { +if (has_initialized) r->initialized =1 ; - r->entropy_total =0 ; - }trace_credit_entropy_bits (r->name, nbits, - entropy_count >>ENTROPY_SHIFT , - r->entropy_total, _RET_IP_); + entropy_count >>ENTROPY_SHIFT , _RET_IP_);if (r == &input_pool) {int entropy_bits = entropy_count >>ENTROPY_SHIFT ; +struct entropy_store *other = &blocking_pool; -if (crng_init <2 && entropy_bits >=128 ) { +if (crng_init <2 ) { +if (entropy_bits <128 ) +return ;crng_reseed (&primary_crng, r); entropy_bits = r->entropy_count >>ENTROPY_SHIFT ; }
@@ -726,20 +727,14 @@ retry:wake_up_interruptible (&random_read_wait);kill_fasync (&fasync,SIGIO ,POLL_IN ); } -/* If the input pool is getting full, send some -* entropy to the blocking pool until it is 75% full. +/* If the input pool is getting full, and the blocking +* pool has room, send some entropy to the blocking +* pool. */ -if (entropy_bits > random_write_wakeup_bits && - r->initialized && - r->entropy_total >=2 *random_read_wakeup_bits) { -struct entropy_store *other = &blocking_pool; - -if (other->entropy_count <= -3 * other->poolinfo->poolfracbits /4 ) { -schedule_work (&other->push_work); - r->entropy_total =0 ; - } - } +if (!work_pending (&other->push_work) && + (ENTROPY_BITS (r) >6 * r->poolinfo->poolbytes) && + (ENTROPY_BITS (other) <=6 * other->poolinfo->poolbytes)) +schedule_work (&other->push_work); } }
@@ -1562,6 +1557,11 @@ static ssize_t extract_entropy_user(struint large_request = (nbytes >256 );trace_extract_entropy_user (r->name, nbytes,ENTROPY_BITS (r), _RET_IP_); +if (!r->initialized && r->pull) { +xfer_secondary_pool (r,ENTROPY_BITS (r->pull)/8 ); +if (!r->initialized) +return 0 ; + }xfer_secondary_pool (r, nbytes); nbytes =account (r, nbytes,0 ,0 );
include/trace/events/random.h +5 -8
@@ -62,15 +62,14 @@ DEFINE_EVENT(random__mix_pool_bytes, mix TRACE_EVENT(credit_entropy_bits, TP_PROTO(const char *pool_name, int bits, int entropy_count, - int entropy_total, unsigned long IP), + unsigned long IP), - TP_ARGS(pool_name, bits, entropy_count, entropy_total, IP), + TP_ARGS(pool_name, bits, entropy_count, IP), TP_STRUCT__entry( __field( const char *, pool_name ) __field( int, bits ) __field( int, entropy_count ) - __field( int, entropy_total ) __field(unsigned long, IP ) ),
@@ -78,14 +77,12 @@ TRACE_EVENT(credit_entropy_bits, __entry->pool_name = pool_name; __entry->bits = bits; __entry->entropy_count = entropy_count; - __entry->entropy_total = entropy_total; __entry->IP =IP ; ), -TP_printk ("%s pool: bits %d entropy_count %d entropy_total %d " -"caller %pS" , __entry->pool_name, __entry->bits, - __entry->entropy_count, __entry->entropy_total, - (void *)__entry->IP) +TP_printk ("%s pool: bits %d entropy_count %d caller %pS" , + __entry->pool_name, __entry->bits, + __entry->entropy_count, (void *)__entry->IP) ); TRACE_EVENT(push_to_pool,
Greg Kroah-Hartman
maintainerPATCH 24/234 random: fix crash on multiple early calls to add_bootloader_randomness()
In reply to Greg Kroah-HartmanFrom: Dominik Brodowski <[email protected]>
commit f7e67b8e803185d0aabe7f29d25a35c8be724a78 upstream.
Currently, if CONFIG_RANDOM_TRUST_BOOTLOADER is enabled, multiple calls to add_bootloader_randomness() are broken and can cause a NULL pointer dereference, as noted by Ivan T. Ivanov. This is not only a hypothetical problem, as qemu on arm64 may provide bootloader entropy via EFI and via devicetree.
On the first call to add_hwgenerator_randomness(), crng_fast_load() is executed, and if the seed is long enough, crng_init will be set to 1. On subsequent calls to add_bootloader_randomness() and then to add_hwgenerator_randomness(), crng_fast_load() will be skipped. Instead, wait_event_interruptible() and then credit_entropy_bits() will be called. If the entropy count for that second seed is large enough, that proceeds to crng_reseed().
However, both wait_event_interruptible() and crng_reseed() depends (at least in numa_crng_init()) on workqueues. Therefore, test whether system_wq is already initialized, which is a sufficient indicator that workqueue_init_early() has progressed far enough.
If we wind up hitting the !system_wq case, we later want to do what would have been done there when wqs are up, so set a flag, and do that work later from the rand_initialize() call.
--- drivers/char/random.c | 59 ++++++++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 21 deletions(-)
drivers/char/random.c +38 -21
@@ -496,6 +496,7 @@ static struct crng_state primary_crng =* its value (from 0->1->2). */ static int crng_init =0 ; +static bool crng_need_final_init = false;#define crng_ready () (likely(crng_init > 1))static int crng_init_cnt =0 ;static unsigned long crng_global_init_time =0 ;
@@ -888,6 +889,38 @@ static void crng_initialize(struct crng_ crng->init_time = jiffies -CRNG_RESEED_INTERVAL -1 ; } +static void crng_finalize_init (struct crng_state *crng) +{ +if (crng != &primary_crng || crng_init >=2 ) +return ; +if (!system_wq) { +/* We can't call numa_crng_init until we have workqueues, +* so mark this for processing later. */ + crng_need_final_init = true; +return ; + } + +invalidate_batched_entropy (); +numa_crng_init (); + crng_init =2 ; +process_random_ready_list (); +wake_up_interruptible (&crng_init_wait); +kill_fasync (&fasync,SIGIO ,POLL_IN ); +pr_notice ("crng init done\n" ); +if (unseeded_warning.missed) { +pr_notice ("random: %d get_random_xx warning(s) missed " +"due to ratelimiting\n" , + unseeded_warning.missed); + unseeded_warning.missed =0 ; + } +if (urandom_warning.missed) { +pr_notice ("random: %d urandom warning(s) missed " +"due to ratelimiting\n" , + urandom_warning.missed); + urandom_warning.missed =0 ; + } +} +#ifdef CONFIG_NUMA static void do_numa_crng_init (struct work_struct *work) {
@@ -1042,26 +1075,7 @@ static void crng_reseed(struct crng_statmemzero_explicit (&buf,sizeof (buf));WRITE_ONCE (crng->init_time, jiffies);spin_unlock_irqrestore (&crng->lock, flags); -if (crng == &primary_crng && crng_init <2 ) { -invalidate_batched_entropy (); -numa_crng_init (); - crng_init =2 ; -process_random_ready_list (); -wake_up_interruptible (&crng_init_wait); -pr_notice ("random: crng init done\n" ); -if (unseeded_warning.missed) { -pr_notice ("random: %d get_random_xx warning(s) missed " -"due to ratelimiting\n" , - unseeded_warning.missed); - unseeded_warning.missed =0 ; - } -if (urandom_warning.missed) { -pr_notice ("random: %d urandom warning(s) missed " -"due to ratelimiting\n" , - urandom_warning.missed); - urandom_warning.missed =0 ; - } - } +crng_finalize_init (crng); } static void _extract_crng(struct crng_state *crng,
@@ -1960,6 +1974,8 @@ int __init rand_initialize(void) {init_std_data (&input_pool);init_std_data (&blocking_pool); +if (crng_need_final_init) +crng_finalize_init (&primary_crng);crng_initialize (&primary_crng); crng_global_init_time = jiffies;if (ratelimit_disable) {
@@ -2478,7 +2494,8 @@ void add_hwgenerator_randomness(const ch* We'll be woken up again once below random_write_wakeup_thresh, * or when the calling thread is about to terminate. */ -wait_event_interruptible (random_write_wait,kthread_should_stop () || +wait_event_interruptible (random_write_wait, + !system_wq ||kthread_should_stop () ||ENTROPY_BITS (&input_pool) <= random_write_wakeup_bits);mix_pool_bytes (poolp, buffer, count);credit_entropy_bits (poolp, entropy);
Greg Kroah-Hartman
maintainerPATCH 48/234 random: document add_hwgenerator_randomness() with other input functions
In reply to Greg Kroah-HartmanFrom: Mark Brown <[email protected]>
commit 2b6c6e3d9ce3aa0e547ac25d60e06fe035cd9f79 upstream.
The section at the top of random.c which documents the input functions available does not document add_hwgenerator_randomness() which might lead a reader to overlook it. Add a brief note about it.
[Jason: reorganize position of function in doc comment and also document
add_bootloader_randomness() while we're at it.]
--- drivers/char/random.c | 11 +++++++++++ 1 file changed, 11 insertions(+)
drivers/char/random.c +11 -0
@@ -202,6 +202,9 @@* unsigned int value); * void add_interrupt_randomness(int irq, int irq_flags); * void add_disk_randomness(struct gendisk *disk); +* void add_hwgenerator_randomness(const char *buffer, size_t count, +* size_t entropy); +* void add_bootloader_randomness(const void *buf, unsigned int size); * * add_device_randomness() is for adding data to the random pool that * is likely to differ between two devices (or possibly even per boot).
@@ -228,6 +231,14 @@* particular randomness source. They do this by keeping track of the * first and second order deltas of the event timings. * +* add_hwgenerator_randomness() is for true hardware RNGs, and will credit +* entropy as specified by the caller. If the entropy pool is full it will +* block until more entropy is needed. +* +* add_bootloader_randomness() is the same as add_hwgenerator_randomness() or +* add_device_randomness(), depending on whether or not the configuration +* option CONFIG_RANDOM_TRUST_BOOTLOADER is set. +* * Ensuring unpredictability at system startup * ============================================ *
Greg Kroah-Hartman
maintainerPATCH 30/234 random: Add and use pr_fmt()
In reply to Greg Kroah-HartmanFrom: Yangtao Li <[email protected]>
commit 12cd53aff5ea0359b1dac91fcd9ddc7b9e646588 upstream.
Prefix all printk/pr_<level> messages with "random: " to make the logging a bit more consistent.
Miscellanea:
o Convert a printks to pr_notice
o Whitespace to align to open parentheses
o Remove embedded "random: " from pr_* as pr_fmt adds it
--- drivers/char/random.c | 21 ++++++++++----------- 1 file changed, 10 insertions(+), 11 deletions(-)
drivers/char/random.c +10 -11
@@ -307,6 +307,8 @@* Eastlake, Steve Crocker, and Jeff Schiller. */ +#define pr_fmt (fmt) KBUILD_MODNAME ": " fmt +#include <linux/utsname.h> #include <linux/module.h> #include <linux/kernel.h>
@@ -739,7 +741,7 @@ retry: }if (WARN_ON (entropy_count <0 )) { -pr_warn ("random: negative entropy/overflow: pool %s count %d\n" , +pr_warn ("negative entropy/overflow: pool %s count %d\n" , r->name, entropy_count); entropy_count =0 ; }else if (entropy_count > pool_size)
@@ -835,7 +837,7 @@ static void crng_initialize(struct crng_invalidate_batched_entropy ();numa_crng_init (); crng_init =2 ; -pr_notice ("random: crng done (trusting CPU's manufacturer)\n" ); +pr_notice ("crng done (trusting CPU's manufacturer)\n" ); } crng->init_time = jiffies -CRNG_RESEED_INTERVAL -1 ; }
@@ -859,14 +861,12 @@ static void crng_finalize_init(struct crkill_fasync (&fasync,SIGIO ,POLL_IN );pr_notice ("crng init done\n" );if (unseeded_warning.missed) { -pr_notice ("random: %d get_random_xx warning(s) missed " -"due to ratelimiting\n" , +pr_notice ("%d get_random_xx warning(s) missed due to ratelimiting\n" , unseeded_warning.missed); unseeded_warning.missed =0 ; }if (urandom_warning.missed) { -pr_notice ("random: %d urandom warning(s) missed " -"due to ratelimiting\n" , +pr_notice ("%d urandom warning(s) missed due to ratelimiting\n" , urandom_warning.missed); urandom_warning.missed =0 ; }
@@ -947,7 +947,7 @@ static int crng_fast_load(const char *cpif (crng_init_cnt >=CRNG_INIT_CNT_THRESH ) {invalidate_batched_entropy (); crng_init =1 ; -pr_notice ("random: fast init done\n" ); +pr_notice ("fast init done\n" ); }return 1 ; }
@@ -1383,7 +1383,7 @@ retry: ibytes =0 ;if (WARN_ON (entropy_count <0 )) { -pr_warn ("random: negative entropy count: pool %s count %d\n" , +pr_warn ("negative entropy count: pool %s count %d\n" , r->name, entropy_count); entropy_count =0 ; }
@@ -1869,9 +1869,8 @@ urandom_read(struct file *file, char __uif (!crng_ready () && maxwarn >0 ) { maxwarn--;if (__ratelimit (&urandom_warning)) -printk (KERN_NOTICE "random: %s: uninitialized " -"urandom read (%zd bytes read)\n" , - current->comm, nbytes); +pr_notice ("%s: uninitialized urandom read (%zd bytes read)\n" , + current->comm, nbytes);spin_lock_irqsave (&primary_crng.lock, flags); crng_init_cnt =0 ;spin_unlock_irqrestore (&primary_crng.lock, flags);
Greg Kroah-Hartman
maintainerPATCH 47/234 crypto: blake2s - adjust include guard naming
In reply to Greg Kroah-HartmanFrom: Eric Biggers <[email protected]>
commit 8786841bc2020f7f2513a6c74e64912f07b9c0dc upstream.
Use the full path in the include guards for the BLAKE2s headers to avoid ambiguity and to match the convention for most files in include/crypto/.
--- include/crypto/blake2s.h | 6 +++--- include/crypto/internal/blake2s.h | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-)
include/crypto/blake2s.h +3 -3
@@ -3,8 +3,8 @@* Copyright (C) 2015-2019 Jason A. Donenfeld <[email protected]>. All Rights Reserved. */ -#ifndef BLAKE2S_H -#define BLAKE2S_H +#ifndef _CRYPTO_BLAKE2S_H +#define _CRYPTO_BLAKE2S_H#include <linux/bug.h> #include <linux/types.h>
@@ -99,4 +99,4 @@ static inline void blake2s(u8 *out, cons blake2s_final(&state, out); } -#endif /* BLAKE2S_H */ +#endif /* _CRYPTO_BLAKE2S_H */
include/crypto/internal/blake2s.h +3 -3
@@ -1,7 +1,7 @@/* SPDX-License-Identifier: GPL-2.0 OR MIT */ -#ifndef BLAKE2S_INTERNAL_H -#define BLAKE2S_INTERNAL_H +#ifndef _CRYPTO_INTERNAL_BLAKE2S_H +#define _CRYPTO_INTERNAL_BLAKE2S_H#include <crypto/blake2s.h>
@@ -16,4 +16,4 @@ static inline void blake2s_set_lastblock state->f[0] = -1; } -#endif /* BLAKE2S_INTERNAL_H */ +#endif /* _CRYPTO_INTERNAL_BLAKE2S_H */
Greg Kroah-Hartman
maintainerPATCH 19/234 random: Add a urandom_read_nowait() for random APIs that dont warn
In reply to Greg Kroah-HartmanFrom: Andy Lutomirski <[email protected]>
commit c6f1deb158789abba02a7eba600747843eeb3a57 upstream.
/dev/random and getrandom() never warn. Split the meat of urandom_read() into urandom_read_nowarn() and leave the warning code in urandom_read().
This has no effect on kernel behavior, but it makes subsequent patches more straightforward. It also makes the fact that getrandom() never warns more obvious.
--- drivers/char/random.c | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-)
drivers/char/random.c +15 -6
@@ -2025,11 +2025,22 @@ random_read(struct file *file, char __us }static ssize_t +urandom_read_nowarn (struct file *file,char __user *buf,size_t nbytes, +loff_t *ppos) +{ +int ret; + + nbytes =min_t (size_t, nbytes,INT_MAX >> (ENTROPY_SHIFT +3 )); + ret =extract_crng_user (buf, nbytes); +trace_urandom_read (8 * nbytes,0 ,ENTROPY_BITS (&input_pool)); +return ret; +} + +static ssize_t urandom_read (struct file *file,char __user *buf,size_t nbytes,loff_t *ppos) {unsigned long flags;static int maxwarn =10 ; -int ret;if (!crng_ready () && maxwarn >0 ) { maxwarn--;
@@ -2041,10 +2052,8 @@ urandom_read(struct file *file, char __u crng_init_cnt =0 ;spin_unlock_irqrestore (&primary_crng.lock, flags); } - nbytes =min_t (size_t, nbytes,INT_MAX >> (ENTROPY_SHIFT +3 )); - ret =extract_crng_user (buf, nbytes); -trace_urandom_read (8 * nbytes,0 ,ENTROPY_BITS (&input_pool)); -return ret; + +return urandom_read_nowarn (file, buf, nbytes, ppos); } static __poll_t
@@ -2204,7 +2213,7 @@ SYSCALL_DEFINE3(getrandom, char __user *if (unlikely (ret))return ret; } -return urandom_read (NULL , buf, count,NULL ); +return urandom_read_nowarn (NULL , buf, count,NULL ); }/********************************************************************
Greg Kroah-Hartman
maintainerPATCH 8/234 latent_entropy: avoid build error when plugin cflags are not set
In reply to Greg Kroah-HartmanFrom: Vasily Gorbik <[email protected]>
commit 7e756f423af808b6571fed3144747db2ef7fa1c5 upstream.
Some architectures set up CFLAGS for linux decompressor phase from scratch and do not include GCC_PLUGINS_CFLAGS. Since "latent_entropy" variable declaration is generated by the plugin code itself including linux/random.h in decompressor code then would cause a build error. E.g. on s390:
In file included from ./include/linux/net.h:22,
from ./include/linux/skbuff.h:29,
from ./include/linux/if_ether.h:23,
from ./arch/s390/include/asm/diag.h:12,
from arch/s390/boot/startup.c:8:
./include/linux/random.h: In function 'add_latent_entropy':
./include/linux/random.h:26:39: error: 'latent_entropy' undeclared
(first use in this function); did you mean 'add_latent_entropy'?
26 | add_device_randomness((const void *)&latent_entropy,
| ^~~~~~~~~~~~~~
| add_latent_entropy./include/linux/random.h:26:39: note: each undeclared identifier is reported only once for each function it appears in
The build error is triggered by commit a80313ff91ab ("s390/kernel:
introduce .dma sections") which made it into 5.2 merge window.
To address that avoid using CONFIG_GCC_PLUGIN_LATENT_ENTROPY in favour of LATENT_ENTROPY_PLUGIN definition which is defined as a part of gcc plugins cflags and hence reflect more accurately when gcc plugin is active. Besides that it is also used for similar purpose in linux/compiler-gcc.h for latent_entropy attribute definition.
--- include/linux/random.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
include/linux/random.h +1 -1
@@ -20,7 +20,7 @@ struct random_ready_callback {extern void add_device_randomness (const void *,unsigned int ); -#if defined(CONFIG_GCC_PLUGIN_LATENT_ENTROPY ) && !defined(__CHECKER__) +#if defined(LATENT_ENTROPY_PLUGIN ) && !defined(__CHECKER__)static inline void add_latent_entropy (void ) { add_device_randomness((const void *)&latent_entropy,
Greg Kroah-Hartman
maintainerPATCH 35/234 x86: Remove arch_has_random, arch_has_random_seed
In reply to Greg Kroah-HartmanFrom: Richard Henderson <[email protected]>
commit 5f2ed7f5b99b54389b74e53309677831ac9cb9d7 upstream.
Use the expansion of these macros directly in arch_get_random_*.
These symbols are currently part of the generic archrandom.h interface, but are currently unused and can be removed.
--- arch/x86/include/asm/archrandom.h | 12 ++++-------- 1 file changed, 4 insertions(+), 8 deletions(-)
arch/x86/include/asm/archrandom.h +4 -8
@@ -86,10 +86,6 @@ static inline bool rdseed_int(unsigned ireturn ok; } -/* Conditional execution based on CPU type */ -#define arch_has_random () static_cpu_has(X86_FEATURE_RDRAND) -#define arch_has_random_seed () static_cpu_has(X86_FEATURE_RDSEED) -/* * These are the generic interfaces; they must not be declared if the * stubs in <linux/random.h> are to be invoked,
@@ -99,22 +95,22 @@ static inline bool rdseed_int(unsigned istatic inline bool arch_get_random_long (unsigned long *v) { -return arch_has_random () ?rdrand_long (v) : false; +return static_cpu_has (X86_FEATURE_RDRAND ) ?rdrand_long (v) : false; }static inline bool arch_get_random_int (unsigned int *v) { -return arch_has_random () ?rdrand_int (v) : false; +return static_cpu_has (X86_FEATURE_RDRAND ) ?rdrand_int (v) : false; }static inline bool arch_get_random_seed_long (unsigned long *v) { -return arch_has_random_seed () ?rdseed_long (v) : false; +return static_cpu_has (X86_FEATURE_RDSEED ) ?rdseed_long (v) : false; }static inline bool arch_get_random_seed_int (unsigned int *v) { -return arch_has_random_seed () ?rdseed_int (v) : false; +return static_cpu_has (X86_FEATURE_RDSEED ) ?rdseed_int (v) : false; }extern void x86_init_rdrand (struct cpuinfo_x86 *c);
Greg Kroah-Hartman
maintainerPATCH 7/234 random: document get_random_int() family
In reply to Greg Kroah-HartmanFrom: George Spelvin <[email protected]>
commit 92e507d216139b356a375afbda2824e85235e748 upstream.
Explain what these functions are for and when they offer an advantage over get_random_bytes().
(We still need documentation on rng_is_initialized(), the random_ready_callback system, and early boot in general.)
--- drivers/char/random.c | 83 +++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 76 insertions(+), 7 deletions(-)
drivers/char/random.c +76 -7
@@ -101,15 +101,13 @@* Exported interfaces ---- output * =============================== * -* There are three exported interfaces; the first is one designed to -* be used from within the kernel: +* There are four exported interfaces; two for use within the kernel, +* and two or use from userspace. * -* void get_random_bytes(void *buf, int nbytes); -* -* This interface will return the requested number of random bytes, -* and place it in the requested buffer. +* Exported interfaces ---- userspace output +* ----------------------------------------- * -* The two other interfaces are two character devices /dev/random and +* The userspace interfaces are two character devices /dev/random and * /dev/urandom. /dev/random is suitable for use when very high * quality randomness is desired (for example, for key generation or * one-time pads), as it will only return a maximum of the number of
@@ -122,6 +120,77 @@* this will result in random numbers that are merely cryptographically * strong. For many applications, however, this is acceptable. * +* Exported interfaces ---- kernel output +* -------------------------------------- +* +* The primary kernel interface is +* +* void get_random_bytes(void *buf, int nbytes); +* +* This interface will return the requested number of random bytes, +* and place it in the requested buffer. This is equivalent to a +* read from /dev/urandom. +* +* For less critical applications, there are the functions: +* +* u32 get_random_u32() +* u64 get_random_u64() +* unsigned int get_random_int() +* unsigned long get_random_long() +* +* These are produced by a cryptographic RNG seeded from get_random_bytes, +* and so do not deplete the entropy pool as much. These are recommended +* for most in-kernel operations *if the result is going to be stored in +* the kernel*. +* +* Specifically, the get_random_int() family do not attempt to do +* "anti-backtracking". If you capture the state of the kernel (e.g. +* by snapshotting the VM), you can figure out previous get_random_int() +* return values. But if the value is stored in the kernel anyway, +* this is not a problem. +* +* It *is* safe to expose get_random_int() output to attackers (e.g. as +* network cookies); given outputs 1..n, it's not feasible to predict +* outputs 0 or n+1. The only concern is an attacker who breaks into +* the kernel later; the get_random_int() engine is not reseeded as +* often as the get_random_bytes() one. +* +* get_random_bytes() is needed for keys that need to stay secret after +* they are erased from the kernel. For example, any key that will +* be wrapped and stored encrypted. And session encryption keys: we'd +* like to know that after the session is closed and the keys erased, +* the plaintext is unrecoverable to someone who recorded the ciphertext. +* +* But for network ports/cookies, stack canaries, PRNG seeds, address +* space layout randomization, session *authentication* keys, or other +* applications where the sensitive data is stored in the kernel in +* plaintext for as long as it's sensitive, the get_random_int() family +* is just fine. +* +* Consider ASLR. We want to keep the address space secret from an +* outside attacker while the process is running, but once the address +* space is torn down, it's of no use to an attacker any more. And it's +* stored in kernel data structures as long as it's alive, so worrying +* about an attacker's ability to extrapolate it from the get_random_int() +* CRNG is silly. +* +* Even some cryptographic keys are safe to generate with get_random_int(). +* In particular, keys for SipHash are generally fine. Here, knowledge +* of the key authorizes you to do something to a kernel object (inject +* packets to a network connection, or flood a hash table), and the +* key is stored with the object being protected. Once it goes away, +* we no longer care if anyone knows the key. +* +* prandom_u32() +* ------------- +* +* For even weaker applications, see the pseudorandom generator +* prandom_u32(), prandom_max(), and prandom_bytes(). If the random +* numbers aren't security-critical at all, these are *far* cheaper. +* Useful for self-tests, random error simulation, randomized backoffs, +* and any other application where you trust that nobody is trying to +* maliciously mess with you by guessing the "random" numbers. +* * Exported interfaces ---- input * ============================== *
Greg Kroah-Hartman
maintainerPATCH 2/234 drivers/char/random.c: constify poolinfo_table
In reply to Greg Kroah-HartmanFrom: Rasmus Villemoes <[email protected]>
commit 26e0854ab3310bbeef1ed404a2c87132fc91f8e1 upstream.
Never modified, might as well be put in .rodata.
--- drivers/char/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
@@ -359,7 +359,7 @@ static int random_write_wakeup_bits = 28* polynomial which improves the resulting TGFSR polynomial to be * irreducible, which we have made here. */ -static struct poolinfo { +static const struct poolinfo { int poolbitshift, poolwords, poolbytes, poolbits, poolfracbits; #define S(x) ilog2(x)+5, (x), (x)*4, (x)*32, (x) << (ENTROPY_SHIFT+5) int tap1, tap2, tap3, tap4, tap5;
Greg Kroah-Hartman
maintainerPATCH 29/234 random: convert to ENTROPY_BITS for better code readability
In reply to Greg Kroah-HartmanFrom: Yangtao Li <[email protected]>
commit 12faac30d157970fdbfa171bbeb1fb88350303b1 upstream.
--- drivers/char/random.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-)
drivers/char/random.c +2 -3
@@ -762,7 +762,7 @@ retry:if (entropy_bits <128 )return ;crng_reseed (&primary_crng, r); - entropy_bits = r->entropy_count >>ENTROPY_SHIFT ; + entropy_bits =ENTROPY_BITS (r); } } }
@@ -1397,8 +1397,7 @@ retry: goto retry;trace_debit_entropy (r->name,8 * ibytes); -if (ibytes && - (r->entropy_count >>ENTROPY_SHIFT ) < random_write_wakeup_bits) { +if (ibytes &&ENTROPY_BITS (r) < random_write_wakeup_bits) {wake_up_interruptible (&random_write_wait);kill_fasync (&fasync,SIGIO ,POLL_OUT ); }
Greg Kroah-Hartman
maintainerPATCH 50/234 random: use BLAKE2s instead of SHA1 in extraction
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 9f9eff85a008b095eafc5f4ecbaf5aca689271c1 upstream.
This commit addresses one of the lower hanging fruits of the RNG: its usage of SHA1.
BLAKE2s is generally faster, and certainly more secure, than SHA1, which has [1] been [2] really [3] very [4] broken [5]. Additionally, the current construction in the RNG doesn't use the full SHA1 function, as specified, and allows overwriting the IV with RDRAND output in an undocumented way, even in the case when RDRAND isn't set to "trusted", which means potential malicious IV choices. And its short length means that keeping only half of it secret when feeding back into the mixer gives us only 2^80 bits of forward secrecy. In other words, not only is the choice of hash function dated, but the use of it isn't really great either.
This commit aims to fix both of these issues while also keeping the general structure and semantics as close to the original as possible. Specifically:
a) Rather than overwriting the hash IV with RDRAND, we put it into BLAKE2's documented "salt" and "personal" fields, which were specifically created for this type of usage.
b) Since this function feeds the full hash result back into the entropy collector, we only return from it half the length of the hash, just as it was done before. This increases the construction's forward secrecy from 2^80 to a much more comfortable 2^128.
c) Rather than using the raw "sha1_transform" function alone, we instead use the full proper BLAKE2s function, with finalization.
This also has the advantage of supplying 16 bytes at a time rather than SHA1's 10 bytes, which, in addition to having a faster compression function to begin with, means faster extraction in general. On an Intel i7-11850H, this commit makes initial seeding around 131% faster.
BLAKE2s itself has the nice property of internally being based on the ChaCha permutation, which the RNG is already using for expansion, so there shouldn't be any issue with newness, funkiness, or surprising CPU behavior, since it's based on something already in use.
[1] https://eprint.iacr.org/2005/010.pdf
[2] https://www.iacr.org/archive/crypto2005/36210017/36210017.pdf
[3] https://eprint.iacr.org/2015/967.pdf
[4] https://shattered.io/static/shattered.pdf
[5] https://www.usenix.org/system/files/sec20-leurent.pdf
--- drivers/char/random.c | 70 +++++++++++++++++++++----------------------------- 1 file changed, 30 insertions(+), 40 deletions(-)
drivers/char/random.c +30 -40
@@ -1,8 +1,7 @@/* * random.c -- A strong random number generator * -* Copyright (C) 2017 Jason A. Donenfeld <[email protected]>. All -* Rights Reserved. +* Copyright (C) 2017-2022 Jason A. Donenfeld <[email protected]>. All Rights Reserved. * * Copyright Matt Mackall <[email protected]>, 2003, 2004, 2005 *
@@ -78,12 +77,12 @@* an *estimate* of how many bits of randomness have been stored into * the random number generator's internal state. * -* When random bytes are desired, they are obtained by taking the SHA -* hash of the contents of the "entropy pool". The SHA hash avoids +* When random bytes are desired, they are obtained by taking the BLAKE2s +* hash of the contents of the "entropy pool". The BLAKE2s hash avoids * exposing the internal state of the entropy pool. It is believed to * be computationally infeasible to derive any useful information -* about the input of SHA from its output. Even if it is possible to -* analyze SHA in some clever way, as long as the amount of data +* about the input of BLAKE2s from its output. Even if it is possible to +* analyze BLAKE2s in some clever way, as long as the amount of data * returned from the generator is less than the inherent entropy in * the pool, the output data is totally unpredictable. For this * reason, the routine decreases its internal estimate of how many
@@ -93,7 +92,7 @@* If this estimate goes to zero, the routine can still generate * random numbers; however, an attacker may (at least in theory) be * able to infer the future output of the generator from prior -* outputs. This requires successful cryptanalysis of SHA, which is +* outputs. This requires successful cryptanalysis of BLAKE2s, which is * not believed to be feasible, but there is a remote possibility. * Nonetheless, these numbers should be useful for the vast majority * of purposes.
@@ -348,6 +347,7 @@#include <linux/completion.h> #include <linux/uuid.h> #include <crypto/chacha20.h> +#include <crypto/blake2s.h> #include <asm/processor.h> #include <linux/uaccess.h>
@@ -367,10 +367,7 @@#define INPUT_POOL_WORDS (1 << (INPUT_POOL_SHIFT-5))#define OUTPUT_POOL_SHIFT 10#define OUTPUT_POOL_WORDS (1 << (OUTPUT_POOL_SHIFT-5)) -#define EXTRACT_SIZE 10 - - -#define LONGS (x) (((x) + sizeof(unsigned long) - 1)/sizeof(unsigned long)) +#define EXTRACT_SIZE (BLAKE2S_HASH_SIZE / 2)/* * To allow fractional bits to be tracked, the entropy_count field is
@@ -406,7 +403,7 @@ static int random_write_wakeup_bits = 28* Thanks to Colin Plumb for suggesting this. * * The mixing operation is much less sensitive than the output hash, -* where we use SHA-1. All that we want of mixing operation is that +* where we use BLAKE2s. All that we want of mixing operation is that * it be a good non-cryptographic hash; i.e. it not produce collisions * when fed "random" data of the sort we expect to see. As long as * the pool state differs for different inputs, we have preserved the
@@ -1398,56 +1395,49 @@ retry:*/ static void extract_buf (struct entropy_store *r,__u8 *out) { -int i; -union { -__u32 w[5 ]; -unsigned long l[LONGS (20 )]; - } hash; -__u32 workspace[SHA_WORKSPACE_WORDS ]; +struct blake2s_state state__aligned(__alignof__(unsigned long)) ; +u8 hash[BLAKE2S_HASH_SIZE ]; +unsigned long *salt;unsigned long flags; +blake2s_init (&state,sizeof (hash)); +/* * If we have an architectural hardware random number -* generator, use it for SHA's initial vector +* generator, use it for BLAKE2's salt & personal fields. */ -sha_init (hash.w); -for (i =0 ; i <LONGS (20 ); i++) { +for (salt = (unsigned long *)&state.h[4 ]; + salt < (unsigned long *)&state.h[8 ]; ++salt) {unsigned long v;if (!arch_get_random_long (&v))break ; - hash.l[i] = v; + *salt ^= v; } -/* Generate a hash across the pool, 16 words (512 bits) at a time */ +/* Generate a hash across the pool */ spin_lock_irqsave (&r->lock, flags); -for (i =0 ; i < r->poolinfo->poolwords; i +=16 ) -sha_transform (hash.w, (__u8 *)(r->pool + i), workspace); +blake2s_update (&state, (const u8 *)r->pool, + r->poolinfo->poolwords *sizeof (*r->pool)); +blake2s_final (&state, hash);/* final zeros out state */ /* * We mix the hash back into the pool to prevent backtracking * attacks (where the attacker knows the state of the pool * plus the current outputs, and attempts to find previous -* ouputs), unless the hash function can be inverted. By -* mixing at least a SHA1 worth of hash data back, we make +* outputs), unless the hash function can be inverted. By +* mixing at least a hash worth of hash data back, we make * brute-forcing the feedback as hard as brute-forcing the * hash. */ -__mix_pool_bytes (r, hash.w,sizeof (hash.w)); +__mix_pool_bytes (r, hash,sizeof (hash));spin_unlock_irqrestore (&r->lock, flags); -memzero_explicit (workspace,sizeof (workspace)); - -/* -* In case the hash function has some recognizable output -* pattern, we fold it in half. Thus, we always feed back -* twice as much data as we output. +/* Note that EXTRACT_SIZE is half of hash size here, because above +* we've dumped the full length back into mixer. By reducing the +* amount that we emit, we retain a level of forward secrecy. */ - hash.w[0 ] ^= hash.w[3 ]; - hash.w[1 ] ^= hash.w[4 ]; - hash.w[2 ] ^=rol32 (hash.w[2 ],16 ); - -memcpy (out, &hash,EXTRACT_SIZE ); -memzero_explicit (&hash,sizeof (hash)); +memcpy (out, hash,EXTRACT_SIZE ); +memzero_explicit (hash,sizeof (hash)); } static ssize_t _extract_entropy(struct entropy_store *r, void *buf,
Greg Kroah-Hartman
maintainerPATCH 51/234 random: do not sign extend bytes for rotation when mixing
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 0d9488ffbf2faddebc6bac055bfa6c93b94056a3 upstream.
By using char instead of unsigned char, certain platforms will sign extend the byte when w = rol32(*bytes++, input_rotate) is called, meaning that bit 7 is overrepresented when mixing. This isn't a real problem (unless the mixer itself is already broken) since it's still invertible, but it's not quite correct either. Fix this by using an explicit unsigned type.
--- drivers/char/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
@@ -549,7 +549,7 @@ static void _mix_pool_bytes(struct entrounsigned long i, tap1, tap2, tap3, tap4, tap5;int input_rotate;int wordmask = r->poolinfo->poolwords -1 ; -const char *bytes = in; +const unsigned char *bytes = in;__u32 w; tap1 = r->poolinfo->tap1;
Greg Kroah-Hartman
maintainerPATCH 52/234 random: do not re-init if crng_reseed completes before primary init
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 9c3ddde3f811aabbb83778a2a615bf141b4909ef upstream.
If the bootloader supplies sufficient material and crng_reseed() is called very early on, but not too early that wqs aren't available yet, then we might transition to crng_init==2 before rand_initialize()'s call to crng_initialize_primary() made. Then, when crng_initialize_primary() is called, if we're trusting the CPU's RDRAND instructions, we'll needlessly reinitialize the RNG and emit a message about it. This is mostly harmless, as numa_crng_init() will allocate and then free what it just allocated, and excessive calls to invalidate_batched_entropy() aren't so harmful. But it is funky and the extra message is confusing, so avoid the re-initialization all together by checking for crng_init < 2 in crng_initialize_primary(), just as we already do in crng_reseed().
--- drivers/char/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
@@ -829,7 +829,7 @@ static void __init crng_initialize_prima {memcpy (&crng->state[0 ],"expand 32-byte k" ,16 );_extract_entropy (&input_pool, &crng->state[4 ],sizeof (__u32) *12 ,0 ); -if (crng_init_try_arch_early (crng) && trust_cpu) { +if (crng_init_try_arch_early (crng) && trust_cpu && crng_init <2 ) {invalidate_batched_entropy ();numa_crng_init (); crng_init =2 ;
Greg Kroah-Hartman
maintainerPATCH 16/234 lib/crypto: blake2s: move hmac construction into wireguard
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit d8d83d8ab0a453e17e68b3a3bed1f940c34b8646 upstream.
Basically nobody should use blake2s in an HMAC construction; it already has a keyed variant. But unfortunately for historical reasons, Noise, used by WireGuard, uses HKDF quite strictly, which means we have to use this. Because this really shouldn't be used by others, this commit moves it into wireguard's noise.c locally, so that kernels that aren't using WireGuard don't get this superfluous code baked in. On m68k systems, this shaves off ~314 bytes.
[Jason: for stable, skip the wireguard changes, since this kernel
doesn't have wireguard.]
--- include/crypto/blake2s.h | 3 --- lib/crypto/blake2s-selftest.c | 31 ------------------------------- lib/crypto/blake2s.c | 37 ------------------------------------- 3 files changed, 71 deletions(-)
include/crypto/blake2s.h +0 -3
@@ -100,7 +100,4 @@ static inline void blake2s(u8 *out, cons blake2s_final(&state, out); } -void blake2s256_hmac (u8 *out,const u8 *in,const u8 *key,const size_t inlen, -const size_t keylen); -#endif /* BLAKE2S_H */
lib/crypto/blake2s-selftest.c +0 -31
@@ -15,7 +15,6 @@* #include <stdio.h> * * #include <openssl/evp.h> -* #include <openssl/hmac.h> * * #define BLAKE2S_TESTVEC_COUNT 256 *
@@ -58,16 +57,6 @@* } * printf("};\n\n"); * -* printf("static const u8 blake2s_hmac_testvecs[][BLAKE2S_HASH_SIZE] __initconst = {\n"); -* -* HMAC(EVP_blake2s256(), key, sizeof(key), buf, sizeof(buf), hash, NULL); -* print_vec(hash, BLAKE2S_OUTBYTES); -* -* HMAC(EVP_blake2s256(), buf, sizeof(buf), key, sizeof(key), hash, NULL); -* print_vec(hash, BLAKE2S_OUTBYTES); -* -* printf("};\n"); -* * return 0; *} */
@@ -554,15 +543,6 @@ static const u8 blake2s_testvecs[][BLAKE 0xd6, 0x98, 0x6b, 0x07, 0x10, 0x65, 0x52, 0x65, }, }; -static const u8 blake2s_hmac_testvecs[][BLAKE2S_HASH_SIZE ]__initconst = { - {0xce ,0xe1 ,0x57 ,0x69 ,0x82 ,0xdc ,0xbf ,0x43 ,0xad ,0x56 ,0x4c ,0x70 , -0xed ,0x68 ,0x16 ,0x96 ,0xcf ,0xa4 ,0x73 ,0xe8 ,0xe8 ,0xfc ,0x32 ,0x79 , -0x08 ,0x0a ,0x75 ,0x82 ,0xda ,0x3f ,0x05 ,0x11 , }, - {0x77 ,0x2f ,0x0c ,0x71 ,0x41 ,0xf4 ,0x4b ,0x2b ,0xb3 ,0xc6 ,0xb6 ,0xf9 , -0x60 ,0xde ,0xe4 ,0x52 ,0x38 ,0x66 ,0xe8 ,0xbf ,0x9b ,0x96 ,0xc4 ,0x9f , -0x60 ,0xd9 ,0x24 ,0x37 ,0x99 ,0xd6 ,0xec ,0x31 , }, -}; -bool __init blake2s_selftest (void ) {u8 key[BLAKE2S_KEY_SIZE ];
@@ -607,16 +587,5 @@ bool __init blake2s_selftest(void) } } -if (success) { -blake2s256_hmac (hash, buf, key,sizeof (buf),sizeof (key)); - success &= !memcmp (hash, blake2s_hmac_testvecs[0 ],BLAKE2S_HASH_SIZE ); - -blake2s256_hmac (hash, key, buf,sizeof (key),sizeof (buf)); - success &= !memcmp (hash, blake2s_hmac_testvecs[1 ],BLAKE2S_HASH_SIZE ); - -if (!success) -pr_err ("blake2s256_hmac self-test: FAIL\n" ); - } -return success; }
lib/crypto/blake2s.c +0 -37
@@ -59,43 +59,6 @@ void blake2s_final(struct blake2s_state }EXPORT_SYMBOL (blake2s_final); -void blake2s256_hmac (u8 *out,const u8 *in,const u8 *key,const size_t inlen, -const size_t keylen) -{ -struct blake2s_state state; -u8 x_key[BLAKE2S_BLOCK_SIZE ]__aligned(__alignof__(u32)) = {0 }; -u8 i_hash[BLAKE2S_HASH_SIZE ]__aligned(__alignof__(u32)) ; -int i; - -if (keylen >BLAKE2S_BLOCK_SIZE ) { -blake2s_init (&state,BLAKE2S_HASH_SIZE ); -blake2s_update (&state, key, keylen); -blake2s_final (&state, x_key); - }else -memcpy (x_key, key, keylen); - -for (i =0 ; i <BLAKE2S_BLOCK_SIZE ; ++i) - x_key[i] ^=0x36 ; - -blake2s_init (&state,BLAKE2S_HASH_SIZE ); -blake2s_update (&state, x_key,BLAKE2S_BLOCK_SIZE ); -blake2s_update (&state, in, inlen); -blake2s_final (&state, i_hash); - -for (i =0 ; i <BLAKE2S_BLOCK_SIZE ; ++i) - x_key[i] ^=0x5c ^0x36 ; - -blake2s_init (&state,BLAKE2S_HASH_SIZE ); -blake2s_update (&state, x_key,BLAKE2S_BLOCK_SIZE ); -blake2s_update (&state, i_hash,BLAKE2S_HASH_SIZE ); -blake2s_final (&state, i_hash); - -memcpy (out, i_hash,BLAKE2S_HASH_SIZE ); -memzero_explicit (x_key,BLAKE2S_BLOCK_SIZE ); -memzero_explicit (i_hash,BLAKE2S_HASH_SIZE ); -} -EXPORT_SYMBOL (blake2s256_hmac); -static int __init mod_init (void ) { if (!IS_ENABLED(CONFIG_CRYPTO_MANAGER_DISABLE_TESTS) &&
Greg Kroah-Hartman
maintainerPATCH 21/234 random: ignore GRND_RANDOM in getentropy(2)
In reply to Greg Kroah-HartmanFrom: Andy Lutomirski <[email protected]>
commit 48446f198f9adcb499b30332488dfd5bc3f176f6 upstream.
The separate blocking pool is going away. Start by ignoring GRND_RANDOM in getentropy(2).
This should not materially break any API. Any code that worked without this change should work at least as well with this change.
--- drivers/char/random.c | 3 --- include/uapi/linux/random.h | 2 +- 2 files changed, 1 insertion(+), 4 deletions(-)
drivers/char/random.c +0 -3
@@ -2210,9 +2210,6 @@ SYSCALL_DEFINE3(getrandom, char __user *if (count >INT_MAX ) count =INT_MAX ; -if (flags &GRND_RANDOM ) -return _random_read (flags &GRND_NONBLOCK , buf, count); -if (!(flags &GRND_INSECURE ) && !crng_ready ()) {if (flags &GRND_NONBLOCK )return -EAGAIN ;
include/uapi/linux/random.h +1 -1
@@ -48,7 +48,7 @@ struct rand_pool_info {* Flags for getrandom(2) * * GRND_NONBLOCK Don't block and return EAGAIN instead -* GRND_RANDOM Use the /dev/random pool instead of /dev/urandom +* GRND_RANDOM No effect * GRND_INSECURE Return non-cryptographic random bytes */ #define GRND_NONBLOCK 0x0001
Greg Kroah-Hartman
maintainerPATCH 22/234 random: make /dev/random be almost like /dev/urandom
In reply to Greg Kroah-HartmanFrom: Andy Lutomirski <[email protected]>
commit 30c08efec8884fb106b8e57094baa51bb4c44e32 upstream.
This patch changes the read semantics of /dev/random to be the same as /dev/urandom except that reads will block until the CRNG is ready.
None of the cleanups that this enables have been done yet. As a result, this gives a warning about an unused function.
--- drivers/char/random.c | 54 ++++++++++++-------------------------------------- 1 file changed, 13 insertions(+), 41 deletions(-)
drivers/char/random.c +13 -41
@@ -354,7 +354,6 @@#define INPUT_POOL_WORDS (1 << (INPUT_POOL_SHIFT-5))#define OUTPUT_POOL_SHIFT 10#define OUTPUT_POOL_WORDS (1 << (OUTPUT_POOL_SHIFT-5)) -#define SEC_XFER_SIZE 512#define EXTRACT_SIZE 10
@@ -804,7 +803,6 @@ retry:if (entropy_bits >= random_read_wakeup_bits &&wq_has_sleeper (&random_read_wait)) {wake_up_interruptible (&random_read_wait); -kill_fasync (&fasync,SIGIO ,POLL_IN ); }/* If the input pool is getting full, and the blocking * pool has room, send some entropy to the blocking
@@ -1988,43 +1986,6 @@ void rand_initialize_disk(struct gendisk#endif static ssize_t -_random_read (int nonblock,char __user *buf,size_t nbytes) -{ -ssize_t n; - -if (nbytes ==0 ) -return 0 ; - - nbytes =min_t (size_t, nbytes,SEC_XFER_SIZE ); -while (1 ) { - n =extract_entropy_user (&blocking_pool, buf, nbytes); -if (n <0 ) -return n; -trace_random_read (n*8 , (nbytes-n)*8 , -ENTROPY_BITS (&blocking_pool), -ENTROPY_BITS (&input_pool)); -if (n >0 ) -return n; - -/* Pool is (near) empty. Maybe wait and retry. */ -if (nonblock) -return -EAGAIN ; - -wait_event_interruptible (random_read_wait, - blocking_pool.initialized && - (ENTROPY_BITS (&input_pool) >= random_read_wakeup_bits)); -if (signal_pending (current)) -return -ERESTARTSYS ; - } -} - -static ssize_t -random_read (struct file *file,char __user *buf,size_t nbytes,loff_t *ppos) -{ -return _random_read (file->f_flags &O_NONBLOCK , buf, nbytes); -} - -static ssize_t urandom_read_nowarn (struct file *file,char __user *buf,size_t nbytes,loff_t *ppos) {
@@ -2056,15 +2017,26 @@ urandom_read(struct file *file, char __ureturn urandom_read_nowarn (file, buf, nbytes, ppos); } +static ssize_t +random_read (struct file *file,char __user *buf,size_t nbytes,loff_t *ppos) +{ +int ret; + + ret =wait_for_random_bytes (); +if (ret !=0 ) +return ret; +return urandom_read_nowarn (file, buf, nbytes, ppos); +} +static __poll_t random_poll (struct file *file,poll_table * wait) {__poll_t mask; -poll_wait (file, &random_read_wait, wait); +poll_wait (file, &crng_init_wait, wait);poll_wait (file, &random_write_wait, wait); mask =0 ; -if (ENTROPY_BITS (&input_pool) >= random_read_wakeup_bits) +if (crng_ready ()) mask |=EPOLLIN |EPOLLRDNORM ;if (ENTROPY_BITS (&input_pool) < random_write_wakeup_bits) mask |=EPOLLOUT |EPOLLWRNORM ;
Greg Kroah-Hartman
maintainerPATCH 26/234 random: delete code to pull data into pools
In reply to Greg Kroah-HartmanFrom: Andy Lutomirski <[email protected]>
commit 84df7cdfbb215a34657b39f4257dab739efa2df9 upstream.
There is no pool that pulls, so it was just dead code.
--- drivers/char/random.c | 40 ---------------------------------------- 1 file changed, 40 deletions(-)
drivers/char/random.c +0 -40
@@ -530,10 +530,8 @@ struct entropy_store {const struct poolinfo *poolinfo;__u32 *pool;const char *name; -struct entropy_store *pull;/* read-write data: */ -unsigned long last_pulled;spinlock_t lock;unsigned short add_ptr;unsigned short input_rotate;
@@ -1367,41 +1365,6 @@ EXPORT_SYMBOL_GPL(add_disk_randomness);*********************************************************************/ /* -* This utility inline function is responsible for transferring entropy -* from the primary pool to the secondary extraction pool. We make -* sure we pull enough for a 'catastrophic reseed'. -*/ -static void _xfer_secondary_pool (struct entropy_store *r,size_t nbytes); -static void xfer_secondary_pool (struct entropy_store *r,size_t nbytes) -{ -if (!r->pull || - r->entropy_count >= (nbytes << (ENTROPY_SHIFT +3 )) || - r->entropy_count > r->poolinfo->poolfracbits) -return ; - -_xfer_secondary_pool (r, nbytes); -} - -static void _xfer_secondary_pool (struct entropy_store *r,size_t nbytes) -{ -__u32 tmp[OUTPUT_POOL_WORDS ]; - -int bytes = nbytes; - -/* pull at least as much as a wakeup */ - bytes =max_t (int, bytes, random_read_wakeup_bits /8 ); -/* but never more than the buffer size */ - bytes =min_t (int, bytes,sizeof (tmp)); - -trace_xfer_secondary_pool (r->name, bytes *8 , nbytes *8 , -ENTROPY_BITS (r),ENTROPY_BITS (r->pull)); - bytes =extract_entropy (r->pull, tmp, bytes, - random_read_wakeup_bits /8 ,0 ); -mix_pool_bytes (r, tmp, bytes); -credit_entropy_bits (r, bytes*8 ); -} - -/* * This function decides how many bytes to actually take from the * given pool, and also debits the entropy count accordingly. */
@@ -1564,7 +1527,6 @@ static ssize_t extract_entropy(struct enspin_unlock_irqrestore (&r->lock, flags);trace_extract_entropy (r->name,EXTRACT_SIZE ,ENTROPY_BITS (r), _RET_IP_); -xfer_secondary_pool (r,EXTRACT_SIZE );extract_buf (r, tmp);spin_lock_irqsave (&r->lock, flags);memcpy (r->last_data, tmp,EXTRACT_SIZE );
@@ -1573,7 +1535,6 @@ static ssize_t extract_entropy(struct en }trace_extract_entropy (r->name, nbytes,ENTROPY_BITS (r), _RET_IP_); -xfer_secondary_pool (r, nbytes); nbytes =account (r, nbytes, min, reserved);return _extract_entropy (r, buf, nbytes, fips_enabled);
@@ -1845,7 +1806,6 @@ static void __init init_std_data(structktime_t now =ktime_get_real ();unsigned long rv; - r->last_pulled = jiffies;mix_pool_bytes (r, &now,sizeof (now));for (i = r->poolinfo->poolbytes; i >0 ; i -=sizeof (rv)) { if (!arch_get_random_seed_long(&rv) &&
Greg Kroah-Hartman
maintainerPATCH 27/234 random: remove kernel.random.read_wakeup_threshold
In reply to Greg Kroah-HartmanFrom: Andy Lutomirski <[email protected]>
commit c95ea0c69ffda19381c116db2be23c7e654dac98 upstream.
It has no effect any more, so remove it. We can revert this if there is some user code that expects to be able to set this sysctl.
--- drivers/char/random.c | 18 +----------------- 1 file changed, 1 insertion(+), 17 deletions(-)
drivers/char/random.c +1 -17
@@ -370,12 +370,6 @@#define ENTROPY_BITS (r) ((r)->entropy_count >> ENTROPY_SHIFT)/* -* The minimum number of bits of entropy before we wake up a read on -* /dev/random. Should be enough to do a significant reseed. -*/ -static int random_read_wakeup_bits =64 ; - -/* * If the entropy count falls under this number of bits, then we * should wake up processes which are selecting or polling on write * access to /dev/random.
@@ -2073,8 +2067,7 @@ SYSCALL_DEFINE3(getrandom, char __user *#include <linux/sysctl.h> -static int min_read_thresh =8 , min_write_thresh; -static int max_read_thresh =OUTPUT_POOL_WORDS *32 ; +static int min_write_thresh;static int max_write_thresh =INPUT_POOL_WORDS *32 ;static int random_min_urandom_seed =60 ;static char sysctl_bootid[16 ];
@@ -2150,15 +2143,6 @@ struct ctl_table random_table[] = { .data = &input_pool.entropy_count, }, { - .procname = "read_wakeup_threshold", - .data = &random_read_wakeup_bits, - .maxlen = sizeof(int), - .mode = 0644, - .proc_handler = proc_dointvec_minmax, - .extra1 = &min_read_thresh, - .extra2 = &max_read_thresh, - }, - { .procname = "write_wakeup_threshold", .data = &random_write_wakeup_bits, .maxlen = sizeof(int),
Greg Kroah-Hartman
maintainerPATCH 31/234 random: fix typo in add_timer_randomness()
In reply to Greg Kroah-HartmanFrom: Yangtao Li <[email protected]>
commit 727d499a6f4f29b6abdb635032f5e53e5905aedb upstream.
s/entimate/estimate
--- drivers/char/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
@@ -1214,7 +1214,7 @@ static void add_timer_randomness(struct/* * delta is now minimum absolute delta. * Round down by 1 bit on general principles, -* and limit entropy entimate to 12 bits. +* and limit entropy estimate to 12 bits. */ credit_entropy_bits (r,min_t (int,fls (delta>>1 ),11 )); }
Greg Kroah-Hartman
maintainerPATCH 32/234 random: remove some dead code of poolinfo
In reply to Greg Kroah-HartmanFrom: Yangtao Li <[email protected]>
commit 09a6d00a42ce0e63e2a15be3d070974bcc656ec7 upstream.
Since it is not being used, so delete it.
--- drivers/char/random.c | 30 ------------------------------ 1 file changed, 30 deletions(-)
drivers/char/random.c +0 -30
@@ -431,36 +431,6 @@ static const struct poolinfo {/* was: x^128 + x^103 + x^76 + x^51 +x^25 + x + 1 */ /* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */ { S(128), 104, 76, 51, 25, 1 }, -/* was: x^32 + x^26 + x^20 + x^14 + x^7 + x + 1 */ -/* x^32 + x^26 + x^19 + x^14 + x^7 + x + 1 */ - { S(32), 26, 19, 14, 7, 1 }, -#if 0 - /* x^2048 + x^1638 + x^1231 + x^819 + x^411 + x + 1 -- 115 */ - { S(2048), 1638, 1231, 819, 411, 1 }, - -/* x^1024 + x^817 + x^615 + x^412 + x^204 + x + 1 -- 290 */ - { S(1024), 817, 615, 412, 204, 1 }, - -/* x^1024 + x^819 + x^616 + x^410 + x^207 + x^2 + 1 -- 115 */ - { S(1024), 819, 616, 410, 207, 2 }, - -/* x^512 + x^411 + x^308 + x^208 + x^104 + x + 1 -- 225 */ - { S(512), 411, 308, 208, 104, 1 }, - -/* x^512 + x^409 + x^307 + x^206 + x^102 + x^2 + 1 -- 95 */ - { S(512), 409, 307, 206, 102, 2 }, -/* x^512 + x^409 + x^309 + x^205 + x^103 + x^2 + 1 -- 95 */ - { S(512), 409, 309, 205, 103, 2 }, - -/* x^256 + x^205 + x^155 + x^101 + x^52 + x + 1 -- 125 */ - { S(256), 205, 155, 101, 52, 1 }, - -/* x^128 + x^103 + x^78 + x^51 + x^27 + x^2 + 1 -- 70 */ - { S(128), 103, 78, 51, 27, 2 }, - -/* x^64 + x^52 + x^39 + x^26 + x^14 + x + 1 -- 15 */ - { S(64), 52, 39, 26, 14, 1 }, -#endif };/*
Greg Kroah-Hartman
maintainerPATCH 9/234 random: fix soft lockup when trying to read from an uninitialized blocking pool
In reply to Greg Kroah-HartmanFrom: Theodore Ts'o <[email protected]>
commit 58be0106c5306b939b07b4b8bf00669a20593f4b upstream.
--- drivers/char/random.c | 16 +++++++++++++--- 1 file changed, 13 insertions(+), 3 deletions(-)
drivers/char/random.c +13 -3
@@ -773,8 +773,11 @@ retry:if (cmpxchg (&r->entropy_count, orig, entropy_count) != orig) goto retry; -if (has_initialized) +if (has_initialized) { r->initialized =1 ; +wake_up_interruptible (&random_read_wait); +kill_fasync (&fasync,SIGIO ,POLL_IN ); + }trace_credit_entropy_bits (r->name, nbits, entropy_count >>ENTROPY_SHIFT , _RET_IP_);
@@ -790,6 +793,13 @@ retry: entropy_bits = r->entropy_count >>ENTROPY_SHIFT ; } +/* initialize the blocking pool if necessary */ +if (entropy_bits >= random_read_wakeup_bits && + !other->initialized) { +schedule_work (&other->push_work); +return ; + } +/* should we wake readers? */ if (entropy_bits >= random_read_wakeup_bits &&wq_has_sleeper (&random_read_wait)) {
@@ -2002,8 +2012,8 @@ _random_read(int nonblock, char __user *return -EAGAIN ;wait_event_interruptible (random_read_wait, -ENTROPY_BITS (&input_pool) >= - random_read_wakeup_bits); + blocking_pool.initialized && + (ENTROPY_BITS (&input_pool) >= random_read_wakeup_bits));if (signal_pending (current))return -ERESTARTSYS ; }
Greg Kroah-Hartman
maintainerPATCH 56/234 random: initialize ChaCha20 constants with correct endianness
In reply to Greg Kroah-HartmanFrom: Eric Biggers <[email protected]>
commit a181e0fdb2164268274453b5b291589edbb9b22d upstream.
On big endian CPUs, the ChaCha20-based CRNG is using the wrong endianness for the ChaCha20 constants.
This doesn't matter cryptographically, but technically it means it's not ChaCha20 anymore. Fix it to always use the standard constants.
--- drivers/char/random.c | 4 ++-- include/crypto/chacha20.h | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-)
drivers/char/random.c +2 -2
@@ -817,7 +817,7 @@ static bool __init crng_init_try_arch_eastatic void crng_initialize_secondary (struct crng_state *crng) { -memcpy (&crng->state[0 ],"expand 32-byte k" ,16 ); +chacha_init_consts (crng->state);_get_random_bytes (&crng->state[4 ],sizeof (__u32) *12 );crng_init_try_arch (crng); crng->init_time = jiffies -CRNG_RESEED_INTERVAL -1 ;
@@ -825,7 +825,7 @@ static void crng_initialize_secondary(ststatic void __init crng_initialize_primary (struct crng_state *crng) { -memcpy (&crng->state[0 ],"expand 32-byte k" ,16 ); +chacha_init_consts (crng->state);_extract_entropy (&input_pool, &crng->state[4 ],sizeof (__u32) *12 ,0 );if (crng_init_try_arch_early (crng) && trust_cpu && crng_init <2 ) {invalidate_batched_entropy ();
include/crypto/chacha20.h +8 -0
@@ -24,4 +24,12 @@ int crypto_chacha20_setkey(struct crypto unsigned int keysize);int crypto_chacha20_crypt (struct skcipher_request *req); +static inline void chacha_init_consts (u32 *state) +{ + state[0 ] =0x61707865 ;/* "expa" */ + state[1 ] =0x3320646e ;/* "nd 3" */ + state[2 ] =0x79622d32 ;/* "2-by" */ + state[3 ] =0x6b206574 ;/* "te k" */ +} +#endif
Greg Kroah-Hartman
maintainercover 4.19.249-rc1 review
This is the start of the stable review cycle for the 4.19.249 release. There are 234 patches in this series, all will be posted as a response to this one. If anyone has any issues with these being applied, please let me know.
Responses should be made by Sat, 25 Jun 2022 16:43:11 +0000. Anything received after that time might be too late.
The whole patch series can be found in one patch at:
https://www.kernel.org/pub/linux/kernel/v4.x/stable-review/patch-4.19.249-rc1.gz
or in the git tree and branch at:
git://git.kernel.org/pub/scm/linux/kernel/git/stable/linux-stable-rc.git linux-4.19.y
and the diffstat can be found below.
thanks,
greg k-h
-------------
Pseudo-Shortlog of commits:
Greg Kroah-Hartman <[email protected]>
Linux 4.19.249-rc1
Greg Kroah-Hartman <[email protected]>
Revert "hwmon: Make chip parameter for with_info API mandatory"
Willy Tarreau <[email protected]>
tcp: drop the hash_32() part from the index calculation
Willy Tarreau <[email protected]>
tcp: increase source port perturb table to 2^16
Willy Tarreau <[email protected]>
tcp: dynamically allocate the perturb table used by source ports
Willy Tarreau <[email protected]>
tcp: add small random increments to the source port
Willy Tarreau <[email protected]>
tcp: use different parts of the port_offset for index and offset
Eric Dumazet <[email protected]>
tcp: add some entropy in __inet_hash_connect()
Colin Ian King <[email protected]>
xprtrdma: fix incorrect header size calculations
Marian Postevca <[email protected]>
usb: gadget: u_ether: fix regression in setting fixed MAC address
Christian Borntraeger <[email protected]>
s390/mm: use non-quiescing sske for KVM switch to keyed guest
Alexey Kardashevskiy <[email protected]>
powerpc/mm: Switch obsolete dssall to .long
Randy Dunlap <[email protected]>
RISC-V: fix barrier() use in <vdso/processor.h>
Ilya Maximets <[email protected]>
net: openvswitch: fix leak of nested actions
Ilya Maximets <[email protected]>
net: openvswitch: fix misuse of the cached connection on tuple changes
Murilo Opsfelder Araujo <[email protected]>
virtio-pci: Remove wrong address verification in vp_del_vqs()
Zhang Yi <[email protected]>
ext4: add reserved GDT blocks check
Ding Xiang <[email protected]>
ext4: make variable "count" signed
Baokun Li <[email protected]>
ext4: fix bug_on ext4_mb_use_inode_pa
Ilpo Järvinen <[email protected]>
serial: 8250: Store to lsr_save_flags after lsr read
Miaoqian Lin <[email protected]>
usb: gadget: lpc32xx_udc: Fix refcount leak in lpc32xx_udc_probe
Miaoqian Lin <[email protected]>
usb: dwc2: Fix memory leak in dwc2_hcd_init
Robert Eckelmann <[email protected]>
USB: serial: io_ti: add Agilent E5805A support
Slark Xiao <[email protected]>
USB: serial: option: add support for Cinterion MV31 with new baseline
Ian Abbott <[email protected]>
comedi: vmk80xx: fix expression for tx buffer size
Miaoqian Lin <[email protected]>
irqchip/gic-v3: Fix refcount leak in gic_populate_ppi_partitions
Miaoqian Lin <[email protected]>
irqchip/gic/realview: Fix refcount leak in realview_gic_of_init
Josh Poimboeuf <[email protected]>
faddr2line: Fix overlapping text section failures, the sequel
Masahiro Yamada <[email protected]>
certs/blacklist_hashes.c: fix const confusion in certs blacklist
Mark Rutland <[email protected]>
arm64: ftrace: fix branch range checks
Christophe JAILLET <[email protected]>
net: bgmac: Fix an erroneous kfree() in bgmac_remove()
Petr Machata <[email protected]>
mlxsw: spectrum_cnt: Reorder counter pools
Miaoqian Lin <[email protected]>
misc: atmel-ssc: Fix IRQ check in ssc_probe
Vincent Whitchurch <[email protected]>
tty: goldfish: Fix free_irq() on remove
Aleksandr Loktionov <[email protected]>
i40e: Fix call trace in setup_tx_descriptors
Grzegorz Szczurek <[email protected]>
i40e: Fix adding ADQ filter to TC0
Trond Myklebust <[email protected]>
pNFS: Don't keep retrying if the server replied NFS4ERR_LAYOUTUNAVAILABLE
Jason A. Donenfeld <[email protected]>
random: credit cpu and bootloader seeds by default
Chen Lin <[email protected]>
net: ethernet: mtk_eth_soc: fix misuse of mem alloc interface netdev[napi]_alloc_frag
Wang Yufen <[email protected]>
ipv6: Fix signed integer overflow in l2tp_ip6_sendmsg
Xiaohui Zhang <[email protected]>
nfc: nfcmrvl: Fix memory leak in nfcmrvl_play_deferred
chengkaitao <[email protected]>
virtio-mmio: fix missing put_device() when vm_cmdline_parent registration failed
Chengguang Xu <[email protected]>
scsi: pmcraid: Fix missing resource cleanup in error case
Chengguang Xu <[email protected]>
scsi: ipr: Fix missing/incorrect resource cleanup in error case
James Smart <[email protected]>
scsi: lpfc: Fix port stuck in bypassed state after LIP in PT2PT topology
Wentao Wang <[email protected]>
scsi: vmw_pvscsi: Expand vcpuHint to 16 bits
Mark Brown <[email protected]>
ASoC: wm_adsp: Fix event generation for wm_adsp_fw_put()
Mark Brown <[email protected]>
ASoC: es8328: Fix event generation for deemphasis control
Adam Ford <[email protected]>
ASoC: wm8962: Fix suspend while playing music
Sergey Shtylyov <[email protected]>
ata: libata-core: fix NULL pointer deref in ata_host_alloc_pinfo()
Charles Keepax <[email protected]>
ASoC: cs42l56: Correct typo in minimum level for SX volume controls
Charles Keepax <[email protected]>
ASoC: cs42l52: Correct TLV for Bypass Volume
Charles Keepax <[email protected]>
ASoC: cs53l30: Correct number of volume levels on SX controls
Charles Keepax <[email protected]>
ASoC: cs42l52: Fix TLV scales for mixer controls
He Ying <[email protected]>
powerpc/kasan: Silence KASAN warnings in __get_wchan()
Jason A. Donenfeld <[email protected]>
random: account for arch randomness in bits
Jason A. Donenfeld <[email protected]>
random: mark bootloader randomness code as __init
Jason A. Donenfeld <[email protected]>
random: avoid checking crng_ready() twice in random_init()
Nicolai Stange <[email protected]>
crypto: drbg - make reseeding from get_random_bytes() synchronous
Stephan Müller <[email protected]>
crypto: drbg - always try to free Jitter RNG instance
Nicolai Stange <[email protected]>
crypto: drbg - move dynamic ->reseed_threshold adjustments to __drbg_seed()
Nicolai Stange <[email protected]>
crypto: drbg - track whether DRBG was seeded with !rng_is_initialized()
Nicolai Stange <[email protected]>
crypto: drbg - prepare for more fine-grained tracking of seeding state
Stephan Müller <[email protected]>
crypto: drbg - always seeded with SP800-90B compliant noise source
Stephan Mueller <[email protected]>
crypto: drbg - add FIPS 140-2 CTRNG for noise source
Jason A. Donenfeld <[email protected]>
Revert "random: use static branch for crng_ready()"
Jason A. Donenfeld <[email protected]>
random: check for signals after page of pool writes
Jens Axboe <[email protected]>
random: wire up fops->splice_{read,write}_iter()Jens Axboe <[email protected]>
random: convert to using fops->write_iter()
Jason A. Donenfeld <[email protected]>
random: move randomize_page() into mm where it belongs
Jason A. Donenfeld <[email protected]>
random: move initialization functions out of hot pages
Jason A. Donenfeld <[email protected]>
random: use proper return types on get_random_{int,long}_wait()
Jason A. Donenfeld <[email protected]>
random: remove extern from functions in header
Jason A. Donenfeld <[email protected]>
random: use static branch for crng_ready()
Jason A. Donenfeld <[email protected]>
random: credit architectural init the exact amount
Jason A. Donenfeld <[email protected]>
random: handle latent entropy and command line from random_init()
Jason A. Donenfeld <[email protected]>
random: use proper jiffies comparison macro
Jason A. Donenfeld <[email protected]>
random: remove ratelimiting for in-kernel unseeded randomness
Jason A. Donenfeld <[email protected]>
random: avoid initializing twice in credit race
Jason A. Donenfeld <[email protected]>
random: use symbolic constants for crng_init states
Jason A. Donenfeld <[email protected]>
siphash: use one source of truth for siphash permutations
Jason A. Donenfeld <[email protected]>
random: help compiler out with fast_mix() by using simpler arguments
Jason A. Donenfeld <[email protected]>
random: do not use input pool from hard IRQs
Jason A. Donenfeld <[email protected]>
random: order timer entropy functions below interrupt functions
Jason A. Donenfeld <[email protected]>
random: do not pretend to handle premature next security model
Jason A. Donenfeld <[email protected]>
random: do not use batches when !crng_ready()
Jason A. Donenfeld <[email protected]>
random: insist on random_get_entropy() existing in order to simplify
Jason A. Donenfeld <[email protected]>
xtensa: use fallback for random_get_entropy() instead of zero
Jason A. Donenfeld <[email protected]>
sparc: use fallback for random_get_entropy() instead of zero
Jason A. Donenfeld <[email protected]>
um: use fallback for random_get_entropy() instead of zero
Jason A. Donenfeld <[email protected]>
x86/tsc: Use fallback for random_get_entropy() instead of zero
Jason A. Donenfeld <[email protected]>
nios2: use fallback for random_get_entropy() instead of zero
Jason A. Donenfeld <[email protected]>
arm: use fallback for random_get_entropy() instead of zero
Jason A. Donenfeld <[email protected]>
mips: use fallback for random_get_entropy() instead of just c0 random
Jason A. Donenfeld <[email protected]>
m68k: use fallback for random_get_entropy() instead of zero
Jason A. Donenfeld <[email protected]>
timekeeping: Add raw clock fallback for random_get_entropy()
Jason A. Donenfeld <[email protected]>
powerpc: define get_cycles macro for arch-override
Jason A. Donenfeld <[email protected]>
alpha: define get_cycles macro for arch-override
Jason A. Donenfeld <[email protected]>
parisc: define get_cycles macro for arch-override
Jason A. Donenfeld <[email protected]>
s390: define get_cycles macro for arch-override
Jason A. Donenfeld <[email protected]>
ia64: define get_cycles macro for arch-override
Jason A. Donenfeld <[email protected]>
init: call time_init() before rand_initialize()
Jason A. Donenfeld <[email protected]>
random: fix sysctl documentation nits
Jason A. Donenfeld <[email protected]>
random: document crng_fast_key_erasure() destination possibility
Jason A. Donenfeld <[email protected]>
random: make random_get_entropy() return an unsigned long
Jason A. Donenfeld <[email protected]>
random: check for signals every PAGE_SIZE chunk of /dev/[u]random
Jann Horn <[email protected]>
random: check for signal_pending() outside of need_resched() check
Jason A. Donenfeld <[email protected]>
random: do not allow user to keep crng key around on stack
Jan Varho <[email protected]>
random: do not split fast init input in add_hwgenerator_randomness()
Jason A. Donenfeld <[email protected]>
random: mix build-time latent entropy into pool at init
Jason A. Donenfeld <[email protected]>
random: re-add removed comment about get_random_{u32,u64} reseeding
Jason A. Donenfeld <[email protected]>
random: treat bootloader trust toggle the same way as cpu trust toggle
Jason A. Donenfeld <[email protected]>
random: skip fast_init if hwrng provides large chunk of entropy
Jason A. Donenfeld <[email protected]>
random: check for signal and try earlier when generating entropy
Jason A. Donenfeld <[email protected]>
random: reseed more often immediately after booting
Jason A. Donenfeld <[email protected]>
random: make consistent usage of crng_ready()
Jason A. Donenfeld <[email protected]>
random: use SipHash as interrupt entropy accumulator
Jason A. Donenfeld <[email protected]>
random: replace custom notifier chain with standard one
Jason A. Donenfeld <[email protected]>
random: don't let 644 read-only sysctls be written to
Jason A. Donenfeld <[email protected]>
random: give sysctl_random_min_urandom_seed a more sensible value
Jason A. Donenfeld <[email protected]>
random: do crng pre-init loading in worker rather than irq
Jason A. Donenfeld <[email protected]>
random: unify cycles_t and jiffies usage and types
Jason A. Donenfeld <[email protected]>
random: cleanup UUID handling
Jason A. Donenfeld <[email protected]>
random: only wake up writers after zap if threshold was passed
Jason A. Donenfeld <[email protected]>
random: round-robin registers as ulong, not u32
Jason A. Donenfeld <[email protected]>
random: clear fast pool, crng, and batches in cpuhp bring up
Jason A. Donenfeld <[email protected]>
random: pull add_hwgenerator_randomness() declaration into random.h
Jason A. Donenfeld <[email protected]>
random: check for crng_init == 0 in add_device_randomness()
Jason A. Donenfeld <[email protected]>
random: unify early init crng load accounting
Jason A. Donenfeld <[email protected]>
random: do not take pool spinlock at boot
Jason A. Donenfeld <[email protected]>
random: defer fast pool mixing to worker
Jason A. Donenfeld <[email protected]>
random: rewrite header introductory comment
Jason A. Donenfeld <[email protected]>
random: group sysctl functions
Jason A. Donenfeld <[email protected]>
random: group userspace read/write functions
Jason A. Donenfeld <[email protected]>
random: group entropy collection functions
Jason A. Donenfeld <[email protected]>
random: group entropy extraction functions
Jason A. Donenfeld <[email protected]>
random: group initialization wait functions
Jason A. Donenfeld <[email protected]>
random: remove whitespace and reorder includes
Jason A. Donenfeld <[email protected]>
random: remove useless header comment
Jason A. Donenfeld <[email protected]>
random: introduce drain_entropy() helper to declutter crng_reseed()
Jason A. Donenfeld <[email protected]>
random: deobfuscate irq u32/u64 contributions
Jason A. Donenfeld <[email protected]>
random: add proper SPDX header
Jason A. Donenfeld <[email protected]>
random: remove unused tracepoints
Jason A. Donenfeld <[email protected]>
random: remove ifdef'd out interrupt bench
Jason A. Donenfeld <[email protected]>
random: tie batched entropy generation to base_crng generation
Jason A. Donenfeld <[email protected]>
random: zero buffer after reading entropy from userspace
Jason A. Donenfeld <[email protected]>
random: remove outdated INT_MAX >> 6 check in urandom_read()
Jason A. Donenfeld <[email protected]>
random: use hash function for crng_slow_load()
Jason A. Donenfeld <[email protected]>
random: absorb fast pool into input pool after fast load
Jason A. Donenfeld <[email protected]>
random: do not xor RDRAND when writing into /dev/random
Jason A. Donenfeld <[email protected]>
random: ensure early RDSEED goes through mixer on init
Jason A. Donenfeld <[email protected]>
random: inline leaves of rand_initialize()
Jason A. Donenfeld <[email protected]>
random: use RDSEED instead of RDRAND in entropy extraction
Dominik Brodowski <[email protected]>
random: fix locking in crng_fast_load()
Jason A. Donenfeld <[email protected]>
random: remove batched entropy locking
Eric Biggers <[email protected]>
random: remove use_input_pool parameter from crng_reseed()
Jason A. Donenfeld <[email protected]>
random: make credit_entropy_bits() always safe
Jason A. Donenfeld <[email protected]>
random: always wake up entropy writers after extraction
Jason A. Donenfeld <[email protected]>
random: use linear min-entropy accumulation crediting
Jason A. Donenfeld <[email protected]>
random: simplify entropy debiting
Jason A. Donenfeld <[email protected]>
random: use computational hash for entropy extraction
Dominik Brodowski <[email protected]>
random: only call crng_finalize_init() for primary_crng
Dominik Brodowski <[email protected]>
random: access primary_pool directly rather than through pointer
Dominik Brodowski <[email protected]>
random: continually use hwgenerator randomness
Jason A. Donenfeld <[email protected]>
random: simplify arithmetic function flow in account()
Jason A. Donenfeld <[email protected]>
random: access input_pool_data directly rather than through pointer
Jason A. Donenfeld <[email protected]>
random: cleanup fractional entropy shift constants
Jason A. Donenfeld <[email protected]>
random: prepend remaining pool constants with POOL_
Jason A. Donenfeld <[email protected]>
random: de-duplicate INPUT_POOL constants
Jason A. Donenfeld <[email protected]>
random: remove unused OUTPUT_POOL constants
Jason A. Donenfeld <[email protected]>
random: rather than entropy_store abstraction, use global
Jason A. Donenfeld <[email protected]>
random: remove unused extract_entropy() reserved argument
Jason A. Donenfeld <[email protected]>
random: remove incomplete last_data logic
Jason A. Donenfeld <[email protected]>
random: cleanup integer types
Jason A. Donenfeld <[email protected]>
random: cleanup poolinfo abstraction
Schspa Shi <[email protected]>
random: fix typo in comments
Jann Horn <[email protected]>
random: don't reset crng_init_cnt on urandom_read()
Jason A. Donenfeld <[email protected]>
random: avoid superfluous call to RDRAND in CRNG extraction
Dominik Brodowski <[email protected]>
random: early initialization of ChaCha constants
Eric Biggers <[email protected]>
random: initialize ChaCha20 constants with correct endianness
Jason A. Donenfeld <[email protected]>
random: use IS_ENABLED(CONFIG_NUMA) instead of ifdefs
Dominik Brodowski <[email protected]>
random: harmonize "crng init done" messages
Jason A. Donenfeld <[email protected]>
random: mix bootloader randomness into pool
Jason A. Donenfeld <[email protected]>
random: do not re-init if crng_reseed completes before primary init
Jason A. Donenfeld <[email protected]>
random: do not sign extend bytes for rotation when mixing
Jason A. Donenfeld <[email protected]>
random: use BLAKE2s instead of SHA1 in extraction
Sebastian Andrzej Siewior <[email protected]>
random: remove unused irq_flags argument from add_interrupt_randomness()
Mark Brown <[email protected]>
random: document add_hwgenerator_randomness() with other input functions
Eric Biggers <[email protected]>
crypto: blake2s - adjust include guard naming
Eric Biggers <[email protected]>
crypto: blake2s - include <linux/bug.h> instead of <asm/bug.h>
Jason A. Donenfeld <[email protected]>
MAINTAINERS: co-maintain random.c
Eric Biggers <[email protected]>
random: remove dead code left over from blocking pool
Ard Biesheuvel <[email protected]>
random: avoid arch_get_random_seed_long() when collecting IRQ randomness
Mark Rutland <[email protected]>
random: add arch_get_random_*long_early()
Richard Henderson <[email protected]>
powerpc: Use bool in archrandom.h
Richard Henderson <[email protected]>
linux/random.h: Mark CONFIG_ARCH_RANDOM functions __must_check
Richard Henderson <[email protected]>
linux/random.h: Use false with bool
Richard Henderson <[email protected]>
linux/random.h: Remove arch_has_random, arch_has_random_seed
Richard Henderson <[email protected]>
s390: Remove arch_has_random, arch_has_random_seed
Richard Henderson <[email protected]>
powerpc: Remove arch_has_random, arch_has_random_seed
Richard Henderson <[email protected]>
x86: Remove arch_has_random, arch_has_random_seed
Mark Rutland <[email protected]>
random: avoid warnings for !CONFIG_NUMA builds
Mark Rutland <[email protected]>
random: split primary/secondary crng init paths
Yangtao Li <[email protected]>
random: remove some dead code of poolinfo
Yangtao Li <[email protected]>
random: fix typo in add_timer_randomness()
Yangtao Li <[email protected]>
random: Add and use pr_fmt()
Yangtao Li <[email protected]>
random: convert to ENTROPY_BITS for better code readability
Yangtao Li <[email protected]>
random: remove unnecessary unlikely()
Andy Lutomirski <[email protected]>
random: remove kernel.random.read_wakeup_threshold
Andy Lutomirski <[email protected]>
random: delete code to pull data into pools
Andy Lutomirski <[email protected]>
random: remove the blocking pool
Dominik Brodowski <[email protected]>
random: fix crash on multiple early calls to add_bootloader_randomness()
Sergey Senozhatsky <[email protected]>
char/random: silence a lockdep splat with printk()
Andy Lutomirski <[email protected]>
random: make /dev/random be almost like /dev/urandom
Andy Lutomirski <[email protected]>
random: ignore GRND_RANDOM in getentropy(2)
Andy Lutomirski <[email protected]>
random: add GRND_INSECURE to return best-effort non-cryptographic bytes
Andy Lutomirski <[email protected]>
random: Add a urandom_read_nowait() for random APIs that don't warn
Andy Lutomirski <[email protected]>
random: Don't wake crng_init_wait when crng_init == 1
Jason A. Donenfeld <[email protected]>
lib/crypto: sha1: re-roll loops to reduce code size
Jason A. Donenfeld <[email protected]>
lib/crypto: blake2s: move hmac construction into wireguard
Jason A. Donenfeld <[email protected]>
crypto: blake2s - generic C library implementation and selftest
Herbert Xu <[email protected]>
Revert "hwrng: core - Freeze khwrng thread during suspend"
Borislav Petkov <[email protected]>
char/random: Add a newline at the end of the file
Stephen Boyd <[email protected]>
random: Use wait_event_freezable() in add_hwgenerator_randomness()
Hsin-Yi Wang <[email protected]>
fdt: add support for rng-seed
Stephen Boyd <[email protected]>
random: Support freezable kthreads in add_hwgenerator_randomness()
Theodore Ts'o <[email protected]>
random: fix soft lockup when trying to read from an uninitialized blocking pool
Vasily Gorbik <[email protected]>
latent_entropy: avoid build error when plugin cflags are not set
George Spelvin <[email protected]>
random: document get_random_int() family
Kees Cook <[email protected]>
random: move rand_initialize() earlier
Theodore Ts'o <[email protected]>
random: only read from /dev/random after its pool has received 128 bits
Rasmus Villemoes <[email protected]>
drivers/char/random.c: make primary_crng static
Rasmus Villemoes <[email protected]>
drivers/char/random.c: remove unused stuct poolinfo::poolbits
Rasmus Villemoes <[email protected]>
drivers/char/random.c: constify poolinfo_table
Al Viro <[email protected]>
9p: missing chunk of "fs/9p: Don't update file type when updating file attributes"
-------------
Diffstat:
Documentation/admin-guide/kernel-parameters.txt | 6 + Documentation/hwmon/hwmon-kernel-api.txt | 2 +- Documentation/sysctl/kernel.txt | 35 +- MAINTAINERS | 1 + Makefile | 4 +- arch/alpha/include/asm/timex.h | 1 + arch/arm/include/asm/timex.h | 1 + arch/arm64/kernel/ftrace.c | 4 +- arch/ia64/include/asm/timex.h | 1 + arch/m68k/include/asm/timex.h | 2 +- arch/mips/include/asm/timex.h | 17 +- arch/nios2/include/asm/timex.h | 3 + arch/parisc/include/asm/timex.h | 3 +- arch/powerpc/include/asm/archrandom.h | 27 +- arch/powerpc/include/asm/ppc-opcode.h | 2 + arch/powerpc/include/asm/timex.h | 1 + arch/powerpc/kernel/idle_6xx.S | 2 +- arch/powerpc/kernel/l2cr_6xx.S | 6 +- arch/powerpc/kernel/process.c | 4 +- arch/powerpc/kernel/swsusp_32.S | 2 +- arch/powerpc/kernel/swsusp_asm64.S | 2 +- arch/powerpc/mm/mmu_context.c | 2 +- arch/powerpc/platforms/powermac/cache.S | 4 +- arch/riscv/include/asm/processor.h | 2 + arch/s390/include/asm/archrandom.h | 12 - arch/s390/include/asm/timex.h | 1 + arch/s390/mm/pgtable.c | 2 +- arch/sparc/include/asm/timex_32.h | 4 +- arch/um/include/asm/timex.h | 9 +- arch/x86/include/asm/archrandom.h | 12 +- arch/x86/include/asm/timex.h | 9 + arch/x86/include/asm/tsc.h | 7 +- arch/xtensa/include/asm/timex.h | 6 +- certs/blacklist_hashes.c | 2 +- crypto/drbg.c | 220 +- drivers/ata/libata-core.c | 4 +- drivers/char/Kconfig | 50 +- drivers/char/hw_random/core.c | 1 + drivers/char/random.c | 3079 +++++++------------- drivers/hv/hv.c | 2 +- drivers/hv/vmbus_drv.c | 2 +- drivers/hwmon/hwmon.c | 16 +- drivers/irqchip/irq-gic-realview.c | 1 + drivers/irqchip/irq-gic-v3.c | 5 +- drivers/misc/atmel-ssc.c | 4 +- drivers/net/ethernet/broadcom/bgmac-bcma.c | 1 - drivers/net/ethernet/intel/i40e/i40e_ethtool.c | 25 +- drivers/net/ethernet/intel/i40e/i40e_main.c | 5 + drivers/net/ethernet/mediatek/mtk_eth_soc.c | 21 +- drivers/net/ethernet/mellanox/mlxsw/spectrum_cnt.h | 2 +- drivers/nfc/nfcmrvl/usb.c | 16 +- drivers/of/fdt.c | 14 +- drivers/scsi/ipr.c | 4 +- drivers/scsi/lpfc/lpfc_nportdisc.c | 3 +- drivers/scsi/pmcraid.c | 2 +- drivers/scsi/vmw_pvscsi.h | 4 +- drivers/staging/comedi/drivers/vmk80xx.c | 2 +- drivers/tty/goldfish.c | 2 +- drivers/tty/serial/8250/8250_port.c | 2 + drivers/usb/dwc2/hcd.c | 2 +- drivers/usb/gadget/function/u_ether.c | 11 +- drivers/usb/gadget/udc/lpc32xx_udc.c | 1 + drivers/usb/serial/io_ti.c | 2 + drivers/usb/serial/io_usbvend.h | 1 + drivers/usb/serial/option.c | 6 + drivers/virtio/virtio_mmio.c | 1 + drivers/virtio/virtio_pci_common.c | 3 +- fs/9p/vfs_inode_dotl.c | 10 +- fs/ext4/mballoc.c | 9 + fs/ext4/namei.c | 3 +- fs/ext4/resize.c | 10 + fs/nfs/pnfs.c | 6 + include/crypto/blake2s.h | 102 + include/crypto/chacha20.h | 15 + include/crypto/drbg.h | 18 +- include/crypto/internal/blake2s.h | 19 + include/linux/cpuhotplug.h | 2 + include/linux/hw_random.h | 2 - include/linux/mm.h | 2 + include/linux/prandom.h | 23 +- include/linux/random.h | 122 +- include/linux/siphash.h | 28 + include/linux/timex.h | 10 +- include/trace/events/random.h | 316 -- include/uapi/linux/random.h | 4 +- init/main.c | 18 +- kernel/cpu.c | 11 + kernel/irq/handle.c | 2 +- kernel/time/timekeeping.c | 15 + lib/Kconfig.debug | 3 +- lib/Makefile | 2 + lib/crypto/Makefile | 7 + lib/crypto/blake2s-generic.c | 111 + lib/crypto/blake2s-selftest.c | 591 ++++ lib/crypto/blake2s.c | 78 + lib/random32.c | 15 +- lib/sha1.c | 95 +- lib/siphash.c | 32 +- lib/vsprintf.c | 10 +- mm/util.c | 33 + net/ipv4/inet_hashtables.c | 31 +- net/l2tp/l2tp_ip6.c | 5 +- net/openvswitch/actions.c | 6 + net/openvswitch/conntrack.c | 3 +- net/openvswitch/flow_netlink.c | 61 +- net/sunrpc/xprtrdma/rpc_rdma.c | 4 +- scripts/faddr2line | 45 +- sound/soc/codecs/cs42l52.c | 8 +- sound/soc/codecs/cs42l56.c | 4 +- sound/soc/codecs/cs53l30.c | 16 +- sound/soc/codecs/es8328.c | 5 +- sound/soc/codecs/wm8962.c | 1 + sound/soc/codecs/wm_adsp.c | 2 +- 113 files changed, 2839 insertions(+), 2788 deletions(-)
Greg Kroah-Hartman
maintainerPATCH 57/234 random: early initialization of ChaCha constants
From: Dominik Brodowski <[email protected]>
commit 96562f286884e2db89c74215b199a1084b5fb7f7 upstream.
Previously, the ChaCha constants for the primary pool were only initialized in crng_initialize_primary(), called by rand_initialize(). However, some randomness is actually extracted from the primary pool beforehand, e.g. by kmem_cache_create(). Therefore, statically initialize the ChaCha constants for the primary pool.
--- drivers/char/random.c | 5 ++++- include/crypto/chacha20.h | 15 +++++++++++---- 2 files changed, 15 insertions(+), 5 deletions(-)
drivers/char/random.c +4 -1
@@ -458,6 +458,10 @@ struct crng_state {static struct crng_state primary_crng = { .lock =__SPIN_LOCK_UNLOCKED (primary_crng.lock), + .state[0 ] =CHACHA_CONSTANT_EXPA , + .state[1 ] =CHACHA_CONSTANT_ND_3 , + .state[2 ] =CHACHA_CONSTANT_2_BY , + .state[3 ] =CHACHA_CONSTANT_TE_K , };/*
@@ -825,7 +829,6 @@ static void crng_initialize_secondary(ststatic void __init crng_initialize_primary (struct crng_state *crng) { -chacha_init_consts (crng->state);_extract_entropy (&input_pool, &crng->state[4 ],sizeof (__u32) *12 ,0 );if (crng_init_try_arch_early (crng) && trust_cpu && crng_init <2 ) {invalidate_batched_entropy ();
include/crypto/chacha20.h +11 -4
@@ -24,12 +24,19 @@ int crypto_chacha20_setkey(struct crypto unsigned int keysize);int crypto_chacha20_crypt (struct skcipher_request *req); +enum chacha_constants {/* expand 32-byte k */ +CHACHA_CONSTANT_EXPA =0x61707865U , +CHACHA_CONSTANT_ND_3 =0x3320646eU , +CHACHA_CONSTANT_2_BY =0x79622d32U , +CHACHA_CONSTANT_TE_K =0x6b206574U +}; +static inline void chacha_init_consts (u32 *state) { - state[0 ] =0x61707865 ;/* "expa" */ - state[1 ] =0x3320646e ;/* "nd 3" */ - state[2 ] =0x79622d32 ;/* "2-by" */ - state[3 ] =0x6b206574 ;/* "te k" */ + state[0 ] =CHACHA_CONSTANT_EXPA ; + state[1 ] =CHACHA_CONSTANT_ND_3 ; + state[2 ] =CHACHA_CONSTANT_2_BY ; + state[3 ] =CHACHA_CONSTANT_TE_K ; }#endif
Greg Kroah-Hartman
maintainerPATCH 45/234 MAINTAINERS: co-maintain random.c
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 58e1100fdc5990b0cc0d4beaf2562a92e621ac7d upstream.
random.c is a bit understaffed, and folks want more prompt reviews. I've got the crypto background and the interest to do these reviews, and have authored parts of the file already.
--- MAINTAINERS | 1 + 1 file changed, 1 insertion(+)
MAINTAINERS +1 -0
@@ -12239,6 +12239,7 @@ F: arch/mips/configs/generic/board-ranch RANDOM NUMBER DRIVER M: "Theodore Ts'o" <[email protected]> +M: Jason A. Donenfeld <[email protected]> S: Maintained F: drivers/char/random.c
Greg Kroah-Hartman
maintainerPATCH 46/234 crypto: blake2s - include <linux/bug.h> instead of <asm/bug.h>
In reply to Greg Kroah-HartmanFrom: Eric Biggers <[email protected]>
commit bbda6e0f1303953c855ee3669655a81b69fbe899 upstream.
Address the following checkpatch warning:
WARNING: Use #include <linux/bug.h> instead of <asm/bug.h>
--- include/crypto/blake2s.h | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
include/crypto/blake2s.h +1 -2
@@ -6,12 +6,11 @@#ifndef BLAKE2S_H #define BLAKE2S_H +#include <linux/bug.h> #include <linux/types.h> #include <linux/kernel.h> #include <linux/string.h> -#include <asm/bug.h> -enum blake2s_lengths {BLAKE2S_BLOCK_SIZE =64 , BLAKE2S_HASH_SIZE = 32,
Greg Kroah-Hartman
maintainerPATCH 36/234 powerpc: Remove arch_has_random, arch_has_random_seed
In reply to Greg Kroah-HartmanFrom: Richard Henderson <[email protected]>
commit cbac004995a0ce8453bdc555fab579e2bdb842a6 upstream.
These symbols are currently part of the generic archrandom.h interface, but are currently unused and can be removed.
--- arch/powerpc/include/asm/archrandom.h | 10 ---------- 1 file changed, 10 deletions(-)
arch/powerpc/include/asm/archrandom.h +0 -10
@@ -34,16 +34,6 @@ static inline int arch_get_random_seed_i return rc; } - -static inline int arch_has_random (void ) -{ -return 0 ; -} - -static inline int arch_has_random_seed (void ) -{ -return !!ppc_md.get_random_seed; -}#endif /* CONFIG_ARCH_RANDOM */ #ifdef CONFIG_PPC_POWERNV
Greg Kroah-Hartman
maintainerPATCH 54/234 random: harmonize "crng init done" messages
In reply to Greg Kroah-HartmanFrom: Dominik Brodowski <[email protected]>
commit 161212c7fd1d9069b232785c75492e50941e2ea8 upstream.
We print out "crng init done" for !TRUST_CPU, so we should also print out the same for TRUST_CPU.
--- drivers/char/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
@@ -833,7 +833,7 @@ static void __init crng_initialize_primainvalidate_batched_entropy ();numa_crng_init (); crng_init =2 ; -pr_notice ("crng done (trusting CPU's manufacturer)\n" ); +pr_notice ("crng init done (trusting CPU's manufacturer)\n" ); } crng->init_time = jiffies -CRNG_RESEED_INTERVAL -1 ; }
Greg Kroah-Hartman
maintainerPATCH 53/234 random: mix bootloader randomness into pool
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 57826feeedb63b091f807ba8325d736775d39afd upstream.
If we're trusting bootloader randomness, crng_fast_load() is called by add_hwgenerator_randomness(), which sets us to crng_init==1. However, usually it is only called once for an initial 64-byte push, so bootloader entropy will not mix any bytes into the input pool. So it's conceivable that crng_init==1 when crng_initialize_primary() is called later, but then the input pool is empty. When that happens, the crng state key will be overwritten with extracted output from the empty input pool. That's bad.
In contrast, if we're not trusting bootloader randomness, we call crng_slow_load() *and* we call mix_pool_bytes(), so that later crng_initialize_primary() isn't drawing on nothing.
In order to prevent crng_initialize_primary() from extracting an empty pool, have the trusted bootloader case mirror that of the untrusted bootloader case, mixing the input into the pool.
--- drivers/char/random.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
drivers/char/random.c +6 -2
@@ -2295,8 +2295,12 @@ void add_hwgenerator_randomness(const chstruct entropy_store *poolp = &input_pool;if (unlikely (crng_init ==0 )) { -crng_fast_load (buffer, count); -return ; +size_t ret =crng_fast_load (buffer, count); +mix_pool_bytes (poolp, buffer, ret); + count -= ret; + buffer += ret; +if (!count || crng_init ==0 ) +return ; }/* Suspend writing if we're above the trickle threshold.
Greg Kroah-Hartman
maintainerPATCH 55/234 random: use IS_ENABLED(CONFIG_NUMA) instead of ifdefs
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 7b87324112df2e1f9b395217361626362dcfb9fb upstream.
Rather than an awkward combination of ifdefs and __maybe_unused, we can ensure more source gets parsed, regardless of the configuration, by using IS_ENABLED for the CONFIG_NUMA conditional code. This makes things cleaner and easier to follow.
I've confirmed that on !CONFIG_NUMA, we don't wind up with excess code by accident; the generated object file is the same.
--- drivers/char/random.c | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-)
drivers/char/random.c +12 -20
@@ -761,7 +761,6 @@ static int credit_entropy_bits_safe(strustatic DECLARE_WAIT_QUEUE_HEAD (crng_init_wait); -#ifdef CONFIG_NUMA /* * Hack to deal with crazy userspace progams when they are all trying * to access /dev/urandom in parallel. The programs are almost
@@ -769,7 +768,6 @@ static DECLARE_WAIT_QUEUE_HEAD(crng_init* their brain damage. */ static struct crng_state **crng_node_pool__read_mostly ; -#endif static void invalidate_batched_entropy (void );static void numa_crng_init (void );
@@ -817,7 +815,7 @@ static bool __init crng_init_try_arch_eareturn arch_init; } -static void __maybe_unused crng_initialize_secondary (struct crng_state *crng) +static void crng_initialize_secondary (struct crng_state *crng) {memcpy (&crng->state[0 ],"expand 32-byte k" ,16 );_get_random_bytes (&crng->state[4 ],sizeof (__u32) *12 );
@@ -868,7 +866,6 @@ static void crng_finalize_init(struct cr } } -#ifdef CONFIG_NUMA static void do_numa_crng_init (struct work_struct *work) {int i;
@@ -895,29 +892,24 @@ static DECLARE_WORK(numa_crng_init_work,static void numa_crng_init (void ) { -schedule_work (&numa_crng_init_work); +if (IS_ENABLED (CONFIG_NUMA )) +schedule_work (&numa_crng_init_work); }static struct crng_state *select_crng (void ) { -struct crng_state **pool; -int nid =numa_node_id (); - -/* pairs with cmpxchg_release() in do_numa_crng_init() */ - pool =READ_ONCE (crng_node_pool); -if (pool && pool[nid]) -return pool[nid]; - -return &primary_crng; -} -#else -static void numa_crng_init (void ) {} +if (IS_ENABLED (CONFIG_NUMA )) { +struct crng_state **pool; +int nid =numa_node_id (); + +/* pairs with cmpxchg_release() in do_numa_crng_init() */ + pool =READ_ONCE (crng_node_pool); +if (pool && pool[nid]) +return pool[nid]; + } -static struct crng_state *select_crng (void ) -{return &primary_crng; } -#endif /* * crng_fast_load() can be called by code in the interrupt service
Greg Kroah-Hartman
maintainerPATCH 58/234 random: avoid superfluous call to RDRAND in CRNG extraction
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 2ee25b6968b1b3c66ffa408de23d023c1bce81cf upstream.
RDRAND is not fast. RDRAND is actually quite slow. We've known this for a while, which is why functions like get_random_u{32,64} were converted to use batching of our ChaCha-based CRNG instead.
Yet CRNG extraction still includes a call to RDRAND, in the hot path of every call to get_random_bytes(), /dev/urandom, and getrandom(2).
This call to RDRAND here seems quite superfluous. CRNG is already extracting things based on a 256-bit key, based on good entropy, which is then reseeded periodically, updated, backtrack-mutated, and so forth. The CRNG extraction construction is something that we're already relying on to be secure and solid. If it's not, that's a serious problem, and it's unlikely that mixing in a measly 32 bits from RDRAND is going to alleviate things.
And in the case where the CRNG doesn't have enough entropy yet, we're already initializing the ChaCha key row with RDRAND in crng_init_try_arch_early().
Removing the call to RDRAND improves performance on an i7-11850H by 370%. In other words, the vast majority of the work done by extract_crng() prior to this commit was devoted to fetching 32 bits of RDRAND.
--- drivers/char/random.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/char/random.c +1 -3
@@ -1023,7 +1023,7 @@ static void crng_reseed(struct crng_statstatic void _extract_crng (struct crng_state *crng,__u8 out[CHACHA20_BLOCK_SIZE ]) { -unsigned long v, flags, init_time; +unsigned long flags, init_time;if (crng_ready ()) { init_time =READ_ONCE (crng->init_time);
@@ -1033,8 +1033,6 @@ static void _extract_crng(struct crng_st &input_pool : NULL); }spin_lock_irqsave (&crng->lock, flags); -if (arch_get_random_long (&v)) - crng->state[14 ] ^= v;chacha20_block (&crng->state[0 ], out);if (crng->state[12 ] ==0 ) crng->state[13 ]++;
Greg Kroah-Hartman
maintainerPATCH 59/234 random: dont reset crng_init_cnt on urandom_read()
In reply to Greg Kroah-HartmanFrom: Jann Horn <[email protected]>
commit 6c8e11e08a5b74bb8a5cdd5cbc1e5143df0fba72 upstream.
At the moment, urandom_read() (used for /dev/urandom) resets crng_init_cnt to zero when it is called at crng_init<2. This is inconsistent: We do it for /dev/urandom reads, but not for the equivalent getrandom(GRND_INSECURE).
(And worse, as Jason pointed out, we're only doing this as long as maxwarn>0.)
crng_init_cnt is only read in crng_fast_load(); it is relevant at crng_init==0 for determining when to switch to crng_init==1 (and where in the RNG state array to write).
As far as I understand:
- crng_init==0 means "we have nothing, we might just be returning the same exact numbers on every boot on every machine, we don't even have non-cryptographic randomness; we should shove every bit of entropy we can get into the RNG immediately"
- crng_init==1 means "well we have something, it might not be cryptographic, but at least we're not gonna return the same data every time or whatever, it's probably good enough for TCP and ASLR and stuff; we now have time to build up actual cryptographic entropy in the input pool"
- crng_init==2 means "this is supposed to be cryptographically secure now, but we'll keep adding more entropy just to be sure".
The current code means that if someone is pulling data from /dev/urandom fast enough at crng_init==0, we'll keep resetting crng_init_cnt, and we'll never make forward progress to crng_init==1. It seems to be intended to prevent an attacker from bruteforcing the contents of small individual RNG inputs on the way from crng_init==0 to crng_init==1, but that's misguided; crng_init==1 isn't supposed to provide proper cryptographic security anyway, RNG users who care about getting secure RNG output have to wait until crng_init==2.
This code was inconsistent, and it probably made things worse - just get rid of it.
--- drivers/char/random.c | 4 ---- 1 file changed, 4 deletions(-)
drivers/char/random.c +0 -4
@@ -1831,7 +1831,6 @@ urandom_read_nowarn(struct file *file, cstatic ssize_t urandom_read (struct file *file,char __user *buf,size_t nbytes,loff_t *ppos) { -unsigned long flags;static int maxwarn =10 ;if (!crng_ready () && maxwarn >0 ) {
@@ -1839,9 +1838,6 @@ urandom_read(struct file *file, char __uif (__ratelimit (&urandom_warning))pr_notice ("%s: uninitialized urandom read (%zd bytes read)\n" , current->comm, nbytes); -spin_lock_irqsave (&primary_crng.lock, flags); - crng_init_cnt =0 ; -spin_unlock_irqrestore (&primary_crng.lock, flags); }return urandom_read_nowarn (file, buf, nbytes, ppos);
Greg Kroah-Hartman
maintainerPATCH 60/234 random: fix typo in comments
In reply to Greg Kroah-HartmanFrom: Schspa Shi <[email protected]>
commit c0a8a61e7abbf66729687ee63659ee25983fbb1e upstream.
s/or/for
--- drivers/char/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
@@ -101,7 +101,7 @@* =============================== * * There are four exported interfaces; two for use within the kernel, -* and two or use from userspace. +* and two for use from userspace. * * Exported interfaces ---- userspace output * -----------------------------------------
Greg Kroah-Hartman
maintainerPATCH 61/234 random: cleanup poolinfo abstraction
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 91ec0fe138f107232cb36bc6112211db37cb5306 upstream.
Now that we're only using one polynomial, we can cleanup its representation into constants, instead of passing around pointers dynamically to select different polynomials. This improves the codegen and makes the code a bit more straightforward.
--- drivers/char/random.c | 67 ++++++++++++++++++++++---------------------------- 1 file changed, 30 insertions(+), 37 deletions(-)
drivers/char/random.c +30 -37
@@ -431,14 +431,20 @@ static int random_write_wakeup_bits = 28* polynomial which improves the resulting TGFSR polynomial to be * irreducible, which we have made here. */ -static const struct poolinfo { -int poolbitshift, poolwords, poolbytes, poolfracbits; -#define S (x) ilog2(x)+5, (x), (x)*4, (x) << (ENTROPY_SHIFT+5) -int tap1, tap2, tap3, tap4, tap5; -} poolinfo_table[] = { -/* was: x^128 + x^103 + x^76 + x^51 +x^25 + x + 1 */ +enum poolinfo { +POOL_WORDS =128 , +POOL_WORDMASK =POOL_WORDS -1 , +POOL_BYTES =POOL_WORDS *sizeof (u32), +POOL_BITS =POOL_BYTES *8 , +POOL_BITSHIFT =ilog2 (POOL_WORDS ) +5 , +POOL_FRACBITS =POOL_WORDS << (ENTROPY_SHIFT +5 ), +/* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */ - {S (128 ),104 ,76 ,51 ,25 ,1 }, +POOL_TAP1 =104 , +POOL_TAP2 =76 , +POOL_TAP3 =51 , +POOL_TAP4 =25 , +POOL_TAP5 =1 };/*
@@ -505,7 +511,6 @@ MODULE_PARM_DESC(ratelimit_disable, "Disstruct entropy_store ;struct entropy_store {/* read-only data: */ -const struct poolinfo *poolinfo;__u32 *pool;const char *name;
@@ -527,7 +532,6 @@ static void crng_reseed(struct crng_stat static __u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy;static struct entropy_store input_pool = { - .poolinfo = &poolinfo_table[0 ], .name ="input" , .lock =__SPIN_LOCK_UNLOCKED (input_pool.lock), .pool = input_pool_data
@@ -550,33 +554,26 @@ static __u32 const twist_table[8] = {static void _mix_pool_bytes (struct entropy_store *r,const void *in,int nbytes) { -unsigned long i, tap1, tap2, tap3, tap4, tap5; +unsigned long i;int input_rotate; -int wordmask = r->poolinfo->poolwords -1 ;const unsigned char *bytes = in;__u32 w; - tap1 = r->poolinfo->tap1; - tap2 = r->poolinfo->tap2; - tap3 = r->poolinfo->tap3; - tap4 = r->poolinfo->tap4; - tap5 = r->poolinfo->tap5; - input_rotate = r->input_rotate; i = r->add_ptr;/* mix one byte at a time to simplify size handling and churn faster */ while (nbytes--) { w =rol32 (*bytes++, input_rotate); - i = (i -1 ) & wordmask; + i = (i -1 ) &POOL_WORDMASK ;/* XOR in the various taps */ w ^= r->pool[i]; - w ^= r->pool[(i + tap1) & wordmask]; - w ^= r->pool[(i + tap2) & wordmask]; - w ^= r->pool[(i + tap3) & wordmask]; - w ^= r->pool[(i + tap4) & wordmask]; - w ^= r->pool[(i + tap5) & wordmask]; + w ^= r->pool[(i +POOL_TAP1 ) &POOL_WORDMASK ]; + w ^= r->pool[(i +POOL_TAP2 ) &POOL_WORDMASK ]; + w ^= r->pool[(i +POOL_TAP3 ) &POOL_WORDMASK ]; + w ^= r->pool[(i +POOL_TAP4 ) &POOL_WORDMASK ]; + w ^= r->pool[(i +POOL_TAP5 ) &POOL_WORDMASK ];/* Mix the result back in with a twist */ r->pool[i] = (w >>3 ) ^ twist_table[w &7 ];
@@ -674,7 +671,6 @@ static void process_random_ready_list(vostatic void credit_entropy_bits (struct entropy_store *r,int nbits) {int entropy_count, orig; -const int pool_size = r->poolinfo->poolfracbits;int nfrac = nbits <<ENTROPY_SHIFT ;if (!nbits)
@@ -708,25 +704,25 @@ retry:* turns no matter how large nbits is. */ int pnfrac = nfrac; -const int s = r->poolinfo->poolbitshift +ENTROPY_SHIFT +2 ; +const int s =POOL_BITSHIFT +ENTROPY_SHIFT +2 ;/* The +2 corresponds to the /4 in the denominator */ do { -unsigned int anfrac =min (pnfrac, pool_size/2 ); +unsigned int anfrac =min (pnfrac,POOL_FRACBITS /2 );unsigned int add = - ((pool_size - entropy_count)*anfrac*3 ) >> s; + ((POOL_FRACBITS - entropy_count)*anfrac*3 ) >> s; entropy_count += add; pnfrac -= anfrac; - }while (unlikely (entropy_count < pool_size-2 && pnfrac)); + }while (unlikely (entropy_count <POOL_FRACBITS -2 && pnfrac)); }if (WARN_ON (entropy_count <0 )) {pr_warn ("negative entropy/overflow: pool %s count %d\n" , r->name, entropy_count); entropy_count =0 ; - }else if (entropy_count > pool_size) - entropy_count = pool_size; + }else if (entropy_count >POOL_FRACBITS ) + entropy_count =POOL_FRACBITS ;if (cmpxchg (&r->entropy_count, orig, entropy_count) != orig) goto retry;
@@ -743,13 +739,11 @@ retry:static int credit_entropy_bits_safe (struct entropy_store *r,int nbits) { -const int nbits_max = r->poolinfo->poolwords *32 ; -if (nbits <0 )return -EINVAL ;/* Cap the value to avoid overflows */ - nbits =min (nbits, nbits_max); + nbits =min (nbits,POOL_BITS );credit_entropy_bits (r, nbits);return 0 ;
@@ -1343,7 +1337,7 @@ static size_t account(struct entropy_stoint entropy_count, orig, have_bytes;size_t ibytes, nfrac; -BUG_ON (r->entropy_count > r->poolinfo->poolfracbits); +BUG_ON (r->entropy_count >POOL_FRACBITS );/* Can we pull enough? */ retry:
@@ -1409,8 +1403,7 @@ static void extract_buf(struct entropy_s/* Generate a hash across the pool */ spin_lock_irqsave (&r->lock, flags); -blake2s_update (&state, (const u8 *)r->pool, - r->poolinfo->poolwords *sizeof (*r->pool)); +blake2s_update (&state, (const u8 *)r->pool,POOL_BYTES );blake2s_final (&state, hash);/* final zeros out state */ /*
@@ -1766,7 +1759,7 @@ static void __init init_std_data(structunsigned long rv;mix_pool_bytes (r, &now,sizeof (now)); -for (i = r->poolinfo->poolbytes; i >0 ; i -=sizeof (rv)) { +for (i =POOL_BYTES ; i >0 ; i -=sizeof (rv)) {if (!arch_get_random_seed_long (&rv) && !arch_get_random_long (&rv)) rv =random_get_entropy ();
Greg Kroah-Hartman
maintainerPATCH 62/234 random: cleanup integer types
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit d38bb0853589c939573ea50e9cb64f733e0e273d upstream.
Rather than using the userspace type, __uXX, switch to using uXX. And rather than using variously chosen char * or unsigned char *, use u8 * uniformly for things that aren't strings, in the case where we are doing byte-by-byte traversal.
--- drivers/char/random.c | 114 +++++++++++++++++++++++++------------------------- 1 file changed, 57 insertions(+), 57 deletions(-)
drivers/char/random.c +57 -57
@@ -457,7 +457,7 @@ static DEFINE_SPINLOCK(random_ready_liststatic LIST_HEAD (random_ready_list);struct crng_state { -__u32 state[16 ]; +u32 state[16 ];unsigned long init_time;spinlock_t lock; };
@@ -484,10 +484,9 @@ static bool crng_need_final_init = falsestatic int crng_init_cnt =0 ;static unsigned long crng_global_init_time =0 ;#define CRNG_INIT_CNT_THRESH (2*CHACHA20_KEY_SIZE) -static void _extract_crng (struct crng_state *crng, -__u8 out[CHACHA20_BLOCK_SIZE ]); +static void _extract_crng (struct crng_state *crng,u8 out[CHACHA20_BLOCK_SIZE ]);static void _crng_backtrack_protect (struct crng_state *crng, -__u8 tmp[CHACHA20_BLOCK_SIZE ],int used); +u8 tmp[CHACHA20_BLOCK_SIZE ],int used);static void process_random_ready_list (void );static void _get_random_bytes (void *buf,int nbytes);
@@ -511,16 +510,16 @@ MODULE_PARM_DESC(ratelimit_disable, "Disstruct entropy_store ;struct entropy_store {/* read-only data: */ -__u32 *pool; +u32 *pool;const char *name;/* read-write data: */ spinlock_t lock; -unsigned short add_ptr; -unsigned short input_rotate; +u16 add_ptr; +u16 input_rotate;int entropy_count;unsigned int last_data_init:1 ; -__u8 last_data[EXTRACT_SIZE ]; +u8 last_data[EXTRACT_SIZE ]; }; static ssize_t extract_entropy(struct entropy_store *r, void *buf,
@@ -529,7 +528,7 @@ static ssize_t _extract_entropy(struct e size_t nbytes, int fips);static void crng_reseed (struct crng_state *crng,struct entropy_store *r); -static __u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy; +static u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy;static struct entropy_store input_pool = { .name ="input" ,
@@ -537,7 +536,7 @@ static struct entropy_store input_pool = .pool = input_pool_data }; -static __u32 const twist_table[8 ] = { +static u32 const twist_table[8 ] = {0x00000000 ,0x3b6e20c8 ,0x76dc4190 ,0x4db26158 ,0xedb88320 ,0xd6d6a3e8 ,0x9b64c2b0 ,0xa00ae278 };
@@ -556,8 +555,8 @@ static void _mix_pool_bytes(struct entro {unsigned long i;int input_rotate; -const unsigned char *bytes = in; -__u32 w; +const u8 *bytes = in; +u32 w; input_rotate = r->input_rotate; i = r->add_ptr;
@@ -610,10 +609,10 @@ static void mix_pool_bytes(struct entrop }struct fast_pool { -__u32 pool[4 ]; +u32 pool[4 ];unsigned long last; -unsigned short reg_idx; -unsigned char count; +u16 reg_idx; +u8 count; };/*
@@ -623,8 +622,8 @@ struct fast_pool {*/ static void fast_mix (struct fast_pool *f) { -__u32 a = f->pool[0 ], b = f->pool[1 ]; -__u32 c = f->pool[2 ], d = f->pool[3 ]; +u32 a = f->pool[0 ], b = f->pool[1 ]; +u32 c = f->pool[2 ], d = f->pool[3 ]; a += b; c += d; b =rol32 (b,6 ); d =rol32 (d,27 );
@@ -816,14 +815,14 @@ static bool __init crng_init_try_arch_eastatic void crng_initialize_secondary (struct crng_state *crng) {chacha_init_consts (crng->state); -_get_random_bytes (&crng->state[4 ],sizeof (__u32) *12 ); +_get_random_bytes (&crng->state[4 ],sizeof (u32) *12 );crng_init_try_arch (crng); crng->init_time = jiffies -CRNG_RESEED_INTERVAL -1 ; }static void __init crng_initialize_primary (struct crng_state *crng) { -_extract_entropy (&input_pool, &crng->state[4 ],sizeof (__u32) *12 ,0 ); +_extract_entropy (&input_pool, &crng->state[4 ],sizeof (u32) *12 ,0 );if (crng_init_try_arch_early (crng) && trust_cpu && crng_init <2 ) {invalidate_batched_entropy ();numa_crng_init ();
@@ -910,12 +909,14 @@ static struct crng_state *select_crng(vo/* * crng_fast_load() can be called by code in the interrupt service -* path. So we can't afford to dilly-dally. +* path. So we can't afford to dilly-dally. Returns the number of +* bytes processed from cp. */ -static int crng_fast_load (const char *cp,size_t len) +static size_t crng_fast_load (const u8 *cp,size_t len) {unsigned long flags; -char *p; +u8 *p; +size_t ret =0 ;if (!spin_trylock_irqsave (&primary_crng.lock, flags))return 0 ;
@@ -923,10 +924,10 @@ static int crng_fast_load(const char *cpspin_unlock_irqrestore (&primary_crng.lock, flags);return 0 ; } - p = (unsigned char *) &primary_crng.state[4 ]; + p = (u8 *) &primary_crng.state[4 ];while (len >0 && crng_init_cnt <CRNG_INIT_CNT_THRESH ) { p[crng_init_cnt %CHACHA20_KEY_SIZE ] ^= *cp; - cp++; crng_init_cnt++; len--; + cp++; crng_init_cnt++; len--; ret++; }spin_unlock_irqrestore (&primary_crng.lock, flags);if (crng_init_cnt >=CRNG_INIT_CNT_THRESH ) {
@@ -934,7 +935,7 @@ static int crng_fast_load(const char *cp crng_init =1 ;pr_notice ("fast init done\n" ); } -return 1 ; +return ret; }/*
@@ -951,14 +952,14 @@ static int crng_fast_load(const char *cp* like a fixed DMI table (for example), which might very well be * unique to the machine, but is otherwise unvarying. */ -static int crng_slow_load (const char *cp,size_t len) +static int crng_slow_load (const u8 *cp,size_t len) {unsigned long flags; -static unsigned char lfsr =1 ; -unsigned char tmp; -unsigned i, max =CHACHA20_KEY_SIZE ; -const char * src_buf = cp; -char * dest_buf = (char *) &primary_crng.state[4 ]; +static u8 lfsr =1 ; +u8 tmp; +unsigned int i, max =CHACHA20_KEY_SIZE ; +const u8 * src_buf = cp; +u8 * dest_buf = (u8 *) &primary_crng.state[4 ];if (!spin_trylock_irqsave (&primary_crng.lock, flags))return 0 ;
@@ -987,8 +988,8 @@ static void crng_reseed(struct crng_statunsigned long flags;int i, num;union { -__u8 block[CHACHA20_BLOCK_SIZE ]; -__u32 key[8 ]; +u8 block[CHACHA20_BLOCK_SIZE ]; +u32 key[8 ]; } buf;if (r) {
@@ -1015,7 +1016,7 @@ static void crng_reseed(struct crng_stat }static void _extract_crng (struct crng_state *crng, -__u8 out[CHACHA20_BLOCK_SIZE ]) +u8 out[CHACHA20_BLOCK_SIZE ]) {unsigned long flags, init_time;
@@ -1033,7 +1034,7 @@ static void _extract_crng(struct crng_stspin_unlock_irqrestore (&crng->lock, flags); } -static void extract_crng (__u8 out[CHACHA20_BLOCK_SIZE ]) +static void extract_crng (u8 out[CHACHA20_BLOCK_SIZE ]) {_extract_crng (select_crng (), out); }
@@ -1043,26 +1044,26 @@ static void extract_crng(__u8 out[CHACHA* enough) to mutate the CRNG key to provide backtracking protection. */ static void _crng_backtrack_protect (struct crng_state *crng, -__u8 tmp[CHACHA20_BLOCK_SIZE ],int used) +u8 tmp[CHACHA20_BLOCK_SIZE ],int used) {unsigned long flags; -__u32 *s, *d; +u32 *s, *d;int i; - used =round_up (used,sizeof (__u32)); + used =round_up (used,sizeof (u32));if (used +CHACHA20_KEY_SIZE >CHACHA20_BLOCK_SIZE ) {extract_crng (tmp); used =0 ; }spin_lock_irqsave (&crng->lock, flags); - s = (__u32 *) &tmp[used]; + s = (u32 *) &tmp[used]; d = &crng->state[4 ];for (i=0 ; i <8 ; i++) *d++ ^= *s++;spin_unlock_irqrestore (&crng->lock, flags); } -static void crng_backtrack_protect (__u8 tmp[CHACHA20_BLOCK_SIZE ],int used) +static void crng_backtrack_protect (u8 tmp[CHACHA20_BLOCK_SIZE ],int used) {_crng_backtrack_protect (select_crng (), tmp, used); }
@@ -1070,7 +1071,7 @@ static void crng_backtrack_protect(__u8static ssize_t extract_crng_user (void __user *buf,size_t nbytes) {ssize_t ret =0 , i =CHACHA20_BLOCK_SIZE ; -__u8 tmp[CHACHA20_BLOCK_SIZE ]__aligned(4) ; +u8 tmp[CHACHA20_BLOCK_SIZE ]__aligned(4) ;int large_request = (nbytes >256 );while (nbytes) {
@@ -1158,8 +1159,8 @@ static void add_timer_randomness(structstruct entropy_store *r;struct {long jiffies; -unsigned cycles; -unsigned num; +unsigned int cycles; +unsigned int num; } sample;long delta, delta2, delta3;
@@ -1241,15 +1242,15 @@ static void add_interrupt_bench(cycles_t#define add_interrupt_bench (x)#endif -static __u32 get_reg (struct fast_pool *f,struct pt_regs *regs) +static u32 get_reg (struct fast_pool *f,struct pt_regs *regs) { -__u32 *ptr = (__u32 *) regs; +u32 *ptr = (u32 *) regs;unsigned int idx;if (regs ==NULL )return 0 ; idx =READ_ONCE (f->reg_idx); -if (idx >=sizeof (struct pt_regs ) /sizeof (__u32)) +if (idx >=sizeof (struct pt_regs ) /sizeof (u32)) idx =0 ; ptr += idx++;WRITE_ONCE (f->reg_idx, idx);
@@ -1263,8 +1264,8 @@ void add_interrupt_randomness(int irq)struct pt_regs *regs =get_irq_regs ();unsigned long now = jiffies;cycles_t cycles =random_get_entropy (); -__u32 c_high, j_high; -__u64 ip; +u32 c_high, j_high; +u64 ip;if (cycles ==0 ) cycles =get_reg (fast_pool, regs);
@@ -1282,8 +1283,7 @@ void add_interrupt_randomness(int irq)if (unlikely (crng_init ==0 )) {if ((fast_pool->count >=64 ) && -crng_fast_load ((char *) fast_pool->pool, -sizeof (fast_pool->pool))) { +crng_fast_load ((u8 *)fast_pool->pool,sizeof (fast_pool->pool)) >0 ) { fast_pool->count =0 ; fast_pool->last = now; }
@@ -1380,7 +1380,7 @@ retry:* * Note: we assume that .poolwords is a multiple of 16 words. */ -static void extract_buf (struct entropy_store *r,__u8 *out) +static void extract_buf (struct entropy_store *r,u8 *out) {struct blake2s_state state__aligned(__alignof__(unsigned long)) ;u8 hash[BLAKE2S_HASH_SIZE ];
@@ -1430,7 +1430,7 @@ static ssize_t _extract_entropy(struct e size_t nbytes, int fips) {ssize_t ret =0 , i; -__u8 tmp[EXTRACT_SIZE ]; +u8 tmp[EXTRACT_SIZE ];unsigned long flags;while (nbytes) {
@@ -1468,7 +1468,7 @@ static ssize_t _extract_entropy(struct estatic ssize_t extract_entropy (struct entropy_store *r,void *buf,size_t nbytes,int min,int reserved) { -__u8 tmp[EXTRACT_SIZE ]; +u8 tmp[EXTRACT_SIZE ];unsigned long flags;/* if last_data isn't primed, we need EXTRACT_SIZE extra bytes */
@@ -1530,7 +1530,7 @@ static void _warn_unseeded_randomness(co*/ static void _get_random_bytes (void *buf,int nbytes) { -__u8 tmp[CHACHA20_BLOCK_SIZE ]__aligned(4) ; +u8 tmp[CHACHA20_BLOCK_SIZE ]__aligned(4) ;trace_get_random_bytes (nbytes, _RET_IP_);
@@ -1724,7 +1724,7 @@ EXPORT_SYMBOL(del_random_ready_callback)int __must_check get_random_bytes_arch (void *buf,int nbytes) {int left = nbytes; -char *p = buf; +u8 *p = buf;trace_get_random_bytes_arch (left, _RET_IP_);while (left) {
@@ -1866,7 +1866,7 @@ static int write_pool(struct entropy_store *r, const char __user *buffer, size_t count) { size_t bytes; -__u32 t, buf[16 ]; +u32 t, buf[16 ];const char __user *p = buffer;while (count >0 ) {
@@ -1876,7 +1876,7 @@ write_pool(struct entropy_store *r, consif (copy_from_user (&buf, p, bytes))return -EFAULT ; -for (b = bytes ; b >0 ; b -=sizeof (__u32), i++) { +for (b = bytes; b >0 ; b -=sizeof (u32), i++) {if (!arch_get_random_int (&t))break ; buf[i] ^= t;
Greg Kroah-Hartman
maintainerPATCH 63/234 random: remove incomplete last_data logic
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit a4bfa9b31802c14ff5847123c12b98d5e36b3985 upstream.
There were a few things added under the "if (fips_enabled)" banner, which never really got completed, and the FIPS people anyway are choosing a different direction. Rather than keep around this halfbaked code, get rid of it so that we can focus on a single design of the RNG rather than two designs.
--- drivers/char/random.c | 40 ++++------------------------------------ 1 file changed, 4 insertions(+), 36 deletions(-)
drivers/char/random.c +4 -36
@@ -337,8 +337,6 @@#include <linux/spinlock.h> #include <linux/kthread.h> #include <linux/percpu.h> -#include <linux/cryptohash.h> -#include <linux/fips.h> #include <linux/ptrace.h> #include <linux/workqueue.h> #include <linux/irq.h>
@@ -518,14 +516,12 @@ struct entropy_store {u16 add_ptr;u16 input_rotate;int entropy_count; - unsigned int last_data_init:1; -u8 last_data[EXTRACT_SIZE ]; };static ssize_t extract_entropy (struct entropy_store *r,void *buf,size_t nbytes,int min,int rsvd);static ssize_t _extract_entropy (struct entropy_store *r,void *buf, -size_t nbytes,int fips); +size_t nbytes);static void crng_reseed (struct crng_state *crng,struct entropy_store *r); static u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy;
@@ -822,7 +818,7 @@ static void crng_initialize_secondary(ststatic void __init crng_initialize_primary (struct crng_state *crng) { -_extract_entropy (&input_pool, &crng->state[4 ],sizeof (u32) *12 ,0 ); +_extract_entropy (&input_pool, &crng->state[4 ],sizeof (u32) *12 );if (crng_init_try_arch_early (crng) && trust_cpu && crng_init <2 ) {invalidate_batched_entropy ();numa_crng_init ();
@@ -1427,22 +1423,13 @@ static void extract_buf(struct entropy_s }static ssize_t _extract_entropy (struct entropy_store *r,void *buf, -size_t nbytes,int fips) +size_t nbytes) {ssize_t ret =0 , i;u8 tmp[EXTRACT_SIZE ]; -unsigned long flags;while (nbytes) {extract_buf (r, tmp); - -if (fips) { -spin_lock_irqsave (&r->lock, flags); -if (!memcmp (tmp, r->last_data,EXTRACT_SIZE )) -panic ("Hardware RNG duplicated output!\n" ); -memcpy (r->last_data, tmp,EXTRACT_SIZE ); -spin_unlock_irqrestore (&r->lock, flags); - } i =min_t (int, nbytes,EXTRACT_SIZE );memcpy (buf, tmp, i); nbytes -= i;
@@ -1468,28 +1455,9 @@ static ssize_t _extract_entropy(struct estatic ssize_t extract_entropy (struct entropy_store *r,void *buf,size_t nbytes,int min,int reserved) { -u8 tmp[EXTRACT_SIZE ]; -unsigned long flags; - -/* if last_data isn't primed, we need EXTRACT_SIZE extra bytes */ -if (fips_enabled) { -spin_lock_irqsave (&r->lock, flags); -if (!r->last_data_init) { - r->last_data_init =1 ; -spin_unlock_irqrestore (&r->lock, flags); -trace_extract_entropy (r->name,EXTRACT_SIZE , -ENTROPY_BITS (r), _RET_IP_); -extract_buf (r, tmp); -spin_lock_irqsave (&r->lock, flags); -memcpy (r->last_data, tmp,EXTRACT_SIZE ); - } -spin_unlock_irqrestore (&r->lock, flags); - } -trace_extract_entropy (r->name, nbytes,ENTROPY_BITS (r), _RET_IP_); nbytes =account (r, nbytes, min, reserved); - -return _extract_entropy (r, buf, nbytes, fips_enabled); +return _extract_entropy (r, buf, nbytes); }#define warn_unseeded_randomness (previous) \
Greg Kroah-Hartman
maintainerPATCH 64/234 random: remove unused extract_entropy() reserved argument
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 8b2d953b91e7f60200c24067ab17b77cc7bfd0d4 upstream.
This argument is always set to zero, as a result of us not caring about keeping a certain amount reserved in the pool these days. So just remove it and cleanup the function signatures.
--- drivers/char/random.c | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-)
drivers/char/random.c +7 -10
@@ -519,7 +519,7 @@ struct entropy_store { };static ssize_t extract_entropy (struct entropy_store *r,void *buf, -size_t nbytes,int min,int rsvd); +size_t nbytes,int min);static ssize_t _extract_entropy (struct entropy_store *r,void *buf,size_t nbytes);
@@ -989,7 +989,7 @@ static void crng_reseed(struct crng_stat } buf;if (r) { - num =extract_entropy (r, &buf,32 ,16 ,0 ); + num =extract_entropy (r, &buf,32 ,16 );if (num ==0 )return ; }else {
@@ -1327,8 +1327,7 @@ EXPORT_SYMBOL_GPL(add_disk_randomness);* This function decides how many bytes to actually take from the * given pool, and also debits the entropy count accordingly. */ -static size_t account (struct entropy_store *r,size_t nbytes,int min, -int reserved) +static size_t account (struct entropy_store *r,size_t nbytes,int min) {int entropy_count, orig, have_bytes;size_t ibytes, nfrac;
@@ -1342,7 +1341,7 @@ retry:/* never pull more than available */ have_bytes = entropy_count >> (ENTROPY_SHIFT +3 ); -if ((have_bytes -= reserved) <0 ) +if (have_bytes <0 ) have_bytes =0 ; ibytes =min_t (size_t, ibytes, have_bytes);if (ibytes < min)
@@ -1448,15 +1447,13 @@ static ssize_t _extract_entropy(struct e* returns it in a buffer. * * The min parameter specifies the minimum amount we can pull before -* failing to avoid races that defeat catastrophic reseeding while the -* reserved parameter indicates how much entropy we must leave in the -* pool after each pull to avoid starving other readers. +* failing to avoid races that defeat catastrophic reseeding. */ static ssize_t extract_entropy (struct entropy_store *r,void *buf, -size_t nbytes,int min,int reserved) +size_t nbytes,int min) {trace_extract_entropy (r->name, nbytes,ENTROPY_BITS (r), _RET_IP_); - nbytes =account (r, nbytes, min, reserved); + nbytes =account (r, nbytes, min);return _extract_entropy (r, buf, nbytes); }
Greg Kroah-Hartman
maintainerPATCH 70/234 random: access input_pool_data directly rather than through pointer
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 6c0eace6e1499712583b6ee62d95161e8b3449f5 upstream.
This gets rid of another abstraction we no longer need. It would be nice if we could instead make pool an array rather than a pointer, but the latent entropy plugin won't be able to do its magic in that case. So instead we put all accesses to the input pool's actual data through the input_pool_data array directly.
--- drivers/char/random.c | 222 +++++++++++++++++++++++--------------------------- 1 file changed, 103 insertions(+), 119 deletions(-)
drivers/char/random.c +103 -119
@@ -124,7 +124,7 @@* * The primary kernel interface is * -* void get_random_bytes(void *buf, int nbytes); +* void get_random_bytes(void *buf, int nbytes); * * This interface will return the requested number of random bytes, * and place it in the requested buffer. This is equivalent to a
@@ -132,10 +132,10 @@* * For less critical applications, there are the functions: * -* u32 get_random_u32() -* u64 get_random_u64() -* unsigned int get_random_int() -* unsigned long get_random_long() +* u32 get_random_u32() +* u64 get_random_u64() +* unsigned int get_random_int() +* unsigned long get_random_long() * * These are produced by a cryptographic RNG seeded from get_random_bytes, * and so do not deplete the entropy pool as much. These are recommended
@@ -197,10 +197,10 @@* from the devices are: * * void add_device_randomness(const void *buf, unsigned int size); -* void add_input_randomness(unsigned int type, unsigned int code, +* void add_input_randomness(unsigned int type, unsigned int code, * unsigned int value); * void add_interrupt_randomness(int irq); -* void add_disk_randomness(struct gendisk *disk); +* void add_disk_randomness(struct gendisk *disk); * void add_hwgenerator_randomness(const char *buffer, size_t count, * size_t entropy); * void add_bootloader_randomness(const void *buf, unsigned int size);
@@ -296,8 +296,8 @@* /dev/random and /dev/urandom created already, they can be created * by using the commands: * -* mknod /dev/random c 1 8 -* mknod /dev/urandom c 1 9 +* mknod /dev/random c 1 8 +* mknod /dev/urandom c 1 9 * * Acknowledgements: * =================
@@ -443,9 +443,9 @@ static DEFINE_SPINLOCK(random_ready_liststatic LIST_HEAD (random_ready_list);struct crng_state { -u32 state[16 ]; -unsigned long init_time; -spinlock_t lock; +u32 state[16 ]; +unsigned long init_time; +spinlock_t lock; };static struct crng_state primary_crng = {
@@ -469,7 +469,7 @@ static bool crng_need_final_init = false#define crng_ready () (likely(crng_init > 1))static int crng_init_cnt =0 ;static unsigned long crng_global_init_time =0 ; -#define CRNG_INIT_CNT_THRESH (2*CHACHA20_KEY_SIZE) +#define CRNG_INIT_CNT_THRESH (2 * CHACHA20_KEY_SIZE)static void _extract_crng (struct crng_state *crng,u8 out[CHACHA20_BLOCK_SIZE ]);static void _crng_backtrack_protect (struct crng_state *crng,u8 tmp[CHACHA20_BLOCK_SIZE ],int used);
@@ -496,17 +496,12 @@ MODULE_PARM_DESC(ratelimit_disable, "Dis static u32 input_pool_data[POOL_WORDS] __latent_entropy;static struct { -/* read-only data: */ -u32 *pool; - -/* read-write data: */ spinlock_t lock;u16 add_ptr;u16 input_rotate;int entropy_count; } input_pool = { .lock =__SPIN_LOCK_UNLOCKED (input_pool.lock), - .pool = input_pool_data };static ssize_t extract_entropy (void *buf,size_t nbytes,int min);
@@ -514,7 +509,7 @@ static ssize_t _extract_entropy(void *bustatic void crng_reseed (struct crng_state *crng,bool use_input_pool); -static u32 const twist_table[8 ] = { +static const u32 twist_table[8 ] = {0x00000000 ,0x3b6e20c8 ,0x76dc4190 ,0x4db26158 ,0xedb88320 ,0xd6d6a3e8 ,0x9b64c2b0 ,0xa00ae278 };
@@ -544,15 +539,15 @@ static void _mix_pool_bytes(const void * i = (i -1 ) &POOL_WORDMASK ;/* XOR in the various taps */ - w ^= input_pool.pool[i]; - w ^= input_pool.pool[(i +POOL_TAP1 ) &POOL_WORDMASK ]; - w ^= input_pool.pool[(i +POOL_TAP2 ) &POOL_WORDMASK ]; - w ^= input_pool.pool[(i +POOL_TAP3 ) &POOL_WORDMASK ]; - w ^= input_pool.pool[(i +POOL_TAP4 ) &POOL_WORDMASK ]; - w ^= input_pool.pool[(i +POOL_TAP5 ) &POOL_WORDMASK ]; + w ^= input_pool_data[i]; + w ^= input_pool_data[(i +POOL_TAP1 ) &POOL_WORDMASK ]; + w ^= input_pool_data[(i +POOL_TAP2 ) &POOL_WORDMASK ]; + w ^= input_pool_data[(i +POOL_TAP3 ) &POOL_WORDMASK ]; + w ^= input_pool_data[(i +POOL_TAP4 ) &POOL_WORDMASK ]; + w ^= input_pool_data[(i +POOL_TAP5 ) &POOL_WORDMASK ];/* Mix the result back in with a twist */ - input_pool.pool[i] = (w >>3 ) ^ twist_table[w &7 ]; + input_pool_data[i] = (w >>3 ) ^ twist_table[w &7 ];/* * Normally, we add 7 bits of rotation to the pool.
@@ -584,10 +579,10 @@ static void mix_pool_bytes(const void *i }struct fast_pool { -u32 pool[4 ]; -unsigned long last; -u16 reg_idx; -u8 count; +u32 pool[4 ]; +unsigned long last; +u16 reg_idx; +u8 count; };/*
@@ -715,7 +710,7 @@ static int credit_entropy_bits_safe(intreturn -EINVAL ;/* Cap the value to avoid overflows */ - nbits =min (nbits,POOL_BITS ); + nbits =min (nbits,POOL_BITS );credit_entropy_bits (nbits);return 0 ;
@@ -727,7 +722,7 @@ static int credit_entropy_bits_safe(int* *********************************************************************/ -#define CRNG_RESEED_INTERVAL (300*HZ) +#define CRNG_RESEED_INTERVAL (300 * HZ)static DECLARE_WAIT_QUEUE_HEAD (crng_init_wait);
@@ -751,9 +746,9 @@ early_param("random.trust_cpu", parse_trstatic bool crng_init_try_arch (struct crng_state *crng) { -int i; -bool arch_init = true; -unsigned long rv; +int i; +bool arch_init = true; +unsigned long rv;for (i =4 ; i <16 ; i++) { if (!arch_get_random_seed_long(&rv) &&
@@ -769,9 +764,9 @@ static bool crng_init_try_arch(struct crstatic bool __init crng_init_try_arch_early (struct crng_state *crng) { -int i; -bool arch_init = true; -unsigned long rv; +int i; +bool arch_init = true; +unsigned long rv;for (i =4 ; i <16 ; i++) { if (!arch_get_random_seed_long_early(&rv) &&
@@ -841,7 +836,7 @@ static void do_numa_crng_init(struct worstruct crng_state *crng;struct crng_state **pool; - pool =kcalloc (nr_node_ids,sizeof (*pool),GFP_KERNEL |__GFP_NOFAIL); + pool =kcalloc (nr_node_ids,sizeof (*pool),GFP_KERNEL | __GFP_NOFAIL);for_each_online_node (i) { crng =kmalloc_node (sizeof (struct crng_state ),GFP_KERNEL | __GFP_NOFAIL, i);
@@ -897,7 +892,7 @@ static size_t crng_fast_load(const u8 *cspin_unlock_irqrestore (&primary_crng.lock, flags);return 0 ; } - p = (u8 *) &primary_crng.state[4 ]; + p = (u8 *)&primary_crng.state[4 ];while (len >0 && crng_init_cnt <CRNG_INIT_CNT_THRESH ) { p[crng_init_cnt %CHACHA20_KEY_SIZE ] ^= *cp; cp++; crng_init_cnt++; len--; ret++;
@@ -927,12 +922,12 @@ static size_t crng_fast_load(const u8 *c*/ static int crng_slow_load (const u8 *cp,size_t len) { -unsigned long flags; -static u8 lfsr =1 ; -u8 tmp; -unsigned int i, max =CHACHA20_KEY_SIZE ; -const u8 * src_buf = cp; -u8 * dest_buf = (u8 *) &primary_crng.state[4 ]; +unsigned long flags; +static u8 lfsr =1 ; +u8 tmp; +unsigned int i, max =CHACHA20_KEY_SIZE ; +const u8 *src_buf = cp; +u8 *dest_buf = (u8 *)&primary_crng.state[4 ];if (!spin_trylock_irqsave (&primary_crng.lock, flags))return 0 ;
@@ -943,7 +938,7 @@ static int crng_slow_load(const u8 *cp,if (len > max) max = len; -for (i =0 ; i < max ; i++) { +for (i =0 ; i < max; i++) { tmp = lfsr; lfsr >>=1 ;if (tmp &1 )
@@ -958,11 +953,11 @@ static int crng_slow_load(const u8 *cp,static void crng_reseed (struct crng_state *crng,bool use_input_pool) { -unsigned long flags; -int i, num; +unsigned long flags; +int i, num;union { -u8 block[CHACHA20_BLOCK_SIZE ]; -u32 key[8 ]; +u8 block[CHACHA20_BLOCK_SIZE ]; +u32 key[8 ]; } buf;if (use_input_pool) {
@@ -976,11 +971,11 @@ static void crng_reseed(struct crng_stat }spin_lock_irqsave (&crng->lock, flags);for (i =0 ; i <8 ; i++) { -unsigned long rv; +unsigned long rv;if (!arch_get_random_seed_long (&rv) && !arch_get_random_long (&rv)) rv =random_get_entropy (); - crng->state[i+4 ] ^= buf.key[i] ^ rv; + crng->state[i +4 ] ^= buf.key[i] ^ rv; }memzero_explicit (&buf,sizeof (buf));WRITE_ONCE (crng->init_time, jiffies);
@@ -988,8 +983,7 @@ static void crng_reseed(struct crng_statcrng_finalize_init (crng); } -static void _extract_crng (struct crng_state *crng, -u8 out[CHACHA20_BLOCK_SIZE ]) +static void _extract_crng (struct crng_state *crng,u8 out[CHACHA20_BLOCK_SIZE ]) {unsigned long flags, init_time;
@@ -1018,9 +1012,9 @@ static void extract_crng(u8 out[CHACHA20static void _crng_backtrack_protect (struct crng_state *crng,u8 tmp[CHACHA20_BLOCK_SIZE ],int used) { -unsigned long flags; -u32 *s, *d; -int i; +unsigned long flags; +u32 *s, *d; +int i; used =round_up (used,sizeof (u32));if (used +CHACHA20_KEY_SIZE >CHACHA20_BLOCK_SIZE ) {
@@ -1028,9 +1022,9 @@ static void _crng_backtrack_protect(stru used =0 ; }spin_lock_irqsave (&crng->lock, flags); - s = (u32 *) &tmp[used]; + s = (u32 *)&tmp[used]; d = &crng->state[4 ]; -for (i=0 ; i <8 ; i++) +for (i =0 ; i <8 ; i++) *d++ ^= *s++;spin_unlock_irqrestore (&crng->lock, flags); }
@@ -1075,7 +1069,6 @@ static ssize_t extract_crng_user(void __return ret; } -/********************************************************************* * * Entropy input management
@@ -1170,11 +1163,11 @@ static void add_timer_randomness(struct* Round down by 1 bit on general principles, * and limit entropy estimate to 12 bits. */ -credit_entropy_bits (min_t (int,fls (delta>>1 ),11 )); +credit_entropy_bits (min_t (int,fls (delta >>1 ),11 )); }void add_input_randomness (unsigned int type,unsigned int code, -unsigned int value) +unsigned int value) {static unsigned char last_value;
@@ -1194,19 +1187,19 @@ static DEFINE_PER_CPU(struct fast_pool,#ifdef ADD_INTERRUPT_BENCH static unsigned long avg_cycles, avg_deviation; -#define AVG_SHIFT 8/* Exponential average factor k=1/256 */ -#define FIXED_1_2 (1 << (AVG_SHIFT-1)) +#define AVG_SHIFT 8/* Exponential average factor k=1/256 */ +#define FIXED_1_2 (1 << (AVG_SHIFT - 1))static void add_interrupt_bench (cycles_t start) { -long delta =random_get_entropy () - start; +long delta =random_get_entropy () - start; -/* Use a weighted moving average */ - delta = delta - ((avg_cycles +FIXED_1_2 ) >>AVG_SHIFT ); - avg_cycles += delta; -/* And average deviation */ - delta =abs (delta) - ((avg_deviation +FIXED_1_2 ) >>AVG_SHIFT ); - avg_deviation += delta; +/* Use a weighted moving average */ + delta = delta - ((avg_cycles +FIXED_1_2 ) >>AVG_SHIFT ); + avg_cycles += delta; +/* And average deviation */ + delta =abs (delta) - ((avg_deviation +FIXED_1_2 ) >>AVG_SHIFT ); + avg_deviation += delta; }#else #define add_interrupt_bench (x)
@@ -1214,7 +1207,7 @@ static void add_interrupt_bench(cycles_tstatic u32 get_reg (struct fast_pool *f,struct pt_regs *regs) { -u32 *ptr = (u32 *) regs; +u32 *ptr = (u32 *)regs;unsigned int idx;if (regs ==NULL )
@@ -1229,12 +1222,12 @@ static u32 get_reg(struct fast_pool *f,void add_interrupt_randomness (int irq) { -struct fast_pool *fast_pool =this_cpu_ptr (&irq_randomness); -struct pt_regs *regs =get_irq_regs (); -unsigned long now = jiffies; -cycles_t cycles =random_get_entropy (); -u32 c_high, j_high; -u64 ip; +struct fast_pool *fast_pool =this_cpu_ptr (&irq_randomness); +struct pt_regs *regs =get_irq_regs (); +unsigned long now = jiffies; +cycles_t cycles =random_get_entropy (); +u32 c_high, j_high; +u64 ip;if (cycles ==0 ) cycles =get_reg (fast_pool, regs);
@@ -1244,8 +1237,8 @@ void add_interrupt_randomness(int irq) fast_pool->pool[1 ] ^= now ^ c_high; ip = regs ?instruction_pointer (regs) : _RET_IP_; fast_pool->pool[2 ] ^= ip; - fast_pool->pool[3 ] ^= (sizeof (ip) >4 ) ? ip >>32 : -get_reg (fast_pool, regs); + fast_pool->pool[3 ] ^= + (sizeof (ip) >4 ) ? ip >>32 :get_reg (fast_pool, regs);fast_mix (fast_pool);add_interrupt_bench (cycles);
@@ -1259,8 +1252,7 @@ void add_interrupt_randomness(int irq)return ; } -if ((fast_pool->count <64 ) && - !time_after (now, fast_pool->last +HZ )) +if ((fast_pool->count <64 ) && !time_after (now, fast_pool->last +HZ ))return ;if (!spin_trylock (&input_pool.lock))
@@ -1324,7 +1316,7 @@ retry: entropy_count =0 ; } nfrac = ibytes << (POOL_ENTROPY_SHIFT +3 ); -if ((size_t ) entropy_count > nfrac) +if ((size_t )entropy_count > nfrac) entropy_count -= nfrac;else entropy_count =0 ;
@@ -1369,7 +1361,7 @@ static void extract_buf(u8 *out)/* Generate a hash across the pool */ spin_lock_irqsave (&input_pool.lock, flags); -blake2s_update (&state, (const u8 *)input_pool.pool,POOL_BYTES ); +blake2s_update (&state, (const u8 *)input_pool_data,POOL_BYTES );blake2s_final (&state, hash);/* final zeros out state */ /*
@@ -1427,10 +1419,9 @@ static ssize_t extract_entropy(void *buf }#define warn_unseeded_randomness (previous) \ - _warn_unseeded_randomness(__func__, (void *) _RET_IP_, (previous)) + _warn_unseeded_randomness(__func__, (void *)_RET_IP_, (previous)) -static void _warn_unseeded_randomness (const char *func_name,void *caller, -void **previous) +static void _warn_unseeded_randomness (const char *func_name,void *caller,void **previous) {#ifdef CONFIG_WARN_ALL_UNSEEDED_RANDOM const bool print_once = false;
@@ -1438,8 +1429,7 @@ static void _warn_unseeded_randomness(costatic bool print_once__read_mostly ;#endif -if (print_once || -crng_ready () || +if (print_once ||crng_ready () || (previous && (caller ==READ_ONCE (*previous))))return ;WRITE_ONCE (*previous, caller);
@@ -1447,9 +1437,8 @@ static void _warn_unseeded_randomness(co print_once = true;#endif if (__ratelimit (&unseeded_warning)) -printk_deferred (KERN_NOTICE "random: %s called from %pS " -"with crng_init=%d\n" , func_name, caller, - crng_init); +printk_deferred (KERN_NOTICE "random: %s called from %pS with crng_init=%d\n" , + func_name, caller, crng_init); }/*
@@ -1492,7 +1481,6 @@ void get_random_bytes(void *buf, int nby }EXPORT_SYMBOL (get_random_bytes); -/* * Each time the timer fires, we expect that we got an unpredictable * jump in the cycle counter. Even if the timer is running on another
@@ -1531,7 +1519,7 @@ static void try_to_generate_entropy(voidtimer_setup_on_stack (&stack.timer, entropy_timer,0 );while (!crng_ready ()) {if (!timer_pending (&stack.timer)) -mod_timer (&stack.timer, jiffies+1 ); +mod_timer (&stack.timer, jiffies +1 );mix_pool_bytes (&stack.now,sizeof (stack.now));schedule (); stack.now =random_get_entropy ();
@@ -1741,9 +1729,8 @@ void rand_initialize_disk(struct gendisk }#endif -static ssize_t -urandom_read_nowarn (struct file *file,char __user *buf,size_t nbytes, -loff_t *ppos) +static ssize_t urandom_read_nowarn (struct file *file,char __user *buf, +size_t nbytes,loff_t *ppos) {int ret;
@@ -1753,8 +1740,8 @@ urandom_read_nowarn(struct file *file, creturn ret; } -static ssize_t -urandom_read (struct file *file,char __user *buf,size_t nbytes,loff_t *ppos) +static ssize_t urandom_read (struct file *file,char __user *buf,size_t nbytes, +loff_t *ppos) {static int maxwarn =10 ;
@@ -1768,8 +1755,8 @@ urandom_read(struct file *file, char __ureturn urandom_read_nowarn (file, buf, nbytes, ppos); } -static ssize_t -random_read (struct file *file,char __user *buf,size_t nbytes,loff_t *ppos) +static ssize_t random_read (struct file *file,char __user *buf,size_t nbytes, +loff_t *ppos) {int ret;
@@ -1779,8 +1766,7 @@ random_read(struct file *file, char __usreturn urandom_read_nowarn (file, buf, nbytes, ppos); } -static __poll_t -random_poll (struct file *file,poll_table * wait) +static __poll_t random_poll (struct file *file,poll_table *wait) {__poll_t mask;
@@ -1794,8 +1780,7 @@ random_poll(struct file *file, poll_tablreturn mask; } -static int -write_pool (const char __user *buffer,size_t count) +static int write_pool (const char __user *buffer,size_t count) {size_t bytes;u32 t, buf[16 ];
@@ -1897,35 +1882,35 @@ static int random_fasync(int fd, struct }const struct file_operations random_fops = { - .read = random_read, + .read = random_read, .write = random_write, - .poll = random_poll, + .poll = random_poll, .unlocked_ioctl = random_ioctl, .fasync = random_fasync, .llseek = noop_llseek, };const struct file_operations urandom_fops = { - .read = urandom_read, + .read = urandom_read, .write = random_write, .unlocked_ioctl = random_ioctl, .fasync = random_fasync, .llseek = noop_llseek, }; -SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, - unsigned int, flags) +SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int, + flags) {int ret; -if (flags & ~(GRND_NONBLOCK |GRND_RANDOM |GRND_INSECURE )) +if (flags & ~(GRND_NONBLOCK |GRND_RANDOM |GRND_INSECURE ))return -EINVAL ;/* * Requesting insecure and blocking randomness at the same time makes * no sense. */ -if ((flags & (GRND_INSECURE |GRND_RANDOM )) == (GRND_INSECURE |GRND_RANDOM )) +if ((flags & (GRND_INSECURE |GRND_RANDOM )) == (GRND_INSECURE |GRND_RANDOM ))return -EINVAL ;if (count >INT_MAX )
@@ -2073,7 +2058,7 @@ struct ctl_table random_table[] = {#endif { } }; -#endif /* CONFIG_SYSCTL */ +#endif /* CONFIG_SYSCTL */ struct batched_entropy { union {
@@ -2093,7 +2078,7 @@ struct batched_entropy {* point prior. */ static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64) = { - .batch_lock =__SPIN_LOCK_UNLOCKED (batched_entropy_u64.lock), + .batch_lock =__SPIN_LOCK_UNLOCKED (batched_entropy_u64.lock), };u64 get_random_u64 (void )
@@ -2118,7 +2103,7 @@ u64 get_random_u64(void)EXPORT_SYMBOL (get_random_u64); static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32) = { - .batch_lock =__SPIN_LOCK_UNLOCKED (batched_entropy_u32.lock), + .batch_lock =__SPIN_LOCK_UNLOCKED (batched_entropy_u32.lock), };u32 get_random_u32 (void ) {
@@ -2150,7 +2135,7 @@ static void invalidate_batched_entropy(vint cpu;unsigned long flags; -for_each_possible_cpu (cpu) { +for_each_possible_cpu (cpu) {struct batched_entropy *batched_entropy; batched_entropy =per_cpu_ptr (&batched_entropy_u32, cpu);
@@ -2179,8 +2164,7 @@ static void invalidate_batched_entropy(v* Return: A page aligned address within [start, start + range). On error, * @start is returned. */ -unsigned long -randomize_page (unsigned long start,unsigned long range) +unsigned long randomize_page (unsigned long start,unsigned long range) {if (!PAGE_ALIGNED (start)) { range -=PAGE_ALIGN (start) - start;
Greg Kroah-Hartman
maintainerPATCH 71/234 random: simplify arithmetic function flow in account()
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit a254a0e4093fce8c832414a83940736067eed515 upstream.
Now that have_bytes is never modified, we can simplify this function. First, we move the check for negative entropy_count to be first. That ensures that subsequent reads of this will be non-negative. Then, have_bytes and ibytes can be folded into their one use site in the min_t() function.
--- drivers/char/random.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
drivers/char/random.c +6 -11
@@ -1293,7 +1293,7 @@ EXPORT_SYMBOL_GPL(add_disk_randomness);*/ static size_t account (size_t nbytes,int min) { -int entropy_count, orig, have_bytes; +int entropy_count, orig;size_t ibytes, nfrac;BUG_ON (input_pool.entropy_count >POOL_FRACBITS );
@@ -1301,20 +1301,15 @@ static size_t account(size_t nbytes, int/* Can we pull enough? */ retry: entropy_count = orig =READ_ONCE (input_pool.entropy_count); - ibytes = nbytes; -/* never pull more than available */ - have_bytes = entropy_count >> (POOL_ENTROPY_SHIFT +3 ); - -if (have_bytes <0 ) - have_bytes =0 ; - ibytes =min_t (size_t, ibytes, have_bytes); -if (ibytes < min) - ibytes =0 ; -if (WARN_ON (entropy_count <0 )) {pr_warn ("negative entropy count: count %d\n" , entropy_count); entropy_count =0 ; } + +/* never pull more than available */ + ibytes =min_t (size_t, nbytes, entropy_count >> (POOL_ENTROPY_SHIFT +3 )); +if (ibytes < min) + ibytes =0 ; nfrac = ibytes << (POOL_ENTROPY_SHIFT +3 );if ((size_t )entropy_count > nfrac) entropy_count -= nfrac;
Greg Kroah-Hartman
maintainerPATCH 38/234 linux/random.h: Remove arch_has_random, arch_has_random_seed
In reply to Greg Kroah-HartmanFrom: Richard Henderson <[email protected]>
commit 647f50d5d9d933b644b29c54f13ac52af1b1774d upstream.
The arm64 version of archrandom.h will need to be able to test for support and read the random number without preemption, so a separate query predicate is not practical.
Since this part of the generic interface is unused, remove it.
--- include/linux/random.h | 8 -------- 1 file changed, 8 deletions(-)
include/linux/random.h +0 -8
@@ -126,10 +126,6 @@ static inline bool arch_get_random_int(u {return 0 ; } -static inline bool arch_has_random (void ) -{ -return 0 ; -}static inline bool arch_get_random_seed_long (unsigned long *v) {return 0 ;
@@ -138,10 +134,6 @@ static inline bool arch_get_random_seed_ {return 0 ; } -static inline bool arch_has_random_seed (void ) -{ -return 0 ; -}#endif #endif /* _LINUX_RANDOM_H */
Greg Kroah-Hartman
maintainerPATCH 73/234 random: access primary_pool directly rather than through pointer
In reply to Greg Kroah-HartmanFrom: Dominik Brodowski <[email protected]>
commit ebf7606388732ecf2821ca21087e9446cb4a5b57 upstream.
Both crng_initialize_primary() and crng_init_try_arch_early() are only called for the primary_pool. Accessing it directly instead of through a function parameter simplifies the code.
--- drivers/char/random.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-)
drivers/char/random.c +7 -7
@@ -762,7 +762,7 @@ static bool crng_init_try_arch(struct crreturn arch_init; } -static bool __init crng_init_try_arch_early (struct crng_state *crng) +static bool __init crng_init_try_arch_early (void ) {int i;bool arch_init = true;
@@ -774,7 +774,7 @@ static bool __init crng_init_try_arch_ea rv =random_get_entropy (); arch_init = false; } - crng->state[i] ^= rv; + primary_crng.state[i] ^= rv; }return arch_init;
@@ -788,16 +788,16 @@ static void crng_initialize_secondary(st crng->init_time = jiffies -CRNG_RESEED_INTERVAL -1 ; } -static void __init crng_initialize_primary (struct crng_state *crng) +static void __init crng_initialize_primary (void ) { -_extract_entropy (&crng->state[4 ],sizeof (u32) *12 ); -if (crng_init_try_arch_early (crng) && trust_cpu && crng_init <2 ) { +_extract_entropy (&primary_crng.state[4 ],sizeof (u32) *12 ); +if (crng_init_try_arch_early () && trust_cpu && crng_init <2 ) {invalidate_batched_entropy ();numa_crng_init (); crng_init =2 ;pr_notice ("crng init done (trusting CPU's manufacturer)\n" ); } - crng->init_time = jiffies -CRNG_RESEED_INTERVAL -1 ; + primary_crng.init_time = jiffies -CRNG_RESEED_INTERVAL -1 ; }static void crng_finalize_init (struct crng_state *crng)
@@ -1698,7 +1698,7 @@ int __init rand_initialize(void)init_std_data ();if (crng_need_final_init)crng_finalize_init (&primary_crng); -crng_initialize_primary (&primary_crng); +crng_initialize_primary (); crng_global_init_time = jiffies;if (ratelimit_disable) { urandom_warning.interval =0 ;
Greg Kroah-Hartman
maintainerPATCH 74/234 random: only call crng_finalize_init() for primary_crng
In reply to Greg Kroah-HartmanFrom: Dominik Brodowski <[email protected]>
commit 9d5505f1eebeca778074a0260ed077fd85f8792c upstream.
crng_finalize_init() returns instantly if it is called for another pool than primary_crng. The test whether crng_finalize_init() is still required can be moved to the relevant caller in crng_reseed(), and crng_need_final_init can be reset to false if crng_finalize_init() is called with workqueues ready. Then, no previous callsite will call crng_finalize_init() unless it is needed, and we can get rid of the superfluous function parameter.
--- drivers/char/random.c | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-)
drivers/char/random.c +5 -5
@@ -800,10 +800,8 @@ static void __init crng_initialize_prima primary_crng.init_time = jiffies -CRNG_RESEED_INTERVAL -1 ; } -static void crng_finalize_init (struct crng_state *crng) +static void crng_finalize_init (void ) { -if (crng != &primary_crng || crng_init >=2 ) -return ;if (!system_wq) {/* We can't call numa_crng_init until we have workqueues, * so mark this for processing later. */
@@ -814,6 +812,7 @@ static void crng_finalize_init(struct crinvalidate_batched_entropy ();numa_crng_init (); crng_init =2 ; + crng_need_final_init = false;process_random_ready_list ();wake_up_interruptible (&crng_init_wait);kill_fasync (&fasync,SIGIO ,POLL_IN );
@@ -980,7 +979,8 @@ static void crng_reseed(struct crng_statmemzero_explicit (&buf,sizeof (buf));WRITE_ONCE (crng->init_time, jiffies);spin_unlock_irqrestore (&crng->lock, flags); -crng_finalize_init (crng); +if (crng == &primary_crng && crng_init <2 ) +crng_finalize_init (); }static void _extract_crng (struct crng_state *crng,u8 out[CHACHA20_BLOCK_SIZE ])
@@ -1697,7 +1697,7 @@ int __init rand_initialize(void) {init_std_data ();if (crng_need_final_init) -crng_finalize_init (&primary_crng); +crng_finalize_init ();crng_initialize_primary (); crng_global_init_time = jiffies;if (ratelimit_disable) {
Greg Kroah-Hartman
maintainerPATCH 75/234 random: use computational hash for entropy extraction
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 6e8ec2552c7d13991148e551e3325a624d73fac6 upstream.
The current 4096-bit LFSR used for entropy collection had a few desirable attributes for the context in which it was created. For example, the state was huge, which meant that /dev/random would be able to output quite a bit of accumulated entropy before blocking. It was also, in its time, quite fast at accumulating entropy byte-by-byte, which matters given the varying contexts in which mix_pool_bytes() is called. And its diffusion was relatively high, which meant that changes would ripple across several words of state rather quickly.
However, it also suffers from a few security vulnerabilities. In particular, inputs learned by an attacker can be undone, but moreover, if the state of the pool leaks, its contents can be controlled and entirely zeroed out. I've demonstrated this attack with this SMT2 script, <https://xn--4db.cc/5o9xO8pb>, which Boolector/CaDiCal solves in a matter of seconds on a single core of my laptop, resulting in little proof of concept C demonstrators such as <https://xn--4db.cc/jCkvvIaH/c>.
For basically all recent formal models of RNGs, these attacks represent a significant cryptographic flaw. But how does this manifest practically? If an attacker has access to the system to such a degree that he can learn the internal state of the RNG, arguably there are other lower hanging vulnerabilities -- side-channel, infoleak, or otherwise -- that might have higher priority. On the other hand, seed files are frequently used on systems that have a hard time generating much entropy on their own, and these seed files, being files, often leak or are duplicated and distributed accidentally, or are even seeded over the Internet intentionally, where their contents might be recorded or tampered with. Seen this way, an otherwise quasi-implausible vulnerability is a bit more practical than initially thought.
Another aspect of the current mix_pool_bytes() function is that, while its performance was arguably competitive for the time in which it was created, it's no longer considered so. This patch improves performance
significantly: on a high-end CPU, an i7-11850H, it improves performance of mix_pool_bytes() by 225%, and on a low-end CPU, a Cortex-A7, it improves performance by 103%.
This commit replaces the LFSR of mix_pool_bytes() with a straight- forward cryptographic hash function, BLAKE2s, which is already in use for pool extraction. Universal hashing with a secret seed was considered too, something along the lines of <https://eprint.iacr.org/2013/338>,
but the requirement for a secret seed makes for a chicken & egg problem. Instead we go with a formally proven scheme using a computational hash function, described in sections 5.1, 6.4, and B.1.8 of
<https://eprint.iacr.org/2019/198>.
BLAKE2s outputs 256 bits, which should give us an appropriate amount of min-entropy accumulation, and a wide enough margin of collision resistance against active attacks. mix_pool_bytes() becomes a simple call to blake2s_update(), for accumulation, while the extraction step becomes a blake2s_final() to generate a seed, with which we can then do a HKDF-like or BLAKE2X-like expansion, the first part of which we fold back as an init key for subsequent blake2s_update()s, and the rest we produce to the caller. This then is provided to our CRNG like usual. In that expansion step, we make opportunistic use of 32 bytes of RDRAND output, just as before. We also always reseed the crng with 32 bytes, unconditionally, or not at all, rather than sometimes with 16 as before, as we don't win anything by limiting beyond the 16 byte threshold.
Going for a hash function as an entropy collector is a conservative, proven approach. The result of all this is a much simpler and much less bespoke construction than what's there now, which not only plugs a vulnerability but also improves performance considerably.
--- drivers/char/random.c | 304 +++++++++----------------------------------------- 1 file changed, 55 insertions(+), 249 deletions(-)
drivers/char/random.c +55 -249
@@ -42,61 +42,6 @@*/ /* -* (now, with legal B.S. out of the way.....) -* -* This routine gathers environmental noise from device drivers, etc., -* and returns good random numbers, suitable for cryptographic use. -* Besides the obvious cryptographic uses, these numbers are also good -* for seeding TCP sequence numbers, and other places where it is -* desirable to have numbers which are not only random, but hard to -* predict by an attacker. -* -* Theory of operation -* =================== -* -* Computers are very predictable devices. Hence it is extremely hard -* to produce truly random numbers on a computer --- as opposed to -* pseudo-random numbers, which can easily generated by using a -* algorithm. Unfortunately, it is very easy for attackers to guess -* the sequence of pseudo-random number generators, and for some -* applications this is not acceptable. So instead, we must try to -* gather "environmental noise" from the computer's environment, which -* must be hard for outside attackers to observe, and use that to -* generate random numbers. In a Unix environment, this is best done -* from inside the kernel. -* -* Sources of randomness from the environment include inter-keyboard -* timings, inter-interrupt timings from some interrupts, and other -* events which are both (a) non-deterministic and (b) hard for an -* outside observer to measure. Randomness from these sources are -* added to an "entropy pool", which is mixed using a CRC-like function. -* This is not cryptographically strong, but it is adequate assuming -* the randomness is not chosen maliciously, and it is fast enough that -* the overhead of doing it on every interrupt is very reasonable. -* As random bytes are mixed into the entropy pool, the routines keep -* an *estimate* of how many bits of randomness have been stored into -* the random number generator's internal state. -* -* When random bytes are desired, they are obtained by taking the BLAKE2s -* hash of the contents of the "entropy pool". The BLAKE2s hash avoids -* exposing the internal state of the entropy pool. It is believed to -* be computationally infeasible to derive any useful information -* about the input of BLAKE2s from its output. Even if it is possible to -* analyze BLAKE2s in some clever way, as long as the amount of data -* returned from the generator is less than the inherent entropy in -* the pool, the output data is totally unpredictable. For this -* reason, the routine decreases its internal estimate of how many -* bits of "true randomness" are contained in the entropy pool as it -* outputs random numbers. -* -* If this estimate goes to zero, the routine can still generate -* random numbers; however, an attacker may (at least in theory) be -* able to infer the future output of the generator from prior -* outputs. This requires successful cryptanalysis of BLAKE2s, which is -* not believed to be feasible, but there is a remote possibility. -* Nonetheless, these numbers should be useful for the vast majority -* of purposes. -* * Exported interfaces ---- output * =============================== *
@@ -298,23 +243,6 @@* * mknod /dev/random c 1 8 * mknod /dev/urandom c 1 9 -* -* Acknowledgements: -* ================= -* -* Ideas for constructing this random number generator were derived -* from Pretty Good Privacy's random number generator, and from private -* discussions with Phil Karn. Colin Plumb provided a faster random -* number generator, which speed up the mixing function of the entropy -* pool, taken from PGPfone. Dale Worley has also contributed many -* useful ideas and suggestions to improve this driver. -* -* Any flaws in the design are solely my responsibility, and should -* not be attributed to the Phil, Colin, or any of authors of PGP. -* -* Further background information on this topic may be obtained from -* RFC 1750, "Randomness Recommendations for Security", by Donald -* Eastlake, Steve Crocker, and Jeff Schiller. */ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt
@@ -358,79 +286,15 @@/* #define ADD_INTERRUPT_BENCH */ -/* -* If the entropy count falls under this number of bits, then we -* should wake up processes which are selecting or polling on write -* access to /dev/random. -*/ -static int random_write_wakeup_bits =28 * (1 <<5 ); - -/* -* Originally, we used a primitive polynomial of degree .poolwords -* over GF(2). The taps for various sizes are defined below. They -* were chosen to be evenly spaced except for the last tap, which is 1 -* to get the twisting happening as fast as possible. -* -* For the purposes of better mixing, we use the CRC-32 polynomial as -* well to make a (modified) twisted Generalized Feedback Shift -* Register. (See M. Matsumoto & Y. Kurita, 1992. Twisted GFSR -* generators. ACM Transactions on Modeling and Computer Simulation -* 2(3):179-194. Also see M. Matsumoto & Y. Kurita, 1994. Twisted -* GFSR generators II. ACM Transactions on Modeling and Computer -* Simulation 4:254-266) -* -* Thanks to Colin Plumb for suggesting this. -* -* The mixing operation is much less sensitive than the output hash, -* where we use BLAKE2s. All that we want of mixing operation is that -* it be a good non-cryptographic hash; i.e. it not produce collisions -* when fed "random" data of the sort we expect to see. As long as -* the pool state differs for different inputs, we have preserved the -* input entropy and done a good job. The fact that an intelligent -* attacker can construct inputs that will produce controlled -* alterations to the pool's state is not important because we don't -* consider such inputs to contribute any randomness. The only -* property we need with respect to them is that the attacker can't -* increase his/her knowledge of the pool's state. Since all -* additions are reversible (knowing the final state and the input, -* you can reconstruct the initial state), if an attacker has any -* uncertainty about the initial state, he/she can only shuffle that -* uncertainty about, but never cause any collisions (which would -* decrease the uncertainty). -* -* Our mixing functions were analyzed by Lacharme, Roeck, Strubel, and -* Videau in their paper, "The Linux Pseudorandom Number Generator -* Revisited" (see: http://eprint.iacr.org/2012/251.pdf). In their -* paper, they point out that we are not using a true Twisted GFSR, -* since Matsumoto & Kurita used a trinomial feedback polynomial (that -* is, with only three taps, instead of the six that we are using). -* As a result, the resulting polynomial is neither primitive nor -* irreducible, and hence does not have a maximal period over -* GF(2**32). They suggest a slight change to the generator -* polynomial which improves the resulting TGFSR polynomial to be -* irreducible, which we have made here. -*/ enum poolinfo { -POOL_WORDS =128 , -POOL_WORDMASK =POOL_WORDS -1 , -POOL_BYTES =POOL_WORDS *sizeof (u32), -POOL_BITS =POOL_BYTES *8 , +POOL_BITS =BLAKE2S_HASH_SIZE *8 ,POOL_BITSHIFT =ilog2 (POOL_BITS ),/* To allow fractional bits to be tracked, the entropy_count field is * denominated in units of 1/8th bits. */ POOL_ENTROPY_SHIFT =3 ,#define POOL_ENTROPY_BITS() (input_pool.entropy_count >> POOL_ENTROPY_SHIFT) - POOL_FRACBITS = POOL_BITS << POOL_ENTROPY_SHIFT, - -/* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */ -POOL_TAP1 =104 , -POOL_TAP2 =76 , -POOL_TAP3 =51 , -POOL_TAP4 =25 , -POOL_TAP5 =1 , - -EXTRACT_SIZE =BLAKE2S_HASH_SIZE /2 + POOL_FRACBITS = POOL_BITS << POOL_ENTROPY_SHIFT };/*
@@ -438,6 +302,12 @@ enum poolinfo {*/ static DECLARE_WAIT_QUEUE_HEAD (random_write_wait);static struct fasync_struct *fasync; +/* +* If the entropy count falls under this number of bits, then we +* should wake up processes which are selecting or polling on write +* access to /dev/random. +*/ +static int random_write_wakeup_bits =POOL_BITS *3 /4 ;static DEFINE_SPINLOCK (random_ready_list_lock);static LIST_HEAD (random_ready_list);
@@ -493,73 +363,31 @@ MODULE_PARM_DESC(ratelimit_disable, "Dis* **********************************************************************/ -static u32 input_pool_data[POOL_WORDS] __latent_entropy; -static struct { +struct blake2s_state hash;spinlock_t lock; -u16 add_ptr; -u16 input_rotate;int entropy_count; } input_pool = { + .hash.h = {BLAKE2S_IV0 ^ (0x01010000 |BLAKE2S_HASH_SIZE ), +BLAKE2S_IV1 ,BLAKE2S_IV2 ,BLAKE2S_IV3 ,BLAKE2S_IV4 , +BLAKE2S_IV5 ,BLAKE2S_IV6 ,BLAKE2S_IV7 }, + .hash.outlen =BLAKE2S_HASH_SIZE , .lock =__SPIN_LOCK_UNLOCKED (input_pool.lock), }; -static ssize_t extract_entropy (void *buf,size_t nbytes,int min); -static ssize_t _extract_entropy (void *buf,size_t nbytes); +static bool extract_entropy (void *buf,size_t nbytes,int min); +static void _extract_entropy (void *buf,size_t nbytes);static void crng_reseed (struct crng_state *crng,bool use_input_pool); -static const u32 twist_table[8 ] = { -0x00000000 ,0x3b6e20c8 ,0x76dc4190 ,0x4db26158 , -0xedb88320 ,0xd6d6a3e8 ,0x9b64c2b0 ,0xa00ae278 }; -/* * This function adds bytes into the entropy "pool". It does not * update the entropy estimate. The caller should call * credit_entropy_bits if this is appropriate. -* -* The pool is stirred with a primitive polynomial of the appropriate -* degree, and then twisted. We twist by three bits at a time because -* it's cheap to do so and helps slightly in the expected case where -* the entropy is concentrated in the low-order bits. */ static void _mix_pool_bytes (const void *in,int nbytes) { -unsigned long i; -int input_rotate; -const u8 *bytes = in; -u32 w; - - input_rotate = input_pool.input_rotate; - i = input_pool.add_ptr; - -/* mix one byte at a time to simplify size handling and churn faster */ -while (nbytes--) { - w =rol32 (*bytes++, input_rotate); - i = (i -1 ) &POOL_WORDMASK ; - -/* XOR in the various taps */ - w ^= input_pool_data[i]; - w ^= input_pool_data[(i +POOL_TAP1 ) &POOL_WORDMASK ]; - w ^= input_pool_data[(i +POOL_TAP2 ) &POOL_WORDMASK ]; - w ^= input_pool_data[(i +POOL_TAP3 ) &POOL_WORDMASK ]; - w ^= input_pool_data[(i +POOL_TAP4 ) &POOL_WORDMASK ]; - w ^= input_pool_data[(i +POOL_TAP5 ) &POOL_WORDMASK ]; - -/* Mix the result back in with a twist */ - input_pool_data[i] = (w >>3 ) ^ twist_table[w &7 ]; - -/* -* Normally, we add 7 bits of rotation to the pool. -* At the beginning of the pool, add an extra 7 bits -* rotation, so that successive passes spread the -* input bits across the pool evenly. -*/ - input_rotate = (input_rotate + (i ?7 :14 )) &31 ; - } - - input_pool.input_rotate = input_rotate; - input_pool.add_ptr = i; +blake2s_update (&input_pool.hash, in, nbytes); }static void __mix_pool_bytes (const void *in,int nbytes)
@@ -953,15 +781,14 @@ static int crng_slow_load(const u8 *cp,static void crng_reseed (struct crng_state *crng,bool use_input_pool) {unsigned long flags; -int i, num; +int i;union {u8 block[CHACHA20_BLOCK_SIZE ];u32 key[8 ]; } buf;if (use_input_pool) { - num =extract_entropy (&buf,32 ,16 ); -if (num ==0 ) +if (!extract_entropy (&buf,32 ,16 ))return ; }else {_extract_crng (&primary_crng, buf.block);
@@ -1329,74 +1156,48 @@ retry: }/* -* This function does the actual extraction for extract_entropy. -* -* Note: we assume that .poolwords is a multiple of 16 words. +* This is an HKDF-like construction for using the hashed collected entropy +* as a PRF key, that's then expanded block-by-block. */ -static void extract_buf (u8 *out) +static void _extract_entropy (void *buf,size_t nbytes) { -struct blake2s_state state__aligned(__alignof__(unsigned long)) ; -u8 hash[BLAKE2S_HASH_SIZE ]; -unsigned long *salt;unsigned long flags; - -blake2s_init (&state,sizeof (hash)); - -/* -* If we have an architectural hardware random number -* generator, use it for BLAKE2's salt & personal fields. -*/ -for (salt = (unsigned long *)&state.h[4 ]; - salt < (unsigned long *)&state.h[8 ]; ++salt) { -unsigned long v; -if (!arch_get_random_long (&v)) -break ; - *salt ^= v; +u8 seed[BLAKE2S_HASH_SIZE ], next_key[BLAKE2S_HASH_SIZE ]; +struct { +unsigned long rdrand[32 /sizeof (long )]; +size_t counter; + } block; +size_t i; + +for (i =0 ; i <ARRAY_SIZE (block.rdrand); ++i) { +if (!arch_get_random_long (&block.rdrand[i])) + block.rdrand[i] =random_get_entropy (); } -/* Generate a hash across the pool */ spin_lock_irqsave (&input_pool.lock, flags); -blake2s_update (&state, (const u8 *)input_pool_data,POOL_BYTES ); -blake2s_final (&state, hash);/* final zeros out state */ -/* -* We mix the hash back into the pool to prevent backtracking -* attacks (where the attacker knows the state of the pool -* plus the current outputs, and attempts to find previous -* outputs), unless the hash function can be inverted. By -* mixing at least a hash worth of hash data back, we make -* brute-forcing the feedback as hard as brute-forcing the -* hash. -*/ -__mix_pool_bytes (hash,sizeof (hash)); -spin_unlock_irqrestore (&input_pool.lock, flags); +/* seed = HASHPRF(last_key, entropy_input) */ +blake2s_final (&input_pool.hash, seed); -/* Note that EXTRACT_SIZE is half of hash size here, because above -* we've dumped the full length back into mixer. By reducing the -* amount that we emit, we retain a level of forward secrecy. -*/ -memcpy (out, hash,EXTRACT_SIZE ); -memzero_explicit (hash,sizeof (hash)); -} +/* next_key = HASHPRF(seed, RDRAND || 0) */ + block.counter =0 ; +blake2s (next_key, (u8 *)&block, seed,sizeof (next_key),sizeof (block),sizeof (seed)); +blake2s_init_key (&input_pool.hash,BLAKE2S_HASH_SIZE , next_key,sizeof (next_key)); -static ssize_t _extract_entropy (void *buf,size_t nbytes) -{ -ssize_t ret =0 , i; -u8 tmp[EXTRACT_SIZE ]; +spin_unlock_irqrestore (&input_pool.lock, flags); +memzero_explicit (next_key,sizeof (next_key));while (nbytes) { -extract_buf (tmp); - i =min_t (int, nbytes,EXTRACT_SIZE ); -memcpy (buf, tmp, i); + i =min_t (size_t, nbytes,BLAKE2S_HASH_SIZE ); +/* output = HASHPRF(seed, RDRAND || ++counter) */ + ++block.counter; +blake2s (buf, (u8 *)&block, seed, i,sizeof (block),sizeof (seed)); nbytes -= i; buf += i; - ret += i; } -/* Wipe data just returned from memory */ -memzero_explicit (tmp,sizeof (tmp)); - -return ret; +memzero_explicit (seed,sizeof (seed)); +memzero_explicit (&block,sizeof (block)); }/*
@@ -1404,13 +1205,18 @@ static ssize_t _extract_entropy(void *bu* returns it in a buffer. * * The min parameter specifies the minimum amount we can pull before -* failing to avoid races that defeat catastrophic reseeding. +* failing to avoid races that defeat catastrophic reseeding. If we +* have less than min entropy available, we return false and buf is +* not filled. */ -static ssize_t extract_entropy (void *buf,size_t nbytes,int min) +static bool extract_entropy (void *buf,size_t nbytes,int min) {trace_extract_entropy (nbytes,POOL_ENTROPY_BITS (), _RET_IP_); - nbytes =account (nbytes, min); -return _extract_entropy (buf, nbytes); +if (account (nbytes, min)) { +_extract_entropy (buf, nbytes); +return true; + } +return false; }#define warn_unseeded_randomness (previous) \
@@ -1674,7 +1480,7 @@ static void __init init_std_data(void)unsigned long rv;mix_pool_bytes (&now,sizeof (now)); -for (i =POOL_BYTES ; i >0 ; i -=sizeof (rv)) { +for (i =BLAKE2S_BLOCK_SIZE ; i >0 ; i -=sizeof (rv)) {if (!arch_get_random_seed_long (&rv) && !arch_get_random_long (&rv)) rv =random_get_entropy ();
Greg Kroah-Hartman
maintainerPATCH 76/234 random: simplify entropy debiting
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 9c07f57869e90140080cfc282cc628d123e27704 upstream.
Our pool is 256 bits, and we only ever use all of it or don't use it at all, which is decided by whether or not it has at least 128 bits in it. So we can drastically simplify the accounting and cmpxchg loop to do exactly this. While we're at it, we move the minimum bit size into a constant so it can be shared between the two places where it matters.
The reason we want any of this is for the case in which an attacker has compromised the current state, and then bruteforces small amounts of entropy added to it. By demanding a particular minimum amount of entropy be present before reseeding, we make that bruteforcing difficult.
Note that this rationale no longer includes anything about /dev/random blocking at the right moment, since /dev/random no longer blocks (except for at ~boot), but rather uses the crng. In a former life, /dev/random was different and therefore required a more nuanced account(), but this is no longer.
Behaviorally, nothing changes here. This is just a simplification of the code.
--- drivers/char/random.c | 91 +++++++++--------------------------------- include/trace/events/random.h | 30 ++----------- 2 files changed, 27 insertions(+), 94 deletions(-)
drivers/char/random.c +21 -70
@@ -289,12 +289,14 @@enum poolinfo {POOL_BITS =BLAKE2S_HASH_SIZE *8 ,POOL_BITSHIFT =ilog2 (POOL_BITS ), +POOL_MIN_BITS =POOL_BITS /2 ,/* To allow fractional bits to be tracked, the entropy_count field is * denominated in units of 1/8th bits. */ POOL_ENTROPY_SHIFT =3 ,#define POOL_ENTROPY_BITS() (input_pool.entropy_count >> POOL_ENTROPY_SHIFT) - POOL_FRACBITS = POOL_BITS << POOL_ENTROPY_SHIFT + POOL_FRACBITS = POOL_BITS << POOL_ENTROPY_SHIFT, +POOL_MIN_FRACBITS =POOL_MIN_BITS <<POOL_ENTROPY_SHIFT };/*
@@ -375,8 +377,7 @@ static struct { .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock), }; -static bool extract_entropy (void *buf,size_t nbytes,int min); -static void _extract_entropy (void *buf,size_t nbytes); +static void extract_entropy (void *buf,size_t nbytes);static void crng_reseed (struct crng_state *crng,bool use_input_pool);
@@ -467,7 +468,7 @@ static void process_random_ready_list(vo*/ static void credit_entropy_bits (int nbits) { -int entropy_count, entropy_bits, orig; +int entropy_count, orig;int nfrac = nbits <<POOL_ENTROPY_SHIFT ;/* Ensure that the multiplication can avoid being 64 bits wide. */
@@ -527,8 +528,7 @@ retry:trace_credit_entropy_bits (nbits, entropy_count >>POOL_ENTROPY_SHIFT , _RET_IP_); - entropy_bits = entropy_count >>POOL_ENTROPY_SHIFT ; -if (crng_init <2 && entropy_bits >=128 ) +if (crng_init <2 && entropy_count >=POOL_MIN_FRACBITS )crng_reseed (&primary_crng, true); }
@@ -618,7 +618,7 @@ static void crng_initialize_secondary(ststatic void __init crng_initialize_primary (void ) { -_extract_entropy (&primary_crng.state[4 ],sizeof (u32) *12 ); +extract_entropy (&primary_crng.state[4 ],sizeof (u32) *12 );if (crng_init_try_arch_early () && trust_cpu && crng_init <2 ) {invalidate_batched_entropy ();numa_crng_init ();
@@ -788,8 +788,17 @@ static void crng_reseed(struct crng_stat } buf;if (use_input_pool) { -if (!extract_entropy (&buf,32 ,16 )) -return ; +int entropy_count; +do { + entropy_count =READ_ONCE (input_pool.entropy_count); +if (entropy_count <POOL_MIN_FRACBITS ) +return ; + }while (cmpxchg (&input_pool.entropy_count, entropy_count,0 ) != entropy_count); +extract_entropy (buf.key,sizeof (buf.key)); +if (random_write_wakeup_bits) { +wake_up_interruptible (&random_write_wait); +kill_fasync (&fasync,SIGIO ,POLL_OUT ); + } }else {_extract_crng (&primary_crng, buf.block); _crng_backtrack_protect(&primary_crng, buf.block,
@@ -1115,51 +1124,10 @@ EXPORT_SYMBOL_GPL(add_disk_randomness);*********************************************************************/ /* -* This function decides how many bytes to actually take from the -* given pool, and also debits the entropy count accordingly. -*/ -static size_t account (size_t nbytes,int min) -{ -int entropy_count, orig; -size_t ibytes, nfrac; - -BUG_ON (input_pool.entropy_count >POOL_FRACBITS ); - -/* Can we pull enough? */ -retry: - entropy_count = orig =READ_ONCE (input_pool.entropy_count); -if (WARN_ON (entropy_count <0 )) { -pr_warn ("negative entropy count: count %d\n" , entropy_count); - entropy_count =0 ; - } - -/* never pull more than available */ - ibytes =min_t (size_t, nbytes, entropy_count >> (POOL_ENTROPY_SHIFT +3 )); -if (ibytes < min) - ibytes =0 ; - nfrac = ibytes << (POOL_ENTROPY_SHIFT +3 ); -if ((size_t )entropy_count > nfrac) - entropy_count -= nfrac; -else - entropy_count =0 ; - -if (cmpxchg (&input_pool.entropy_count, orig, entropy_count) != orig) - goto retry; - -trace_debit_entropy (8 * ibytes); -if (ibytes &&POOL_ENTROPY_BITS () < random_write_wakeup_bits) { -wake_up_interruptible (&random_write_wait); -kill_fasync (&fasync,SIGIO ,POLL_OUT ); - } - -return ibytes; -} - -/* * This is an HKDF-like construction for using the hashed collected entropy * as a PRF key, that's then expanded block-by-block. */ -static void _extract_entropy (void *buf,size_t nbytes) +static void extract_entropy (void *buf,size_t nbytes) {unsigned long flags;u8 seed[BLAKE2S_HASH_SIZE ], next_key[BLAKE2S_HASH_SIZE ];
@@ -1169,6 +1137,8 @@ static void _extract_entropy(void *buf, } block;size_t i; +trace_extract_entropy (nbytes,POOL_ENTROPY_BITS ()); +for (i =0 ; i <ARRAY_SIZE (block.rdrand); ++i) {if (!arch_get_random_long (&block.rdrand[i])) block.rdrand[i] =random_get_entropy ();
@@ -1200,25 +1170,6 @@ static void _extract_entropy(void *buf,memzero_explicit (&block,sizeof (block)); } -/* -* This function extracts randomness from the "entropy pool", and -* returns it in a buffer. -* -* The min parameter specifies the minimum amount we can pull before -* failing to avoid races that defeat catastrophic reseeding. If we -* have less than min entropy available, we return false and buf is -* not filled. -*/ -static bool extract_entropy (void *buf,size_t nbytes,int min) -{ -trace_extract_entropy (nbytes,POOL_ENTROPY_BITS (), _RET_IP_); -if (account (nbytes, min)) { -_extract_entropy (buf, nbytes); -return true; - } -return false; -} -#define warn_unseeded_randomness (previous) \ _warn_unseeded_randomness(__func__, (void *)_RET_IP_, (previous))
include/trace/events/random.h +6 -24
@@ -79,22 +79,6 @@ TRACE_EVENT(credit_entropy_bits, __entry->bits, __entry->entropy_count, (void *)__entry->IP) ); -TRACE_EVENT (debit_entropy, - TP_PROTO(int debit_bits), - -TP_ARGS ( debit_bits), - -TP_STRUCT__entry ( -__field ( int, debit_bits ) - ), - -TP_fast_assign ( - __entry->debit_bits = debit_bits; - ), - -TP_printk ("input pool: debit_bits %d" , __entry->debit_bits) -); - TRACE_EVENT(add_input_randomness, TP_PROTO(int input_bits),
@@ -161,31 +145,29 @@ DEFINE_EVENT(random__get_random_bytes, g );DECLARE_EVENT_CLASS (random__extract_entropy, - TP_PROTO(int nbytes, int entropy_count, unsigned long IP), + TP_PROTO(int nbytes, int entropy_count), -TP_ARGS (nbytes, entropy_count,IP ), +TP_ARGS (nbytes, entropy_count),TP_STRUCT__entry ( __field( int, nbytes )__field ( int, entropy_count ) - __field(unsigned long, IP ) ),TP_fast_assign ( __entry->nbytes = nbytes; __entry->entropy_count = entropy_count; - __entry->IP = IP; ), -TP_printk ("input pool: nbytes %d entropy_count %d caller %pS" , - __entry->nbytes, __entry->entropy_count, (void *)__entry->IP) +TP_printk ("input pool: nbytes %d entropy_count %d" , + __entry->nbytes, __entry->entropy_count) );DEFINE_EVENT (random__extract_entropy, extract_entropy, - TP_PROTO(int nbytes, int entropy_count, unsigned long IP), + TP_PROTO(int nbytes, int entropy_count), -TP_ARGS (nbytes, entropy_count,IP ) +TP_ARGS (nbytes, entropy_count) ); TRACE_EVENT(urandom_read,
Greg Kroah-Hartman
maintainerPATCH 72/234 random: continually use hwgenerator randomness
In reply to Greg Kroah-HartmanFrom: Dominik Brodowski <[email protected]>
commit c321e907aa4803d562d6e70ebed9444ad082f953 upstream.
The rngd kernel thread may sleep indefinitely if the entropy count is kept above random_write_wakeup_bits by other entropy sources. To make best use of multiple sources of randomness, mix entropy from hardware RNGs into the pool at least once within CRNG_RESEED_INTERVAL.
--- drivers/char/random.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-)
drivers/char/random.c +6 -4
@@ -2193,13 +2193,15 @@ void add_hwgenerator_randomness(const chreturn ; } -/* Suspend writing if we're above the trickle threshold. +/* Throttle writing if we're above the trickle threshold. * We'll be woken up again once below random_write_wakeup_thresh, -* or when the calling thread is about to terminate. +* when the calling thread is about to terminate, or once +* CRNG_RESEED_INTERVAL has lapsed. */ -wait_event_interruptible (random_write_wait, +wait_event_interruptible_timeout (random_write_wait, !system_wq ||kthread_should_stop () || -POOL_ENTROPY_BITS () <= random_write_wakeup_bits); +POOL_ENTROPY_BITS () <= random_write_wakeup_bits, +CRNG_RESEED_INTERVAL );mix_pool_bytes (buffer, count);credit_entropy_bits (entropy); }
Greg Kroah-Hartman
maintainerPATCH 40/234 linux/random.h: Mark CONFIG_ARCH_RANDOM functions __must_check
In reply to Greg Kroah-HartmanFrom: Richard Henderson <[email protected]>
commit 904caa6413c87aacbf7d0682da617c39ca18cf1a upstream.
We must not use the pointer output without validating the success of the random read.
--- include/linux/random.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/random.h +4 -4
@@ -118,19 +118,19 @@ unsigned long randomize_page(unsigned lo#ifdef CONFIG_ARCH_RANDOM # include <asm/archrandom.h> #else -static inline bool arch_get_random_long (unsigned long *v) +static inline bool __must_check arch_get_random_long (unsigned long *v) {return false; } -static inline bool arch_get_random_int (unsigned int *v) +static inline bool __must_check arch_get_random_int (unsigned int *v) {return false; } -static inline bool arch_get_random_seed_long (unsigned long *v) +static inline bool __must_check arch_get_random_seed_long (unsigned long *v) {return false; } -static inline bool arch_get_random_seed_int (unsigned int *v) +static inline bool __must_check arch_get_random_seed_int (unsigned int *v) {return false; }
Greg Kroah-Hartman
maintainerPATCH 68/234 random: prepend remaining pool constants with POOL_
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit b3d51c1f542113342ddfbf6007e38a684b9dbec9 upstream.
The other pool constants are prepended with POOL_, but not these last ones. Rename them. This will then let us move them into the enum in the following commit.
--- drivers/char/random.c | 40 ++++++++++++++++++++-------------------- 1 file changed, 20 insertions(+), 20 deletions(-)
drivers/char/random.c +20 -20
@@ -362,11 +362,11 @@* To allow fractional bits to be tracked, the entropy_count field is * denominated in units of 1/8th bits. * -* 2*(ENTROPY_SHIFT + poolbitshift) must <= 31, or the multiply in +* 2*(POOL_ENTROPY_SHIFT + poolbitshift) must <= 31, or the multiply in * credit_entropy_bits() needs to be 64 bits wide. */ -#define ENTROPY_SHIFT 3 -#define ENTROPY_BITS () (input_pool.entropy_count >> ENTROPY_SHIFT) +#define POOL_ENTROPY_SHIFT 3 +#define POOL_ENTROPY_BITS () (input_pool.entropy_count >> POOL_ENTROPY_SHIFT)/* * If the entropy count falls under this number of bits, then we
@@ -426,7 +426,7 @@ enum poolinfo {POOL_BYTES =POOL_WORDS *sizeof (u32),POOL_BITS =POOL_BYTES *8 ,POOL_BITSHIFT =ilog2 (POOL_WORDS ) +5 , -POOL_FRACBITS =POOL_WORDS << (ENTROPY_SHIFT +5 ), +POOL_FRACBITS =POOL_WORDS << (POOL_ENTROPY_SHIFT +5 ),/* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */ POOL_TAP1 = 104,
@@ -650,7 +650,7 @@ static void process_random_ready_list(vostatic void credit_entropy_bits (int nbits) {int entropy_count, entropy_bits, orig; -int nfrac = nbits <<ENTROPY_SHIFT ; +int nfrac = nbits <<POOL_ENTROPY_SHIFT ;if (!nbits)return ;
@@ -683,7 +683,7 @@ retry:* turns no matter how large nbits is. */ int pnfrac = nfrac; -const int s =POOL_BITSHIFT +ENTROPY_SHIFT +2 ; +const int s =POOL_BITSHIFT +POOL_ENTROPY_SHIFT +2 ;/* The +2 corresponds to the /4 in the denominator */ do {
@@ -704,9 +704,9 @@ retry:if (cmpxchg (&input_pool.entropy_count, orig, entropy_count) != orig) goto retry; -trace_credit_entropy_bits (nbits, entropy_count >>ENTROPY_SHIFT , _RET_IP_); +trace_credit_entropy_bits (nbits, entropy_count >>POOL_ENTROPY_SHIFT , _RET_IP_); - entropy_bits = entropy_count >>ENTROPY_SHIFT ; + entropy_bits = entropy_count >>POOL_ENTROPY_SHIFT ;if (crng_init <2 && entropy_bits >=128 )crng_reseed (&primary_crng, true); }
@@ -1187,7 +1187,7 @@ void add_input_randomness(unsigned int t last_value = value;add_timer_randomness (&input_timer_state, (type <<4 ) ^ code ^ (code >>4 ) ^ value); -trace_add_input_randomness (ENTROPY_BITS ()); +trace_add_input_randomness (POOL_ENTROPY_BITS ()); }EXPORT_SYMBOL_GPL (add_input_randomness);
@@ -1286,7 +1286,7 @@ void add_disk_randomness(struct gendiskreturn ;/* first major is 1, so we get >= 0x200 here */ add_timer_randomness (disk->random,0x100 +disk_devt (disk)); - trace_add_disk_randomness(disk_devt(disk), ENTROPY_BITS()); + trace_add_disk_randomness(disk_devt(disk), POOL_ENTROPY_BITS()); }EXPORT_SYMBOL_GPL (add_disk_randomness);#endif
@@ -1313,7 +1313,7 @@ retry: entropy_count = orig =READ_ONCE (input_pool.entropy_count); ibytes = nbytes;/* never pull more than available */ - have_bytes = entropy_count >> (ENTROPY_SHIFT +3 ); + have_bytes = entropy_count >> (POOL_ENTROPY_SHIFT +3 );if (have_bytes <0 ) have_bytes =0 ;
@@ -1325,7 +1325,7 @@ retry:pr_warn ("negative entropy count: count %d\n" , entropy_count); entropy_count =0 ; } - nfrac = ibytes << (ENTROPY_SHIFT +3 ); + nfrac = ibytes << (POOL_ENTROPY_SHIFT +3 );if ((size_t ) entropy_count > nfrac) entropy_count -= nfrac;else
@@ -1335,7 +1335,7 @@ retry: goto retry;trace_debit_entropy (8 * ibytes); -if (ibytes &&ENTROPY_BITS () < random_write_wakeup_bits) { +if (ibytes &&POOL_ENTROPY_BITS () < random_write_wakeup_bits) {wake_up_interruptible (&random_write_wait);kill_fasync (&fasync,SIGIO ,POLL_OUT ); }
@@ -1423,7 +1423,7 @@ static ssize_t _extract_entropy(void *bu*/ static ssize_t extract_entropy (void *buf,size_t nbytes,int min) { -trace_extract_entropy (nbytes,ENTROPY_BITS (), _RET_IP_); +trace_extract_entropy (nbytes,POOL_ENTROPY_BITS (), _RET_IP_); nbytes =account (nbytes, min);return _extract_entropy (buf, nbytes); }
@@ -1749,9 +1749,9 @@ urandom_read_nowarn(struct file *file, c {int ret; - nbytes =min_t (size_t, nbytes,INT_MAX >> (ENTROPY_SHIFT +3 )); + nbytes =min_t (size_t, nbytes,INT_MAX >> (POOL_ENTROPY_SHIFT +3 )); ret =extract_crng_user (buf, nbytes); -trace_urandom_read (8 * nbytes,0 ,ENTROPY_BITS ()); +trace_urandom_read (8 * nbytes,0 ,POOL_ENTROPY_BITS ());return ret; }
@@ -1791,7 +1791,7 @@ random_poll(struct file *file, poll_tabl mask =0 ;if (crng_ready ()) mask |=EPOLLIN |EPOLLRDNORM ; -if (ENTROPY_BITS () < random_write_wakeup_bits) +if (POOL_ENTROPY_BITS () < random_write_wakeup_bits) mask |=EPOLLOUT |EPOLLWRNORM ;return mask; }
@@ -1847,7 +1847,7 @@ static long random_ioctl(struct file *f,switch (cmd) {case RNDGETENTCNT :/* inherently racy, no point locking */ - ent_count =ENTROPY_BITS (); + ent_count =POOL_ENTROPY_BITS ();if (put_user (ent_count, p))return -EFAULT ;return 0 ;
@@ -2003,7 +2003,7 @@ static int proc_do_entropy(struct ctl_tastruct ctl_table fake_table;int entropy_count; - entropy_count = *(int *)table->data >>ENTROPY_SHIFT ; + entropy_count = *(int *)table->data >>POOL_ENTROPY_SHIFT ; fake_table.data = &entropy_count; fake_table.maxlen =sizeof (entropy_count);
@@ -2222,7 +2222,7 @@ void add_hwgenerator_randomness(const ch*/ wait_event_interruptible (random_write_wait, !system_wq ||kthread_should_stop () || -ENTROPY_BITS () <= random_write_wakeup_bits); +POOL_ENTROPY_BITS () <= random_write_wakeup_bits);mix_pool_bytes (buffer, count);credit_entropy_bits (entropy); }
Greg Kroah-Hartman
maintainerPATCH 77/234 random: use linear min-entropy accumulation crediting
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit c570449094844527577c5c914140222cb1893e3f upstream.
30e37ec516ae ("random: account for entropy loss due to overwrites") assumed that adding new entropy to the LFSR pool probabilistically cancelled out old entropy there, so entropy was credited asymptotically, approximating Shannon entropy of independent sources (rather than a stronger min-entropy notion) using 1/8th fractional bits and replacing a constant 2-2/√𝑒 term (~0.786938) with 3/4 (0.75) to slightly underestimate it. This wasn't superb, but it was perhaps better than nothing, so that's what was done. Which entropy specifically was being cancelled out and how much precisely each time is hard to tell, though as I showed with the attack code in my previous commit, a motivated adversary with sufficient information can actually cancel out everything.
Since we're no longer using an LFSR for entropy accumulation, this probabilistic cancellation is no longer relevant. Rather, we're now using a computational hash function as the accumulator and we've switched to working in the random oracle model, from which we can now revisit the question of min-entropy accumulation, which is done in detail in <https://eprint.iacr.org/2019/198>.
Consider a long input bit string that is built by concatenating various smaller independent input bit strings. Each one of these inputs has a designated min-entropy, which is what we're passing to credit_entropy_bits(h). When we pass the concatenation of these to a random oracle, it means that an adversary trying to receive back the same reply as us would need to become certain about each part of the concatenated bit string we passed in, which means becoming certain about all of those h values. That means we can estimate the accumulation by simply adding up the h values in calls to credit_entropy_bits(h); there's no probabilistic cancellation at play like there was said to be for the LFSR. Incidentally, this is also what other entropy accumulators based on computational hash functions do as well.
So this commit replaces credit_entropy_bits(h) with essentially total = min(POOL_BITS, total + h), done with a cmpxchg loop as before.
What if we're wrong and the above is nonsense? It's not, but let's assume we don't want the actual _behavior_ of the code to change much. Currently that behavior is not extracting from the input pool until it has 128 bits of entropy in it. With the old algorithm, we'd hit that magic 128 number after roughly 256 calls to credit_entropy_bits(1). So, we can retain more or less the old behavior by waiting to extract from the input pool until it hits 256 bits of entropy using the new code. For people concerned about this change, it means that there's not that much practical behavioral change. And for folks actually trying to model the behavior rigorously, it means that we have an even higher margin against attacks.
--- drivers/char/random.c | 114 ++++++++------------------------------------------ 1 file changed, 20 insertions(+), 94 deletions(-)
drivers/char/random.c +20 -94
@@ -286,17 +286,9 @@/* #define ADD_INTERRUPT_BENCH */ -enum poolinfo { +enum {POOL_BITS =BLAKE2S_HASH_SIZE *8 , -POOL_BITSHIFT =ilog2 (POOL_BITS ), -POOL_MIN_BITS =POOL_BITS /2 , - -/* To allow fractional bits to be tracked, the entropy_count field is -* denominated in units of 1/8th bits. */ -POOL_ENTROPY_SHIFT =3 , -#define POOL_ENTROPY_BITS() (input_pool.entropy_count >> POOL_ENTROPY_SHIFT) - POOL_FRACBITS = POOL_BITS << POOL_ENTROPY_SHIFT, -POOL_MIN_FRACBITS =POOL_MIN_BITS <<POOL_ENTROPY_SHIFT +POOL_MIN_BITS =POOL_BITS /* No point in settling for less. */ };/*
@@ -309,7 +301,7 @@ static struct fasync_struct *fasync;* should wake up processes which are selecting or polling on write * access to /dev/random. */ -static int random_write_wakeup_bits =POOL_BITS *3 /4 ; +static int random_write_wakeup_bits =POOL_MIN_BITS ;static DEFINE_SPINLOCK (random_ready_list_lock);static LIST_HEAD (random_ready_list);
@@ -469,66 +461,18 @@ static void process_random_ready_list(vostatic void credit_entropy_bits (int nbits) {int entropy_count, orig; -int nfrac = nbits <<POOL_ENTROPY_SHIFT ; - -/* Ensure that the multiplication can avoid being 64 bits wide. */ -BUILD_BUG_ON (2 * (POOL_ENTROPY_SHIFT +POOL_BITSHIFT ) >31 );if (!nbits)return ; -retry: - entropy_count = orig =READ_ONCE (input_pool.entropy_count); -if (nfrac <0 ) { -/* Debit */ - entropy_count += nfrac; - }else { -/* -* Credit: we have to account for the possibility of -* overwriting already present entropy. Even in the -* ideal case of pure Shannon entropy, new contributions -* approach the full value asymptotically: -* -* entropy <- entropy + (pool_size - entropy) * -* (1 - exp(-add_entropy/pool_size)) -* -* For add_entropy <= pool_size/2 then -* (1 - exp(-add_entropy/pool_size)) >= -* (add_entropy/pool_size)*0.7869... -* so we can approximate the exponential with -* 3/4*add_entropy/pool_size and still be on the -* safe side by adding at most pool_size/2 at a time. -* -* The use of pool_size-2 in the while statement is to -* prevent rounding artifacts from making the loop -* arbitrarily long; this limits the loop to log2(pool_size)*2 -* turns no matter how large nbits is. -*/ -int pnfrac = nfrac; -const int s =POOL_BITSHIFT +POOL_ENTROPY_SHIFT +2 ; -/* The +2 corresponds to the /4 in the denominator */ - -do { -unsigned int anfrac =min (pnfrac,POOL_FRACBITS /2 ); -unsigned int add = - ((POOL_FRACBITS - entropy_count) * anfrac *3 ) >> s; - - entropy_count += add; - pnfrac -= anfrac; - }while (unlikely (entropy_count <POOL_FRACBITS -2 && pnfrac)); - } - -if (WARN_ON (entropy_count <0 )) { -pr_warn ("negative entropy/overflow: count %d\n" , entropy_count); - entropy_count =0 ; - }else if (entropy_count >POOL_FRACBITS ) - entropy_count =POOL_FRACBITS ; -if (cmpxchg (&input_pool.entropy_count, orig, entropy_count) != orig) - goto retry; +do { + orig =READ_ONCE (input_pool.entropy_count); + entropy_count =min (POOL_BITS , orig + nbits); + }while (cmpxchg (&input_pool.entropy_count, orig, entropy_count) != orig); -trace_credit_entropy_bits (nbits, entropy_count >>POOL_ENTROPY_SHIFT , _RET_IP_); +trace_credit_entropy_bits (nbits, entropy_count, _RET_IP_); -if (crng_init <2 && entropy_count >=POOL_MIN_FRACBITS ) +if (crng_init <2 && entropy_count >=POOL_MIN_BITS )crng_reseed (&primary_crng, true); }
@@ -791,7 +735,7 @@ static void crng_reseed(struct crng_statint entropy_count;do { entropy_count =READ_ONCE (input_pool.entropy_count); -if (entropy_count <POOL_MIN_FRACBITS ) +if (entropy_count <POOL_MIN_BITS )return ; }while (cmpxchg (&input_pool.entropy_count, entropy_count,0 ) != entropy_count);extract_entropy (buf.key,sizeof (buf.key));
@@ -1014,7 +958,7 @@ void add_input_randomness(unsigned int t last_value = value;add_timer_randomness (&input_timer_state, (type <<4 ) ^ code ^ (code >>4 ) ^ value); -trace_add_input_randomness (POOL_ENTROPY_BITS ()); +trace_add_input_randomness (input_pool.entropy_count); }EXPORT_SYMBOL_GPL (add_input_randomness);
@@ -1112,7 +1056,7 @@ void add_disk_randomness(struct gendiskreturn ;/* first major is 1, so we get >= 0x200 here */ add_timer_randomness (disk->random,0x100 +disk_devt (disk)); - trace_add_disk_randomness(disk_devt(disk), POOL_ENTROPY_BITS()); + trace_add_disk_randomness(disk_devt(disk), input_pool.entropy_count); }EXPORT_SYMBOL_GPL (add_disk_randomness);#endif
@@ -1137,7 +1081,7 @@ static void extract_entropy(void *buf, s } block;size_t i; -trace_extract_entropy (nbytes,POOL_ENTROPY_BITS ()); +trace_extract_entropy (nbytes, input_pool.entropy_count);for (i =0 ; i <ARRAY_SIZE (block.rdrand); ++i) {if (!arch_get_random_long (&block.rdrand[i]))
@@ -1486,9 +1430,9 @@ static ssize_t urandom_read_nowarn(struc {int ret; - nbytes =min_t (size_t, nbytes,INT_MAX >> (POOL_ENTROPY_SHIFT +3 )); + nbytes =min_t (size_t, nbytes,INT_MAX >>6 ); ret =extract_crng_user (buf, nbytes); -trace_urandom_read (8 * nbytes,0 ,POOL_ENTROPY_BITS ()); +trace_urandom_read (8 * nbytes,0 , input_pool.entropy_count);return ret; }
@@ -1527,7 +1471,7 @@ static __poll_t random_poll(struct file mask =0 ;if (crng_ready ()) mask |=EPOLLIN |EPOLLRDNORM ; -if (POOL_ENTROPY_BITS () < random_write_wakeup_bits) +if (input_pool.entropy_count < random_write_wakeup_bits) mask |=EPOLLOUT |EPOLLWRNORM ;return mask; }
@@ -1582,8 +1526,7 @@ static long random_ioctl(struct file *f,switch (cmd) {case RNDGETENTCNT :/* inherently racy, no point locking */ - ent_count =POOL_ENTROPY_BITS (); -if (put_user (ent_count, p)) +if (put_user (input_pool.entropy_count, p))return -EFAULT ;return 0 ;case RNDADDTOENTCNT :
@@ -1729,23 +1672,6 @@ static int proc_do_uuid(struct ctl_tablereturn proc_dostring (&fake_table, write, buffer, lenp, ppos); } -/* -* Return entropy available scaled to integral bits -*/ -static int proc_do_entropy (struct ctl_table *table,int write, -void __user *buffer,size_t *lenp,loff_t *ppos) -{ -struct ctl_table fake_table; -int entropy_count; - - entropy_count = *(int *)table->data >>POOL_ENTROPY_SHIFT ; - - fake_table.data = &entropy_count; - fake_table.maxlen =sizeof (entropy_count); - -return proc_dointvec (&fake_table, write, buffer, lenp, ppos); -} -static int sysctl_poolsize =POOL_BITS ;extern struct ctl_table random_table[];struct ctl_table random_table[] = {
@@ -1758,10 +1684,10 @@ struct ctl_table random_table[] = { }, { .procname = "entropy_avail", + .data = &input_pool.entropy_count, .maxlen = sizeof(int), .mode = 0444, - .proc_handler = proc_do_entropy, - .data = &input_pool.entropy_count, + .proc_handler = proc_dointvec, }, { .procname = "write_wakeup_threshold",
@@ -1957,7 +1883,7 @@ void add_hwgenerator_randomness(const ch*/ wait_event_interruptible_timeout (random_write_wait, !system_wq ||kthread_should_stop () || -POOL_ENTROPY_BITS () <= random_write_wakeup_bits, + input_pool.entropy_count <= random_write_wakeup_bits,CRNG_RESEED_INTERVAL );mix_pool_bytes (buffer, count);credit_entropy_bits (entropy);
Greg Kroah-Hartman
maintainerPATCH 67/234 random: de-duplicate INPUT_POOL constants
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 5b87adf30f1464477169a1d653e9baf8c012bbfe upstream.
We already had the POOL_* constants, so deduplicate the older INPUT_POOL ones. As well, fold EXTRACT_SIZE into the poolinfo enum, since it's related.
--- drivers/char/random.c | 17 ++++++----------- 1 file changed, 6 insertions(+), 11 deletions(-)
drivers/char/random.c +6 -11
@@ -359,13 +359,6 @@/* #define ADD_INTERRUPT_BENCH */ /* -* Configuration information -*/ -#define INPUT_POOL_SHIFT 12 -#define INPUT_POOL_WORDS (1 << (INPUT_POOL_SHIFT-5)) -#define EXTRACT_SIZE (BLAKE2S_HASH_SIZE / 2) - -/* * To allow fractional bits to be tracked, the entropy_count field is * denominated in units of 1/8th bits. *
@@ -440,7 +433,9 @@ enum poolinfo {POOL_TAP2 =76 ,POOL_TAP3 =51 ,POOL_TAP4 =25 , - POOL_TAP5 = 1 +POOL_TAP5 =1 , + + EXTRACT_SIZE = BLAKE2S_HASH_SIZE / 2 };/*
@@ -503,7 +498,7 @@ MODULE_PARM_DESC(ratelimit_disable, "Dis* **********************************************************************/ -static u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy; +static u32 input_pool_data[POOL_WORDS] __latent_entropy; static struct { /* read-only data: */
@@ -1959,7 +1954,7 @@ SYSCALL_DEFINE3(getrandom, char __user *#include <linux/sysctl.h> static int min_write_thresh; -static int max_write_thresh =INPUT_POOL_WORDS *32 ; +static int max_write_thresh =POOL_BITS ;static int random_min_urandom_seed =60 ;static char sysctl_bootid[16 ];
@@ -2016,7 +2011,7 @@ static int proc_do_entropy(struct ctl_tareturn proc_dointvec (&fake_table, write, buffer, lenp, ppos); } -static int sysctl_poolsize =INPUT_POOL_WORDS *32 ; +static int sysctl_poolsize =POOL_BITS ;extern struct ctl_table random_table[];struct ctl_table random_table[] = { {
Greg Kroah-Hartman
maintainerPATCH 37/234 s390: Remove arch_has_random, arch_has_random_seed
In reply to Greg Kroah-HartmanFrom: Richard Henderson <[email protected]>
commit 5e054c820f59bbb9714d5767f5f476581c309ca8 upstream.
These symbols are currently part of the generic archrandom.h interface, but are currently unused and can be removed.
--- arch/s390/include/asm/archrandom.h | 12 ------------ 1 file changed, 12 deletions(-)
arch/s390/include/asm/archrandom.h +0 -12
@@ -21,18 +21,6 @@ extern atomic64_t s390_arch_random_countbool s390_arch_random_generate (u8 *buf,unsigned int nbytes); -static inline bool arch_has_random (void ) -{ -return false; -} - -static inline bool arch_has_random_seed (void ) -{ -if (static_branch_likely (&s390_arch_random_available)) -return true; -return false; -} -static inline bool arch_get_random_long (unsigned long *v) {return false;
Greg Kroah-Hartman
maintainerPATCH 65/234 random: rather than entropy_store abstraction, use global
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 90ed1e67e896cc8040a523f8428fc02f9b164394 upstream.
Originally, the RNG used several pools, so having things abstracted out over a generic entropy_store object made sense. These days, there's only one input pool, and then an uneven mix of usage via the abstraction and usage via &input_pool. Rather than this uneasy mixture, just get rid of the abstraction entirely and have things always use the global. This simplifies the code and makes reading it a bit easier.
--- drivers/char/random.c | 219 ++++++++++++++++++------------------------ include/trace/events/random.h | 56 ++++------ 2 files changed, 117 insertions(+), 158 deletions(-)
drivers/char/random.c +96 -123
@@ -375,7 +375,7 @@* credit_entropy_bits() needs to be 64 bits wide. */ #define ENTROPY_SHIFT 3 -#define ENTROPY_BITS (r) ((r)->entropy_count >> ENTROPY_SHIFT) +#define ENTROPY_BITS () (input_pool.entropy_count >> ENTROPY_SHIFT)/* * If the entropy count falls under this number of bits, then we
@@ -505,33 +505,27 @@ MODULE_PARM_DESC(ratelimit_disable, "Dis* **********************************************************************/ -struct entropy_store ; -struct entropy_store { +static u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy; + +static struct {/* read-only data: */ u32 *pool; -const char *name;/* read-write data: */ spinlock_t lock;u16 add_ptr;u16 input_rotate;int entropy_count; -}; - -static ssize_t extract_entropy (struct entropy_store *r,void *buf, -size_t nbytes,int min); -static ssize_t _extract_entropy (struct entropy_store *r,void *buf, -size_t nbytes); - -static void crng_reseed (struct crng_state *crng,struct entropy_store *r); -static u32 input_pool_data[INPUT_POOL_WORDS] __latent_entropy; - -static struct entropy_store input_pool = { - .name ="input" , +} input_pool = { .lock =__SPIN_LOCK_UNLOCKED (input_pool.lock), .pool = input_pool_data }; +static ssize_t extract_entropy (void *buf,size_t nbytes,int min); +static ssize_t _extract_entropy (void *buf,size_t nbytes); + +static void crng_reseed (struct crng_state *crng,bool use_input_pool); +static u32 const twist_table[8 ] = {0x00000000 ,0x3b6e20c8 ,0x76dc4190 ,0x4db26158 ,0xedb88320 ,0xd6d6a3e8 ,0x9b64c2b0 ,0xa00ae278 };
@@ -546,16 +540,15 @@ static u32 const twist_table[8] = {* it's cheap to do so and helps slightly in the expected case where * the entropy is concentrated in the low-order bits. */ -static void _mix_pool_bytes (struct entropy_store *r,const void *in, -int nbytes) +static void _mix_pool_bytes (const void *in,int nbytes) {unsigned long i;int input_rotate;const u8 *bytes = in;u32 w; - input_rotate = r->input_rotate; - i = r->add_ptr; + input_rotate = input_pool.input_rotate; + i = input_pool.add_ptr;/* mix one byte at a time to simplify size handling and churn faster */ while (nbytes--) {
@@ -563,15 +556,15 @@ static void _mix_pool_bytes(struct entro i = (i -1 ) &POOL_WORDMASK ;/* XOR in the various taps */ - w ^= r->pool[i]; - w ^= r->pool[(i +POOL_TAP1 ) &POOL_WORDMASK ]; - w ^= r->pool[(i +POOL_TAP2 ) &POOL_WORDMASK ]; - w ^= r->pool[(i +POOL_TAP3 ) &POOL_WORDMASK ]; - w ^= r->pool[(i +POOL_TAP4 ) &POOL_WORDMASK ]; - w ^= r->pool[(i +POOL_TAP5 ) &POOL_WORDMASK ]; + w ^= input_pool.pool[i]; + w ^= input_pool.pool[(i +POOL_TAP1 ) &POOL_WORDMASK ]; + w ^= input_pool.pool[(i +POOL_TAP2 ) &POOL_WORDMASK ]; + w ^= input_pool.pool[(i +POOL_TAP3 ) &POOL_WORDMASK ]; + w ^= input_pool.pool[(i +POOL_TAP4 ) &POOL_WORDMASK ]; + w ^= input_pool.pool[(i +POOL_TAP5 ) &POOL_WORDMASK ];/* Mix the result back in with a twist */ - r->pool[i] = (w >>3 ) ^ twist_table[w &7 ]; + input_pool.pool[i] = (w >>3 ) ^ twist_table[w &7 ];/* * Normally, we add 7 bits of rotation to the pool.
@@ -582,26 +575,24 @@ static void _mix_pool_bytes(struct entro input_rotate = (input_rotate + (i ?7 :14 )) &31 ; } - r->input_rotate = input_rotate; - r->add_ptr = i; + input_pool.input_rotate = input_rotate; + input_pool.add_ptr = i; } -static void __mix_pool_bytes (struct entropy_store *r,const void *in, -int nbytes) +static void __mix_pool_bytes (const void *in,int nbytes) { -trace_mix_pool_bytes_nolock (r->name, nbytes, _RET_IP_); -_mix_pool_bytes (r, in, nbytes); +trace_mix_pool_bytes_nolock (nbytes, _RET_IP_); +_mix_pool_bytes (in, nbytes); } -static void mix_pool_bytes (struct entropy_store *r,const void *in, -int nbytes) +static void mix_pool_bytes (const void *in,int nbytes) {unsigned long flags; -trace_mix_pool_bytes (r->name, nbytes, _RET_IP_); -spin_lock_irqsave (&r->lock, flags); -_mix_pool_bytes (r, in, nbytes); -spin_unlock_irqrestore (&r->lock, flags); +trace_mix_pool_bytes (nbytes, _RET_IP_); +spin_lock_irqsave (&input_pool.lock, flags); +_mix_pool_bytes (in, nbytes); +spin_unlock_irqrestore (&input_pool.lock, flags); }struct fast_pool {
@@ -663,16 +654,16 @@ static void process_random_ready_list(vo* Use credit_entropy_bits_safe() if the value comes from userspace * or otherwise should be checked for extreme values. */ -static void credit_entropy_bits (struct entropy_store *r,int nbits) +static void credit_entropy_bits (int nbits) { -int entropy_count, orig; +int entropy_count, entropy_bits, orig;int nfrac = nbits <<ENTROPY_SHIFT ;if (!nbits)return ; retry: - entropy_count = orig =READ_ONCE (r->entropy_count); + entropy_count = orig =READ_ONCE (input_pool.entropy_count);if (nfrac <0 ) {/* Debit */ entropy_count += nfrac;
@@ -713,26 +704,21 @@ retry: }if (WARN_ON (entropy_count <0 )) { -pr_warn ("negative entropy/overflow: pool %s count %d\n" , - r->name, entropy_count); +pr_warn ("negative entropy/overflow: count %d\n" , entropy_count); entropy_count =0 ; }else if (entropy_count >POOL_FRACBITS ) entropy_count =POOL_FRACBITS ; -if (cmpxchg (&r->entropy_count, orig, entropy_count) != orig) +if (cmpxchg (&input_pool.entropy_count, orig, entropy_count) != orig) goto retry; -trace_credit_entropy_bits (r->name, nbits, - entropy_count >>ENTROPY_SHIFT , _RET_IP_); +trace_credit_entropy_bits (nbits, entropy_count >>ENTROPY_SHIFT , _RET_IP_); -if (r == &input_pool) { -int entropy_bits = entropy_count >>ENTROPY_SHIFT ; - -if (crng_init <2 && entropy_bits >=128 ) -crng_reseed (&primary_crng, r); - } + entropy_bits = entropy_count >>ENTROPY_SHIFT ; +if (crng_init <2 && entropy_bits >=128 ) +crng_reseed (&primary_crng, true); } -static int credit_entropy_bits_safe (struct entropy_store *r,int nbits) +static int credit_entropy_bits_safe (int nbits) {if (nbits <0 )return -EINVAL ;
@@ -740,7 +726,7 @@ static int credit_entropy_bits_safe(stru/* Cap the value to avoid overflows */ nbits =min (nbits,POOL_BITS ); -credit_entropy_bits (r, nbits); +credit_entropy_bits (nbits);return 0 ; }
@@ -818,7 +804,7 @@ static void crng_initialize_secondary(ststatic void __init crng_initialize_primary (struct crng_state *crng) { -_extract_entropy (&input_pool, &crng->state[4 ],sizeof (u32) *12 ); +_extract_entropy (&crng->state[4 ],sizeof (u32) *12 );if (crng_init_try_arch_early (crng) && trust_cpu && crng_init <2 ) {invalidate_batched_entropy ();numa_crng_init ();
@@ -979,7 +965,7 @@ static int crng_slow_load(const u8 *cp,return 1 ; } -static void crng_reseed (struct crng_state *crng,struct entropy_store *r) +static void crng_reseed (struct crng_state *crng,bool use_input_pool) {unsigned long flags;int i, num;
@@ -988,8 +974,8 @@ static void crng_reseed(struct crng_statu32 key[8 ]; } buf; -if (r) { - num =extract_entropy (r, &buf,32 ,16 ); +if (use_input_pool) { + num =extract_entropy (&buf,32 ,16 );if (num ==0 )return ; }else {
@@ -1020,8 +1006,7 @@ static void _extract_crng(struct crng_st init_time =READ_ONCE (crng->init_time);if (time_after (READ_ONCE (crng_global_init_time), init_time) ||time_after (jiffies, init_time +CRNG_RESEED_INTERVAL )) -crng_reseed (crng, crng == &primary_crng ? - &input_pool :NULL ); +crng_reseed (crng, crng == &primary_crng); }spin_lock_irqsave (&crng->lock, flags);chacha20_block (&crng->state[0 ], out);
@@ -1132,8 +1117,8 @@ void add_device_randomness(const void *btrace_add_device_randomness (size, _RET_IP_);spin_lock_irqsave (&input_pool.lock, flags); -_mix_pool_bytes (&input_pool, buf, size); -_mix_pool_bytes (&input_pool, &time,sizeof (time)); +_mix_pool_bytes (buf, size); +_mix_pool_bytes (&time,sizeof (time));spin_unlock_irqrestore (&input_pool.lock, flags); }EXPORT_SYMBOL (add_device_randomness);
@@ -1152,7 +1137,6 @@ static struct timer_rand_state input_tim*/ static void add_timer_randomness (struct timer_rand_state *state,unsigned num) { -struct entropy_store *r;struct {long jiffies;unsigned int cycles;
@@ -1163,8 +1147,7 @@ static void add_timer_randomness(struct sample.jiffies = jiffies; sample.cycles =random_get_entropy (); sample.num = num; - r = &input_pool; -mix_pool_bytes (r, &sample,sizeof (sample)); +mix_pool_bytes (&sample,sizeof (sample));/* * Calculate number of bits of randomness we probably added.
@@ -1196,7 +1179,7 @@ static void add_timer_randomness(struct* Round down by 1 bit on general principles, * and limit entropy estimate to 12 bits. */ -credit_entropy_bits (r,min_t (int,fls (delta>>1 ),11 )); +credit_entropy_bits (min_t (int,fls (delta>>1 ),11 )); } void add_input_randomness(unsigned int type, unsigned int code,
@@ -1211,7 +1194,7 @@ void add_input_randomness(unsigned int t last_value = value;add_timer_randomness (&input_timer_state, (type <<4 ) ^ code ^ (code >>4 ) ^ value); -trace_add_input_randomness (ENTROPY_BITS (&input_pool)); +trace_add_input_randomness (ENTROPY_BITS ()); }EXPORT_SYMBOL_GPL (add_input_randomness);
@@ -1255,7 +1238,6 @@ static u32 get_reg(struct fast_pool *f,void add_interrupt_randomness (int irq) { -struct entropy_store *r;struct fast_pool *fast_pool =this_cpu_ptr (&irq_randomness);struct pt_regs *regs =get_irq_regs ();unsigned long now = jiffies;
@@ -1290,18 +1272,17 @@ void add_interrupt_randomness(int irq) !time_after(now, fast_pool->last + HZ))return ; - r = &input_pool; -if (!spin_trylock (&r->lock)) +if (!spin_trylock (&input_pool.lock))return ; fast_pool->last = now; -__mix_pool_bytes (r, &fast_pool->pool,sizeof (fast_pool->pool)); -spin_unlock (&r->lock); +__mix_pool_bytes (&fast_pool->pool,sizeof (fast_pool->pool)); +spin_unlock (&input_pool.lock); fast_pool->count =0 ;/* award one bit for the contents of the fast pool */ -credit_entropy_bits (r,1 ); +credit_entropy_bits (1 ); }EXPORT_SYMBOL_GPL (add_interrupt_randomness);
@@ -1312,7 +1293,7 @@ void add_disk_randomness(struct gendiskreturn ;/* first major is 1, so we get >= 0x200 here */ add_timer_randomness (disk->random,0x100 +disk_devt (disk)); - trace_add_disk_randomness(disk_devt(disk), ENTROPY_BITS(&input_pool)); + trace_add_disk_randomness(disk_devt(disk), ENTROPY_BITS()); }EXPORT_SYMBOL_GPL (add_disk_randomness);#endif
@@ -1327,16 +1308,16 @@ EXPORT_SYMBOL_GPL(add_disk_randomness);* This function decides how many bytes to actually take from the * given pool, and also debits the entropy count accordingly. */ -static size_t account (struct entropy_store *r,size_t nbytes,int min) +static size_t account (size_t nbytes,int min) {int entropy_count, orig, have_bytes;size_t ibytes, nfrac; -BUG_ON (r->entropy_count >POOL_FRACBITS ); +BUG_ON (input_pool.entropy_count >POOL_FRACBITS );/* Can we pull enough? */ retry: - entropy_count = orig =READ_ONCE (r->entropy_count); + entropy_count = orig =READ_ONCE (input_pool.entropy_count); ibytes = nbytes;/* never pull more than available */ have_bytes = entropy_count >> (ENTROPY_SHIFT +3 );
@@ -1348,8 +1329,7 @@ retry: ibytes =0 ;if (WARN_ON (entropy_count <0 )) { -pr_warn ("negative entropy count: pool %s count %d\n" , - r->name, entropy_count); +pr_warn ("negative entropy count: count %d\n" , entropy_count); entropy_count =0 ; } nfrac = ibytes << (ENTROPY_SHIFT +3 );
@@ -1358,11 +1338,11 @@ retry:else entropy_count =0 ; -if (cmpxchg (&r->entropy_count, orig, entropy_count) != orig) +if (cmpxchg (&input_pool.entropy_count, orig, entropy_count) != orig) goto retry; -trace_debit_entropy (r->name,8 * ibytes); -if (ibytes &&ENTROPY_BITS (r) < random_write_wakeup_bits) { +trace_debit_entropy (8 * ibytes); +if (ibytes &&ENTROPY_BITS () < random_write_wakeup_bits) {wake_up_interruptible (&random_write_wait);kill_fasync (&fasync,SIGIO ,POLL_OUT ); }
@@ -1375,7 +1355,7 @@ retry:* * Note: we assume that .poolwords is a multiple of 16 words. */ -static void extract_buf (struct entropy_store *r,u8 *out) +static void extract_buf (u8 *out) {struct blake2s_state state__aligned(__alignof__(unsigned long)) ;u8 hash[BLAKE2S_HASH_SIZE ];
@@ -1397,8 +1377,8 @@ static void extract_buf(struct entropy_s }/* Generate a hash across the pool */ -spin_lock_irqsave (&r->lock, flags); -blake2s_update (&state, (const u8 *)r->pool,POOL_BYTES ); +spin_lock_irqsave (&input_pool.lock, flags); +blake2s_update (&state, (const u8 *)input_pool.pool,POOL_BYTES );blake2s_final (&state, hash);/* final zeros out state */ /*
@@ -1410,8 +1390,8 @@ static void extract_buf(struct entropy_s* brute-forcing the feedback as hard as brute-forcing the * hash. */ -__mix_pool_bytes (r, hash,sizeof (hash)); -spin_unlock_irqrestore (&r->lock, flags); +__mix_pool_bytes (hash,sizeof (hash)); +spin_unlock_irqrestore (&input_pool.lock, flags);/* Note that EXTRACT_SIZE is half of hash size here, because above * we've dumped the full length back into mixer. By reducing the
@@ -1421,14 +1401,13 @@ static void extract_buf(struct entropy_smemzero_explicit (hash,sizeof (hash)); } -static ssize_t _extract_entropy (struct entropy_store *r,void *buf, -size_t nbytes) +static ssize_t _extract_entropy (void *buf,size_t nbytes) {ssize_t ret =0 , i;u8 tmp[EXTRACT_SIZE ];while (nbytes) { -extract_buf (r, tmp); +extract_buf (tmp); i =min_t (int, nbytes,EXTRACT_SIZE );memcpy (buf, tmp, i); nbytes -= i;
@@ -1449,12 +1428,11 @@ static ssize_t _extract_entropy(struct e* The min parameter specifies the minimum amount we can pull before * failing to avoid races that defeat catastrophic reseeding. */ -static ssize_t extract_entropy (struct entropy_store *r,void *buf, -size_t nbytes,int min) +static ssize_t extract_entropy (void *buf,size_t nbytes,int min) { -trace_extract_entropy (r->name, nbytes,ENTROPY_BITS (r), _RET_IP_); - nbytes =account (r, nbytes, min); -return _extract_entropy (r, buf, nbytes); +trace_extract_entropy (nbytes,ENTROPY_BITS (), _RET_IP_); + nbytes =account (nbytes, min); +return _extract_entropy (buf, nbytes); }#define warn_unseeded_randomness (previous) \
@@ -1539,7 +1517,7 @@ EXPORT_SYMBOL(get_random_bytes);*/ static void entropy_timer (struct timer_list *t) { -credit_entropy_bits (&input_pool,1 ); +credit_entropy_bits (1 ); }/*
@@ -1563,14 +1541,14 @@ static void try_to_generate_entropy(voidwhile (!crng_ready ()) {if (!timer_pending (&stack.timer))mod_timer (&stack.timer, jiffies+1 ); -mix_pool_bytes (&input_pool, &stack.now,sizeof (stack.now)); +mix_pool_bytes (&stack.now,sizeof (stack.now));schedule (); stack.now =random_get_entropy (); }del_timer_sync (&stack.timer);destroy_timer_on_stack (&stack.timer); -mix_pool_bytes (&input_pool, &stack.now,sizeof (stack.now)); +mix_pool_bytes (&stack.now,sizeof (stack.now)); }/*
@@ -1711,26 +1689,24 @@ EXPORT_SYMBOL(get_random_bytes_arch);/* * init_std_data - initialize pool with system data * -* @r: pool to initialize -* * This function clears the pool's entropy count and mixes some system * data into the pool to prepare it for use. The pool is not cleared * as that can only decrease the entropy in the pool. */ -static void __init init_std_data (struct entropy_store *r) +static void __init init_std_data (void ) {int i;ktime_t now =ktime_get_real ();unsigned long rv; -mix_pool_bytes (r, &now,sizeof (now)); +mix_pool_bytes (&now,sizeof (now));for (i =POOL_BYTES ; i >0 ; i -=sizeof (rv)) {if (!arch_get_random_seed_long (&rv) && !arch_get_random_long (&rv)) rv =random_get_entropy (); -mix_pool_bytes (r, &rv,sizeof (rv)); +mix_pool_bytes (&rv,sizeof (rv)); } -mix_pool_bytes (r,utsname (),sizeof (*(utsname ()))); +mix_pool_bytes (utsname (),sizeof (*(utsname ()))); }/*
@@ -1745,7 +1721,7 @@ static void __init init_std_data(struct*/ int __init rand_initialize (void ) { -init_std_data (&input_pool); +init_std_data ();if (crng_need_final_init)crng_finalize_init (&primary_crng);crng_initialize_primary (&primary_crng);
@@ -1782,7 +1758,7 @@ urandom_read_nowarn(struct file *file, c nbytes =min_t (size_t, nbytes,INT_MAX >> (ENTROPY_SHIFT +3 )); ret =extract_crng_user (buf, nbytes); -trace_urandom_read (8 * nbytes,0 ,ENTROPY_BITS (&input_pool)); +trace_urandom_read (8 * nbytes,0 ,ENTROPY_BITS ());return ret; }
@@ -1822,13 +1798,13 @@ random_poll(struct file *file, poll_tabl mask =0 ;if (crng_ready ()) mask |=EPOLLIN |EPOLLRDNORM ; -if (ENTROPY_BITS (&input_pool) < random_write_wakeup_bits) +if (ENTROPY_BITS () < random_write_wakeup_bits) mask |=EPOLLOUT |EPOLLWRNORM ;return mask; }static int -write_pool (struct entropy_store *r,const char __user *buffer,size_t count) +write_pool (const char __user *buffer,size_t count) {size_t bytes;u32 t, buf[16 ];
@@ -1850,7 +1826,7 @@ write_pool(struct entropy_store *r, cons count -= bytes; p += bytes; -mix_pool_bytes (r, buf, bytes); +mix_pool_bytes (buf, bytes);cond_resched (); }
@@ -1862,7 +1838,7 @@ static ssize_t random_write(struct file {size_t ret; - ret =write_pool (&input_pool, buffer, count); + ret =write_pool (buffer, count);if (ret)return ret;
@@ -1878,7 +1854,7 @@ static long random_ioctl(struct file *f,switch (cmd) {case RNDGETENTCNT :/* inherently racy, no point locking */ - ent_count =ENTROPY_BITS (&input_pool); + ent_count =ENTROPY_BITS ();if (put_user (ent_count, p))return -EFAULT ;return 0 ;
@@ -1887,7 +1863,7 @@ static long random_ioctl(struct file *f,return -EPERM ;if (get_user (ent_count, p))return -EFAULT ; -return credit_entropy_bits_safe (&input_pool, ent_count); +return credit_entropy_bits_safe (ent_count);case RNDADDENTROPY :if (!capable (CAP_SYS_ADMIN ))return -EPERM ;
@@ -1897,11 +1873,10 @@ static long random_ioctl(struct file *f,return -EINVAL ;if (get_user (size, p++))return -EFAULT ; - retval =write_pool (&input_pool, (const char __user *)p, - size); + retval =write_pool ((const char __user *)p, size);if (retval <0 )return retval; -return credit_entropy_bits_safe (&input_pool, ent_count); +return credit_entropy_bits_safe (ent_count);case RNDZAPENTCNT :case RNDCLEARPOOL :/*
@@ -1917,7 +1892,7 @@ static long random_ioctl(struct file *f,return -EPERM ;if (crng_init <2 )return -ENODATA ; -crng_reseed (&primary_crng, &input_pool); +crng_reseed (&primary_crng, true);WRITE_ONCE (crng_global_init_time, jiffies -1 );return 0 ;default :
@@ -2239,11 +2214,9 @@ randomize_page(unsigned long start, unsivoid add_hwgenerator_randomness (const char *buffer,size_t count,size_t entropy) { -struct entropy_store *poolp = &input_pool; -if (unlikely (crng_init ==0 )) {size_t ret =crng_fast_load (buffer, count); -mix_pool_bytes (poolp, buffer, ret); +mix_pool_bytes (buffer, ret); count -= ret; buffer += ret;if (!count || crng_init ==0 )
@@ -2256,9 +2229,9 @@ void add_hwgenerator_randomness(const ch*/ wait_event_interruptible (random_write_wait, !system_wq ||kthread_should_stop () || -ENTROPY_BITS (&input_pool) <= random_write_wakeup_bits); -mix_pool_bytes (poolp, buffer, count); -credit_entropy_bits (poolp, entropy); +ENTROPY_BITS () <= random_write_wakeup_bits); +mix_pool_bytes (buffer, count); +credit_entropy_bits (entropy); }EXPORT_SYMBOL_GPL (add_hwgenerator_randomness);
include/trace/events/random.h +21 -35
137 lines not on this page · raw message
Greg Kroah-Hartman
maintainerPATCH 66/234 random: remove unused OUTPUT_POOL constants
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 0f63702718c91d89c922081ac1e6baeddc2d8b1a upstream.
We no longer have an output pool. Rather, we have just a wakeup bits threshold for /dev/random reads, presumably so that processes don't hang. This value, random_write_wakeup_bits, is configurable anyway. So all the no longer usefully named OUTPUT_POOL constants were doing was setting a reasonable default for random_write_wakeup_bits. This commit gets rid of the constants and just puts it all in the default value of random_write_wakeup_bits.
--- drivers/char/random.c | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-)
drivers/char/random.c +1 -3
@@ -363,8 +363,6 @@*/ #define INPUT_POOL_SHIFT 12#define INPUT_POOL_WORDS (1 << (INPUT_POOL_SHIFT-5)) -#define OUTPUT_POOL_SHIFT 10 -#define OUTPUT_POOL_WORDS (1 << (OUTPUT_POOL_SHIFT-5))#define EXTRACT_SIZE (BLAKE2S_HASH_SIZE / 2)/*
@@ -382,7 +380,7 @@* should wake up processes which are selecting or polling on write * access to /dev/random. */ -static int random_write_wakeup_bits =28 *OUTPUT_POOL_WORDS ; +static int random_write_wakeup_bits =28 * (1 <<5 );/* * Originally, we used a primitive polynomial of degree .poolwords
Greg Kroah-Hartman
maintainerPATCH 39/234 linux/random.h: Use false with bool
In reply to Greg Kroah-HartmanFrom: Richard Henderson <[email protected]>
commit 66f5ae899ada79c0e9a3d8d954f93a72344cd350 upstream.
Keep the generic fallback versions in sync with the other architecture specific implementations and use the proper name for false.
--- include/linux/random.h | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-)
include/linux/random.h +4 -4
@@ -120,19 +120,19 @@ unsigned long randomize_page(unsigned lo#else static inline bool arch_get_random_long (unsigned long *v) { -return 0 ; +return false; }static inline bool arch_get_random_int (unsigned int *v) { -return 0 ; +return false; }static inline bool arch_get_random_seed_long (unsigned long *v) { -return 0 ; +return false; }static inline bool arch_get_random_seed_int (unsigned int *v) { -return 0 ; +return false; }#endif
Greg Kroah-Hartman
maintainerPATCH 69/234 random: cleanup fractional entropy shift constants
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 18263c4e8e62f7329f38f5eadc568751242ca89c upstream.
The entropy estimator is calculated in terms of 1/8 bits, which means there are various constants where things are shifted by 3. Move these into our pool info enum with the other relevant constants. While we're at it, move an English assertion about sizes into a proper BUILD_BUG_ON so that the compiler can ensure this invariant.
--- drivers/char/random.c | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-)
drivers/char/random.c +13 -15
@@ -359,16 +359,6 @@/* #define ADD_INTERRUPT_BENCH */ /* -* To allow fractional bits to be tracked, the entropy_count field is -* denominated in units of 1/8th bits. -* -* 2*(POOL_ENTROPY_SHIFT + poolbitshift) must <= 31, or the multiply in -* credit_entropy_bits() needs to be 64 bits wide. -*/ -#define POOL_ENTROPY_SHIFT 3 -#define POOL_ENTROPY_BITS () (input_pool.entropy_count >> POOL_ENTROPY_SHIFT) - -/* * If the entropy count falls under this number of bits, then we * should wake up processes which are selecting or polling on write * access to /dev/random.
@@ -425,8 +415,13 @@ enum poolinfo {POOL_WORDMASK =POOL_WORDS -1 ,POOL_BYTES =POOL_WORDS *sizeof (u32),POOL_BITS =POOL_BYTES *8 , -POOL_BITSHIFT =ilog2 (POOL_WORDS ) +5 , -POOL_FRACBITS =POOL_WORDS << (POOL_ENTROPY_SHIFT +5 ), +POOL_BITSHIFT =ilog2 (POOL_BITS ), + +/* To allow fractional bits to be tracked, the entropy_count field is +* denominated in units of 1/8th bits. */ +POOL_ENTROPY_SHIFT =3 , +#define POOL_ENTROPY_BITS() (input_pool.entropy_count >> POOL_ENTROPY_SHIFT) +POOL_FRACBITS =POOL_BITS <<POOL_ENTROPY_SHIFT ,/* x^128 + x^104 + x^76 + x^51 +x^25 + x + 1 */ POOL_TAP1 = 104,
@@ -652,6 +647,9 @@ static void credit_entropy_bits(int nbitint entropy_count, entropy_bits, orig;int nfrac = nbits <<POOL_ENTROPY_SHIFT ; +/* Ensure that the multiplication can avoid being 64 bits wide. */ +BUILD_BUG_ON (2 * (POOL_ENTROPY_SHIFT +POOL_BITSHIFT ) >31 ); +if (!nbits)return ;
@@ -687,13 +685,13 @@ retry:/* The +2 corresponds to the /4 in the denominator */ do { -unsigned int anfrac =min (pnfrac,POOL_FRACBITS /2 ); +unsigned int anfrac =min (pnfrac,POOL_FRACBITS /2 );unsigned int add = - ((POOL_FRACBITS - entropy_count)*anfrac*3 ) >> s; + ((POOL_FRACBITS - entropy_count) * anfrac *3 ) >> s; entropy_count += add; pnfrac -= anfrac; - }while (unlikely (entropy_count <POOL_FRACBITS -2 && pnfrac)); + }while (unlikely (entropy_count <POOL_FRACBITS -2 && pnfrac)); }if (WARN_ON (entropy_count <0 )) {
Greg Kroah-Hartman
maintainerPATCH 41/234 powerpc: Use bool in archrandom.h
In reply to Greg Kroah-HartmanFrom: Richard Henderson <[email protected]>
commit 98dcfce69729f9ce0fb14f96a39bbdba21429597 upstream.
The generic interface uses bool not int; match that.
--- arch/powerpc/include/asm/archrandom.h | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-)
arch/powerpc/include/asm/archrandom.h +9 -8
@@ -6,27 +6,28 @@#include <asm/machdep.h> -static inline int arch_get_random_long (unsigned long *v) +static inline bool arch_get_random_long (unsigned long *v) { -return 0 ; +return false; } -static inline int arch_get_random_int (unsigned int *v) +static inline bool arch_get_random_int (unsigned int *v) { -return 0 ; +return false; } -static inline int arch_get_random_seed_long (unsigned long *v) +static inline bool arch_get_random_seed_long (unsigned long *v) {if (ppc_md.get_random_seed)return ppc_md.get_random_seed (v); -return 0 ; +return false; } -static inline int arch_get_random_seed_int (unsigned int *v) + +static inline bool arch_get_random_seed_int (unsigned int *v) {unsigned long val; -int rc; +bool rc; rc =arch_get_random_seed_long (&val);if (rc)
Greg Kroah-Hartman
maintainerPATCH 42/234 random: add arch_get_random_*long_early()
In reply to Greg Kroah-HartmanFrom: Mark Rutland <[email protected]>
commit 253d3194c2b58152fe830fd27c2fd83ebc6fe5ee upstream.
Some architectures (e.g. arm64) can have heterogeneous CPUs, and the boot CPU may be able to provide entropy while secondary CPUs cannot. On such systems, arch_get_random_long() and arch_get_random_seed_long() will fail unless support for RNG instructions has been detected on all CPUs. This prevents the boot CPU from being able to provide (potentially) trusted entropy when seeding the primary CRNG.
To make it possible to seed the primary CRNG from the boot CPU without adversely affecting the runtime versions of arch_get_random_long() and arch_get_random_seed_long(), this patch adds new early versions of the functions used when initializing the primary CRNG.
Default implementations are provided atop of the existing arch_get_random_long() and arch_get_random_seed_long() so that only architectures with such constraints need to provide the new helpers.
There should be no functional change as a result of this patch.
--- drivers/char/random.c | 20 +++++++++++++++++++- include/linux/random.h | 22 ++++++++++++++++++++++ 2 files changed, 41 insertions(+), 1 deletion(-)
drivers/char/random.c +19 -1
@@ -801,6 +801,24 @@ static bool crng_init_try_arch(struct crreturn arch_init; } +static bool __init crng_init_try_arch_early (struct crng_state *crng) +{ +int i; +bool arch_init = true; +unsigned long rv; + +for (i =4 ; i <16 ; i++) { +if (!arch_get_random_seed_long_early (&rv) && + !arch_get_random_long_early (&rv)) { + rv =random_get_entropy (); + arch_init = false; + } + crng->state[i] ^= rv; + } + +return arch_init; +} +static void __maybe_unused crng_initialize_secondary (struct crng_state *crng) {memcpy (&crng->state[0 ],"expand 32-byte k" ,16 );
@@ -813,7 +831,7 @@ static void __init crng_initialize_prima {memcpy (&crng->state[0 ],"expand 32-byte k" ,16 );_extract_entropy (&input_pool, &crng->state[4 ],sizeof (__u32) *12 ,0 ); -if (crng_init_try_arch (crng) && trust_cpu) { +if (crng_init_try_arch_early (crng) && trust_cpu) {invalidate_batched_entropy ();numa_crng_init (); crng_init =2 ;
include/linux/random.h +22 -0
@@ -7,6 +7,8 @@#ifndef _LINUX_RANDOM_H#define _LINUX_RANDOM_H +#include <linux/bug.h> +#include <linux/kernel.h> #include <linux/list.h> #include <linux/once.h>
@@ -136,4 +138,24 @@ static inline bool __must_check arch_get }#endif +/* +* Called from the boot CPU during startup; not valid to call once +* secondary CPUs are up and preemption is possible. +*/ +#ifndef arch_get_random_seed_long_early +static inline bool __init arch_get_random_seed_long_early (unsigned long *v) +{ +WARN_ON (system_state !=SYSTEM_BOOTING ); +return arch_get_random_seed_long (v); +} +#endif + +#ifndef arch_get_random_long_early +static inline bool __init arch_get_random_long_early (unsigned long *v) +{ +WARN_ON (system_state !=SYSTEM_BOOTING ); +return arch_get_random_long (v); +} +#endif +#endif /* _LINUX_RANDOM_H */
Greg Kroah-Hartman
maintainerPATCH 34/234 random: avoid warnings for !CONFIG_NUMA builds
In reply to Greg Kroah-HartmanFrom: Mark Rutland <[email protected]>
commit ab9a7e27044b87ff2be47b8f8e095400e7fccc44 upstream.
As crng_initialize_secondary() is only called by do_numa_crng_init(), and the latter is under ifdeffery for CONFIG_NUMA, when CONFIG_NUMA is not selected the compiler will warn that the former is unused:
| drivers/char/random.c:820:13: warning: 'crng_initialize_secondary' defined but not used [-Wunused-function] | 820 | static void crng_initialize_secondary(struct crng_state *crng) | | ^~~~~~~~~~~~~~~~~~~~~~~~~
Stephen reports that this happens for x86_64 noallconfig builds.
We could move crng_initialize_secondary() and crng_init_try_arch() under the CONFIG_NUMA ifdeffery, but this has the unfortunate property of separating them from crng_initialize_primary() and crng_init_try_arch_early() respectively. Instead, let's mark crng_initialize_secondary() as __maybe_unused.
--- drivers/char/random.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/random.c +1 -1
@@ -801,7 +801,7 @@ static bool crng_init_try_arch(struct crreturn arch_init; } -static void crng_initialize_secondary (struct crng_state *crng) +static void __maybe_unused crng_initialize_secondary (struct crng_state *crng) {memcpy (&crng->state[0 ],"expand 32-byte k" ,16 );_get_random_bytes (&crng->state[4 ],sizeof (__u32) *12 );
Greg Kroah-Hartman
maintainerPATCH 43/234 random: avoid arch_get_random_seed_long() when collecting IRQ randomness
In reply to Greg Kroah-HartmanFrom: Ard Biesheuvel <[email protected]>
commit 390596c9959c2a4f5b456df339f0604df3d55fe0 upstream.
When reseeding the CRNG periodically, arch_get_random_seed_long() is called to obtain entropy from an architecture specific source if one is implemented. In most cases, these are special instructions, but in some cases, such as on ARM, we may want to back this using firmware calls, which are considerably more expensive.
Another call to arch_get_random_seed_long() exists in the CRNG driver, in add_interrupt_randomness(), which collects entropy by capturing inter-interrupt timing and relying on interrupt jitter to provide random bits. This is done by keeping a per-CPU state, and mixing in the IRQ number, the cycle counter and the return address every time an interrupt is taken, and mixing this per-CPU state into the entropy pool every 64 invocations, or at least once per second. The entropy that is gathered this way is credited as 1 bit of entropy. Every time this happens, arch_get_random_seed_long() is invoked, and the result is mixed in as well, and also credited with 1 bit of entropy.
This means that arch_get_random_seed_long() is called at least once per second on every CPU, which seems excessive, and doesn't really scale, especially in a virtualization scenario where CPUs may be
oversubscribed: in cases where arch_get_random_seed_long() is backed by an instruction that actually goes back to a shared hardware entropy source (such as RNDRRS on ARM), we will end up hitting it hundreds of times per second.
So let's drop the call to arch_get_random_seed_long() from add_interrupt_randomness(), and instead, rely on crng_reseed() to call the arch hook to get random seed material from the platform.
--- drivers/char/random.c | 15 +-------------- 1 file changed, 1 insertion(+), 14 deletions(-)
drivers/char/random.c +1 -14
@@ -1280,8 +1280,6 @@ void add_interrupt_randomness(int irq, icycles_t cycles =random_get_entropy ();__u32 c_high, j_high;__u64 ip; -unsigned long seed; -int credit =0 ;if (cycles ==0 ) cycles =get_reg (fast_pool, regs);
@@ -1317,23 +1315,12 @@ void add_interrupt_randomness(int irq, i fast_pool->last = now;__mix_pool_bytes (r, &fast_pool->pool,sizeof (fast_pool->pool)); - -/* -* If we have architectural seed generator, produce a seed and -* add it to the pool. For the sake of paranoia don't let the -* architectural seed generator dominate the input from the -* interrupt noise. -*/ -if (arch_get_random_seed_long (&seed)) { -__mix_pool_bytes (r, &seed,sizeof (seed)); - credit =1 ; - }spin_unlock (&r->lock); fast_pool->count =0 ;/* award one bit for the contents of the fast pool */ -credit_entropy_bits (r, credit +1 ); +credit_entropy_bits (r,1 ); }EXPORT_SYMBOL_GPL (add_interrupt_randomness);
Greg Kroah-Hartman
maintainerPATCH 79/234 random: make credit_entropy_bits() always safe
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit a49c010e61e1938be851f5e49ac219d49b704103 upstream.
This is called from various hwgenerator drivers, so rather than having one "safe" version for userspace and one "unsafe" version for the kernel, just make everything safe; the checks are cheap and sensible to have anyway.
--- drivers/char/random.c | 29 +++++++++-------------------- 1 file changed, 9 insertions(+), 20 deletions(-)
drivers/char/random.c +9 -20
@@ -447,18 +447,15 @@ static void process_random_ready_list(vospin_unlock_irqrestore (&random_ready_list_lock, flags); } -/* -* Credit (or debit) the entropy store with n bits of entropy. -* Use credit_entropy_bits_safe() if the value comes from userspace -* or otherwise should be checked for extreme values. -*/ static void credit_entropy_bits (int nbits) {int entropy_count, orig; -if (!nbits) +if (nbits <=0 )return ; + nbits =min (nbits,POOL_BITS ); + do { orig = READ_ONCE(input_pool.entropy_count); entropy_count = min(POOL_BITS, orig + nbits);
@@ -470,18 +467,6 @@ static void credit_entropy_bits(int nbitcrng_reseed (&primary_crng, true); } -static int credit_entropy_bits_safe (int nbits) -{ -if (nbits <0 ) -return -EINVAL ; - -/* Cap the value to avoid overflows */ - nbits =min (nbits,POOL_BITS ); - -credit_entropy_bits (nbits); -return 0 ; -} -/********************************************************************* * * CRNG using CHACHA20
@@ -1526,7 +1511,10 @@ static long random_ioctl(struct file *f,return -EPERM ;if (get_user (ent_count, p))return -EFAULT ; -return credit_entropy_bits_safe (ent_count); +if (ent_count <0 ) +return -EINVAL ; +credit_entropy_bits (ent_count); +return 0 ;case RNDADDENTROPY :if (!capable (CAP_SYS_ADMIN ))return -EPERM ;
@@ -1539,7 +1527,8 @@ static long random_ioctl(struct file *f, retval =write_pool ((const char __user *)p, size);if (retval <0 )return retval; -return credit_entropy_bits_safe (ent_count); +credit_entropy_bits (ent_count); +return 0 ;case RNDZAPENTCNT :case RNDCLEARPOOL :/*
Greg Kroah-Hartman
maintainerPATCH 88/234 random: use hash function for crng_slow_load()
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 66e4c2b9541503d721e936cc3898c9f25f4591ff upstream.
Since we have a hash function that's really fast, and the goal of crng_slow_load() is reportedly to "touch all of the crng's state", we can just hash the old state together with the new state and call it a day. This way we dont need to reason about another LFSR or worry about various attacks there. This code is only ever used at early boot and then never again.
--- drivers/char/random.c | 163 +++++++++++++++++------------------------- include/linux/hw_random.h | 2 include/linux/random.h | 10 +- include/trace/events/random.h | 79 +++++++++----------- 4 files changed, 113 insertions(+), 141 deletions(-)
drivers/char/random.c +69 -94
@@ -69,7 +69,7 @@* * The primary kernel interfaces are: * -* void get_random_bytes(void *buf, int nbytes); +* void get_random_bytes(void *buf, size_t nbytes); * u32 get_random_u32() * u64 get_random_u64() * unsigned int get_random_int()
@@ -97,14 +97,14 @@* The current exported interfaces for gathering environmental noise * from the devices are: * -* void add_device_randomness(const void *buf, unsigned int size); +* void add_device_randomness(const void *buf, size_t size); * void add_input_randomness(unsigned int type, unsigned int code, * unsigned int value); * void add_interrupt_randomness(int irq); * void add_disk_randomness(struct gendisk *disk); -* void add_hwgenerator_randomness(const char *buffer, size_t count, +* void add_hwgenerator_randomness(const void *buffer, size_t count, * size_t entropy); -* void add_bootloader_randomness(const void *buf, unsigned int size); +* void add_bootloader_randomness(const void *buf, size_t size); * * add_device_randomness() is for adding data to the random pool that * is likely to differ between two devices (or possibly even per boot).
@@ -268,7 +268,7 @@ static int crng_init = 0;#define crng_ready () (likely(crng_init > 1))static int crng_init_cnt =0 ;static void process_random_ready_list (void ); -static void _get_random_bytes (void *buf,int nbytes); +static void _get_random_bytes (void *buf,size_t nbytes);static struct ratelimit_state unseeded_warning =RATELIMIT_STATE_INIT ("warn_unseeded_randomness" ,HZ ,3 );
@@ -290,7 +290,7 @@ MODULE_PARM_DESC(ratelimit_disable, "Disstatic struct {struct blake2s_state hash;spinlock_t lock; -int entropy_count; +unsigned int entropy_count; } input_pool = { .hash.h = {BLAKE2S_IV0 ^ (0x01010000 |BLAKE2S_HASH_SIZE ),BLAKE2S_IV1 ,BLAKE2S_IV2 ,BLAKE2S_IV3 ,BLAKE2S_IV4 ,
@@ -308,18 +308,12 @@ static void crng_reseed(void);* update the entropy estimate. The caller should call * credit_entropy_bits if this is appropriate. */ -static void _mix_pool_bytes (const void *in,int nbytes) +static void _mix_pool_bytes (const void *in,size_t nbytes) {blake2s_update (&input_pool.hash, in, nbytes); } -static void __mix_pool_bytes (const void *in,int nbytes) -{ -trace_mix_pool_bytes_nolock (nbytes, _RET_IP_); -_mix_pool_bytes (in, nbytes); -} - -static void mix_pool_bytes (const void *in,int nbytes) +static void mix_pool_bytes (const void *in,size_t nbytes) {unsigned long flags;
@@ -383,18 +377,18 @@ static void process_random_ready_list(vospin_unlock_irqrestore (&random_ready_list_lock, flags); } -static void credit_entropy_bits (int nbits) +static void credit_entropy_bits (size_t nbits) { -int entropy_count, orig; +unsigned int entropy_count, orig, add; -if (nbits <=0 ) +if (!nbits)return ; - nbits =min (nbits,POOL_BITS ); + add =min_t (size_t, nbits,POOL_BITS );do { orig =READ_ONCE (input_pool.entropy_count); - entropy_count =min (POOL_BITS , orig + nbits); + entropy_count = min_t(unsigned int, POOL_BITS, orig + add); }while (cmpxchg (&input_pool.entropy_count, orig, entropy_count) != orig);trace_credit_entropy_bits (nbits, entropy_count, _RET_IP_);
@@ -441,10 +435,10 @@ static void invalidate_batched_entropy(v* path. So we can't afford to dilly-dally. Returns the number of * bytes processed from cp. */ -static size_t crng_fast_load (const u8 *cp,size_t len) +static size_t crng_fast_load (const void *cp,size_t len) {unsigned long flags; -u8 *p; +const u8 *src = (const u8 *)cp;size_t ret =0 ;if (!spin_trylock_irqsave (&base_crng.lock, flags))
@@ -453,10 +447,9 @@ static size_t crng_fast_load(const u8 *cspin_unlock_irqrestore (&base_crng.lock, flags);return 0 ; } - p = base_crng.key;while (len >0 && crng_init_cnt <CRNG_INIT_CNT_THRESH ) { - p[crng_init_cnt %sizeof (base_crng.key)] ^= *cp; - cp++; crng_init_cnt++; len--; ret++; + base_crng.key[crng_init_cnt %sizeof (base_crng.key)] ^= *src; + src++; crng_init_cnt++; len--; ret++; }if (crng_init_cnt >=CRNG_INIT_CNT_THRESH ) {invalidate_batched_entropy ();
@@ -475,42 +468,30 @@ static size_t crng_fast_load(const u8 *c* all), and (2) it doesn't have the performance constraints of * crng_fast_load(). * -* So we do something more comprehensive which is guaranteed to touch -* all of the primary_crng's state, and which uses a LFSR with a -* period of 255 as part of the mixing algorithm. Finally, we do -* *not* advance crng_init_cnt since buffer we may get may be something -* like a fixed DMI table (for example), which might very well be -* unique to the machine, but is otherwise unvarying. +* So, we simply hash the contents in with the current key. Finally, +* we do *not* advance crng_init_cnt since buffer we may get may be +* something like a fixed DMI table (for example), which might very +* well be unique to the machine, but is otherwise unvarying. */ -static int crng_slow_load (const u8 *cp,size_t len) +static void crng_slow_load (const void *cp,size_t len) {unsigned long flags; -static u8 lfsr =1 ; -u8 tmp; -unsigned int i, max =sizeof (base_crng.key); -const u8 *src_buf = cp; -u8 *dest_buf = base_crng.key; +struct blake2s_state hash; + +blake2s_init (&hash,sizeof (base_crng.key));if (!spin_trylock_irqsave (&base_crng.lock, flags)) -return 0 ; +return ;if (crng_init !=0 ) {spin_unlock_irqrestore (&base_crng.lock, flags); -return 0 ; +return ; } -if (len > max) - max = len; -for (i =0 ; i < max; i++) { - tmp = lfsr; - lfsr >>=1 ; -if (tmp &1 ) - lfsr ^=0xE1 ; - tmp = dest_buf[i %sizeof (base_crng.key)]; - dest_buf[i %sizeof (base_crng.key)] ^= src_buf[i % len] ^ lfsr; - lfsr += (tmp <<3 ) | (tmp >>5 ); - } +blake2s_update (&hash, base_crng.key,sizeof (base_crng.key)); +blake2s_update (&hash, cp, len); +blake2s_final (&hash, base_crng.key); +spin_unlock_irqrestore (&base_crng.lock, flags); -return 1 ; }static void crng_reseed (void )
@@ -666,14 +647,15 @@ static void crng_make_state(u32 chacha_sstatic ssize_t get_random_bytes_user (void __user *buf,size_t nbytes) {bool large_request = nbytes >256 ; -ssize_t ret =0 , len; +ssize_t ret =0 ; +size_t len;u32 chacha_state[CHACHA20_BLOCK_SIZE /sizeof (u32)];u8 output[CHACHA20_BLOCK_SIZE ];if (!nbytes)return 0 ; - len =min_t (ssize_t,32 , nbytes); + len =min_t (size_t,32 , nbytes);crng_make_state (chacha_state, output, len);if (copy_to_user (buf, output, len))
@@ -693,7 +675,7 @@ static ssize_t get_random_bytes_user(voiif (unlikely (chacha_state[12 ] ==0 )) ++chacha_state[13 ]; - len =min_t (ssize_t, nbytes,CHACHA20_BLOCK_SIZE ); + len =min_t (size_t, nbytes,CHACHA20_BLOCK_SIZE );if (copy_to_user (buf, output, len)) { ret = -EFAULT ;break ;
@@ -731,7 +713,7 @@ struct timer_rand_state {* the entropy pool having similar initial state across largely * identical devices. */ -void add_device_randomness (const void *buf,unsigned int size) +void add_device_randomness (const void *buf,size_t size) {unsigned long time =random_get_entropy () ^ jiffies;unsigned long flags;
@@ -759,7 +741,7 @@ static struct timer_rand_state input_tim* keyboard scan codes, and 256 upwards for interrupts. * */ -static void add_timer_randomness (struct timer_rand_state *state,unsigned num) +static void add_timer_randomness (struct timer_rand_state *state,unsigned int num) {struct {long jiffies;
@@ -803,7 +785,7 @@ static void add_timer_randomness(struct* Round down by 1 bit on general principles, * and limit entropy estimate to 12 bits. */ -credit_entropy_bits (min_t (int,fls (delta >>1 ),11 )); + credit_entropy_bits(min_t(unsigned int, fls(delta >> 1), 11)); } void add_input_randomness(unsigned int type, unsigned int code,
@@ -884,8 +866,8 @@ void add_interrupt_randomness(int irq)add_interrupt_bench (cycles);if (unlikely (crng_init ==0 )) { -if ((fast_pool->count >=64 ) && -crng_fast_load ((u8 *)fast_pool->pool,sizeof (fast_pool->pool)) >0 ) { +if (fast_pool->count >=64 && +crng_fast_load (fast_pool->pool,sizeof (fast_pool->pool)) >0 ) { fast_pool->count =0 ; fast_pool->last = now;if (spin_trylock (&input_pool.lock)) {
@@ -903,7 +885,7 @@ void add_interrupt_randomness(int irq)return ; fast_pool->last = now; -__mix_pool_bytes (&fast_pool->pool,sizeof (fast_pool->pool)); +_mix_pool_bytes (&fast_pool->pool,sizeof (fast_pool->pool));spin_unlock (&input_pool.lock); fast_pool->count =0 ;
@@ -1012,18 +994,18 @@ static void _warn_unseeded_randomness(co* wait_for_random_bytes() should be called and return 0 at least once * at any point prior. */ -static void _get_random_bytes (void *buf,int nbytes) +static void _get_random_bytes (void *buf,size_t nbytes) {u32 chacha_state[CHACHA20_BLOCK_SIZE /sizeof (u32)];u8 tmp[CHACHA20_BLOCK_SIZE ]; -ssize_t len; +size_t len;trace_get_random_bytes (nbytes, _RET_IP_);if (!nbytes)return ; - len =min_t (ssize_t,32 , nbytes); + len =min_t (size_t,32 , nbytes);crng_make_state (chacha_state, buf, len); nbytes -= len; buf += len;
@@ -1046,7 +1028,7 @@ static void _get_random_bytes(void *buf,memzero_explicit (chacha_state,sizeof (chacha_state)); } -void get_random_bytes (void *buf,int nbytes) +void get_random_bytes (void *buf,size_t nbytes) {static void *previous;
@@ -1207,25 +1189,19 @@ EXPORT_SYMBOL(del_random_ready_callback)/* * This function will use the architecture-specific hardware random -* number generator if it is available. The arch-specific hw RNG will -* almost certainly be faster than what we can do in software, but it -* is impossible to verify that it is implemented securely (as -* opposed, to, say, the AES encryption of a sequence number using a -* key known by the NSA). So it's useful if we need the speed, but -* only if we're willing to trust the hardware manufacturer not to -* have put in a back door. -* -* Return number of bytes filled in. +* number generator if it is available. It is not recommended for +* use. Use get_random_bytes() instead. It returns the number of +* bytes filled in. */ -int __must_check get_random_bytes_arch (void *buf,int nbytes) +size_t __must_check get_random_bytes_arch (void *buf,size_t nbytes) { -int left = nbytes; +size_t left = nbytes;u8 *p = buf;trace_get_random_bytes_arch (left, _RET_IP_);while (left) {unsigned long v; -int chunk =min_t (int, left,sizeof (unsigned long )); +size_t chunk =min_t (size_t, left,sizeof (unsigned long ));if (!arch_get_random_long (&v))break ;
@@ -1258,12 +1234,12 @@ early_param("random.trust_cpu", parse_tr*/ int __init rand_initialize (void ) { -int i; +size_t i;ktime_t now =ktime_get_real ();bool arch_init = true;unsigned long rv; -for (i =BLAKE2S_BLOCK_SIZE ; i >0 ; i -=sizeof (rv)) { +for (i =0 ; i <BLAKE2S_BLOCK_SIZE ; i +=sizeof (rv)) {if (!arch_get_random_seed_long_early (&rv) && !arch_get_random_long_early (&rv)) { rv =random_get_entropy ();
@@ -1312,7 +1288,7 @@ static ssize_t urandom_read_nowarn(struc nbytes =min_t (size_t, nbytes,INT_MAX >>6 ); ret =get_random_bytes_user (buf, nbytes); -trace_urandom_read (8 * nbytes,0 , input_pool.entropy_count); +trace_urandom_read (nbytes, input_pool.entropy_count);return ret; }
@@ -1356,19 +1332,18 @@ static __poll_t random_poll(struct filereturn mask; } -static int write_pool (const char __user *buffer,size_t count) +static int write_pool (const char __user *ubuf,size_t count) { -size_t bytes; -u8 buf[BLAKE2S_BLOCK_SIZE ]; -const char __user *p = buffer; - -while (count >0 ) { - bytes =min (count,sizeof (buf)); -if (copy_from_user (buf, p, bytes)) +size_t len; +u8 block[BLAKE2S_BLOCK_SIZE ]; + +while (count) { + len =min (count,sizeof (block)); +if (copy_from_user (block, ubuf, len))return -EFAULT ; - count -= bytes; - p += bytes; -mix_pool_bytes (buf, bytes); + count -= len; + ubuf += len; +mix_pool_bytes (block, len);cond_resched (); }
@@ -1378,7 +1353,7 @@ static int write_pool(const char __userstatic ssize_t random_write (struct file *file,const char __user *buffer,size_t count,loff_t *ppos) { -size_t ret; +int ret; ret =write_pool (buffer, count);if (ret)
@@ -1472,8 +1447,6 @@ const struct file_operations urandom_fop SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int, flags) { -int ret; -if (flags & ~(GRND_NONBLOCK |GRND_RANDOM |GRND_INSECURE ))return -EINVAL ;
@@ -1488,6 +1461,8 @@ SYSCALL_DEFINE3(getrandom, char __user * count =INT_MAX ;if (!(flags &GRND_INSECURE ) && !crng_ready ()) { +int ret; +if (flags &GRND_NONBLOCK )return -EAGAIN ; ret =wait_for_random_bytes ();
@@ -1746,7 +1721,7 @@ unsigned long randomize_page(unsigned lo* Those devices may produce endless random bits and will be throttled * when our pool is full. */ -void add_hwgenerator_randomness (const char *buffer,size_t count, +void add_hwgenerator_randomness (const void *buffer,size_t count,size_t entropy) {if (unlikely (crng_init ==0 )) {
@@ -1777,7 +1752,7 @@ EXPORT_SYMBOL_GPL(add_hwgenerator_random* it would be regarded as device data. * The decision is controlled by CONFIG_RANDOM_TRUST_BOOTLOADER. */ -void add_bootloader_randomness (const void *buf,unsigned int size) +void add_bootloader_randomness (const void *buf,size_t size) {if (IS_ENABLED (CONFIG_RANDOM_TRUST_BOOTLOADER ))add_hwgenerator_randomness (buf, size, size *8 );
include/linux/hw_random.h +1 -1
@@ -60,6 +60,6 @@ extern int devm_hwrng_register(struct deextern void hwrng_unregister (struct hwrng *rng);extern void devm_hwrng_unregister (struct device *dve,struct hwrng *rng);/** Feed random bits into the pool. */ -extern void add_hwgenerator_randomness (const char *buffer,size_t count,size_t entropy); +extern void add_hwgenerator_randomness (const void *buffer,size_t count,size_t entropy);#endif /* LINUX_HWRANDOM_H_ */
include/linux/random.h +5 -5
@@ -20,8 +20,8 @@ struct random_ready_callback {struct module *owner; }; -extern void add_device_randomness (const void *,unsigned int ); -extern void add_bootloader_randomness (const void *,unsigned int ); +extern void add_device_randomness (const void *,size_t ); +extern void add_bootloader_randomness (const void *,size_t );#if defined(LATENT_ENTROPY_PLUGIN ) && !defined(__CHECKER__)static inline void add_latent_entropy (void )
@@ -37,13 +37,13 @@ extern void add_input_randomness(unsigne unsigned int value) __latent_entropy; extern void add_interrupt_randomness(int irq) __latent_entropy; -extern void get_random_bytes (void *buf,int nbytes); +extern void get_random_bytes (void *buf,size_t nbytes);extern int wait_for_random_bytes (void );extern int __init rand_initialize (void );extern bool rng_is_initialized (void );extern int add_random_ready_callback (struct random_ready_callback *rdy);extern void del_random_ready_callback (struct random_ready_callback *rdy); -extern int __must_check get_random_bytes_arch (void *buf,int nbytes); +extern size_t __must_check get_random_bytes_arch (void *buf,size_t nbytes);#ifndef MODULE extern const struct file_operations random_fops, urandom_fops;
@@ -87,7 +87,7 @@ static inline unsigned long get_random_c/* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes). * Returns the result of the call to wait_for_random_bytes. */ -static inline int get_random_bytes_wait (void *buf,int nbytes) +static inline int get_random_bytes_wait (void *buf,size_t nbytes) {int ret =wait_for_random_bytes ();get_random_bytes (buf, nbytes);
include/trace/events/random.h +38 -41
211 lines not on this page · raw message
Greg Kroah-Hartman
maintainerPATCH 89/234 random: remove outdated INT_MAX >> 6 check in urandom_read()
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 434537ae54ad37e93555de21b6ac8133d6d773a9 upstream.
In 79a8468747c5 ("random: check for increase of entropy_count because of signed conversion"), a number of checks were added around what values were passed to account(), because account() was doing fancy fixed point fractional arithmetic, and a user had some ability to pass large values directly into it. One of things in that commit was limiting those values to INT_MAX >> 6. The first >> 3 was for bytes to bits, and the next >> 3 was for bits to 1/8 fractional bits.
However, for several years now, urandom reads no longer touch entropy accounting, and so this check serves no purpose. The current flow is:
urandom_read_nowarn()-->get_random_bytes_user()-->chacha20_block()
Of course, we don't want that size_t to be truncated when adding it into the ssize_t. But we arrive at urandom_read_nowarn() in the first place either via ordinary fops, which limits reads to MAX_RW_COUNT, or via getrandom() which limits reads to INT_MAX.
--- drivers/char/random.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/char/random.c +1 -2
@@ -1284,9 +1284,8 @@ void rand_initialize_disk(struct gendiskstatic ssize_t urandom_read_nowarn (struct file *file,char __user *buf,size_t nbytes,loff_t *ppos) { -int ret; +ssize_t ret; - nbytes =min_t (size_t, nbytes,INT_MAX >>6 ); ret =get_random_bytes_user (buf, nbytes);trace_urandom_read (nbytes, input_pool.entropy_count);return ret;
Greg Kroah-Hartman
maintainerPATCH 90/234 random: zero buffer after reading entropy from userspace
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 7b5164fb1279bf0251371848e40bae646b59b3a8 upstream.
This buffer may contain entropic data that shouldn't stick around longer than needed, so zero out the temporary buffer at the end of write_pool().
--- drivers/char/random.c | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-)
drivers/char/random.c +14 -6
@@ -500,6 +500,7 @@ static void crng_reseed(void)int entropy_count;unsigned long next_gen;u8 key[CHACHA20_KEY_SIZE ]; +bool finalize_init = false;/* * First we make sure we have POOL_MIN_BITS of entropy in the pool,
@@ -527,12 +528,14 @@ static void crng_reseed(void) ++next_gen;WRITE_ONCE (base_crng.generation, next_gen);WRITE_ONCE (base_crng.birth, jiffies); -spin_unlock_irqrestore (&base_crng.lock, flags); -memzero_explicit (key,sizeof (key)); -if (crng_init <2 ) {invalidate_batched_entropy (); crng_init =2 ; + finalize_init = true; + } +spin_unlock_irqrestore (&base_crng.lock, flags); +memzero_explicit (key,sizeof (key)); +if (finalize_init) {process_random_ready_list ();wake_up_interruptible (&crng_init_wait);kill_fasync (&fasync,SIGIO ,POLL_IN );
@@ -1334,19 +1337,24 @@ static __poll_t random_poll(struct filestatic int write_pool (const char __user *ubuf,size_t count) {size_t len; +int ret =0 ;u8 block[BLAKE2S_BLOCK_SIZE ];while (count) { len =min (count,sizeof (block)); -if (copy_from_user (block, ubuf, len)) -return -EFAULT ; +if (copy_from_user (block, ubuf, len)) { + ret = -EFAULT ; + goto out; + } count -= len; ubuf += len;mix_pool_bytes (block, len);cond_resched (); } -return 0 ; +out: +memzero_explicit (block,sizeof (block)); +return ret; } static ssize_t random_write(struct file *file, const char __user *buffer,
Greg Kroah-Hartman
maintainerPATCH 91/234 random: tie batched entropy generation to base_crng generation
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 0791e8b655cc373718f0f58800fdc625a3447ac5 upstream.
Now that we have an explicit base_crng generation counter, we don't need a separate one for batched entropy. Rather, we can just move the generation forward every time we change crng_init state or update the base_crng key.
--- drivers/char/random.c | 29 ++++++++--------------------- 1 file changed, 8 insertions(+), 21 deletions(-)
drivers/char/random.c +8 -21
@@ -428,8 +428,6 @@ static DEFINE_PER_CPU(struct crng, crngsstatic DECLARE_WAIT_QUEUE_HEAD (crng_init_wait); -static void invalidate_batched_entropy (void ); -/* * crng_fast_load() can be called by code in the interrupt service * path. So we can't afford to dilly-dally. Returns the number of
@@ -452,7 +450,7 @@ static size_t crng_fast_load(const void src++; crng_init_cnt++; len--; ret++; }if (crng_init_cnt >=CRNG_INIT_CNT_THRESH ) { -invalidate_batched_entropy (); + ++base_crng.generation; crng_init =1 ; }spin_unlock_irqrestore (&base_crng.lock, flags);
@@ -529,7 +527,6 @@ static void crng_reseed(void)WRITE_ONCE (base_crng.generation, next_gen);WRITE_ONCE (base_crng.birth, jiffies);if (crng_init <2 ) { -invalidate_batched_entropy (); crng_init =2 ; finalize_init = true; }
@@ -1254,8 +1251,9 @@ int __init rand_initialize(void)mix_pool_bytes (utsname (),sizeof (*(utsname ())));extract_entropy (base_crng.key,sizeof (base_crng.key)); + ++base_crng.generation; +if (arch_init && trust_cpu && crng_init <2 ) { -invalidate_batched_entropy (); crng_init =2 ;pr_notice ("crng init done (trusting CPU's manufacturer)\n" ); }
@@ -1593,8 +1591,6 @@ struct ctl_table random_table[] = { };#endif /* CONFIG_SYSCTL */ -static atomic_t batch_generation =ATOMIC_INIT (0 ); - struct batched_entropy { union {/*
@@ -1607,8 +1603,8 @@ struct batched_entropy {u64 entropy_u64[CHACHA20_BLOCK_SIZE *3 / (2 *sizeof (u64))];u32 entropy_u32[CHACHA20_BLOCK_SIZE *3 / (2 *sizeof (u32))]; }; +unsigned long generation;unsigned int position; -int generation; };/*
@@ -1627,14 +1623,14 @@ u64 get_random_u64(void)unsigned long flags;struct batched_entropy *batch;static void *previous; -int next_gen; +unsigned long next_gen;warn_unseeded_randomness (&previous);local_irq_save (flags); batch =raw_cpu_ptr (&batched_entropy_u64); - next_gen =atomic_read (&batch_generation); + next_gen =READ_ONCE (base_crng.generation);if (batch->position >=ARRAY_SIZE (batch->entropy_u64) || next_gen != batch->generation) {_get_random_bytes (batch->entropy_u64,sizeof (batch->entropy_u64));
@@ -1660,14 +1656,14 @@ u32 get_random_u32(void)unsigned long flags;struct batched_entropy *batch;static void *previous; -int next_gen; +unsigned long next_gen;warn_unseeded_randomness (&previous);local_irq_save (flags); batch =raw_cpu_ptr (&batched_entropy_u32); - next_gen =atomic_read (&batch_generation); + next_gen =READ_ONCE (base_crng.generation);if (batch->position >=ARRAY_SIZE (batch->entropy_u32) || next_gen != batch->generation) {_get_random_bytes (batch->entropy_u32,sizeof (batch->entropy_u32));
@@ -1683,15 +1679,6 @@ u32 get_random_u32(void) }EXPORT_SYMBOL (get_random_u32); -/* It's important to invalidate all potential batched entropy that might -* be stored before the crng is initialized, which we can do lazily by -* bumping the generation counter. -*/ -static void invalidate_batched_entropy (void ) -{ -atomic_inc (&batch_generation); -} -/** * randomize_page - Generate a random, page aligned address * @start: The smallest acceptable address the caller will take.
Greg Kroah-Hartman
maintainerPATCH 92/234 random: remove ifdefd out interrupt bench
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 95e6060c20a7f5db60163274c5222a725ac118f9 upstream.
With tools like kbench9000 giving more finegrained responses, and this basically never having been used ever since it was initially added, let's just get rid of this. There *is* still work to be done on the interrupt handler, but this really isn't the way it's being developed.
--- Documentation/sysctl/kernel.txt | 9 --------- drivers/char/random.c | 40 ---------------------------------------- 2 files changed, 49 deletions(-)
Documentation/sysctl/kernel.txt +0 -9
@@ -852,15 +852,6 @@ This is a directory, with the following are woken up. This file is writable for compatibility purposes, but writing to it has no effect on any RNG behavior. -If ``drivers/char/random.c`` is built with ``ADD_INTERRUPT_BENCH`` -defined, these additional entries are present: - -* ``add_interrupt_avg_cycles``: the average number of cycles between - interrupts used to feed the pool; - -* ``add_interrupt_avg_deviation``: the standard deviation seen on the - number of cycles between interrupts used to feed the pool. - randomize_va_space ==================
drivers/char/random.c +0 -40
@@ -240,8 +240,6 @@#define CREATE_TRACE_POINTS #include <trace/events/random.h> -/* #define ADD_INTERRUPT_BENCH */ -enum {POOL_BITS =BLAKE2S_HASH_SIZE *8 ,POOL_MIN_BITS =POOL_BITS /* No point in settling for less. */
@@ -806,27 +804,6 @@ EXPORT_SYMBOL_GPL(add_input_randomness); static DEFINE_PER_CPU(struct fast_pool, irq_randomness); -#ifdef ADD_INTERRUPT_BENCH -static unsigned long avg_cycles, avg_deviation; - -#define AVG_SHIFT 8/* Exponential average factor k=1/256 */ -#define FIXED_1_2 (1 << (AVG_SHIFT - 1)) - -static void add_interrupt_bench (cycles_t start) -{ -long delta =random_get_entropy () - start; - -/* Use a weighted moving average */ - delta = delta - ((avg_cycles +FIXED_1_2 ) >>AVG_SHIFT ); - avg_cycles += delta; -/* And average deviation */ - delta =abs (delta) - ((avg_deviation +FIXED_1_2 ) >>AVG_SHIFT ); - avg_deviation += delta; -} -#else -#define add_interrupt_bench (x) -#endif -static u32 get_reg (struct fast_pool *f,struct pt_regs *regs) {u32 *ptr = (u32 *)regs;
@@ -863,7 +840,6 @@ void add_interrupt_randomness(int irq) (sizeof (ip) >4 ) ? ip >>32 :get_reg (fast_pool, regs);fast_mix (fast_pool); -add_interrupt_bench (cycles);if (unlikely (crng_init ==0 )) { if (fast_pool->count >= 64 &&
@@ -1571,22 +1547,6 @@ struct ctl_table random_table[] = { .mode = 0444, .proc_handler = proc_do_uuid, }, -#ifdef ADD_INTERRUPT_BENCH - { - .procname = "add_interrupt_avg_cycles", - .data = &avg_cycles, - .maxlen = sizeof(avg_cycles), - .mode = 0444, - .proc_handler = proc_doulongvec_minmax, - }, - { - .procname = "add_interrupt_avg_deviation", - .data = &avg_deviation, - .maxlen = sizeof(avg_deviation), - .mode = 0444, - .proc_handler = proc_doulongvec_minmax, - }, -#endif { } };#endif /* CONFIG_SYSCTL */
Greg Kroah-Hartman
maintainerPATCH 93/234 random: remove unused tracepoints
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 14c174633f349cb41ea90c2c0aaddac157012f74 upstream.
These explicit tracepoints aren't really used and show sign of aging. It's work to keep these up to date, and before I attempted to keep them up to date, they weren't up to date, which indicates that they're not really used. These days there are better ways of introspecting anyway.
--- drivers/char/random.c | 30 ------ include/trace/events/random.h | 195 ------------------------------------------ lib/random32.c | 2 3 files changed, 5 insertions(+), 222 deletions(-) delete mode 100644 include/trace/events/random.h
drivers/char/random.c +3 -27
@@ -237,9 +237,6 @@#include <asm/irq_regs.h> #include <asm/io.h> -#define CREATE_TRACE_POINTS -#include <trace/events/random.h> -enum {POOL_BITS =BLAKE2S_HASH_SIZE *8 ,POOL_MIN_BITS =POOL_BITS /* No point in settling for less. */
@@ -315,7 +312,6 @@ static void mix_pool_bytes(const void *i {unsigned long flags; -trace_mix_pool_bytes (nbytes, _RET_IP_);spin_lock_irqsave (&input_pool.lock, flags);_mix_pool_bytes (in, nbytes);spin_unlock_irqrestore (&input_pool.lock, flags);
@@ -389,8 +385,6 @@ static void credit_entropy_bits(size_t n entropy_count = min_t(unsigned int, POOL_BITS, orig + add); }while (cmpxchg (&input_pool.entropy_count, orig, entropy_count) != orig); -trace_credit_entropy_bits (nbits, entropy_count, _RET_IP_); -if (crng_init <2 && entropy_count >=POOL_MIN_BITS )crng_reseed (); }
@@ -719,7 +713,6 @@ void add_device_randomness(const void *bif (!crng_ready () && size)crng_slow_load (buf, size); -trace_add_device_randomness (size, _RET_IP_);spin_lock_irqsave (&input_pool.lock, flags);_mix_pool_bytes (buf, size);_mix_pool_bytes (&time,sizeof (time));
@@ -798,7 +791,6 @@ void add_input_randomness(unsigned int t last_value = value;add_timer_randomness (&input_timer_state, (type <<4 ) ^ code ^ (code >>4 ) ^ value); -trace_add_input_randomness (input_pool.entropy_count); }EXPORT_SYMBOL_GPL (add_input_randomness);
@@ -878,7 +870,6 @@ void add_disk_randomness(struct gendiskreturn ;/* first major is 1, so we get >= 0x200 here */ add_timer_randomness(disk->random, 0x100 + disk_devt(disk)); - trace_add_disk_randomness(disk_devt(disk), input_pool.entropy_count); }EXPORT_SYMBOL_GPL (add_disk_randomness);#endif
@@ -903,8 +894,6 @@ static void extract_entropy(void *buf, s } block;size_t i; -trace_extract_entropy (nbytes, input_pool.entropy_count); -for (i =0 ; i <ARRAY_SIZE (block.rdseed); ++i) {if (!arch_get_random_seed_long (&block.rdseed[i]) && !arch_get_random_long (&block.rdseed[i]))
@@ -976,8 +965,6 @@ static void _get_random_bytes(void *buf,u8 tmp[CHACHA20_BLOCK_SIZE ];size_t len; -trace_get_random_bytes (nbytes, _RET_IP_); -if (!nbytes)return ;
@@ -1174,7 +1161,6 @@ size_t __must_check get_random_bytes_arcsize_t left = nbytes;u8 *p = buf; -trace_get_random_bytes_arch (left, _RET_IP_);while (left) {unsigned long v;size_t chunk =min_t (size_t, left,sizeof (unsigned long ));
@@ -1258,16 +1244,6 @@ void rand_initialize_disk(struct gendisk }#endif -static ssize_t urandom_read_nowarn (struct file *file,char __user *buf, -size_t nbytes,loff_t *ppos) -{ -ssize_t ret; - - ret =get_random_bytes_user (buf, nbytes); -trace_urandom_read (nbytes, input_pool.entropy_count); -return ret; -} -static ssize_t urandom_read (struct file *file,char __user *buf,size_t nbytes,loff_t *ppos) {
@@ -1280,7 +1256,7 @@ static ssize_t urandom_read(struct file current->comm, nbytes); } -return urandom_read_nowarn (file, buf, nbytes, ppos); +return get_random_bytes_user (buf, nbytes); } static ssize_t random_read(struct file *file, char __user *buf, size_t nbytes,
@@ -1291,7 +1267,7 @@ static ssize_t random_read(struct file * ret =wait_for_random_bytes ();if (ret !=0 )return ret; -return urandom_read_nowarn (file, buf, nbytes, ppos); +return get_random_bytes_user (buf, nbytes); }static __poll_t random_poll (struct file *file,poll_table *wait)
@@ -1450,7 +1426,7 @@ SYSCALL_DEFINE3(getrandom, char __user *if (unlikely (ret))return ret; } -return urandom_read_nowarn (NULL , buf, count,NULL ); +return get_random_bytes_user (buf, count); }/********************************************************************
include/trace/events/random.h deleted +0 -195
195 lines not on this page · raw message
lib/random32.c +2 -0
@@ -38,6 +38,8 @@#include <linux/jiffies.h> #include <linux/random.h> #include <linux/sched.h> +#include <linux/bitops.h> +#include <linux/slab.h> #include <asm/unaligned.h> /**
Greg Kroah-Hartman
maintainerPATCH 94/234 random: add proper SPDX header
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit a07fdae346c35c6ba286af1c88e0effcfa330bf9 upstream.
Convert the current license into the SPDX notation of "(GPL-2.0 OR BSD-3-Clause)". This infers GPL-2.0 from the text "ALTERNATIVELY, this product may be distributed under the terms of the GNU General Public License, in which case the provisions of the GPL are required INSTEAD OF the above restrictions" and it infers BSD-3-Clause from the verbatim BSD 3 clause license in the file.
--- drivers/char/random.c | 37 +------------------------------------ 1 file changed, 1 insertion(+), 36 deletions(-)
drivers/char/random.c +1 -36
@@ -1,44 +1,9 @@ +// SPDX-License-Identifier: (GPL-2.0 OR BSD-3-Clause) /* -* random.c -- A strong random number generator -* * Copyright (C) 2017-2022 Jason A. Donenfeld <[email protected]>. All Rights Reserved. -* * Copyright Matt Mackall <[email protected]>, 2003, 2004, 2005 -* * Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All * rights reserved. -* -* Redistribution and use in source and binary forms, with or without -* modification, are permitted provided that the following conditions -* are met: -* 1. Redistributions of source code must retain the above copyright -* notice, and the entire permission notice in its entirety, -* including the disclaimer of warranties. -* 2. Redistributions in binary form must reproduce the above copyright -* notice, this list of conditions and the following disclaimer in the -* documentation and/or other materials provided with the distribution. -* 3. The name of the author may not be used to endorse or promote -* products derived from this software without specific prior -* written permission. -* -* ALTERNATIVELY, this product may be distributed under the terms of -* the GNU General Public License, in which case the provisions of the GPL are -* required INSTEAD OF the above restrictions. (This clause is -* necessary due to a potential bad interaction between the GPL and -* the restrictions contained in a BSD-style copyright.) -* -* THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED -* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES -* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ALL OF -* WHICH ARE HEREBY DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE -* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR -* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT -* OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR -* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF -* LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE -* USE OF THIS SOFTWARE, EVEN IF NOT ADVISED OF THE POSSIBILITY OF SUCH -* DAMAGE. */ /*
Greg Kroah-Hartman
maintainerPATCH 95/234 random: deobfuscate irq u32/u64 contributions
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit b2f408fe403800c91a49f6589d95b6759ce1b30b upstream.
In the irq handler, we fill out 16 bytes differently on 32-bit and 64-bit platforms, and for 32-bit vs 64-bit cycle counters, which doesn't always correspond with the bitness of the platform. Whether or not you like this strangeness, it is a matter of fact. But it might not be a fact you well realized until now, because the code that loaded the irq info into 4 32-bit words was quite confusing. Instead, this commit makes everything explicit by having separate (compile-time) branches for 32-bit and 64-bit types.
--- drivers/char/random.c | 49 ++++++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 21 deletions(-)
drivers/char/random.c +28 -21
@@ -283,7 +283,10 @@ static void mix_pool_bytes(const void *i }struct fast_pool { -u32 pool[4 ]; +union { +u32 pool32[4 ]; +u64 pool64[2 ]; + };unsigned long last;u16 reg_idx;u8 count;
@@ -294,10 +297,10 @@ struct fast_pool {* collector. It's hardcoded for an 128 bit pool and assumes that any * locks that might be needed are taken by the caller. */ -static void fast_mix (struct fast_pool *f) +static void fast_mix (u32 pool[4 ]) { -u32 a = f->pool[0 ], b = f->pool[1 ]; -u32 c = f->pool[2 ], d = f->pool[3 ]; +u32 a = pool[0 ], b = pool[1 ]; +u32 c = pool[2 ], d = pool[3 ]; a += b; c += d; b =rol32 (b,6 ); d =rol32 (d,27 );
@@ -315,9 +318,8 @@ static void fast_mix(struct fast_pool *f b =rol32 (b,16 ); d =rol32 (d,14 ); d ^= a; b ^= c; - f->pool[0 ] = a; f->pool[1 ] = b; - f->pool[2 ] = c; f->pool[3 ] = d; - f->count++; + pool[0 ] = a; pool[1 ] = b; + pool[2 ] = c; pool[3 ] = d; }static void process_random_ready_list (void )
@@ -782,29 +784,34 @@ void add_interrupt_randomness(int irq)struct pt_regs *regs =get_irq_regs ();unsigned long now = jiffies;cycles_t cycles =random_get_entropy (); -u32 c_high, j_high; -u64 ip;if (cycles ==0 ) cycles =get_reg (fast_pool, regs); - c_high = (sizeof (cycles) >4 ) ? cycles >>32 :0 ; - j_high = (sizeof (now) >4 ) ? now >>32 :0 ; - fast_pool->pool[0 ] ^= cycles ^ j_high ^ irq; - fast_pool->pool[1 ] ^= now ^ c_high; - ip = regs ?instruction_pointer (regs) : _RET_IP_; - fast_pool->pool[2 ] ^= ip; - fast_pool->pool[3 ] ^= - (sizeof (ip) >4 ) ? ip >>32 :get_reg (fast_pool, regs); -fast_mix (fast_pool); +if (sizeof (cycles) ==8 ) + fast_pool->pool64[0 ] ^= cycles ^rol64 (now,32 ) ^ irq; +else { + fast_pool->pool32[0 ] ^= cycles ^ irq; + fast_pool->pool32[1 ] ^= now; + } + +if (sizeof (unsigned long ) ==8 ) + fast_pool->pool64[1 ] ^= regs ?instruction_pointer (regs) : _RET_IP_; +else { + fast_pool->pool32[2 ] ^= regs ?instruction_pointer (regs) : _RET_IP_; + fast_pool->pool32[3 ] ^=get_reg (fast_pool, regs); + } + +fast_mix (fast_pool->pool32); + ++fast_pool->count;if (unlikely (crng_init ==0 )) {if (fast_pool->count >=64 && -crng_fast_load (fast_pool->pool,sizeof (fast_pool->pool)) >0 ) { +crng_fast_load (fast_pool->pool32,sizeof (fast_pool->pool32)) >0 ) { fast_pool->count =0 ; fast_pool->last = now;if (spin_trylock (&input_pool.lock)) { -_mix_pool_bytes (&fast_pool->pool,sizeof (fast_pool->pool)); +_mix_pool_bytes (&fast_pool->pool32,sizeof (fast_pool->pool32));spin_unlock (&input_pool.lock); } }
@@ -818,7 +825,7 @@ void add_interrupt_randomness(int irq)return ; fast_pool->last = now; -_mix_pool_bytes (&fast_pool->pool,sizeof (fast_pool->pool)); +_mix_pool_bytes (&fast_pool->pool32,sizeof (fast_pool->pool32));spin_unlock (&input_pool.lock); fast_pool->count =0 ;
Greg Kroah-Hartman
maintainerPATCH 96/234 random: introduce drain_entropy() helper to declutter crng_reseed()
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 246c03dd899164d0186b6d685d6387f228c28d93 upstream.
In preparation for separating responsibilities, break out the entropy count management part of crng_reseed() into its own function.
No functional changes.
--- drivers/char/random.c | 36 +++++++++++++++++++++++------------- 1 file changed, 23 insertions(+), 13 deletions(-)
drivers/char/random.c +23 -13
@@ -260,6 +260,7 @@ static struct { };static void extract_entropy (void *buf,size_t nbytes); +static bool drain_entropy (void *buf,size_t nbytes);static void crng_reseed (void );
@@ -454,23 +455,13 @@ static void crng_slow_load(const void *cstatic void crng_reseed (void ) {unsigned long flags; -int entropy_count;unsigned long next_gen;u8 key[CHACHA20_KEY_SIZE ];bool finalize_init = false; -/* -* First we make sure we have POOL_MIN_BITS of entropy in the pool, -* and then we drain all of it. Only then can we extract a new key. -*/ -do { - entropy_count =READ_ONCE (input_pool.entropy_count); -if (entropy_count <POOL_MIN_BITS ) -return ; - }while (cmpxchg (&input_pool.entropy_count, entropy_count,0 ) != entropy_count); -extract_entropy (key,sizeof (key)); -wake_up_interruptible (&random_write_wait); -kill_fasync (&fasync,SIGIO ,POLL_OUT ); +/* Only reseed if we can, to prevent brute forcing a small amount of new bits. */ +if (!drain_entropy (key,sizeof (key))) +return ;/* * We copy the new key into the base_crng, overwriting the old one,
@@ -898,6 +889,25 @@ static void extract_entropy(void *buf, smemzero_explicit (&block,sizeof (block)); } +/* +* First we make sure we have POOL_MIN_BITS of entropy in the pool, and then we +* set the entropy count to zero (but don't actually touch any data). Only then +* can we extract a new key with extract_entropy(). +*/ +static bool drain_entropy (void *buf,size_t nbytes) +{ +unsigned int entropy_count; +do { + entropy_count =READ_ONCE (input_pool.entropy_count); +if (entropy_count <POOL_MIN_BITS ) +return false; + }while (cmpxchg (&input_pool.entropy_count, entropy_count,0 ) != entropy_count); +extract_entropy (buf, nbytes); +wake_up_interruptible (&random_write_wait); +kill_fasync (&fasync,SIGIO ,POLL_OUT ); +return true; +} +#define warn_unseeded_randomness (previous) \ _warn_unseeded_randomness(__func__, (void *)_RET_IP_, (previous))
Greg Kroah-Hartman
maintainerPATCH 80/234 random: remove use_input_pool parameter from crng_reseed()
In reply to Greg Kroah-HartmanFrom: Eric Biggers <[email protected]>
commit 5d58ea3a31cc98b9fa563f6921d3d043bf0103d1 upstream.
The primary_crng is always reseeded from the input_pool, while the NUMA crngs are always reseeded from the primary_crng. Remove the redundant 'use_input_pool' parameter from crng_reseed() and just directly check whether the crng is the primary_crng.
--- drivers/char/random.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/char/random.c +6 -6
@@ -365,7 +365,7 @@ static struct {static void extract_entropy (void *buf,size_t nbytes); -static void crng_reseed (struct crng_state *crng,bool use_input_pool); +static void crng_reseed (struct crng_state *crng);/* * This function adds bytes into the entropy "pool". It does not
@@ -464,7 +464,7 @@ static void credit_entropy_bits(int nbittrace_credit_entropy_bits (nbits, entropy_count, _RET_IP_);if (crng_init <2 && entropy_count >=POOL_MIN_BITS ) -crng_reseed (&primary_crng, true); +crng_reseed (&primary_crng); }/*********************************************************************
@@ -701,7 +701,7 @@ static int crng_slow_load(const u8 *cp,return 1 ; } -static void crng_reseed (struct crng_state *crng,bool use_input_pool) +static void crng_reseed (struct crng_state *crng) {unsigned long flags;int i;
@@ -710,7 +710,7 @@ static void crng_reseed(struct crng_statu32 key[8 ]; } buf; -if (use_input_pool) { +if (crng == &primary_crng) {int entropy_count; do { entropy_count = READ_ONCE(input_pool.entropy_count);
@@ -748,7 +748,7 @@ static void _extract_crng(struct crng_st init_time =READ_ONCE (crng->init_time);if (time_after (READ_ONCE (crng_global_init_time), init_time) ||time_after (jiffies, init_time +CRNG_RESEED_INTERVAL )) -crng_reseed (crng, crng == &primary_crng); +crng_reseed (crng); }spin_lock_irqsave (&crng->lock, flags);chacha20_block (&crng->state[0 ], out);
@@ -1547,7 +1547,7 @@ static long random_ioctl(struct file *f,return -EPERM ;if (crng_init <2 )return -ENODATA ; -crng_reseed (&primary_crng, true); +crng_reseed (&primary_crng);WRITE_ONCE (crng_global_init_time, jiffies -1 );return 0 ;default :
Greg Kroah-Hartman
maintainerPATCH 97/234 random: remove useless header comment
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 6071a6c0fba2d747742cadcbb3ba26ed756ed73b upstream.
This really adds nothing at all useful.
--- include/linux/random.h | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-)
include/linux/random.h +1 -5
@@ -1,9 +1,5 @@/* SPDX-License-Identifier: GPL-2.0 */ -/* -* include/linux/random.h -* -* Include file for the random number generator. -*/ +#ifndef _LINUX_RANDOM_H#define _LINUX_RANDOM_H
Greg Kroah-Hartman
maintainerPATCH 98/234 random: remove whitespace and reorder includes
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 87e7d5abad0cbc9312dea7f889a57d294c1a5fcc upstream.
This is purely cosmetic. Future work involves figuring out which of these headers we need and which we don't.
--- drivers/char/random.c | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-)
drivers/char/random.c +1 -2
@@ -193,11 +193,10 @@#include <linux/syscalls.h> #include <linux/completion.h> #include <linux/uuid.h> +#include <linux/uaccess.h> #include <crypto/chacha20.h> #include <crypto/blake2s.h> -#include <asm/processor.h> -#include <linux/uaccess.h> #include <asm/irq.h> #include <asm/irq_regs.h> #include <asm/io.h>
Greg Kroah-Hartman
maintainerPATCH 99/234 random: group initialization wait functions
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 5f1bb112006b104b3e2a1e1b39bbb9b2617581e6 upstream.
This pulls all of the readiness waiting-focused functions into the first labeled section.
No functional changes.
--- drivers/char/random.c | 1015 +++++++++++++++++++++++++------------------------- 1 file changed, 527 insertions(+), 488 deletions(-)
drivers/char/random.c +527 -488
@@ -201,126 +201,144 @@#include <asm/irq_regs.h> #include <asm/io.h> -enum { -POOL_BITS =BLAKE2S_HASH_SIZE *8 , -POOL_MIN_BITS =POOL_BITS /* No point in settling for less. */ -}; - -/* -* Static global variables -*/ -static DECLARE_WAIT_QUEUE_HEAD (random_write_wait); -static struct fasync_struct *fasync; - -static DEFINE_SPINLOCK (random_ready_list_lock); -static LIST_HEAD (random_ready_list); +/********************************************************************* +* +* Initialization and readiness waiting. +* +* Much of the RNG infrastructure is devoted to various dependencies +* being able to wait until the RNG has collected enough entropy and +* is ready for safe consumption. +* +*********************************************************************/ /* * crng_init = 0 --> Uninitialized * 1 --> Initialized * 2 --> Initialized from input_pool * -* crng_init is protected by primary_crng->lock, and only increases +* crng_init is protected by base_crng->lock, and only increases * its value (from 0->1->2). */ static int crng_init =0 ;#define crng_ready () (likely(crng_init > 1)) -static int crng_init_cnt =0 ; -static void process_random_ready_list (void ); -static void _get_random_bytes (void *buf,size_t nbytes); +/* Various types of waiters for crng_init->2 transition. */ +static DECLARE_WAIT_QUEUE_HEAD (crng_init_wait); +static struct fasync_struct *fasync; +static DEFINE_SPINLOCK (random_ready_list_lock); +static LIST_HEAD (random_ready_list); +/* Control how we warn userspace. */ static struct ratelimit_state unseeded_warning =RATELIMIT_STATE_INIT ("warn_unseeded_randomness" ,HZ ,3 );static struct ratelimit_state urandom_warning =RATELIMIT_STATE_INIT ("warn_urandom_randomness" ,HZ ,3 ); -static int ratelimit_disable__read_mostly ; -module_param_named (ratelimit_disable, ratelimit_disable, int,0644 );MODULE_PARM_DESC (ratelimit_disable,"Disable random ratelimit suppression" ); -/********************************************************************** -* -* OS independent entropy store. Here are the functions which handle -* storing entropy in an entropy pool. +/* +* Returns whether or not the input pool has been seeded and thus guaranteed +* to supply cryptographically secure random numbers. This applies to: the +* /dev/urandom device, the get_random_bytes function, and the get_random_{u32, +* ,u64,int,long} family of functions. * -**********************************************************************/ - -static struct { -struct blake2s_state hash; -spinlock_t lock; -unsigned int entropy_count; -} input_pool = { - .hash.h = {BLAKE2S_IV0 ^ (0x01010000 |BLAKE2S_HASH_SIZE ), -BLAKE2S_IV1 ,BLAKE2S_IV2 ,BLAKE2S_IV3 ,BLAKE2S_IV4 , -BLAKE2S_IV5 ,BLAKE2S_IV6 ,BLAKE2S_IV7 }, - .hash.outlen =BLAKE2S_HASH_SIZE , - .lock =__SPIN_LOCK_UNLOCKED (input_pool.lock), -}; - -static void extract_entropy (void *buf,size_t nbytes); -static bool drain_entropy (void *buf,size_t nbytes); +* Returns: true if the input pool has been seeded. +* false if the input pool has not been seeded. +*/ +bool rng_is_initialized (void ) +{ +return crng_ready (); +} +EXPORT_SYMBOL (rng_is_initialized); -static void crng_reseed (void ); +/* Used by wait_for_random_bytes(), and considered an entropy collector, below. */ +static void try_to_generate_entropy (void );/* -* This function adds bytes into the entropy "pool". It does not -* update the entropy estimate. The caller should call -* credit_entropy_bits if this is appropriate. +* Wait for the input pool to be seeded and thus guaranteed to supply +* cryptographically secure random numbers. This applies to: the /dev/urandom +* device, the get_random_bytes function, and the get_random_{u32,u64,int,long} +* family of functions. Using any of these functions without first calling +* this function forfeits the guarantee of security. +* +* Returns: 0 if the input pool has been seeded. +* -ERESTARTSYS if the function was interrupted by a signal. */ -static void _mix_pool_bytes (const void *in,size_t nbytes) +int wait_for_random_bytes (void ) { -blake2s_update (&input_pool.hash, in, nbytes); -} +if (likely (crng_ready ())) +return 0 ; -static void mix_pool_bytes (const void *in,size_t nbytes) -{ -unsigned long flags; +do { +int ret; + ret =wait_event_interruptible_timeout (crng_init_wait,crng_ready (),HZ ); +if (ret) +return ret >0 ?0 : ret; -spin_lock_irqsave (&input_pool.lock, flags); -_mix_pool_bytes (in, nbytes); -spin_unlock_irqrestore (&input_pool.lock, flags); -} +try_to_generate_entropy (); + }while (!crng_ready ()); -struct fast_pool { -union { -u32 pool32[4 ]; -u64 pool64[2 ]; - }; -unsigned long last; -u16 reg_idx; -u8 count; -}; +return 0 ; +} +EXPORT_SYMBOL (wait_for_random_bytes);/* -* This is a fast mixing routine used by the interrupt randomness -* collector. It's hardcoded for an 128 bit pool and assumes that any -* locks that might be needed are taken by the caller. +* Add a callback function that will be invoked when the input +* pool is initialised. +* +* returns: 0 if callback is successfully added +* -EALREADY if pool is already initialised (callback not called) +* -ENOENT if module for callback is not alive */ -static void fast_mix (u32 pool[4 ]) +int add_random_ready_callback (struct random_ready_callback *rdy) { -u32 a = pool[0 ], b = pool[1 ]; -u32 c = pool[2 ], d = pool[3 ]; +struct module *owner; +unsigned long flags; +int err = -EALREADY ; - a += b; c += d; - b =rol32 (b,6 ); d =rol32 (d,27 ); - d ^= a; b ^= c; +if (crng_ready ()) +return err; - a += b; c += d; - b =rol32 (b,16 ); d =rol32 (d,14 ); - d ^= a; b ^= c; + owner = rdy->owner; +if (!try_module_get (owner)) +return -ENOENT ; - a += b; c += d; - b =rol32 (b,6 ); d =rol32 (d,27 ); - d ^= a; b ^= c; +spin_lock_irqsave (&random_ready_list_lock, flags); +if (crng_ready ()) + goto out; - a += b; c += d; - b =rol32 (b,16 ); d =rol32 (d,14 ); - d ^= a; b ^= c; + owner =NULL ; - pool[0 ] = a; pool[1 ] = b; - pool[2 ] = c; pool[3 ] = d; +list_add (&rdy->list, &random_ready_list); + err =0 ; + +out: +spin_unlock_irqrestore (&random_ready_list_lock, flags); + +module_put (owner); + +return err; +} +EXPORT_SYMBOL (add_random_ready_callback); + +/* +* Delete a previously registered readiness callback function. +*/ +void del_random_ready_callback (struct random_ready_callback *rdy) +{ +unsigned long flags; +struct module *owner =NULL ; + +spin_lock_irqsave (&random_ready_list_lock, flags); +if (!list_empty (&rdy->list)) { +list_del_init (&rdy->list); + owner = rdy->owner; + } +spin_unlock_irqrestore (&random_ready_list_lock, flags); + +module_put (owner); } +EXPORT_SYMBOL (del_random_ready_callback);static void process_random_ready_list (void ) {
@@ -338,27 +356,51 @@ static void process_random_ready_list(vospin_unlock_irqrestore (&random_ready_list_lock, flags); } -static void credit_entropy_bits (size_t nbits) +#define warn_unseeded_randomness (previous) \ + _warn_unseeded_randomness(__func__, (void *)_RET_IP_, (previous)) + +static void _warn_unseeded_randomness (const char *func_name,void *caller,void **previous) { -unsigned int entropy_count, orig, add; +#ifdef CONFIG_WARN_ALL_UNSEEDED_RANDOM +const bool print_once = false; +#else +static bool print_once__read_mostly ; +#endif -if (!nbits) +if (print_once ||crng_ready () || + (previous && (caller ==READ_ONCE (*previous))))return ; - - add =min_t (size_t, nbits,POOL_BITS ); - -do { - orig =READ_ONCE (input_pool.entropy_count); - entropy_count = min_t(unsigned int, POOL_BITS, orig + add); - }while (cmpxchg (&input_pool.entropy_count, orig, entropy_count) != orig); - -if (crng_init <2 && entropy_count >=POOL_MIN_BITS ) -crng_reseed (); +WRITE_ONCE (*previous, caller); +#ifndef CONFIG_WARN_ALL_UNSEEDED_RANDOM + print_once = true; +#endif +if (__ratelimit (&unseeded_warning)) +printk_deferred (KERN_NOTICE "random: %s called from %pS with crng_init=%d\n" , + func_name, caller, crng_init); } +/********************************************************************* * -* CRNG using CHACHA20 +* Fast key erasure RNG, the "crng". +* +* These functions expand entropy from the entropy extractor into +* long streams for external consumption using the "fast key erasure" +* RNG described at <https://blog.cr.yp.to/20170723-random.html>. +* +* There are a few exported interfaces for use by other drivers: +* +* void get_random_bytes(void *buf, size_t nbytes) +* u32 get_random_u32() +* u64 get_random_u64() +* unsigned int get_random_int() +* unsigned long get_random_long() +* +* These interfaces will return the requested number of random bytes +* into the given buffer or as a return value. This is equivalent to +* a read from /dev/urandom. The integer family of functions may be +* higher performance for one-off random integers, because they do a +* bit of buffering. * *********************************************************************/
@@ -385,72 +427,14 @@ static DEFINE_PER_CPU(struct crng, crngs .generation = ULONG_MAX }; -static DECLARE_WAIT_QUEUE_HEAD (crng_init_wait); +/* Used by crng_reseed() to extract a new seed from the input pool. */ +static bool drain_entropy (void *buf,size_t nbytes);/* -* crng_fast_load() can be called by code in the interrupt service -* path. So we can't afford to dilly-dally. Returns the number of -* bytes processed from cp. +* This extracts a new crng key from the input pool, but only if there is a +* sufficient amount of entropy available, in order to mitigate bruteforcing +* of newly added bits. */ -static size_t crng_fast_load (const void *cp,size_t len) -{ -unsigned long flags; -const u8 *src = (const u8 *)cp; -size_t ret =0 ; - -if (!spin_trylock_irqsave (&base_crng.lock, flags)) -return 0 ; -if (crng_init !=0 ) { -spin_unlock_irqrestore (&base_crng.lock, flags); -return 0 ; - } -while (len >0 && crng_init_cnt <CRNG_INIT_CNT_THRESH ) { - base_crng.key[crng_init_cnt %sizeof (base_crng.key)] ^= *src; - src++; crng_init_cnt++; len--; ret++; - } -if (crng_init_cnt >=CRNG_INIT_CNT_THRESH ) { - ++base_crng.generation; - crng_init =1 ; - } -spin_unlock_irqrestore (&base_crng.lock, flags); -if (crng_init ==1 ) -pr_notice ("fast init done\n" ); -return ret; -} - -/* -* crng_slow_load() is called by add_device_randomness, which has two -* attributes. (1) We can't trust the buffer passed to it is -* guaranteed to be unpredictable (so it might not have any entropy at -* all), and (2) it doesn't have the performance constraints of -* crng_fast_load(). -* -* So, we simply hash the contents in with the current key. Finally, -* we do *not* advance crng_init_cnt since buffer we may get may be -* something like a fixed DMI table (for example), which might very -* well be unique to the machine, but is otherwise unvarying. -*/ -static void crng_slow_load (const void *cp,size_t len) -{ -unsigned long flags; -struct blake2s_state hash; - -blake2s_init (&hash,sizeof (base_crng.key)); - -if (!spin_trylock_irqsave (&base_crng.lock, flags)) -return ; -if (crng_init !=0 ) { -spin_unlock_irqrestore (&base_crng.lock, flags); -return ; - } - -blake2s_update (&hash, base_crng.key,sizeof (base_crng.key)); -blake2s_update (&hash, cp, len); -blake2s_final (&hash, base_crng.key); - -spin_unlock_irqrestore (&base_crng.lock, flags); -} -static void crng_reseed (void ) {unsigned long flags;
@@ -500,13 +484,11 @@ static void crng_reseed(void) }/* -* The general form here is based on a "fast key erasure RNG" from -* <https://blog.cr.yp.to/20170723-random.html>. It generates a ChaCha -* block using the provided key, and then immediately overwites that -* key with half the block. It returns the resultant ChaCha state to the -* user, along with the second half of the block containing 32 bytes of -* random data that may be used; random_data_len may not be greater than -* 32. +* This generates a ChaCha block using the provided key, and then +* immediately overwites that key with half the block. It returns +* the resultant ChaCha state to the user, along with the second +* half of the block containing 32 bytes of random data that may +* be used; random_data_len may not be greater than 32. */ static void crng_fast_key_erasure(u8 key[CHACHA20_KEY_SIZE], u32 chacha_state[CHACHA20_BLOCK_SIZE / sizeof(u32)],
@@ -593,6 +575,126 @@ static void crng_make_state(u32 chacha_slocal_irq_restore (flags); } +/* +* This function is for crng_init == 0 only. +* +* crng_fast_load() can be called by code in the interrupt service +* path. So we can't afford to dilly-dally. Returns the number of +* bytes processed from cp. +*/ +static size_t crng_fast_load (const void *cp,size_t len) +{ +static int crng_init_cnt =0 ; +unsigned long flags; +const u8 *src = (const u8 *)cp; +size_t ret =0 ; + +if (!spin_trylock_irqsave (&base_crng.lock, flags)) +return 0 ; +if (crng_init !=0 ) { +spin_unlock_irqrestore (&base_crng.lock, flags); +return 0 ; + } +while (len >0 && crng_init_cnt <CRNG_INIT_CNT_THRESH ) { + base_crng.key[crng_init_cnt %sizeof (base_crng.key)] ^= *src; + src++; crng_init_cnt++; len--; ret++; + } +if (crng_init_cnt >=CRNG_INIT_CNT_THRESH ) { + ++base_crng.generation; + crng_init =1 ; + } +spin_unlock_irqrestore (&base_crng.lock, flags); +if (crng_init ==1 ) +pr_notice ("fast init done\n" ); +return ret; +} + +/* +* This function is for crng_init == 0 only. +* +* crng_slow_load() is called by add_device_randomness, which has two +* attributes. (1) We can't trust the buffer passed to it is +* guaranteed to be unpredictable (so it might not have any entropy at +* all), and (2) it doesn't have the performance constraints of +* crng_fast_load(). +* +* So, we simply hash the contents in with the current key. Finally, +* we do *not* advance crng_init_cnt since buffer we may get may be +* something like a fixed DMI table (for example), which might very +* well be unique to the machine, but is otherwise unvarying. +*/ +static void crng_slow_load (const void *cp,size_t len) +{ +unsigned long flags; +struct blake2s_state hash; + +blake2s_init (&hash,sizeof (base_crng.key)); + +if (!spin_trylock_irqsave (&base_crng.lock, flags)) +return ; +if (crng_init !=0 ) { +spin_unlock_irqrestore (&base_crng.lock, flags); +return ; + } + +blake2s_update (&hash, base_crng.key,sizeof (base_crng.key)); +blake2s_update (&hash, cp, len); +blake2s_final (&hash, base_crng.key); + +spin_unlock_irqrestore (&base_crng.lock, flags); +} + +static void _get_random_bytes (void *buf,size_t nbytes) +{ +u32 chacha_state[CHACHA20_BLOCK_SIZE /sizeof (u32)]; +u8 tmp[CHACHA20_BLOCK_SIZE ]; +size_t len; + +if (!nbytes) +return ; + + len =min_t (size_t,32 , nbytes); +crng_make_state (chacha_state, buf, len); + nbytes -= len; + buf += len; + +while (nbytes) { +if (nbytes <CHACHA20_BLOCK_SIZE ) { +chacha20_block (chacha_state, tmp); +memcpy (buf, tmp, nbytes); +memzero_explicit (tmp,sizeof (tmp)); +break ; + } + +chacha20_block (chacha_state, buf); +if (unlikely (chacha_state[12 ] ==0 )) + ++chacha_state[13 ]; + nbytes -=CHACHA20_BLOCK_SIZE ; + buf +=CHACHA20_BLOCK_SIZE ; + } + +memzero_explicit (chacha_state,sizeof (chacha_state)); +} + +/* +* This function is the exported kernel interface. It returns some +* number of good random numbers, suitable for key generation, seeding +* TCP sequence numbers, etc. It does not rely on the hardware random +* number generator. For random bytes direct from the hardware RNG +* (when available), use get_random_bytes_arch(). In order to ensure +* that the randomness provided by this function is okay, the function +* wait_for_random_bytes() should be called and return 0 at least once +* at any point prior. +*/ +void get_random_bytes (void *buf,size_t nbytes) +{ +static void *previous; + +warn_unseeded_randomness (&previous); +_get_random_bytes (buf, nbytes); +} +EXPORT_SYMBOL (get_random_bytes); +static ssize_t get_random_bytes_user (void __user *buf,size_t nbytes) {bool large_request = nbytes >256 ;
@@ -640,6 +742,265 @@ static ssize_t get_random_bytes_user(voireturn ret; } +/* +* Batched entropy returns random integers. The quality of the random +* number is good as /dev/urandom. In order to ensure that the randomness +* provided by this function is okay, the function wait_for_random_bytes() +* should be called and return 0 at least once at any point prior. +*/ +struct batched_entropy { +union { +/* +* We make this 1.5x a ChaCha block, so that we get the +* remaining 32 bytes from fast key erasure, plus one full +* block from the detached ChaCha state. We can increase +* the size of this later if needed so long as we keep the +* formula of (integer_blocks + 0.5) * CHACHA20_BLOCK_SIZE. +*/ +u64 entropy_u64[CHACHA20_BLOCK_SIZE *3 / (2 *sizeof (u64))]; +u32 entropy_u32[CHACHA20_BLOCK_SIZE *3 / (2 *sizeof (u32))]; + }; +unsigned long generation; +unsigned int position; +}; + + +static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64) = { + .position =UINT_MAX +}; + +u64 get_random_u64 (void ) +{ +u64 ret; +unsigned long flags; +struct batched_entropy *batch; +static void *previous; +unsigned long next_gen; + +warn_unseeded_randomness (&previous); + +local_irq_save (flags); + batch =raw_cpu_ptr (&batched_entropy_u64); + + next_gen =READ_ONCE (base_crng.generation); +if (batch->position >=ARRAY_SIZE (batch->entropy_u64) || + next_gen != batch->generation) { +_get_random_bytes (batch->entropy_u64,sizeof (batch->entropy_u64)); + batch->position =0 ; + batch->generation = next_gen; + } + + ret = batch->entropy_u64[batch->position]; + batch->entropy_u64[batch->position] =0 ; + ++batch->position; +local_irq_restore (flags); +return ret; +} +EXPORT_SYMBOL (get_random_u64); + +static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32) = { + .position =UINT_MAX +}; + +u32 get_random_u32 (void ) +{ +u32 ret; +unsigned long flags; +struct batched_entropy *batch; +static void *previous; +unsigned long next_gen; + +warn_unseeded_randomness (&previous); + +local_irq_save (flags); + batch =raw_cpu_ptr (&batched_entropy_u32); + + next_gen =READ_ONCE (base_crng.generation); +if (batch->position >=ARRAY_SIZE (batch->entropy_u32) || + next_gen != batch->generation) { +_get_random_bytes (batch->entropy_u32,sizeof (batch->entropy_u32)); + batch->position =0 ; + batch->generation = next_gen; + } + + ret = batch->entropy_u32[batch->position]; + batch->entropy_u32[batch->position] =0 ; + ++batch->position; +local_irq_restore (flags); +return ret; +} +EXPORT_SYMBOL (get_random_u32); + +/** +* randomize_page - Generate a random, page aligned address +* @start: The smallest acceptable address the caller will take. +* @range: The size of the area, starting at @start, within which the +* random address must fall. +* +* If @start + @range would overflow, @range is capped. +* +* NOTE: Historical use of randomize_range, which this replaces, presumed that +* @start was already page aligned. We now align it regardless. +* +* Return: A page aligned address within [start, start + range). On error, +* @start is returned. +*/ +unsigned long randomize_page (unsigned long start,unsigned long range) +{ +if (!PAGE_ALIGNED (start)) { + range -=PAGE_ALIGN (start) - start; + start =PAGE_ALIGN (start); + } + +if (start >ULONG_MAX - range) + range =ULONG_MAX - start; + + range >>=PAGE_SHIFT ; + +if (range ==0 ) +return start; + +return start + (get_random_long () % range <<PAGE_SHIFT ); +} + +/* +* This function will use the architecture-specific hardware random +* number generator if it is available. It is not recommended for +* use. Use get_random_bytes() instead. It returns the number of +* bytes filled in. +*/ +size_t __must_check get_random_bytes_arch (void *buf,size_t nbytes) +{ +size_t left = nbytes; +u8 *p = buf; + +while (left) { +unsigned long v; +size_t chunk =min_t (size_t, left,sizeof (unsigned long )); + +if (!arch_get_random_long (&v)) +break ; + +memcpy (p, &v, chunk); + p += chunk; + left -= chunk; + } + +return nbytes - left; +} +EXPORT_SYMBOL (get_random_bytes_arch); + +enum { +POOL_BITS =BLAKE2S_HASH_SIZE *8 , +POOL_MIN_BITS =POOL_BITS /* No point in settling for less. */ +}; + +/* +* Static global variables +*/ +static DECLARE_WAIT_QUEUE_HEAD (random_write_wait); + +/********************************************************************** +* +* OS independent entropy store. Here are the functions which handle +* storing entropy in an entropy pool. +* +**********************************************************************/ + +static struct { +struct blake2s_state hash; +spinlock_t lock; +unsigned int entropy_count; +} input_pool = { + .hash.h = {BLAKE2S_IV0 ^ (0x01010000 |BLAKE2S_HASH_SIZE ), +BLAKE2S_IV1 ,BLAKE2S_IV2 ,BLAKE2S_IV3 ,BLAKE2S_IV4 , +BLAKE2S_IV5 ,BLAKE2S_IV6 ,BLAKE2S_IV7 }, + .hash.outlen =BLAKE2S_HASH_SIZE , + .lock =__SPIN_LOCK_UNLOCKED (input_pool.lock), +}; + +static void extract_entropy (void *buf,size_t nbytes); +static bool drain_entropy (void *buf,size_t nbytes); + +static void crng_reseed (void ); + +/* +* This function adds bytes into the entropy "pool". It does not +* update the entropy estimate. The caller should call +* credit_entropy_bits if this is appropriate. +*/ +static void _mix_pool_bytes (const void *in,size_t nbytes) +{ +blake2s_update (&input_pool.hash, in, nbytes); +} + +static void mix_pool_bytes (const void *in,size_t nbytes) +{ +unsigned long flags; + +spin_lock_irqsave (&input_pool.lock, flags); +_mix_pool_bytes (in, nbytes); +spin_unlock_irqrestore (&input_pool.lock, flags); +} + +struct fast_pool { +union { +u32 pool32[4 ]; +u64 pool64[2 ]; + }; +unsigned long last; +u16 reg_idx; +u8 count; +}; + +/* +* This is a fast mixing routine used by the interrupt randomness +* collector. It's hardcoded for an 128 bit pool and assumes that any +* locks that might be needed are taken by the caller. +*/ +static void fast_mix (u32 pool[4 ]) +{ +u32 a = pool[0 ], b = pool[1 ]; +u32 c = pool[2 ], d = pool[3 ]; + + a += b; c += d; + b =rol32 (b,6 ); d =rol32 (d,27 ); + d ^= a; b ^= c; + + a += b; c += d; + b =rol32 (b,16 ); d =rol32 (d,14 ); + d ^= a; b ^= c; + + a += b; c += d; + b =rol32 (b,6 ); d =rol32 (d,27 ); + d ^= a; b ^= c; + + a += b; c += d; + b =rol32 (b,16 ); d =rol32 (d,14 ); + d ^= a; b ^= c; + + pool[0 ] = a; pool[1 ] = b; + pool[2 ] = c; pool[3 ] = d; +} + +static void credit_entropy_bits (size_t nbits) +{ +unsigned int entropy_count, orig, add; + +if (!nbits) +return ; + + add =min_t (size_t, nbits,POOL_BITS ); + +do { + orig =READ_ONCE (input_pool.entropy_count); + entropy_count = min_t(unsigned int, POOL_BITS, orig + add); + }while (cmpxchg (&input_pool.entropy_count, orig, entropy_count) != orig); + +if (crng_init <2 && entropy_count >=POOL_MIN_BITS ) +crng_reseed (); +} +/********************************************************************* * * Entropy input management
@@ -907,80 +1268,6 @@ static bool drain_entropy(void *buf, sizreturn true; } -#define warn_unseeded_randomness (previous) \ - _warn_unseeded_randomness(__func__, (void *)_RET_IP_, (previous)) - -static void _warn_unseeded_randomness (const char *func_name,void *caller,void **previous) -{ -#ifdef CONFIG_WARN_ALL_UNSEEDED_RANDOM -const bool print_once = false; -#else -static bool print_once__read_mostly ; -#endif - -if (print_once ||crng_ready () || - (previous && (caller ==READ_ONCE (*previous)))) -return ; -WRITE_ONCE (*previous, caller); -#ifndef CONFIG_WARN_ALL_UNSEEDED_RANDOM - print_once = true; -#endif -if (__ratelimit (&unseeded_warning)) -printk_deferred (KERN_NOTICE "random: %s called from %pS with crng_init=%d\n" , - func_name, caller, crng_init); -} - -/* -* This function is the exported kernel interface. It returns some -* number of good random numbers, suitable for key generation, seeding -* TCP sequence numbers, etc. It does not rely on the hardware random -* number generator. For random bytes direct from the hardware RNG -* (when available), use get_random_bytes_arch(). In order to ensure -* that the randomness provided by this function is okay, the function -* wait_for_random_bytes() should be called and return 0 at least once -* at any point prior. -*/ -static void _get_random_bytes (void *buf,size_t nbytes) -{ -u32 chacha_state[CHACHA20_BLOCK_SIZE /sizeof (u32)]; -u8 tmp[CHACHA20_BLOCK_SIZE ]; -size_t len; - -if (!nbytes) -return ; - - len =min_t (size_t,32 , nbytes); -crng_make_state (chacha_state, buf, len); - nbytes -= len; - buf += len; - -while (nbytes) { -if (nbytes <CHACHA20_BLOCK_SIZE ) { -chacha20_block (chacha_state, tmp); -memcpy (buf, tmp, nbytes); -memzero_explicit (tmp,sizeof (tmp)); -break ; - } - -chacha20_block (chacha_state, buf); -if (unlikely (chacha_state[12 ] ==0 )) - ++chacha_state[13 ]; - nbytes -=CHACHA20_BLOCK_SIZE ; - buf +=CHACHA20_BLOCK_SIZE ; - } - -memzero_explicit (chacha_state,sizeof (chacha_state)); -} - -void get_random_bytes (void *buf,size_t nbytes) -{ -static void *previous; - -warn_unseeded_randomness (&previous); -_get_random_bytes (buf, nbytes); -} -EXPORT_SYMBOL (get_random_bytes); -/* * Each time the timer fires, we expect that we got an unpredictable * jump in the cycle counter. Even if the timer is running on another
@@ -1030,134 +1317,6 @@ static void try_to_generate_entropy(voidmix_pool_bytes (&stack.now,sizeof (stack.now)); } -/* -* Wait for the urandom pool to be seeded and thus guaranteed to supply -* cryptographically secure random numbers. This applies to: the /dev/urandom -* device, the get_random_bytes function, and the get_random_{u32,u64,int,long} -* family of functions. Using any of these functions without first calling -* this function forfeits the guarantee of security. -* -* Returns: 0 if the urandom pool has been seeded. -* -ERESTARTSYS if the function was interrupted by a signal. -*/ -int wait_for_random_bytes (void ) -{ -if (likely (crng_ready ())) -return 0 ; - -do { -int ret; - ret =wait_event_interruptible_timeout (crng_init_wait,crng_ready (),HZ ); -if (ret) -return ret >0 ?0 : ret; - -try_to_generate_entropy (); - }while (!crng_ready ()); - -return 0 ; -} -EXPORT_SYMBOL (wait_for_random_bytes); - -/* -* Returns whether or not the urandom pool has been seeded and thus guaranteed -* to supply cryptographically secure random numbers. This applies to: the -* /dev/urandom device, the get_random_bytes function, and the get_random_{u32, -* ,u64,int,long} family of functions. -* -* Returns: true if the urandom pool has been seeded. -* false if the urandom pool has not been seeded. -*/ -bool rng_is_initialized (void ) -{ -return crng_ready (); -} -EXPORT_SYMBOL (rng_is_initialized); - -/* -* Add a callback function that will be invoked when the nonblocking -* pool is initialised. -* -* returns: 0 if callback is successfully added -* -EALREADY if pool is already initialised (callback not called) -* -ENOENT if module for callback is not alive -*/ -int add_random_ready_callback (struct random_ready_callback *rdy) -{ -struct module *owner; -unsigned long flags; -int err = -EALREADY ; - -if (crng_ready ()) -return err; - - owner = rdy->owner; -if (!try_module_get (owner)) -return -ENOENT ; - -spin_lock_irqsave (&random_ready_list_lock, flags); -if (crng_ready ()) - goto out; - - owner =NULL ; - -list_add (&rdy->list, &random_ready_list); - err =0 ; - -out: -spin_unlock_irqrestore (&random_ready_list_lock, flags); - -module_put (owner); - -return err; -} -EXPORT_SYMBOL (add_random_ready_callback); - -/* -* Delete a previously registered readiness callback function. -*/ -void del_random_ready_callback (struct random_ready_callback *rdy) -{ -unsigned long flags; -struct module *owner =NULL ; - -spin_lock_irqsave (&random_ready_list_lock, flags); -if (!list_empty (&rdy->list)) { -list_del_init (&rdy->list); - owner = rdy->owner; - } -spin_unlock_irqrestore (&random_ready_list_lock, flags); - -module_put (owner); -} -EXPORT_SYMBOL (del_random_ready_callback); - -/* -* This function will use the architecture-specific hardware random -* number generator if it is available. It is not recommended for -* use. Use get_random_bytes() instead. It returns the number of -* bytes filled in. -*/ -size_t __must_check get_random_bytes_arch (void *buf,size_t nbytes) -{ -size_t left = nbytes; -u8 *p = buf; - -while (left) { -unsigned long v; -size_t chunk =min_t (size_t, left,sizeof (unsigned long )); - -if (!arch_get_random_long (&v)) -break ; - -memcpy (p, &v, chunk); - p += chunk; - left -= chunk; - } - -return nbytes - left; -} -EXPORT_SYMBOL (get_random_bytes_arch); -static bool trust_cpu__ro_after_init =IS_ENABLED (CONFIG_RANDOM_TRUST_CPU );static int __init parse_trust_cpu (char *arg) {
@@ -1508,126 +1667,6 @@ struct ctl_table random_table[] = { };#endif /* CONFIG_SYSCTL */ -struct batched_entropy { -union { -/* -* We make this 1.5x a ChaCha block, so that we get the -* remaining 32 bytes from fast key erasure, plus one full -* block from the detached ChaCha state. We can increase -* the size of this later if needed so long as we keep the -* formula of (integer_blocks + 0.5) * CHACHA20_BLOCK_SIZE. -*/ -u64 entropy_u64[CHACHA20_BLOCK_SIZE *3 / (2 *sizeof (u64))]; -u32 entropy_u32[CHACHA20_BLOCK_SIZE *3 / (2 *sizeof (u32))]; - }; -unsigned long generation; -unsigned int position; -}; - -/* -* Get a random word for internal kernel use only. The quality of the random -* number is good as /dev/urandom. In order to ensure that the randomness -* provided by this function is okay, the function wait_for_random_bytes() -* should be called and return 0 at least once at any point prior. -*/ -static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u64) = { - .position =UINT_MAX -}; - -u64 get_random_u64 (void ) -{ -u64 ret; -unsigned long flags; -struct batched_entropy *batch; -static void *previous; -unsigned long next_gen; - -warn_unseeded_randomness (&previous); - -local_irq_save (flags); - batch =raw_cpu_ptr (&batched_entropy_u64); - - next_gen =READ_ONCE (base_crng.generation); -if (batch->position >=ARRAY_SIZE (batch->entropy_u64) || - next_gen != batch->generation) { -_get_random_bytes (batch->entropy_u64,sizeof (batch->entropy_u64)); - batch->position =0 ; - batch->generation = next_gen; - } - - ret = batch->entropy_u64[batch->position]; - batch->entropy_u64[batch->position] =0 ; - ++batch->position; -local_irq_restore (flags); -return ret; -} -EXPORT_SYMBOL (get_random_u64); - -static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_u32) = { - .position =UINT_MAX -}; - -u32 get_random_u32 (void ) -{ -u32 ret; -unsigned long flags; -struct batched_entropy *batch; -static void *previous; -unsigned long next_gen; - -warn_unseeded_randomness (&previous); - -local_irq_save (flags); - batch =raw_cpu_ptr (&batched_entropy_u32); - - next_gen =READ_ONCE (base_crng.generation); -if (batch->position >=ARRAY_SIZE (batch->entropy_u32) || - next_gen != batch->generation) { -_get_random_bytes (batch->entropy_u32,sizeof (batch->entropy_u32)); - batch->position =0 ; - batch->generation = next_gen; - } - - ret = batch->entropy_u32[batch->position]; - batch->entropy_u32[batch->position] =0 ; - ++batch->position; -local_irq_restore (flags); -return ret; -} -EXPORT_SYMBOL (get_random_u32); - -/** -* randomize_page - Generate a random, page aligned address -* @start: The smallest acceptable address the caller will take. -* @range: The size of the area, starting at @start, within which the -* random address must fall. -* -* If @start + @range would overflow, @range is capped. -* -* NOTE: Historical use of randomize_range, which this replaces, presumed that -* @start was already page aligned. We now align it regardless. -* -* Return: A page aligned address within [start, start + range). On error, -* @start is returned. -*/ -unsigned long randomize_page (unsigned long start,unsigned long range) -{ -if (!PAGE_ALIGNED (start)) { - range -=PAGE_ALIGN (start) - start; - start =PAGE_ALIGN (start); - } - -if (start >ULONG_MAX - range) - range =ULONG_MAX - start; - - range >>=PAGE_SHIFT ; - -if (range ==0 ) -return start; - -return start + (get_random_long () % range <<PAGE_SHIFT ); -} -/* Interface for in-kernel drivers of true hardware RNGs. * Those devices may produce endless random bits and will be throttled * when our pool is full.
Greg Kroah-Hartman
maintainerPATCH 100/234 random: group entropy extraction functions
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit a5ed7cb1a7732ef11959332d507889fbc39ebbb4 upstream.
This pulls all of the entropy extraction-focused functions into the third labeled section.
No functional changes.
--- drivers/char/random.c | 216 +++++++++++++++++++++++++------------------------- 1 file changed, 109 insertions(+), 107 deletions(-)
drivers/char/random.c +109 -107
@@ -890,23 +890,36 @@ size_t __must_check get_random_bytes_arc }EXPORT_SYMBOL (get_random_bytes_arch); + +/********************************************************************** +* +* Entropy accumulation and extraction routines. +* +* Callers may add entropy via: +* +* static void mix_pool_bytes(const void *in, size_t nbytes) +* +* After which, if added entropy should be credited: +* +* static void credit_entropy_bits(size_t nbits) +* +* Finally, extract entropy via these two, with the latter one +* setting the entropy count to zero and extracting only if there +* is POOL_MIN_BITS entropy credited prior: +* +* static void extract_entropy(void *buf, size_t nbytes) +* static bool drain_entropy(void *buf, size_t nbytes) +* +**********************************************************************/ +enum {POOL_BITS =BLAKE2S_HASH_SIZE *8 ,POOL_MIN_BITS =POOL_BITS /* No point in settling for less. */ }; -/* -* Static global variables -*/ +/* For notifying userspace should write into /dev/random. */ static DECLARE_WAIT_QUEUE_HEAD (random_write_wait); -/********************************************************************** -* -* OS independent entropy store. Here are the functions which handle -* storing entropy in an entropy pool. -* -**********************************************************************/ - static struct { struct blake2s_state hash; spinlock_t lock;
@@ -919,28 +932,106 @@ static struct { .lock = __SPIN_LOCK_UNLOCKED(input_pool.lock), }; -static void extract_entropy (void *buf,size_t nbytes); -static bool drain_entropy (void *buf,size_t nbytes); - -static void crng_reseed (void ); +static void _mix_pool_bytes (const void *in,size_t nbytes) +{ +blake2s_update (&input_pool.hash, in, nbytes); +}/* * This function adds bytes into the entropy "pool". It does not * update the entropy estimate. The caller should call * credit_entropy_bits if this is appropriate. */ -static void _mix_pool_bytes (const void *in,size_t nbytes) +static void mix_pool_bytes (const void *in,size_t nbytes) { -blake2s_update (&input_pool.hash, in, nbytes); +unsigned long flags; + +spin_lock_irqsave (&input_pool.lock, flags); +_mix_pool_bytes (in, nbytes); +spin_unlock_irqrestore (&input_pool.lock, flags); } -static void mix_pool_bytes (const void *in,size_t nbytes) +static void credit_entropy_bits (size_t nbits) +{ +unsigned int entropy_count, orig, add; + +if (!nbits) +return ; + + add =min_t (size_t, nbits,POOL_BITS ); + +do { + orig =READ_ONCE (input_pool.entropy_count); + entropy_count = min_t(unsigned int, POOL_BITS, orig + add); + }while (cmpxchg (&input_pool.entropy_count, orig, entropy_count) != orig); + +if (crng_init <2 && entropy_count >=POOL_MIN_BITS ) +crng_reseed (); +} + +/* +* This is an HKDF-like construction for using the hashed collected entropy +* as a PRF key, that's then expanded block-by-block. +*/ +static void extract_entropy (void *buf,size_t nbytes) {unsigned long flags; +u8 seed[BLAKE2S_HASH_SIZE ], next_key[BLAKE2S_HASH_SIZE ]; +struct { +unsigned long rdseed[32 /sizeof (long )]; +size_t counter; + } block; +size_t i; + +for (i =0 ; i <ARRAY_SIZE (block.rdseed); ++i) { +if (!arch_get_random_seed_long (&block.rdseed[i]) && + !arch_get_random_long (&block.rdseed[i])) + block.rdseed[i] =random_get_entropy (); + }spin_lock_irqsave (&input_pool.lock, flags); -_mix_pool_bytes (in, nbytes); + +/* seed = HASHPRF(last_key, entropy_input) */ +blake2s_final (&input_pool.hash, seed); + +/* next_key = HASHPRF(seed, RDSEED || 0) */ + block.counter =0 ; +blake2s (next_key, (u8 *)&block, seed,sizeof (next_key),sizeof (block),sizeof (seed)); +blake2s_init_key (&input_pool.hash,BLAKE2S_HASH_SIZE , next_key,sizeof (next_key)); +spin_unlock_irqrestore (&input_pool.lock, flags); +memzero_explicit (next_key,sizeof (next_key)); + +while (nbytes) { + i =min_t (size_t, nbytes,BLAKE2S_HASH_SIZE ); +/* output = HASHPRF(seed, RDSEED || ++counter) */ + ++block.counter; +blake2s (buf, (u8 *)&block, seed, i,sizeof (block),sizeof (seed)); + nbytes -= i; + buf += i; + } + +memzero_explicit (seed,sizeof (seed)); +memzero_explicit (&block,sizeof (block)); +} + +/* +* First we make sure we have POOL_MIN_BITS of entropy in the pool, and then we +* set the entropy count to zero (but don't actually touch any data). Only then +* can we extract a new key with extract_entropy(). +*/ +static bool drain_entropy (void *buf,size_t nbytes) +{ +unsigned int entropy_count; +do { + entropy_count =READ_ONCE (input_pool.entropy_count); +if (entropy_count <POOL_MIN_BITS ) +return false; + }while (cmpxchg (&input_pool.entropy_count, entropy_count,0 ) != entropy_count); +extract_entropy (buf, nbytes); +wake_up_interruptible (&random_write_wait); +kill_fasync (&fasync,SIGIO ,POLL_OUT ); +return true; }struct fast_pool {
@@ -983,24 +1074,6 @@ static void fast_mix(u32 pool[4]) pool[2 ] = c; pool[3 ] = d; } -static void credit_entropy_bits (size_t nbits) -{ -unsigned int entropy_count, orig, add; - -if (!nbits) -return ; - - add =min_t (size_t, nbits,POOL_BITS ); - -do { - orig =READ_ONCE (input_pool.entropy_count); - entropy_count = min_t(unsigned int, POOL_BITS, orig + add); - }while (cmpxchg (&input_pool.entropy_count, orig, entropy_count) != orig); - -if (crng_init <2 && entropy_count >=POOL_MIN_BITS ) -crng_reseed (); -} -/********************************************************************* * * Entropy input management
@@ -1197,77 +1270,6 @@ void add_disk_randomness(struct gendiskEXPORT_SYMBOL_GPL (add_disk_randomness);#endif -/********************************************************************* -* -* Entropy extraction routines -* -*********************************************************************/ - -/* -* This is an HKDF-like construction for using the hashed collected entropy -* as a PRF key, that's then expanded block-by-block. -*/ -static void extract_entropy (void *buf,size_t nbytes) -{ -unsigned long flags; -u8 seed[BLAKE2S_HASH_SIZE ], next_key[BLAKE2S_HASH_SIZE ]; -struct { -unsigned long rdseed[32 /sizeof (long )]; -size_t counter; - } block; -size_t i; - -for (i =0 ; i <ARRAY_SIZE (block.rdseed); ++i) { -if (!arch_get_random_seed_long (&block.rdseed[i]) && - !arch_get_random_long (&block.rdseed[i])) - block.rdseed[i] =random_get_entropy (); - } - -spin_lock_irqsave (&input_pool.lock, flags); - -/* seed = HASHPRF(last_key, entropy_input) */ -blake2s_final (&input_pool.hash, seed); - -/* next_key = HASHPRF(seed, RDSEED || 0) */ - block.counter =0 ; -blake2s (next_key, (u8 *)&block, seed,sizeof (next_key),sizeof (block),sizeof (seed)); -blake2s_init_key (&input_pool.hash,BLAKE2S_HASH_SIZE , next_key,sizeof (next_key)); - -spin_unlock_irqrestore (&input_pool.lock, flags); -memzero_explicit (next_key,sizeof (next_key)); - -while (nbytes) { - i =min_t (size_t, nbytes,BLAKE2S_HASH_SIZE ); -/* output = HASHPRF(seed, RDSEED || ++counter) */ - ++block.counter; -blake2s (buf, (u8 *)&block, seed, i,sizeof (block),sizeof (seed)); - nbytes -= i; - buf += i; - } - -memzero_explicit (seed,sizeof (seed)); -memzero_explicit (&block,sizeof (block)); -} - -/* -* First we make sure we have POOL_MIN_BITS of entropy in the pool, and then we -* set the entropy count to zero (but don't actually touch any data). Only then -* can we extract a new key with extract_entropy(). -*/ -static bool drain_entropy (void *buf,size_t nbytes) -{ -unsigned int entropy_count; -do { - entropy_count =READ_ONCE (input_pool.entropy_count); -if (entropy_count <POOL_MIN_BITS ) -return false; - }while (cmpxchg (&input_pool.entropy_count, entropy_count,0 ) != entropy_count); -extract_entropy (buf, nbytes); -wake_up_interruptible (&random_write_wait); -kill_fasync (&fasync,SIGIO ,POLL_OUT ); -return true; -} -/* * Each time the timer fires, we expect that we got an unpredictable * jump in the cycle counter. Even if the timer is running on another
Greg Kroah-Hartman
maintainerPATCH 101/234 random: group entropy collection functions
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 92c653cf14400946f376a29b828d6af7e01f38dd upstream.
This pulls all of the entropy collection-focused functions into the fourth labeled section.
No functional changes.
--- drivers/char/random.c | 370 +++++++++++++++++++++++++++----------------------- 1 file changed, 206 insertions(+), 164 deletions(-)
drivers/char/random.c +206 -164
@@ -1034,60 +1034,112 @@ static bool drain_entropy(void *buf, sizreturn true; } -struct fast_pool { -union { -u32 pool32[4 ]; -u64 pool64[2 ]; - }; -unsigned long last; -u16 reg_idx; -u8 count; -}; + +/********************************************************************** +* +* Entropy collection routines. +* +* The following exported functions are used for pushing entropy into +* the above entropy accumulation routines: +* +* void add_device_randomness(const void *buf, size_t size); +* void add_input_randomness(unsigned int type, unsigned int code, +* unsigned int value); +* void add_disk_randomness(struct gendisk *disk); +* void add_hwgenerator_randomness(const void *buffer, size_t count, +* size_t entropy); +* void add_bootloader_randomness(const void *buf, size_t size); +* void add_interrupt_randomness(int irq); +* +* add_device_randomness() adds data to the input pool that +* is likely to differ between two devices (or possibly even per boot). +* This would be things like MAC addresses or serial numbers, or the +* read-out of the RTC. This does *not* credit any actual entropy to +* the pool, but it initializes the pool to different values for devices +* that might otherwise be identical and have very little entropy +* available to them (particularly common in the embedded world). +* +* add_input_randomness() uses the input layer interrupt timing, as well +* as the event type information from the hardware. +* +* add_disk_randomness() uses what amounts to the seek time of block +* layer request events, on a per-disk_devt basis, as input to the +* entropy pool. Note that high-speed solid state drives with very low +* seek times do not make for good sources of entropy, as their seek +* times are usually fairly consistent. +* +* The above two routines try to estimate how many bits of entropy +* to credit. They do this by keeping track of the first and second +* order deltas of the event timings. +* +* add_hwgenerator_randomness() is for true hardware RNGs, and will credit +* entropy as specified by the caller. If the entropy pool is full it will +* block until more entropy is needed. +* +* add_bootloader_randomness() is the same as add_hwgenerator_randomness() or +* add_device_randomness(), depending on whether or not the configuration +* option CONFIG_RANDOM_TRUST_BOOTLOADER is set. +* +* add_interrupt_randomness() uses the interrupt timing as random +* inputs to the entropy pool. Using the cycle counters and the irq source +* as inputs, it feeds the input pool roughly once a second or after 64 +* interrupts, crediting 1 bit of entropy for whichever comes first. +* +**********************************************************************/ + +static bool trust_cpu__ro_after_init =IS_ENABLED (CONFIG_RANDOM_TRUST_CPU ); +static int __init parse_trust_cpu (char *arg) +{ +return kstrtobool (arg, &trust_cpu); +} +early_param ("random.trust_cpu" , parse_trust_cpu);/* -* This is a fast mixing routine used by the interrupt randomness -* collector. It's hardcoded for an 128 bit pool and assumes that any -* locks that might be needed are taken by the caller. +* The first collection of entropy occurs at system boot while interrupts +* are still turned off. Here we push in RDSEED, a timestamp, and utsname(). +* Depending on the above configuration knob, RDSEED may be considered +* sufficient for initialization. Note that much earlier setup may already +* have pushed entropy into the input pool by the time we get here. */ -static void fast_mix (u32 pool[4 ]) +int __init rand_initialize (void ) { -u32 a = pool[0 ], b = pool[1 ]; -u32 c = pool[2 ], d = pool[3 ]; - - a += b; c += d; - b =rol32 (b,6 ); d =rol32 (d,27 ); - d ^= a; b ^= c; +size_t i; +ktime_t now =ktime_get_real (); +bool arch_init = true; +unsigned long rv; - a += b; c += d; - b =rol32 (b,16 ); d =rol32 (d,14 ); - d ^= a; b ^= c; +for (i =0 ; i <BLAKE2S_BLOCK_SIZE ; i +=sizeof (rv)) { +if (!arch_get_random_seed_long_early (&rv) && + !arch_get_random_long_early (&rv)) { + rv =random_get_entropy (); + arch_init = false; + } +mix_pool_bytes (&rv,sizeof (rv)); + } +mix_pool_bytes (&now,sizeof (now)); +mix_pool_bytes (utsname (),sizeof (*(utsname ()))); - a += b; c += d; - b =rol32 (b,6 ); d =rol32 (d,27 ); - d ^= a; b ^= c; +extract_entropy (base_crng.key,sizeof (base_crng.key)); + ++base_crng.generation; - a += b; c += d; - b =rol32 (b,16 ); d =rol32 (d,14 ); - d ^= a; b ^= c; +if (arch_init && trust_cpu && crng_init <2 ) { + crng_init =2 ; +pr_notice ("crng init done (trusting CPU's manufacturer)\n" ); + } - pool[0 ] = a; pool[1 ] = b; - pool[2 ] = c; pool[3 ] = d; +if (ratelimit_disable) { + urandom_warning.interval =0 ; + unseeded_warning.interval =0 ; + } +return 0 ; } -/********************************************************************* -* -* Entropy input management -* -*********************************************************************/ -/* There is one of these per entropy source */ struct timer_rand_state {cycles_t last_time;long last_delta, last_delta2; }; -#define INIT_TIMER_RAND_STATE { INITIAL_JIFFIES, }; -/* * Add device- or boot-specific data to the input pool to help * initialize it.
@@ -1111,8 +1163,6 @@ void add_device_randomness(const void *b }EXPORT_SYMBOL (add_device_randomness); -static struct timer_rand_state input_timer_state =INIT_TIMER_RAND_STATE ; -/* * This function adds entropy to the entropy "pool" by using timing * delays. It uses the timer_rand_state structure to make an estimate
@@ -1174,8 +1224,9 @@ void add_input_randomness(unsigned int t unsigned int value) {static unsigned char last_value; +static struct timer_rand_state input_timer_state = {INITIAL_JIFFIES }; -/* ignore autorepeat and the like */ +/* Ignore autorepeat and the like. */ if (value == last_value)return ;
@@ -1185,6 +1236,119 @@ void add_input_randomness(unsigned int t }EXPORT_SYMBOL_GPL (add_input_randomness); +#ifdef CONFIG_BLOCK +void add_disk_randomness (struct gendisk *disk) +{ +if (!disk || !disk->random) +return ; +/* First major is 1, so we get >= 0x200 here. */ +add_timer_randomness (disk->random,0x100 +disk_devt (disk)); +} +EXPORT_SYMBOL_GPL (add_disk_randomness); + +void rand_initialize_disk (struct gendisk *disk) +{ +struct timer_rand_state *state; + +/* +* If kzalloc returns null, we just won't use that entropy +* source. +*/ + state =kzalloc (sizeof (struct timer_rand_state ),GFP_KERNEL ); +if (state) { + state->last_time =INITIAL_JIFFIES ; + disk->random = state; + } +} +#endif + +/* +* Interface for in-kernel drivers of true hardware RNGs. +* Those devices may produce endless random bits and will be throttled +* when our pool is full. +*/ +void add_hwgenerator_randomness (const void *buffer,size_t count, +size_t entropy) +{ +if (unlikely (crng_init ==0 )) { +size_t ret =crng_fast_load (buffer, count); +mix_pool_bytes (buffer, ret); + count -= ret; + buffer += ret; +if (!count || crng_init ==0 ) +return ; + } + +/* +* Throttle writing if we're above the trickle threshold. +* We'll be woken up again once below POOL_MIN_BITS, when +* the calling thread is about to terminate, or once +* CRNG_RESEED_INTERVAL has elapsed. +*/ +wait_event_interruptible_timeout (random_write_wait, + !system_wq ||kthread_should_stop () || + input_pool.entropy_count <POOL_MIN_BITS , +CRNG_RESEED_INTERVAL ); +mix_pool_bytes (buffer, count); +credit_entropy_bits (entropy); +} +EXPORT_SYMBOL_GPL (add_hwgenerator_randomness); + +/* +* Handle random seed passed by bootloader. +* If the seed is trustworthy, it would be regarded as hardware RNGs. Otherwise +* it would be regarded as device data. +* The decision is controlled by CONFIG_RANDOM_TRUST_BOOTLOADER. +*/ +void add_bootloader_randomness (const void *buf,size_t size) +{ +if (IS_ENABLED (CONFIG_RANDOM_TRUST_BOOTLOADER )) +add_hwgenerator_randomness (buf, size, size *8 ); +else +add_device_randomness (buf, size); +} +EXPORT_SYMBOL_GPL (add_bootloader_randomness); + +struct fast_pool { +union { +u32 pool32[4 ]; +u64 pool64[2 ]; + }; +unsigned long last; +u16 reg_idx; +u8 count; +}; + +/* +* This is a fast mixing routine used by the interrupt randomness +* collector. It's hardcoded for an 128 bit pool and assumes that any +* locks that might be needed are taken by the caller. +*/ +static void fast_mix (u32 pool[4 ]) +{ +u32 a = pool[0 ], b = pool[1 ]; +u32 c = pool[2 ], d = pool[3 ]; + + a += b; c += d; + b =rol32 (b,6 ); d =rol32 (d,27 ); + d ^= a; b ^= c; + + a += b; c += d; + b =rol32 (b,16 ); d =rol32 (d,14 ); + d ^= a; b ^= c; + + a += b; c += d; + b =rol32 (b,6 ); d =rol32 (d,27 ); + d ^= a; b ^= c; + + a += b; c += d; + b =rol32 (b,16 ); d =rol32 (d,14 ); + d ^= a; b ^= c; + + pool[0 ] = a; pool[1 ] = b; + pool[2 ] = c; pool[3 ] = d; +} + static DEFINE_PER_CPU(struct fast_pool, irq_randomness);static u32 get_reg (struct fast_pool *f,struct pt_regs *regs)
@@ -1254,22 +1418,11 @@ void add_interrupt_randomness(int irq) fast_pool->count =0 ; -/* award one bit for the contents of the fast pool */ +/* Award one bit for the contents of the fast pool. */ credit_entropy_bits (1 ); }EXPORT_SYMBOL_GPL (add_interrupt_randomness); -#ifdef CONFIG_BLOCK -void add_disk_randomness (struct gendisk *disk) -{ -if (!disk || !disk->random) -return ; -/* first major is 1, so we get >= 0x200 here */ -add_timer_randomness (disk->random,0x100 +disk_devt (disk)); -} -EXPORT_SYMBOL_GPL (add_disk_randomness); -#endif -/* * Each time the timer fires, we expect that we got an unpredictable * jump in the cycle counter. Even if the timer is running on another
@@ -1319,73 +1472,6 @@ static void try_to_generate_entropy(voidmix_pool_bytes (&stack.now,sizeof (stack.now)); } -static bool trust_cpu__ro_after_init =IS_ENABLED (CONFIG_RANDOM_TRUST_CPU ); -static int __init parse_trust_cpu (char *arg) -{ -return kstrtobool (arg, &trust_cpu); -} -early_param ("random.trust_cpu" , parse_trust_cpu); - -/* -* Note that setup_arch() may call add_device_randomness() -* long before we get here. This allows seeding of the pools -* with some platform dependent data very early in the boot -* process. But it limits our options here. We must use -* statically allocated structures that already have all -* initializations complete at compile time. We should also -* take care not to overwrite the precious per platform data -* we were given. -*/ -int __init rand_initialize (void ) -{ -size_t i; -ktime_t now =ktime_get_real (); -bool arch_init = true; -unsigned long rv; - -for (i =0 ; i <BLAKE2S_BLOCK_SIZE ; i +=sizeof (rv)) { -if (!arch_get_random_seed_long_early (&rv) && - !arch_get_random_long_early (&rv)) { - rv =random_get_entropy (); - arch_init = false; - } -mix_pool_bytes (&rv,sizeof (rv)); - } -mix_pool_bytes (&now,sizeof (now)); -mix_pool_bytes (utsname (),sizeof (*(utsname ()))); - -extract_entropy (base_crng.key,sizeof (base_crng.key)); - ++base_crng.generation; - -if (arch_init && trust_cpu && crng_init <2 ) { - crng_init =2 ; -pr_notice ("crng init done (trusting CPU's manufacturer)\n" ); - } - -if (ratelimit_disable) { - urandom_warning.interval =0 ; - unseeded_warning.interval =0 ; - } -return 0 ; -} - -#ifdef CONFIG_BLOCK -void rand_initialize_disk (struct gendisk *disk) -{ -struct timer_rand_state *state; - -/* -* If kzalloc returns null, we just won't use that entropy -* source. -*/ - state =kzalloc (sizeof (struct timer_rand_state ),GFP_KERNEL ); -if (state) { - state->last_time =INITIAL_JIFFIES ; - disk->random = state; - } -} -#endif -static ssize_t urandom_read (struct file *file,char __user *buf,size_t nbytes,loff_t *ppos) {
@@ -1668,47 +1754,3 @@ struct ctl_table random_table[] = { { } };#endif /* CONFIG_SYSCTL */ - -/* Interface for in-kernel drivers of true hardware RNGs. -* Those devices may produce endless random bits and will be throttled -* when our pool is full. -*/ -void add_hwgenerator_randomness (const void *buffer,size_t count, -size_t entropy) -{ -if (unlikely (crng_init ==0 )) { -size_t ret =crng_fast_load (buffer, count); -mix_pool_bytes (buffer, ret); - count -= ret; - buffer += ret; -if (!count || crng_init ==0 ) -return ; - } - -/* Throttle writing if we're above the trickle threshold. -* We'll be woken up again once below POOL_MIN_BITS, when -* the calling thread is about to terminate, or once -* CRNG_RESEED_INTERVAL has elapsed. -*/ -wait_event_interruptible_timeout (random_write_wait, - !system_wq ||kthread_should_stop () || - input_pool.entropy_count <POOL_MIN_BITS , -CRNG_RESEED_INTERVAL ); -mix_pool_bytes (buffer, count); -credit_entropy_bits (entropy); -} -EXPORT_SYMBOL_GPL (add_hwgenerator_randomness); - -/* Handle random seed passed by bootloader. -* If the seed is trustworthy, it would be regarded as hardware RNGs. Otherwise -* it would be regarded as device data. -* The decision is controlled by CONFIG_RANDOM_TRUST_BOOTLOADER. -*/ -void add_bootloader_randomness (const void *buf,size_t size) -{ -if (IS_ENABLED (CONFIG_RANDOM_TRUST_BOOTLOADER )) -add_hwgenerator_randomness (buf, size, size *8 ); -else -add_device_randomness (buf, size); -} -EXPORT_SYMBOL_GPL (add_bootloader_randomness);
Greg Kroah-Hartman
maintainerPATCH 103/234 random: group sysctl functions
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 0deff3c43206c24e746b1410f11125707ad3040e upstream.
This pulls all of the sysctl-focused functions into the sixth labeled section.
No functional changes.
--- drivers/char/random.c | 35 ++++++++++++++++++++++++++++++----- 1 file changed, 30 insertions(+), 5 deletions(-)
drivers/char/random.c +30 -5
@@ -1686,9 +1686,34 @@ const struct file_operations urandom_fop .llseek = noop_llseek, }; +/******************************************************************** * -* Sysctl interface +* Sysctl interface. +* +* These are partly unused legacy knobs with dummy values to not break +* userspace and partly still useful things. They are usually accessible +* in /proc/sys/kernel/random/ and are as follows: +* +* - boot_id - a UUID representing the current boot. +* +* - uuid - a random UUID, different each time the file is read. +* +* - poolsize - the number of bits of entropy that the input pool can +* hold, tied to the POOL_BITS constant. +* +* - entropy_avail - the number of bits of entropy currently in the +* input pool. Always <= poolsize. +* +* - write_wakeup_threshold - the amount of entropy in the input pool +* below which write polls to /dev/random will unblock, requesting +* more entropy, tied to the POOL_MIN_BITS constant. It is writable +* to avoid breaking old userspaces, but writing to it does not +* change any behavior of the RNG. +* +* - urandom_min_reseed_secs - fixed to the meaningless value "60". +* It is writable to avoid breaking old userspaces, but writing +* to it does not change any behavior of the RNG. * ********************************************************************/
@@ -1696,8 +1721,8 @@ const struct file_operations urandom_fop#include <linux/sysctl.h> -static int random_min_urandom_seed =60 ; -static int random_write_wakeup_bits =POOL_MIN_BITS ; +static int sysctl_random_min_urandom_seed =60 ; +static int sysctl_random_write_wakeup_bits =POOL_MIN_BITS ;static int sysctl_poolsize =POOL_BITS ;static char sysctl_bootid[16 ];
@@ -1755,14 +1780,14 @@ struct ctl_table random_table[] = { }, { .procname = "write_wakeup_threshold", - .data = &random_write_wakeup_bits, + .data = &sysctl_random_write_wakeup_bits, .maxlen = sizeof(int), .mode = 0644, .proc_handler = proc_dointvec, }, { .procname = "urandom_min_reseed_secs", - .data = &random_min_urandom_seed, + .data = &sysctl_random_min_urandom_seed, .maxlen = sizeof(int), .mode = 0644, .proc_handler = proc_dointvec,
Greg Kroah-Hartman
maintainerPATCH 102/234 random: group userspace read/write functions
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit a6adf8e7a605250b911e94793fd077933709ff9e upstream.
This pulls all of the userspace read/write-focused functions into the fifth labeled section.
No functional changes.
--- drivers/char/random.c | 125 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 77 insertions(+), 48 deletions(-)
drivers/char/random.c +77 -48
@@ -1472,30 +1472,61 @@ static void try_to_generate_entropy(voidmix_pool_bytes (&stack.now,sizeof (stack.now)); } -static ssize_t urandom_read (struct file *file,char __user *buf,size_t nbytes, -loff_t *ppos) + +/********************************************************************** +* +* Userspace reader/writer interfaces. +* +* getrandom(2) is the primary modern interface into the RNG and should +* be used in preference to anything else. +* +* Reading from /dev/random has the same functionality as calling +* getrandom(2) with flags=0. In earlier versions, however, it had +* vastly different semantics and should therefore be avoided, to +* prevent backwards compatibility issues. +* +* Reading from /dev/urandom has the same functionality as calling +* getrandom(2) with flags=GRND_INSECURE. Because it does not block +* waiting for the RNG to be ready, it should not be used. +* +* Writing to either /dev/random or /dev/urandom adds entropy to +* the input pool but does not credit it. +* +* Polling on /dev/random indicates when the RNG is initialized, on +* the read side, and when it wants new entropy, on the write side. +* +* Both /dev/random and /dev/urandom have the same set of ioctls for +* adding entropy, getting the entropy count, zeroing the count, and +* reseeding the crng. +* +**********************************************************************/ + +SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int, + flags) { -static int maxwarn =10 ; +if (flags & ~(GRND_NONBLOCK |GRND_RANDOM |GRND_INSECURE )) +return -EINVAL ; -if (!crng_ready () && maxwarn >0 ) { - maxwarn--; -if (__ratelimit (&urandom_warning)) -pr_notice ("%s: uninitialized urandom read (%zd bytes read)\n" , - current->comm, nbytes); - } +/* +* Requesting insecure and blocking randomness at the same time makes +* no sense. +*/ +if ((flags & (GRND_INSECURE |GRND_RANDOM )) == (GRND_INSECURE |GRND_RANDOM )) +return -EINVAL ; -return get_random_bytes_user (buf, nbytes); -} +if (count >INT_MAX ) + count =INT_MAX ; -static ssize_t random_read (struct file *file,char __user *buf,size_t nbytes, -loff_t *ppos) -{ -int ret; +if (!(flags &GRND_INSECURE ) && !crng_ready ()) { +int ret; - ret =wait_for_random_bytes (); -if (ret !=0 ) -return ret; -return get_random_bytes_user (buf, nbytes); +if (flags &GRND_NONBLOCK ) +return -EAGAIN ; + ret =wait_for_random_bytes (); +if (unlikely (ret)) +return ret; + } +return get_random_bytes_user (buf, count); }static __poll_t random_poll (struct file *file,poll_table *wait)
@@ -1547,6 +1578,32 @@ static ssize_t random_write(struct filereturn (ssize_t )count; } +static ssize_t urandom_read (struct file *file,char __user *buf,size_t nbytes, +loff_t *ppos) +{ +static int maxwarn =10 ; + +if (!crng_ready () && maxwarn >0 ) { + maxwarn--; +if (__ratelimit (&urandom_warning)) +pr_notice ("%s: uninitialized urandom read (%zd bytes read)\n" , + current->comm, nbytes); + } + +return get_random_bytes_user (buf, nbytes); +} + +static ssize_t random_read (struct file *file,char __user *buf,size_t nbytes, +loff_t *ppos) +{ +int ret; + + ret =wait_for_random_bytes (); +if (ret !=0 ) +return ret; +return get_random_bytes_user (buf, nbytes); +} +static long random_ioctl (struct file *f,unsigned int cmd,unsigned long arg) {int size, ent_count;
@@ -1555,7 +1612,7 @@ static long random_ioctl(struct file *f,switch (cmd) {case RNDGETENTCNT : -/* inherently racy, no point locking */ +/* Inherently racy, no point locking. */ if (put_user (input_pool.entropy_count, p))return -EFAULT ;return 0 ;
@@ -1629,34 +1686,6 @@ const struct file_operations urandom_fop .llseek = noop_llseek, }; -SYSCALL_DEFINE3(getrandom, char __user *, buf, size_t, count, unsigned int, - flags) -{ -if (flags & ~(GRND_NONBLOCK |GRND_RANDOM |GRND_INSECURE )) -return -EINVAL ; - -/* -* Requesting insecure and blocking randomness at the same time makes -* no sense. -*/ -if ((flags & (GRND_INSECURE |GRND_RANDOM )) == (GRND_INSECURE |GRND_RANDOM )) -return -EINVAL ; - -if (count >INT_MAX ) - count =INT_MAX ; - -if (!(flags &GRND_INSECURE ) && !crng_ready ()) { -int ret; - -if (flags &GRND_NONBLOCK ) -return -EAGAIN ; - ret =wait_for_random_bytes (); -if (unlikely (ret)) -return ret; - } -return get_random_bytes_user (buf, count); -} -/******************************************************************** * * Sysctl interface
Greg Kroah-Hartman
maintainerPATCH 104/234 random: rewrite header introductory comment
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 5f75d9f3babea8ae0a2d06724656874f41d317f5 upstream.
Now that we've re-documented the various sections, we can remove the outdated text here and replace it with a high-level overview.
--- drivers/char/random.c | 179 +++++--------------------------------------------- 1 file changed, 19 insertions(+), 160 deletions(-)
drivers/char/random.c +19 -160
@@ -2,168 +2,27 @@/* * Copyright (C) 2017-2022 Jason A. Donenfeld <[email protected]>. All Rights Reserved. * Copyright Matt Mackall <[email protected]>, 2003, 2004, 2005 -* Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All -* rights reserved. -*/ - -/* -* Exported interfaces ---- output -* =============================== -* -* There are four exported interfaces; two for use within the kernel, -* and two for use from userspace. -* -* Exported interfaces ---- userspace output -* ----------------------------------------- -* -* The userspace interfaces are two character devices /dev/random and -* /dev/urandom. /dev/random is suitable for use when very high -* quality randomness is desired (for example, for key generation or -* one-time pads), as it will only return a maximum of the number of -* bits of randomness (as estimated by the random number generator) -* contained in the entropy pool. -* -* The /dev/urandom device does not have this limit, and will return -* as many bytes as are requested. As more and more random bytes are -* requested without giving time for the entropy pool to recharge, -* this will result in random numbers that are merely cryptographically -* strong. For many applications, however, this is acceptable. -* -* Exported interfaces ---- kernel output -* -------------------------------------- -* -* The primary kernel interfaces are: -* -* void get_random_bytes(void *buf, size_t nbytes); -* u32 get_random_u32() -* u64 get_random_u64() -* unsigned int get_random_int() -* unsigned long get_random_long() -* -* These interfaces will return the requested number of random bytes -* into the given buffer or as a return value. This is equivalent to a -* read from /dev/urandom. The get_random_{u32,u64,int,long}() family -* of functions may be higher performance for one-off random integers, -* because they do a bit of buffering. -* -* prandom_u32() -* ------------- -* -* For even weaker applications, see the pseudorandom generator -* prandom_u32(), prandom_max(), and prandom_bytes(). If the random -* numbers aren't security-critical at all, these are *far* cheaper. -* Useful for self-tests, random error simulation, randomized backoffs, -* and any other application where you trust that nobody is trying to -* maliciously mess with you by guessing the "random" numbers. -* -* Exported interfaces ---- input -* ============================== -* -* The current exported interfaces for gathering environmental noise -* from the devices are: -* -* void add_device_randomness(const void *buf, size_t size); -* void add_input_randomness(unsigned int type, unsigned int code, -* unsigned int value); -* void add_interrupt_randomness(int irq); -* void add_disk_randomness(struct gendisk *disk); -* void add_hwgenerator_randomness(const void *buffer, size_t count, -* size_t entropy); -* void add_bootloader_randomness(const void *buf, size_t size); -* -* add_device_randomness() is for adding data to the random pool that -* is likely to differ between two devices (or possibly even per boot). -* This would be things like MAC addresses or serial numbers, or the -* read-out of the RTC. This does *not* add any actual entropy to the -* pool, but it initializes the pool to different values for devices -* that might otherwise be identical and have very little entropy -* available to them (particularly common in the embedded world). -* -* add_input_randomness() uses the input layer interrupt timing, as well as -* the event type information from the hardware. -* -* add_interrupt_randomness() uses the interrupt timing as random -* inputs to the entropy pool. Using the cycle counters and the irq source -* as inputs, it feeds the randomness roughly once a second. -* -* add_disk_randomness() uses what amounts to the seek time of block -* layer request events, on a per-disk_devt basis, as input to the -* entropy pool. Note that high-speed solid state drives with very low -* seek times do not make for good sources of entropy, as their seek -* times are usually fairly consistent. -* -* All of these routines try to estimate how many bits of randomness a -* particular randomness source. They do this by keeping track of the -* first and second order deltas of the event timings. -* -* add_hwgenerator_randomness() is for true hardware RNGs, and will credit -* entropy as specified by the caller. If the entropy pool is full it will -* block until more entropy is needed. -* -* add_bootloader_randomness() is the same as add_hwgenerator_randomness() or -* add_device_randomness(), depending on whether or not the configuration -* option CONFIG_RANDOM_TRUST_BOOTLOADER is set. -* -* Ensuring unpredictability at system startup -* ============================================ -* -* When any operating system starts up, it will go through a sequence -* of actions that are fairly predictable by an adversary, especially -* if the start-up does not involve interaction with a human operator. -* This reduces the actual number of bits of unpredictability in the -* entropy pool below the value in entropy_count. In order to -* counteract this effect, it helps to carry information in the -* entropy pool across shut-downs and start-ups. To do this, put the -* following lines an appropriate script which is run during the boot -* sequence: -* -* echo "Initializing random number generator..." -* random_seed=/var/run/random-seed -* # Carry a random seed from start-up to start-up -* # Load and then save the whole entropy pool -* if [ -f $random_seed ]; then -* cat $random_seed >/dev/urandom -* else -* touch $random_seed -* fi -* chmod 600 $random_seed -* dd if=/dev/urandom of=$random_seed count=1 bs=512 -* -* and the following lines in an appropriate script which is run as -* the system is shutdown: -* -* # Carry a random seed from shut-down to start-up -* # Save the whole entropy pool -* echo "Saving random seed..." -* random_seed=/var/run/random-seed -* touch $random_seed -* chmod 600 $random_seed -* dd if=/dev/urandom of=$random_seed count=1 bs=512 -* -* For example, on most modern systems using the System V init -* scripts, such code fragments would be found in -* /etc/rc.d/init.d/random. On older Linux systems, the correct script -* location might be in /etc/rcb.d/rc.local or /etc/rc.d/rc.0. -* -* Effectively, these commands cause the contents of the entropy pool -* to be saved at shut-down time and reloaded into the entropy pool at -* start-up. (The 'dd' in the addition to the bootup script is to -* make sure that /etc/random-seed is different for every start-up, -* even if the system crashes without executing rc.0.) Even with -* complete knowledge of the start-up activities, predicting the state -* of the entropy pool requires knowledge of the previous history of -* the system. -* -* Configuring the /dev/random driver under Linux -* ============================================== +* Copyright Theodore Ts'o, 1994, 1995, 1996, 1997, 1998, 1999. All rights reserved. * -* The /dev/random driver under Linux uses minor numbers 8 and 9 of -* the /dev/mem major number (#1). So if your system does not have -* /dev/random and /dev/urandom created already, they can be created -* by using the commands: +* This driver produces cryptographically secure pseudorandom data. It is divided +* into roughly six sections, each with a section header: * -* mknod /dev/random c 1 8 -* mknod /dev/urandom c 1 9 +* - Initialization and readiness waiting. +* - Fast key erasure RNG, the "crng". +* - Entropy accumulation and extraction routines. +* - Entropy collection routines. +* - Userspace reader/writer interfaces. +* - Sysctl interface. +* +* The high level overview is that there is one input pool, into which +* various pieces of data are hashed. Some of that data is then "credited" as +* having a certain number of bits of entropy. When enough bits of entropy are +* available, the hash is finalized and handed as a key to a stream cipher that +* expands it indefinitely for various consumers. This key is periodically +* refreshed as the various entropy collectors, described below, add data to the +* input pool and credit it. There is currently no Fortuna-like scheduler +* involved, which can lead to malicious entropy sources causing a premature +* reseed, and the entropy estimates are, at best, conservative guesses. */ #define pr_fmt (fmt) KBUILD_MODNAME ": " fmt
Greg Kroah-Hartman
maintainerPATCH 105/234 random: defer fast pool mixing to worker
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit 58340f8e952b613e0ead0bed58b97b05bf4743c5 upstream.
On PREEMPT_RT, it's problematic to take spinlocks from hard irq handlers. We can fix this by deferring to a workqueue the dumping of the fast pool into the input pool.
We accomplish this with some careful rules on fast_pool->count:
- When it's incremented to >= 64, we schedule the work.
- If the top bit is set, we never schedule the work, even if >= 64.
- The worker is responsible for setting it back to 0 when it's done.
There are two small issues around using workqueues for this purpose that we work around.
The first issue is that mix_interrupt_randomness() might be migrated to another CPU during CPU hotplug. This issue is rectified by checking that it hasn't been migrated (after disabling irqs). If it has been migrated, then we set the count to zero, so that when the CPU comes online again, it can requeue the work. As part of this, we switch to using an atomic_t, so that the increment in the irq handler doesn't wipe out the zeroing if the CPU comes back online while this worker is running.
The second issue is that, though relatively minor in effect, we probably want to make sure we get a consistent view of the pool onto the stack, in case it's interrupted by an irq while reading. To do this, we don't reenable irqs until after the copy. There are only 18 instructions between the cli and sti, so this is a pretty tiny window.
--- drivers/char/random.c | 63 ++++++++++++++++++++++++++++++++++++++------------ 1 file changed, 49 insertions(+), 14 deletions(-)
drivers/char/random.c +49 -14
@@ -1173,9 +1173,10 @@ struct fast_pool {u32 pool32[4 ];u64 pool64[2 ]; }; +struct work_struct mix;unsigned long last; +atomic_t count;u16 reg_idx; -u8 count; };/*
@@ -1225,12 +1226,49 @@ static u32 get_reg(struct fast_pool *f,return *ptr; } +static void mix_interrupt_randomness (struct work_struct *work) +{ + struct fast_pool *fast_pool = container_of(work, struct fast_pool, mix); +u32 pool[4 ]; + +/* Check to see if we're running on the wrong CPU due to hotplug. */ +local_irq_disable (); +if (fast_pool !=this_cpu_ptr (&irq_randomness)) { +local_irq_enable (); +/* +* If we are unlucky enough to have been moved to another CPU, +* during CPU hotplug while the CPU was shutdown then we set +* our count to zero atomically so that when the CPU comes +* back online, it can enqueue work again. The _release here +* pairs with the atomic_inc_return_acquire in +* add_interrupt_randomness(). +*/ +atomic_set_release (&fast_pool->count,0 ); +return ; + } + +/* +* Copy the pool to the stack so that the mixer always has a +* consistent view, before we reenable irqs again. +*/ +memcpy (pool, fast_pool->pool32,sizeof (pool)); +atomic_set (&fast_pool->count,0 ); + fast_pool->last = jiffies; +local_irq_enable (); + +mix_pool_bytes (pool,sizeof (pool)); +credit_entropy_bits (1 ); +memzero_explicit (pool,sizeof (pool)); +} +void add_interrupt_randomness (int irq) { +enum {MIX_INFLIGHT =1U <<31 };struct fast_pool *fast_pool =this_cpu_ptr (&irq_randomness);struct pt_regs *regs =get_irq_regs ();unsigned long now = jiffies;cycles_t cycles =random_get_entropy (); +unsigned int new_count;if (cycles ==0 ) cycles =get_reg (fast_pool, regs);
@@ -1250,12 +1288,13 @@ void add_interrupt_randomness(int irq) }fast_mix (fast_pool->pool32); - ++fast_pool->count; +/* The _acquire here pairs with the atomic_set_release in mix_interrupt_randomness(). */ + new_count = (unsigned int )atomic_inc_return_acquire (&fast_pool->count);if (unlikely (crng_init ==0 )) { -if (fast_pool->count >=64 && +if (new_count >=64 &&crng_fast_load (fast_pool->pool32,sizeof (fast_pool->pool32)) >0 ) { - fast_pool->count =0 ; +atomic_set (&fast_pool->count,0 ); fast_pool->last = now;if (spin_trylock (&input_pool.lock)) {_mix_pool_bytes (&fast_pool->pool32,sizeof (fast_pool->pool32));
@@ -1265,20 +1304,16 @@ void add_interrupt_randomness(int irq)return ; } -if ((fast_pool->count <64 ) && !time_after (now, fast_pool->last +HZ )) +if (new_count &MIX_INFLIGHT )return ; -if (!spin_trylock (&input_pool.lock)) +if (new_count <64 && !time_after (now, fast_pool->last +HZ ))return ; - fast_pool->last = now; -_mix_pool_bytes (&fast_pool->pool32,sizeof (fast_pool->pool32)); -spin_unlock (&input_pool.lock); - - fast_pool->count =0 ; - -/* Award one bit for the contents of the fast pool. */ -credit_entropy_bits (1 ); +if (unlikely (!fast_pool->mix.func)) +INIT_WORK (&fast_pool->mix, mix_interrupt_randomness); +atomic_or (MIX_INFLIGHT , &fast_pool->count); +queue_work_on (raw_smp_processor_id (), system_highpri_wq, &fast_pool->mix); }EXPORT_SYMBOL_GPL (add_interrupt_randomness);
Greg Kroah-Hartman
maintainerPATCH 106/234 random: do not take pool spinlock at boot
In reply to Greg Kroah-HartmanFrom: "Jason A. Donenfeld" <[email protected]>
commit afba0b80b977b2a8f16234f2acd982f82710ba33 upstream.
Since rand_initialize() is run while interrupts are still off and nothing else is running, we don't need to repeatedly take and release the pool spinlock, especially in the RDSEED loop.
--- drivers/char/random.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/char/random.c +3 -3
@@ -973,10 +973,10 @@ int __init rand_initialize(void) rv =random_get_entropy (); arch_init = false; } -mix_pool_bytes (&rv,sizeof (rv)); +_mix_pool_bytes (&rv,sizeof (rv)); } -mix_pool_bytes (&now,sizeof (now)); -mix_pool_bytes (utsname (),sizeof (*(utsname ()))); +_mix_pool_bytes (&now,sizeof (now)); +_mix_pool_bytes (utsname (),sizeof (*(utsname ())));extract_entropy (base_crng.key,sizeof (base_crng.key)); ++base_crng.generation;