Blame SOURCES/ovt-Fixes-for-few-leaks-and-improved-error-handling.patch

604589
From 46a724937bfbfe4fa4e64269f057c71893f25b30 Mon Sep 17 00:00:00 2001
604589
From: Cathy Avery <cavery@redhat.com>
604589
Date: Thu, 25 Jul 2019 12:32:32 +0200
604589
Subject: [PATCH 09/16] Fixes for few leaks and improved error handling.
604589
604589
RH-Author: Cathy Avery <cavery@redhat.com>
604589
Message-id: <20190725123239.18274-10-cavery@redhat.com>
604589
Patchwork-id: 89723
604589
O-Subject: [RHEL8.1 open-vm-tools PATCH 09/16] Fixes for few leaks and improved error handling.
604589
Bugzilla: 1602648
604589
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
604589
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
604589
604589
commit 2bbd56da4314856dfc1a8fed2db5b55cd9ef8860
604589
Author: Oliver Kurth <okurth@vmware.com>
604589
Date:   Wed May 8 15:27:18 2019 -0700
604589
604589
    Fixes for few leaks and improved error handling.
604589
604589
    Fix a memory leak detected by coverity scan. It is not critical,
604589
    but it is real in an error case when there is no end mark. While
604589
    fixing it, also enhanced code to handle different error cases
604589
    properly because we would want valid content to be decoded even
604589
    when there are invalid marks in the log file. Invalid log marks
604589
    are possible when vmware.log gets rotated in the middle of guest
604589
    logging.
604589
604589
    While verifying the fix using valgrind, found a couple of more
604589
    leaks in panic and warning stubs. Addressed those as well.
604589
604589
Signed-off-by: Cathy Avery <cavery@redhat.com>
604589
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
604589
---
604589
 open-vm-tools/lib/stubs/stub-panic.c   |  3 ++-
604589
 open-vm-tools/lib/stubs/stub-warning.c |  5 ++--
604589
 open-vm-tools/xferlogs/xferlogs.c      | 47 ++++++++++++++++++++++++++++------
604589
 3 files changed, 44 insertions(+), 11 deletions(-)
604589
604589
diff --git a/lib/stubs/stub-panic.c b/lib/stubs/stub-panic.c
604589
index 615a810..4b88f59 100644
604589
--- a/lib/stubs/stub-panic.c
604589
+++ b/lib/stubs/stub-panic.c
604589
@@ -1,5 +1,5 @@
604589
 /*********************************************************
604589
- * Copyright (C) 2008 VMware, Inc. All rights reserved.
604589
+ * Copyright (C) 2008,2019 VMware, Inc. All rights reserved.
604589
  *
604589
  * This program is free software; you can redistribute it and/or modify it
604589
  * under the terms of the GNU Lesser General Public License as published
604589
@@ -43,6 +43,7 @@ Panic(const char *fmt, ...)
604589
 
604589
    if (str != NULL) {
604589
       fputs(str, stderr);
604589
+      free(str);
604589
    }
604589
 
604589
    assert(FALSE);
604589
diff --git a/lib/stubs/stub-warning.c b/lib/stubs/stub-warning.c
604589
index c32fa69..3a49617 100644
604589
--- a/lib/stubs/stub-warning.c
604589
+++ b/lib/stubs/stub-warning.c
604589
@@ -1,5 +1,5 @@
604589
 /*********************************************************
604589
- * Copyright (C) 2008-2016 VMware, Inc. All rights reserved.
604589
+ * Copyright (C) 2008-2016,2019 VMware, Inc. All rights reserved.
604589
  *
604589
  * This program is free software; you can redistribute it and/or modify it
604589
  * under the terms of the GNU Lesser General Public License as published
604589
@@ -24,6 +24,7 @@
604589
  */
604589
 
604589
 #include <stdio.h>
604589
+#include <stdlib.h>
604589
 #include "str.h"
604589
 
604589
 
604589
@@ -39,6 +40,6 @@ Warning(const char *fmt, ...)
604589
 
604589
    if (str != NULL) {
604589
       fputs(str, stderr);
604589
+      free(str);
604589
    }
604589
 }
604589
-
604589
diff --git a/xferlogs/xferlogs.c b/xferlogs/xferlogs.c
604589
index 9aa9b3a..d4a600f 100644
604589
--- a/xferlogs/xferlogs.c
604589
+++ b/xferlogs/xferlogs.c
604589
@@ -184,8 +184,19 @@ extractFile(char *filename) //IN: vmx log filename e.g. vmware.log
604589
             char tstamp[32];
604589
             time_t now;
604589
 
604589
-            ASSERT(outfp == NULL);
604589
-            ASSERT(state == NOT_IN_GUEST_LOGGING);
604589
+            /*
604589
+             * There could be multiple LOG_START_MARK in the log,
604589
+             * close existing one before opening a new file.
604589
+             */
604589
+            if (outfp) {
604589
+               ASSERT(state == IN_GUEST_LOGGING);
604589
+               Warning("Found a new start mark before end mark for "
604589
+                       "previous one\n");
604589
+               fclose(outfp);
604589
+               outfp = NULL;
604589
+            } else {
604589
+               ASSERT(state == NOT_IN_GUEST_LOGGING);
604589
+            }
604589
             DEBUG_ONLY(state = IN_GUEST_LOGGING);
604589
 
604589
             /*
604589
@@ -234,23 +245,32 @@ extractFile(char *filename) //IN: vmx log filename e.g. vmware.log
604589
                ver = ver + sizeof "ver - " - 1;
604589
                version = strtol(ver, NULL, 0);
604589
                if (version != LOG_VERSION) {
604589
-                  Warning("input version %d doesnt match the\
604589
+                  Warning("Input version %d doesn't match the\
604589
                           version of this binary %d", version, LOG_VERSION);
604589
                } else {
604589
-                  printf("reading file %s to %s \n", logInpFilename, fname);
604589
+                  printf("Reading file %s to %s \n", logInpFilename, fname);
604589
                   if (!(outfp = fopen(fname, "wb"))) {
604589
                      Warning("Error opening file %s\n", fname);
604589
                   }
604589
                }
604589
             }
604589
          } else if (strstr(buf, LOG_END_MARK)) { // close the output file.
604589
-            ASSERT(state == IN_GUEST_LOGGING);
604589
+            /*
604589
+             * Need to check outfp, because we might get LOG_END_MARK
604589
+             * before LOG_START_MARK due to log rotation.
604589
+             */
604589
+            if (outfp) {
604589
+               ASSERT(state == IN_GUEST_LOGGING);
604589
+               fclose(outfp);
604589
+               outfp = NULL;
604589
+            } else {
604589
+               ASSERT(state == NOT_IN_GUEST_LOGGING);
604589
+               Warning("Reached file end mark without start mark\n");
604589
+            }
604589
             DEBUG_ONLY(state = NOT_IN_GUEST_LOGGING);
604589
-            fclose(outfp);
604589
-            outfp = NULL;
604589
          } else { // write to the output file
604589
-            ASSERT(state == IN_GUEST_LOGGING);
604589
             if (outfp) {
604589
+               ASSERT(state == IN_GUEST_LOGGING);
604589
                ptrStr = strstr(buf, LOG_GUEST_MARK);
604589
                ptrStr += sizeof LOG_GUEST_MARK - 1;
604589
                if (Base64_Decode(ptrStr, base64Out, BUF_OUT_SIZE, &lenOut)) {
604589
@@ -260,10 +280,21 @@ extractFile(char *filename) //IN: vmx log filename e.g. vmware.log
604589
                } else {
604589
                   Warning("Error decoding output %s\n", ptrStr);
604589
                }
604589
+            } else {
604589
+               ASSERT(state == NOT_IN_GUEST_LOGGING);
604589
+               Warning("Missing file start mark\n");
604589
             }
604589
          }
604589
       }
604589
    }
604589
+
604589
+   /*
604589
+    * We may need to close file in case LOG_END_MARK is missing.
604589
+    */
604589
+   if (outfp) {
604589
+      ASSERT(state == IN_GUEST_LOGGING);
604589
+      fclose(outfp);
604589
+   }
604589
    fclose(fp);
604589
 }
604589
 
604589
-- 
604589
1.8.3.1
604589