From nobody Mon Apr 29 05:56:09 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+97213+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+97213+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1670619871; cv=none; d=zohomail.com; s=zohoarc; b=WCLvDZJgLfLhSgUCR7KmmqyGv8P725xVBqocUCto6ZwAtRHT2eNKVMRslAvaEUjP0TzZTxHOy7ABp1LPwms60awhhrAFVwm5px4DbPpGaOCV9NSCtTS3OPLLJtX3drU4jqaER633WucVpJjMmQdgJ/CULVY1AoJhBPoSsJoPVZY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1670619871; h=Content-Type:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=yhInNQYSZQCzwy7OCXOlAaibeJjshUClR4V3IkXI/kg=; b=YyrbTVQAkODh3LUU34bDztw8QUKSt96Ol0aSdhkjgJB5TkC1537gZxTvbt51lDys94nJ8OwDoejiaKw9rxNhr4mZgMXL2ljCsSaaQdHKBn74sT9oz+/Iy4u3LR8wf1bsvciJG+y9PUDlX4TE3JrH1/+w+FDAWugGQdFLPmafxLw= 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+97213+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1670619871124924.4621253521738; Fri, 9 Dec 2022 13:04:31 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id MRuzYY1788612xmZmGzNJPMx; Fri, 09 Dec 2022 13:04:30 -0800 X-Received: from mail-pj1-f74.google.com (mail-pj1-f74.google.com [209.85.216.74]) by mx.groups.io with SMTP id smtpd.web10.4725.1670619869768464730 for ; Fri, 09 Dec 2022 13:04:29 -0800 X-Received: by mail-pj1-f74.google.com with SMTP id z12-20020a17090abd8c00b0021a0a65a7e2so7199122pjr.7 for ; Fri, 09 Dec 2022 13:04:29 -0800 (PST) X-Gm-Message-State: fRPNEKgiZXg0GMiJRO9KNQuox1787277AA= X-Google-Smtp-Source: AA0mqf65o2cE9ECqlCIhK5QmGAut+6rivohqXhZY6sHgfFmBYzKSvnrNXKtnEWcbCIEssZ3QAYCTpyQJfY/BwA== X-Received: from acdunlap03.bve.corp.google.com ([2620:0:1008:11:9c55:ddcf:a880:90f3]) (user=acdunlap job=sendgmr) by 2002:a17:902:ebc1:b0:176:a6c5:20c9 with SMTP id p1-20020a170902ebc100b00176a6c520c9mr34463120plg.57.1670619869008; Fri, 09 Dec 2022 13:04:29 -0800 (PST) Date: Fri, 9 Dec 2022 13:04:16 -0800 Mime-Version: 1.0 Message-ID: Subject: [edk2-devel] [PATCH v3] OvmfPkg/PlatformPei: Validate SEC's GHCB page From: "Adam Dunlap via groups.io" To: devel@edk2.groups.io Cc: Ard Biesheuvel , Jiewen Yao , Jordan Justen , Gerd Hoffmann , Brijesh Singh , Erdem Aktas , James Bottomley , Min Xu , Tom Lendacky , Dionna Glaze , Adam Dunlap 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,acdunlap@google.com DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1670619870; bh=O6mNpgE9RXB6vJjwSlDGsbbq+qeFVGJhX5PxtQra41o=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=FEe5EiqXXEb1MZrYu0gEoiIRnQj/eZqURjEbZfQ4LqJI8Jdg5c7xwzEESBPv9gFTmLs bluKJr6HFWR+I5q71vLwPacYROvvLngHSo3ihiF7gqCem0292YxpyDbpQx1aVWVOYRXBG qgWDGNbp6Ex2xh7fOpQcpCkfXgDASExXVvY= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1670619873252100003 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" When running under SEV-ES, a page of shared memory is allocated for the GHCB during the SEC phase at address 0x809000. This page of memory is eventually passed to the OS as EfiConventionalMemory. When running SEV-SNP, this page is not PVALIDATE'd in the RMP table, meaning that if the guest OS tries to access the page, it will think that the host has voilated the security guarantees and will likely crash. This patch validates this page immediately after EDK2 switches to using the GHCB page allocated for the PEI phase. This was tested by writing a UEFI application that reads to and writes from one byte of each page of memory and checks to see if a #VC exception is generated indicating that the page was not validated. Fixes: 6995a1b79bab ("OvmfPkg: Create a GHCB page for use during Sec phase") Signed-off-by: Adam Dunlap Reviewed-by: Tom Lendacky --- Removed the PcdStatus variable and just use Status for all statuses in this function. Use uncrustify to fix some formatting errors. OvmfPkg/PlatformPei/AmdSev.c | 40 ++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 13 deletions(-) diff --git a/OvmfPkg/PlatformPei/AmdSev.c b/OvmfPkg/PlatformPei/AmdSev.c index e1b9fd9b7f..b2f2f3ac26 100644 --- a/OvmfPkg/PlatformPei/AmdSev.c +++ b/OvmfPkg/PlatformPei/AmdSev.c @@ -212,7 +212,7 @@ AmdSevEsInitialize ( UINTN GhcbBackupPageCount; SEV_ES_PER_CPU_DATA *SevEsData; UINTN PageCount; - RETURN_STATUS PcdStatus, DecryptStatus; + RETURN_STATUS Status; IA32_DESCRIPTOR Gdtr; VOID *Gdt; =20 @@ -220,8 +220,8 @@ AmdSevEsInitialize ( return; } =20 - PcdStatus =3D PcdSetBoolS (PcdSevEsIsEnabled, TRUE); - ASSERT_RETURN_ERROR (PcdStatus); + Status =3D PcdSetBoolS (PcdSevEsIsEnabled, TRUE); + ASSERT_RETURN_ERROR (Status); =20 // // Allocate GHCB and per-CPU variable pages. @@ -240,20 +240,20 @@ AmdSevEsInitialize ( // only clear the encryption mask for the GHCB pages. // for (PageCount =3D 0; PageCount < GhcbPageCount; PageCount +=3D 2) { - DecryptStatus =3D MemEncryptSevClearPageEncMask ( - 0, - GhcbBasePa + EFI_PAGES_TO_SIZE (PageCount), - 1 - ); - ASSERT_RETURN_ERROR (DecryptStatus); + Status =3D MemEncryptSevClearPageEncMask ( + 0, + GhcbBasePa + EFI_PAGES_TO_SIZE (PageCount), + 1 + ); + ASSERT_RETURN_ERROR (Status); } =20 ZeroMem (GhcbBase, EFI_PAGES_TO_SIZE (GhcbPageCount)); =20 - PcdStatus =3D PcdSet64S (PcdGhcbBase, GhcbBasePa); - ASSERT_RETURN_ERROR (PcdStatus); - PcdStatus =3D PcdSet64S (PcdGhcbSize, EFI_PAGES_TO_SIZE (GhcbPageCount)); - ASSERT_RETURN_ERROR (PcdStatus); + Status =3D PcdSet64S (PcdGhcbBase, GhcbBasePa); + ASSERT_RETURN_ERROR (Status); + Status =3D PcdSet64S (PcdGhcbSize, EFI_PAGES_TO_SIZE (GhcbPageCount)); + ASSERT_RETURN_ERROR (Status); =20 DEBUG (( DEBUG_INFO, @@ -295,6 +295,20 @@ AmdSevEsInitialize ( =20 AsmWriteMsr64 (MSR_SEV_ES_GHCB, GhcbBasePa); =20 + // + // Now that the PEI GHCB is set up, the SEC GHCB page is no longer neces= sary + // to keep shared. Later, it is exposed to the OS as EfiConventionalMemo= ry, so + // it needs to be marked private. The size of the region is hardcoded in + // OvmfPkg/ResetVector/ResetVector.nasmb in the definition of + // SNP_SEC_MEM_BASE_DESC_2. + // + Status =3D MemEncryptSevSetPageEncMask ( + 0, // Cr3 -- use system Cr3 + FixedPcdGet32 (PcdOvmfSecGhcbBase), // BaseAddress + 1 // NumPages + ); + ASSERT_RETURN_ERROR (Status); + // // The SEV support will clear the C-bit from non-RAM areas. The early G= DT // lives in a non-RAM area, so when an exception occurs (like a #VC) the= GDT --=20 2.39.0.rc1.256.g54fd8350bd-goog -=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 (#97213): https://edk2.groups.io/g/devel/message/97213 Mute This Topic: https://groups.io/mt/95569639/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-