| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 "base/debug/close_handle_hook_win.h" | 5 #include "base/debug/close_handle_hook_win.h" |
| 6 | 6 |
| 7 #include <Windows.h> | 7 #include <Windows.h> |
| 8 #include <psapi.h> | 8 #include <psapi.h> |
| 9 #include <stddef.h> | 9 #include <stddef.h> |
| 10 | 10 |
| 11 #include <algorithm> | 11 #include <algorithm> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/lazy_instance.h" | |
| 16 #include "base/macros.h" | 15 #include "base/macros.h" |
| 17 #include "base/win/iat_patch_function.h" | 16 #include "base/win/iat_patch_function.h" |
| 18 #include "base/win/pe_image.h" | 17 #include "base/win/pe_image.h" |
| 19 #include "base/win/scoped_handle.h" | 18 #include "base/win/scoped_handle.h" |
| 20 #include "build/build_config.h" | 19 #include "build/build_config.h" |
| 21 | 20 |
| 22 namespace { | 21 namespace { |
| 23 | 22 |
| 24 typedef BOOL (WINAPI* CloseHandleType) (HANDLE handle); | 23 typedef BOOL (WINAPI* CloseHandleType) (HANDLE handle); |
| 25 | 24 |
| (...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 196 ~HandleHooks() {} | 195 ~HandleHooks() {} |
| 197 | 196 |
| 198 void AddIATPatch(HMODULE module); | 197 void AddIATPatch(HMODULE module); |
| 199 void AddEATPatch(); | 198 void AddEATPatch(); |
| 200 void Unpatch(); | 199 void Unpatch(); |
| 201 | 200 |
| 202 private: | 201 private: |
| 203 std::vector<base::win::IATPatchFunction*> hooks_; | 202 std::vector<base::win::IATPatchFunction*> hooks_; |
| 204 DISALLOW_COPY_AND_ASSIGN(HandleHooks); | 203 DISALLOW_COPY_AND_ASSIGN(HandleHooks); |
| 205 }; | 204 }; |
| 206 base::LazyInstance<HandleHooks> g_hooks = LAZY_INSTANCE_INITIALIZER; | |
| 207 | 205 |
| 208 void HandleHooks::AddIATPatch(HMODULE module) { | 206 void HandleHooks::AddIATPatch(HMODULE module) { |
| 209 if (!module) | 207 if (!module) |
| 210 return; | 208 return; |
| 211 | 209 |
| 212 base::win::IATPatchFunction* patch = NULL; | 210 base::win::IATPatchFunction* patch = NULL; |
| 213 patch = IATPatch(module, "CloseHandle", &CloseHandleHook, | 211 patch = IATPatch(module, "CloseHandle", &CloseHandleHook, |
| 214 reinterpret_cast<void**>(&g_close_function)); | 212 reinterpret_cast<void**>(&g_close_function)); |
| 215 if (!patch) | 213 if (!patch) |
| 216 return; | 214 return; |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 252 returned = std::min(kSize, returned); | 250 returned = std::min(kSize, returned); |
| 253 | 251 |
| 254 for (DWORD current = 0; current < returned; current++) { | 252 for (DWORD current = 0; current < returned; current++) { |
| 255 hooks->AddIATPatch(modules[current]); | 253 hooks->AddIATPatch(modules[current]); |
| 256 } | 254 } |
| 257 } | 255 } |
| 258 | 256 |
| 259 } // namespace | 257 } // namespace |
| 260 | 258 |
| 261 void InstallHandleHooks() { | 259 void InstallHandleHooks() { |
| 262 HandleHooks* hooks = g_hooks.Pointer(); | 260 static HandleHooks* hooks = new HandleHooks(); |
| 263 | 261 |
| 264 // Performing EAT interception first is safer in the presence of other | 262 // Performing EAT interception first is safer in the presence of other |
| 265 // threads attempting to call CloseHandle. | 263 // threads attempting to call CloseHandle. |
| 266 hooks->AddEATPatch(); | 264 hooks->AddEATPatch(); |
| 267 PatchLoadedModules(hooks); | 265 PatchLoadedModules(hooks); |
| 268 } | 266 } |
| 269 | 267 |
| 270 void RemoveHandleHooks() { | 268 void RemoveHandleHooks() { |
| 271 // We are partching all loaded modules without forcing them to stay in memory, | 269 // We are partching all loaded modules without forcing them to stay in memory, |
| 272 // removing patches is not safe. | 270 // removing patches is not safe. |
| 273 } | 271 } |
| 274 | 272 |
| 275 } // namespace debug | 273 } // namespace debug |
| 276 } // namespace base | 274 } // namespace base |
| OLD | NEW |