]> git.ipfire.org Git - thirdparty/open-vm-tools.git/commitdiff
Check the real path for the alias store file
authorKruti Pendharkar <kp025370@broadcom.com>
Wed, 11 Jun 2025 00:48:53 +0000 (17:48 -0700)
committerKruti Pendharkar <kp025370@broadcom.com>
Wed, 11 Jun 2025 00:48:53 +0000 (17:48 -0700)
Verify the real path, which is the fully resolved path from junctions,
symbolic links, and path traversal characters.

open-vm-tools/vgauth/serviceImpl/alias.c
open-vm-tools/vgauth/serviceImpl/serviceInt.h

index 2a371f9ef6a722e32a6dbad48514e91a337d331d..bca3e7281ed2006114615ab77da836ca5c797b22 100644 (file)
@@ -253,6 +253,12 @@ mapping file layout:
 
  */
 
+#ifdef _WIN32
+#define ISPATHSEP(c)  ((c) == '\\' || (c) == '/')
+#else
+#define ISPATHSEP(c)  ((c) == '/')
+#endif
+
 
 /*
  ******************************************************************************
@@ -467,6 +473,7 @@ ServiceLoadFileContentsWin(const gchar *fileName,
    gunichar2 *fileNameW = NULL;
    BOOL ok;
    DWORD bytesRead;
+   gchar *realPath = NULL;
 
    *fileSize = 0;
    *contents = NULL;
@@ -623,6 +630,20 @@ ServiceLoadFileContentsWin(const gchar *fileName,
       goto done;
    }
 
+   /*
+    * Check if fileName is real path.
+    */
+   if ((realPath = ServiceFileGetPathByHandle(hFile)) == NULL) {
+      err = VGAUTH_E_FAIL;
+      goto done;
+   }
+   if (g_strcmp0(realPath, fileName) != 0) {
+      Warning("%s: Real path (%s) is not same as file path (%s)\n",
+              __FUNCTION__, realPath, fileName);
+      err = VGAUTH_E_FAIL;
+      goto done;
+   }
+
    /*
     * Now finally read the contents.
     */
@@ -651,6 +672,7 @@ done:
       CloseHandle(hFile);
    }
    g_free(fileNameW);
+   g_free(realPath);
 
    return err;
 }
@@ -673,6 +695,7 @@ ServiceLoadFileContentsPosix(const gchar *fileName,
    gchar *buf;
    gchar *bp;
    int fd = -1;
+   gchar realPath[PATH_MAX] = { 0 };
 
    *fileSize = 0;
    *contents = NULL;
@@ -818,6 +841,21 @@ ServiceLoadFileContentsPosix(const gchar *fileName,
       goto done;
    }
 
+   /*
+    * Check if fileName is real path.
+    */
+   if (realpath(fileName, realPath) == NULL) {
+      Warning("%s: realpath() failed. errno (%d)\n", __FUNCTION__, errno);
+      err = VGAUTH_E_FAIL;
+      goto done;
+   }
+   if (g_strcmp0(realPath, fileName) != 0) {
+      Warning("%s: Real path (%s) is not same as file path (%s)\n",
+              __FUNCTION__, realPath, fileName);
+      err = VGAUTH_E_FAIL;
+      goto done;
+   }
+
    /*
     * All confidence checks passed; read the bits.
     */
@@ -3310,6 +3348,7 @@ ServiceAliasInitAliasStore(void)
    VGAuthError err = VGAUTH_E_OK;
    gboolean saveBadDir = FALSE;
    char *defaultDir = NULL;
+   size_t len;
 
 #ifdef _WIN32
    {
@@ -3353,6 +3392,14 @@ ServiceAliasInitAliasStore(void)
                                       VGAUTH_PREF_GROUP_NAME_SERVICE,
                                       defaultDir);
 
+   /*
+    * Remove the trailing separator if any from aliasStoreRootDir path.
+    */
+   len = strlen(aliasStoreRootDir);
+   if (ISPATHSEP(aliasStoreRootDir[len - 1])) {
+      aliasStoreRootDir[len - 1] = '\0';
+   }
+
    Log("Using '%s' for alias store root directory\n", aliasStoreRootDir);
 
    g_free(defaultDir);
index 5f420192bc4c14f6dfbda587b0132efd7abf15b7..768da1b583b5cffeb81e09c4077ba4b8a9abd2ab 100644 (file)
@@ -1,5 +1,6 @@
 /*********************************************************
- * Copyright (c) 2011-2017,2023 VMware, Inc. All rights reserved.
+ * Copyright (c) 2011-2025 Broadcom. All Rights Reserved.
+ * The term "Broadcom" refers to Broadcom Inc. and/or its subsidiaries.
  *
  * This program is free software; you can redistribute it and/or modify it
  * under the terms of the GNU Lesser General Public License as published
@@ -441,6 +442,7 @@ VGAuthError ServiceFileVerifyAdminGroupOwnedByHandle(const HANDLE hFile);
 VGAuthError ServiceFileVerifyEveryoneReadableByHandle(const HANDLE hFile);
 VGAuthError ServiceFileVerifyUserAccessByHandle(const HANDLE hFile,
                                                 const char *userName);
+gchar *ServiceFileGetPathByHandle(HANDLE hFile);
 #else
 VGAuthError ServiceFileVerifyFileOwnerAndPerms(const char *fileName,
                                                const char *userName,