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