Blame SOURCES/ovt-Fix-memory-leak-in-GetFormattedCommandLine-function-.patch

604589
From da6b1c1b22ce0386214bbc5395d4cc42529be4fb Mon Sep 17 00:00:00 2001
604589
From: Cathy Avery <cavery@redhat.com>
604589
Date: Thu, 25 Jul 2019 12:32:25 +0200
604589
Subject: [PATCH 02/16] Fix memory leak in GetFormattedCommandLine() function
604589
 (linuxDeployment.c)
604589
604589
RH-Author: Cathy Avery <cavery@redhat.com>
604589
Message-id: <20190725123239.18274-3-cavery@redhat.com>
604589
Patchwork-id: 89712
604589
O-Subject: [RHEL8.1 open-vm-tools PATCH 02/16] Fix memory leak in GetFormattedCommandLine() function (linuxDeployment.c)
604589
Bugzilla: 1602648
604589
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
604589
RH-Acked-by: Miroslav Rezanina <mrezanin@redhat.com>
604589
604589
commit d93219282ff7e89e3f581bf757dfd807c7568452
604589
Author: Oliver Kurth <okurth@vmware.com>
604589
Date:   Thu Mar 28 12:42:59 2019 -0700
604589
604589
    Fix memory leak in GetFormattedCommandLine() function (linuxDeployment.c)
604589
604589
    1. There are malloc() calls happening in a loop; this function returns
604589
       NULL when one of malloc fails.  If a malloc call fails in the loop,
604589
       all memory allocated in previous iterations should be freed before
604589
       the return NULL.
604589
    2. Clear allocated resources before return NULL in this file.
604589
    3. Add NULL check following malloc calls in this file.
604589
    4. Encapsulate %s in () only if %s is strerror(errno), otherwise encapsulate
604589
       %s in single quotes.
604589
    5. End with \n in sLog.
604589
604589
Signed-off-by: Cathy Avery <cavery@redhat.com>
604589
604589
Partial port: Only the parts of the patch that addesses the coverity defects were backported.
604589
604589
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
604589
---
604589
 open-vm-tools/libDeployPkg/linuxDeployment.c | 14 +++++++++++++-
604589
 1 file changed, 13 insertions(+), 1 deletion(-)
604589
604589
diff --git a/libDeployPkg/linuxDeployment.c b/libDeployPkg/linuxDeployment.c
604589
index 6e22aac..74b2f90 100644
604589
--- a/libDeployPkg/linuxDeployment.c
604589
+++ b/libDeployPkg/linuxDeployment.c
604589
@@ -454,6 +454,8 @@ AddToList(struct List* head, const char* token)
604589
    l = malloc(sizeof(struct List));
604589
    if (!l) {
604589
       SetDeployError("Error allocating memory. (%s)", strerror(errno));
604589
+      // clear allocated resource
604589
+      free(data);
604589
       return NULL;
604589
    }
604589
 
604589
@@ -1495,13 +1497,23 @@ GetFormattedCommandLine(const char* command)
604589
    args = malloc((ListSize(commandTokens) + 1) * sizeof(char*));
604589
    if (!args) {
604589
       SetDeployError("Error allocating memory.");
604589
+      // clear resources
604589
+      DeleteList(commandTokens);
604589
       return NULL;
604589
    }
604589
 
604589
    for(l = commandTokens, i = 0; l; l = l->next, i++) {
604589
       char* arg = malloc(strlen(l->data) + 1);
604589
       if (!arg) {
604589
-         SetDeployError("Error allocating memory.(%s)", strerror(errno));
604589
+         unsigned int j;
604589
+         SetDeployError("Error allocating memory. (%s)", strerror(errno));
604589
+         // free allocated memories in previous iterations if any
604589
+         for (j = 0; j < i; j++) {
604589
+            free(args[j]);
604589
+         }
604589
+         free(args);
604589
+         // clear resources
604589
+         DeleteList(commandTokens);
604589
          return NULL;
604589
       }
604589
 
604589
-- 
604589
1.8.3.1
604589