]> git.ipfire.org Git - thirdparty/kernel/stable.git/commitdiff
random: move rand_initialize() earlier
authorKees Cook <keescook@chromium.org>
Sat, 20 Apr 2019 03:27:05 +0000 (23:27 -0400)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Sat, 25 Jun 2022 09:48:57 +0000 (11:48 +0200)
commit d55535232c3dbde9a523a9d10d68670f5fe5dec3 upstream.

Right now rand_initialize() is run as an early_initcall(), but it only
depends on timekeeping_init() (for mixing ktime_get_real() into the
pools). However, the call to boot_init_stack_canary() for stack canary
initialization runs earlier, which triggers a warning at boot:

random: get_random_bytes called from start_kernel+0x357/0x548 with crng_init=0

Instead, this moves rand_initialize() to after timekeeping_init(), and moves
canary initialization here as well.

Note that this warning may still remain for machines that do not have
UEFI RNG support (which initializes the RNG pools during setup_arch()),
or for x86 machines without RDRAND (or booting without "random.trust=on"
or CONFIG_RANDOM_TRUST_CPU=y).

Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/char/random.c
include/linux/random.h
init/main.c

index f7cc1df8bdfdc77bbb2e62c8f3270e224b5692e9..291a6fa326685af7eb99f7847b9a7cd62c6bccfa 100644 (file)
@@ -1852,7 +1852,7 @@ EXPORT_SYMBOL(get_random_bytes_arch);
  * data into the pool to prepare it for use. The pool is not cleared
  * as that can only decrease the entropy in the pool.
  */
-static void init_std_data(struct entropy_store *r)
+static void __init init_std_data(struct entropy_store *r)
 {
        int i;
        ktime_t now = ktime_get_real();
@@ -1879,7 +1879,7 @@ static void init_std_data(struct entropy_store *r)
  * take care not to overwrite the precious per platform data
  * we were given.
  */
-static int rand_initialize(void)
+int __init rand_initialize(void)
 {
        init_std_data(&input_pool);
        init_std_data(&blocking_pool);
@@ -1891,7 +1891,6 @@ static int rand_initialize(void)
        }
        return 0;
 }
-early_initcall(rand_initialize);
 
 #ifdef CONFIG_BLOCK
 void rand_initialize_disk(struct gendisk *disk)
index 37209b3b22ae498170f059092d06707b83857f01..45bf50d0e886f53be92709639c69a1811340b6af 100644 (file)
@@ -36,6 +36,7 @@ extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;
 
 extern void get_random_bytes(void *buf, int nbytes);
 extern int wait_for_random_bytes(void);
+extern int __init rand_initialize(void);
 extern bool rng_is_initialized(void);
 extern int add_random_ready_callback(struct random_ready_callback *rdy);
 extern void del_random_ready_callback(struct random_ready_callback *rdy);
index 272ec131211c0667e0b25b4e6df95094dfff281f..c101dd088b948327726378a789a08e13c3e9c19e 100644 (file)
@@ -549,13 +549,6 @@ asmlinkage __visible void __init start_kernel(void)
        page_address_init();
        pr_notice("%s", linux_banner);
        setup_arch(&command_line);
-       /*
-        * Set up the the initial canary and entropy after arch
-        * and after adding latent and command line entropy.
-        */
-       add_latent_entropy();
-       add_device_randomness(command_line, strlen(command_line));
-       boot_init_stack_canary();
        mm_init_cpumask(&init_mm);
        setup_command_line(command_line);
        setup_nr_cpu_ids();
@@ -640,6 +633,20 @@ asmlinkage __visible void __init start_kernel(void)
        hrtimers_init();
        softirq_init();
        timekeeping_init();
+
+       /*
+        * For best initial stack canary entropy, prepare it after:
+        * - setup_arch() for any UEFI RNG entropy and boot cmdline access
+        * - timekeeping_init() for ktime entropy used in rand_initialize()
+        * - rand_initialize() to get any arch-specific entropy like RDRAND
+        * - add_latent_entropy() to get any latent entropy
+        * - adding command line entropy
+        */
+       rand_initialize();
+       add_latent_entropy();
+       add_device_randomness(command_line, strlen(command_line));
+       boot_init_stack_canary();
+
        time_init();
        perf_event_init();
        profile_init();