4 Caution: This module requires additional review when modified.
5 This driver will have external input - communicate buffer in SMM mode.
6 This external input must be validated carefully to avoid security issue like
7 buffer overflow, integer overflow.
9 SmmLockBoxHandler(), SmmLockBoxRestore(), SmmLockBoxUpdate(), SmmLockBoxSave()
10 will receive untrusted input and do basic validation.
12 Copyright (c) 2010 - 2015, Intel Corporation. All rights reserved.<BR>
14 This program and the accompanying materials
15 are licensed and made available under the terms and conditions
16 of the BSD License which accompanies this distribution. The
17 full text of the license may be found at
18 http://opensource.org/licenses/bsd-license.php
20 THE PROGRAM IS DISTRIBUTED UNDER THE BSD LICENSE ON AN "AS IS" BASIS,
21 WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
26 #include <Library/UefiDriverEntryPoint.h>
27 #include <Library/UefiBootServicesTableLib.h>
28 #include <Library/UefiRuntimeServicesTableLib.h>
29 #include <Library/SmmServicesTableLib.h>
30 #include <Library/BaseLib.h>
31 #include <Library/BaseMemoryLib.h>
32 #include <Library/DebugLib.h>
33 #include <Library/SmmMemLib.h>
34 #include <Library/LockBoxLib.h>
36 #include <Protocol/SmmReadyToLock.h>
37 #include <Protocol/SmmCommunication.h>
38 #include <Protocol/LockBox.h>
39 #include <Guid/SmmLockBox.h>
41 BOOLEAN mLocked
= FALSE
;
44 Dispatch function for SMM lock box save.
46 Caution: This function may receive untrusted input.
47 Restore buffer and length are external input, so this function will validate
50 @param LockBoxParameterSave parameter of lock box save
54 IN EFI_SMM_LOCK_BOX_PARAMETER_SAVE
*LockBoxParameterSave
58 EFI_SMM_LOCK_BOX_PARAMETER_SAVE TempLockBoxParameterSave
;
64 DEBUG ((EFI_D_ERROR
, "SmmLockBox Locked!\n"));
65 LockBoxParameterSave
->Header
.ReturnStatus
= (UINT64
)EFI_ACCESS_DENIED
;
69 CopyMem (&TempLockBoxParameterSave
, LockBoxParameterSave
, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SAVE
));
74 if (!SmmIsBufferOutsideSmmValid ((UINTN
)TempLockBoxParameterSave
.Buffer
, (UINTN
)TempLockBoxParameterSave
.Length
)) {
75 DEBUG ((EFI_D_ERROR
, "SmmLockBox Save address in SMRAM or buffer overflow!\n"));
76 LockBoxParameterSave
->Header
.ReturnStatus
= (UINT64
)EFI_ACCESS_DENIED
;
83 Status
= SaveLockBox (
84 &TempLockBoxParameterSave
.Guid
,
85 (VOID
*)(UINTN
)TempLockBoxParameterSave
.Buffer
,
86 (UINTN
)TempLockBoxParameterSave
.Length
88 LockBoxParameterSave
->Header
.ReturnStatus
= (UINT64
)Status
;
93 Dispatch function for SMM lock box set attributes.
95 @param LockBoxParameterSetAttributes parameter of lock box set attributes
98 SmmLockBoxSetAttributes (
99 IN EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES
*LockBoxParameterSetAttributes
103 EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES TempLockBoxParameterSetAttributes
;
109 DEBUG ((EFI_D_ERROR
, "SmmLockBox Locked!\n"));
110 LockBoxParameterSetAttributes
->Header
.ReturnStatus
= (UINT64
)EFI_ACCESS_DENIED
;
114 CopyMem (&TempLockBoxParameterSetAttributes
, LockBoxParameterSetAttributes
, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES
));
119 Status
= SetLockBoxAttributes (
120 &TempLockBoxParameterSetAttributes
.Guid
,
121 TempLockBoxParameterSetAttributes
.Attributes
123 LockBoxParameterSetAttributes
->Header
.ReturnStatus
= (UINT64
)Status
;
128 Dispatch function for SMM lock box update.
130 Caution: This function may receive untrusted input.
131 Restore buffer and length are external input, so this function will validate
134 @param LockBoxParameterUpdate parameter of lock box update
138 IN EFI_SMM_LOCK_BOX_PARAMETER_UPDATE
*LockBoxParameterUpdate
142 EFI_SMM_LOCK_BOX_PARAMETER_UPDATE TempLockBoxParameterUpdate
;
148 DEBUG ((EFI_D_ERROR
, "SmmLockBox Locked!\n"));
149 LockBoxParameterUpdate
->Header
.ReturnStatus
= (UINT64
)EFI_ACCESS_DENIED
;
153 CopyMem (&TempLockBoxParameterUpdate
, LockBoxParameterUpdate
, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE
));
158 if (!SmmIsBufferOutsideSmmValid ((UINTN
)TempLockBoxParameterUpdate
.Buffer
, (UINTN
)TempLockBoxParameterUpdate
.Length
)) {
159 DEBUG ((EFI_D_ERROR
, "SmmLockBox Update address in SMRAM or buffer overflow!\n"));
160 LockBoxParameterUpdate
->Header
.ReturnStatus
= (UINT64
)EFI_ACCESS_DENIED
;
167 Status
= UpdateLockBox (
168 &TempLockBoxParameterUpdate
.Guid
,
169 (UINTN
)TempLockBoxParameterUpdate
.Offset
,
170 (VOID
*)(UINTN
)TempLockBoxParameterUpdate
.Buffer
,
171 (UINTN
)TempLockBoxParameterUpdate
.Length
173 LockBoxParameterUpdate
->Header
.ReturnStatus
= (UINT64
)Status
;
178 Dispatch function for SMM lock box restore.
180 Caution: This function may receive untrusted input.
181 Restore buffer and length are external input, so this function will validate
184 @param LockBoxParameterRestore parameter of lock box restore
188 IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE
*LockBoxParameterRestore
192 EFI_SMM_LOCK_BOX_PARAMETER_RESTORE TempLockBoxParameterRestore
;
194 CopyMem (&TempLockBoxParameterRestore
, LockBoxParameterRestore
, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE
));
199 if (!SmmIsBufferOutsideSmmValid ((UINTN
)TempLockBoxParameterRestore
.Buffer
, (UINTN
)TempLockBoxParameterRestore
.Length
)) {
200 DEBUG ((EFI_D_ERROR
, "SmmLockBox Restore address in SMRAM or buffer overflow!\n"));
201 LockBoxParameterRestore
->Header
.ReturnStatus
= (UINT64
)EFI_ACCESS_DENIED
;
208 if ((TempLockBoxParameterRestore
.Length
== 0) && (TempLockBoxParameterRestore
.Buffer
== 0)) {
209 Status
= RestoreLockBox (
210 &TempLockBoxParameterRestore
.Guid
,
215 Status
= RestoreLockBox (
216 &TempLockBoxParameterRestore
.Guid
,
217 (VOID
*)(UINTN
)TempLockBoxParameterRestore
.Buffer
,
218 (UINTN
*)&TempLockBoxParameterRestore
.Length
221 LockBoxParameterRestore
->Header
.ReturnStatus
= (UINT64
)Status
;
226 Dispatch function for SMM lock box restore all in place.
228 @param LockBoxParameterRestoreAllInPlace parameter of lock box restore all in place
231 SmmLockBoxRestoreAllInPlace (
232 IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE
*LockBoxParameterRestoreAllInPlace
237 Status
= RestoreAllLockBoxInPlace ();
238 LockBoxParameterRestoreAllInPlace
->Header
.ReturnStatus
= (UINT64
)Status
;
243 Dispatch function for a Software SMI handler.
245 Caution: This function may receive untrusted input.
246 Communicate buffer and buffer size are external input, so this function will do basic validation.
248 @param DispatchHandle The unique handle assigned to this handler by SmiHandlerRegister().
249 @param Context Points to an optional handler context which was specified when the
250 handler was registered.
251 @param CommBuffer A pointer to a collection of data in memory that will
252 be conveyed from a non-SMM environment into an SMM environment.
253 @param CommBufferSize The size of the CommBuffer.
255 @retval EFI_SUCCESS Command is handled successfully.
261 IN EFI_HANDLE DispatchHandle
,
262 IN CONST VOID
*Context OPTIONAL
,
263 IN OUT VOID
*CommBuffer OPTIONAL
,
264 IN OUT UINTN
*CommBufferSize OPTIONAL
267 EFI_SMM_LOCK_BOX_PARAMETER_HEADER
*LockBoxParameterHeader
;
268 UINTN TempCommBufferSize
;
270 DEBUG ((EFI_D_ERROR
, "SmmLockBox SmmLockBoxHandler Enter\n"));
273 // If input is invalid, stop processing this SMI
275 if (CommBuffer
== NULL
|| CommBufferSize
== NULL
) {
279 TempCommBufferSize
= *CommBufferSize
;
284 if (TempCommBufferSize
< sizeof(EFI_SMM_LOCK_BOX_PARAMETER_HEADER
)) {
285 DEBUG ((EFI_D_ERROR
, "SmmLockBox Command Buffer Size invalid!\n"));
288 if (!SmmIsBufferOutsideSmmValid ((UINTN
)CommBuffer
, TempCommBufferSize
)) {
289 DEBUG ((EFI_D_ERROR
, "SmmLockBox Command Buffer in SMRAM or overflow!\n"));
293 LockBoxParameterHeader
= (EFI_SMM_LOCK_BOX_PARAMETER_HEADER
*)((UINTN
)CommBuffer
);
295 LockBoxParameterHeader
->ReturnStatus
= (UINT64
)-1;
297 DEBUG ((EFI_D_ERROR
, "SmmLockBox LockBoxParameterHeader - %x\n", (UINTN
)LockBoxParameterHeader
));
299 DEBUG ((EFI_D_ERROR
, "SmmLockBox Command - %x\n", (UINTN
)LockBoxParameterHeader
->Command
));
301 switch (LockBoxParameterHeader
->Command
) {
302 case EFI_SMM_LOCK_BOX_COMMAND_SAVE
:
303 if (TempCommBufferSize
< sizeof(EFI_SMM_LOCK_BOX_PARAMETER_SAVE
)) {
304 DEBUG ((EFI_D_ERROR
, "SmmLockBox Command Buffer Size for SAVE invalid!\n"));
307 SmmLockBoxSave ((EFI_SMM_LOCK_BOX_PARAMETER_SAVE
*)(UINTN
)LockBoxParameterHeader
);
309 case EFI_SMM_LOCK_BOX_COMMAND_UPDATE
:
310 if (TempCommBufferSize
< sizeof(EFI_SMM_LOCK_BOX_PARAMETER_UPDATE
)) {
311 DEBUG ((EFI_D_ERROR
, "SmmLockBox Command Buffer Size for UPDATE invalid!\n"));
314 SmmLockBoxUpdate ((EFI_SMM_LOCK_BOX_PARAMETER_UPDATE
*)(UINTN
)LockBoxParameterHeader
);
316 case EFI_SMM_LOCK_BOX_COMMAND_RESTORE
:
317 if (TempCommBufferSize
< sizeof(EFI_SMM_LOCK_BOX_PARAMETER_RESTORE
)) {
318 DEBUG ((EFI_D_ERROR
, "SmmLockBox Command Buffer Size for RESTORE invalid!\n"));
321 SmmLockBoxRestore ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE
*)(UINTN
)LockBoxParameterHeader
);
323 case EFI_SMM_LOCK_BOX_COMMAND_SET_ATTRIBUTES
:
324 if (TempCommBufferSize
< sizeof(EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES
)) {
325 DEBUG ((EFI_D_ERROR
, "SmmLockBox Command Buffer Size for SET_ATTRIBUTES invalid!\n"));
328 SmmLockBoxSetAttributes ((EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES
*)(UINTN
)LockBoxParameterHeader
);
330 case EFI_SMM_LOCK_BOX_COMMAND_RESTORE_ALL_IN_PLACE
:
331 if (TempCommBufferSize
< sizeof(EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE
)) {
332 DEBUG ((EFI_D_ERROR
, "SmmLockBox Command Buffer Size for RESTORE_ALL_IN_PLACE invalid!\n"));
335 SmmLockBoxRestoreAllInPlace ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE
*)(UINTN
)LockBoxParameterHeader
);
338 DEBUG ((EFI_D_ERROR
, "SmmLockBox Command invalid!\n"));
342 LockBoxParameterHeader
->Command
= (UINT32
)-1;
344 DEBUG ((EFI_D_ERROR
, "SmmLockBox SmmLockBoxHandler Exit\n"));
350 Smm Ready To Lock event notification handler.
352 It sets a flag indicating that SMRAM has been locked.
354 @param[in] Protocol Points to the protocol's unique identifier.
355 @param[in] Interface Points to the interface instance.
356 @param[in] Handle The handle on which the interface was installed.
358 @retval EFI_SUCCESS Notification handler runs successfully.
362 SmmReadyToLockEventNotify (
363 IN CONST EFI_GUID
*Protocol
,
373 Entry Point for LockBox SMM driver.
375 @param[in] ImageHandle Image handle of this driver.
376 @param[in] SystemTable A Pointer to the EFI System Table.
379 @return Others Some error occurs.
383 SmmLockBoxEntryPoint (
384 IN EFI_HANDLE ImageHandle
,
385 IN EFI_SYSTEM_TABLE
*SystemTable
389 EFI_HANDLE DispatchHandle
;
393 // Register LockBox communication handler
395 Status
= gSmst
->SmiHandlerRegister (
397 &gEfiSmmLockBoxCommunicationGuid
,
400 ASSERT_EFI_ERROR (Status
);
403 // Register SMM Ready To Lock Protocol notification
405 Status
= gSmst
->SmmRegisterProtocolNotify (
406 &gEfiSmmReadyToLockProtocolGuid
,
407 SmmReadyToLockEventNotify
,
410 ASSERT_EFI_ERROR (Status
);
413 // Install NULL to DXE data base as notify
416 Status
= gBS
->InstallProtocolInterface (
418 &gEfiLockBoxProtocolGuid
,
419 EFI_NATIVE_INTERFACE
,
422 ASSERT_EFI_ERROR (Status
);