Blame SOURCES/libvirt-secret-Add-check-validation-for-correct-usage-when-LookupByUUID.patch

edecca
From 44b8a63a2e0bcd5740db4c9f49d635ab1d5b9b25 Mon Sep 17 00:00:00 2001
edecca
Message-Id: <44b8a63a2e0bcd5740db4c9f49d635ab1d5b9b25@dist-git>
edecca
From: John Ferlan <jferlan@redhat.com>
edecca
Date: Thu, 13 Dec 2018 10:26:46 -0500
edecca
Subject: [PATCH] secret: Add check/validation for correct usage when
edecca
 LookupByUUID
edecca
MIME-Version: 1.0
edecca
Content-Type: text/plain; charset=UTF-8
edecca
Content-Transfer-Encoding: 8bit
edecca
edecca
https://bugzilla.redhat.com/show_bug.cgi?id=1656255
edecca
edecca
If virSecretGetSecretString is using by secretLookupByUUID,
edecca
then it's possible the found sec->usageType doesn't match the
edecca
desired @secretUsageType. If this occurs for the encrypted
edecca
volume creation processing and a subsequent pool refresh is
edecca
executed, then the secret used to create the volume will not
edecca
be found by the storageBackendLoadDefaultSecrets which expects
edecca
to find secrets by VIR_SECRET_USAGE_TYPE_VOLUME.
edecca
edecca
Add a check to virSecretGetSecretString to avoid the possibility
edecca
along with an error indicating the incorrect matched types.
edecca
edecca
Signed-off-by: John Ferlan <jferlan@redhat.com>
edecca
ACKed-by: Michal Privoznik <mprivozn@redhat.com>
edecca
(cherry picked from commit e0eb8a8a696ee334fa33281b880e480e76348052)
edecca
Reviewed-by: Ján Tomko <jtomko@redhat.com>
edecca
---
edecca
 src/secret/secret_util.c | 17 +++++++++++++++++
edecca
 tests/qemuxml2argvtest.c |  4 +++-
edecca
 2 files changed, 20 insertions(+), 1 deletion(-)
edecca
edecca
diff --git a/src/secret/secret_util.c b/src/secret/secret_util.c
edecca
index 16e43ab2cc..27e164a425 100644
edecca
--- a/src/secret/secret_util.c
edecca
+++ b/src/secret/secret_util.c
edecca
@@ -71,6 +71,23 @@ virSecretGetSecretString(virConnectPtr conn,
edecca
     if (!sec)
edecca
         goto cleanup;
edecca
 
edecca
+    /* NB: NONE is a byproduct of the qemuxml2argvtest test mocking
edecca
+     * for UUID lookups. Normal secret XML processing would fail if
edecca
+     * the usage type was NONE and since we have no way to set the
edecca
+     * expected usage in that environment, let's just accept NONE */
edecca
+    if (sec->usageType != VIR_SECRET_USAGE_TYPE_NONE &&
edecca
+        sec->usageType != secretUsageType) {
edecca
+        char uuidstr[VIR_UUID_STRING_BUFLEN];
edecca
+
edecca
+        virUUIDFormat(seclookupdef->u.uuid, uuidstr);
edecca
+        virReportError(VIR_ERR_INVALID_ARG,
edecca
+                       _("secret with uuid %s is of type '%s' not "
edecca
+                         "expected '%s' type"),
edecca
+                       uuidstr, virSecretUsageTypeToString(sec->usageType),
edecca
+                       virSecretUsageTypeToString(secretUsageType));
edecca
+        goto cleanup;
edecca
+    }
edecca
+
edecca
     *secret = conn->secretDriver->secretGetValue(sec, secret_size, 0,
edecca
                                                  VIR_SECRET_GET_VALUE_INTERNAL_CALL);
edecca
 
edecca
diff --git a/tests/qemuxml2argvtest.c b/tests/qemuxml2argvtest.c
edecca
index 0908bc5d08..bd4aa1266d 100644
edecca
--- a/tests/qemuxml2argvtest.c
edecca
+++ b/tests/qemuxml2argvtest.c
edecca
@@ -80,7 +80,9 @@ static virSecretPtr
edecca
 fakeSecretLookupByUUID(virConnectPtr conn,
edecca
                        const unsigned char *uuid)
edecca
 {
edecca
-    return virGetSecret(conn, uuid, 0, "");
edecca
+    /* NB: This mocked value could be "tls" or "volume" depending on
edecca
+     * which test is being run, we'll leave at NONE (or 0) */
edecca
+    return virGetSecret(conn, uuid, VIR_SECRET_USAGE_TYPE_NONE, "");
edecca
 }
edecca
 
edecca
 static virSecretDriver fakeSecretDriver = {
edecca
-- 
edecca
2.20.1
edecca