c96d1d0
From 9b25436662d5fb4c66eb527ead53cab15f596ee0 Mon Sep 17 00:00:00 2001
c96d1d0
From: Kees Cook <keescook@chromium.org>
c96d1d0
Date: Mon, 27 Aug 2018 14:51:54 -0700
c96d1d0
Subject: [PATCH] random: make CPU trust a boot parameter
c96d1d0
c96d1d0
Instead of forcing a distro or other system builder to choose
c96d1d0
at build time whether the CPU is trusted for CRNG seeding via
c96d1d0
CONFIG_RANDOM_TRUST_CPU, provide a boot-time parameter for end users to
c96d1d0
control the choice. The CONFIG will set the default state instead.
c96d1d0
c96d1d0
Signed-off-by: Kees Cook <keescook@chromium.org>
c96d1d0
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
c96d1d0
---
c96d1d0
 Documentation/admin-guide/kernel-parameters.txt |  6 ++++++
c96d1d0
 drivers/char/Kconfig                            |  4 ++--
c96d1d0
 drivers/char/random.c                           | 11 ++++++++---
c96d1d0
 3 files changed, 16 insertions(+), 5 deletions(-)
c96d1d0
c96d1d0
diff --git a/Documentation/admin-guide/kernel-parameters.txt b/Documentation/admin-guide/kernel-parameters.txt
c96d1d0
index 0c8f7889efa1..227c5c6fa4c1 100644
c96d1d0
--- a/Documentation/admin-guide/kernel-parameters.txt
c96d1d0
+++ b/Documentation/admin-guide/kernel-parameters.txt
c96d1d0
@@ -3390,6 +3390,12 @@
c96d1d0
 	ramdisk_size=	[RAM] Sizes of RAM disks in kilobytes
c96d1d0
 			See Documentation/blockdev/ramdisk.txt.
c96d1d0
 
c96d1d0
+	random.trust_cpu={on,off}
c96d1d0
+			[KNL] Enable or disable trusting the use of the
c96d1d0
+			CPU's random number generator (if available) to
c96d1d0
+			fully seed the kernel's CRNG. Default is controlled
c96d1d0
+			by CONFIG_RANDOM_TRUST_CPU.
c96d1d0
+
c96d1d0
 	ras=option[,option,...]	[KNL] RAS-specific options
c96d1d0
 
c96d1d0
 		cec_disable	[X86]
c96d1d0
diff --git a/drivers/char/Kconfig b/drivers/char/Kconfig
c96d1d0
index ce277ee0a28a..40728491f37b 100644
c96d1d0
--- a/drivers/char/Kconfig
c96d1d0
+++ b/drivers/char/Kconfig
c96d1d0
@@ -566,5 +566,5 @@ config RANDOM_TRUST_CPU
c96d1d0
 	that CPU manufacturer (perhaps with the insistence or mandate
c96d1d0
 	of a Nation State's intelligence or law enforcement agencies)
c96d1d0
 	has not installed a hidden back door to compromise the CPU's
c96d1d0
-	random number generation facilities.
c96d1d0
-
c96d1d0
+	random number generation facilities. This can also be configured
c96d1d0
+	at boot with "random.trust_cpu=on/off".
c96d1d0
diff --git a/drivers/char/random.c b/drivers/char/random.c
c96d1d0
index bf5f99fc36f1..c75b6cdf0053 100644
c96d1d0
--- a/drivers/char/random.c
c96d1d0
+++ b/drivers/char/random.c
c96d1d0
@@ -779,6 +779,13 @@ static struct crng_state **crng_node_pool __read_mostly;
c96d1d0
 
c96d1d0
 static void invalidate_batched_entropy(void);
c96d1d0
 
c96d1d0
+static bool trust_cpu __ro_after_init = IS_ENABLED(CONFIG_RANDOM_TRUST_CPU);
c96d1d0
+static int __init parse_trust_cpu(char *arg)
c96d1d0
+{
c96d1d0
+	return kstrtobool(arg, &trust_cpu);
c96d1d0
+}
c96d1d0
+early_param("random.trust_cpu", parse_trust_cpu);
c96d1d0
+
c96d1d0
 static void crng_initialize(struct crng_state *crng)
c96d1d0
 {
c96d1d0
 	int		i;
c96d1d0
@@ -799,12 +806,10 @@ static void crng_initialize(struct crng_state *crng)
c96d1d0
 		}
c96d1d0
 		crng->state[i] ^= rv;
c96d1d0
 	}
c96d1d0
-#ifdef CONFIG_RANDOM_TRUST_CPU
c96d1d0
-	if (arch_init) {
c96d1d0
+	if (trust_cpu && arch_init) {
c96d1d0
 		crng_init = 2;
c96d1d0
 		pr_notice("random: crng done (trusting CPU's manufacturer)\n");
c96d1d0
 	}
c96d1d0
-#endif
c96d1d0
 	crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1;
c96d1d0
 }
c96d1d0
 
c96d1d0
-- 
c96d1d0
2.17.1
c96d1d0