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 4/6] MdePkg/SmmMemLib: Check EFI_MEMORY_RO in UEFI mem attrib table.
Date: Fri, 20 Jul 2018 13:26:24 +0800	[thread overview]
Message-ID: <20180720052626.24932-5-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 the UEFI runtime page with EFI_MEMORY_RO attribute 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   | 58 +++++++++++++++++++-
 MdePkg/Library/SmmMemLib/SmmMemLib.inf |  4 ++
 2 files changed, 59 insertions(+), 3 deletions(-)

diff --git a/MdePkg/Library/SmmMemLib/SmmMemLib.c b/MdePkg/Library/SmmMemLib/SmmMemLib.c
index 3f79e46d46..3409ddf87c 100644
--- a/MdePkg/Library/SmmMemLib/SmmMemLib.c
+++ b/MdePkg/Library/SmmMemLib/SmmMemLib.c
@@ -27,10 +27,12 @@
 #include <Library/UefiBootServicesTableLib.h>
 #include <Library/DxeServicesTableLib.h>
 #include <Library/SmmServicesTableLib.h>
+#include <Library/UefiLib.h>
 #include <Library/HobLib.h>
 #include <Protocol/SmmAccess2.h>
 #include <Protocol/SmmReadyToLock.h>
 #include <Protocol/SmmEndOfDxe.h>
+#include <Guid/MemoryAttributesTable.h>
 
 //
 // attributes for reserved memory before it is promoted to system memory
@@ -39,9 +41,6 @@
 #define EFI_MEMORY_INITIALIZED  0x0200000000000000ULL
 #define EFI_MEMORY_TESTED       0x0400000000000000ULL
 
-#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
-  ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))
-
 EFI_SMRAM_DESCRIPTOR *mSmmMemLibInternalSmramRanges;
 UINTN                mSmmMemLibInternalSmramCount;
 
@@ -57,6 +56,8 @@ UINTN                 mDescriptorSize;
 EFI_GCD_MEMORY_SPACE_DESCRIPTOR   *mSmmMemLibGcdMemSpace       = NULL;
 UINTN                             mSmmMemLibGcdMemNumberOfDesc = 0;
 
+EFI_MEMORY_ATTRIBUTES_TABLE  *mSmmMemLibMemoryAttributesTable = NULL;
+
 VOID                  *mRegistrationEndOfDxe;
 VOID                  *mRegistrationReadyToLock;
 
@@ -204,6 +205,32 @@ SmmIsBufferOutsideSmmValid (
         return FALSE;
       }
     }
+
+    //
+    // Check UEFI runtime memory with EFI_MEMORY_RO as invalid communication buffer.
+    //
+    if (mSmmMemLibMemoryAttributesTable != NULL) {
+      EFI_MEMORY_DESCRIPTOR *Entry;
+
+      Entry = (EFI_MEMORY_DESCRIPTOR *)(mSmmMemLibMemoryAttributesTable + 1);
+      for (Index = 0; Index < mSmmMemLibMemoryAttributesTable->NumberOfEntries; Index++) {
+        if (Entry->Type == EfiRuntimeServicesCode || Entry->Type == EfiRuntimeServicesData) {
+          if ((Entry->Attribute & EFI_MEMORY_RO) != 0) {
+            if (((Buffer >= Entry->PhysicalStart) && (Buffer < Entry->PhysicalStart + LShiftU64 (Entry->NumberOfPages, EFI_PAGE_SHIFT))) ||
+                ((Entry->PhysicalStart >= Buffer) && (Entry->PhysicalStart < Buffer + Length))) {
+              DEBUG ((
+                EFI_D_ERROR,
+                "SmmIsBufferOutsideSmmValid: In RuntimeCode Region: Buffer (0x%lx) - Length (0x%lx)\n",
+                Buffer,
+                Length
+                ));
+              return FALSE;
+            }
+          }
+        }
+        Entry = NEXT_MEMORY_DESCRIPTOR (Entry, mSmmMemLibMemoryAttributesTable->DescriptorSize);
+      }
+    }
   }
   return TRUE;
 }
@@ -399,6 +426,26 @@ SmmMemLibInternalGetGcdMemoryMap (
   gBS->FreePool (MemSpaceMap);
 }
 
+/**
+  Get UEFI MemoryAttributesTable.
+**/
+VOID
+SmmMemLibInternalGetUefiMemoryAttributesTable (
+  VOID
+  )
+{
+  EFI_STATUS                   Status;
+  EFI_MEMORY_ATTRIBUTES_TABLE  *MemoryAttributesTable;
+  UINTN                        MemoryAttributesTableSize;
+
+  Status = EfiGetSystemConfigurationTable (&gEfiMemoryAttributesTableGuid, (VOID **)&MemoryAttributesTable);
+  if (!EFI_ERROR (Status)) {
+    MemoryAttributesTableSize = sizeof(EFI_MEMORY_ATTRIBUTES_TABLE) + MemoryAttributesTable->DescriptorSize * MemoryAttributesTable->NumberOfEntries;
+    mSmmMemLibMemoryAttributesTable = AllocateCopyPool (MemoryAttributesTableSize, MemoryAttributesTable);
+    ASSERT (mSmmMemLibMemoryAttributesTable != NULL);
+  }
+}
+
 /**
   Notification for SMM EndOfDxe protocol.
 
@@ -502,6 +549,11 @@ SmmLibInternalEndOfDxeNotify (
   //
   SmmMemLibInternalGetGcdMemoryMap ();
 
+  //
+  // Get UEFI memory attributes table.
+  //
+  SmmMemLibInternalGetUefiMemoryAttributesTable ();
+
   return EFI_SUCCESS;
 }
 
diff --git a/MdePkg/Library/SmmMemLib/SmmMemLib.inf b/MdePkg/Library/SmmMemLib/SmmMemLib.inf
index 36576a4f2f..525449c25c 100644
--- a/MdePkg/Library/SmmMemLib/SmmMemLib.inf
+++ b/MdePkg/Library/SmmMemLib/SmmMemLib.inf
@@ -48,11 +48,15 @@
   BaseMemoryLib
   HobLib
   MemoryAllocationLib
+  UefiLib
 
 [Protocols]
   gEfiSmmAccess2ProtocolGuid     ## CONSUMES
   gEfiSmmReadyToLockProtocolGuid ## CONSUMES
   gEfiSmmEndOfDxeProtocolGuid    ## CONSUMES
 
+[Guids]
+  gEfiMemoryAttributesTableGuid  ## CONSUMES ## SystemTable
+
 [Depex]
   gEfiSmmAccess2ProtocolGuid
-- 
2.16.2.windows.1



  parent 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 ` [PATCH 1/6] MdePkg/SmmMemLib: Check for untested memory in GCD Hao Wu
2018-07-20  5:26 ` [PATCH 2/6] UefiCpuPkg/PiSmmCpu: " 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 ` Hao Wu [this message]
2018-07-20  5:26 ` [PATCH 5/6] UefiCpuPkg/PiSmmCpu: Check EFI_RUNTIME_RO in UEFI mem attrib table 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-5-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