LKML Archive on lore.kernel.org
help / color / mirror / Atom feed
From: Dominik Brodowski <linux@dominikbrodowski.net>
To: "Jason A. Donenfeld" <Jason@zx2c4.com>
Cc: Theodore Ts'o <tytso@mit.edu>, "Ivan T. Ivanov" <iivanov@suse.de>,
Ard Biesheuvel <ardb@kernel.org>,
linux-efi@vger.kernel.org, LKML <linux-kernel@vger.kernel.org>,
hsinyi@chromium.org
Subject: [PATCH v4] random: fix crash on multiple early calls to add_bootloader_randomness()
Date: Fri, 3 Dec 2021 08:58:26 +0100 [thread overview]
Message-ID: <YanOIvAV1iPBEXR3@light.dominikbrodowski.net> (raw)
In-Reply-To: <CAHmME9qxBeBzfKCjzfAFX9ZWAGKv1TKCQw3x22d_DmJtaAewLw@mail.gmail.com>
Hi Jason,
Am Thu, Dec 02, 2021 at 11:55:10AM -0500 schrieb Jason A. Donenfeld:
> Thanks for the patch. One trivial nit and one question:
Thanks for your review!
> On Thu, Dec 2, 2021 at 6:35 AM Dominik Brodowski
> <linux@dominikbrodowski.net> wrote:
> > + /* We cannot do much with the input pool until it is set up in
> > + * rand_initalize(); therefore just mix into the crng state.
>
> I think you meant "rand_initialize()" here (missing 'i').
Indeed, sorry about that.
> > If the added entropy suffices to increase crng_init to 1, future calls
> > to add_bootloader_randomness() or add_hwgenerator_randomness() used to
> > progress to credit_entropy_bits(). However, if the input pool is not yet
> > properly set up, the cmpxchg call within that function can lead to an
> > infinite recursion.
>
> I see what this patch does with crng_global_init_time, and that seems
> probably sensible, but I didn't understand this part of the reasoning
> in the commit message; I might just be a bit slow here. Where's the
> recursion exactly? Or even an infinite loop?
On arm64, it was actually a NULL pointer dereference reported by Ivan T.
Ivanov; see
https://lore.kernel.org/lkml/20211012082708.121931-1-iivanov@suse.de/
Trying to reproduce this rather bluntly on x86/qemu by multiple manual calls
to add_bootloader_randomness(), I mis-interpreted the symptoms to point to an
infinite recursion. The real problem seems to be that crng_reseed() isn't
ready to be called too early in the boot process, in particular before
workqueues are ready (see the call to numa_crng_init()).
However, there seem be additional issues with add_bootloader_randomness()
not yet addressed (or worsened) by my patch:
- If CONFIG_RANDOM_TRUST_BOOTLOADER is enabled and crng_init==0,
add_hwgenerator_randomness() calls crng_fast_load() and returns
immediately. If it is disabled and crng_init==0,
add_device_randnomness() calls crng_slow_load() but still
continues to call _mix_pool_bytes(). That means the seed is
used more extensively if CONFIG_RANDOM_TRUST_BOOTLOADER is not
set!
- If CONFIG_RANDOM_TRUST_BOOTLOADER is enabled and crng_init==0,
the entropy is not credited -- same as if
CONFIG_RANDOM_TRUST_BOOTLOADER is not set. Only subsequent calls
to add_bootloader_randomness() would credit entropy, but that
causes the issue NULL pointer dereference or the hang...
- As crng_fast_load() returns early, that actually means that my
patch causes the additional entropy submitted to
add_hwgenerator_randomness() by subsequent calls to be completely
lost.
- For add_bootloader_randomness(), it makes no sense at all to call
wait_event_interruptible().
Therefore, it might make more sense to
- modify add_bootloader_randomness() to always call
add_device_randomness(), and if CONFIG_RANDOM_TRUST_BOOTLOADER is
enabled, to call credit_entropy_bits().
- update credit_entropy_bits() to not call credit_entropy_bits()
if crng_global_init_time==0, as workqueues (and possibly other
infrastructure) might not be available at that time.
What do you think? Draft patch below. @Ivan: Could you re-test on your
system, please?
Thanks,
Dominik
---
Currently, if CONFIG_RANDOM_TRUST_BOOTLOADER is enabled, mutliple 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.
However, no entropy is currently credited for that, even though the
name and description of CONFIG_RANDOM_TRUST_BOOTLOADER states otherwise.
On subsequent calls to add_bootloader_randomness() and then to
add_hwgenerator_randomness(), crng_fast_load() will be skipped. Instead,
wait_event_interruptible() (which makes no sense for the init process)
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, crng_reseed() may depend on workqueues being available, which
is not the case early during boot.
To fix these issues, unconditionally call add_device_randomness() but not
add_hwgenerator_randomness() in add_bootloader_randomness(). This has the
additional advantage that the seed provided by the first call to
add_bootloader_randomness() is not only used by crng_{fast,slow}_load(),
but also mixed into the input pool. If CONFIG_RANDOM_TRUST_BOOTLOADER is
set, explicitly credit the entropy. However, avoid a call to crng_reseed()
too early during boot. It is safe to be called after rand_initialize(),
so use crng_global_init_time (which is set to != 0 in that function) to
determine which branch to take.
Reported-by: Ivan T. Ivanov <iivanov@suse.de>
Fixes: 18b915ac6b0a ("efi/random: Treat EFI_RNG_PROTOCOL output as bootloader randomness")
Signed-off-by: Dominik Brodowski <linux@dominikbrodowski.net>
---
v3->v4: complete rewrite
v2->v3: only one unlikely (Ard Biesheuvel)
v1->v2: fix commit message; unmerge Reported-and-tested-by-tag (Ard Biesheuvel)
diff --git a/drivers/char/random.c b/drivers/char/random.c
index 605969ed0f96..d8614b426dfb 100644
--- a/drivers/char/random.c
+++ b/drivers/char/random.c
@@ -722,7 +722,8 @@ static void credit_entropy_bits(struct entropy_store *r, int nbits)
if (r == &input_pool) {
int entropy_bits = entropy_count >> ENTROPY_SHIFT;
- if (crng_init < 2 && entropy_bits >= 128)
+ if (crng_init < 2 && entropy_bits >= 128 &&
+ crng_global_init_time > 0)
crng_reseed(&primary_crng, r);
}
}
@@ -1763,8 +1764,8 @@ static void __init init_std_data(struct entropy_store *r)
}
/*
- * Note that setup_arch() may call add_device_randomness()
- * long before we get here. This allows seeding of the pools
+ * add_device_randomness() or add_bootloader_randomness() may be
+ * called 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
@@ -2291,15 +2292,13 @@ void add_hwgenerator_randomness(const char *buffer, size_t count,
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.
+ * If the seed is trustworthy, its entropy will be credited.
* The decision is controlled by CONFIG_RANDOM_TRUST_BOOTLOADER.
*/
void add_bootloader_randomness(const void *buf, unsigned int size)
{
+ add_device_randomness(buf, size);
if (IS_ENABLED(CONFIG_RANDOM_TRUST_BOOTLOADER))
- add_hwgenerator_randomness(buf, size, size * 8);
- else
- add_device_randomness(buf, size);
+ credit_entropy_bits(&input_pool, size * 8);
}
EXPORT_SYMBOL_GPL(add_bootloader_randomness);
next prev parent reply other threads:[~2021-12-03 7:59 UTC|newest]
Thread overview: 31+ messages / expand[flat|nested] mbox.gz Atom feed top
2021-10-12 8:27 [PATCH] Revert "efi/random: Treat EFI_RNG_PROTOCOL output as bootloader randomness" Ivan T. Ivanov
2021-10-12 8:40 ` Dominik Brodowski
2021-10-13 7:30 ` [RESEND] " Ivan T. Ivanov
2021-10-13 7:50 ` Ard Biesheuvel
2021-10-13 8:05 ` Ivan T. Ivanov
2021-10-13 9:51 ` [RESEND] " Ivan T. Ivanov
2021-10-13 9:53 ` Ivan T. Ivanov
2021-10-13 13:23 ` Ivan T. Ivanov
2021-10-31 6:30 ` [PATCH] random: fix crash on multiple early calls to add_bootloader_randomness() Dominik Brodowski
2021-10-31 12:33 ` Ard Biesheuvel
2021-11-03 7:14 ` Dominik Brodowski
2021-11-03 7:27 ` Ard Biesheuvel
2021-11-05 6:04 ` Dominik Brodowski
2021-11-03 7:17 ` [PATCH v2] " Dominik Brodowski
2021-11-05 6:04 ` [PATCH v3] " Dominik Brodowski
2021-11-24 12:32 ` Ivan T. Ivanov
2021-12-02 11:35 ` [PATCH v3, resend] " Dominik Brodowski
2021-12-02 16:55 ` Jason A. Donenfeld
2021-12-03 7:58 ` Dominik Brodowski [this message]
2021-12-03 15:39 ` [PATCH v4] " Jason A. Donenfeld
2021-12-03 16:47 ` Jason A. Donenfeld
2021-12-03 17:01 ` Dominik Brodowski
2021-12-06 8:14 ` Ivan T. Ivanov
2021-12-30 18:05 ` Jason A. Donenfeld
2022-01-04 15:06 ` Ivan T. Ivanov
2021-12-06 5:42 ` Hsin-Yi Wang
2021-12-06 20:57 ` [PATCH v5] " Dominik Brodowski
2021-12-07 7:09 ` Hsin-Yi Wang
2021-12-07 7:14 ` Dominik Brodowski
2021-12-07 17:22 ` Jason A. Donenfeld
2021-12-20 14:48 ` Jason A. Donenfeld
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=YanOIvAV1iPBEXR3@light.dominikbrodowski.net \
--to=linux@dominikbrodowski.net \
--cc=Jason@zx2c4.com \
--cc=ardb@kernel.org \
--cc=hsinyi@chromium.org \
--cc=iivanov@suse.de \
--cc=linux-efi@vger.kernel.org \
--cc=linux-kernel@vger.kernel.org \
--cc=tytso@mit.edu \
--subject='Re: [PATCH v4] random: fix crash on multiple early calls to add_bootloader_randomness()' \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).