random: split initialization into early arch step and later non-arch step
Subsystems (2)
Jason A. Donenfeld
maintainerPATCH 1/1 random: split initialization into early arch step and later non-arch step
The full RNG initialization relies on some timestamps, made possible with general functions like time_init() and timekeeping_init(). However, these are only available rather late in initialization. Meanwhile, other things, such as memory allocator functions, make use of the RNG much earlier.
So split RNG initialization into two phases. We can give arch randomness very early on, and then later, after timekeeping and such are available, initialize the rest.
This ensures that, for example, slabs are properly randomized if RDRAND is available. Another positive consequence is that on systems with RDRAND, running with CONFIG_WARN_ALL_UNSEEDED_RANDOM=y results in no warnings at all.
---
I intend to take this through the random.git tree, but reviews/acks would be appreciated, given that I'm touching init/main.c.
drivers/char/random.c | 47 ++++++++++++++++++++++++------------------ include/linux/random.h | 3 ++- init/main.c | 17 +++++++-------- 3 files changed, 37 insertions(+), 30 deletions(-)
drivers/char/random.c +27 -20
@@ -772,18 +772,13 @@ static int random_pm_notification(struct notifier_block *nb, unsigned long actiostatic struct notifier_block pm_notifier = { .notifier_call = random_pm_notification };/* -* The first collection of entropy occurs at system boot while interrupts -* are still turned off. Here we push in latent entropy, RDSEED, a timestamp, -* utsname(), and the command line. Depending on the above configuration knob, -* RDSEED may be considered sufficient for initialization. Note that much -* earlier setup may already have pushed entropy into the input pool by the -* time we get here. +* This is called extremely early, before time keeping functionality is +* available, but arch randomness is. Interrupts are not yet enabled. */ -int __init random_init (const char *command_line) +void __init random_init_early (const char *command_line) { -ktime_t now =ktime_get_real (); -size_t i, longs, arch_bits;unsigned long entropy[BLAKE2S_BLOCK_SIZE /sizeof (long )]; +size_t i, longs, arch_bits;#if defined(LATENT_ENTROPY_PLUGIN ) static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE] __initconst __latent_entropy;
@@ -803,34 +798,46 @@ int __init random_init(const char *command_line) i += longs;continue ; } - entropy[0 ] =random_get_entropy (); -_mix_pool_bytes (entropy,sizeof (*entropy)); arch_bits -=sizeof (*entropy) *8 ; ++i; } -_mix_pool_bytes (&now,sizeof (now)); -_mix_pool_bytes (utsname (),sizeof (*(utsname ()))); +_mix_pool_bytes (command_line,strlen (command_line)); + +if (trust_cpu) +credit_init_bits (arch_bits); +} + +/* +* This is called a little bit after the prior function, and now there is +* access to timestamps counters. Interrupts are not yet enabled. +*/ +void __init random_init (void ) +{ +unsigned long entropy =random_get_entropy (); +ktime_t now =ktime_get_real (); + +_mix_pool_bytes (utsname (),sizeof (*(utsname ()))); +_mix_pool_bytes (&now,sizeof (now)); +_mix_pool_bytes (&entropy,sizeof (entropy));add_latent_entropy ();/* -* If we were initialized by the bootloader before jump labels are -* initialized, then we should enable the static branch here, where +* If we were initialized by the cpu or 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 ); +/* Reseed if already seeded by earlier phases. */ if (crng_ready ())crng_reseed (); -else if (trust_cpu) -_credit_init_bits (arch_bits);WARN_ON (register_pm_notifier (&pm_notifier)); -WARN (!random_get_entropy (),"Missing cycle counter and fallback timer; RNG " -"entropy collection will consequently suffer." ); -return 0 ; +WARN (!entropy,"Missing cycle counter and fallback timer; RNG " +"entropy collection will consequently suffer." ); }/*
include/linux/random.h +2 -1
@@ -72,7 +72,8 @@ static inline unsigned long get_random_canary(void)return get_random_long () &CANARY_MASK ; } -int __init random_init (const char *command_line); +void __init random_init_early (const char *command_line); +void __init random_init (void );bool rng_is_initialized (void );int wait_for_random_bytes (void );
init/main.c +8 -9
@@ -976,6 +976,9 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void)parse_args ("Setting extra init args" , extra_init_args,NULL ,0 ,-1 ,-1 ,NULL , set_init_arg); +/* Call before any memory or allocators are initialized */ +random_init_early (command_line); +/* * These use large bootmem allocations and must precede * kmem_cache_init()
@@ -1035,17 +1038,13 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void)hrtimers_init ();softirq_init ();timekeeping_init (); -kfence_init ();time_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 random_init() -* - time_init() for making random_get_entropy() work on some platforms -* - random_init() to initialize the RNG from from early entropy sources -*/ -random_init (command_line); +/* This must be after timekeeping is initialized */ +random_init (); + +/* These make use of the initialized randomness */ +kfence_init ();boot_init_stack_canary ();perf_event_init ();
Signature · 1 line
2.37.3
Quoted 6 lines · On Mon, 26 Sep 2022 18:03:32 +0200 "Jason A. Donenfeld" <[email protected]> wrote:
The full RNG initialization relies on some timestamps, made possible with general functions like
time_init()andtimekeeping_init(). However, these are only available rather late in initialization. Meanwhile, other things, such as memory allocator functions, make use of the RNG much earlier.
Quoted 8 lines
So split RNG initialization into two phases. We can give arch randomness very early on, and then later, after timekeeping and such are available, initialize the rest.
This ensures that, for example, slabs are properly randomized if RDRAND is available. Another positive consequence is that on systems with RDRAND, running with
CONFIG_WARN_ALL_UNSEEDED_RANDOM=yresults in no warnings at all.
Please give a full description of the user-visible runtime effects of this shortcoming.
Quoted 3 lines
Which is important when proposing a -stable backport.
Jason A. Donenfeld
maintainerQuoted 13 lines · Andrew Morton
Quoted 10 lines · On Mon, 26 Sep 2022 18:03:32 +0200 "Jason A. Donenfeld" <[email protected]> wrote:
The full RNG initialization relies on some timestamps, made possible with general functions like
time_init()andtimekeeping_init(). However, these are only available rather late in initialization. Meanwhile, other things, such as memory allocator functions, make use of the RNG much earlier.So split RNG initialization into two phases. We can give arch randomness very early on, and then later, after timekeeping and such are available, initialize the rest.
Quoted 7 lines
Quoted 4 lines
This ensures that, for example, slabs are properly randomized if RDRAND is available. Another positive consequence is that on systems with RDRAND, running with
CONFIG_WARN_ALL_UNSEEDED_RANDOM=yresults in no warnings at all.Please give a full description of the user-visible runtime effects of this shortcoming.
Sure. I'll expand that paragraph to read:
This ensures that, for example, slabs are properly randomized if RDRAND is available. Without this, CONFIG_SLAB_FREELIST_RANDOM=y loses a degree of its security, because its random seed is potentially deterministic, since it hasn't yet incorporated RDRAND. It also makes it possible to use a better seed in kfence, which currently relies on only the cycle counter.
Another positive consequence is that on systems with RDRAND, running with CONFIG_WARN_ALL_UNSEEDED_RANDOM=y results in no warnings at all.
Quoted 6 lines · Jason A. Donenfeld
The full RNG initialization relies on some timestamps, made possible with general functions like
time_init()andtimekeeping_init(). However, these are only available rather late in initialization. Meanwhile, other things, such as memory allocator functions, make use of the RNG much earlier.
Quoted 8 lines
So split RNG initialization into two phases. We can give arch randomness very early on, and then later, after timekeeping and such are available, initialize the rest.
This ensures that, for example, slabs are properly randomized if RDRAND is available. Another positive consequence is that on systems with RDRAND, running with
CONFIG_WARN_ALL_UNSEEDED_RANDOM=yresults in no warnings at all.
Nice! I like it. Notes below...
Quoted 42 lines
Cc Kees Cook <[email protected]>Cc Andrew Morton <[email protected]>Signed-off-by Jason A. Donenfeld <[email protected]>---
I intend to take this through the random.git tree, but reviews/acks would be appreciated, given that I'm touchinginit/main.c.drivers/char/random.c | 47 ++++++++++++++++++++++++------------------ include/linux/random.h | 3 ++- init/main.c | 17 +++++++-------- 3 files changed, 37 insertions(+), 30 deletions(-)drivers/char/random.c +4
-9@@ -772,18 +772,13 @@ static int random_pm_notification(struct notifier_block *nb, unsigned long actiostatic struct notifier_block pm_notifier = { .notifier_call = random_pm_notification };/* -* The first collection of entropy occurs at system boot while interrupts -* are still turned off. Here we push in latent entropy, RDSEED, a timestamp, -* utsname(), and the command line. Depending on the above configuration knob, -* RDSEED may be considered sufficient for initialization. Note that much -* earlier setup may already have pushed entropy into the input pool by the -* time we get here. +* This is called extremely early, before time keeping functionality is +* available, but arch randomness is. Interrupts are not yet enabled. */ -int __init random_init (const char *command_line) +void __init random_init_early (const char *command_line) { -ktime_t now =ktime_get_real (); -size_t i, longs, arch_bits;unsigned long entropy[BLAKE2S_BLOCK_SIZE /sizeof (long )]; +size_t i, longs, arch_bits;#if defined(LATENT_ENTROPY_PLUGIN ) static const u8 compiletime_seed[BLAKE2S_BLOCK_SIZE] __initconst __latent_entropy;
drivers/char/random.c int __init random_init(const char *command_line) · line 800
Quoted 3 lines
drivers/char/random.c +0
-0@@ -803,34 +798,46 @@ int __init random_init(const char *command_line) i += longs;continue ; }
Can find a way to get efi_get_random_bytes() in here too? (As a separate patch.) I don't see where that actually happens anywhere currently, and we should have it available at this point in the boot, yes?
drivers/char/random.c int __init random_init(const char *command_line) · line 812
Quoted 7 lines
drivers/char/random.c +0
-4- entropy[0 ] =random_get_entropy (); -_mix_pool_bytes (entropy,sizeof (*entropy)); arch_bits -=sizeof (*entropy) *8 ; ++i; } -_mix_pool_bytes (&now,sizeof (now)); -_mix_pool_bytes (utsname (),sizeof (*(utsname ())));
Hm, can't we keep utsname in the early half by using init_utsname() ?
Quoted 7 lines
drivers/char/random.c +6
-0+_mix_pool_bytes (command_line,strlen (command_line)); + +if (trust_cpu) +credit_init_bits (arch_bits); +} +
drivers/char/random.c int __init random_init(const char *command_line) · line 820
Quoted 10 lines
drivers/char/random.c +10
-0+/* +* This is called a little bit after the prior function, and now there is +* access to timestamps counters. Interrupts are not yet enabled. +*/ +void __init random_init (void ) +{ +unsigned long entropy =random_get_entropy (); +ktime_t now =ktime_get_real (); + +_mix_pool_bytes (utsname (),sizeof (*(utsname ())));
(...and then obviously don't repeat it here.)
Quoted 49 lines
drivers/char/random.c +7
-7+_mix_pool_bytes (&now,sizeof (now)); +_mix_pool_bytes (&entropy,sizeof (entropy));add_latent_entropy ();/* -* If we were initialized by the bootloader before jump labels are -* initialized, then we should enable the static branch here, where +* If we were initialized by the cpu or 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 ); +/* Reseed if already seeded by earlier phases. */ if (crng_ready ())crng_reseed (); -else if (trust_cpu) -_credit_init_bits (arch_bits);WARN_ON (register_pm_notifier (&pm_notifier)); -WARN (!random_get_entropy (),"Missing cycle counter and fallback timer; RNG " -"entropy collection will consequently suffer." ); -return 0 ; +WARN (!entropy,"Missing cycle counter and fallback timer; RNG " +"entropy collection will consequently suffer." ); }/* include/linux/random.h +2
-1@@ -72,7 +72,8 @@ static inline unsigned long get_random_canary(void)return get_random_long () &CANARY_MASK ; } -int __init random_init (const char *command_line); +void __init random_init_early (const char *command_line); +void __init random_init (void );bool rng_is_initialized (void );int wait_for_random_bytes (void );init/main.c +0
-0
init/main.c asmlinkage __visible void __init __no_sanitize_address start_kernel(void) · line 979
Quoted 4 lines
init/main.c +1
-0@@ -976,6 +976,9 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void)parse_args ("Setting extra init args" , extra_init_args,NULL ,0 ,-1 ,-1 ,NULL , set_init_arg); +/* Call before any memory or allocators are initialized */
Maybe for greater clarity:
/* Pre-time-keeping entropy collection before allocator init. */
init/main.c asmlinkage __visible void __init __no_sanitize_address start_kernel(void) · line 1041
Quoted 11 lines
init/main.c +2
-1+random_init_early (command_line); +/* * These use large bootmem allocations and must precede * kmem_cache_init() @@ -1035,17 +1038,13 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void)hrtimers_init ();softirq_init ();timekeeping_init (); -kfence_init ();time_init ();
Was there a reason kfence_init() was happening before time_init()?
init/main.c asmlinkage __visible void __init __no_sanitize_address start_kernel(void) · line 1046
Quoted 13 lines
init/main.c +4
-8-/* -* 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 random_init() -* - time_init() for making random_get_entropy() work on some platforms -* - random_init() to initialize the RNG from from early entropy sources -*/ -random_init (command_line); +/* This must be after timekeeping is initialized */ +random_init (); + +/* These make use of the initialized randomness */
I'd clarify this more:
/* These make use of the fully initialized randomness entropy. */
Quoted 7 lines
init/main.c +1
-0+kfence_init ();boot_init_stack_canary ();perf_event_init ();Signature · 1 line
2.37.3
Signature · 1 line
Kees Cook
Jason A. Donenfeld
maintainerQuoted 3 lines · Kees Cook
Can find a way to get
efi_get_random_bytes()in here too? (As a separate patch.) I don't see where that actually happens anywhere currently, and we should have it available at this point in the boot, yes?
No, absolutely not. That is not how EFI works. EFI gets its seed to random.c much earlier by way of add_bootloader_randomness().
Quoted 9 lines
drivers/char/random.c int __init random_init(const char *command_line) · line 812
Quoted 7 lines
drivers/char/random.c +0
-4- entropy[0 ] =random_get_entropy (); -_mix_pool_bytes (entropy,sizeof (*entropy)); arch_bits -=sizeof (*entropy) *8 ; ++i; } -_mix_pool_bytes (&now,sizeof (now)); -_mix_pool_bytes (utsname (),sizeof (*(utsname ())));Hm, can't we keep utsname in the early half by using
init_utsname()?
Yes, we could maybe *change* to using init_utsname if we wanted. That seems kind of different though. So I'd prefer that to be a different patch, which would require looking at the interaction with early hostname setting and such. If you want to do that work, I'd certainly welcome the patch.
Quoted 9 lines
init/main.c asmlinkage __visible void __init __no_sanitize_address start_kernel(void) · line 979
Quoted 5 lines
init/main.c +1
-0@@ -976,6 +976,9 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void)parse_args ("Setting extra init args" , extra_init_args,NULL ,0 ,-1 ,-1 ,NULL , set_init_arg); +/* Call before any memory or allocators are initialized */ Maybe for greater clarity:
/* Pre-time-keeping entropy collection before allocator init. */
Will do.
Quoted 14 lines
init/main.c asmlinkage __visible void __init __no_sanitize_address start_kernel(void) · line 1041
Quoted 12 lines
init/main.c +2
-1+random_init_early (command_line); +/* * These use large bootmem allocations and must precede * kmem_cache_init() @@ -1035,17 +1038,13 @@ asmlinkage __visible void __init __no_sanitize_address start_kernel(void)hrtimers_init ();softirq_init ();timekeeping_init (); -kfence_init ();time_init ();Was there a reason
kfence_init()was happening beforetime_init()?
Historically there was, I think, because random_init() used to make weird allocations. But that's been gone for a while. At this point it's a mistake, and removing it allows me to do this:
https://groups.google.com/g/kasan-dev/c/jhExcSv_Pj4
Quoted 15 lines
Quoted 4 lines
init/main.c +0
-2-/* -* For best initial stack canary entropy, prepare it after: init/main.c asmlinkage __visible void __init __no_sanitize_address start_kernel(void) · line 1046
Quoted 10 lines
init/main.c +4
-6-* - setup_arch() for any UEFI RNG entropy and boot cmdline access -* - timekeeping_init() for ktime entropy used in random_init() -* - time_init() for making random_get_entropy() work on some platforms -* - random_init() to initialize the RNG from from early entropy sources -*/ -random_init (command_line); +/* This must be after timekeeping is initialized */ +random_init (); + +/* These make use of the initialized randomness */
Quoted 3 lines
I'd clarify this more:
/* These make use of the fully initialized randomness entropy. */
Okay will do.
Jason
Quoted 7 lines · Jason A. Donenfeld
Quoted 3 lines · Kees Cook
Can find a way to get
efi_get_random_bytes()in here too? (As a separate patch.) I don't see where that actually happens anywhere currently, and we should have it available at this point in the boot, yes?No, absolutely not. That is not how EFI works. EFI gets its seed to
random.cmuch earlier by way ofadd_bootloader_randomness().
Ah! Okay, so, yes, it _does_ get entropy in there, just via a path I didn't see?
Quoted 9 lines
Quoted 9 lines
Quoted 8 lines
drivers/char/random.c +0
-4- entropy[0 ] =random_get_entropy (); -_mix_pool_bytes (entropy,sizeof (*entropy)); arch_bits -=sizeof (*entropy) *8 ; ++i; } -_mix_pool_bytes (&now,sizeof (now)); -_mix_pool_bytes (utsname (),sizeof (*(utsname ())));
Quoted 7 lines
Quoted 1 line
Hm, can't we keep utsname in the early half by using
init_utsname()?Yes, we could maybe *change* to using
init_utsnameif we wanted. That seems kind of different though. So I'd prefer that to be a different patch, which would require looking at the interaction with early hostname setting and such. If you want to do that work, I'd certainly welcome the patch.
Er, isn't that _WAY_ later? Like, hostname isn't set until sysctls up and running, etc. I haven't actually verified 100% but it looks like current->utsname is exactly init_utsname currently.
But if not, I guess it could just get added in both places. I'd be nice to keep kernel version as part of the pre-time-keeping entropy stuffing.
Quoted 7 lines
Quoted 1 line
Was there a reason
kfence_init()was happening beforetime_init()?Historically there was, I think, because
random_init()used to make weird allocations. But that's been gone for a while. At this point it's a mistake, and removing it allows me to do this:
Cool. Is that true for all the -stable releases this is aimed at?
Anyway, just to repeat before: yay! I really like seeing this split up. :)
Signature · 1 line
Kees Cook
Jason A. Donenfeld
maintainerQuoted 7 lines · Kees Cook
Quoted 5 lines · Jason A. Donenfeld
Quoted 3 lines · Kees Cook
Can find a way to get
efi_get_random_bytes()in here too? (As a separate patch.) I don't see where that actually happens anywhere currently, and we should have it available at this point in the boot, yes?
Quoted 5 lines
Quoted 2 lines
No, absolutely not. That is not how EFI works. EFI gets its seed to
random.cmuch earlier by way ofadd_bootloader_randomness().Ah! Okay, so, yes, it _does_ get entropy in there, just via a path I didn't see?
Yes.
Quoted 9 lines
Quoted 5 lines
Yes, we could maybe *change* to using
init_utsnameif we wanted. That seems kind of different though. So I'd prefer that to be a different patch, which would require looking at the interaction with early hostname setting and such. If you want to do that work, I'd certainly welcome the patch.Er, isn't that _WAY_ later? Like, hostname isn't set until sysctls up and running, etc. I haven't actually verified 100% but it looks like
current->utsnameis exactlyinit_utsnamecurrently.
If init_utsname()==utsname() and all is fine, can you please send a patch atop random.git adjusting that and explaining why? I would happily take such a patch. If your suspicion is correct, it would make a most welcome improvement.
Quoted 9 lines
Quoted 7 lines
Quoted 1 line
Was there a reason
kfence_init()was happening beforetime_init()?Historically there was, I think, because
random_init()used to make weird allocations. But that's been gone for a while. At this point it's a mistake, and removing it allows me to do this:Cool. Is that true for all the -stable releases this is aimed at?
Yes.
Though I'll likely drop the stable@ tag for this, and instead visit backporting it later in 6.1's cycle, or even after. There's no need to rush it, and this is an area that has been historically temperamental.
Jason
Jason A. Donenfeld
maintainerQuoted 6 lines · Jason A. Donenfeld
Quoted 6 lines
Quoted 5 lines
Yes, we could maybe *change* to using
init_utsnameif we wanted. That seems kind of different though. So I'd prefer that to be a different patch, which would require looking at the interaction with early hostname setting and such. If you want to do that work, I'd certainly welcome the patch.
Quoted 8 lines
Quoted 3 lines
Er, isn't that _WAY_ later? Like, hostname isn't set until sysctls up and running, etc. I haven't actually verified 100% but it looks like
current->utsnameis exactlyinit_utsnamecurrently.If init_utsname()==utsname() and all is fine, can you please send a patch atop random.git adjusting that and explaining why? I would happily take such a patch. If your suspicion is correct, it would make a most welcome improvement.