]> git.proxmox.com Git - mirror_edk2.git/blobdiff - MdeModulePkg/Core/PiSmmCore/HeapGuard.c
MdeModulePkg/PiSmmCore: fix #PF caused by freeing read-only memory
[mirror_edk2.git] / MdeModulePkg / Core / PiSmmCore / HeapGuard.c
index f9657f9baa529c5b03c29106546dfdc66db2d6df..d5556eb79cb1cfd73ba1569fe8c9e2ee02708cbb 100644 (file)
@@ -941,6 +941,7 @@ AdjustMemoryF (
   EFI_PHYSICAL_ADDRESS  MemoryToTest;\r
   UINTN                 PagesToFree;\r
   UINT64                GuardBitmap;\r
+  UINT64                Attributes;\r
 \r
   if (Memory == NULL || NumberOfPages == NULL || *NumberOfPages == 0) {\r
     return;\r
@@ -949,6 +950,27 @@ AdjustMemoryF (
   Start = *Memory;\r
   PagesToFree = *NumberOfPages;\r
 \r
+  //\r
+  // In case the memory to free is marked as read-only (e.g. EfiRuntimeServicesCode).\r
+  //\r
+  if (mSmmMemoryAttribute != NULL) {\r
+    Attributes = 0;\r
+    mSmmMemoryAttribute->GetMemoryAttributes (\r
+                           mSmmMemoryAttribute,\r
+                           Start,\r
+                           EFI_PAGES_TO_SIZE (PagesToFree),\r
+                           &Attributes\r
+                           );\r
+    if ((Attributes & EFI_MEMORY_RO) != 0) {\r
+      mSmmMemoryAttribute->ClearMemoryAttributes (\r
+                             mSmmMemoryAttribute,\r
+                             Start,\r
+                             EFI_PAGES_TO_SIZE (PagesToFree),\r
+                             EFI_MEMORY_RO\r
+                             );\r
+    }\r
+  }\r
+\r
   //\r
   // Head Guard must be one page before, if any.\r
   //\r