Messages in this thread Patch in this message | | | Date | Thu, 23 Dec 2021 20:04:19 +0100 | From | Dominik Brodowski <> | Subject | [PATCH v6] random: fix crash on multiple early calls to add_bootloader_randomness() |
| |
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.
Reported-by: Ivan T. Ivanov <iivanov@suse.de> Fixes: 18b915ac6b0a ("efi/random: Treat EFI_RNG_PROTOCOL output as bootloader randomness") Cc: stable@vger.kernel.org Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>
---
drivers/char/random.c | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
---
This is now a truly minimalist approach which tests for system_wq != NULL, as suggested by Jason.
Another issue remains, though, but should be addressed separately: If one trusts the randnomness provided by the bootloader, and if the primary_crng is then seeded with 512 bits of entropy, warnings will still be emited that unseeded randomness is used with crng_init=1.
diff --git a/drivers/char/random.c b/drivers/char/random.c index 13c968b950c5..3c44f5ff6cc4 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -993,7 +993,10 @@ static void crng_reseed(struct crng_state *crng, struct entropy_store *r) memzero_explicit(&buf, sizeof(buf)); WRITE_ONCE(crng->init_time, jiffies); spin_unlock_irqrestore(&crng->lock, flags); - if (crng == &primary_crng && crng_init < 2) { + /* Only finalize initialization if workqueues are ready; otherwise + * numa_crng_init() and other things may go wrong. + */ + if (crng == &primary_crng && crng_init < 2 && system_wq) { invalidate_batched_entropy(); numa_crng_init(); crng_init = 2; @@ -2299,7 +2302,8 @@ void add_hwgenerator_randomness(const char *buffer, size_t count, * 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);
| |