Skip to content

Commit

Permalink
[MWB] Fix use after free in service registration (#31633)
Browse files Browse the repository at this point in the history
* [MWB] Fix use after free in service registration

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

---------

Co-authored-by: Andrey Nekrasov <[email protected]>
  • Loading branch information
yuyoyuppe and yuyoyuppe committed Feb 29, 2024
1 parent eeedbc7 commit 70d790c
Showing 1 changed file with 17 additions and 16 deletions.
33 changes: 17 additions & 16 deletions src/modules/MouseWithoutBorders/ModuleInterface/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ class MouseWithoutBorders : public PowertoyModuleIface
Logger::error("Failed to delete MWB service");
return;
}

Trace::MouseWithoutBorders::ToggleServiceRegistration(false);
}

Expand Down Expand Up @@ -242,11 +242,10 @@ class MouseWithoutBorders : public PowertoyModuleIface
}

// 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"\" ";
Expand All @@ -259,7 +258,8 @@ class MouseWithoutBorders : public PowertoyModuleIface
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);
}

Expand Down Expand Up @@ -291,18 +291,19 @@ class MouseWithoutBorders : public PowertoyModuleIface

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());
}
Expand Down

0 comments on commit 70d790c

Please sign in to comment.