[PATCH v2] x86/setup: Allow passing RNG seeds via e820 setup table

From: Jason A. Donenfeld
Date: Thu Jun 30 2022 - 08:00:06 EST


Currently the only way x86 can get an early boot RNG seed is via EFI,
which is generally always used now for physical machines, but is very
rarely used in VMs, especially VMs that are optimized for starting
"instantaneously", such as Firecracker's MicroVM. Here, we really want
the ability for the firmware to pass a random seed, similar to what OF
platforms do with the "rng-seed" property. It also would be nice for
bootloaders to be able to append seeds to the kernel before launching.

This patch accomplishes that by adding SETUP_RNG_SEED, similar to the
other 7 SETUP_* entries that are parsed from the e820 setup table. I've
verified that this works well with QEMU.

Signed-off-by: Jason A. Donenfeld <Jason@xxxxxxxxx>
---
Changes v1->v2:
- Fix small typo of data_len -> data->len.

arch/x86/include/uapi/asm/bootparam.h | 1 +
arch/x86/kernel/setup.c | 8 +++++++-
2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/arch/x86/include/uapi/asm/bootparam.h b/arch/x86/include/uapi/asm/bootparam.h
index bea5cdcdf532..a60676b8d1d4 100644
--- a/arch/x86/include/uapi/asm/bootparam.h
+++ b/arch/x86/include/uapi/asm/bootparam.h
@@ -11,6 +11,7 @@
#define SETUP_APPLE_PROPERTIES 5
#define SETUP_JAILHOUSE 6
#define SETUP_CC_BLOB 7
+#define SETUP_RNG_SEED 8

#define SETUP_INDIRECT (1<<31)

diff --git a/arch/x86/kernel/setup.c b/arch/x86/kernel/setup.c
index bd6c6fd373ae..ce8bb744e576 100644
--- a/arch/x86/kernel/setup.c
+++ b/arch/x86/kernel/setup.c
@@ -23,6 +23,7 @@
#include <linux/usb/xhci-dbgp.h>
#include <linux/static_call.h>
#include <linux/swiotlb.h>
+#include <linux/random.h>

#include <uapi/linux/mount.h>

@@ -343,7 +344,8 @@ static void __init parse_setup_data(void)
data_len = data->len + sizeof(struct setup_data);
data_type = data->type;
pa_next = data->next;
- early_memunmap(data, sizeof(*data));
+ if (data_type != SETUP_RNG_SEED)
+ early_memunmap(data, sizeof(*data));

switch (data_type) {
case SETUP_E820_EXT:
@@ -355,6 +357,10 @@ static void __init parse_setup_data(void)
case SETUP_EFI:
parse_efi_setup(pa_data, data_len);
break;
+ case SETUP_RNG_SEED:
+ add_bootloader_randomness(data + 1, data->len);
+ early_memunmap(data, sizeof(*data));
+ break;
default:
break;
}
--
2.35.1