]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
storage_backend_iscsi(_direct): Properly clear secrets
authorPeter Krempa <pkrempa@redhat.com>
Mon, 1 Feb 2021 11:15:57 +0000 (12:15 +0100)
committerPeter Krempa <pkrempa@redhat.com>
Wed, 3 Feb 2021 12:07:12 +0000 (13:07 +0100)
The code pretends that it cares about clearing the secret values, but
passes the secret value to a realloc, which may copy the value somewhere
else and doesn't sanitize the original location when it does so.

Since we want to construct a string from the value, let's copy it to a
new piece of memory which has the space for the 'NUL' byte ourselves, to
prevent a random realloc keeping the data around.

While at it, use virSecureErase instead of VIR_DISPOSE_N since it's
being phased out.

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

index 45167e4490c0415ab42cd61b3e209abad9ee69a2..9127d663b198b6a8898e39d8be4c503a90da962c 100644 (file)
@@ -41,6 +41,7 @@
 #include "virsecret.h"
 #include "storage_util.h"
 #include "virutil.h"
+#include "virsecureerase.h"
 
 #define VIR_FROM_THIS VIR_FROM_STORAGE
 
@@ -256,8 +257,9 @@ static int
 virStorageBackendISCSISetAuth(const char *portal,
                               virStoragePoolSourcePtr source)
 {
-    unsigned char *secret_value = NULL;
+    g_autofree unsigned char *secret_value = NULL;
     size_t secret_size;
+    g_autofree char *secret_str = NULL;
     virStorageAuthDefPtr authdef = source->auth;
     int ret = -1;
     virConnectPtr conn = NULL;
@@ -282,10 +284,10 @@ virStorageBackendISCSISetAuth(const char *portal,
                                  &secret_value, &secret_size) < 0)
         goto cleanup;
 
-    if (VIR_REALLOC_N(secret_value, secret_size + 1) < 0)
-        goto cleanup;
-
-    secret_value[secret_size] = '\0';
+    secret_str = g_new0(char, secret_size + 1);
+    memcpy(secret_str, secret_value, secret_size);
+    virSecureErase(secret_value, secret_size);
+    secret_str[secret_size] = '\0';
 
     if (virISCSINodeUpdate(portal,
                            source->devices[0].path,
@@ -298,13 +300,13 @@ virStorageBackendISCSISetAuth(const char *portal,
         virISCSINodeUpdate(portal,
                            source->devices[0].path,
                            "node.session.auth.password",
-                           (const char *)secret_value) < 0)
+                           secret_str) < 0)
         goto cleanup;
 
     ret = 0;
 
  cleanup:
-    VIR_DISPOSE_N(secret_value, secret_size);
+    virSecureErase(secret_str, secret_size);
     virObjectUnref(conn);
     return ret;
 }
index 12b075db0bc926ff9198c173129078f655114c0a..3511f39214b1f1844d7dca542289cd6d5e39f469 100644 (file)
@@ -34,6 +34,7 @@
 #include "virstring.h"
 #include "virtime.h"
 #include "viruuid.h"
+#include "virsecureerase.h"
 
 #define VIR_FROM_THIS VIR_FROM_STORAGE
 
@@ -87,8 +88,9 @@ static int
 virStorageBackendISCSIDirectSetAuth(struct iscsi_context *iscsi,
                                     virStoragePoolSourcePtr source)
 {
-    unsigned char *secret_value = NULL;
+    g_autofree unsigned char *secret_value = NULL;
     size_t secret_size;
+    g_autofree char *secret_str = NULL;
     virStorageAuthDefPtr authdef = source->auth;
     int ret = -1;
     virConnectPtr conn = NULL;
@@ -113,14 +115,13 @@ virStorageBackendISCSIDirectSetAuth(struct iscsi_context *iscsi,
                                  &secret_value, &secret_size) < 0)
         goto cleanup;
 
-    if (VIR_REALLOC_N(secret_value, secret_size + 1) < 0)
-        goto cleanup;
-
-    secret_value[secret_size] = '\0';
+    secret_str = g_new0(char, secret_size + 1);
+    memcpy(secret_str, secret_value, secret_size);
+    virSecureErase(secret_value, secret_size);
+    secret_str[secret_size] = '\0';
 
     if (iscsi_set_initiator_username_pwd(iscsi,
-                                         authdef->username,
-                                         (const char *)secret_value) < 0) {
+                                         authdef->username, secret_str) < 0) {
         virReportError(VIR_ERR_INTERNAL_ERROR,
                        _("Failed to set credential: %s"),
                        iscsi_get_error(iscsi));
@@ -129,7 +130,7 @@ virStorageBackendISCSIDirectSetAuth(struct iscsi_context *iscsi,
 
     ret = 0;
  cleanup:
-    VIR_DISPOSE_N(secret_value, secret_size);
+    virSecureErase(secret_str, secret_size);
     virObjectUnref(conn);
     return ret;
 }