public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [edk2-devel] [PATCH 0/3] Add Standalone MM Lockbox Driver
@ 2024-05-07  6:09 Yuanhao Xie
  2024-05-07  6:09 ` [edk2-devel] [PATCH 1/3] StandaloneMmPkg: Add LockBox Dependency DXE Driver Yuanhao Xie
                   ` (2 more replies)
  0 siblings, 3 replies; 10+ messages in thread
From: Yuanhao Xie @ 2024-05-07  6:09 UTC (permalink / raw)
  To: devel

The Lockbox Driver allows sensitive data to be securely stored in a
designated area, thus protected against unauthorized access.

This patch adds a Standalone MM Lockbox Driver with main modifications
xieyuanh (3):
  StandaloneMmPkg: Add LockBox Dependency DXE Driver
  MdeModulePkg: Refactors SmmLockBox.c.
  MdeModulePkg: Add Standalone MM Lockbox Driver.

 MdeModulePkg/MdeModulePkg.dsc                                             |   1 +
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c                    | 361 ++++++++++++-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf                  |   4 +++-
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c              | 384 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h              | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.c        |  84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.inf      |  56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.uni      |  14 ++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMmExtra.uni |  14 ++++++++++++++
 StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.c   |  52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.inf |  37 +++++++++++++++++++++++++++++++++++++
 StandaloneMmPkg/StandaloneMmPkg.dsc                                       |   3 ++-
 12 files changed, 807 insertions(+), 351 deletions(-)
 create mode 100644 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c
 create mode 100644 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h
 create mode 100644 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.c
 create mode 100644 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.inf
 create mode 100644 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.uni
 create mode 100644 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMmExtra.uni
 create mode 100644 StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.c
 create mode 100644 StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.inf

-- 
2.39.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118636): https://edk2.groups.io/g/devel/message/118636
Mute This Topic: https://groups.io/mt/105955698/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply	[flat|nested] 10+ messages in thread

* [edk2-devel] [PATCH 1/3] StandaloneMmPkg: Add LockBox Dependency DXE Driver
  2024-05-07  6:09 [edk2-devel] [PATCH 0/3] Add Standalone MM Lockbox Driver Yuanhao Xie
@ 2024-05-07  6:09 ` Yuanhao Xie
  2024-05-08  2:46   ` Ni, Ray
  2024-05-07  6:09 ` [edk2-devel] [PATCH 2/3] MdeModulePkg: Refactors SmmLockBox.c Yuanhao Xie
  2024-05-07  6:09 ` [edk2-devel] [PATCH 3/3] MdeModulePkg: Add Standalone MM Lockbox Driver Yuanhao Xie
  2 siblings, 1 reply; 10+ messages in thread
From: Yuanhao Xie @ 2024-05-07  6:09 UTC (permalink / raw)
  To: devel; +Cc: Liming Gao, Jiaxin Wu, Ray Ni, Yuanhao Xie

The LockBox Dependency DXE Driver is designed for use with standalone
mm where gBS are not accessible to indicates that LockBox API is ready
for use.

For DXE drivers use lockbox APIs via a communication mechanism
triggering an SMI, it's must to have the corresponding SMI handler
pre-installed for interrupt management. To ensure orderly operations
and proper notification, besides specified the guid in
the [Depex] section of the .inf file. The installation of smi handler,
along with the LockBox protocol marked by gEfiLockBoxProtocolGuid,
must be informed to the DXE driver. This protocol installation signifies
 to the DXE driver that the LockBox API is ready for use.

Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
Cc: Ray Ni <ray.ni@intel.com>

Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
---
 StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.c   | 52 ++++++++++++++++++++++++++++++++++++++++++++++++++++
 StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.inf | 37 +++++++++++++++++++++++++++++++++++++
 StandaloneMmPkg/StandaloneMmPkg.dsc                                       |  3 ++-
 3 files changed, 91 insertions(+), 1 deletion(-)

diff --git a/StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.c b/StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.c
new file mode 100644
index 0000000000..6445d4c569
--- /dev/null
+++ b/StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.c
@@ -0,0 +1,52 @@
+/** @file
+  LockBox Dependency DXE Driver.
+
+  By installing the LockBox protocol with the gEfiLockBoxProtocolGuid,
+  it signals that the LockBox API is fully operational and ready for use.
+  Drivers that intend to utilize the LockBox functionality at their entry
+  point should declare this dependency explicitly.
+
+  Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+
+  SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <Uefi.h>
+#include <Library/UefiBootServicesTableLib.h>
+#include <Library/DebugLib.h>
+#include <Library/BaseLib.h>
+#include <Protocol/LockBox.h>
+
+/**
+  It attempts to install the gEfiLockBoxProtocolGuid protocol into the system's DXE database
+  with NULL as the protocol interface to mark the protocol as handled in the system or to
+  act as a trigger.
+
+  @param  ImageHandle   The firmware allocated handle for the EFI image.
+  @param  SystemTable   A pointer to the Management mode System Table.
+
+  @retval EFI_SUCCESS           The protocol was successfully installed into the DXE database.
+  @retval Others                An error occurred while installing the protocol.
+**/
+EFI_STATUS
+EFIAPI
+SmmLockBoxMmDependencyConstructor (
+  IN EFI_HANDLE        ImageHandle,
+  IN EFI_SYSTEM_TABLE  *SystemTable
+  )
+{
+  EFI_STATUS  Status;
+
+  //
+  // Install NULL to DXE data base as notify
+  //
+  Status = gBS->InstallProtocolInterface (
+                  &ImageHandle,
+                  &gEfiLockBoxProtocolGuid,
+                  EFI_NATIVE_INTERFACE,
+                  NULL
+                  );
+  ASSERT_EFI_ERROR (Status);
+  return Status;
+}
diff --git a/StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.inf b/StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.inf
new file mode 100644
index 0000000000..7fc7b67d21
--- /dev/null
+++ b/StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.inf
@@ -0,0 +1,37 @@
+## @file
+# LockBox Dependency DXE Driver.
+
+# By installing the LockBox protocol with the gEfiLockBoxProtocolGuid,
+# it signals that the LockBox API is fully operational and ready for use.
+# Drivers that intend to utilize the LockBox functionality at their entry
+# point should declare this dependency explicitly.
+#
+# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+#
+# SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010006
+  BASE_NAME                      = SmmLockBoxMmDependency
+  FILE_GUID                      = c45ce910-7f8b-4f49-88e2-2c26c5743ee2
+  MODULE_TYPE                    = DXE_DRIVER
+  VERSION_STRING                 = 1.0
+  LIBRARY_CLASS                  = NULL
+  CONSTRUCTOR                    = SmmLockBoxMmDependencyConstructor
+
+[Sources]
+  SmmLockBoxMmDependency.c
+
+[Packages]
+  MdePkg/MdePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+
+[Protocols]
+  gEfiLockBoxProtocolGuid
+
+[LibraryClasses]
+  BaseLib
+  UefiBootServicesTableLib
diff --git a/StandaloneMmPkg/StandaloneMmPkg.dsc b/StandaloneMmPkg/StandaloneMmPkg.dsc
index 8012f93b7d..8a4f9a20ec 100644
--- a/StandaloneMmPkg/StandaloneMmPkg.dsc
+++ b/StandaloneMmPkg/StandaloneMmPkg.dsc
@@ -1,7 +1,7 @@
 ## @file
 # Standalone MM Platform.
 #
-# Copyright (c) 2015 - 2021, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2015 - 2024, Intel Corporation. All rights reserved.<BR>
 # Copyright (c) 2016 - 2021, Arm Limited. All rights reserved.<BR>
 # Copyright (C) Microsoft Corporation<BR>
 #
@@ -117,6 +117,7 @@
   StandaloneMmPkg/Library/StandaloneMmMemLib/StandaloneMmMemLib.inf
   StandaloneMmPkg/Library/StandaloneMmMemoryAllocationLib/StandaloneMmMemoryAllocationLib.inf
   StandaloneMmPkg/Library/VariableMmDependency/VariableMmDependency.inf
+  StandaloneMmPkg/Library/SmmLockBoxMmDependency/SmmLockBoxMmDependency.inf
 
 [Components.AARCH64, Components.ARM]
   StandaloneMmPkg/Drivers/StandaloneMmCpu/StandaloneMmCpu.inf
-- 
2.39.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118637): https://edk2.groups.io/g/devel/message/118637
Mute This Topic: https://groups.io/mt/105955699/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [edk2-devel] [PATCH 2/3] MdeModulePkg: Refactors SmmLockBox.c.
  2024-05-07  6:09 [edk2-devel] [PATCH 0/3] Add Standalone MM Lockbox Driver Yuanhao Xie
  2024-05-07  6:09 ` [edk2-devel] [PATCH 1/3] StandaloneMmPkg: Add LockBox Dependency DXE Driver Yuanhao Xie
@ 2024-05-07  6:09 ` Yuanhao Xie
  2024-05-08  2:50   ` Ni, Ray
  2024-05-09  3:41   ` Wu, Jiaxin
  2024-05-07  6:09 ` [edk2-devel] [PATCH 3/3] MdeModulePkg: Add Standalone MM Lockbox Driver Yuanhao Xie
  2 siblings, 2 replies; 10+ messages in thread
From: Yuanhao Xie @ 2024-05-07  6:09 UTC (permalink / raw)
  To: devel; +Cc: Liming Gao, Jiaxin Wu, Ray Ni, Yuanhao Xie

The Lockbox Driver allows sensitive data to be securely stored in a
designated area, thus protected against unauthorized access.

This patch does not introduce any functional modifications.
It refactors the existing logic into a common component to facilitates
the integration of the Standalone MM Lockbox Driver in an upcoming patch

Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
Cc: Ray Ni <ray.ni@intel.com>

Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
---
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c       | 361 ++++++++++++-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf     |   4 +++-
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c | 384 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 547 insertions(+), 350 deletions(-)

diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c
index c1e15c596b..2774979c34 100644
--- a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c
@@ -9,7 +9,7 @@
   SmmLockBoxHandler(), SmmLockBoxRestore(), SmmLockBoxUpdate(), SmmLockBoxSave()
   will receive untrusted input and do basic validation.
 
-Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.<BR>
 
 SPDX-License-Identifier: BSD-2-Clause-Patent
 
@@ -31,360 +31,24 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Protocol/LockBox.h>
 #include <Guid/SmmLockBox.h>
 
-BOOLEAN  mLocked = FALSE;
+#include "SmmLockBoxCommon.h"
 
 /**
-  Dispatch function for SMM lock box save.
+  This function is an abstraction layer for implementation specific Mm buffer validation routine.
 
-  Caution: This function may receive untrusted input.
-  Restore buffer and length are external input, so this function will validate
-  it is in SMRAM.
+  @param Buffer  The buffer start address to be checked.
+  @param Length  The buffer length to be checked.
 
-  @param LockBoxParameterSave  parameter of lock box save
+  @retval TRUE  This buffer is valid per processor architecture and not overlap with SMRAM.
+  @retval FALSE This buffer is not valid per processor architecture or overlap with SMRAM.
 **/
-VOID
-SmmLockBoxSave (
-  IN EFI_SMM_LOCK_BOX_PARAMETER_SAVE  *LockBoxParameterSave
+BOOLEAN
+IsBufferOutsideMmValid (
+  IN EFI_PHYSICAL_ADDRESS  Buffer,
+  IN UINT64                Length
   )
 {
-  EFI_STATUS                       Status;
-  EFI_SMM_LOCK_BOX_PARAMETER_SAVE  TempLockBoxParameterSave;
-
-  //
-  // Sanity check
-  //
-  if (mLocked) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
-    LockBoxParameterSave->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
-    return;
-  }
-
-  CopyMem (&TempLockBoxParameterSave, LockBoxParameterSave, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SAVE));
-
-  //
-  // Sanity check
-  //
-  if (!SmmIsBufferOutsideSmmValid ((UINTN)TempLockBoxParameterSave.Buffer, (UINTN)TempLockBoxParameterSave.Length)) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Save address in SMRAM or buffer overflow!\n"));
-    LockBoxParameterSave->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
-    return;
-  }
-
-  //
-  // The SpeculationBarrier() call here is to ensure the above range check for
-  // the CommBuffer have been completed before calling into SaveLockBox().
-  //
-  SpeculationBarrier ();
-
-  //
-  // Save data
-  //
-  Status = SaveLockBox (
-             &TempLockBoxParameterSave.Guid,
-             (VOID *)(UINTN)TempLockBoxParameterSave.Buffer,
-             (UINTN)TempLockBoxParameterSave.Length
-             );
-  LockBoxParameterSave->Header.ReturnStatus = (UINT64)Status;
-  return;
-}
-
-/**
-  Dispatch function for SMM lock box set attributes.
-
-  @param LockBoxParameterSetAttributes  parameter of lock box set attributes
-**/
-VOID
-SmmLockBoxSetAttributes (
-  IN EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES  *LockBoxParameterSetAttributes
-  )
-{
-  EFI_STATUS                                 Status;
-  EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES  TempLockBoxParameterSetAttributes;
-
-  //
-  // Sanity check
-  //
-  if (mLocked) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
-    LockBoxParameterSetAttributes->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
-    return;
-  }
-
-  CopyMem (&TempLockBoxParameterSetAttributes, LockBoxParameterSetAttributes, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES));
-
-  //
-  // Update data
-  //
-  Status = SetLockBoxAttributes (
-             &TempLockBoxParameterSetAttributes.Guid,
-             TempLockBoxParameterSetAttributes.Attributes
-             );
-  LockBoxParameterSetAttributes->Header.ReturnStatus = (UINT64)Status;
-  return;
-}
-
-/**
-  Dispatch function for SMM lock box update.
-
-  Caution: This function may receive untrusted input.
-  Restore buffer and length are external input, so this function will validate
-  it is in SMRAM.
-
-  @param LockBoxParameterUpdate  parameter of lock box update
-**/
-VOID
-SmmLockBoxUpdate (
-  IN EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  *LockBoxParameterUpdate
-  )
-{
-  EFI_STATUS                         Status;
-  EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  TempLockBoxParameterUpdate;
-
-  //
-  // Sanity check
-  //
-  if (mLocked) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
-    LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
-    return;
-  }
-
-  CopyMem (&TempLockBoxParameterUpdate, LockBoxParameterUpdate, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE));
-
-  //
-  // Sanity check
-  //
-  if (!SmmIsBufferOutsideSmmValid ((UINTN)TempLockBoxParameterUpdate.Buffer, (UINTN)TempLockBoxParameterUpdate.Length)) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Update address in SMRAM or buffer overflow!\n"));
-    LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
-    return;
-  }
-
-  //
-  // The SpeculationBarrier() call here is to ensure the above range check for
-  // the CommBuffer have been completed before calling into UpdateLockBox().
-  //
-  SpeculationBarrier ();
-
-  //
-  // Update data
-  //
-  Status = UpdateLockBox (
-             &TempLockBoxParameterUpdate.Guid,
-             (UINTN)TempLockBoxParameterUpdate.Offset,
-             (VOID *)(UINTN)TempLockBoxParameterUpdate.Buffer,
-             (UINTN)TempLockBoxParameterUpdate.Length
-             );
-  LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)Status;
-  return;
-}
-
-/**
-  Dispatch function for SMM lock box restore.
-
-  Caution: This function may receive untrusted input.
-  Restore buffer and length are external input, so this function will validate
-  it is in SMRAM.
-
-  @param LockBoxParameterRestore  parameter of lock box restore
-**/
-VOID
-SmmLockBoxRestore (
-  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE  *LockBoxParameterRestore
-  )
-{
-  EFI_STATUS                          Status;
-  EFI_SMM_LOCK_BOX_PARAMETER_RESTORE  TempLockBoxParameterRestore;
-
-  CopyMem (&TempLockBoxParameterRestore, LockBoxParameterRestore, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE));
-
-  //
-  // Sanity check
-  //
-  if (!SmmIsBufferOutsideSmmValid ((UINTN)TempLockBoxParameterRestore.Buffer, (UINTN)TempLockBoxParameterRestore.Length)) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Restore address in SMRAM or buffer overflow!\n"));
-    LockBoxParameterRestore->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
-    return;
-  }
-
-  //
-  // Restore data
-  //
-  if ((TempLockBoxParameterRestore.Length == 0) && (TempLockBoxParameterRestore.Buffer == 0)) {
-    Status = RestoreLockBox (
-               &TempLockBoxParameterRestore.Guid,
-               NULL,
-               NULL
-               );
-  } else {
-    Status = RestoreLockBox (
-               &TempLockBoxParameterRestore.Guid,
-               (VOID *)(UINTN)TempLockBoxParameterRestore.Buffer,
-               (UINTN *)&TempLockBoxParameterRestore.Length
-               );
-    if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)) {
-      //
-      // Return the actual Length value.
-      //
-      LockBoxParameterRestore->Length = TempLockBoxParameterRestore.Length;
-    }
-  }
-
-  LockBoxParameterRestore->Header.ReturnStatus = (UINT64)Status;
-  return;
-}
-
-/**
-  Dispatch function for SMM lock box restore all in place.
-
-  @param LockBoxParameterRestoreAllInPlace  parameter of lock box restore all in place
-**/
-VOID
-SmmLockBoxRestoreAllInPlace (
-  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE  *LockBoxParameterRestoreAllInPlace
-  )
-{
-  EFI_STATUS  Status;
-
-  Status                                                 = RestoreAllLockBoxInPlace ();
-  LockBoxParameterRestoreAllInPlace->Header.ReturnStatus = (UINT64)Status;
-  return;
-}
-
-/**
-  Dispatch function for a Software SMI handler.
-
-  Caution: This function may receive untrusted input.
-  Communicate buffer and buffer size are external input, so this function will do basic validation.
-
-  @param DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
-  @param Context         Points to an optional handler context which was specified when the
-                         handler was registered.
-  @param CommBuffer      A pointer to a collection of data in memory that will
-                         be conveyed from a non-SMM environment into an SMM environment.
-  @param CommBufferSize  The size of the CommBuffer.
-
-  @retval EFI_SUCCESS Command is handled successfully.
-
-**/
-EFI_STATUS
-EFIAPI
-SmmLockBoxHandler (
-  IN EFI_HANDLE  DispatchHandle,
-  IN CONST VOID  *Context         OPTIONAL,
-  IN OUT VOID    *CommBuffer      OPTIONAL,
-  IN OUT UINTN   *CommBufferSize  OPTIONAL
-  )
-{
-  EFI_SMM_LOCK_BOX_PARAMETER_HEADER  *LockBoxParameterHeader;
-  UINTN                              TempCommBufferSize;
-
-  DEBUG ((DEBUG_INFO, "SmmLockBox SmmLockBoxHandler Enter\n"));
-
-  //
-  // If input is invalid, stop processing this SMI
-  //
-  if ((CommBuffer == NULL) || (CommBufferSize == NULL)) {
-    return EFI_SUCCESS;
-  }
-
-  TempCommBufferSize = *CommBufferSize;
-
-  //
-  // Sanity check
-  //
-  if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_HEADER)) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size invalid!\n"));
-    return EFI_SUCCESS;
-  }
-
-  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer in SMRAM or overflow!\n"));
-    return EFI_SUCCESS;
-  }
-
-  LockBoxParameterHeader = (EFI_SMM_LOCK_BOX_PARAMETER_HEADER *)((UINTN)CommBuffer);
-
-  LockBoxParameterHeader->ReturnStatus = (UINT64)-1;
-
-  DEBUG ((DEBUG_INFO, "SmmLockBox LockBoxParameterHeader - %x\n", (UINTN)LockBoxParameterHeader));
-
-  DEBUG ((DEBUG_INFO, "SmmLockBox Command - %x\n", (UINTN)LockBoxParameterHeader->Command));
-
-  switch (LockBoxParameterHeader->Command) {
-    case EFI_SMM_LOCK_BOX_COMMAND_SAVE:
-      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SAVE)) {
-        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for SAVE invalid!\n"));
-        break;
-      }
-
-      SmmLockBoxSave ((EFI_SMM_LOCK_BOX_PARAMETER_SAVE *)(UINTN)LockBoxParameterHeader);
-      break;
-    case EFI_SMM_LOCK_BOX_COMMAND_UPDATE:
-      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE)) {
-        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for UPDATE invalid!\n"));
-        break;
-      }
-
-      SmmLockBoxUpdate ((EFI_SMM_LOCK_BOX_PARAMETER_UPDATE *)(UINTN)LockBoxParameterHeader);
-      break;
-    case EFI_SMM_LOCK_BOX_COMMAND_RESTORE:
-      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE)) {
-        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for RESTORE invalid!\n"));
-        break;
-      }
-
-      SmmLockBoxRestore ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE *)(UINTN)LockBoxParameterHeader);
-      break;
-    case EFI_SMM_LOCK_BOX_COMMAND_SET_ATTRIBUTES:
-      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES)) {
-        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for SET_ATTRIBUTES invalid!\n"));
-        break;
-      }
-
-      SmmLockBoxSetAttributes ((EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES *)(UINTN)LockBoxParameterHeader);
-      break;
-    case EFI_SMM_LOCK_BOX_COMMAND_RESTORE_ALL_IN_PLACE:
-      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE)) {
-        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for RESTORE_ALL_IN_PLACE invalid!\n"));
-        break;
-      }
-
-      SmmLockBoxRestoreAllInPlace ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE *)(UINTN)LockBoxParameterHeader);
-      break;
-    default:
-      DEBUG ((DEBUG_ERROR, "SmmLockBox Command invalid!\n"));
-      break;
-  }
-
-  LockBoxParameterHeader->Command = (UINT32)-1;
-
-  DEBUG ((DEBUG_INFO, "SmmLockBox SmmLockBoxHandler Exit\n"));
-
-  return EFI_SUCCESS;
-}
-
-/**
-  Smm Ready To Lock event notification handler.
-
-  It sets a flag indicating that SMRAM has been locked.
-
-  @param[in] Protocol   Points to the protocol's unique identifier.
-  @param[in] Interface  Points to the interface instance.
-  @param[in] Handle     The handle on which the interface was installed.
-
-  @retval EFI_SUCCESS   Notification handler runs successfully.
- **/
-EFI_STATUS
-EFIAPI
-SmmReadyToLockEventNotify (
-  IN CONST EFI_GUID  *Protocol,
-  IN VOID            *Interface,
-  IN EFI_HANDLE      Handle
-  )
-{
-  mLocked = TRUE;
-  return EFI_SUCCESS;
+  return SmmIsBufferOutsideSmmValid (Buffer, Length);
 }
 
 /**
@@ -438,6 +102,5 @@ SmmLockBoxEntryPoint (
                        NULL
                        );
   ASSERT_EFI_ERROR (Status);
-
   return Status;
 }
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
index 5081b2d7f2..f279706e90 100644
--- a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
@@ -6,7 +6,7 @@
 #  This external input must be validated carefully to avoid security issue like
 #  buffer overflow, integer overflow.
 #
-#  Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.<BR>
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -30,6 +30,8 @@
 
 [Sources]
   SmmLockBox.c
+  SmmLockBoxCommon.c
+  SmmLockBoxCommon.h
 
 [Packages]
   MdePkg/MdePkg.dec
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c
new file mode 100644
index 0000000000..5c6eae05af
--- /dev/null
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c
@@ -0,0 +1,384 @@
+/** @file
+  LockBox SMM/MM driver.
+
+  Caution: This module requires additional review when modified.
+  This driver will have external input - communicate buffer in SMM mode.
+  This external input must be validated carefully to avoid security issue like
+  buffer overflow, integer overflow.
+
+  SmmLockBoxHandler(), SmmLockBoxRestore(), SmmLockBoxUpdate(), SmmLockBoxSave()
+  will receive untrusted input and do basic validation.
+
+Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.<BR>
+
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiSmm.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/LockBoxLib.h>
+
+#include <Protocol/SmmReadyToLock.h>
+#include <Protocol/SmmCommunication.h>
+#include <Protocol/LockBox.h>
+#include <Guid/SmmLockBox.h>
+#include "SmmLockBoxCommon.h"
+
+BOOLEAN  mLocked = FALSE;
+
+/**
+  Dispatch function for SMM lock box save.
+
+  Caution: This function may receive untrusted input.
+  Restore buffer and length are external input, so this function will validate
+  it is in SMRAM.
+
+  @param LockBoxParameterSave  parameter of lock box save
+**/
+VOID
+SmmLockBoxSave (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_SAVE  *LockBoxParameterSave
+  )
+{
+  EFI_STATUS                       Status;
+  EFI_SMM_LOCK_BOX_PARAMETER_SAVE  TempLockBoxParameterSave;
+
+  //
+  // Sanity check
+  //
+  if (mLocked) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
+    LockBoxParameterSave->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
+    return;
+  }
+
+  CopyMem (&TempLockBoxParameterSave, LockBoxParameterSave, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SAVE));
+
+  //
+  // Sanity check
+  //
+  if (!IsBufferOutsideMmValid ((UINTN)TempLockBoxParameterSave.Buffer, (UINTN)TempLockBoxParameterSave.Length)) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Save address in SMRAM or buffer overflow!\n"));
+    LockBoxParameterSave->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
+    return;
+  }
+
+  //
+  // The SpeculationBarrier() call here is to ensure the above range check for
+  // the CommBuffer have been completed before calling into SaveLockBox().
+  //
+  SpeculationBarrier ();
+
+  //
+  // Save data
+  //
+  Status = SaveLockBox (
+             &TempLockBoxParameterSave.Guid,
+             (VOID *)(UINTN)TempLockBoxParameterSave.Buffer,
+             (UINTN)TempLockBoxParameterSave.Length
+             );
+  LockBoxParameterSave->Header.ReturnStatus = (UINT64)Status;
+  return;
+}
+
+/**
+  Dispatch function for SMM lock box set attributes.
+
+  @param LockBoxParameterSetAttributes  parameter of lock box set attributes
+**/
+VOID
+SmmLockBoxSetAttributes (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES  *LockBoxParameterSetAttributes
+  )
+{
+  EFI_STATUS                                 Status;
+  EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES  TempLockBoxParameterSetAttributes;
+
+  //
+  // Sanity check
+  //
+  if (mLocked) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
+    LockBoxParameterSetAttributes->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
+    return;
+  }
+
+  CopyMem (&TempLockBoxParameterSetAttributes, LockBoxParameterSetAttributes, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES));
+
+  //
+  // Update data
+  //
+  Status = SetLockBoxAttributes (
+             &TempLockBoxParameterSetAttributes.Guid,
+             TempLockBoxParameterSetAttributes.Attributes
+             );
+  LockBoxParameterSetAttributes->Header.ReturnStatus = (UINT64)Status;
+  return;
+}
+
+/**
+  Dispatch function for SMM lock box update.
+
+  Caution: This function may receive untrusted input.
+  Restore buffer and length are external input, so this function will validate
+  it is in SMRAM.
+
+  @param LockBoxParameterUpdate  parameter of lock box update
+**/
+VOID
+SmmLockBoxUpdate (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  *LockBoxParameterUpdate
+  )
+{
+  EFI_STATUS                         Status;
+  EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  TempLockBoxParameterUpdate;
+
+  //
+  // Sanity check
+  //
+  if (mLocked) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
+    LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
+    return;
+  }
+
+  CopyMem (&TempLockBoxParameterUpdate, LockBoxParameterUpdate, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE));
+
+  //
+  // Sanity check
+  //
+  if (!IsBufferOutsideMmValid ((UINTN)TempLockBoxParameterUpdate.Buffer, (UINTN)TempLockBoxParameterUpdate.Length)) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Update address in SMRAM or buffer overflow!\n"));
+    LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
+    return;
+  }
+
+  //
+  // The SpeculationBarrier() call here is to ensure the above range check for
+  // the CommBuffer have been completed before calling into UpdateLockBox().
+  //
+  SpeculationBarrier ();
+
+  //
+  // Update data
+  //
+  Status = UpdateLockBox (
+             &TempLockBoxParameterUpdate.Guid,
+             (UINTN)TempLockBoxParameterUpdate.Offset,
+             (VOID *)(UINTN)TempLockBoxParameterUpdate.Buffer,
+             (UINTN)TempLockBoxParameterUpdate.Length
+             );
+  LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)Status;
+  return;
+}
+
+/**
+  Dispatch function for SMM lock box restore.
+
+  Caution: This function may receive untrusted input.
+  Restore buffer and length are external input, so this function will validate
+  it is in SMRAM.
+
+  @param LockBoxParameterRestore  parameter of lock box restore
+**/
+VOID
+SmmLockBoxRestore (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE  *LockBoxParameterRestore
+  )
+{
+  EFI_STATUS                          Status;
+  EFI_SMM_LOCK_BOX_PARAMETER_RESTORE  TempLockBoxParameterRestore;
+
+  CopyMem (&TempLockBoxParameterRestore, LockBoxParameterRestore, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE));
+
+  //
+  // Sanity check
+  //
+  if (!IsBufferOutsideMmValid ((UINTN)TempLockBoxParameterRestore.Buffer, (UINTN)TempLockBoxParameterRestore.Length)) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Restore address in SMRAM or buffer overflow!\n"));
+    LockBoxParameterRestore->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
+    return;
+  }
+
+  //
+  // Restore data
+  //
+  if ((TempLockBoxParameterRestore.Length == 0) && (TempLockBoxParameterRestore.Buffer == 0)) {
+    Status = RestoreLockBox (
+               &TempLockBoxParameterRestore.Guid,
+               NULL,
+               NULL
+               );
+  } else {
+    Status = RestoreLockBox (
+               &TempLockBoxParameterRestore.Guid,
+               (VOID *)(UINTN)TempLockBoxParameterRestore.Buffer,
+               (UINTN *)&TempLockBoxParameterRestore.Length
+               );
+    if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)) {
+      //
+      // Return the actual Length value.
+      //
+      LockBoxParameterRestore->Length = TempLockBoxParameterRestore.Length;
+    }
+  }
+
+  LockBoxParameterRestore->Header.ReturnStatus = (UINT64)Status;
+  return;
+}
+
+/**
+  Dispatch function for SMM lock box restore all in place.
+
+  @param LockBoxParameterRestoreAllInPlace  parameter of lock box restore all in place
+**/
+VOID
+SmmLockBoxRestoreAllInPlace (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE  *LockBoxParameterRestoreAllInPlace
+  )
+{
+  EFI_STATUS  Status;
+
+  Status                                                 = RestoreAllLockBoxInPlace ();
+  LockBoxParameterRestoreAllInPlace->Header.ReturnStatus = (UINT64)Status;
+  return;
+}
+
+/**
+  Dispatch function for a Software SMI handler.
+
+  Caution: This function may receive untrusted input.
+  Communicate buffer and buffer size are external input, so this function will do basic validation.
+
+  @param DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
+  @param Context         Points to an optional handler context which was specified when the
+                         handler was registered.
+  @param CommBuffer      A pointer to a collection of data in memory that will
+                         be conveyed from a non-SMM environment into an SMM environment.
+  @param CommBufferSize  The size of the CommBuffer.
+
+  @retval EFI_SUCCESS Command is handled successfully.
+
+**/
+EFI_STATUS
+EFIAPI
+SmmLockBoxHandler (
+  IN EFI_HANDLE  DispatchHandle,
+  IN CONST VOID  *Context         OPTIONAL,
+  IN OUT VOID    *CommBuffer      OPTIONAL,
+  IN OUT UINTN   *CommBufferSize  OPTIONAL
+  )
+{
+  EFI_SMM_LOCK_BOX_PARAMETER_HEADER  *LockBoxParameterHeader;
+  UINTN                              TempCommBufferSize;
+
+  DEBUG ((DEBUG_INFO, "SmmLockBox SmmLockBoxHandler Enter\n"));
+
+  //
+  // If input is invalid, stop processing this SMI
+  //
+  if ((CommBuffer == NULL) || (CommBufferSize == NULL)) {
+    return EFI_SUCCESS;
+  }
+
+  TempCommBufferSize = *CommBufferSize;
+
+  //
+  // Sanity check
+  //
+  if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_HEADER)) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size invalid!\n"));
+    return EFI_SUCCESS;
+  }
+
+  if (!IsBufferOutsideMmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer in SMRAM or overflow!\n"));
+    return EFI_SUCCESS;
+  }
+
+  LockBoxParameterHeader = (EFI_SMM_LOCK_BOX_PARAMETER_HEADER *)((UINTN)CommBuffer);
+
+  LockBoxParameterHeader->ReturnStatus = (UINT64)-1;
+
+  DEBUG ((DEBUG_INFO, "SmmLockBox LockBoxParameterHeader - %x\n", (UINTN)LockBoxParameterHeader));
+
+  DEBUG ((DEBUG_INFO, "SmmLockBox Command - %x\n", (UINTN)LockBoxParameterHeader->Command));
+
+  switch (LockBoxParameterHeader->Command) {
+    case EFI_SMM_LOCK_BOX_COMMAND_SAVE:
+      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SAVE)) {
+        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for SAVE invalid!\n"));
+        break;
+      }
+
+      SmmLockBoxSave ((EFI_SMM_LOCK_BOX_PARAMETER_SAVE *)(UINTN)LockBoxParameterHeader);
+      break;
+    case EFI_SMM_LOCK_BOX_COMMAND_UPDATE:
+      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE)) {
+        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for UPDATE invalid!\n"));
+        break;
+      }
+
+      SmmLockBoxUpdate ((EFI_SMM_LOCK_BOX_PARAMETER_UPDATE *)(UINTN)LockBoxParameterHeader);
+      break;
+    case EFI_SMM_LOCK_BOX_COMMAND_RESTORE:
+      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE)) {
+        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for RESTORE invalid!\n"));
+        break;
+      }
+
+      SmmLockBoxRestore ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE *)(UINTN)LockBoxParameterHeader);
+      break;
+    case EFI_SMM_LOCK_BOX_COMMAND_SET_ATTRIBUTES:
+      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES)) {
+        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for SET_ATTRIBUTES invalid!\n"));
+        break;
+      }
+
+      SmmLockBoxSetAttributes ((EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES *)(UINTN)LockBoxParameterHeader);
+      break;
+    case EFI_SMM_LOCK_BOX_COMMAND_RESTORE_ALL_IN_PLACE:
+      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE)) {
+        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for RESTORE_ALL_IN_PLACE invalid!\n"));
+        break;
+      }
+
+      SmmLockBoxRestoreAllInPlace ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE *)(UINTN)LockBoxParameterHeader);
+      break;
+    default:
+      DEBUG ((DEBUG_ERROR, "SmmLockBox Command invalid!\n"));
+      break;
+  }
+
+  LockBoxParameterHeader->Command = (UINT32)-1;
+
+  DEBUG ((DEBUG_INFO, "SmmLockBox SmmLockBoxHandler Exit\n"));
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Smm Ready To Lock event notification handler.
+
+  It sets a flag indicating that SMRAM has been locked.
+
+  @param[in] Protocol   Points to the protocol's unique identifier.
+  @param[in] Interface  Points to the interface instance.
+  @param[in] Handle     The handle on which the interface was installed.
+
+  @retval EFI_SUCCESS   Notification handler runs successfully.
+ **/
+EFI_STATUS
+EFIAPI
+SmmReadyToLockEventNotify (
+  IN CONST EFI_GUID  *Protocol,
+  IN VOID            *Interface,
+  IN EFI_HANDLE      Handle
+  )
+{
+  mLocked = TRUE;
+  return EFI_SUCCESS;
+}
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h
new file mode 100644
index 0000000000..2205c4fb3b
--- /dev/null
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h
@@ -0,0 +1,148 @@
+/** @file
+  LockBox SMM/MM driver.
+
+  Caution: This module requires additional review when modified.
+  This driver will have external input - communicate buffer in SMM mode.
+  This external input must be validated carefully to avoid security issue like
+  buffer overflow, integer overflow.
+
+  SmmLockBoxHandler(), SmmLockBoxRestore(), SmmLockBoxUpdate(), SmmLockBoxSave()
+  will receive untrusted input and do basic validation.
+
+Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.<BR>
+
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiSmm.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/LockBoxLib.h>
+
+#include <Protocol/SmmReadyToLock.h>
+#include <Protocol/SmmCommunication.h>
+#include <Protocol/LockBox.h>
+#include <Guid/SmmLockBox.h>
+
+/**
+  This function is an abstraction layer for implementation specific Mm buffer validation routine.
+
+  @param Buffer  The buffer start address to be checked.
+  @param Length  The buffer length to be checked.
+
+  @retval TRUE  This buffer is valid per processor architecture and not overlap with SMRAM.
+  @retval FALSE This buffer is not valid per processor architecture or overlap with SMRAM.
+**/
+BOOLEAN
+IsBufferOutsideMmValid (
+  IN EFI_PHYSICAL_ADDRESS  Buffer,
+  IN UINT64                Length
+  );
+
+/**
+  Dispatch function for SMM lock box save.
+
+  Caution: This function may receive untrusted input.
+  Restore buffer and length are external input, so this function will validate
+  it is in SMRAM.
+
+  @param LockBoxParameterSave  parameter of lock box save
+**/
+VOID
+SmmLockBoxSave (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_SAVE  *LockBoxParameterSave
+  );
+
+/**
+  Dispatch function for SMM lock box set attributes.
+
+  @param LockBoxParameterSetAttributes  parameter of lock box set attributes
+**/
+VOID
+SmmLockBoxSetAttributes (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES  *LockBoxParameterSetAttributes
+  );
+
+/**
+  Dispatch function for SMM lock box update.
+
+  Caution: This function may receive untrusted input.
+  Restore buffer and length are external input, so this function will validate
+  it is in SMRAM.
+
+  @param LockBoxParameterUpdate  parameter of lock box update
+**/
+VOID
+SmmLockBoxUpdate (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  *LockBoxParameterUpdate
+  );
+
+/**
+  Dispatch function for SMM lock box restore.
+
+  Caution: This function may receive untrusted input.
+  Restore buffer and length are external input, so this function will validate
+  it is in SMRAM.
+
+  @param LockBoxParameterRestore  parameter of lock box restore
+**/
+VOID
+SmmLockBoxRestore (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE  *LockBoxParameterRestore
+  );
+
+/**
+  Dispatch function for SMM lock box restore all in place.
+
+  @param LockBoxParameterRestoreAllInPlace  parameter of lock box restore all in place
+**/
+VOID
+SmmLockBoxRestoreAllInPlace (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE  *LockBoxParameterRestoreAllInPlace
+  );
+
+/**
+  Dispatch function for a Software SMI handler.
+
+  Caution: This function may receive untrusted input.
+  Communicate buffer and buffer size are external input, so this function will do basic validation.
+
+  @param DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
+  @param Context         Points to an optional handler context which was specified when the
+                         handler was registered.
+  @param CommBuffer      A pointer to a collection of data in memory that will
+                         be conveyed from a non-SMM environment into an SMM environment.
+  @param CommBufferSize  The size of the CommBuffer.
+
+  @retval EFI_SUCCESS Command is handled successfully.
+
+**/
+EFI_STATUS
+EFIAPI
+SmmLockBoxHandler (
+  IN EFI_HANDLE  DispatchHandle,
+  IN CONST VOID  *Context         OPTIONAL,
+  IN OUT VOID    *CommBuffer      OPTIONAL,
+  IN OUT UINTN   *CommBufferSize  OPTIONAL
+  );
+
+/**
+  Smm Ready To Lock event notification handler.
+
+  It sets a flag indicating that SMRAM has been locked.
+
+  @param[in] Protocol   Points to the protocol's unique identifier.
+  @param[in] Interface  Points to the interface instance.
+  @param[in] Handle     The handle on which the interface was installed.
+
+  @retval EFI_SUCCESS   Notification handler runs successfully.
+ **/
+EFI_STATUS
+EFIAPI
+SmmReadyToLockEventNotify (
+  IN CONST EFI_GUID  *Protocol,
+  IN VOID            *Interface,
+  IN EFI_HANDLE      Handle
+  );
-- 
2.39.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118638): https://edk2.groups.io/g/devel/message/118638
Mute This Topic: https://groups.io/mt/105955700/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* [edk2-devel] [PATCH 3/3] MdeModulePkg: Add Standalone MM Lockbox Driver.
  2024-05-07  6:09 [edk2-devel] [PATCH 0/3] Add Standalone MM Lockbox Driver Yuanhao Xie
  2024-05-07  6:09 ` [edk2-devel] [PATCH 1/3] StandaloneMmPkg: Add LockBox Dependency DXE Driver Yuanhao Xie
  2024-05-07  6:09 ` [edk2-devel] [PATCH 2/3] MdeModulePkg: Refactors SmmLockBox.c Yuanhao Xie
@ 2024-05-07  6:09 ` Yuanhao Xie
  2024-05-08  2:53   ` Ni, Ray
  2024-05-09  3:42   ` Wu, Jiaxin
  2 siblings, 2 replies; 10+ messages in thread
From: Yuanhao Xie @ 2024-05-07  6:09 UTC (permalink / raw)
  To: devel; +Cc: Liming Gao, Jiaxin Wu, Ray Ni, Yuanhao Xie

The Lockbox Driver allows sensitive data to be securely stored in a
designated area, thus protected against unauthorized access.

This patch adds a Standalone MM Lockbox Driver with main modifications:
1. Separating shared code between the Standalone MM driver and the
DXE MM Driver.
2. Utilizing services from the SMM Services Table (gSmst) as opposed to
 relying on Boot Services.

Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
Cc: Ray Ni <ray.ni@intel.com>

Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
---
 MdeModulePkg/MdeModulePkg.dsc                                             |  1 +
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.c        | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.inf      | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.uni      | 14 ++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMmExtra.uni | 14 ++++++++++++++
 5 files changed, 169 insertions(+)

diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index 6bed9205ea..f0f02f180f 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -500,6 +500,7 @@
   MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.inf
   MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterStandaloneMm.inf
   MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
+  MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.inf
   MdeModulePkg/Library/SmmMemoryAllocationProfileLib/SmmMemoryAllocationProfileLib.inf
   MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationProfileLib.inf
   MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationLib.inf
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.c b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.c
new file mode 100644
index 0000000000..503be7efa8
--- /dev/null
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.c
@@ -0,0 +1,84 @@
+/** @file
+  LockBox MM driver.
+
+Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiSmm.h>
+#include <Library/StandaloneMmDriverEntryPoint.h>
+#include <Library/MmServicesTableLib.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/StandaloneMmMemLib.h>
+#include <Library/LockBoxLib.h>
+
+#include <Protocol/SmmReadyToLock.h>
+#include <Protocol/SmmCommunication.h>
+#include <Protocol/LockBox.h>
+#include <Guid/SmmLockBox.h>
+
+#include "SmmLockBoxCommon.h"
+
+/**
+  This function is an abstraction layer for implementation specific Mm buffer validation routine.
+
+  @param Buffer  The buffer start address to be checked.
+  @param Length  The buffer length to be checked.
+
+  @retval TRUE  This buffer is valid per processor architecture and not overlap with SMRAM.
+  @retval FALSE This buffer is not valid per processor architecture or overlap with SMRAM.
+**/
+BOOLEAN
+IsBufferOutsideMmValid (
+  IN EFI_PHYSICAL_ADDRESS  Buffer,
+  IN UINT64                Length
+  )
+{
+  return MmIsBufferOutsideMmValid (Buffer, Length);
+}
+
+/**
+  Entry Point for LockBox MM driver.
+
+  @param[in] ImageHandle  Image handle of this driver.
+  @param[in] SystemTable  A Pointer to the EFI System Table.
+
+  @retval EFI_SUCEESS
+  @return Others          Some error occurs.
+**/
+EFI_STATUS
+EFIAPI
+SmmLockBoxStandaloneMmEntryPoint (
+  IN EFI_HANDLE           ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE  *SystemTable
+  )
+{
+  EFI_STATUS  Status;
+  EFI_HANDLE  DispatchHandle;
+  VOID        *Registration;
+
+  //
+  // Register LockBox communication handler
+  //
+  Status = gMmst->MmiHandlerRegister (
+                    SmmLockBoxHandler,
+                    &gEfiSmmLockBoxCommunicationGuid,
+                    &DispatchHandle
+                    );
+  ASSERT_EFI_ERROR (Status);
+
+  //
+  // Register SMM Ready To Lock Protocol notification
+  //
+  Status = gMmst->MmRegisterProtocolNotify (
+                    &gEfiSmmReadyToLockProtocolGuid,
+                    SmmReadyToLockEventNotify,
+                    &Registration
+                    );
+  ASSERT_EFI_ERROR (Status);
+  return Status;
+}
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.inf b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.inf
new file mode 100644
index 0000000000..544c87790c
--- /dev/null
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.inf
@@ -0,0 +1,56 @@
+## @file
+#  LockBox MM driver.
+#
+# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = SmmLockBoxStandaloneMm
+  MODULE_UNI_FILE                = SmmLockBoxStandaloneMm.uni
+  FILE_GUID                      = a83a87a0-8a3e-482d-86c8-84a139f6ded0
+  MODULE_TYPE                    = MM_STANDALONE
+  VERSION_STRING                 = 1.0
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  ENTRY_POINT                    = SmmLockBoxStandaloneMmEntryPoint
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+#  VALID_ARCHITECTURES           = IA32 X64
+#
+
+[Sources]
+  SmmLockBoxStandaloneMm.c
+  SmmLockBoxCommon.c
+  SmmLockBoxCommon.h
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+
+[LibraryClasses]
+  MmServicesTableLib
+  BaseLib
+  BaseMemoryLib
+  DebugLib
+  LockBoxLib
+  MemLib
+  StandaloneMmDriverEntryPoint
+
+[Guids]
+  gEfiSmmLockBoxCommunicationGuid   ## PRODUCES ## GUID # SmiHandlerRegister
+
+[Protocols]
+  gEfiSmmReadyToLockProtocolGuid    ## NOTIFY
+  gEfiLockBoxProtocolGuid           ## PRODUCES
+
+[Depex]
+  TRUE
+
+[UserExtensions.TianoCore."ExtraFiles"]
+  SmmLockBoxStandaloneMm.uni
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.uni b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.uni
new file mode 100644
index 0000000000..7f6218102f
--- /dev/null
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.uni
@@ -0,0 +1,14 @@
+// /** @file
+// LockBox MM driver.
+//
+// Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+
+#string STR_MODULE_ABSTRACT             #language en-US "LockBox MM driver."
+
+#string STR_MODULE_DESCRIPTION          #language en-US "LockBox MM driver."
+
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMmExtra.uni b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMmExtra.uni
new file mode 100644
index 0000000000..a5443ca5f9
--- /dev/null
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMmExtra.uni
@@ -0,0 +1,14 @@
+// /** @file
+// SmmLockBox Localized Strings and Content
+//
+// Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+#string STR_PROPERTIES_MODULE_NAME
+#language en-US
+"MM Lock Box Driver"
+
+
-- 
2.39.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118639): https://edk2.groups.io/g/devel/message/118639
Mute This Topic: https://groups.io/mt/105955701/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [edk2-devel] [PATCH 1/3] StandaloneMmPkg: Add LockBox Dependency DXE Driver
  2024-05-07  6:09 ` [edk2-devel] [PATCH 1/3] StandaloneMmPkg: Add LockBox Dependency DXE Driver Yuanhao Xie
@ 2024-05-08  2:46   ` Ni, Ray
  2024-05-09  3:42     ` Wu, Jiaxin
  0 siblings, 1 reply; 10+ messages in thread
From: Ni, Ray @ 2024-05-08  2:46 UTC (permalink / raw)
  To: Xie, Yuanhao, devel@edk2.groups.io; +Cc: Liming Gao, Wu, Jiaxin

[-- Attachment #1: Type: text/plain, Size: 810 bytes --]


+#include <Library/BaseLib.h>

[Ray] Can you check if BaseLib is really needed?

+#include <Protocol/LockBox.h>
+
+/**
+  It attempts to install the gEfiLockBoxProtocolGuid protocol into the system's DXE database
+  with NULL as the protocol interface to mark the protocol as handled in the system or to
+  act as a trigger.
[Ray] "mark the protocol as handled in the system or to act as a trigger", I don't quite understand it.



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118653): https://edk2.groups.io/g/devel/message/118653
Mute This Topic: https://groups.io/mt/105955699/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



[-- Attachment #2: Type: text/html, Size: 2137 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [edk2-devel] [PATCH 2/3] MdeModulePkg: Refactors SmmLockBox.c.
  2024-05-07  6:09 ` [edk2-devel] [PATCH 2/3] MdeModulePkg: Refactors SmmLockBox.c Yuanhao Xie
@ 2024-05-08  2:50   ` Ni, Ray
  2024-05-09  3:41   ` Wu, Jiaxin
  1 sibling, 0 replies; 10+ messages in thread
From: Ni, Ray @ 2024-05-08  2:50 UTC (permalink / raw)
  To: Xie, Yuanhao, devel@edk2.groups.io; +Cc: Liming Gao, Wu, Jiaxin

[-- Attachment #1: Type: text/plain, Size: 34690 bytes --]

Reviewed-by: Ray Ni <ray.ni@intel.com>

Thanks,
Ray
________________________________
From: Xie, Yuanhao <yuanhao.xie@intel.com>
Sent: Tuesday, May 7, 2024 14:09
To: devel@edk2.groups.io <devel@edk2.groups.io>
Cc: Liming Gao <gaoliming@byosoft.com.cn>; Wu, Jiaxin <jiaxin.wu@intel.com>; Ni, Ray <ray.ni@intel.com>; Xie, Yuanhao <yuanhao.xie@intel.com>
Subject: [PATCH 2/3] MdeModulePkg: Refactors SmmLockBox.c.

The Lockbox Driver allows sensitive data to be securely stored in a
designated area, thus protected against unauthorized access.

This patch does not introduce any functional modifications.
It refactors the existing logic into a common component to facilitates
the integration of the Standalone MM Lockbox Driver in an upcoming patch

Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
Cc: Ray Ni <ray.ni@intel.com>

Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
---
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c       | 361 ++++++++++++-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf     |   4 +++-
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c | 384 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 547 insertions(+), 350 deletions(-)

diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c
index c1e15c596b..2774979c34 100644
--- a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c
@@ -9,7 +9,7 @@
   SmmLockBoxHandler(), SmmLockBoxRestore(), SmmLockBoxUpdate(), SmmLockBoxSave()
   will receive untrusted input and do basic validation.

-Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
+Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.<BR>

 SPDX-License-Identifier: BSD-2-Clause-Patent

@@ -31,360 +31,24 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
 #include <Protocol/LockBox.h>
 #include <Guid/SmmLockBox.h>

-BOOLEAN  mLocked = FALSE;
+#include "SmmLockBoxCommon.h"

 /**
-  Dispatch function for SMM lock box save.
+  This function is an abstraction layer for implementation specific Mm buffer validation routine.

-  Caution: This function may receive untrusted input.
-  Restore buffer and length are external input, so this function will validate
-  it is in SMRAM.
+  @param Buffer  The buffer start address to be checked.
+  @param Length  The buffer length to be checked.

-  @param LockBoxParameterSave  parameter of lock box save
+  @retval TRUE  This buffer is valid per processor architecture and not overlap with SMRAM.
+  @retval FALSE This buffer is not valid per processor architecture or overlap with SMRAM.
 **/
-VOID
-SmmLockBoxSave (
-  IN EFI_SMM_LOCK_BOX_PARAMETER_SAVE  *LockBoxParameterSave
+BOOLEAN
+IsBufferOutsideMmValid (
+  IN EFI_PHYSICAL_ADDRESS  Buffer,
+  IN UINT64                Length
   )
 {
-  EFI_STATUS                       Status;
-  EFI_SMM_LOCK_BOX_PARAMETER_SAVE  TempLockBoxParameterSave;
-
-  //
-  // Sanity check
-  //
-  if (mLocked) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
-    LockBoxParameterSave->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
-    return;
-  }
-
-  CopyMem (&TempLockBoxParameterSave, LockBoxParameterSave, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SAVE));
-
-  //
-  // Sanity check
-  //
-  if (!SmmIsBufferOutsideSmmValid ((UINTN)TempLockBoxParameterSave.Buffer, (UINTN)TempLockBoxParameterSave.Length)) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Save address in SMRAM or buffer overflow!\n"));
-    LockBoxParameterSave->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
-    return;
-  }
-
-  //
-  // The SpeculationBarrier() call here is to ensure the above range check for
-  // the CommBuffer have been completed before calling into SaveLockBox().
-  //
-  SpeculationBarrier ();
-
-  //
-  // Save data
-  //
-  Status = SaveLockBox (
-             &TempLockBoxParameterSave.Guid,
-             (VOID *)(UINTN)TempLockBoxParameterSave.Buffer,
-             (UINTN)TempLockBoxParameterSave.Length
-             );
-  LockBoxParameterSave->Header.ReturnStatus = (UINT64)Status;
-  return;
-}
-
-/**
-  Dispatch function for SMM lock box set attributes.
-
-  @param LockBoxParameterSetAttributes  parameter of lock box set attributes
-**/
-VOID
-SmmLockBoxSetAttributes (
-  IN EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES  *LockBoxParameterSetAttributes
-  )
-{
-  EFI_STATUS                                 Status;
-  EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES  TempLockBoxParameterSetAttributes;
-
-  //
-  // Sanity check
-  //
-  if (mLocked) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
-    LockBoxParameterSetAttributes->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
-    return;
-  }
-
-  CopyMem (&TempLockBoxParameterSetAttributes, LockBoxParameterSetAttributes, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES));
-
-  //
-  // Update data
-  //
-  Status = SetLockBoxAttributes (
-             &TempLockBoxParameterSetAttributes.Guid,
-             TempLockBoxParameterSetAttributes.Attributes
-             );
-  LockBoxParameterSetAttributes->Header.ReturnStatus = (UINT64)Status;
-  return;
-}
-
-/**
-  Dispatch function for SMM lock box update.
-
-  Caution: This function may receive untrusted input.
-  Restore buffer and length are external input, so this function will validate
-  it is in SMRAM.
-
-  @param LockBoxParameterUpdate  parameter of lock box update
-**/
-VOID
-SmmLockBoxUpdate (
-  IN EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  *LockBoxParameterUpdate
-  )
-{
-  EFI_STATUS                         Status;
-  EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  TempLockBoxParameterUpdate;
-
-  //
-  // Sanity check
-  //
-  if (mLocked) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
-    LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
-    return;
-  }
-
-  CopyMem (&TempLockBoxParameterUpdate, LockBoxParameterUpdate, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE));
-
-  //
-  // Sanity check
-  //
-  if (!SmmIsBufferOutsideSmmValid ((UINTN)TempLockBoxParameterUpdate.Buffer, (UINTN)TempLockBoxParameterUpdate.Length)) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Update address in SMRAM or buffer overflow!\n"));
-    LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
-    return;
-  }
-
-  //
-  // The SpeculationBarrier() call here is to ensure the above range check for
-  // the CommBuffer have been completed before calling into UpdateLockBox().
-  //
-  SpeculationBarrier ();
-
-  //
-  // Update data
-  //
-  Status = UpdateLockBox (
-             &TempLockBoxParameterUpdate.Guid,
-             (UINTN)TempLockBoxParameterUpdate.Offset,
-             (VOID *)(UINTN)TempLockBoxParameterUpdate.Buffer,
-             (UINTN)TempLockBoxParameterUpdate.Length
-             );
-  LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)Status;
-  return;
-}
-
-/**
-  Dispatch function for SMM lock box restore.
-
-  Caution: This function may receive untrusted input.
-  Restore buffer and length are external input, so this function will validate
-  it is in SMRAM.
-
-  @param LockBoxParameterRestore  parameter of lock box restore
-**/
-VOID
-SmmLockBoxRestore (
-  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE  *LockBoxParameterRestore
-  )
-{
-  EFI_STATUS                          Status;
-  EFI_SMM_LOCK_BOX_PARAMETER_RESTORE  TempLockBoxParameterRestore;
-
-  CopyMem (&TempLockBoxParameterRestore, LockBoxParameterRestore, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE));
-
-  //
-  // Sanity check
-  //
-  if (!SmmIsBufferOutsideSmmValid ((UINTN)TempLockBoxParameterRestore.Buffer, (UINTN)TempLockBoxParameterRestore.Length)) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Restore address in SMRAM or buffer overflow!\n"));
-    LockBoxParameterRestore->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
-    return;
-  }
-
-  //
-  // Restore data
-  //
-  if ((TempLockBoxParameterRestore.Length == 0) && (TempLockBoxParameterRestore.Buffer == 0)) {
-    Status = RestoreLockBox (
-               &TempLockBoxParameterRestore.Guid,
-               NULL,
-               NULL
-               );
-  } else {
-    Status = RestoreLockBox (
-               &TempLockBoxParameterRestore.Guid,
-               (VOID *)(UINTN)TempLockBoxParameterRestore.Buffer,
-               (UINTN *)&TempLockBoxParameterRestore.Length
-               );
-    if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)) {
-      //
-      // Return the actual Length value.
-      //
-      LockBoxParameterRestore->Length = TempLockBoxParameterRestore.Length;
-    }
-  }
-
-  LockBoxParameterRestore->Header.ReturnStatus = (UINT64)Status;
-  return;
-}
-
-/**
-  Dispatch function for SMM lock box restore all in place.
-
-  @param LockBoxParameterRestoreAllInPlace  parameter of lock box restore all in place
-**/
-VOID
-SmmLockBoxRestoreAllInPlace (
-  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE  *LockBoxParameterRestoreAllInPlace
-  )
-{
-  EFI_STATUS  Status;
-
-  Status                                                 = RestoreAllLockBoxInPlace ();
-  LockBoxParameterRestoreAllInPlace->Header.ReturnStatus = (UINT64)Status;
-  return;
-}
-
-/**
-  Dispatch function for a Software SMI handler.
-
-  Caution: This function may receive untrusted input.
-  Communicate buffer and buffer size are external input, so this function will do basic validation.
-
-  @param DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
-  @param Context         Points to an optional handler context which was specified when the
-                         handler was registered.
-  @param CommBuffer      A pointer to a collection of data in memory that will
-                         be conveyed from a non-SMM environment into an SMM environment.
-  @param CommBufferSize  The size of the CommBuffer.
-
-  @retval EFI_SUCCESS Command is handled successfully.
-
-**/
-EFI_STATUS
-EFIAPI
-SmmLockBoxHandler (
-  IN EFI_HANDLE  DispatchHandle,
-  IN CONST VOID  *Context         OPTIONAL,
-  IN OUT VOID    *CommBuffer      OPTIONAL,
-  IN OUT UINTN   *CommBufferSize  OPTIONAL
-  )
-{
-  EFI_SMM_LOCK_BOX_PARAMETER_HEADER  *LockBoxParameterHeader;
-  UINTN                              TempCommBufferSize;
-
-  DEBUG ((DEBUG_INFO, "SmmLockBox SmmLockBoxHandler Enter\n"));
-
-  //
-  // If input is invalid, stop processing this SMI
-  //
-  if ((CommBuffer == NULL) || (CommBufferSize == NULL)) {
-    return EFI_SUCCESS;
-  }
-
-  TempCommBufferSize = *CommBufferSize;
-
-  //
-  // Sanity check
-  //
-  if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_HEADER)) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size invalid!\n"));
-    return EFI_SUCCESS;
-  }
-
-  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
-    DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer in SMRAM or overflow!\n"));
-    return EFI_SUCCESS;
-  }
-
-  LockBoxParameterHeader = (EFI_SMM_LOCK_BOX_PARAMETER_HEADER *)((UINTN)CommBuffer);
-
-  LockBoxParameterHeader->ReturnStatus = (UINT64)-1;
-
-  DEBUG ((DEBUG_INFO, "SmmLockBox LockBoxParameterHeader - %x\n", (UINTN)LockBoxParameterHeader));
-
-  DEBUG ((DEBUG_INFO, "SmmLockBox Command - %x\n", (UINTN)LockBoxParameterHeader->Command));
-
-  switch (LockBoxParameterHeader->Command) {
-    case EFI_SMM_LOCK_BOX_COMMAND_SAVE:
-      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SAVE)) {
-        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for SAVE invalid!\n"));
-        break;
-      }
-
-      SmmLockBoxSave ((EFI_SMM_LOCK_BOX_PARAMETER_SAVE *)(UINTN)LockBoxParameterHeader);
-      break;
-    case EFI_SMM_LOCK_BOX_COMMAND_UPDATE:
-      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE)) {
-        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for UPDATE invalid!\n"));
-        break;
-      }
-
-      SmmLockBoxUpdate ((EFI_SMM_LOCK_BOX_PARAMETER_UPDATE *)(UINTN)LockBoxParameterHeader);
-      break;
-    case EFI_SMM_LOCK_BOX_COMMAND_RESTORE:
-      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE)) {
-        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for RESTORE invalid!\n"));
-        break;
-      }
-
-      SmmLockBoxRestore ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE *)(UINTN)LockBoxParameterHeader);
-      break;
-    case EFI_SMM_LOCK_BOX_COMMAND_SET_ATTRIBUTES:
-      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES)) {
-        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for SET_ATTRIBUTES invalid!\n"));
-        break;
-      }
-
-      SmmLockBoxSetAttributes ((EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES *)(UINTN)LockBoxParameterHeader);
-      break;
-    case EFI_SMM_LOCK_BOX_COMMAND_RESTORE_ALL_IN_PLACE:
-      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE)) {
-        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for RESTORE_ALL_IN_PLACE invalid!\n"));
-        break;
-      }
-
-      SmmLockBoxRestoreAllInPlace ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE *)(UINTN)LockBoxParameterHeader);
-      break;
-    default:
-      DEBUG ((DEBUG_ERROR, "SmmLockBox Command invalid!\n"));
-      break;
-  }
-
-  LockBoxParameterHeader->Command = (UINT32)-1;
-
-  DEBUG ((DEBUG_INFO, "SmmLockBox SmmLockBoxHandler Exit\n"));
-
-  return EFI_SUCCESS;
-}
-
-/**
-  Smm Ready To Lock event notification handler.
-
-  It sets a flag indicating that SMRAM has been locked.
-
-  @param[in] Protocol   Points to the protocol's unique identifier.
-  @param[in] Interface  Points to the interface instance.
-  @param[in] Handle     The handle on which the interface was installed.
-
-  @retval EFI_SUCCESS   Notification handler runs successfully.
- **/
-EFI_STATUS
-EFIAPI
-SmmReadyToLockEventNotify (
-  IN CONST EFI_GUID  *Protocol,
-  IN VOID            *Interface,
-  IN EFI_HANDLE      Handle
-  )
-{
-  mLocked = TRUE;
-  return EFI_SUCCESS;
+  return SmmIsBufferOutsideSmmValid (Buffer, Length);
 }

 /**
@@ -438,6 +102,5 @@ SmmLockBoxEntryPoint (
                        NULL
                        );
   ASSERT_EFI_ERROR (Status);
-
   return Status;
 }
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
index 5081b2d7f2..f279706e90 100644
--- a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
@@ -6,7 +6,7 @@
 #  This external input must be validated carefully to avoid security issue like
 #  buffer overflow, integer overflow.
 #
-#  Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
+#  Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.<BR>
 #
 #  SPDX-License-Identifier: BSD-2-Clause-Patent
 #
@@ -30,6 +30,8 @@

 [Sources]
   SmmLockBox.c
+  SmmLockBoxCommon.c
+  SmmLockBoxCommon.h

 [Packages]
   MdePkg/MdePkg.dec
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c
new file mode 100644
index 0000000000..5c6eae05af
--- /dev/null
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c
@@ -0,0 +1,384 @@
+/** @file
+  LockBox SMM/MM driver.
+
+  Caution: This module requires additional review when modified.
+  This driver will have external input - communicate buffer in SMM mode.
+  This external input must be validated carefully to avoid security issue like
+  buffer overflow, integer overflow.
+
+  SmmLockBoxHandler(), SmmLockBoxRestore(), SmmLockBoxUpdate(), SmmLockBoxSave()
+  will receive untrusted input and do basic validation.
+
+Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.<BR>
+
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiSmm.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/LockBoxLib.h>
+
+#include <Protocol/SmmReadyToLock.h>
+#include <Protocol/SmmCommunication.h>
+#include <Protocol/LockBox.h>
+#include <Guid/SmmLockBox.h>
+#include "SmmLockBoxCommon.h"
+
+BOOLEAN  mLocked = FALSE;
+
+/**
+  Dispatch function for SMM lock box save.
+
+  Caution: This function may receive untrusted input.
+  Restore buffer and length are external input, so this function will validate
+  it is in SMRAM.
+
+  @param LockBoxParameterSave  parameter of lock box save
+**/
+VOID
+SmmLockBoxSave (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_SAVE  *LockBoxParameterSave
+  )
+{
+  EFI_STATUS                       Status;
+  EFI_SMM_LOCK_BOX_PARAMETER_SAVE  TempLockBoxParameterSave;
+
+  //
+  // Sanity check
+  //
+  if (mLocked) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
+    LockBoxParameterSave->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
+    return;
+  }
+
+  CopyMem (&TempLockBoxParameterSave, LockBoxParameterSave, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SAVE));
+
+  //
+  // Sanity check
+  //
+  if (!IsBufferOutsideMmValid ((UINTN)TempLockBoxParameterSave.Buffer, (UINTN)TempLockBoxParameterSave.Length)) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Save address in SMRAM or buffer overflow!\n"));
+    LockBoxParameterSave->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
+    return;
+  }
+
+  //
+  // The SpeculationBarrier() call here is to ensure the above range check for
+  // the CommBuffer have been completed before calling into SaveLockBox().
+  //
+  SpeculationBarrier ();
+
+  //
+  // Save data
+  //
+  Status = SaveLockBox (
+             &TempLockBoxParameterSave.Guid,
+             (VOID *)(UINTN)TempLockBoxParameterSave.Buffer,
+             (UINTN)TempLockBoxParameterSave.Length
+             );
+  LockBoxParameterSave->Header.ReturnStatus = (UINT64)Status;
+  return;
+}
+
+/**
+  Dispatch function for SMM lock box set attributes.
+
+  @param LockBoxParameterSetAttributes  parameter of lock box set attributes
+**/
+VOID
+SmmLockBoxSetAttributes (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES  *LockBoxParameterSetAttributes
+  )
+{
+  EFI_STATUS                                 Status;
+  EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES  TempLockBoxParameterSetAttributes;
+
+  //
+  // Sanity check
+  //
+  if (mLocked) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
+    LockBoxParameterSetAttributes->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
+    return;
+  }
+
+  CopyMem (&TempLockBoxParameterSetAttributes, LockBoxParameterSetAttributes, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES));
+
+  //
+  // Update data
+  //
+  Status = SetLockBoxAttributes (
+             &TempLockBoxParameterSetAttributes.Guid,
+             TempLockBoxParameterSetAttributes.Attributes
+             );
+  LockBoxParameterSetAttributes->Header.ReturnStatus = (UINT64)Status;
+  return;
+}
+
+/**
+  Dispatch function for SMM lock box update.
+
+  Caution: This function may receive untrusted input.
+  Restore buffer and length are external input, so this function will validate
+  it is in SMRAM.
+
+  @param LockBoxParameterUpdate  parameter of lock box update
+**/
+VOID
+SmmLockBoxUpdate (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  *LockBoxParameterUpdate
+  )
+{
+  EFI_STATUS                         Status;
+  EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  TempLockBoxParameterUpdate;
+
+  //
+  // Sanity check
+  //
+  if (mLocked) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
+    LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
+    return;
+  }
+
+  CopyMem (&TempLockBoxParameterUpdate, LockBoxParameterUpdate, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE));
+
+  //
+  // Sanity check
+  //
+  if (!IsBufferOutsideMmValid ((UINTN)TempLockBoxParameterUpdate.Buffer, (UINTN)TempLockBoxParameterUpdate.Length)) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Update address in SMRAM or buffer overflow!\n"));
+    LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
+    return;
+  }
+
+  //
+  // The SpeculationBarrier() call here is to ensure the above range check for
+  // the CommBuffer have been completed before calling into UpdateLockBox().
+  //
+  SpeculationBarrier ();
+
+  //
+  // Update data
+  //
+  Status = UpdateLockBox (
+             &TempLockBoxParameterUpdate.Guid,
+             (UINTN)TempLockBoxParameterUpdate.Offset,
+             (VOID *)(UINTN)TempLockBoxParameterUpdate.Buffer,
+             (UINTN)TempLockBoxParameterUpdate.Length
+             );
+  LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)Status;
+  return;
+}
+
+/**
+  Dispatch function for SMM lock box restore.
+
+  Caution: This function may receive untrusted input.
+  Restore buffer and length are external input, so this function will validate
+  it is in SMRAM.
+
+  @param LockBoxParameterRestore  parameter of lock box restore
+**/
+VOID
+SmmLockBoxRestore (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE  *LockBoxParameterRestore
+  )
+{
+  EFI_STATUS                          Status;
+  EFI_SMM_LOCK_BOX_PARAMETER_RESTORE  TempLockBoxParameterRestore;
+
+  CopyMem (&TempLockBoxParameterRestore, LockBoxParameterRestore, sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE));
+
+  //
+  // Sanity check
+  //
+  if (!IsBufferOutsideMmValid ((UINTN)TempLockBoxParameterRestore.Buffer, (UINTN)TempLockBoxParameterRestore.Length)) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Restore address in SMRAM or buffer overflow!\n"));
+    LockBoxParameterRestore->Header.ReturnStatus = (UINT64)EFI_ACCESS_DENIED;
+    return;
+  }
+
+  //
+  // Restore data
+  //
+  if ((TempLockBoxParameterRestore.Length == 0) && (TempLockBoxParameterRestore.Buffer == 0)) {
+    Status = RestoreLockBox (
+               &TempLockBoxParameterRestore.Guid,
+               NULL,
+               NULL
+               );
+  } else {
+    Status = RestoreLockBox (
+               &TempLockBoxParameterRestore.Guid,
+               (VOID *)(UINTN)TempLockBoxParameterRestore.Buffer,
+               (UINTN *)&TempLockBoxParameterRestore.Length
+               );
+    if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)) {
+      //
+      // Return the actual Length value.
+      //
+      LockBoxParameterRestore->Length = TempLockBoxParameterRestore.Length;
+    }
+  }
+
+  LockBoxParameterRestore->Header.ReturnStatus = (UINT64)Status;
+  return;
+}
+
+/**
+  Dispatch function for SMM lock box restore all in place.
+
+  @param LockBoxParameterRestoreAllInPlace  parameter of lock box restore all in place
+**/
+VOID
+SmmLockBoxRestoreAllInPlace (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE  *LockBoxParameterRestoreAllInPlace
+  )
+{
+  EFI_STATUS  Status;
+
+  Status                                                 = RestoreAllLockBoxInPlace ();
+  LockBoxParameterRestoreAllInPlace->Header.ReturnStatus = (UINT64)Status;
+  return;
+}
+
+/**
+  Dispatch function for a Software SMI handler.
+
+  Caution: This function may receive untrusted input.
+  Communicate buffer and buffer size are external input, so this function will do basic validation.
+
+  @param DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
+  @param Context         Points to an optional handler context which was specified when the
+                         handler was registered.
+  @param CommBuffer      A pointer to a collection of data in memory that will
+                         be conveyed from a non-SMM environment into an SMM environment.
+  @param CommBufferSize  The size of the CommBuffer.
+
+  @retval EFI_SUCCESS Command is handled successfully.
+
+**/
+EFI_STATUS
+EFIAPI
+SmmLockBoxHandler (
+  IN EFI_HANDLE  DispatchHandle,
+  IN CONST VOID  *Context         OPTIONAL,
+  IN OUT VOID    *CommBuffer      OPTIONAL,
+  IN OUT UINTN   *CommBufferSize  OPTIONAL
+  )
+{
+  EFI_SMM_LOCK_BOX_PARAMETER_HEADER  *LockBoxParameterHeader;
+  UINTN                              TempCommBufferSize;
+
+  DEBUG ((DEBUG_INFO, "SmmLockBox SmmLockBoxHandler Enter\n"));
+
+  //
+  // If input is invalid, stop processing this SMI
+  //
+  if ((CommBuffer == NULL) || (CommBufferSize == NULL)) {
+    return EFI_SUCCESS;
+  }
+
+  TempCommBufferSize = *CommBufferSize;
+
+  //
+  // Sanity check
+  //
+  if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_HEADER)) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size invalid!\n"));
+    return EFI_SUCCESS;
+  }
+
+  if (!IsBufferOutsideMmValid ((UINTN)CommBuffer, TempCommBufferSize)) {
+    DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer in SMRAM or overflow!\n"));
+    return EFI_SUCCESS;
+  }
+
+  LockBoxParameterHeader = (EFI_SMM_LOCK_BOX_PARAMETER_HEADER *)((UINTN)CommBuffer);
+
+  LockBoxParameterHeader->ReturnStatus = (UINT64)-1;
+
+  DEBUG ((DEBUG_INFO, "SmmLockBox LockBoxParameterHeader - %x\n", (UINTN)LockBoxParameterHeader));
+
+  DEBUG ((DEBUG_INFO, "SmmLockBox Command - %x\n", (UINTN)LockBoxParameterHeader->Command));
+
+  switch (LockBoxParameterHeader->Command) {
+    case EFI_SMM_LOCK_BOX_COMMAND_SAVE:
+      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SAVE)) {
+        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for SAVE invalid!\n"));
+        break;
+      }
+
+      SmmLockBoxSave ((EFI_SMM_LOCK_BOX_PARAMETER_SAVE *)(UINTN)LockBoxParameterHeader);
+      break;
+    case EFI_SMM_LOCK_BOX_COMMAND_UPDATE:
+      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE)) {
+        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for UPDATE invalid!\n"));
+        break;
+      }
+
+      SmmLockBoxUpdate ((EFI_SMM_LOCK_BOX_PARAMETER_UPDATE *)(UINTN)LockBoxParameterHeader);
+      break;
+    case EFI_SMM_LOCK_BOX_COMMAND_RESTORE:
+      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE)) {
+        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for RESTORE invalid!\n"));
+        break;
+      }
+
+      SmmLockBoxRestore ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE *)(UINTN)LockBoxParameterHeader);
+      break;
+    case EFI_SMM_LOCK_BOX_COMMAND_SET_ATTRIBUTES:
+      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES)) {
+        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for SET_ATTRIBUTES invalid!\n"));
+        break;
+      }
+
+      SmmLockBoxSetAttributes ((EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES *)(UINTN)LockBoxParameterHeader);
+      break;
+    case EFI_SMM_LOCK_BOX_COMMAND_RESTORE_ALL_IN_PLACE:
+      if (TempCommBufferSize < sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE)) {
+        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for RESTORE_ALL_IN_PLACE invalid!\n"));
+        break;
+      }
+
+      SmmLockBoxRestoreAllInPlace ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE *)(UINTN)LockBoxParameterHeader);
+      break;
+    default:
+      DEBUG ((DEBUG_ERROR, "SmmLockBox Command invalid!\n"));
+      break;
+  }
+
+  LockBoxParameterHeader->Command = (UINT32)-1;
+
+  DEBUG ((DEBUG_INFO, "SmmLockBox SmmLockBoxHandler Exit\n"));
+
+  return EFI_SUCCESS;
+}
+
+/**
+  Smm Ready To Lock event notification handler.
+
+  It sets a flag indicating that SMRAM has been locked.
+
+  @param[in] Protocol   Points to the protocol's unique identifier.
+  @param[in] Interface  Points to the interface instance.
+  @param[in] Handle     The handle on which the interface was installed.
+
+  @retval EFI_SUCCESS   Notification handler runs successfully.
+ **/
+EFI_STATUS
+EFIAPI
+SmmReadyToLockEventNotify (
+  IN CONST EFI_GUID  *Protocol,
+  IN VOID            *Interface,
+  IN EFI_HANDLE      Handle
+  )
+{
+  mLocked = TRUE;
+  return EFI_SUCCESS;
+}
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h
new file mode 100644
index 0000000000..2205c4fb3b
--- /dev/null
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h
@@ -0,0 +1,148 @@
+/** @file
+  LockBox SMM/MM driver.
+
+  Caution: This module requires additional review when modified.
+  This driver will have external input - communicate buffer in SMM mode.
+  This external input must be validated carefully to avoid security issue like
+  buffer overflow, integer overflow.
+
+  SmmLockBoxHandler(), SmmLockBoxRestore(), SmmLockBoxUpdate(), SmmLockBoxSave()
+  will receive untrusted input and do basic validation.
+
+Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.<BR>
+
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiSmm.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/LockBoxLib.h>
+
+#include <Protocol/SmmReadyToLock.h>
+#include <Protocol/SmmCommunication.h>
+#include <Protocol/LockBox.h>
+#include <Guid/SmmLockBox.h>
+
+/**
+  This function is an abstraction layer for implementation specific Mm buffer validation routine.
+
+  @param Buffer  The buffer start address to be checked.
+  @param Length  The buffer length to be checked.
+
+  @retval TRUE  This buffer is valid per processor architecture and not overlap with SMRAM.
+  @retval FALSE This buffer is not valid per processor architecture or overlap with SMRAM.
+**/
+BOOLEAN
+IsBufferOutsideMmValid (
+  IN EFI_PHYSICAL_ADDRESS  Buffer,
+  IN UINT64                Length
+  );
+
+/**
+  Dispatch function for SMM lock box save.
+
+  Caution: This function may receive untrusted input.
+  Restore buffer and length are external input, so this function will validate
+  it is in SMRAM.
+
+  @param LockBoxParameterSave  parameter of lock box save
+**/
+VOID
+SmmLockBoxSave (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_SAVE  *LockBoxParameterSave
+  );
+
+/**
+  Dispatch function for SMM lock box set attributes.
+
+  @param LockBoxParameterSetAttributes  parameter of lock box set attributes
+**/
+VOID
+SmmLockBoxSetAttributes (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES  *LockBoxParameterSetAttributes
+  );
+
+/**
+  Dispatch function for SMM lock box update.
+
+  Caution: This function may receive untrusted input.
+  Restore buffer and length are external input, so this function will validate
+  it is in SMRAM.
+
+  @param LockBoxParameterUpdate  parameter of lock box update
+**/
+VOID
+SmmLockBoxUpdate (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  *LockBoxParameterUpdate
+  );
+
+/**
+  Dispatch function for SMM lock box restore.
+
+  Caution: This function may receive untrusted input.
+  Restore buffer and length are external input, so this function will validate
+  it is in SMRAM.
+
+  @param LockBoxParameterRestore  parameter of lock box restore
+**/
+VOID
+SmmLockBoxRestore (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE  *LockBoxParameterRestore
+  );
+
+/**
+  Dispatch function for SMM lock box restore all in place.
+
+  @param LockBoxParameterRestoreAllInPlace  parameter of lock box restore all in place
+**/
+VOID
+SmmLockBoxRestoreAllInPlace (
+  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE  *LockBoxParameterRestoreAllInPlace
+  );
+
+/**
+  Dispatch function for a Software SMI handler.
+
+  Caution: This function may receive untrusted input.
+  Communicate buffer and buffer size are external input, so this function will do basic validation.
+
+  @param DispatchHandle  The unique handle assigned to this handler by SmiHandlerRegister().
+  @param Context         Points to an optional handler context which was specified when the
+                         handler was registered.
+  @param CommBuffer      A pointer to a collection of data in memory that will
+                         be conveyed from a non-SMM environment into an SMM environment.
+  @param CommBufferSize  The size of the CommBuffer.
+
+  @retval EFI_SUCCESS Command is handled successfully.
+
+**/
+EFI_STATUS
+EFIAPI
+SmmLockBoxHandler (
+  IN EFI_HANDLE  DispatchHandle,
+  IN CONST VOID  *Context         OPTIONAL,
+  IN OUT VOID    *CommBuffer      OPTIONAL,
+  IN OUT UINTN   *CommBufferSize  OPTIONAL
+  );
+
+/**
+  Smm Ready To Lock event notification handler.
+
+  It sets a flag indicating that SMRAM has been locked.
+
+  @param[in] Protocol   Points to the protocol's unique identifier.
+  @param[in] Interface  Points to the interface instance.
+  @param[in] Handle     The handle on which the interface was installed.
+
+  @retval EFI_SUCCESS   Notification handler runs successfully.
+ **/
+EFI_STATUS
+EFIAPI
+SmmReadyToLockEventNotify (
+  IN CONST EFI_GUID  *Protocol,
+  IN VOID            *Interface,
+  IN EFI_HANDLE      Handle
+  );
--
2.39.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118654): https://edk2.groups.io/g/devel/message/118654
Mute This Topic: https://groups.io/mt/105955700/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



[-- Attachment #2: Type: text/html, Size: 53127 bytes --]

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [edk2-devel] [PATCH 3/3] MdeModulePkg: Add Standalone MM Lockbox Driver.
  2024-05-07  6:09 ` [edk2-devel] [PATCH 3/3] MdeModulePkg: Add Standalone MM Lockbox Driver Yuanhao Xie
@ 2024-05-08  2:53   ` Ni, Ray
  2024-05-09  3:42   ` Wu, Jiaxin
  1 sibling, 0 replies; 10+ messages in thread
From: Ni, Ray @ 2024-05-08  2:53 UTC (permalink / raw)
  To: Xie, Yuanhao, devel@edk2.groups.io; +Cc: Liming Gao, Wu, Jiaxin

[-- Attachment #1: Type: text/plain, Size: 8507 bytes --]

Reviewed-by: Ray Ni <ray.ni@intel.com>

Thanks,
Ray
________________________________
From: Xie, Yuanhao <yuanhao.xie@intel.com>
Sent: Tuesday, May 7, 2024 14:09
To: devel@edk2.groups.io <devel@edk2.groups.io>
Cc: Liming Gao <gaoliming@byosoft.com.cn>; Wu, Jiaxin <jiaxin.wu@intel.com>; Ni, Ray <ray.ni@intel.com>; Xie, Yuanhao <yuanhao.xie@intel.com>
Subject: [PATCH 3/3] MdeModulePkg: Add Standalone MM Lockbox Driver.

The Lockbox Driver allows sensitive data to be securely stored in a
designated area, thus protected against unauthorized access.

This patch adds a Standalone MM Lockbox Driver with main modifications:
1. Separating shared code between the Standalone MM driver and the
DXE MM Driver.
2. Utilizing services from the SMM Services Table (gSmst) as opposed to
 relying on Boot Services.

Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Jiaxin Wu <jiaxin.wu@intel.com>
Cc: Ray Ni <ray.ni@intel.com>

Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
---
 MdeModulePkg/MdeModulePkg.dsc                                             |  1 +
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.c        | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.inf      | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.uni      | 14 ++++++++++++++
 MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMmExtra.uni | 14 ++++++++++++++
 5 files changed, 169 insertions(+)

diff --git a/MdeModulePkg/MdeModulePkg.dsc b/MdeModulePkg/MdeModulePkg.dsc
index 6bed9205ea..f0f02f180f 100644
--- a/MdeModulePkg/MdeModulePkg.dsc
+++ b/MdeModulePkg/MdeModulePkg.dsc
@@ -500,6 +500,7 @@
   MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterSmm.inf
   MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCodeRouterStandaloneMm.inf
   MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
+  MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.inf
   MdeModulePkg/Library/SmmMemoryAllocationProfileLib/SmmMemoryAllocationProfileLib.inf
   MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationProfileLib.inf
   MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemoryAllocationLib.inf
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.c b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.c
new file mode 100644
index 0000000000..503be7efa8
--- /dev/null
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.c
@@ -0,0 +1,84 @@
+/** @file
+  LockBox MM driver.
+
+Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+
+SPDX-License-Identifier: BSD-2-Clause-Patent
+
+**/
+
+#include <PiSmm.h>
+#include <Library/StandaloneMmDriverEntryPoint.h>
+#include <Library/MmServicesTableLib.h>
+#include <Library/BaseLib.h>
+#include <Library/BaseMemoryLib.h>
+#include <Library/DebugLib.h>
+#include <Library/StandaloneMmMemLib.h>
+#include <Library/LockBoxLib.h>
+
+#include <Protocol/SmmReadyToLock.h>
+#include <Protocol/SmmCommunication.h>
+#include <Protocol/LockBox.h>
+#include <Guid/SmmLockBox.h>
+
+#include "SmmLockBoxCommon.h"
+
+/**
+  This function is an abstraction layer for implementation specific Mm buffer validation routine.
+
+  @param Buffer  The buffer start address to be checked.
+  @param Length  The buffer length to be checked.
+
+  @retval TRUE  This buffer is valid per processor architecture and not overlap with SMRAM.
+  @retval FALSE This buffer is not valid per processor architecture or overlap with SMRAM.
+**/
+BOOLEAN
+IsBufferOutsideMmValid (
+  IN EFI_PHYSICAL_ADDRESS  Buffer,
+  IN UINT64                Length
+  )
+{
+  return MmIsBufferOutsideMmValid (Buffer, Length);
+}
+
+/**
+  Entry Point for LockBox MM driver.
+
+  @param[in] ImageHandle  Image handle of this driver.
+  @param[in] SystemTable  A Pointer to the EFI System Table.
+
+  @retval EFI_SUCEESS
+  @return Others          Some error occurs.
+**/
+EFI_STATUS
+EFIAPI
+SmmLockBoxStandaloneMmEntryPoint (
+  IN EFI_HANDLE           ImageHandle,
+  IN EFI_MM_SYSTEM_TABLE  *SystemTable
+  )
+{
+  EFI_STATUS  Status;
+  EFI_HANDLE  DispatchHandle;
+  VOID        *Registration;
+
+  //
+  // Register LockBox communication handler
+  //
+  Status = gMmst->MmiHandlerRegister (
+                    SmmLockBoxHandler,
+                    &gEfiSmmLockBoxCommunicationGuid,
+                    &DispatchHandle
+                    );
+  ASSERT_EFI_ERROR (Status);
+
+  //
+  // Register SMM Ready To Lock Protocol notification
+  //
+  Status = gMmst->MmRegisterProtocolNotify (
+                    &gEfiSmmReadyToLockProtocolGuid,
+                    SmmReadyToLockEventNotify,
+                    &Registration
+                    );
+  ASSERT_EFI_ERROR (Status);
+  return Status;
+}
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.inf b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.inf
new file mode 100644
index 0000000000..544c87790c
--- /dev/null
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.inf
@@ -0,0 +1,56 @@
+## @file
+#  LockBox MM driver.
+#
+# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+#
+#  SPDX-License-Identifier: BSD-2-Clause-Patent
+#
+##
+
+[Defines]
+  INF_VERSION                    = 0x00010005
+  BASE_NAME                      = SmmLockBoxStandaloneMm
+  MODULE_UNI_FILE                = SmmLockBoxStandaloneMm.uni
+  FILE_GUID                      = a83a87a0-8a3e-482d-86c8-84a139f6ded0
+  MODULE_TYPE                    = MM_STANDALONE
+  VERSION_STRING                 = 1.0
+  PI_SPECIFICATION_VERSION       = 0x00010032
+  ENTRY_POINT                    = SmmLockBoxStandaloneMmEntryPoint
+
+#
+# The following information is for reference only and not required by the build tools.
+#
+#  VALID_ARCHITECTURES           = IA32 X64
+#
+
+[Sources]
+  SmmLockBoxStandaloneMm.c
+  SmmLockBoxCommon.c
+  SmmLockBoxCommon.h
+
+[Packages]
+  MdePkg/MdePkg.dec
+  MdeModulePkg/MdeModulePkg.dec
+  StandaloneMmPkg/StandaloneMmPkg.dec
+
+[LibraryClasses]
+  MmServicesTableLib
+  BaseLib
+  BaseMemoryLib
+  DebugLib
+  LockBoxLib
+  MemLib
+  StandaloneMmDriverEntryPoint
+
+[Guids]
+  gEfiSmmLockBoxCommunicationGuid   ## PRODUCES ## GUID # SmiHandlerRegister
+
+[Protocols]
+  gEfiSmmReadyToLockProtocolGuid    ## NOTIFY
+  gEfiLockBoxProtocolGuid           ## PRODUCES
+
+[Depex]
+  TRUE
+
+[UserExtensions.TianoCore."ExtraFiles"]
+  SmmLockBoxStandaloneMm.uni
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.uni b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.uni
new file mode 100644
index 0000000000..7f6218102f
--- /dev/null
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMm.uni
@@ -0,0 +1,14 @@
+// /** @file
+// LockBox MM driver.
+//
+// Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+
+#string STR_MODULE_ABSTRACT             #language en-US "LockBox MM driver."
+
+#string STR_MODULE_DESCRIPTION          #language en-US "LockBox MM driver."
+
diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMmExtra.uni b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMmExtra.uni
new file mode 100644
index 0000000000..a5443ca5f9
--- /dev/null
+++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneMmExtra.uni
@@ -0,0 +1,14 @@
+// /** @file
+// SmmLockBox Localized Strings and Content
+//
+// Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
+//
+// SPDX-License-Identifier: BSD-2-Clause-Patent
+//
+// **/
+
+#string STR_PROPERTIES_MODULE_NAME
+#language en-US
+"MM Lock Box Driver"
+
+
--
2.39.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118655): https://edk2.groups.io/g/devel/message/118655
Mute This Topic: https://groups.io/mt/105955701/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



[-- Attachment #2: Type: text/html, Size: 13989 bytes --]

^ permalink raw reply related	[flat|nested] 10+ messages in thread

* Re: [edk2-devel] [PATCH 2/3] MdeModulePkg: Refactors SmmLockBox.c.
  2024-05-07  6:09 ` [edk2-devel] [PATCH 2/3] MdeModulePkg: Refactors SmmLockBox.c Yuanhao Xie
  2024-05-08  2:50   ` Ni, Ray
@ 2024-05-09  3:41   ` Wu, Jiaxin
  1 sibling, 0 replies; 10+ messages in thread
From: Wu, Jiaxin @ 2024-05-09  3:41 UTC (permalink / raw)
  To: Xie, Yuanhao, devel@edk2.groups.io; +Cc: Liming Gao, Ni, Ray

Reviewed-by: Jiaxin Wu <jiaxin.wu@intel.com>

> -----Original Message-----
> From: Xie, Yuanhao <yuanhao.xie@intel.com>
> Sent: Tuesday, May 7, 2024 2:09 PM
> To: devel@edk2.groups.io
> Cc: Liming Gao <gaoliming@byosoft.com.cn>; Wu, Jiaxin
> <jiaxin.wu@intel.com>; Ni, Ray <ray.ni@intel.com>; Xie, Yuanhao
> <yuanhao.xie@intel.com>
> Subject: [PATCH 2/3] MdeModulePkg: Refactors SmmLockBox.c.
> 
> The Lockbox Driver allows sensitive data to be securely stored in a
> designated area, thus protected against unauthorized access.
> 
> This patch does not introduce any functional modifications.
> It refactors the existing logic into a common component to facilitates
> the integration of the Standalone MM Lockbox Driver in an upcoming patch
> 
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Jiaxin Wu <jiaxin.wu@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> 
> Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
> ---
>  MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c       | 361
> ++++++++++++---------------------------------------------------------------------------
> ----------------------------------------------------------------------------------------------
> ----------------------------------------------------------------------------------------------
> --------------------------------------------------------------------------------------
>  MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf     |   4
> +++-
>  MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c |
> 384
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ++++++++++++++++++++++++++++++++++++
>  MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h |
> 148
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ++++++++++++++++++++++++++++++++
>  4 files changed, 547 insertions(+), 350 deletions(-)
> 
> diff --git a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c
> index c1e15c596b..2774979c34 100644
> --- a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c
> +++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.c
> @@ -9,7 +9,7 @@
>    SmmLockBoxHandler(), SmmLockBoxRestore(), SmmLockBoxUpdate(),
> SmmLockBoxSave()
>    will receive untrusted input and do basic validation.
> 
> -Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
> +Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.<BR>
> 
>  SPDX-License-Identifier: BSD-2-Clause-Patent
> 
> @@ -31,360 +31,24 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
>  #include <Protocol/LockBox.h>
>  #include <Guid/SmmLockBox.h>
> 
> -BOOLEAN  mLocked = FALSE;
> +#include "SmmLockBoxCommon.h"
> 
>  /**
> -  Dispatch function for SMM lock box save.
> +  This function is an abstraction layer for implementation specific Mm buffer
> validation routine.
> 
> -  Caution: This function may receive untrusted input.
> -  Restore buffer and length are external input, so this function will validate
> -  it is in SMRAM.
> +  @param Buffer  The buffer start address to be checked.
> +  @param Length  The buffer length to be checked.
> 
> -  @param LockBoxParameterSave  parameter of lock box save
> +  @retval TRUE  This buffer is valid per processor architecture and not overlap
> with SMRAM.
> +  @retval FALSE This buffer is not valid per processor architecture or overlap
> with SMRAM.
>  **/
> -VOID
> -SmmLockBoxSave (
> -  IN EFI_SMM_LOCK_BOX_PARAMETER_SAVE  *LockBoxParameterSave
> +BOOLEAN
> +IsBufferOutsideMmValid (
> +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> +  IN UINT64                Length
>    )
>  {
> -  EFI_STATUS                       Status;
> -  EFI_SMM_LOCK_BOX_PARAMETER_SAVE  TempLockBoxParameterSave;
> -
> -  //
> -  // Sanity check
> -  //
> -  if (mLocked) {
> -    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
> -    LockBoxParameterSave->Header.ReturnStatus =
> (UINT64)EFI_ACCESS_DENIED;
> -    return;
> -  }
> -
> -  CopyMem (&TempLockBoxParameterSave, LockBoxParameterSave, sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_SAVE));
> -
> -  //
> -  // Sanity check
> -  //
> -  if (!SmmIsBufferOutsideSmmValid
> ((UINTN)TempLockBoxParameterSave.Buffer,
> (UINTN)TempLockBoxParameterSave.Length)) {
> -    DEBUG ((DEBUG_ERROR, "SmmLockBox Save address in SMRAM or buffer
> overflow!\n"));
> -    LockBoxParameterSave->Header.ReturnStatus =
> (UINT64)EFI_ACCESS_DENIED;
> -    return;
> -  }
> -
> -  //
> -  // The SpeculationBarrier() call here is to ensure the above range check for
> -  // the CommBuffer have been completed before calling into SaveLockBox().
> -  //
> -  SpeculationBarrier ();
> -
> -  //
> -  // Save data
> -  //
> -  Status = SaveLockBox (
> -             &TempLockBoxParameterSave.Guid,
> -             (VOID *)(UINTN)TempLockBoxParameterSave.Buffer,
> -             (UINTN)TempLockBoxParameterSave.Length
> -             );
> -  LockBoxParameterSave->Header.ReturnStatus = (UINT64)Status;
> -  return;
> -}
> -
> -/**
> -  Dispatch function for SMM lock box set attributes.
> -
> -  @param LockBoxParameterSetAttributes  parameter of lock box set
> attributes
> -**/
> -VOID
> -SmmLockBoxSetAttributes (
> -  IN EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES
> *LockBoxParameterSetAttributes
> -  )
> -{
> -  EFI_STATUS                                 Status;
> -  EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES
> TempLockBoxParameterSetAttributes;
> -
> -  //
> -  // Sanity check
> -  //
> -  if (mLocked) {
> -    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
> -    LockBoxParameterSetAttributes->Header.ReturnStatus =
> (UINT64)EFI_ACCESS_DENIED;
> -    return;
> -  }
> -
> -  CopyMem (&TempLockBoxParameterSetAttributes,
> LockBoxParameterSetAttributes, sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES));
> -
> -  //
> -  // Update data
> -  //
> -  Status = SetLockBoxAttributes (
> -             &TempLockBoxParameterSetAttributes.Guid,
> -             TempLockBoxParameterSetAttributes.Attributes
> -             );
> -  LockBoxParameterSetAttributes->Header.ReturnStatus = (UINT64)Status;
> -  return;
> -}
> -
> -/**
> -  Dispatch function for SMM lock box update.
> -
> -  Caution: This function may receive untrusted input.
> -  Restore buffer and length are external input, so this function will validate
> -  it is in SMRAM.
> -
> -  @param LockBoxParameterUpdate  parameter of lock box update
> -**/
> -VOID
> -SmmLockBoxUpdate (
> -  IN EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  *LockBoxParameterUpdate
> -  )
> -{
> -  EFI_STATUS                         Status;
> -  EFI_SMM_LOCK_BOX_PARAMETER_UPDATE
> TempLockBoxParameterUpdate;
> -
> -  //
> -  // Sanity check
> -  //
> -  if (mLocked) {
> -    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
> -    LockBoxParameterUpdate->Header.ReturnStatus =
> (UINT64)EFI_ACCESS_DENIED;
> -    return;
> -  }
> -
> -  CopyMem (&TempLockBoxParameterUpdate, LockBoxParameterUpdate,
> sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE));
> -
> -  //
> -  // Sanity check
> -  //
> -  if (!SmmIsBufferOutsideSmmValid
> ((UINTN)TempLockBoxParameterUpdate.Buffer,
> (UINTN)TempLockBoxParameterUpdate.Length)) {
> -    DEBUG ((DEBUG_ERROR, "SmmLockBox Update address in SMRAM or
> buffer overflow!\n"));
> -    LockBoxParameterUpdate->Header.ReturnStatus =
> (UINT64)EFI_ACCESS_DENIED;
> -    return;
> -  }
> -
> -  //
> -  // The SpeculationBarrier() call here is to ensure the above range check for
> -  // the CommBuffer have been completed before calling into
> UpdateLockBox().
> -  //
> -  SpeculationBarrier ();
> -
> -  //
> -  // Update data
> -  //
> -  Status = UpdateLockBox (
> -             &TempLockBoxParameterUpdate.Guid,
> -             (UINTN)TempLockBoxParameterUpdate.Offset,
> -             (VOID *)(UINTN)TempLockBoxParameterUpdate.Buffer,
> -             (UINTN)TempLockBoxParameterUpdate.Length
> -             );
> -  LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)Status;
> -  return;
> -}
> -
> -/**
> -  Dispatch function for SMM lock box restore.
> -
> -  Caution: This function may receive untrusted input.
> -  Restore buffer and length are external input, so this function will validate
> -  it is in SMRAM.
> -
> -  @param LockBoxParameterRestore  parameter of lock box restore
> -**/
> -VOID
> -SmmLockBoxRestore (
> -  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE  *LockBoxParameterRestore
> -  )
> -{
> -  EFI_STATUS                          Status;
> -  EFI_SMM_LOCK_BOX_PARAMETER_RESTORE
> TempLockBoxParameterRestore;
> -
> -  CopyMem (&TempLockBoxParameterRestore, LockBoxParameterRestore,
> sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE));
> -
> -  //
> -  // Sanity check
> -  //
> -  if (!SmmIsBufferOutsideSmmValid
> ((UINTN)TempLockBoxParameterRestore.Buffer,
> (UINTN)TempLockBoxParameterRestore.Length)) {
> -    DEBUG ((DEBUG_ERROR, "SmmLockBox Restore address in SMRAM or
> buffer overflow!\n"));
> -    LockBoxParameterRestore->Header.ReturnStatus =
> (UINT64)EFI_ACCESS_DENIED;
> -    return;
> -  }
> -
> -  //
> -  // Restore data
> -  //
> -  if ((TempLockBoxParameterRestore.Length == 0) &&
> (TempLockBoxParameterRestore.Buffer == 0)) {
> -    Status = RestoreLockBox (
> -               &TempLockBoxParameterRestore.Guid,
> -               NULL,
> -               NULL
> -               );
> -  } else {
> -    Status = RestoreLockBox (
> -               &TempLockBoxParameterRestore.Guid,
> -               (VOID *)(UINTN)TempLockBoxParameterRestore.Buffer,
> -               (UINTN *)&TempLockBoxParameterRestore.Length
> -               );
> -    if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)) {
> -      //
> -      // Return the actual Length value.
> -      //
> -      LockBoxParameterRestore->Length =
> TempLockBoxParameterRestore.Length;
> -    }
> -  }
> -
> -  LockBoxParameterRestore->Header.ReturnStatus = (UINT64)Status;
> -  return;
> -}
> -
> -/**
> -  Dispatch function for SMM lock box restore all in place.
> -
> -  @param LockBoxParameterRestoreAllInPlace  parameter of lock box restore
> all in place
> -**/
> -VOID
> -SmmLockBoxRestoreAllInPlace (
> -  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE
> *LockBoxParameterRestoreAllInPlace
> -  )
> -{
> -  EFI_STATUS  Status;
> -
> -  Status                                                 = RestoreAllLockBoxInPlace ();
> -  LockBoxParameterRestoreAllInPlace->Header.ReturnStatus =
> (UINT64)Status;
> -  return;
> -}
> -
> -/**
> -  Dispatch function for a Software SMI handler.
> -
> -  Caution: This function may receive untrusted input.
> -  Communicate buffer and buffer size are external input, so this function will
> do basic validation.
> -
> -  @param DispatchHandle  The unique handle assigned to this handler by
> SmiHandlerRegister().
> -  @param Context         Points to an optional handler context which was
> specified when the
> -                         handler was registered.
> -  @param CommBuffer      A pointer to a collection of data in memory that will
> -                         be conveyed from a non-SMM environment into an SMM
> environment.
> -  @param CommBufferSize  The size of the CommBuffer.
> -
> -  @retval EFI_SUCCESS Command is handled successfully.
> -
> -**/
> -EFI_STATUS
> -EFIAPI
> -SmmLockBoxHandler (
> -  IN EFI_HANDLE  DispatchHandle,
> -  IN CONST VOID  *Context         OPTIONAL,
> -  IN OUT VOID    *CommBuffer      OPTIONAL,
> -  IN OUT UINTN   *CommBufferSize  OPTIONAL
> -  )
> -{
> -  EFI_SMM_LOCK_BOX_PARAMETER_HEADER  *LockBoxParameterHeader;
> -  UINTN                              TempCommBufferSize;
> -
> -  DEBUG ((DEBUG_INFO, "SmmLockBox SmmLockBoxHandler Enter\n"));
> -
> -  //
> -  // If input is invalid, stop processing this SMI
> -  //
> -  if ((CommBuffer == NULL) || (CommBufferSize == NULL)) {
> -    return EFI_SUCCESS;
> -  }
> -
> -  TempCommBufferSize = *CommBufferSize;
> -
> -  //
> -  // Sanity check
> -  //
> -  if (TempCommBufferSize < sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_HEADER)) {
> -    DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size invalid!\n"));
> -    return EFI_SUCCESS;
> -  }
> -
> -  if (!SmmIsBufferOutsideSmmValid ((UINTN)CommBuffer,
> TempCommBufferSize)) {
> -    DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer in SMRAM or
> overflow!\n"));
> -    return EFI_SUCCESS;
> -  }
> -
> -  LockBoxParameterHeader = (EFI_SMM_LOCK_BOX_PARAMETER_HEADER
> *)((UINTN)CommBuffer);
> -
> -  LockBoxParameterHeader->ReturnStatus = (UINT64)-1;
> -
> -  DEBUG ((DEBUG_INFO, "SmmLockBox LockBoxParameterHeader - %x\n",
> (UINTN)LockBoxParameterHeader));
> -
> -  DEBUG ((DEBUG_INFO, "SmmLockBox Command - %x\n",
> (UINTN)LockBoxParameterHeader->Command));
> -
> -  switch (LockBoxParameterHeader->Command) {
> -    case EFI_SMM_LOCK_BOX_COMMAND_SAVE:
> -      if (TempCommBufferSize < sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_SAVE)) {
> -        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for SAVE
> invalid!\n"));
> -        break;
> -      }
> -
> -      SmmLockBoxSave ((EFI_SMM_LOCK_BOX_PARAMETER_SAVE
> *)(UINTN)LockBoxParameterHeader);
> -      break;
> -    case EFI_SMM_LOCK_BOX_COMMAND_UPDATE:
> -      if (TempCommBufferSize < sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE)) {
> -        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for
> UPDATE invalid!\n"));
> -        break;
> -      }
> -
> -      SmmLockBoxUpdate ((EFI_SMM_LOCK_BOX_PARAMETER_UPDATE
> *)(UINTN)LockBoxParameterHeader);
> -      break;
> -    case EFI_SMM_LOCK_BOX_COMMAND_RESTORE:
> -      if (TempCommBufferSize < sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE)) {
> -        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for
> RESTORE invalid!\n"));
> -        break;
> -      }
> -
> -      SmmLockBoxRestore ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE
> *)(UINTN)LockBoxParameterHeader);
> -      break;
> -    case EFI_SMM_LOCK_BOX_COMMAND_SET_ATTRIBUTES:
> -      if (TempCommBufferSize < sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES)) {
> -        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for
> SET_ATTRIBUTES invalid!\n"));
> -        break;
> -      }
> -
> -      SmmLockBoxSetAttributes
> ((EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES
> *)(UINTN)LockBoxParameterHeader);
> -      break;
> -    case EFI_SMM_LOCK_BOX_COMMAND_RESTORE_ALL_IN_PLACE:
> -      if (TempCommBufferSize < sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE)) {
> -        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for
> RESTORE_ALL_IN_PLACE invalid!\n"));
> -        break;
> -      }
> -
> -      SmmLockBoxRestoreAllInPlace
> ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE
> *)(UINTN)LockBoxParameterHeader);
> -      break;
> -    default:
> -      DEBUG ((DEBUG_ERROR, "SmmLockBox Command invalid!\n"));
> -      break;
> -  }
> -
> -  LockBoxParameterHeader->Command = (UINT32)-1;
> -
> -  DEBUG ((DEBUG_INFO, "SmmLockBox SmmLockBoxHandler Exit\n"));
> -
> -  return EFI_SUCCESS;
> -}
> -
> -/**
> -  Smm Ready To Lock event notification handler.
> -
> -  It sets a flag indicating that SMRAM has been locked.
> -
> -  @param[in] Protocol   Points to the protocol's unique identifier.
> -  @param[in] Interface  Points to the interface instance.
> -  @param[in] Handle     The handle on which the interface was installed.
> -
> -  @retval EFI_SUCCESS   Notification handler runs successfully.
> - **/
> -EFI_STATUS
> -EFIAPI
> -SmmReadyToLockEventNotify (
> -  IN CONST EFI_GUID  *Protocol,
> -  IN VOID            *Interface,
> -  IN EFI_HANDLE      Handle
> -  )
> -{
> -  mLocked = TRUE;
> -  return EFI_SUCCESS;
> +  return SmmIsBufferOutsideSmmValid (Buffer, Length);
>  }
> 
>  /**
> @@ -438,6 +102,5 @@ SmmLockBoxEntryPoint (
>                         NULL
>                         );
>    ASSERT_EFI_ERROR (Status);
> -
>    return Status;
>  }
> diff --git
> a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
> index 5081b2d7f2..f279706e90 100644
> --- a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
> +++ b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
> @@ -6,7 +6,7 @@
>  #  This external input must be validated carefully to avoid security issue like
>  #  buffer overflow, integer overflow.
>  #
> -#  Copyright (c) 2010 - 2018, Intel Corporation. All rights reserved.<BR>
> +#  Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.<BR>
>  #
>  #  SPDX-License-Identifier: BSD-2-Clause-Patent
>  #
> @@ -30,6 +30,8 @@
> 
>  [Sources]
>    SmmLockBox.c
> +  SmmLockBoxCommon.c
> +  SmmLockBoxCommon.h
> 
>  [Packages]
>    MdePkg/MdePkg.dec
> diff --git
> a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c
> new file mode 100644
> index 0000000000..5c6eae05af
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.c
> @@ -0,0 +1,384 @@
> +/** @file
> +  LockBox SMM/MM driver.
> +
> +  Caution: This module requires additional review when modified.
> +  This driver will have external input - communicate buffer in SMM mode.
> +  This external input must be validated carefully to avoid security issue like
> +  buffer overflow, integer overflow.
> +
> +  SmmLockBoxHandler(), SmmLockBoxRestore(), SmmLockBoxUpdate(),
> SmmLockBoxSave()
> +  will receive untrusted input and do basic validation.
> +
> +Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.<BR>
> +
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <PiSmm.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/LockBoxLib.h>
> +
> +#include <Protocol/SmmReadyToLock.h>
> +#include <Protocol/SmmCommunication.h>
> +#include <Protocol/LockBox.h>
> +#include <Guid/SmmLockBox.h>
> +#include "SmmLockBoxCommon.h"
> +
> +BOOLEAN  mLocked = FALSE;
> +
> +/**
> +  Dispatch function for SMM lock box save.
> +
> +  Caution: This function may receive untrusted input.
> +  Restore buffer and length are external input, so this function will validate
> +  it is in SMRAM.
> +
> +  @param LockBoxParameterSave  parameter of lock box save
> +**/
> +VOID
> +SmmLockBoxSave (
> +  IN EFI_SMM_LOCK_BOX_PARAMETER_SAVE  *LockBoxParameterSave
> +  )
> +{
> +  EFI_STATUS                       Status;
> +  EFI_SMM_LOCK_BOX_PARAMETER_SAVE  TempLockBoxParameterSave;
> +
> +  //
> +  // Sanity check
> +  //
> +  if (mLocked) {
> +    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
> +    LockBoxParameterSave->Header.ReturnStatus =
> (UINT64)EFI_ACCESS_DENIED;
> +    return;
> +  }
> +
> +  CopyMem (&TempLockBoxParameterSave, LockBoxParameterSave, sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_SAVE));
> +
> +  //
> +  // Sanity check
> +  //
> +  if (!IsBufferOutsideMmValid ((UINTN)TempLockBoxParameterSave.Buffer,
> (UINTN)TempLockBoxParameterSave.Length)) {
> +    DEBUG ((DEBUG_ERROR, "SmmLockBox Save address in SMRAM or buffer
> overflow!\n"));
> +    LockBoxParameterSave->Header.ReturnStatus =
> (UINT64)EFI_ACCESS_DENIED;
> +    return;
> +  }
> +
> +  //
> +  // The SpeculationBarrier() call here is to ensure the above range check for
> +  // the CommBuffer have been completed before calling into SaveLockBox().
> +  //
> +  SpeculationBarrier ();
> +
> +  //
> +  // Save data
> +  //
> +  Status = SaveLockBox (
> +             &TempLockBoxParameterSave.Guid,
> +             (VOID *)(UINTN)TempLockBoxParameterSave.Buffer,
> +             (UINTN)TempLockBoxParameterSave.Length
> +             );
> +  LockBoxParameterSave->Header.ReturnStatus = (UINT64)Status;
> +  return;
> +}
> +
> +/**
> +  Dispatch function for SMM lock box set attributes.
> +
> +  @param LockBoxParameterSetAttributes  parameter of lock box set
> attributes
> +**/
> +VOID
> +SmmLockBoxSetAttributes (
> +  IN EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES
> *LockBoxParameterSetAttributes
> +  )
> +{
> +  EFI_STATUS                                 Status;
> +  EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES
> TempLockBoxParameterSetAttributes;
> +
> +  //
> +  // Sanity check
> +  //
> +  if (mLocked) {
> +    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
> +    LockBoxParameterSetAttributes->Header.ReturnStatus =
> (UINT64)EFI_ACCESS_DENIED;
> +    return;
> +  }
> +
> +  CopyMem (&TempLockBoxParameterSetAttributes,
> LockBoxParameterSetAttributes, sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES));
> +
> +  //
> +  // Update data
> +  //
> +  Status = SetLockBoxAttributes (
> +             &TempLockBoxParameterSetAttributes.Guid,
> +             TempLockBoxParameterSetAttributes.Attributes
> +             );
> +  LockBoxParameterSetAttributes->Header.ReturnStatus = (UINT64)Status;
> +  return;
> +}
> +
> +/**
> +  Dispatch function for SMM lock box update.
> +
> +  Caution: This function may receive untrusted input.
> +  Restore buffer and length are external input, so this function will validate
> +  it is in SMRAM.
> +
> +  @param LockBoxParameterUpdate  parameter of lock box update
> +**/
> +VOID
> +SmmLockBoxUpdate (
> +  IN EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  *LockBoxParameterUpdate
> +  )
> +{
> +  EFI_STATUS                         Status;
> +  EFI_SMM_LOCK_BOX_PARAMETER_UPDATE
> TempLockBoxParameterUpdate;
> +
> +  //
> +  // Sanity check
> +  //
> +  if (mLocked) {
> +    DEBUG ((DEBUG_ERROR, "SmmLockBox Locked!\n"));
> +    LockBoxParameterUpdate->Header.ReturnStatus =
> (UINT64)EFI_ACCESS_DENIED;
> +    return;
> +  }
> +
> +  CopyMem (&TempLockBoxParameterUpdate, LockBoxParameterUpdate,
> sizeof (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE));
> +
> +  //
> +  // Sanity check
> +  //
> +  if (!IsBufferOutsideMmValid
> ((UINTN)TempLockBoxParameterUpdate.Buffer,
> (UINTN)TempLockBoxParameterUpdate.Length)) {
> +    DEBUG ((DEBUG_ERROR, "SmmLockBox Update address in SMRAM or
> buffer overflow!\n"));
> +    LockBoxParameterUpdate->Header.ReturnStatus =
> (UINT64)EFI_ACCESS_DENIED;
> +    return;
> +  }
> +
> +  //
> +  // The SpeculationBarrier() call here is to ensure the above range check for
> +  // the CommBuffer have been completed before calling into
> UpdateLockBox().
> +  //
> +  SpeculationBarrier ();
> +
> +  //
> +  // Update data
> +  //
> +  Status = UpdateLockBox (
> +             &TempLockBoxParameterUpdate.Guid,
> +             (UINTN)TempLockBoxParameterUpdate.Offset,
> +             (VOID *)(UINTN)TempLockBoxParameterUpdate.Buffer,
> +             (UINTN)TempLockBoxParameterUpdate.Length
> +             );
> +  LockBoxParameterUpdate->Header.ReturnStatus = (UINT64)Status;
> +  return;
> +}
> +
> +/**
> +  Dispatch function for SMM lock box restore.
> +
> +  Caution: This function may receive untrusted input.
> +  Restore buffer and length are external input, so this function will validate
> +  it is in SMRAM.
> +
> +  @param LockBoxParameterRestore  parameter of lock box restore
> +**/
> +VOID
> +SmmLockBoxRestore (
> +  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE
> *LockBoxParameterRestore
> +  )
> +{
> +  EFI_STATUS                          Status;
> +  EFI_SMM_LOCK_BOX_PARAMETER_RESTORE
> TempLockBoxParameterRestore;
> +
> +  CopyMem (&TempLockBoxParameterRestore, LockBoxParameterRestore,
> sizeof (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE));
> +
> +  //
> +  // Sanity check
> +  //
> +  if (!IsBufferOutsideMmValid
> ((UINTN)TempLockBoxParameterRestore.Buffer,
> (UINTN)TempLockBoxParameterRestore.Length)) {
> +    DEBUG ((DEBUG_ERROR, "SmmLockBox Restore address in SMRAM or
> buffer overflow!\n"));
> +    LockBoxParameterRestore->Header.ReturnStatus =
> (UINT64)EFI_ACCESS_DENIED;
> +    return;
> +  }
> +
> +  //
> +  // Restore data
> +  //
> +  if ((TempLockBoxParameterRestore.Length == 0) &&
> (TempLockBoxParameterRestore.Buffer == 0)) {
> +    Status = RestoreLockBox (
> +               &TempLockBoxParameterRestore.Guid,
> +               NULL,
> +               NULL
> +               );
> +  } else {
> +    Status = RestoreLockBox (
> +               &TempLockBoxParameterRestore.Guid,
> +               (VOID *)(UINTN)TempLockBoxParameterRestore.Buffer,
> +               (UINTN *)&TempLockBoxParameterRestore.Length
> +               );
> +    if ((Status == EFI_BUFFER_TOO_SMALL) || (Status == EFI_SUCCESS)) {
> +      //
> +      // Return the actual Length value.
> +      //
> +      LockBoxParameterRestore->Length =
> TempLockBoxParameterRestore.Length;
> +    }
> +  }
> +
> +  LockBoxParameterRestore->Header.ReturnStatus = (UINT64)Status;
> +  return;
> +}
> +
> +/**
> +  Dispatch function for SMM lock box restore all in place.
> +
> +  @param LockBoxParameterRestoreAllInPlace  parameter of lock box restore
> all in place
> +**/
> +VOID
> +SmmLockBoxRestoreAllInPlace (
> +  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE
> *LockBoxParameterRestoreAllInPlace
> +  )
> +{
> +  EFI_STATUS  Status;
> +
> +  Status                                                 = RestoreAllLockBoxInPlace ();
> +  LockBoxParameterRestoreAllInPlace->Header.ReturnStatus =
> (UINT64)Status;
> +  return;
> +}
> +
> +/**
> +  Dispatch function for a Software SMI handler.
> +
> +  Caution: This function may receive untrusted input.
> +  Communicate buffer and buffer size are external input, so this function will
> do basic validation.
> +
> +  @param DispatchHandle  The unique handle assigned to this handler by
> SmiHandlerRegister().
> +  @param Context         Points to an optional handler context which was
> specified when the
> +                         handler was registered.
> +  @param CommBuffer      A pointer to a collection of data in memory that will
> +                         be conveyed from a non-SMM environment into an SMM
> environment.
> +  @param CommBufferSize  The size of the CommBuffer.
> +
> +  @retval EFI_SUCCESS Command is handled successfully.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +SmmLockBoxHandler (
> +  IN EFI_HANDLE  DispatchHandle,
> +  IN CONST VOID  *Context         OPTIONAL,
> +  IN OUT VOID    *CommBuffer      OPTIONAL,
> +  IN OUT UINTN   *CommBufferSize  OPTIONAL
> +  )
> +{
> +  EFI_SMM_LOCK_BOX_PARAMETER_HEADER  *LockBoxParameterHeader;
> +  UINTN                              TempCommBufferSize;
> +
> +  DEBUG ((DEBUG_INFO, "SmmLockBox SmmLockBoxHandler Enter\n"));
> +
> +  //
> +  // If input is invalid, stop processing this SMI
> +  //
> +  if ((CommBuffer == NULL) || (CommBufferSize == NULL)) {
> +    return EFI_SUCCESS;
> +  }
> +
> +  TempCommBufferSize = *CommBufferSize;
> +
> +  //
> +  // Sanity check
> +  //
> +  if (TempCommBufferSize < sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_HEADER)) {
> +    DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size
> invalid!\n"));
> +    return EFI_SUCCESS;
> +  }
> +
> +  if (!IsBufferOutsideMmValid ((UINTN)CommBuffer, TempCommBufferSize))
> {
> +    DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer in SMRAM or
> overflow!\n"));
> +    return EFI_SUCCESS;
> +  }
> +
> +  LockBoxParameterHeader = (EFI_SMM_LOCK_BOX_PARAMETER_HEADER
> *)((UINTN)CommBuffer);
> +
> +  LockBoxParameterHeader->ReturnStatus = (UINT64)-1;
> +
> +  DEBUG ((DEBUG_INFO, "SmmLockBox LockBoxParameterHeader - %x\n",
> (UINTN)LockBoxParameterHeader));
> +
> +  DEBUG ((DEBUG_INFO, "SmmLockBox Command - %x\n",
> (UINTN)LockBoxParameterHeader->Command));
> +
> +  switch (LockBoxParameterHeader->Command) {
> +    case EFI_SMM_LOCK_BOX_COMMAND_SAVE:
> +      if (TempCommBufferSize < sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_SAVE)) {
> +        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for SAVE
> invalid!\n"));
> +        break;
> +      }
> +
> +      SmmLockBoxSave ((EFI_SMM_LOCK_BOX_PARAMETER_SAVE
> *)(UINTN)LockBoxParameterHeader);
> +      break;
> +    case EFI_SMM_LOCK_BOX_COMMAND_UPDATE:
> +      if (TempCommBufferSize < sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_UPDATE)) {
> +        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for
> UPDATE invalid!\n"));
> +        break;
> +      }
> +
> +      SmmLockBoxUpdate ((EFI_SMM_LOCK_BOX_PARAMETER_UPDATE
> *)(UINTN)LockBoxParameterHeader);
> +      break;
> +    case EFI_SMM_LOCK_BOX_COMMAND_RESTORE:
> +      if (TempCommBufferSize < sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE)) {
> +        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for
> RESTORE invalid!\n"));
> +        break;
> +      }
> +
> +      SmmLockBoxRestore ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE
> *)(UINTN)LockBoxParameterHeader);
> +      break;
> +    case EFI_SMM_LOCK_BOX_COMMAND_SET_ATTRIBUTES:
> +      if (TempCommBufferSize < sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES)) {
> +        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for
> SET_ATTRIBUTES invalid!\n"));
> +        break;
> +      }
> +
> +      SmmLockBoxSetAttributes
> ((EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES
> *)(UINTN)LockBoxParameterHeader);
> +      break;
> +    case EFI_SMM_LOCK_BOX_COMMAND_RESTORE_ALL_IN_PLACE:
> +      if (TempCommBufferSize < sizeof
> (EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE)) {
> +        DEBUG ((DEBUG_ERROR, "SmmLockBox Command Buffer Size for
> RESTORE_ALL_IN_PLACE invalid!\n"));
> +        break;
> +      }
> +
> +      SmmLockBoxRestoreAllInPlace
> ((EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE
> *)(UINTN)LockBoxParameterHeader);
> +      break;
> +    default:
> +      DEBUG ((DEBUG_ERROR, "SmmLockBox Command invalid!\n"));
> +      break;
> +  }
> +
> +  LockBoxParameterHeader->Command = (UINT32)-1;
> +
> +  DEBUG ((DEBUG_INFO, "SmmLockBox SmmLockBoxHandler Exit\n"));
> +
> +  return EFI_SUCCESS;
> +}
> +
> +/**
> +  Smm Ready To Lock event notification handler.
> +
> +  It sets a flag indicating that SMRAM has been locked.
> +
> +  @param[in] Protocol   Points to the protocol's unique identifier.
> +  @param[in] Interface  Points to the interface instance.
> +  @param[in] Handle     The handle on which the interface was installed.
> +
> +  @retval EFI_SUCCESS   Notification handler runs successfully.
> + **/
> +EFI_STATUS
> +EFIAPI
> +SmmReadyToLockEventNotify (
> +  IN CONST EFI_GUID  *Protocol,
> +  IN VOID            *Interface,
> +  IN EFI_HANDLE      Handle
> +  )
> +{
> +  mLocked = TRUE;
> +  return EFI_SUCCESS;
> +}
> diff --git
> a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h
> new file mode 100644
> index 0000000000..2205c4fb3b
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxCommon.h
> @@ -0,0 +1,148 @@
> +/** @file
> +  LockBox SMM/MM driver.
> +
> +  Caution: This module requires additional review when modified.
> +  This driver will have external input - communicate buffer in SMM mode.
> +  This external input must be validated carefully to avoid security issue like
> +  buffer overflow, integer overflow.
> +
> +  SmmLockBoxHandler(), SmmLockBoxRestore(), SmmLockBoxUpdate(),
> SmmLockBoxSave()
> +  will receive untrusted input and do basic validation.
> +
> +Copyright (c) 2010 - 2024, Intel Corporation. All rights reserved.<BR>
> +
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <PiSmm.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/LockBoxLib.h>
> +
> +#include <Protocol/SmmReadyToLock.h>
> +#include <Protocol/SmmCommunication.h>
> +#include <Protocol/LockBox.h>
> +#include <Guid/SmmLockBox.h>
> +
> +/**
> +  This function is an abstraction layer for implementation specific Mm buffer
> validation routine.
> +
> +  @param Buffer  The buffer start address to be checked.
> +  @param Length  The buffer length to be checked.
> +
> +  @retval TRUE  This buffer is valid per processor architecture and not overlap
> with SMRAM.
> +  @retval FALSE This buffer is not valid per processor architecture or overlap
> with SMRAM.
> +**/
> +BOOLEAN
> +IsBufferOutsideMmValid (
> +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> +  IN UINT64                Length
> +  );
> +
> +/**
> +  Dispatch function for SMM lock box save.
> +
> +  Caution: This function may receive untrusted input.
> +  Restore buffer and length are external input, so this function will validate
> +  it is in SMRAM.
> +
> +  @param LockBoxParameterSave  parameter of lock box save
> +**/
> +VOID
> +SmmLockBoxSave (
> +  IN EFI_SMM_LOCK_BOX_PARAMETER_SAVE  *LockBoxParameterSave
> +  );
> +
> +/**
> +  Dispatch function for SMM lock box set attributes.
> +
> +  @param LockBoxParameterSetAttributes  parameter of lock box set
> attributes
> +**/
> +VOID
> +SmmLockBoxSetAttributes (
> +  IN EFI_SMM_LOCK_BOX_PARAMETER_SET_ATTRIBUTES
> *LockBoxParameterSetAttributes
> +  );
> +
> +/**
> +  Dispatch function for SMM lock box update.
> +
> +  Caution: This function may receive untrusted input.
> +  Restore buffer and length are external input, so this function will validate
> +  it is in SMRAM.
> +
> +  @param LockBoxParameterUpdate  parameter of lock box update
> +**/
> +VOID
> +SmmLockBoxUpdate (
> +  IN EFI_SMM_LOCK_BOX_PARAMETER_UPDATE  *LockBoxParameterUpdate
> +  );
> +
> +/**
> +  Dispatch function for SMM lock box restore.
> +
> +  Caution: This function may receive untrusted input.
> +  Restore buffer and length are external input, so this function will validate
> +  it is in SMRAM.
> +
> +  @param LockBoxParameterRestore  parameter of lock box restore
> +**/
> +VOID
> +SmmLockBoxRestore (
> +  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE
> *LockBoxParameterRestore
> +  );
> +
> +/**
> +  Dispatch function for SMM lock box restore all in place.
> +
> +  @param LockBoxParameterRestoreAllInPlace  parameter of lock box restore
> all in place
> +**/
> +VOID
> +SmmLockBoxRestoreAllInPlace (
> +  IN EFI_SMM_LOCK_BOX_PARAMETER_RESTORE_ALL_IN_PLACE
> *LockBoxParameterRestoreAllInPlace
> +  );
> +
> +/**
> +  Dispatch function for a Software SMI handler.
> +
> +  Caution: This function may receive untrusted input.
> +  Communicate buffer and buffer size are external input, so this function will
> do basic validation.
> +
> +  @param DispatchHandle  The unique handle assigned to this handler by
> SmiHandlerRegister().
> +  @param Context         Points to an optional handler context which was
> specified when the
> +                         handler was registered.
> +  @param CommBuffer      A pointer to a collection of data in memory that will
> +                         be conveyed from a non-SMM environment into an SMM
> environment.
> +  @param CommBufferSize  The size of the CommBuffer.
> +
> +  @retval EFI_SUCCESS Command is handled successfully.
> +
> +**/
> +EFI_STATUS
> +EFIAPI
> +SmmLockBoxHandler (
> +  IN EFI_HANDLE  DispatchHandle,
> +  IN CONST VOID  *Context         OPTIONAL,
> +  IN OUT VOID    *CommBuffer      OPTIONAL,
> +  IN OUT UINTN   *CommBufferSize  OPTIONAL
> +  );
> +
> +/**
> +  Smm Ready To Lock event notification handler.
> +
> +  It sets a flag indicating that SMRAM has been locked.
> +
> +  @param[in] Protocol   Points to the protocol's unique identifier.
> +  @param[in] Interface  Points to the interface instance.
> +  @param[in] Handle     The handle on which the interface was installed.
> +
> +  @retval EFI_SUCCESS   Notification handler runs successfully.
> + **/
> +EFI_STATUS
> +EFIAPI
> +SmmReadyToLockEventNotify (
> +  IN CONST EFI_GUID  *Protocol,
> +  IN VOID            *Interface,
> +  IN EFI_HANDLE      Handle
> +  );
> --
> 2.39.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118709): https://edk2.groups.io/g/devel/message/118709
Mute This Topic: https://groups.io/mt/105955700/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [edk2-devel] [PATCH 3/3] MdeModulePkg: Add Standalone MM Lockbox Driver.
  2024-05-07  6:09 ` [edk2-devel] [PATCH 3/3] MdeModulePkg: Add Standalone MM Lockbox Driver Yuanhao Xie
  2024-05-08  2:53   ` Ni, Ray
@ 2024-05-09  3:42   ` Wu, Jiaxin
  1 sibling, 0 replies; 10+ messages in thread
From: Wu, Jiaxin @ 2024-05-09  3:42 UTC (permalink / raw)
  To: Xie, Yuanhao, devel@edk2.groups.io; +Cc: Liming Gao, Ni, Ray

Reviewed-by: Jiaxin Wu <jiaxin.wu@intel.com>

> -----Original Message-----
> From: Xie, Yuanhao <yuanhao.xie@intel.com>
> Sent: Tuesday, May 7, 2024 2:09 PM
> To: devel@edk2.groups.io
> Cc: Liming Gao <gaoliming@byosoft.com.cn>; Wu, Jiaxin
> <jiaxin.wu@intel.com>; Ni, Ray <ray.ni@intel.com>; Xie, Yuanhao
> <yuanhao.xie@intel.com>
> Subject: [PATCH 3/3] MdeModulePkg: Add Standalone MM Lockbox Driver.
> 
> The Lockbox Driver allows sensitive data to be securely stored in a
> designated area, thus protected against unauthorized access.
> 
> This patch adds a Standalone MM Lockbox Driver with main modifications:
> 1. Separating shared code between the Standalone MM driver and the
> DXE MM Driver.
> 2. Utilizing services from the SMM Services Table (gSmst) as opposed to
>  relying on Boot Services.
> 
> Cc: Liming Gao <gaoliming@byosoft.com.cn>
> Cc: Jiaxin Wu <jiaxin.wu@intel.com>
> Cc: Ray Ni <ray.ni@intel.com>
> 
> Signed-off-by: Yuanhao Xie <yuanhao.xie@intel.com>
> ---
>  MdeModulePkg/MdeModulePkg.dsc                                             |  1 +
> 
> MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneM
> m.c        | 84
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> ++++++++++++++++++++++++++
> 
> MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneM
> m.inf      | 56
> ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
> 
> MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneM
> m.uni      | 14 ++++++++++++++
> 
> MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneM
> mExtra.uni | 14 ++++++++++++++
>  5 files changed, 169 insertions(+)
> 
> diff --git a/MdeModulePkg/MdeModulePkg.dsc
> b/MdeModulePkg/MdeModulePkg.dsc
> index 6bed9205ea..f0f02f180f 100644
> --- a/MdeModulePkg/MdeModulePkg.dsc
> +++ b/MdeModulePkg/MdeModulePkg.dsc
> @@ -500,6 +500,7 @@
> 
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCode
> RouterSmm.inf
> 
> MdeModulePkg/Universal/ReportStatusCodeRouter/Smm/ReportStatusCode
> RouterStandaloneMm.inf
>    MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBox.inf
> +
> MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandaloneM
> m.inf
> 
> MdeModulePkg/Library/SmmMemoryAllocationProfileLib/SmmMemoryAlloc
> ationProfileLib.inf
> 
> MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemo
> ryAllocationProfileLib.inf
> 
> MdeModulePkg/Library/PiSmmCoreMemoryAllocationLib/PiSmmCoreMemo
> ryAllocationLib.inf
> diff --git
> a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandalone
> Mm.c
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandalone
> Mm.c
> new file mode 100644
> index 0000000000..503be7efa8
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandalone
> Mm.c
> @@ -0,0 +1,84 @@
> +/** @file
> +  LockBox MM driver.
> +
> +Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
> +
> +SPDX-License-Identifier: BSD-2-Clause-Patent
> +
> +**/
> +
> +#include <PiSmm.h>
> +#include <Library/StandaloneMmDriverEntryPoint.h>
> +#include <Library/MmServicesTableLib.h>
> +#include <Library/BaseLib.h>
> +#include <Library/BaseMemoryLib.h>
> +#include <Library/DebugLib.h>
> +#include <Library/StandaloneMmMemLib.h>
> +#include <Library/LockBoxLib.h>
> +
> +#include <Protocol/SmmReadyToLock.h>
> +#include <Protocol/SmmCommunication.h>
> +#include <Protocol/LockBox.h>
> +#include <Guid/SmmLockBox.h>
> +
> +#include "SmmLockBoxCommon.h"
> +
> +/**
> +  This function is an abstraction layer for implementation specific Mm buffer
> validation routine.
> +
> +  @param Buffer  The buffer start address to be checked.
> +  @param Length  The buffer length to be checked.
> +
> +  @retval TRUE  This buffer is valid per processor architecture and not overlap
> with SMRAM.
> +  @retval FALSE This buffer is not valid per processor architecture or overlap
> with SMRAM.
> +**/
> +BOOLEAN
> +IsBufferOutsideMmValid (
> +  IN EFI_PHYSICAL_ADDRESS  Buffer,
> +  IN UINT64                Length
> +  )
> +{
> +  return MmIsBufferOutsideMmValid (Buffer, Length);
> +}
> +
> +/**
> +  Entry Point for LockBox MM driver.
> +
> +  @param[in] ImageHandle  Image handle of this driver.
> +  @param[in] SystemTable  A Pointer to the EFI System Table.
> +
> +  @retval EFI_SUCEESS
> +  @return Others          Some error occurs.
> +**/
> +EFI_STATUS
> +EFIAPI
> +SmmLockBoxStandaloneMmEntryPoint (
> +  IN EFI_HANDLE           ImageHandle,
> +  IN EFI_MM_SYSTEM_TABLE  *SystemTable
> +  )
> +{
> +  EFI_STATUS  Status;
> +  EFI_HANDLE  DispatchHandle;
> +  VOID        *Registration;
> +
> +  //
> +  // Register LockBox communication handler
> +  //
> +  Status = gMmst->MmiHandlerRegister (
> +                    SmmLockBoxHandler,
> +                    &gEfiSmmLockBoxCommunicationGuid,
> +                    &DispatchHandle
> +                    );
> +  ASSERT_EFI_ERROR (Status);
> +
> +  //
> +  // Register SMM Ready To Lock Protocol notification
> +  //
> +  Status = gMmst->MmRegisterProtocolNotify (
> +                    &gEfiSmmReadyToLockProtocolGuid,
> +                    SmmReadyToLockEventNotify,
> +                    &Registration
> +                    );
> +  ASSERT_EFI_ERROR (Status);
> +  return Status;
> +}
> diff --git
> a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandalone
> Mm.inf
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandalone
> Mm.inf
> new file mode 100644
> index 0000000000..544c87790c
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandalone
> Mm.inf
> @@ -0,0 +1,56 @@
> +## @file
> +#  LockBox MM driver.
> +#
> +# Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
> +#
> +#  SPDX-License-Identifier: BSD-2-Clause-Patent
> +#
> +##
> +
> +[Defines]
> +  INF_VERSION                    = 0x00010005
> +  BASE_NAME                      = SmmLockBoxStandaloneMm
> +  MODULE_UNI_FILE                = SmmLockBoxStandaloneMm.uni
> +  FILE_GUID                      = a83a87a0-8a3e-482d-86c8-84a139f6ded0
> +  MODULE_TYPE                    = MM_STANDALONE
> +  VERSION_STRING                 = 1.0
> +  PI_SPECIFICATION_VERSION       = 0x00010032
> +  ENTRY_POINT                    = SmmLockBoxStandaloneMmEntryPoint
> +
> +#
> +# The following information is for reference only and not required by the build
> tools.
> +#
> +#  VALID_ARCHITECTURES           = IA32 X64
> +#
> +
> +[Sources]
> +  SmmLockBoxStandaloneMm.c
> +  SmmLockBoxCommon.c
> +  SmmLockBoxCommon.h
> +
> +[Packages]
> +  MdePkg/MdePkg.dec
> +  MdeModulePkg/MdeModulePkg.dec
> +  StandaloneMmPkg/StandaloneMmPkg.dec
> +
> +[LibraryClasses]
> +  MmServicesTableLib
> +  BaseLib
> +  BaseMemoryLib
> +  DebugLib
> +  LockBoxLib
> +  MemLib
> +  StandaloneMmDriverEntryPoint
> +
> +[Guids]
> +  gEfiSmmLockBoxCommunicationGuid   ## PRODUCES ## GUID #
> SmiHandlerRegister
> +
> +[Protocols]
> +  gEfiSmmReadyToLockProtocolGuid    ## NOTIFY
> +  gEfiLockBoxProtocolGuid           ## PRODUCES
> +
> +[Depex]
> +  TRUE
> +
> +[UserExtensions.TianoCore."ExtraFiles"]
> +  SmmLockBoxStandaloneMm.uni
> diff --git
> a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandalone
> Mm.uni
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandalone
> Mm.uni
> new file mode 100644
> index 0000000000..7f6218102f
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandalone
> Mm.uni
> @@ -0,0 +1,14 @@
> +// /** @file
> +// LockBox MM driver.
> +//
> +// Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
> +//
> +// SPDX-License-Identifier: BSD-2-Clause-Patent
> +//
> +// **/
> +
> +
> +#string STR_MODULE_ABSTRACT             #language en-US "LockBox MM
> driver."
> +
> +#string STR_MODULE_DESCRIPTION          #language en-US "LockBox MM
> driver."
> +
> diff --git
> a/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandalone
> MmExtra.uni
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandalone
> MmExtra.uni
> new file mode 100644
> index 0000000000..a5443ca5f9
> --- /dev/null
> +++
> b/MdeModulePkg/Universal/LockBox/SmmLockBox/SmmLockBoxStandalone
> MmExtra.uni
> @@ -0,0 +1,14 @@
> +// /** @file
> +// SmmLockBox Localized Strings and Content
> +//
> +// Copyright (c) 2024, Intel Corporation. All rights reserved.<BR>
> +//
> +// SPDX-License-Identifier: BSD-2-Clause-Patent
> +//
> +// **/
> +
> +#string STR_PROPERTIES_MODULE_NAME
> +#language en-US
> +"MM Lock Box Driver"
> +
> +
> --
> 2.39.1.windows.1



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118710): https://edk2.groups.io/g/devel/message/118710
Mute This Topic: https://groups.io/mt/105955701/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: [edk2-devel] [PATCH 1/3] StandaloneMmPkg: Add LockBox Dependency DXE Driver
  2024-05-08  2:46   ` Ni, Ray
@ 2024-05-09  3:42     ` Wu, Jiaxin
  0 siblings, 0 replies; 10+ messages in thread
From: Wu, Jiaxin @ 2024-05-09  3:42 UTC (permalink / raw)
  To: Ni, Ray, Xie, Yuanhao, devel@edk2.groups.io; +Cc: Liming Gao

[-- Attachment #1: Type: text/plain, Size: 1210 bytes --]

Reviewed-by: Jiaxin Wu <jiaxin.wu@intel.com<mailto:jiaxin.wu@intel.com>> after resolve Ray's concern.


From: Ni, Ray <ray.ni@intel.com>
Sent: Wednesday, May 8, 2024 10:46 AM
To: Xie, Yuanhao <yuanhao.xie@intel.com>; devel@edk2.groups.io
Cc: Liming Gao <gaoliming@byosoft.com.cn>; Wu, Jiaxin <jiaxin.wu@intel.com>
Subject: Re: [PATCH 1/3] StandaloneMmPkg: Add LockBox Dependency DXE Driver


+#include <Library/BaseLib.h>

[Ray] Can you check if BaseLib is really needed?

+#include <Protocol/LockBox.h>
+
+/**
+  It attempts to install the gEfiLockBoxProtocolGuid protocol into the system's DXE database
+  with NULL as the protocol interface to mark the protocol as handled in the system or to
+  act as a trigger.
[Ray] "mark the protocol as handled in the system or to act as a trigger", I don't quite understand it.



-=-=-=-=-=-=-=-=-=-=-=-
Groups.io Links: You receive all messages sent to this group.
View/Reply Online (#118711): https://edk2.groups.io/g/devel/message/118711
Mute This Topic: https://groups.io/mt/105955699/7686176
Group Owner: devel+owner@edk2.groups.io
Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io]
-=-=-=-=-=-=-=-=-=-=-=-



[-- Attachment #2: Type: text/html, Size: 4874 bytes --]

^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2024-05-09  3:42 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-05-07  6:09 [edk2-devel] [PATCH 0/3] Add Standalone MM Lockbox Driver Yuanhao Xie
2024-05-07  6:09 ` [edk2-devel] [PATCH 1/3] StandaloneMmPkg: Add LockBox Dependency DXE Driver Yuanhao Xie
2024-05-08  2:46   ` Ni, Ray
2024-05-09  3:42     ` Wu, Jiaxin
2024-05-07  6:09 ` [edk2-devel] [PATCH 2/3] MdeModulePkg: Refactors SmmLockBox.c Yuanhao Xie
2024-05-08  2:50   ` Ni, Ray
2024-05-09  3:41   ` Wu, Jiaxin
2024-05-07  6:09 ` [edk2-devel] [PATCH 3/3] MdeModulePkg: Add Standalone MM Lockbox Driver Yuanhao Xie
2024-05-08  2:53   ` Ni, Ray
2024-05-09  3:42   ` Wu, Jiaxin

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox