]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/AuthVariableLib/AuthVariableLib.c
6467c0695c6cdaf476b16d7832ef1c44a98007de
[mirror_edk2.git] / SecurityPkg / Library / AuthVariableLib / AuthVariableLib.c
1 /** @file
2 Implement authentication services for the authenticated variables.
3
4 Caution: This module requires additional review when modified.
5 This driver will have external input - variable data. It may be input in SMM mode.
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
8 Variable attribute should also be checked to avoid authentication bypass.
9 The whole SMM authentication variable design relies on the integrity of flash part and SMM.
10 which is assumed to be protected by platform. All variable code and metadata in flash/SMM Memory
11 may not be modified without authorization. If platform fails to protect these resources,
12 the authentication service provided in this driver will be broken, and the behavior is undefined.
13
14 Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
15 This program and the accompanying materials
16 are licensed and made available under the terms and conditions of the BSD License
17 which accompanies this distribution. The full text of the license may be found at
18 http://opensource.org/licenses/bsd-license.php
19
20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
21 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
22
23 **/
24
25 #include "AuthServiceInternal.h"
26
27 ///
28 /// Global database array for scratch
29 ///
30 UINT8 *mPubKeyStore;
31 UINT32 mPubKeyNumber;
32 UINT32 mMaxKeyNumber;
33 UINT32 mMaxKeyDbSize;
34 UINT8 *mCertDbStore;
35 UINT32 mMaxCertDbSize;
36 UINT8 mVendorKeyState;
37
38 EFI_GUID mSignatureSupport[] = {EFI_CERT_SHA1_GUID, EFI_CERT_SHA256_GUID, EFI_CERT_RSA2048_GUID, EFI_CERT_X509_GUID};
39
40 //
41 // Hash context pointer
42 //
43 VOID *mHashCtx = NULL;
44
45 VARIABLE_ENTRY_PROPERTY mAuthVarEntry[] = {
46 {
47 &gEfiSecureBootEnableDisableGuid,
48 EFI_SECURE_BOOT_ENABLE_NAME,
49 {
50 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
51 0,
52 VARIABLE_ATTRIBUTE_NV_BS,
53 sizeof (UINT8),
54 sizeof (UINT8)
55 }
56 },
57 {
58 &gEfiCustomModeEnableGuid,
59 EFI_CUSTOM_MODE_NAME,
60 {
61 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
62 0,
63 VARIABLE_ATTRIBUTE_NV_BS,
64 sizeof (UINT8),
65 sizeof (UINT8)
66 }
67 },
68 {
69 &gEfiVendorKeysNvGuid,
70 EFI_VENDOR_KEYS_NV_VARIABLE_NAME,
71 {
72 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
73 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
74 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
75 sizeof (UINT8),
76 sizeof (UINT8)
77 }
78 },
79 {
80 &gEfiAuthenticatedVariableGuid,
81 AUTHVAR_KEYDB_NAME,
82 {
83 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
84 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
85 VARIABLE_ATTRIBUTE_NV_BS_RT_AW,
86 sizeof (UINT8),
87 MAX_UINTN
88 }
89 },
90 {
91 &gEfiCertDbGuid,
92 EFI_CERT_DB_NAME,
93 {
94 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
95 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
96 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
97 sizeof (UINT32),
98 MAX_UINTN
99 }
100 },
101 {
102 &gEfiCertDbGuid,
103 EFI_CERT_DB_VOLATILE_NAME,
104 {
105 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
106 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
107 VARIABLE_ATTRIBUTE_BS_RT_AT,
108 sizeof (UINT32),
109 MAX_UINTN
110 }
111 },
112 {
113 &gEdkiiSecureBootModeGuid,
114 L"SecureBootMode",
115 {
116 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
117 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
118 VARIABLE_ATTRIBUTE_NV_BS_RT,
119 sizeof (UINT8),
120 sizeof (UINT8)
121 }
122 }
123 };
124
125 VOID **mAuthVarAddressPointer[10];
126
127 AUTH_VAR_LIB_CONTEXT_IN *mAuthVarLibContextIn = NULL;
128
129 /**
130 Initialization for authenticated varibale services.
131 If this initialization returns error status, other APIs will not work
132 and expect to be not called then.
133
134 @param[in] AuthVarLibContextIn Pointer to input auth variable lib context.
135 @param[out] AuthVarLibContextOut Pointer to output auth variable lib context.
136
137 @retval EFI_SUCCESS Function successfully executed.
138 @retval EFI_INVALID_PARAMETER If AuthVarLibContextIn == NULL or AuthVarLibContextOut == NULL.
139 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough resource.
140 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.
141
142 **/
143 EFI_STATUS
144 EFIAPI
145 AuthVariableLibInitialize (
146 IN AUTH_VAR_LIB_CONTEXT_IN *AuthVarLibContextIn,
147 OUT AUTH_VAR_LIB_CONTEXT_OUT *AuthVarLibContextOut
148 )
149 {
150 EFI_STATUS Status;
151 UINT8 VarValue;
152 UINT32 VarAttr;
153 UINT8 *Data;
154 UINTN DataSize;
155 UINTN CtxSize;
156 UINT8 CustomMode;
157 UINT32 ListSize;
158
159 if ((AuthVarLibContextIn == NULL) || (AuthVarLibContextOut == NULL)) {
160 return EFI_INVALID_PARAMETER;
161 }
162
163 mAuthVarLibContextIn = AuthVarLibContextIn;
164
165 //
166 // Initialize hash context.
167 //
168 CtxSize = Sha256GetContextSize ();
169 mHashCtx = AllocateRuntimePool (CtxSize);
170 if (mHashCtx == NULL) {
171 return EFI_OUT_OF_RESOURCES;
172 }
173
174 //
175 // Reserve runtime buffer for public key database. The size excludes variable header and name size.
176 //
177 mMaxKeyDbSize = (UINT32) (mAuthVarLibContextIn->MaxAuthVariableSize - sizeof (AUTHVAR_KEYDB_NAME));
178 mMaxKeyNumber = mMaxKeyDbSize / sizeof (AUTHVAR_KEY_DB_DATA);
179 mPubKeyStore = AllocateRuntimePool (mMaxKeyDbSize);
180 if (mPubKeyStore == NULL) {
181 return EFI_OUT_OF_RESOURCES;
182 }
183
184 //
185 // Reserve runtime buffer for certificate database. The size excludes variable header and name size.
186 // Use EFI_CERT_DB_VOLATILE_NAME size since it is longer.
187 //
188 mMaxCertDbSize = (UINT32) (mAuthVarLibContextIn->MaxAuthVariableSize - sizeof (EFI_CERT_DB_VOLATILE_NAME));
189 mCertDbStore = AllocateRuntimePool (mMaxCertDbSize);
190 if (mCertDbStore == NULL) {
191 return EFI_OUT_OF_RESOURCES;
192 }
193
194 //
195 // Check "AuthVarKeyDatabase" variable's existence.
196 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
197 //
198 Status = AuthServiceInternalFindVariable (
199 AUTHVAR_KEYDB_NAME,
200 &gEfiAuthenticatedVariableGuid,
201 (VOID **) &Data,
202 &DataSize
203 );
204 if (EFI_ERROR (Status)) {
205 VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS;
206 VarValue = 0;
207 mPubKeyNumber = 0;
208 Status = AuthServiceInternalUpdateVariable (
209 AUTHVAR_KEYDB_NAME,
210 &gEfiAuthenticatedVariableGuid,
211 &VarValue,
212 sizeof(UINT8),
213 VarAttr
214 );
215 if (EFI_ERROR (Status)) {
216 return Status;
217 }
218 } else {
219 //
220 // Load database in global variable for cache.
221 //
222 ASSERT ((DataSize != 0) && (Data != NULL));
223 //
224 // "AuthVarKeyDatabase" is an internal variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
225 // Therefore, there is no memory overflow in underlying CopyMem.
226 //
227 CopyMem (mPubKeyStore, (UINT8 *) Data, DataSize);
228 mPubKeyNumber = (UINT32) (DataSize / sizeof (AUTHVAR_KEY_DB_DATA));
229 }
230
231 //
232 // Init Secure Boot variables
233 //
234 Status = InitSecureBootVariables ();
235
236
237 //
238 // Create "SignatureSupport" variable with BS+RT attribute set.
239 //
240 Status = AuthServiceInternalUpdateVariable (
241 EFI_SIGNATURE_SUPPORT_NAME,
242 &gEfiGlobalVariableGuid,
243 mSignatureSupport,
244 sizeof(mSignatureSupport),
245 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS
246 );
247 if (EFI_ERROR (Status)) {
248 return Status;
249 }
250
251 //
252 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
253 //
254 CustomMode = STANDARD_SECURE_BOOT_MODE;
255 Status = AuthServiceInternalUpdateVariable (
256 EFI_CUSTOM_MODE_NAME,
257 &gEfiCustomModeEnableGuid,
258 &CustomMode,
259 sizeof (UINT8),
260 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS
261 );
262 if (EFI_ERROR (Status)) {
263 return Status;
264 }
265
266 DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME, CustomMode));
267
268 //
269 // Check "certdb" variable's existence.
270 // If it doesn't exist, then create a new one with
271 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
272 //
273 Status = AuthServiceInternalFindVariable (
274 EFI_CERT_DB_NAME,
275 &gEfiCertDbGuid,
276 (VOID **) &Data,
277 &DataSize
278 );
279 if (EFI_ERROR (Status)) {
280 VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
281 ListSize = sizeof (UINT32);
282 Status = AuthServiceInternalUpdateVariable (
283 EFI_CERT_DB_NAME,
284 &gEfiCertDbGuid,
285 &ListSize,
286 sizeof (UINT32),
287 VarAttr
288 );
289 if (EFI_ERROR (Status)) {
290 return Status;
291 }
292 } else {
293 //
294 // Clean up Certs to make certDB & Time based auth variable consistent
295 //
296 Status = CleanCertsFromDb();
297 if (EFI_ERROR (Status)) {
298 DEBUG ((EFI_D_ERROR, "Clean up CertDB fail! Status %x\n", Status));
299 return Status;
300 }
301 }
302
303 //
304 // Create "certdbv" variable with RT+BS+AT set.
305 //
306 VarAttr = EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
307 ListSize = sizeof (UINT32);
308 Status = AuthServiceInternalUpdateVariable (
309 EFI_CERT_DB_VOLATILE_NAME,
310 &gEfiCertDbGuid,
311 &ListSize,
312 sizeof (UINT32),
313 VarAttr
314 );
315 if (EFI_ERROR (Status)) {
316 return Status;
317 }
318
319 //
320 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
321 //
322 Status = AuthServiceInternalFindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME, &gEfiVendorKeysNvGuid, (VOID **) &Data, &DataSize);
323 if (!EFI_ERROR (Status)) {
324 mVendorKeyState = *(UINT8 *)Data;
325 } else {
326 //
327 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
328 //
329 mVendorKeyState = VENDOR_KEYS_VALID;
330 Status = AuthServiceInternalUpdateVariable (
331 EFI_VENDOR_KEYS_NV_VARIABLE_NAME,
332 &gEfiVendorKeysNvGuid,
333 &mVendorKeyState,
334 sizeof (UINT8),
335 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
336 );
337 if (EFI_ERROR (Status)) {
338 return Status;
339 }
340 }
341
342 //
343 // Create "VendorKeys" variable with BS+RT attribute set.
344 //
345 Status = AuthServiceInternalUpdateVariable (
346 EFI_VENDOR_KEYS_VARIABLE_NAME,
347 &gEfiGlobalVariableGuid,
348 &mVendorKeyState,
349 sizeof (UINT8),
350 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS
351 );
352 if (EFI_ERROR (Status)) {
353 return Status;
354 }
355
356 DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME, mVendorKeyState));
357
358 AuthVarLibContextOut->StructVersion = AUTH_VAR_LIB_CONTEXT_OUT_STRUCT_VERSION;
359 AuthVarLibContextOut->StructSize = sizeof (AUTH_VAR_LIB_CONTEXT_OUT);
360 AuthVarLibContextOut->AuthVarEntry = mAuthVarEntry;
361 AuthVarLibContextOut->AuthVarEntryCount = sizeof (mAuthVarEntry) / sizeof (mAuthVarEntry[0]);
362 mAuthVarAddressPointer[0] = (VOID **) &mPubKeyStore;
363 mAuthVarAddressPointer[1] = (VOID **) &mCertDbStore;
364 mAuthVarAddressPointer[2] = (VOID **) &mHashCtx;
365 mAuthVarAddressPointer[3] = (VOID **) &mAuthVarLibContextIn;
366 mAuthVarAddressPointer[4] = (VOID **) &(mAuthVarLibContextIn->FindVariable),
367 mAuthVarAddressPointer[5] = (VOID **) &(mAuthVarLibContextIn->FindNextVariable),
368 mAuthVarAddressPointer[6] = (VOID **) &(mAuthVarLibContextIn->UpdateVariable),
369 mAuthVarAddressPointer[7] = (VOID **) &(mAuthVarLibContextIn->GetScratchBuffer),
370 mAuthVarAddressPointer[8] = (VOID **) &(mAuthVarLibContextIn->CheckRemainingSpaceForConsistency),
371 mAuthVarAddressPointer[9] = (VOID **) &(mAuthVarLibContextIn->AtRuntime),
372 AuthVarLibContextOut->AddressPointer = mAuthVarAddressPointer;
373 AuthVarLibContextOut->AddressPointerCount = sizeof (mAuthVarAddressPointer) / sizeof (mAuthVarAddressPointer[0]);
374
375 return Status;
376 }
377
378 /**
379 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
380
381 @param[in] VariableName Name of the variable.
382 @param[in] VendorGuid Variable vendor GUID.
383 @param[in] Data Data pointer.
384 @param[in] DataSize Size of Data.
385 @param[in] Attributes Attribute value of the variable.
386
387 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
388 defined by the Attributes.
389 @retval EFI_INVALID_PARAMETER Invalid parameter.
390 @retval EFI_WRITE_PROTECTED Variable is write-protected.
391 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
392 @retval EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
393 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS
394 set, but the AuthInfo does NOT pass the validation
395 check carried out by the firmware.
396 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.
397
398 **/
399 EFI_STATUS
400 EFIAPI
401 AuthVariableLibProcessVariable (
402 IN CHAR16 *VariableName,
403 IN EFI_GUID *VendorGuid,
404 IN VOID *Data,
405 IN UINTN DataSize,
406 IN UINT32 Attributes
407 )
408 {
409 EFI_STATUS Status;
410
411 //
412 // Process PK, KEK, Sigdb, AuditMode, DeployedMode separately.
413 //
414 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){
415 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, TRUE);
416 } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) {
417 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, FALSE);
418 } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid)
419 && (StrCmp (VariableName, EFI_AUDIT_MODE_NAME) == 0 || StrCmp (VariableName, EFI_DEPLOYED_MODE_NAME) == 0)) {
420 Status = ProcessSecureBootModeVar(VariableName, VendorGuid, Data, DataSize, Attributes);
421 } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&
422 ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) ||
423 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||
424 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0)
425 )) {
426 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, FALSE);
427 if (EFI_ERROR (Status)) {
428 Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, Attributes);
429 }
430 } else {
431 Status = ProcessVariable (VariableName, VendorGuid, Data, DataSize, Attributes);
432 }
433
434 return Status;
435 }