Skip to content

Commit 99485c4

Browse files
zx2c4bp3tk0v
authored andcommitted
x86/coco: Require seeding RNG with RDRAND on CoCo systems
There are few uses of CoCo that don't rely on working cryptography and hence a working RNG. Unfortunately, the CoCo threat model means that the VM host cannot be trusted and may actively work against guests to extract secrets or manipulate computation. Since a malicious host can modify or observe nearly all inputs to guests, the only remaining source of entropy for CoCo guests is RDRAND. If RDRAND is broken -- due to CPU hardware fault -- the RNG as a whole is meant to gracefully continue on gathering entropy from other sources, but since there aren't other sources on CoCo, this is catastrophic. This is mostly a concern at boot time when initially seeding the RNG, as after that the consequences of a broken RDRAND are much more theoretical. So, try at boot to seed the RNG using 256 bits of RDRAND output. If this fails, panic(). This will also trigger if the system is booted without RDRAND, as RDRAND is essential for a safe CoCo boot. Add this deliberately to be "just a CoCo x86 driver feature" and not part of the RNG itself. Many device drivers and platforms have some desire to contribute something to the RNG, and add_device_randomness() is specifically meant for this purpose. Any driver can call it with seed data of any quality, or even garbage quality, and it can only possibly make the quality of the RNG better or have no effect, but can never make it worse. Rather than trying to build something into the core of the RNG, consider the particular CoCo issue just a CoCo issue, and therefore separate it all out into driver (well, arch/platform) code. [ bp: Massage commit message. ] Signed-off-by: Jason A. Donenfeld <[email protected]> Signed-off-by: Borislav Petkov (AMD) <[email protected]> Reviewed-by: Elena Reshetova <[email protected]> Reviewed-by: Kirill A. Shutemov <[email protected]> Reviewed-by: Theodore Ts'o <[email protected]> Cc: [email protected] Link: https://lore.kernel.org/r/[email protected]
1 parent 9852b1d commit 99485c4

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

arch/x86/coco/core.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,17 @@
33
* Confidential Computing Platform Capability checks
44
*
55
* Copyright (C) 2021 Advanced Micro Devices, Inc.
6+
* Copyright (C) 2024 Jason A. Donenfeld <[email protected]>. All Rights Reserved.
67
*
78
* Author: Tom Lendacky <[email protected]>
89
*/
910

1011
#include <linux/export.h>
1112
#include <linux/cc_platform.h>
13+
#include <linux/string.h>
14+
#include <linux/random.h>
1215

16+
#include <asm/archrandom.h>
1317
#include <asm/coco.h>
1418
#include <asm/processor.h>
1519

@@ -148,3 +152,40 @@ u64 cc_mkdec(u64 val)
148152
}
149153
}
150154
EXPORT_SYMBOL_GPL(cc_mkdec);
155+
156+
__init void cc_random_init(void)
157+
{
158+
/*
159+
* The seed is 32 bytes (in units of longs), which is 256 bits, which
160+
* is the security level that the RNG is targeting.
161+
*/
162+
unsigned long rng_seed[32 / sizeof(long)];
163+
size_t i, longs;
164+
165+
if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
166+
return;
167+
168+
/*
169+
* Since the CoCo threat model includes the host, the only reliable
170+
* source of entropy that can be neither observed nor manipulated is
171+
* RDRAND. Usually, RDRAND failure is considered tolerable, but since
172+
* CoCo guests have no other unobservable source of entropy, it's
173+
* important to at least ensure the RNG gets some initial random seeds.
174+
*/
175+
for (i = 0; i < ARRAY_SIZE(rng_seed); i += longs) {
176+
longs = arch_get_random_longs(&rng_seed[i], ARRAY_SIZE(rng_seed) - i);
177+
178+
/*
179+
* A zero return value means that the guest doesn't have RDRAND
180+
* or the CPU is physically broken, and in both cases that
181+
* means most crypto inside of the CoCo instance will be
182+
* broken, defeating the purpose of CoCo in the first place. So
183+
* just panic here because it's absolutely unsafe to continue
184+
* executing.
185+
*/
186+
if (longs == 0)
187+
panic("RDRAND is defective.");
188+
}
189+
add_device_randomness(rng_seed, sizeof(rng_seed));
190+
memzero_explicit(rng_seed, sizeof(rng_seed));
191+
}

arch/x86/include/asm/coco.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ static inline void cc_set_mask(u64 mask)
2222

2323
u64 cc_mkenc(u64 val);
2424
u64 cc_mkdec(u64 val);
25+
void cc_random_init(void);
2526
#else
2627
#define cc_vendor (CC_VENDOR_NONE)
2728

@@ -34,6 +35,7 @@ static inline u64 cc_mkdec(u64 val)
3435
{
3536
return val;
3637
}
38+
static inline void cc_random_init(void) { }
3739
#endif
3840

3941
#endif /* _ASM_X86_COCO_H */

arch/x86/kernel/setup.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#include <asm/bios_ebda.h>
3636
#include <asm/bugs.h>
3737
#include <asm/cacheinfo.h>
38+
#include <asm/coco.h>
3839
#include <asm/cpu.h>
3940
#include <asm/efi.h>
4041
#include <asm/gart.h>
@@ -991,6 +992,7 @@ void __init setup_arch(char **cmdline_p)
991992
* memory size.
992993
*/
993994
mem_encrypt_setup_arch();
995+
cc_random_init();
994996

995997
efi_fake_memmap();
996998
efi_find_mirror();

0 commit comments

Comments
 (0)