From nobody Mon Feb 9 10:13:29 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) client-ip=8.43.85.245; envelope-from=devel-bounces@lists.libvirt.org; helo=lists.libvirt.org; Authentication-Results: mx.zohomail.com; dkim=fail; spf=pass (zohomail.com: domain of lists.libvirt.org designates 8.43.85.245 as permitted sender) smtp.mailfrom=devel-bounces@lists.libvirt.org; dmarc=fail(p=none dis=none) header.from=redhat.com Return-Path: Received: from lists.libvirt.org (lists.libvirt.org [8.43.85.245]) by mx.zohomail.com with SMTPS id 1729535171745972.7100825442366; Mon, 21 Oct 2024 11:26:11 -0700 (PDT) Received: by lists.libvirt.org (Postfix, from userid 996) id AD7EB15BB; Mon, 21 Oct 2024 14:26:10 -0400 (EDT) Received: from lists.libvirt.org (localhost [IPv6:::1]) by lists.libvirt.org (Postfix) with ESMTP id 125E81829; Mon, 21 Oct 2024 14:24:23 -0400 (EDT) Received: by lists.libvirt.org (Postfix, from userid 996) id 2A09818B1; Mon, 21 Oct 2024 14:24:19 -0400 (EDT) Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.133.124]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by lists.libvirt.org (Postfix) with ESMTPS id AD58C162A for ; Mon, 21 Oct 2024 14:24:00 -0400 (EDT) Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-668-XSfZa_HrOtms88k4A6d8iQ-1; Mon, 21 Oct 2024 14:23:57 -0400 Received: from mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.4]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id 25F2F195608A; Mon, 21 Oct 2024 18:23:56 +0000 (UTC) Received: from localhost (unknown [10.39.208.14]) by mx-prod-int-01.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id 0A546300018D; Mon, 21 Oct 2024 18:23:54 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on lists.libvirt.org X-Spam-Level: X-Spam-Status: No, score=-0.6 required=5.0 tests=DKIM_INVALID,DKIM_SIGNED, HEADER_FROM_DIFFERENT_DOMAINS,MAILING_LIST_MULTI,RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H3,RCVD_IN_MSPIKE_WL,RCVD_IN_VALIDITY_RPBL_BLOCKED, RCVD_IN_VALIDITY_SAFE_BLOCKED,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.4 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1729535040; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=0N0oy4AcdLJjHo4fmlyCXi5P3Gxv84lu0LqR0iOxxZg=; b=L25HtX/AgrIyL/auSb3PCkYfGsY1JN+tUU2bWat6RUq8xGtnQvvfJHiq5a/zhdn3wyyJVE 7M/IM4LW0hscCgWU42G1N0BgFT/GrFnfxi2gnAYl0Z/1+6/lnpKkrZSXMuKKLXK5jT2tyK wfkRXSLg7QTKwa+aWA6WrI3l6mxFJT4= X-MC-Unique: XSfZa_HrOtms88k4A6d8iQ-1 From: marcandre.lureau@redhat.com To: devel@lists.libvirt.org Subject: [PATCH v4 5/6] qemu_tpm: handle file/block storage source Date: Mon, 21 Oct 2024 22:23:24 +0400 Message-ID: <20241021182325.1670505-6-marcandre.lureau@redhat.com> In-Reply-To: <20241021182325.1670505-1-marcandre.lureau@redhat.com> References: <20241021182325.1670505-1-marcandre.lureau@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.4.1 on 10.30.177.4 X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Message-ID-Hash: K4PK72FYWNILHKKFPIN64EKKSKCWEWXH X-Message-ID-Hash: K4PK72FYWNILHKKFPIN64EKKSKCWEWXH X-MailFrom: marcandre.lureau@redhat.com X-Mailman-Rule-Hits: nonmember-moderation X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; header-match-config-1; header-match-config-2; header-match-config-3; header-match-devel.lists.libvirt.org-0 CC: mkletzan@redhat.com, stefanb@us.ibm.com, =?UTF-8?q?Marc-Andr=C3=A9=20Lureau?= X-Mailman-Version: 3.2.2 Precedence: list List-Id: Development discussions about the libvirt library & tools Archived-At: List-Archive: List-Help: List-Post: List-Subscribe: List-Unsubscribe: X-ZohoMail-DKIM: fail (Header signature does not verify) X-ZM-MESSAGEID: 1729535173200116600 Content-Type: text/plain; charset="utf-8" From: Marc-Andr=C3=A9 Lureau When swtpm reports "nvram-backend-dir", it can accepts a single file or block device where TPM state will be stored. --tpmstate must be backend-uri=3Dfile://. Teach the storage to use custom directory or file source location. Signed-off-by: Marc-Andr=C3=A9 Lureau --- src/qemu/qemu_tpm.c | 78 +++++++++++++++++++++++++++++++++++++-------- 1 file changed, 65 insertions(+), 13 deletions(-) diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c index 2d9cd59643..243d02e64e 100644 --- a/src/qemu/qemu_tpm.c +++ b/src/qemu/qemu_tpm.c @@ -340,9 +340,29 @@ qemuTPMVirCommandAddEncryption(virCommand *cmd, } =20 =20 +static char * +qemuTPMGetSwtpmSetupStateArg(const virDomainTPMSourceType source_type, + const char *source_path) +{ + switch (source_type) { + case VIR_DOMAIN_TPM_SOURCE_TYPE_FILE: + /* the file:// prefix is supported since swtpm_setup 0.7.0 */ + /* assume the capability check for swtpm is redundant. */ + return g_strdup_printf("file://%s", source_path); + case VIR_DOMAIN_TPM_SOURCE_TYPE_DIR: + case VIR_DOMAIN_TPM_SOURCE_TYPE_DEFAULT: + case VIR_DOMAIN_TPM_SOURCE_TYPE_LAST: + return g_strdup_printf("%s", source_path); + } + + return NULL; +} + + /* * qemuTPMEmulatorRunSetup * + * @source_type: type of storage * @source_path: path to the directory for TPM state * @vmname: the name of the VM * @vmuuid: the UUID of the VM @@ -360,7 +380,8 @@ qemuTPMVirCommandAddEncryption(virCommand *cmd, * certificates for it. */ static int -qemuTPMEmulatorRunSetup(const char *source_path, +qemuTPMEmulatorRunSetup(const virDomainTPMSourceType source_type, + const char *source_path, const char *vmname, const unsigned char *vmuuid, bool privileged, @@ -376,6 +397,7 @@ qemuTPMEmulatorRunSetup(const char *source_path, char uuid[VIR_UUID_STRING_BUFLEN]; g_autofree char *vmid =3D NULL; g_autofree char *swtpm_setup =3D virTPMGetSwtpmSetup(); + g_autofree char *tpm_state =3D qemuTPMGetSwtpmSetupStateArg(source_typ= e, source_path); =20 if (!swtpm_setup) return -1; @@ -413,7 +435,7 @@ qemuTPMEmulatorRunSetup(const char *source_path, =20 if (!incomingMigration) { virCommandAddArgList(cmd, - "--tpm-state", source_path, + "--tpm-state", tpm_state, "--vmid", vmid, "--logfile", logfile, "--createek", @@ -424,7 +446,7 @@ qemuTPMEmulatorRunSetup(const char *source_path, NULL); } else { virCommandAddArgList(cmd, - "--tpm-state", source_path, + "--tpm-state", tpm_state, "--logfile", logfile, "--overwrite", NULL); @@ -465,6 +487,7 @@ qemuTPMPcrBankBitmapToStr(virBitmap *activePcrBanks) * qemuTPMEmulatorReconfigure * * + * @source_type: type of storage * @source_path: path to the directory for TPM state * @swtpm_user: The userid to switch to when setting up the TPM; * typically this should be the uid of 'tss' or 'root' @@ -478,7 +501,8 @@ qemuTPMPcrBankBitmapToStr(virBitmap *activePcrBanks) * Reconfigure the active PCR banks of a TPM 2. */ static int -qemuTPMEmulatorReconfigure(const char *source_path, +qemuTPMEmulatorReconfigure(const virDomainTPMSourceType source_type, + const char *source_path, uid_t swtpm_user, gid_t swtpm_group, virBitmap *activePcrBanks, @@ -490,6 +514,7 @@ qemuTPMEmulatorReconfigure(const char *source_path, int exitstatus; g_autofree char *activePcrBanksStr =3D NULL; g_autofree char *swtpm_setup =3D virTPMGetSwtpmSetup(); + g_autofree char *tpm_state =3D qemuTPMGetSwtpmSetupStateArg(source_typ= e, source_path); =20 if (!swtpm_setup) return -1; @@ -510,7 +535,7 @@ qemuTPMEmulatorReconfigure(const char *source_path, return -1; =20 virCommandAddArgList(cmd, - "--tpm-state", source_path, + "--tpm-state", tpm_state, "--logfile", logfile, "--pcr-banks", activePcrBanksStr, "--reconfigure", @@ -557,6 +582,7 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDef *tpm, { g_autoptr(virCommand) cmd =3D NULL; bool created =3D false; + bool run_setup =3D false; g_autofree char *swtpm =3D virTPMGetSwtpm(); int pwdfile_fd =3D -1; int migpwdfile_fd =3D -1; @@ -567,6 +593,18 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDef *tpm, if (!swtpm) return NULL; =20 + if (tpm->data.emulator.source_type =3D=3D VIR_DOMAIN_TPM_SOURCE_TYPE_F= ILE) { + if (!virTPMSwtpmCapsGet(VIR_TPM_SWTPM_FEATURE_NVRAM_BACKEND_DIR)) { + virReportError(VIR_ERR_ARGUMENT_UNSUPPORTED, + _("%1$s does not support file storage"), + swtpm); + goto error; + } + create_storage =3D false; + /* setup is run with --not-overwrite */ + run_setup =3D true; + } + /* Do not create storage and run swtpm_setup on incoming migration over * shared storage */ @@ -574,15 +612,18 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDef *tpm, if (incomingMigration && on_shared_storage) create_storage =3D false; =20 - if (create_storage && - qemuTPMEmulatorCreateStorage(tpm, &created, swtpm_user, swtpm_grou= p) < 0) - return NULL; + if (create_storage) { + if (qemuTPMEmulatorCreateStorage(tpm, &created, swtpm_user, swtpm_= group) < 0) + return NULL; + run_setup =3D created; + } =20 if (tpm->data.emulator.hassecretuuid) secretuuid =3D tpm->data.emulator.secretuuid; =20 - if (created && - qemuTPMEmulatorRunSetup(tpm->data.emulator.source_path, vmname, vm= uuid, + if (run_setup && + qemuTPMEmulatorRunSetup(tpm->data.emulator.source_type, + tpm->data.emulator.source_path, vmname, vm= uuid, privileged, swtpm_user, swtpm_group, tpm->data.emulator.logfile, tpm->data.emulator.version, @@ -590,7 +631,8 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDef *tpm, goto error; =20 if (!incomingMigration && - qemuTPMEmulatorReconfigure(tpm->data.emulator.source_path, + qemuTPMEmulatorReconfigure(tpm->data.emulator.source_type, + tpm->data.emulator.source_path, swtpm_user, swtpm_group, tpm->data.emulator.activePcrBanks, tpm->data.emulator.logfile, @@ -609,8 +651,18 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDef *tpm, tpm->data.emulator.source->data.nix.path); =20 virCommandAddArg(cmd, "--tpmstate"); - virCommandAddArgFormat(cmd, "dir=3D%s,mode=3D0600", - tpm->data.emulator.source_path); + switch (tpm->data.emulator.source_type) { + case VIR_DOMAIN_TPM_SOURCE_TYPE_FILE: + virCommandAddArgFormat(cmd, "backend-uri=3Dfile://%s", + tpm->data.emulator.source_path); + break; + case VIR_DOMAIN_TPM_SOURCE_TYPE_DIR: + case VIR_DOMAIN_TPM_SOURCE_TYPE_DEFAULT: + case VIR_DOMAIN_TPM_SOURCE_TYPE_LAST: + virCommandAddArgFormat(cmd, "dir=3D%s,mode=3D0600", + tpm->data.emulator.source_path); + break; + } =20 virCommandAddArg(cmd, "--log"); if (tpm->data.emulator.debug !=3D 0) --=20 2.47.0