Commit 0791e8b6 authored by Jason A. Donenfeld's avatar Jason A. Donenfeld

random: tie batched entropy generation to base_crng generation

Now that we have an explicit base_crng generation counter, we don't need
a separate one for batched entropy. Rather, we can just move the
generation forward every time we change crng_init state or update the
base_crng key.

Cc: Theodore Ts'o <tytso@mit.edu>
Reviewed-by: default avatarEric Biggers <ebiggers@google.com>
Reviewed-by: default avatarDominik Brodowski <linux@dominikbrodowski.net>
Signed-off-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
parent 7191c628
...@@ -430,8 +430,6 @@ static DEFINE_PER_CPU(struct crng, crngs) = { ...@@ -430,8 +430,6 @@ static DEFINE_PER_CPU(struct crng, crngs) = {
static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait); static DECLARE_WAIT_QUEUE_HEAD(crng_init_wait);
static void invalidate_batched_entropy(void);
/* /*
* crng_fast_load() can be called by code in the interrupt service * crng_fast_load() can be called by code in the interrupt service
* path. So we can't afford to dilly-dally. Returns the number of * path. So we can't afford to dilly-dally. Returns the number of
...@@ -454,7 +452,7 @@ static size_t crng_fast_load(const void *cp, size_t len) ...@@ -454,7 +452,7 @@ static size_t crng_fast_load(const void *cp, size_t len)
src++; crng_init_cnt++; len--; ret++; src++; crng_init_cnt++; len--; ret++;
} }
if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) { if (crng_init_cnt >= CRNG_INIT_CNT_THRESH) {
invalidate_batched_entropy(); ++base_crng.generation;
crng_init = 1; crng_init = 1;
} }
spin_unlock_irqrestore(&base_crng.lock, flags); spin_unlock_irqrestore(&base_crng.lock, flags);
...@@ -531,7 +529,6 @@ static void crng_reseed(void) ...@@ -531,7 +529,6 @@ static void crng_reseed(void)
WRITE_ONCE(base_crng.generation, next_gen); WRITE_ONCE(base_crng.generation, next_gen);
WRITE_ONCE(base_crng.birth, jiffies); WRITE_ONCE(base_crng.birth, jiffies);
if (crng_init < 2) { if (crng_init < 2) {
invalidate_batched_entropy();
crng_init = 2; crng_init = 2;
finalize_init = true; finalize_init = true;
} }
...@@ -1256,8 +1253,9 @@ int __init rand_initialize(void) ...@@ -1256,8 +1253,9 @@ int __init rand_initialize(void)
mix_pool_bytes(utsname(), sizeof(*(utsname()))); mix_pool_bytes(utsname(), sizeof(*(utsname())));
extract_entropy(base_crng.key, sizeof(base_crng.key)); extract_entropy(base_crng.key, sizeof(base_crng.key));
++base_crng.generation;
if (arch_init && trust_cpu && crng_init < 2) { if (arch_init && trust_cpu && crng_init < 2) {
invalidate_batched_entropy();
crng_init = 2; crng_init = 2;
pr_notice("crng init done (trusting CPU's manufacturer)\n"); pr_notice("crng init done (trusting CPU's manufacturer)\n");
} }
...@@ -1607,8 +1605,6 @@ static int __init random_sysctls_init(void) ...@@ -1607,8 +1605,6 @@ static int __init random_sysctls_init(void)
device_initcall(random_sysctls_init); device_initcall(random_sysctls_init);
#endif /* CONFIG_SYSCTL */ #endif /* CONFIG_SYSCTL */
static atomic_t batch_generation = ATOMIC_INIT(0);
struct batched_entropy { struct batched_entropy {
union { union {
/* /*
...@@ -1622,8 +1618,8 @@ struct batched_entropy { ...@@ -1622,8 +1618,8 @@ struct batched_entropy {
u32 entropy_u32[CHACHA_BLOCK_SIZE * 3 / (2 * sizeof(u32))]; u32 entropy_u32[CHACHA_BLOCK_SIZE * 3 / (2 * sizeof(u32))];
}; };
local_lock_t lock; local_lock_t lock;
unsigned long generation;
unsigned int position; unsigned int position;
int generation;
}; };
/* /*
...@@ -1643,14 +1639,14 @@ u64 get_random_u64(void) ...@@ -1643,14 +1639,14 @@ u64 get_random_u64(void)
unsigned long flags; unsigned long flags;
struct batched_entropy *batch; struct batched_entropy *batch;
static void *previous; static void *previous;
int next_gen; unsigned long next_gen;
warn_unseeded_randomness(&previous); warn_unseeded_randomness(&previous);
local_lock_irqsave(&batched_entropy_u64.lock, flags); local_lock_irqsave(&batched_entropy_u64.lock, flags);
batch = raw_cpu_ptr(&batched_entropy_u64); batch = raw_cpu_ptr(&batched_entropy_u64);
next_gen = atomic_read(&batch_generation); next_gen = READ_ONCE(base_crng.generation);
if (batch->position >= ARRAY_SIZE(batch->entropy_u64) || if (batch->position >= ARRAY_SIZE(batch->entropy_u64) ||
next_gen != batch->generation) { next_gen != batch->generation) {
_get_random_bytes(batch->entropy_u64, sizeof(batch->entropy_u64)); _get_random_bytes(batch->entropy_u64, sizeof(batch->entropy_u64));
...@@ -1677,14 +1673,14 @@ u32 get_random_u32(void) ...@@ -1677,14 +1673,14 @@ u32 get_random_u32(void)
unsigned long flags; unsigned long flags;
struct batched_entropy *batch; struct batched_entropy *batch;
static void *previous; static void *previous;
int next_gen; unsigned long next_gen;
warn_unseeded_randomness(&previous); warn_unseeded_randomness(&previous);
local_lock_irqsave(&batched_entropy_u32.lock, flags); local_lock_irqsave(&batched_entropy_u32.lock, flags);
batch = raw_cpu_ptr(&batched_entropy_u32); batch = raw_cpu_ptr(&batched_entropy_u32);
next_gen = atomic_read(&batch_generation); next_gen = READ_ONCE(base_crng.generation);
if (batch->position >= ARRAY_SIZE(batch->entropy_u32) || if (batch->position >= ARRAY_SIZE(batch->entropy_u32) ||
next_gen != batch->generation) { next_gen != batch->generation) {
_get_random_bytes(batch->entropy_u32, sizeof(batch->entropy_u32)); _get_random_bytes(batch->entropy_u32, sizeof(batch->entropy_u32));
...@@ -1700,15 +1696,6 @@ u32 get_random_u32(void) ...@@ -1700,15 +1696,6 @@ u32 get_random_u32(void)
} }
EXPORT_SYMBOL(get_random_u32); EXPORT_SYMBOL(get_random_u32);
/* It's important to invalidate all potential batched entropy that might
* be stored before the crng is initialized, which we can do lazily by
* bumping the generation counter.
*/
static void invalidate_batched_entropy(void)
{
atomic_inc(&batch_generation);
}
/** /**
* randomize_page - Generate a random, page aligned address * randomize_page - Generate a random, page aligned address
* @start: The smallest acceptable address the caller will take. * @start: The smallest acceptable address the caller will take.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment