UefiCpuPkg/PiSmmCpu: 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 19:00:02 +08:00
parent 233ffa90cc
commit ac6613db46
1 changed files with 120 additions and 24 deletions

View File

@ -13,6 +13,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED.
#include "PiSmmCpuDxeSmm.h" #include "PiSmmCpuDxeSmm.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)))
@ -23,6 +30,9 @@ EFI_MEMORY_DESCRIPTOR *mUefiMemoryMap;
UINTN mUefiMemoryMapSize; UINTN mUefiMemoryMapSize;
UINTN mUefiDescriptorSize; UINTN mUefiDescriptorSize;
EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mGcdMemSpace = NULL;
UINTN mGcdMemNumberOfDesc = 0;
PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] = { PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] = {
{Page4K, SIZE_4KB, PAGING_4K_ADDRESS_MASK_64}, {Page4K, SIZE_4KB, PAGING_4K_ADDRESS_MASK_64},
{Page2M, SIZE_2MB, PAGING_2M_ADDRESS_MASK_64}, {Page2M, SIZE_2MB, PAGING_2M_ADDRESS_MASK_64},
@ -1022,6 +1032,60 @@ MergeMemoryMapForNotPresentEntry (
return ; return ;
} }
/**
This function caches the GCD memory map information.
**/
VOID
GetGcdMemoryMap (
VOID
)
{
UINTN NumberOfDescriptors;
EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap;
EFI_STATUS Status;
UINTN Index;
Status = gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap);
if (EFI_ERROR (Status)) {
return ;
}
mGcdMemNumberOfDesc = 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)
) {
mGcdMemNumberOfDesc++;
}
}
mGcdMemSpace = AllocateZeroPool (mGcdMemNumberOfDesc * sizeof (EFI_GCD_MEMORY_SPACE_DESCRIPTOR));
ASSERT (mGcdMemSpace != NULL);
if (mGcdMemSpace == NULL) {
mGcdMemNumberOfDesc = 0;
gBS->FreePool (MemSpaceMap);
return ;
}
mGcdMemNumberOfDesc = 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 (
&mGcdMemSpace[mGcdMemNumberOfDesc],
&MemSpaceMap[Index],
sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR)
);
mGcdMemNumberOfDesc++;
}
}
gBS->FreePool (MemSpaceMap);
}
/** /**
This function caches the UEFI memory map information. This function caches the UEFI memory map information.
**/ **/
@ -1081,6 +1145,11 @@ GetUefiMemoryMap (
ASSERT (mUefiMemoryMap != NULL); ASSERT (mUefiMemoryMap != NULL);
gBS->FreePool (MemoryMap); gBS->FreePool (MemoryMap);
//
// Get additional information from GCD memory map.
//
GetGcdMemoryMap ();
} }
/** /**
@ -1102,33 +1171,52 @@ SetUefiMemMapAttributes (
DEBUG ((DEBUG_INFO, "SetUefiMemMapAttributes\n")); DEBUG ((DEBUG_INFO, "SetUefiMemMapAttributes\n"));
if (mUefiMemoryMap == NULL) { if (mUefiMemoryMap != NULL) {
DEBUG ((DEBUG_INFO, "UefiMemoryMap - NULL\n")); MemoryMapEntryCount = mUefiMemoryMapSize/mUefiDescriptorSize;
return ; MemoryMap = mUefiMemoryMap;
for (Index = 0; Index < MemoryMapEntryCount; Index++) {
if (IsUefiPageNotPresent(MemoryMap)) {
Status = SmmSetMemoryAttributes (
MemoryMap->PhysicalStart,
EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages),
EFI_MEMORY_RP
);
DEBUG ((
DEBUG_INFO,
"UefiMemory protection: 0x%lx - 0x%lx %r\n",
MemoryMap->PhysicalStart,
MemoryMap->PhysicalStart + (UINT64)EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages),
Status
));
}
MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize);
}
} }
//
// Do not free mUefiMemoryMap, it will be checked in IsSmmCommBufferForbiddenAddress().
//
MemoryMapEntryCount = mUefiMemoryMapSize/mUefiDescriptorSize; //
MemoryMap = mUefiMemoryMap; // Set untested memory as not present.
for (Index = 0; Index < MemoryMapEntryCount; Index++) { //
if (IsUefiPageNotPresent(MemoryMap)) { if (mGcdMemSpace != NULL) {
for (Index = 0; Index < mGcdMemNumberOfDesc; Index++) {
Status = SmmSetMemoryAttributes ( Status = SmmSetMemoryAttributes (
MemoryMap->PhysicalStart, mGcdMemSpace[Index].BaseAddress,
EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages), mGcdMemSpace[Index].Length,
EFI_MEMORY_RP EFI_MEMORY_RP
); );
DEBUG (( DEBUG ((
DEBUG_INFO, DEBUG_INFO,
"UefiMemory protection: 0x%lx - 0x%lx %r\n", "GcdMemory protection: 0x%lx - 0x%lx %r\n",
MemoryMap->PhysicalStart, mGcdMemSpace[Index].BaseAddress,
MemoryMap->PhysicalStart + (UINT64)EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages), mGcdMemSpace[Index].BaseAddress + mGcdMemSpace[Index].Length,
Status Status
)); ));
} }
MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize);
} }
// //
// Do free mUefiMemoryMap, it will be checked in IsSmmCommBufferForbiddenAddress(). // Do not free mGcdMemSpace, it will be checked in IsSmmCommBufferForbiddenAddress().
// //
} }
@ -1149,21 +1237,29 @@ IsSmmCommBufferForbiddenAddress (
UINTN MemoryMapEntryCount; UINTN MemoryMapEntryCount;
UINTN Index; UINTN Index;
if (mUefiMemoryMap == NULL) { if (mUefiMemoryMap != NULL) {
return FALSE; MemoryMap = mUefiMemoryMap;
MemoryMapEntryCount = mUefiMemoryMapSize/mUefiDescriptorSize;
for (Index = 0; Index < MemoryMapEntryCount; Index++) {
if (IsUefiPageNotPresent (MemoryMap)) {
if ((Address >= MemoryMap->PhysicalStart) &&
(Address < MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages)) ) {
return TRUE;
}
}
MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize);
}
} }
MemoryMap = mUefiMemoryMap; if (mGcdMemSpace != NULL) {
MemoryMapEntryCount = mUefiMemoryMapSize/mUefiDescriptorSize; for (Index = 0; Index < mGcdMemNumberOfDesc; Index++) {
for (Index = 0; Index < MemoryMapEntryCount; Index++) { if ((Address >= mGcdMemSpace[Index].BaseAddress) &&
if (IsUefiPageNotPresent (MemoryMap)) { (Address < mGcdMemSpace[Index].BaseAddress + mGcdMemSpace[Index].Length) ) {
if ((Address >= MemoryMap->PhysicalStart) &&
(Address < MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages)) ) {
return TRUE; return TRUE;
} }
} }
MemoryMap = NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize);
} }
return FALSE; return FALSE;
} }