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 12 matching lines...) Expand all Loading... | |
23 NTSTATUS status = STATUS_UNSUCCESSFUL; | 23 NTSTATUS status = STATUS_UNSUCCESSFUL; |
24 __try { | 24 __try { |
25 status = QueryObject(handle, ObjectTypeInformation, buffer, *size, size); | 25 status = QueryObject(handle, ObjectTypeInformation, buffer, *size, size); |
26 } __except(GetExceptionCode() == STATUS_INVALID_HANDLE ? | 26 } __except(GetExceptionCode() == STATUS_INVALID_HANDLE ? |
27 EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) { | 27 EXCEPTION_EXECUTE_HANDLER : EXCEPTION_CONTINUE_SEARCH) { |
28 status = STATUS_INVALID_HANDLE; | 28 status = STATUS_INVALID_HANDLE; |
29 } | 29 } |
30 return status; | 30 return status; |
31 } | 31 } |
32 | 32 |
33 bool AttemptToStuffHandleSlot(HANDLE to_stuff, const base::string16& type) { | |
cpu_(ooo_6.6-7.5)
2015/02/18 18:00:00
to_stuff -> closed_handle ?
Will Harris
2015/02/18 22:11:28
Done.
| |
34 // Only attempt to stuff Files and Events at the moment. | |
35 if (type != L"Event" && | |
36 type != L"File") { | |
37 return true; | |
38 } | |
39 | |
40 HANDLE dummy = ::CreateEvent(NULL, FALSE, FALSE, NULL); | |
forshaw
2015/02/18 10:50:10
Can we just use one event object for all stuffed h
cpu_(ooo_6.6-7.5)
2015/02/18 18:00:00
given that is single threaded we can indeed use a
Will Harris
2015/02/18 22:11:29
we can't create the template handle static in this
| |
41 | |
42 if (dummy == INVALID_HANDLE_VALUE) | |
forshaw
2015/02/18 10:50:10
While unlikely to fail CreateEvent actually return
Will Harris
2015/02/18 22:11:29
Done.
| |
43 return false; | |
44 | |
45 std::vector<HANDLE> to_close; | |
46 DWORD options = DUPLICATE_SAME_ACCESS; | |
Sigurður Ásgeirsson
2015/02/18 13:56:00
looks pretty reasonable - you may want to document
Will Harris
2015/02/18 22:11:29
changed this to 0 access as per other comments.
| |
47 | |
48 while (reinterpret_cast<uintptr_t>(dummy) < | |
49 reinterpret_cast<uintptr_t>(to_stuff)) { | |
50 HANDLE dup_dummy; | |
cpu_(ooo_6.6-7.5)
2015/02/18 18:00:00
move dup_dummy below 51
Will Harris
2015/02/18 22:11:29
Acknowledged.
| |
51 to_close.push_back(dummy); | |
52 | |
53 if (!::DuplicateHandle(::GetCurrentProcess(), dummy, ::GetCurrentProcess(), | |
54 &dup_dummy, 0, false, options)) | |
55 break; | |
Sigurður Ásgeirsson
2015/02/18 19:02:47
come to think of it, another check you may want to
Will Harris
2015/02/18 22:11:28
We can't do this because we don't know if the hand
| |
56 dummy = dup_dummy; | |
57 } | |
58 | |
59 if (dummy != to_stuff) | |
60 to_close.push_back(dummy); | |
61 | |
62 for (auto h : to_close) | |
63 ::CloseHandle(h); | |
64 | |
65 // We want to know when we're not able to stuff handles. | |
66 DCHECK(dummy == to_stuff); | |
67 | |
68 return dummy == to_stuff; | |
69 } | |
70 | |
33 } // namespace | 71 } // namespace |
34 | 72 |
35 namespace sandbox { | 73 namespace sandbox { |
36 | 74 |
37 // Memory buffer mapped from the parent, with the list of handles. | 75 // Memory buffer mapped from the parent, with the list of handles. |
38 SANDBOX_INTERCEPT HandleCloserInfo* g_handles_to_close = NULL; | 76 SANDBOX_INTERCEPT HandleCloserInfo* g_handles_to_close = NULL; |
39 | 77 |
40 bool HandleCloserAgent::NeedsHandlesClosed() { | 78 bool HandleCloserAgent::NeedsHandlesClosed() { |
41 return g_handles_to_close != NULL; | 79 return g_handles_to_close != NULL; |
42 } | 80 } |
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
129 if (!names.empty()) { | 167 if (!names.empty()) { |
130 // Move on to the next handle if this name doesn't match. | 168 // Move on to the next handle if this name doesn't match. |
131 if (!GetHandleName(handle, &handle_name) || !names.count(handle_name)) | 169 if (!GetHandleName(handle, &handle_name) || !names.count(handle_name)) |
132 continue; | 170 continue; |
133 } | 171 } |
134 | 172 |
135 if (!::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0)) | 173 if (!::SetHandleInformation(handle, HANDLE_FLAG_PROTECT_FROM_CLOSE, 0)) |
136 return false; | 174 return false; |
137 if (!::CloseHandle(handle)) | 175 if (!::CloseHandle(handle)) |
138 return false; | 176 return false; |
177 // Attempt to stuff this handle with a new empty Event. | |
cpu_(ooo_6.6-7.5)
2015/02/18 18:00:00
"new dummy event"
Will Harris
2015/02/18 22:11:29
Done.
| |
178 AttemptToStuffHandleSlot(handle, result->first); | |
139 } | 179 } |
140 } | 180 } |
141 | 181 |
142 return true; | 182 return true; |
143 } | 183 } |
144 | 184 |
145 } // namespace sandbox | 185 } // namespace sandbox |
OLD | NEW |