From nobody Fri May 3 16:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1522965384291400.23796692769304; Thu, 5 Apr 2018 14:56:24 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.phx2.redhat.com [10.5.11.14]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 2F6BB36807; Thu, 5 Apr 2018 21:56:22 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 7959682AD7; Thu, 5 Apr 2018 21:56:21 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id E44FD4CA99; Thu, 5 Apr 2018 21:56:19 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w35LuIMj021244 for ; Thu, 5 Apr 2018 17:56:18 -0400 Received: by smtp.corp.redhat.com (Postfix) id A387B171FC; Thu, 5 Apr 2018 21:56:18 +0000 (UTC) Received: from mx1.redhat.com (ext-mx09.extmail.prod.ext.phx2.redhat.com [10.5.110.38]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 962ED82270 for ; Thu, 5 Apr 2018 21:56:15 +0000 (UTC) Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 63AE04E8AB for ; Thu, 5 Apr 2018 21:56:14 +0000 (UTC) Received: from pps.filterd (m0098399.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w35LsNgG067749 for ; Thu, 5 Apr 2018 17:56:14 -0400 Received: from e35.co.us.ibm.com (e35.co.us.ibm.com [32.97.110.153]) by mx0a-001b2d01.pphosted.com with ESMTP id 2h5rnd0733-1 (version=TLSv1.2 cipher=AES256-SHA256 bits=256 verify=NOT) for ; Thu, 05 Apr 2018 17:56:13 -0400 Received: from localhost by e35.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 5 Apr 2018 15:56:12 -0600 Received: from b03cxnp08028.gho.boulder.ibm.com (9.17.130.20) by e35.co.us.ibm.com (192.168.1.135) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 5 Apr 2018 15:56:10 -0600 Received: from b03ledav002.gho.boulder.ibm.com (b03ledav002.gho.boulder.ibm.com [9.17.130.233]) by b03cxnp08028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w35LuAQf8978824; Thu, 5 Apr 2018 14:56:10 -0700 Received: from b03ledav002.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 235D013603A; Thu, 5 Apr 2018 15:56:10 -0600 (MDT) Received: from sbct-3.watson.ibm.com (unknown [9.47.158.153]) by b03ledav002.gho.boulder.ibm.com (Postfix) with ESMTP id CFA8413603C; Thu, 5 Apr 2018 15:56:09 -0600 (MDT) From: Stefan Berger To: libvir-list@redhat.com Date: Thu, 5 Apr 2018 17:56:01 -0400 In-Reply-To: <1522965366-836-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1522965366-836-1-git-send-email-stefanb@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18040521-0012-0000-0000-00001600E9B5 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00008810; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000256; SDB=6.01013642; UDB=6.00516698; IPR=6.00792902; MB=3.00020438; MTD=3.00000008; XFM=3.00000015; UTC=2018-04-05 21:56:11 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18040521-0013-0000-0000-0000522B194F Message-Id: <1522965366-836-2-git-send-email-stefanb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2018-04-05_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=15 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1804050222 X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 207 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 05 Apr 2018 21:56:14 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 05 Apr 2018 21:56:14 +0000 (UTC) for IP:'148.163.156.1' DOMAIN:'mx0a-001b2d01.pphosted.com' HELO:'mx0a-001b2d01.pphosted.com' FROM:'stefanb@linux.vnet.ibm.com' RCPT:'' X-RedHat-Spam-Score: -1.71 (RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL) 148.163.156.1 mx0a-001b2d01.pphosted.com 148.163.156.1 mx0a-001b2d01.pphosted.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.38 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 1/6] tpm: Enable TPM CRB interface X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.14 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 05 Apr 2018 21:56:23 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Enable the TPM CRB interface added in QEMU 2.12. The TPM CRB interface is a simpler interface than the TPM TIS and should only be used with a TPM 2. Signed-off-by: Stefan Berger --- docs/formatdomain.html.in | 2 ++ docs/schemas/domaincommon.rng | 5 +++- src/conf/domain_conf.c | 5 ++-- src/conf/domain_conf.h | 1 + src/qemu/qemu_capabilities.c | 5 ++++ src/qemu/qemu_capabilities.h | 1 + tests/qemucapabilitiesdata/caps_2.12.0.x86_64.xml | 1 + tests/qemuxml2argvdata/tpm-passthrough-crb.args | 24 +++++++++++++++ tests/qemuxml2argvdata/tpm-passthrough-crb.xml | 32 ++++++++++++++++++++ tests/qemuxml2argvtest.c | 3 ++ tests/qemuxml2xmloutdata/tpm-passthrough-crb.xml | 36 +++++++++++++++++++= ++++ 11 files changed, 111 insertions(+), 4 deletions(-) create mode 100644 tests/qemuxml2argvdata/tpm-passthrough-crb.args create mode 100644 tests/qemuxml2argvdata/tpm-passthrough-crb.xml create mode 100644 tests/qemuxml2xmloutdata/tpm-passthrough-crb.xml diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in index 08dc74b..16fc7db 100644 --- a/docs/formatdomain.html.in +++ b/docs/formatdomain.html.in @@ -7628,6 +7628,8 @@ qemu-kvm -net nic,model=3D? /dev/null The model attribute specifies what device model QEMU provides to the guest. If no model name is provided, tpm-tis will automatically be chosen. + Another available choice is the tpm-crb, which + should only be used when the backend is a TPM 2.

backend
diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng index 8165e69..be5c628 100644 --- a/docs/schemas/domaincommon.rng +++ b/docs/schemas/domaincommon.rng @@ -4112,7 +4112,10 @@ - tpm-tis + + tpm-tis + tpm-crb + diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index ae7c0d9..232174a 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -858,7 +858,8 @@ VIR_ENUM_IMPL(virDomainRNGBackend, "egd"); =20 VIR_ENUM_IMPL(virDomainTPMModel, VIR_DOMAIN_TPM_MODEL_LAST, - "tpm-tis") + "tpm-tis", + "tpm-crb") =20 VIR_ENUM_IMPL(virDomainTPMBackend, VIR_DOMAIN_TPM_TYPE_LAST, "passthrough") @@ -12549,8 +12550,6 @@ virDomainTPMDefParseXML(virDomainXMLOptionPtr xmlop= t, virReportError(VIR_ERR_CONFIG_UNSUPPORTED, _("Unknown TPM frontend model '%s'"), model); goto error; - } else { - def->model =3D VIR_DOMAIN_TPM_MODEL_TIS; } =20 ctxt->node =3D node; diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 61379e5..1724340 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1277,6 +1277,7 @@ struct _virDomainHubDef { =20 typedef enum { VIR_DOMAIN_TPM_MODEL_TIS, + VIR_DOMAIN_TPM_MODEL_CRB, =20 VIR_DOMAIN_TPM_MODEL_LAST } virDomainTPMModel; diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index e54dde6..0952663 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -466,6 +466,7 @@ VIR_ENUM_IMPL(virQEMUCaps, QEMU_CAPS_LAST, /* 285 */ "virtio-mouse-ccw", "virtio-tablet-ccw", + "tpm-crb", ); =20 =20 @@ -3104,6 +3105,10 @@ const struct tpmTypeToCaps virQEMUCapsTPMModelsToCap= s[] =3D { .type =3D VIR_DOMAIN_TPM_MODEL_TIS, .caps =3D QEMU_CAPS_DEVICE_TPM_TIS, }, + { + .type =3D VIR_DOMAIN_TPM_MODEL_CRB, + .caps =3D QEMU_CAPS_DEVICE_TPM_CRB, + }, }; =20 static int diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h index 3f3c29f..604525a 100644 --- a/src/qemu/qemu_capabilities.h +++ b/src/qemu/qemu_capabilities.h @@ -450,6 +450,7 @@ typedef enum { /* 285 */ QEMU_CAPS_DEVICE_VIRTIO_MOUSE_CCW, /* -device virtio-mouse-ccw */ QEMU_CAPS_DEVICE_VIRTIO_TABLET_CCW, /* -device virtio-tablet-ccw */ + QEMU_CAPS_DEVICE_TPM_CRB, /* -device tpm-crb */ =20 QEMU_CAPS_LAST /* this must always be the last item */ } virQEMUCapsFlags; diff --git a/tests/qemucapabilitiesdata/caps_2.12.0.x86_64.xml b/tests/qemu= capabilitiesdata/caps_2.12.0.x86_64.xml index 334296e..39ee4f4 100644 --- a/tests/qemucapabilitiesdata/caps_2.12.0.x86_64.xml +++ b/tests/qemucapabilitiesdata/caps_2.12.0.x86_64.xml @@ -225,6 +225,7 @@ + 2011090 0 390060 diff --git a/tests/qemuxml2argvdata/tpm-passthrough-crb.args b/tests/qemuxm= l2argvdata/tpm-passthrough-crb.args new file mode 100644 index 0000000..ae052b4 --- /dev/null +++ b/tests/qemuxml2argvdata/tpm-passthrough-crb.args @@ -0,0 +1,24 @@ +LC_ALL=3DC \ +PATH=3D/bin \ +HOME=3D/home/test \ +USER=3Dtest \ +LOGNAME=3Dtest \ +QEMU_AUDIO_DRV=3Dnone \ +/usr/bin/qemu-system-x86_64 \ +-name TPM-VM \ +-S \ +-M pc-0.12 \ +-m 2048 \ +-smp 1,sockets=3D1,cores=3D1,threads=3D1 \ +-uuid 11d7cd22-da89-3094-6212-079a48a309a1 \ +-nographic \ +-nodefaults \ +-chardev socket,id=3Dcharmonitor,path=3D/tmp/lib/domain--1-TPM-VM/monitor.= sock,\ +server,nowait \ +-mon chardev=3Dcharmonitor,id=3Dmonitor,mode=3Dreadline \ +-boot c \ +-usb \ +-tpmdev passthrough,id=3Dtpm-tpm0,path=3D/dev/tpm0,\ +cancel-path=3D/sys/class/misc/tpm0/device/cancel \ +-device tpm-crb,tpmdev=3Dtpm-tpm0,id=3Dtpm0 \ +-device virtio-balloon-pci,id=3Dballoon0,bus=3Dpci.0,addr=3D0x3 diff --git a/tests/qemuxml2argvdata/tpm-passthrough-crb.xml b/tests/qemuxml= 2argvdata/tpm-passthrough-crb.xml new file mode 100644 index 0000000..d4f3873 --- /dev/null +++ b/tests/qemuxml2argvdata/tpm-passthrough-crb.xml @@ -0,0 +1,32 @@ + + TPM-VM + 11d7cd22-da89-3094-6212-079a48a309a1 + 2097152 + 512288 + 1 + + hvm + + + + + + + + destroy + restart + destroy + + /usr/bin/qemu-system-x86_64 + + + + + + + + + + + + diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index 308d71f..2992197 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -2134,6 +2134,9 @@ mymain(void) =20 DO_TEST("tpm-passthrough", QEMU_CAPS_DEVICE_TPM_PASSTHROUGH, QEMU_CAPS_DEVICE_TPM_TIS); + DO_TEST("tpm-passthrough-crb", + QEMU_CAPS_DEVICE_TPM_PASSTHROUGH, QEMU_CAPS_DEVICE_TPM_TIS, + QEMU_CAPS_DEVICE_TPM_CRB); DO_TEST_PARSE_ERROR("tpm-no-backend-invalid", QEMU_CAPS_DEVICE_TPM_PASSTHROUGH, QEMU_CAPS_DEVICE= _TPM_TIS); =20 diff --git a/tests/qemuxml2xmloutdata/tpm-passthrough-crb.xml b/tests/qemux= ml2xmloutdata/tpm-passthrough-crb.xml new file mode 100644 index 0000000..ad094a4 --- /dev/null +++ b/tests/qemuxml2xmloutdata/tpm-passthrough-crb.xml @@ -0,0 +1,36 @@ + + TPM-VM + 11d7cd22-da89-3094-6212-079a48a309a1 + 2097152 + 512288 + 1 + + hvm + + + + + + + + destroy + restart + destroy + + /usr/bin/qemu-system-x86_64 + +
+ + + + + + + + + + +
+ + + --=20 2.5.5 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 16:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1522965402005762.4277033301155; Thu, 5 Apr 2018 14:56:42 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 9C40D804F8; Thu, 5 Apr 2018 21:56:40 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6748B82274; Thu, 5 Apr 2018 21:56:40 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 1100A180BAD9; Thu, 5 Apr 2018 21:56:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w35LuNgW021545 for ; Thu, 5 Apr 2018 17:56:23 -0400 Received: by smtp.corp.redhat.com (Postfix) id 189B07D90D; Thu, 5 Apr 2018 21:56:23 +0000 (UTC) Received: from mx1.redhat.com (ext-mx03.extmail.prod.ext.phx2.redhat.com [10.5.110.27]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 1075E7D647 for ; Thu, 5 Apr 2018 21:56:23 +0000 (UTC) Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id AA33083F43 for ; Thu, 5 Apr 2018 21:56:15 +0000 (UTC) Received: from pps.filterd (m0098410.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w35LuCCH021134 for ; Thu, 5 Apr 2018 17:56:15 -0400 Received: from e34.co.us.ibm.com (e34.co.us.ibm.com [32.97.110.152]) by mx0a-001b2d01.pphosted.com with ESMTP id 2h5uk794w2-1 (version=TLSv1.2 cipher=AES256-SHA256 bits=256 verify=NOT) for ; Thu, 05 Apr 2018 17:56:14 -0400 Received: from localhost by e34.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 5 Apr 2018 15:56:13 -0600 Received: from b03cxnp08028.gho.boulder.ibm.com (9.17.130.20) by e34.co.us.ibm.com (192.168.1.134) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 5 Apr 2018 15:56:12 -0600 Received: from b03ledav002.gho.boulder.ibm.com (b03ledav002.gho.boulder.ibm.com [9.17.130.233]) by b03cxnp08028.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w35LuBe010879308; Thu, 5 Apr 2018 14:56:11 -0700 Received: from b03ledav002.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id A7511136043; Thu, 5 Apr 2018 15:56:11 -0600 (MDT) Received: from sbct-3.watson.ibm.com (unknown [9.47.158.153]) by b03ledav002.gho.boulder.ibm.com (Postfix) with ESMTP id 3A50013603A; Thu, 5 Apr 2018 15:56:11 -0600 (MDT) From: Stefan Berger To: libvir-list@redhat.com Date: Thu, 5 Apr 2018 17:56:02 -0400 In-Reply-To: <1522965366-836-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1522965366-836-1-git-send-email-stefanb@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18040521-0016-0000-0000-0000087FC432 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00008810; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000256; SDB=6.01013642; UDB=6.00516697; IPR=6.00792903; MB=3.00020438; MTD=3.00000008; XFM=3.00000015; UTC=2018-04-05 21:56:13 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18040521-0017-0000-0000-00003E22F254 Message-Id: <1522965366-836-3-git-send-email-stefanb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2018-04-05_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=43 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1804050222 X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 207 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 05 Apr 2018 21:56:15 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 05 Apr 2018 21:56:15 +0000 (UTC) for IP:'148.163.156.1' DOMAIN:'mx0a-001b2d01.pphosted.com' HELO:'mx0a-001b2d01.pphosted.com' FROM:'stefanb@linux.vnet.ibm.com' RCPT:'' X-RedHat-Spam-Score: -1.71 (RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL) 148.163.156.1 mx0a-001b2d01.pphosted.com 148.163.156.1 mx0a-001b2d01.pphosted.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.27 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 2/6] tpm: Add support for external swtpm TPM emulator X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 05 Apr 2018 21:56:41 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This patch adds support for an external swtpm TPM emulator. The XML for this type of TPM looks as follows: The XML will currently only start a TPM 1.2. Upon the first start, libvirt will run `swtpm_setup`, which will simulate t= he manufacturing of a TPM and create certificates for it and write them into t= he NVRAM location of the emulated TPM. Then, libvirt will automatically start the swtpm TPM emulator using the `sw= tpm` executable. Once the VM terminates, libvirt uses the swtpm_ioctl executable to graceful= ly shut down the `swtpm` in case it is still running (QEMU did not send shutdo= wn) or clean up the socket file. The above mentioned executables must be found in the PATH. The executables can either be run as root or started as root and switch to the tss user. The requirement for the tss user comes through 'tcsd', which is used for the simulation of the manufacturing. Which user is used can be configured through qemu.conf. The swtpm writes out state into files. The state is kept in /var/lib/libvir= t/tpm: [root@localhost libvirt]# ls -lZ | grep tpm drwx--x--x. 7 root root unconfined_u:object_r:virt_var_lib_t:s0 4096 Apr 5= 16:22 tpm The directory /var/lib/libvirt/tpm maintains per-TPM state directories but also hosts the UnixIO socket of running swtpms, which QEMU uses for communi= cating with them. At this point only the socket file is labeled properly and made = accessible for QEMU, which runs under the qemu user: [root@localhost tpm]# ls -lZ total 4 drwx------. 2 tss tss system_u:object_r:virt_var_lib_t:s0 4096 A= pr 5 16:46 485d0004-a48f-436a-8457-8a3b73e28567 srw-------. 1 qemu qemu system_u:object_r:svirt_image_t:s0:c413,c430 0 A= pr 5 16:46 485d0004-a48f-436a-8457-8a3b73e28567.sock [root@localhost 485d0004-a48f-436a-8457-8a3b73e28567]# ls -lZ total 8 -rw-r--r--. 1 tss tss system_u:object_r:virt_var_lib_t:s0 3648 Apr 5 16:46= tpm-00.permall -rw-r--r--. 1 tss tss system_u:object_r:virt_var_lib_t:s0 2237 Apr 5 16:46= vtpm.log root@sbct-3 485d0004-a48f-436a-8457-8a3b73e28567]# ps auxZ | grep swtpm | g= rep -v grep system_u:system_r:virtd_t:s0-s0:c0.c1023 tss 18697 0.0 0.0 28172 3892 ? = Ss 16:46 0:00 /usr/bin/swtpm socket --daemon --ctrl type=3Dunixio,p= ath=3D/var/lib/libvirt/tpm/485d0004-a48f-436a-8457-8a3b73e28567.sock,mode= =3D0600 --tpmstate dir=3D/var/lib/libvirt/tpm/485d0004-a48f-436a-8457-8a3b7= 3e28567 --log file=3D/var/lib/libvirt/tpm/485d0004-a48f-436a-8457-8a3b73e28= 567/vtpm.log --runas 59 [root@sbct-3 485d0004-a48f-436a-8457-8a3b73e28567]# ps auxZ | grep qemu | g= rep tpm | grep -v grep system_u:system_r:svirt_t:s0:c413,c430 qemu 18702 2.5 0.0 3036052 48676 ? = Sl 16:46 0:08 /bin/qemu-system-x86_64 -name guest=3Dcentos7.0,debug= -threads=3Don -S -object secret,id=3DmasterKey0,format=3Draw,file=3D/var/li= b/libvirt/qemu/domain-6-centos7.0/master-key.aes -machine pc-i440fx-2.8,acc= el=3Dkvm,usb=3Doff,dump-guest-core=3Doff -cpu kvm64 -m 2048 -realtime mlock= =3Doff -smp 2,sockets=3D2,cores=3D1,threads=3D1 -uuid 485d0004-a48f-436a-84= 57-8a3b73e28567 [...] -tpmdev emulator,id=3Dtpm-tpm0,chardev=3Dchrtpm -char= dev socket,id=3Dchrtpm,path=3D/var/lib/libvirt/tpm/485d0004-a48f-436a-8457-= 8a3b73e28567.sock -device tpm-tis,tpmdev=3Dtpm-tpm0,id=3Dtpm0 -device usb-m= ouse,id=3Dinput0,bus=3Dusb.0,port=3D1 -vnc 127.0.0.1:0 -device cirrus-vga,i= d=3Dvideo0,bus=3Dpci.0,addr=3D0x2 -device virtio-balloon-pci,id=3Dballoon0,= bus=3Dpci.0,addr=3D0x6 -msg timestamp=3Don Signed-off-by: Stefan Berger --- docs/formatdomain.html.in | 30 ++ docs/schemas/domaincommon.rng | 5 + src/conf/domain_audit.c | 2 + src/conf/domain_conf.c | 51 ++- src/conf/domain_conf.h | 5 + src/libvirt_private.syms | 5 + src/qemu/Makefile.inc.am | 2 + src/qemu/libvirtd_qemu.aug | 3 + src/qemu/qemu.conf | 7 + src/qemu/qemu_capabilities.c | 5 + src/qemu/qemu_capabilities.h | 1 + src/qemu/qemu_cgroup.c | 1 + src/qemu/qemu_command.c | 52 ++- src/qemu/qemu_conf.c | 11 +- src/qemu/qemu_conf.h | 2 + src/qemu/qemu_domain.c | 2 + src/qemu/qemu_driver.c | 13 + src/qemu/qemu_extdevice.c | 195 ++++++++++ src/qemu/qemu_extdevice.h | 36 ++ src/qemu/qemu_process.c | 8 + src/qemu/test_libvirtd_qemu.aug.in | 1 + src/security/security_dac.c | 6 + src/security/security_selinux.c | 11 + src/util/virfile.c | 12 + src/util/virfile.h | 2 +- src/util/virtpm.c | 432 +++++++++++++++++= ++++ src/util/virtpm.h | 12 + tests/qemucapabilitiesdata/caps_2.11.0.s390x.xml | 1 + tests/qemucapabilitiesdata/caps_2.12.0.aarch64.xml | 1 + tests/qemucapabilitiesdata/caps_2.12.0.ppc64.xml | 1 + tests/qemucapabilitiesdata/caps_2.12.0.s390x.xml | 1 + tests/qemucapabilitiesdata/caps_2.12.0.x86_64.xml | 1 + tests/qemuxml2argvdata/tpm-emulator.args | 24 ++ tests/qemuxml2argvdata/tpm-emulator.xml | 30 ++ tests/qemuxml2argvmock.c | 2 + tests/qemuxml2argvtest.c | 17 + tests/qemuxml2xmloutdata/tpm-emulator.xml | 34 ++ tests/qemuxml2xmltest.c | 1 + 38 files changed, 1011 insertions(+), 14 deletions(-) create mode 100644 src/qemu/qemu_extdevice.c create mode 100644 src/qemu/qemu_extdevice.h create mode 100644 tests/qemuxml2argvdata/tpm-emulator.args create mode 100644 tests/qemuxml2argvdata/tpm-emulator.xml create mode 100644 tests/qemuxml2xmloutdata/tpm-emulator.xml diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in index 16fc7db..bd6fedc 100644 --- a/docs/formatdomain.html.in +++ b/docs/formatdomain.html.in @@ -7621,6 +7621,26 @@ qemu-kvm -net nic,model=3D? /dev/null </devices> ... + +

+ The emulator device type gives access to a TPM emulator providing + TPM functionlity for each VM. QEMU talks to it over a UnixIO socket.= With + the emulator device type each guest gets its own private TPM. + 'emulator' since 4.x.y +

+

+ Example: usage of the TPM Emulator +

+
+  ...
+  <devices>
+    <tpm model=3D'tpm-tis'>
+      <backend type=3D'emulator'>
+      </backend>
+    </tpm>
+  </devices>
+  ...
+
model
@@ -7653,6 +7673,16 @@ qemu-kvm -net nic,model=3D? /dev/null

+
+
emulator
+
+

+ For this backend type the 'swtpm' TPM Emulator must be insta= lled on the + host. Libvirt will automatically start an independent TPM em= ulator + for each QEMU guest requesting access to it. +

+
+
=20 diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng index be5c628..d628444 100644 --- a/docs/schemas/domaincommon.rng +++ b/docs/schemas/domaincommon.rng @@ -4134,6 +4134,11 @@ + + + emulator + + diff --git a/src/conf/domain_audit.c b/src/conf/domain_audit.c index 82868bc..25cccdd 100644 --- a/src/conf/domain_audit.c +++ b/src/conf/domain_audit.c @@ -586,6 +586,8 @@ virDomainAuditTPM(virDomainObjPtr vm, virDomainTPMDefPt= r tpm, "virt=3D%s resrc=3Ddev reason=3D%s %s uuid=3D%s %s", virt, reason, vmname, uuidstr, device); break; + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + break; case VIR_DOMAIN_TPM_TYPE_LAST: default: break; diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 232174a..da14ef8 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -862,7 +862,8 @@ VIR_ENUM_IMPL(virDomainTPMModel, VIR_DOMAIN_TPM_MODEL_L= AST, "tpm-crb") =20 VIR_ENUM_IMPL(virDomainTPMBackend, VIR_DOMAIN_TPM_TYPE_LAST, - "passthrough") + "passthrough", + "emulator") =20 VIR_ENUM_IMPL(virDomainIOMMUModel, VIR_DOMAIN_IOMMU_MODEL_LAST, "intel") @@ -2588,6 +2589,29 @@ void virDomainHostdevDefClear(virDomainHostdevDefPtr= def) } } =20 +static void virDomainTPMDeleteAny(const virDomainDef *def) +{ + virTPMDeleteEmulatorStorage(def->uuid); +} + +void virDomainTPMDelete(virDomainDefPtr def) +{ + virDomainTPMDefPtr tpm =3D def->tpm; + + if (!tpm) + return; + + switch (tpm->type) { + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + virTPMDeleteEmulatorStorage(def->uuid); + break; + case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: + case VIR_DOMAIN_TPM_TYPE_LAST: + /* nothing to do */ + break; + } +} + void virDomainTPMDefFree(virDomainTPMDefPtr def) { if (!def) @@ -2597,6 +2621,9 @@ void virDomainTPMDefFree(virDomainTPMDefPtr def) case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: VIR_FREE(def->data.passthrough.source.data.file.path); break; + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + VIR_FREE(def->data.emulator.source.data.nix.path); + break; case VIR_DOMAIN_TPM_TYPE_LAST: break; } @@ -12525,6 +12552,11 @@ virDomainSmartcardDefParseXML(virDomainXMLOptionPt= r xmlopt, * * * + * or like this: + * + * + * + * */ static virDomainTPMDefPtr virDomainTPMDefParseXML(virDomainXMLOptionPtr xmlopt, @@ -12591,6 +12623,8 @@ virDomainTPMDefParseXML(virDomainXMLOptionPtr xmlop= t, def->data.passthrough.source.type =3D VIR_DOMAIN_CHR_TYPE_DEV; path =3D NULL; break; + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + break; case VIR_DOMAIN_TPM_TYPE_LAST: goto error; } @@ -24760,24 +24794,32 @@ virDomainTPMDefFormat(virBufferPtr buf, virDomainTPMDefPtr def, unsigned int flags) { + bool did_nl =3D false; + virBufferAsprintf(buf, "\n", virDomainTPMModelTypeToString(def->model)); virBufferAdjustIndent(buf, 2); - virBufferAsprintf(buf, "\n", + virBufferAsprintf(buf, "type)); virBufferAdjustIndent(buf, 2); =20 switch (def->type) { case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: + virBufferAddLit(buf, ">\n"); + did_nl =3D true; virBufferEscapeString(buf, "\n", def->data.passthrough.source.data.file.path); break; + case VIR_DOMAIN_TPM_TYPE_EMULATOR: case VIR_DOMAIN_TPM_TYPE_LAST: break; } =20 virBufferAdjustIndent(buf, -2); - virBufferAddLit(buf, "\n"); + if (did_nl) + virBufferAddLit(buf, "\n"); + else + virBufferAddLit(buf, "/>\n"); =20 virDomainDeviceInfoFormat(buf, &def->info, flags); =20 @@ -27548,6 +27590,9 @@ virDomainDeleteConfig(const char *configDir, goto cleanup; } =20 + /* in case it had a TPM, remove it */ + virDomainTPMDeleteAny(dom->def); + ret =3D 0; =20 cleanup: diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 1724340..4ecc70d 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1284,6 +1284,7 @@ typedef enum { =20 typedef enum { VIR_DOMAIN_TPM_TYPE_PASSTHROUGH, + VIR_DOMAIN_TPM_TYPE_EMULATOR, =20 VIR_DOMAIN_TPM_TYPE_LAST } virDomainTPMBackendType; @@ -1298,6 +1299,9 @@ struct _virDomainTPMDef { struct { virDomainChrSourceDef source; } passthrough; + struct { + virDomainChrSourceDef source; + } emulator; } data; }; =20 @@ -2810,6 +2814,7 @@ int virDomainDeviceAddressIsValid(virDomainDeviceInfo= Ptr info, int type); virDomainDeviceInfoPtr virDomainDeviceGetInfo(virDomainDeviceDefPtr device= ); void virDomainTPMDefFree(virDomainTPMDefPtr def); +void virDomainTPMDelete(virDomainDefPtr def); =20 typedef int (*virDomainDeviceInfoCallback)(virDomainDefPtr def, virDomainDeviceDefPtr dev, diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 03fe3b3..e64bbef 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -556,6 +556,7 @@ virDomainTimerTrackTypeToString; virDomainTPMBackendTypeFromString; virDomainTPMBackendTypeToString; virDomainTPMDefFree; +virDomainTPMDelete; virDomainTPMModelTypeFromString; virDomainTPMModelTypeToString; virDomainUSBDeviceDefForeach; @@ -2971,6 +2972,10 @@ virTimeStringThenRaw; =20 # util/virtpm.h virTPMCreateCancelPath; +virTPMDeleteEmulatorStorage; +virTPMEmulatorBuildCommand; +virTPMStopEmulator; +virTPMTryConnect; =20 =20 # util/virtypedparam.h diff --git a/src/qemu/Makefile.inc.am b/src/qemu/Makefile.inc.am index 8ef290a..6c8daf8 100644 --- a/src/qemu/Makefile.inc.am +++ b/src/qemu/Makefile.inc.am @@ -19,6 +19,8 @@ QEMU_DRIVER_SOURCES =3D \ qemu/qemu_domain_address.h \ qemu/qemu_cgroup.c \ qemu/qemu_cgroup.h \ + qemu/qemu_extdevice.c \ + qemu/qemu_extdevice.h \ qemu/qemu_hostdev.c \ qemu/qemu_hostdev.h \ qemu/qemu_hotplug.c \ diff --git a/src/qemu/libvirtd_qemu.aug b/src/qemu/libvirtd_qemu.aug index c19bf3a..cc5d657 100644 --- a/src/qemu/libvirtd_qemu.aug +++ b/src/qemu/libvirtd_qemu.aug @@ -118,6 +118,8 @@ module Libvirtd_qemu =3D let vxhs_entry =3D bool_entry "vxhs_tls" | str_entry "vxhs_tls_x509_cert_dir" =20 + let swtpm_entry =3D str_entry "swtpm_user" + (* Each entry in the config is one of the following ... *) let entry =3D default_tls_entry | vnc_entry @@ -137,6 +139,7 @@ module Libvirtd_qemu =3D | gluster_debug_level_entry | memory_entry | vxhs_entry + | swtpm_entry =20 let comment =3D [ label "#comment" . del /#[ \t]*/ "# " . store /([^ \= t\n][^\n]*)?/ . del /\n/ "\n" ] let empty =3D [ label "#empty" . eol ] diff --git a/src/qemu/qemu.conf b/src/qemu/qemu.conf index 43dd561..f64ae68 100644 --- a/src/qemu/qemu.conf +++ b/src/qemu/qemu.conf @@ -775,3 +775,10 @@ # This directory is used for memoryBacking source if configured as file. # NOTE: big files will be stored here #memory_backing_dir =3D "/var/lib/libvirt/qemu/ram" + +# User for the swtpm TPM Emulator +# +# Default is 'tss'; this is the same user that tcsd (TrouSerS) installs +# and uses; alternative is 'root' +# +#swtpm_user =3D "tss" diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 0952663..ce4db62 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -467,6 +467,7 @@ VIR_ENUM_IMPL(virQEMUCaps, QEMU_CAPS_LAST, "virtio-mouse-ccw", "virtio-tablet-ccw", "tpm-crb", + "tpm-emulator", ); =20 =20 @@ -3098,6 +3099,10 @@ static const struct tpmTypeToCaps virQEMUCapsTPMType= sToCaps[] =3D { .type =3D VIR_DOMAIN_TPM_TYPE_PASSTHROUGH, .caps =3D QEMU_CAPS_DEVICE_TPM_PASSTHROUGH, }, + { + .type =3D VIR_DOMAIN_TPM_TYPE_EMULATOR, + .caps =3D QEMU_CAPS_DEVICE_TPM_EMULATOR, + }, }; =20 const struct tpmTypeToCaps virQEMUCapsTPMModelsToCaps[] =3D { diff --git a/src/qemu/qemu_capabilities.h b/src/qemu/qemu_capabilities.h index 604525a..0cc2882 100644 --- a/src/qemu/qemu_capabilities.h +++ b/src/qemu/qemu_capabilities.h @@ -451,6 +451,7 @@ typedef enum { QEMU_CAPS_DEVICE_VIRTIO_MOUSE_CCW, /* -device virtio-mouse-ccw */ QEMU_CAPS_DEVICE_VIRTIO_TABLET_CCW, /* -device virtio-tablet-ccw */ QEMU_CAPS_DEVICE_TPM_CRB, /* -device tpm-crb */ + QEMU_CAPS_DEVICE_TPM_EMULATOR, /* -tpmdev emulator */ =20 QEMU_CAPS_LAST /* this must always be the last item */ } virQEMUCapsFlags; diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c index b604edb..bd4859c 100644 --- a/src/qemu/qemu_cgroup.c +++ b/src/qemu/qemu_cgroup.c @@ -238,6 +238,7 @@ qemuSetupTPMCgroup(virDomainObjPtr vm) case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: ret =3D qemuSetupChrSourceCgroup(vm, &dev->data.passthrough.source= ); break; + case VIR_DOMAIN_TPM_TYPE_EMULATOR: case VIR_DOMAIN_TPM_TYPE_LAST: break; } diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c index 89fd08b..878a147 100644 --- a/src/qemu/qemu_command.c +++ b/src/qemu/qemu_command.c @@ -9614,21 +9614,33 @@ qemuBuildTPMDevStr(const virDomainDef *def, =20 =20 static char * -qemuBuildTPMBackendStr(const virDomainDef *def, +qemuBuildTPMBackendStr(virDomainDef *def, + virQEMUDriverPtr driver, virCommandPtr cmd, virQEMUCapsPtr qemuCaps, int *tpmfd, - int *cancelfd) + int *cancelfd, + char **chardev) { - const virDomainTPMDef *tpm =3D def->tpm; + virDomainTPMDef *tpm =3D def->tpm; virBuffer buf =3D VIR_BUFFER_INITIALIZER; - const char *type =3D virDomainTPMBackendTypeToString(tpm->type); + const char *type =3D NULL; char *cancel_path =3D NULL, *devset =3D NULL; const char *tpmdev; + virQEMUDriverConfigPtr cfg =3D virQEMUDriverGetConfig(driver); =20 *tpmfd =3D -1; *cancelfd =3D -1; =20 + switch (tpm->type) { + case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + type =3D virDomainTPMBackendTypeToString(tpm->type); + break; + case VIR_DOMAIN_TPM_TYPE_LAST: + goto error; + } + virBufferAsprintf(&buf, "%s,id=3Dtpm-%s", type, tpm->info.alias); =20 switch (tpm->type) { @@ -9679,6 +9691,17 @@ qemuBuildTPMBackendStr(const virDomainDef *def, VIR_FREE(cancel_path); =20 break; + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + if (!virQEMUCapsGet(qemuCaps, QEMU_CAPS_DEVICE_TPM_EMULATOR)) + goto no_support; + + virBufferAddLit(&buf, ",chardev=3Dchrtpm"); + + if (virAsprintf(chardev, "socket,id=3Dchrtpm,path=3D%s", + tpm->data.emulator.source.data.nix.path) < 0) + goto error; + + break; case VIR_DOMAIN_TPM_TYPE_LAST: goto error; } @@ -9686,6 +9709,8 @@ qemuBuildTPMBackendStr(const virDomainDef *def, if (virBufferCheckError(&buf) < 0) goto error; =20 + virObjectUnref(cfg); + return virBufferContentAndReset(&buf); =20 no_support: @@ -9699,16 +9724,19 @@ qemuBuildTPMBackendStr(const virDomainDef *def, VIR_FREE(cancel_path); =20 virBufferFreeAndReset(&buf); + virObjectUnref(cfg); return NULL; } =20 =20 static int -qemuBuildTPMCommandLine(virCommandPtr cmd, - const virDomainDef *def, +qemuBuildTPMCommandLine(virQEMUDriverPtr driver, + virCommandPtr cmd, + virDomainDef *def, virQEMUCapsPtr qemuCaps) { char *optstr; + char *chardev =3D NULL; int tpmfd =3D -1; int cancelfd =3D -1; char *fdset; @@ -9716,13 +9744,19 @@ qemuBuildTPMCommandLine(virCommandPtr cmd, if (!def->tpm) return 0; =20 - if (!(optstr =3D qemuBuildTPMBackendStr(def, cmd, qemuCaps, - &tpmfd, &cancelfd))) + if (!(optstr =3D qemuBuildTPMBackendStr(def, driver, cmd, qemuCaps, + &tpmfd, &cancelfd, + &chardev))) return -1; =20 virCommandAddArgList(cmd, "-tpmdev", optstr, NULL); VIR_FREE(optstr); =20 + if (chardev) { + virCommandAddArgList(cmd, "-chardev", chardev, NULL); + VIR_FREE(chardev); + } + if (tpmfd >=3D 0) { fdset =3D qemuVirCommandGetFDSet(cmd, tpmfd); if (!fdset) @@ -10151,7 +10185,7 @@ qemuBuildCommandLine(virQEMUDriverPtr driver, chardevStdioLogd) < 0) goto error; =20 - if (qemuBuildTPMCommandLine(cmd, def, qemuCaps) < 0) + if (qemuBuildTPMCommandLine(driver, cmd, def, qemuCaps) < 0) goto error; =20 if (qemuBuildInputCommandLine(cmd, def, qemuCaps) < 0) diff --git a/src/qemu/qemu_conf.c b/src/qemu/qemu_conf.c index 36cf3a2..a204105 100644 --- a/src/qemu/qemu_conf.c +++ b/src/qemu/qemu_conf.c @@ -336,6 +336,9 @@ virQEMUDriverConfigPtr virQEMUDriverConfigNew(bool priv= ileged) &cfg->nfirmwares) < 0) goto error; =20 + if (virGetUserID("tss", &cfg->swtpm_user) < 0) + cfg->swtpm_user =3D 0; /* root */ + return cfg; =20 error: @@ -475,7 +478,7 @@ int virQEMUDriverConfigLoadFile(virQEMUDriverConfigPtr = cfg, int rv; size_t i, j; char *stdioHandler =3D NULL; - char *user =3D NULL, *group =3D NULL; + char *user =3D NULL, *group =3D NULL, *swtpm_user =3D NULL; char **controllers =3D NULL; char **hugetlbfs =3D NULL; char **nvram =3D NULL; @@ -912,6 +915,11 @@ int virQEMUDriverConfigLoadFile(virQEMUDriverConfigPtr= cfg, if (virConfGetValueString(conf, "memory_backing_dir", &cfg->memoryBack= ingDir) < 0) goto cleanup; =20 + if (virConfGetValueString(conf, "swtpm_user", &swtpm_user) < 0) + goto cleanup; + if (swtpm_user && virGetUserID(swtpm_user, &cfg->swtpm_user) < 0) + goto cleanup; + ret =3D 0; =20 cleanup: @@ -922,6 +930,7 @@ int virQEMUDriverConfigLoadFile(virQEMUDriverConfigPtr = cfg, VIR_FREE(corestr); VIR_FREE(user); VIR_FREE(group); + VIR_FREE(swtpm_user); virConfFree(conf); return ret; } diff --git a/src/qemu/qemu_conf.h b/src/qemu/qemu_conf.h index e1ad546..6908c36 100644 --- a/src/qemu/qemu_conf.h +++ b/src/qemu/qemu_conf.h @@ -206,6 +206,8 @@ struct _virQEMUDriverConfig { =20 bool vxhsTLS; char *vxhsTLSx509certdir; + + uid_t swtpm_user; }; =20 /* Main driver state */ diff --git a/src/qemu/qemu_domain.c b/src/qemu/qemu_domain.c index 580e0f8..9b7f8ff 100644 --- a/src/qemu/qemu_domain.c +++ b/src/qemu/qemu_domain.c @@ -7088,6 +7088,7 @@ qemuDomainRemoveInactive(virQEMUDriverPtr driver, VIR_WARN("unable to remove snapshot directory %s", snapDir); VIR_FREE(snapDir); } + virDomainTPMDelete(vm->def); =20 virObjectRef(vm); =20 @@ -10280,6 +10281,7 @@ qemuDomainSetupTPM(virQEMUDriverConfigPtr cfg ATTRI= BUTE_UNUSED, return -1; break; =20 + case VIR_DOMAIN_TPM_TYPE_EMULATOR: case VIR_DOMAIN_TPM_TYPE_LAST: /* nada */ break; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index 7bcc493..ef0d0c9 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -14365,6 +14365,19 @@ qemuDomainSnapshotPrepare(virDomainObjPtr vm, goto cleanup; } =20 + if (vm->def->tpm) { + switch (vm->def->tpm->type) { + case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("attached '%s' TPM does not allow snapshots"), + virDomainTPMBackendTypeToString(vm->def->tpm->t= ype)); + goto cleanup; + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + case VIR_DOMAIN_TPM_TYPE_LAST: + break; + } + } + for (i =3D 0; i < def->ndisks; i++) { virDomainSnapshotDiskDefPtr disk =3D &def->disks[i]; virDomainDiskDefPtr dom_disk =3D vm->def->disks[i]; diff --git a/src/qemu/qemu_extdevice.c b/src/qemu/qemu_extdevice.c new file mode 100644 index 0000000..4f42c9b --- /dev/null +++ b/src/qemu/qemu_extdevice.c @@ -0,0 +1,195 @@ +/* + * qemu_extdevice.c: QEMU external devices support + * + * Copyright (C) 2014 IBM Corporation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + * Author: Stefan Berger + */ + +#include + +#include "qemu_extdevice.h" +#include "qemu_domain.h" + +#include "viralloc.h" +#include "virlog.h" +#include "virstring.h" +#include "virtime.h" +#include "virtpm.h" + +#define VIR_FROM_THIS VIR_FROM_QEMU + +VIR_LOG_INIT("qemu.qemu_extdevice") + +static int +qemuExtDeviceLogCommand(qemuDomainLogContextPtr logCtxt, + virCommandPtr cmd, + const char *info) +{ + int ret =3D -1; + char *timestamp =3D NULL; + char *logline =3D NULL; + int logFD; + + logFD =3D qemuDomainLogContextGetWriteFD(logCtxt); + + if ((timestamp =3D virTimeStringNow()) =3D=3D NULL) + goto cleanup; + + if (virAsprintf(&logline, "%s: Starting external device: %s\n", + timestamp, info) < 0) + goto cleanup; + + if (safewrite(logFD, logline, strlen(logline)) < 0) + goto cleanup; + + virCommandWriteArgLog(cmd, logFD); + + ret =3D 0; + + cleanup: + VIR_FREE(timestamp); + VIR_FREE(logline); + + return ret; +} + + +/* + * qemuExtTPMStartEmulator: + * + * @comm: virConnect pointer + * @driver: QEMU driver + * @vm: domain object + * + * Start the external TPM Emulator: + * - have the command line built + * - start the external TPM Emulator and sync with it before QEMU start + */ +static int +qemuExtTPMStartEmulator(virQEMUDriverPtr driver, + virDomainObjPtr vm, + qemuDomainLogContextPtr logCtxt) +{ + int ret =3D -1; + virCommandPtr cmd =3D NULL; + int exitstatus; + char *errbuf =3D NULL; + virQEMUDriverConfigPtr cfg =3D virQEMUDriverGetConfig(driver); + virDomainDefPtr def =3D vm->def; + unsigned char *vmuuid =3D def->uuid; + virDomainTPMDefPtr tpm =3D def->tpm; + + /* stop any left-over TPM emulator for this VM */ + virTPMStopEmulator(tpm, vmuuid, false); + + if (!(cmd =3D virTPMEmulatorBuildCommand(tpm, vmuuid, cfg->swtpm_user)= )) + goto cleanup; + + if (qemuExtDeviceLogCommand(logCtxt, cmd, "TPM Emulator") < 0) + goto cleanup; + + virCommandSetErrorBuffer(cmd, &errbuf); + + if (virCommandRun(cmd, &exitstatus) < 0 || exitstatus !=3D 0) { + VIR_ERROR("Could not start 'swtpm'. exitstatus: %d\n" + "stderr: %s\n", exitstatus, errbuf); + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Could not start 'swtpm'. exitstatus: %d, " + "error: %s"), exitstatus, errbuf); + goto error; + } + + /* sync the startup of the swtpm's Unix socket with the start of QEMU = */ + if (virTPMTryConnect(tpm->data.emulator.source.data.nix.path, + 3 * 1000 * 1000) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Could not connect to the swtpm on '%s'"), + tpm->data.emulator.source.data.nix.path); + goto error; + } + + ret =3D 0; + + cleanup: + VIR_FREE(errbuf); + virCommandFree(cmd); + + virObjectUnref(cfg); + + return ret; + + error: + virTPMStopEmulator(tpm, vmuuid, false); + VIR_FREE(tpm->data.emulator.source.data.nix.path); + + goto cleanup; +} + + +static int +qemuExtTPMStart(virQEMUDriverPtr driver, + virDomainObjPtr vm, + qemuDomainLogContextPtr logCtxt) +{ + int ret =3D 0; + virDomainTPMDefPtr tpm =3D vm->def->tpm; + + switch (tpm->type) { + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + ret =3D qemuExtTPMStartEmulator(driver, vm, logCtxt); + break; + case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: + case VIR_DOMAIN_TPM_TYPE_LAST: + break; + } + + return ret; +} + +static void +qemuExtTPMStop(virDomainObjPtr vm) +{ + switch (vm->def->tpm->type) { + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + virTPMStopEmulator(vm->def->tpm, vm->def->uuid, false); + break; + case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: + case VIR_DOMAIN_TPM_TYPE_LAST: + break; + } +} + +int +qemuExtDevicesStart(virQEMUDriverPtr driver, + virDomainObjPtr vm, + qemuDomainLogContextPtr logCtxt) +{ + int ret =3D 0; + + if (vm->def->tpm) + ret =3D qemuExtTPMStart(driver, vm, logCtxt); + + return ret; +} + +void +qemuExtDevicesStop(virDomainObjPtr vm) +{ + if (vm->def->tpm) + qemuExtTPMStop(vm); +} diff --git a/src/qemu/qemu_extdevice.h b/src/qemu/qemu_extdevice.h new file mode 100644 index 0000000..4dcaec3 --- /dev/null +++ b/src/qemu/qemu_extdevice.h @@ -0,0 +1,36 @@ +/* + * qemu_extdevice.h: QEMU external devices support + * + * Copyright (C) 2014 IBM Corporation + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library. If not, see + * . + * + * Author: Stefan Berger + */ +#ifndef __QEMU_EXTDEVICE_H__ +# define __QEMU_EXTDEVICE_H__ + +# include "qemu_conf.h" +# include "qemu_domain.h" + +int qemuExtDevicesStart(virQEMUDriverPtr driver, + virDomainObjPtr vm, + qemuDomainLogContextPtr logCtxt) + ATTRIBUTE_RETURN_CHECK; + +void qemuExtDevicesStop(virDomainObjPtr vm); + +#endif /* __QEMU_EXTDEVICE_H__ */ + diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 1afb71f..26acfab 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -47,6 +47,7 @@ #include "qemu_migration.h" #include "qemu_interface.h" #include "qemu_security.h" +#include "qemu_extdevice.h" =20 #include "cpu/cpu.h" #include "datatypes.h" @@ -5952,6 +5953,9 @@ qemuProcessLaunch(virConnectPtr conn, goto cleanup; logfile =3D qemuDomainLogContextGetWriteFD(logCtxt); =20 + if (qemuExtDevicesStart(driver, vm, logCtxt) < 0) + goto cleanup; + VIR_DEBUG("Building emulator command line"); if (!(cmd =3D qemuBuildCommandLine(driver, qemuDomainLogContextGetManager(logCtx= t), @@ -6191,6 +6195,8 @@ qemuProcessLaunch(virConnectPtr conn, ret =3D 0; =20 cleanup: + if (ret) + qemuExtDevicesStop(vm); qemuDomainSecretDestroy(vm); virCommandFree(cmd); virObjectUnref(logCtxt); @@ -6557,6 +6563,8 @@ void qemuProcessStop(virQEMUDriverPtr driver, /* Clear network bandwidth */ virDomainClearNetBandwidth(vm); =20 + qemuExtDevicesStop(vm); + virDomainConfVMNWFilterTeardown(vm); =20 if (cfg->macFilter) { diff --git a/src/qemu/test_libvirtd_qemu.aug.in b/src/qemu/test_libvirtd_qe= mu.aug.in index 688e5b9..03bef74 100644 --- a/src/qemu/test_libvirtd_qemu.aug.in +++ b/src/qemu/test_libvirtd_qemu.aug.in @@ -100,3 +100,4 @@ module Test_libvirtd_qemu =3D { "1" =3D "mount" } } { "memory_backing_dir" =3D "/var/lib/libvirt/qemu/ram" } +{ "swtpm_user" =3D "tss" } diff --git a/src/security/security_dac.c b/src/security/security_dac.c index 663c8c9..351f6f4 100644 --- a/src/security/security_dac.c +++ b/src/security/security_dac.c @@ -1372,6 +1372,11 @@ virSecurityDACSetTPMFileLabel(virSecurityManagerPtr = mgr, &tpm->data.passthrough.source, false); break; + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + ret =3D virSecurityDACSetChardevLabel(mgr, def, + &tpm->data.emulator.source, + false); + break; case VIR_DOMAIN_TPM_TYPE_LAST: break; } @@ -1393,6 +1398,7 @@ virSecurityDACRestoreTPMFileLabel(virSecurityManagerP= tr mgr, &tpm->data.passthrough.sou= rce, false); break; + case VIR_DOMAIN_TPM_TYPE_EMULATOR: case VIR_DOMAIN_TPM_TYPE_LAST: break; } diff --git a/src/security/security_selinux.c b/src/security/security_selinu= x.c index c26cdac..cfc8311 100644 --- a/src/security/security_selinux.c +++ b/src/security/security_selinux.c @@ -1472,6 +1472,12 @@ virSecuritySELinuxSetTPMFileLabel(virSecurityManager= Ptr mgr, return -1; } break; + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + tpmdev =3D tpm->data.emulator.source.data.nix.path; + rc =3D virSecuritySELinuxSetFilecon(mgr, tpmdev, seclabel->imagela= bel); + if (rc < 0) + return -1; + break; case VIR_DOMAIN_TPM_TYPE_LAST: break; } @@ -1505,6 +1511,11 @@ virSecuritySELinuxRestoreTPMFileLabelInt(virSecurity= ManagerPtr mgr, VIR_FREE(cancel_path); } break; + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + tpmdev =3D tpm->data.emulator.source.data.nix.path; + if (tpmdev) + rc =3D virSecuritySELinuxRestoreFileLabel(mgr, tpmdev); + break; case VIR_DOMAIN_TPM_TYPE_LAST: break; } diff --git a/src/util/virfile.c b/src/util/virfile.c index 5e9bd20..101f071 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -38,6 +38,7 @@ #include #include #include +#include #if defined HAVE_MNTENT_H && defined HAVE_GETMNTENT_R # include #endif @@ -3031,6 +3032,17 @@ virFileMakeParentPath(const char *path) return ret; } =20 +static int +_virFileDeletePathCB(const char *fpath, const struct stat *sb ATTRIBUTE_UN= USED, + int typeflag ATTRIBUTE_UNUSED, struct FTW *ftwbuf ATT= RIBUTE_UNUSED) +{ + return remove(fpath); +} + +int virFileDeletePath(const char *path) +{ + return nftw(path, _virFileDeletePathCB, 64, FTW_DEPTH | FTW_PHYS); +} =20 /* Build up a fully qualified path for a config file to be * associated with a persistent guest or network */ diff --git a/src/util/virfile.h b/src/util/virfile.h index cd2a386..e7fa736 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -258,6 +258,7 @@ int virFileMakePath(const char *path) ATTRIBUTE_RETURN_= CHECK; int virFileMakePathWithMode(const char *path, mode_t mode) ATTRIBUTE_RETURN_CHECK; int virFileMakeParentPath(const char *path) ATTRIBUTE_RETURN_CHECK; +int virFileDeletePath(const char *path) ATTRIBUTE_RETURN_CHECK; =20 char *virFileBuildPath(const char *dir, const char *name, @@ -353,7 +354,6 @@ int virFileReadValueString(char **value, const char *fo= rmat, ...) =20 int virFileWaitForExists(const char *path, size_t ms, size_t tries); =20 - int virFileInData(int fd, int *inData, long long *length); diff --git a/src/util/virtpm.c b/src/util/virtpm.c index d5c10da..8a99876 100644 --- a/src/util/virtpm.c +++ b/src/util/virtpm.c @@ -22,16 +22,36 @@ =20 #include =20 +#include #include +#include +#include +#include =20 +#include "conf/domain_conf.h" +#include "viralloc.h" +#include "vircommand.h" #include "virstring.h" #include "virerror.h" #include "viralloc.h" #include "virfile.h" +#include "virkmod.h" +#include "virlog.h" #include "virtpm.h" +#include "virutil.h" +#include "configmake.h" =20 #define VIR_FROM_THIS VIR_FROM_NONE =20 +VIR_LOG_INIT("util.tpm") + +/* + * executables for the swtpm; to be found on the host + */ +static char *swtpm_path; +static char *swtpm_setup; +static char *swtpm_ioctl; + /** * virTPMCreateCancelPath: * @devpath: Path to the TPM device @@ -74,3 +94,415 @@ virTPMCreateCancelPath(const char *devpath) cleanup: return path; } + +/* + * virTPMEmulatorInit + * + * Initialize the Emulator functions by searching for necessary + * executables that we will use to start and setup the swtpm + */ +static int +virTPMEmulatorInit(void) +{ + if (!swtpm_path) { + swtpm_path =3D virFindFileInPath("swtpm"); + if (!swtpm_path) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Could not find swtpm 'swtpm' in PATH")); + return -1; + } + if (!virFileIsExecutable(swtpm_path)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("TPM emulator %s is not an executable"), + swtpm_path); + VIR_FREE(swtpm_path); + return -1; + } + } + + if (!swtpm_setup) { + swtpm_setup =3D virFindFileInPath("swtpm_setup"); + if (!swtpm_setup) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Could not find 'swtpm_setup' in PATH")); + return -1; + } + if (!virFileIsExecutable(swtpm_setup)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("'%s' is not an executable"), + swtpm_setup); + VIR_FREE(swtpm_setup); + return -1; + } + } + + if (!swtpm_ioctl) { + swtpm_ioctl =3D virFindFileInPath("swtpm_ioctl"); + if (!swtpm_ioctl) { + virReportError(VIR_ERR_INTERNAL_ERROR, "%s", + _("Could not find swtpm_ioctl in PATH")); + return -1; + } + if (!virFileIsExecutable(swtpm_ioctl)) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("swtpm_ioctl program %s is not an executable"= ), + swtpm_ioctl); + VIR_FREE(swtpm_ioctl); + return -1; + } + } + + return 0; +} + +/* + * virTPMCreateEmulatorStoragePath + * + * @vmuuid: The UUID of the VM for which to create the storage; + * may be NULL + * @suffix: A suffix to append to the storage path; this can be + * used to create a file path + * + * Create the swtpm's storage path + */ +static char * +virTPMCreateEmulatorStoragePath(const unsigned char *vmuuid, + const char *suffix) +{ + char *path =3D NULL; + char uuid[VIR_UUID_STRING_BUFLEN]; + + if (vmuuid) + virUUIDFormat(vmuuid, uuid); + else + uuid[0] =3D '\0'; + + if (virAsprintf(&path, + "%s/lib/libvirt/tpm/%s%s", + LOCALSTATEDIR, uuid, suffix) < 0) + virReportOOMError(); + + return path; +} + +/* + * virTPMEmulatorInitStorage + * + * Initialize the TPM Emulator storage by creating its root directory, + * which is typically found in /var/lib/libvirt/tpm. + * + */ +static int +virTPMEmulatorInitStorage(void) +{ + char *path =3D NULL; + int rc =3D 0; + + if (!(path =3D virTPMCreateEmulatorStoragePath(NULL, ""))) + return -1; + + if (virFileExists(path)) + goto cleanup; + + /* allow others to cd into this dir */ + if (virFileMakePathWithMode(path, 0711) < 0) { + virReportSystemError(errno, + _("Could not create TPM directory %s"), + path); + rc =3D -1; + } + + cleanup: + VIR_FREE(path); + + return rc; +} + +/* + * virTPMCreateEmulatorStorage + * + * @vmuuid: The UUID of the VM + * @created: a pointer to a bool that will be set to true if the + * storage was created because it did not exist yet + * @userid: The userid that needs to be able to access the directory + * + * Unless the storage path for the swtpm for the given VM + * already exists, create it and make it accessible for the given userid. + */ +static char * +virTPMCreateEmulatorStorage(const unsigned char *vmuuid, bool *created, + uid_t swtpm_user) +{ + char *path; + mode_t mode; + + if (virTPMEmulatorInitStorage() < 0) + return NULL; + + *created =3D false; + + if (!(path =3D virTPMCreateEmulatorStoragePath(vmuuid, ""))) + return NULL; + + if (virFileExists(path)) + goto exit; + + *created =3D true; + + mode =3D S_IRUSR | S_IWUSR | S_IXUSR; + + if (virDirCreate(path, mode, swtpm_user, swtpm_user, 0) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Could not create directory %s as uid %u"), + path, swtpm_user); + VIR_FREE(path); + } + + exit: + return path; +} + +void +virTPMDeleteEmulatorStorage(const unsigned char *vmuuid) +{ + char *path; + + if (!(path =3D virTPMCreateEmulatorStoragePath(vmuuid, ""))) + return; + + ignore_value(virFileDeletePath(path)); + VIR_FREE(path); +} + + +/* + * virTPMCreateEmulatorSocket: + * + * @uuid: the UUID of the VM + * + * Create the vTPM device name from the given parameters + */ +static char * +virTPMCreateEmulatorSocket(unsigned const char *vmuuid) +{ + return virTPMCreateEmulatorStoragePath(vmuuid, ".sock"); +} + +/* + * virTPMTryConnect + * + * @pathname: The device pathname to try to open() + * @timeout_ms: The time in ms to spend trying to connect + * + * Try to connect to the given device pathname using open(). + */ +int +virTPMTryConnect(const char *pathname, unsigned long timeout_ms) +{ + return virFileWaitForExists(pathname, 10, timeout_ms / 10); +} + +/* + * virTPMSetupEmulator + * + * @storagepath: path to the directory for TPM state + * @vmuuid: the UUID of the VM + * @userid: The userid to switch to when setting up the TPM; + * typically this should be 'tss' + * @logfile: The file to write the log into; it must be writable + * for the user given by userid or 'tss' + * + * Setup the external swtpm + */ +static int +virTPMSetupEmulator(const char *storagepath, const unsigned char *vmuuid, + uid_t swtpm_user, const char *logfile) +{ + virCommandPtr cmd =3D NULL; + int exitstatus; + int rc =3D 0; + char uuid[VIR_UUID_STRING_BUFLEN]; + + cmd =3D virCommandNew(swtpm_setup); + if (!cmd) { + rc =3D -1; + goto cleanup; + } + + virUUIDFormat(vmuuid, uuid); + + if (swtpm_user > 0) { + virCommandAddArg(cmd, "--runas"); + virCommandAddArgFormat(cmd, "%u", swtpm_user); + } + virCommandAddArgList(cmd, + "--tpm-state", storagepath, + "--vmid", uuid, + "--logfile", logfile, + "--createek", + "--create-ek-cert", + "--create-platform-cert", + "--lock-nvram", + "--not-overwrite", + NULL); + + virCommandClearCaps(cmd); + + if (virCommandRun(cmd, &exitstatus) < 0 || exitstatus !=3D 0) { + /* copy the log to libvirt error since the log will be deleted */ + char *buffer =3D NULL; + ignore_value(virFileReadAllQuiet(logfile, 10240, &buffer)); + VIR_ERROR(_("Error setting up swtpm:\n%s"), buffer); + VIR_FREE(buffer); + + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Could not run '%s'. exitstatus: %d; " + "please check the libvirt error log"), + swtpm_setup, exitstatus); + rc =3D -1; + } + virCommandFree(cmd); + + cleanup: + + return rc; +} + +/* + * virTPMBuildEmulatorCommand + * + * @tpm: TPM definition + * @vmuuid: The UUID of the VM + * @swtpm_user: The uid for the swtpm to run as (drop privileges to from r= oot) + * + * Create the virCommand use for starting the emulator + * Do some initializations on the way, such as creation of storage + * and emulator setup. + */ +virCommandPtr +virTPMEmulatorBuildCommand(virDomainTPMDefPtr tpm, const unsigned char *vm= uuid, + uid_t swtpm_user) +{ + virCommandPtr cmd =3D NULL; + char *storagepath =3D NULL; + char *logfile =3D NULL; + bool created =3D false; + + if (virTPMEmulatorInit() < 0) + return NULL; + + if (!(storagepath =3D virTPMCreateEmulatorStorage(vmuuid, &created, + swtpm_user))) + return NULL; + + /* create logfile in dir where user creating the state will have acces= s */ + if (!(logfile =3D virTPMCreateEmulatorStoragePath(vmuuid, "/vtpm.log")= )) + goto error; + + if (created && + virTPMSetupEmulator(storagepath, vmuuid, swtpm_user, logfile) < 0) + goto error; + + if (!(tpm->data.emulator.source.data.nix.path =3D + virTPMCreateEmulatorSocket(vmuuid))) + goto error; + + unlink(tpm->data.emulator.source.data.nix.path); + + tpm->data.emulator.source.type =3D VIR_DOMAIN_CHR_TYPE_UNIX; + + cmd =3D virCommandNew(swtpm_path); + if (!cmd) + goto error; + + virCommandClearCaps(cmd); + + virCommandAddArgList(cmd, "socket", "--daemon", "--ctrl", NULL); + virCommandAddArgFormat(cmd, "type=3Dunixio,path=3D%s,mode=3D0600", + tpm->data.emulator.source.data.nix.path); + + virCommandAddArg(cmd, "--tpmstate"); + virCommandAddArgFormat(cmd, "dir=3D%s", storagepath); + + virCommandAddArg(cmd, "--log"); + virCommandAddArgFormat(cmd, "file=3D%s", logfile); + + /* allow process to open logfile by root before dropping privileges */ + virCommandAllowCap(cmd, CAP_DAC_OVERRIDE); + + if (swtpm_user > 0) { + virCommandAddArg(cmd, "--runas"); + virCommandAddArgFormat(cmd, "%u", swtpm_user); + virCommandAllowCap(cmd, CAP_SETGID); + virCommandAllowCap(cmd, CAP_SETUID); + } + + VIR_FREE(storagepath); + VIR_FREE(logfile); + + return cmd; + + error: + if (created) + virTPMDeleteEmulatorStorage(vmuuid); + + VIR_FREE(tpm->data.emulator.source.data.nix.path); + VIR_FREE(storagepath); + VIR_FREE(logfile); + + virCommandFree(cmd); + + return NULL; +} + +/* + * virTPMStopEmulator + * @tpm: TPM definition + * @vmuuid: the UUID of the VM + * @verbose: whether to report errors + * + * Gracefully stop the swptm + */ +void +virTPMStopEmulator(virDomainTPMDefPtr tpm, const unsigned char *vmuuid, + bool verbose) +{ + virCommandPtr cmd; + int exitstatus; + char *pathname; + char *errbuf =3D NULL; + + (void)vmuuid; + if (virTPMEmulatorInit() < 0) + return; + + if (!(pathname =3D virTPMCreateEmulatorSocket(vmuuid))) + return; + + cmd =3D virCommandNew(swtpm_ioctl); + if (!cmd) { + VIR_FREE(pathname); + return; + } + + virCommandAddArgList(cmd, "--unix", pathname, "-s", NULL); + + virCommandSetErrorBuffer(cmd, &errbuf); + + if (virCommandRun(cmd, &exitstatus) < 0 || exitstatus !=3D 0) { + if (verbose) + VIR_ERROR(_("Could not run swtpm_ioctl -s '%s'." + " existstatus: %d\nstderr: %s"), + swtpm_ioctl, exitstatus, errbuf); + } + + virCommandFree(cmd); + + /* clean up the socket */ + unlink(pathname); + VIR_FREE(pathname); + + VIR_FREE(tpm->data.emulator.source.data.nix.path); + tpm->data.emulator.source.type =3D 0; + VIR_FREE(errbuf); +} diff --git a/src/util/virtpm.h b/src/util/virtpm.h index b21fc05..424718b 100644 --- a/src/util/virtpm.h +++ b/src/util/virtpm.h @@ -22,6 +22,18 @@ #ifndef __VIR_TPM_H__ # define __VIR_TPM_H__ =20 +# include "vircommand.h" + +typedef struct _virDomainTPMDef virDomainTPMDef; +typedef virDomainTPMDef *virDomainTPMDefPtr; + char *virTPMCreateCancelPath(const char *devpath) ATTRIBUTE_NOINLINE; +virCommandPtr virTPMEmulatorBuildCommand(virDomainTPMDefPtr tpm, + const unsigned char *vmuuid, + uid_t swtpm_user) ATTRIBUTE_RETURN_CHECK; +void virTPMStopEmulator(virDomainTPMDefPtr tpm, const unsigned char *vmuui= d, + bool verbose); +void virTPMDeleteEmulatorStorage(const unsigned char *vmuuid); +int virTPMTryConnect(const char *pathname, unsigned long timeout_ms); =20 #endif /* __VIR_TPM_H__ */ diff --git a/tests/qemucapabilitiesdata/caps_2.11.0.s390x.xml b/tests/qemuc= apabilitiesdata/caps_2.11.0.s390x.xml index 70a35ef..376f58a 100644 --- a/tests/qemucapabilitiesdata/caps_2.11.0.s390x.xml +++ b/tests/qemucapabilitiesdata/caps_2.11.0.s390x.xml @@ -150,6 +150,7 @@ + 2011000 0 342058 diff --git a/tests/qemucapabilitiesdata/caps_2.12.0.aarch64.xml b/tests/qem= ucapabilitiesdata/caps_2.12.0.aarch64.xml index ff48293..069e0ae 100644 --- a/tests/qemucapabilitiesdata/caps_2.12.0.aarch64.xml +++ b/tests/qemucapabilitiesdata/caps_2.12.0.aarch64.xml @@ -187,6 +187,7 @@ + 2011090 0 342346 diff --git a/tests/qemucapabilitiesdata/caps_2.12.0.ppc64.xml b/tests/qemuc= apabilitiesdata/caps_2.12.0.ppc64.xml index ee7fb9e..46d2463 100644 --- a/tests/qemucapabilitiesdata/caps_2.12.0.ppc64.xml +++ b/tests/qemucapabilitiesdata/caps_2.12.0.ppc64.xml @@ -185,6 +185,7 @@ + 2011090 0 419215 diff --git a/tests/qemucapabilitiesdata/caps_2.12.0.s390x.xml b/tests/qemuc= apabilitiesdata/caps_2.12.0.s390x.xml index b5b6b5b..36ffd75 100644 --- a/tests/qemucapabilitiesdata/caps_2.12.0.s390x.xml +++ b/tests/qemucapabilitiesdata/caps_2.12.0.s390x.xml @@ -150,6 +150,7 @@ + 2011090 0 0 diff --git a/tests/qemucapabilitiesdata/caps_2.12.0.x86_64.xml b/tests/qemu= capabilitiesdata/caps_2.12.0.x86_64.xml index 39ee4f4..b2f06b3 100644 --- a/tests/qemucapabilitiesdata/caps_2.12.0.x86_64.xml +++ b/tests/qemucapabilitiesdata/caps_2.12.0.x86_64.xml @@ -226,6 +226,7 @@ + 2011090 0 390060 diff --git a/tests/qemuxml2argvdata/tpm-emulator.args b/tests/qemuxml2argvd= ata/tpm-emulator.args new file mode 100644 index 0000000..9418c74 --- /dev/null +++ b/tests/qemuxml2argvdata/tpm-emulator.args @@ -0,0 +1,24 @@ +LC_ALL=3DC \ +PATH=3D/bin \ +HOME=3D/home/test \ +USER=3Dtest \ +LOGNAME=3Dtest \ +QEMU_AUDIO_DRV=3Dnone \ +/usr/bin/qemu-system-x86_64 \ +-name TPM-VM \ +-S \ +-M pc-0.12 \ +-m 2048 \ +-smp 1,sockets=3D1,cores=3D1,threads=3D1 \ +-uuid 11d7cd22-da89-3094-6212-079a48a309a1 \ +-nographic \ +-nodefaults \ +-chardev socket,id=3Dcharmonitor,path=3D/tmp/lib/domain--1-TPM-VM/monitor.= sock,\ +server,nowait \ +-mon chardev=3Dcharmonitor,id=3Dmonitor,mode=3Dreadline \ +-boot c \ +-usb \ +-tpmdev emulator,id=3Dtpm-tpm0,chardev=3Dchrtpm \ +-chardev socket,id=3Dchrtpm,path=3D/dev/test \ +-device tpm-tis,tpmdev=3Dtpm-tpm0,id=3Dtpm0 \ +-device virtio-balloon-pci,id=3Dballoon0,bus=3Dpci.0,addr=3D0x3 diff --git a/tests/qemuxml2argvdata/tpm-emulator.xml b/tests/qemuxml2argvda= ta/tpm-emulator.xml new file mode 100644 index 0000000..2f4e777 --- /dev/null +++ b/tests/qemuxml2argvdata/tpm-emulator.xml @@ -0,0 +1,30 @@ + + TPM-VM + 11d7cd22-da89-3094-6212-079a48a309a1 + 2097152 + 512288 + 1 + + hvm + + + + + + + + destroy + restart + destroy + + /usr/bin/qemu-system-x86_64 + + + + + + + + + + diff --git a/tests/qemuxml2argvmock.c b/tests/qemuxml2argvmock.c index 177b24e..2b970fe 100644 --- a/tests/qemuxml2argvmock.c +++ b/tests/qemuxml2argvmock.c @@ -161,6 +161,7 @@ virNetDevRunEthernetScript(const char *ifname ATTRIBUTE= _UNUSED, return 0; } =20 +#if 0 void virCommandPassFD(virCommandPtr cmd ATTRIBUTE_UNUSED, int fd ATTRIBUTE_UNUSED, @@ -168,6 +169,7 @@ virCommandPassFD(virCommandPtr cmd ATTRIBUTE_UNUSED, { /* nada */ } +#endif =20 uint8_t * virCryptoGenerateRandom(size_t nbytes) diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index 2992197..92846c3 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -497,6 +497,19 @@ testCompareXMLToArgv(const void *data) } } =20 + if (vm->def->tpm) { + switch (vm->def->tpm->type) { + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + if (VIR_STRDUP(vm->def->tpm->data.emulator.source.data.file.pat= h, + "/dev/test") < 0) + goto cleanup; + break; + case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: + case VIR_DOMAIN_TPM_TYPE_LAST: + break; + } + } + if (!(cmd =3D qemuProcessCreatePretendCmd(&driver, vm, migrateURI, (flags & FLAG_FIPS), false, VIR_QEMU_PROCESS_START_COLD)))= { @@ -504,6 +517,7 @@ testCompareXMLToArgv(const void *data) goto ok; goto cleanup; } + if (flags & FLAG_EXPECT_FAILURE) { VIR_TEST_DEBUG("passed instead of expected failure"); goto cleanup; @@ -2139,6 +2153,9 @@ mymain(void) QEMU_CAPS_DEVICE_TPM_CRB); DO_TEST_PARSE_ERROR("tpm-no-backend-invalid", QEMU_CAPS_DEVICE_TPM_PASSTHROUGH, QEMU_CAPS_DEVICE= _TPM_TIS); + DO_TEST("tpm-emulator", + QEMU_CAPS_DEVICE_TPM_PASSTHROUGH, QEMU_CAPS_DEVICE_TPM_EMULATO= R, + QEMU_CAPS_DEVICE_TPM_TIS); =20 =20 DO_TEST_PARSE_ERROR("pci-domain-invalid", NONE); diff --git a/tests/qemuxml2xmloutdata/tpm-emulator.xml b/tests/qemuxml2xmlo= utdata/tpm-emulator.xml new file mode 100644 index 0000000..1f783bb --- /dev/null +++ b/tests/qemuxml2xmloutdata/tpm-emulator.xml @@ -0,0 +1,34 @@ + + TPM-VM + 11d7cd22-da89-3094-6212-079a48a309a1 + 2097152 + 512288 + 1 + + hvm + + + + + + + + destroy + restart + destroy + + /usr/bin/qemu-system-x86_64 + +
+ + + + + + + + +
+ + + diff --git a/tests/qemuxml2xmltest.c b/tests/qemuxml2xmltest.c index 0f56029..b3e7c8e 100644 --- a/tests/qemuxml2xmltest.c +++ b/tests/qemuxml2xmltest.c @@ -700,6 +700,7 @@ mymain(void) DO_TEST("usb-ich9-ehci-addr", NONE); DO_TEST("disk-copy_on_read", NONE); DO_TEST("tpm-passthrough", NONE); + DO_TEST("tpm-emulator", NONE); =20 DO_TEST("metadata", NONE); DO_TEST("metadata-duplicate", NONE); --=20 2.5.5 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 16:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1522965384717257.89557745293723; Thu, 5 Apr 2018 14:56:24 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 6E74581252; Thu, 5 Apr 2018 21:56:23 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 4496B70581; Thu, 5 Apr 2018 21:56:23 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id DE5931806103; Thu, 5 Apr 2018 21:56:22 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w35LuLoG021424 for ; Thu, 5 Apr 2018 17:56:21 -0400 Received: by smtp.corp.redhat.com (Postfix) id 37F6660927; Thu, 5 Apr 2018 21:56:21 +0000 (UTC) Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2B7E37BE43 for ; Thu, 5 Apr 2018 21:56:17 +0000 (UTC) Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5091B80B29 for ; Thu, 5 Apr 2018 21:56:16 +0000 (UTC) Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w35LsNxU175417 for ; Thu, 5 Apr 2018 17:56:15 -0400 Received: from e36.co.us.ibm.com (e36.co.us.ibm.com [32.97.110.154]) by mx0b-001b2d01.pphosted.com with ESMTP id 2h5rfhruy7-1 (version=TLSv1.2 cipher=AES256-SHA256 bits=256 verify=NOT) for ; Thu, 05 Apr 2018 17:56:15 -0400 Received: from localhost by e36.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 5 Apr 2018 15:56:14 -0600 Received: from b03cxnp08026.gho.boulder.ibm.com (9.17.130.18) by e36.co.us.ibm.com (192.168.1.136) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 5 Apr 2018 15:56:13 -0600 Received: from b03ledav002.gho.boulder.ibm.com (b03ledav002.gho.boulder.ibm.com [9.17.130.233]) by b03cxnp08026.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w35LuD8Q10944932; Thu, 5 Apr 2018 14:56:13 -0700 Received: from b03ledav002.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 12D30136040; Thu, 5 Apr 2018 15:56:13 -0600 (MDT) Received: from sbct-3.watson.ibm.com (unknown [9.47.158.153]) by b03ledav002.gho.boulder.ibm.com (Postfix) with ESMTP id BD93E136044; Thu, 5 Apr 2018 15:56:12 -0600 (MDT) From: Stefan Berger To: libvir-list@redhat.com Date: Thu, 5 Apr 2018 17:56:03 -0400 In-Reply-To: <1522965366-836-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1522965366-836-1-git-send-email-stefanb@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18040521-0020-0000-0000-00000DB2C496 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00008810; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000256; SDB=6.01013642; UDB=6.00516697; IPR=6.00792902; MB=3.00020438; MTD=3.00000008; XFM=3.00000015; UTC=2018-04-05 21:56:14 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18040521-0021-0000-0000-000060C9EBDC Message-Id: <1522965366-836-4-git-send-email-stefanb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2018-04-05_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1011 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1804050222 X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 207 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 05 Apr 2018 21:56:16 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 05 Apr 2018 21:56:16 +0000 (UTC) for IP:'148.163.158.5' DOMAIN:'mx0b-001b2d01.pphosted.com' HELO:'mx0a-001b2d01.pphosted.com' FROM:'stefanb@linux.vnet.ibm.com' RCPT:'' X-RedHat-Spam-Score: -0.7 (RCVD_IN_DNSWL_LOW) 148.163.158.5 mx0b-001b2d01.pphosted.com 148.163.158.5 mx0b-001b2d01.pphosted.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.28 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 3/6] tpm: Label the external swtpm with SELinux labels X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 05 Apr 2018 21:56:23 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" In this patch we label the swtpm process with SELinux labels. We give it the same label as the QEMU process has. We label its state directory and files as well. The file and process labels now look as follows: [root@localhost tpm]# ls -lZ total 4 rwx------. 2 tss tss system_u:object_r:svirt_image_t:s0:c254,c932 4096 Ap= r 5 16:46 485d0004-a48f-436a-8457-8a3b73e28567 srw-------. 1 qemu qemu system_u:object_r:svirt_image_t:s0:c254,c932 0 A= pr 5 16:57 485d0004-a48f-436a-8457-8a3b73e28567.sock [root@localhost 485d0004-a48f-436a-8457-8a3b73e28567]# ls -lZ total 8 -rw-r--r--. 1 tss tss system_u:object_r:svirt_image_t:s0:c254,c932 3648 Apr= 5 16:46 tpm-00.permall -rw-r--r--. 1 tss tss system_u:object_r:svirt_image_t:s0:c254,c932 2237 Apr= 5 16:46 vtpm.log root@sbct-3 485d0004-a48f-436a-8457-8a3b73e28567]# ps auxZ | grep swtpm | g= rep -v grep system_u:system_r:svirt_t:s0:c254,c932 tss 25664 0.0 0.0 28172 3892 ? = Ss 16:57 0:00 /usr/bin/swtpm socket --daemon --ctrl type=3Dunixio,p= ath=3D/var/lib/libvirt/tpm/485d0004-a48f-436a-8457-8a3b73e28567.sock,mode= =3D0600 --tpmstate dir=3D/var/lib/libvirt/tpm/485d0004-a48f-436a-8457-8a3b7= 3e28567 --log file=3D/var/lib/libvirt/tpm/485d0004-a48f-436a-8457-8a3b73e28= 567/vtpm.log --runas 59 [root@sbct-3 485d0004-a48f-436a-8457-8a3b73e28567]# ps auxZ | grep qemu | g= rep tpm | grep -v grep system_u:system_r:svirt_t:s0:c254,c932 qemu 25669 99.0 0.0 3096704 48500 ?= Sl 16:57 3:28 /bin/qemu-system-x86_64 -name guest=3Dcentos7.0,debug= -threads=3Don -S -object secret,id=3DmasterKey0,format=3Draw,file=3D/var/li= b/libvirt/qemu/domain-1-centos7.0/master-key.aes -machine pc-i440fx-2.8,acc= el=3Dkvm,usb=3Doff,dump-guest-core=3Doff -cpu kvm64 -m 2048 -realtime mlock= =3Doff -smp 2,sockets=3D2,cores=3D1,threads=3D1 -uuid 485d0004-a48f-436a-84= 57-8a3b73e28567 [...] -tpmdev emulator,id=3Dtpm-tpm0,chardev=3Dchrtpm -char= dev socket,id=3Dchrtpm,path=3D/var/lib/libvirt/tpm/485d0004-a48f-436a-8457-= 8a3b73e28567.sock -device tpm-tis,tpmdev=3Dtpm-tpm0,id=3Dtpm0 -device usb-m= ouse,id=3Dinput0,bus=3Dusb.0,port=3D1 -vnc 127.0.0.1:0 -device cirrus-vga,i= d=3Dvideo0,bus=3Dpci.0,addr=3D0x2 -device virtio-balloon-pci,id=3Dballoon0,= bus=3Dpci.0,addr=3D0x6 -msg timestamp=3Don Signed-off-by: Stefan Berger --- src/conf/domain_conf.c | 2 + src/conf/domain_conf.h | 3 ++ src/libvirt_private.syms | 1 + src/qemu/qemu_extdevice.c | 26 +++++++++++- src/security/security_driver.h | 5 ++- src/security/security_manager.c | 15 +++++++ src/security/security_manager.h | 3 ++ src/security/security_selinux.c | 90 +++++++++++++++++++++++++++++++++++++= ++++ src/security/security_stack.c | 19 +++++++++ src/util/virtpm.c | 5 ++- 10 files changed, 165 insertions(+), 4 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index da14ef8..9f7f3ce 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -2623,6 +2623,8 @@ void virDomainTPMDefFree(virDomainTPMDefPtr def) break; case VIR_DOMAIN_TPM_TYPE_EMULATOR: VIR_FREE(def->data.emulator.source.data.nix.path); + VIR_FREE(def->data.emulator.storagepath); + VIR_FREE(def->data.emulator.logfile); break; case VIR_DOMAIN_TPM_TYPE_LAST: break; diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 4ecc70d..9802533 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1301,6 +1301,9 @@ struct _virDomainTPMDef { } passthrough; struct { virDomainChrSourceDef source; + /* swtpm storage path and logfile */ + char *storagepath; + char *logfile; } emulator; } data; }; diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index e64bbef..191142e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1334,6 +1334,7 @@ virSecurityManagerSetProcessLabel; virSecurityManagerSetSavedStateLabel; virSecurityManagerSetSocketLabel; virSecurityManagerSetTapFDLabel; +virSecurityManagerSetTPMLabels; virSecurityManagerStackAddNested; virSecurityManagerTransactionAbort; virSecurityManagerTransactionCommit; diff --git a/src/qemu/qemu_extdevice.c b/src/qemu/qemu_extdevice.c index 4f42c9b..e685faf 100644 --- a/src/qemu/qemu_extdevice.c +++ b/src/qemu/qemu_extdevice.c @@ -105,12 +105,36 @@ qemuExtTPMStartEmulator(virQEMUDriverPtr driver, =20 virCommandSetErrorBuffer(cmd, &errbuf); =20 - if (virCommandRun(cmd, &exitstatus) < 0 || exitstatus !=3D 0) { + if (virSecurityManagerSetTPMLabels(driver->securityManager, + def) < 0) + goto error; + + if (virSecurityManagerSetChildProcessLabel(driver->securityManager, + def, cmd) < 0) + goto error; + + if (virSecurityManagerPreFork(driver->securityManager) < 0) + goto error; + + /* + * make sure we run this as root + * note: when installing libvirtd via make install we don't need this, + * but when installed from RPM, this is necessary. + */ + virCommandSetUID(cmd, 0); + virCommandSetGID(cmd, 0); + + ret =3D virCommandRun(cmd, &exitstatus); + + virSecurityManagerPostFork(driver->securityManager); + + if (ret < 0 || exitstatus !=3D 0) { VIR_ERROR("Could not start 'swtpm'. exitstatus: %d\n" "stderr: %s\n", exitstatus, errbuf); virReportError(VIR_ERR_INTERNAL_ERROR, _("Could not start 'swtpm'. exitstatus: %d, " "error: %s"), exitstatus, errbuf); + ret =3D -1; goto error; } =20 diff --git a/src/security/security_driver.h b/src/security/security_driver.h index 95e7c4d..c654d2b 100644 --- a/src/security/security_driver.h +++ b/src/security/security_driver.h @@ -149,7 +149,8 @@ typedef int (*virSecurityDomainRestoreChardevLabel) (vi= rSecurityManagerPtr mgr, virDomainDefPtr def, virDomainChrSourceDef= Ptr dev_source, bool chardevStdioLogd= ); - +typedef int (*virSecurityDomainSetTPMLabels) (virSecurityManagerPtr mgr, + virDomainDefPtr def); =20 struct _virSecurityDriver { size_t privateDataLen; @@ -213,6 +214,8 @@ struct _virSecurityDriver { =20 virSecurityDomainSetChardevLabel domainSetSecurityChardevLabel; virSecurityDomainRestoreChardevLabel domainRestoreSecurityChardevLabel; + + virSecurityDomainSetTPMLabels domainSetSecurityTPMLabels; }; =20 virSecurityDriverPtr virSecurityDriverLookup(const char *name, diff --git a/src/security/security_manager.c b/src/security/security_manage= r.c index fdeea4d..7ff9050 100644 --- a/src/security/security_manager.c +++ b/src/security/security_manager.c @@ -1207,3 +1207,18 @@ virSecurityManagerRestoreChardevLabel(virSecurityMan= agerPtr mgr, virReportUnsupportedError(); return -1; } + + +int virSecurityManagerSetTPMLabels(virSecurityManagerPtr mgr, + virDomainDefPtr vm) +{ + if (mgr->drv->domainSetSecurityTPMLabels) { + int ret; + virObjectLock(mgr); + ret =3D mgr->drv->domainSetSecurityTPMLabels(mgr, vm); + virObjectUnlock(mgr); + return ret; + } + + return 0; +} diff --git a/src/security/security_manager.h b/src/security/security_manage= r.h index c36a8b4..671f6a8 100644 --- a/src/security/security_manager.h +++ b/src/security/security_manager.h @@ -194,4 +194,7 @@ int virSecurityManagerRestoreChardevLabel(virSecurityMa= nagerPtr mgr, virDomainChrSourceDefPtr dev_sou= rce, bool chardevStdioLogd); =20 +int virSecurityManagerSetTPMLabels(virSecurityManagerPtr mgr, + virDomainDefPtr vm); + #endif /* VIR_SECURITY_MANAGER_H__ */ diff --git a/src/security/security_selinux.c b/src/security/security_selinu= x.c index cfc8311..7bc1786 100644 --- a/src/security/security_selinux.c +++ b/src/security/security_selinux.c @@ -30,6 +30,7 @@ #if HAVE_SELINUX_LABEL_H # include #endif +#include =20 #include "security_driver.h" #include "security_selinux.h" @@ -3051,6 +3052,93 @@ virSecuritySELinuxDomainSetPathLabel(virSecurityMana= gerPtr mgr, return virSecuritySELinuxSetFilecon(mgr, path, seclabel->imagelabel); } =20 +static int +_virSecuritySELinuxSetSecurityFileLabels(virSecurityManagerPtr mgr, + const char *path, + virSecurityLabelDefPtr seclabel, + bool recurse) +{ + int ret =3D 0; + int n =3D 0; + struct dirent dirent, *result =3D NULL; + struct stat stat; + char *filename =3D NULL; + DIR *dir =3D opendir(path); + + if (!dir) + return virSecuritySELinuxSetFilecon(mgr, path, seclabel->imagelabe= l); + + while ((n =3D readdir_r(dir, &dirent, &result)) =3D=3D 0) { + if (result =3D=3D NULL) + break; + /* do NOT step into parent dir */ + if (STREQ("..", dirent.d_name)) + continue; + if (virAsprintf(&filename, "%s/%s", path, dirent.d_name) < 0) { + ret =3D -1; + break; + } + n =3D lstat(filename, &stat); + if (n !=3D 0) + break; + if (S_ISDIR(stat.st_mode)) { + ret =3D virSecuritySELinuxSetFilecon(mgr, filename, seclabel->= imagelabel); + if (ret) + break; + if (recurse) + ret =3D _virSecuritySELinuxSetSecurityFileLabels(mgr, file= name, + seclabel, + recurse); + } else if (S_ISREG(stat.st_mode)) { + ret =3D virSecuritySELinuxSetFilecon(mgr, filename, + seclabel->imagelabel); + } + VIR_FREE(filename); + if (ret) + break; + } + if (n) { + virReportSystemError(errno, _("Unable to label files under %s"), + path); + ret =3D -1; + } + + VIR_FREE(filename); + closedir(dir); + + return ret; +} + +static int +virSecuritySELinuxSetSecurityTPMLabels(virSecurityManagerPtr mgr, + virDomainDefPtr def) +{ + int ret =3D 0; + virSecurityLabelDefPtr seclabel; + + seclabel =3D virDomainDefGetSecurityLabelDef(def, SECURITY_SELINUX_NAM= E); + if (seclabel =3D=3D NULL) + return 0; + + switch (def->tpm->type) { + case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: + break; + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + ret =3D _virSecuritySELinuxSetSecurityFileLabels( + mgr, def->tpm->data.emulator.storagepath, + seclabel, false); + if (ret =3D=3D 0 && def->tpm->data.emulator.logfile) + ret =3D _virSecuritySELinuxSetSecurityFileLabels( + mgr, def->tpm->data.emulator.logfile, + seclabel, false); + break; + case VIR_DOMAIN_TPM_TYPE_LAST: + break; + } + + return ret; +} + virSecurityDriver virSecurityDriverSELinux =3D { .privateDataLen =3D sizeof(virSecuritySELinuxData), .name =3D SECURITY_SELINUX_NAME, @@ -3110,4 +3198,6 @@ virSecurityDriver virSecurityDriverSELinux =3D { =20 .domainSetSecurityChardevLabel =3D virSecuritySELinuxSetChardevLa= bel, .domainRestoreSecurityChardevLabel =3D virSecuritySELinuxRestoreChard= evLabel, + + .domainSetSecurityTPMLabels =3D virSecuritySELinuxSetSecurityT= PMLabels, }; diff --git a/src/security/security_stack.c b/src/security/security_stack.c index 9615f9f..7f10ef0 100644 --- a/src/security/security_stack.c +++ b/src/security/security_stack.c @@ -760,6 +760,23 @@ virSecurityStackDomainRestoreChardevLabel(virSecurityM= anagerPtr mgr, return rc; } =20 +static int +virSecurityStackSetSecurityTPMLabels(virSecurityManagerPtr mgr, + virDomainDefPtr vm) +{ + virSecurityStackDataPtr priv =3D virSecurityManagerGetPrivateData(mgr); + virSecurityStackItemPtr item =3D priv->itemsHead; + int rc =3D 0; + + for (; item; item =3D item->next) { + if (virSecurityManagerSetTPMLabels(item->securityManager, + vm) < 0) + rc =3D -1; + } + + return rc; +} + virSecurityDriver virSecurityDriverStack =3D { .privateDataLen =3D sizeof(virSecurityStackData), .name =3D "stack", @@ -822,4 +839,6 @@ virSecurityDriver virSecurityDriverStack =3D { =20 .domainSetSecurityChardevLabel =3D virSecurityStackDomainSetChard= evLabel, .domainRestoreSecurityChardevLabel =3D virSecurityStackDomainRestoreC= hardevLabel, + + .domainSetSecurityTPMLabels =3D virSecurityStackSetSecurityTPM= Labels, }; diff --git a/src/util/virtpm.c b/src/util/virtpm.c index 8a99876..354761e 100644 --- a/src/util/virtpm.c +++ b/src/util/virtpm.c @@ -437,8 +437,9 @@ virTPMEmulatorBuildCommand(virDomainTPMDefPtr tpm, cons= t unsigned char *vmuuid, virCommandAllowCap(cmd, CAP_SETUID); } =20 - VIR_FREE(storagepath); - VIR_FREE(logfile); + tpm->data.emulator.storagepath =3D storagepath; + VIR_FREE(tpm->data.emulator.logfile); + tpm->data.emulator.logfile =3D logfile; =20 return cmd; =20 --=20 2.5.5 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 16:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1522965397501195.13685452559196; Thu, 5 Apr 2018 14:56:37 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 149534E918; Thu, 5 Apr 2018 21:56:36 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id D76A2189D4; Thu, 5 Apr 2018 21:56:35 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 9DD3B4CAA0; Thu, 5 Apr 2018 21:56:35 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w35LuL2C021504 for ; Thu, 5 Apr 2018 17:56:21 -0400 Received: by smtp.corp.redhat.com (Postfix) id 940FD7D647; Thu, 5 Apr 2018 21:56:21 +0000 (UTC) Received: from mx1.redhat.com (ext-mx02.extmail.prod.ext.phx2.redhat.com [10.5.110.26]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8CB427D923 for ; Thu, 5 Apr 2018 21:56:18 +0000 (UTC) Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 1096B85A01 for ; Thu, 5 Apr 2018 21:56:18 +0000 (UTC) Received: from pps.filterd (m0098394.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w35LsvoY007783 for ; Thu, 5 Apr 2018 17:56:17 -0400 Received: from e36.co.us.ibm.com (e36.co.us.ibm.com [32.97.110.154]) by mx0a-001b2d01.pphosted.com with ESMTP id 2h5smu5upj-1 (version=TLSv1.2 cipher=AES256-SHA256 bits=256 verify=NOT) for ; Thu, 05 Apr 2018 17:56:17 -0400 Received: from localhost by e36.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 5 Apr 2018 15:56:16 -0600 Received: from b03cxnp08027.gho.boulder.ibm.com (9.17.130.19) by e36.co.us.ibm.com (192.168.1.136) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 5 Apr 2018 15:56:14 -0600 Received: from b03ledav002.gho.boulder.ibm.com (b03ledav002.gho.boulder.ibm.com [9.17.130.233]) by b03cxnp08027.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w35LuEt812190122; Thu, 5 Apr 2018 14:56:14 -0700 Received: from b03ledav002.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 63D4213603A; Thu, 5 Apr 2018 15:56:14 -0600 (MDT) Received: from sbct-3.watson.ibm.com (unknown [9.47.158.153]) by b03ledav002.gho.boulder.ibm.com (Postfix) with ESMTP id 287F4136040; Thu, 5 Apr 2018 15:56:14 -0600 (MDT) From: Stefan Berger To: libvir-list@redhat.com Date: Thu, 5 Apr 2018 17:56:04 -0400 In-Reply-To: <1522965366-836-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1522965366-836-1-git-send-email-stefanb@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18040521-0020-0000-0000-00000DB2C499 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00008810; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000256; SDB=6.01013642; UDB=6.00516697; IPR=6.00792902; MB=3.00020438; MTD=3.00000008; XFM=3.00000015; UTC=2018-04-05 21:56:16 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18040521-0021-0000-0000-000060C9EBE1 Message-Id: <1522965366-836-5-git-send-email-stefanb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2018-04-05_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1804050222 X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 207 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 05 Apr 2018 21:56:18 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.26]); Thu, 05 Apr 2018 21:56:18 +0000 (UTC) for IP:'148.163.156.1' DOMAIN:'mx0a-001b2d01.pphosted.com' HELO:'mx0a-001b2d01.pphosted.com' FROM:'stefanb@linux.vnet.ibm.com' RCPT:'' X-RedHat-Spam-Score: -1.71 (RCVD_IN_DNSWL_LOW, RCVD_IN_MSPIKE_H5, RCVD_IN_MSPIKE_WL) 148.163.156.1 mx0a-001b2d01.pphosted.com 148.163.156.1 mx0a-001b2d01.pphosted.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.26 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 4/6] tpm: Handle TPM emulator device reconfigurations X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.38]); Thu, 05 Apr 2018 21:56:36 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This patch looks at device changes due to modified XML. With the TPM emulator we have to handle cases where the TPM is removed from the VM or when it is reconfigured in some way. In the device removal case we remove the storage path. The function we create here will be used later to handle other device changes as well. Signed-off-by: Stefan Berger --- src/conf/domain_conf.c | 38 ++++++++++++++++++++++++++++++++++++++ src/conf/domain_conf.h | 3 +++ src/libvirt_private.syms | 1 + src/qemu/qemu_driver.c | 3 ++- 4 files changed, 44 insertions(+), 1 deletion(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 9f7f3ce..72e4412 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -29412,3 +29412,41 @@ virDomainDiskTranslateSourcePool(virDomainDiskDefP= tr def) virStoragePoolDefFree(pooldef); return ret; } + + +static int +virDomainCheckTPMChanges(virDomainDefPtr def, + virDomainDefPtr newDef) +{ + int ret =3D 0; + + if (!def->tpm) + return 0; + + if (!newDef->tpm) { + /* TPM removed */ + virDomainTPMDeleteAny(def); + } else { + if (newDef->tpm->type !=3D def->tpm->type) { + /* type changed */ + virDomainTPMDeleteAny(def); + } + } + + return ret; +} + + +int +virDomainCheckDeviceChanges(virDomainDefPtr def, + virDomainDefPtr newDef) +{ + int ret; + + if (!def || !newDef) + return 0; + + ret =3D virDomainCheckTPMChanges(def, newDef); + + return ret; +} diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 9802533..1f479b0 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -3537,5 +3537,8 @@ virDomainNetResolveActualType(virDomainNetDefPtr ifac= e) =20 int virDomainDiskTranslateSourcePool(virDomainDiskDefPtr def); =20 +int virDomainCheckDeviceChanges(virDomainDefPtr def, + virDomainDefPtr newDef) + ATTRIBUTE_NONNULL(1); =20 #endif /* __DOMAIN_CONF_H */ diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 191142e..488882d 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -198,6 +198,7 @@ virDomainBootTypeFromString; virDomainBootTypeToString; virDomainCapabilitiesPolicyTypeToString; virDomainCapsFeatureTypeToString; +virDomainCheckDeviceChanges; virDomainChrConsoleTargetTypeFromString; virDomainChrConsoleTargetTypeToString; virDomainChrDefForeach; diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c index ef0d0c9..e7a325c 100644 --- a/src/qemu/qemu_driver.c +++ b/src/qemu/qemu_driver.c @@ -7438,7 +7438,8 @@ qemuDomainDefineXMLFlags(virConnectPtr conn, } vm->persistent =3D 1; =20 - if (virDomainSaveConfig(cfg->configDir, driver->caps, + if (virDomainCheckDeviceChanges(oldDef, vm->def) < 0 || + virDomainSaveConfig(cfg->configDir, driver->caps, vm->newDef ? vm->newDef : vm->def) < 0) { if (oldDef) { /* There is backup so this VM was defined before. --=20 2.5.5 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 16:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1522965384862900.5901432331605; Thu, 5 Apr 2018 14:56:24 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.11]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4FE9283F43; Thu, 5 Apr 2018 21:56:23 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 2BE2B60182; Thu, 5 Apr 2018 21:56:23 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 62FC11800CA0; Thu, 5 Apr 2018 21:56:22 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w35LuLpb021505 for ; Thu, 5 Apr 2018 17:56:21 -0400 Received: by smtp.corp.redhat.com (Postfix) id 95BC67D910; Thu, 5 Apr 2018 21:56:21 +0000 (UTC) Received: from mx1.redhat.com (ext-mx01.extmail.prod.ext.phx2.redhat.com [10.5.110.25]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 8C9A87D921 for ; Thu, 5 Apr 2018 21:56:20 +0000 (UTC) Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id F2F2481F0C for ; Thu, 5 Apr 2018 21:56:18 +0000 (UTC) Received: from pps.filterd (m0098414.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w35LsNSF082076 for ; Thu, 5 Apr 2018 17:56:18 -0400 Received: from e37.co.us.ibm.com (e37.co.us.ibm.com [32.97.110.158]) by mx0b-001b2d01.pphosted.com with ESMTP id 2h5u6sj1nv-1 (version=TLSv1.2 cipher=AES256-SHA256 bits=256 verify=NOT) for ; Thu, 05 Apr 2018 17:56:18 -0400 Received: from localhost by e37.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 5 Apr 2018 15:56:17 -0600 Received: from b03cxnp08026.gho.boulder.ibm.com (9.17.130.18) by e37.co.us.ibm.com (192.168.1.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 5 Apr 2018 15:56:16 -0600 Received: from b03ledav002.gho.boulder.ibm.com (b03ledav002.gho.boulder.ibm.com [9.17.130.233]) by b03cxnp08026.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w35LuFvD10944950; Thu, 5 Apr 2018 14:56:15 -0700 Received: from b03ledav002.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C440F136046; Thu, 5 Apr 2018 15:56:15 -0600 (MDT) Received: from sbct-3.watson.ibm.com (unknown [9.47.158.153]) by b03ledav002.gho.boulder.ibm.com (Postfix) with ESMTP id 79D0913603C; Thu, 5 Apr 2018 15:56:15 -0600 (MDT) From: Stefan Berger To: libvir-list@redhat.com Date: Thu, 5 Apr 2018 17:56:05 -0400 In-Reply-To: <1522965366-836-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1522965366-836-1-git-send-email-stefanb@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18040521-0024-0000-0000-0000182F9B66 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00008810; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000256; SDB=6.01013642; UDB=6.00516697; IPR=6.00792902; MB=3.00020438; MTD=3.00000008; XFM=3.00000015; UTC=2018-04-05 21:56:16 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18040521-0025-0000-0000-00004F66FCBD Message-Id: <1522965366-836-6-git-send-email-stefanb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2018-04-05_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=43 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1804050222 X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 207 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 05 Apr 2018 21:56:19 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Thu, 05 Apr 2018 21:56:19 +0000 (UTC) for IP:'148.163.158.5' DOMAIN:'mx0b-001b2d01.pphosted.com' HELO:'mx0a-001b2d01.pphosted.com' FROM:'stefanb@linux.vnet.ibm.com' RCPT:'' X-RedHat-Spam-Score: -0.7 (RCVD_IN_DNSWL_LOW) 148.163.158.5 mx0b-001b2d01.pphosted.com 148.163.158.5 mx0b-001b2d01.pphosted.com X-Scanned-By: MIMEDefang 2.83 on 10.5.110.25 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 5/6] tpm: Add support for choosing emulation of a TPM 2 X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.11 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.27]); Thu, 05 Apr 2018 21:56:23 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" This patch extends the TPM's device XML with TPM 2 support. This only works for the emulator type backend and looks as follows: Once the version of a TPM has been chosen it cannot be changed anymore unle= ss one removes the TPM device first and then reads it. However, one looses all the secrets stored inside or tied to the emulated TPM by doing this. Signed-off-by: Stefan Berger --- docs/formatdomain.html.in | 17 ++++++- docs/schemas/domaincommon.rng | 13 ++++++ src/conf/domain_conf.c | 36 ++++++++++++++- src/conf/domain_conf.h | 6 +++ src/util/virtpm.c | 64 ++++++++++++++++++++++= +++- tests/qemuxml2argvdata/tpm-emulator-tpm2.args | 24 ++++++++++ tests/qemuxml2argvdata/tpm-emulator-tpm2.xml | 30 ++++++++++++ tests/qemuxml2argvtest.c | 3 ++ tests/qemuxml2xmloutdata/tpm-emulator-tpm2.xml | 34 ++++++++++++++ 9 files changed, 223 insertions(+), 4 deletions(-) create mode 100644 tests/qemuxml2argvdata/tpm-emulator-tpm2.args create mode 100644 tests/qemuxml2argvdata/tpm-emulator-tpm2.xml create mode 100644 tests/qemuxml2xmloutdata/tpm-emulator-tpm2.xml diff --git a/docs/formatdomain.html.in b/docs/formatdomain.html.in index bd6fedc..e5463a0 100644 --- a/docs/formatdomain.html.in +++ b/docs/formatdomain.html.in @@ -7635,7 +7635,7 @@ qemu-kvm -net nic,model=3D? /dev/null ... <devices> <tpm model=3D'tpm-tis'> - <backend type=3D'emulator'> + <backend type=3D'emulator' tpmversion=3D'2'> </backend> </tpm> </devices> @@ -7684,6 +7684,21 @@ qemu-kvm -net nic,model=3D? /dev/null +
tpmversion
+
+

+ The tpmversion attribute indicates the version + of the TPM. By default a TPM 1.2 is created. This attribute + only works with the emulator backend. The following + versions are supported: +

+
    +
  • '1.2' : creates a TPM 1.2
  • +
  • '2.0' or '2' : creates a TPM 2
  • +
+ Note that once a certain version of a TPM has been created for + a guest, the version must not be changed anymore. +
=20

NVRAM device

diff --git a/docs/schemas/domaincommon.rng b/docs/schemas/domaincommon.rng index d628444..77328bd 100644 --- a/docs/schemas/domaincommon.rng +++ b/docs/schemas/domaincommon.rng @@ -4140,6 +4140,19 @@ + + + + + + 1.2 + 2 + 2.0 + + + + + =20 diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 72e4412..5498e2e 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -12557,7 +12557,7 @@ virDomainSmartcardDefParseXML(virDomainXMLOptionPtr= xmlopt, * or like this: * * - * + * * */ static virDomainTPMDefPtr @@ -12570,6 +12570,7 @@ virDomainTPMDefParseXML(virDomainXMLOptionPtr xmlop= t, char *path =3D NULL; char *model =3D NULL; char *backend =3D NULL; + char *tpmversion =3D NULL; virDomainTPMDefPtr def; xmlNodePtr save =3D ctxt->node; xmlNodePtr *backends =3D NULL; @@ -12616,6 +12617,20 @@ virDomainTPMDefParseXML(virDomainXMLOptionPtr xmlo= pt, goto error; } =20 + tpmversion =3D virXMLPropString(backends[0], "tpmversion"); + if (!tpmversion || STREQ(tpmversion, "1.2")) { + def->tpmversion =3D VIR_DOMAIN_TPM_VERSION_1_2; + /* only TIS available for emulator */ + if (def->type =3D=3D VIR_DOMAIN_TPM_TYPE_EMULATOR) + def->model =3D VIR_DOMAIN_TPM_MODEL_TIS; + } else if (STREQ(tpmversion, "2.0") || STREQ(tpmversion, "2")) { + def->tpmversion =3D VIR_DOMAIN_TPM_VERSION_2; + } else { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, + _("Unsupported TPM version '%s'"), + tpmversion); + } + switch (def->type) { case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: path =3D virXPathString("string(./backend/device/@path)", ctxt); @@ -12640,6 +12655,7 @@ virDomainTPMDefParseXML(virDomainXMLOptionPtr xmlop= t, VIR_FREE(model); VIR_FREE(backend); VIR_FREE(backends); + VIR_FREE(tpmversion); ctxt->node =3D save; return def; =20 @@ -24803,6 +24819,8 @@ virDomainTPMDefFormat(virBufferPtr buf, virBufferAdjustIndent(buf, 2); virBufferAsprintf(buf, "type)); + if (def->tpmversion =3D=3D VIR_DOMAIN_TPM_VERSION_2) + virBufferAddLit(buf, " tpmversion=3D'2'"); virBufferAdjustIndent(buf, 2); =20 switch (def->type) { @@ -29430,6 +29448,22 @@ virDomainCheckTPMChanges(virDomainDefPtr def, if (newDef->tpm->type !=3D def->tpm->type) { /* type changed */ virDomainTPMDeleteAny(def); + } else { + switch (def->tpm->type) { + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + if (def->tpm->tpmversion !=3D newDef->tpm->tpmversion) { + virReportError(VIR_ERR_CONFIG_UNSUPPORTED, "%s", + _("The version of the TPM cannot " + "be changed; the TPM must be removed " + "from the VM and a new TPM added; " + "Note: all secrets will be lost")); + ret =3D -1; + } + break; + case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: + case VIR_DOMAIN_TPM_TYPE_LAST: + break; + } } } =20 diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 1f479b0..4aab54b 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1289,12 +1289,18 @@ typedef enum { VIR_DOMAIN_TPM_TYPE_LAST } virDomainTPMBackendType; =20 +typedef enum { + VIR_DOMAIN_TPM_VERSION_1_2, + VIR_DOMAIN_TPM_VERSION_2, +} virDomainTPMVersion; + # define VIR_DOMAIN_TPM_DEFAULT_DEVICE "/dev/tpm0" =20 struct _virDomainTPMDef { virDomainTPMBackendType type; virDomainDeviceInfo info; virDomainTPMModel model; + virDomainTPMVersion tpmversion; union { struct { virDomainChrSourceDef source; diff --git a/src/util/virtpm.c b/src/util/virtpm.c index 354761e..7390895 100644 --- a/src/util/virtpm.c +++ b/src/util/virtpm.c @@ -52,6 +52,8 @@ static char *swtpm_path; static char *swtpm_setup; static char *swtpm_ioctl; =20 +static bool swtpm_supports_tpm2; + /** * virTPMCreateCancelPath: * @devpath: Path to the TPM device @@ -96,6 +98,40 @@ virTPMCreateCancelPath(const char *devpath) } =20 /* + * virTPMCheckForTPM2Support + * + * Check whether swtpm_setup supports TPM 2 + */ +static void +virTPMCheckForTPM2Support(void) +{ + virCommandPtr cmd; + char *help =3D NULL; + + if (!swtpm_setup) + return; + + cmd =3D virCommandNew(swtpm_setup); + if (!cmd) + return; + + virCommandAddArg(cmd, "--help"); + virCommandSetOutputBuffer(cmd, &help); + + if (virCommandRun(cmd, NULL) < 0) + goto cleanup; + + if (strstr(help, "--tpm2")) { + fprintf(stderr, "TPM2 is supported by swtpm_setup\n"); + swtpm_supports_tpm2 =3D true; + } + + cleanup: + virCommandFree(cmd); + VIR_FREE(help); +} + +/* * virTPMEmulatorInit * * Initialize the Emulator functions by searching for necessary @@ -134,6 +170,7 @@ virTPMEmulatorInit(void) VIR_FREE(swtpm_setup); return -1; } + virTPMCheckForTPM2Support(); } =20 if (!swtpm_ioctl) { @@ -311,12 +348,14 @@ virTPMTryConnect(const char *pathname, unsigned long = timeout_ms) * typically this should be 'tss' * @logfile: The file to write the log into; it must be writable * for the user given by userid or 'tss' + * @tpmversion: The version of the TPM, either a TPM 1.2 or TPM 2 * * Setup the external swtpm */ static int virTPMSetupEmulator(const char *storagepath, const unsigned char *vmuuid, - uid_t swtpm_user, const char *logfile) + uid_t swtpm_user, const char *logfile, + const virDomainTPMVersion tpmversion) { virCommandPtr cmd =3D NULL; int exitstatus; @@ -335,6 +374,18 @@ virTPMSetupEmulator(const char *storagepath, const uns= igned char *vmuuid, virCommandAddArg(cmd, "--runas"); virCommandAddArgFormat(cmd, "%u", swtpm_user); } + + switch (tpmversion) { + case VIR_DOMAIN_TPM_VERSION_1_2: + break; + case VIR_DOMAIN_TPM_VERSION_2: + virCommandAddArgList(cmd, "--tpm2", NULL); + if (!swtpm_supports_tpm2) { + goto cleanup; + } + break; + } + virCommandAddArgList(cmd, "--tpm-state", storagepath, "--vmid", uuid, @@ -400,7 +451,8 @@ virTPMEmulatorBuildCommand(virDomainTPMDefPtr tpm, cons= t unsigned char *vmuuid, goto error; =20 if (created && - virTPMSetupEmulator(storagepath, vmuuid, swtpm_user, logfile) < 0) + virTPMSetupEmulator(storagepath, vmuuid, swtpm_user, logfile, + tpm->tpmversion) < 0) goto error; =20 if (!(tpm->data.emulator.source.data.nix.path =3D @@ -437,6 +489,14 @@ virTPMEmulatorBuildCommand(virDomainTPMDefPtr tpm, con= st unsigned char *vmuuid, virCommandAllowCap(cmd, CAP_SETUID); } =20 + switch (tpm->tpmversion) { + case VIR_DOMAIN_TPM_VERSION_1_2: + break; + case VIR_DOMAIN_TPM_VERSION_2: + virCommandAddArg(cmd, "--tpm2"); + break; + } + tpm->data.emulator.storagepath =3D storagepath; VIR_FREE(tpm->data.emulator.logfile); tpm->data.emulator.logfile =3D logfile; diff --git a/tests/qemuxml2argvdata/tpm-emulator-tpm2.args b/tests/qemuxml2= argvdata/tpm-emulator-tpm2.args new file mode 100644 index 0000000..9418c74 --- /dev/null +++ b/tests/qemuxml2argvdata/tpm-emulator-tpm2.args @@ -0,0 +1,24 @@ +LC_ALL=3DC \ +PATH=3D/bin \ +HOME=3D/home/test \ +USER=3Dtest \ +LOGNAME=3Dtest \ +QEMU_AUDIO_DRV=3Dnone \ +/usr/bin/qemu-system-x86_64 \ +-name TPM-VM \ +-S \ +-M pc-0.12 \ +-m 2048 \ +-smp 1,sockets=3D1,cores=3D1,threads=3D1 \ +-uuid 11d7cd22-da89-3094-6212-079a48a309a1 \ +-nographic \ +-nodefaults \ +-chardev socket,id=3Dcharmonitor,path=3D/tmp/lib/domain--1-TPM-VM/monitor.= sock,\ +server,nowait \ +-mon chardev=3Dcharmonitor,id=3Dmonitor,mode=3Dreadline \ +-boot c \ +-usb \ +-tpmdev emulator,id=3Dtpm-tpm0,chardev=3Dchrtpm \ +-chardev socket,id=3Dchrtpm,path=3D/dev/test \ +-device tpm-tis,tpmdev=3Dtpm-tpm0,id=3Dtpm0 \ +-device virtio-balloon-pci,id=3Dballoon0,bus=3Dpci.0,addr=3D0x3 diff --git a/tests/qemuxml2argvdata/tpm-emulator-tpm2.xml b/tests/qemuxml2a= rgvdata/tpm-emulator-tpm2.xml new file mode 100644 index 0000000..070bedb --- /dev/null +++ b/tests/qemuxml2argvdata/tpm-emulator-tpm2.xml @@ -0,0 +1,30 @@ + + TPM-VM + 11d7cd22-da89-3094-6212-079a48a309a1 + 2097152 + 512288 + 1 + + hvm + + + + + + + + destroy + restart + destroy + + /usr/bin/qemu-system-x86_64 + + + + + + + + + + diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c index 92846c3..5d0650b 100644 --- a/tests/qemuxml2argvtest.c +++ b/tests/qemuxml2argvtest.c @@ -2156,6 +2156,9 @@ mymain(void) DO_TEST("tpm-emulator", QEMU_CAPS_DEVICE_TPM_PASSTHROUGH, QEMU_CAPS_DEVICE_TPM_EMULATO= R, QEMU_CAPS_DEVICE_TPM_TIS); + DO_TEST("tpm-emulator-tpm2", + QEMU_CAPS_DEVICE_TPM_PASSTHROUGH, QEMU_CAPS_DEVICE_TPM_EMULATO= R, + QEMU_CAPS_DEVICE_TPM_TIS); =20 =20 DO_TEST_PARSE_ERROR("pci-domain-invalid", NONE); diff --git a/tests/qemuxml2xmloutdata/tpm-emulator-tpm2.xml b/tests/qemuxml= 2xmloutdata/tpm-emulator-tpm2.xml new file mode 100644 index 0000000..4a68bd8 --- /dev/null +++ b/tests/qemuxml2xmloutdata/tpm-emulator-tpm2.xml @@ -0,0 +1,34 @@ + + TPM-VM + 11d7cd22-da89-3094-6212-079a48a309a1 + 2097152 + 512288 + 1 + + hvm + + + + + + + + destroy + restart + destroy + + /usr/bin/qemu-system-x86_64 + +
+ + + + + + + + +
+ + + --=20 2.5.5 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list From nobody Fri May 3 16:27:50 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1522965400757565.097242443969; Thu, 5 Apr 2018 14:56:40 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx06.intmail.prod.int.phx2.redhat.com [10.5.11.16]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 5BAB03D95C; Thu, 5 Apr 2018 21:56:39 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 21A427F64C; Thu, 5 Apr 2018 21:56:39 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 880B8180BAD4; Thu, 5 Apr 2018 21:56:38 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w35LuPJb021567 for ; Thu, 5 Apr 2018 17:56:25 -0400 Received: by smtp.corp.redhat.com (Postfix) id CC51E7D90D; Thu, 5 Apr 2018 21:56:25 +0000 (UTC) Received: from mx1.redhat.com (ext-mx04.extmail.prod.ext.phx2.redhat.com [10.5.110.28]) by smtp.corp.redhat.com (Postfix) with ESMTPS id C32657D923 for ; Thu, 5 Apr 2018 21:56:24 +0000 (UTC) Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 99AE880B29 for ; Thu, 5 Apr 2018 21:56:22 +0000 (UTC) Received: from pps.filterd (m0098416.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w35LsPAk175566 for ; Thu, 5 Apr 2018 17:56:22 -0400 Received: from e31.co.us.ibm.com (e31.co.us.ibm.com [32.97.110.149]) by mx0b-001b2d01.pphosted.com with ESMTP id 2h5rfhrv1a-1 (version=TLSv1.2 cipher=AES256-SHA256 bits=256 verify=NOT) for ; Thu, 05 Apr 2018 17:56:21 -0400 Received: from localhost by e31.co.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Thu, 5 Apr 2018 15:56:20 -0600 Received: from b03cxnp07029.gho.boulder.ibm.com (9.17.130.16) by e31.co.us.ibm.com (192.168.1.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; Thu, 5 Apr 2018 15:56:17 -0600 Received: from b03ledav002.gho.boulder.ibm.com (b03ledav002.gho.boulder.ibm.com [9.17.130.233]) by b03cxnp07029.gho.boulder.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w35LuHfE7143870; Thu, 5 Apr 2018 14:56:17 -0700 Received: from b03ledav002.gho.boulder.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 2F08B136046; Thu, 5 Apr 2018 15:56:17 -0600 (MDT) Received: from sbct-3.watson.ibm.com (unknown [9.47.158.153]) by b03ledav002.gho.boulder.ibm.com (Postfix) with ESMTP id DA394136040; Thu, 5 Apr 2018 15:56:16 -0600 (MDT) From: Stefan Berger To: libvir-list@redhat.com Date: Thu, 5 Apr 2018 17:56:06 -0400 In-Reply-To: <1522965366-836-1-git-send-email-stefanb@linux.vnet.ibm.com> References: <1522965366-836-1-git-send-email-stefanb@linux.vnet.ibm.com> X-TM-AS-GCONF: 00 x-cbid: 18040521-8235-0000-0000-00000D45B6E9 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00008810; HX=3.00000241; KW=3.00000007; PH=3.00000004; SC=3.00000256; SDB=6.01013642; UDB=6.00516698; IPR=6.00792902; MB=3.00020438; MTD=3.00000008; XFM=3.00000015; UTC=2018-04-05 21:56:18 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18040521-8236-0000-0000-0000405CCC8E Message-Id: <1522965366-836-7-git-send-email-stefanb@linux.vnet.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10432:, , definitions=2018-04-05_09:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=3 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 impostorscore=0 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1709140000 definitions=main-1804050222 X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 207 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 05 Apr 2018 21:56:22 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.28]); Thu, 05 Apr 2018 21:56:22 +0000 (UTC) for IP:'148.163.158.5' DOMAIN:'mx0b-001b2d01.pphosted.com' HELO:'mx0a-001b2d01.pphosted.com' FROM:'stefanb@linux.vnet.ibm.com' RCPT:'' X-RedHat-Spam-Score: -0.7 (RCVD_IN_DNSWL_LOW) 148.163.158.5 mx0b-001b2d01.pphosted.com 148.163.158.5 mx0b-001b2d01.pphosted.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.28 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH 6/6] tpm: Add swtpm to emulator cgroup X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.16 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.30]); Thu, 05 Apr 2018 21:56:39 +0000 (UTC) X-ZohoMail: RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Add the external swtpm to the emulator cgroup so that upper limits of CPU usage can be enforced on the emulated TPM. To enable this we need to have the swtpm write its pid into a file. We then take the pid from this file to configure the emulator cgroup. Signed-off-by: Stefan Berger --- src/conf/domain_conf.c | 1 + src/conf/domain_conf.h | 1 + src/libvirt_private.syms | 1 + src/qemu/qemu_cgroup.c | 53 ++++++++++++++++++++++++++++++++++++++++ src/qemu/qemu_cgroup.h | 1 + src/qemu/qemu_extdevice.c | 40 +++++++++++++++++++++++++------ src/qemu/qemu_extdevice.h | 2 +- src/qemu/qemu_process.c | 8 +++++-- src/util/vircgroup.c | 42 ++++++++++++++++++++++++++++++++ src/util/vircgroup.h | 1 + src/util/virtpm.c | 61 +++++++++++++++++++++++++++++++++++++++++++= ++-- src/util/virtpm.h | 6 +++-- 12 files changed, 203 insertions(+), 14 deletions(-) diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c index 5498e2e..1b41584 100644 --- a/src/conf/domain_conf.c +++ b/src/conf/domain_conf.c @@ -2625,6 +2625,7 @@ void virDomainTPMDefFree(virDomainTPMDefPtr def) VIR_FREE(def->data.emulator.source.data.nix.path); VIR_FREE(def->data.emulator.storagepath); VIR_FREE(def->data.emulator.logfile); + VIR_FREE(def->data.emulator.pidfile); break; case VIR_DOMAIN_TPM_TYPE_LAST: break; diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h index 4aab54b..29ef7ff 100644 --- a/src/conf/domain_conf.h +++ b/src/conf/domain_conf.h @@ -1310,6 +1310,7 @@ struct _virDomainTPMDef { /* swtpm storage path and logfile */ char *storagepath; char *logfile; + char *pidfile; } emulator; } data; }; diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 488882d..db69b56 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1468,6 +1468,7 @@ virBufferVasprintf; =20 # util/vircgroup.h virCgroupAddMachineTask; +virCgroupAddProc; virCgroupAddTask; virCgroupAddTaskController; virCgroupAllowAllDevices; diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c index bd4859c..859ed55 100644 --- a/src/qemu/qemu_cgroup.c +++ b/src/qemu/qemu_cgroup.c @@ -37,6 +37,7 @@ #include "virtypedparam.h" #include "virnuma.h" #include "virsystemd.h" +#include "virpidfile.h" =20 #define VIR_FROM_THIS VIR_FROM_QEMU =20 @@ -1106,6 +1107,58 @@ qemuSetupCgroupCpusetCpus(virCgroupPtr cgroup, =20 =20 int +qemuSetupCgroupForExtDevices(virDomainObjPtr vm) +{ + qemuDomainObjPrivatePtr priv =3D vm->privateData; + virDomainTPMDefPtr tpm =3D vm->def->tpm; + virCgroupPtr cgroup_temp =3D NULL; + pid_t pid; + int ret =3D -1; + + if (priv->cgroup =3D=3D NULL) + return 0; /* Not supported, so claim success */ + + /* + * If CPU cgroup controller is not initialized here, then we need + * neither period nor quota settings. And if CPUSET controller is + * not initialized either, then there's nothing to do anyway. + */ + if (!virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPU) && + !virCgroupHasController(priv->cgroup, VIR_CGROUP_CONTROLLER_CPUSET= )) + return 0; + + if (virCgroupNewThread(priv->cgroup, VIR_CGROUP_THREAD_EMULATOR, 0, + false, &cgroup_temp) < 0) + goto cleanup; + + if (tpm) { + switch (tpm->type) { + case VIR_DOMAIN_TPM_TYPE_EMULATOR: + if (virPidFileReadPath(tpm->data.emulator.pidfile, &pid) < 0) { + virReportError(VIR_ERR_INTERNAL_ERROR, + _("Could not read swtpm's pidfile %s"), + tpm->data.emulator.pidfile); + goto cleanup; + } + if (virCgroupAddProc(cgroup_temp, pid) < 0) + goto cleanup; + break; + case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: + case VIR_DOMAIN_TPM_TYPE_LAST: + break; + } + } + + ret =3D 0; + +cleanup: + virCgroupFree(&cgroup_temp); + + return ret; +} + + +int qemuSetupGlobalCpuCgroup(virDomainObjPtr vm) { qemuDomainObjPrivatePtr priv =3D vm->privateData; diff --git a/src/qemu/qemu_cgroup.h b/src/qemu/qemu_cgroup.h index 3b8ff60..478bf7e 100644 --- a/src/qemu/qemu_cgroup.h +++ b/src/qemu/qemu_cgroup.h @@ -69,6 +69,7 @@ int qemuSetupCgroupVcpuBW(virCgroupPtr cgroup, long long quota); int qemuSetupCgroupCpusetCpus(virCgroupPtr cgroup, virBitmapPtr cpumask); int qemuSetupGlobalCpuCgroup(virDomainObjPtr vm); +int qemuSetupCgroupForExtDevices(virDomainObjPtr vm); int qemuRemoveCgroup(virDomainObjPtr vm); =20 typedef struct _qemuCgroupEmulatorAllNodesData qemuCgroupEmulatorAllNodesD= ata; diff --git a/src/qemu/qemu_extdevice.c b/src/qemu/qemu_extdevice.c index e685faf..439cb31 100644 --- a/src/qemu/qemu_extdevice.c +++ b/src/qemu/qemu_extdevice.c @@ -35,6 +35,7 @@ =20 VIR_LOG_INIT("qemu.qemu_extdevice") =20 + static int qemuExtDeviceLogCommand(qemuDomainLogContextPtr logCtxt, virCommandPtr cmd, @@ -93,11 +94,15 @@ qemuExtTPMStartEmulator(virQEMUDriverPtr driver, virDomainDefPtr def =3D vm->def; unsigned char *vmuuid =3D def->uuid; virDomainTPMDefPtr tpm =3D def->tpm; + char *pidfiledata =3D NULL; + int timeout; + int len; =20 /* stop any left-over TPM emulator for this VM */ - virTPMStopEmulator(tpm, vmuuid, false); + virTPMStopEmulator(tpm, vmuuid, false, cfg->stateDir, def->name); =20 - if (!(cmd =3D virTPMEmulatorBuildCommand(tpm, vmuuid, cfg->swtpm_user)= )) + if (!(cmd =3D virTPMEmulatorBuildCommand(tpm, vmuuid, cfg->swtpm_user, + cfg->stateDir, def->name))) goto cleanup; =20 if (qemuExtDeviceLogCommand(logCtxt, cmd, "TPM Emulator") < 0) @@ -147,6 +152,22 @@ qemuExtTPMStartEmulator(virQEMUDriverPtr driver, goto error; } =20 + /* check that the swtpm has written its pid into the file */ + timeout =3D 1000; /* ms */ + while ((len =3D virFileReadHeaderQuiet(tpm->data.emulator.pidfile, + 10, &pidfiledata)) <=3D 0) { + if (len =3D=3D 0 && timeout > 0) { + timeout -=3D 50; + usleep(50 * 1000); + continue; + } + virReportError(VIR_ERR_INTERNAL_ERROR, + _("swtpm did not write pidfile '%s'"), + tpm->data.emulator.pidfile); + goto error; + } + VIR_FREE(pidfiledata); + ret =3D 0; =20 cleanup: @@ -158,7 +179,7 @@ qemuExtTPMStartEmulator(virQEMUDriverPtr driver, return ret; =20 error: - virTPMStopEmulator(tpm, vmuuid, false); + virTPMStopEmulator(tpm, vmuuid, false, cfg->stateDir, def->name); VIR_FREE(tpm->data.emulator.source.data.nix.path); =20 goto cleanup; @@ -186,11 +207,15 @@ qemuExtTPMStart(virQEMUDriverPtr driver, } =20 static void -qemuExtTPMStop(virDomainObjPtr vm) +qemuExtTPMStop(virQEMUDriverPtr driver, + virDomainObjPtr vm) { + virQEMUDriverConfigPtr cfg =3D virQEMUDriverGetConfig(driver); + switch (vm->def->tpm->type) { case VIR_DOMAIN_TPM_TYPE_EMULATOR: - virTPMStopEmulator(vm->def->tpm, vm->def->uuid, false); + virTPMStopEmulator(vm->def->tpm, vm->def->uuid, false, + cfg->stateDir, vm->def->name); break; case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH: case VIR_DOMAIN_TPM_TYPE_LAST: @@ -212,8 +237,9 @@ qemuExtDevicesStart(virQEMUDriverPtr driver, } =20 void -qemuExtDevicesStop(virDomainObjPtr vm) +qemuExtDevicesStop(virQEMUDriverPtr driver, + virDomainObjPtr vm) { if (vm->def->tpm) - qemuExtTPMStop(vm); + qemuExtTPMStop(driver, vm); } diff --git a/src/qemu/qemu_extdevice.h b/src/qemu/qemu_extdevice.h index 4dcaec3..3bf81ad 100644 --- a/src/qemu/qemu_extdevice.h +++ b/src/qemu/qemu_extdevice.h @@ -30,7 +30,7 @@ int qemuExtDevicesStart(virQEMUDriverPtr driver, qemuDomainLogContextPtr logCtxt) ATTRIBUTE_RETURN_CHECK; =20 -void qemuExtDevicesStop(virDomainObjPtr vm); +void qemuExtDevicesStop(virQEMUDriverPtr driver, virDomainObjPtr vm); =20 #endif /* __QEMU_EXTDEVICE_H__ */ =20 diff --git a/src/qemu/qemu_process.c b/src/qemu/qemu_process.c index 26acfab..804412a 100644 --- a/src/qemu/qemu_process.c +++ b/src/qemu/qemu_process.c @@ -6068,6 +6068,10 @@ qemuProcessLaunch(virConnectPtr conn, if (qemuProcessSetupEmulator(vm) < 0) goto cleanup; =20 + VIR_DEBUG("Setting cgroup for external devices (if required)"); + if (qemuSetupCgroupForExtDevices(vm) < 0) + goto cleanup; + VIR_DEBUG("Setting up resctrl"); if (qemuProcessResctrlCreate(driver, vm) < 0) goto cleanup; @@ -6196,7 +6200,7 @@ qemuProcessLaunch(virConnectPtr conn, =20 cleanup: if (ret) - qemuExtDevicesStop(vm); + qemuExtDevicesStop(driver, vm); qemuDomainSecretDestroy(vm); virCommandFree(cmd); virObjectUnref(logCtxt); @@ -6563,7 +6567,7 @@ void qemuProcessStop(virQEMUDriverPtr driver, /* Clear network bandwidth */ virDomainClearNetBandwidth(vm); =20 - qemuExtDevicesStop(vm); + qemuExtDevicesStop(driver, vm); =20 virDomainConfVMNWFilterTeardown(vm); =20 diff --git a/src/util/vircgroup.c b/src/util/vircgroup.c index 0a31947..4809f12 100644 --- a/src/util/vircgroup.c +++ b/src/util/vircgroup.c @@ -1245,6 +1245,38 @@ virCgroupAddMachineTask(virCgroupPtr group, pid_t pi= d) return virCgroupAddTaskInternal(group, pid, true); } =20 +/** + * virCgroupAddProc: + * + * @group: The cgroup to add a process to + * @pid: The pid of the process to add + * + * Returns: 0 on success, -1 on error + */ +int +virCgroupAddProc(virCgroupPtr group, pid_t pid) +{ + int ret =3D -1; + size_t i; + + for (i =3D 0; i < VIR_CGROUP_CONTROLLER_LAST; i++) { + /* Skip over controllers not mounted */ + if (!group->controllers[i].mountPoint) + continue; + + /* We must never add tasks in systemd's hierarchy */ + if (i =3D=3D VIR_CGROUP_CONTROLLER_SYSTEMD) + continue; + + if (virCgroupSetValueU64(group, i, "cgroup.procs", pid) < 0) + goto cleanup; + } + + ret =3D 0; + cleanup: + return ret; +} + =20 /** * virCgroupAddTaskController: @@ -4298,6 +4330,16 @@ virCgroupAddMachineTask(virCgroupPtr group ATTRIBUTE= _UNUSED, =20 =20 int +virCgroupAddProc(virCgroupPtr group ATTRIBUTE_UNUSED, + pid_t pid ATTRIBUTE_UNUSED) +{ + virReportSystemError(ENXIO, "%s", + _("Control groups not supported on this platform"= )); + return -1; +} + + +int virCgroupAddTaskController(virCgroupPtr group ATTRIBUTE_UNUSED, pid_t pid ATTRIBUTE_UNUSED, int controller ATTRIBUTE_UNUSED) diff --git a/src/util/vircgroup.h b/src/util/vircgroup.h index d833927..82b3964 100644 --- a/src/util/vircgroup.h +++ b/src/util/vircgroup.h @@ -132,6 +132,7 @@ int virCgroupPathOfController(virCgroupPtr group, =20 int virCgroupAddTask(virCgroupPtr group, pid_t pid); int virCgroupAddMachineTask(virCgroupPtr group, pid_t pid); +int virCgroupAddProc(virCgroupPtr group, pid_t pid); =20 int virCgroupAddTaskController(virCgroupPtr group, pid_t pid, diff --git a/src/util/virtpm.c b/src/util/virtpm.c index 7390895..6c4a024 100644 --- a/src/util/virtpm.c +++ b/src/util/virtpm.c @@ -39,6 +39,7 @@ #include "virlog.h" #include "virtpm.h" #include "virutil.h" +#include "virpidfile.h" #include "configmake.h" =20 #define VIR_FROM_THIS VIR_FROM_NONE @@ -420,11 +421,31 @@ virTPMSetupEmulator(const char *storagepath, const un= signed char *vmuuid, } =20 /* + * virTPMGetPidfile + */ +static char *virTPMGetPidfile(const char *stateDir, const char *vmname) +{ + char *pidfile =3D NULL; + char *devname =3D NULL; + + if (virAsprintf(&devname, "%s-swtpm", vmname) < 0) + return NULL; + + pidfile =3D virPidFileBuildPath(stateDir, devname); + + VIR_FREE(devname); + + return pidfile; +} + +/* * virTPMBuildEmulatorCommand * * @tpm: TPM definition * @vmuuid: The UUID of the VM * @swtpm_user: The uid for the swtpm to run as (drop privileges to from r= oot) + * @statedir: libvirt's statedir + * @vmname: name of the VM * * Create the virCommand use for starting the emulator * Do some initializations on the way, such as creation of storage @@ -432,12 +453,15 @@ virTPMSetupEmulator(const char *storagepath, const un= signed char *vmuuid, */ virCommandPtr virTPMEmulatorBuildCommand(virDomainTPMDefPtr tpm, const unsigned char *vm= uuid, - uid_t swtpm_user) + uid_t swtpm_user, const char *stateDir, + const char *vmname) { virCommandPtr cmd =3D NULL; char *storagepath =3D NULL; char *logfile =3D NULL; + char *pidfile =3D NULL; bool created =3D false; + int pidfilefd =3D -1; =20 if (virTPMEmulatorInit() < 0) return NULL; @@ -497,9 +521,31 @@ virTPMEmulatorBuildCommand(virDomainTPMDefPtr tpm, con= st unsigned char *vmuuid, break; } =20 + if (!(pidfile =3D virTPMGetPidfile(stateDir, vmname))) + goto error; + + /* + * We create the pidfile and pass it as a file descriptor so + * the swtpm can write its pid after daemonizing. + * swtpm_user cannot delete files in stateDir, so we have to + * delete it later as well. + */ + pidfilefd =3D virFileOpenAs(pidfile, O_CREAT|O_WRONLY|O_TRUNC, 0644, + 0, 0, VIR_FILE_OPEN_NOFORK); + if (pidfilefd < 0) { + virReportSystemError(errno, + _("Could not open file %s for writing"), + pidfile); + goto error; + } + virCommandAddArg(cmd, "--pid"); + virCommandAddArgFormat(cmd, "fd=3D%u", pidfilefd); + virCommandPassFD(cmd, pidfilefd, VIR_COMMAND_PASS_FD_CLOSE_PARENT); + tpm->data.emulator.storagepath =3D storagepath; VIR_FREE(tpm->data.emulator.logfile); tpm->data.emulator.logfile =3D logfile; + tpm->data.emulator.pidfile =3D pidfile; =20 return cmd; =20 @@ -510,6 +556,8 @@ virTPMEmulatorBuildCommand(virDomainTPMDefPtr tpm, cons= t unsigned char *vmuuid, VIR_FREE(tpm->data.emulator.source.data.nix.path); VIR_FREE(storagepath); VIR_FREE(logfile); + VIR_FREE(pidfile); + VIR_FORCE_CLOSE(pidfilefd); =20 virCommandFree(cmd); =20 @@ -521,17 +569,20 @@ virTPMEmulatorBuildCommand(virDomainTPMDefPtr tpm, co= nst unsigned char *vmuuid, * @tpm: TPM definition * @vmuuid: the UUID of the VM * @verbose: whether to report errors + * @statedir: libvirt's statedir + * @vmname: name of the VM * * Gracefully stop the swptm */ void virTPMStopEmulator(virDomainTPMDefPtr tpm, const unsigned char *vmuuid, - bool verbose) + bool verbose, const char *stateDir, const char *vmname) { virCommandPtr cmd; int exitstatus; char *pathname; char *errbuf =3D NULL; + char *pidfile; =20 (void)vmuuid; if (virTPMEmulatorInit() < 0) @@ -563,6 +614,12 @@ virTPMStopEmulator(virDomainTPMDefPtr tpm, const unsig= ned char *vmuuid, unlink(pathname); VIR_FREE(pathname); =20 + /* clean up the PID file */ + if ((pidfile =3D virTPMGetPidfile(stateDir, vmname))) { + unlink(pidfile); + VIR_FREE(pidfile); + } + VIR_FREE(tpm->data.emulator.source.data.nix.path); tpm->data.emulator.source.type =3D 0; VIR_FREE(errbuf); diff --git a/src/util/virtpm.h b/src/util/virtpm.h index 424718b..9a6eccb 100644 --- a/src/util/virtpm.h +++ b/src/util/virtpm.h @@ -30,9 +30,11 @@ typedef virDomainTPMDef *virDomainTPMDefPtr; char *virTPMCreateCancelPath(const char *devpath) ATTRIBUTE_NOINLINE; virCommandPtr virTPMEmulatorBuildCommand(virDomainTPMDefPtr tpm, const unsigned char *vmuuid, - uid_t swtpm_user) ATTRIBUTE_RETURN_CHECK; + uid_t swtpm_user, + const char *stateDir, + const char *vmname) ATTRIBUTE_RETURN_CHECK; void virTPMStopEmulator(virDomainTPMDefPtr tpm, const unsigned char *vmuui= d, - bool verbose); + bool verbose, const char *stateDir, const char *vmn= ame); void virTPMDeleteEmulatorStorage(const unsigned char *vmuuid); int virTPMTryConnect(const char *pathname, unsigned long timeout_ms); =20 --=20 2.5.5 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list