[PATCH 15/16] conf: Properly instantiate virDomainChrSourceDef in virDomainTPMDef

Peter Krempa pkrempa at redhat.com
Thu Nov 18 16:33:40 UTC 2021


'virDomainChrSourceDef' contains private data so 'virDomainChrSourceDefNew'
must be used to allocate it. 'virDomainTPMDef' was using it directly
which won't work with the chardev helper functions.

Convert it to a pointer to properly allocate private data.

Signed-off-by: Peter Krempa <pkrempa at redhat.com>
---
 src/conf/domain_audit.c         |  4 ++--
 src/conf/domain_conf.c          | 14 +++++++++-----
 src/conf/domain_conf.h          |  4 ++--
 src/qemu/qemu_cgroup.c          |  2 +-
 src/qemu/qemu_command.c         |  6 +++---
 src/qemu/qemu_namespace.c       |  2 +-
 src/qemu/qemu_tpm.c             | 10 +++++-----
 src/security/security_dac.c     |  6 +++---
 src/security/security_selinux.c |  6 +++---
 tests/qemuxml2argvtest.c        |  6 +++---
 10 files changed, 32 insertions(+), 28 deletions(-)

diff --git a/src/conf/domain_audit.c b/src/conf/domain_audit.c
index 69c5792b07..17a01c51ba 100644
--- a/src/conf/domain_audit.c
+++ b/src/conf/domain_audit.c
@@ -536,7 +536,7 @@ virDomainAuditTPM(virDomainObj *vm, virDomainTPMDef *tpm,

     switch (tpm->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
-        path = tpm->data.passthrough.source.data.file.path;
+        path = tpm->data.passthrough.source->data.file.path;
         if (!(device = virAuditEncode("device", VIR_AUDIT_STR(path)))) {
             VIR_WARN("OOM while encoding audit message");
             goto cleanup;
@@ -547,7 +547,7 @@ virDomainAuditTPM(virDomainObj *vm, virDomainTPMDef *tpm,
                   virt, reason, vmname, uuidstr, device);
         break;
     case VIR_DOMAIN_TPM_TYPE_EMULATOR:
-        path = tpm->data.emulator.source.data.nix.path;
+        path = tpm->data.emulator.source->data.nix.path;
         if (!(device = virAuditEncode("device", VIR_AUDIT_STR(path)))) {
             VIR_WARN("OOM while encoding audit message");
             goto cleanup;
diff --git a/src/conf/domain_conf.c b/src/conf/domain_conf.c
index 52f513f488..7231d8fc3f 100644
--- a/src/conf/domain_conf.c
+++ b/src/conf/domain_conf.c
@@ -3211,10 +3211,10 @@ void virDomainTPMDefFree(virDomainTPMDef *def)

     switch (def->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
-        virDomainChrSourceDefClear(&def->data.passthrough.source);
+        virObjectUnref(def->data.passthrough.source);
         break;
     case VIR_DOMAIN_TPM_TYPE_EMULATOR:
-        virDomainChrSourceDefClear(&def->data.emulator.source);
+        virObjectUnref(def->data.emulator.source);
         g_free(def->data.emulator.storagepath);
         g_free(def->data.emulator.logfile);
         break;
@@ -11831,13 +11831,17 @@ virDomainTPMDefParseXML(virDomainXMLOption *xmlopt,

     switch (def->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
+        if (!(def->data.passthrough.source = virDomainChrSourceDefNew(xmlopt)))
+            goto error;
         path = virXPathString("string(./backend/device/@path)", ctxt);
         if (!path)
             path = g_strdup(VIR_DOMAIN_TPM_DEFAULT_DEVICE);
-        def->data.passthrough.source.data.file.path = g_steal_pointer(&path);
-        def->data.passthrough.source.type = VIR_DOMAIN_CHR_TYPE_DEV;
+        def->data.passthrough.source->type = VIR_DOMAIN_CHR_TYPE_DEV;
+        def->data.passthrough.source->data.file.path = g_steal_pointer(&path);
         break;
     case VIR_DOMAIN_TPM_TYPE_EMULATOR:
+        if (!(def->data.emulator.source = virDomainChrSourceDefNew(xmlopt)))
+            goto error;
         secretuuid = virXPathString("string(./backend/encryption/@secret)", ctxt);
         if (secretuuid) {
             if (virUUIDParse(secretuuid, def->data.emulator.secretuuid) < 0) {
@@ -25456,7 +25460,7 @@ virDomainTPMDefFormat(virBuffer *buf,
         virBufferAddLit(buf, ">\n");
         virBufferAdjustIndent(buf, 2);
         virBufferEscapeString(buf, "<device path='%s'/>\n",
-                              def->data.passthrough.source.data.file.path);
+                              def->data.passthrough.source->data.file.path);
         virBufferAdjustIndent(buf, -2);
         virBufferAddLit(buf, "</backend>\n");
         break;
diff --git a/src/conf/domain_conf.h b/src/conf/domain_conf.h
index 3cb68c5d0a..c1b2a814aa 100644
--- a/src/conf/domain_conf.h
+++ b/src/conf/domain_conf.h
@@ -1381,10 +1381,10 @@ struct _virDomainTPMDef {
     int version; /* virDomainTPMVersion */
     union {
         struct {
-            virDomainChrSourceDef source;
+            virDomainChrSourceDef *source;
         } passthrough;
         struct {
-            virDomainChrSourceDef source;
+            virDomainChrSourceDef *source;
             char *storagepath;
             char *logfile;
             unsigned char secretuuid[VIR_UUID_BUFLEN];
diff --git a/src/qemu/qemu_cgroup.c b/src/qemu/qemu_cgroup.c
index 471cbc3b8f..1e7b562b33 100644
--- a/src/qemu/qemu_cgroup.c
+++ b/src/qemu/qemu_cgroup.c
@@ -340,7 +340,7 @@ qemuSetupTPMCgroup(virDomainObj *vm,

     switch (dev->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
-        ret = qemuSetupChrSourceCgroup(vm, &dev->data.passthrough.source);
+        ret = qemuSetupChrSourceCgroup(vm, dev->data.passthrough.source);
         break;
     case VIR_DOMAIN_TPM_TYPE_EMULATOR:
     case VIR_DOMAIN_TPM_TYPE_LAST:
diff --git a/src/qemu/qemu_command.c b/src/qemu/qemu_command.c
index f3b02d3438..623e3a20a9 100644
--- a/src/qemu/qemu_command.c
+++ b/src/qemu/qemu_command.c
@@ -9947,7 +9947,7 @@ qemuBuildTPMBackendStr(virCommand *cmd,

     switch (tpm->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
-        tpmdev = tpm->data.passthrough.source.data.file.path;
+        tpmdev = tpm->data.passthrough.source->data.file.path;
         if (!(cancel_path = virTPMCreateCancelPath(tpmdev)))
             return NULL;

@@ -9972,7 +9972,7 @@ qemuBuildTPMBackendStr(virCommand *cmd,
         virBufferAddLit(&buf, ",chardev=chrtpm");

         *chardev = g_strdup_printf("socket,id=chrtpm,path=%s",
-                                   tpm->data.emulator.source.data.nix.path);
+                                   tpm->data.emulator.source->data.nix.path);

         break;
     case VIR_DOMAIN_TPM_TYPE_LAST:
@@ -10041,7 +10041,7 @@ qemuBuildTPMProxyCommandLine(virCommand *cmd,
     if (virJSONValueObjectAdd(&props,
                               "s:driver", virDomainTPMModelTypeToString(tpm->model),
                               "s:id", tpm->info.alias,
-                              "s:host-path", tpm->data.passthrough.source.data.file.path,
+                              "s:host-path", tpm->data.passthrough.source->data.file.path,
                               NULL) < 0)
         return -1;

diff --git a/src/qemu/qemu_namespace.c b/src/qemu/qemu_namespace.c
index f1aaca86b1..23b1160c5e 100644
--- a/src/qemu/qemu_namespace.c
+++ b/src/qemu/qemu_namespace.c
@@ -422,7 +422,7 @@ qemuDomainSetupTPM(virDomainTPMDef *dev,
 {
     switch (dev->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
-        *paths = g_slist_prepend(*paths, g_strdup(dev->data.passthrough.source.data.file.path));
+        *paths = g_slist_prepend(*paths, g_strdup(dev->data.passthrough.source->data.file.path));
         break;

     case VIR_DOMAIN_TPM_TYPE_EMULATOR:
diff --git a/src/qemu/qemu_tpm.c b/src/qemu/qemu_tpm.c
index 7d05394356..62f54f56ab 100644
--- a/src/qemu/qemu_tpm.c
+++ b/src/qemu/qemu_tpm.c
@@ -332,11 +332,11 @@ qemuTPMEmulatorPrepareHost(virDomainTPMDef *tpm,
         return -1;

     /* create the socket filename */
-    if (!tpm->data.emulator.source.data.nix.path &&
-        !(tpm->data.emulator.source.data.nix.path =
+    if (!tpm->data.emulator.source->data.nix.path &&
+        !(tpm->data.emulator.source->data.nix.path =
           qemuTPMCreateEmulatorSocket(swtpmStateDir, shortName)))
         return -1;
-    tpm->data.emulator.source.type = VIR_DOMAIN_CHR_TYPE_UNIX;
+    tpm->data.emulator.source->type = VIR_DOMAIN_CHR_TYPE_UNIX;

     return 0;
 }
@@ -716,7 +716,7 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDef *tpm,
                                    secretuuid) < 0)
         goto error;

-    unlink(tpm->data.emulator.source.data.nix.path);
+    unlink(tpm->data.emulator.source->data.nix.path);

     cmd = virCommandNew(swtpm);
     if (!cmd)
@@ -726,7 +726,7 @@ qemuTPMEmulatorBuildCommand(virDomainTPMDef *tpm,

     virCommandAddArgList(cmd, "socket", "--daemon", "--ctrl", NULL);
     virCommandAddArgFormat(cmd, "type=unixio,path=%s,mode=0600",
-                           tpm->data.emulator.source.data.nix.path);
+                           tpm->data.emulator.source->data.nix.path);

     virCommandAddArg(cmd, "--tpmstate");
     virCommandAddArgFormat(cmd, "dir=%s,mode=0600",
diff --git a/src/security/security_dac.c b/src/security/security_dac.c
index 1733d63410..e9e316551e 100644
--- a/src/security/security_dac.c
+++ b/src/security/security_dac.c
@@ -1686,12 +1686,12 @@ virSecurityDACSetTPMFileLabel(virSecurityManager *mgr,
     switch (tpm->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
         ret = virSecurityDACSetChardevLabelHelper(mgr, def,
-                                                  &tpm->data.passthrough.source,
+                                                  tpm->data.passthrough.source,
                                                   false, false);
         break;
     case VIR_DOMAIN_TPM_TYPE_EMULATOR:
         ret = virSecurityDACSetChardevLabelHelper(mgr, def,
-                                                  &tpm->data.emulator.source,
+                                                  tpm->data.emulator.source,
                                                   false, false);
         break;
     case VIR_DOMAIN_TPM_TYPE_LAST:
@@ -1712,7 +1712,7 @@ virSecurityDACRestoreTPMFileLabel(virSecurityManager *mgr,
     switch (tpm->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
         ret = virSecurityDACRestoreChardevLabelHelper(mgr, def,
-                                                      &tpm->data.passthrough.source,
+                                                      tpm->data.passthrough.source,
                                                       false, false);
         break;
     case VIR_DOMAIN_TPM_TYPE_EMULATOR:
diff --git a/src/security/security_selinux.c b/src/security/security_selinux.c
index 622a8f4c02..840a05844e 100644
--- a/src/security/security_selinux.c
+++ b/src/security/security_selinux.c
@@ -1637,7 +1637,7 @@ virSecuritySELinuxSetTPMFileLabel(virSecurityManager *mgr,

     switch (tpm->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
-        tpmdev = tpm->data.passthrough.source.data.file.path;
+        tpmdev = tpm->data.passthrough.source->data.file.path;
         rc = virSecuritySELinuxSetFilecon(mgr, tpmdev, seclabel->imagelabel, false);
         if (rc < 0)
             return -1;
@@ -1656,7 +1656,7 @@ virSecuritySELinuxSetTPMFileLabel(virSecurityManager *mgr,
         }
         break;
     case VIR_DOMAIN_TPM_TYPE_EMULATOR:
-        tpmdev = tpm->data.emulator.source.data.nix.path;
+        tpmdev = tpm->data.emulator.source->data.nix.path;
         rc = virSecuritySELinuxSetFilecon(mgr, tpmdev, seclabel->imagelabel, false);
         if (rc < 0)
             return -1;
@@ -1685,7 +1685,7 @@ virSecuritySELinuxRestoreTPMFileLabelInt(virSecurityManager *mgr,

     switch (tpm->type) {
     case VIR_DOMAIN_TPM_TYPE_PASSTHROUGH:
-        tpmdev = tpm->data.passthrough.source.data.file.path;
+        tpmdev = tpm->data.passthrough.source->data.file.path;
         rc = virSecuritySELinuxRestoreFileLabel(mgr, tpmdev, false);

         if ((cancel_path = virTPMCreateCancelPath(tpmdev)) != NULL) {
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
index 161e7efa62..1d0d6e14ba 100644
--- a/tests/qemuxml2argvtest.c
+++ b/tests/qemuxml2argvtest.c
@@ -450,9 +450,9 @@ testCompareXMLToArgvCreateArgs(virQEMUDriver *drv,
         if (vm->def->tpms[i]->type != VIR_DOMAIN_TPM_TYPE_EMULATOR)
             continue;

-        VIR_FREE(vm->def->tpms[i]->data.emulator.source.data.file.path);
-        vm->def->tpms[i]->data.emulator.source.data.file.path = g_strdup("/dev/test");
-        vm->def->tpms[i]->data.emulator.source.type = VIR_DOMAIN_CHR_TYPE_FILE;
+        VIR_FREE(vm->def->tpms[i]->data.emulator.source->data.file.path);
+        vm->def->tpms[i]->data.emulator.source->data.file.path = g_strdup("/dev/test");
+        vm->def->tpms[i]->data.emulator.source->type = VIR_DOMAIN_CHR_TYPE_FILE;
     }

     for (i = 0; i < vm->def->nvideos; i++) {
-- 
2.31.1




More information about the libvir-list mailing list