]> xenbits.xensource.com Git - libvirt.git/commitdiff
qemuDomainMasterKeyCreate: Don't use VIR_DISPOSE_N on failure
authorPeter Krempa <pkrempa@redhat.com>
Mon, 1 Feb 2021 11:52:07 +0000 (12:52 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 3 Feb 2021 12:07:12 +0000 (13:07 +0100)
When virRandomBytes fails we don't get any random bytes and even if we
did they don't have to be treated as secret as they weren't used in any
way.

Add a temporary variable with automatic freeing for the secret buffer
and assign it only on success.

Signed-off-by: Peter Krempa <pkrempa@redhat.com>
Reviewed-by: Daniel P. Berrangé <berrange@redhat.com>
src/qemu/qemu_domain.c

index 0c078a9388fd7bd3d16b0d858a8fc2366c580e2d..2c34307c823c956f5e52bc362124ae20b84fe164 100644 (file)
@@ -562,18 +562,19 @@ int
 qemuDomainMasterKeyCreate(virDomainObjPtr vm)
 {
     qemuDomainObjPrivatePtr priv = vm->privateData;
+    g_autofree uint8_t *key = NULL;
 
     /* If we don't have the capability, then do nothing. */
     if (!virQEMUCapsGet(priv->qemuCaps, QEMU_CAPS_OBJECT_SECRET))
         return 0;
 
-    priv->masterKey = g_new0(uint8_t, QEMU_DOMAIN_MASTER_KEY_LEN);
-    priv->masterKeyLen = QEMU_DOMAIN_MASTER_KEY_LEN;
+    key = g_new0(uint8_t, QEMU_DOMAIN_MASTER_KEY_LEN);
 
-    if (virRandomBytes(priv->masterKey, priv->masterKeyLen) < 0) {
-        VIR_DISPOSE_N(priv->masterKey, priv->masterKeyLen);
+    if (virRandomBytes(key, QEMU_DOMAIN_MASTER_KEY_LEN) < 0)
         return -1;
-    }
+
+    priv->masterKey = g_steal_pointer(&key);
+    priv->masterKeyLen = QEMU_DOMAIN_MASTER_KEY_LEN;
 
     return 0;
 }