Skip to content

Commit 48bff10

Browse files
committed
random: opportunistically initialize on /dev/urandom reads
In 6f98a4b ("random: block in /dev/urandom"), we tried to make a successful try_to_generate_entropy() call *required* if the RNG was not already initialized. Unfortunately, weird architectures and old userspaces combined in TCG test harnesses, making that change still not realistic, so it was reverted in 0313bc2 ("Revert "random: block in /dev/urandom""). However, rather than making a successful try_to_generate_entropy() call *required*, we can instead make it *best-effort*. If try_to_generate_entropy() fails, it fails, and nothing changes from the current behavior. If it succeeds, then /dev/urandom becomes safe to use for free. This way, we don't risk the regression potential that led to us reverting the required-try_to_generate_entropy() call before. Practically speaking, this means that at least on x86, /dev/urandom becomes safe. Probably other architectures with working cycle counters will also become safe. And architectures with slow or broken cycle counters at least won't be affected at all by this change. So it may not be the glorious "all things are unified!" change we were hoping for initially, but practically speaking, it makes a positive impact. Cc: Theodore Ts'o <tytso@mit.edu> Cc: Dominik Brodowski <linux@dominikbrodowski.net> Cc: Linus Torvalds <torvalds@linux-foundation.org> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
1 parent 527a986 commit 48bff10

1 file changed

Lines changed: 7 additions & 0 deletions

File tree

drivers/char/random.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1534,6 +1534,13 @@ static ssize_t urandom_read(struct file *file, char __user *buf, size_t nbytes,
15341534
{
15351535
static int maxwarn = 10;
15361536

1537+
/*
1538+
* Opportunistically attempt to initialize the RNG on platforms that
1539+
* have fast cycle counters, but don't (for now) require it to succeed.
1540+
*/
1541+
if (!crng_ready())
1542+
try_to_generate_entropy();
1543+
15371544
if (!crng_ready() && maxwarn > 0) {
15381545
maxwarn--;
15391546
if (__ratelimit(&urandom_warning))

0 commit comments

Comments
 (0)