1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "content/browser/handle_enumerator_win.h"
10 #include "base/logging.h"
11 #include "base/process.h"
12 #include "base/process_util.h"
13 #include "base/utf_string_conversions.h"
14 #include "base/win/windows_version.h"
15 #include "content/browser/browser_child_process_host.h"
16 #include "content/browser/browser_thread.h"
17 #include "content/browser/renderer_host/render_process_host.h"
18 #include "content/common/result_codes.h"
19 #include "sandbox/src/handle_table.h"
23 typedef std::map
<const string16
, content::HandleType
> HandleTypeMap
;
25 HandleTypeMap
& MakeHandleTypeMap() {
26 HandleTypeMap
& handle_types
= *(new HandleTypeMap());
27 handle_types
[sandbox::HandleTable::kTypeProcess
] = content::ProcessHandle
;
28 handle_types
[sandbox::HandleTable::kTypeThread
] = content::ThreadHandle
;
29 handle_types
[sandbox::HandleTable::kTypeFile
] = content::FileHandle
;
30 handle_types
[sandbox::HandleTable::kTypeDirectory
] =
31 content::DirectoryHandle
;
32 handle_types
[sandbox::HandleTable::kTypeKey
] = content::KeyHandle
;
33 handle_types
[sandbox::HandleTable::kTypeWindowStation
] =
34 content::WindowStationHandle
;
35 handle_types
[sandbox::HandleTable::kTypeDesktop
] = content::DesktopHandle
;
36 handle_types
[sandbox::HandleTable::kTypeService
] = content::ServiceHandle
;
37 handle_types
[sandbox::HandleTable::kTypeMutex
] = content::MutexHandle
;
38 handle_types
[sandbox::HandleTable::kTypeSemaphore
] =
39 content::SemaphoreHandle
;
40 handle_types
[sandbox::HandleTable::kTypeEvent
] = content::EventHandle
;
41 handle_types
[sandbox::HandleTable::kTypeTimer
] = content::TimerHandle
;
42 handle_types
[sandbox::HandleTable::kTypeNamedPipe
] =
43 content::NamedPipeHandle
;
44 handle_types
[sandbox::HandleTable::kTypeJobObject
] = content::JobHandle
;
45 handle_types
[sandbox::HandleTable::kTypeFileMap
] = content::FileMapHandle
;
46 handle_types
[sandbox::HandleTable::kTypeAlpcPort
] =
47 content::AlpcPortHandle
;
56 const wchar_t kNtdllDllName
[] = L
"ntdll.dll";
57 const size_t kMaxHandleNameLength
= 1024;
59 void HandleEnumerator::EnumerateHandles() {
60 sandbox::HandleTable handles
;
62 string16 output
= ProcessTypeString(type_
);
63 output
.append(ASCIIToUTF16(" Process - Handles at shutdown:\n"));
64 for (sandbox::HandleTable::Iterator sys_handle
65 = handles
.HandlesForProcess(::GetProcessId(handle_
));
66 sys_handle
!= handles
.end(); ++sys_handle
) {
67 HandleType current_type
= StringToHandleType(sys_handle
->Type());
68 if (!all_handles_
&& (current_type
!= ProcessHandle
&&
69 current_type
!= FileHandle
&&
70 current_type
!= DirectoryHandle
&&
71 current_type
!= KeyHandle
&&
72 current_type
!= WindowStationHandle
&&
73 current_type
!= DesktopHandle
&&
74 current_type
!= ServiceHandle
))
77 output
+= ASCIIToUTF16("[");
78 output
+= sys_handle
->Type();
79 output
+= ASCIIToUTF16("] (");
80 output
+= sys_handle
->Name();
81 output
+= ASCIIToUTF16(")\n");
82 output
+= GetAccessString(current_type
,
83 sys_handle
->handle_entry()->GrantedAccess
);
88 void HandleEnumerator::RunHandleEnumeration() {
89 DCHECK(status_
== Starting
);
90 if (BrowserThread::CurrentlyOn(BrowserThread::IO
))
91 FindProcessOnIOThread();
92 else if (BrowserThread::CurrentlyOn(BrowserThread::UI
))
93 FindProcessOnUIThread();
95 BrowserThread::PostTask(
96 BrowserThread::IO
, FROM_HERE
,
97 NewRunnableMethod(this,
98 &HandleEnumerator::FindProcessOnIOThread
));
101 void HandleEnumerator::FindProcessOnIOThread() {
102 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::IO
));
104 for (BrowserChildProcessHost::Iterator iter
; !iter
.Done(); ++iter
) {
105 if (iter
->handle() == handle_
) {
106 type_
= iter
->type();
112 if (status_
== Starting
) {
113 status_
= InProgress
;
114 BrowserThread::PostTask(
115 BrowserThread::UI
, FROM_HERE
,
116 NewRunnableMethod(this,
117 &HandleEnumerator::FindProcessOnUIThread
));
120 BrowserThread::PostTask(
121 BrowserThread::PROCESS_LAUNCHER
, FROM_HERE
,
122 NewRunnableMethod(this,
123 &HandleEnumerator::EnumerateHandlesAndTerminateProcess
));
127 void HandleEnumerator::FindProcessOnUIThread() {
128 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI
));
130 for (RenderProcessHost::iterator
renderer_iter(
131 RenderProcessHost::AllHostsIterator()); !renderer_iter
.IsAtEnd();
132 renderer_iter
.Advance()) {
133 RenderProcessHost
* render_process_host
= renderer_iter
.GetCurrentValue();
134 if (render_process_host
->GetHandle() == handle_
) {
135 type_
= ChildProcessInfo::RENDER_PROCESS
;
141 if (status_
== Starting
) {
142 status_
= InProgress
;
143 BrowserThread::PostTask(
144 BrowserThread::IO
, FROM_HERE
,
145 NewRunnableMethod(this,
146 &HandleEnumerator::FindProcessOnIOThread
));
149 BrowserThread::PostTask(
150 BrowserThread::PROCESS_LAUNCHER
, FROM_HERE
,
151 NewRunnableMethod(this,
152 &HandleEnumerator::EnumerateHandlesAndTerminateProcess
));
156 void HandleEnumerator::EnumerateHandlesAndTerminateProcess() {
157 HandleEnumerator::EnumerateHandles();
158 base::Process
process(handle_
);
159 process
.Terminate(content::RESULT_CODE_NORMAL_EXIT
);
163 string16
ProcessTypeString(ChildProcessInfo::ProcessType process_type
) {
164 switch (process_type
) {
165 case ChildProcessInfo::UNKNOWN_PROCESS
:
166 return ASCIIToUTF16("Unknown");
167 case ChildProcessInfo::BROWSER_PROCESS
:
168 return ASCIIToUTF16("Browser");
169 case ChildProcessInfo::RENDER_PROCESS
:
170 return ASCIIToUTF16("Renderer");
171 case ChildProcessInfo::PLUGIN_PROCESS
:
172 return ASCIIToUTF16("Plugin");
173 case ChildProcessInfo::WORKER_PROCESS
:
174 return ASCIIToUTF16("Worker");
175 case ChildProcessInfo::NACL_LOADER_PROCESS
:
176 return ASCIIToUTF16("NaCL Loader");
177 case ChildProcessInfo::UTILITY_PROCESS
:
178 return ASCIIToUTF16("Utility");
179 case ChildProcessInfo::PROFILE_IMPORT_PROCESS
:
180 return ASCIIToUTF16("Profile Import");
181 case ChildProcessInfo::ZYGOTE_PROCESS
:
182 return ASCIIToUTF16("Zygote");
183 case ChildProcessInfo::SANDBOX_HELPER_PROCESS
:
184 return ASCIIToUTF16("Sandbox Helper");
185 case ChildProcessInfo::NACL_BROKER_PROCESS
:
186 return ASCIIToUTF16("NaCL Broker");
187 case ChildProcessInfo::GPU_PROCESS
:
188 return ASCIIToUTF16("GPU");
189 case ChildProcessInfo::PPAPI_PLUGIN_PROCESS
:
190 return ASCIIToUTF16("Pepper Plugin");
191 case ChildProcessInfo::PPAPI_BROKER_PROCESS
:
192 return ASCIIToUTF16("Pepper Broker");
198 HandleType
StringToHandleType(const string16
& type
) {
199 static HandleTypeMap handle_types
= MakeHandleTypeMap();
200 HandleTypeMap::iterator result
= handle_types
.find(type
);
201 return result
!= handle_types
.end() ? result
->second
: OtherHandle
;
204 string16
GetAccessString(HandleType handle_type
,
205 ACCESS_MASK access
) {
207 if (access
& GENERIC_READ
)
208 output
.append(ASCIIToUTF16("\tGENERIC_READ\n"));
209 if (access
& GENERIC_WRITE
)
210 output
.append(ASCIIToUTF16("\tGENERIC_WRITE\n"));
211 if (access
& GENERIC_EXECUTE
)
212 output
.append(ASCIIToUTF16("\tGENERIC_EXECUTE\n"));
213 if (access
& GENERIC_ALL
)
214 output
.append(ASCIIToUTF16("\tGENERIC_ALL\n"));
216 output
.append(ASCIIToUTF16("\tDELETE\n"));
217 if (access
& READ_CONTROL
)
218 output
.append(ASCIIToUTF16("\tREAD_CONTROL\n"));
219 if (access
& WRITE_DAC
)
220 output
.append(ASCIIToUTF16("\tWRITE_DAC\n"));
221 if (access
& WRITE_OWNER
)
222 output
.append(ASCIIToUTF16("\tWRITE_OWNER\n"));
223 if (access
& SYNCHRONIZE
)
224 output
.append(ASCIIToUTF16("\tSYNCHRONIZE\n"));
226 switch (handle_type
) {
228 if (access
& PROCESS_CREATE_PROCESS
)
229 output
.append(ASCIIToUTF16("\tPROCESS_CREATE_PROCESS\n"));
230 if (access
& PROCESS_CREATE_THREAD
)
231 output
.append(ASCIIToUTF16("\tPROCESS_CREATE_THREAD\n"));
232 if (access
& PROCESS_DUP_HANDLE
)
233 output
.append(ASCIIToUTF16("\tPROCESS_DUP_HANDLE\n"));
234 if (access
& PROCESS_QUERY_INFORMATION
)
235 output
.append(ASCIIToUTF16("\tPROCESS_QUERY_INFORMATION\n"));
236 if (access
& PROCESS_QUERY_LIMITED_INFORMATION
)
237 output
.append(ASCIIToUTF16("\tPROCESS_QUERY_LIMITED_INFORMATION\n"));
238 if (access
& PROCESS_SET_INFORMATION
)
239 output
.append(ASCIIToUTF16("\tPROCESS_SET_INFORMATION\n"));
240 if (access
& PROCESS_SET_QUOTA
)
241 output
.append(ASCIIToUTF16("\tPROCESS_SET_QUOTA\n"));
242 if (access
& PROCESS_SUSPEND_RESUME
)
243 output
.append(ASCIIToUTF16("\tPROCESS_SUSPEND_RESUME\n"));
244 if (access
& PROCESS_TERMINATE
)
245 output
.append(ASCIIToUTF16("\tPROCESS_TERMINATE\n"));
246 if (access
& PROCESS_VM_OPERATION
)
247 output
.append(ASCIIToUTF16("\tPROCESS_VM_OPERATION\n"));
248 if (access
& PROCESS_VM_READ
)
249 output
.append(ASCIIToUTF16("\tPROCESS_VM_READ\n"));
250 if (access
& PROCESS_VM_WRITE
)
251 output
.append(ASCIIToUTF16("\tPROCESS_VM_WRITE\n"));
254 if (access
& THREAD_DIRECT_IMPERSONATION
)
255 output
.append(ASCIIToUTF16("\tTHREAD_DIRECT_IMPERSONATION\n"));
256 if (access
& THREAD_GET_CONTEXT
)
257 output
.append(ASCIIToUTF16("\tTHREAD_GET_CONTEXT\n"));
258 if (access
& THREAD_IMPERSONATE
)
259 output
.append(ASCIIToUTF16("\tTHREAD_IMPERSONATE\n"));
260 if (access
& THREAD_QUERY_INFORMATION
)
261 output
.append(ASCIIToUTF16("\tTHREAD_QUERY_INFORMATION\n"));
262 if (access
& THREAD_QUERY_LIMITED_INFORMATION
)
263 output
.append(ASCIIToUTF16("\tTHREAD_QUERY_LIMITED_INFORMATION\n"));
264 if (access
& THREAD_SET_CONTEXT
)
265 output
.append(ASCIIToUTF16("\tTHREAD_SET_CONTEXT\n"));
266 if (access
& THREAD_SET_INFORMATION
)
267 output
.append(ASCIIToUTF16("\tTHREAD_SET_INFORMATION\n"));
268 if (access
& THREAD_SET_LIMITED_INFORMATION
)
269 output
.append(ASCIIToUTF16("\tTHREAD_SET_LIMITED_INFORMATION\n"));
270 if (access
& THREAD_SET_THREAD_TOKEN
)
271 output
.append(ASCIIToUTF16("\tTHREAD_SET_THREAD_TOKEN\n"));
272 if (access
& THREAD_SUSPEND_RESUME
)
273 output
.append(ASCIIToUTF16("\tTHREAD_SUSPEND_RESUME\n"));
274 if (access
& THREAD_TERMINATE
)
275 output
.append(ASCIIToUTF16("\tTHREAD_TERMINATE\n"));
278 if (access
& FILE_APPEND_DATA
)
279 output
.append(ASCIIToUTF16("\tFILE_APPEND_DATA\n"));
280 if (access
& FILE_EXECUTE
)
281 output
.append(ASCIIToUTF16("\tFILE_EXECUTE\n"));
282 if (access
& FILE_READ_ATTRIBUTES
)
283 output
.append(ASCIIToUTF16("\tFILE_READ_ATTRIBUTES\n"));
284 if (access
& FILE_READ_DATA
)
285 output
.append(ASCIIToUTF16("\tFILE_READ_DATA\n"));
286 if (access
& FILE_READ_EA
)
287 output
.append(ASCIIToUTF16("\tFILE_READ_EA\n"));
288 if (access
& FILE_WRITE_ATTRIBUTES
)
289 output
.append(ASCIIToUTF16("\tFILE_WRITE_ATTRIBUTES\n"));
290 if (access
& FILE_WRITE_DATA
)
291 output
.append(ASCIIToUTF16("\tFILE_WRITE_DATA\n"));
292 if (access
& FILE_WRITE_EA
)
293 output
.append(ASCIIToUTF16("\tFILE_WRITE_EA\n"));
294 if (access
& FILE_WRITE_EA
)
295 output
.append(ASCIIToUTF16("\tFILE_WRITE_EA\n"));
297 case DirectoryHandle
:
298 if (access
& FILE_ADD_FILE
)
299 output
.append(ASCIIToUTF16("\tFILE_ADD_FILE\n"));
300 if (access
& FILE_ADD_SUBDIRECTORY
)
301 output
.append(ASCIIToUTF16("\tFILE_ADD_SUBDIRECTORY\n"));
302 if (access
& FILE_APPEND_DATA
)
303 output
.append(ASCIIToUTF16("\tFILE_APPEND_DATA\n"));
304 if (access
& FILE_DELETE_CHILD
)
305 output
.append(ASCIIToUTF16("\tFILE_DELETE_CHILD\n"));
306 if (access
& FILE_LIST_DIRECTORY
)
307 output
.append(ASCIIToUTF16("\tFILE_LIST_DIRECTORY\n"));
308 if (access
& FILE_READ_DATA
)
309 output
.append(ASCIIToUTF16("\tFILE_READ_DATA\n"));
310 if (access
& FILE_TRAVERSE
)
311 output
.append(ASCIIToUTF16("\tFILE_TRAVERSE\n"));
312 if (access
& FILE_WRITE_DATA
)
313 output
.append(ASCIIToUTF16("\tFILE_WRITE_DATA\n"));
316 if (access
& KEY_CREATE_LINK
)
317 output
.append(ASCIIToUTF16("\tKEY_CREATE_LINK\n"));
318 if (access
& KEY_CREATE_SUB_KEY
)
319 output
.append(ASCIIToUTF16("\tKEY_CREATE_SUB_KEY\n"));
320 if (access
& KEY_ENUMERATE_SUB_KEYS
)
321 output
.append(ASCIIToUTF16("\tKEY_ENUMERATE_SUB_KEYS\n"));
322 if (access
& KEY_EXECUTE
)
323 output
.append(ASCIIToUTF16("\tKEY_EXECUTE\n"));
324 if (access
& KEY_NOTIFY
)
325 output
.append(ASCIIToUTF16("\tKEY_NOTIFY\n"));
326 if (access
& KEY_QUERY_VALUE
)
327 output
.append(ASCIIToUTF16("\tKEY_QUERY_VALUE\n"));
328 if (access
& KEY_READ
)
329 output
.append(ASCIIToUTF16("\tKEY_READ\n"));
330 if (access
& KEY_SET_VALUE
)
331 output
.append(ASCIIToUTF16("\tKEY_SET_VALUE\n"));
332 if (access
& KEY_WOW64_32KEY
)
333 output
.append(ASCIIToUTF16("\tKEY_WOW64_32KEY\n"));
334 if (access
& KEY_WOW64_64KEY
)
335 output
.append(ASCIIToUTF16("\tKEY_WOW64_64KEY\n"));
337 case WindowStationHandle
:
338 if (access
& WINSTA_ACCESSCLIPBOARD
)
339 output
.append(ASCIIToUTF16("\tWINSTA_ACCESSCLIPBOARD\n"));
340 if (access
& WINSTA_ACCESSGLOBALATOMS
)
341 output
.append(ASCIIToUTF16("\tWINSTA_ACCESSGLOBALATOMS\n"));
342 if (access
& WINSTA_CREATEDESKTOP
)
343 output
.append(ASCIIToUTF16("\tWINSTA_CREATEDESKTOP\n"));
344 if (access
& WINSTA_ENUMDESKTOPS
)
345 output
.append(ASCIIToUTF16("\tWINSTA_ENUMDESKTOPS\n"));
346 if (access
& WINSTA_ENUMERATE
)
347 output
.append(ASCIIToUTF16("\tWINSTA_ENUMERATE\n"));
348 if (access
& WINSTA_EXITWINDOWS
)
349 output
.append(ASCIIToUTF16("\tWINSTA_EXITWINDOWS\n"));
350 if (access
& WINSTA_READATTRIBUTES
)
351 output
.append(ASCIIToUTF16("\tWINSTA_READATTRIBUTES\n"));
352 if (access
& WINSTA_READSCREEN
)
353 output
.append(ASCIIToUTF16("\tWINSTA_READSCREEN\n"));
354 if (access
& WINSTA_WRITEATTRIBUTES
)
355 output
.append(ASCIIToUTF16("\tWINSTA_WRITEATTRIBUTES\n"));
358 if (access
& DESKTOP_CREATEMENU
)
359 output
.append(ASCIIToUTF16("\tDESKTOP_CREATEMENU\n"));
360 if (access
& DESKTOP_CREATEWINDOW
)
361 output
.append(ASCIIToUTF16("\tDESKTOP_CREATEWINDOW\n"));
362 if (access
& DESKTOP_ENUMERATE
)
363 output
.append(ASCIIToUTF16("\tDESKTOP_ENUMERATE\n"));
364 if (access
& DESKTOP_HOOKCONTROL
)
365 output
.append(ASCIIToUTF16("\tDESKTOP_HOOKCONTROL\n"));
366 if (access
& DESKTOP_JOURNALPLAYBACK
)
367 output
.append(ASCIIToUTF16("\tDESKTOP_JOURNALPLAYBACK\n"));
368 if (access
& DESKTOP_JOURNALRECORD
)
369 output
.append(ASCIIToUTF16("\tDESKTOP_JOURNALRECORD\n"));
370 if (access
& DESKTOP_READOBJECTS
)
371 output
.append(ASCIIToUTF16("\tDESKTOP_READOBJECTS\n"));
372 if (access
& DESKTOP_SWITCHDESKTOP
)
373 output
.append(ASCIIToUTF16("\tDESKTOP_SWITCHDESKTOP\n"));
374 if (access
& DESKTOP_WRITEOBJECTS
)
375 output
.append(ASCIIToUTF16("\tDESKTOP_WRITEOBJECTS\n"));
378 if (access
& SC_MANAGER_CREATE_SERVICE
)
379 output
.append(ASCIIToUTF16("\tSC_MANAGER_CREATE_SERVICE\n"));
380 if (access
& SC_MANAGER_CONNECT
)
381 output
.append(ASCIIToUTF16("\tSC_MANAGER_CONNECT\n"));
382 if (access
& SC_MANAGER_ENUMERATE_SERVICE
)
383 output
.append(ASCIIToUTF16("\tSC_MANAGER_ENUMERATE_SERVICE\n"));
384 if (access
& SC_MANAGER_LOCK
)
385 output
.append(ASCIIToUTF16("\tSC_MANAGER_LOCK\n"));
386 if (access
& SC_MANAGER_MODIFY_BOOT_CONFIG
)
387 output
.append(ASCIIToUTF16("\tSC_MANAGER_MODIFY_BOOT_CONFIG\n"));
388 if (access
& SC_MANAGER_QUERY_LOCK_STATUS
)
389 output
.append(ASCIIToUTF16("\tSC_MANAGER_QUERY_LOCK_STATUS\n"));
392 if (access
& EVENT_MODIFY_STATE
)
393 output
.append(ASCIIToUTF16("\tEVENT_MODIFY_STATE\n"));
396 if (access
& MUTEX_MODIFY_STATE
)
397 output
.append(ASCIIToUTF16("\tMUTEX_MODIFY_STATE\n"));
399 case SemaphoreHandle
:
400 if (access
& SEMAPHORE_MODIFY_STATE
)
401 output
.append(ASCIIToUTF16("\tSEMAPHORE_MODIFY_STATE\n"));
404 if (access
& TIMER_MODIFY_STATE
)
405 output
.append(ASCIIToUTF16("\tTIMER_MODIFY_STATE\n"));
406 if (access
& TIMER_QUERY_STATE
)
407 output
.append(ASCIIToUTF16("\tTIMER_QUERY_STATE\n"));
409 case NamedPipeHandle
:
410 if (access
& PIPE_ACCESS_INBOUND
)
411 output
.append(ASCIIToUTF16("\tPIPE_ACCESS_INBOUND\n"));
412 if (access
& PIPE_ACCESS_OUTBOUND
)
413 output
.append(ASCIIToUTF16("\tPIPE_ACCESS_OUTBOUND\n"));
416 if (access
& JOB_OBJECT_ASSIGN_PROCESS
)
417 output
.append(ASCIIToUTF16("\tJOB_OBJECT_ASSIGN_PROCESS\n"));
418 if (access
& JOB_OBJECT_QUERY
)
419 output
.append(ASCIIToUTF16("\tJOB_OBJECT_QUERY\n"));
420 if (access
& JOB_OBJECT_SET_ATTRIBUTES
)
421 output
.append(ASCIIToUTF16("\tJOB_OBJECT_SET_ATTRIBUTES\n"));
422 if (access
& JOB_OBJECT_SET_SECURITY_ATTRIBUTES
)
423 output
.append(ASCIIToUTF16("\tJOB_OBJECT_SET_SECURITY_ATTRIBUTES\n"));
424 if (access
& JOB_OBJECT_TERMINATE
)
425 output
.append(ASCIIToUTF16("\tJOB_OBJECT_TERMINATE\n"));
428 if (access
& FILE_MAP_EXECUTE
)
429 output
.append(ASCIIToUTF16("\tFILE_MAP_EXECUTE\n"));
430 if (access
& FILE_MAP_READ
)
431 output
.append(ASCIIToUTF16("\tFILE_MAP_READ\n"));
432 if (access
& FILE_MAP_WRITE
)
433 output
.append(ASCIIToUTF16("\tFILE_MAP_WRITE\n"));
439 } // namespace content