Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(664)

Side by Side Diff: chrome/app/close_handle_hook_win.cc

Issue 587923002: Extend the CloseHandle interception to all DLLs loaded in the process. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Review comments Created 6 years, 2 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « base/win/iat_patch_function.cc ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2014 The Chromium Authors. All rights reserved. 1 // Copyright 2014 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/app/close_handle_hook_win.h" 5 #include "chrome/app/close_handle_hook_win.h"
6 6
7 #include <Windows.h> 7 #include <Windows.h>
8 #include <psapi.h>
8 9
9 #include <vector> 10 #include <vector>
10 11
11 #include "base/files/file_path.h"
12 #include "base/lazy_instance.h" 12 #include "base/lazy_instance.h"
13 #include "base/strings/string16.h"
14 #include "base/win/iat_patch_function.h" 13 #include "base/win/iat_patch_function.h"
14 #include "base/win/pe_image.h"
15 #include "base/win/scoped_handle.h" 15 #include "base/win/scoped_handle.h"
16 #include "chrome/common/chrome_version_info.h" 16 #include "chrome/common/chrome_version_info.h"
17 17
18 namespace { 18 namespace {
19 19
20 typedef BOOL (WINAPI* CloseHandleType) (HANDLE handle); 20 typedef BOOL (WINAPI* CloseHandleType) (HANDLE handle);
21 CloseHandleType g_close_function = NULL; 21 CloseHandleType g_close_function = NULL;
22 22
23 // The entry point for CloseHandle interception. This function notifies the 23 // The entry point for CloseHandle interception. This function notifies the
24 // verifier about the handle that is being closed, and calls the original 24 // verifier about the handle that is being closed, and calls the original
25 // function. 25 // function.
26 BOOL WINAPI CloseHandleHook(HANDLE handle) { 26 BOOL WINAPI CloseHandleHook(HANDLE handle) {
27 base::win::OnHandleBeingClosed(handle); 27 base::win::OnHandleBeingClosed(handle);
28 return g_close_function(handle); 28 return g_close_function(handle);
29 } 29 }
30 30
31 // Provides a simple way to temporarily change the protection of a memory page.
32 class AutoProtectMemory {
33 public:
34 AutoProtectMemory()
35 : changed_(false), address_(NULL), bytes_(0), old_protect_(0) {}
36
37 ~AutoProtectMemory() {
38 RevertProtection();
39 }
40
41 // Grants write access to a given memory range.
42 bool ChangeProtection(void* address, size_t bytes);
43
44 // Restores the original page protection.
45 void RevertProtection();
46
47 private:
48 bool changed_;
49 void* address_;
50 size_t bytes_;
51 DWORD old_protect_;
52
53 DISALLOW_COPY_AND_ASSIGN(AutoProtectMemory);
54 };
55
56 bool AutoProtectMemory::ChangeProtection(void* address, size_t bytes) {
57 DCHECK(!changed_);
58 DCHECK(address);
59
60 // Change the page protection so that we can write.
61 MEMORY_BASIC_INFORMATION memory_info;
62 if (!VirtualQuery(address, &memory_info, sizeof(memory_info)))
63 return false;
64
65 DWORD is_executable = (PAGE_EXECUTE | PAGE_EXECUTE_READ |
66 PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY) &
67 memory_info.Protect;
68
69 DWORD protect = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE;
70 if (!VirtualProtect(address, bytes, protect, &old_protect_))
71 return false;
72
73 changed_ = true;
74 address_ = address;
75 bytes_ = bytes;
76 return true;
77 }
78
79 void AutoProtectMemory::RevertProtection() {
80 if (!changed_)
81 return;
82
83 DCHECK(address_);
84 DCHECK(bytes_);
85
86 VirtualProtect(address_, bytes_, old_protect_, &old_protect_);
87 changed_ = false;
88 address_ = NULL;
89 bytes_ = 0;
90 old_protect_ = 0;
91 }
92
93 // Performs an EAT interception.
94 void EATPatch(HMODULE module, const char* function_name,
95 void* new_function, void** old_function) {
96 if (!module)
97 return;
98
99 base::win::PEImage pe(module);
100 if (!pe.VerifyMagic())
101 return;
102
103 DWORD* eat_entry = pe.GetExportEntry(function_name);
104 if (!eat_entry)
105 return;
106
107 if (!(*old_function))
108 *old_function = pe.RVAToAddr(*eat_entry);
109
110 AutoProtectMemory memory;
111 if (!memory.ChangeProtection(eat_entry, sizeof(DWORD)))
112 return;
113
114 // Perform the patch.
115 #pragma warning(push)
116 #pragma warning(disable: 4311)
117 // These casts generate warnings because they are 32 bit specific.
118 *eat_entry = reinterpret_cast<DWORD>(new_function) -
119 reinterpret_cast<DWORD>(module);
120 #pragma warning(pop)
121 }
122
31 // Keeps track of all the hooks needed to intercept CloseHandle. 123 // Keeps track of all the hooks needed to intercept CloseHandle.
32 class CloseHandleHooks { 124 class CloseHandleHooks {
33 public: 125 public:
34 CloseHandleHooks() {} 126 CloseHandleHooks() {}
35 ~CloseHandleHooks() {} 127 ~CloseHandleHooks() {}
36 128
37 void AddIATPatch(const base::string16& module); 129 void AddIATPatch(HMODULE module);
130 void AddEATPatch();
38 void Unpatch(); 131 void Unpatch();
39 132
40 private: 133 private:
41 std::vector<base::win::IATPatchFunction*> hooks_; 134 std::vector<base::win::IATPatchFunction*> hooks_;
42 DISALLOW_COPY_AND_ASSIGN(CloseHandleHooks); 135 DISALLOW_COPY_AND_ASSIGN(CloseHandleHooks);
43 }; 136 };
44 base::LazyInstance<CloseHandleHooks> g_hooks = LAZY_INSTANCE_INITIALIZER; 137 base::LazyInstance<CloseHandleHooks> g_hooks = LAZY_INSTANCE_INITIALIZER;
45 138
46 void CloseHandleHooks::AddIATPatch(const base::string16& module) { 139 void CloseHandleHooks::AddIATPatch(HMODULE module) {
47 if (module.empty()) 140 if (!module)
48 return; 141 return;
49 142
50 base::win::IATPatchFunction* patch = new base::win::IATPatchFunction; 143 base::win::IATPatchFunction* patch = new base::win::IATPatchFunction;
51 patch->Patch(module.c_str(), "kernel32.dll", "CloseHandle", CloseHandleHook); 144 if (patch->PatchFromModule(module, "kernel32.dll", "CloseHandle",
145 CloseHandleHook)) {
146 delete patch;
147 return;
148 }
149
52 hooks_.push_back(patch); 150 hooks_.push_back(patch);
53 if (!g_close_function) { 151 if (!g_close_function) {
54 // Things are probably messed up if each intercepted function points to 152 // Things are probably messed up if each intercepted function points to
55 // a different place, but we need only one function to call. 153 // a different place, but we need only one function to call.
56 g_close_function = 154 g_close_function =
57 reinterpret_cast<CloseHandleType>(patch->original_function()); 155 reinterpret_cast<CloseHandleType>(patch->original_function());
58 } 156 }
59 } 157 }
60 158
159 void CloseHandleHooks::AddEATPatch() {
160 // An attempt to restore the entry on the table at destruction is not safe.
161 EATPatch(GetModuleHandleA("kernel32.dll"), "CloseHandle",
162 &CloseHandleHook, reinterpret_cast<void**>(&g_close_function));
163 }
164
61 void CloseHandleHooks::Unpatch() { 165 void CloseHandleHooks::Unpatch() {
62 for (std::vector<base::win::IATPatchFunction*>::iterator it = hooks_.begin(); 166 for (std::vector<base::win::IATPatchFunction*>::iterator it = hooks_.begin();
63 it != hooks_.end(); ++it) { 167 it != hooks_.end(); ++it) {
64 (*it)->Unpatch(); 168 (*it)->Unpatch();
169 delete *it;
65 } 170 }
66 } 171 }
67 172
68 bool UseHooks() { 173 bool UseHooks() {
174 #if defined(ARCH_CPU_X86_64)
175 return false;
176 #elif defined(NDEBUG)
69 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); 177 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel();
70 if (channel == chrome::VersionInfo::CHANNEL_CANARY || 178 if (channel == chrome::VersionInfo::CHANNEL_CANARY ||
71 channel == chrome::VersionInfo::CHANNEL_DEV) { 179 channel == chrome::VersionInfo::CHANNEL_DEV) {
72 return true; 180 return true;
73 } 181 }
74 182
75 return false; 183 return false;
184 #else // NDEBUG
185 return true;
186 #endif
76 } 187 }
77 188
78 base::string16 GetModuleName(HMODULE module) { 189 void PatchLoadedModules(CloseHandleHooks* hooks) {
79 base::string16 name; 190 const DWORD kSize = 256;
80 if (!module) 191 DWORD returned;
81 return name; 192 scoped_ptr<HMODULE[]> modules(new HMODULE[kSize]);
82 wchar_t buffer[MAX_PATH]; 193 if (!EnumProcessModules(GetCurrentProcess(), modules.get(),
83 int rv = GetModuleFileName(module, buffer, MAX_PATH); 194 kSize * sizeof(HMODULE), &returned)) {
84 if (rv == MAX_PATH) 195 return;
85 return name; 196 }
197 returned /= sizeof(HMODULE);
198 returned = std::min(kSize, returned);
86 199
87 buffer[MAX_PATH - 1] = L'\0'; 200 for (DWORD current = 0; current < returned; current++) {
88 name.assign(buffer); 201 hooks->AddIATPatch(modules[current]);
89 base::FilePath path(name);
90 return path.BaseName().AsUTF16Unsafe();
91 }
92
93 HMODULE GetChromeDLLModule() {
94 HMODULE module;
95 if (!GetModuleHandleEx(GET_MODULE_HANDLE_EX_FLAG_FROM_ADDRESS |
96 GET_MODULE_HANDLE_EX_FLAG_UNCHANGED_REFCOUNT,
97 reinterpret_cast<wchar_t*>(&GetChromeDLLModule),
98 &module)) {
99 return NULL;
100 } 202 }
101 return module;
102 } 203 }
103 204
104 } // namespace 205 } // namespace
105 206
106 void InstallCloseHandleHooks() { 207 void InstallCloseHandleHooks() {
107 if (UseHooks()) { 208 if (UseHooks()) {
108 CloseHandleHooks* hooks = g_hooks.Pointer(); 209 CloseHandleHooks* hooks = g_hooks.Pointer();
109 hooks->AddIATPatch(L"chrome.exe"); 210
110 hooks->AddIATPatch(GetModuleName(GetChromeDLLModule())); 211 // Performing EAT interception first is safer in the presence of other
212 // threads attempting to call CloseHandle.
213 hooks->AddEATPatch();
214 PatchLoadedModules(hooks);
111 } else { 215 } else {
112 base::win::DisableHandleVerifier(); 216 base::win::DisableHandleVerifier();
113 } 217 }
114 } 218 }
115 219
116 void RemoveCloseHandleHooks() { 220 void RemoveCloseHandleHooks() {
117 g_hooks.Get().Unpatch(); 221 g_hooks.Get().Unpatch();
118 } 222 }
OLDNEW
« no previous file with comments | « base/win/iat_patch_function.cc ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698