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

From: Jason A. Donenfeld
Date: Thu Jun 30 2022 - 08:10:17 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 v2->v3:
- Actually memmap the right area with the random bytes in it. This
worked before because of page sizes, but the code wasn't right. Now
it's right.

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

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..33e9d23296b6 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>

@@ -355,6 +356,13 @@ static void __init parse_setup_data(void)
case SETUP_EFI:
parse_efi_setup(pa_data, data_len);
break;
+ case SETUP_RNG_SEED:
+ pa_data += sizeof(*data);
+ data_len -= sizeof(*data);
+ data = early_memremap(pa_data, data_len);
+ add_bootloader_randomness(data, data_len);
+ early_memunmap(data, data_len);
+ break;
default:
break;
}
--
2.35.1