random: introduce drain_entropy() helper to declutter crng_reseed()
authorJason A. Donenfeld <Jason@zx2c4.com>
Fri, 11 Feb 2022 11:19:49 +0000 (12:19 +0100)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 25 Jun 2022 09:46:34 +0000 (11:46 +0200)
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.

Cc: Theodore Ts'o <tytso@mit.edu>
Reviewed-by: Dominik Brodowski <linux@dominikbrodowski.net>
Reviewed-by: Eric Biggers <ebiggers@google.com>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/char/random.c

index c10000f578bbaf2fc33d05f429d533c937ec614a..a334d67dc1d2925a83397b51e3c01cdcbb4f28bc 100644 (file)
@@ -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 *cp, size_t len)
 static 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, size_t nbytes)
        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;
+}
+
 #define warn_unseeded_randomness(previous) \
        _warn_unseeded_randomness(__func__, (void *)_RET_IP_, (previous))