linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mips: allow firmware to pass RNG seed to kernel
@ 2022-09-30 14:01 Jason A. Donenfeld
  2022-10-01 16:09 ` Thomas Bogendoerfer
  2022-10-03 22:07 ` Philippe Mathieu-Daudé
  0 siblings, 2 replies; 5+ messages in thread
From: Jason A. Donenfeld @ 2022-09-30 14:01 UTC (permalink / raw)
  To: tsbogend, linux-mips, linux-kernel; +Cc: 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.

Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
---
 arch/mips/kernel/setup.c | 21 +++++++++++++++++++++
 1 file changed, 21 insertions(+)

diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c
index 2ca156a5b231..39c79f67c7a3 100644
--- a/arch/mips/kernel/setup.c
+++ b/arch/mips/kernel/setup.c
@@ -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];
-- 
2.37.3


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] mips: allow firmware to pass RNG seed to kernel
  2022-09-30 14:01 [PATCH] mips: allow firmware to pass RNG seed to kernel Jason A. Donenfeld
@ 2022-10-01 16:09 ` Thomas Bogendoerfer
  2022-10-03 22:07 ` Philippe Mathieu-Daudé
  1 sibling, 0 replies; 5+ messages in thread
From: Thomas Bogendoerfer @ 2022-10-01 16:09 UTC (permalink / raw)
  To: Jason A. Donenfeld; +Cc: linux-mips, linux-kernel

On Fri, Sep 30, 2022 at 04:01:38PM +0200, Jason A. Donenfeld wrote:
> 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.
> 
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
>  arch/mips/kernel/setup.c | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)

applied to mips-next.

Thomas.

-- 
Crap can work. Given enough thrust pigs will fly, but it's not necessarily a
good idea.                                                [ RFC1925, 2.3 ]

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mips: allow firmware to pass RNG seed to kernel
  2022-09-30 14:01 [PATCH] mips: allow firmware to pass RNG seed to kernel Jason A. Donenfeld
  2022-10-01 16:09 ` Thomas Bogendoerfer
@ 2022-10-03 22:07 ` Philippe Mathieu-Daudé
  2022-10-03 22:30   ` Jason A. Donenfeld
  1 sibling, 1 reply; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2022-10-03 22:07 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: Thomas Bogendoerfer, open list:BROADCOM NVRAM DRIVER, open list

Hi Jason,

On Fri, Sep 30, 2022 at 4:05 PM Jason A. Donenfeld <Jason@zx2c4.com> wrote:
>
> 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.
>
> Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com>
> ---
>  arch/mips/kernel/setup.c | 21 +++++++++++++++++++++
>  1 file changed, 21 insertions(+)
>
> diff --git a/arch/mips/kernel/setup.c b/arch/mips/kernel/setup.c
> index 2ca156a5b231..39c79f67c7a3 100644
> --- a/arch/mips/kernel/setup.c
> +++ b/arch/mips/kernel/setup.c
> @@ -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;
> +

Assuming rngseed="x", ...

> +       len = min(sizeof(rng_seed), strlen(rng_seed_hex) / 2);

... len = 0 ...

> +       if (hex2bin(rng_seed, rng_seed_hex, len))
> +               return;

hex2bin(..., len=0) = 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?

> +       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
>

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mips: allow firmware to pass RNG seed to kernel
  2022-10-03 22:07 ` Philippe Mathieu-Daudé
@ 2022-10-03 22:30   ` Jason A. Donenfeld
  2022-10-06 11:28     ` Philippe Mathieu-Daudé
  0 siblings, 1 reply; 5+ messages in thread
From: Jason A. Donenfeld @ 2022-10-03 22:30 UTC (permalink / raw)
  To: Philippe Mathieu-Daudé
  Cc: Thomas Bogendoerfer, open list:BROADCOM NVRAM DRIVER, open list

Hi Philippe,

On Tue, Oct 4, 2022 at 12:07 AM Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
> > +       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

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] mips: allow firmware to pass RNG seed to kernel
  2022-10-03 22:30   ` Jason A. Donenfeld
@ 2022-10-06 11:28     ` Philippe Mathieu-Daudé
  0 siblings, 0 replies; 5+ messages in thread
From: Philippe Mathieu-Daudé @ 2022-10-06 11:28 UTC (permalink / raw)
  To: Jason A. Donenfeld
  Cc: Thomas Bogendoerfer, open list:BROADCOM NVRAM DRIVER, open list

On 4/10/22 00:30, Jason A. Donenfeld wrote:
> Hi Philippe,
> 
> On Tue, Oct 4, 2022 at 12:07 AM Philippe Mathieu-Daudé <f4bug@amsat.org> wrote:
>>> +       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().

OK, thanks for the clarification.

Phil.

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2022-10-06 11:28 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-09-30 14:01 [PATCH] mips: allow firmware to pass RNG seed to kernel Jason A. Donenfeld
2022-10-01 16:09 ` Thomas Bogendoerfer
2022-10-03 22:07 ` Philippe Mathieu-Daudé
2022-10-03 22:30   ` Jason A. Donenfeld
2022-10-06 11:28     ` Philippe Mathieu-Daudé

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).