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