]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/Dxe/Misc/MemoryProtection.c
MdeModulePkg/Core: Fix feature conflict between NX and Stack guard
[mirror_edk2.git] / MdeModulePkg / Core / Dxe / Misc / MemoryProtection.c
index a2ea445eefa6046084d1f1bbed2a9ee757c9f8ac..407aece80786715e7265fc0ff53ea8d3126f8829 100644 (file)
@@ -801,6 +801,9 @@ InitializeDxeNxMemoryProtectionPolicy (
   UINT64                            Attributes;\r
   LIST_ENTRY                        *Link;\r
   EFI_GCD_MAP_ENTRY                 *Entry;\r
+  EFI_PEI_HOB_POINTERS              Hob;\r
+  EFI_HOB_MEMORY_ALLOCATION         *MemoryHob;\r
+  EFI_PHYSICAL_ADDRESS              StackBase;\r
 \r
   //\r
   // Get the EFI memory map.\r
@@ -832,6 +835,40 @@ InitializeDxeNxMemoryProtectionPolicy (
   } while (Status == EFI_BUFFER_TOO_SMALL);\r
   ASSERT_EFI_ERROR (Status);\r
 \r
+  StackBase = 0;\r
+  if (PcdGetBool (PcdCpuStackGuard)) {\r
+    //\r
+    // Get the base of stack from Hob.\r
+    //\r
+    Hob.Raw = GetHobList ();\r
+    while ((Hob.Raw = GetNextHob (EFI_HOB_TYPE_MEMORY_ALLOCATION, Hob.Raw)) != NULL) {\r
+      MemoryHob = Hob.MemoryAllocation;\r
+      if (CompareGuid(&gEfiHobMemoryAllocStackGuid, &MemoryHob->AllocDescriptor.Name)) {\r
+        DEBUG ((\r
+          DEBUG_INFO,\r
+          "%a: StackBase = 0x%016lx  StackSize = 0x%016lx\n",\r
+          __FUNCTION__,\r
+          MemoryHob->AllocDescriptor.MemoryBaseAddress,\r
+          MemoryHob->AllocDescriptor.MemoryLength\r
+          ));\r
+\r
+        StackBase = MemoryHob->AllocDescriptor.MemoryBaseAddress;\r
+        //\r
+        // Ensure the base of the stack is page-size aligned.\r
+        //\r
+        ASSERT ((StackBase & EFI_PAGE_MASK) == 0);\r
+        break;\r
+      }\r
+      Hob.Raw = GET_NEXT_HOB (Hob);\r
+    }\r
+\r
+    //\r
+    // Ensure the base of stack can be found from Hob when stack guard is\r
+    // enabled.\r
+    //\r
+    ASSERT (StackBase != 0);\r
+  }\r
+\r
   DEBUG ((\r
     DEBUG_INFO,\r
     "%a: applying strict permissions to active memory regions\n",\r
@@ -851,19 +888,36 @@ InitializeDxeNxMemoryProtectionPolicy (
         LShiftU64 (MemoryMapEntry->NumberOfPages, EFI_PAGE_SHIFT),\r
         Attributes);\r
 \r
+      //\r
+      // Add EFI_MEMORY_RP attribute for page 0 if NULL pointer detection is\r
+      // enabled.\r
+      //\r
       if (MemoryMapEntry->PhysicalStart == 0 &&\r
           PcdGet8 (PcdNullPointerDetectionPropertyMask) != 0) {\r
 \r
         ASSERT (MemoryMapEntry->NumberOfPages > 0);\r
-        //\r
-        // Add EFI_MEMORY_RP attribute for page 0 if NULL pointer detection is\r
-        // enabled.\r
-        //\r
         SetUefiImageMemoryAttributes (\r
           0,\r
           EFI_PAGES_TO_SIZE (1),\r
           EFI_MEMORY_RP | Attributes);\r
       }\r
+\r
+      //\r
+      // Add EFI_MEMORY_RP attribute for the first page of the stack if stack\r
+      // guard is enabled.\r
+      //\r
+      if (StackBase != 0 &&\r
+          (StackBase >= MemoryMapEntry->PhysicalStart &&\r
+           StackBase <  MemoryMapEntry->PhysicalStart +\r
+                        LShiftU64 (MemoryMapEntry->NumberOfPages, EFI_PAGE_SHIFT)) &&\r
+          PcdGetBool (PcdCpuStackGuard)) {\r
+\r
+        SetUefiImageMemoryAttributes (\r
+          StackBase,\r
+          EFI_PAGES_TO_SIZE (1),\r
+          EFI_MEMORY_RP | Attributes);\r
+      }\r
+\r
     }\r
     MemoryMapEntry = NEXT_MEMORY_DESCRIPTOR (MemoryMapEntry, DescriptorSize);\r
   }\r