From: Kun Qin Date: Tue, 26 Apr 2022 00:47:46 +0000 (+0800) Subject: MdeModulePkg: PiSmmCore: Inspect memory guarded with pool headers X-Git-Tag: edk2-stable202205~29 X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=commitdiff_plain;h=17702186b56209842e002235c29ffec5ed69745a MdeModulePkg: PiSmmCore: Inspect memory guarded with pool headers REF: https://bugzilla.tianocore.org/show_bug.cgi?id=3488 Current free pool routine from PiSmmCore will inspect memory guard status for target buffer without considering pool headers. This could lead to `IsMemoryGuarded` function to return incorrect results. In that sense, allocating a 0 sized pool could cause an allocated buffer directly points into a guard page, which is legal. However, trying to free this pool will cause the routine changed in this commit to read XP pages, which leads to page fault. This change will inspect memory guarded with pool headers. This can avoid errors when a pool content happens to be on a page boundary. Cc: Jiewen Yao Cc: Eric Dong Cc: Ray Ni Cc: Jian J Wang Cc: Liming Gao Signed-off-by: Kun Qin Reviewed-by: Jian J Wang Reviewed-by: Liming Gao --- diff --git a/MdeModulePkg/Core/PiSmmCore/Pool.c b/MdeModulePkg/Core/PiSmmCore/Pool.c index 96ebe811c6..e1ff40a8ea 100644 --- a/MdeModulePkg/Core/PiSmmCore/Pool.c +++ b/MdeModulePkg/Core/PiSmmCore/Pool.c @@ -382,11 +382,6 @@ SmmInternalFreePool ( return EFI_INVALID_PARAMETER; } - MemoryGuarded = IsHeapGuardEnabled () && - IsMemoryGuarded ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer); - HasPoolTail = !(MemoryGuarded && - ((PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) == 0)); - FreePoolHdr = (FREE_POOL_HEADER *)((POOL_HEADER *)Buffer - 1); ASSERT (FreePoolHdr->Header.Signature == POOL_HEAD_SIGNATURE); ASSERT (!FreePoolHdr->Header.Available); @@ -394,6 +389,11 @@ SmmInternalFreePool ( return EFI_INVALID_PARAMETER; } + MemoryGuarded = IsHeapGuardEnabled () && + IsMemoryGuarded ((EFI_PHYSICAL_ADDRESS)(UINTN)FreePoolHdr); + HasPoolTail = !(MemoryGuarded && + ((PcdGet8 (PcdHeapGuardPropertyMask) & BIT7) == 0)); + if (HasPoolTail) { PoolTail = HEAD_TO_TAIL (&FreePoolHdr->Header); ASSERT (PoolTail->Signature == POOL_TAIL_SIGNATURE);