From nobody Sun Feb 8 17:21:35 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 1532064404714942.4217952939655; Thu, 19 Jul 2018 22:26:44 -0700 (PDT) Received: from [127.0.0.1] (localhost [IPv6:::1]) by ml01.01.org (Postfix) with ESMTP id ECBC32098EABE; Thu, 19 Jul 2018 22:26:37 -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 610EE2098EAB3 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:36 -0700 Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.19]) by orsmga002.jf.intel.com with ESMTP; 19 Jul 2018 22:26:35 -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="76331912" From: Hao Wu To: edk2-devel@lists.01.org Date: Fri, 20 Jul 2018 13:26:25 +0800 Message-Id: <20180720052626.24932-6-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 5/6] UefiCpuPkg/PiSmmCpu: Check EFI_RUNTIME_RO in UEFI mem attrib table. 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 the UEFI runtime page with EFI_MEMORY_RO attribute as invalid SMM communication buffer. Cc: Star Zeng Contributed-under: TianoCore Contribution Agreement 1.1 Signed-off-by: Jiewen Yao --- UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h | 2 + UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf | 1 + UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 75 ++++++++++++++++++= +- 3 files changed, 75 insertions(+), 3 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h b/UefiCpuPkg/PiSmmC= puDxeSmm/PiSmmCpuDxeSmm.h index 1d016594e0..e3c7cff81c 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.h @@ -28,6 +28,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER= EXPRESS OR IMPLIED. #include =20 #include +#include #include =20 #include @@ -45,6 +46,7 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER= EXPRESS OR IMPLIED. #include #include #include +#include #include #include #include diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf b/UefiCpuPkg/PiSm= mCpuDxeSmm/PiSmmCpuDxeSmm.inf index 52d8c55075..a7fb7b0b14 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/PiSmmCpuDxeSmm.inf @@ -117,6 +117,7 @@ gEfiAcpi20TableGuid ## SOMETIMES_CONSUMES ## System= Table gEfiAcpi10TableGuid ## SOMETIMES_CONSUMES ## System= Table gEdkiiPiSmmMemoryAttributesTableGuid ## CONSUMES ## SystemTable + gEfiMemoryAttributesTableGuid ## CONSUMES ## SystemTable =20 [FeaturePcd] gUefiCpuPkgTokenSpaceGuid.PcdCpuSmmDebug ## CONS= UMES diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index b2ace6334e..7d99f23426 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -20,9 +20,6 @@ WITHOUT WARRANTIES OR REPRESENTATIONS OF ANY KIND, EITHER= EXPRESS OR IMPLIED. #define EFI_MEMORY_INITIALIZED 0x0200000000000000ULL #define EFI_MEMORY_TESTED 0x0400000000000000ULL =20 -#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ - ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size))) - #define PREVIOUS_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) - (Size))) =20 @@ -33,6 +30,8 @@ UINTN mUefiDescriptorSize; EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mGcdMemSpace =3D NULL; UINTN mGcdMemNumberOfDesc =3D 0; =20 +EFI_MEMORY_ATTRIBUTES_TABLE *mUefiMemoryAttributesTable =3D NULL; + PAGE_ATTRIBUTE_TABLE mPageAttributeTable[] =3D { {Page4K, SIZE_4KB, PAGING_4K_ADDRESS_MASK_64}, {Page2M, SIZE_2MB, PAGING_2M_ADDRESS_MASK_64}, @@ -1086,6 +1085,26 @@ GetGcdMemoryMap ( gBS->FreePool (MemSpaceMap); } =20 +/** + Get UEFI MemoryAttributesTable. +**/ +VOID +GetUefiMemoryAttributesTable ( + VOID + ) +{ + EFI_STATUS Status; + EFI_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable; + UINTN MemoryAttributesTableSize; + + Status =3D EfiGetSystemConfigurationTable (&gEfiMemoryAttributesTableGui= d, (VOID **)&MemoryAttributesTable); + if (!EFI_ERROR (Status)) { + MemoryAttributesTableSize =3D sizeof(EFI_MEMORY_ATTRIBUTES_TABLE) + Me= moryAttributesTable->DescriptorSize * MemoryAttributesTable->NumberOfEntrie= s; + mUefiMemoryAttributesTable =3D AllocateCopyPool (MemoryAttributesTable= Size, MemoryAttributesTable); + ASSERT (mUefiMemoryAttributesTable !=3D NULL); + } +} + /** This function caches the UEFI memory map information. **/ @@ -1150,6 +1169,11 @@ GetUefiMemoryMap ( // Get additional information from GCD memory map. // GetGcdMemoryMap (); + + // + // Get UEFI memory attributes table. + // + GetUefiMemoryAttributesTable (); } =20 /** @@ -1168,6 +1192,7 @@ SetUefiMemMapAttributes ( EFI_MEMORY_DESCRIPTOR *MemoryMap; UINTN MemoryMapEntryCount; UINTN Index; + EFI_MEMORY_DESCRIPTOR *Entry; =20 DEBUG ((DEBUG_INFO, "SetUefiMemMapAttributes\n")); =20 @@ -1218,6 +1243,35 @@ SetUefiMemMapAttributes ( // // Do not free mGcdMemSpace, it will be checked in IsSmmCommBufferForbid= denAddress(). // + + // + // Set UEFI runtime memory with EFI_MEMORY_RO as not present. + // + if (mUefiMemoryAttributesTable !=3D NULL) { + Entry =3D (EFI_MEMORY_DESCRIPTOR *)(mUefiMemoryAttributesTable + 1); + for (Index =3D 0; Index < mUefiMemoryAttributesTable->NumberOfEntries;= Index++) { + if (Entry->Type =3D=3D EfiRuntimeServicesCode || Entry->Type =3D=3D = EfiRuntimeServicesData) { + if ((Entry->Attribute & EFI_MEMORY_RO) !=3D 0) { + Status =3D SmmSetMemoryAttributes ( + Entry->PhysicalStart, + EFI_PAGES_TO_SIZE((UINTN)Entry->NumberOfPages), + EFI_MEMORY_RP + ); + DEBUG (( + DEBUG_INFO, + "UefiMemoryAttribute protection: 0x%lx - 0x%lx %r\n", + Entry->PhysicalStart, + Entry->PhysicalStart + (UINT64)EFI_PAGES_TO_SIZE((UINTN)Entry-= >NumberOfPages), + Status + )); + } + } + Entry =3D NEXT_MEMORY_DESCRIPTOR (Entry, mUefiMemoryAttributesTable-= >DescriptorSize); + } + } + // + // Do not free mUefiMemoryAttributesTable, it will be checked in IsSmmCo= mmBufferForbiddenAddress(). + // } =20 /** @@ -1236,6 +1290,7 @@ IsSmmCommBufferForbiddenAddress ( EFI_MEMORY_DESCRIPTOR *MemoryMap; UINTN MemoryMapEntryCount; UINTN Index; + EFI_MEMORY_DESCRIPTOR *Entry; =20 if (mUefiMemoryMap !=3D NULL) { MemoryMap =3D mUefiMemoryMap; @@ -1260,6 +1315,20 @@ IsSmmCommBufferForbiddenAddress ( } } =20 + if (mUefiMemoryAttributesTable !=3D NULL) { + Entry =3D (EFI_MEMORY_DESCRIPTOR *)(mUefiMemoryAttributesTable + 1); + for (Index =3D 0; Index < mUefiMemoryAttributesTable->NumberOfEntries;= Index++) { + if (Entry->Type =3D=3D EfiRuntimeServicesCode || Entry->Type =3D=3D = EfiRuntimeServicesData) { + if ((Entry->Attribute & EFI_MEMORY_RO) !=3D 0) { + if ((Address >=3D Entry->PhysicalStart) && + (Address < Entry->PhysicalStart + LShiftU64 (Entry->NumberOf= Pages, EFI_PAGE_SHIFT))) { + return TRUE; + } + Entry =3D NEXT_MEMORY_DESCRIPTOR (Entry, mUefiMemoryAttributesTa= ble->DescriptorSize); + } + } + } + } 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