OLD | NEW |
---|---|
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 Loading... | |
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 | |
cpu_(ooo_6.6-7.5)
2015/02/19 00:10:02
write a comment here about the technique including
| |
48 bool HandleCloserAgent::AttemptToStuffHandleSlot(HANDLE closed_handle, | |
49 const base::string16& type) { | |
50 // Only attempt to stuff Files and Events at the moment. | |
51 if (type != L"Event" && type != L"File") { | |
52 return true; | |
53 } | |
54 | |
55 if (!dummy_handle_.IsValid()) | |
56 return false; | |
57 | |
58 // This should never happen, as g_dummy is created before closing to_stuff. | |
59 DCHECK(dummy_handle_.Get() != closed_handle); | |
60 | |
61 std::vector<HANDLE> to_close; | |
62 HANDLE dup_dummy = NULL; | |
63 size_t count = 16; | |
64 | |
65 do { | |
66 if (!::DuplicateHandle(::GetCurrentProcess(), dummy_handle_.Get(), | |
67 ::GetCurrentProcess(), &dup_dummy, 0, FALSE, 0)) | |
68 break; | |
69 if (dup_dummy != closed_handle) | |
70 to_close.push_back(dup_dummy); | |
71 } while (count-- && | |
72 reinterpret_cast<uintptr_t>(dup_dummy) < | |
73 reinterpret_cast<uintptr_t>(closed_handle)); | |
74 | |
75 for (auto h : to_close) | |
76 ::CloseHandle(h); | |
77 | |
78 // Useful to know when we're not able to stuff handles. | |
79 DCHECK(dup_dummy == closed_handle); | |
80 | |
81 return dup_dummy == closed_handle; | |
82 } | |
83 | |
44 // Reads g_handles_to_close and creates the lookup map. | 84 // Reads g_handles_to_close and creates the lookup map. |
45 void HandleCloserAgent::InitializeHandlesToClose() { | 85 void HandleCloserAgent::InitializeHandlesToClose() { |
46 CHECK(g_handles_to_close != NULL); | 86 CHECK(g_handles_to_close != NULL); |
47 | 87 |
48 // Grab the header. | 88 // Grab the header. |
49 HandleListEntry* entry = g_handles_to_close->handle_entries; | 89 HandleListEntry* entry = g_handles_to_close->handle_entries; |
50 for (size_t i = 0; i < g_handles_to_close->num_handle_types; ++i) { | 90 for (size_t i = 0; i < g_handles_to_close->num_handle_types; ++i) { |
51 // Set the type name. | 91 // Set the type name. |
52 base::char16* input = entry->handle_type; | 92 base::char16* input = entry->handle_type; |
53 HandleMap::mapped_type& handle_names = handles_to_close_[input]; | 93 HandleMap::mapped_type& handle_names = handles_to_close_[input]; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 if (!names.empty()) { | 169 if (!names.empty()) { |
130 // Move on to the next handle if this name doesn't match. | 170 // Move on to the next handle if this name doesn't match. |
131 if (!GetHandleName(handle, &handle_name) || !names.count(handle_name)) | 171 if (!GetHandleName(handle, &handle_name) || !names.count(handle_name)) |
132 continue; | 172 continue; |
133 } | 173 } |
134 | 174 |
135 if (!::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0)) | 175 if (!::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0)) |
136 return false; | 176 return false; |
137 if (!::CloseHandle(handle)) | 177 if (!::CloseHandle(handle)) |
138 return false; | 178 return false; |
179 // Attempt to stuff this handle with a new dummy Event. | |
180 AttemptToStuffHandleSlot(handle, result->first); | |
139 } | 181 } |
140 } | 182 } |
141 | 183 |
142 return true; | 184 return true; |
143 } | 185 } |
144 | 186 |
145 } // namespace sandbox | 187 } // namespace sandbox |
OLD | NEW |