Blame SOURCES/kvm-target-i386-sev-fix-memory-leaks.patch

357786
From db0396e4d2663f41aaea944eaaf29141b20f5e1f Mon Sep 17 00:00:00 2001
357786
From: Markus Armbruster <armbru@redhat.com>
357786
Date: Fri, 31 Aug 2018 14:24:58 +0200
357786
Subject: [PATCH 07/29] target/i386: sev: fix memory leaks
357786
357786
RH-Author: Markus Armbruster <armbru@redhat.com>
357786
Message-id: <20180831142459.18567-2-armbru@redhat.com>
357786
Patchwork-id: 81984
357786
O-Subject: [RHEL-7.6 qemu-kvm-rhev PATCH 1/2] target/i386: sev: fix memory leaks
357786
Bugzilla: 1624390
357786
RH-Acked-by: Laszlo Ersek <lersek@redhat.com>
357786
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
RH-Acked-by: Auger Eric <eric.auger@redhat.com>
357786
357786
From: Paolo Bonzini <pbonzini@redhat.com>
357786
357786
Reported by Coverity.
357786
357786
Signed-off-by: Paolo Bonzini <pbonzini@redhat.com>
357786
(cherry picked from commit bf3175b49952628f96d72d1247d8bb3aa5c2466c)
357786
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
357786
---
357786
 target/i386/sev.c | 32 +++++++++++++++++---------------
357786
 1 file changed, 17 insertions(+), 15 deletions(-)
357786
357786
diff --git a/target/i386/sev.c b/target/i386/sev.c
357786
index c011671..2395171 100644
357786
--- a/target/i386/sev.c
357786
+++ b/target/i386/sev.c
357786
@@ -430,7 +430,8 @@ static int
357786
 sev_get_pdh_info(int fd, guchar **pdh, size_t *pdh_len, guchar **cert_chain,
357786
                  size_t *cert_chain_len)
357786
 {
357786
-    guchar *pdh_data, *cert_chain_data;
357786
+    guchar *pdh_data = NULL;
357786
+    guchar *cert_chain_data = NULL;
357786
     struct sev_user_data_pdh_cert_export export = {};
357786
     int err, r;
357786
 
357786
@@ -471,8 +472,9 @@ e_free:
357786
 SevCapability *
357786
 sev_get_capabilities(void)
357786
 {
357786
-    SevCapability *cap;
357786
-    guchar *pdh_data, *cert_chain_data;
357786
+    SevCapability *cap = NULL;
357786
+    guchar *pdh_data = NULL;
357786
+    guchar *cert_chain_data = NULL;
357786
     size_t pdh_len = 0, cert_chain_len = 0;
357786
     uint32_t ebx;
357786
     int fd;
357786
@@ -486,7 +488,7 @@ sev_get_capabilities(void)
357786
 
357786
     if (sev_get_pdh_info(fd, &pdh_data, &pdh_len,
357786
                          &cert_chain_data, &cert_chain_len)) {
357786
-        return NULL;
357786
+        goto out;
357786
     }
357786
 
357786
     cap = g_new0(SevCapability, 1);
357786
@@ -502,9 +504,9 @@ sev_get_capabilities(void)
357786
      */
357786
     cap->reduced_phys_bits = 1;
357786
 
357786
+out:
357786
     g_free(pdh_data);
357786
     g_free(cert_chain_data);
357786
-
357786
     close(fd);
357786
     return cap;
357786
 }
357786
@@ -530,7 +532,7 @@ sev_launch_start(SEVState *s)
357786
 {
357786
     gsize sz;
357786
     int ret = 1;
357786
-    int fw_error;
357786
+    int fw_error, rc;
357786
     QSevGuestInfo *sev = s->sev_info;
357786
     struct kvm_sev_launch_start *start;
357786
     guchar *session = NULL, *dh_cert = NULL;
357786
@@ -543,7 +545,7 @@ sev_launch_start(SEVState *s)
357786
                                             &error_abort);
357786
     if (sev->session_file) {
357786
         if (sev_read_file_base64(sev->session_file, &session, &sz) < 0) {
357786
-            return 1;
357786
+            goto out;
357786
         }
357786
         start->session_uaddr = (unsigned long)session;
357786
         start->session_len = sz;
357786
@@ -551,18 +553,18 @@ sev_launch_start(SEVState *s)
357786
 
357786
     if (sev->dh_cert_file) {
357786
         if (sev_read_file_base64(sev->dh_cert_file, &dh_cert, &sz) < 0) {
357786
-            return 1;
357786
+            goto out;
357786
         }
357786
         start->dh_uaddr = (unsigned long)dh_cert;
357786
         start->dh_len = sz;
357786
     }
357786
 
357786
     trace_kvm_sev_launch_start(start->policy, session, dh_cert);
357786
-    ret = sev_ioctl(s->sev_fd, KVM_SEV_LAUNCH_START, start, &fw_error);
357786
-    if (ret < 0) {
357786
+    rc = sev_ioctl(s->sev_fd, KVM_SEV_LAUNCH_START, start, &fw_error);
357786
+    if (rc < 0) {
357786
         error_report("%s: LAUNCH_START ret=%d fw_error=%d '%s'",
357786
                 __func__, ret, fw_error, fw_error_to_str(fw_error));
357786
-        return 1;
357786
+        goto out;
357786
     }
357786
 
357786
     object_property_set_int(OBJECT(sev), start->handle, "handle",
357786
@@ -570,12 +572,13 @@ sev_launch_start(SEVState *s)
357786
     sev_set_guest_state(SEV_STATE_LAUNCH_UPDATE);
357786
     s->handle = start->handle;
357786
     s->policy = start->policy;
357786
+    ret = 0;
357786
 
357786
+out:
357786
     g_free(start);
357786
     g_free(session);
357786
     g_free(dh_cert);
357786
-
357786
-    return 0;
357786
+    return ret;
357786
 }
357786
 
357786
 static int
357786
@@ -712,7 +715,7 @@ sev_guest_init(const char *id)
357786
     uint32_t host_cbitpos;
357786
     struct sev_user_data_status status = {};
357786
 
357786
-    s = g_new0(SEVState, 1);
357786
+    sev_state = s = g_new0(SEVState, 1);
357786
     s->sev_info = lookup_sev_guest_info(id);
357786
     if (!s->sev_info) {
357786
         error_report("%s: '%s' is not a valid '%s' object",
357786
@@ -720,7 +723,6 @@ sev_guest_init(const char *id)
357786
         goto err;
357786
     }
357786
 
357786
-    sev_state = s;
357786
     s->state = SEV_STATE_UNINIT;
357786
 
357786
     host_cpuid(0x8000001F, 0, NULL, &ebx, NULL, NULL);
357786
-- 
357786
1.8.3.1
357786