From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail05.groups.io (mail05.groups.io [45.79.224.7]) by spool.mail.gandi.net (Postfix) with ESMTPS id EEBCF941920 for ; Sun, 14 Jul 2024 12:25:16 +0000 (UTC) DKIM-Signature: a=rsa-sha256; bh=DJwue4VcIIe+erCswLGFQLmoRMdyAwt42jrkPFkkjEo=; 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:Resent-Date:Resent-From:Reply-To:List-Unsubscribe-Post:List-Unsubscribe:Content-Transfer-Encoding; s=20240206; t=1720959916; v=1; b=BenMYR3ZKD/kdf15SKkiBPi+q++96ZyeOsi2FvNU130BoNvhekv3gQeq9gL601MNhS5yB6/e wzafDSEGAoLmce05n3u5Oo3EX1dp9Hbbc0QbYMqJ6divr0eahNurq7GkFGpyo++Cz33tnh2Yo2u tsXO4lRMUPR170gCFTl+Oo1ClatvYRLG5bU27KndkCfhnYOE5GMTVZM2ObUcg2O8LkcBGinMPnG e0/ajCaNITWuJR8WrYV54YJ3oPAcp6SabJhbwLPKZLiqwq5cNa82C/ZzGvFqmv5YshtvQQPgnUz Ckcy6RzbXOf1urWyyXo1f6fS8x7GyTDlglylUVuU2B88Q== X-Received: by 127.0.0.2 with SMTP id T5xCYY7687511xTidsYvdI5L; Sun, 14 Jul 2024 05:25:15 -0700 X-Received: from m16.mail.163.com (m16.mail.163.com [117.135.210.2]) by mx.groups.io with SMTP id smtpd.web10.15295.1720959909310469595 for ; Sun, 14 Jul 2024 05:25:10 -0700 X-Received: from sh-hanliyang.Hygon.cn (unknown [118.242.3.34]) by gzga-smtp-mta-g3-5 (Coremail) with SMTP id _____wD3fy6Xw5Nmhb3wCw--.63654S5; Sun, 14 Jul 2024 20:25:02 +0800 (CST) From: wojiaohanliyang@163.com To: devel@edk2.groups.io Cc: erdemaktas@google.com, jejb@linux.ibm.com, jiewen.yao@intel.com, min.m.xu@intel.com, thomas.lendacky@amd.com, kraxel@redhat.com, hanliyang Subject: [edk2-devel] [PATCH 3/3] OvmfPkg/PlatformPei: Update mapping of FlashNvVarStore before validate it Date: Sun, 14 Jul 2024 20:24:55 +0800 Message-Id: <20240714122455.136148-4-wojiaohanliyang@163.com> In-Reply-To: <20240714122455.136148-1-wojiaohanliyang@163.com> References: <20240714122455.136148-1-wojiaohanliyang@163.com> MIME-Version: 1.0 X-CM-TRANSID: _____wD3fy6Xw5Nmhb3wCw--.63654S5 X-Coremail-Antispam: 1Uf129KBjvJXoW3WFWUZrWDtw4rZF43GF4rZrb_yoW7Cw4Dp3 yDtw4rCryUWay7t3ykAa4xJw4Iya4fu3s8JrWUX3W7uas5GFnxCa18ZF18ZayxJF18J3WD XF4jv3WqgFyqvaDanT9S1TB71UUUUU7qnTZGkaVYY2UrUUUUjbIjqfuFe4nvWSU5nxnvy2 9KBjDUYxBIdaVFxhVjvjDU0xZFpf9x07U1E_iUUUUU= X-Originating-IP: [118.242.3.34] X-CM-SenderInfo: 5zrmxthrkd0zxl1d0wi6rwjhhfrp/xtbBZx0cL2V4IGb4JAABsP 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 Resent-Date: Sun, 14 Jul 2024 05:25:10 -0700 Resent-From: wojiaohanliyang@163.com Reply-To: devel@edk2.groups.io,wojiaohanliyang@163.com List-Unsubscribe-Post: List-Unsubscribe=One-Click List-Unsubscribe: X-Gm-Message-State: 9ds3LGXxsKmHfk9h2kfowdflx7686176AA= Content-Transfer-Encoding: 8bit X-GND-Status: LEGIT Authentication-Results: spool.mail.gandi.net; dkim=pass header.d=groups.io header.s=20240206 header.b=BenMYR3Z; dmarc=fail reason="SPF not aligned (relaxed), DKIM not aligned (relaxed)" header.from=163.com (policy=none); spf=pass (spool.mail.gandi.net: domain of bounce@groups.io designates 45.79.224.7 as permitted sender) smtp.mailfrom=bounce@groups.io From: hanliyang BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=4807 In the case of launch a SEV-ES VM with just OVMF_CODE.fd, the validation process in PlatformValidateNvVarStore will trigger MMIO NPF, and the #VC handler will detect that mmio access is invalid because the mmio address range of FlashNvVarStore is mapped as encrypted. In the case of launch a SEV VM with both OVMF_CODE.fd and OVMF_VARS.fd, PlatformValidateNvVarStore will fail to validate FlashNvVarStore because the mapping of FlashNvVarStore address range is encrypted in the guest but the corresponding data in system physical memory was not encrypted by guest key. We should map FlashNvVarStore address range as unencrypted for the above cases. Fixes: 4f173db8b45b ("OvmfPkg/PlatformInitLib: Add functions for EmuVariableNvStore") Signed-off-by: hanliyang --- OvmfPkg/PlatformPei/AmdSev.c | 105 ++++++++++++++++++++++++++++ OvmfPkg/PlatformPei/Platform.c | 6 ++ OvmfPkg/PlatformPei/Platform.h | 6 ++ OvmfPkg/PlatformPei/PlatformPei.inf | 1 + 4 files changed, 118 insertions(+) diff --git a/OvmfPkg/PlatformPei/AmdSev.c b/OvmfPkg/PlatformPei/AmdSev.c index 8562787035..89f4c02b6a 100644 --- a/OvmfPkg/PlatformPei/AmdSev.c +++ b/OvmfPkg/PlatformPei/AmdSev.c @@ -575,3 +575,108 @@ SevInitializeRam ( ); } } + +/** + Prepared for FlashNvVarStore access. + + **/ +VOID +SevFlashNvVarStoreUpdateMapping ( + IN UINTN NvVarStoreBase, + IN UINTN NvVarStoreSize + ) +{ + volatile UINT8 *Ptr; + + RETURN_STATUS DecEncStatus; + UINT8 Offset; + + DEBUG ((DEBUG_INFO, "%a\n", __func__)); + + if (!MemEncryptSevIsEnabled ()) { + return; + } + + // + // In the case of launch a SEV-ES VM with just OVMF_CODE.fd, the + // validation process in PlatformValidateNvVarStore will trigger MMIO + // NPF, and the #VC handler will detect that mmio access is invalid + // because the mmio address range of FlashNvVarStore is mapped as + // encrypted. So, first we should update the mapping of address range + // of FlashNvVarStore as decrypted. + // + DEBUG ( + ( + DEBUG_INFO, + "%a: mapping FlashNvVarStore address range unencrypted [0x%p - 0x%p]\n", + __func__, + (UINT8 *)NvVarStoreBase, + (UINT8 *)NvVarStoreBase + NvVarStoreSize - 1 + ) + ); + + DecEncStatus = MemEncryptSevClearMmioPageEncMask ( + 0, + NvVarStoreBase, + EFI_SIZE_TO_PAGES (NvVarStoreSize) + ); + if (RETURN_ERROR (DecEncStatus)) { + DEBUG ( + ( + DEBUG_ERROR, + "%a: failed to map FlashNvStorage address range unencrypted\n", + __func__ + ) + ); + ASSERT_RETURN_ERROR (DecEncStatus); + } + + // + // Here, the first 16 bytes of FlashNvVarStore will be all zeros in + // the following cases: + // a. Launch VM with just OVMF_CODE.fd + // b. Launch VM with OVMF_CODE.fd and OVMF_VARS.fd + // In these cases, the access of FlashNvVarStore will be as expected. + // + // But if launch VM with just OVMF.fd, the first 16 bytes of + // FlashNvVarStore will be scrambled data because the data of + // FlashNvVarStore are encrypted by SEV API. In this case, we need + // mapping FlashNvVarStore address range as encrypted again, otherwise + // the validation of FlashNvVarStore will fail and trigger + // ASSERT (FALSE). + // + for (Offset = 0; Offset < 16; Offset++) { + Ptr = (UINT8 *)NvVarStoreBase + Offset; + if (*Ptr) { + break; + } + } + + if (Offset == 16) { + return; + } + + DEBUG ( + ( + DEBUG_INFO, + "%a: mapping FlashNvStorage address range encrypted\n", + __func__ + ) + ); + + DecEncStatus = MemEncryptSevSetPageEncMask ( + 0, + NvVarStoreBase, + EFI_SIZE_TO_PAGES (NvVarStoreSize) + ); + if (RETURN_ERROR (DecEncStatus)) { + DEBUG ( + ( + DEBUG_ERROR, + "%a: failed to map FlashNvStorage address range encrypted\n", + __func__ + ) + ); + ASSERT_RETURN_ERROR (DecEncStatus); + } +} diff --git a/OvmfPkg/PlatformPei/Platform.c b/OvmfPkg/PlatformPei/Platform.c index 0114529778..bcb18dacac 100644 --- a/OvmfPkg/PlatformPei/Platform.c +++ b/OvmfPkg/PlatformPei/Platform.c @@ -224,6 +224,12 @@ ReserveEmuVariableNvStore ( PcdStatus = PcdSet64S (PcdEmuVariableNvStoreReserved, VariableStore); if (FeaturePcdGet (PcdSecureBootSupported)) { + // update mapping of FlashNvVarStore address range + SevFlashNvVarStoreUpdateMapping ( + PcdGet32 (PcdOvmfFlashNvStorageVariableBase), + 2 * PcdGet32 (PcdFlashNvStorageFtwSpareSize) + ); + // restore emulated VarStore from pristine ROM copy PlatformInitEmuVariableNvStore ((VOID *)(UINTN)VariableStore); } diff --git a/OvmfPkg/PlatformPei/Platform.h b/OvmfPkg/PlatformPei/Platform.h index 0a59547cfc..b8ad8df1dc 100644 --- a/OvmfPkg/PlatformPei/Platform.h +++ b/OvmfPkg/PlatformPei/Platform.h @@ -111,4 +111,10 @@ SevInitializeRam ( VOID ); +VOID +SevFlashNvVarStoreUpdateMapping ( + IN UINTN NvVarStoreBase, + IN UINTN NvVarStoreSize + ); + #endif // _PLATFORM_PEI_H_INCLUDED_ diff --git a/OvmfPkg/PlatformPei/PlatformPei.inf b/OvmfPkg/PlatformPei/PlatformPei.inf index 0bb1a46291..a3dd3db72d 100644 --- a/OvmfPkg/PlatformPei/PlatformPei.inf +++ b/OvmfPkg/PlatformPei/PlatformPei.inf @@ -139,6 +139,7 @@ gUefiOvmfPkgTokenSpaceGuid.PcdOvmfWorkAreaSize gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSnpSecretsBase gUefiOvmfPkgTokenSpaceGuid.PcdOvmfSnpSecretsSize + gUefiOvmfPkgTokenSpaceGuid.PcdOvmfFlashNvStorageVariableBase [FeaturePcd] gUefiOvmfPkgTokenSpaceGuid.PcdSmmSmramRequire -- 2.25.1 -=-=-=-=-=-=-=-=-=-=-=- Groups.io Links: You receive all messages sent to this group. View/Reply Online (#119929): https://edk2.groups.io/g/devel/message/119929 Mute This Topic: https://groups.io/mt/107212944/7686176 Group Owner: devel+owner@edk2.groups.io Unsubscribe: https://edk2.groups.io/g/devel/unsub [rebecca@openfw.io] -=-=-=-=-=-=-=-=-=-=-=-