Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 | |
| 59 // Change the page protection so that we can write. | |
| 60 MEMORY_BASIC_INFORMATION memory_info; | |
| 61 if (!VirtualQuery(address, &memory_info, sizeof(memory_info))) | |
| 62 return false; | |
| 63 | |
| 64 DWORD is_executable = (PAGE_EXECUTE | PAGE_EXECUTE_READ | | |
| 65 PAGE_EXECUTE_READWRITE | PAGE_EXECUTE_WRITECOPY) & | |
| 66 memory_info.Protect; | |
| 67 | |
| 68 DWORD protect = is_executable ? PAGE_EXECUTE_READWRITE : PAGE_READWRITE; | |
| 69 if (!VirtualProtect(address, bytes, protect, &old_protect_)) | |
|
cpu_(ooo_6.6-7.5)
2014/09/26 01:10:01
do we want to deal with non executable pages?
Giv
rvargas (doing something else)
2014/09/26 18:47:49
Yes because this depends on the structure of the d
cpu_(ooo_6.6-7.5)
2014/09/29 18:24:53
Acknowledged.
| |
| 70 return false; | |
| 71 | |
| 72 changed_ = true; | |
| 73 address_ = address; | |
| 74 bytes_ = bytes; | |
| 75 return true; | |
| 76 } | |
| 77 | |
| 78 void AutoProtectMemory::RevertProtection() { | |
| 79 if (!changed_) | |
| 80 return; | |
| 81 | |
| 82 DCHECK(address_); | |
|
cpu_(ooo_6.6-7.5)
2014/09/26 01:10:01
remove address_ check, put it in ChangeProtection.
rvargas (doing something else)
2014/09/26 18:47:49
Why? This verifies an invariant from this function
cpu_(ooo_6.6-7.5)
2014/09/29 18:24:53
because the only setter of address_ is ChangeProte
| |
| 83 DCHECK(bytes_); | |
| 84 | |
| 85 VirtualProtect(address_, bytes_, old_protect_, &old_protect_); | |
|
cpu_(ooo_6.6-7.5)
2014/09/26 01:10:01
sholdn't we dcheck that this VP fails?
rvargas (doing something else)
2014/09/26 18:47:49
Maybe, but what do we gain? I don't like to DCHECK
cpu_(ooo_6.6-7.5)
2014/09/29 18:24:52
Acknowledged.
| |
| 86 changed_ = false; | |
| 87 address_ = NULL; | |
| 88 bytes_ = 0; | |
| 89 old_protect_ = 0; | |
| 90 } | |
| 91 | |
| 92 // Performs an EAT interception. | |
| 93 void EATPatch(HMODULE module, const char* function_name, | |
| 94 void* new_function, void** old_function) { | |
| 95 DCHECK(module); | |
| 96 | |
| 97 base::win::PEImage pe(module); | |
| 98 if (!pe.VerifyMagic()) | |
|
cpu_(ooo_6.6-7.5)
2014/09/26 01:10:01
I guess I am confused why this is a silent return
rvargas (doing something else)
2014/09/26 18:47:49
Because it's valid for the caller to pass an addre
cpu_(ooo_6.6-7.5)
2014/09/29 18:24:53
I think either removing dcheck or adding a comment
| |
| 99 return; | |
| 100 | |
| 101 DWORD* eat_entry = pe.GetExportEntry(function_name); | |
| 102 if (!eat_entry) | |
| 103 return; | |
| 104 | |
| 105 if (!(*old_function)) | |
| 106 *old_function = pe.RVAToAddr(*eat_entry); | |
| 107 | |
| 108 AutoProtectMemory memory; | |
| 109 if (!memory.ChangeProtection(eat_entry, sizeof(DWORD))) | |
| 110 return; | |
| 111 | |
| 112 // Perform the patch. | |
| 113 #pragma warning(push) | |
| 114 #pragma warning(disable: 4311) | |
| 115 // These casts generate warnings because they are 32 bit specific. | |
| 116 *eat_entry = reinterpret_cast<DWORD>(new_function) - | |
| 117 reinterpret_cast<DWORD>(module); | |
| 118 #pragma warning(pop) | |
| 119 } | |
| 120 | |
| 31 // Keeps track of all the hooks needed to intercept CloseHandle. | 121 // Keeps track of all the hooks needed to intercept CloseHandle. |
| 32 class CloseHandleHooks { | 122 class CloseHandleHooks { |
| 33 public: | 123 public: |
| 34 CloseHandleHooks() {} | 124 CloseHandleHooks() {} |
| 35 ~CloseHandleHooks() {} | 125 ~CloseHandleHooks() {} |
| 36 | 126 |
| 37 void AddIATPatch(const base::string16& module); | 127 void AddIATPatch(HMODULE module); |
| 128 void AddEATPatch(); | |
| 38 void Unpatch(); | 129 void Unpatch(); |
| 39 | 130 |
| 40 private: | 131 private: |
| 41 std::vector<base::win::IATPatchFunction*> hooks_; | 132 std::vector<base::win::IATPatchFunction*> hooks_; |
| 42 DISALLOW_COPY_AND_ASSIGN(CloseHandleHooks); | 133 DISALLOW_COPY_AND_ASSIGN(CloseHandleHooks); |
| 43 }; | 134 }; |
| 44 base::LazyInstance<CloseHandleHooks> g_hooks = LAZY_INSTANCE_INITIALIZER; | 135 base::LazyInstance<CloseHandleHooks> g_hooks = LAZY_INSTANCE_INITIALIZER; |
| 45 | 136 |
| 46 void CloseHandleHooks::AddIATPatch(const base::string16& module) { | 137 void CloseHandleHooks::AddIATPatch(HMODULE module) { |
| 47 if (module.empty()) | 138 if (!module) |
| 48 return; | 139 return; |
| 49 | 140 |
| 50 base::win::IATPatchFunction* patch = new base::win::IATPatchFunction; | 141 base::win::IATPatchFunction* patch = new base::win::IATPatchFunction; |
| 51 patch->Patch(module.c_str(), "kernel32.dll", "CloseHandle", CloseHandleHook); | 142 if (patch->PatchFromModule(module, "kernel32.dll", "CloseHandle", |
| 143 CloseHandleHook)) { | |
| 144 delete patch; | |
| 145 return; | |
| 146 } | |
| 147 | |
| 52 hooks_.push_back(patch); | 148 hooks_.push_back(patch); |
| 53 if (!g_close_function) { | 149 if (!g_close_function) { |
| 54 // Things are probably messed up if each intercepted function points to | 150 // Things are probably messed up if each intercepted function points to |
| 55 // a different place, but we need only one function to call. | 151 // a different place, but we need only one function to call. |
| 56 g_close_function = | 152 g_close_function = |
| 57 reinterpret_cast<CloseHandleType>(patch->original_function()); | 153 reinterpret_cast<CloseHandleType>(patch->original_function()); |
| 58 } | 154 } |
| 59 } | 155 } |
| 60 | 156 |
| 157 void CloseHandleHooks::AddEATPatch() { | |
| 158 // An attempt to restore the entry on the table at destruction is not safe. | |
| 159 EATPatch(GetModuleHandleA("kernel32.dll"), "CloseHandle", | |
| 160 &CloseHandleHook, reinterpret_cast<void**>(&g_close_function)); | |
| 161 } | |
| 162 | |
| 61 void CloseHandleHooks::Unpatch() { | 163 void CloseHandleHooks::Unpatch() { |
| 62 for (std::vector<base::win::IATPatchFunction*>::iterator it = hooks_.begin(); | 164 for (std::vector<base::win::IATPatchFunction*>::iterator it = hooks_.begin(); |
| 63 it != hooks_.end(); ++it) { | 165 it != hooks_.end(); ++it) { |
| 64 (*it)->Unpatch(); | 166 (*it)->Unpatch(); |
| 167 delete *it; | |
| 65 } | 168 } |
| 66 } | 169 } |
| 67 | 170 |
| 68 bool UseHooks() { | 171 bool UseHooks() { |
| 172 #if defined(ARCH_CPU_X86_64) | |
| 173 return false; | |
| 174 #elif defined(NDEBUG) | |
| 69 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); | 175 chrome::VersionInfo::Channel channel = chrome::VersionInfo::GetChannel(); |
| 70 if (channel == chrome::VersionInfo::CHANNEL_CANARY || | 176 if (channel == chrome::VersionInfo::CHANNEL_CANARY || |
| 71 channel == chrome::VersionInfo::CHANNEL_DEV) { | 177 channel == chrome::VersionInfo::CHANNEL_DEV) { |
| 72 return true; | 178 return true; |
| 73 } | 179 } |
| 74 | 180 |
| 75 return false; | 181 return false; |
| 182 #else // NDEBUG | |
| 183 return true; | |
| 184 #endif | |
| 76 } | 185 } |
| 77 | 186 |
| 78 base::string16 GetModuleName(HMODULE module) { | 187 void PatchLoadedModules(CloseHandleHooks* hooks) { |
| 79 base::string16 name; | 188 const DWORD kSize = 256; |
| 80 if (!module) | 189 DWORD returned; |
| 81 return name; | 190 scoped_ptr<HMODULE[]> modules(new HMODULE[kSize]); |
| 82 wchar_t buffer[MAX_PATH]; | 191 if (!EnumProcessModules(GetCurrentProcess(), modules.get(), |
| 83 int rv = GetModuleFileName(module, buffer, MAX_PATH); | 192 kSize * sizeof(HMODULE), &returned)) { |
| 84 if (rv == MAX_PATH) | 193 return; |
| 85 return name; | 194 } |
| 195 returned /= sizeof(HMODULE); | |
| 196 returned = std::min(kSize, returned); | |
| 86 | 197 |
| 87 buffer[MAX_PATH - 1] = L'\0'; | 198 for (DWORD current = 0; current < returned; current++) { |
| 88 name.assign(buffer); | 199 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 } | 200 } |
| 101 return module; | |
| 102 } | 201 } |
| 103 | 202 |
| 104 } // namespace | 203 } // namespace |
| 105 | 204 |
| 106 void InstallCloseHandleHooks() { | 205 void InstallCloseHandleHooks() { |
| 107 if (UseHooks()) { | 206 if (UseHooks()) { |
| 108 CloseHandleHooks* hooks = g_hooks.Pointer(); | 207 CloseHandleHooks* hooks = g_hooks.Pointer(); |
| 109 hooks->AddIATPatch(L"chrome.exe"); | 208 |
| 110 hooks->AddIATPatch(GetModuleName(GetChromeDLLModule())); | 209 // Performing EAT interception first is safer in the presence of other |
| 210 // threads attempting to call CloseHandle. | |
| 211 hooks->AddEATPatch(); | |
| 212 PatchLoadedModules(hooks); | |
| 111 } else { | 213 } else { |
| 112 base::win::DisableHandleVerifier(); | 214 base::win::DisableHandleVerifier(); |
| 113 } | 215 } |
| 114 } | 216 } |
| 115 | 217 |
| 116 void RemoveCloseHandleHooks() { | 218 void RemoveCloseHandleHooks() { |
| 117 g_hooks.Get().Unpatch(); | 219 g_hooks.Get().Unpatch(); |
| 118 } | 220 } |
| OLD | NEW |