[email protected] | 8447932 | 2011-04-18 22:06:22 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
| 5 | #include "base/native_library.h" |
| 6 | |
| 7 | #include <windows.h> |
| 8 | |
[email protected] | e3177dd5 | 2014-08-13 20:22:14 | [diff] [blame] | 9 | #include "base/files/file_util.h" |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 10 | #include "base/metrics/histogram_macros.h" |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 11 | #include "base/path_service.h" |
| 12 | #include "base/scoped_native_library.h" |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 13 | #include "base/strings/string_util.h" |
[email protected] | f4e91145 | 2014-03-20 06:07:26 | [diff] [blame] | 14 | #include "base/strings/stringprintf.h" |
[email protected] | a4ea1f1 | 2013-06-07 18:37:07 | [diff] [blame] | 15 | #include "base/strings/utf_string_conversions.h" |
Etienne Pierre-Doray | 3879b05 | 2018-09-17 14:17:22 | [diff] [blame] | 16 | #include "base/threading/scoped_blocking_call.h" |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 17 | |
| 18 | namespace base { |
| 19 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 20 | namespace { |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 21 | |
| 22 | // forward declare |
| 23 | HMODULE AddDllDirectory(PCWSTR new_directory); |
| 24 | |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 25 | // This enum is used to back an UMA histogram, and should therefore be treated |
| 26 | // as append-only. |
| 27 | enum LoadLibraryResult { |
| 28 | // LoadLibraryExW API/flags are available and the call succeeds. |
| 29 | SUCCEED = 0, |
| 30 | // LoadLibraryExW API/flags are availabe to use but the call fails, then |
| 31 | // LoadLibraryW is used and succeeds. |
| 32 | FAIL_AND_SUCCEED, |
| 33 | // LoadLibraryExW API/flags are availabe to use but the call fails, then |
| 34 | // LoadLibraryW is used but fails as well. |
| 35 | FAIL_AND_FAIL, |
| 36 | // LoadLibraryExW API/flags are unavailabe to use, then LoadLibraryW is used |
| 37 | // and succeeds. |
| 38 | UNAVAILABLE_AND_SUCCEED, |
| 39 | // LoadLibraryExW API/flags are unavailabe to use, then LoadLibraryW is used |
| 40 | // but fails. |
| 41 | UNAVAILABLE_AND_FAIL, |
| 42 | // Add new items before this one, always keep this one at the end. |
| 43 | END |
| 44 | }; |
| 45 | |
| 46 | // A helper method to log library loading result to UMA. |
| 47 | void LogLibrarayLoadResultToUMA(LoadLibraryResult result) { |
| 48 | UMA_HISTOGRAM_ENUMERATION("LibraryLoader.LoadNativeLibraryWindows", result, |
| 49 | LoadLibraryResult::END); |
| 50 | } |
| 51 | |
| 52 | // A helper method to check if AddDllDirectory method is available, thus |
| 53 | // LOAD_LIBRARY_SEARCH_* flags are available on systems. |
| 54 | bool AreSearchFlagsAvailable() { |
| 55 | // The LOAD_LIBRARY_SEARCH_* flags are available on systems that have |
| 56 | // KB2533623 installed. To determine whether the flags are available, use |
| 57 | // GetProcAddress to get the address of the AddDllDirectory, |
| 58 | // RemoveDllDirectory, or SetDefaultDllDirectories function. If GetProcAddress |
| 59 | // succeeds, the LOAD_LIBRARY_SEARCH_* flags can be used with LoadLibraryEx. |
| 60 | // https://msdn.microsoft.com/en-us/library/windows/desktop/ms684179(v=vs.85).aspx |
| 61 | // The LOAD_LIBRARY_SEARCH_* flags are used in the LoadNativeLibraryHelper |
| 62 | // method. |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 63 | static const auto add_dll_dir_func = |
| 64 | reinterpret_cast<decltype(AddDllDirectory)*>( |
| 65 | GetProcAddress(GetModuleHandle(L"kernel32.dll"), "AddDllDirectory")); |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 66 | return !!add_dll_dir_func; |
| 67 | } |
| 68 | |
| 69 | // A helper method to encode the library loading result to enum |
| 70 | // LoadLibraryResult. |
| 71 | LoadLibraryResult GetLoadLibraryResult(bool are_search_flags_available, |
| 72 | bool has_load_library_succeeded) { |
| 73 | LoadLibraryResult result; |
| 74 | if (are_search_flags_available) { |
| 75 | if (has_load_library_succeeded) |
| 76 | result = LoadLibraryResult::FAIL_AND_SUCCEED; |
| 77 | else |
| 78 | result = LoadLibraryResult::FAIL_AND_FAIL; |
| 79 | } else if (has_load_library_succeeded) { |
| 80 | result = LoadLibraryResult::UNAVAILABLE_AND_SUCCEED; |
| 81 | } else { |
| 82 | result = LoadLibraryResult::UNAVAILABLE_AND_FAIL; |
| 83 | } |
| 84 | return result; |
| 85 | } |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 86 | |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 87 | NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path, |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 88 | NativeLibraryLoadError* error) { |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 89 | // LoadLibrary() opens the file off disk and acquires the LoaderLock, hence |
| 90 | // must not be called from DllMain. |
Etienne Bergeron | 436d4221 | 2019-02-26 17:15:12 | [diff] [blame] | 91 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
[email protected] | be13068 | 2010-11-12 21:53:16 | [diff] [blame] | 92 | |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 93 | HMODULE module = nullptr; |
| 94 | |
| 95 | // This variable records the library loading result. |
| 96 | LoadLibraryResult load_library_result = LoadLibraryResult::SUCCEED; |
| 97 | |
| 98 | bool are_search_flags_available = AreSearchFlagsAvailable(); |
| 99 | if (are_search_flags_available) { |
| 100 | // LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR flag is needed to search the library |
| 101 | // directory as the library may have dependencies on DLLs in this |
| 102 | // directory. |
| 103 | module = ::LoadLibraryExW( |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 104 | library_path.value().c_str(), nullptr, |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 105 | LOAD_LIBRARY_SEARCH_DLL_LOAD_DIR | LOAD_LIBRARY_SEARCH_DEFAULT_DIRS); |
| 106 | // If LoadLibraryExW succeeds, log this metric and return. |
| 107 | if (module) { |
| 108 | LogLibrarayLoadResultToUMA(load_library_result); |
| 109 | return module; |
| 110 | } |
| 111 | // GetLastError() needs to be called immediately after |
| 112 | // LoadLibraryExW call. |
| 113 | if (error) |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 114 | error->code = ::GetLastError(); |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | // If LoadLibraryExW API/flags are unavailable or API call fails, try |
Xi Cheng | 2740c2c | 2018-11-20 22:25:22 | [diff] [blame] | 118 | // LoadLibraryW API. From UMA, this fallback is necessary for many users. |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 119 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 120 | // Switch the current directory to the library directory as the library |
| 121 | // may have dependencies on DLLs in this directory. |
| 122 | bool restore_directory = false; |
[email protected] | 18850528 | 2009-09-16 16:31:28 | [diff] [blame] | 123 | FilePath current_directory; |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 124 | if (GetCurrentDirectory(¤t_directory)) { |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 125 | FilePath plugin_path = library_path.DirName(); |
[email protected] | 18850528 | 2009-09-16 16:31:28 | [diff] [blame] | 126 | if (!plugin_path.empty()) { |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 127 | SetCurrentDirectory(plugin_path); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 128 | restore_directory = true; |
| 129 | } |
| 130 | } |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 131 | module = ::LoadLibraryW(library_path.value().c_str()); |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 132 | |
| 133 | // GetLastError() needs to be called immediately after LoadLibraryW call. |
| 134 | if (!module && error) |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 135 | error->code = ::GetLastError(); |
[email protected] | f4e91145 | 2014-03-20 06:07:26 | [diff] [blame] | 136 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 137 | if (restore_directory) |
[email protected] | 37b3c199 | 2014-03-11 20:59:02 | [diff] [blame] | 138 | SetCurrentDirectory(current_directory); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 139 | |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 140 | // Get the library loading result and log it to UMA. |
| 141 | LogLibrarayLoadResultToUMA( |
| 142 | GetLoadLibraryResult(are_search_flags_available, !!module)); |
| 143 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 144 | return module; |
| 145 | } |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 146 | |
| 147 | NativeLibrary LoadSystemLibraryHelper(const FilePath& library_path, |
| 148 | NativeLibraryLoadError* error) { |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 149 | // GetModuleHandleEx and subsequently LoadLibraryEx acquire the LoaderLock, |
| 150 | // hence must not be called from Dllmain. |
| 151 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 152 | NativeLibrary module; |
| 153 | BOOL module_found = |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 154 | ::GetModuleHandleExW(0, library_path.value().c_str(), &module); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 155 | if (!module_found) { |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 156 | bool are_search_flags_available = AreSearchFlagsAvailable(); |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 157 | // Prefer LOAD_LIBRARY_SEARCH_SYSTEM32 to avoid DLL preloading attacks. |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 158 | DWORD flags = are_search_flags_available ? LOAD_LIBRARY_SEARCH_SYSTEM32 |
| 159 | : LOAD_WITH_ALTERED_SEARCH_PATH; |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 160 | module = ::LoadLibraryExW(library_path.value().c_str(), nullptr, flags); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 161 | |
| 162 | if (!module && error) |
| 163 | error->code = ::GetLastError(); |
| 164 | |
| 165 | LogLibrarayLoadResultToUMA( |
| 166 | GetLoadLibraryResult(are_search_flags_available, !!module)); |
| 167 | } |
| 168 | |
| 169 | return module; |
| 170 | } |
| 171 | |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 172 | FilePath GetSystemLibraryName(FilePath::StringPieceType name) { |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 173 | FilePath library_path; |
| 174 | // Use an absolute path to load the DLL to avoid DLL preloading attacks. |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 175 | if (PathService::Get(DIR_SYSTEM, &library_path)) |
| 176 | library_path = library_path.Append(name); |
| 177 | return library_path; |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 178 | } |
| 179 | |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 180 | } // namespace |
| 181 | |
| 182 | std::string NativeLibraryLoadError::ToString() const { |
Bruce Dawson | 1917584 | 2017-08-02 17:00:45 | [diff] [blame] | 183 | return StringPrintf("%lu", code); |
[email protected] | 0f99844 | 2014-03-25 01:59:09 | [diff] [blame] | 184 | } |
| 185 | |
rockot | 596a0dd | 2016-08-26 00:57:51 | [diff] [blame] | 186 | NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path, |
| 187 | const NativeLibraryOptions& options, |
| 188 | NativeLibraryLoadError* error) { |
chengx | 5946c92 | 2017-03-16 05:49:21 | [diff] [blame] | 189 | return LoadNativeLibraryHelper(library_path, error); |
[email protected] | 3e24622 | 2010-11-19 23:33:13 | [diff] [blame] | 190 | } |
| 191 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 192 | void UnloadNativeLibrary(NativeLibrary library) { |
| 193 | FreeLibrary(library); |
| 194 | } |
| 195 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 196 | void* GetFunctionPointerFromNativeLibrary(NativeLibrary library, |
thestig | e38fbd6 | 2016-06-10 21:54:40 | [diff] [blame] | 197 | StringPiece name) { |
Nico Weber | b6499668a | 2018-03-09 12:55:03 | [diff] [blame] | 198 | return reinterpret_cast<void*>(GetProcAddress(library, name.data())); |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 199 | } |
| 200 | |
thestig | 02c965b | 2016-06-14 18:52:23 | [diff] [blame] | 201 | std::string GetNativeLibraryName(StringPiece name) { |
| 202 | DCHECK(IsStringASCII(name)); |
| 203 | return name.as_string() + ".dll"; |
[email protected] | 108c2a1 | 2009-06-05 22:18:09 | [diff] [blame] | 204 | } |
| 205 | |
Xiaohan Wang | d807ec3 | 2018-04-03 01:31:44 | [diff] [blame] | 206 | std::string GetLoadableModuleName(StringPiece name) { |
| 207 | return GetNativeLibraryName(name); |
| 208 | } |
| 209 | |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 210 | NativeLibrary LoadSystemLibrary(FilePath::StringPieceType name, |
| 211 | NativeLibraryLoadError* error) { |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 212 | FilePath library_path = GetSystemLibraryName(name); |
| 213 | if (library_path.empty()) { |
| 214 | if (error) |
| 215 | error->code = ERROR_NOT_FOUND; |
| 216 | return nullptr; |
| 217 | } |
| 218 | return LoadSystemLibraryHelper(library_path, error); |
Cliff Smolinsky | f395bef | 2019-04-12 23:45:44 | [diff] [blame] | 219 | } |
| 220 | |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 221 | NativeLibrary PinSystemLibrary(FilePath::StringPieceType name, |
| 222 | NativeLibraryLoadError* error) { |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 223 | FilePath library_path = GetSystemLibraryName(name); |
| 224 | if (library_path.empty()) { |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 225 | if (error) |
| 226 | error->code = ERROR_NOT_FOUND; |
| 227 | return nullptr; |
| 228 | } |
| 229 | |
| 230 | // GetModuleHandleEx acquires the LoaderLock, hence must not be called from |
| 231 | // Dllmain. |
| 232 | ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK); |
| 233 | ScopedNativeLibrary module; |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 234 | if (::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 235 | library_path.value().c_str(), |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 236 | ScopedNativeLibrary::Receiver(module).get())) { |
| 237 | return module.release(); |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 238 | } |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 239 | |
| 240 | // Load and pin the library since it wasn't already loaded. |
| 241 | module = ScopedNativeLibrary(LoadSystemLibraryHelper(library_path, error)); |
| 242 | if (!module.is_valid()) |
| 243 | return nullptr; |
| 244 | |
| 245 | ScopedNativeLibrary temp; |
| 246 | if (::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN, |
Jan Wilken Dörrie | b630aca | 2019-12-04 10:59:11 | [diff] [blame] | 247 | library_path.value().c_str(), |
Lei Zhang | 4c83669 | 2019-09-27 02:14:55 | [diff] [blame] | 248 | ScopedNativeLibrary::Receiver(temp).get())) { |
| 249 | return module.release(); |
| 250 | } |
| 251 | |
| 252 | if (error) |
| 253 | error->code = ::GetLastError(); |
| 254 | // Return nullptr since we failed to pin the module. |
| 255 | return nullptr; |
Cliff Smolinsky | c5c5210 | 2019-05-03 20:51:54 | [diff] [blame] | 256 | } |
| 257 | |
[email protected] | f38e25f | 2009-04-21 00:56:07 | [diff] [blame] | 258 | } // namespace base |