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

Side by Side Diff: base/debug/close_handle_hook_win.cc

Issue 1549633005: Move CloseHandle hook into base/debug. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: rebase Created 4 years, 11 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/debug/close_handle_hook_win.h ('k') | chrome/BUILD.gn » ('j') | 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 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 "chrome/app/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 <vector> 12 #include <vector>
13 13
14 #include "base/lazy_instance.h" 14 #include "base/lazy_instance.h"
15 #include "base/macros.h" 15 #include "base/macros.h"
16 #include "base/memory/scoped_ptr.h" 16 #include "base/memory/scoped_ptr.h"
17 #include "base/win/iat_patch_function.h" 17 #include "base/win/iat_patch_function.h"
18 #include "base/win/pe_image.h" 18 #include "base/win/pe_image.h"
19 #include "base/win/scoped_handle.h" 19 #include "base/win/scoped_handle.h"
20 #include "build/build_config.h" 20 #include "build/build_config.h"
21 #include "chrome/common/channel_info.h"
22 #include "components/version_info/version_info.h"
23 21
24 namespace { 22 namespace {
25 23
26 typedef BOOL (WINAPI* CloseHandleType) (HANDLE handle); 24 typedef BOOL (WINAPI* CloseHandleType) (HANDLE handle);
27 25
28 typedef BOOL (WINAPI* DuplicateHandleType)(HANDLE source_process, 26 typedef BOOL (WINAPI* DuplicateHandleType)(HANDLE source_process,
29 HANDLE source_handle, 27 HANDLE source_handle,
30 HANDLE target_process, 28 HANDLE target_process,
31 HANDLE* target_handle, 29 HANDLE* target_handle,
32 DWORD desired_access, 30 DWORD desired_access,
(...skipping 21 matching lines...) Expand all
54 if ((options & DUPLICATE_CLOSE_SOURCE) && 52 if ((options & DUPLICATE_CLOSE_SOURCE) &&
55 (GetProcessId(source_process) == ::GetCurrentProcessId())) { 53 (GetProcessId(source_process) == ::GetCurrentProcessId())) {
56 base::win::OnHandleBeingClosed(source_handle); 54 base::win::OnHandleBeingClosed(source_handle);
57 } 55 }
58 56
59 return g_duplicate_function(source_process, source_handle, target_process, 57 return g_duplicate_function(source_process, source_handle, target_process,
60 target_handle, desired_access, inherit_handle, 58 target_handle, desired_access, inherit_handle,
61 options); 59 options);
62 } 60 }
63 61
62 } // namespace
63
64 namespace base {
65 namespace debug {
66
67 namespace {
68
64 // Provides a simple way to temporarily change the protection of a memory page. 69 // Provides a simple way to temporarily change the protection of a memory page.
65 class AutoProtectMemory { 70 class AutoProtectMemory {
66 public: 71 public:
67 AutoProtectMemory() 72 AutoProtectMemory()
68 : changed_(false), address_(NULL), bytes_(0), old_protect_(0) {} 73 : changed_(false), address_(NULL), bytes_(0), old_protect_(0) {}
69 74
70 ~AutoProtectMemory() { 75 ~AutoProtectMemory() {
71 RevertProtection(); 76 RevertProtection();
72 } 77 }
73 78
(...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after
228 } 233 }
229 234
230 void HandleHooks::Unpatch() { 235 void HandleHooks::Unpatch() {
231 for (std::vector<base::win::IATPatchFunction*>::iterator it = hooks_.begin(); 236 for (std::vector<base::win::IATPatchFunction*>::iterator it = hooks_.begin();
232 it != hooks_.end(); ++it) { 237 it != hooks_.end(); ++it) {
233 (*it)->Unpatch(); 238 (*it)->Unpatch();
234 delete *it; 239 delete *it;
235 } 240 }
236 } 241 }
237 242
238 bool UseHooks() {
239 #if defined(ARCH_CPU_X86_64)
240 return false;
241 #elif defined(NDEBUG)
242 version_info::Channel channel = chrome::GetChannel();
243 if (channel == version_info::Channel::CANARY ||
244 channel == version_info::Channel::DEV) {
245 return true;
246 }
247
248 return false;
249 #else // NDEBUG
250 return true;
251 #endif
252 }
253
254 void PatchLoadedModules(HandleHooks* hooks) { 243 void PatchLoadedModules(HandleHooks* hooks) {
255 const DWORD kSize = 256; 244 const DWORD kSize = 256;
256 DWORD returned; 245 DWORD returned;
257 scoped_ptr<HMODULE[]> modules(new HMODULE[kSize]); 246 scoped_ptr<HMODULE[]> modules(new HMODULE[kSize]);
258 if (!EnumProcessModules(GetCurrentProcess(), modules.get(), 247 if (!EnumProcessModules(GetCurrentProcess(), modules.get(),
259 kSize * sizeof(HMODULE), &returned)) { 248 kSize * sizeof(HMODULE), &returned)) {
260 return; 249 return;
261 } 250 }
262 returned /= sizeof(HMODULE); 251 returned /= sizeof(HMODULE);
263 returned = std::min(kSize, returned); 252 returned = std::min(kSize, returned);
264 253
265 for (DWORD current = 0; current < returned; current++) { 254 for (DWORD current = 0; current < returned; current++) {
266 hooks->AddIATPatch(modules[current]); 255 hooks->AddIATPatch(modules[current]);
267 } 256 }
268 } 257 }
269 258
270 } // namespace 259 } // namespace
271 260
272 void InstallHandleHooks() { 261 void InstallHandleHooks() {
273 if (UseHooks()) { 262 HandleHooks* hooks = g_hooks.Pointer();
274 HandleHooks* hooks = g_hooks.Pointer();
275 263
276 // Performing EAT interception first is safer in the presence of other 264 // Performing EAT interception first is safer in the presence of other
277 // threads attempting to call CloseHandle. 265 // threads attempting to call CloseHandle.
278 hooks->AddEATPatch(); 266 hooks->AddEATPatch();
279 PatchLoadedModules(hooks); 267 PatchLoadedModules(hooks);
280 } else {
281 base::win::DisableHandleVerifier();
282 }
283 } 268 }
284 269
285 void RemoveHandleHooks() { 270 void RemoveHandleHooks() {
286 // We are partching all loaded modules without forcing them to stay in memory, 271 // We are partching all loaded modules without forcing them to stay in memory,
287 // removing patches is not safe. 272 // removing patches is not safe.
288 } 273 }
274
275 } // namespace debug
276 } // namespace base
OLDNEW
« no previous file with comments | « base/debug/close_handle_hook_win.h ('k') | chrome/BUILD.gn » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698