public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
From: Hao Wu <hao.a.wu@intel.com>
To: edk2-devel@lists.01.org
Cc: Jiewen Yao <jiewen.yao@intel.com>, Star Zeng <star.zeng@intel.com>
Subject: [PATCH 1/6] MdePkg/SmmMemLib: Check for untested memory in GCD
Date: Fri, 20 Jul 2018 13:26:21 +0800	[thread overview]
Message-ID: <20180720052626.24932-2-hao.a.wu@intel.com> (raw)
In-Reply-To: <20180720052626.24932-1-hao.a.wu@intel.com>

From: Jiewen Yao <jiewen.yao@intel.com>

It treats GCD untested memory as invalid SMM
communication buffer.

Cc: Star Zeng <star.zeng@intel.com>
Contributed-under: TianoCore Contribution Agreement 1.1
Signed-off-by: Jiewen Yao <jiewen.yao@intel.com>
---
 MdePkg/Library/SmmMemLib/SmmMemLib.c   | 96 +++++++++++++++++++-
 MdePkg/Library/SmmMemLib/SmmMemLib.inf |  1 +
 2 files changed, 92 insertions(+), 5 deletions(-)

diff --git a/MdePkg/Library/SmmMemLib/SmmMemLib.c b/MdePkg/Library/SmmMemLib/SmmMemLib.c
index 8c78a0b426..3f79e46d46 100644
--- a/MdePkg/Library/SmmMemLib/SmmMemLib.c
+++ b/MdePkg/Library/SmmMemLib/SmmMemLib.c
@@ -25,12 +25,20 @@
 #include <Library/DebugLib.h>
 #include <Library/MemoryAllocationLib.h>
 #include <Library/UefiBootServicesTableLib.h>
+#include <Library/DxeServicesTableLib.h>
 #include <Library/SmmServicesTableLib.h>
 #include <Library/HobLib.h>
 #include <Protocol/SmmAccess2.h>
 #include <Protocol/SmmReadyToLock.h>
 #include <Protocol/SmmEndOfDxe.h>
 
+//
+// attributes for reserved memory before it is promoted to system memory
+//
+#define EFI_MEMORY_PRESENT      0x0100000000000000ULL
+#define EFI_MEMORY_INITIALIZED  0x0200000000000000ULL
+#define EFI_MEMORY_TESTED       0x0400000000000000ULL
+
 #define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
   ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))
 
@@ -46,10 +54,13 @@ UINTN                 mMemoryMapEntryCount;
 EFI_MEMORY_DESCRIPTOR *mMemoryMap;
 UINTN                 mDescriptorSize;
 
+EFI_GCD_MEMORY_SPACE_DESCRIPTOR   *mSmmMemLibGcdMemSpace       = NULL;
+UINTN                             mSmmMemLibGcdMemNumberOfDesc = 0;
+
 VOID                  *mRegistrationEndOfDxe;
 VOID                  *mRegistrationReadyToLock;
 
-BOOLEAN               mSmmReadyToLock = FALSE;
+BOOLEAN               mSmmMemLibSmmReadyToLock = FALSE;
 
 /**
   Calculate and save the maximum support address.
@@ -154,7 +165,7 @@ SmmIsBufferOutsideSmmValid (
   //
   // Check override for Valid Communication Region
   //
-  if (mSmmReadyToLock) {
+  if (mSmmMemLibSmmReadyToLock) {
     EFI_MEMORY_DESCRIPTOR          *MemoryMap;
     BOOLEAN                        InValidCommunicationRegion;
 
@@ -171,12 +182,28 @@ SmmIsBufferOutsideSmmValid (
     if (!InValidCommunicationRegion) {
       DEBUG ((
         EFI_D_ERROR,
-        "SmmIsBufferOutsideSmmValid: Not in ValidCommunicationRegion: Buffer (0x%lx) - Length (0x%lx), ",
+        "SmmIsBufferOutsideSmmValid: Not in ValidCommunicationRegion: Buffer (0x%lx) - Length (0x%lx)\n",
         Buffer,
         Length
         ));
       return FALSE;
     }
+
+    //
+    // Check untested memory as invalid communication buffer.
+    //
+    for (Index = 0; Index < mSmmMemLibGcdMemNumberOfDesc; Index++) {
+      if (((Buffer >= mSmmMemLibGcdMemSpace[Index].BaseAddress) && (Buffer < mSmmMemLibGcdMemSpace[Index].BaseAddress + mSmmMemLibGcdMemSpace[Index].Length)) ||
+          ((mSmmMemLibGcdMemSpace[Index].BaseAddress >= Buffer) && (mSmmMemLibGcdMemSpace[Index].BaseAddress < Buffer + Length))) {
+        DEBUG ((
+          EFI_D_ERROR,
+          "SmmIsBufferOutsideSmmValid: In Untested Memory Region: Buffer (0x%lx) - Length (0x%lx)\n",
+          Buffer,
+          Length
+          ));
+        return FALSE;
+      }
+    }
   }
   return TRUE;
 }
@@ -317,6 +344,61 @@ SmmSetMem (
   return EFI_SUCCESS;
 }
 
+/**
+  Get GCD memory map.
+  Only record untested memory as invalid communication buffer.
+**/
+VOID
+SmmMemLibInternalGetGcdMemoryMap (
+  VOID
+  )
+{
+  UINTN                            NumberOfDescriptors;
+  EFI_GCD_MEMORY_SPACE_DESCRIPTOR  *MemSpaceMap;
+  EFI_STATUS                       Status;
+  UINTN                            Index;
+
+  Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap);
+  if (EFI_ERROR (Status)) {
+    return ;
+  }
+
+  mSmmMemLibGcdMemNumberOfDesc = 0;
+  for (Index = 0; Index < NumberOfDescriptors; Index++) {
+    if (MemSpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeReserved &&
+        (MemSpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==
+          (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)
+          ) {
+      mSmmMemLibGcdMemNumberOfDesc++;
+    }
+  }
+
+  mSmmMemLibGcdMemSpace = AllocateZeroPool (mSmmMemLibGcdMemNumberOfDesc * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
+  ASSERT (mSmmMemLibGcdMemSpace != NULL);
+  if (mSmmMemLibGcdMemSpace == NULL) {
+    mSmmMemLibGcdMemNumberOfDesc = 0;
+    gBS->FreePool (MemSpaceMap);
+    return ;
+  }
+
+  mSmmMemLibGcdMemNumberOfDesc = 0;
+  for (Index = 0; Index < NumberOfDescriptors; Index++) {
+    if (MemSpaceMap[Index].GcdMemoryType == EfiGcdMemoryTypeReserved &&
+        (MemSpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED | EFI_MEMORY_TESTED)) ==
+          (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED)
+          ) {
+      CopyMem (
+        &mSmmMemLibGcdMemSpace[mSmmMemLibGcdMemNumberOfDesc],
+        &MemSpaceMap[Index],
+        sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR)
+        );
+      mSmmMemLibGcdMemNumberOfDesc++;
+    }
+  }
+
+  gBS->FreePool (MemSpaceMap);
+}
+
 /**
   Notification for SMM EndOfDxe protocol.
 
@@ -415,10 +497,14 @@ SmmLibInternalEndOfDxeNotify (
 
   gBS->FreePool (MemoryMap);
 
+  //
+  // Get additional information from GCD memory map.
+  //
+  SmmMemLibInternalGetGcdMemoryMap ();
+
   return EFI_SUCCESS;
 }
 
-
 /**
   Notification for SMM ReadyToLock protocol.
 
@@ -436,7 +522,7 @@ SmmLibInternalReadyToLockNotify (
   IN EFI_HANDLE      Handle
   )
 {
-  mSmmReadyToLock = TRUE;
+  mSmmMemLibSmmReadyToLock = TRUE;
   return EFI_SUCCESS;
 }
 /**
diff --git a/MdePkg/Library/SmmMemLib/SmmMemLib.inf b/MdePkg/Library/SmmMemLib/SmmMemLib.inf
index e4edad3af2..36576a4f2f 100644
--- a/MdePkg/Library/SmmMemLib/SmmMemLib.inf
+++ b/MdePkg/Library/SmmMemLib/SmmMemLib.inf
@@ -43,6 +43,7 @@
 [LibraryClasses]
   SmmServicesTableLib
   UefiBootServicesTableLib
+  DxeServicesTableLib
   DebugLib
   BaseMemoryLib
   HobLib
-- 
2.16.2.windows.1



  reply	other threads:[~2018-07-20  5:26 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-20  5:26 [PATCH 0/6] Check untested memory and EFI_MEMORY_RO Hao Wu
2018-07-20  5:26 ` Hao Wu [this message]
2018-07-20  5:26 ` [PATCH 2/6] UefiCpuPkg/PiSmmCpu: Check for untested memory in GCD Hao Wu
2018-07-20  5:26 ` [PATCH 3/6] MdeModulePkg/DxeCore: Install UEFI mem attrib table at EndOfDxe Hao Wu
2018-07-20  5:26 ` [PATCH 4/6] MdePkg/SmmMemLib: Check EFI_MEMORY_RO in UEFI mem attrib table Hao Wu
2018-07-20  5:26 ` [PATCH 5/6] UefiCpuPkg/PiSmmCpu: Check EFI_RUNTIME_RO " Hao Wu
2018-07-20  5:26 ` [PATCH 6/6] MdeModulePkg/DxeCore: Not update RtCode in MemAttrTable after EndOfDxe Hao Wu
2018-07-23  0:47 ` [PATCH 0/6] Check untested memory and EFI_MEMORY_RO Zeng, Star

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-list from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20180720052626.24932-2-hao.a.wu@intel.com \
    --to=devel@edk2.groups.io \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox