From: Jian J Wang Date: Mon, 29 Jan 2018 11:07:29 +0000 (+0800) Subject: MdeModulePkg/Core: fix feature conflict between NX and NULL detection X-Git-Tag: edk2-stable201903~2491 X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=commitdiff_plain;h=9b17c6511d50db493c49324aaf81c50c3676a111;ds=sidebyside MdeModulePkg/Core: fix feature conflict between NX and NULL detection If enabled, NX memory protection feature will mark all free memory as NX (non-executable), including page 0. This will overwrite the attributes of page 0 if NULL pointer detection feature is also enabled and then compromise the functionality of it. The solution is skipping the NX attributes setting to page 0 if NULL pointer detection feature is enabled. Cc: Star Zeng Cc: Eric Dong Cc: Jiewen Yao Cc: Ruiyu Ni Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jian J Wang Reviewed-by: Ruiyu Ni --- diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c index 862593f562..a4705bc7ce 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c @@ -845,10 +845,24 @@ InitializeDxeNxMemoryProtectionPolicy ( Attributes = GetPermissionAttributeForMemoryType (MemoryMapEntry->Type); if (Attributes != 0) { - SetUefiImageMemoryAttributes ( - MemoryMapEntry->PhysicalStart, - LShiftU64 (MemoryMapEntry->NumberOfPages, EFI_PAGE_SHIFT), - Attributes); + if (MemoryMapEntry->PhysicalStart == 0 && + PcdGet8 (PcdNullPointerDetectionPropertyMask) != 0) { + + ASSERT (MemoryMapEntry->NumberOfPages > 0); + // + // Skip page 0 if NULL pointer detection is enabled to avoid attributes + // overwritten. + // + SetUefiImageMemoryAttributes ( + MemoryMapEntry->PhysicalStart + EFI_PAGE_SIZE, + LShiftU64 (MemoryMapEntry->NumberOfPages - 1, EFI_PAGE_SHIFT), + Attributes); + } else { + SetUefiImageMemoryAttributes ( + MemoryMapEntry->PhysicalStart, + LShiftU64 (MemoryMapEntry->NumberOfPages, EFI_PAGE_SHIFT), + Attributes); + } } MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize); }