From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received-SPF: Pass (sender SPF authorized) identity=mailfrom; client-ip=192.55.52.88; helo=mga01.intel.com; envelope-from=hao.a.wu@intel.com; receiver=edk2-devel@lists.01.org Received: from mga01.intel.com (mga01.intel.com [192.55.52.88]) (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 AC2F32098EAB3 for ; Thu, 19 Jul 2018 22:26:36 -0700 (PDT) X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga002.jf.intel.com ([10.7.209.21]) by fmsmga101.fm.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 19 Jul 2018 22:26:36 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.51,377,1526367600"; d="scan'208";a="76331904" Received: from shwdeopenpsi014.ccr.corp.intel.com ([10.239.9.19]) by orsmga002.jf.intel.com with ESMTP; 19 Jul 2018 22:26:33 -0700 From: Hao Wu To: edk2-devel@lists.01.org Cc: Jiewen Yao , Star Zeng Date: Fri, 20 Jul 2018 13:26:24 +0800 Message-Id: <20180720052626.24932-5-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: [PATCH 4/6] MdePkg/SmmMemLib: Check EFI_MEMORY_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: , X-List-Received-Date: Fri, 20 Jul 2018 05:26:36 -0000 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 --- MdePkg/Library/SmmMemLib/SmmMemLib.c | 58 +++++++++++++++++++- MdePkg/Library/SmmMemLib/SmmMemLib.inf | 4 ++ 2 files changed, 59 insertions(+), 3 deletions(-) diff --git a/MdePkg/Library/SmmMemLib/SmmMemLib.c b/MdePkg/Library/SmmMemLib/SmmMemLib.c index 3f79e46d46..3409ddf87c 100644 --- a/MdePkg/Library/SmmMemLib/SmmMemLib.c +++ b/MdePkg/Library/SmmMemLib/SmmMemLib.c @@ -27,10 +27,12 @@ #include #include #include +#include #include #include #include #include +#include // // attributes for reserved memory before it is promoted to system memory @@ -39,9 +41,6 @@ #define EFI_MEMORY_INITIALIZED 0x0200000000000000ULL #define EFI_MEMORY_TESTED 0x0400000000000000ULL -#define NEXT_MEMORY_DESCRIPTOR(MemoryDescriptor, Size) \ - ((EFI_MEMORY_DESCRIPTOR *)((UINT8 *)(MemoryDescriptor) + (Size))) - EFI_SMRAM_DESCRIPTOR *mSmmMemLibInternalSmramRanges; UINTN mSmmMemLibInternalSmramCount; @@ -57,6 +56,8 @@ UINTN mDescriptorSize; EFI_GCD_MEMORY_SPACE_DESCRIPTOR *mSmmMemLibGcdMemSpace = NULL; UINTN mSmmMemLibGcdMemNumberOfDesc = 0; +EFI_MEMORY_ATTRIBUTES_TABLE *mSmmMemLibMemoryAttributesTable = NULL; + VOID *mRegistrationEndOfDxe; VOID *mRegistrationReadyToLock; @@ -204,6 +205,32 @@ SmmIsBufferOutsideSmmValid ( return FALSE; } } + + // + // Check UEFI runtime memory with EFI_MEMORY_RO as invalid communication buffer. + // + if (mSmmMemLibMemoryAttributesTable != NULL) { + EFI_MEMORY_DESCRIPTOR *Entry; + + Entry = (EFI_MEMORY_DESCRIPTOR *)(mSmmMemLibMemoryAttributesTable + 1); + for (Index = 0; Index < mSmmMemLibMemoryAttributesTable->NumberOfEntries; Index++) { + if (Entry->Type == EfiRuntimeServicesCode || Entry->Type == EfiRuntimeServicesData) { + if ((Entry->Attribute & EFI_MEMORY_RO) != 0) { + if (((Buffer >= Entry->PhysicalStart) && (Buffer < Entry->PhysicalStart + LShiftU64 (Entry->NumberOfPages, EFI_PAGE_SHIFT))) || + ((Entry->PhysicalStart >= Buffer) && (Entry->PhysicalStart < Buffer + Length))) { + DEBUG (( + EFI_D_ERROR, + "SmmIsBufferOutsideSmmValid: In RuntimeCode Region: Buffer (0x%lx) - Length (0x%lx)\n", + Buffer, + Length + )); + return FALSE; + } + } + } + Entry = NEXT_MEMORY_DESCRIPTOR (Entry, mSmmMemLibMemoryAttributesTable->DescriptorSize); + } + } } return TRUE; } @@ -399,6 +426,26 @@ SmmMemLibInternalGetGcdMemoryMap ( gBS->FreePool (MemSpaceMap); } +/** + Get UEFI MemoryAttributesTable. +**/ +VOID +SmmMemLibInternalGetUefiMemoryAttributesTable ( + VOID + ) +{ + EFI_STATUS Status; + EFI_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable; + UINTN MemoryAttributesTableSize; + + Status = EfiGetSystemConfigurationTable (&gEfiMemoryAttributesTableGuid, (VOID **)&MemoryAttributesTable); + if (!EFI_ERROR (Status)) { + MemoryAttributesTableSize = sizeof(EFI_MEMORY_ATTRIBUTES_TABLE) + MemoryAttributesTable->DescriptorSize * MemoryAttributesTable->NumberOfEntries; + mSmmMemLibMemoryAttributesTable = AllocateCopyPool (MemoryAttributesTableSize, MemoryAttributesTable); + ASSERT (mSmmMemLibMemoryAttributesTable != NULL); + } +} + /** Notification for SMM EndOfDxe protocol. @@ -502,6 +549,11 @@ SmmLibInternalEndOfDxeNotify ( // SmmMemLibInternalGetGcdMemoryMap (); + // + // Get UEFI memory attributes table. + // + SmmMemLibInternalGetUefiMemoryAttributesTable (); + return EFI_SUCCESS; } diff --git a/MdePkg/Library/SmmMemLib/SmmMemLib.inf b/MdePkg/Library/SmmMemLib/SmmMemLib.inf index 36576a4f2f..525449c25c 100644 --- a/MdePkg/Library/SmmMemLib/SmmMemLib.inf +++ b/MdePkg/Library/SmmMemLib/SmmMemLib.inf @@ -48,11 +48,15 @@ BaseMemoryLib HobLib MemoryAllocationLib + UefiLib [Protocols] gEfiSmmAccess2ProtocolGuid ## CONSUMES gEfiSmmReadyToLockProtocolGuid ## CONSUMES gEfiSmmEndOfDxeProtocolGuid ## CONSUMES +[Guids] + gEfiMemoryAttributesTableGuid ## CONSUMES ## SystemTable + [Depex] gEfiSmmAccess2ProtocolGuid -- 2.16.2.windows.1