Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/release/202311' into update_memo…
Browse files Browse the repository at this point in the history
…ry_protection_readme
  • Loading branch information
TaylorBeebe committed Apr 27, 2024
2 parents fa075a2 + 9dd0624 commit 4d673a3
Show file tree
Hide file tree
Showing 19 changed files with 633 additions and 321 deletions.
2 changes: 2 additions & 0 deletions MdeModulePkg/Core/Dxe/DxeMain.inf
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
gMuEventPreExitBootServicesGuid ## PRODUCES ## Event // MU_CHANGE
gDxeMemoryProtectionSettingsGuid ## CONSUMES ## HOB // MU_CHANGE
gMemoryProtectionSpecialRegionHobGuid ## CONSUMES ## HOB // MU_CHANGE
gCompatibilityModeActivatedEventGuid ## SOMETIMES_PRODUCES ## Event // MU_CHANGE

[Ppis]
gEfiVectorHandoffInfoPpiGuid ## UNDEFINED # HOB
Expand Down Expand Up @@ -183,6 +184,7 @@
gMemoryProtectionDebugProtocolGuid ## SOMETIMES_PRODUCES ## MS_CHANGE
gEfiMemoryAttributeProtocolGuid ## CONSUMES ## MS_CHANGE
gMemoryProtectionSpecialRegionProtocolGuid ## PRODUCES ## MU_CHANGE
gEdkiiGcdSyncCompleteProtocolGuid ## CONSUMES ## MU_CHANGE

[Pcd]
gEfiMdeModulePkgTokenSpaceGuid.PcdLoadFixAddressBootTimeCodePageNumber ## SOMETIMES_CONSUMES
Expand Down
51 changes: 44 additions & 7 deletions MdeModulePkg/Core/Dxe/Mem/HeapGuard.c
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ SPDX-License-Identifier: BSD-2-Clause-Patent
//
GLOBAL_REMOVE_IF_UNREFERENCED BOOLEAN mOnGuarding = FALSE;

extern BOOLEAN mPageAttributesInitialized; // MU_CHANGE

//
// Pointer to table tracking the Guarded memory with bitmap, in which '1'
// is used to indicate memory guarded. '0' might be free memory or Guard
Expand Down Expand Up @@ -250,8 +252,22 @@ FindGuardedMemoryMap (
&MapMemory,
FALSE
);
ASSERT_EFI_ERROR (Status);
ASSERT (MapMemory != 0);
// MU_CHANGE START: Check if memory was successfully allocated
if (EFI_ERROR (Status) || (MapMemory == 0)) {
ASSERT_EFI_ERROR (Status);
ASSERT (MapMemory != 0);
return 0;
}

// MU_CHANGE END
// MU_CHANGE START: Apply Protection policy to the allocated memory
ApplyMemoryProtectionPolicy (
EfiConventionalMemory,
EfiBootServicesData,
MapMemory,
ALIGN_VALUE (Size, EFI_PAGE_SIZE)
);
// MU_CHANGE END

SetMem ((VOID *)(UINTN)MapMemory, Size, 0);

Expand Down Expand Up @@ -281,8 +297,22 @@ FindGuardedMemoryMap (
&MapMemory,
FALSE
);
ASSERT_EFI_ERROR (Status);
ASSERT (MapMemory != 0);
// MU_CHANGE START: Check if memory was successfully allocated
if (EFI_ERROR (Status) || (MapMemory == 0)) {
ASSERT_EFI_ERROR (Status);
ASSERT (MapMemory != 0);
return 0;
}

// MU_CHANGE END
// MU_CHANGE START: Apply Protection policy to the allocated memory
ApplyMemoryProtectionPolicy (
EfiConventionalMemory,
EfiBootServicesData,
MapMemory,
ALIGN_VALUE (Size, EFI_PAGE_SIZE)
);
// MU_CHANGE END

SetMem ((VOID *)(UINTN)MapMemory, Size, 0);
*GuardMap = MapMemory;
Expand Down Expand Up @@ -560,6 +590,13 @@ UnsetGuardPage (
Attributes |= EFI_MEMORY_XP;
}

// MU_CHANGE START: Add support for RP on free mem
if (gDxeMps.FreeMemoryReadProtected) {
Attributes |= EFI_MEMORY_RP;
}

// MU_CHANGE END

//
// Set flag to make sure allocating memory without GUARD for page table
// operation; otherwise infinite loops could be caused.
Expand Down Expand Up @@ -690,10 +727,10 @@ IsHeapGuardEnabled (
UINT8 GuardType
)
{
// MU_CHANGE START Update to work with memory protection settings HOB
// MU_CHANGE START: Update to work with memory protection settings HOB,
// remove freed memory guard.
if ((GuardType & GUARD_HEAP_TYPE_PAGE && gDxeMps.HeapGuardPolicy.Fields.UefiPageGuard) ||
(GuardType & GUARD_HEAP_TYPE_POOL && gDxeMps.HeapGuardPolicy.Fields.UefiPoolGuard) ||
(GuardType & GUARD_HEAP_TYPE_FREED && gDxeMps.HeapGuardPolicy.Fields.UefiFreedMemoryGuard))
(GuardType & GUARD_HEAP_TYPE_POOL && gDxeMps.HeapGuardPolicy.Fields.UefiPoolGuard))
{
return TRUE;
}
Expand Down
59 changes: 38 additions & 21 deletions MdeModulePkg/Core/Dxe/Mem/Page.c
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,15 @@ AllocateMemoryMapEntry (
DEFAULT_PAGE_ALLOCATION_GRANULARITY,
FALSE
);
// MU_CHANGE START: The above call to CoreAllocatePoolPages() sidesteps the application of the
// memory protection policy so apply it here to avoid a potential page fault
ApplyMemoryProtectionPolicy (
EfiConventionalMemory,
EfiBootServicesData,
(EFI_PHYSICAL_ADDRESS)(UINTN)FreeDescriptorEntries,
DEFAULT_PAGE_ALLOCATION_GRANULARITY
);
// MU_CHANGE END
if (FreeDescriptorEntries != NULL) {
//
// Enque the free memmory map entries into the list
Expand Down Expand Up @@ -947,6 +956,21 @@ CoreConvertPagesEx (
Entry = NULL;
}

// MU_CHANGE [BEGIN]
// The below call may allocate pages which, if we're freeing memory (implied by
// the new type being EfiConventionalMemory), could cause the memory we're currently
// freeing to be allocated before we're done freeing it if CoreFreeMemoryMapStack()
// is called after AddRange(). So, if we are freeing, let's free the memory map
// stack before adding memory we're converting to the free list.
if (ChangingType && (NewType == EfiConventionalMemory)) {
//
// Move any map descriptor stack to general pool
//
CoreFreeMemoryMapStack ();
}

// MU_CHANGE [END]

//
// Add our new range in. Don't do this for freed pages if freed-memory
// guard is enabled.
Expand All @@ -973,10 +997,20 @@ CoreConvertPagesEx (
}
}

//
// Move any map descriptor stack to general pool
//
CoreFreeMemoryMapStack ();
// MU_CHANGE [BEGIN]
// The below call may allocate pages which, if we're allocating memory (implied by
// the new type not being EfiConventionalMemory), could cause the range we're currently
// converting to also be allocated in the below call. To avoid this case, we should
// call CoreFreeMemoryMapStack() after we've called AddRange() to mark this memory
// as allocated.
if (!ChangingType || (ChangingType && (NewType != EfiConventionalMemory))) {
//
// Move any map descriptor stack to general pool
//
CoreFreeMemoryMapStack ();
}

// MU_CHANGE [END]

//
// Bump the starting address, and convert the next range
Expand Down Expand Up @@ -1578,23 +1612,6 @@ CoreInternalFreePages (
UINTN Alignment;
BOOLEAN IsGuarded;

// MU_CHANGE Start: Unprotect page(s) before free if the memory will be cleared on free
UINT64 Attributes;

if (DebugClearMemoryEnabled () && (mMemoryAttributeProtocol != NULL)) {
Status = mMemoryAttributeProtocol->GetMemoryAttributes (mMemoryAttributeProtocol, Memory, EFI_PAGES_TO_SIZE (NumberOfPages), &Attributes);

if ((Attributes & EFI_MEMORY_RO) || (Attributes & EFI_MEMORY_RP) || (Status == EFI_NO_MAPPING)) {
Status = ClearAccessAttributesFromMemoryRange (Memory, EFI_PAGES_TO_SIZE (NumberOfPages));

if (EFI_ERROR (Status) && (Status != EFI_NOT_READY)) {
DEBUG ((DEBUG_WARN, "%a - Unable to clear attributes from memory at base: 0x%llx\n", __FUNCTION__, Memory));
}
}
}

// MU_CHANGE End

//
// Free the range
//
Expand Down
Loading

0 comments on commit 4d673a3

Please sign in to comment.