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