Skip to content

Commit

Permalink
[PowerToys Run] run non-elevated when runner is elevated (#3451)
Browse files Browse the repository at this point in the history
* run Launcher non-elevated

* Implemented acquiring PID of Launcher

Co-authored-by: ivan100sic <ivan100sic@gmail.com>
  • Loading branch information
enricogior and ivan100sic committed May 21, 2020
1 parent 582d132 commit a856263
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 11 deletions.
23 changes: 22 additions & 1 deletion src/action_runner/action_runner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,28 @@ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
}
std::wstring_view action{ args[1] };

if (action == L"-install_dotnet")

if (action == L"-start_PowerLauncher")
{
if (is_process_elevated(false) == true)
{
drop_elevated_privileges();
}

HANDLE hMapFile = OpenFileMappingW(FILE_MAP_WRITE, FALSE, POWER_LAUNCHER_PID_SHARED_FILE);
PDWORD pidBuffer = reinterpret_cast<PDWORD>(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD)));
if (pidBuffer)
{
*pidBuffer = 0;
run_non_elevated(L"modules\\launcher\\PowerLauncher.exe", L"", pidBuffer);
FlushViewOfFile(pidBuffer, sizeof(DWORD));
UnmapViewOfFile(pidBuffer);
}

FlushFileBuffers(hMapFile);
CloseHandle(hMapFile);
}
else if (action == L"-install_dotnet")
{
if (dotnet_is_installed())
{
Expand Down
8 changes: 7 additions & 1 deletion src/common/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ bool run_elevated(const std::wstring& file, const std::wstring& params)
}
}

bool run_non_elevated(const std::wstring& file, const std::wstring& params)
bool run_non_elevated(const std::wstring& file, const std::wstring& params, DWORD* returnPid)
{
auto executable_args = L"\"" + file + L"\"";
if (!params.empty())
Expand Down Expand Up @@ -521,8 +521,14 @@ bool run_non_elevated(const std::wstring& file, const std::wstring& params)
nullptr,
&siex.StartupInfo,
&process_info);

if (process_info.hProcess)
{
if (returnPid)
{
*returnPid = GetProcessId(process_info.hProcess);
}

CloseHandle(process_info.hProcess);
}
if (process_info.hThread)
Expand Down
6 changes: 4 additions & 2 deletions src/common/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ bool drop_elevated_privileges();
// Run command as elevated user, returns true if succeeded
bool run_elevated(const std::wstring& file, const std::wstring& params);

// Run command as non-elevated user, returns true if succeeded
bool run_non_elevated(const std::wstring& file, const std::wstring& params);
// Run command as non-elevated user, returns true if succeeded, puts the process id into returnPid if returnPid != NULL
bool run_non_elevated(const std::wstring& file, const std::wstring& params, DWORD* returnPid);

// Run command with the same elevation, returns true if succedded
bool run_same_elevation(const std::wstring& file, const std::wstring& params);
Expand Down Expand Up @@ -136,3 +136,5 @@ struct overloaded : Ts...
};
template<class... Ts>
overloaded(Ts...)->overloaded<Ts...>;

#define POWER_LAUNCHER_PID_SHARED_FILE L"Global\\3cbfbad4-199b-4e2c-9825-942d5d3d3c74"
47 changes: 41 additions & 6 deletions src/modules/launcher/Microsoft.Launcher/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <interface/lowlevel_keyboard_event_data.h>
#include <interface/win_hook_event_data.h>
#include <common/settings_objects.h>
#include <common/common.h>
#include "trace.h"
#include "resource.h"

Expand Down Expand Up @@ -131,13 +132,47 @@ class Microsoft_Launcher : public PowertoyModuleIface {
// Enable the powertoy
virtual void enable()
{
SHELLEXECUTEINFO sei{ sizeof(sei) };
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
sei.lpFile = L"modules\\launcher\\PowerLauncher.exe";
sei.nShow = SW_SHOWNORMAL;
ShellExecuteEx(&sei);
if (is_process_elevated(false) == false)
{
SHELLEXECUTEINFOW sei{ sizeof(sei) };
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
sei.lpFile = L"modules\\launcher\\PowerLauncher.exe";
sei.nShow = SW_SHOWNORMAL;
ShellExecuteExW(&sei);

m_hProcess = sei.hProcess;
m_hProcess = sei.hProcess;
}
else
{
std::wstring action_runner_path = get_module_folderpath();
action_runner_path += L"\\action_runner.exe";
SHELLEXECUTEINFOW sei{ sizeof(sei) };
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI | SEE_MASK_NOASYNC };
sei.lpFile = action_runner_path.c_str();
sei.nShow = SW_SHOWNORMAL;
sei.lpParameters = L"-start_PowerLauncher";

// Set up the shared file from which to retrieve the PID of PowerLauncher
HANDLE hMapFile = CreateFileMappingW(INVALID_HANDLE_VALUE, NULL, PAGE_READWRITE, 0, sizeof(DWORD), POWER_LAUNCHER_PID_SHARED_FILE);
PDWORD pidBuffer = reinterpret_cast<PDWORD>(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD)));
*pidBuffer = 0;
m_hProcess = NULL;
ShellExecuteExW(&sei);

const int maxRetries = 20;
for (int retry = 0; retry < maxRetries; ++retry)
{
Sleep(50);
DWORD pid = *pidBuffer;
if (pid)
{
m_hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
break;
}
}

CloseHandle(hMapFile);
}

m_enabled = true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/runner/restart_elevated.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ bool restart_if_scheduled()
case RestartAsElevated:
return run_elevated(exe_path.get(), {});
case RestartAsNonElevated:
return run_non_elevated(exe_path.get(), L"--dont-elevate");
return run_non_elevated(exe_path.get(), L"--dont-elevate", NULL);
default:
return false;
}
Expand Down

0 comments on commit a856263

Please sign in to comment.