Skip to content

Commit d555352

Browse files
keestytso
authored andcommitted
random: move rand_initialize() earlier
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 <[email protected]> Signed-off-by: Theodore Ts'o <[email protected]>
1 parent eb9d1bf commit d555352

File tree

3 files changed

+17
-10
lines changed

3 files changed

+17
-10
lines changed

drivers/char/random.c

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1783,7 +1783,7 @@ EXPORT_SYMBOL(get_random_bytes_arch);
17831783
* data into the pool to prepare it for use. The pool is not cleared
17841784
* as that can only decrease the entropy in the pool.
17851785
*/
1786-
static void init_std_data(struct entropy_store *r)
1786+
static void __init init_std_data(struct entropy_store *r)
17871787
{
17881788
int i;
17891789
ktime_t now = ktime_get_real();
@@ -1810,7 +1810,7 @@ static void init_std_data(struct entropy_store *r)
18101810
* take care not to overwrite the precious per platform data
18111811
* we were given.
18121812
*/
1813-
static int rand_initialize(void)
1813+
int __init rand_initialize(void)
18141814
{
18151815
init_std_data(&input_pool);
18161816
init_std_data(&blocking_pool);
@@ -1822,7 +1822,6 @@ static int rand_initialize(void)
18221822
}
18231823
return 0;
18241824
}
1825-
early_initcall(rand_initialize);
18261825

18271826
#ifdef CONFIG_BLOCK
18281827
void rand_initialize_disk(struct gendisk *disk)

include/linux/random.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ extern void add_interrupt_randomness(int irq, int irq_flags) __latent_entropy;
3636

3737
extern void get_random_bytes(void *buf, int nbytes);
3838
extern int wait_for_random_bytes(void);
39+
extern int __init rand_initialize(void);
3940
extern bool rng_is_initialized(void);
4041
extern int add_random_ready_callback(struct random_ready_callback *rdy);
4142
extern void del_random_ready_callback(struct random_ready_callback *rdy);

init/main.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -564,13 +564,6 @@ asmlinkage __visible void __init start_kernel(void)
564564
page_address_init();
565565
pr_notice("%s", linux_banner);
566566
setup_arch(&command_line);
567-
/*
568-
* Set up the the initial canary and entropy after arch
569-
* and after adding latent and command line entropy.
570-
*/
571-
add_latent_entropy();
572-
add_device_randomness(command_line, strlen(command_line));
573-
boot_init_stack_canary();
574567
mm_init_cpumask(&init_mm);
575568
setup_command_line(command_line);
576569
setup_nr_cpu_ids();
@@ -655,6 +648,20 @@ asmlinkage __visible void __init start_kernel(void)
655648
hrtimers_init();
656649
softirq_init();
657650
timekeeping_init();
651+
652+
/*
653+
* For best initial stack canary entropy, prepare it after:
654+
* - setup_arch() for any UEFI RNG entropy and boot cmdline access
655+
* - timekeeping_init() for ktime entropy used in rand_initialize()
656+
* - rand_initialize() to get any arch-specific entropy like RDRAND
657+
* - add_latent_entropy() to get any latent entropy
658+
* - adding command line entropy
659+
*/
660+
rand_initialize();
661+
add_latent_entropy();
662+
add_device_randomness(command_line, strlen(command_line));
663+
boot_init_stack_canary();
664+
658665
time_init();
659666
printk_safe_init();
660667
perf_event_init();

0 commit comments

Comments
 (0)