From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from NAM04-BN8-obe.outbound.protection.outlook.com (NAM04-BN8-obe.outbound.protection.outlook.com [40.107.100.84]) by mx.groups.io with SMTP id smtpd.web10.1125.1610045389790134580 for ; Thu, 07 Jan 2021 10:49:50 -0800 Authentication-Results: mx.groups.io; dkim=fail reason="body hash did not verify" header.i=@amdcloud.onmicrosoft.com header.s=selector2-amdcloud-onmicrosoft-com header.b=GKhJM9KM; spf=permerror, err=parse error for token &{10 18 %{i}._ip.%{h}._ehlo.%{d}._spf.vali.email}: invalid domain name (domain: amd.com, ip: 40.107.100.84, mailfrom: thomas.lendacky@amd.com) ARC-Seal: i=1; a=rsa-sha256; s=arcselector9901; d=microsoft.com; cv=none; b=T/yEDJVKc/p+6WWZl3fWCRSHNMhwRDfybJPUhsRuWSG+B20JsqrQ5Bsi0RHvoFCQxVq+4IyERAQhL7oqW7P4qtby6eYVlF8wUTlcZ5b+GKwBFJ8/1/pN4CHvgX6RhqkrAiVg9J/yH+19posTRF1kwgdpidWI0xLWTVu6eW+YHXPxOPsbFbo9UOGlTr1DGJO02KqkC0X9IwtxM5m+Unaxy5sIjH4kINL57atJlm/AL2/bc6ql6qDPOu+J3jLhmNqVxCMbTpa+HiZqdpZwxVjpxxsGG4pags4eAiVubemdJZTjCF9beK0U6HOg0xSjm4i8LHJVXR7rY6g8XdeTLBP/0w== 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=OTh4hohiDu88SGE8ARY51sHUHt+yCYlsbSMsVrewsRs=; b=Uigym0PUphw5VpZKOTzmcp/jgDl8ugARJLtQ17mq5kPEvM/JXzcaiomARzK0vzzQrzHvfawVlMrgELKk5RTeN7xRFFNevTUrFnsR7uxbr/A9Gz/PH+jPJHkk5SjA6Vme5Z2cQsOkNrZjHdmVKIn12XQQ4YSgLbyGPn/imiZ7hbzrKlehuLlJQGRIEnfhROjfUE/F5UK5g01VcJPBtaDc499xGkI/Tjdeh0Mcxu+aiEGZpRbsfPZgaw7r0sw5DYsz198jAou7uXYlCOkV+/EAeLUtd/xl192AXCU4ynMTCuMgPEwshjcp9UgdEjMEDMZm5Y7qsLosXGXWg/8AYrkxrA== 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 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amdcloud.onmicrosoft.com; s=selector2-amdcloud-onmicrosoft-com; h=From:Date:Subject:Message-ID:Content-Type:MIME-Version:X-MS-Exchange-SenderADCheck; bh=OTh4hohiDu88SGE8ARY51sHUHt+yCYlsbSMsVrewsRs=; b=GKhJM9KMd0IVtzDAg10K9GF9g5BrbmBIIYrq0sIO3SYeAm0OZ6GjPE6KC+HgK0alKKZRNMVF7GM2/UcZNw3btCYJLKyvR3t2ueZQPQjGHP5z2iFuqpBKhc0tOh4Etc7cxx75cwklMzaOpo5czd+86Cgm5Gnbrmwst6UXLj9zIuA= Authentication-Results: edk2.groups.io; dkim=none (message not signed) header.d=none;edk2.groups.io; dmarc=none action=none header.from=amd.com; Received: from DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) by DM5PR1201MB0121.namprd12.prod.outlook.com (2603:10b6:4:56::9) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3721.23; Thu, 7 Jan 2021 18:49:47 +0000 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; Thu, 7 Jan 2021 18:49:47 +0000 From: "Lendacky, Thomas" To: devel@edk2.groups.io CC: Brijesh Singh , James Bottomley , Jordan Justen , Laszlo Ersek , Ard Biesheuvel Subject: [PATCH v3 03/15] OvmfPkg/ResetVector: Validate the encryption bit position for SEV/SEV-ES Date: Thu, 7 Jan 2021 12:48:13 -0600 Message-ID: X-Mailer: git-send-email 2.30.0 In-Reply-To: References: X-Originating-IP: [165.204.77.1] X-ClientProxiedBy: SN1PR12CA0055.namprd12.prod.outlook.com (2603:10b6:802:20::26) To DM5PR12MB1355.namprd12.prod.outlook.com (2603:10b6:3:6e::7) Return-Path: thomas.lendacky@amd.com MIME-Version: 1.0 X-MS-Exchange-MessageSentRepresentingType: 1 Received: from tlendack-t1.amd.com (165.204.77.1) by SN1PR12CA0055.namprd12.prod.outlook.com (2603:10b6:802:20::26) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id 15.20.3742.6 via Frontend Transport; Thu, 7 Jan 2021 18:49:46 +0000 X-MS-PublicTrafficType: Email X-MS-Office365-Filtering-HT: Tenant X-MS-Office365-Filtering-Correlation-Id: 89a8abb3-dabe-462e-a352-08d8b33d0177 X-MS-TrafficTypeDiagnostic: DM5PR1201MB0121: 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: BCL:0; X-Microsoft-Antispam-Message-Info: Bb/B3kENZEPhAweDi34bn0YgqnydbNG+nVkn+nwashjLWn4ZWks9L63onbZVxIChuPO4nyGpgZSynLq1BhfCYRr+EyV5iocKfacBPWYypmLWsFn55mqV8uyltbO8P2giOebBvaddGuJ0EXXoArqW+CRhmCVYW3m3C+3k0bTJmULBVHnb1EldOJ1fF9bRO5tOjeYAIPx+deqc1D8+7PMQ9BYKMA0vXz/JH+LIrv7Nte8K56bMw/6rKYC0y9mu9i0Gq4Aq8YZpF6f+HWLIJcGNt/ZcJwU2MnrqtI840pzUDhjC0v5LHrPzzI4d6GQ62ubPlzn7G3NSw6NFBHC1dU79OFzgfzd6oa8M1+3vEeowKx2STJfQp3twxpGz8OQldIw9rpHOc7DnXTwCBjkDxcSblnweRrQ3ap/nOdw+rUo49qXYRyEIPchRHWP2ow+iNmx/gHYSJasgIe6DoGks8P/LyA== X-Forefront-Antispam-Report: CIP:255.255.255.255;CTRY:;LANG:en;SCL:1;SRV:;IPV:NLI;SFV:NSPM;H:DM5PR12MB1355.namprd12.prod.outlook.com;PTR:;CAT:NONE;SFS:(4636009)(39860400002)(136003)(366004)(346002)(376002)(396003)(54906003)(36756003)(316002)(52116002)(16526019)(186003)(6666004)(15650500001)(7696005)(4326008)(83380400001)(66556008)(66946007)(6916009)(956004)(2906002)(8936002)(5660300002)(6486002)(66476007)(8676002)(86362001)(478600001)(966005)(2616005)(26005)(19627235002);DIR:OUT;SFP:1101; X-MS-Exchange-AntiSpam-MessageData: =?us-ascii?Q?zBNYGJqkekyZc7xf+zxNNg2pWO0whR7mE4qlayCLUma8ojPP9Cn+9mHlgwnb?= =?us-ascii?Q?l5rmhy3R6L8JFEXy0FqlFpFBDuHPyq4MN8XbNr45s2/ZrskuGWFWna5gJk+o?= =?us-ascii?Q?T3gr4FopytneWFmpEaom9e+eU0+BHjlIU8eMFewEIhboRoBdj+ioXjrGIvWb?= =?us-ascii?Q?QrkdJt7HV5GXn86EYe36Bfq3o0EFsRPmNuNFeK8VLVckKcOkDG69MWFh6b+T?= =?us-ascii?Q?ZSqf7Fb4TEbWy36z0NuphmeuINi1OM5xJbDJM8oRh6+wemf6gdMc2lr7UoPp?= =?us-ascii?Q?WfTlfrG6HAc9BDM8A1IOtg5w2hs6A1ebxcpCkWkklE0qgM8xWitmEfV0tvxy?= =?us-ascii?Q?KVHwtTjKo7Du2D4oqMd6bCZ+wau3XfG3J8GerbxKoztjeMCwpWo3qRyD8QQY?= =?us-ascii?Q?U2BeQMknCTxkzvBeQNHDN4uTPgcA+qVIwO8AGryDteAyisx9nnQCALzh9ya/?= =?us-ascii?Q?W69VcirdeD50nG6SVveloJs1/3yUigMkD05JxONCH+VrzL3pYmjjp3GWiKMt?= =?us-ascii?Q?KIBxqG7MWgaIKG4QfRnUqILzTrkU0caNGQ7/M2Drge78MX2lWguVTnjzDlrR?= =?us-ascii?Q?+ILeJLAtdbief/l1E+y8EZcrD8cK+neO8hd76CfWNgkByKt0a6c7Ve+lmtg1?= =?us-ascii?Q?pGpGoWy1nYSxnp4uucW7vjIog/Bc7tHBWWkI8S26fS05TyURYVg0CXFgC4sE?= =?us-ascii?Q?AgkPatotf1QhqWAJFqfFeRAZzoiC82VqeT3iCYWzkESVbBYnqoKrGK+F1kQw?= =?us-ascii?Q?llVH1oIE/79X/c3VpkuD8zwBlSoxZMXPh2ydKpDDZxbXlAsmwUQSd+148hAR?= =?us-ascii?Q?wcLj4X81+Ut46CFCFoE8W0cFvAuMx8+AjuMKmbn4MupLNQfKdvlfE5R2blix?= =?us-ascii?Q?I/LER3XWA0dH6TWnaPeoRP3Q8ISSqBprKzYKRqti8/EHm2GFejyo6FAIlkoe?= =?us-ascii?Q?Xro1cagXqreixBVcoaKLiRsbqameIrYjMnOqZT5s3kGpuL6gcg6Me+mF39XW?= =?us-ascii?Q?k0I9?= 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: 07 Jan 2021 18:49:47.0920 (UTC) X-MS-Exchange-CrossTenant-FromEntityHeader: Hosted X-MS-Exchange-CrossTenant-Id: 3dd8961f-e488-4e60-8e11-a82d994e183d X-MS-Exchange-CrossTenant-Network-Message-Id: 89a8abb3-dabe-462e-a352-08d8b33d0177 X-MS-Exchange-CrossTenant-MailboxType: HOSTED X-MS-Exchange-CrossTenant-UserPrincipalName: CWc92UVacwJRtS2GGqnzMCKv9cb+CICFFLh/O0IlzKBO6dycUXnbJcPOFpcJ5VNXTvS5nhnokv0zp5J3n1MLnw== X-MS-Exchange-Transport-CrossTenantHeadersStamped: DM5PR1201MB0121 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: quoted-printable 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 Reviewed-by: Laszlo Ersek Signed-off-by: Tom Lendacky --- 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 cod= e ; ; 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