]> git.proxmox.com Git - mirror_edk2.git/blame - MdeModulePkg/Universal/Variable/RuntimeDxe/TcgMorLockSmm.c
MdeModulePkg VariablePei: Don't check BOOT_IN_RECOVERY_MODE
[mirror_edk2.git] / MdeModulePkg / Universal / Variable / RuntimeDxe / TcgMorLockSmm.c
CommitLineData
abad83e6
JY
1/** @file\r
2 TCG MOR (Memory Overwrite Request) Lock Control support (SMM version).\r
3\r
4 This module initilizes MemoryOverwriteRequestControlLock variable.\r
5 This module adds Variable Hook and check MemoryOverwriteRequestControlLock.\r
6\r
5da2c9b2 7Copyright (c) 2016 - 2018, Intel Corporation. All rights reserved.<BR>\r
abad83e6
JY
8This program and the accompanying materials\r
9are licensed and made available under the terms and conditions of the BSD License\r
10which accompanies this distribution. The full text of the license may be found at\r
11http://opensource.org/licenses/bsd-license.php\r
12\r
13THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,\r
14WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.\r
15\r
16**/\r
17\r
18#include <PiDxe.h>\r
19#include <Guid/MemoryOverwriteControl.h>\r
20#include <IndustryStandard/MemoryOverwriteRequestControlLock.h>\r
21#include <Library/DebugLib.h>\r
22#include <Library/BaseLib.h>\r
23#include <Library/BaseMemoryLib.h>\r
24#include "Variable.h"\r
25\r
26typedef struct {\r
27 CHAR16 *VariableName;\r
28 EFI_GUID *VendorGuid;\r
29} VARIABLE_TYPE;\r
30\r
31VARIABLE_TYPE mMorVariableType[] = {\r
32 {MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME, &gEfiMemoryOverwriteControlDataGuid},\r
33 {MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME, &gEfiMemoryOverwriteRequestControlLockGuid},\r
34};\r
35\r
fda8f631
LE
36BOOLEAN mMorPassThru = FALSE;\r
37\r
abad83e6
JY
38#define MOR_LOCK_DATA_UNLOCKED 0x0\r
39#define MOR_LOCK_DATA_LOCKED_WITHOUT_KEY 0x1\r
40#define MOR_LOCK_DATA_LOCKED_WITH_KEY 0x2\r
41\r
42#define MOR_LOCK_V1_SIZE 1\r
43#define MOR_LOCK_V2_KEY_SIZE 8\r
44\r
45typedef enum {\r
46 MorLockStateUnlocked = 0,\r
47 MorLockStateLocked = 1,\r
48} MOR_LOCK_STATE;\r
49\r
7516532f 50BOOLEAN mMorLockInitializationRequired = FALSE;\r
abad83e6
JY
51UINT8 mMorLockKey[MOR_LOCK_V2_KEY_SIZE];\r
52BOOLEAN mMorLockKeyEmpty = TRUE;\r
53BOOLEAN mMorLockPassThru = FALSE;\r
54MOR_LOCK_STATE mMorLockState = MorLockStateUnlocked;\r
55\r
56/**\r
57 Returns if this is MOR related variable.\r
58\r
59 @param VariableName the name of the vendor's variable, it's a Null-Terminated Unicode String\r
60 @param VendorGuid Unify identifier for vendor.\r
61\r
62 @retval TRUE The variable is MOR related.\r
63 @retval FALSE The variable is NOT MOR related.\r
64**/\r
65BOOLEAN\r
66IsAnyMorVariable (\r
67 IN CHAR16 *VariableName,\r
68 IN EFI_GUID *VendorGuid\r
69 )\r
70{\r
71 UINTN Index;\r
72\r
73 for (Index = 0; Index < sizeof(mMorVariableType)/sizeof(mMorVariableType[0]); Index++) {\r
74 if ((StrCmp (VariableName, mMorVariableType[Index].VariableName) == 0) &&\r
75 (CompareGuid (VendorGuid, mMorVariableType[Index].VendorGuid))) {\r
76 return TRUE;\r
77 }\r
78 }\r
79 return FALSE;\r
80}\r
81\r
82/**\r
83 Returns if this is MOR lock variable.\r
84\r
85 @param VariableName the name of the vendor's variable, it's a Null-Terminated Unicode String\r
86 @param VendorGuid Unify identifier for vendor.\r
87\r
88 @retval TRUE The variable is MOR lock variable.\r
89 @retval FALSE The variable is NOT MOR lock variable.\r
90**/\r
91BOOLEAN\r
92IsMorLockVariable (\r
93 IN CHAR16 *VariableName,\r
94 IN EFI_GUID *VendorGuid\r
95 )\r
96{\r
97 if ((StrCmp (VariableName, MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME) == 0) &&\r
98 (CompareGuid (VendorGuid, &gEfiMemoryOverwriteRequestControlLockGuid))) {\r
99 return TRUE;\r
100 }\r
101 return FALSE;\r
102}\r
103\r
104/**\r
105 Set MOR lock variable.\r
106\r
107 @param Data MOR Lock variable data.\r
108\r
109 @retval EFI_SUCCESS The firmware has successfully stored the variable and its data as\r
110 defined by the Attributes.\r
111 @retval EFI_INVALID_PARAMETER An invalid combination of attribute bits was supplied, or the\r
112 DataSize exceeds the maximum allowed.\r
113 @retval EFI_INVALID_PARAMETER VariableName is an empty Unicode string.\r
114 @retval EFI_OUT_OF_RESOURCES Not enough storage is available to hold the variable and its data.\r
115 @retval EFI_DEVICE_ERROR The variable could not be saved due to a hardware failure.\r
116 @retval EFI_WRITE_PROTECTED The variable in question is read-only.\r
117 @retval EFI_WRITE_PROTECTED The variable in question cannot be deleted.\r
4073f85d 118 @retval EFI_SECURITY_VIOLATION The variable could not be written due to EFI_VARIABLE_TIME_BASED_AUTHENTICATED_WRITE_ACCESS\r
abad83e6
JY
119 set but the AuthInfo does NOT pass the validation check carried\r
120 out by the firmware.\r
121 @retval EFI_NOT_FOUND The variable trying to be updated or deleted was not found.\r
122**/\r
123EFI_STATUS\r
124SetMorLockVariable (\r
125 IN UINT8 Data\r
126 )\r
127{\r
128 EFI_STATUS Status;\r
129\r
130 mMorLockPassThru = TRUE;\r
131 Status = VariableServiceSetVariable (\r
132 MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME,\r
133 &gEfiMemoryOverwriteRequestControlLockGuid,\r
134 EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS,\r
135 sizeof(Data),\r
136 &Data\r
137 );\r
138 mMorLockPassThru = FALSE;\r
139 return Status;\r
140}\r
141\r
142/**\r
143 This service is an MorLock checker handler for the SetVariable().\r
144\r
145 @param VariableName the name of the vendor's variable, as a\r
146 Null-Terminated Unicode String\r
147 @param VendorGuid Unify identifier for vendor.\r
148 @param Attributes Point to memory location to return the attributes of variable. If the point\r
149 is NULL, the parameter would be ignored.\r
150 @param DataSize The size in bytes of Data-Buffer.\r
151 @param Data Point to the content of the variable.\r
152\r
153 @retval EFI_SUCCESS The MorLock check pass, and Variable driver can store the variable data.\r
154 @retval EFI_INVALID_PARAMETER The MorLock data or data size or attributes is not allowed.\r
155 @retval EFI_ACCESS_DENIED The MorLock is locked.\r
156 @retval EFI_WRITE_PROTECTED The MorLock deletion is not allowed.\r
157 @retval EFI_ALREADY_STARTED The MorLock variable is handled inside this function.\r
158 Variable driver can just return EFI_SUCCESS.\r
159**/\r
160EFI_STATUS\r
161SetVariableCheckHandlerMorLock (\r
162 IN CHAR16 *VariableName,\r
163 IN EFI_GUID *VendorGuid,\r
164 IN UINT32 Attributes,\r
165 IN UINTN DataSize,\r
166 IN VOID *Data\r
167 )\r
168{\r
169 EFI_STATUS Status;\r
170\r
171 //\r
172 // Basic Check\r
173 //\r
174 if (Attributes == 0 || DataSize == 0 || Data == NULL) {\r
e3531164
LE
175 //\r
176 // Permit deletion for passthru request, deny it otherwise.\r
177 //\r
178 return mMorLockPassThru ? EFI_SUCCESS : EFI_WRITE_PROTECTED;\r
abad83e6
JY
179 }\r
180\r
181 if ((Attributes != (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS)) ||\r
182 ((DataSize != MOR_LOCK_V1_SIZE) && (DataSize != MOR_LOCK_V2_KEY_SIZE))) {\r
183 return EFI_INVALID_PARAMETER;\r
184 }\r
185\r
186 //\r
187 // Do not check if the request is passthru.\r
188 //\r
189 if (mMorLockPassThru) {\r
190 return EFI_SUCCESS;\r
191 }\r
192\r
193 if (mMorLockState == MorLockStateUnlocked) {\r
194 //\r
195 // In Unlocked State\r
196 //\r
197 if (DataSize == MOR_LOCK_V1_SIZE) {\r
198 //\r
0a18956d 199 // V1 - lock permanently\r
abad83e6
JY
200 //\r
201 if (*(UINT8 *)Data == MOR_LOCK_DATA_UNLOCKED) {\r
202 //\r
203 // Unlock\r
204 //\r
205 Status = SetMorLockVariable (MOR_LOCK_DATA_UNLOCKED);\r
206 if (!EFI_ERROR (Status)) {\r
207 //\r
208 // return EFI_ALREADY_STARTED to skip variable set.\r
209 //\r
210 return EFI_ALREADY_STARTED;\r
211 } else {\r
212 //\r
213 // SetVar fail\r
214 //\r
215 return Status;\r
216 }\r
217 } else if (*(UINT8 *)Data == MOR_LOCK_DATA_LOCKED_WITHOUT_KEY) {\r
218 //\r
219 // Lock without key\r
220 //\r
221 Status = SetMorLockVariable (MOR_LOCK_DATA_LOCKED_WITHOUT_KEY);\r
222 if (!EFI_ERROR (Status)) {\r
223 //\r
224 // Lock success\r
225 //\r
226 mMorLockState = MorLockStateLocked;\r
227 //\r
228 // return EFI_ALREADY_STARTED to skip variable set.\r
229 //\r
230 return EFI_ALREADY_STARTED;\r
231 } else {\r
232 //\r
233 // SetVar fail\r
234 //\r
235 return Status;\r
236 }\r
237 } else {\r
238 return EFI_INVALID_PARAMETER;\r
239 }\r
240 } else if (DataSize == MOR_LOCK_V2_KEY_SIZE) {\r
241 //\r
242 // V2 lock and provision the key\r
243 //\r
244\r
245 //\r
246 // Need set here because the data value on flash is different\r
247 //\r
248 Status = SetMorLockVariable (MOR_LOCK_DATA_LOCKED_WITH_KEY);\r
249 if (EFI_ERROR(Status)) {\r
250 //\r
251 // SetVar fail, do not provision the key\r
252 //\r
253 return Status;\r
254 } else {\r
255 //\r
256 // Lock success, provision the key\r
257 //\r
258 mMorLockKeyEmpty = FALSE;\r
259 CopyMem (mMorLockKey, Data, MOR_LOCK_V2_KEY_SIZE);\r
260 mMorLockState = MorLockStateLocked;\r
261 //\r
262 // return EFI_ALREADY_STARTED to skip variable set.\r
263 //\r
264 return EFI_ALREADY_STARTED;\r
265 }\r
266 } else {\r
267 ASSERT (FALSE);\r
268 return EFI_OUT_OF_RESOURCES;\r
269 }\r
270 } else {\r
271 //\r
272 // In Locked State\r
273 //\r
274 if (mMorLockKeyEmpty || (DataSize != MOR_LOCK_V2_KEY_SIZE)) {\r
275 return EFI_ACCESS_DENIED;\r
276 }\r
277 if ((CompareMem (Data, mMorLockKey, MOR_LOCK_V2_KEY_SIZE) == 0)) {\r
278 //\r
279 // Key match - unlock\r
280 //\r
281\r
282 //\r
283 // Need set here because the data value on flash is different\r
284 //\r
285 Status = SetMorLockVariable (MOR_LOCK_DATA_UNLOCKED);\r
286 if (EFI_ERROR (Status)) {\r
287 //\r
288 // SetVar fail\r
289 //\r
290 return Status;\r
291 } else {\r
292 //\r
293 // Unlock Success\r
294 //\r
295 mMorLockState = MorLockStateUnlocked;\r
296 mMorLockKeyEmpty = TRUE;\r
297 ZeroMem (mMorLockKey, sizeof(mMorLockKey));\r
298 //\r
299 // return EFI_ALREADY_STARTED to skip variable set.\r
300 //\r
301 return EFI_ALREADY_STARTED;\r
302 }\r
303 } else {\r
304 //\r
305 // Key mismatch - Prevent Dictionary Attack\r
306 //\r
307 mMorLockState = MorLockStateLocked;\r
308 mMorLockKeyEmpty = TRUE;\r
309 ZeroMem (mMorLockKey, sizeof(mMorLockKey));\r
310 return EFI_ACCESS_DENIED;\r
311 }\r
312 }\r
313}\r
314\r
315/**\r
316 This service is an MOR/MorLock checker handler for the SetVariable().\r
317\r
03877377
LE
318 @param[in] VariableName the name of the vendor's variable, as a\r
319 Null-Terminated Unicode String\r
320 @param[in] VendorGuid Unify identifier for vendor.\r
321 @param[in] Attributes Attributes bitmask to set for the variable.\r
322 @param[in] DataSize The size in bytes of Data-Buffer.\r
323 @param[in] Data Point to the content of the variable.\r
324\r
325 @retval EFI_SUCCESS The MOR/MorLock check pass, and Variable\r
326 driver can store the variable data.\r
327 @retval EFI_INVALID_PARAMETER The MOR/MorLock data or data size or\r
328 attributes is not allowed for MOR variable.\r
abad83e6 329 @retval EFI_ACCESS_DENIED The MOR/MorLock is locked.\r
03877377
LE
330 @retval EFI_ALREADY_STARTED The MorLock variable is handled inside this\r
331 function. Variable driver can just return\r
332 EFI_SUCCESS.\r
abad83e6
JY
333**/\r
334EFI_STATUS\r
335SetVariableCheckHandlerMor (\r
336 IN CHAR16 *VariableName,\r
337 IN EFI_GUID *VendorGuid,\r
338 IN UINT32 Attributes,\r
339 IN UINTN DataSize,\r
340 IN VOID *Data\r
341 )\r
342{\r
343 //\r
344 // do not handle non-MOR variable\r
345 //\r
346 if (!IsAnyMorVariable (VariableName, VendorGuid)) {\r
347 return EFI_SUCCESS;\r
348 }\r
349\r
350 //\r
351 // MorLock variable\r
352 //\r
353 if (IsMorLockVariable (VariableName, VendorGuid)) {\r
354 return SetVariableCheckHandlerMorLock (\r
355 VariableName,\r
356 VendorGuid,\r
357 Attributes,\r
358 DataSize,\r
359 Data\r
360 );\r
361 }\r
362\r
363 //\r
364 // Mor Variable\r
365 //\r
366\r
fda8f631
LE
367 //\r
368 // Permit deletion for passthru request.\r
369 //\r
370 if (((Attributes == 0) || (DataSize == 0)) && mMorPassThru) {\r
371 return EFI_SUCCESS;\r
372 }\r
373\r
abad83e6
JY
374 //\r
375 // Basic Check\r
376 //\r
377 if ((Attributes != (EFI_VARIABLE_NON_VOLATILE | EFI_VARIABLE_BOOTSERVICE_ACCESS | EFI_VARIABLE_RUNTIME_ACCESS)) ||\r
378 (DataSize != sizeof(UINT8)) ||\r
379 (Data == NULL)) {\r
380 return EFI_INVALID_PARAMETER;\r
381 }\r
382 if (mMorLockState == MorLockStateLocked) {\r
383 //\r
384 // If lock, deny access\r
385 //\r
386 return EFI_ACCESS_DENIED;\r
387 }\r
388 //\r
389 // grant access\r
390 //\r
391 return EFI_SUCCESS;\r
392}\r
393\r
394/**\r
03877377 395 Initialization for MOR Control Lock.\r
abad83e6 396\r
03877377 397 @retval EFI_SUCCESS MorLock initialization success.\r
abad83e6
JY
398 @return Others Some error occurs.\r
399**/\r
400EFI_STATUS\r
401MorLockInit (\r
402 VOID\r
403 )\r
404{\r
7516532f
LE
405 mMorLockInitializationRequired = TRUE;\r
406 return EFI_SUCCESS;\r
abad83e6 407}\r
f1304280
LE
408\r
409/**\r
410 Delayed initialization for MOR Control Lock at EndOfDxe.\r
411\r
412 This function performs any operations queued by MorLockInit().\r
413**/\r
414VOID\r
415MorLockInitAtEndOfDxe (\r
416 VOID\r
417 )\r
418{\r
7516532f
LE
419 UINTN MorSize;\r
420 EFI_STATUS MorStatus;\r
421\r
422 if (!mMorLockInitializationRequired) {\r
423 //\r
424 // The EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL has never been installed, thus\r
425 // the variable write service is unavailable. This should never happen.\r
426 //\r
427 ASSERT (FALSE);\r
428 return;\r
429 }\r
430\r
431 //\r
432 // Check if the MOR variable exists.\r
433 //\r
434 MorSize = 0;\r
435 MorStatus = VariableServiceGetVariable (\r
436 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
437 &gEfiMemoryOverwriteControlDataGuid,\r
438 NULL, // Attributes\r
439 &MorSize,\r
440 NULL // Data\r
441 );\r
f1304280 442 //\r
7516532f 443 // We provided a zero-sized buffer, so the above call can never succeed.\r
f1304280 444 //\r
7516532f
LE
445 ASSERT (EFI_ERROR (MorStatus));\r
446\r
447 if (MorStatus == EFI_BUFFER_TOO_SMALL) {\r
448 //\r
fda8f631 449 // The MOR variable exists.\r
7516532f 450 //\r
fda8f631
LE
451 // Some OSes don't follow the TCG's Platform Reset Attack Mitigation spec\r
452 // in that the OS should never create the MOR variable, only read and write\r
453 // it -- these OSes (unintentionally) create MOR if the platform firmware\r
454 // does not produce it. Whether this is the case (from the last OS boot)\r
455 // can be deduced from the absence of the TCG / TCG2 protocols, as edk2's\r
456 // MOR implementation depends on (one of) those protocols.\r
457 //\r
a855f63e 458 if (VariableHaveTcgProtocols ()) {\r
fda8f631
LE
459 //\r
460 // The MOR variable originates from the platform firmware; set the MOR\r
461 // Control Lock variable to report the locking capability to the OS.\r
462 //\r
463 SetMorLockVariable (0);\r
464 return;\r
465 }\r
466\r
467 //\r
468 // The MOR variable's origin is inexplicable; delete it.\r
469 //\r
470 DEBUG ((\r
471 DEBUG_WARN,\r
472 "%a: deleting unexpected / unsupported variable %g:%s\n",\r
473 __FUNCTION__,\r
474 &gEfiMemoryOverwriteControlDataGuid,\r
475 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME\r
476 ));\r
477\r
478 mMorPassThru = TRUE;\r
479 VariableServiceSetVariable (\r
480 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
481 &gEfiMemoryOverwriteControlDataGuid,\r
482 0, // Attributes\r
483 0, // DataSize\r
484 NULL // Data\r
485 );\r
486 mMorPassThru = FALSE;\r
7516532f
LE
487 }\r
488\r
489 //\r
fda8f631
LE
490 // The MOR variable is absent; the platform firmware does not support it.\r
491 // Lock the variable so that no other module may create it.\r
492 //\r
493 VariableLockRequestToLock (\r
494 NULL, // This\r
495 MEMORY_OVERWRITE_REQUEST_VARIABLE_NAME,\r
496 &gEfiMemoryOverwriteControlDataGuid\r
497 );\r
498\r
499 //\r
500 // Delete the MOR Control Lock variable too (should it exists for some\r
501 // reason) and prevent other modules from creating it.\r
7516532f
LE
502 //\r
503 mMorLockPassThru = TRUE;\r
504 VariableServiceSetVariable (\r
505 MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME,\r
506 &gEfiMemoryOverwriteRequestControlLockGuid,\r
507 0, // Attributes\r
508 0, // DataSize\r
509 NULL // Data\r
510 );\r
511 mMorLockPassThru = FALSE;\r
512\r
513 VariableLockRequestToLock (\r
514 NULL, // This\r
515 MEMORY_OVERWRITE_REQUEST_CONTROL_LOCK_NAME,\r
516 &gEfiMemoryOverwriteRequestControlLockGuid\r
517 );\r
f1304280 518}\r