]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/Library/AuthVariableLib/AuthVariableLib.c
SecurityPkg AuthVariableLib: Correct address pointers data
[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, 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 UINT32 mPlatformMode;
37 UINT8 mVendorKeyState;
38
39 EFI_GUID mSignatureSupport[] = {EFI_CERT_SHA1_GUID, EFI_CERT_SHA256_GUID, EFI_CERT_RSA2048_GUID, EFI_CERT_X509_GUID};
40
41 //
42 // Hash context pointer
43 //
44 VOID *mHashCtx = NULL;
45
46 VARIABLE_ENTRY_PROPERTY mAuthVarEntry[] = {
47 {
48 &gEfiSecureBootEnableDisableGuid,
49 EFI_SECURE_BOOT_ENABLE_NAME,
50 {
51 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
52 0,
53 VARIABLE_ATTRIBUTE_NV_BS,
54 sizeof (UINT8),
55 sizeof (UINT8)
56 }
57 },
58 {
59 &gEfiCustomModeEnableGuid,
60 EFI_CUSTOM_MODE_NAME,
61 {
62 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
63 0,
64 VARIABLE_ATTRIBUTE_NV_BS,
65 sizeof (UINT8),
66 sizeof (UINT8)
67 }
68 },
69 {
70 &gEfiVendorKeysNvGuid,
71 EFI_VENDOR_KEYS_NV_VARIABLE_NAME,
72 {
73 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
74 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
75 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
76 sizeof (UINT8),
77 sizeof (UINT8)
78 }
79 },
80 {
81 &gEfiAuthenticatedVariableGuid,
82 AUTHVAR_KEYDB_NAME,
83 {
84 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
85 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
86 VARIABLE_ATTRIBUTE_NV_BS_RT_AW,
87 sizeof (UINT8),
88 MAX_UINTN
89 }
90 },
91 {
92 &gEfiCertDbGuid,
93 EFI_CERT_DB_NAME,
94 {
95 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
96 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
97 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
98 sizeof (UINT32),
99 MAX_UINTN
100 }
101 },
102 };
103
104 VOID **mAuthVarAddressPointer[10];
105
106 AUTH_VAR_LIB_CONTEXT_IN *mAuthVarLibContextIn = NULL;
107
108 /**
109 Initialization for authenticated varibale services.
110 If this initialization returns error status, other APIs will not work
111 and expect to be not called then.
112
113 @param[in] AuthVarLibContextIn Pointer to input auth variable lib context.
114 @param[out] AuthVarLibContextOut Pointer to output auth variable lib context.
115
116 @retval EFI_SUCCESS Function successfully executed.
117 @retval EFI_INVALID_PARAMETER If AuthVarLibContextIn == NULL or AuthVarLibContextOut == NULL.
118 @retval EFI_OUT_OF_RESOURCES Fail to allocate enough resource.
119 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.
120
121 **/
122 EFI_STATUS
123 EFIAPI
124 AuthVariableLibInitialize (
125 IN AUTH_VAR_LIB_CONTEXT_IN *AuthVarLibContextIn,
126 OUT AUTH_VAR_LIB_CONTEXT_OUT *AuthVarLibContextOut
127 )
128 {
129 EFI_STATUS Status;
130 UINT8 VarValue;
131 UINT32 VarAttr;
132 UINT8 *Data;
133 UINTN DataSize;
134 UINTN CtxSize;
135 UINT8 SecureBootMode;
136 UINT8 SecureBootEnable;
137 UINT8 CustomMode;
138 UINT32 ListSize;
139
140 if ((AuthVarLibContextIn == NULL) || (AuthVarLibContextOut == NULL)) {
141 return EFI_INVALID_PARAMETER;
142 }
143
144 mAuthVarLibContextIn = AuthVarLibContextIn;
145
146 //
147 // Initialize hash context.
148 //
149 CtxSize = Sha256GetContextSize ();
150 mHashCtx = AllocateRuntimePool (CtxSize);
151 if (mHashCtx == NULL) {
152 return EFI_OUT_OF_RESOURCES;
153 }
154
155 //
156 // Reserve runtime buffer for public key database. The size excludes variable header and name size.
157 //
158 mMaxKeyDbSize = (UINT32) (mAuthVarLibContextIn->MaxAuthVariableSize - sizeof (AUTHVAR_KEYDB_NAME));
159 mMaxKeyNumber = mMaxKeyDbSize / sizeof (AUTHVAR_KEY_DB_DATA);
160 mPubKeyStore = AllocateRuntimePool (mMaxKeyDbSize);
161 if (mPubKeyStore == NULL) {
162 return EFI_OUT_OF_RESOURCES;
163 }
164
165 //
166 // Reserve runtime buffer for certificate database. The size excludes variable header and name size.
167 //
168 mMaxCertDbSize = (UINT32) (mAuthVarLibContextIn->MaxAuthVariableSize - sizeof (EFI_CERT_DB_NAME));
169 mCertDbStore = AllocateRuntimePool (mMaxCertDbSize);
170 if (mCertDbStore == NULL) {
171 return EFI_OUT_OF_RESOURCES;
172 }
173
174 //
175 // Check "AuthVarKeyDatabase" variable's existence.
176 // If it doesn't exist, create a new one with initial value of 0 and EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS set.
177 //
178 Status = AuthServiceInternalFindVariable (
179 AUTHVAR_KEYDB_NAME,
180 &gEfiAuthenticatedVariableGuid,
181 (VOID **) &Data,
182 &DataSize
183 );
184 if (EFI_ERROR (Status)) {
185 VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS;
186 VarValue = 0;
187 mPubKeyNumber = 0;
188 Status = AuthServiceInternalUpdateVariable (
189 AUTHVAR_KEYDB_NAME,
190 &gEfiAuthenticatedVariableGuid,
191 &VarValue,
192 sizeof(UINT8),
193 VarAttr
194 );
195 if (EFI_ERROR (Status)) {
196 return Status;
197 }
198 } else {
199 //
200 // Load database in global variable for cache.
201 //
202 ASSERT ((DataSize != 0) && (Data != NULL));
203 //
204 // "AuthVarKeyDatabase" is an internal variable. Its DataSize is always ensured not to exceed mPubKeyStore buffer size(See definition before)
205 // Therefore, there is no memory overflow in underlying CopyMem.
206 //
207 CopyMem (mPubKeyStore, (UINT8 *) Data, DataSize);
208 mPubKeyNumber = (UINT32) (DataSize / sizeof (AUTHVAR_KEY_DB_DATA));
209 }
210
211 Status = AuthServiceInternalFindVariable (EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid, (VOID **) &Data, &DataSize);
212 if (EFI_ERROR (Status)) {
213 DEBUG ((EFI_D_INFO, "Variable %s does not exist.\n", EFI_PLATFORM_KEY_NAME));
214 } else {
215 DEBUG ((EFI_D_INFO, "Variable %s exists.\n", EFI_PLATFORM_KEY_NAME));
216 }
217
218 //
219 // Create "SetupMode" variable with BS+RT attribute set.
220 //
221 if (EFI_ERROR (Status)) {
222 mPlatformMode = SETUP_MODE;
223 } else {
224 mPlatformMode = USER_MODE;
225 }
226 Status = AuthServiceInternalUpdateVariable (
227 EFI_SETUP_MODE_NAME,
228 &gEfiGlobalVariableGuid,
229 &mPlatformMode,
230 sizeof(UINT8),
231 EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS
232 );
233 if (EFI_ERROR (Status)) {
234 return Status;
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 // If "SecureBootEnable" variable exists, then update "SecureBoot" variable.
253 // If "SecureBootEnable" variable is SECURE_BOOT_ENABLE and in USER_MODE, Set "SecureBoot" variable to SECURE_BOOT_MODE_ENABLE.
254 // If "SecureBootEnable" variable is SECURE_BOOT_DISABLE, Set "SecureBoot" variable to SECURE_BOOT_MODE_DISABLE.
255 //
256 SecureBootEnable = SECURE_BOOT_DISABLE;
257 Status = AuthServiceInternalFindVariable (EFI_SECURE_BOOT_ENABLE_NAME, &gEfiSecureBootEnableDisableGuid, (VOID **) &Data, &DataSize);
258 if (!EFI_ERROR (Status)) {
259 if (mPlatformMode == SETUP_MODE){
260 //
261 // PK is cleared in runtime. "SecureBootMode" is not updated before reboot
262 // Delete "SecureBootMode" in SetupMode
263 //
264 Status = AuthServiceInternalUpdateVariable (
265 EFI_SECURE_BOOT_ENABLE_NAME,
266 &gEfiSecureBootEnableDisableGuid,
267 &SecureBootEnable,
268 0,
269 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS
270 );
271 } else {
272 SecureBootEnable = *(UINT8 *) Data;
273 }
274 } else if (mPlatformMode == USER_MODE) {
275 //
276 // "SecureBootEnable" not exist, initialize it in USER_MODE.
277 //
278 SecureBootEnable = SECURE_BOOT_ENABLE;
279 Status = AuthServiceInternalUpdateVariable (
280 EFI_SECURE_BOOT_ENABLE_NAME,
281 &gEfiSecureBootEnableDisableGuid,
282 &SecureBootEnable,
283 sizeof (UINT8),
284 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS
285 );
286 if (EFI_ERROR (Status)) {
287 return Status;
288 }
289 }
290
291 //
292 // Create "SecureBoot" variable with BS+RT attribute set.
293 //
294 if (SecureBootEnable == SECURE_BOOT_ENABLE && mPlatformMode == USER_MODE) {
295 SecureBootMode = SECURE_BOOT_MODE_ENABLE;
296 } else {
297 SecureBootMode = SECURE_BOOT_MODE_DISABLE;
298 }
299 Status = AuthServiceInternalUpdateVariable (
300 EFI_SECURE_BOOT_MODE_NAME,
301 &gEfiGlobalVariableGuid,
302 &SecureBootMode,
303 sizeof (UINT8),
304 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS
305 );
306 if (EFI_ERROR (Status)) {
307 return Status;
308 }
309
310 DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SETUP_MODE_NAME, mPlatformMode));
311 DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_MODE_NAME, SecureBootMode));
312 DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_SECURE_BOOT_ENABLE_NAME, SecureBootEnable));
313
314 //
315 // Initialize "CustomMode" in STANDARD_SECURE_BOOT_MODE state.
316 //
317 CustomMode = STANDARD_SECURE_BOOT_MODE;
318 Status = AuthServiceInternalUpdateVariable (
319 EFI_CUSTOM_MODE_NAME,
320 &gEfiCustomModeEnableGuid,
321 &CustomMode,
322 sizeof (UINT8),
323 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS
324 );
325 if (EFI_ERROR (Status)) {
326 return Status;
327 }
328
329 DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_CUSTOM_MODE_NAME, CustomMode));
330
331 //
332 // Check "certdb" variable's existence.
333 // If it doesn't exist, then create a new one with
334 // EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
335 //
336 Status = AuthServiceInternalFindVariable (
337 EFI_CERT_DB_NAME,
338 &gEfiCertDbGuid,
339 (VOID **) &Data,
340 &DataSize
341 );
342 if (EFI_ERROR (Status)) {
343 VarAttr = EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS;
344 ListSize = sizeof (UINT32);
345 Status = AuthServiceInternalUpdateVariable (
346 EFI_CERT_DB_NAME,
347 &gEfiCertDbGuid,
348 &ListSize,
349 sizeof (UINT32),
350 VarAttr
351 );
352 if (EFI_ERROR (Status)) {
353 return Status;
354 }
355 } else {
356 //
357 // Clean up Certs to make certDB & Time based auth variable consistent
358 //
359 Status = CleanCertsFromDb();
360 if (EFI_ERROR (Status)) {
361 DEBUG ((EFI_D_INFO, "Clean up CertDB fail! Status %x\n", Status));
362 return Status;
363 }
364 }
365
366 //
367 // Check "VendorKeysNv" variable's existence and create "VendorKeys" variable accordingly.
368 //
369 Status = AuthServiceInternalFindVariable (EFI_VENDOR_KEYS_NV_VARIABLE_NAME, &gEfiVendorKeysNvGuid, (VOID **) &Data, &DataSize);
370 if (!EFI_ERROR (Status)) {
371 mVendorKeyState = *(UINT8 *)Data;
372 } else {
373 //
374 // "VendorKeysNv" not exist, initialize it in VENDOR_KEYS_VALID state.
375 //
376 mVendorKeyState = VENDOR_KEYS_VALID;
377 Status = AuthServiceInternalUpdateVariable (
378 EFI_VENDOR_KEYS_NV_VARIABLE_NAME,
379 &gEfiVendorKeysNvGuid,
380 &mVendorKeyState,
381 sizeof (UINT8),
382 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS
383 );
384 if (EFI_ERROR (Status)) {
385 return Status;
386 }
387 }
388
389 //
390 // Create "VendorKeys" variable with BS+RT attribute set.
391 //
392 Status = AuthServiceInternalUpdateVariable (
393 EFI_VENDOR_KEYS_VARIABLE_NAME,
394 &gEfiGlobalVariableGuid,
395 &mVendorKeyState,
396 sizeof (UINT8),
397 EFI_VARIABLE_RUNTIME_ACCESS | EFI_VARIABLE_BOOTSERVICE_ACCESS
398 );
399 if (EFI_ERROR (Status)) {
400 return Status;
401 }
402
403 DEBUG ((EFI_D_INFO, "Variable %s is %x\n", EFI_VENDOR_KEYS_VARIABLE_NAME, mVendorKeyState));
404
405 AuthVarLibContextOut->StructVersion = AUTH_VAR_LIB_CONTEXT_OUT_STRUCT_VERSION;
406 AuthVarLibContextOut->StructSize = sizeof (AUTH_VAR_LIB_CONTEXT_OUT);
407 AuthVarLibContextOut->AuthVarEntry = mAuthVarEntry;
408 AuthVarLibContextOut->AuthVarEntryCount = sizeof (mAuthVarEntry) / sizeof (mAuthVarEntry[0]);
409 mAuthVarAddressPointer[0] = (VOID **) &mPubKeyStore;
410 mAuthVarAddressPointer[1] = (VOID **) &mCertDbStore;
411 mAuthVarAddressPointer[2] = (VOID **) &mHashCtx;
412 mAuthVarAddressPointer[3] = (VOID **) &mAuthVarLibContextIn;
413 mAuthVarAddressPointer[4] = (VOID **) &(mAuthVarLibContextIn->FindVariable),
414 mAuthVarAddressPointer[5] = (VOID **) &(mAuthVarLibContextIn->FindNextVariable),
415 mAuthVarAddressPointer[6] = (VOID **) &(mAuthVarLibContextIn->UpdateVariable),
416 mAuthVarAddressPointer[7] = (VOID **) &(mAuthVarLibContextIn->GetScratchBuffer),
417 mAuthVarAddressPointer[8] = (VOID **) &(mAuthVarLibContextIn->CheckRemainingSpaceForConsistency),
418 mAuthVarAddressPointer[9] = (VOID **) &(mAuthVarLibContextIn->AtRuntime),
419 AuthVarLibContextOut->AddressPointer = mAuthVarAddressPointer;
420 AuthVarLibContextOut->AddressPointerCount = sizeof (mAuthVarAddressPointer) / sizeof (mAuthVarAddressPointer[0]);
421
422 return Status;
423 }
424
425 /**
426 Process variable with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS/EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS set.
427
428 @param[in] VariableName Name of the variable.
429 @param[in] VendorGuid Variable vendor GUID.
430 @param[in] Data Data pointer.
431 @param[in] DataSize Size of Data.
432 @param[in] Attributes Attribute value of the variable.
433
434 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as
435 defined by the Attributes.
436 @retval EFI_INVALID_PARAMETER Invalid parameter.
437 @retval EFI_WRITE_PROTECTED Variable is write-protected.
438 @retval EFI_OUT_OF_RESOURCES There is not enough resource.
439 @retval EFI_SECURITY_VIOLATION The variable is with EFI_VARIABLE_AUTHENTICATED_WRITE_ACCESS
440 or EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACESS
441 set, but the AuthInfo does NOT pass the validation
442 check carried out by the firmware.
443 @retval EFI_UNSUPPORTED Unsupported to process authenticated variable.
444
445 **/
446 EFI_STATUS
447 EFIAPI
448 AuthVariableLibProcessVariable (
449 IN CHAR16 *VariableName,
450 IN EFI_GUID *VendorGuid,
451 IN VOID *Data,
452 IN UINTN DataSize,
453 IN UINT32 Attributes
454 )
455 {
456 EFI_STATUS Status;
457
458 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0)){
459 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, TRUE);
460 } else if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid) && (StrCmp (VariableName, EFI_KEY_EXCHANGE_KEY_NAME) == 0)) {
461 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, FALSE);
462 } else if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid) &&
463 ((StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE) == 0) ||
464 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE1) == 0) ||
465 (StrCmp (VariableName, EFI_IMAGE_SECURITY_DATABASE2) == 0)
466 )) {
467 Status = ProcessVarWithPk (VariableName, VendorGuid, Data, DataSize, Attributes, FALSE);
468 if (EFI_ERROR (Status)) {
469 Status = ProcessVarWithKek (VariableName, VendorGuid, Data, DataSize, Attributes);
470 }
471 } else {
472 Status = ProcessVariable (VariableName, VendorGuid, Data, DataSize, Attributes);
473 }
474
475 return Status;
476 }