summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJiewen Yao <jiewen.yao@intel.com>2017-11-02 18:21:13 +0800
committerJiewen Yao <jiewen.yao@intel.com>2018-07-26 23:06:41 +0800
commitc127d3953b971cc31d8c5377b96ff9576d145938 (patch)
tree02066f689861319206adcb4eafbb315742e184ba
parent993fb9ee224d38f0370d34ea188d3e61ecedf687 (diff)
downloadedk2-c127d3953b971cc31d8c5377b96ff9576d145938.tar.gz
edk2-c127d3953b971cc31d8c5377b96ff9576d145938.tar.bz2
edk2-c127d3953b971cc31d8c5377b96ff9576d145938.zip
MdePkg/SmmMemLib: Check for untested memory in GCD
It treats GCD untested memory as invalid SMM communication buffer. Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jiewen Yao <jiewen.yao@intel.com>
-rw-r--r--MdePkg/Library/SmmMemLib/SmmMemLib.c97
-rw-r--r--MdePkg/Library/SmmMemLib/SmmMemLib.inf3
2 files changed, 93 insertions, 7 deletions
diff --git a/MdePkg/Library/SmmMemLib/SmmMemLib.c b/MdePkg/Library/SmmMemLib/SmmMemLib.c
index b4e3156cb4..1e6d4e15a1 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,13 +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
));
- ASSERT (FALSE);
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;
}
@@ -319,6 +345,61 @@ SmmSetMem (
}
/**
+ 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.
@param[in] Protocol Points to the protocol's unique identifier.
@@ -416,10 +497,14 @@ SmmLibInternalEndOfDxeNotify (
gBS->FreePool (MemoryMap);
+ //
+ // Get additional information from GCD memory map.
+ //
+ SmmMemLibInternalGetGcdMemoryMap ();
+
return EFI_SUCCESS;
}
-
/**
Notification for SMM ReadyToLock protocol.
@@ -437,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 b1691cc277..1f8fd2766d 100644
--- a/MdePkg/Library/SmmMemLib/SmmMemLib.inf
+++ b/MdePkg/Library/SmmMemLib/SmmMemLib.inf
@@ -6,7 +6,7 @@
# all SMRAM range via SMM_ACCESS2_PROTOCOL, including the range for firmware (like SMM Core
# and SMM driver) and/or specific dedicated hardware.
#
-# Copyright (c) 2015 - 2016, Intel Corporation. All rights reserved.<BR>
+# Copyright (c) 2015 - 2017, Intel Corporation. All rights reserved.<BR>
# This program and the accompanying materials
# are licensed and made available under the terms and conditions of the BSD License
# which accompanies this distribution. The full text of the license may be found at
@@ -43,6 +43,7 @@
[LibraryClasses]
SmmServicesTableLib
UefiBootServicesTableLib
+ DxeServicesTableLib
DebugLib
BaseMemoryLib
HobLib