2 Measure TCG required variable.
4 Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>
5 SPDX-License-Identifier: BSD-2-Clause-Patent
10 #include <Guid/ImageAuthentication.h>
11 #include <IndustryStandard/UefiTcgPlatform.h>
13 #include <Library/UefiBootServicesTableLib.h>
14 #include <Library/UefiRuntimeServicesTableLib.h>
15 #include <Library/MemoryAllocationLib.h>
16 #include <Library/BaseMemoryLib.h>
17 #include <Library/DebugLib.h>
18 #include <Library/BaseLib.h>
19 #include <Library/TpmMeasurementLib.h>
21 #include "PrivilegePolymorphic.h"
28 VARIABLE_TYPE mVariableType
[] = {
29 {EFI_SECURE_BOOT_MODE_NAME
, &gEfiGlobalVariableGuid
},
30 {EFI_PLATFORM_KEY_NAME
, &gEfiGlobalVariableGuid
},
31 {EFI_KEY_EXCHANGE_KEY_NAME
, &gEfiGlobalVariableGuid
},
32 {EFI_IMAGE_SECURITY_DATABASE
, &gEfiImageSecurityDatabaseGuid
},
33 {EFI_IMAGE_SECURITY_DATABASE1
, &gEfiImageSecurityDatabaseGuid
},
34 {EFI_IMAGE_SECURITY_DATABASE2
, &gEfiImageSecurityDatabaseGuid
},
38 // "SecureBoot" may update following PK Del/Add
39 // Cache its value to detect value update
41 UINT8
*mSecureBootVarData
= NULL
;
42 UINTN mSecureBootVarDataSize
= 0;
45 This function will return if this variable is SecureBootPolicy Variable.
47 @param[in] VariableName A Null-terminated string that is the name of the vendor's variable.
48 @param[in] VendorGuid A unique identifier for the vendor.
50 @retval TRUE This is SecureBootPolicy Variable
51 @retval FALSE This is not SecureBootPolicy Variable
54 IsSecureBootPolicyVariable (
55 IN CHAR16
*VariableName
,
56 IN EFI_GUID
*VendorGuid
61 for (Index
= 0; Index
< sizeof(mVariableType
)/sizeof(mVariableType
[0]); Index
++) {
62 if ((StrCmp (VariableName
, mVariableType
[Index
].VariableName
) == 0) &&
63 (CompareGuid (VendorGuid
, mVariableType
[Index
].VendorGuid
))) {
71 Measure and log an EFI variable, and extend the measurement result into a specific PCR.
73 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.
74 @param[in] VendorGuid A unique identifier for the vendor.
75 @param[in] VarData The content of the variable data.
76 @param[in] VarSize The size of the variable data.
78 @retval EFI_SUCCESS Operation completed successfully.
79 @retval EFI_OUT_OF_RESOURCES Out of memory.
80 @retval EFI_DEVICE_ERROR The operation was unsuccessful.
87 IN EFI_GUID
*VendorGuid
,
94 UEFI_VARIABLE_DATA
*VarLog
;
97 ASSERT ((VarSize
== 0 && VarData
== NULL
) || (VarSize
!= 0 && VarData
!= NULL
));
99 VarNameLength
= StrLen (VarName
);
100 VarLogSize
= (UINT32
)(sizeof (*VarLog
) + VarNameLength
* sizeof (*VarName
) + VarSize
101 - sizeof (VarLog
->UnicodeName
) - sizeof (VarLog
->VariableData
));
103 VarLog
= (UEFI_VARIABLE_DATA
*) AllocateZeroPool (VarLogSize
);
104 if (VarLog
== NULL
) {
105 return EFI_OUT_OF_RESOURCES
;
108 CopyMem (&VarLog
->VariableName
, VendorGuid
, sizeof(VarLog
->VariableName
));
109 VarLog
->UnicodeNameLength
= VarNameLength
;
110 VarLog
->VariableDataLength
= VarSize
;
114 VarNameLength
* sizeof (*VarName
)
118 (CHAR16
*)VarLog
->UnicodeName
+ VarNameLength
,
124 DEBUG ((EFI_D_INFO
, "VariableDxe: MeasureVariable (Pcr - %x, EventType - %x, ", (UINTN
)7, (UINTN
)EV_EFI_VARIABLE_DRIVER_CONFIG
));
125 DEBUG ((EFI_D_INFO
, "VariableName - %s, VendorGuid - %g)\n", VarName
, VendorGuid
));
127 Status
= TpmMeasureAndLogData (
129 EV_EFI_VARIABLE_DRIVER_CONFIG
,
140 Returns the status whether get the variable success. The function retrieves
141 variable through the UEFI Runtime Service GetVariable(). The
142 returned buffer is allocated using AllocatePool(). The caller is responsible
143 for freeing this buffer with FreePool().
145 This API is only invoked in boot time. It may NOT be invoked at runtime.
147 @param[in] Name The pointer to a Null-terminated Unicode string.
148 @param[in] Guid The pointer to an EFI_GUID structure
149 @param[out] Value The buffer point saved the variable info.
150 @param[out] Size The buffer size of the variable.
152 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.
153 @return EFI_SUCCESS Find the specified variable.
154 @return Others Errors Return errors from call to gRT->GetVariable.
158 InternalGetVariable (
159 IN CONST CHAR16
*Name
,
160 IN CONST EFI_GUID
*Guid
,
169 // Try to get the variable size.
177 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &BufferSize
, *Value
);
178 if (Status
!= EFI_BUFFER_TOO_SMALL
) {
183 // Allocate buffer to get the variable.
185 *Value
= AllocatePool (BufferSize
);
186 ASSERT (*Value
!= NULL
);
187 if (*Value
== NULL
) {
188 return EFI_OUT_OF_RESOURCES
;
192 // Get the variable data.
194 Status
= gRT
->GetVariable ((CHAR16
*) Name
, (EFI_GUID
*) Guid
, NULL
, &BufferSize
, *Value
);
195 if (EFI_ERROR (Status
)) {
208 SecureBoot Hook for SetVariable.
210 @param[in] VariableName Name of Variable to be found.
211 @param[in] VendorGuid Variable vendor GUID.
217 IN CHAR16
*VariableName
,
218 IN EFI_GUID
*VendorGuid
222 UINTN VariableDataSize
;
225 if (!IsSecureBootPolicyVariable (VariableName
, VendorGuid
)) {
230 // We should NOT use Data and DataSize here,because it may include signature,
231 // or is just partial with append attributes, or is deleted.
232 // We should GetVariable again, to get full variable content.
234 Status
= InternalGetVariable (
240 if (EFI_ERROR (Status
)) {
242 // Measure DBT only if present and not empty
244 if (StrCmp (VariableName
, EFI_IMAGE_SECURITY_DATABASE2
) == 0 &&
245 CompareGuid (VendorGuid
, &gEfiImageSecurityDatabaseGuid
)) {
246 DEBUG((DEBUG_INFO
, "Skip measuring variable %s since it's deleted\n", EFI_IMAGE_SECURITY_DATABASE2
));
250 VariableDataSize
= 0;
254 Status
= MeasureVariable (
260 DEBUG ((EFI_D_INFO
, "MeasureBootPolicyVariable - %r\n", Status
));
262 if (VariableData
!= NULL
) {
263 FreePool (VariableData
);
267 // "SecureBoot" is 8bit & read-only. It can only be changed according to PK update
269 if ((StrCmp (VariableName
, EFI_PLATFORM_KEY_NAME
) == 0) &&
270 CompareGuid (VendorGuid
, &gEfiGlobalVariableGuid
)) {
271 Status
= InternalGetVariable (
272 EFI_SECURE_BOOT_MODE_NAME
,
273 &gEfiGlobalVariableGuid
,
277 if (EFI_ERROR (Status
)) {
282 // If PK update is successful. "SecureBoot" shall always exist ever since variable write service is ready
284 ASSERT(mSecureBootVarData
!= NULL
);
286 if (CompareMem(mSecureBootVarData
, VariableData
, VariableDataSize
) != 0) {
287 FreePool(mSecureBootVarData
);
288 mSecureBootVarData
= VariableData
;
289 mSecureBootVarDataSize
= VariableDataSize
;
291 DEBUG((DEBUG_INFO
, "%s variable updated according to PK change. Remeasure the value!\n", EFI_SECURE_BOOT_MODE_NAME
));
292 Status
= MeasureVariable (
293 EFI_SECURE_BOOT_MODE_NAME
,
294 &gEfiGlobalVariableGuid
,
296 mSecureBootVarDataSize
298 DEBUG ((DEBUG_INFO
, "MeasureBootPolicyVariable - %r\n", Status
));
301 // "SecureBoot" variable is not changed
303 FreePool(VariableData
);
311 Some Secure Boot Policy Variable may update following other variable changes(SecureBoot follows PK change, etc).
312 Record their initial State when variable write service is ready.
317 RecordSecureBootPolicyVarData(
324 // Record initial "SecureBoot" variable value.
325 // It is used to detect SecureBoot variable change in SecureBootHook.
327 Status
= InternalGetVariable (
328 EFI_SECURE_BOOT_MODE_NAME
,
329 &gEfiGlobalVariableGuid
,
330 (VOID
**)&mSecureBootVarData
,
331 &mSecureBootVarDataSize
333 if (EFI_ERROR(Status
)) {
335 // Read could fail when Auth Variable solution is not supported
337 DEBUG((DEBUG_INFO
, "RecordSecureBootPolicyVarData GetVariable %s Status %x\n", EFI_SECURE_BOOT_MODE_NAME
, Status
));