From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by mx.groups.io with SMTP id smtpd.web10.55514.1672714655624962507 for ; Mon, 02 Jan 2023 18:57:35 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="unable to parse pub key" header.i=@intel.com header.s=intel header.b=RuRiqnv1; spf=pass (domain: intel.com, ip: 192.55.52.43, mailfrom: dun.tan@intel.com) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1672714655; x=1704250655; h=from:to:cc:subject:date:message-id:mime-version: content-transfer-encoding; bh=MP1lMTGl3xR7v9avCOPINjoIPCszIUI7qci1j+/eWw8=; b=RuRiqnv18utZnUMz3jNX23V4TxX8PTL2N96BKh3e5k47xTVDsO8Yxm9h 3XDPyYVvh6RqrhRfm+Mh7AHSP05Hr3P59yBM531LA+vux+cZo7D7FCGaJ QytZL9/1sUyWupOHG/nG+T/w6rkxV99wnTHx7lN5oEJwhj/RVR5gfrm8/ lsCWcoomwLF5nFuYy8m6nPLQnuVk5mRq0JjqZiHVKNSLNM93CZLvFaitS I5F1DNlIvtoYmiKmYqNYPAvyLK694cYWPX+y1gLXLDdi7R8qlAWXi8Ywh q5RwZoxW4gkwm8rLS3Hf2CDBRdJxM/PeZwAuRZrRDRlx4KNofeiAVHNZH Q==; X-IronPort-AV: E=McAfee;i="6500,9779,10578"; a="407830441" X-IronPort-AV: E=Sophos;i="5.96,295,1665471600"; d="scan'208";a="407830441" Received: from orsmga003.jf.intel.com ([10.7.209.27]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jan 2023 18:57:34 -0800 X-IronPort-AV: E=McAfee;i="6500,9779,10578"; a="604684797" X-IronPort-AV: E=Sophos;i="5.96,295,1665471600"; d="scan'208";a="604684797" Received: from duntan-mobl.ccr.corp.intel.com ([10.239.157.24]) by orsmga003-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 02 Jan 2023 18:57:30 -0800 From: "duntan" To: devel@edk2.groups.io Cc: Eric Dong , Ray Ni , Rahul Kumar Subject: [PATCH] UefiCpuPkg/PiSmmCpuDxeSmm:Fix PF issue caused by smm page table code Date: Tue, 3 Jan 2023 10:56:55 +0800 Message-Id: <20230103025655.1867-1-dun.tan@intel.com> X-Mailer: git-send-email 2.31.1.windows.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit When setting new page table pool to RO, only disable/enable WP when Cr0.WP has been set to 1 to fix potential PF caused by b822be1a20 (UefiCpuPkg/PiSmmCpuDxeSmm: Introduce page table pool mechanism). With previous code, if someone want to modify the page table and Cr0.WP has been cleared before modify page table, Cr0.WP may be set to 1 again since new pool may be generated during this process Then PF fault may happens. Signed-off-by: Dun Tan Cc: Eric Dong Cc: Ray Ni Cc: Rahul Kumar --- UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c index 4bb23f6920..c385f12d9c 100644 --- a/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c +++ b/UefiCpuPkg/PiSmmCpuDxeSmm/SmmCpuMemoryManagement.c @@ -67,8 +67,10 @@ InitializePageTablePool ( IN UINTN PoolPages ) { - VOID *Buffer; - BOOLEAN CetEnabled; + VOID *Buffer; + BOOLEAN CetEnabled; + BOOLEAN WpEnabled; + IA32_CR0 Cr0; // // Always reserve at least PAGE_TABLE_POOL_UNIT_PAGES, including one page for @@ -106,21 +108,35 @@ InitializePageTablePool ( // if (mIsReadOnlyPageTable) { CetEnabled = ((AsmReadCr4 () & CR4_CET_ENABLE) != 0) ? TRUE : FALSE; - if (CetEnabled) { + Cr0.UintN = AsmReadCr0 (); + WpEnabled = (Cr0.Bits.WP != 0) ? TRUE : FALSE; + if (WpEnabled) { // - // CET must be disabled if WP is disabled. + // Only disable/enable WP when Cr0.Bits.WP has been set to 1. // - DisableCet (); + Cr0.Bits.WP = 0; + AsmWriteCr0 (Cr0.UintN); + + if (CetEnabled) { + // + // CET must be disabled if WP is disabled. + // + DisableCet (); + } } - AsmWriteCr0 (AsmReadCr0 () & ~CR0_WP); SmmSetMemoryAttributes ((EFI_PHYSICAL_ADDRESS)(UINTN)Buffer, EFI_PAGES_TO_SIZE (PoolPages), EFI_MEMORY_RO); - AsmWriteCr0 (AsmReadCr0 () | CR0_WP); - if (CetEnabled) { - // - // re-enable CET. - // - EnableCet (); + if (WpEnabled) { + Cr0.UintN = AsmReadCr0 (); + Cr0.Bits.WP = 1; + AsmWriteCr0 (Cr0.UintN); + + if (CetEnabled) { + // + // re-enable CET. + // + EnableCet (); + } } } -- 2.31.1.windows.1