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

Side by Side Diff: sandbox/win/src/handle_closer_agent.cc

Issue 919893002: Replace handles that the handle closer closes with dummy Events. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: add comment Created 5 years, 10 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 | « sandbox/win/src/handle_closer_agent.h ('k') | sandbox/win/src/handle_closer_test.cc » ('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 (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2012 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 "sandbox/win/src/handle_closer_agent.h" 5 #include "sandbox/win/src/handle_closer_agent.h"
6 6
7 #include "base/logging.h" 7 #include "base/logging.h"
8 #include "sandbox/win/src/nt_internals.h" 8 #include "sandbox/win/src/nt_internals.h"
9 #include "sandbox/win/src/win_utils.h" 9 #include "sandbox/win/src/win_utils.h"
10 10
(...skipping 23 matching lines...) Expand all
34 34
35 namespace sandbox { 35 namespace sandbox {
36 36
37 // Memory buffer mapped from the parent, with the list of handles. 37 // Memory buffer mapped from the parent, with the list of handles.
38 SANDBOX_INTERCEPT HandleCloserInfo* g_handles_to_close = NULL; 38 SANDBOX_INTERCEPT HandleCloserInfo* g_handles_to_close = NULL;
39 39
40 bool HandleCloserAgent::NeedsHandlesClosed() { 40 bool HandleCloserAgent::NeedsHandlesClosed() {
41 return g_handles_to_close != NULL; 41 return g_handles_to_close != NULL;
42 } 42 }
43 43
44 HandleCloserAgent::HandleCloserAgent()
45 : dummy_handle_(::CreateEvent(NULL, FALSE, FALSE, NULL)) {
46 }
47
48 // Attempts to stuff |closed_handle| with a duplicated handle for a dummy Event
49 // with no access. This should allow the handle to be closed, to avoid
50 // generating EXCEPTION_INVALID_HANDLE on shutdown, but nothing else. For now
51 // the only supported |type| is Event or File.
52 bool HandleCloserAgent::AttemptToStuffHandleSlot(HANDLE closed_handle,
53 const base::string16& type) {
54 // Only attempt to stuff Files and Events at the moment.
55 if (type != L"Event" && type != L"File") {
56 return true;
57 }
58
59 if (!dummy_handle_.IsValid())
60 return false;
61
62 // This should never happen, as g_dummy is created before closing to_stuff.
63 DCHECK(dummy_handle_.Get() != closed_handle);
64
65 std::vector<HANDLE> to_close;
66 HANDLE dup_dummy = NULL;
67 size_t count = 16;
68
69 do {
70 if (!::DuplicateHandle(::GetCurrentProcess(), dummy_handle_.Get(),
71 ::GetCurrentProcess(), &dup_dummy, 0, FALSE, 0))
72 break;
73 if (dup_dummy != closed_handle)
74 to_close.push_back(dup_dummy);
75 } while (count-- &&
76 reinterpret_cast<uintptr_t>(dup_dummy) <
77 reinterpret_cast<uintptr_t>(closed_handle));
78
79 for (auto h : to_close)
80 ::CloseHandle(h);
81
82 // Useful to know when we're not able to stuff handles.
83 DCHECK(dup_dummy == closed_handle);
84
85 return dup_dummy == closed_handle;
86 }
87
44 // Reads g_handles_to_close and creates the lookup map. 88 // Reads g_handles_to_close and creates the lookup map.
45 void HandleCloserAgent::InitializeHandlesToClose() { 89 void HandleCloserAgent::InitializeHandlesToClose() {
46 CHECK(g_handles_to_close != NULL); 90 CHECK(g_handles_to_close != NULL);
47 91
48 // Grab the header. 92 // Grab the header.
49 HandleListEntry* entry = g_handles_to_close->handle_entries; 93 HandleListEntry* entry = g_handles_to_close->handle_entries;
50 for (size_t i = 0; i < g_handles_to_close->num_handle_types; ++i) { 94 for (size_t i = 0; i < g_handles_to_close->num_handle_types; ++i) {
51 // Set the type name. 95 // Set the type name.
52 base::char16* input = entry->handle_type; 96 base::char16* input = entry->handle_type;
53 HandleMap::mapped_type& handle_names = handles_to_close_[input]; 97 HandleMap::mapped_type& handle_names = handles_to_close_[input];
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after
129 if (!names.empty()) { 173 if (!names.empty()) {
130 // Move on to the next handle if this name doesn't match. 174 // Move on to the next handle if this name doesn't match.
131 if (!GetHandleName(handle, &handle_name) || !names.count(handle_name)) 175 if (!GetHandleName(handle, &handle_name) || !names.count(handle_name))
132 continue; 176 continue;
133 } 177 }
134 178
135 if (!::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0)) 179 if (!::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0))
136 return false; 180 return false;
137 if (!::CloseHandle(handle)) 181 if (!::CloseHandle(handle))
138 return false; 182 return false;
183 // Attempt to stuff this handle with a new dummy Event.
184 AttemptToStuffHandleSlot(handle, result->first);
139 } 185 }
140 } 186 }
141 187
142 return true; 188 return true;
143 } 189 }
144 190
145 } // namespace sandbox 191 } // namespace sandbox
OLDNEW
« no previous file with comments | « sandbox/win/src/handle_closer_agent.h ('k') | sandbox/win/src/handle_closer_test.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698