From 821fe09e97e71247f5454caa7076263647056aa8 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Wed, 4 Oct 2017 18:52:46 +0100 Subject: [PATCH 1/4] - Add large page initialisation on Windows - Add a proper UAC manifest requesting elevation when we start - Fix self_test() - this is integral part of initialisation --- CMakeLists.txt | 4 + xmrstak/backend/backendConnector.cpp | 3 +- .../backend/cpu/crypto/cryptonight_common.cpp | 81 ++++++++++++++++++- 3 files changed, 84 insertions(+), 4 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 3c7a585..0da4f06 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -401,6 +401,10 @@ set(LIBRARY_OUTPUT_PATH "bin") target_link_libraries(xmr-stak ${LIBS} xmr-stak-c xmr-stak-backend) +if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") + set_property(TARGET xmr-stak PROPERTY LINK_FLAGS "/level='requireAdministrator' /uiAccess='false'") +endif() + ################################################################################ # Install ################################################################################ diff --git a/xmrstak/backend/backendConnector.cpp b/xmrstak/backend/backendConnector.cpp index 6106267..28ba1ac 100644 --- a/xmrstak/backend/backendConnector.cpp +++ b/xmrstak/backend/backendConnector.cpp @@ -52,8 +52,7 @@ namespace xmrstak bool BackendConnector::self_test() { - - return true; + return cpu::minethd::self_test(); } std::vector* BackendConnector::thread_starter(miner_work& pWork) diff --git a/xmrstak/backend/cpu/crypto/cryptonight_common.cpp b/xmrstak/backend/cpu/crypto/cryptonight_common.cpp index 9d03ed7..1cff5bb 100644 --- a/xmrstak/backend/cpu/crypto/cryptonight_common.cpp +++ b/xmrstak/backend/cpu/crypto/cryptonight_common.cpp @@ -45,6 +45,7 @@ extern "C" #ifdef _WIN32 #include +#include #else #include #include @@ -92,14 +93,90 @@ BOOL AddPrivilege(TCHAR* pszPrivilege) CloseHandle(hToken); return TRUE; } + +BOOL AddLargePageRights() +{ + HANDLE hToken; + PTOKEN_USER user = NULL; + + if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken) == TRUE) + { + TOKEN_ELEVATION Elevation; + DWORD cbSize = sizeof(TOKEN_ELEVATION); + BOOL bIsElevated = FALSE; + + if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize)) + bIsElevated = Elevation.TokenIsElevated; + + DWORD size = 0; + GetTokenInformation(hToken, TokenUser, NULL, 0, &size); + + if (size > 0 && bIsElevated) + { + user = (PTOKEN_USER)LocalAlloc(LPTR, size); + GetTokenInformation(hToken, TokenUser, user, size, &size); + } + + CloseHandle(hToken); + } + + if (!user) + return FALSE; + + LSA_HANDLE handle; + LSA_OBJECT_ATTRIBUTES attributes; + ZeroMemory(&attributes, sizeof(attributes)); + + BOOL result = FALSE; + if (LsaOpenPolicy(NULL, &attributes, POLICY_ALL_ACCESS, &handle) == 0) + { + LSA_UNICODE_STRING lockmem; + lockmem.Buffer = L"SeLockMemoryPrivilege"; + lockmem.Length = 42; + lockmem.MaximumLength = 44; + + PLSA_UNICODE_STRING rights = NULL; + ULONG cnt = 0; + BOOL bHasRights = FALSE; + if (LsaEnumerateAccountRights(handle, user->User.Sid, &rights, &cnt) == 0) + { + for (size_t i = 0; i < cnt; i++) + { + if (rights[i].Length == lockmem.Length && + memcmp(rights[i].Buffer, lockmem.Buffer, 42) == 0) + { + bHasRights = TRUE; + break; + } + } + + LsaFreeMemory(rights); + } + + if(!bHasRights) + result = LsaAddAccountRights(handle, user->User.Sid, &lockmem, 1) == 0; + + LsaClose(handle); + } + + LocalFree(user); + return result; +} #endif size_t cryptonight_init(size_t use_fast_mem, size_t use_mlock, alloc_msg* msg) { #ifdef _WIN32 - if (AddPrivilege(TEXT("SeLockMemoryPrivilege")) == 0) + if(use_fast_mem == 0) + return 1; + + if(AddPrivilege(TEXT("SeLockMemoryPrivilege")) == 0) { - msg->warning = "Obtaning SeLockMemoryPrivilege failed."; + if(AddLargePageRights()) + msg->warning = "Added SeLockMemoryPrivilege to the current account. You need to reboot for it to work"; + else + msg->warning = "Obtaning SeLockMemoryPrivilege failed."; + return 0; } return 1; From 2b9bcc2dfd835d3c0a6a7de3f5f224c9afd5434b Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Wed, 4 Oct 2017 19:04:40 +0100 Subject: [PATCH 2/4] Add a suggestion to reboot in cases where it might help --- xmrstak/backend/cpu/crypto/cryptonight_common.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/xmrstak/backend/cpu/crypto/cryptonight_common.cpp b/xmrstak/backend/cpu/crypto/cryptonight_common.cpp index 1cff5bb..aeb5a83 100644 --- a/xmrstak/backend/cpu/crypto/cryptonight_common.cpp +++ b/xmrstak/backend/cpu/crypto/cryptonight_common.cpp @@ -164,6 +164,7 @@ BOOL AddLargePageRights() } #endif +BOOL bRebootDesirable = FALSE; //If VirtualAlloc fails, suggest a reboot size_t cryptonight_init(size_t use_fast_mem, size_t use_mlock, alloc_msg* msg) { #ifdef _WIN32 @@ -173,12 +174,17 @@ size_t cryptonight_init(size_t use_fast_mem, size_t use_mlock, alloc_msg* msg) if(AddPrivilege(TEXT("SeLockMemoryPrivilege")) == 0) { if(AddLargePageRights()) + { msg->warning = "Added SeLockMemoryPrivilege to the current account. You need to reboot for it to work"; + bRebootDesirable = TRUE; + } else msg->warning = "Obtaning SeLockMemoryPrivilege failed."; return 0; } + + bRebootDesirable = TRUE; return 1; #else return 1; @@ -210,7 +216,10 @@ cryptonight_ctx* cryptonight_alloc_ctx(size_t use_fast_mem, size_t use_mlock, al if(ptr->long_state == NULL) { _mm_free(ptr); - msg->warning = "VirtualAlloc failed."; + if(bRebootDesirable) + msg->warning = "VirtualAlloc failed. Reboot might help."; + else + msg->warning = "VirtualAlloc failed."; return NULL; } else From f9bbdd50bb8a3ceb45606cc02cc7fc127921ea48 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Wed, 4 Oct 2017 20:33:18 +0100 Subject: [PATCH 3/4] put global into proper place --- xmrstak/backend/cpu/crypto/cryptonight_common.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/xmrstak/backend/cpu/crypto/cryptonight_common.cpp b/xmrstak/backend/cpu/crypto/cryptonight_common.cpp index aeb5a83..0690415 100644 --- a/xmrstak/backend/cpu/crypto/cryptonight_common.cpp +++ b/xmrstak/backend/cpu/crypto/cryptonight_common.cpp @@ -71,6 +71,8 @@ void do_skein_hash(const void* input, size_t len, char* output) { void (* const extra_hashes[4])(const void *, size_t, char *) = {do_blake_hash, do_groestl_hash, do_jh_hash, do_skein_hash}; #ifdef _WIN32 +BOOL bRebootDesirable = FALSE; //If VirtualAlloc fails, suggest a reboot + BOOL AddPrivilege(TCHAR* pszPrivilege) { HANDLE hToken; @@ -164,7 +166,6 @@ BOOL AddLargePageRights() } #endif -BOOL bRebootDesirable = FALSE; //If VirtualAlloc fails, suggest a reboot size_t cryptonight_init(size_t use_fast_mem, size_t use_mlock, alloc_msg* msg) { #ifdef _WIN32 From d8d403b2ace66ba05c9c6ff9da35361920fde908 Mon Sep 17 00:00:00 2001 From: fireice-uk Date: Wed, 4 Oct 2017 22:11:31 +0100 Subject: [PATCH 4/4] Non-manifest UAC elevation --- CMakeLists.txt | 4 ---- xmrstak/cli/cli-miner.cpp | 11 +++++++++- xmrstak/misc/uac.hpp | 44 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 54 insertions(+), 5 deletions(-) create mode 100644 xmrstak/misc/uac.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 0da4f06..3c7a585 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -401,10 +401,6 @@ set(LIBRARY_OUTPUT_PATH "bin") target_link_libraries(xmr-stak ${LIBS} xmr-stak-c xmr-stak-backend) -if(CMAKE_CXX_COMPILER_ID MATCHES "MSVC") - set_property(TARGET xmr-stak PROPERTY LINK_FLAGS "/level='requireAdministrator' /uiAccess='false'") -endif() - ################################################################################ # Install ################################################################################ diff --git a/xmrstak/cli/cli-miner.cpp b/xmrstak/cli/cli-miner.cpp index ff31d2c..fbd5039 100644 --- a/xmrstak/cli/cli-miner.cpp +++ b/xmrstak/cli/cli-miner.cpp @@ -33,7 +33,11 @@ #include "xmrstak/version.hpp" #ifndef CONF_NO_HTTPD -# include "xmrstak/http//httpd.hpp" +# include "xmrstak/http/httpd.hpp" +#endif + +#ifdef _WIN32 +# include "xmrstak/misc/uac.hpp" #endif #include @@ -88,6 +92,11 @@ void help() int main(int argc, char *argv[]) { +#ifdef _WIN32 + if (!IsElevated() && SelfElevate(argv[0])) + return 0; +#endif + #ifndef CONF_NO_TLS SSL_library_init(); SSL_load_error_strings(); diff --git a/xmrstak/misc/uac.hpp b/xmrstak/misc/uac.hpp new file mode 100644 index 0000000..fdf3be9 --- /dev/null +++ b/xmrstak/misc/uac.hpp @@ -0,0 +1,44 @@ +#pragma once +#include + +BOOL IsElevated() +{ + BOOL fRet = FALSE; + HANDLE hToken = NULL; + if (OpenProcessToken(GetCurrentProcess(), TOKEN_QUERY, &hToken)) + { + TOKEN_ELEVATION Elevation; + DWORD cbSize = sizeof(TOKEN_ELEVATION); + if (GetTokenInformation(hToken, TokenElevation, &Elevation, sizeof(Elevation), &cbSize)) + fRet = Elevation.TokenIsElevated; + } + if (hToken) + CloseHandle(hToken); + return fRet; +} + +BOOL SelfElevate(const char* my_path) +{ + if (IsElevated()) + return FALSE; + + SHELLEXECUTEINFO shExecInfo = { 0 }; + shExecInfo.cbSize = sizeof(SHELLEXECUTEINFO); + shExecInfo.fMask = NULL; + shExecInfo.hwnd = NULL; + shExecInfo.lpVerb = "runas"; + shExecInfo.lpFile = my_path; + shExecInfo.lpParameters = NULL; + shExecInfo.lpDirectory = NULL; + shExecInfo.nShow = SW_SHOW; + shExecInfo.hInstApp = NULL; + + if (!ShellExecuteEx(&shExecInfo)) + return FALSE; + + // Hide our window and loiter in the background to make scripting easier + // ShowWindow(GetConsoleWindow(), SW_HIDE); + // WaitForSingleObject(shExecInfo.hProcess, INFINITE); + + return TRUE; +}