LKML Archive on
help / color / mirror / Atom feed
* [PATCH] kasan: fix random seed generation for tag-based mode
@ 2019-02-20 15:55 Andrey Konovalov
  0 siblings, 0 replies; only message in thread
From: Andrey Konovalov @ 2019-02-20 15:55 UTC (permalink / raw)
  To: Catalin Marinas, Will Deacon, Andrey Ryabinin,
	Alexander Potapenko, Dmitry Vyukov, Ard Biesheuvel, Michal Hocko,
	Mike Rapoport, Andrew Morton, Andrey Konovalov, Miles Chen,
	James Morse, Andrew Murray, linux-arm-kernel, linux-kernel,
	kasan-dev, linux-mm
  Cc: Qian Cai, Vincenzo Frascino, Kostya Serebryany, Evgeniy Stepanov

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: Andrey Konovalov <>
 arch/arm64/kernel/setup.c  | 3 +++
 arch/arm64/mm/kasan_init.c | 2 --
 mm/kasan/tags.c            | 2 +-
 3 files changed, 4 insertions(+), 3 deletions(-)

diff --git a/arch/arm64/kernel/setup.c b/arch/arm64/kernel/setup.c
index d09ec76f08cf..009849328289 100644
--- a/arch/arm64/kernel/setup.c
+++ b/arch/arm64/kernel/setup.c
@@ -339,6 +339,9 @@ void __init setup_arch(char **cmdline_p)
+	/* Init percpu seeds for random tags after cpus are set up. */
+	kasan_init_tags();
 	 * Make sure init_thread_info.ttbr0 always generates translation
diff --git a/arch/arm64/mm/kasan_init.c b/arch/arm64/mm/kasan_init.c
index 4b55b15707a3..f37a86d2a69d 100644
--- a/arch/arm64/mm/kasan_init.c
+++ b/arch/arm64/mm/kasan_init.c
@@ -252,8 +252,6 @@ void __init kasan_init(void)
 	memset(kasan_early_shadow_page, KASAN_SHADOW_INIT, PAGE_SIZE);
-	kasan_init_tags();
 	/* At this point kasan is fully initialized. Enable error messages */
 	init_task.kasan_depth = 0;
 	pr_info("KernelAddressSanitizer initialized\n");
diff --git a/mm/kasan/tags.c b/mm/kasan/tags.c
index 0777649e07c4..63fca3172659 100644
--- a/mm/kasan/tags.c
+++ b/mm/kasan/tags.c
@@ -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();

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2019-02-20 15:55 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2019-02-20 15:55 [PATCH] kasan: fix random seed generation for tag-based mode Andrey Konovalov

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).