]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
conf: Properly instantiate virDomainChrSourceDef in virDomainTPMDef
authorPeter Krempa <pkrempa@redhat.com>
Fri, 5 Nov 2021 15:51:22 +0000 (16:51 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Fri, 19 Nov 2021 11:38:35 +0000 (12:38 +0100)
'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@redhat.com>
Reviewed-by: Ján Tomko <jtomko@redhat.com>
src/conf/domain_audit.c
src/conf/domain_conf.c
src/conf/domain_conf.h
src/qemu/qemu_cgroup.c
src/qemu/qemu_command.c
src/qemu/qemu_namespace.c
src/qemu/qemu_tpm.c
src/security/security_dac.c
src/security/security_selinux.c
tests/qemuxml2argvtest.c

index 69c5792b07fe33263a6a4beb29b24e3b31bb40c9..17a01c51ba55d908cd25d4a8c071f8434e6ab283 100644 (file)
@@ -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;
index 52f513f488cbe5058aa26511a75554233fc70538..7231d8fc3f4e8f86e1baba6e2fd2595e201500dd 100644 (file)
@@ -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;
index f222d8ca88ac0f21a05e3297cb844a4c652a3e52..86349603132f1f53fcc597db52ebcb793c2f15de 100644 (file)
@@ -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];
index 471cbc3b8f719129ceb9639f0808fe18f6149164..1e7b562b33575e46ed4d9ba327f6f424f38ae23c 100644 (file)
@@ -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:
index f3b02d343820fd14f27700f6e5e371d2255c8409..623e3a20a9ce7bf3cf1d769849f524c1378d90f2 100644 (file)
@@ -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;
 
index f1aaca86b135ec5eaa5f8794c55e374c842d295c..23b1160c5e009c618ab410805a44af087ba6a2fe 100644 (file)
@@ -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:
index 7d053943569c977d74d403739b132a4e5c471c8e..62f54f56ab7e652c81897bd89537853b36732c1c 100644 (file)
@@ -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",
index 1733d63410b3df23c3c23e7dc4a4a42cc189473f..e9e316551ebb1fa25bd434fdc5b269d3b5cc64a0 100644 (file)
@@ -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:
index 622a8f4c02300df6b38059b9ea41cdfcc66f36c3..840a05844e4ff914cd2f31ef506ab3dcce51cc3c 100644 (file)
@@ -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) {
index 161e7efa62e9a69884ac84a17848a318677a5d04..1d0d6e14ba42d6d2ec158b3401a99745842071f5 100644 (file)
@@ -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++) {