[MWB] Fix use after free in service registration (#31633)

* [MWB] Fix use after free in service registration

* Update src/modules/MouseWithoutBorders/ModuleInterface/dllmain.cpp

---------

Co-authored-by: Andrey Nekrasov <1828123+yuyoyuppe@users.noreply.github.com>
This commit is contained in:
Andrey Nekrasov 2024-02-29 18:05:26 +01:00 committed by GitHub
parent eeedbc7982
commit 70d790cfde
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -198,7 +198,7 @@ private:
Logger::error("Failed to delete MWB service");
return;
}
Trace::MouseWithoutBorders::ToggleServiceRegistration(false);
}
@ -242,11 +242,10 @@ private:
}
// Pass local app data of the current user to the service
PWSTR cLocalAppPath;
wil::unique_cotaskmem_string cLocalAppPath;
winrt::check_hresult(SHGetKnownFolderPath(FOLDERID_LocalAppData, 0, nullptr, &cLocalAppPath));
CoTaskMemFree(cLocalAppPath);
std::wstring localAppPath{ cLocalAppPath };
std::wstring localAppPath{ cLocalAppPath.get() };
std::wstring binaryWithArgsPath = L"\"";
binaryWithArgsPath += servicePath;
binaryWithArgsPath += L"\" ";
@ -259,7 +258,8 @@ private:
std::wstring_view existingServicePath{ pServiceConfig->lpBinaryPathName };
alreadyRegistered = true;
isServicePathCorrect = (existingServicePath == binaryWithArgsPath);
if (isServicePathCorrect) {
if (isServicePathCorrect)
{
Logger::warn(L"The service path is not correct. Current: {} Expected: {}", existingServicePath, binaryWithArgsPath);
}
@ -291,18 +291,19 @@ private:
if (alreadyRegistered)
{
if (!isServicePathCorrect) {
if (!isServicePathCorrect)
{
if (!ChangeServiceConfigW(schService,
SERVICE_NO_CHANGE,
SERVICE_NO_CHANGE,
SERVICE_NO_CHANGE,
binaryWithArgsPath.c_str(),
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr))
SERVICE_NO_CHANGE,
SERVICE_NO_CHANGE,
SERVICE_NO_CHANGE,
binaryWithArgsPath.c_str(),
nullptr,
nullptr,
nullptr,
nullptr,
nullptr,
nullptr))
{
Logger::error(L"Failed to update the service's path. ERROR: {}", GetLastError());
}