From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga06.intel.com (mga06.intel.com [134.134.136.31]) by mx.groups.io with SMTP id smtpd.web09.2334.1666402435957930030 for ; Fri, 21 Oct 2022 18:33:56 -0700 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=Qt5mBdYz; spf=pass (domain: intel.com, ip: 134.134.136.31, mailfrom: nathaniel.l.desimone@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1666402436; x=1697938436; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=AW8YTBFfwqq+C9dum8/uT92/TaoP+5PhpKJXkVUcZIc=; b=Qt5mBdYzcSAt3MVbTM6cLkTmFtia/8+yUH8dtzfKXI1F4S26sMg985GW 2t7TYuYRE7aaz/RMss9n0j+YcgTbF35rlf5wnm5JdB/KekCYjzbnTqbEt Ug4QT29Wqks9Ed6/9lt5KAJS4kpVL2o4NCnc49oM3g9g7CY+sA8bxpL3S NBqpu++Au5Kk3juIja3pxSBcu8GWUyLDEo4kWn6cDdmmtGp3iYjfs/5rI f79vu6XXxbmhl2C5/AJMfi5tUCjDlPmdfJ1+bDsmVm5j88AJoRvMgcKDa MrSCUcEt/WUzt33PjqeAR8nWscmreCjzc9HrgIaGvxwCqI/GYNivh1Mlz w==; X-IronPort-AV: E=McAfee;i="6500,9779,10507"; a="369211321" X-IronPort-AV: E=Sophos;i="5.95,203,1661842800"; d="scan'208";a="369211321" Received: from orsmga007.jf.intel.com ([10.7.209.58]) by orsmga104.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Oct 2022 18:33:55 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10507"; a="625547368" X-IronPort-AV: E=Sophos;i="5.95,203,1661842800"; d="scan'208";a="625547368" Received: from nldesimo-desk1.amr.corp.intel.com ([10.24.80.62]) by orsmga007-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 21 Oct 2022 18:33:55 -0700 From: "Nate DeSimone" To: devel@edk2.groups.io Cc: Liming Gao , Guomin Jiang , Jian J Wang , Michael D Kinney Subject: [PATCH V3] MdeModulePkg: Memory Corruption Error in CapsuleRuntimeDxe Date: Fri, 21 Oct 2022 18:33:30 -0700 Message-Id: <20221022013330.2308-1-nathaniel.l.desimone@intel.com> X-Mailer: git-send-email 2.27.0.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 Cc: Guomin Jiang Cc: Jian J Wang Cc: Michael D Kinney Signed-off-by: Nate DeSimone --- .../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