Mention branches and keyring.
[releases.git] / x86 / coco / core.c
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * Confidential Computing Platform Capability checks
4  *
5  * Copyright (C) 2021 Advanced Micro Devices, Inc.
6  * Copyright (C) 2024 Jason A. Donenfeld <Jason@zx2c4.com>. All Rights Reserved.
7  *
8  * Author: Tom Lendacky <thomas.lendacky@amd.com>
9  */
10
11 #include <linux/export.h>
12 #include <linux/cc_platform.h>
13 #include <linux/string.h>
14 #include <linux/random.h>
15
16 #include <asm/archrandom.h>
17 #include <asm/coco.h>
18 #include <asm/processor.h>
19
20 enum cc_vendor cc_vendor __ro_after_init = CC_VENDOR_NONE;
21 u64 cc_mask __ro_after_init;
22
23 static bool intel_cc_platform_has(enum cc_attr attr)
24 {
25         switch (attr) {
26         case CC_ATTR_GUEST_UNROLL_STRING_IO:
27         case CC_ATTR_HOTPLUG_DISABLED:
28         case CC_ATTR_GUEST_MEM_ENCRYPT:
29         case CC_ATTR_MEM_ENCRYPT:
30                 return true;
31         default:
32                 return false;
33         }
34 }
35
36 /*
37  * SME and SEV are very similar but they are not the same, so there are
38  * times that the kernel will need to distinguish between SME and SEV. The
39  * cc_platform_has() function is used for this.  When a distinction isn't
40  * needed, the CC_ATTR_MEM_ENCRYPT attribute can be used.
41  *
42  * The trampoline code is a good example for this requirement.  Before
43  * paging is activated, SME will access all memory as decrypted, but SEV
44  * will access all memory as encrypted.  So, when APs are being brought
45  * up under SME the trampoline area cannot be encrypted, whereas under SEV
46  * the trampoline area must be encrypted.
47  */
48 static bool amd_cc_platform_has(enum cc_attr attr)
49 {
50 #ifdef CONFIG_AMD_MEM_ENCRYPT
51         switch (attr) {
52         case CC_ATTR_MEM_ENCRYPT:
53                 return sme_me_mask;
54
55         case CC_ATTR_HOST_MEM_ENCRYPT:
56                 return sme_me_mask && !(sev_status & MSR_AMD64_SEV_ENABLED);
57
58         case CC_ATTR_GUEST_MEM_ENCRYPT:
59                 return sev_status & MSR_AMD64_SEV_ENABLED;
60
61         case CC_ATTR_GUEST_STATE_ENCRYPT:
62                 return sev_status & MSR_AMD64_SEV_ES_ENABLED;
63
64         /*
65          * With SEV, the rep string I/O instructions need to be unrolled
66          * but SEV-ES supports them through the #VC handler.
67          */
68         case CC_ATTR_GUEST_UNROLL_STRING_IO:
69                 return (sev_status & MSR_AMD64_SEV_ENABLED) &&
70                         !(sev_status & MSR_AMD64_SEV_ES_ENABLED);
71
72         case CC_ATTR_GUEST_SEV_SNP:
73                 return sev_status & MSR_AMD64_SEV_SNP_ENABLED;
74
75         default:
76                 return false;
77         }
78 #else
79         return false;
80 #endif
81 }
82
83 static bool hyperv_cc_platform_has(enum cc_attr attr)
84 {
85         return attr == CC_ATTR_GUEST_MEM_ENCRYPT;
86 }
87
88 bool cc_platform_has(enum cc_attr attr)
89 {
90         switch (cc_vendor) {
91         case CC_VENDOR_AMD:
92                 return amd_cc_platform_has(attr);
93         case CC_VENDOR_INTEL:
94                 return intel_cc_platform_has(attr);
95         case CC_VENDOR_HYPERV:
96                 return hyperv_cc_platform_has(attr);
97         default:
98                 return false;
99         }
100 }
101 EXPORT_SYMBOL_GPL(cc_platform_has);
102
103 u64 cc_mkenc(u64 val)
104 {
105         /*
106          * Both AMD and Intel use a bit in the page table to indicate
107          * encryption status of the page.
108          *
109          * - for AMD, bit *set* means the page is encrypted
110          * - for Intel *clear* means encrypted.
111          */
112         switch (cc_vendor) {
113         case CC_VENDOR_AMD:
114                 return val | cc_mask;
115         case CC_VENDOR_INTEL:
116                 return val & ~cc_mask;
117         default:
118                 return val;
119         }
120 }
121
122 u64 cc_mkdec(u64 val)
123 {
124         /* See comment in cc_mkenc() */
125         switch (cc_vendor) {
126         case CC_VENDOR_AMD:
127                 return val & ~cc_mask;
128         case CC_VENDOR_INTEL:
129                 return val | cc_mask;
130         default:
131                 return val;
132         }
133 }
134 EXPORT_SYMBOL_GPL(cc_mkdec);
135
136 __init void cc_random_init(void)
137 {
138         /*
139          * The seed is 32 bytes (in units of longs), which is 256 bits, which
140          * is the security level that the RNG is targeting.
141          */
142         unsigned long rng_seed[32 / sizeof(long)];
143         size_t i, longs;
144
145         if (!cc_platform_has(CC_ATTR_GUEST_MEM_ENCRYPT))
146                 return;
147
148         /*
149          * Since the CoCo threat model includes the host, the only reliable
150          * source of entropy that can be neither observed nor manipulated is
151          * RDRAND. Usually, RDRAND failure is considered tolerable, but since
152          * CoCo guests have no other unobservable source of entropy, it's
153          * important to at least ensure the RNG gets some initial random seeds.
154          */
155         for (i = 0; i < ARRAY_SIZE(rng_seed); i += longs) {
156                 longs = arch_get_random_longs(&rng_seed[i], ARRAY_SIZE(rng_seed) - i);
157
158                 /*
159                  * A zero return value means that the guest doesn't have RDRAND
160                  * or the CPU is physically broken, and in both cases that
161                  * means most crypto inside of the CoCo instance will be
162                  * broken, defeating the purpose of CoCo in the first place. So
163                  * just panic here because it's absolutely unsafe to continue
164                  * executing.
165                  */
166                 if (longs == 0)
167                         panic("RDRAND is defective.");
168         }
169         add_device_randomness(rng_seed, sizeof(rng_seed));
170         memzero_explicit(rng_seed, sizeof(rng_seed));
171 }