From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.158.5]) by mx.groups.io with SMTP id smtpd.web10.456.1614718141956026929 for ; Tue, 02 Mar 2021 12:49:02 -0800 Authentication-Results: mx.groups.io; dkim=pass header.i=@ibm.com header.s=pp1 header.b=o0aI/6eR; spf=pass (domain: linux.ibm.com, ip: 148.163.158.5, mailfrom: tobin@linux.ibm.com) Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.43/8.16.0.43) with SMTP id 122KhKu5191359; Tue, 2 Mar 2021 15:49:00 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=from : to : cc : subject : date : message-id : in-reply-to : references : mime-version : content-transfer-encoding; s=pp1; bh=JaSZizAbjKyeb7XvuEOIogmiMXmwtPeDTJdTYX1sW/g=; b=o0aI/6eR+BEV//kz3HFL6Iz2QaJ02Lq7MF8ZfLz2UECFTNst3gePjCNrPRZZ4r+y4A9j BZetm5OwXyPPlsPIezhJfnNSkr5SGBoPAXE0LUQtHZU0QRUTSh+S5XIGTxLsashpR1dL OR926cpawuoHpZQC6EkdszonsQjKlbp3Xw/y4+N30HkOjUwdlSW72PQOCEm13R3Mtbgr P73gT/3bw+J16C/im26KZDD4zFX9f2616kccBB457e5YN+v+FQtVR1+m6Yzzz39ikvoj nwXwqcq8QvnUjgsunBkfWNm2GHcPAB36z00ig60vGj3U5YWjZ0BQw9NMN/Q1/NKAOys3 jA== Received: from ppma03dal.us.ibm.com (b.bd.3ea9.ip4.static.sl-reverse.com [169.62.189.11]) by mx0b-001b2d01.pphosted.com with ESMTP id 371vn7r8bs-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 02 Mar 2021 15:48:59 -0500 Received: from pps.filterd (ppma03dal.us.ibm.com [127.0.0.1]) by ppma03dal.us.ibm.com (8.16.0.42/8.16.0.42) with SMTP id 122Km7j8013967; Tue, 2 Mar 2021 20:48:58 GMT Received: from b03cxnp08026.gho.boulder.ibm.com (b03cxnp08026.gho.boulder.ibm.com [9.17.130.18]) by ppma03dal.us.ibm.com with ESMTP id 37103w5skx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 02 Mar 2021 20:48:58 +0000 Received: from b03ledav006.gho.boulder.ibm.com (b03ledav006.gho.boulder.ibm.com [9.17.130.237]) by b03cxnp08026.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 122KmtWe26673640 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 2 Mar 2021 20:48:55 GMT Received: from b03ledav006.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 97C5DC6057; Tue, 2 Mar 2021 20:48:55 +0000 (GMT) Received: from b03ledav006.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 183C1C605F; Tue, 2 Mar 2021 20:48:55 +0000 (GMT) Received: from amdrome1.watson.ibm.com (unknown [9.2.130.16]) by b03ledav006.gho.boulder.ibm.com (Postfix) with ESMTP; Tue, 2 Mar 2021 20:48:55 +0000 (GMT) From: "Tobin Feldman-Fitzthum" To: devel@edk2.groups.io Cc: Dov Murik , Tobin Feldman-Fitzthum , Tobin Feldman-Fitzthum , James Bottomley , Hubertus Franke , Brijesh Singh , Ashish Kalra , Jon Grimm , Tom Lendacky Subject: [RFC PATCH 14/14] OvmfPkg/AmdSev: MH page encryption POC Date: Tue, 2 Mar 2021 15:48:39 -0500 Message-Id: <20210302204839.82042-15-tobin@linux.ibm.com> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20210302204839.82042-1-tobin@linux.ibm.com> References: <20210302204839.82042-1-tobin@linux.ibm.com> MIME-Version: 1.0 X-TM-AS-GCONF: 00 X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:6.0.369,18.0.761 definitions=2021-03-02_08:2021-03-01,2021-03-02 signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 mlxlogscore=999 adultscore=0 suspectscore=0 spamscore=0 lowpriorityscore=0 mlxscore=0 phishscore=0 priorityscore=1501 bulkscore=0 impostorscore=0 clxscore=1015 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2009150000 definitions=main-2103020156 Content-Transfer-Encoding: 8bit This code is for demonstration purposes only. It is not secure or robust. The purpose is to show where encryption will be incorporated and to get a sense of the performance impact of adding encryption. We plan to use AES-GCM to encrypt the pages as a stream. This will also allow us to verify the GPA as part of the AAD, ensuring that a malicious hypervisor hasn't exchanged pages in-flight. Currently the CryptoPkg and the BaseCryptLib do not expose AES-GCM, despite it being included in OpensslLib. Thus, we use CBC here. Key sharing is out of scope for this part of the RFC. We assume that the source and destination MH share a key. This will probably be implemented via inject-launch-secret in the future. For now, we hardcode a key, but this is strictly temporary. We have had trouble using RandomBytes() in the MH when SEV is enabled. Thus the IV is hardcoded. Again, this is temporary. The HV and the MH will exchange information pertaining to encryption, like the IV, via an additional header on the shared mailbox page. This patch does not do any safety checks or handle encrypt/decrypt failures. Again, this is only here to show where encryption will go and generally how the MH on the source and target can share pages without exposing guest memory to the HV. Signed-off-by: Tobin Feldman-Fitzthum --- .../ConfidentialMigrationDxe.inf | 2 ++ .../ConfidentialMigrationDxe.c | 36 +++++++++++++++++-- 2 files changed, 36 insertions(+), 2 deletions(-) diff --git a/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.inf b/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.inf index 2816952863..ae074a8b07 100644 --- a/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.inf +++ b/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.inf @@ -22,6 +22,7 @@ MdePkg/MdePkg.dec OvmfPkg/OvmfPkg.dec UefiCpuPkg/UefiCpuPkg.dec + CryptoPkg/CryptoPkg.dec [LibraryClasses] MemoryAllocationLib @@ -29,6 +30,7 @@ UefiBootServicesTableLib MpInitLib UefiDriverEntryPoint + BaseCryptLib [Protocols] gEfiMpServiceProtocolGuid diff --git a/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.c b/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.c index 42b99be552..a9cb490561 100644 --- a/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.c +++ b/OvmfPkg/AmdSev/ConfidentialMigration/ConfidentialMigrationDxe.c @@ -10,6 +10,7 @@ #include #include #include +#include #include "VirtualMemory.h" #include "MpLib.h" @@ -46,6 +47,14 @@ typedef volatile struct { UINT32 done; } MH_COMMAND_PARAMETERS; +// +// Additional header for encryption support. +// +struct page_hdr { + UINT8 IV[16]; + UINT8 tag[16]; +}; + // // Addresses for MH page table. // @@ -57,6 +66,20 @@ STATIC PHYSICAL_ADDRESS mMigrationHelperPageTables = 0; // #define UNENC_VIRT_ADDR_BASE 0xffffff8000000000ULL +// +// Key shared between source and target MH (temporary) +// +GLOBAL_REMOVE_IF_UNREFERENCED CONST UINT8 cipher_key[] = { + 0xc2, 0x86, 0x69, 0x6d, 0x88, 0x7c, 0x9a, 0xa0, 0x61, 0x1b, 0xbb, 0x3e, 0x20, 0x25, 0xa4, 0x5a + }; + +// +// IV for CBC cipher (temporary). We are having trouble with +// calling RandomBytes from inside the Migration Handler +// +GLOBAL_REMOVE_IF_UNREFERENCED UINT8 Ivec[] = { + 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 +}; /** Allocates and fills in custom page tables for Migration Handler. @@ -159,6 +182,8 @@ MigrationHandlerMain ( UINT64 params_base; MH_COMMAND_PARAMETERS *params; VOID *page_va; + VOID *cipher_ctx; + INTN ctx_size; DebugPrint (DEBUG_INFO,"MIGRATION Handler Started\n"); @@ -171,6 +196,7 @@ MigrationHandlerMain ( params_base = mailbox_start + UNENC_VIRT_ADDR_BASE; params = (VOID *)params_base; page_va = (VOID *)params_base + 0x1000; + //struct page_hdr *hdr_va = (void *) params_base + 0x800; mailbox_end = mailbox_start + 2 * EFI_PAGE_SIZE; @@ -180,6 +206,9 @@ MigrationHandlerMain ( stack_end = GetMHTopOfStack(); stack_start = stack_end - PcdGet32(PcdCpuApStackSize); + ctx_size = AesGetContextSize (); + cipher_ctx = AllocateRuntimePool (ctx_size); + DisableInterrupts(); params->go = 0; @@ -195,7 +224,9 @@ MigrationHandlerMain ( break; case MH_FUNC_SAVE_PAGE: - CopyMem(page_va, (VOID *)params->gpa, 4096); + AesInit (cipher_ctx, cipher_key, 128); + AesCbcEncrypt(cipher_ctx, (VOID *)params->gpa, 4096, Ivec, page_va); + params->ret = MH_SUCCESS; break; @@ -208,7 +239,8 @@ MigrationHandlerMain ( (params->gpa >= stack_start && params->gpa < stack_end)) { } else { - CopyMem((VOID *)params->gpa, page_va, 4096); + AesInit (cipher_ctx, cipher_key, 128); + AesCbcDecrypt (cipher_ctx, page_va, 4096, Ivec, (VOID *)params->gpa); } params->ret = MH_SUCCESS; break; -- 2.20.1