]> git.proxmox.com Git - mirror_edk2.git/blob - SecurityPkg/VariableAuthenticated/RuntimeDxe/VariableSmm.c
Add comment for modules which have external input.
[mirror_edk2.git] / SecurityPkg / VariableAuthenticated / RuntimeDxe / VariableSmm.c
1 /** @file
2 The sample implementation for SMM variable protocol. And this driver
3 implements an SMI handler to communicate with the DXE runtime driver
4 to provide variable services.
5
6 Caution: This module requires additional review when modified.
7 This driver will have external input - variable data and communicate buffer in SMM mode.
8 This external input must be validated carefully to avoid security issue like
9 buffer overflow, integer overflow.
10
11 SmmVariableHandler() will receive untrusted input and do basic validation.
12
13 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
14 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
15 SmmVariableGetStatistics() should also do validation based on its own knowledge.
16
17 Copyright (c) 2010 - 2012, Intel Corporation. All rights reserved.<BR>
18 This program and the accompanying materials
19 are licensed and made available under the terms and conditions of the BSD License
20 which accompanies this distribution. The full text of the license may be found at
21 http://opensource.org/licenses/bsd-license.php
22
23 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
24 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
25
26 **/
27
28 #include <Protocol/SmmVariable.h>
29 #include <Protocol/SmmFirmwareVolumeBlock.h>
30 #include <Protocol/SmmFaultTolerantWrite.h>
31 #include <Library/SmmServicesTableLib.h>
32
33 #include <Guid/AuthenticatedVariableFormat.h>
34 #include <Guid/SmmVariableCommon.h>
35 #include "Variable.h"
36
37 extern VARIABLE_INFO_ENTRY *gVariableInfo;
38 EFI_HANDLE mSmmVariableHandle = NULL;
39 EFI_HANDLE mVariableHandle = NULL;
40 BOOLEAN mAtRuntime = FALSE;
41 EFI_GUID mZeroGuid = {0, 0, 0, {0, 0, 0, 0, 0, 0, 0, 0}};
42
43 EFI_SMM_VARIABLE_PROTOCOL gSmmVariable = {
44 VariableServiceGetVariable,
45 VariableServiceGetNextVariableName,
46 VariableServiceSetVariable,
47 VariableServiceQueryVariableInfo
48 };
49
50
51 /**
52 Return TRUE if ExitBootServices () has been called.
53
54 @retval TRUE If ExitBootServices () has been called.
55 **/
56 BOOLEAN
57 AtRuntime (
58 VOID
59 )
60 {
61 return mAtRuntime;
62 }
63
64 /**
65 Initializes a basic mutual exclusion lock.
66
67 This function initializes a basic mutual exclusion lock to the released state
68 and returns the lock. Each lock provides mutual exclusion access at its task
69 priority level. Since there is no preemption or multiprocessor support in EFI,
70 acquiring the lock only consists of raising to the locks TPL.
71 If Lock is NULL, then ASSERT().
72 If Priority is not a valid TPL value, then ASSERT().
73
74 @param Lock A pointer to the lock data structure to initialize.
75 @param Priority EFI TPL is associated with the lock.
76
77 @return The lock.
78
79 **/
80 EFI_LOCK *
81 InitializeLock (
82 IN OUT EFI_LOCK *Lock,
83 IN EFI_TPL Priority
84 )
85 {
86 return Lock;
87 }
88
89 /**
90 Acquires lock only at boot time. Simply returns at runtime.
91
92 This is a temperary function that will be removed when
93 EfiAcquireLock() in UefiLib can handle the call in UEFI
94 Runtimer driver in RT phase.
95 It calls EfiAcquireLock() at boot time, and simply returns
96 at runtime.
97
98 @param Lock A pointer to the lock to acquire.
99
100 **/
101 VOID
102 AcquireLockOnlyAtBootTime (
103 IN EFI_LOCK *Lock
104 )
105 {
106
107 }
108
109
110 /**
111 Releases lock only at boot time. Simply returns at runtime.
112
113 This is a temperary function which will be removed when
114 EfiReleaseLock() in UefiLib can handle the call in UEFI
115 Runtimer driver in RT phase.
116 It calls EfiReleaseLock() at boot time and simply returns
117 at runtime.
118
119 @param Lock A pointer to the lock to release.
120
121 **/
122 VOID
123 ReleaseLockOnlyAtBootTime (
124 IN EFI_LOCK *Lock
125 )
126 {
127
128 }
129
130 /**
131 Retrive the SMM Fault Tolerent Write protocol interface.
132
133 @param[out] FtwProtocol The interface of SMM Ftw protocol
134
135 @retval EFI_SUCCESS The SMM FTW protocol instance was found and returned in FtwProtocol.
136 @retval EFI_NOT_FOUND The SMM FTW protocol instance was not found.
137 @retval EFI_INVALID_PARAMETER SarProtocol is NULL.
138
139 **/
140 EFI_STATUS
141 GetFtwProtocol (
142 OUT VOID **FtwProtocol
143 )
144 {
145 EFI_STATUS Status;
146
147 //
148 // Locate Smm Fault Tolerent Write protocol
149 //
150 Status = gSmst->SmmLocateProtocol (
151 &gEfiSmmFaultTolerantWriteProtocolGuid,
152 NULL,
153 FtwProtocol
154 );
155 return Status;
156 }
157
158
159 /**
160 Retrive the SMM FVB protocol interface by HANDLE.
161
162 @param[in] FvBlockHandle The handle of SMM FVB protocol that provides services for
163 reading, writing, and erasing the target block.
164 @param[out] FvBlock The interface of SMM FVB protocol
165
166 @retval EFI_SUCCESS The interface information for the specified protocol was returned.
167 @retval EFI_UNSUPPORTED The device does not support the SMM FVB protocol.
168 @retval EFI_INVALID_PARAMETER FvBlockHandle is not a valid EFI_HANDLE or FvBlock is NULL.
169
170 **/
171 EFI_STATUS
172 GetFvbByHandle (
173 IN EFI_HANDLE FvBlockHandle,
174 OUT EFI_FIRMWARE_VOLUME_BLOCK_PROTOCOL **FvBlock
175 )
176 {
177 //
178 // To get the SMM FVB protocol interface on the handle
179 //
180 return gSmst->SmmHandleProtocol (
181 FvBlockHandle,
182 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
183 (VOID **) FvBlock
184 );
185 }
186
187
188 /**
189 Function returns an array of handles that support the SMM FVB protocol
190 in a buffer allocated from pool.
191
192 @param[out] NumberHandles The number of handles returned in Buffer.
193 @param[out] Buffer A pointer to the buffer to return the requested
194 array of handles that support SMM FVB protocol.
195
196 @retval EFI_SUCCESS The array of handles was returned in Buffer, and the number of
197 handles in Buffer was returned in NumberHandles.
198 @retval EFI_NOT_FOUND No SMM FVB handle was found.
199 @retval EFI_OUT_OF_RESOURCES There is not enough pool memory to store the matching results.
200 @retval EFI_INVALID_PARAMETER NumberHandles is NULL or Buffer is NULL.
201
202 **/
203 EFI_STATUS
204 GetFvbCountAndBuffer (
205 OUT UINTN *NumberHandles,
206 OUT EFI_HANDLE **Buffer
207 )
208 {
209 EFI_STATUS Status;
210 UINTN BufferSize;
211
212 if ((NumberHandles == NULL) || (Buffer == NULL)) {
213 return EFI_INVALID_PARAMETER;
214 }
215
216 BufferSize = 0;
217 *NumberHandles = 0;
218 *Buffer = NULL;
219 Status = gSmst->SmmLocateHandle (
220 ByProtocol,
221 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
222 NULL,
223 &BufferSize,
224 *Buffer
225 );
226 if (EFI_ERROR(Status) && Status != EFI_BUFFER_TOO_SMALL) {
227 return EFI_NOT_FOUND;
228 }
229
230 *Buffer = AllocatePool (BufferSize);
231 if (*Buffer == NULL) {
232 return EFI_OUT_OF_RESOURCES;
233 }
234
235 Status = gSmst->SmmLocateHandle (
236 ByProtocol,
237 &gEfiSmmFirmwareVolumeBlockProtocolGuid,
238 NULL,
239 &BufferSize,
240 *Buffer
241 );
242
243 *NumberHandles = BufferSize / sizeof(EFI_HANDLE);
244 if (EFI_ERROR(Status)) {
245 *NumberHandles = 0;
246 }
247
248 return Status;
249 }
250
251
252 /**
253 Get the variable statistics information from the information buffer pointed by gVariableInfo.
254
255 Caution: This function may be invoked at SMM runtime.
256 InfoEntry and InfoSize are external input. Care must be taken to make sure not security issue at runtime.
257
258 @param[in, out] InfoEntry A pointer to the buffer of variable information entry.
259 On input, point to the variable information returned last time. if
260 InfoEntry->VendorGuid is zero, return the first information.
261 On output, point to the next variable information.
262 @param[in, out] InfoSize On input, the size of the variable information buffer.
263 On output, the returned variable information size.
264
265 @retval EFI_SUCCESS The variable information is found and returned successfully.
266 @retval EFI_UNSUPPORTED No variable inoformation exists in variable driver. The
267 PcdVariableCollectStatistics should be set TRUE to support it.
268 @retval EFI_BUFFER_TOO_SMALL The buffer is too small to hold the next variable information.
269 @retval EFI_INVALID_PARAMETER Input parameter is invalid.
270
271 **/
272 EFI_STATUS
273 SmmVariableGetStatistics (
274 IN OUT VARIABLE_INFO_ENTRY *InfoEntry,
275 IN OUT UINTN *InfoSize
276 )
277 {
278 VARIABLE_INFO_ENTRY *VariableInfo;
279 UINTN NameLength;
280 UINTN StatisticsInfoSize;
281 CHAR16 *InfoName;
282
283 if (InfoEntry == NULL) {
284 return EFI_INVALID_PARAMETER;
285 }
286
287 VariableInfo = gVariableInfo;
288 if (VariableInfo == NULL) {
289 return EFI_UNSUPPORTED;
290 }
291
292 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);
293 if (*InfoSize < sizeof (VARIABLE_INFO_ENTRY)) {
294 *InfoSize = StatisticsInfoSize;
295 return EFI_BUFFER_TOO_SMALL;
296 }
297 InfoName = (CHAR16 *)(InfoEntry + 1);
298
299 if (CompareGuid (&InfoEntry->VendorGuid, &mZeroGuid)) {
300 //
301 // Return the first variable info
302 //
303 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));
304 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));
305 *InfoSize = StatisticsInfoSize;
306 return EFI_SUCCESS;
307 }
308
309 //
310 // Get the next variable info
311 //
312 while (VariableInfo != NULL) {
313 if (CompareGuid (&VariableInfo->VendorGuid, &InfoEntry->VendorGuid)) {
314 NameLength = StrSize (VariableInfo->Name);
315 if (NameLength == StrSize (InfoName)) {
316 if (CompareMem (VariableInfo->Name, InfoName, NameLength) == 0) {
317 //
318 // Find the match one
319 //
320 VariableInfo = VariableInfo->Next;
321 break;
322 }
323 }
324 }
325 VariableInfo = VariableInfo->Next;
326 };
327
328 if (VariableInfo == NULL) {
329 *InfoSize = 0;
330 return EFI_SUCCESS;
331 }
332
333 //
334 // Output the new variable info
335 //
336 StatisticsInfoSize = sizeof (VARIABLE_INFO_ENTRY) + StrSize (VariableInfo->Name);
337 if (*InfoSize < StatisticsInfoSize) {
338 *InfoSize = StatisticsInfoSize;
339 return EFI_BUFFER_TOO_SMALL;
340 }
341
342 CopyMem (InfoEntry, VariableInfo, sizeof (VARIABLE_INFO_ENTRY));
343 CopyMem (InfoName, VariableInfo->Name, StrSize (VariableInfo->Name));
344 *InfoSize = StatisticsInfoSize;
345
346 return EFI_SUCCESS;
347 }
348
349
350 /**
351 Communication service SMI Handler entry.
352
353 This SMI handler provides services for the variable wrapper driver.
354
355 Caution: This function may receive untrusted input.
356 This variable data and communicate buffer are external input, so this function will do basic validation.
357 Each sub function VariableServiceGetVariable(), VariableServiceGetNextVariableName(),
358 VariableServiceSetVariable(), VariableServiceQueryVariableInfo(), ReclaimForOS(),
359 SmmVariableGetStatistics() should also do validation based on its own knowledge.
360
361 @param[in] DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
362 @param[in] RegisterContext Points to an optional handler context which was specified when the
363 handler was registered.
364 @param[in, out] CommBuffer A pointer to a collection of data in memory that will
365 be conveyed from a non-SMM environment into an SMM environment.
366 @param[in, out] CommBufferSize The size of the CommBuffer.
367
368 @retval EFI_SUCCESS The interrupt was handled and quiesced. No other handlers
369 should still be called.
370 @retval EFI_WARN_INTERRUPT_SOURCE_QUIESCED The interrupt has been quiesced but other handlers should
371 still be called.
372 @retval EFI_WARN_INTERRUPT_SOURCE_PENDING The interrupt is still pending and other handlers should still
373 be called.
374 @retval EFI_INTERRUPT_PENDING The interrupt could not be quiesced.
375 @retval EFI_INVALID_PARAMETER Input parameter is invalid.
376
377 **/
378 EFI_STATUS
379 EFIAPI
380 SmmVariableHandler (
381 IN EFI_HANDLE DispatchHandle,
382 IN CONST VOID *RegisterContext,
383 IN OUT VOID *CommBuffer,
384 IN OUT UINTN *CommBufferSize
385 )
386 {
387 EFI_STATUS Status;
388 SMM_VARIABLE_COMMUNICATE_HEADER *SmmVariableFunctionHeader;
389 SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *SmmVariableHeader;
390 SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *GetNextVariableName;
391 SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *QueryVariableInfo;
392 VARIABLE_INFO_ENTRY *VariableInfo;
393 UINTN InfoSize;
394
395 if (CommBuffer == NULL) {
396 return EFI_INVALID_PARAMETER;
397 }
398
399 SmmVariableFunctionHeader = (SMM_VARIABLE_COMMUNICATE_HEADER *)CommBuffer;
400 switch (SmmVariableFunctionHeader->Function) {
401 case SMM_VARIABLE_FUNCTION_GET_VARIABLE:
402 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data;
403 Status = VariableServiceGetVariable (
404 SmmVariableHeader->Name,
405 &SmmVariableHeader->Guid,
406 &SmmVariableHeader->Attributes,
407 &SmmVariableHeader->DataSize,
408 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize
409 );
410 break;
411
412 case SMM_VARIABLE_FUNCTION_GET_NEXT_VARIABLE_NAME:
413 GetNextVariableName = (SMM_VARIABLE_COMMUNICATE_GET_NEXT_VARIABLE_NAME *) SmmVariableFunctionHeader->Data;
414 Status = VariableServiceGetNextVariableName (
415 &GetNextVariableName->NameSize,
416 GetNextVariableName->Name,
417 &GetNextVariableName->Guid
418 );
419 break;
420
421 case SMM_VARIABLE_FUNCTION_SET_VARIABLE:
422 SmmVariableHeader = (SMM_VARIABLE_COMMUNICATE_ACCESS_VARIABLE *) SmmVariableFunctionHeader->Data;
423 Status = VariableServiceSetVariable (
424 SmmVariableHeader->Name,
425 &SmmVariableHeader->Guid,
426 SmmVariableHeader->Attributes,
427 SmmVariableHeader->DataSize,
428 (UINT8 *)SmmVariableHeader->Name + SmmVariableHeader->NameSize
429 );
430 break;
431
432 case SMM_VARIABLE_FUNCTION_QUERY_VARIABLE_INFO:
433 QueryVariableInfo = (SMM_VARIABLE_COMMUNICATE_QUERY_VARIABLE_INFO *) SmmVariableFunctionHeader->Data;
434 Status = VariableServiceQueryVariableInfo (
435 QueryVariableInfo->Attributes,
436 &QueryVariableInfo->MaximumVariableStorageSize,
437 &QueryVariableInfo->RemainingVariableStorageSize,
438 &QueryVariableInfo->MaximumVariableSize
439 );
440 break;
441
442 case SMM_VARIABLE_FUNCTION_READY_TO_BOOT:
443 ReclaimForOS ();
444 Status = EFI_SUCCESS;
445 break;
446
447 case SMM_VARIABLE_FUNCTION_EXIT_BOOT_SERVICE:
448 mAtRuntime = TRUE;
449 Status = EFI_SUCCESS;
450 break;
451
452 case SMM_VARIABLE_FUNCTION_GET_STATISTICS:
453 VariableInfo = (VARIABLE_INFO_ENTRY *) SmmVariableFunctionHeader->Data;
454 InfoSize = *CommBufferSize - OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data);
455 Status = SmmVariableGetStatistics (VariableInfo, &InfoSize);
456 *CommBufferSize = InfoSize + OFFSET_OF (SMM_VARIABLE_COMMUNICATE_HEADER, Data);
457 break;
458
459 default:
460 ASSERT (FALSE);
461 Status = EFI_UNSUPPORTED;
462 }
463
464 SmmVariableFunctionHeader->ReturnStatus = Status;
465
466 return EFI_SUCCESS;
467 }
468
469
470 /**
471 SMM Fault Tolerant Write protocol notification event handler.
472
473 Non-Volatile variable write may needs FTW protocol to reclaim when
474 writting variable.
475
476 @param Protocol Points to the protocol's unique identifier
477 @param Interface Points to the interface instance
478 @param Handle The handle on which the interface was installed
479
480 @retval EFI_SUCCESS SmmEventCallback runs successfully
481 @retval EFI_NOT_FOUND The Fvb protocol for variable is not found.
482
483 **/
484 EFI_STATUS
485 EFIAPI
486 SmmFtwNotificationEvent (
487 IN CONST EFI_GUID *Protocol,
488 IN VOID *Interface,
489 IN EFI_HANDLE Handle
490 )
491 {
492 EFI_STATUS Status;
493 EFI_SMM_FIRMWARE_VOLUME_BLOCK_PROTOCOL *FvbProtocol;
494 EFI_SMM_FAULT_TOLERANT_WRITE_PROTOCOL *FtwProtocol;
495 EFI_PHYSICAL_ADDRESS NvStorageVariableBase;
496
497 if (mVariableModuleGlobal->FvbInstance != NULL) {
498 return EFI_SUCCESS;
499 }
500
501 //
502 // Ensure SMM FTW protocol is installed.
503 //
504 Status = GetFtwProtocol ((VOID **)&FtwProtocol);
505 if (EFI_ERROR (Status)) {
506 return Status;
507 }
508
509 //
510 // Find the proper FVB protocol for variable.
511 //
512 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet64 (PcdFlashNvStorageVariableBase64);
513 if (NvStorageVariableBase == 0) {
514 NvStorageVariableBase = (EFI_PHYSICAL_ADDRESS) PcdGet32 (PcdFlashNvStorageVariableBase);
515 }
516 Status = GetFvbInfoByAddress (NvStorageVariableBase, NULL, &FvbProtocol);
517 if (EFI_ERROR (Status)) {
518 return EFI_NOT_FOUND;
519 }
520
521 mVariableModuleGlobal->FvbInstance = FvbProtocol;
522
523 Status = VariableWriteServiceInitialize ();
524 ASSERT_EFI_ERROR (Status);
525
526 //
527 // Notify the variable wrapper driver the variable write service is ready
528 //
529 Status = gBS->InstallProtocolInterface (
530 &mSmmVariableHandle,
531 &gSmmVariableWriteGuid,
532 EFI_NATIVE_INTERFACE,
533 NULL
534 );
535 ASSERT_EFI_ERROR (Status);
536
537 return EFI_SUCCESS;
538 }
539
540
541 /**
542 Variable Driver main entry point. The Variable driver places the 4 EFI
543 runtime services in the EFI System Table and installs arch protocols
544 for variable read and write services being available. It also registers
545 a notification function for an EVT_SIGNAL_VIRTUAL_ADDRESS_CHANGE event.
546
547 @param[in] ImageHandle The firmware allocated handle for the EFI image.
548 @param[in] SystemTable A pointer to the EFI System Table.
549
550 @retval EFI_SUCCESS Variable service successfully initialized.
551
552 **/
553 EFI_STATUS
554 EFIAPI
555 VariableServiceInitialize (
556 IN EFI_HANDLE ImageHandle,
557 IN EFI_SYSTEM_TABLE *SystemTable
558 )
559 {
560 EFI_STATUS Status;
561 EFI_HANDLE VariableHandle;
562 VOID *SmmFtwRegistration;
563
564 //
565 // Variable initialize.
566 //
567 Status = VariableCommonInitialize ();
568 ASSERT_EFI_ERROR (Status);
569
570 //
571 // Install the Smm Variable Protocol on a new handle.
572 //
573 VariableHandle = NULL;
574 Status = gSmst->SmmInstallProtocolInterface (
575 &VariableHandle,
576 &gEfiSmmVariableProtocolGuid,
577 EFI_NATIVE_INTERFACE,
578 &gSmmVariable
579 );
580 ASSERT_EFI_ERROR (Status);
581
582 ///
583 /// Register SMM variable SMI handler
584 ///
585 VariableHandle = NULL;
586 Status = gSmst->SmiHandlerRegister (SmmVariableHandler, &gEfiSmmVariableProtocolGuid, &VariableHandle);
587 ASSERT_EFI_ERROR (Status);
588
589 //
590 // Notify the variable wrapper driver the variable service is ready
591 //
592 Status = SystemTable->BootServices->InstallProtocolInterface (
593 &mVariableHandle,
594 &gEfiSmmVariableProtocolGuid,
595 EFI_NATIVE_INTERFACE,
596 &gSmmVariable
597 );
598 ASSERT_EFI_ERROR (Status);
599
600 //
601 // Register FtwNotificationEvent () notify function.
602 //
603 Status = gSmst->SmmRegisterProtocolNotify (
604 &gEfiSmmFaultTolerantWriteProtocolGuid,
605 SmmFtwNotificationEvent,
606 &SmmFtwRegistration
607 );
608 ASSERT_EFI_ERROR (Status);
609
610 SmmFtwNotificationEvent (NULL, NULL, NULL);
611
612 return EFI_SUCCESS;
613 }
614
615