Blob Blame History Raw
From 6c240f1d7c3e348af0aa95d5dfba08e95601fcc8 Mon Sep 17 00:00:00 2001
From: Cathy Avery <cavery@redhat.com>
Date: Fri, 15 Nov 2019 14:49:07 +0100
Subject: [PATCH 2/3] End VGAuth impersonation in the

RH-Author: Cathy Avery <cavery@redhat.com>
Message-id: <20191115144908.7669-3-cavery@redhat.com>
Patchwork-id: 92390
O-Subject: [RHEL7.7.z open-vm-tools PATCH 2/3] End VGAuth impersonation in the case of error.
Bugzilla: 1772825
RH-Acked-by: Vitaly Kuznetsov <vkuznets@redhat.com>
RH-Acked-by: Eduardo Otubo <otubo@redhat.com>

commit 7b874f37f970aab2adddb063a8363594f47abf70
Author: Oliver Kurth <okurth@vmware.com>
Date:   Tue Sep 4 15:40:58 2018 -0700

    End VGAuth impersonation in the case of error.

    * In GuestAuthPasswordAuthenticateImpersonate():
    When VGAuth_UserHandleAccessToken fails, unimpersonation is not
    being done. This can cause issues. Fixed it.

    * In GuestAuthSAMLAuthenticateAndImpersonate(), fixed the following issues:
    The 'newHandle' is not being freed which causes a memory leak.
    When VGAuth_UserHandleAccessToken fails, unimpersonation is not
    being done.

Signed-off-by: Cathy Avery <cavery@redhat.com>
Signed-off-by: Miroslav Rezanina <mrezanin@redhat.com>
---
 services/plugins/vix/vixTools.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)

diff --git a/services/plugins/vix/vixTools.c b/services/plugins/vix/vixTools.c
index 7ed9f43..3d327e4 100644
--- a/services/plugins/vix/vixTools.c
+++ b/services/plugins/vix/vixTools.c
@@ -11392,6 +11392,7 @@ GuestAuthPasswordAuthenticateImpersonate(
    VGAuthError vgErr;
    VGAuthUserHandle *newHandle = NULL;
    VGAuthExtraParams extraParams[1];
+   Bool impersonated = FALSE;
 
    extraParams[0].name = VGAUTH_PARAM_LOAD_USER_PROFILE;
    extraParams[0].value = VGAUTH_PARAM_VALUE_TRUE;
@@ -11427,6 +11428,8 @@ GuestAuthPasswordAuthenticateImpersonate(
       goto done;
    }
 
+   impersonated = TRUE;
+
 #ifdef _WIN32
    // this is making a copy of the token, be sure to close it
    vgErr = VGAuth_UserHandleAccessToken(ctx, newHandle, userToken);
@@ -11446,6 +11449,10 @@ done:
    Util_ZeroFreeString(password);
 
    if (VIX_OK != err) {
+      if (impersonated) {
+         vgErr = VGAuth_EndImpersonation(ctx);
+         ASSERT(vgErr == VGAUTH_E_OK);
+      }
       VGAuth_UserHandleFree(newHandle);
       newHandle = NULL;
    }
@@ -11480,12 +11487,13 @@ GuestAuthSAMLAuthenticateAndImpersonate(
 {
 #if SUPPORT_VGAUTH
    VixError err;
-   char *token;
-   char *username;
+   char *token = NULL;
+   char *username = NULL;
    VGAuthContext *ctx = NULL;
    VGAuthError vgErr;
    VGAuthUserHandle *newHandle = NULL;
    VGAuthExtraParams extraParams[1];
+   Bool impersonated = FALSE;
 
    extraParams[0].name = VGAUTH_PARAM_LOAD_USER_PROFILE;
    extraParams[0].value = VGAUTH_PARAM_VALUE_TRUE;
@@ -11577,6 +11585,8 @@ impersonate:
       goto done;
    }
 
+   impersonated = TRUE;
+
 #ifdef _WIN32
    // this is making a copy of the token, be sure to close it
    vgErr = VGAuth_UserHandleAccessToken(ctx, newHandle, userToken);
@@ -11592,6 +11602,17 @@ impersonate:
    err = VIX_OK;
 
 done:
+   Util_ZeroFreeString(token);
+   Util_ZeroFreeString(username);
+
+   if (VIX_OK != err) {
+      if (impersonated) {
+         vgErr = VGAuth_EndImpersonation(ctx);
+         ASSERT(vgErr == VGAUTH_E_OK);
+      }
+      VGAuth_UserHandleFree(newHandle);
+      newHandle = NULL;
+   }
 
    return err;
 #else
-- 
1.8.3.1