Blob Blame History Raw
From 88cf0236e1b98b00bcaa5762f9207b99fb98e084 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20P=2E=20Berrang=C3=A9?= <berrange@redhat.com>
Date: Mon, 6 Aug 2018 17:11:57 +0100
Subject: [PATCH] Clear typed parameter elements as well as array
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Some typed parameter elements may contain strings that need to be
freed.

We must ensure that the parameter array has dup'd the strings
rather than borrowing a pointer.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
(cherry picked from commit 54f0d2c64021e6e143f40185f0181cf5d5a51f53)
---
 lib/Sys/Virt.xs | 124 ++++++++++++++++++++++++++----------------------
 1 file changed, 66 insertions(+), 58 deletions(-)

diff --git a/lib/Sys/Virt.xs b/lib/Sys/Virt.xs
index 3d8eb9b..6c2bf48 100644
--- a/lib/Sys/Virt.xs
+++ b/lib/Sys/Virt.xs
@@ -86,7 +86,6 @@ virt_newSVull(unsigned long long val) {
 }
 
 
-
 static void
 ignoreVirErrorFunc(void * userData, virErrorPtr error) {
   /* Do nothing */
@@ -263,7 +262,8 @@ vir_typed_param_from_hv(HV *newparams, virTypedParameterPtr params, int nparams)
 
         case VIR_TYPED_PARAM_STRING:
             ptr = SvPV(*val, len);
-            params[i].value.s = (char *)ptr;
+            if (!(params[i].value.s = strdup((char *)ptr)))
+                abort();
             break;
         }
     }
@@ -300,7 +300,8 @@ vir_typed_param_add_string_list_from_hv(HV *newparams,
       localparams[*nparams + i].field[VIR_TYPED_PARAM_FIELD_LENGTH - 1] = '\0';
 
       localparams[*nparams + i].type = VIR_TYPED_PARAM_STRING;
-      localparams[*nparams + i].value.s = ptr;
+      if (!(localparams[*nparams + i].value.s = strdup(ptr)))
+	  abort();
     }
 
     *params = localparams;
@@ -308,6 +309,14 @@ vir_typed_param_add_string_list_from_hv(HV *newparams,
 }
 
 
+static void
+vir_typed_param_safe_free(virTypedParameterPtr params, int nparams)
+{
+    virTypedParamsClear(params, nparams);
+    Safefree(params);
+}
+
+
 static int
 _domain_event_lifecycle_callback(virConnectPtr con,
                                  virDomainPtr dom,
@@ -2411,7 +2420,7 @@ get_node_sev_info(conn, flags=0)
       }
 
       RETVAL = vir_typed_param_to_hv(params, nparams);
-      free(params);
+      virTypedParamsFree(params, nparams);
   OUTPUT:
       RETVAL
 
@@ -2651,12 +2660,12 @@ get_node_memory_parameters(conn, flags=0)
       Newx(params, nparams, virTypedParameter);
 
       if (virNodeGetMemoryParameters(conn, params, &nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
       RETVAL = vir_typed_param_to_hv(params, nparams);
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
   OUTPUT:
       RETVAL
 
@@ -2677,17 +2686,17 @@ set_node_memory_parameters(conn, newparams, flags=0)
       Newx(params, nparams, virTypedParameter);
 
       if (virNodeGetMemoryParameters(conn, params, &nparams, 0) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
 
       if (virNodeSetMemoryParameters(conn, params, nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
 
 
 
@@ -4562,13 +4571,12 @@ get_job_stats(dom, flags=0)
       SV *typeSv;
     PPCODE:
       if (virDomainGetJobStats(dom, &type, &params, &nparams, flags) < 0) {
-          Safefree(params);
           _croak_error();
       }
 
       typeSv = newSViv(type);
       paramsHv = vir_typed_param_to_hv(params, nparams);
-      free(params);
+      virTypedParamsFree(params, nparams);
 
       EXTEND(SP, 2);
       PUSHs(newRV_noinc((SV*)typeSv));
@@ -4676,11 +4684,11 @@ block_copy(dom, path, destxml, newparams, flags=0)
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
 
       if (virDomainBlockCopy(dom, path, destxml, params, nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
 
 
 void
@@ -4741,17 +4749,17 @@ get_scheduler_parameters(dom, flags=0)
       Newx(params, nparams, virTypedParameter);
       if (flags) {
           if (virDomainGetSchedulerParametersFlags(dom, params, &nparams, flags) < 0) {
-              Safefree(params);
+              vir_typed_param_safe_free(params, nparams);
               _croak_error();
           }
       } else {
           if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0) {
-              Safefree(params);
+              vir_typed_param_safe_free(params, nparams);
               _croak_error();
           }
       }
       RETVAL = vir_typed_param_to_hv(params, nparams);
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
   OUTPUT:
       RETVAL
 
@@ -4772,22 +4780,22 @@ set_scheduler_parameters(dom, newparams, flags=0)
       free(type);
       Newx(params, nparams, virTypedParameter);
       if (virDomainGetSchedulerParameters(dom, params, &nparams) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
       if (flags) {
           if (virDomainSetSchedulerParametersFlags(dom, params, nparams, flags) < 0) {
-              Safefree(params);
+              vir_typed_param_safe_free(params, nparams);
               _croak_error();
           }
       } else {
           if (virDomainSetSchedulerParameters(dom, params, nparams) < 0) {
-              Safefree(params);
+              vir_typed_param_safe_free(params, nparams);
               _croak_error();
           }
       }
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
 
 
 HV *
@@ -4805,12 +4813,12 @@ get_memory_parameters(dom, flags=0)
       Newx(params, nparams, virTypedParameter);
 
       if (virDomainGetMemoryParameters(dom, params, &nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
       RETVAL = vir_typed_param_to_hv(params, nparams);
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
   OUTPUT:
       RETVAL
 
@@ -4831,17 +4839,17 @@ set_memory_parameters(dom, newparams, flags=0)
       Newx(params, nparams, virTypedParameter);
 
       if (virDomainGetMemoryParameters(dom, params, &nparams, 0) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
 
       if (virDomainSetMemoryParameters(dom, params, nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
 
 
 HV *
@@ -4859,12 +4867,12 @@ get_numa_parameters(dom, flags=0)
       Newx(params, nparams, virTypedParameter);
 
       if (virDomainGetNumaParameters(dom, params, &nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
       RETVAL = vir_typed_param_to_hv(params, nparams);
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
   OUTPUT:
       RETVAL
 
@@ -4885,17 +4893,17 @@ set_numa_parameters(dom, newparams, flags=0)
       Newx(params, nparams, virTypedParameter);
 
       if (virDomainGetNumaParameters(dom, params, &nparams, 0) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
 
       if (virDomainSetNumaParameters(dom, params, nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
 
 
 HV *
@@ -4913,12 +4921,12 @@ get_blkio_parameters(dom, flags=0)
       Newx(params, nparams, virBlkioParameter);
 
       if (virDomainGetBlkioParameters(dom, params, &nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
       RETVAL = vir_typed_param_to_hv(params, nparams);
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
   OUTPUT:
       RETVAL
 
@@ -4939,7 +4947,7 @@ set_blkio_parameters(dom, newparams, flags=0)
       Newx(params, nparams, virBlkioParameter);
 
       if (virDomainGetBlkioParameters(dom, params, &nparams, 0) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
@@ -4947,10 +4955,10 @@ set_blkio_parameters(dom, newparams, flags=0)
 
       if (virDomainSetBlkioParameters(dom, params, nparams,
                                       flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
 
 
 HV *
@@ -4962,12 +4970,12 @@ get_perf_events(dom, flags=0)
       int nparams = 0;
     CODE:
       if (virDomainGetPerfEvents(dom, &params, &nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
       RETVAL = vir_typed_param_to_hv(params, nparams);
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
   OUTPUT:
       RETVAL
 
@@ -4982,7 +4990,7 @@ set_perf_events(dom, newparams, flags=0)
       int nparams = 0;
     PPCODE:
       if (virDomainGetPerfEvents(dom, &params, &nparams, 0) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
@@ -4990,7 +4998,7 @@ set_perf_events(dom, newparams, flags=0)
 
       if (virDomainSetPerfEvents(dom, params, nparams, flags) < 0)
           _croak_error();
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
 
 
 HV *
@@ -5005,7 +5013,7 @@ get_launch_security_info(dom, flags=0)
           _croak_error();
       }
       RETVAL = vir_typed_param_to_hv(params, nparams);
-      free(params);
+      virTypedParamsFree(params, nparams);
   OUTPUT:
       RETVAL
 
@@ -5386,10 +5394,10 @@ _migrate(dom, destcon, newparams, flags=0)
       * if it is possible todo so
       */
      if ((RETVAL = virDomainMigrate3(dom, destcon, params, nparams, flags)) == NULL) {
-         Safefree(params);
+         vir_typed_param_safe_free(params, nparams);
          _croak_error();
      }
-     Safefree(params);
+     vir_typed_param_safe_free(params, nparams);
  OUTPUT:
      RETVAL
 
@@ -5477,10 +5485,10 @@ _migrate_to_uri(dom, desturi, newparams, flags=0)
       * if it is possible todo so
       */
      if (virDomainMigrateToURI3(dom, desturi, params, nparams, flags) < 0) {
-         Safefree(params);
+         vir_typed_param_safe_free(params, nparams);
          _croak_error();
      }
-     Safefree(params);
+     vir_typed_param_safe_free(params, nparams);
 
 
 void
@@ -5639,12 +5647,12 @@ get_block_iotune(dom, disk, flags=0)
 
       Newx(params, nparams, virTypedParameter);
       if (virDomainGetBlockIoTune(dom, disk, params, &nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
       RETVAL = vir_typed_param_to_hv(params, nparams);
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
   OUTPUT:
       RETVAL
 
@@ -5665,16 +5673,16 @@ set_block_iotune(dom, disk, newparams, flags=0)
       Newx(params, nparams, virTypedParameter);
 
       if (virDomainGetBlockIoTune(dom, disk, params, &nparams, 0) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
       if (virDomainSetBlockIoTune(dom, disk, params, nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
 
 
 HV *
@@ -5693,12 +5701,12 @@ get_interface_parameters(dom, intf, flags=0)
 
       Newx(params, nparams, virTypedParameter);
       if (virDomainGetInterfaceParameters(dom, intf, params, &nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
       RETVAL = vir_typed_param_to_hv(params, nparams);
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
   OUTPUT:
       RETVAL
 
@@ -5719,16 +5727,16 @@ set_interface_parameters(dom, intf, newparams, flags=0)
       Newx(params, nparams, virTypedParameter);
 
       if (virDomainGetInterfaceParameters(dom, intf, params, &nparams, 0) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
       nparams = vir_typed_param_from_hv(newparams, params, nparams);
       if (virDomainSetInterfaceParameters(dom, intf, params, nparams, flags) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
 
 
 HV *
@@ -5764,7 +5772,7 @@ block_stats(dom, path, flags=0)
           Newx(params, nparams, virTypedParameter);
 
           if (virDomainBlockStatsFlags(dom, path, params, &nparams, flags) < 0) {
-              Safefree(params);
+              vir_typed_param_safe_free(params, nparams);
               _croak_error();
           }
 
@@ -5784,7 +5792,7 @@ block_stats(dom, path, flags=0)
                   (void)hv_store(RETVAL, field, strlen(field), val, 0);
               }
           }
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
       }
   OUTPUT:
       RETVAL
@@ -6018,7 +6026,7 @@ get_cpu_stats(dom, start_cpu, ncpus, flags=0)
 
       Newx(params, ncpus * nparams, virTypedParameter);
       if ((ret = virDomainGetCPUStats(dom, params, nparams, start_cpu, ncpus, flags)) < 0) {
-          Safefree(params);
+          vir_typed_param_safe_free(params, nparams);
           _croak_error();
       }
 
@@ -6028,7 +6036,7 @@ get_cpu_stats(dom, start_cpu, ncpus, flags=0)
           PUSHs(newRV_noinc((SV *)rec));
       }
 
-      Safefree(params);
+      vir_typed_param_safe_free(params, nparams);
 
 
 void