mirror of
https://github.com/microsoft/PowerToys.git
synced 2025-12-16 11:48:06 +01:00
[runner] Change way of dropping privileges to start PT Run
This commit is contained in:
committed by
Enrico Giordani
parent
e96c82b171
commit
e6408a0c37
@@ -186,26 +186,23 @@ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
|
|||||||
}
|
}
|
||||||
std::wstring_view action{ args[1] };
|
std::wstring_view action{ args[1] };
|
||||||
|
|
||||||
|
|
||||||
if (action == L"-start_PowerLauncher")
|
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);
|
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 (hMapFile)
|
||||||
if (pidBuffer)
|
|
||||||
{
|
{
|
||||||
*pidBuffer = 0;
|
PDWORD pidBuffer = reinterpret_cast<PDWORD>(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD)));
|
||||||
run_non_elevated(L"modules\\launcher\\PowerLauncher.exe", L"", pidBuffer);
|
if (pidBuffer)
|
||||||
FlushViewOfFile(pidBuffer, sizeof(DWORD));
|
{
|
||||||
UnmapViewOfFile(pidBuffer);
|
*pidBuffer = 0;
|
||||||
}
|
run_same_elevation(L"modules\\launcher\\PowerLauncher.exe", L"", pidBuffer);
|
||||||
|
FlushViewOfFile(pidBuffer, sizeof(DWORD));
|
||||||
|
UnmapViewOfFile(pidBuffer);
|
||||||
|
}
|
||||||
|
|
||||||
FlushFileBuffers(hMapFile);
|
FlushFileBuffers(hMapFile);
|
||||||
CloseHandle(hMapFile);
|
CloseHandle(hMapFile);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
else if (action == L"-install_dotnet")
|
else if (action == L"-install_dotnet")
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -510,7 +510,7 @@ bool run_non_elevated(const std::wstring& file, const std::wstring& params, DWOR
|
|||||||
siex.lpAttributeList = pptal;
|
siex.lpAttributeList = pptal;
|
||||||
siex.StartupInfo.cb = sizeof(siex);
|
siex.StartupInfo.cb = sizeof(siex);
|
||||||
|
|
||||||
PROCESS_INFORMATION process_info = { 0 };
|
PROCESS_INFORMATION pi = { 0 };
|
||||||
auto succeeded = CreateProcessW(file.c_str(),
|
auto succeeded = CreateProcessW(file.c_str(),
|
||||||
const_cast<LPWSTR>(executable_args.c_str()),
|
const_cast<LPWSTR>(executable_args.c_str()),
|
||||||
nullptr,
|
nullptr,
|
||||||
@@ -520,31 +520,35 @@ bool run_non_elevated(const std::wstring& file, const std::wstring& params, DWOR
|
|||||||
nullptr,
|
nullptr,
|
||||||
nullptr,
|
nullptr,
|
||||||
&siex.StartupInfo,
|
&siex.StartupInfo,
|
||||||
&process_info);
|
&pi);
|
||||||
|
if (succeeded)
|
||||||
if (process_info.hProcess)
|
|
||||||
{
|
{
|
||||||
if (returnPid)
|
if (pi.hProcess)
|
||||||
{
|
{
|
||||||
*returnPid = GetProcessId(process_info.hProcess);
|
if (returnPid)
|
||||||
}
|
{
|
||||||
|
*returnPid = GetProcessId(pi.hProcess);
|
||||||
|
}
|
||||||
|
|
||||||
CloseHandle(process_info.hProcess);
|
CloseHandle(pi.hProcess);
|
||||||
}
|
}
|
||||||
if (process_info.hThread)
|
if (pi.hThread)
|
||||||
{
|
{
|
||||||
CloseHandle(process_info.hThread);
|
CloseHandle(pi.hThread);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return succeeded;
|
return succeeded;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool run_same_elevation(const std::wstring& file, const std::wstring& params)
|
bool run_same_elevation(const std::wstring& file, const std::wstring& params, DWORD* returnPid)
|
||||||
{
|
{
|
||||||
auto executable_args = L"\"" + file + L"\"";
|
auto executable_args = L"\"" + file + L"\"";
|
||||||
if (!params.empty())
|
if (!params.empty())
|
||||||
{
|
{
|
||||||
executable_args += L" " + params;
|
executable_args += L" " + params;
|
||||||
}
|
}
|
||||||
|
|
||||||
STARTUPINFO si = { 0 };
|
STARTUPINFO si = { 0 };
|
||||||
PROCESS_INFORMATION pi = { 0 };
|
PROCESS_INFORMATION pi = { 0 };
|
||||||
auto succeeded = CreateProcessW(file.c_str(),
|
auto succeeded = CreateProcessW(file.c_str(),
|
||||||
@@ -557,13 +561,23 @@ bool run_same_elevation(const std::wstring& file, const std::wstring& params)
|
|||||||
nullptr,
|
nullptr,
|
||||||
&si,
|
&si,
|
||||||
&pi);
|
&pi);
|
||||||
if (pi.hProcess)
|
|
||||||
|
if (succeeded)
|
||||||
{
|
{
|
||||||
CloseHandle(pi.hProcess);
|
if (pi.hProcess)
|
||||||
}
|
{
|
||||||
if (pi.hThread)
|
if (returnPid)
|
||||||
{
|
{
|
||||||
CloseHandle(pi.hThread);
|
*returnPid = GetProcessId(pi.hProcess);
|
||||||
|
}
|
||||||
|
|
||||||
|
CloseHandle(pi.hProcess);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (pi.hThread)
|
||||||
|
{
|
||||||
|
CloseHandle(pi.hThread);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return succeeded;
|
return succeeded;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -73,7 +73,7 @@ bool run_elevated(const std::wstring& file, const std::wstring& params);
|
|||||||
bool run_non_elevated(const std::wstring& file, const std::wstring& params, DWORD* returnPid);
|
bool run_non_elevated(const std::wstring& file, const std::wstring& params, DWORD* returnPid);
|
||||||
|
|
||||||
// Run command with the same elevation, returns true if succeeded
|
// Run command with the same elevation, returns true if succeeded
|
||||||
bool run_same_elevation(const std::wstring& file, const std::wstring& params);
|
bool run_same_elevation(const std::wstring& file, const std::wstring& params, DWORD* returnPid);
|
||||||
|
|
||||||
// Returns true if the current process is running from administrator account
|
// Returns true if the current process is running from administrator account
|
||||||
bool check_user_is_admin();
|
bool check_user_is_admin();
|
||||||
@@ -137,4 +137,4 @@ struct overloaded : Ts...
|
|||||||
template<class... Ts>
|
template<class... Ts>
|
||||||
overloaded(Ts...)->overloaded<Ts...>;
|
overloaded(Ts...)->overloaded<Ts...>;
|
||||||
|
|
||||||
#define POWER_LAUNCHER_PID_SHARED_FILE L"Global\\3cbfbad4-199b-4e2c-9825-942d5d3d3c74"
|
#define POWER_LAUNCHER_PID_SHARED_FILE L"Local\\3cbfbad4-199b-4e2c-9825-942d5d3d3c74"
|
||||||
|
|||||||
@@ -132,7 +132,7 @@ public:
|
|||||||
// Enable the powertoy
|
// Enable the powertoy
|
||||||
virtual void enable()
|
virtual void enable()
|
||||||
{
|
{
|
||||||
if (is_process_elevated(false) == false)
|
if (!is_process_elevated(false))
|
||||||
{
|
{
|
||||||
SHELLEXECUTEINFOW sei{ sizeof(sei) };
|
SHELLEXECUTEINFOW sei{ sizeof(sei) };
|
||||||
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
|
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
|
||||||
@@ -146,32 +146,34 @@ public:
|
|||||||
{
|
{
|
||||||
std::wstring action_runner_path = get_module_folderpath();
|
std::wstring action_runner_path = get_module_folderpath();
|
||||||
action_runner_path += L"\\action_runner.exe";
|
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
|
// 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);
|
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)));
|
if (hMapFile)
|
||||||
*pidBuffer = 0;
|
|
||||||
m_hProcess = NULL;
|
|
||||||
ShellExecuteExW(&sei);
|
|
||||||
|
|
||||||
const int maxRetries = 20;
|
|
||||||
for (int retry = 0; retry < maxRetries; ++retry)
|
|
||||||
{
|
{
|
||||||
Sleep(50);
|
PDWORD pidBuffer = reinterpret_cast<PDWORD>(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD)));
|
||||||
DWORD pid = *pidBuffer;
|
if (pidBuffer)
|
||||||
if (pid)
|
|
||||||
{
|
{
|
||||||
m_hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid);
|
*pidBuffer = 0;
|
||||||
break;
|
m_hProcess = NULL;
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
CloseHandle(hMapFile);
|
if (run_non_elevated(action_runner_path, L"-start_PowerLauncher", nullptr))
|
||||||
|
{
|
||||||
|
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;
|
m_enabled = true;
|
||||||
|
|||||||
@@ -47,5 +47,5 @@ bool restart_same_elevation()
|
|||||||
constexpr DWORD exe_path_size = 0xFFFF;
|
constexpr DWORD exe_path_size = 0xFFFF;
|
||||||
auto exe_path = std::make_unique<wchar_t[]>(exe_path_size);
|
auto exe_path = std::make_unique<wchar_t[]>(exe_path_size);
|
||||||
GetModuleFileNameW(nullptr, exe_path.get(), exe_path_size);
|
GetModuleFileNameW(nullptr, exe_path.get(), exe_path_size);
|
||||||
return run_same_elevation(exe_path.get(), L"--dont-elevate");
|
return run_same_elevation(exe_path.get(), L"--dont-elevate", nullptr);
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user