Skip to content

Commit 6e513bc

Browse files
tytsogregkh
authored andcommitted
random: fix crng_ready() test
commit 43838a2 upstream. The crng_init variable has three states: 0: The CRNG is not initialized at all 1: The CRNG has a small amount of entropy, hopefully good enough for early-boot, non-cryptographical use cases 2: The CRNG is fully initialized and we are sure it is safe for cryptographic use cases. The crng_ready() function should only return true once we are in the last state. This addresses CVE-2018-1108. Reported-by: Jann Horn <jannh@google.com> Fixes: e192be9 ("random: replace non-blocking pool...") Cc: stable@kernel.org # 4.8+ Signed-off-by: Theodore Ts'o <tytso@mit.edu> Reviewed-by: Jann Horn <jannh@google.com> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 8036cda commit 6e513bc

1 file changed

Lines changed: 5 additions & 5 deletions

File tree

‎drivers/char/random.c‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ struct crng_state primary_crng = {
427427
* its value (from 0->1->2).
428428
*/
429429
static int crng_init = 0;
430-
#define crng_ready() (likely(crng_init > 0))
430+
#define crng_ready() (likely(crng_init > 1))
431431
static int crng_init_cnt = 0;
432432
#define CRNG_INIT_CNT_THRESH (2*CHACHA20_KEY_SIZE)
433433
static void _extract_crng(struct crng_state *crng,
@@ -793,7 +793,7 @@ static int crng_fast_load(const char *cp, size_t len)
793793

794794
if (!spin_trylock_irqsave(&primary_crng.lock, flags))
795795
return 0;
796-
if (crng_ready()) {
796+
if (crng_init != 0) {
797797
spin_unlock_irqrestore(&primary_crng.lock, flags);
798798
return 0;
799799
}
@@ -855,7 +855,7 @@ static void _extract_crng(struct crng_state *crng,
855855
{
856856
unsigned long v, flags;
857857

858-
if (crng_init > 1 &&
858+
if (crng_ready() &&
859859
time_after(jiffies, crng->init_time + CRNG_RESEED_INTERVAL))
860860
crng_reseed(crng, crng == &primary_crng ? &input_pool : NULL);
861861
spin_lock_irqsave(&crng->lock, flags);
@@ -1141,7 +1141,7 @@ void add_interrupt_randomness(int irq, int irq_flags)
11411141
fast_mix(fast_pool);
11421142
add_interrupt_bench(cycles);
11431143

1144-
if (!crng_ready()) {
1144+
if (unlikely(crng_init == 0)) {
11451145
if ((fast_pool->count >= 64) &&
11461146
crng_fast_load((char *) fast_pool->pool,
11471147
sizeof(fast_pool->pool))) {
@@ -2214,7 +2214,7 @@ void add_hwgenerator_randomness(const char *buffer, size_t count,
22142214
{
22152215
struct entropy_store *poolp = &input_pool;
22162216

2217-
if (!crng_ready()) {
2217+
if (unlikely(crng_init == 0)) {
22182218
crng_fast_load(buffer, count);
22192219
return;
22202220
}

0 commit comments

Comments
 (0)