Skip to content

Commit 3f41b60

Browse files
xairytorvalds
authored andcommitted
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. Link: http://lkml.kernel.org/r/1f815cc914b61f3516ed4cc9bfd9eeca9bd5d9de.1550677973.git.andreyknvl@google.com Signed-off-by: Andrey Konovalov <andreyknvl@google.com> Cc: Catalin Marinas <catalin.marinas@arm.com> Cc: Will Deacon <will.deacon@arm.com> Cc: Andrey Ryabinin <aryabinin@virtuozzo.com> Cc: Alexander Potapenko <glider@google.com> Cc: Dmitry Vyukov <dvyukov@google.com> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent 1062af9 commit 3f41b60

File tree

3 files changed

+4
-3
lines changed

3 files changed

+4
-3
lines changed

arch/arm64/kernel/setup.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,9 @@ void __init setup_arch(char **cmdline_p)
339339
smp_init_cpus();
340340
smp_build_mpidr_hash();
341341

342+
/* Init percpu seeds for random tags after cpus are set up. */
343+
kasan_init_tags();
344+
342345
#ifdef CONFIG_ARM64_SW_TTBR0_PAN
343346
/*
344347
* Make sure init_thread_info.ttbr0 always generates translation

arch/arm64/mm/kasan_init.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -252,8 +252,6 @@ void __init kasan_init(void)
252252
memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
253253
cpu_replace_ttbr1(lm_alias(swapper_pg_dir));
254254

255-
kasan_init_tags();
256-
257255
/* At this point kasan is fully initialized. Enable error messages */
258256
init_task.kasan_depth = 0;
259257
pr_info("KernelAddressSanitizer initialized\n");

mm/kasan/tags.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ void kasan_init_tags(void)
4646
int cpu;
4747

4848
for_each_possible_cpu(cpu)
49-
per_cpu(prng_state, cpu) = get_random_u32();
49+
per_cpu(prng_state, cpu) = (u32)get_cycles();
5050
}
5151

5252
/*

0 commit comments

Comments
 (0)