blob: a80d2803bd4f24a61eba0ba7122d847ccdf3ff3b [file] [log] [blame]
[email protected]84479322011-04-18 22:06:221// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]f38e25f2009-04-21 00:56:072// 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]e3177dd52014-08-13 20:22:149#include "base/files/file_util.h"
chengx5946c922017-03-16 05:49:2110#include "base/metrics/histogram_macros.h"
Cliff Smolinskyf395bef2019-04-12 23:45:4411#include "base/path_service.h"
12#include "base/scoped_native_library.h"
thestig02c965b2016-06-14 18:52:2313#include "base/strings/string_util.h"
[email protected]f4e911452014-03-20 06:07:2614#include "base/strings/stringprintf.h"
[email protected]a4ea1f12013-06-07 18:37:0715#include "base/strings/utf_string_conversions.h"
Etienne Pierre-Doray3879b052018-09-17 14:17:2216#include "base/threading/scoped_blocking_call.h"
[email protected]f38e25f2009-04-21 00:56:0717
18namespace base {
19
[email protected]0f998442014-03-25 01:59:0920namespace {
Cliff Smolinskyf395bef2019-04-12 23:45:4421
22// forward declare
23HMODULE AddDllDirectory(PCWSTR new_directory);
24
chengx5946c922017-03-16 05:49:2125// This enum is used to back an UMA histogram, and should therefore be treated
26// as append-only.
27enum 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.
47void 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.
54bool 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 Smolinskyf395bef2019-04-12 23:45:4463 static const auto add_dll_dir_func =
64 reinterpret_cast<decltype(AddDllDirectory)*>(
65 GetProcAddress(GetModuleHandle(L"kernel32.dll"), "AddDllDirectory"));
chengx5946c922017-03-16 05:49:2166 return !!add_dll_dir_func;
67}
68
69// A helper method to encode the library loading result to enum
70// LoadLibraryResult.
71LoadLibraryResult 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]0f998442014-03-25 01:59:0986
[email protected]3e246222010-11-19 23:33:1387NativeLibrary LoadNativeLibraryHelper(const FilePath& library_path,
[email protected]0f998442014-03-25 01:59:0988 NativeLibraryLoadError* error) {
Cliff Smolinskyf395bef2019-04-12 23:45:4489 // LoadLibrary() opens the file off disk and acquires the LoaderLock, hence
90 // must not be called from DllMain.
Etienne Bergeron436d42212019-02-26 17:15:1291 ScopedBlockingCall scoped_blocking_call(FROM_HERE, BlockingType::MAY_BLOCK);
[email protected]be130682010-11-12 21:53:1692
chengx5946c922017-03-16 05:49:2193 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örrieb630aca2019-12-04 10:59:11104 library_path.value().c_str(), nullptr,
chengx5946c922017-03-16 05:49:21105 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 Smolinskyc5c52102019-05-03 20:51:54114 error->code = ::GetLastError();
chengx5946c922017-03-16 05:49:21115 }
116
117 // If LoadLibraryExW API/flags are unavailable or API call fails, try
Xi Cheng2740c2c2018-11-20 22:25:22118 // LoadLibraryW API. From UMA, this fallback is necessary for many users.
chengx5946c922017-03-16 05:49:21119
[email protected]f38e25f2009-04-21 00:56:07120 // 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]188505282009-09-16 16:31:28123 FilePath current_directory;
[email protected]37b3c1992014-03-11 20:59:02124 if (GetCurrentDirectory(&current_directory)) {
[email protected]f38e25f2009-04-21 00:56:07125 FilePath plugin_path = library_path.DirName();
[email protected]188505282009-09-16 16:31:28126 if (!plugin_path.empty()) {
[email protected]37b3c1992014-03-11 20:59:02127 SetCurrentDirectory(plugin_path);
[email protected]f38e25f2009-04-21 00:56:07128 restore_directory = true;
129 }
130 }
Jan Wilken Dörrieb630aca2019-12-04 10:59:11131 module = ::LoadLibraryW(library_path.value().c_str());
chengx5946c922017-03-16 05:49:21132
133 // GetLastError() needs to be called immediately after LoadLibraryW call.
134 if (!module && error)
Cliff Smolinskyc5c52102019-05-03 20:51:54135 error->code = ::GetLastError();
[email protected]f4e911452014-03-20 06:07:26136
[email protected]f38e25f2009-04-21 00:56:07137 if (restore_directory)
[email protected]37b3c1992014-03-11 20:59:02138 SetCurrentDirectory(current_directory);
[email protected]f38e25f2009-04-21 00:56:07139
chengx5946c922017-03-16 05:49:21140 // Get the library loading result and log it to UMA.
141 LogLibrarayLoadResultToUMA(
142 GetLoadLibraryResult(are_search_flags_available, !!module));
143
[email protected]f38e25f2009-04-21 00:56:07144 return module;
145}
Cliff Smolinskyf395bef2019-04-12 23:45:44146
147NativeLibrary LoadSystemLibraryHelper(const FilePath& library_path,
148 NativeLibraryLoadError* error) {
Cliff Smolinskyc5c52102019-05-03 20:51:54149 // 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 Smolinskyf395bef2019-04-12 23:45:44152 NativeLibrary module;
153 BOOL module_found =
Jan Wilken Dörrieb630aca2019-12-04 10:59:11154 ::GetModuleHandleExW(0, library_path.value().c_str(), &module);
Cliff Smolinskyf395bef2019-04-12 23:45:44155 if (!module_found) {
Cliff Smolinskyf395bef2019-04-12 23:45:44156 bool are_search_flags_available = AreSearchFlagsAvailable();
Cliff Smolinskyc5c52102019-05-03 20:51:54157 // Prefer LOAD_LIBRARY_SEARCH_SYSTEM32 to avoid DLL preloading attacks.
Cliff Smolinskyf395bef2019-04-12 23:45:44158 DWORD flags = are_search_flags_available ? LOAD_LIBRARY_SEARCH_SYSTEM32
159 : LOAD_WITH_ALTERED_SEARCH_PATH;
Jan Wilken Dörrieb630aca2019-12-04 10:59:11160 module = ::LoadLibraryExW(library_path.value().c_str(), nullptr, flags);
Cliff Smolinskyf395bef2019-04-12 23:45:44161
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 Zhang4c836692019-09-27 02:14:55172FilePath GetSystemLibraryName(FilePath::StringPieceType name) {
Cliff Smolinskyf395bef2019-04-12 23:45:44173 FilePath library_path;
174 // Use an absolute path to load the DLL to avoid DLL preloading attacks.
Lei Zhang4c836692019-09-27 02:14:55175 if (PathService::Get(DIR_SYSTEM, &library_path))
176 library_path = library_path.Append(name);
177 return library_path;
Cliff Smolinskyf395bef2019-04-12 23:45:44178}
179
[email protected]0f998442014-03-25 01:59:09180} // namespace
181
182std::string NativeLibraryLoadError::ToString() const {
Bruce Dawson19175842017-08-02 17:00:45183 return StringPrintf("%lu", code);
[email protected]0f998442014-03-25 01:59:09184}
185
rockot596a0dd2016-08-26 00:57:51186NativeLibrary LoadNativeLibraryWithOptions(const FilePath& library_path,
187 const NativeLibraryOptions& options,
188 NativeLibraryLoadError* error) {
chengx5946c922017-03-16 05:49:21189 return LoadNativeLibraryHelper(library_path, error);
[email protected]3e246222010-11-19 23:33:13190}
191
[email protected]f38e25f2009-04-21 00:56:07192void UnloadNativeLibrary(NativeLibrary library) {
193 FreeLibrary(library);
194}
195
[email protected]f38e25f2009-04-21 00:56:07196void* GetFunctionPointerFromNativeLibrary(NativeLibrary library,
thestige38fbd62016-06-10 21:54:40197 StringPiece name) {
Nico Weberb6499668a2018-03-09 12:55:03198 return reinterpret_cast<void*>(GetProcAddress(library, name.data()));
[email protected]f38e25f2009-04-21 00:56:07199}
200
thestig02c965b2016-06-14 18:52:23201std::string GetNativeLibraryName(StringPiece name) {
202 DCHECK(IsStringASCII(name));
203 return name.as_string() + ".dll";
[email protected]108c2a12009-06-05 22:18:09204}
205
Xiaohan Wangd807ec32018-04-03 01:31:44206std::string GetLoadableModuleName(StringPiece name) {
207 return GetNativeLibraryName(name);
208}
209
Cliff Smolinskyf395bef2019-04-12 23:45:44210NativeLibrary LoadSystemLibrary(FilePath::StringPieceType name,
211 NativeLibraryLoadError* error) {
Lei Zhang4c836692019-09-27 02:14:55212 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 Smolinskyf395bef2019-04-12 23:45:44219}
220
Cliff Smolinskyc5c52102019-05-03 20:51:54221NativeLibrary PinSystemLibrary(FilePath::StringPieceType name,
222 NativeLibraryLoadError* error) {
Lei Zhang4c836692019-09-27 02:14:55223 FilePath library_path = GetSystemLibraryName(name);
224 if (library_path.empty()) {
Cliff Smolinskyc5c52102019-05-03 20:51:54225 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 Zhang4c836692019-09-27 02:14:55234 if (::GetModuleHandleExW(GET_MODULE_HANDLE_EX_FLAG_PIN,
Jan Wilken Dörrieb630aca2019-12-04 10:59:11235 library_path.value().c_str(),
Lei Zhang4c836692019-09-27 02:14:55236 ScopedNativeLibrary::Receiver(module).get())) {
237 return module.release();
Cliff Smolinskyc5c52102019-05-03 20:51:54238 }
Lei Zhang4c836692019-09-27 02:14:55239
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örrieb630aca2019-12-04 10:59:11247 library_path.value().c_str(),
Lei Zhang4c836692019-09-27 02:14:55248 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 Smolinskyc5c52102019-05-03 20:51:54256}
257
[email protected]f38e25f2009-04-21 00:56:07258} // namespace base