From nobody Fri Dec 19 19:16:12 2025 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+100852+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+100852+1787277+3901457@groups.io; dmarc=fail(p=none dis=none) header.from=intel.com ARC-Seal: i=1; a=rsa-sha256; t=1678270144; cv=none; d=zohomail.com; s=zohoarc; b=GUNoe1JZvvbLzgx9HnOt2v8sbzfytxpgY36WEvg6d/QXqnOde+1B/SYalwAaaPgVGsRMgJr3oBgExDahhxHY75PL2lyYeYhKU9QRz6HBZqLe4myJyJC5cDg057+6oPF7OhZsPXlchriYGXF/KSBoyUqnLXSPV82RNtuohR/sSrs= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1678270144; 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=j7I0sH6B5fDZ3mRi0JuQ4X+b3j0vGWJhDxsWPif4dos=; b=PLdQJWQsU0TsxR/sIUUeGxwgEY+bg3a6ugoLkh9kThdG9Jv55L7xgZCAAPIq4xyCWRv+sBS4azeayrOgKFEmzyTfGoqCvHca75D1vUy67KowmPdmOiWdxOvTJZqq+LdSJ6UpWyctjWqJzqHrnVe38XLgnjTwp5oRI1U9B2NZ8uo= 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+100852+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 1678270144271478.93169262009326; Wed, 8 Mar 2023 02:09:04 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id diCLYY1788612xXxAJJiVHfg; Wed, 08 Mar 2023 02:09:03 -0800 X-Received: from mga03.intel.com (mga03.intel.com [134.134.136.65]) by mx.groups.io with SMTP id smtpd.web10.3470.1678270139326397803 for ; Wed, 08 Mar 2023 02:09:03 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10642"; a="338442687" X-IronPort-AV: E=Sophos;i="5.98,243,1673942400"; d="scan'208";a="338442687" X-Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga103.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Mar 2023 02:09:02 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10642"; a="745862650" X-IronPort-AV: E=Sophos;i="5.98,243,1673942400"; d="scan'208";a="745862650" X-Received: from shwdeopenlab702.ccr.corp.intel.com ([10.239.55.92]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 08 Mar 2023 02:09:01 -0800 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar , Gerd Hoffmann Subject: [edk2-devel] [Patch V2 05/14] UefiCpuPkg/CpuPageTebleLib: Check Mask and Attr in PageTableMap Date: Wed, 8 Mar 2023 18:07:49 +0800 Message-Id: <20230308100758.669-6-dun.tan@intel.com> In-Reply-To: <20230308100758.669-1-dun.tan@intel.com> References: <20230308100758.669-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: pUIk9S7ZBV5hFkQy9VkRrdhsx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1678270143; bh=aJqpTIUTbVuv6Et5OeowdRsTrhhjE/RlmTnvZiAT+iY=; h=Cc:Date:From:Reply-To:Subject:To; b=CqF/hdlU0Mo6X/laSUGUQ1jnBCBa34c2W6hpRJzl8h54EDzY/8hkNbON6IzEs5rGceB zl7bBlLEePU59IqvYVy97r2JdBWbpveceC++b/cWd7kaksu4Faqgn/bIoNU69sxLXwgqQ lexXqWFW82DpJSJ8hHvZJo2q1XZMAdvoAZ8= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1678270146398100002 Content-Type: text/plain; charset="utf-8" When creating new page table or mapping not-present range in existing page table, we need to make sure all the non-reserved fields of input Mask are not 0 and Present field of input Attribute is 1. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar Cc: Gerd Hoffmann --- UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableMap.c | 54 ++++++++++++++++= +++++++++++++++++++++++++++++++++++++- 1 file changed, 53 insertions(+), 1 deletion(-) diff --git a/UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableMap.c b/UefiCpu= Pkg/Library/CpuPageTableLib/CpuPageTableMap.c index 0f3d0d684e..56f762a15e 100644 --- a/UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableMap.c +++ b/UefiCpuPkg/Library/CpuPageTableLib/CpuPageTableMap.c @@ -214,6 +214,33 @@ PageTableLibSetPnle ( Pnle->Bits.CacheDisabled =3D 0; } =20 +/** + Check if any Non-Reserved field of Mask is 0 or Attribute->Bits.Present = is 0 + when creating new page table or mapping not-present range. + + @param[in] Attribute The attribute of the linear address range. + @param[in] Mask The mask used for attribute to check. + + @retval RETURN_INVALID_PARAMETER There is 0-value field in Non-Reserv= ed fields of Mask or Attribute->Bits.Present is 0. + @retval RETURN_SUCCESS All Non-Reserved fields of Mask are = not 0 and Attribute->Bits.Present is 1. +**/ +RETURN_STATUS +CheckMaskAndAttrForNotPresentEntry ( + IN IA32_MAP_ATTRIBUTE *Attribute, + IN IA32_MAP_ATTRIBUTE *Mask + ) +{ + if ((Attribute->Bits.Present =3D=3D 0) || (Mask->Bits.Present =3D=3D 0) = || (Mask->Bits.ReadWrite =3D=3D 0) || + (Mask->Bits.UserSupervisor =3D=3D 0) || (Mask->Bits.WriteThrough =3D= =3D 0) || (Mask->Bits.CacheDisabled =3D=3D 0) || + (Mask->Bits.Accessed =3D=3D 0) || (Mask->Bits.Dirty =3D=3D 0) || (Ma= sk->Bits.Pat =3D=3D 0) || (Mask->Bits.Global =3D=3D 0) || + (Mask->Bits.PageTableBaseAddress =3D=3D 0) || (Mask->Bits.Protection= Key =3D=3D 0) || (Mask->Bits.Nx =3D=3D 0)) + { + return RETURN_INVALID_PARAMETER; + } + + return RETURN_SUCCESS; +} + /** Update page table to map [LinearAddress, LinearAddress + Length) with sp= ecified attribute in the specified level. =20 @@ -259,6 +286,7 @@ PageTableLibMapInLevel ( UINTN Index; IA32_PAGING_ENTRY *PagingEntry; UINTN PagingEntryIndex; + UINTN PagingEntryIndexLimit; IA32_PAGING_ENTRY *CurrentPagingEntry; UINT64 RegionLength; UINT64 SubLength; @@ -302,6 +330,14 @@ PageTableLibMapInLevel ( // =20 if (ParentPagingEntry->Pce.Present =3D=3D 0) { + // + // [LinearAddress, LinearAddress + Length] contains not-present range. + // + Status =3D CheckMaskAndAttrForNotPresentEntry (Attribute, Mask); + if (RETURN_ERROR (Status)) { + return Status; + } + // // The parent entry is CR3 or PML5E/PML4E/PDPTE/PDE. // It does NOT point to an existing page directory. @@ -372,6 +408,23 @@ PageTableLibMapInLevel ( PageTableLibSetPnle (&ParentPagingEntry->Pnle, &NopAttribute, &AllOn= eMask); } } else { + PagingEntry =3D (IA32_PAGING_ENTRY *)(UINTN)IA32_PNLE_PAGE_T= ABLE_BASE_ADDRESS (&ParentPagingEntry->Pnle); + PagingEntryIndexLimit =3D (BitFieldRead64 (LinearAddress + Length - 1,= BitStart + 9, 63) > BitFieldRead64 (LinearAddress + Offset, BitStart + 9, = 63)) ? 511 : + (UINTN)BitFieldRead64 (LinearAddress + Length = - 1, BitStart, BitStart + 9 - 1); + for (Index =3D PagingEntryIndex; Index <=3D PagingEntryIndexLimit; Ind= ex++) { + if (PagingEntry[Index].Pce.Present =3D=3D 0) { + // + // [LinearAddress, LinearAddress + Length] contains not-present ra= nge. + // + Status =3D CheckMaskAndAttrForNotPresentEntry (Attribute, Mask); + if (RETURN_ERROR (Status)) { + return Status; + } + + break; + } + } + // // It's a non-leaf entry // @@ -419,7 +472,6 @@ PageTableLibMapInLevel ( // Update child entries to use restrictive attribute inherited fro= m parent. // e.g.: Set PDE[0-255].ReadWrite =3D 0 // - PagingEntry =3D (IA32_PAGING_ENTRY *)(UINTN)IA32_PNLE_PAGE_TABLE_B= ASE_ADDRESS (&ParentPagingEntry->Pnle); for (Index =3D 0; Index < 512; Index++) { if (PagingEntry[Index].Pce.Present =3D=3D 0) { continue; --=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 (#100852): https://edk2.groups.io/g/devel/message/100852 Mute This Topic: https://groups.io/mt/97469476/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-