From nobody Fri Oct 18 05:23:44 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) client-ip=66.175.222.108; envelope-from=bounce+27952+104926+1787277+3901457@groups.io; helo=mail02.groups.io; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+104926+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1684231227; cv=none; d=zohomail.com; s=zohoarc; b=daepvLi4h54Cuh8BGicZzWX6yzj9wB3SqQ3FOcH1zcGzSpOzESOKxQHxRiWmLL12EFvpryRp2OT67zXzese/Ua+oJ5yoybcO2S/+94Ebq4dnQzJA1krEt6rEkOlgk1HAQi9A2k2LMwW5oF71+lSU0TwBiwOaoVEAOhOhyFbVA5s= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1684231227; h=Content-Transfer-Encoding:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=x7lFR78zlc84xT7WCHtkWIMN8pcZTCN+MXxyA9JLIFE=; b=CoJ+bJagLPfJsiwGYb8uUJQ8u4qYPjpSVqgCkeya4AnTV1gzCJZ77YY1hJ47SIIGLhIfyJgORZFBUxt3LOuGndKyVAEQebKa1A5ZRwRdkip2s2u4W3Y+Znv9eUU8NwMIQxeCe6wO78hQphm9iPL7ZXKgxQx8vBQZV0dIu8da4BA= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce+27952+104926+1787277+3901457@groups.io; dmarc=fail header.from= (p=none dis=none) Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1684231227085724.3980917797802; Tue, 16 May 2023 03:00:27 -0700 (PDT) Return-Path: X-Received: by 127.0.0.2 with SMTP id krucYY1788612xlL5O92zPDU; Tue, 16 May 2023 03:00:26 -0700 X-Received: from mga07.intel.com (mga07.intel.com [134.134.136.100]) by mx.groups.io with SMTP id smtpd.web11.17915.1684231210780691710 for ; Tue, 16 May 2023 03:00:26 -0700 X-IronPort-AV: E=McAfee;i="6600,9927,10711"; a="417093243" X-IronPort-AV: E=Sophos;i="5.99,278,1677571200"; d="scan'208";a="417093243" X-Received: from fmsmga003.fm.intel.com ([10.253.24.29]) by orsmga105.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 May 2023 03:00:26 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10711"; a="791019545" X-IronPort-AV: E=Sophos;i="5.99,278,1677571200"; d="scan'208";a="791019545" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.158]) by fmsmga003-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 16 May 2023 03:00:24 -0700 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [Patch V4 06/15] UefiCpuPkg/PiSmmCpuDxeSmm: Avoid setting non-present range to RO/NX Date: Tue, 16 May 2023 17:59:23 +0800 Message-Id: <20230516095932.1525-7-dun.tan@intel.com> In-Reply-To: <20230516095932.1525-1-dun.tan@intel.com> References: <20230516095932.1525-1-dun.tan@intel.com> MIME-Version: 1.0 Precedence: Bulk List-Unsubscribe: List-Subscribe: List-Help: Sender: devel@edk2.groups.io List-Id: Mailing-List: list devel@edk2.groups.io; contact devel+owner@edk2.groups.io Reply-To: devel@edk2.groups.io,dun.tan@intel.com X-Gm-Message-State: cWndeaR5xzuxEBvuyDqDmC6nx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1684231226; bh=TgGwiu6EssmXmLHpil1fHI2ZG4EjTW/R9xRdx4kNa3A=; h=Cc:Date:From:Reply-To:Subject:To; b=fzr8lcmUspBlBAaxOBfE3lEfrXflvG4fWE07VY7E3or8ox9vETdun9iKBWkU4lVFNK/ x44aDAqrzT53NVEpU/wTwvkhBDhcFK7wUsLDPkblguk2hu46MJn/V2RAxNrzgj09pQDBs N4AR5854J/CR35GLDV3ugWiou/R2F9E51Aw= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1684231228606100011 Content-Type: text/plain; charset="utf-8" In PiSmmCpuDxeSmm code, SetMemMapAttributes() marks memory ranges in SmmMemoryAttributesTable to RO/NX. There may exist non-present range in these memory ranges. Set other attributes for a non-present range is not permitted in CpuPageTableMapLib. So add code to handle this case. Only map the present ranges in SmmMemoryAttributesTable to RO or NX. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann --- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 147 +++++++++++++++++= +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++= +++++++++++++++++++++++++++++++++---------------------- 1 file changed, 125 insertions(+), 22 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPk= g/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 73ad9fb017..2faee8f859 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -902,6 +902,89 @@ PatchGdtIdtMap ( ); } =20 +/** + This function remove the non-present range in [MemMapStart, MemMapLimit]= and + set [MemMapStart, NonPresentRangeStart] as MemoryAttribute in page table. + + @param MemMapStart Pointer to the start address of range. + @param MemMapLimit Limit address of range. + @param NonPresentRangeStart Start address of non-present range. + @param NonPresentRangeLimit Limit address of non-present range. + @param MemoryAttribute The bit mask of attributes to modify = for the memory region. + +**/ +VOID +RemoveNonPresentRange ( + UINT64 *MemMapStart, + UINT64 MemMapLimit, + UINT64 NonPresentRangeStart, + UINT64 NonPresentRangeLimit, + UINT64 MemoryAttribute + ) +{ + if (*MemMapStart < NonPresentRangeStart) { + SmmSetMemoryAttributes ( + *MemMapStart, + NonPresentRangeStart - *MemMapStart, + MemoryAttribute + ); + } + + *MemMapStart =3D NonPresentRangeLimit; +} + +/** + This function set [MemMapStart, MemMapLimit] to the input MemoryAttribut= e. + + @param MemMapStart Start address of range. + @param MemMapLimit Limit address of range. + @param Map Pointer to the array of Cr3 IA32_MAP_ENTRY. + @param Count Count of IA32_MAP_ENTRY in Map. + @param MemoryAttribute The bit mask of attributes to modify for the= memory region. + +**/ +VOID +SetMemMapWithNonPresentRange ( + UINT64 MemMapStart, + UINT64 MemMapLimit, + IA32_MAP_ENTRY *Map, + UINTN Count, + UINT64 MemoryAttribute + ) +{ + UINTN Index; + UINT64 NonPresentRangeStart; + + NonPresentRangeStart =3D 0; + + for (Index =3D 0; Index < Count; Index++) { + if ((Map[Index].LinearAddress > NonPresentRangeStart) && + (MemMapStart < Map[Index].LinearAddress) && (MemMapLimit > NonPres= entRangeStart)) + { + // + // [NonPresentRangeStart, Map[Index].LinearAddress] is non-present. + // + RemoveNonPresentRange (&MemMapStart, MemMapLimit, NonPresentRangeSta= rt, Map[Index].LinearAddress, MemoryAttribute); + } + + NonPresentRangeStart =3D Map[Index].LinearAddress + Map[Index].Length; + if (NonPresentRangeStart >=3D MemMapLimit) { + break; + } + } + + // + // There is no non-present in current [MemMapStart, MemMapLimit] anymore. + // + if (MemMapStart < MemMapLimit) { + SmmSetMemoryAttributes ( + MemMapStart, + MemMapLimit - MemMapStart, + MemoryAttribute + ); + } +} + /** This function sets memory attribute according to MemoryAttributesTable. **/ @@ -916,6 +999,11 @@ SetMemMapAttributes ( UINTN DescriptorSize; UINTN Index; EDKII_PI_SMM_MEMORY_ATTRIBUTES_TABLE *MemoryAttributesTable; + UINTN PageTable; + EFI_STATUS Status; + IA32_MAP_ENTRY *Map; + UINTN Count; + UINT64 MemoryAttribute; =20 SmmGetSystemConfigurationTable (&gEdkiiPiSmmMemoryAttributesTableGuid, (= VOID **)&MemoryAttributesTable); if (MemoryAttributesTable =3D=3D NULL) { @@ -942,36 +1030,51 @@ SetMemMapAttributes ( MemoryMap =3D NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize); } =20 + Count =3D 0; + Map =3D NULL; + PageTable =3D AsmReadCr3 (); + Status =3D PageTableParse (PageTable, mPagingMode, NULL, &Count); + while (Status =3D=3D RETURN_BUFFER_TOO_SMALL) { + if (Map !=3D NULL) { + FreePool (Map); + } + + Map =3D AllocatePool (Count * sizeof (IA32_MAP_ENTRY)); + ASSERT (Map !=3D NULL); + Status =3D PageTableParse (PageTable, mPagingMode, Map, &Count); + } + + ASSERT_RETURN_ERROR (Status); + MemoryMap =3D MemoryMapStart; for (Index =3D 0; Index < MemoryMapEntryCount; Index++) { DEBUG ((DEBUG_VERBOSE, "SetAttribute: Memory Entry - 0x%lx, 0x%x\n", M= emoryMap->PhysicalStart, MemoryMap->NumberOfPages)); - switch (MemoryMap->Type) { - case EfiRuntimeServicesCode: - SmmSetMemoryAttributes ( - MemoryMap->PhysicalStart, - EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages), - EFI_MEMORY_RO - ); - break; - case EfiRuntimeServicesData: - SmmSetMemoryAttributes ( - MemoryMap->PhysicalStart, - EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages), - EFI_MEMORY_XP - ); - break; - default: - SmmSetMemoryAttributes ( - MemoryMap->PhysicalStart, - EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->NumberOfPages), - EFI_MEMORY_XP - ); - break; + if (MemoryMap->Type =3D=3D EfiRuntimeServicesCode) { + MemoryAttribute =3D EFI_MEMORY_RO; + } else { + // + // Set other type memory as NX. + // + MemoryAttribute =3D EFI_MEMORY_XP; } =20 + // + // There may exist non-present range overlaps with the MemoryMap range. + // Do not change other attributes of non-present range while still rem= aining it as non-present + // + SetMemMapWithNonPresentRange ( + MemoryMap->PhysicalStart, + MemoryMap->PhysicalStart + EFI_PAGES_TO_SIZE ((UINTN)MemoryMap->Numb= erOfPages), + Map, + Count, + MemoryAttribute + ); + MemoryMap =3D NEXT_MEMORY_DESCRIPTOR (MemoryMap, DescriptorSize); } =20 + FreePool (Map); + PatchSmmSaveStateMap (); PatchGdtIdtMap (); =20 --=20 2.31.1.windows.1 -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#104926): https://edk2.groups.io/g/devel/message/104926 Mute This Topic: https://groups.io/mt/98922931/1787277 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [importer@patchew.org] -=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-=3D-