public inbox for devel@edk2.groups.io
 help / color / mirror / Atom feed
* [PATCH V3] MdeModulePkg: Memory Corruption Error in CapsuleRuntimeDxe
@ 2022-10-22  1:33 Nate DeSimone
  2022-10-25 22:02 ` [edk2-devel] " Michael Kubacki
  0 siblings, 1 reply; 3+ messages in thread
From: Nate DeSimone @ 2022-10-22  1:33 UTC (permalink / raw)
  To: devel; +Cc: Liming Gao, Guomin Jiang, Jian J Wang, Michael D Kinney

REF: https://bugzilla.tianocore.org/show_bug.cgi?id=4112

In AllocateReservedMemoryBelow4G(), if gBS->AllocatePages()
returns an error, and ASSERTs are disabled, then the
function will overwrite memory from 0xFFFFFFFF -> (0xFFFFFFFF + Size).

Cc: Liming Gao <gaoliming@byosoft.com.cn>
Cc: Guomin Jiang <guomin.jiang@intel.com>
Cc: Jian J Wang <jian.j.wang@intel.com>
Cc: Michael D Kinney <michael.d.kinney@intel.com>
Signed-off-by: Nate DeSimone <nathaniel.l.desimone@intel.com>
---
 .../X64/SaveLongModeContext.c                 | 24 ++++++++++++++++---
 1 file changed, 21 insertions(+), 3 deletions(-)

diff --git a/MdeModulePkg/Universal/CapsuleRuntimeDxe/X64/SaveLongModeContext.c b/MdeModulePkg/Universal/CapsuleRuntimeDxe/X64/SaveLongModeContext.c
index dab297dd0a..a1bac730bf 100644
--- a/MdeModulePkg/Universal/CapsuleRuntimeDxe/X64/SaveLongModeContext.c
+++ b/MdeModulePkg/Universal/CapsuleRuntimeDxe/X64/SaveLongModeContext.c
@@ -59,7 +59,15 @@ AllocateReservedMemoryBelow4G (
                   Pages,
                   &Address
                   );
-  ASSERT_EFI_ERROR (Status);
+  if (EFI_ERROR (Status)) {
+    DEBUG ((DEBUG_ERROR, "ERROR AllocateReservedMemoryBelow4G(): %r\n", Status));
+    return NULL;
+  }
+
+  if (Address == 0) {
+    DEBUG ((DEBUG_ERROR, "ERROR AllocateReservedMemoryBelow4G(): AllocatePages() returned NULL"));
+    return NULL;
+  }
 
   Buffer = (VOID *)(UINTN)Address;
   ZeroMem (Buffer, Size);
@@ -159,14 +167,23 @@ PrepareContextForCapsulePei (
   DEBUG ((DEBUG_INFO, "CapsuleRuntimeDxe X64 TotalPagesNum - 0x%x pages\n", TotalPagesNum));
 
   LongModeBuffer.PageTableAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocateReservedMemoryBelow4G (EFI_PAGES_TO_SIZE (TotalPagesNum));
-  ASSERT (LongModeBuffer.PageTableAddress != 0);
+  if (LongModeBuffer.PageTableAddress == 0) {
+    DEBUG ((DEBUG_ERROR, "FATAL ERROR: CapsuleLongModeBuffer cannot be saved, "));
+    DEBUG ((DEBUG_ERROR, "PageTableAddress allocation failed. Capsule in PEI may fail!\n"));
+    return;
+  }
 
   //
   // Allocate stack
   //
   LongModeBuffer.StackSize        = PcdGet32 (PcdCapsulePeiLongModeStackSize);
   LongModeBuffer.StackBaseAddress = (EFI_PHYSICAL_ADDRESS)(UINTN)AllocateReservedMemoryBelow4G (PcdGet32 (PcdCapsulePeiLongModeStackSize));
-  ASSERT (LongModeBuffer.StackBaseAddress != 0);
+  if (LongModeBuffer.StackBaseAddress == 0) {
+    DEBUG ((DEBUG_ERROR, "FATAL ERROR: CapsuleLongModeBuffer cannot be saved, "));
+    DEBUG ((DEBUG_ERROR, "StackBaseAddress allocation failed. Capsule in PEI may fail!\n"));
+    gBS->FreePages (LongModeBuffer.PageTableAddress, TotalPagesNum);
+    return;
+  }
 
   Status = gRT->SetVariable (
                   EFI_CAPSULE_LONG_MODE_BUFFER_NAME,
@@ -189,6 +206,7 @@ PrepareContextForCapsulePei (
       );
   } else {
     DEBUG ((DEBUG_ERROR, "FATAL ERROR: CapsuleLongModeBuffer cannot be saved: %r. Capsule in PEI may fail!\n", Status));
+    gBS->FreePages (LongModeBuffer.PageTableAddress, TotalPagesNum);
     gBS->FreePages (LongModeBuffer.StackBaseAddress, EFI_SIZE_TO_PAGES (LongModeBuffer.StackSize));
   }
 }
-- 
2.27.0.windows.1


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: [edk2-devel] [PATCH V3] MdeModulePkg: Memory Corruption Error in CapsuleRuntimeDxe
  2022-10-22  1:33 [PATCH V3] MdeModulePkg: Memory Corruption Error in CapsuleRuntimeDxe Nate DeSimone
@ 2022-10-25 22:02 ` Michael Kubacki
  2022-10-25 22:32   ` Nate DeSimone
  0 siblings, 1 reply; 3+ messages in thread
From: Michael Kubacki @ 2022-10-25 22:02 UTC (permalink / raw)
  To: Nate DeSimone, devel

[-- Attachment #1: Type: text/plain, Size: 230 bytes --]

It would be nice to update @return for AllocateReservedMemoryBelow4G() to indicate NULL should be expected if an internal memory allocation fails.

Other than that:
Reviewed-by: Michael Kubacki <michael.kubacki@microsoft.com>

[-- Attachment #2: Type: text/html, Size: 254 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [edk2-devel] [PATCH V3] MdeModulePkg: Memory Corruption Error in CapsuleRuntimeDxe
  2022-10-25 22:02 ` [edk2-devel] " Michael Kubacki
@ 2022-10-25 22:32   ` Nate DeSimone
  0 siblings, 0 replies; 3+ messages in thread
From: Nate DeSimone @ 2022-10-25 22:32 UTC (permalink / raw)
  To: Michael Kubacki, devel@edk2.groups.io

Thanks for the review Michael, I've sent a V4 patch with the added @return 

> From: Michael Kubacki <mikuback@linux.microsoft.com> 
> Sent: Tuesday, October 25, 2022 3:03 PM
> To: Desimone, Nathaniel L <nathaniel.l.desimone@intel.com>; devel@edk2.groups.io
> Subject: Re: [edk2-devel] [PATCH V3] MdeModulePkg: Memory Corruption Error in CapsuleRuntimeDxe
> 
> It would be nice to update @return for AllocateReservedMemoryBelow4G() to indicate NULL should be expected if an internal memory allocation fails.
> 
> Other than that:
> Reviewed-by: Michael Kubacki <mailto:michael.kubacki@microsoft.com>

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2022-10-25 22:32 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2022-10-22  1:33 [PATCH V3] MdeModulePkg: Memory Corruption Error in CapsuleRuntimeDxe Nate DeSimone
2022-10-25 22:02 ` [edk2-devel] " Michael Kubacki
2022-10-25 22:32   ` Nate DeSimone

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox