|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <string> |
| 4 | +#include <system_error> |
| 5 | +#include <utility> |
| 6 | + |
| 7 | +#include <windows.h> |
| 8 | + |
| 9 | +/* |
| 10 | +
|
| 11 | +This class ensures a DLL is loaded from the system32, or its localized equivalent, folder |
| 12 | +to mitigate DLL hijacking attacks. |
| 13 | +
|
| 14 | +NOTES: |
| 15 | +- You do not need to use this class for 'known' DLLs listed in HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\KnownDLLs. |
| 16 | + The OS loader ensures all DLLs in that list, and any DLLs loaded by them, are only loaded from system32. |
| 17 | +- One should use this class to replace any statically linked DLLs listed in cmake's target_link_libraries declaration. |
| 18 | + TL;DR your cmake file should not have a target_link_libraries declaration. |
| 19 | +
|
| 20 | +*/ |
| 21 | + |
| 22 | +namespace wsl { |
| 23 | + |
| 24 | +class SystemLibLoader { |
| 25 | +public: |
| 26 | + SystemLibLoader(const SystemLibLoader&) = delete; |
| 27 | + SystemLibLoader& operator=(const SystemLibLoader&) = delete; |
| 28 | + |
| 29 | + SystemLibLoader(SystemLibLoader &&other) noexcept : handle_(other.handle_) |
| 30 | + { |
| 31 | + other.handle_ = nullptr; |
| 32 | + } |
| 33 | + |
| 34 | + SystemLibLoader& operator=(SystemLibLoader &&other) noexcept |
| 35 | + { |
| 36 | + if (this != &other) { |
| 37 | + std::swap(handle_, other.handle_); |
| 38 | + } |
| 39 | + return *this; |
| 40 | + } |
| 41 | + |
| 42 | + explicit SystemLibLoader(const char *libName) |
| 43 | + { |
| 44 | + if (!libName) { |
| 45 | + throw std::invalid_argument("Null parameter passed to SystemLibLoader"); |
| 46 | + } |
| 47 | + |
| 48 | + handle_ = ::LoadLibraryExA(libName, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32 | LOAD_LIBRARY_REQUIRE_SIGNED_TARGET); |
| 49 | + |
| 50 | + if (!handle_) { |
| 51 | + throw std::system_error(::GetLastError(), std::system_category(), std::string("SystemLibLoader failed to load DLL ") + libName); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + ~SystemLibLoader() |
| 56 | + { |
| 57 | + if (handle_) { |
| 58 | + ::FreeLibrary(handle_); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + static bool isAvailable(const char *libName) noexcept |
| 63 | + { |
| 64 | + if (!libName) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + |
| 68 | + HMODULE handle = ::LoadLibraryExA(libName, NULL, LOAD_LIBRARY_SEARCH_SYSTEM32 | LOAD_LIBRARY_REQUIRE_SIGNED_TARGET); |
| 69 | + if (handle) { |
| 70 | + ::FreeLibrary(handle); |
| 71 | + return true; |
| 72 | + } |
| 73 | + |
| 74 | + return false; |
| 75 | + } |
| 76 | + |
| 77 | + template<typename T> |
| 78 | + T *getFunction(const char *name) const |
| 79 | + { |
| 80 | + return reinterpret_cast<T *>(getProcAddress(name)); |
| 81 | + } |
| 82 | + |
| 83 | +private: |
| 84 | + HMODULE handle_ = nullptr; |
| 85 | + |
| 86 | + FARPROC getProcAddress(const char *name) const |
| 87 | + { |
| 88 | + if (!name) { |
| 89 | + throw std::invalid_argument("Null parameter passed to getProcAddress"); |
| 90 | + } |
| 91 | + |
| 92 | + if (!handle_) { |
| 93 | + throw std::logic_error("The DLL has not been loaded"); |
| 94 | + } |
| 95 | + |
| 96 | + auto symbol = ::GetProcAddress(handle_, name); |
| 97 | + |
| 98 | + if (symbol == nullptr) { |
| 99 | + throw std::system_error(::GetLastError(), std::system_category(), std::string("SystemLibLoader failed to load function ") + name); |
| 100 | + } |
| 101 | + |
| 102 | + return symbol; |
| 103 | + } |
| 104 | +}; |
| 105 | + |
| 106 | +} |
0 commit comments