From f81ccdda3d4f9af76a6bcfb0f7bca72074eeb43a Mon Sep 17 00:00:00 2001 From: Enrico Giordani Date: Fri, 29 May 2020 19:02:37 +0200 Subject: [PATCH] [runner] Change way of dropping privileges to start PT Run --- src/action_runner/action_runner.cpp | 27 ++++---- src/common/common.cpp | 61 ++++++++++++------- src/common/common.h | 6 +- .../launcher/Microsoft.Launcher/dllmain.cpp | 46 +++++++------- src/runner/restart_elevated.cpp | 2 +- 5 files changed, 78 insertions(+), 64 deletions(-) diff --git a/src/action_runner/action_runner.cpp b/src/action_runner/action_runner.cpp index 814916881f..7867dc2a21 100644 --- a/src/action_runner/action_runner.cpp +++ b/src/action_runner/action_runner.cpp @@ -186,26 +186,23 @@ int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int) } std::wstring_view action{ args[1] }; - 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(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD))); - if (pidBuffer) + if (hMapFile) { - *pidBuffer = 0; - run_non_elevated(L"modules\\launcher\\PowerLauncher.exe", L"", pidBuffer); - FlushViewOfFile(pidBuffer, sizeof(DWORD)); - UnmapViewOfFile(pidBuffer); - } + PDWORD pidBuffer = reinterpret_cast(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD))); + if (pidBuffer) + { + *pidBuffer = 0; + run_same_elevation(L"modules\\launcher\\PowerLauncher.exe", L"", pidBuffer); + FlushViewOfFile(pidBuffer, sizeof(DWORD)); + UnmapViewOfFile(pidBuffer); + } - FlushFileBuffers(hMapFile); - CloseHandle(hMapFile); + FlushFileBuffers(hMapFile); + CloseHandle(hMapFile); + } } else if (action == L"-install_dotnet") { diff --git a/src/common/common.cpp b/src/common/common.cpp index e822e3bd68..fe36bf560e 100644 --- a/src/common/common.cpp +++ b/src/common/common.cpp @@ -510,8 +510,8 @@ bool run_non_elevated(const std::wstring& file, const std::wstring& params, DWOR siex.lpAttributeList = pptal; siex.StartupInfo.cb = sizeof(siex); - PROCESS_INFORMATION process_info = { 0 }; - auto succedded = CreateProcessW(file.c_str(), + PROCESS_INFORMATION pi = { 0 }; + auto succeeded = CreateProcessW(file.c_str(), const_cast(executable_args.c_str()), nullptr, nullptr, @@ -520,34 +520,38 @@ bool run_non_elevated(const std::wstring& file, const std::wstring& params, DWOR nullptr, nullptr, &siex.StartupInfo, - &process_info); - - if (process_info.hProcess) + &pi); + if (succeeded) { - if (returnPid) + if (pi.hProcess) { - *returnPid = GetProcessId(process_info.hProcess); - } + if (returnPid) + { + *returnPid = GetProcessId(pi.hProcess); + } - CloseHandle(process_info.hProcess); + CloseHandle(pi.hProcess); + } + if (pi.hThread) + { + CloseHandle(pi.hThread); + } } - if (process_info.hThread) - { - CloseHandle(process_info.hThread); - } - return succedded; + + 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"\""; if (!params.empty()) { executable_args += L" " + params; } + STARTUPINFO si = { 0 }; PROCESS_INFORMATION pi = { 0 }; - auto succedded = CreateProcessW(file.c_str(), + auto succeeded = CreateProcessW(file.c_str(), const_cast(executable_args.c_str()), nullptr, nullptr, @@ -557,15 +561,26 @@ bool run_same_elevation(const std::wstring& file, const std::wstring& params) nullptr, &si, &pi); - if (pi.hProcess) + + if (succeeded) { - CloseHandle(pi.hProcess); + if (pi.hProcess) + { + if (returnPid) + { + *returnPid = GetProcessId(pi.hProcess); + } + + CloseHandle(pi.hProcess); + } + + if (pi.hThread) + { + CloseHandle(pi.hThread); + } } - if (pi.hThread) - { - CloseHandle(pi.hThread); - } - return succedded; + + return succeeded; } std::wstring get_process_path(HWND window) noexcept diff --git a/src/common/common.h b/src/common/common.h index cd9758860e..7547f98907 100644 --- a/src/common/common.h +++ b/src/common/common.h @@ -72,8 +72,8 @@ bool run_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); +// Run command with the same elevation, returns true if succeeded +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 bool check_user_is_admin(); @@ -137,4 +137,4 @@ struct overloaded : Ts... template overloaded(Ts...)->overloaded; -#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" diff --git a/src/modules/launcher/Microsoft.Launcher/dllmain.cpp b/src/modules/launcher/Microsoft.Launcher/dllmain.cpp index 3245949e12..15f0dd1475 100644 --- a/src/modules/launcher/Microsoft.Launcher/dllmain.cpp +++ b/src/modules/launcher/Microsoft.Launcher/dllmain.cpp @@ -132,7 +132,7 @@ public: // Enable the powertoy virtual void enable() { - if (is_process_elevated(false) == false) + if (!is_process_elevated(false)) { SHELLEXECUTEINFOW sei{ sizeof(sei) }; sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI }; @@ -146,32 +146,34 @@ public: { 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(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) + if (hMapFile) { - Sleep(50); - DWORD pid = *pidBuffer; - if (pid) + PDWORD pidBuffer = reinterpret_cast(MapViewOfFile(hMapFile, FILE_MAP_ALL_ACCESS, 0, 0, sizeof(DWORD))); + if (pidBuffer) { - m_hProcess = OpenProcess(PROCESS_TERMINATE, FALSE, pid); - break; - } - } + *pidBuffer = 0; + 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; diff --git a/src/runner/restart_elevated.cpp b/src/runner/restart_elevated.cpp index b86e2c640c..f96a78d2b4 100644 --- a/src/runner/restart_elevated.cpp +++ b/src/runner/restart_elevated.cpp @@ -47,5 +47,5 @@ bool restart_same_elevation() constexpr DWORD exe_path_size = 0xFFFF; auto exe_path = std::make_unique(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); }