From nobody Mon Nov 25 13:31:35 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+69861+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+69861+1787277+3901457@groups.io; arc=fail (BodyHash is different from the expected one); dmarc=fail(p=none dis=none) header.from=amd.com Received: from mail02.groups.io (mail02.groups.io [66.175.222.108]) by mx.zohomail.com with SMTPS id 1609968162907804.99222188246; Wed, 6 Jan 2021 13:22:42 -0800 (PST) Return-Path: X-Received: by 127.0.0.2 with SMTP id iNubYY1788612x5Sna33knge; Wed, 06 Jan 2021 13:22:42 -0800 X-Received: from NAM11-DM6-obe.outbound.protection.outlook.com (NAM11-DM6-obe.outbound.protection.outlook.com [40.107.223.82]) by mx.groups.io with SMTP id smtpd.web12.488.1609968162089265518 for ; Wed, 06 Jan 2021 13:22:42 -0800 ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=RXB1jThLtGYoSZyA6lqOzdC+yTcIwDwdNUWFuk+J1QfWa9qbErUV9cTqNQKHKL5sQLZ0xL/x8OD4vDvUZG6kixK9nIAdZo/OfGySPccWayDhJxDd5zPoFRgTyBpJC0viYF1A9YKJfoab0VQyHOLbFaF04dibFHsFZUli4IkRt271nIWddUMOWsuFBH29fIUDZIidDfb8RLC//6HWWJU0IAwJpvUo/PosW1tVwM2j3f3rIsm1j02tF9NYrybgNabc6yvWvlMJA4d20pwmzkcNJBwByHiDRBjiHdrNwDiW9siH89fnHEUP6LRO8d26BR298PV03z70uBkF28iHK2OLIA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=microsoft.com; s=arcselector9901; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=Uy4MNSu+RZ4EyvSqVuS56xXhFd+OKhnER+BJ1Mwx7vs=; b=aSqPxPBjsWLwkZBAV3hZP+btbHA3ZGPMhhABblAm0ivw/w2iSMBIaFyy+wdQrMnHXIYyZWzgwB4d9Xk5hc5R6VXdofBzj8/EqjaL3cPrp21nTgu+FtNMYhoT7/t8Hnf0YOAZ6lWqHqDHgXloX2tpgup6Uc+fxYMAEM/z+bTwdGtRHiwcdDpMyW2oCLrJ9lm60CU05yN6m/VoBzS1kuLjxLPINcY5KOlCbFAFtkRBBCEsCzeerAIQ11qUkRFC3vjZVB+GFPyPaeilZoQz3QPf2nq35/aOaqJ1DaXfQGqQO3U3duhpttFL/rzNTwwd5k2WMWP404y67wm+8lWyg07PNA== ARC-Authentication-Results: i=1; mx.microsoft.com 1; spf=pass smtp.mailfrom=amd.com; dmarc=pass action=none header.from=amd.com; dkim=pass header.d=amd.com; arc=none X-Received: from DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) by DM6PR12MB3578.namprd12.prod.outlook.com (2603:10b6:5:3c::33) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3721.24; Wed, 6 Jan 2021 21:22:39 +0000 X-Received: from DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::d95e:b9d:1d6a:e845]) by DM5PR12MB1355.namprd12.prod.outlook.com ([fe80::d95e:b9d:1d6a:e845%12]) with mapi id 15.20.3721.024; Wed, 6 Jan 2021 21:22:39 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io CC: Brijesh Singh , James Bottomley , Jordan Justen , Laszlo Ersek , Ard Biesheuvel Subject: [edk2-devel] [PATCH v2 03/15] OvmfPkg/ResetVector: Validate the encryption bit position for SEV/SEV-ES Date: Wed, 6 Jan 2021 15:21:29 -0600 Message-ID: <1bc8d8d9f1d6ad1e3640ae0df955087f073dbec2.1609968101.git.thomas.lendacky@amd.com> In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SA0PR11CA0055.namprd11.prod.outlook.com (2603:10b6:806:d0::30) To DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 X-Received: from tlendack-t1.amd.com (165.204.77.1) by SA0PR11CA0055.namprd11.prod.outlook.com (2603:10b6:806:d0::30) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6 via Frontend Transport; Wed, 6 Jan 2021 21:22:38 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: cc078590-a182-4644-dc81-08d8b2893230 X-MS-TrafficTypeDiagnostic: DM6PR12MB3578: X-MS-Exchange-Transport-Forked: True X-Microsoft-Antispam-PRVS: X-MS-Oob-TLC-OOBClassifiers: OLM:5236; X-MS-Exchange-SenderADCheck: 1 X-Microsoft-Antispam-Message-Info: 24rojy+ux8WzxtKM2VjrV6Mz7+nLn2h08tCXtON+gd11ISkKwnqERQ92uLGgAzTZvOI14og34uQ5h1QjFoSyTGsq5fhpfAnSKUm8c8MmVYUOxuwfOk4kXE8kweARdSaVvZiV1gSLI81U0Z5JeNOzAHrIpefQj1Obj9KZLTou3zCIU3deFr0TsGuccgo/A4fibFIt2dK1TWOC8kAX8Bv0gauIzg8DzPGF5Ig1FFgrL5AZdmZi8rxzWyoopcUfSSNunZ0CQtSN/n4oMm25CKYxbB/OsyJfXrDQiimuk3MhlGqFreBsf4pJyY+uFakdmDLg6dmZD6W/QLPNW7nGx8PgjtSDFEc9y8m2XjeMyMnEO9jsfvBwlgA+nQnSWEc2lYUQGuZzK4iegijQr2YJ7K1mv1GTHnxXOo+B+J72Xq4CD+5LUJ5XL+FKwYDnF0A/DxLVeKzFkasRc/cotiq63Kn7iA== X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?+U6TRKB2uRzwSS+fdwypW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7vQ9c?= =?us-ascii?Q?x2QZSpTCB6sQsK3Sz9b/XYfFtHlDdvIWPhWRrwlMdOu5mUWNDgZCoCcpuAxY?= =?us-ascii?Q?yLL9okQmAUYoC4B+cAt+TlkFqn/bFnRtVuDvQXE2jyGDgRvX91XOCEqykS+x?= =?us-ascii?Q?GVSP?= X-OriginatorOrg: amd.com X-MS-Exchange-CrossTenant-AuthSource: DM5PR12MB1355.namprd12.prod.outlook.com X-MS-Exchange-CrossTenant-AuthAs: Internal X-MS-Exchange-CrossTenant-OriginalArrivalTime: 06 Jan 2021 21:22:39.3602 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-Network-Message-Id: cc078590-a182-4644-dc81-08d8b2893230 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: DHcy0ui8yrUU/RTEoQDG/ysL9wY8RQHYlNGKfD+wbB9vgEq7+vuA5PXtWA0Folg8QmI+16BTere4Mp5o/NOc4A== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM6PR12MB3578 Precedence: Bulk List-Unsubscribe: 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,thomas.lendacky@amd.com X-Gm-Message-State: YGfJNPhMzBVHSuRBGmsFMLqnx1787277AA= Content-Transfer-Encoding: quoted-printable DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=groups.io; q=dns/txt; s=20140610; t=1609968162; bh=li6Jfdgr3z1OFZv0OsGbOVS4sUsw3nCp40pm0ydJtJ4=; h=CC:Content-Type:Date:From:Reply-To:Subject:To; b=bQb/cFCSSKiHdTk0owAG+B38QsJwI1I8gxYoCZbcj/MYDosW55cTEmWOiqRPx6wP3BE TSP4uhj/kkkJg+XLmfvTj4ANzo8ikzW2ja6ewBR8XSm76TgZaNEuIn1837aMKA/EYk/mX mGM3iFufhwL2BLkrr4NZMHpRGm5YlvL8quw= X-ZohoMail-DKIM: pass (identity @groups.io) Content-Type: text/plain; charset="utf-8" From: Tom Lendacky BZ: https://bugzilla.tianocore.org/show_bug.cgi?id=3D3108 To help mitigate against ROP attacks, add some checks to validate the encryption bit position that is reported by the hypervisor. The first check is to ensure that the hypervisor reports a bit position above bit 31. After extracting the encryption bit position from the CPUID information, the code checks that the value is above 31. If the value is not above 31, then the bit position is not valid, so the code enters a HLT loop. The second check is specific to SEV-ES guests and is a two step process. The first step will obtain random data using RDRAND and store that data to memory before paging is enabled. When paging is not enabled, all writes to memory are encrypted. The random data is maintained in registers, which are protected. The second step is that, after enabling paging, the random data in memory is compared to the register contents. If they don't match, then the reported bit position is not valid, so the code enters a HLT loop. The third check is after switching to 64-bit long mode. Use the fact that instruction fetches are automatically decrypted, while a memory fetch is decrypted only if the encryption bit is set in the page table. By comparing the bytes of an instruction fetch against a memory read of that same instruction, the encryption bit position can be validated. If the compare is not equal, then SEV/SEV-ES is active but the reported bit position is not valid, so the code enters a HLT loop. To keep the changes local to the OvmfPkg, an OvmfPkg version of the Flat32ToFlat64.asm file has been created based on the UefiCpuPkg file UefiCpuPkg/ResetVector/Vtf0/Ia32/Flat32ToFlat64.asm. Cc: Jordan Justen Cc: Laszlo Ersek Cc: Ard Biesheuvel Cc: Brijesh Singh Signed-off-by: Tom Lendacky Reviewed-by: Laszlo Ersek --- OvmfPkg/Include/Library/MemEncryptSevLib.h | 4 + OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm | 118 ++++++++++++++++++++ OvmfPkg/ResetVector/Ia32/PageTables64.asm | 13 ++- OvmfPkg/ResetVector/ResetVector.nasmb | 4 +- 4 files changed, 136 insertions(+), 3 deletions(-) diff --git a/OvmfPkg/Include/Library/MemEncryptSevLib.h b/OvmfPkg/Include/L= ibrary/MemEncryptSevLib.h index a6d82dac7fac..dc09c61e58bb 100644 --- a/OvmfPkg/Include/Library/MemEncryptSevLib.h +++ b/OvmfPkg/Include/Library/MemEncryptSevLib.h @@ -21,10 +21,14 @@ // This structure is also used by assembler files: // OvmfPkg/ResetVector/ResetVector.nasmb // OvmfPkg/ResetVector/Ia32/PageTables64.asm +// OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm // any changes must stay in sync with its usage. // typedef struct _SEC_SEV_ES_WORK_AREA { UINT8 SevEsEnabled; + UINT8 Reserved1[7]; + + UINT64 RandomData; } SEC_SEV_ES_WORK_AREA; =20 /** diff --git a/OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm b/OvmfPkg/ResetVec= tor/Ia32/Flat32ToFlat64.asm new file mode 100644 index 000000000000..c6d0d898bcd1 --- /dev/null +++ b/OvmfPkg/ResetVector/Ia32/Flat32ToFlat64.asm @@ -0,0 +1,118 @@ +;-------------------------------------------------------------------------= ----- +; @file +; Transition from 32 bit flat protected mode into 64 bit flat protected mo= de +; +; Copyright (c) 2008 - 2018, Intel Corporation. All rights reserved.
+; Copyright (c) 2020, Advanced Micro Devices, Inc. All rights reserved.
+; SPDX-License-Identifier: BSD-2-Clause-Patent +; +;-------------------------------------------------------------------------= ----- + +BITS 32 + +; +; Modified: EAX, ECX, EDX +; +Transition32FlatTo64Flat: + + OneTimeCall SetCr3ForPageTables64 + + mov eax, cr4 + bts eax, 5 ; enable PAE + mov cr4, eax + + mov ecx, 0xc0000080 + rdmsr + bts eax, 8 ; set LME + wrmsr + + ; + ; SEV-ES mitigation check support + ; + xor ebx, ebx + + cmp byte[SEV_ES_WORK_AREA], 0 + jz EnablePaging + + ; + ; SEV-ES is active, perform a quick sanity check against the reported + ; encryption bit position. This is to help mitigate against attacks wh= ere + ; the hypervisor reports an incorrect encryption bit position. + ; + ; This is the first step in a two step process. Before paging is enabl= ed + ; writes to memory are encrypted. Using the RDRAND instruction (availa= ble + ; on all SEV capable processors), write 64-bits of random data to the + ; SEV_ES_WORK_AREA and maintain the random data in registers (register + ; state is protected under SEV-ES). This will be used in the second st= ep. + ; +RdRand1: + rdrand ecx + jnc RdRand1 + mov dword[SEV_ES_WORK_AREA_RDRAND], ecx +RdRand2: + rdrand edx + jnc RdRand2 + mov dword[SEV_ES_WORK_AREA_RDRAND + 4], edx + + ; + ; Use EBX instead of the SEV_ES_WORK_AREA memory to determine whether = to + ; perform the second step. + ; + mov ebx, 1 + +EnablePaging: + mov eax, cr0 + bts eax, 31 ; set PG + mov cr0, eax ; enable paging + + jmp LINEAR_CODE64_SEL:ADDR_OF(jumpTo64BitAndLandHere) +BITS 64 +jumpTo64BitAndLandHere: + + ; + ; Check if the second step of the SEV-ES mitigation is to be performed. + ; + test ebx, ebx + jz InsnCompare + + ; + ; SEV-ES is active, perform the second step of the encryption bit post= ion + ; mitigation check. The ECX and EDX register contain data from RDRAND = that + ; was stored to memory in encrypted form. If the encryption bit positi= on is + ; valid, the contents of ECX and EDX will match the memory location. + ; + cmp dword[SEV_ES_WORK_AREA_RDRAND], ecx + jne SevEncBitHlt + cmp dword[SEV_ES_WORK_AREA_RDRAND + 4], edx + jne SevEncBitHlt + + ; + ; If SEV or SEV-ES is active, perform a quick sanity check against + ; the reported encryption bit position. This is to help mitigate + ; against attacks where the hypervisor reports an incorrect encryption + ; bit position. If SEV is not active, this check will always succeed. + ; + ; The cmp instruction compares the first four bytes of the cmp instruc= tion + ; itself (which will be read decrypted if SEV or SEV-ES is active and = the + ; encryption bit position is valid) against the immediate within the + ; instruction (an instruction fetch is always decrypted correctly by + ; hardware) based on RIP relative addressing. + ; +InsnCompare: + cmp dword[rel InsnCompare], 0xFFF63D81 + je GoodCompare + + ; + ; The hypervisor provided an incorrect encryption bit position, do not + ; proceed. + ; +SevEncBitHlt: + cli + hlt + jmp SevEncBitHlt + +GoodCompare: + debugShowPostCode POSTCODE_64BIT_MODE + + OneTimeCallRet Transition32FlatTo64Flat + diff --git a/OvmfPkg/ResetVector/Ia32/PageTables64.asm b/OvmfPkg/ResetVecto= r/Ia32/PageTables64.asm index 4032719c3075..ccc95ad4715d 100644 --- a/OvmfPkg/ResetVector/Ia32/PageTables64.asm +++ b/OvmfPkg/ResetVector/Ia32/PageTables64.asm @@ -140,9 +140,18 @@ GetSevEncBit: ; Get pte bit position to enable memory encryption ; CPUID Fn8000_001F[EBX] - Bits 5:0 ; + and ebx, 0x3f mov eax, ebx - and eax, 0x3f - jmp SevExit + + ; The encryption bit position is always above 31 + sub ebx, 32 + jns SevExit + + ; Encryption bit was reported as 31 or below, enter a HLT loop +SevEncBitLowHlt: + cli + hlt + jmp SevEncBitLowHlt =20 NoSev: xor eax, eax diff --git a/OvmfPkg/ResetVector/ResetVector.nasmb b/OvmfPkg/ResetVector/Re= setVector.nasmb index c5e0fe93abf4..d3aa87982959 100644 --- a/OvmfPkg/ResetVector/ResetVector.nasmb +++ b/OvmfPkg/ResetVector/ResetVector.nasmb @@ -3,6 +3,7 @@ ; This file includes all other code files to assemble the reset vector code ; ; Copyright (c) 2008 - 2013, Intel Corporation. All rights reserved.
+; Copyright (c) 2020, Advanced Micro Devices, Inc. All rights reserved.
; SPDX-License-Identifier: BSD-2-Clause-Patent ; ;-------------------------------------------------------------------------= ----- @@ -67,13 +68,14 @@ %endif =20 %define PT_ADDR(Offset) (FixedPcdGet32 (PcdOvmfSecPageTablesBase) + (Off= set)) -%include "Ia32/Flat32ToFlat64.asm" =20 %define GHCB_PT_ADDR (FixedPcdGet32 (PcdOvmfSecGhcbPageTableBase)) %define GHCB_BASE (FixedPcdGet32 (PcdOvmfSecGhcbBase)) %define GHCB_SIZE (FixedPcdGet32 (PcdOvmfSecGhcbSize)) %define SEV_ES_WORK_AREA (FixedPcdGet32 (PcdSevEsWorkAreaBase)) + %define SEV_ES_WORK_AREA_RDRAND (FixedPcdGet32 (PcdSevEsWorkAreaBase) + = 8) %define SEV_ES_VC_TOP_OF_STACK (FixedPcdGet32 (PcdOvmfSecPeiTempRamBase)= + FixedPcdGet32 (PcdOvmfSecPeiTempRamSize)) +%include "Ia32/Flat32ToFlat64.asm" %include "Ia32/PageTables64.asm" %endif =20 --=20 2.30.0 -=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 (#69861): https://edk2.groups.io/g/devel/message/69861 Mute This Topic: https://groups.io/mt/79485048/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-