summaryrefslogtreecommitdiff
path: root/windows
diff options
context:
space:
mode:
authorvisuve <[email protected]>2022-08-10 17:08:51 +0300
committervisuve <[email protected]>2022-08-10 17:08:51 +0300
commit378e4ea34ef950188565182e63db40763da49699 (patch)
tree7c803060992ff76aa8ea79aa978ef0f0ce63821b /windows
parentff670d044a9a62389d6bda701178eb434c95bad7 (diff)
Fix service installation MAX_PATH bug on Windows
- On newer Windows, a path might hold up to 32,767 characters - https://docs.microsoft.com/en-us/windows/win32/fileio/maximum-file-path-limitation - The previous installation did not also check for ERROR_INSUFFICIENT_BUFFER which could lead to an ill formatted path
Diffstat (limited to 'windows')
-rw-r--r--windows/ZeroTierOne/ServiceInstaller.cpp26
1 files changed, 16 insertions, 10 deletions
diff --git a/windows/ZeroTierOne/ServiceInstaller.cpp b/windows/ZeroTierOne/ServiceInstaller.cpp
index 05a78002..39a0519f 100644
--- a/windows/ZeroTierOne/ServiceInstaller.cpp
+++ b/windows/ZeroTierOne/ServiceInstaller.cpp
@@ -49,28 +49,34 @@ std::string InstallService(PSTR pszServiceName,
PSTR pszAccount,
PSTR pszPassword)
{
- std::string ret;
- char szPathTmp[MAX_PATH],szPath[MAX_PATH];
+ std::string ret;
+ std::string path(0x7FFF, '\0');
+
SC_HANDLE schSCManager = NULL;
SC_HANDLE schService = NULL;
SERVICE_DESCRIPTION sd;
LPTSTR szDesc = TEXT("ZeroTier network virtualization service.");
- if (GetModuleFileName(NULL, szPathTmp, ARRAYSIZE(szPath)) == 0)
+ DWORD dwCharacters = GetModuleFileName(NULL, path.data(), path.size());
+
+ if (dwCharacters == 0)
{
- ret = "GetModuleFileName failed, unable to get path to self";
+ ret = "GetModuleFileName failed, unable to get path to self";
goto Cleanup;
}
- // Quote path in case it contains spaces
- _snprintf_s(szPath,sizeof(szPath),"\"%s\"",szPathTmp);
+ // Trim excess nulls which the returned size does not include
+ path.resize(dwCharacters);
+
+ // Quote path in case it contains spaces
+ path = '"' + path + '"';
// Open the local default service control manager database
schSCManager = OpenSCManager(NULL, NULL, SC_MANAGER_CONNECT |
SC_MANAGER_CREATE_SERVICE);
if (schSCManager == NULL)
{
- ret = "OpenSCManager failed";
+ ret = "OpenSCManager failed";
goto Cleanup;
}
@@ -83,7 +89,7 @@ std::string InstallService(PSTR pszServiceName,
SERVICE_WIN32_OWN_PROCESS, // Service type
dwStartType, // Service start type
SERVICE_ERROR_NORMAL, // Error control type
- szPath, // Service's binary
+ path.c_str(), // Service's binary
NULL, // No load ordering group
NULL, // No tag identifier
pszDependencies, // Dependencies
@@ -92,7 +98,7 @@ std::string InstallService(PSTR pszServiceName,
);
if (schService == NULL)
{
- ret = "CreateService failed";
+ ret = "CreateService failed";
goto Cleanup;
}
@@ -112,7 +118,7 @@ Cleanup:
schService = NULL;
}
- return ret;
+ return ret;
}