From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by spool.mail.gandi.net (Postfix) with ESMTPS id 37D9778003C for ; Mon, 9 Oct 2023 00:08:19 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=uS4vdaqkLrn5mDIEAUy0yYUxWIJcuEMlRyapVPPsJ38=; c=relaxed/simple; d=groups.io; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References:MIME-Version:Precedence:List-Subscribe:List-Help:Sender:List-Id:Mailing-List:Delivered-To:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Type:Content-Transfer-Encoding; s=20140610; t=1696810097; v=1; b=se2pZdsiZrCrzc3NZl8V99LQeHVTi9o9+KxD0tsnOXl1HC+dbOj9fsm/GEdXT43dFLxI60Pc X5lhJzFktlfh+cs3L4XVSSPXPSBbtgrhNog8qan09fwHJO6rSFfvqGC9G5qWFuF/RW54Ij5IMnP kQqRl7OydQfUqrMwLcsreQjk= X-Received: by 127.0.0.2 with SMTP id UYGTYY7687511xzMKaskqTir; Sun, 08 Oct 2023 17:08:17 -0700 X-Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by mx.groups.io with SMTP id smtpd.web11.50051.1696810090985008367 for ; Sun, 08 Oct 2023 17:08:11 -0700 X-Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-690b7cb71aeso2892904b3a.0 for ; Sun, 08 Oct 2023 17:08:10 -0700 (PDT) X-Gm-Message-State: akWEseqPeIGaNqJyPLyctVbNx7686176AA= X-Google-Smtp-Source: AGHT+IEJJZkolkiX+5QbGvRMCWMyiSygRg76ti5n3CRdUnCeyPeggp9ft1Jz76b4junzxrsk5mOQig== X-Received: by 2002:a05:6a00:1143:b0:68b:e29c:b69 with SMTP id b3-20020a056a00114300b0068be29c0b69mr16231227pfm.9.1696810090306; Sun, 08 Oct 2023 17:08:10 -0700 (PDT) X-Received: from localhost.localdomain ([50.46.253.1]) by smtp.gmail.com with ESMTPSA id t20-20020a62ea14000000b0068fcc7f6b00sm5048320pfh.74.2023.10.08.17.08.09 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Oct 2023 17:08:09 -0700 (PDT) From: "Taylor Beebe" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Jiewen Yao , Jordan Justen , Gerd Hoffmann , Rebecca Cran , Peter Grehan , =?UTF-8?q?Corvin=20K=C3=B6hne?= Subject: [edk2-devel] [PATCH v5 23/28] OvmfPkg: Enable Choosing Memory Protection Profile via QemuCfg Date: Sun, 8 Oct 2023 17:07:35 -0700 Message-ID: <20231009000742.1792-24-taylor.d.beebe@gmail.com> In-Reply-To: <20231009000742.1792-1-taylor.d.beebe@gmail.com> References: <20231009000742.1792-1-taylor.d.beebe@gmail.com> MIME-Version: 1.0 Precedence: Bulk 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,taylor.d.beebe@gmail.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20140610 header.b=se2pZdsi; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=gmail.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 66.175.222.108 as permitted sender) smtp.mailfrom=bounce@groups.io Now that the EDK2 tree uses GetMemoryProtectionsLib to query the platform memory protection settings, OvmfPkg can be updated to use QemuCfg to set the entire memory protection profile instead of just SetNxForStack. For example, the following will set the DXE memory protection to the RELEASE preset. -fw_cfg name=opt/org.tianocore/DxeMemoryProtectionProfile,string=release The following will set the MM memory protection to the RELEASE preset. -fw_cfg name=opt/org.tianocore/MmMemoryProtectionProfile,string=release For users of Stuart, DXE_MEMORY_PROTECTION_PROFILE=release and MM_MEMORY_PROTECTION_PROFILE=release are equivalent to the above examples. Signed-off-by: Taylor Beebe Cc: Ard Biesheuvel Cc: Jiewen Yao Cc: Jordan Justen Cc: Gerd Hoffmann Cc: Rebecca Cran Cc: Peter Grehan Cc: Corvin Köhne --- OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c | 21 ++++++++----- OvmfPkg/Library/PeilessStartupLib/X64/VirtualMemory.c | 13 +------- OvmfPkg/Library/PlatformInitLib/Platform.c | 15 --------- OvmfPkg/PlatformPei/IntelTdx.c | 2 -- OvmfPkg/PlatformPei/Platform.c | 33 +++++++------------- OvmfPkg/TdxDxe/TdxDxe.c | 7 ++--- OvmfPkg/Include/Library/PlatformInitLib.h | 13 -------- OvmfPkg/Library/PeilessStartupLib/PeilessStartupLib.inf | 2 +- OvmfPkg/PlatformCI/PlatformBuildLib.py | 8 +++++ OvmfPkg/PlatformPei/PlatformPei.inf | 1 + 10 files changed, 37 insertions(+), 78 deletions(-) diff --git a/OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c b/OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c index cf645aad3246..2f8fd51f3fc5 100644 --- a/OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c +++ b/OvmfPkg/Library/PeilessStartupLib/PeilessStartup.c @@ -20,7 +20,7 @@ #include #include #include -#include +#include #include #include #include @@ -109,18 +109,23 @@ InitializePlatform ( PlatformMemMapInitialization (PlatformInfoHob); - DxeSettings = DxeMemoryProtectionProfiles[DxeMemoryProtectionSettingsPcd].Settings; - MmSettings = MmMemoryProtectionProfiles[MmMemoryProtectionSettingsPcd].Settings; - DxeSettings.StackExecutionProtectionEnabled = PcdGetBool (PcdSetNxForStack); - QemuFwCfgParseBool ("opt/ovmf/PcdSetNxForStack", &DxeSettings.StackExecutionProtectionEnabled); + if (EFI_ERROR (ParseFwCfgDxeMemoryProtectionSettings (&DxeSettings))) { + DxeSettings = DxeMemoryProtectionProfiles[DxeMemoryProtectionSettingsRelease].Settings; + } - SetDxeMemoryProtectionSettings (&DxeSettings, DxeMemoryProtectionSettingsPcd); - SetMmMemoryProtectionSettings (&MmSettings, MmMemoryProtectionSettingsPcd); + if (EFI_ERROR (ParseFwCfgMmMemoryProtectionSettings (&MmSettings))) { + MmSettings = MmMemoryProtectionProfiles[MmMemoryProtectionSettingsOff].Settings; + } + + // Always disable NullPointerDetection in EndOfDxe phase for shim compatability + DxeSettings.NullPointerDetection.DisableEndOfDxe = TRUE; + + SetDxeMemoryProtectionSettings (&DxeSettings, DxeMemoryProtectionSettingsRelease); + SetMmMemoryProtectionSettings (&MmSettings, MmMemoryProtectionSettingsOff); if (TdIsEnabled ()) { PlatformInfoHob->PcdConfidentialComputingGuestAttr = CCAttrIntelTdx; PlatformInfoHob->PcdTdxSharedBitMask = TdSharedPageMask (); - PlatformInfoHob->PcdSetNxForStack = TRUE; } PlatformMiscInitialization (PlatformInfoHob); diff --git a/OvmfPkg/Library/PeilessStartupLib/X64/VirtualMemory.c b/OvmfPkg/Library/PeilessStartupLib/X64/VirtualMemory.c index 41521e3d3d71..7ae9b5743810 100644 --- a/OvmfPkg/Library/PeilessStartupLib/X64/VirtualMemory.c +++ b/OvmfPkg/Library/PeilessStartupLib/X64/VirtualMemory.c @@ -53,18 +53,7 @@ IsSetNxForStack ( VOID ) { - EFI_HOB_GUID_TYPE *GuidHob; - EFI_HOB_PLATFORM_INFO *PlatformInfo; - - GuidHob = GetFirstGuidHob (&gUefiOvmfPkgPlatformInfoGuid); - if (GuidHob == NULL) { - ASSERT (FALSE); - return FALSE; - } - - PlatformInfo = (EFI_HOB_PLATFORM_INFO *)GET_GUID_HOB_DATA (GuidHob); - - return PlatformInfo->PcdSetNxForStack; + return mMps.Dxe.StackExecutionProtectionEnabled; } /** diff --git a/OvmfPkg/Library/PlatformInitLib/Platform.c b/OvmfPkg/Library/PlatformInitLib/Platform.c index f48bf16ae300..bc9becc4016e 100644 --- a/OvmfPkg/Library/PlatformInitLib/Platform.c +++ b/OvmfPkg/Library/PlatformInitLib/Platform.c @@ -249,21 +249,6 @@ PlatformMemMapInitialization ( PlatformInfoHob->PcdPciIoSize = PciIoSize; } -/** - * Fetch "opt/ovmf/PcdSetNxForStack" from QEMU - * - * @param Setting The pointer to the setting of "/opt/ovmf/PcdSetNxForStack". - * @return EFI_SUCCESS Successfully fetch the settings. - */ -EFI_STATUS -EFIAPI -PlatformNoexecDxeInitialization ( - IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob - ) -{ - return QemuFwCfgParseBool ("opt/ovmf/PcdSetNxForStack", &PlatformInfoHob->PcdSetNxForStack); -} - VOID PciExBarInitialization ( VOID diff --git a/OvmfPkg/PlatformPei/IntelTdx.c b/OvmfPkg/PlatformPei/IntelTdx.c index 3d625cabd844..1cb6729e56e6 100644 --- a/OvmfPkg/PlatformPei/IntelTdx.c +++ b/OvmfPkg/PlatformPei/IntelTdx.c @@ -48,7 +48,5 @@ IntelTdxInitialize ( PcdStatus = PcdSet64S (PcdTdxSharedBitMask, TdSharedPageMask ()); ASSERT_RETURN_ERROR (PcdStatus); - PcdStatus = PcdSetBoolS (PcdSetNxForStack, TRUE); - ASSERT_RETURN_ERROR (PcdStatus); #endif } diff --git a/OvmfPkg/PlatformPei/Platform.c b/OvmfPkg/PlatformPei/Platform.c index bcd8d3a1be14..fa76ad2de202 100644 --- a/OvmfPkg/PlatformPei/Platform.c +++ b/OvmfPkg/PlatformPei/Platform.c @@ -39,6 +39,7 @@ #include #include #include +#include #include "Platform.h" @@ -74,21 +75,6 @@ MemMapInitialization ( ASSERT_RETURN_ERROR (PcdStatus); } -STATIC -VOID -NoexecDxeInitialization ( - IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob - ) -{ - RETURN_STATUS Status; - - Status = PlatformNoexecDxeInitialization (PlatformInfoHob); - if (!RETURN_ERROR (Status)) { - Status = PcdSetBoolS (PcdSetNxForStack, PlatformInfoHob->PcdSetNxForStack); - ASSERT_RETURN_ERROR (Status); - } -} - static const UINT8 EmptyFdt[] = { 0xd0, 0x0d, 0xfe, 0xed, 0x00, 0x00, 0x00, 0x48, 0x00, 0x00, 0x00, 0x38, 0x00, 0x00, 0x00, 0x48, @@ -345,13 +331,17 @@ InitializePlatform ( PublishPeiMemory (PlatformInfoHob); - DxeSettings = DxeMemoryProtectionProfiles[DxeMemoryProtectionSettingsPcd].Settings; - MmSettings = MmMemoryProtectionProfiles[MmMemoryProtectionSettingsPcd].Settings; - DxeSettings.StackExecutionProtectionEnabled = PcdGetBool (PcdSetNxForStack); - QemuFwCfgParseBool ("opt/ovmf/PcdSetNxForStack", &DxeSettings.StackExecutionProtectionEnabled); + if (EFI_ERROR (ParseFwCfgDxeMemoryProtectionSettings (&DxeSettings))) { + SetDxeMemoryProtectionSettings (NULL, DxeMemoryProtectionSettingsGrubCompat); + } else { + SetDxeMemoryProtectionSettings (&DxeSettings, DxeMemoryProtectionSettingsGrubCompat); + } - SetDxeMemoryProtectionSettings (&DxeSettings, DxeMemoryProtectionSettingsPcd); - SetMmMemoryProtectionSettings (&MmSettings, MmMemoryProtectionSettingsPcd); + if (EFI_ERROR (ParseFwCfgMmMemoryProtectionSettings (&MmSettings))) { + SetMmMemoryProtectionSettings (NULL, MmMemoryProtectionSettingsOff); + } else { + SetMmMemoryProtectionSettings (&MmSettings, MmMemoryProtectionSettingsOff); + } PlatformQemuUc32BaseInitialization (PlatformInfoHob); @@ -365,7 +355,6 @@ InitializePlatform ( PeiFvInitialization (PlatformInfoHob); MemTypeInfoInitialization (PlatformInfoHob); MemMapInitialization (PlatformInfoHob); - NoexecDxeInitialization (PlatformInfoHob); } InstallClearCacheCallback (); diff --git a/OvmfPkg/TdxDxe/TdxDxe.c b/OvmfPkg/TdxDxe/TdxDxe.c index 30732f421bb6..5e497ba66227 100644 --- a/OvmfPkg/TdxDxe/TdxDxe.c +++ b/OvmfPkg/TdxDxe/TdxDxe.c @@ -131,15 +131,12 @@ SetPcdSettings ( PcdStatus = PcdSet64S (PcdConfidentialComputingGuestAttr, PlatformInfoHob->PcdConfidentialComputingGuestAttr); ASSERT_RETURN_ERROR (PcdStatus); - PcdStatus = PcdSetBoolS (PcdSetNxForStack, PlatformInfoHob->PcdSetNxForStack); - ASSERT_RETURN_ERROR (PcdStatus); DEBUG (( DEBUG_INFO, - "HostBridgeDevId=0x%x, CCAttr=0x%x, SetNxForStack=%x\n", + "HostBridgeDevId=0x%x, CCAttr=0x%x\n", PlatformInfoHob->HostBridgeDevId, - PlatformInfoHob->PcdConfidentialComputingGuestAttr, - PlatformInfoHob->PcdSetNxForStack + PlatformInfoHob->PcdConfidentialComputingGuestAttr )); PcdStatus = PcdSet32S (PcdCpuBootLogicalProcessorNumber, PlatformInfoHob->PcdCpuBootLogicalProcessorNumber); diff --git a/OvmfPkg/Include/Library/PlatformInitLib.h b/OvmfPkg/Include/Library/PlatformInitLib.h index 57b18b94d9b8..b2468f206321 100644 --- a/OvmfPkg/Include/Library/PlatformInitLib.h +++ b/OvmfPkg/Include/Library/PlatformInitLib.h @@ -32,7 +32,6 @@ typedef struct { UINT32 Uc32Base; UINT32 Uc32Size; - BOOLEAN PcdSetNxForStack; UINT64 PcdTdxSharedBitMask; UINT64 PcdPciMmio64Base; @@ -182,18 +181,6 @@ PlatformMemMapInitialization ( IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob ); -/** - * Fetch "opt/ovmf/PcdSetNxForStack" from QEMU - * - * @param Setting The pointer to the setting of "/opt/ovmf/PcdSetNxForStack". - * @return EFI_SUCCESS Successfully fetch the settings. - */ -EFI_STATUS -EFIAPI -PlatformNoexecDxeInitialization ( - IN OUT EFI_HOB_PLATFORM_INFO *PlatformInfoHob - ); - VOID EFIAPI PlatformMiscInitialization ( diff --git a/OvmfPkg/Library/PeilessStartupLib/PeilessStartupLib.inf b/OvmfPkg/Library/PeilessStartupLib/PeilessStartupLib.inf index 47bd42d23d11..a6d7b53f52cf 100644 --- a/OvmfPkg/Library/PeilessStartupLib/PeilessStartupLib.inf +++ b/OvmfPkg/Library/PeilessStartupLib/PeilessStartupLib.inf @@ -57,7 +57,7 @@ [LibraryClasses] QemuFwCfgLib PlatformInitLib SetMemoryProtectionsLib - QemuFwCfgSimpleParserLib + MemoryProtectionConfigLib [Guids] gEfiHobMemoryAllocModuleGuid diff --git a/OvmfPkg/PlatformCI/PlatformBuildLib.py b/OvmfPkg/PlatformCI/PlatformBuildLib.py index f829738cdda4..0d5d39c078d0 100644 --- a/OvmfPkg/PlatformCI/PlatformBuildLib.py +++ b/OvmfPkg/PlatformCI/PlatformBuildLib.py @@ -183,6 +183,8 @@ class PlatformBuilder( UefiBuilder, BuildSettingsManager): VirtualDrive = os.path.join(self.env.GetValue("BUILD_OUTPUT_BASE"), "VirtualDrive") os.makedirs(VirtualDrive, exist_ok=True) OutputPath_FV = os.path.join(self.env.GetValue("BUILD_OUTPUT_BASE"), "FV") + DxeMemoryProtection = self.env.GetValue("DXE_MEMORY_PROTECTION_PROFILE", "") + MmMemoryProtection = self.env.GetValue("MM_MEMORY_PROTECTION_PROFILE", "") if (self.env.GetValue("QEMU_SKIP") and self.env.GetValue("QEMU_SKIP").upper() == "TRUE"): @@ -199,6 +201,12 @@ class PlatformBuilder( UefiBuilder, BuildSettingsManager): args += " -smp 4" args += f" -drive file=fat:rw:{VirtualDrive},format=raw,media=disk" # Mount disk with startup.nsh + if (DxeMemoryProtection.lower() != ""): + args += " -fw_cfg name=opt/org.tianocore/DxeMemoryProtectionProfile,string=" + DxeMemoryProtection.lower() + + if (MmMemoryProtection.lower() != ""): + args += " -fw_cfg name=opt/org.tianocore/MmMemoryProtectionProfile,string=" + MmMemoryProtection.lower() + if (self.env.GetValue("QEMU_HEADLESS").upper() == "TRUE"): args += " -display none" # no graphics diff --git a/OvmfPkg/PlatformPei/PlatformPei.inf b/OvmfPkg/PlatformPei/PlatformPei.inf index 6b8442d12b2c..fbaa6bdc8ee5 100644 --- a/OvmfPkg/PlatformPei/PlatformPei.inf +++ b/OvmfPkg/PlatformPei/PlatformPei.inf @@ -66,6 +66,7 @@ [LibraryClasses] CcExitLib PlatformInitLib SetMemoryProtectionsLib + MemoryProtectionConfigLib [Pcd] gUefiOvmfPkgTokenSpaceGuid.PcdOvmfPeiMemFvBase -- 2.42.0.windows.2 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#109428): https://edk2.groups.io/g/devel/message/109428 Mute This Topic: https://groups.io/mt/101843367/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-