random: fix soft lockup when trying to read from an uninitialized blocking pool
authorTheodore Ts'o <tytso@mit.edu>
Wed, 22 May 2019 16:02:16 +0000 (12:02 -0400)
committerTheodore Ts'o <tytso@mit.edu>
Sun, 26 May 2019 04:11:49 +0000 (00:11 -0400)
Fixes: eb9d1bf079bb: "random: only read from /dev/random after its pool has received 128 bits"
Reported-by: kernel test robot <lkp@intel.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
drivers/char/random.c

index a42b3d764da84e979d12587afdf43643f9af6606..5d5ea4ce144293e3e99cc7b7bcc68250cf12df5b 100644 (file)
@@ -772,8 +772,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_);
@@ -789,6 +792,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)) {
@@ -1936,8 +1946,8 @@ _random_read(int nonblock, char __user *buf, size_t nbytes)
                        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;
        }