From nobody Fri May 3 19:37:33 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+97206+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+97206+1787277+3901457@groups.io ARC-Seal: i=1; a=rsa-sha256; t=1670612332; cv=none; d=zohomail.com; s=zohoarc; b=HyqTEkU8kc3jMwhNobJRS1D2f/x+F769EsQsR2qXsr+ws1oSxqWTPbUwX8erx5RIblmzcPRoVm3pzKUnGJdqoJXaZv6QKmQg3hejYAsrCwdZSuQNN2WcCbTGIDsMedPiAM3G8raJpI/E1Q62hPdcmKRiK777DV5OS+zn6WNwy/g= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1670612332; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:References:Sender:Subject:To; bh=9cXhlEhnP9XPyVAsaxg8UwJDM/sA4EWqAl44cMnDr3I=; b=Lq9AbdggWI4kFTFqFY/qS5DPW/GBGBva5+QY6WRw/o3oTmQ6vJegkSVCDbFG/xSHXvExEr0r+QYvZxn/s4Uvqmrx7R5CwkoRnLBhxV1zI3LoVKFZS4cz/8PG4mo8GHbgRDIdMcs1OG7l2xDCZ1JRODhIOxgVf6uo4Nn6oNB7ZDo= 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+97206+1787277+3901457@groups.io Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1670612332435580.54812650603; Fri, 9 Dec 2022 10:58:52 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id t3FLYY1788612xaQgEwzJeBr; Fri, 09 Dec 2022 10:58:52 -0800 X-Received: from mail-pl1-f201.google.com (mail-pl1-f201.google.com [209.85.214.201]) by mx.groups.io with SMTP id smtpd.web11.1243.1670612331242607378 for ; Fri, 09 Dec 2022 10:58:51 -0800 X-Received: by mail-pl1-f201.google.com with SMTP id b17-20020a170903229100b00189da3b178bso4847936plh.7 for ; Fri, 09 Dec 2022 10:58:51 -0800 (PST) X-Gm-Message-State: PacVrfBI5dSxNtF68ftx9lTLx1787277AA= X-Google-Smtp-Source: AA0mqf6/VHEIh6BW7wA1PibREytTkPA3tteWmxxSWeaomvaA9iQdWjsDs983FfrYhUtPbGN+CReYxXdEmUxFLg== X-Received: from acdunlap03.bve.corp.google.com ([2620:0:1008:11:9c55:ddcf:a880:90f3]) (user=acdunlap job=sendgmr) by 2002:a05:6a00:a88:b0:574:2c5e:b18c with SMTP id b8-20020a056a000a8800b005742c5eb18cmr74165102pfl.10.1670612330392; Fri, 09 Dec 2022 10:58:50 -0800 (PST) Date: Fri, 9 Dec 2022 10:58:41 -0800 In-Reply-To: Mime-Version: 1.0 References: Message-ID: <9b9c4946b7b825d2dc6ba2d6aab9ea703db81bf7.1670611881.git.acdunlap@google.com> Subject: [edk2-devel] [PATCH v2] 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=1670612332; bh=xhj+MWZKSKFpgatJ3hBvcsttW9387UVIYotWjEBMMWU=; h=Cc:Content-Type:Date:From:Reply-To:Subject:To; b=P5pmpRwrXZ62oy67zmcI6Jb/e9/VeJr6cyOX9Wju3+ipBEqysATYBf5wwFJaqg+3o0u x8IwqFEAxFAqX4DbmIcYz1QLX7NKN57i7g3FNMyMrk+cvwy2qXGE8excnrwSWnonzhthh sKyGtxnyIKzUHoJwVe26HCXlG4RR9F6Y6Zo= X-ZohoMail-DKIM: pass (identity @groups.io) X-ZM-MESSAGEID: 1670612332783100001 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 --- OvmfPkg/PlatformPei/AmdSev.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) diff --git a/OvmfPkg/PlatformPei/AmdSev.c b/OvmfPkg/PlatformPei/AmdSev.c index e1b9fd9b7f..df560a8679 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 PcdStatus, Status; IA32_DESCRIPTOR Gdtr; VOID *Gdt; =20 @@ -240,12 +240,12 @@ AmdSevEsInitialize ( // only clear the encryption mask for the GHCB pages. // for (PageCount =3D 0; PageCount < GhcbPageCount; PageCount +=3D 2) { - DecryptStatus =3D MemEncryptSevClearPageEncMask ( + Status =3D MemEncryptSevClearPageEncMask ( 0, GhcbBasePa + EFI_PAGES_TO_SIZE (PageCount), 1 ); - ASSERT_RETURN_ERROR (DecryptStatus); + ASSERT_RETURN_ERROR (Status); } =20 ZeroMem (GhcbBase, EFI_PAGES_TO_SIZE (GhcbPageCount)); @@ -295,6 +295,21 @@ 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), + 1 // Number of pages + ); + 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 (#97206): https://edk2.groups.io/g/devel/message/97206 Mute This Topic: https://groups.io/mt/95567040/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-