From nobody Sun Feb 8 12:39:07 2026 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) smtp.mailfrom=edk2-devel-bounces@lists.01.org; dmarc=fail(p=none dis=none) header.from=intel.com Return-Path: Received: from ml01.01.org (ml01.01.org [198.145.21.10]) by mx.zohomail.com with SMTPS id 1532064398862270.3466048953411; Thu, 19 Jul 2018 22:26:38 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id 97C552098EAB6; Thu, 19 Jul 2018 22:26:36 -0700 (PDT) Received: from mga14.intel.com (mga14.intel.com [192.55.52.115]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ml01.01.org (Postfix) with ESMTPS id 0A5B421BADAB3 for ; Thu, 19 Jul 2018 22:26:36 -0700 (PDT) Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga103.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Jul 2018 22:26:35 -0700 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.19]) by orsmga002.jf.intel.com with ESMTP; 19 Jul 2018 22:26:31 -0700 X-Original-To: edk2-devel@lists.01.org Received-SPF: none (zoho.com: 198.145.21.10 is neither permitted nor denied by domain of lists.01.org) client-ip=198.145.21.10; envelope-from=edk2-devel-bounces@lists.01.org; helo=ml01.01.org; Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.115; helo=mga14.intel.com; envelope-from=hao.a.wu@intel.com; receiver=edk2-devel@lists.01.org X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,377,1526367600"; d="scan'208";a="76331895" From: Hao Wu To: edk2-devel@lists.01.org Date: Fri, 20 Jul 2018 13:26:22 +0800 Message-Id: <20180720052626.24932-3-hao.a.wu@intel.com> X-Mailer: git-send-email 2.12.0.windows.1 In-Reply-To: <20180720052626.24932-1-hao.a.wu@intel.com> References: <20180720052626.24932-1-hao.a.wu@intel.com> Subject: [edk2] [PATCH 2/6] UefiCpuPkg/PiSmmCpu: Check for untested memory in GCD X-BeenThere: edk2-devel@lists.01.org X-Mailman-Version: 2.1.27 Precedence: list List-Id: EDK II Development List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Jiewen Yao , Star Zeng MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Errors-To: edk2-devel-bounces@lists.01.org Sender: "edk2-devel" X-ZohoMail: RSF_4 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" From: Jiewen Yao It treats GCD untested memory as invalid SMM communication buffer. Cc: Star Zeng Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jiewen Yao --- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 144 ++++++++++++++++-= --- 1 file changed, 120 insertions(+), 24 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 85e06b2e6a..b2ace6334e 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -13,6 +13,13 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHE= R EXPRESS OR IMPLIED. =20 #include "PiSmmCpuDxeSmm.h" =20 +// +// attributes for reserved memory before it is promoted to system memory +// +#define EFI_MEMORY_PRESENT 0x0100000000000000ULL +#define EFI_MEMORY_INITIALIZED 0x0200000000000000ULL +#define EFI_MEMORY_TESTED 0x0400000000000000ULL + #define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size))) =20 @@ -23,6 +30,9 @@ EFI_MEMORY_DESCRIPTOR *mUefiMemoryMap; UINTN mUefiMemoryMapSize; UINTN mUefiDescriptorSize; =20 +EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mGcdMemSpace =3D NULL; +UINTN mGcdMemNumberOfDesc =3D 0; + PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] =3D { {Page4K, SIZE_4KB, PAGING_4K_ADDRESS_MASK_64}, {Page2M, SIZE_2MB, PAGING_2M_ADDRESS_MASK_64}, @@ -1022,6 +1032,60 @@ MergeMemoryMapForNotPresentEntry ( return ; } =20 +/** + This function caches the GCD memory map information. +**/ +VOID +GetGcdMemoryMap ( + VOID + ) +{ + UINTN NumberOfDescriptors; + EFI_GCD_MEMORY_SPACE_DESCRIPTOR *MemSpaceMap; + EFI_STATUS Status; + UINTN Index; + + Status =3D gDS->GetMemorySpaceMap (&NumberOfDescriptors, &MemSpaceMap); + if (EFI_ERROR (Status)) { + return ; + } + + mGcdMemNumberOfDesc =3D 0; + for (Index =3D 0; Index < NumberOfDescriptors; Index++) { + if (MemSpaceMap[Index].GcdMemoryType =3D=3D EfiGcdMemoryTypeReserved && + (MemSpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMOR= Y_INITIALIZED | EFI_MEMORY_TESTED)) =3D=3D + (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED) + ) { + mGcdMemNumberOfDesc++; + } + } + + mGcdMemSpace =3D AllocateZeroPool (mGcdMemNumberOfDesc * sizeof (EFI_GCD= _MEMORY_SPACE_DESCRIPTOR)); + ASSERT (mGcdMemSpace !=3D NULL); + if (mGcdMemSpace =3D=3D NULL) { + mGcdMemNumberOfDesc =3D 0; + gBS->FreePool (MemSpaceMap); + return ; + } + + mGcdMemNumberOfDesc =3D 0; + for (Index =3D 0; Index < NumberOfDescriptors; Index++) { + if (MemSpaceMap[Index].GcdMemoryType =3D=3D EfiGcdMemoryTypeReserved && + (MemSpaceMap[Index].Capabilities & (EFI_MEMORY_PRESENT | EFI_MEMOR= Y_INITIALIZED | EFI_MEMORY_TESTED)) =3D=3D + (EFI_MEMORY_PRESENT | EFI_MEMORY_INITIALIZED) + ) { + CopyMem ( + &mGcdMemSpace[mGcdMemNumberOfDesc], + &MemSpaceMap[Index], + sizeof(EFI_GCD_MEMORY_SPACE_DESCRIPTOR) + ); + mGcdMemNumberOfDesc++; + } + } + + gBS->FreePool (MemSpaceMap); +} + /** This function caches the UEFI memory map information. **/ @@ -1081,6 +1145,11 @@ GetUefiMemoryMap ( ASSERT (mUefiMemoryMap !=3D NULL); =20 gBS->FreePool (MemoryMap); + + // + // Get additional information from GCD memory map. + // + GetGcdMemoryMap (); } =20 /** @@ -1102,33 +1171,52 @@ SetUefiMemMapAttributes ( =20 DEBUG ((DEBUG_INFO, "SetUefiMemMapAttributes\n")); =20 - if (mUefiMemoryMap =3D=3D NULL) { - DEBUG ((DEBUG_INFO, "UefiMemoryMap - NULL\n")); - return ; + if (mUefiMemoryMap !=3D NULL) { + MemoryMapEntryCount =3D mUefiMemoryMapSize/mUefiDescriptorSize; + MemoryMap =3D mUefiMemoryMap; + for (Index =3D 0; Index < MemoryMapEntryCount; Index++) { + if (IsUefiPageNotPresent(MemoryMap)) { + Status =3D SmmSetMemoryAttributes ( + MemoryMap->PhysicalStart, + EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages), + EFI_MEMORY_RP + ); + DEBUG (( + DEBUG_INFO, + "UefiMemory protection: 0x%lx - 0x%lx %r\n", + MemoryMap->PhysicalStart, + MemoryMap->PhysicalStart + (UINT64)EFI_PAGES_TO_SIZE((UINTN)Memo= ryMap->NumberOfPages), + Status + )); + } + MemoryMap =3D NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize); + } } + // + // Do not free mUefiMemoryMap, it will be checked in IsSmmCommBufferForb= iddenAddress(). + // =20 - MemoryMapEntryCount =3D mUefiMemoryMapSize/mUefiDescriptorSize; - MemoryMap =3D mUefiMemoryMap; - for (Index =3D 0; Index < MemoryMapEntryCount; Index++) { - if (IsUefiPageNotPresent(MemoryMap)) { + // + // Set untested memory as not present. + // + if (mGcdMemSpace !=3D NULL) { + for (Index =3D 0; Index < mGcdMemNumberOfDesc; Index++) { Status =3D SmmSetMemoryAttributes ( - MemoryMap->PhysicalStart, - EFI_PAGES_TO_SIZE((UINTN)MemoryMap->NumberOfPages), + mGcdMemSpace[Index].BaseAddress, + mGcdMemSpace[Index].Length, EFI_MEMORY_RP ); DEBUG (( DEBUG_INFO, - "UefiMemory protection: 0x%lx - 0x%lx %r\n", - MemoryMap->PhysicalStart, - MemoryMap->PhysicalStart + (UINT64)EFI_PAGES_TO_SIZE((UINTN)Memory= Map->NumberOfPages), + "GcdMemory protection: 0x%lx - 0x%lx %r\n", + mGcdMemSpace[Index].BaseAddress, + mGcdMemSpace[Index].BaseAddress + mGcdMemSpace[Index].Length, Status )); } - MemoryMap =3D NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize); } - // - // Do free mUefiMemoryMap, it will be checked in IsSmmCommBufferForbidde= nAddress(). + // Do not free mGcdMemSpace, it will be checked in IsSmmCommBufferForbid= denAddress(). // } =20 @@ -1149,21 +1237,29 @@ IsSmmCommBufferForbiddenAddress ( UINTN MemoryMapEntryCount; UINTN Index; =20 - if (mUefiMemoryMap =3D=3D NULL) { - return FALSE; + if (mUefiMemoryMap !=3D NULL) { + MemoryMap =3D mUefiMemoryMap; + MemoryMapEntryCount =3D mUefiMemoryMapSize/mUefiDescriptorSize; + for (Index =3D 0; Index < MemoryMapEntryCount; Index++) { + if (IsUefiPageNotPresent (MemoryMap)) { + if ((Address >=3D MemoryMap->PhysicalStart) && + (Address < MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE((UINTN= )MemoryMap->NumberOfPages)) ) { + return TRUE; + } + } + MemoryMap =3D NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize); + } } =20 - MemoryMap =3D mUefiMemoryMap; - MemoryMapEntryCount =3D mUefiMemoryMapSize/mUefiDescriptorSize; - for (Index =3D 0; Index < MemoryMapEntryCount; Index++) { - if (IsUefiPageNotPresent (MemoryMap)) { - if ((Address >=3D MemoryMap->PhysicalStart) && - (Address < MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE((UINTN)M= emoryMap->NumberOfPages)) ) { + if (mGcdMemSpace !=3D NULL) { + for (Index =3D 0; Index < mGcdMemNumberOfDesc; Index++) { + if ((Address >=3D mGcdMemSpace[Index].BaseAddress) && + (Address < mGcdMemSpace[Index].BaseAddress + mGcdMemSpace[Index]= .Length) ) { return TRUE; } } - MemoryMap =3D NEXT_MEMORY_DESCRIPTOR(MemoryMap, mUefiDescriptorSize); } + return FALSE; } =20 --=20 2.16.2.windows.1 _______________________________________________ edk2-devel mailing list edk2-devel@lists.01.org https://lists.01.org/mailman/listinfo/edk2-devel