]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/Measurement.c
MdePkg: UefiTcgPlatform.h: Add UEFI_VARIABLE_DATA
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / Measurement.c
CommitLineData
fa0737a8
SZ
1/** @file\r
2 Measure TrEE required variable.\r
3\r
dc9bd6ed 4Copyright (c) 2013 - 2017, Intel Corporation. All rights reserved.<BR>\r
fa0737a8
SZ
5This program and the accompanying materials\r
6are licensed and made available under the terms and conditions of the BSD License\r
7which accompanies this distribution. The full text of the license may be found at\r
8http://opensource.org/licenses/bsd-license.php\r
9\r
10THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
11WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
12\r
13**/\r
14\r
15#include <PiDxe.h>\r
16#include <Guid/ImageAuthentication.h>\r
17#include <IndustryStandard/UefiTcgPlatform.h>\r
18#include <Protocol/TrEEProtocol.h>\r
19\r
20#include <Library/UefiBootServicesTableLib.h>\r
21#include <Library/UefiRuntimeServicesTableLib.h>\r
22#include <Library/MemoryAllocationLib.h>\r
23#include <Library/BaseMemoryLib.h>\r
24#include <Library/DebugLib.h>\r
25#include <Library/BaseLib.h>\r
26#include <Library/TpmMeasurementLib.h>\r
27\r
28typedef struct {\r
29 CHAR16 *VariableName;\r
30 EFI_GUID *VendorGuid;\r
31} VARIABLE_TYPE;\r
32\r
33VARIABLE_TYPE mVariableType[] = {\r
34 {EFI_SECURE_BOOT_MODE_NAME, &gEfiGlobalVariableGuid},\r
35 {EFI_PLATFORM_KEY_NAME, &gEfiGlobalVariableGuid},\r
36 {EFI_KEY_EXCHANGE_KEY_NAME, &gEfiGlobalVariableGuid},\r
37 {EFI_IMAGE_SECURITY_DATABASE, &gEfiImageSecurityDatabaseGuid},\r
38 {EFI_IMAGE_SECURITY_DATABASE1, &gEfiImageSecurityDatabaseGuid},\r
dc9bd6ed 39 {EFI_IMAGE_SECURITY_DATABASE2, &gEfiImageSecurityDatabaseGuid},\r
fa0737a8
SZ
40};\r
41\r
dc9bd6ed
ZC
42//\r
43// "SecureBoot" may update following PK Del/Add\r
44// Cache its value to detect value update\r
45//\r
46UINT8 *mSecureBootVarData = NULL;\r
47UINTN mSecureBootVarDataSize = 0;\r
48\r
fa0737a8
SZ
49/**\r
50 This function will return if this variable is SecureBootPolicy Variable.\r
51\r
52 @param[in] VariableName A Null-terminated string that is the name of the vendor's variable.\r
53 @param[in] VendorGuid A unique identifier for the vendor.\r
54\r
55 @retval TRUE This is SecureBootPolicy Variable\r
56 @retval FALSE This is not SecureBootPolicy Variable\r
57**/\r
58BOOLEAN\r
59IsSecureBootPolicyVariable (\r
60 IN CHAR16 *VariableName,\r
61 IN EFI_GUID *VendorGuid\r
62 )\r
63{\r
64 UINTN Index;\r
65\r
66 for (Index = 0; Index < sizeof(mVariableType)/sizeof(mVariableType[0]); Index++) {\r
67 if ((StrCmp (VariableName, mVariableType[Index].VariableName) == 0) &&\r
68 (CompareGuid (VendorGuid, mVariableType[Index].VendorGuid))) {\r
69 return TRUE;\r
70 }\r
71 }\r
72 return FALSE;\r
73}\r
74\r
75/**\r
76 Measure and log an EFI variable, and extend the measurement result into a specific PCR.\r
77\r
78 @param[in] VarName A Null-terminated string that is the name of the vendor's variable.\r
79 @param[in] VendorGuid A unique identifier for the vendor.\r
80 @param[in] VarData The content of the variable data.\r
81 @param[in] VarSize The size of the variable data.\r
82\r
83 @retval EFI_SUCCESS Operation completed successfully.\r
84 @retval EFI_OUT_OF_RESOURCES Out of memory.\r
85 @retval EFI_DEVICE_ERROR The operation was unsuccessful.\r
86\r
87**/\r
88EFI_STATUS\r
89EFIAPI\r
90MeasureVariable (\r
91 IN CHAR16 *VarName,\r
92 IN EFI_GUID *VendorGuid,\r
93 IN VOID *VarData,\r
94 IN UINTN VarSize\r
95 )\r
96{\r
97 EFI_STATUS Status;\r
98 UINTN VarNameLength;\r
99 EFI_VARIABLE_DATA_TREE *VarLog;\r
100 UINT32 VarLogSize;\r
101\r
102 ASSERT ((VarSize == 0 && VarData == NULL) || (VarSize != 0 && VarData != NULL));\r
103\r
104 VarNameLength = StrLen (VarName);\r
105 VarLogSize = (UINT32)(sizeof (*VarLog) + VarNameLength * sizeof (*VarName) + VarSize\r
106 - sizeof (VarLog->UnicodeName) - sizeof (VarLog->VariableData));\r
107\r
108 VarLog = (EFI_VARIABLE_DATA_TREE *) AllocateZeroPool (VarLogSize);\r
109 if (VarLog == NULL) {\r
110 return EFI_OUT_OF_RESOURCES;\r
111 }\r
112\r
113 CopyMem (&VarLog->VariableName, VendorGuid, sizeof(VarLog->VariableName));\r
114 VarLog->UnicodeNameLength = VarNameLength;\r
115 VarLog->VariableDataLength = VarSize;\r
116 CopyMem (\r
117 VarLog->UnicodeName,\r
118 VarName,\r
119 VarNameLength * sizeof (*VarName)\r
120 );\r
121 if (VarSize != 0) {\r
122 CopyMem (\r
123 (CHAR16 *)VarLog->UnicodeName + VarNameLength,\r
124 VarData,\r
125 VarSize\r
126 );\r
127 }\r
128\r
129 DEBUG ((EFI_D_INFO, "AuthVariableDxe: MeasureVariable (Pcr - %x, EventType - %x, ", (UINTN)7, (UINTN)EV_EFI_VARIABLE_AUTHORITY));\r
130 DEBUG ((EFI_D_INFO, "VariableName - %s, VendorGuid - %g)\n", VarName, VendorGuid));\r
131\r
132 Status = TpmMeasureAndLogData (\r
133 7,\r
134 EV_EFI_VARIABLE_DRIVER_CONFIG,\r
135 VarLog,\r
136 VarLogSize,\r
137 VarLog,\r
138 VarLogSize\r
139 );\r
140 FreePool (VarLog);\r
141 return Status;\r
142}\r
143\r
144/**\r
145 Returns the status whether get the variable success. The function retrieves\r
146 variable through the UEFI Runtime Service GetVariable(). The\r
147 returned buffer is allocated using AllocatePool(). The caller is responsible\r
148 for freeing this buffer with FreePool().\r
149\r
150 This API is only invoked in boot time. It may NOT be invoked at runtime.\r
151\r
152 @param[in] Name The pointer to a Null-terminated Unicode string.\r
153 @param[in] Guid The pointer to an EFI_GUID structure\r
154 @param[out] Value The buffer point saved the variable info.\r
155 @param[out] Size The buffer size of the variable.\r
156\r
157 @return EFI_OUT_OF_RESOURCES Allocate buffer failed.\r
158 @return EFI_SUCCESS Find the specified variable.\r
159 @return Others Errors Return errors from call to gRT->GetVariable.\r
160\r
161**/\r
162EFI_STATUS\r
163InternalGetVariable (\r
164 IN CONST CHAR16 *Name,\r
165 IN CONST EFI_GUID *Guid,\r
166 OUT VOID **Value,\r
167 OUT UINTN *Size\r
168 )\r
169{\r
170 EFI_STATUS Status;\r
171 UINTN BufferSize;\r
172\r
173 //\r
174 // Try to get the variable size.\r
175 //\r
176 BufferSize = 0;\r
177 *Value = NULL;\r
178 if (Size != NULL) {\r
179 *Size = 0;\r
180 }\r
181\r
182 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
183 if (Status != EFI_BUFFER_TOO_SMALL) {\r
184 return Status;\r
185 }\r
186\r
187 //\r
188 // Allocate buffer to get the variable.\r
189 //\r
190 *Value = AllocatePool (BufferSize);\r
191 ASSERT (*Value != NULL);\r
192 if (*Value == NULL) {\r
193 return EFI_OUT_OF_RESOURCES;\r
194 }\r
195\r
196 //\r
197 // Get the variable data.\r
198 //\r
199 Status = gRT->GetVariable ((CHAR16 *) Name, (EFI_GUID *) Guid, NULL, &BufferSize, *Value);\r
200 if (EFI_ERROR (Status)) {\r
201 FreePool(*Value);\r
202 *Value = NULL;\r
203 }\r
204\r
205 if (Size != NULL) {\r
206 *Size = BufferSize;\r
207 }\r
208\r
209 return Status;\r
210}\r
211\r
212/**\r
213 SecureBoot Hook for SetVariable.\r
214\r
215 @param[in] VariableName Name of Variable to be found.\r
216 @param[in] VendorGuid Variable vendor GUID.\r
217\r
218**/\r
219VOID\r
220EFIAPI\r
221SecureBootHook (\r
222 IN CHAR16 *VariableName,\r
223 IN EFI_GUID *VendorGuid\r
224 )\r
225{\r
226 EFI_STATUS Status;\r
227 UINTN VariableDataSize;\r
228 VOID *VariableData;\r
229\r
230 if (!IsSecureBootPolicyVariable (VariableName, VendorGuid)) {\r
231 return ;\r
232 }\r
233\r
234 //\r
235 // We should NOT use Data and DataSize here,because it may include signature,\r
236 // or is just partial with append attributes, or is deleted.\r
237 // We should GetVariable again, to get full variable content.\r
238 //\r
239 Status = InternalGetVariable (\r
240 VariableName,\r
241 VendorGuid,\r
242 &VariableData,\r
243 &VariableDataSize\r
244 );\r
245 if (EFI_ERROR (Status)) {\r
246 VariableData = NULL;\r
247 VariableDataSize = 0;\r
248 }\r
249\r
250 Status = MeasureVariable (\r
251 VariableName,\r
252 VendorGuid,\r
253 VariableData,\r
254 VariableDataSize\r
255 );\r
256 DEBUG ((EFI_D_INFO, "MeasureBootPolicyVariable - %r\n", Status));\r
257\r
258 if (VariableData != NULL) {\r
259 FreePool (VariableData);\r
260 }\r
261\r
dc9bd6ed
ZC
262 //\r
263 // "SecureBoot" is 8bit & read-only. It can only be changed according to PK update\r
264 //\r
265 if ((StrCmp (VariableName, EFI_PLATFORM_KEY_NAME) == 0) &&\r
266 CompareGuid (VendorGuid, &gEfiGlobalVariableGuid)) {\r
267 Status = InternalGetVariable (\r
268 EFI_SECURE_BOOT_MODE_NAME,\r
269 &gEfiGlobalVariableGuid,\r
270 &VariableData,\r
271 &VariableDataSize\r
272 );\r
273 if (EFI_ERROR (Status)) {\r
274 return;\r
275 }\r
276\r
277 //\r
278 // If PK update is successful. "SecureBoot" shall always exist ever since variable write service is ready\r
279 //\r
280 ASSERT(mSecureBootVarData != NULL);\r
281\r
282 if (CompareMem(mSecureBootVarData, VariableData, VariableDataSize) != 0) {\r
283 FreePool(mSecureBootVarData);\r
284 mSecureBootVarData = VariableData;\r
285 mSecureBootVarDataSize = VariableDataSize;\r
286\r
287 DEBUG((DEBUG_INFO, "%s variable updated according to PK change. Remeasure the value!\n", EFI_SECURE_BOOT_MODE_NAME));\r
288 Status = MeasureVariable (\r
289 EFI_SECURE_BOOT_MODE_NAME,\r
290 &gEfiGlobalVariableGuid,\r
291 mSecureBootVarData,\r
292 mSecureBootVarDataSize\r
293 );\r
294 DEBUG ((DEBUG_INFO, "MeasureBootPolicyVariable - %r\n", Status));\r
295 } else {\r
296 //\r
297 // "SecureBoot" variable is not changed\r
298 //\r
299 FreePool(VariableData);\r
300 }\r
301 }\r
302\r
fa0737a8
SZ
303 return ;\r
304}\r
dc9bd6ed
ZC
305\r
306/**\r
307 Some Secure Boot Policy Variable may update following other variable changes(SecureBoot follows PK change, etc).\r
308 Record their initial State when variable write service is ready.\r
309\r
310**/\r
311VOID\r
312EFIAPI\r
313RecordSecureBootPolicyVarData(\r
314 VOID\r
315 )\r
316{\r
317 EFI_STATUS Status;\r
318\r
319 //\r
320 // Record initial "SecureBoot" variable value.\r
321 // It is used to detect SecureBoot variable change in SecureBootHook.\r
322 //\r
323 Status = InternalGetVariable (\r
324 EFI_SECURE_BOOT_MODE_NAME,\r
325 &gEfiGlobalVariableGuid,\r
326 (VOID **)&mSecureBootVarData,\r
327 &mSecureBootVarDataSize\r
328 );\r
329 if (EFI_ERROR(Status)) {\r
330 //\r
331 // Read could fail when Auth Variable solution is not supported\r
332 //\r
333 DEBUG((DEBUG_INFO, "RecordSecureBootPolicyVarData GetVariable %s Status %x\n", EFI_SECURE_BOOT_MODE_NAME, Status));\r
334 }\r
335}\r