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>
Reviewed-by: Star Zeng <star.zeng@intel.com>
This commit is contained in:
Jiewen Yao 2017-11-02 18:21:13 +08:00
parent cf4e79e466
commit 233ffa90cc
2 changed files with 92 additions and 5 deletions

View File

@ -25,12 +25,20 @@
#include <Library/DebugLib.h> #include <Library/DebugLib.h>
#include <Library/MemoryAllocationLib.h> #include <Library/MemoryAllocationLib.h>
#include <Library/UefiBootServicesTableLib.h> #include <Library/UefiBootServicesTableLib.h>
#include <Library/DxeServicesTableLib.h>
#include <Library/SmmServicesTableLib.h> #include <Library/SmmServicesTableLib.h>
#include <Library/HobLib.h> #include <Library/HobLib.h>
#include <Protocol/SmmAccess2.h> #include <Protocol/SmmAccess2.h>
#include <Protocol/SmmReadyToLock.h> #include <Protocol/SmmReadyToLock.h>
#include <Protocol/SmmEndOfDxe.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) \ #define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \
((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size))) ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size)))
@ -46,10 +54,13 @@ UINTN mMemoryMapEntryCount;
EFI_MEMORY_DESCRIPTOR *mMemoryMap; EFI_MEMORY_DESCRIPTOR *mMemoryMap;
UINTN mDescriptorSize; UINTN mDescriptorSize;
EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mSmmMemLibGcdMemSpace = NULL;
UINTN mSmmMemLibGcdMemNumberOfDesc = 0;
VOID *mRegistrationEndOfDxe; VOID *mRegistrationEndOfDxe;
VOID *mRegistrationReadyToLock; VOID *mRegistrationReadyToLock;
BOOLEAN mSmmReadyToLock = FALSE; BOOLEAN mSmmMemLibSmmReadyToLock = FALSE;
/** /**
Calculate and save the maximum support address. Calculate and save the maximum support address.
@ -154,7 +165,7 @@ SmmIsBufferOutsideSmmValid (
// //
// Check override for Valid Communication Region // Check override for Valid Communication Region
// //
if (mSmmReadyToLock) { if (mSmmMemLibSmmReadyToLock) {
EFI_MEMORY_DESCRIPTOR *MemoryMap; EFI_MEMORY_DESCRIPTOR *MemoryMap;
BOOLEAN InValidCommunicationRegion; BOOLEAN InValidCommunicationRegion;
@ -171,12 +182,28 @@ SmmIsBufferOutsideSmmValid (
if (!InValidCommunicationRegion) { if (!InValidCommunicationRegion) {
DEBUG (( DEBUG ((
EFI_D_ERROR, 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, Buffer,
Length Length
)); ));
return 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; return TRUE;
} }
@ -317,6 +344,61 @@ SmmSetMem (
return EFI_SUCCESS; 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. Notification for SMM EndOfDxe protocol.
@ -415,10 +497,14 @@ SmmLibInternalEndOfDxeNotify (
gBS->FreePool (MemoryMap); gBS->FreePool (MemoryMap);
//
// Get additional information from GCD memory map.
//
SmmMemLibInternalGetGcdMemoryMap ();
return EFI_SUCCESS; return EFI_SUCCESS;
} }
/** /**
Notification for SMM ReadyToLock protocol. Notification for SMM ReadyToLock protocol.
@ -436,7 +522,7 @@ SmmLibInternalReadyToLockNotify (
IN EFI_HANDLE Handle IN EFI_HANDLE Handle
) )
{ {
mSmmReadyToLock = TRUE; mSmmMemLibSmmReadyToLock = TRUE;
return EFI_SUCCESS; return EFI_SUCCESS;
} }
/** /**

View File

@ -43,6 +43,7 @@
[LibraryClasses] [LibraryClasses]
SmmServicesTableLib SmmServicesTableLib
UefiBootServicesTableLib UefiBootServicesTableLib
DxeServicesTableLib
DebugLib DebugLib
BaseMemoryLib BaseMemoryLib
HobLib HobLib