Skip to contentSkip to conversation

random/arm64: enable RANDOM_TRUST_CPU for arm64

PATCH In review Mark Rutland 2 participants 7 messages Feb 10, 2020 – Feb 28, 2020

Subsystems (3)
lore
Share & tools
lore

Mark Rutland

author reviewer
#0 lore raw

cover random/arm64: enable RANDOM_TRUST_CPU for arm64

On arm64 systems some CPUs may have RNG instructions while others do not, and consequently we cannot generally enable the use of RNG instructions until all CPUs have been booted (as otherwise we'd have problems with preemption, etc). This prevents us from seeding the primary CRNG using the RNG, as this occurs before secondary CPUs are onlined.

These patches rework the core CRNG intialization code so that the arch code can (optionally) distinguish boot-time usage from runtime usage of the arch_get_random_*() functions. This allows arm64 to use the boot CPU's RNG to seed the primary CRNG, regardless of whether secondary CPUs support the RNG instructions. Other architectures should see no functional change as a result of this patches.

Thanks,
Mark.

Mark Rutland (3):
random: split primary/secondary crng init paths
random: add arch_get_random_*long_early()
arm64: add credited/trusted RNG support

Richard Henderson (1):
random: Make RANDOM_TRUST_CPU depend on ARCH_RANDOM

 arch/arm64/include/asm/archrandom.h | 14 ++++++++++
 drivers/char/Kconfig                |  2 +-
 drivers/char/random.c               | 52 ++++++++++++++++++++++++++++---------
 include/linux/random.h              | 22 ++++++++++++++++
 4 files changed, 77 insertions(+), 13 deletions(-)
Signature · 7 lines
2.11.0


_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
author reviewer
#1 lore raw

PATCH 1/4 random: split primary/secondary crng init paths

Currently crng_initialize() is used for both the primary CRNG and secondary CRNGs. While we wish to share common logic, we need to do a number of additional things for the primary CRNG, and this would be easier to deal with were these handled in separate functions.

This patch splits crng_initialize() into crng_initialize_primary() and crng_initialize_secondary(), with common logic factored out into a crng_init_try_arch() helper.

There should be no functional change as a result of this patch.

Cc Mark Brown <[email protected]>
Cc Theodore Ts'o <[email protected]>
---
 drivers/char/random.c | 34 ++++++++++++++++++++++------------
 1 file changed, 22 insertions(+), 12 deletions(-)
drivers/char/random.c +22 -12
index c7f9584de2c8..62d32e62f2da 100644
@@ -781,27 +781,37 @@ static int __init parse_trust_cpu(char *arg)
 }
 early_param("random.trust_cpu", parse_trust_cpu);
 
-static void crng_initialize(struct crng_state *crng)
+static bool crng_init_try_arch(struct crng_state *crng)
 {
 	int		i;
-	int		arch_init = 1;
+	bool		arch_init = true;
 	unsigned long	rv;
 
-	memcpy(&crng->state[0], "expand 32-byte k", 16);
-	if (crng == &primary_crng)
-		_extract_entropy(&input_pool, &crng->state[4],
-				 sizeof(__u32) * 12, 0);
-	else
-		_get_random_bytes(&crng->state[4], sizeof(__u32) * 12);
 	for (i = 4; i < 16; i++) {
 		if (!arch_get_random_seed_long(&rv) &&
 		    !arch_get_random_long(&rv)) {
 			rv = random_get_entropy();
-			arch_init = 0;
+			arch_init = false;
 		}
 		crng->state[i] ^= rv;
 	}
-	if (trust_cpu && arch_init && crng == &primary_crng) {
+
+	return arch_init;
+}
+
+static void crng_initialize_secondary(struct crng_state *crng)
+{
+	memcpy(&crng->state[0], "expand 32-byte k", 16);
+	_get_random_bytes(&crng->state[4], sizeof(__u32) * 12);
+	crng_init_try_arch(crng);
+	crng->init_time = jiffies - CRNG_RESEED_INTERVAL - 1;
+}
+
+static void __init crng_initialize_primary(struct crng_state *crng)
+{
+	memcpy(&crng->state[0], "expand 32-byte k", 16);
+	_extract_entropy(&input_pool, &crng->state[4], sizeof(__u32) * 12, 0);
+	if (crng_init_try_arch(crng) && trust_cpu) {
 		invalidate_batched_entropy();
 		numa_crng_init();
 		crng_init = 2;
@@ -822,7 +832,7 @@ static void do_numa_crng_init(struct work_struct *work)
 		crng = kmalloc_node(sizeof(struct crng_state),
 				    GFP_KERNEL | __GFP_NOFAIL, i);
 		spin_lock_init(&crng->lock);
-		crng_initialize(crng);
+		crng_initialize_secondary(crng);
 		pool[i] = crng;
 	}
 	mb();
@@ -1771,7 +1781,7 @@ static void __init init_std_data(struct entropy_store *r)
 int __init rand_initialize(void)
 {
 	init_std_data(&input_pool);
-	crng_initialize(&primary_crng);
+	crng_initialize_primary(&primary_crng);
 	crng_global_init_time = jiffies;
 	if (ratelimit_disable) {
 		urandom_warning.interval = 0;
Signature · 1 line
2.11.0
author reviewer
#2 lore raw

PATCH 2/4 random: add arch_get_random_*long_early()

In reply to Mark Rutland

Some architectures (e.g. arm64) can have heterogeneous CPUs, and the boot CPU may be able to provide entropy while secondary CPUs cannot. On such systems, arch_get_random_long() and arch_get_random_seed_long() will fail unless support for RNG instructions has been detected on all CPUs. This prevents the boot CPU from being able to provide (potentially) trusted entropy when seeding the primary CRNG.

To make it possible to seed the primary CRNG from the boot CPU without adversely affecting the runtime versions of arch_get_random_long() and arch_get_random_seed_long(), this patch adds new early versions of the functions used when initializing the primary CRNG.

Default implementations are provided atop of the existing arch_get_random_long() and arch_get_random_seed_long() so that only architectures with such constraints need to provide the new helpers.

There should be no functional change as a result of this patch.

Cc Mark Brown <[email protected]>
Cc Theodore Ts'o <[email protected]>
---
 drivers/char/random.c  | 20 +++++++++++++++++++-
 include/linux/random.h | 22 ++++++++++++++++++++++
 2 files changed, 41 insertions(+), 1 deletion(-)
drivers/char/random.c +19 -1
index 62d32e62f2da..02a85b87b993 100644
@@ -799,6 +799,24 @@ static bool crng_init_try_arch(struct crng_state *crng)
 	return arch_init;
 }
 
+static bool __init crng_init_try_arch_early(struct crng_state *crng)
+{
+	int		i;
+	bool		arch_init = true;
+	unsigned long	rv;
+
+	for (i = 4; i < 16; i++) {
+		if (!arch_get_random_seed_long_early(&rv) &&
+		    !arch_get_random_long_early(&rv)) {
+			rv = random_get_entropy();
+			arch_init = false;
+		}
+		crng->state[i] ^= rv;
+	}
+
+	return arch_init;
+}
+
 static void crng_initialize_secondary(struct crng_state *crng)
 {
 	memcpy(&crng->state[0], "expand 32-byte k", 16);
@@ -811,7 +829,7 @@ static void __init crng_initialize_primary(struct crng_state *crng)
 {
 	memcpy(&crng->state[0], "expand 32-byte k", 16);
 	_extract_entropy(&input_pool, &crng->state[4], sizeof(__u32) * 12, 0);
-	if (crng_init_try_arch(crng) && trust_cpu) {
+	if (crng_init_try_arch_early(crng) && trust_cpu) {
 		invalidate_batched_entropy();
 		numa_crng_init();
 		crng_init = 2;
include/linux/random.h +22 -0
index d319f9a1e429..45e1f8fa742b 100644
@@ -7,6 +7,8 @@
 #ifndef _LINUX_RANDOM_H
 #define _LINUX_RANDOM_H
 
+#include <linux/bug.h>
+#include <linux/kernel.h>
 #include <linux/list.h>
 #include <linux/once.h>
 
@@ -185,6 +187,26 @@ static inline bool __must_check arch_get_random_seed_int(unsigned int *v)
 }
 #endif
 
+/*
+ * Called from the boot CPU during startup; not valid to call once
+ * secondary CPUs are up and preemption is possible.
+ */
+#ifndef arch_get_random_seed_long_early
+static inline bool __init arch_get_random_seed_long_early(unsigned long *v)
+{
+	WARN_ON(system_state != SYSTEM_BOOTING);
+	return arch_get_random_seed_long(v);
+}
+#endif
+
+#ifndef arch_get_random_long_early
+static inline bool __init arch_get_random_long_early(unsigned long *v)
+{
+	WARN_ON(system_state != SYSTEM_BOOTING);
+	return arch_get_random_long(v);
+}
+#endif
+
 /* Pseudo random number generator from numerical recipes. */
 static inline u32 next_pseudo_random32(u32 seed)
 {
Signature · 1 line
2.11.0
author reviewer
#3 lore raw

PATCH 3/4 arm64: add credited/trusted RNG support

In reply to Mark Rutland

Currently arm64 doesn't initialize the primary CRNG in a (potentially) trusted manner as we only detect the presence of the RNG once secondary CPUs are up.

Now that the core RNG code distinguishes the early initialization of the primary CRNG, we can implement arch_get_random_seed_long_early() to support this.

This patch does so.

Cc Catalin Marinas <[email protected]>
Cc Mark Brown <[email protected]>
Cc Theodore Ts'o <[email protected]>
Cc Will Deacon <[email protected]>
---
 arch/arm64/include/asm/archrandom.h | 14 ++++++++++++++
 1 file changed, 14 insertions(+)
arch/arm64/include/asm/archrandom.h +14 -0
index 3fe02da70004..fc1594a0710e 100644
@@ -4,6 +4,8 @@
 
 #ifdef CONFIG_ARCH_RANDOM
 
+#include <linux/bug.h>
+#include <linux/kernel.h>
 #include <linux/random.h>
 #include <asm/cpufeature.h>
 
@@ -66,6 +68,18 @@ static inline bool __init __early_cpu_has_rndr(void)
 	return (ftr >> ID_AA64ISAR0_RNDR_SHIFT) & 0xf;
 }
 
+static inline bool __init __must_check
+arch_get_random_seed_long_early(unsigned long *v)
+{
+	WARN_ON(system_state != SYSTEM_BOOTING);
+
+	if (!__early_cpu_has_rndr())
+		return false;
+
+	return __arm64_rndr(v);
+}
+#define arch_get_random_seed_long_early arch_get_random_seed_long_early
+
 #else
 
 static inline bool __arm64_rndr(unsigned long *v) { return false; }
Signature · 1 line
2.11.0
author reviewer
#4 lore raw

PATCH 4/4 random: Make RANDOM_TRUST_CPU depend on ARCH_RANDOM

In reply to Mark Rutland

From: Richard Henderson <[email protected]>

Listing the set of host architectures does not scale. Depend instead on the existence of the architecture rng.

This will allow RANDOM_TRUST_CPU to be selected on arm64. Today ARCH_RANDOM is only selected by x86, s390, and powerpc, so this does not adversely affect other architectures.

---
 drivers/char/Kconfig | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/char/Kconfig +1 -1
index 26956c006987..84207d5a9bb0 100644
@@ -539,7 +539,7 @@ endmenu
 
 config RANDOM_TRUST_CPU
 	bool "Trust the CPU manufacturer to initialize Linux's CRNG"
-	depends on X86 || S390 || PPC
+	depends on ARCH_RANDOM
 	default n
 	help
 	Assume that CPU manufacturer (e.g., Intel or AMD for RDSEED or
Signature · 1 line
2.11.0
author reviewer
#5 lore raw
In reply to Mark Rutland

Ted, sorry to ping, but do you have any thoughts on this series?

I'm happy to rework this, or drop it if you think it's completely wrong, but if you're not too concerned it would be nice to be able to queue this soon.

Thanks,
Mark.

Quoted 34 lines · Mark Rutland

On arm64 systems some CPUs may have RNG instructions while others do not, and consequently we cannot generally enable the use of RNG instructions until all CPUs have been booted (as otherwise we'd have problems with preemption, etc). This prevents us from seeding the primary CRNG using the RNG, as this occurs before secondary CPUs are onlined.

These patches rework the core CRNG intialization code so that the arch code can (optionally) distinguish boot-time usage from runtime usage of the arch_get_random_*() functions. This allows arm64 to use the boot CPU's RNG to seed the primary CRNG, regardless of whether secondary CPUs support the RNG instructions. Other architectures should see no functional change as a result of this patches.

Thanks,
Mark.

Mark Rutland (3):
random: split primary/secondary crng init paths
random: add arch_get_random_*long_early()
arm64: add credited/trusted RNG support

Richard Henderson (1):
random: Make RANDOM_TRUST_CPU depend on ARCH_RANDOM

 arch/arm64/include/asm/archrandom.h | 14 ++++++++++
 drivers/char/Kconfig                |  2 +-
 drivers/char/random.c               | 52 ++++++++++++++++++++++++++++---------
 include/linux/random.h              | 22 ++++++++++++++++
 4 files changed, 77 insertions(+), 13 deletions(-)
Signature · 1 line
2.11.0
maintainer
#6 lore raw
Quoted 5 lines · Mark Rutland

Ted, sorry to ping, but do you have any thoughts on this series?

I'm happy to rework this, or drop it if you think it's completely wrong, but if you're not too concerned it would be nice to be able to queue this soon.

Thanks, I've applied it to the random git tree.

- Ted

_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel