From ff77873ad65995b56cd802d44d1837f36ccf17c8 Mon Sep 17 00:00:00 2001 From: Jamie Reece Wilson Date: Wed, 11 Oct 2023 02:13:49 +0100 Subject: [PATCH] [*] Further reduce win32 link time requirements (advanced api) --- Source/AuProcAddresses.NT.cpp | 16 ++- Source/AuProcAddresses.NT.hpp | 132 ++++++++++++++++++++---- Source/Debug/ExceptionWatcher.Win32.cpp | 13 ++- Source/IO/FS/Resources.cpp | 53 +++++++--- Source/Logging/Sinks/EventLog.Win32.cpp | 18 +++- Source/SWInfo/AuSWInfo.NT.cpp | 17 ++- 6 files changed, 202 insertions(+), 47 deletions(-) diff --git a/Source/AuProcAddresses.NT.cpp b/Source/AuProcAddresses.NT.cpp index 9087dd06..54f91fc1 100644 --- a/Source/AuProcAddresses.NT.cpp +++ b/Source/AuProcAddresses.NT.cpp @@ -168,7 +168,21 @@ namespace Aurora ADD_GET_PROC(AdvancedApi, CryptAcquireContextW) ADD_GET_PROC(AdvancedApi, CryptReleaseContext) ADD_GET_PROC(AdvancedApi, CryptGenRandom) - + + ADD_GET_PROC(AdvancedApi, RegSetValueExW) + ADD_GET_PROC(AdvancedApi, RegCloseKey) + ADD_GET_PROC(AdvancedApi, RegOpenKeyExW) + ADD_GET_PROC(AdvancedApi, RegQueryValueExW) + + ADD_GET_PROC(AdvancedApi, DeregisterEventSource) + ADD_GET_PROC(AdvancedApi, RegisterEventSourceW) + ADD_GET_PROC(AdvancedApi, ReportEventW) + + ADD_GET_PROC(AdvancedApi, SetEntriesInAclA) + ADD_GET_PROC(AdvancedApi, AllocateAndInitializeSid) + ADD_GET_PROC(AdvancedApi, SetNamedSecurityInfoW) + ADD_GET_PROC(AdvancedApi, FreeSid) + ADD_GET_PROC_INTERNAL_MAP(AdvancedApi, RtlGenRandom, SystemFunction036) ADD_GET_PROC(BCrypt, BCryptGenRandom) diff --git a/Source/AuProcAddresses.NT.hpp b/Source/AuProcAddresses.NT.hpp index fd095f35..8991c7a6 100644 --- a/Source/AuProcAddresses.NT.hpp +++ b/Source/AuProcAddresses.NT.hpp @@ -17,6 +17,9 @@ struct _MINIDUMP_CALLBACK_INFORMATION; struct _MIB_IPADDRTABLE; struct _IP_ADAPTER_INFO; struct _CREATEFILE2_EXTENDED_PARAMETERS; +struct _EXPLICIT_ACCESS_A; +struct _ACL; +enum _SE_OBJECT_TYPE; enum _MINIDUMP_TYPE; #if defined(AURORA_COMPILER_MSVC) @@ -292,32 +295,12 @@ namespace Aurora ULONG Flags ); - inline NTSTATUS(__stdcall *pBCryptGenRandom)( - PVOID hAlgorithm, - PUCHAR pbBuffer, - ULONG cbBuffer, - ULONG dwFlags - ); - inline BOOL(__stdcall *pCryptGenRandom)( ULONG_PTR hProv, DWORD dwLen, BYTE *pbBuffer ); - inline BOOL(__stdcall *pCryptAcquireContextW)( - ULONG_PTR * hProv, - LPCWSTR szContainer, - LPCWSTR szProvider, - DWORD dwProvType, - DWORD dwFlags - ); - - inline BOOL(__stdcall *pCryptReleaseContext)( - ULONG_PTR hProvz, - DWORD dwFlags - ); - inline NTSTATUS(__stdcall *pZwSetTimerResolution)( ULONG RequestedResolution, BOOLEAN Set, @@ -438,6 +421,13 @@ namespace Aurora HANDLE hThread ); + inline NTSTATUS(__stdcall *pBCryptGenRandom)( + PVOID hAlgorithm, + PUCHAR pbBuffer, + ULONG cbBuffer, + ULONG dwFlags + ); + // dbghelp inline DWORD(__stdcall *pUnDecorateSymbolName)( @@ -583,6 +573,108 @@ namespace Aurora int * pNumArgs ); + // Advanced API + + inline LSTATUS(__stdcall *pRegSetValueExW)( + HKEY hKey, + LPCWSTR lpValueName, + DWORD Reserved, + DWORD dwType, + CONST BYTE * lpData, + DWORD cbData + ); + + inline LSTATUS(__stdcall *pRegQueryValueExW)( + HKEY hKey, + LPCWSTR lpValueName, + LPDWORD lpReserved, + LPDWORD lpType, + LPBYTE lpData, + LPDWORD lpcbData + ); + + inline LSTATUS(__stdcall *pRegCloseKey)( + HKEY hKey + ); + + inline LSTATUS(__stdcall *pRegOpenKeyExW)( + HKEY hKey, + LPCWSTR lpSubKey, + DWORD ulOptions, + REGSAM samDesired, + PHKEY phkResult + ); + + inline HANDLE(__stdcall *pRegisterEventSourceW)( + LPCWSTR lpUNCServerName, + LPCWSTR lpSourceName + ); + + inline BOOL(__stdcall *pDeregisterEventSource)( + HANDLE hEventLog + ); + + inline BOOL(__stdcall *pReportEventW)( + HANDLE hEventLog, + WORD wType, + WORD wCategory, + DWORD dwEventID, + PSID lpUserSid, + WORD wNumStrings, + DWORD dwDataSize, + LPCWSTR * lpStrings, + LPVOID lpRawData + ); + + inline BOOL(__stdcall *pCryptAcquireContextW)( + ULONG_PTR * hProv, + LPCWSTR szContainer, + LPCWSTR szProvider, + DWORD dwProvType, + DWORD dwFlags + ); + + inline BOOL(__stdcall *pCryptReleaseContext)( + ULONG_PTR hProvz, + DWORD dwFlags + ); + + inline DWORD(__stdcall *pSetEntriesInAclA)( + ULONG cCountOfExplicitEntries, + _EXPLICIT_ACCESS_A * pListOfExplicitEntries, + _ACL * OldAcl, + _ACL ** NewAcl + ); + + inline BOOL(__stdcall *pAllocateAndInitializeSid)( + PSID_IDENTIFIER_AUTHORITY pIdentifierAuthority, + BYTE nSubAuthorityCount, + DWORD nSubAuthority0, + DWORD nSubAuthority1, + DWORD nSubAuthority2, + DWORD nSubAuthority3, + DWORD nSubAuthority4, + DWORD nSubAuthority5, + DWORD nSubAuthority6, + DWORD nSubAuthority7, + VOID ** pSid + ); + + inline DWORD(__stdcall *pSetNamedSecurityInfoW)( + LPWSTR pObjectName, + _SE_OBJECT_TYPE ObjectType, + SECURITY_INFORMATION SecurityInfo, + VOID * psidOwner, + VOID * psidGroup, + _ACL * pDacl, + _ACL * pSacl + + ); + + inline PVOID(__stdcall *pFreeSid)( + VOID * pSid + ); + inline bool gUseNativeWaitMutex {}; inline bool gUseNativeWaitCondvar {}; inline bool gUseNativeWaitSemapahore {}; diff --git a/Source/Debug/ExceptionWatcher.Win32.cpp b/Source/Debug/ExceptionWatcher.Win32.cpp index 049128b0..c4e9ffac 100644 --- a/Source/Debug/ExceptionWatcher.Win32.cpp +++ b/Source/Debug/ExceptionWatcher.Win32.cpp @@ -546,11 +546,18 @@ namespace Aurora::Debug auto procNameWide = AuLocale::ConvertFromUTF8(procName); HKEY hKey; - if (::RegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\Windows Error Reporting\\ExcludedApplications", 0, KEY_WRITE, &hKey) == ERROR_SUCCESS) + if (pRegOpenKeyExW && + pRegOpenKeyExW(HKEY_CURRENT_USER, L"SOFTWARE\\Microsoft\\Windows\\Windows Error Reporting\\ExcludedApplications", 0, KEY_WRITE, &hKey) == ERROR_SUCCESS) { DWORD bioluminescenceReductionFactor { 1 }; - (void)::RegSetValueExW(hKey, procNameWide.c_str(), 0, REG_DWORD, (const BYTE *)&bioluminescenceReductionFactor, sizeof(DWORD)); - ::RegCloseKey(hKey); + if (pRegSetValueExW) + { + (void)pRegSetValueExW(hKey, procNameWide.c_str(), 0, REG_DWORD, (const BYTE *)&bioluminescenceReductionFactor, sizeof(DWORD)); + } + if (pRegCloseKey) + { + pRegCloseKey(hKey); + } } #endif diff --git a/Source/IO/FS/Resources.cpp b/Source/IO/FS/Resources.cpp index 410295b6..e3e32b34 100644 --- a/Source/IO/FS/Resources.cpp +++ b/Source/IO/FS/Resources.cpp @@ -490,22 +490,28 @@ namespace Aurora::IO::FS SID_IDENTIFIER_AUTHORITY SIDAuthWorld = SECURITY_WORLD_SID_AUTHORITY; const int NUM_ACES = 1; - EXPLICIT_ACCESS ea[NUM_ACES]; + EXPLICIT_ACCESS_A ea[NUM_ACES]; DWORD dwRes; - // Specify the DACL to use. - // Create a SID for the Everyone group. - if (!AllocateAndInitializeSid(&SIDAuthWorld, 1, - SECURITY_WORLD_RID, - 0, - 0, 0, 0, 0, 0, 0, - &pSIDEveryone)) + if (!pAllocateAndInitializeSid) { SysPushErrorFIO("AllocateAndInitializeSid (Everyone) error"); goto Cleanup; } - ZeroMemory(&ea, NUM_ACES * sizeof(EXPLICIT_ACCESS)); + // Specify the DACL to use. + // Create a SID for the Everyone group. + if (!pAllocateAndInitializeSid(&SIDAuthWorld, 1, + SECURITY_WORLD_RID, + 0, + 0, 0, 0, 0, 0, 0, + &pSIDEveryone)) + { + SysPushErrorFIO("AllocateAndInitializeSid (Everyone) error"); + goto Cleanup; + } + + ZeroMemory(&ea, NUM_ACES * sizeof(EXPLICIT_ACCESS_A)); // Set read access for Everyone. ea[0].grfAccessPermissions = GENERIC_ALL; @@ -515,17 +521,29 @@ namespace Aurora::IO::FS ea[0].Trustee.TrusteeType = TRUSTEE_IS_WELL_KNOWN_GROUP; ea[0].Trustee.ptstrName = (LPTSTR) pSIDEveryone; - if (ERROR_SUCCESS != SetEntriesInAcl(NUM_ACES, - ea, - NULL, - &pACL)) + if (!pSetEntriesInAclA) { SysPushErrorFIO("Failed SetEntriesInAcl"); goto Cleanup; } + if (ERROR_SUCCESS != pSetEntriesInAclA(NUM_ACES, + ea, + NULL, + &pACL)) + { + SysPushErrorFIO("Failed SetEntriesInAcl"); + goto Cleanup; + } + + if (!pSetNamedSecurityInfoW) + { + SysPushErrorFIO("Failed SetNamedSecurityInfoW"); + goto Cleanup; + } + // Try to modify the object's DACL. - dwRes = SetNamedSecurityInfoW( + dwRes = pSetNamedSecurityInfoW( Locale::ConvertFromUTF8(FS::NormalizePathRet(path)).data(), // name of the object SE_FILE_OBJECT, // type of object DACL_SECURITY_INFORMATION, // change only the object's DACL @@ -549,7 +567,10 @@ namespace Aurora::IO::FS if (pSIDEveryone) { - FreeSid(pSIDEveryone); + if (pFreeSid) + { + pFreeSid(pSIDEveryone); + } } if (pACL) @@ -559,7 +580,7 @@ namespace Aurora::IO::FS if (hToken) { - CloseHandle(hToken); + AuWin32CloseHandle(hToken); } if (!bRetval) diff --git a/Source/Logging/Sinks/EventLog.Win32.cpp b/Source/Logging/Sinks/EventLog.Win32.cpp index ae98132b..ae8801a2 100644 --- a/Source/Logging/Sinks/EventLog.Win32.cpp +++ b/Source/Logging/Sinks/EventLog.Win32.cpp @@ -14,12 +14,14 @@ namespace Aurora::Logging::Sinks EventLogSink::EventLogSink(const AuString &value) { - this->winCurrentSink_ = RegisterEventSourceW(NULL, Locale::ConvertFromUTF8(value).c_str()); + SysAssert(pRegisterEventSourceW); + this->winCurrentSink_ = pRegisterEventSourceW(NULL, Locale::ConvertFromUTF8(value).c_str()); } EventLogSink::~EventLogSink() { - DeregisterEventSource(winCurrentSink_); + SysAssert(pDeregisterEventSource); + pDeregisterEventSource(winCurrentSink_); } @@ -104,7 +106,12 @@ namespace Aurora::Logging::Sinks { auto &strs = AuGet<1>(sortedMessages); auto handle = strs.data(); - ReportEventW(this->winCurrentSink_, AuGet<0>(sortedMessages), 0, kMagicEventNumber, NULL, 1, 0, (CONST WCHAR **)&handle, NULL); + + if (!pReportEventW) + { + return; + } + pReportEventW(this->winCurrentSink_, AuGet<0>(sortedMessages), 0, kMagicEventNumber, NULL, 1, 0, (CONST WCHAR **)&handle, NULL); } this->winCompleteArrayBuffer_.clear(); @@ -112,6 +119,11 @@ namespace Aurora::Logging::Sinks IBasicSink *NewOSNamedEventDirectorySinkNew(const AuString &name) { + if (!pReportEventW) + { + return {}; + } + try { return _new EventLogSink(name); diff --git a/Source/SWInfo/AuSWInfo.NT.cpp b/Source/SWInfo/AuSWInfo.NT.cpp index 0d698388..35264ae6 100644 --- a/Source/SWInfo/AuSWInfo.NT.cpp +++ b/Source/SWInfo/AuSWInfo.NT.cpp @@ -45,7 +45,8 @@ namespace Aurora::SWInfo { DWORD dwBufferSize {}; - if (RegQueryValueExW(hKey, pWideKey, 0, NULL, NULL, &dwBufferSize) != ERROR_SUCCESS) + if (!pRegQueryValueExW || + pRegQueryValueExW(hKey, pWideKey, 0, NULL, NULL, &dwBufferSize) != ERROR_SUCCESS) { SysPushErrorUnavailableError("Couldn't access registery key"); return false; @@ -59,7 +60,8 @@ namespace Aurora::SWInfo return false; } - if (RegQueryValueExW(hKey, pWideKey, 0, NULL, reinterpret_cast(in.data()), &dwBufferSize) != ERROR_SUCCESS) + if (!pRegQueryValueExW || + pRegQueryValueExW(hKey, pWideKey, 0, NULL, reinterpret_cast(in.data()), &dwBufferSize) != ERROR_SUCCESS) { SysPushErrorUnavailableError("Couldn't access registery key"); return false; @@ -140,14 +142,21 @@ namespace Aurora::SWInfo #if defined(AURORA_PLATFORM_WIN32) HKEY hKey; - if (RegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &hKey) == ERROR_SUCCESS) + if (pRegOpenKeyExW && + pRegOpenKeyExW(HKEY_LOCAL_MACHINE, L"SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion", 0, KEY_READ, &hKey) == ERROR_SUCCESS) { + if (!Win32ReadRegistry(hKey, L"BuildLabEx", gBuildString)) { Win32ReadRegistry(hKey, L"BuildLab", gBuildString); } + Win32ReadRegistry(hKey, L"ProductName", gUserlandBrand); - RegCloseKey(hKey); + + if (pRegCloseKey) + { + pRegCloseKey(hKey); + } } #endif