X-Git-Url: https://git.proxmox.com/?p=mirror_edk2.git;a=blobdiff_plain;f=MdeModulePkg%2FCore%2FDxe%2FMisc%2FMemoryProtection.c;h=fa8f8fe91ac7a8e9f7f5e9805bf2ac63c77f4d5e;hp=451cc35b92908f9ba1c83724cdab06ae6f593f8f;hb=05cfd73f011f67541cf4add560ab213daa8a99ed;hpb=38b15ebe4fd5888493131d30fc31833a5e9a7d36 diff --git a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c index 451cc35b92..fa8f8fe91a 100644 --- a/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c +++ b/MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c @@ -19,7 +19,7 @@ Once the image is unloaded, the protection is removed automatically. -Copyright (c) 2017, Intel Corporation. All rights reserved.
+Copyright (c) 2017 - 2018, Intel Corporation. All rights reserved.
This program and the accompanying materials are licensed and made available under the terms and conditions of the BSD License which accompanies this distribution. The full text of the license may be found at @@ -44,10 +44,10 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER EXPRESS OR IMPLIED. #include #include -#include #include #include "DxeMain.h" +#include "Mem/HeapGuard.h" #define CACHE_ATTRIBUTE_MASK (EFI_MEMORY_UC | EFI_MEMORY_WC | EFI_MEMORY_WT | EFI_MEMORY_WB | EFI_MEMORY_UCE | EFI_MEMORY_WP) #define MEMORY_ATTRIBUTE_MASK (EFI_MEMORY_RP | EFI_MEMORY_XP | EFI_MEMORY_RO) @@ -74,6 +74,8 @@ UINT32 mImageProtectionPolicy; extern LIST_ENTRY mGcdMemorySpaceMap; +STATIC LIST_ENTRY mProtectedImageRecordList; + /** Sort code section in image record, based upon CodeSegmentBase from low to high. @@ -238,13 +240,10 @@ SetUefiImageMemoryAttributes ( Set UEFI image protection attributes. @param[in] ImageRecord A UEFI image record - @param[in] Protect TRUE: Protect the UEFI image. - FALSE: Unprotect the UEFI image. **/ VOID SetUefiImageProtectionAttributes ( - IN IMAGE_PROPERTIES_RECORD *ImageRecord, - IN BOOLEAN Protect + IN IMAGE_PROPERTIES_RECORD *ImageRecord ) { IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; @@ -253,7 +252,6 @@ SetUefiImageProtectionAttributes ( LIST_ENTRY *ImageRecordCodeSectionList; UINT64 CurrentBase; UINT64 ImageEnd; - UINT64 Attribute; ImageRecordCodeSectionList = &ImageRecord->CodeSegmentList; @@ -276,29 +274,19 @@ SetUefiImageProtectionAttributes ( // // DATA // - if (Protect) { - Attribute = EFI_MEMORY_XP; - } else { - Attribute = 0; - } SetUefiImageMemoryAttributes ( CurrentBase, ImageRecordCodeSection->CodeSegmentBase - CurrentBase, - Attribute + EFI_MEMORY_XP ); } // // CODE // - if (Protect) { - Attribute = EFI_MEMORY_RO; - } else { - Attribute = 0; - } SetUefiImageMemoryAttributes ( ImageRecordCodeSection->CodeSegmentBase, ImageRecordCodeSection->CodeSegmentSize, - Attribute + EFI_MEMORY_RO ); CurrentBase = ImageRecordCodeSection->CodeSegmentBase + ImageRecordCodeSection->CodeSegmentSize; } @@ -310,15 +298,10 @@ SetUefiImageProtectionAttributes ( // // DATA // - if (Protect) { - Attribute = EFI_MEMORY_XP; - } else { - Attribute = 0; - } SetUefiImageMemoryAttributes ( CurrentBase, ImageEnd - CurrentBase, - Attribute + EFI_MEMORY_XP ); } return ; @@ -401,18 +384,15 @@ FreeImageRecord ( } /** - Protect or unprotect UEFI image common function. + Protect UEFI PE/COFF image. @param[in] LoadedImage The loaded image protocol @param[in] LoadedImageDevicePath The loaded image device path protocol - @param[in] Protect TRUE: Protect the UEFI image. - FALSE: Unprotect the UEFI image. **/ VOID -ProtectUefiImageCommon ( +ProtectUefiImage ( IN EFI_LOADED_IMAGE_PROTOCOL *LoadedImage, - IN EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath, - IN BOOLEAN Protect + IN EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath ) { VOID *ImageAddress; @@ -426,7 +406,6 @@ ProtectUefiImageCommon ( IMAGE_PROPERTIES_RECORD *ImageRecord; CHAR8 *PdbPointer; IMAGE_PROPERTIES_RECORD_CODE_SECTION *ImageRecordCodeSection; - UINT16 Magic; BOOLEAN IsAligned; UINT32 ProtectionPolicy; @@ -486,21 +465,7 @@ ProtectUefiImageCommon ( // // Get SectionAlignment // - if (Hdr.Pe32->FileHeader.Machine == IMAGE_FILE_MACHINE_IA64 && Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { - // - // NOTE: Some versions of Linux ELILO for Itanium have an incorrect magic value - // in the PE/COFF Header. If the MachineType is Itanium(IA64) and the - // Magic value in the OptionalHeader is EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC - // then override the magic value to EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC - // - Magic = EFI_IMAGE_NT_OPTIONAL_HDR64_MAGIC; - } else { - // - // Get the magic value from the PE/COFF Optional Header - // - Magic = Hdr.Pe32->OptionalHeader.Magic; - } - if (Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { + if (Hdr.Pe32->OptionalHeader.Magic == EFI_IMAGE_NT_OPTIONAL_HDR32_MAGIC) { SectionAlignment = Hdr.Pe32->OptionalHeader.SectionAlignment; } else { SectionAlignment = Hdr.Pe32Plus->OptionalHeader.SectionAlignment; @@ -617,34 +582,17 @@ ProtectUefiImageCommon ( // // CPU ARCH present. Update memory attribute directly. // - SetUefiImageProtectionAttributes (ImageRecord, Protect); + SetUefiImageProtectionAttributes (ImageRecord); // - // Clean up + // Record the image record in the list so we can undo the protections later // - FreeImageRecord (ImageRecord); + InsertTailList (&mProtectedImageRecordList, &ImageRecord->Link); Finish: return ; } -/** - Protect UEFI image. - - @param[in] LoadedImage The loaded image protocol - @param[in] LoadedImageDevicePath The loaded image device path protocol -**/ -VOID -ProtectUefiImage ( - IN EFI_LOADED_IMAGE_PROTOCOL *LoadedImage, - IN EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath - ) -{ - if (PcdGet32(PcdImageProtectionPolicy) != 0) { - ProtectUefiImageCommon (LoadedImage, LoadedImageDevicePath, TRUE); - } -} - /** Unprotect UEFI image. @@ -657,8 +605,28 @@ UnprotectUefiImage ( IN EFI_DEVICE_PATH_PROTOCOL *LoadedImageDevicePath ) { + IMAGE_PROPERTIES_RECORD *ImageRecord; + LIST_ENTRY *ImageRecordLink; + if (PcdGet32(PcdImageProtectionPolicy) != 0) { - ProtectUefiImageCommon (LoadedImage, LoadedImageDevicePath, FALSE); + for (ImageRecordLink = mProtectedImageRecordList.ForwardLink; + ImageRecordLink != &mProtectedImageRecordList; + ImageRecordLink = ImageRecordLink->ForwardLink) { + ImageRecord = CR ( + ImageRecordLink, + IMAGE_PROPERTIES_RECORD, + Link, + IMAGE_PROPERTIES_RECORD_SIGNATURE + ); + + if (ImageRecord->ImageBase == (EFI_PHYSICAL_ADDRESS)(UINTN)LoadedImage->ImageBase) { + SetUefiImageMemoryAttributes (ImageRecord->ImageBase, + ImageRecord->ImageSize, + 0); + FreeImageRecord (ImageRecord); + return; + } + } } } @@ -817,6 +785,9 @@ InitializeDxeNxMemoryProtectionPolicy ( UINT64 Attributes; LIST_ENTRY *Link; EFI_GCD_MAP_ENTRY *Entry; + EFI_PEI_HOB_POINTERS Hob; + EFI_HOB_MEMORY_ALLOCATION *MemoryHob; + EFI_PHYSICAL_ADDRESS StackBase; // // Get the EFI memory map. @@ -848,8 +819,45 @@ InitializeDxeNxMemoryProtectionPolicy ( } while (Status == EFI_BUFFER_TOO_SMALL); ASSERT_EFI_ERROR (Status); - DEBUG((DEBUG_ERROR, "%a: applying strict permissions to active memory regions\n", - __FUNCTION__)); + StackBase = 0; + if (PcdGetBool (PcdCpuStackGuard)) { + // + // Get the base of stack from Hob. + // + Hob.Raw = GetHobList (); + while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw)) != NULL) { + MemoryHob = Hob.MemoryAllocation; + if (CompareGuid(&gEfiHobMemoryAllocStackGuid, &MemoryHob->AllocDescriptor.Name)) { + DEBUG (( + DEBUG_INFO, + "%a: StackBase = 0x%016lx StackSize = 0x%016lx\n", + __FUNCTION__, + MemoryHob->AllocDescriptor.MemoryBaseAddress, + MemoryHob->AllocDescriptor.MemoryLength + )); + + StackBase = MemoryHob->AllocDescriptor.MemoryBaseAddress; + // + // Ensure the base of the stack is page-size aligned. + // + ASSERT ((StackBase & EFI_PAGE_MASK) == 0); + break; + } + Hob.Raw = GET_NEXT_HOB (Hob); + } + + // + // Ensure the base of stack can be found from Hob when stack guard is + // enabled. + // + ASSERT (StackBase != 0); + } + + DEBUG (( + DEBUG_INFO, + "%a: applying strict permissions to active memory regions\n", + __FUNCTION__ + )); MergeMemoryMapForProtectionPolicy (MemoryMap, &MemoryMapSize, DescriptorSize); @@ -863,6 +871,37 @@ InitializeDxeNxMemoryProtectionPolicy ( MemoryMapEntry->PhysicalStart, LShiftU64 (MemoryMapEntry->NumberOfPages, EFI_PAGE_SHIFT), Attributes); + + // + // Add EFI_MEMORY_RP attribute for page 0 if NULL pointer detection is + // enabled. + // + if (MemoryMapEntry->PhysicalStart == 0 && + PcdGet8 (PcdNullPointerDetectionPropertyMask) != 0) { + + ASSERT (MemoryMapEntry->NumberOfPages > 0); + SetUefiImageMemoryAttributes ( + 0, + EFI_PAGES_TO_SIZE (1), + EFI_MEMORY_RP | Attributes); + } + + // + // Add EFI_MEMORY_RP attribute for the first page of the stack if stack + // guard is enabled. + // + if (StackBase != 0 && + (StackBase >= MemoryMapEntry->PhysicalStart && + StackBase < MemoryMapEntry->PhysicalStart + + LShiftU64 (MemoryMapEntry->NumberOfPages, EFI_PAGE_SHIFT)) && + PcdGetBool (PcdCpuStackGuard)) { + + SetUefiImageMemoryAttributes ( + StackBase, + EFI_PAGES_TO_SIZE (1), + EFI_MEMORY_RP | Attributes); + } + } MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize); } @@ -873,9 +912,11 @@ InitializeDxeNxMemoryProtectionPolicy ( // accessible, but have not been added to the UEFI memory map (yet). // if (GetPermissionAttributeForMemoryType (EfiConventionalMemory) != 0) { - DEBUG((DEBUG_ERROR, + DEBUG (( + DEBUG_INFO, "%a: applying strict permissions to inactive memory regions\n", - __FUNCTION__)); + __FUNCTION__ + )); CoreAcquireGcdMemoryLock (); @@ -944,6 +985,11 @@ MemoryProtectionCpuArchProtocolNotify ( InitializeDxeNxMemoryProtectionPolicy (); } + // + // Call notify function meant for Heap Guard. + // + HeapGuardCpuArchProtocolNotify (); + if (mImageProtectionPolicy == 0) { return; } @@ -1012,6 +1058,53 @@ MemoryProtectionExitBootServicesCallback ( } } +/** + Disable NULL pointer detection after EndOfDxe. This is a workaround resort in + order to skip unfixable NULL pointer access issues detected in OptionROM or + boot loaders. + + @param[in] Event The Event this notify function registered to. + @param[in] Context Pointer to the context data registered to the Event. +**/ +VOID +EFIAPI +DisableNullDetectionAtTheEndOfDxe ( + EFI_EVENT Event, + VOID *Context + ) +{ + EFI_STATUS Status; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR Desc; + + DEBUG ((DEBUG_INFO, "DisableNullDetectionAtTheEndOfDxe(): start\r\n")); + // + // Disable NULL pointer detection by enabling first 4K page + // + Status = CoreGetMemorySpaceDescriptor (0, &Desc); + ASSERT_EFI_ERROR (Status); + + if ((Desc.Capabilities & EFI_MEMORY_RP) == 0) { + Status = CoreSetMemorySpaceCapabilities ( + 0, + EFI_PAGE_SIZE, + Desc.Capabilities | EFI_MEMORY_RP + ); + ASSERT_EFI_ERROR (Status); + } + + Status = CoreSetMemorySpaceAttributes ( + 0, + EFI_PAGE_SIZE, + Desc.Attributes & ~EFI_MEMORY_RP + ); + ASSERT_EFI_ERROR (Status); + + CoreCloseEvent (Event); + DEBUG ((DEBUG_INFO, "DisableNullDetectionAtTheEndOfDxe(): end\r\n")); + + return; +} + /** Initialize Memory Protection support. **/ @@ -1023,10 +1116,13 @@ CoreInitializeMemoryProtection ( { EFI_STATUS Status; EFI_EVENT Event; + EFI_EVENT EndOfDxeEvent; VOID *Registration; mImageProtectionPolicy = PcdGet32(PcdImageProtectionPolicy); + InitializeListHead (&mProtectedImageRecordList); + // // Sanity check the PcdDxeNxMemoryProtectionPolicy setting: // - code regions should have no EFI_MEMORY_XP attribute @@ -1059,6 +1155,23 @@ CoreInitializeMemoryProtection ( ); ASSERT_EFI_ERROR(Status); } + + // + // Register a callback to disable NULL pointer detection at EndOfDxe + // + if ((PcdGet8 (PcdNullPointerDetectionPropertyMask) & (BIT0|BIT7)) + == (BIT0|BIT7)) { + Status = CoreCreateEventEx ( + EVT_NOTIFY_SIGNAL, + TPL_NOTIFY, + DisableNullDetectionAtTheEndOfDxe, + NULL, + &gEfiEndOfDxeEventGroupGuid, + &EndOfDxeEvent + ); + ASSERT_EFI_ERROR (Status); + } + return ; } @@ -1133,6 +1246,27 @@ ApplyMemoryProtectionPolicy ( return EFI_SUCCESS; } + // + // Don't overwrite Guard pages, which should be the first and/or last page, + // if any. + // + if (IsHeapGuardEnabled ()) { + if (IsGuardPage (Memory)) { + Memory += EFI_PAGE_SIZE; + Length -= EFI_PAGE_SIZE; + if (Length == 0) { + return EFI_SUCCESS; + } + } + + if (IsGuardPage (Memory + Length - EFI_PAGE_SIZE)) { + Length -= EFI_PAGE_SIZE; + if (Length == 0) { + return EFI_SUCCESS; + } + } + } + // // Update the executable permissions according to the DXE memory // protection policy, but only if