Skip to content

Commit 196dff2

Browse files
committed
efi: random: combine bootloader provided RNG seed with RNG protocol output
Instead of blindly creating the EFI random seed configuration table if the RNG protocol is implemented and works, check whether such a EFI configuration table was provided by an earlier boot stage and if so, concatenate the existing and the new seeds, leaving it up to the core code to mix it in and credit it the way it sees fit. This can be used for, e.g., systemd-boot, to pass an additional seed to Linux in a way that can be consumed by the kernel very early. In that case, the following definitions should be used to pass the seed to the EFI stub: struct linux_efi_random_seed { u32 size; // of the 'seed' array in bytes u8 seed[]; }; The memory for the struct must be allocated as EFI_ACPI_RECLAIM_MEMORY pool memory, and the address of the struct in memory should be installed as a EFI configuration table using the following GUID: LINUX_EFI_RANDOM_SEED_TABLE_GUID 1ce1e5bc-7ceb-42f2-81e5-8aadf180f57b Note that doing so is safe even on kernels that were built without this patch applied, but the seed will simply be overwritten with a seed derived from the EFI RNG protocol, if available. The recommended seed size is 32 bytes, and seeds larger than 512 bytes are considered corrupted and ignored entirely. In order to preserve forward secrecy, seeds from previous bootloaders are memzero'd out, and in order to preserve memory, those older seeds are also freed from memory. Freeing from memory without first memzeroing is not safe to do, as it's possible that nothing else will ever overwrite those pages used by EFI. Reviewed-by: Jason A. Donenfeld <[email protected]> [ardb: incorporate Jason's followup changes to extend the maximum seed size on the consumer end, memzero() it and drop a needless printk] Signed-off-by: Ard Biesheuvel <[email protected]>
1 parent 2fb6999 commit 196dff2

4 files changed

Lines changed: 40 additions & 10 deletions

File tree

drivers/firmware/efi/efi.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,7 +613,7 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
613613

614614
seed = early_memremap(efi_rng_seed, sizeof(*seed));
615615
if (seed != NULL) {
616-
size = min(seed->size, EFI_RANDOM_SEED_SIZE);
616+
size = min_t(u32, seed->size, SZ_1K); // sanity check
617617
early_memunmap(seed, sizeof(*seed));
618618
} else {
619619
pr_err("Could not map UEFI random seed!\n");
@@ -622,8 +622,8 @@ int __init efi_config_parse_tables(const efi_config_table_t *config_tables,
622622
seed = early_memremap(efi_rng_seed,
623623
sizeof(*seed) + size);
624624
if (seed != NULL) {
625-
pr_notice("seeding entropy pool\n");
626625
add_bootloader_randomness(seed->bits, size);
626+
memzero_explicit(seed->bits, size);
627627
early_memunmap(seed, sizeof(*seed) + size);
628628
} else {
629629
pr_err("Could not map UEFI random seed!\n");

drivers/firmware/efi/libstub/efistub.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -933,6 +933,8 @@ efi_status_t efi_random_alloc(unsigned long size, unsigned long align,
933933
unsigned long *addr, unsigned long random_seed,
934934
int memory_type);
935935

936+
efi_status_t efi_random_get_seed(void);
937+
936938
efi_status_t check_platform_features(void);
937939

938940
void *get_efi_config_table(efi_guid_t guid);

drivers/firmware/efi/libstub/random.c

Lines changed: 36 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,27 +67,43 @@ efi_status_t efi_random_get_seed(void)
6767
efi_guid_t rng_proto = EFI_RNG_PROTOCOL_GUID;
6868
efi_guid_t rng_algo_raw = EFI_RNG_ALGORITHM_RAW;
6969
efi_guid_t rng_table_guid = LINUX_EFI_RANDOM_SEED_TABLE_GUID;
70+
struct linux_efi_random_seed *prev_seed, *seed = NULL;
71+
int prev_seed_size = 0, seed_size = EFI_RANDOM_SEED_SIZE;
7072
efi_rng_protocol_t *rng = NULL;
71-
struct linux_efi_random_seed *seed = NULL;
7273
efi_status_t status;
7374

7475
status = efi_bs_call(locate_protocol, &rng_proto, NULL, (void **)&rng);
7576
if (status != EFI_SUCCESS)
7677
return status;
7778

79+
/*
80+
* Check whether a seed was provided by a prior boot stage. In that
81+
* case, instead of overwriting it, let's create a new buffer that can
82+
* hold both, and concatenate the existing and the new seeds.
83+
* Note that we should read the seed size with caution, in case the
84+
* table got corrupted in memory somehow.
85+
*/
86+
prev_seed = get_efi_config_table(LINUX_EFI_RANDOM_SEED_TABLE_GUID);
87+
if (prev_seed && prev_seed->size <= 512U) {
88+
prev_seed_size = prev_seed->size;
89+
seed_size += prev_seed_size;
90+
}
91+
7892
/*
7993
* Use EFI_ACPI_RECLAIM_MEMORY here so that it is guaranteed that the
8094
* allocation will survive a kexec reboot (although we refresh the seed
8195
* beforehand)
8296
*/
8397
status = efi_bs_call(allocate_pool, EFI_ACPI_RECLAIM_MEMORY,
84-
sizeof(*seed) + EFI_RANDOM_SEED_SIZE,
98+
struct_size(seed, bits, seed_size),
8599
(void **)&seed);
86-
if (status != EFI_SUCCESS)
87-
return status;
100+
if (status != EFI_SUCCESS) {
101+
efi_warn("Failed to allocate memory for RNG seed.\n");
102+
goto err_warn;
103+
}
88104

89105
status = efi_call_proto(rng, get_rng, &rng_algo_raw,
90-
EFI_RANDOM_SEED_SIZE, seed->bits);
106+
EFI_RANDOM_SEED_SIZE, seed->bits);
91107

92108
if (status == EFI_UNSUPPORTED)
93109
/*
@@ -100,14 +116,28 @@ efi_status_t efi_random_get_seed(void)
100116
if (status != EFI_SUCCESS)
101117
goto err_freepool;
102118

103-
seed->size = EFI_RANDOM_SEED_SIZE;
119+
seed->size = seed_size;
120+
if (prev_seed_size)
121+
memcpy(seed->bits + EFI_RANDOM_SEED_SIZE, prev_seed->bits,
122+
prev_seed_size);
123+
104124
status = efi_bs_call(install_configuration_table, &rng_table_guid, seed);
105125
if (status != EFI_SUCCESS)
106126
goto err_freepool;
107127

128+
if (prev_seed_size) {
129+
/* wipe and free the old seed if we managed to install the new one */
130+
memzero_explicit(prev_seed->bits, prev_seed_size);
131+
efi_bs_call(free_pool, prev_seed);
132+
}
108133
return EFI_SUCCESS;
109134

110135
err_freepool:
136+
memzero_explicit(seed, struct_size(seed, bits, seed_size));
111137
efi_bs_call(free_pool, seed);
138+
efi_warn("Failed to obtain seed from EFI_RNG_PROTOCOL\n");
139+
err_warn:
140+
if (prev_seed)
141+
efi_warn("Retaining bootloader-supplied seed only");
112142
return status;
113143
}

include/linux/efi.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1133,8 +1133,6 @@ void efi_check_for_embedded_firmwares(void);
11331133
static inline void efi_check_for_embedded_firmwares(void) { }
11341134
#endif
11351135

1136-
efi_status_t efi_random_get_seed(void);
1137-
11381136
#define arch_efi_call_virt(p, f, args...) ((p)->f(args))
11391137

11401138
/*

0 commit comments

Comments
 (0)