diff options
Diffstat (limited to 'drivers/char/random.c')
| -rw-r--r-- | drivers/char/random.c | 45 | 
1 files changed, 20 insertions, 25 deletions
diff --git a/drivers/char/random.c b/drivers/char/random.c index b691b9d59503..e3dd1dd3dd22 100644 --- a/drivers/char/random.c +++ b/drivers/char/random.c @@ -87,7 +87,7 @@ static struct fasync_struct *fasync;  /* Control how we warn userspace. */  static struct ratelimit_state urandom_warning = -	RATELIMIT_STATE_INIT("warn_urandom_randomness", HZ, 3); +	RATELIMIT_STATE_INIT_FLAGS("urandom_warning", HZ, 3, RATELIMIT_MSG_ON_RELEASE);  static int ratelimit_disable __read_mostly =  	IS_ENABLED(CONFIG_WARN_ALL_UNSEEDED_RANDOM);  module_param_named(ratelimit_disable, ratelimit_disable, int, 0644); @@ -408,7 +408,7 @@ static ssize_t get_random_bytes_user(struct iov_iter *iter)  	/*  	 * Immediately overwrite the ChaCha key at index 4 with random -	 * bytes, in case userspace causes copy_to_user() below to sleep +	 * bytes, in case userspace causes copy_to_iter() below to sleep  	 * forever, so that we still retain forward secrecy in that case.  	 */  	crng_make_state(chacha_state, (u8 *)&chacha_state[4], CHACHA_KEY_SIZE); @@ -650,7 +650,8 @@ static void __cold _credit_init_bits(size_t bits)  	if (orig < POOL_READY_BITS && new >= POOL_READY_BITS) {  		crng_reseed(); /* Sets crng_init to CRNG_READY under base_crng.lock. */ -		execute_in_process_context(crng_set_ready, &set_ready); +		if (static_key_initialized) +			execute_in_process_context(crng_set_ready, &set_ready);  		wake_up_interruptible(&crng_init_wait);  		kill_fasync(&fasync, SIGIO, POLL_IN);  		pr_notice("crng init done\n"); @@ -724,9 +725,8 @@ static void __cold _credit_init_bits(size_t bits)   *   **********************************************************************/ -static bool used_arch_random; -static bool trust_cpu __ro_after_init = IS_ENABLED(CONFIG_RANDOM_TRUST_CPU); -static bool trust_bootloader __ro_after_init = IS_ENABLED(CONFIG_RANDOM_TRUST_BOOTLOADER); +static bool trust_cpu __initdata = IS_ENABLED(CONFIG_RANDOM_TRUST_CPU); +static bool trust_bootloader __initdata = IS_ENABLED(CONFIG_RANDOM_TRUST_BOOTLOADER);  static int __init parse_trust_cpu(char *arg)  {  	return kstrtobool(arg, &trust_cpu); @@ -776,7 +776,7 @@ static struct notifier_block pm_notifier = { .notifier_call = random_pm_notifica  int __init random_init(const char *command_line)  {  	ktime_t now = ktime_get_real(); -	unsigned int i, arch_bytes; +	unsigned int i, arch_bits;  	unsigned long entropy;  #if defined(LATENT_ENTROPY_PLUGIN) @@ -784,12 +784,12 @@ int __init random_init(const char *command_line)  	_mix_pool_bytes(compiletime_seed, sizeof(compiletime_seed));  #endif -	for (i = 0, arch_bytes = BLAKE2S_BLOCK_SIZE; +	for (i = 0, arch_bits = BLAKE2S_BLOCK_SIZE * 8;  	     i < BLAKE2S_BLOCK_SIZE; i += sizeof(entropy)) {  		if (!arch_get_random_seed_long_early(&entropy) &&  		    !arch_get_random_long_early(&entropy)) {  			entropy = random_get_entropy(); -			arch_bytes -= sizeof(entropy); +			arch_bits -= sizeof(entropy) * 8;  		}  		_mix_pool_bytes(&entropy, sizeof(entropy));  	} @@ -798,11 +798,18 @@ int __init random_init(const char *command_line)  	_mix_pool_bytes(command_line, strlen(command_line));  	add_latent_entropy(); +	/* +	 * If we were initialized by the bootloader before jump labels are +	 * initialized, then we should enable the static branch here, where +	 * it's guaranteed that jump labels have been initialized. +	 */ +	if (!static_branch_likely(&crng_is_ready) && crng_init >= CRNG_READY) +		crng_set_ready(NULL); +  	if (crng_ready())  		crng_reseed();  	else if (trust_cpu) -		credit_init_bits(arch_bytes * 8); -	used_arch_random = arch_bytes * 8 >= POOL_READY_BITS; +		_credit_init_bits(arch_bits);  	WARN_ON(register_pm_notifier(&pm_notifier)); @@ -812,17 +819,6 @@ int __init random_init(const char *command_line)  }  /* - * Returns whether arch randomness has been mixed into the initial - * state of the RNG, regardless of whether or not that randomness - * was credited. Knowing this is only good for a very limited set - * of uses, such as early init printk pointer obfuscation. - */ -bool rng_has_arch_random(void) -{ -	return used_arch_random; -} - -/*   * Add device- or boot-specific data to the input pool to help   * initialize it.   * @@ -865,13 +861,12 @@ EXPORT_SYMBOL_GPL(add_hwgenerator_randomness);   * Handle random seed passed by bootloader, and credit it if   * CONFIG_RANDOM_TRUST_BOOTLOADER is set.   */ -void __cold add_bootloader_randomness(const void *buf, size_t len) +void __init add_bootloader_randomness(const void *buf, size_t len)  {  	mix_pool_bytes(buf, len);  	if (trust_bootloader)  		credit_init_bits(len * 8);  } -EXPORT_SYMBOL_GPL(add_bootloader_randomness);  #if IS_ENABLED(CONFIG_VMGENID)  static BLOCKING_NOTIFIER_HEAD(vmfork_chain); @@ -1014,7 +1009,7 @@ void add_interrupt_randomness(int irq)  	if (new_count & MIX_INFLIGHT)  		return; -	if (new_count < 64 && !time_is_before_jiffies(fast_pool->last + HZ)) +	if (new_count < 1024 && !time_is_before_jiffies(fast_pool->last + HZ))  		return;  	if (unlikely(!fast_pool->mix.func))  |