From nobody Wed May 8 09:52:59 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+97158+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+97158+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1670568062; cv=none; d=zohomail.com; s=zohoarc; b=Vjhsnsh7OAJ5xotJ79w0H3AKwUtK/IDZ6Eac+4meu1S7Snol5/rPapiu1x9oxJUSb6+I1aBzNw2gNpB528ZTsk8Cg3UtxK5qhTAx6lM3aouFPMn93yhhSSCk3+IcaE/HhJ8ed2jWFndTosb0+mE67WioVA755SZwSWseVqpENyY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1670568062; h=Content-Type:Cc:Date:From:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Sender:Subject:To; bh=df847Iny5Bi+eUpDdEfl2VtuvkRd6U3omdpPW/uX0FA=; b=N3Y/5RIjU6zxQ3DmZZTFN/ocFp4woDIkcwF8U1rbmdVd21/myvHQzKL25O2u42WQ5CWblvpAyepjSbECKcyhgqB9+9xgNkgoEGYcNin2Oqx9HrUT/Iy2mQPgHQ4KGr16dgmDDQqrBbo/5fIUTw++xp1ym00Wl8dZ95av+fqATDU= 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+97158+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1670568062445519.1291450813197; Thu, 8 Dec 2022 22:41:02 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id hAGKYY1788612xFxS6ZWs4yQ; Thu, 08 Dec 2022 22:41:01 -0800 X-Received: from mail-pf1-f201.google.com (mail-pf1-f201.google.com [209.85.210.201]) by mx.groups.io with SMTP id smtpd.web10.6045.1670543095069963485 for ; Thu, 08 Dec 2022 15:44:55 -0800 X-Received: by mail-pf1-f201.google.com with SMTP id b13-20020a056a000a8d00b0057348c50123so2086248pfl.18 for ; Thu, 08 Dec 2022 15:44:54 -0800 (PST) X-Gm-Message-State: rs6x6Qj8BXzJsy7TC5pEa73sx1787277AA= X-Google-Smtp-Source: AA0mqf7Sromy13SRRDL1DPC6ESme4zLmyjmROv4Fgf1Voru8n5PzgDrB+ZfSyEJ5gl9n/FYUMn80Ja5x4i1bHQ== X-Received: from acdunlap03.bve.corp.google.com ([2620:0:1008:11:3835:4494:83a9:5841]) (user=acdunlap job=sendgmr) by 2002:a62:3347:0:b0:574:d1f9:9b16 with SMTP id z68-20020a623347000000b00574d1f99b16mr60569544pfz.65.1670543094260; Thu, 08 Dec 2022 15:44:54 -0800 (PST) Date: Thu, 8 Dec 2022 15:43:36 -0800 Mime-Version: 1.0 Message-ID: Subject: [edk2-devel] [PATCH] OvmfPkg/PlatformPei: Validate SEC's GHCB page From: "Adam Dunlap via groups.io" To: devel@edk2.groups.io Cc: 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=1670568061; bh=uoWX770LqUonD6lAjInZSaKxzIgcy7c5CpvnUswnUc0=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=NE8tYIEfDX7gLBrzY88aM/zHXKYaYX0nvkNTF+hrsaRc85M5ag2mWhordGMTpTPIfVM W2CNf4WldSQUeT24zvMCZvhyvbJ4oIwAYEG3a7gdQVfkrPaJiiuliJk078LVeu0QIYjTu whRCBefVArqmAyBihLSgMElYz90HZDaUT/4= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1670568063615100004 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 bytes of each page of memory and checks to see if a #VC exception is generated indicating that the page was not validated. Signed-off-by: Adam Dunlap --- OvmfPkg/PlatformPei/AmdSev.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/OvmfPkg/PlatformPei/AmdSev.c b/OvmfPkg/PlatformPei/AmdSev.c index e1b9fd9b7f..c465732068 100644 --- a/OvmfPkg/PlatformPei/AmdSev.c +++ b/OvmfPkg/PlatformPei/AmdSev.c @@ -206,6 +206,7 @@ AmdSevEsInitialize ( { UINT8 *GhcbBase; PHYSICAL_ADDRESS GhcbBasePa; + PHYSICAL_ADDRESS PrevGhcbPa; UINTN GhcbPageCount; UINT8 *GhcbBackupBase; UINT8 *GhcbBackupPages; @@ -293,8 +294,24 @@ AmdSevEsInitialize ( GhcbRegister (GhcbBasePa); } =20 + PrevGhcbPa =3D AsmReadMsr64 (MSR_SEV_ES_GHCB); + 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. + // + ASSERT (PrevGhcbPa =3D=3D FixedPcdGet32(PcdOvmfSecGhcbBase)); + + ASSERT_RETURN_ERROR(MemEncryptSevSetPageEncMask( + 0 /*Cr3 -- use system Cr3*/, + PrevGhcbPa, + 1 /*Number of pages*/)); + // // 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 (#97158): https://edk2.groups.io/g/devel/message/97158 Mute This Topic: https://groups.io/mt/95556055/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-