Commit 3f41b609 authored by Andrey Konovalov's avatar Andrey Konovalov Committed by Linus Torvalds
Browse files

kasan: fix random seed generation for tag-based mode

There are two issues with assigning random percpu seeds right now:

1. We use for_each_possible_cpu() to iterate over cpus, but cpumask is
   not set up yet at the moment of kasan_init(), and thus we only set
   the seed for cpu #0.

2. A call to get_random_u32() always returns the same number and produces
   a message in dmesg, since the random subsystem is not yet initialized.

Fix 1 by calling kasan_init_tags() after cpumask is set up.

Fix 2 by using get_cycles() instead of get_random_u32(). This gives us
lower quality random numbers, but it's good enough, as KASAN is meant to
be used as a debugging tool and not a mitigation.


Signed-off-by: default avatarAndrey Konovalov <>
Cc: Catalin Marinas <>
Cc: Will Deacon <>
Cc: Andrey Ryabinin <>
Cc: Alexander Potapenko <>
Cc: Dmitry Vyukov <>
Signed-off-by: default avatarAndrew Morton <>
Signed-off-by: default avatarLinus Torvalds <>
parent 1062af92
......@@ -339,6 +339,9 @@ void __init setup_arch(char **cmdline_p)
/* Init percpu seeds for random tags after cpus are set up. */
* Make sure init_thread_info.ttbr0 always generates translation
......@@ -252,8 +252,6 @@ void __init kasan_init(void)
memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
/* At this point kasan is fully initialized. Enable error messages */
init_task.kasan_depth = 0;
pr_info("KernelAddressSanitizer initialized\n");
......@@ -46,7 +46,7 @@ void kasan_init_tags(void)
int cpu;
per_cpu(prng_state, cpu) = get_random_u32();
per_cpu(prng_state, cpu) = (u32)get_cycles();
Supports Markdown
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