]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/RuntimeDxe/VarCheck.c
SecurityPkg Variable: Implement variable quota management.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / VarCheck.c
1 /** @file
2 Implementation functions and structures for var check protocol.
3
4 Copyright (c) 2015, Intel Corporation. All rights reserved.<BR>
5 This program and the accompanying materials
6 are licensed and made available under the terms and conditions of the BSD License
7 which accompanies this distribution. The full text of the license may be found at
8 http://opensource.org/licenses/bsd-license.php
9
10 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
11 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
12
13 **/
14
15 #include "Variable.h"
16 #include <Library/DevicePathLib.h>
17
18 extern LIST_ENTRY mLockedVariableList;
19 extern BOOLEAN mEndOfDxe;
20 extern BOOLEAN mEnableLocking;
21
22 #define VAR_CHECK_HANDLER_TABLE_SIZE 0x8
23
24 UINT32 mNumberOfHandler = 0;
25 UINT32 mMaxNumberOfHandler = 0;
26 VAR_CHECK_SET_VARIABLE_CHECK_HANDLER *mHandlerTable = NULL;
27
28 typedef struct {
29 LIST_ENTRY Link;
30 EFI_GUID Guid;
31 VAR_CHECK_VARIABLE_PROPERTY VariableProperty;
32 //CHAR16 *Name;
33 } VAR_CHECK_VARIABLE_ENTRY;
34
35 LIST_ENTRY mVarCheckVariableList = INITIALIZE_LIST_HEAD_VARIABLE (mVarCheckVariableList);
36
37 typedef
38 EFI_STATUS
39 (EFIAPI *INTERNAL_VAR_CHECK_FUNCTION) (
40 IN VAR_CHECK_VARIABLE_PROPERTY *Propery,
41 IN UINTN DataSize,
42 IN VOID *Data
43 );
44
45 typedef struct {
46 CHAR16 *Name;
47 VAR_CHECK_VARIABLE_PROPERTY VariableProperty;
48 INTERNAL_VAR_CHECK_FUNCTION CheckFunction;
49 } UEFI_DEFINED_VARIABLE_ENTRY;
50
51 /**
52 Internal check for load option.
53
54 @param[in] VariablePropery Pointer to variable property.
55 @param[in] DataSize Data size.
56 @param[in] Data Pointer to data buffer.
57
58 @retval EFI_SUCCESS The SetVariable check result was success.
59 @retval EFI_INVALID_PARAMETER The data buffer is not a valid load option.
60
61 **/
62 EFI_STATUS
63 EFIAPI
64 InternalVarCheckLoadOption (
65 IN VAR_CHECK_VARIABLE_PROPERTY *VariablePropery,
66 IN UINTN DataSize,
67 IN VOID *Data
68 )
69 {
70 UINT16 FilePathListLength;
71 CHAR16 *Description;
72 EFI_DEVICE_PATH_PROTOCOL *FilePathList;
73
74 FilePathListLength = *((UINT16 *) ((UINTN) Data + sizeof (UINT32)));
75
76 //
77 // Check Description
78 //
79 Description = (CHAR16 *) ((UINTN) Data + sizeof (UINT32) + sizeof (UINT16));
80 while (Description < (CHAR16 *) ((UINTN) Data + DataSize)) {
81 if (*Description == L'\0') {
82 break;
83 }
84 Description++;
85 }
86 if ((UINTN) Description >= ((UINTN) Data + DataSize)) {
87 return EFI_INVALID_PARAMETER;
88 }
89 Description++;
90
91 //
92 // Check FilePathList
93 //
94 FilePathList = (EFI_DEVICE_PATH_PROTOCOL *) Description;
95 if ((UINTN) FilePathList > (MAX_ADDRESS - FilePathListLength)) {
96 return EFI_INVALID_PARAMETER;
97 }
98 if (((UINTN) FilePathList + FilePathListLength) > ((UINTN) Data + DataSize)) {
99 return EFI_INVALID_PARAMETER;
100 }
101 if (FilePathListLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)) {
102 return EFI_INVALID_PARAMETER;
103 }
104 if (!IsDevicePathValid (FilePathList, FilePathListLength)) {
105 return EFI_INVALID_PARAMETER;
106 }
107
108 return EFI_SUCCESS;
109 }
110
111 /**
112 Internal check for key option.
113
114 @param[in] VariablePropery Pointer to variable property.
115 @param[in] DataSize Data size.
116 @param[in] Data Pointer to data buffer.
117
118 @retval EFI_SUCCESS The SetVariable check result was success.
119 @retval EFI_INVALID_PARAMETER The data buffer is not a valid key option.
120
121 **/
122 EFI_STATUS
123 EFIAPI
124 InternalVarCheckKeyOption (
125 IN VAR_CHECK_VARIABLE_PROPERTY *VariablePropery,
126 IN UINTN DataSize,
127 IN VOID *Data
128 )
129 {
130 if (((DataSize - sizeof (EFI_KEY_OPTION)) % sizeof (EFI_INPUT_KEY)) != 0) {
131 return EFI_INVALID_PARAMETER;
132 }
133
134 return EFI_SUCCESS;
135 }
136
137 /**
138 Internal check for device path.
139
140 @param[in] VariablePropery Pointer to variable property.
141 @param[in] DataSize Data size.
142 @param[in] Data Pointer to data buffer.
143
144 @retval EFI_SUCCESS The SetVariable check result was success.
145 @retval EFI_INVALID_PARAMETER The data buffer is not a valid device path.
146
147 **/
148 EFI_STATUS
149 EFIAPI
150 InternalVarCheckDevicePath (
151 IN VAR_CHECK_VARIABLE_PROPERTY *VariablePropery,
152 IN UINTN DataSize,
153 IN VOID *Data
154 )
155 {
156 if (!IsDevicePathValid ((EFI_DEVICE_PATH_PROTOCOL *) Data, DataSize)) {
157 return EFI_INVALID_PARAMETER;
158 }
159 return EFI_SUCCESS;
160 }
161
162 /**
163 Internal check for ASCII string.
164
165 @param[in] VariablePropery Pointer to variable property.
166 @param[in] DataSize Data size.
167 @param[in] Data Pointer to data buffer.
168
169 @retval EFI_SUCCESS The SetVariable check result was success.
170 @retval EFI_INVALID_PARAMETER The data buffer is not a Null-terminated ASCII string.
171
172 **/
173 EFI_STATUS
174 EFIAPI
175 InternalVarCheckAsciiString (
176 IN VAR_CHECK_VARIABLE_PROPERTY *VariablePropery,
177 IN UINTN DataSize,
178 IN VOID *Data
179 )
180 {
181 CHAR8 *String;
182 UINTN Index;
183
184 String = (CHAR8 *) Data;
185 if (String[DataSize - 1] == '\0') {
186 return EFI_SUCCESS;
187 } else {
188 for (Index = 1; Index < DataSize && (String[DataSize - 1 - Index] != '\0'); Index++);
189 if (Index == DataSize) {
190 return EFI_INVALID_PARAMETER;
191 }
192 }
193 return EFI_SUCCESS;
194 }
195
196 /**
197 Internal check for size array.
198
199 @param[in] VariablePropery Pointer to variable property.
200 @param[in] DataSize Data size.
201 @param[in] Data Pointer to data buffer.
202
203 @retval EFI_SUCCESS The SetVariable check result was success.
204 @retval EFI_INVALID_PARAMETER The DataSize is not size array.
205
206 **/
207 EFI_STATUS
208 EFIAPI
209 InternalVarCheckSizeArray (
210 IN VAR_CHECK_VARIABLE_PROPERTY *VariablePropery,
211 IN UINTN DataSize,
212 IN VOID *Data
213 )
214 {
215 if ((DataSize % VariablePropery->MinSize) != 0) {
216 return EFI_INVALID_PARAMETER;
217 }
218 return EFI_SUCCESS;
219 }
220
221 //
222 // To prevent name collisions with possible future globally defined variables,
223 // other internal firmware data variables that are not defined here must be
224 // saved with a unique VendorGuid other than EFI_GLOBAL_VARIABLE or
225 // any other GUID defined by the UEFI Specification. Implementations must
226 // only permit the creation of variables with a UEFI Specification-defined
227 // VendorGuid when these variables are documented in the UEFI Specification.
228 //
229 UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList[] = {
230 {
231 EFI_LANG_CODES_VARIABLE_NAME,
232 {
233 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
234 0,
235 VARIABLE_ATTRIBUTE_BS_RT,
236 1,
237 MAX_UINTN
238 },
239 InternalVarCheckAsciiString
240 },
241 {
242 EFI_LANG_VARIABLE_NAME,
243 {
244 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
245 0,
246 VARIABLE_ATTRIBUTE_NV_BS_RT,
247 1,
248 MAX_UINTN
249 },
250 InternalVarCheckAsciiString
251 },
252 {
253 EFI_TIME_OUT_VARIABLE_NAME,
254 {
255 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
256 0,
257 VARIABLE_ATTRIBUTE_NV_BS_RT,
258 sizeof (UINT16),
259 sizeof (UINT16)
260 },
261 NULL
262 },
263 {
264 EFI_PLATFORM_LANG_CODES_VARIABLE_NAME,
265 {
266 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
267 0,
268 VARIABLE_ATTRIBUTE_BS_RT,
269 1,
270 MAX_UINTN
271 },
272 InternalVarCheckAsciiString
273 },
274 {
275 EFI_PLATFORM_LANG_VARIABLE_NAME,
276 {
277 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
278 0,
279 VARIABLE_ATTRIBUTE_NV_BS_RT,
280 1,
281 MAX_UINTN
282 },
283 InternalVarCheckAsciiString
284 },
285 {
286 EFI_CON_IN_VARIABLE_NAME,
287 {
288 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
289 0,
290 VARIABLE_ATTRIBUTE_NV_BS_RT,
291 sizeof (EFI_DEVICE_PATH_PROTOCOL),
292 MAX_UINTN
293 },
294 InternalVarCheckDevicePath
295 },
296 {
297 EFI_CON_OUT_VARIABLE_NAME,
298 {
299 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
300 0,
301 VARIABLE_ATTRIBUTE_NV_BS_RT,
302 sizeof (EFI_DEVICE_PATH_PROTOCOL),
303 MAX_UINTN
304 },
305 InternalVarCheckDevicePath
306 },
307 {
308 EFI_ERR_OUT_VARIABLE_NAME,
309 {
310 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
311 0,
312 VARIABLE_ATTRIBUTE_NV_BS_RT,
313 sizeof (EFI_DEVICE_PATH_PROTOCOL),
314 MAX_UINTN
315 },
316 InternalVarCheckDevicePath
317 },
318 {
319 EFI_CON_IN_DEV_VARIABLE_NAME,
320 {
321 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
322 0,
323 VARIABLE_ATTRIBUTE_BS_RT,
324 sizeof (EFI_DEVICE_PATH_PROTOCOL),
325 MAX_UINTN
326 },
327 InternalVarCheckDevicePath
328 },
329 {
330 EFI_CON_OUT_DEV_VARIABLE_NAME,
331 {
332 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
333 0,
334 VARIABLE_ATTRIBUTE_BS_RT,
335 sizeof (EFI_DEVICE_PATH_PROTOCOL),
336 MAX_UINTN
337 },
338 InternalVarCheckDevicePath
339 },
340 {
341 EFI_ERR_OUT_DEV_VARIABLE_NAME,
342 {
343 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
344 0,
345 VARIABLE_ATTRIBUTE_BS_RT,
346 sizeof (EFI_DEVICE_PATH_PROTOCOL),
347 MAX_UINTN
348 },
349 InternalVarCheckDevicePath
350 },
351 {
352 EFI_BOOT_ORDER_VARIABLE_NAME,
353 {
354 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
355 0,
356 VARIABLE_ATTRIBUTE_NV_BS_RT,
357 sizeof (UINT16),
358 MAX_UINTN
359 },
360 InternalVarCheckSizeArray
361 },
362 {
363 EFI_BOOT_NEXT_VARIABLE_NAME,
364 {
365 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
366 0,
367 VARIABLE_ATTRIBUTE_NV_BS_RT,
368 sizeof (UINT16),
369 sizeof (UINT16)
370 },
371 NULL
372 },
373 {
374 EFI_BOOT_CURRENT_VARIABLE_NAME,
375 {
376 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
377 0,
378 VARIABLE_ATTRIBUTE_BS_RT,
379 sizeof (UINT16),
380 sizeof (UINT16)
381 },
382 NULL
383 },
384 {
385 EFI_BOOT_OPTION_SUPPORT_VARIABLE_NAME,
386 {
387 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
388 0,
389 VARIABLE_ATTRIBUTE_BS_RT,
390 sizeof (UINT32),
391 sizeof (UINT32)
392 },
393 NULL
394 },
395 {
396 EFI_DRIVER_ORDER_VARIABLE_NAME,
397 {
398 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
399 0,
400 VARIABLE_ATTRIBUTE_NV_BS_RT,
401 sizeof (UINT16),
402 MAX_UINTN
403 },
404 InternalVarCheckSizeArray
405 },
406 {
407 EFI_HW_ERR_REC_SUPPORT_VARIABLE_NAME,
408 {
409 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
410 0,
411 VARIABLE_ATTRIBUTE_NV_BS_RT,
412 sizeof (UINT16),
413 sizeof (UINT16)
414 },
415 NULL
416 },
417 {
418 EFI_SETUP_MODE_NAME,
419 {
420 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
421 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
422 VARIABLE_ATTRIBUTE_BS_RT,
423 sizeof (UINT8),
424 sizeof (UINT8)
425 },
426 NULL
427 },
428 {
429 EFI_KEY_EXCHANGE_KEY_NAME,
430 {
431 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
432 0,
433 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
434 1,
435 MAX_UINTN
436 },
437 NULL
438 },
439 {
440 EFI_PLATFORM_KEY_NAME,
441 {
442 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
443 0,
444 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
445 1,
446 MAX_UINTN
447 },
448 NULL
449 },
450 {
451 EFI_SIGNATURE_SUPPORT_NAME,
452 {
453 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
454 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
455 VARIABLE_ATTRIBUTE_BS_RT,
456 sizeof (EFI_GUID),
457 MAX_UINTN
458 },
459 InternalVarCheckSizeArray
460 },
461 {
462 EFI_SECURE_BOOT_MODE_NAME,
463 {
464 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
465 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
466 VARIABLE_ATTRIBUTE_BS_RT,
467 sizeof (UINT8),
468 sizeof (UINT8)
469 },
470 NULL
471 },
472 {
473 EFI_KEK_DEFAULT_VARIABLE_NAME,
474 {
475 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
476 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
477 VARIABLE_ATTRIBUTE_BS_RT,
478 1,
479 MAX_UINTN
480 },
481 NULL
482 },
483 {
484 EFI_PK_DEFAULT_VARIABLE_NAME,
485 {
486 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
487 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
488 VARIABLE_ATTRIBUTE_BS_RT,
489 1,
490 MAX_UINTN
491 },
492 NULL
493 },
494 {
495 EFI_DB_DEFAULT_VARIABLE_NAME,
496 {
497 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
498 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
499 VARIABLE_ATTRIBUTE_BS_RT,
500 1,
501 MAX_UINTN
502 },
503 NULL
504 },
505 {
506 EFI_DBX_DEFAULT_VARIABLE_NAME,
507 {
508 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
509 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
510 VARIABLE_ATTRIBUTE_BS_RT,
511 1,
512 MAX_UINTN
513 },
514 NULL
515 },
516 {
517 EFI_DBT_DEFAULT_VARIABLE_NAME,
518 {
519 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
520 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
521 VARIABLE_ATTRIBUTE_BS_RT,
522 1,
523 MAX_UINTN
524 },
525 NULL
526 },
527 {
528 EFI_OS_INDICATIONS_SUPPORT_VARIABLE_NAME,
529 {
530 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
531 0,
532 VARIABLE_ATTRIBUTE_BS_RT,
533 sizeof (UINT64),
534 sizeof (UINT64)
535 },
536 NULL
537 },
538 {
539 EFI_OS_INDICATIONS_VARIABLE_NAME,
540 {
541 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
542 0,
543 VARIABLE_ATTRIBUTE_NV_BS_RT,
544 sizeof (UINT64),
545 sizeof (UINT64)
546 },
547 NULL
548 },
549 {
550 EFI_VENDOR_KEYS_VARIABLE_NAME,
551 {
552 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
553 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
554 VARIABLE_ATTRIBUTE_BS_RT,
555 sizeof (UINT8),
556 sizeof (UINT8)
557 },
558 NULL
559 },
560 };
561 UEFI_DEFINED_VARIABLE_ENTRY mGlobalVariableList2[] = {
562 {
563 L"Boot####",
564 {
565 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
566 0,
567 VARIABLE_ATTRIBUTE_NV_BS_RT,
568 sizeof (UINT32) + sizeof (UINT16),
569 MAX_UINTN
570 },
571 InternalVarCheckLoadOption
572 },
573 {
574 L"Driver####",
575 {
576 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
577 0,
578 VARIABLE_ATTRIBUTE_NV_BS_RT,
579 sizeof (UINT32) + sizeof (UINT16),
580 MAX_UINTN
581 },
582 InternalVarCheckLoadOption
583 },
584 {
585 L"Key####",
586 {
587 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
588 0,
589 VARIABLE_ATTRIBUTE_NV_BS_RT,
590 sizeof (EFI_KEY_OPTION),
591 sizeof (EFI_KEY_OPTION) + 3 * sizeof (EFI_INPUT_KEY)
592 },
593 InternalVarCheckKeyOption
594 },
595 };
596
597 //
598 // EFI_IMAGE_SECURITY_DATABASE_GUID
599 //
600 UEFI_DEFINED_VARIABLE_ENTRY mImageSecurityVariableList[] = {
601 {
602 EFI_IMAGE_SECURITY_DATABASE,
603 {
604 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
605 0,
606 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
607 1,
608 MAX_UINTN
609 },
610 NULL
611 },
612 {
613 EFI_IMAGE_SECURITY_DATABASE1,
614 {
615 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
616 0,
617 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
618 1,
619 MAX_UINTN
620 },
621 NULL
622 },
623 {
624 EFI_IMAGE_SECURITY_DATABASE2,
625 {
626 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
627 0,
628 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
629 1,
630 MAX_UINTN
631 },
632 NULL
633 },
634 };
635
636 typedef struct {
637 EFI_GUID *Guid;
638 CHAR16 *Name;
639 VAR_CHECK_VARIABLE_PROPERTY VariableProperty;
640 INTERNAL_VAR_CHECK_FUNCTION CheckFunction;
641 } VARIABLE_DRIVER_VARIABLE_ENTRY;
642
643 VARIABLE_DRIVER_VARIABLE_ENTRY mVariableDriverVariableList[] = {
644 {
645 &gEfiSecureBootEnableDisableGuid,
646 EFI_SECURE_BOOT_ENABLE_NAME,
647 {
648 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
649 0,
650 VARIABLE_ATTRIBUTE_NV_BS,
651 sizeof (UINT8),
652 sizeof (UINT8)
653 }
654 },
655 {
656 &gEfiCustomModeEnableGuid,
657 EFI_CUSTOM_MODE_NAME,
658 {
659 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
660 0,
661 VARIABLE_ATTRIBUTE_NV_BS,
662 sizeof (UINT8),
663 sizeof (UINT8)
664 }
665 },
666 {
667 &gEfiVendorKeysNvGuid,
668 EFI_VENDOR_KEYS_NV_VARIABLE_NAME,
669 {
670 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
671 0,
672 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
673 sizeof (UINT8),
674 sizeof (UINT8)
675 }
676 },
677 {
678 &gEfiAuthenticatedVariableGuid,
679 L"AuthVarKeyDatabase",
680 {
681 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
682 0,
683 VARIABLE_ATTRIBUTE_NV_BS_RT_AW,
684 sizeof (UINT8),
685 MAX_UINTN
686 }
687 },
688 {
689 &gEfiCertDbGuid,
690 L"certdb",
691 {
692 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
693 0,
694 VARIABLE_ATTRIBUTE_NV_BS_RT_AT,
695 sizeof (UINT32),
696 MAX_UINTN
697 }
698 },
699 {
700 &gEdkiiVarErrorFlagGuid,
701 VAR_ERROR_FLAG_NAME,
702 {
703 VAR_CHECK_VARIABLE_PROPERTY_REVISION,
704 VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY,
705 VARIABLE_ATTRIBUTE_NV_BS_RT,
706 sizeof (VAR_ERROR_FLAG),
707 sizeof (VAR_ERROR_FLAG)
708 }
709 },
710 };
711
712 /**
713 Get UEFI defined global variable or image security database variable property.
714 The code will check if variable guid is global variable or image security database guid first.
715 If yes, further check if variable name is in mGlobalVariableList, mGlobalVariableList2 or mImageSecurityVariableList.
716
717 @param[in] VariableName Pointer to variable name.
718 @param[in] VendorGuid Variable Vendor Guid.
719 @param[in] WildcardMatch Try wildcard match or not.
720 @param[out] VariableProperty Pointer to variable property.
721 @param[out] VarCheckFunction Pointer to check function.
722
723 @retval EFI_SUCCESS Variable is not global variable or image security database variable.
724 @retval EFI_INVALID_PARAMETER Variable is global variable or image security database variable, but variable name is not in the lists.
725
726 **/
727 EFI_STATUS
728 GetUefiDefinedVariableProperty (
729 IN CHAR16 *VariableName,
730 IN EFI_GUID *VendorGuid,
731 IN BOOLEAN WildcardMatch,
732 OUT VAR_CHECK_VARIABLE_PROPERTY **VariableProperty,
733 OUT INTERNAL_VAR_CHECK_FUNCTION *VarCheckFunction OPTIONAL
734 )
735 {
736 UINTN Index;
737 UINTN NameLength;
738
739 if (CompareGuid (VendorGuid, &gEfiGlobalVariableGuid)) {
740 //
741 // Try list 1, exactly match.
742 //
743 for (Index = 0; Index < sizeof (mGlobalVariableList)/sizeof (mGlobalVariableList[0]); Index++) {
744 if (StrCmp (mGlobalVariableList[Index].Name, VariableName) == 0) {
745 if (VarCheckFunction != NULL) {
746 *VarCheckFunction = mGlobalVariableList[Index].CheckFunction;
747 }
748 *VariableProperty = &mGlobalVariableList[Index].VariableProperty;
749 return EFI_SUCCESS;
750 }
751 }
752
753 //
754 // Try list 2.
755 //
756 NameLength = StrLen (VariableName) - 4;
757 for (Index = 0; Index < sizeof (mGlobalVariableList2)/sizeof (mGlobalVariableList2[0]); Index++) {
758 if (WildcardMatch) {
759 if ((StrLen (VariableName) == StrLen (mGlobalVariableList2[Index].Name)) &&
760 (StrnCmp (mGlobalVariableList2[Index].Name, VariableName, NameLength) == 0) &&
761 IsHexaDecimalDigitCharacter (VariableName[NameLength]) &&
762 IsHexaDecimalDigitCharacter (VariableName[NameLength + 1]) &&
763 IsHexaDecimalDigitCharacter (VariableName[NameLength + 2]) &&
764 IsHexaDecimalDigitCharacter (VariableName[NameLength + 3])) {
765 if (VarCheckFunction != NULL) {
766 *VarCheckFunction = mGlobalVariableList2[Index].CheckFunction;
767 }
768 *VariableProperty = &mGlobalVariableList2[Index].VariableProperty;
769 return EFI_SUCCESS;
770 }
771 }
772 if (StrCmp (mGlobalVariableList2[Index].Name, VariableName) == 0) {
773 if (VarCheckFunction != NULL) {
774 *VarCheckFunction = mGlobalVariableList2[Index].CheckFunction;
775 }
776 *VariableProperty = &mGlobalVariableList2[Index].VariableProperty;
777 return EFI_SUCCESS;
778 }
779 }
780
781 //
782 // The variable name is not in the lists.
783 //
784 return EFI_INVALID_PARAMETER;
785 }
786
787 if (CompareGuid (VendorGuid, &gEfiImageSecurityDatabaseGuid)){
788 for (Index = 0; Index < sizeof (mImageSecurityVariableList)/sizeof (mImageSecurityVariableList[0]); Index++) {
789 if (StrCmp (mImageSecurityVariableList[Index].Name, VariableName) == 0) {
790 if (VarCheckFunction != NULL) {
791 *VarCheckFunction = mImageSecurityVariableList[Index].CheckFunction;
792 }
793 *VariableProperty = &mImageSecurityVariableList[Index].VariableProperty;
794 return EFI_SUCCESS;
795 }
796 }
797
798 return EFI_INVALID_PARAMETER;
799 }
800
801 //
802 // It is not global variable or image security database variable.
803 //
804 return EFI_SUCCESS;
805 }
806
807 /**
808 Get variable property for variables managed by Varaible driver.
809
810 @param[in] VariableName Pointer to variable name.
811 @param[in] VendorGuid Variable Vendor Guid.
812
813 @return Pointer to variable property.
814
815 **/
816 VAR_CHECK_VARIABLE_PROPERTY *
817 GetVariableDriverVariableProperty (
818 IN CHAR16 *VariableName,
819 IN EFI_GUID *VendorGuid
820 )
821 {
822 UINTN Index;
823
824 for (Index = 0; Index < sizeof (mVariableDriverVariableList)/sizeof (mVariableDriverVariableList[0]); Index++) {
825 if ((CompareGuid (mVariableDriverVariableList[Index].Guid, VendorGuid)) && (StrCmp (mVariableDriverVariableList[Index].Name, VariableName) == 0)) {
826 return &mVariableDriverVariableList[Index].VariableProperty;
827 }
828 }
829
830 return NULL;
831 }
832
833 /**
834 Internal SetVariable check.
835
836 @param[in] VariableName Name of Variable to set.
837 @param[in] VendorGuid Variable vendor GUID.
838 @param[in] Attributes Attribute value of the variable.
839 @param[in] DataSize Size of Data to set.
840 @param[in] Data Data pointer.
841
842 @retval EFI_SUCCESS The SetVariable check result was success.
843 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits, name, and GUID was supplied,
844 or the DataSize exceeds the minimum or maximum allowed,
845 or the Data value is not following UEFI spec for UEFI defined variables.
846 @retval EFI_WRITE_PROTECTED The variable in question is read-only.
847 @retval Others The return status from check handler.
848
849 **/
850 EFI_STATUS
851 EFIAPI
852 InternalVarCheckSetVariableCheck (
853 IN CHAR16 *VariableName,
854 IN EFI_GUID *VendorGuid,
855 IN UINT32 Attributes,
856 IN UINTN DataSize,
857 IN VOID *Data
858 )
859 {
860 EFI_STATUS Status;
861 UINTN Index;
862 LIST_ENTRY *Link;
863 VAR_CHECK_VARIABLE_ENTRY *Entry;
864 CHAR16 *Name;
865 VAR_CHECK_VARIABLE_PROPERTY *Property;
866 INTERNAL_VAR_CHECK_FUNCTION VarCheckFunction;
867
868 if (!mEndOfDxe) {
869 //
870 // Only do check after End Of Dxe.
871 //
872 return EFI_SUCCESS;
873 }
874
875 Property = NULL;
876 VarCheckFunction = NULL;
877
878 for ( Link = GetFirstNode (&mVarCheckVariableList)
879 ; !IsNull (&mVarCheckVariableList, Link)
880 ; Link = GetNextNode (&mVarCheckVariableList, Link)
881 ) {
882 Entry = BASE_CR (Link, VAR_CHECK_VARIABLE_ENTRY, Link);
883 Name = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));
884 if (CompareGuid (&Entry->Guid, VendorGuid) && (StrCmp (Name, VariableName) == 0)) {
885 Property = &Entry->VariableProperty;
886 break;
887 }
888 }
889 if (Property == NULL) {
890 Property = GetVariableDriverVariableProperty (VariableName, VendorGuid);
891 }
892 if (Property == NULL) {
893 Status = GetUefiDefinedVariableProperty (VariableName, VendorGuid, TRUE, &Property, &VarCheckFunction);
894 if (EFI_ERROR (Status)) {
895 DEBUG ((EFI_D_INFO, "[Variable]: Var Check UEFI defined variable fail %r - %g:%s\n", Status, VendorGuid, VariableName));
896 return Status;
897 }
898 }
899 if (Property != NULL) {
900 if (mEnableLocking && ((Property->Property & VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY) != 0)) {
901 DEBUG ((EFI_D_INFO, "[Variable]: Var Check ReadOnly variable fail %r - %g:%s\n", EFI_WRITE_PROTECTED, VendorGuid, VariableName));
902 return EFI_WRITE_PROTECTED;
903 }
904 if (!((((Attributes & EFI_VARIABLE_APPEND_WRITE) == 0) && (DataSize == 0)) || (Attributes == 0))) {
905 //
906 // Not to delete variable.
907 //
908 if ((Attributes & (~EFI_VARIABLE_APPEND_WRITE)) != Property->Attributes) {
909 DEBUG ((EFI_D_INFO, "[Variable]: Var Check Attributes(0x%08x to 0x%08x) fail %r - %g:%s\n", Property->Attributes, Attributes, EFI_INVALID_PARAMETER, VendorGuid, VariableName));
910 return EFI_INVALID_PARAMETER;
911 }
912 if (DataSize != 0) {
913 if ((DataSize < Property->MinSize) || (DataSize > Property->MaxSize)) {
914 DEBUG ((EFI_D_INFO, "[Variable]: Var Check DataSize fail(0x%x not in 0x%x - 0x%x) %r - %g:%s\n", DataSize, Property->MinSize, Property->MaxSize, EFI_INVALID_PARAMETER, VendorGuid, VariableName));
915 return EFI_INVALID_PARAMETER;
916 }
917 if (VarCheckFunction != NULL) {
918 Status = VarCheckFunction (
919 Property,
920 DataSize,
921 Data
922 );
923 if (EFI_ERROR (Status)) {
924 DEBUG ((EFI_D_INFO, "[Variable]: Internal Var Check function fail %r - %g:%s\n", Status, VendorGuid, VariableName));
925 return Status;
926 }
927 }
928 }
929 }
930 }
931
932 for (Index = 0; Index < mNumberOfHandler; Index ++) {
933 Status = mHandlerTable[Index] (
934 VariableName,
935 VendorGuid,
936 Attributes,
937 DataSize,
938 Data
939 );
940 if (EFI_ERROR (Status)) {
941 DEBUG ((EFI_D_INFO, "[Variable]: Var Check handler fail %r - %g:%s\n", Status, VendorGuid, VariableName));
942 return Status;
943 }
944 }
945 return EFI_SUCCESS;
946 }
947
948 /**
949 Reallocates more global memory to store the registered handler list.
950
951 @retval RETURN_SUCCESS Reallocate memory successfully.
952 @retval RETURN_OUT_OF_RESOURCES No enough memory to allocate.
953
954 **/
955 RETURN_STATUS
956 EFIAPI
957 ReallocateHandlerTable (
958 VOID
959 )
960 {
961 VAR_CHECK_SET_VARIABLE_CHECK_HANDLER *HandlerTable;
962
963 //
964 // Reallocate memory for check handler table.
965 //
966 HandlerTable = ReallocateRuntimePool (
967 mMaxNumberOfHandler * sizeof (VAR_CHECK_SET_VARIABLE_CHECK_HANDLER),
968 (mMaxNumberOfHandler + VAR_CHECK_HANDLER_TABLE_SIZE) * sizeof (VAR_CHECK_SET_VARIABLE_CHECK_HANDLER),
969 mHandlerTable
970 );
971
972 //
973 // No enough resource to allocate.
974 //
975 if (HandlerTable == NULL) {
976 return RETURN_OUT_OF_RESOURCES;
977 }
978
979 mHandlerTable = HandlerTable;
980 //
981 // Increase max handler number.
982 //
983 mMaxNumberOfHandler = mMaxNumberOfHandler + VAR_CHECK_HANDLER_TABLE_SIZE;
984 return RETURN_SUCCESS;
985 }
986
987 /**
988 Register SetVariable check handler.
989
990 @param[in] Handler Pointer to check handler.
991
992 @retval EFI_SUCCESS The SetVariable check handler was registered successfully.
993 @retval EFI_INVALID_PARAMETER Handler is NULL.
994 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
995 already been signaled.
996 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the SetVariable check handler register request.
997 @retval EFI_UNSUPPORTED This interface is not implemented.
998 For example, it is unsupported in VarCheck protocol if both VarCheck and SmmVarCheck protocols are present.
999
1000 **/
1001 EFI_STATUS
1002 EFIAPI
1003 VarCheckRegisterSetVariableCheckHandler (
1004 IN VAR_CHECK_SET_VARIABLE_CHECK_HANDLER Handler
1005 )
1006 {
1007 EFI_STATUS Status;
1008
1009 if (Handler == NULL) {
1010 return EFI_INVALID_PARAMETER;
1011 }
1012
1013 if (mEndOfDxe) {
1014 return EFI_ACCESS_DENIED;
1015 }
1016
1017 DEBUG ((EFI_D_INFO, "RegisterSetVariableCheckHandler - 0x%x\n", Handler));
1018
1019 //
1020 // Check whether the handler list is enough to store new handler.
1021 //
1022 if (mNumberOfHandler == mMaxNumberOfHandler) {
1023 //
1024 // Allocate more resources for new handler.
1025 //
1026 Status = ReallocateHandlerTable();
1027 if (EFI_ERROR (Status)) {
1028 return Status;
1029 }
1030 }
1031
1032 //
1033 // Register new handler into the handler list.
1034 //
1035 mHandlerTable[mNumberOfHandler] = Handler;
1036 mNumberOfHandler ++;
1037
1038 return EFI_SUCCESS;
1039 }
1040
1041 /**
1042 Variable property get function.
1043
1044 @param[in] Name Pointer to the variable name.
1045 @param[in] Guid Pointer to the vendor GUID.
1046 @param[in] WildcardMatch Try wildcard match or not.
1047
1048 @return Pointer to the property of variable specified by the Name and Guid.
1049
1050 **/
1051 VAR_CHECK_VARIABLE_PROPERTY *
1052 VariablePropertyGetFunction (
1053 IN CHAR16 *Name,
1054 IN EFI_GUID *Guid,
1055 IN BOOLEAN WildcardMatch
1056 )
1057 {
1058 LIST_ENTRY *Link;
1059 VAR_CHECK_VARIABLE_ENTRY *Entry;
1060 CHAR16 *VariableName;
1061 VAR_CHECK_VARIABLE_PROPERTY *Property;
1062
1063 for ( Link = GetFirstNode (&mVarCheckVariableList)
1064 ; !IsNull (&mVarCheckVariableList, Link)
1065 ; Link = GetNextNode (&mVarCheckVariableList, Link)
1066 ) {
1067 Entry = BASE_CR (Link, VAR_CHECK_VARIABLE_ENTRY, Link);
1068 VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));
1069 if (CompareGuid (&Entry->Guid, Guid) && (StrCmp (VariableName, Name) == 0)) {
1070 return &Entry->VariableProperty;
1071 }
1072 }
1073
1074 Property = GetVariableDriverVariableProperty (Name, Guid);
1075 if (Property == NULL) {
1076 GetUefiDefinedVariableProperty (Name, Guid, WildcardMatch, &Property, NULL);
1077 }
1078
1079 return Property;
1080 }
1081
1082 /**
1083 Variable property set.
1084
1085 @param[in] Name Pointer to the variable name.
1086 @param[in] Guid Pointer to the vendor GUID.
1087 @param[in] VariableProperty Pointer to the input variable property.
1088
1089 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was set successfully.
1090 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string,
1091 or the fields of VariableProperty are not valid.
1092 @retval EFI_ACCESS_DENIED EFI_END_OF_DXE_EVENT_GROUP_GUID or EFI_EVENT_GROUP_READY_TO_BOOT has
1093 already been signaled.
1094 @retval EFI_OUT_OF_RESOURCES There is not enough resource for the variable property set request.
1095
1096 **/
1097 EFI_STATUS
1098 EFIAPI
1099 VarCheckVariablePropertySet (
1100 IN CHAR16 *Name,
1101 IN EFI_GUID *Guid,
1102 IN VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
1103 )
1104 {
1105 EFI_STATUS Status;
1106 VAR_CHECK_VARIABLE_ENTRY *Entry;
1107 CHAR16 *VariableName;
1108 VAR_CHECK_VARIABLE_PROPERTY *Property;
1109
1110 if (Name == NULL || Name[0] == 0 || Guid == NULL) {
1111 return EFI_INVALID_PARAMETER;
1112 }
1113
1114 if (VariableProperty == NULL) {
1115 return EFI_INVALID_PARAMETER;
1116 }
1117
1118 if (VariableProperty->Revision != VAR_CHECK_VARIABLE_PROPERTY_REVISION) {
1119 return EFI_INVALID_PARAMETER;
1120 }
1121
1122 if (mEndOfDxe) {
1123 return EFI_ACCESS_DENIED;
1124 }
1125
1126 Status = EFI_SUCCESS;
1127
1128 AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1129
1130 Property = VariablePropertyGetFunction (Name, Guid, FALSE);
1131 if (Property != NULL) {
1132 CopyMem (Property, VariableProperty, sizeof (*VariableProperty));
1133 } else {
1134 Entry = AllocateRuntimeZeroPool (sizeof (*Entry) + StrSize (Name));
1135 if (Entry == NULL) {
1136 Status = EFI_OUT_OF_RESOURCES;
1137 goto Done;
1138 }
1139 VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));
1140 StrnCpy (VariableName, Name, StrLen (Name));
1141 CopyGuid (&Entry->Guid, Guid);
1142 CopyMem (&Entry->VariableProperty, VariableProperty, sizeof (*VariableProperty));
1143 InsertTailList (&mVarCheckVariableList, &Entry->Link);
1144 }
1145
1146 Done:
1147 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1148
1149 return Status;
1150 }
1151
1152 /**
1153 Internal variable property get.
1154
1155 @param[in] Name Pointer to the variable name.
1156 @param[in] Guid Pointer to the vendor GUID.
1157 @param[out] VariableProperty Pointer to the output variable property.
1158
1159 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully.
1160 @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found.
1161
1162 **/
1163 EFI_STATUS
1164 EFIAPI
1165 InternalVarCheckVariablePropertyGet (
1166 IN CHAR16 *Name,
1167 IN EFI_GUID *Guid,
1168 OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
1169 )
1170 {
1171 LIST_ENTRY *Link;
1172 VARIABLE_ENTRY *Entry;
1173 CHAR16 *VariableName;
1174 BOOLEAN Found;
1175 VAR_CHECK_VARIABLE_PROPERTY *Property;
1176
1177 Found = FALSE;
1178
1179 Property = VariablePropertyGetFunction (Name, Guid, TRUE);
1180 if (Property != NULL) {
1181 CopyMem (VariableProperty, Property, sizeof (*VariableProperty));
1182 Found = TRUE;
1183 }
1184
1185 for ( Link = GetFirstNode (&mLockedVariableList)
1186 ; !IsNull (&mLockedVariableList, Link)
1187 ; Link = GetNextNode (&mLockedVariableList, Link)
1188 ) {
1189 Entry = BASE_CR (Link, VARIABLE_ENTRY, Link);
1190 VariableName = (CHAR16 *) ((UINTN) Entry + sizeof (*Entry));
1191 if (CompareGuid (&Entry->Guid, Guid) && (StrCmp (VariableName, Name) == 0)) {
1192 VariableProperty->Property |= VAR_CHECK_VARIABLE_PROPERTY_READ_ONLY;
1193 if (!Found) {
1194 VariableProperty->Revision = VAR_CHECK_VARIABLE_PROPERTY_REVISION;
1195 Found = TRUE;
1196 }
1197 }
1198 }
1199
1200 return (Found ? EFI_SUCCESS : EFI_NOT_FOUND);
1201 }
1202
1203 /**
1204 Variable property get.
1205
1206 @param[in] Name Pointer to the variable name.
1207 @param[in] Guid Pointer to the vendor GUID.
1208 @param[out] VariableProperty Pointer to the output variable property.
1209
1210 @retval EFI_SUCCESS The property of variable specified by the Name and Guid was got successfully.
1211 @retval EFI_INVALID_PARAMETER Name, Guid or VariableProperty is NULL, or Name is an empty string.
1212 @retval EFI_NOT_FOUND The property of variable specified by the Name and Guid was not found.
1213
1214 **/
1215 EFI_STATUS
1216 EFIAPI
1217 VarCheckVariablePropertyGet (
1218 IN CHAR16 *Name,
1219 IN EFI_GUID *Guid,
1220 OUT VAR_CHECK_VARIABLE_PROPERTY *VariableProperty
1221 )
1222 {
1223 EFI_STATUS Status;
1224
1225 if (Name == NULL || Name[0] == 0 || Guid == NULL) {
1226 return EFI_INVALID_PARAMETER;
1227 }
1228
1229 if (VariableProperty == NULL) {
1230 return EFI_INVALID_PARAMETER;
1231 }
1232
1233 AcquireLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1234
1235 Status = InternalVarCheckVariablePropertyGet (Name, Guid, VariableProperty);
1236
1237 ReleaseLockOnlyAtBootTime (&mVariableModuleGlobal->VariableGlobal.VariableServicesLock);
1238
1239 return Status;
1240 }
1241