From nobody Sun Nov 24 14:45:45 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass(p=none dis=none) header.from=nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1723561398; cv=none; d=zohomail.com; s=zohoarc; b=U44ngV9WXbFq5ec/bgv1v0KrrFYQKkLjkXBJ35mAofGe2fUaW9YhvW+puztiAwmX4nZQ7dtP8awZ15I5E2aSHqoxABRh3VjPRMCcH8OLOEq2kWqxjZ98YN2PKpysPyUp9JqVJf9U+U3DrhC8MkQCUGTESPSjEEwFTH8hNM5xIEo= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1723561398; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:Reply-To:Reply-To:References:Sender:Subject:Subject:To:To:Message-Id; bh=C7PxTjfQAPyNEHo2CfrTK3u9fHDrnOBIMoLx3448DEo=; b=lksQxyvIILPT4udSxz5kq0LyF8UApJ+79H1amtiChd9vKpa7RFMVEJt7YIhW2TvQwN5CVmS2lC72IZocJwbjxA7sg4Nd4UpMbw6zVjeP8IQLtnYxHwPadGiFxVuaH9O5zS+EK8aKdicqUOVveb7gva7OEOTUh8ehqfPiWQW1T9k= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org; dmarc=pass header.from= (p=none dis=none) Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1723561398788534.5527828585459; Tue, 13 Aug 2024 08:03:18 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sdt1z-0003zw-7K; Tue, 13 Aug 2024 11:01:51 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sdt1w-0003tT-JL for qemu-devel@nongnu.org; Tue, 13 Aug 2024 11:01:48 -0400 Received: from smtp-out1.suse.de ([2a07:de40:b251:101:10:150:64:1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sdt1u-0002FF-Jm for qemu-devel@nongnu.org; Tue, 13 Aug 2024 11:01:48 -0400 Received: from imap1.dmz-prg2.suse.org (imap1.dmz-prg2.suse.org [IPv6:2a07:de40:b281:104:10:150:64:97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id 6F83E22830; Tue, 13 Aug 2024 15:01:45 +0000 (UTC) Received: from imap1.dmz-prg2.suse.org (localhost [127.0.0.1]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by imap1.dmz-prg2.suse.org (Postfix) with ESMTPS id A01C713983; Tue, 13 Aug 2024 15:01:44 +0000 (UTC) Received: from dovecot-director2.suse.de ([2a07:de40:b281:106:10:150:64:167]) by imap1.dmz-prg2.suse.org with ESMTPSA id EPrkJFh1u2Z/NAAAD6G6ig (envelope-from ); Tue, 13 Aug 2024 15:01:44 +0000 Authentication-Results: smtp-out1.suse.de; none To: qemu-devel@nongnu.org Cc: Roy Hopkins , Paolo Bonzini , =?UTF-8?q?Daniel=20P=20=2E=20Berrang=C3=A9?= , Stefano Garzarella , Marcelo Tosatti , "Michael S . Tsirkin" , Cornelia Huck , Marcel Apfelbaum , Sergio Lopez , Eduardo Habkost , Alistair Francis , Peter Xu , David Hildenbrand , Igor Mammedov , Tom Lendacky , Michael Roth , Ani Sinha , =?UTF-8?q?J=C3=B6rg=20Roedel?= Subject: [PATCH v5 14/16] backends/igvm: Handle policy for SEV guests Date: Tue, 13 Aug 2024 16:01:16 +0100 Message-ID: <5e016204212c11ef3f7efe99cf52fc139dc17666.1723560001.git.roy.hopkins@suse.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Spam-Score: -4.00 X-Spamd-Result: default: False [-4.00 / 50.00]; REPLY(-4.00)[]; TAGGED_RCPT(0.00)[] X-Rspamd-Queue-Id: 6F83E22830 X-Rspamd-Server: rspamd2.dmz-prg2.suse.org X-Rspamd-Pre-Result: action=no action; module=replies; Message is reply to one we originated X-Rspamd-Action: no action Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: pass client-ip=2a07:de40:b251:101:10:150:64:1; envelope-from=roy.hopkins@suse.com; helo=smtp-out1.suse.de X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-to: Roy Hopkins From: Roy Hopkins via Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZM-MESSAGEID: 1723561400578116600 Content-Type: text/plain; charset="utf-8" Adds a handler for the guest policy initialization IGVM section and builds an SEV policy based on this information and the ID block directive if present. The policy is applied using by calling 'set_guest_policy()' on the ConfidentialGuestSupport object. Signed-off-by: Roy Hopkins Acked-by: Michael S. Tsirkin --- backends/igvm.c | 138 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 138 insertions(+) diff --git a/backends/igvm.c b/backends/igvm.c index 9120922a95..322873e349 100644 --- a/backends/igvm.c +++ b/backends/igvm.c @@ -28,6 +28,33 @@ typedef struct QIgvmParameterData { uint32_t index; } QIgvmParameterData; =20 +/* + * Some directives are specific to particular confidential computing platf= orms. + * Define required types for each of those platforms here. + */ + +/* SEV/SEV-ES/SEV-SNP */ +struct QEMU_PACKED sev_id_block { + uint8_t ld[48]; + uint8_t family_id[16]; + uint8_t image_id[16]; + uint32_t version; + uint32_t guest_svn; + uint64_t policy; +}; + +struct QEMU_PACKED sev_id_authentication { + uint32_t id_key_alg; + uint32_t auth_key_algo; + uint8_t reserved[56]; + uint8_t id_block_sig[512]; + uint8_t id_key[1028]; + uint8_t reserved2[60]; + uint8_t id_key_sig[512]; + uint8_t author_key[1028]; + uint8_t reserved3[892]; +}; + /* * QIgvm contains the information required during processing * of a single IGVM file. @@ -39,6 +66,17 @@ typedef struct QIgvm { uint32_t compatibility_mask; unsigned current_header_index; QTAILQ_HEAD(, QIgvmParameterData) parameter_data; + IgvmPlatformType platform_type; + + /* + * SEV-SNP platforms can contain an ID block and authentication + * that should be verified by the guest. + */ + struct sev_id_block *id_block; + struct sev_id_authentication *id_auth; + + /* Define the guest policy for SEV guests */ + uint64_t sev_policy; =20 /* These variables keep track of contiguous page regions */ IGVM_VHS_PAGE_DATA region_prev_page_data; @@ -68,6 +106,11 @@ static int qigvm_directive_environment_info(QIgvm *ctx, static int qigvm_directive_required_memory(QIgvm *ctx, const uint8_t *header_data, Error **errp); +static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_= data, + Error **errp); +static int qigvm_initialization_guest_policy(QIgvm *ctx, + const uint8_t *header_data, + Error **errp); =20 struct QIGVMHandler { uint32_t type; @@ -92,6 +135,10 @@ static struct QIGVMHandler handlers[] =3D { qigvm_directive_environment_info }, { IGVM_VHT_REQUIRED_MEMORY, IGVM_HEADER_SECTION_DIRECTIVE, qigvm_directive_required_memory }, + { IGVM_VHT_SNP_ID_BLOCK, IGVM_HEADER_SECTION_DIRECTIVE, + qigvm_directive_snp_id_block }, + { IGVM_VHT_GUEST_POLICY, IGVM_HEADER_SECTION_INITIALIZATION, + qigvm_initialization_guest_policy }, }; =20 static int qigvm_handler(QIgvm *ctx, uint32_t type, Error **errp) @@ -633,6 +680,70 @@ static int qigvm_directive_required_memory(QIgvm *ctx, return 0; } =20 +static int qigvm_directive_snp_id_block(QIgvm *ctx, const uint8_t *header_= data, + Error **errp) +{ + const IGVM_VHS_SNP_ID_BLOCK *igvm_id =3D + (const IGVM_VHS_SNP_ID_BLOCK *)header_data; + + if (!(igvm_id->compatibility_mask & ctx->compatibility_mask)) { + return 0; + } + + if (ctx->id_block) { + error_setg(errp, "IGVM: Multiple ID blocks encountered " + "in IGVM file."); + return -1; + } + ctx->id_block =3D g_new0(struct sev_id_block, 1); + ctx->id_auth =3D g_new0(struct sev_id_authentication, 1); + + memcpy(ctx->id_block->family_id, igvm_id->family_id, + sizeof(ctx->id_block->family_id)); + memcpy(ctx->id_block->image_id, igvm_id->image_id, + sizeof(ctx->id_block->image_id)); + ctx->id_block->guest_svn =3D igvm_id->guest_svn; + ctx->id_block->version =3D 1; + memcpy(ctx->id_block->ld, igvm_id->ld, sizeof(ctx->id_block->ld)); + + ctx->id_auth->id_key_alg =3D igvm_id->id_key_algorithm; + memcpy(ctx->id_auth->id_block_sig, &igvm_id->id_key_signature, + sizeof(igvm_id->id_key_signature)); + + ctx->id_auth->auth_key_algo =3D igvm_id->author_key_algorithm; + memcpy(ctx->id_auth->id_key_sig, &igvm_id->author_key_signature, + sizeof(igvm_id->author_key_signature)); + + /* + * SEV and IGVM public key structure population are slightly different. + * See SEV Secure Nested Paging Firmware ABI Specification, Chapter 10. + */ + *((uint32_t *)ctx->id_auth->id_key) =3D igvm_id->id_public_key.curve; + memcpy(&ctx->id_auth->id_key[4], &igvm_id->id_public_key.qx, 72); + memcpy(&ctx->id_auth->id_key[76], &igvm_id->id_public_key.qy, 72); + + *((uint32_t *)ctx->id_auth->author_key) =3D + igvm_id->author_public_key.curve; + memcpy(&ctx->id_auth->author_key[4], &igvm_id->author_public_key.qx, + 72); + memcpy(&ctx->id_auth->author_key[76], &igvm_id->author_public_key.qy, + 72); + + return 0; +} + +static int qigvm_initialization_guest_policy(QIgvm *ctx, + const uint8_t *header_data, Error *= *errp) +{ + const IGVM_VHS_GUEST_POLICY *guest =3D + (const IGVM_VHS_GUEST_POLICY *)header_data; + + if (guest->compatibility_mask & ctx->compatibility_mask) { + ctx->sev_policy =3D guest->policy; + } + return 0; +} + static int qigvm_supported_platform_compat_mask(QIgvm *ctx, Error **errp) { int32_t header_count; @@ -702,12 +813,16 @@ static int qigvm_supported_platform_compat_mask(QIgvm= *ctx, Error **errp) /* Choose the strongest supported isolation technology */ if (compatibility_mask_sev_snp !=3D 0) { ctx->compatibility_mask =3D compatibility_mask_sev_snp; + ctx->platform_type =3D IGVM_PLATFORM_TYPE_SEV_SNP; } else if (compatibility_mask_sev_es !=3D 0) { ctx->compatibility_mask =3D compatibility_mask_sev_es; + ctx->platform_type =3D IGVM_PLATFORM_TYPE_SEV_ES; } else if (compatibility_mask_sev !=3D 0) { ctx->compatibility_mask =3D compatibility_mask_sev; + ctx->platform_type =3D IGVM_PLATFORM_TYPE_SEV; } else if (compatibility_mask !=3D 0) { ctx->compatibility_mask =3D compatibility_mask; + ctx->platform_type =3D IGVM_PLATFORM_TYPE_NATIVE; } else { error_setg( errp, @@ -717,6 +832,23 @@ static int qigvm_supported_platform_compat_mask(QIgvm = *ctx, Error **errp) return 0; } =20 +static int qigvm_handle_policy(QIgvm *ctx, Error **errp) +{ + if (ctx->platform_type =3D=3D IGVM_PLATFORM_TYPE_SEV_SNP) { + int id_block_len =3D 0; + int id_auth_len =3D 0; + if (ctx->id_block) { + ctx->id_block->policy =3D ctx->sev_policy; + id_block_len =3D sizeof(struct sev_id_block); + id_auth_len =3D sizeof(struct sev_id_authentication); + } + return ctx->cgsc->set_guest_policy(GUEST_POLICY_SEV, ctx->sev_poli= cy, + ctx->id_block, id_block_len, + ctx->id_auth, id_auth_len, errp); + } + return 0; +} + static IgvmHandle qigvm_file_init(char *filename, Error **errp) { IgvmHandle igvm; @@ -815,12 +947,18 @@ int qigvm_process_file(IgvmCfg *cfg, ConfidentialGues= tSupport *cgs, */ retval =3D qigvm_process_mem_page(&ctx, NULL, errp); =20 + if (retval =3D=3D 0) { + retval =3D qigvm_handle_policy(&ctx, errp); + } + cleanup: QTAILQ_FOREACH(parameter, &ctx.parameter_data, next) { g_free(parameter->data); parameter->data =3D NULL; } + g_free(ctx.id_block); + g_free(ctx.id_auth); =20 return retval; } --=20 2.43.0