[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

View File

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