[PowerToys Run] run non-elevated when runner is elevated (#3451)

* run Launcher non-elevated

* Implemented acquiring PID of Launcher

Co-authored-by: ivan100sic <ivan100sic@gmail.com>
This commit is contained in:
Enrico Giordani 2020-05-21 19:44:32 +02:00 committed by GitHub
parent 582d1320a3
commit a856263081
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 75 additions and 11 deletions

View File

@ -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())
{

View File

@ -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())
@ -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)

View File

@ -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);
@ -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"

View File

@ -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"
@ -131,13 +132,47 @@ public:
// 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;
}

View File

@ -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;
}