Skip to contentSkip to conversation

mips: allow firmware to pass RNG seed to kernel

PATCH Applied Jason A. Donenfeld 4 participants 5 messages Sep 30, 2022 – Oct 6, 2022 MIPS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mips: allow firmware to pass RNG seed to kernel

Nearly all other firmware environments have some way of passing a RNG seed to initialize the RNG: DTB's rng-seed, EFI's RNG protocol, m68k's bootinfo block, x86's setup_data, and so forth. This adds something similar for MIPS, which will allow various firmware environments, bootloaders, and hypervisors to pass an RNG seed to initialize the kernel's RNG.

---
 arch/mips/kernel/setup.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/mips/kernel/setup.c +21 -0
index 2ca156a5b231..39c79f67c7a3 100644
@@ -42,6 +42,7 @@
 #include <asm/setup.h>
 #include <asm/smp-ops.h>
 #include <asm/prom.h>
+#include <asm/fw/fw.h>
 
 #ifdef CONFIG_MIPS_ELF_APPENDED_DTB
 char __section(".appended_dtb") __appended_dtb[0x100000];
@@ -756,6 +757,24 @@ static void __init prefill_possible_map(void)
 static inline void prefill_possible_map(void) {}
 #endif
 
+static void __init setup_rng_seed(void)
+{
+	char *rng_seed_hex = fw_getenv("rngseed");
+	u8 rng_seed[512];
+	size_t len;
+
+	if (!rng_seed_hex)
+		return;
+
+	len = min(sizeof(rng_seed), strlen(rng_seed_hex) / 2);
+	if (hex2bin(rng_seed, rng_seed_hex, len))
+		return;
+
+	add_bootloader_randomness(rng_seed, len);
+	memzero_explicit(rng_seed, len);
+	memzero_explicit(rng_seed_hex, len * 2);
+}
+
 void __init setup_arch(char **cmdline_p)
 {
 	cpu_probe();
@@ -786,6 +805,8 @@ void __init setup_arch(char **cmdline_p)
 	paging_init();
 
 	memblock_dump_all();
+
+	setup_rng_seed();
 }
 
 unsigned long kernelsp[NR_CPUS];
Signature · 1 line
2.37.3
maintainer Applied
#1 lore raw
Quoted 11 lines · Jason A. Donenfeld

Nearly all other firmware environments have some way of passing a RNG seed to initialize the RNG: DTB's rng-seed, EFI's RNG protocol, m68k's bootinfo block, x86's setup_data, and so forth. This adds something similar for MIPS, which will allow various firmware environments, bootloaders, and hypervisors to pass an RNG seed to initialize the kernel's RNG.

---
 arch/mips/kernel/setup.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

applied to mips-next.

Thomas.

Signature · 2 lines
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]
#2 lore raw
In reply to Jason A. Donenfeld

Hi Jason,

Quoted 28 lines · Jason A. Donenfeld

Nearly all other firmware environments have some way of passing a RNG seed to initialize the RNG: DTB's rng-seed, EFI's RNG protocol, m68k's bootinfo block, x86's setup_data, and so forth. This adds something similar for MIPS, which will allow various firmware environments, bootloaders, and hypervisors to pass an RNG seed to initialize the kernel's RNG.

---
 arch/mips/kernel/setup.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)
arch/mips/kernel/setup.c +1 -0
index 2ca156a5b231..39c79f67c7a3 100644
@@ -42,6 +42,7 @@
 #include <asm/setup.h>
 #include <asm/smp-ops.h>
 #include <asm/prom.h>
+#include <asm/fw/fw.h>
 
 #ifdef CONFIG_MIPS_ELF_APPENDED_DTB
 char __section(".appended_dtb") __appended_dtb[0x100000];
@@ -756,6 +757,24 @@ static void __init prefill_possible_map(void)
 static inline void prefill_possible_map(void) {}
 #endif

arch/mips/kernel/setup.c static void __init prefill_possible_map(void) · line 768

Quoted 10 lines
arch/mips/kernel/setup.c +9 -0
 
+static void __init setup_rng_seed(void)
+{
+       char *rng_seed_hex = fw_getenv("rngseed");
+       u8 rng_seed[512];
+       size_t len;
+
+       if (!rng_seed_hex)
+               return;
+

Assuming rngseed="x", ...

arch/mips/kernel/setup.c static void __init prefill_possible_map(void) · line 769

Quoted 1 line
arch/mips/kernel/setup.c +1 -0
+       len = min(sizeof(rng_seed), strlen(rng_seed_hex) / 2);

... len = 0 ...

arch/mips/kernel/setup.c static void __init prefill_possible_map(void) · line 771

Quoted 2 lines
arch/mips/kernel/setup.c +2 -0
+       if (hex2bin(rng_seed, rng_seed_hex, len))
+               return;

hex2bin(..., len=0) = 0

arch/mips/kernel/setup.c static void __init prefill_possible_map(void) · line 773

Quoted 2 lines
arch/mips/kernel/setup.c +2 -0
+
+       add_bootloader_randomness(rng_seed, len);

So we call char/random code with len=0. Is it safe? Maybe simply safer to check len before calling hex2bin?

Quoted 19 lines
arch/mips/kernel/setup.c +6 -1
+       memzero_explicit(rng_seed, len);
+       memzero_explicit(rng_seed_hex, len * 2);
+}
+
 void __init setup_arch(char **cmdline_p)
 {
        cpu_probe();
@@ -786,6 +805,8 @@ void __init setup_arch(char **cmdline_p)
        paging_init();
 
        memblock_dump_all();
+
+       setup_rng_seed();
 }
 
 unsigned long kernelsp[NR_CPUS];
--

2.37.3

#3 lore raw

Hi Philippe,

Quoted 4 lines · Philippe Mathieu-Daudé
Quoted 1 line

+ add_bootloader_randomness(rng_seed, len);

So we call char/random code with len=0. Is it safe? Maybe simply safer to check len before calling hex2bin?

add_bootloader_randomness() is safe for all input sizes, and is written to be callable with len=0 and have no effect. So this function should be good as-is; there's no need to special case an unlikely instance that's already handled by add_bootloader_randomness().

Jason

#4 lore raw
Quoted 5 lines · Jason A. Donenfeld

Hi Philippe,

Quoted 2 lines · Philippe Mathieu-Daudé
Quoted 1 line

+ add_bootloader_randomness(rng_seed, len);

Quoted 7 lines
Quoted 2 lines

So we call char/random code with len=0. Is it safe? Maybe simply safer to check len before calling hex2bin?

add_bootloader_randomness() is safe for all input sizes, and is written to be callable with len=0 and have no effect. So this function should be good as-is; there's no need to special case an unlikely instance that's already handled by add_bootloader_randomness().

OK, thanks for the clarification.

Phil.