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/common/handle_enumerator_win.h"
10 #include "base/command_line.h"
11 #include "base/logging.h"
12 #include "base/process.h"
13 #include "base/process_util.h"
14 #include "base/utf_string_conversions.h"
15 #include "base/win/windows_version.h"
16 #include "content/public/common/content_switches.h"
17 #include "content/public/common/result_codes.h"
18 #include "sandbox/win/src/handle_table.h"
23 typedef std::map
<const string16
, HandleType
> HandleTypeMap
;
25 HandleTypeMap
& MakeHandleTypeMap() {
26 HandleTypeMap
& handle_types
= *(new HandleTypeMap());
27 handle_types
[sandbox::HandleTable::kTypeProcess
] = ProcessHandle
;
28 handle_types
[sandbox::HandleTable::kTypeThread
] = ThreadHandle
;
29 handle_types
[sandbox::HandleTable::kTypeFile
] = FileHandle
;
30 handle_types
[sandbox::HandleTable::kTypeDirectory
] = DirectoryHandle
;
31 handle_types
[sandbox::HandleTable::kTypeKey
] = KeyHandle
;
32 handle_types
[sandbox::HandleTable::kTypeWindowStation
] = WindowStationHandle
;
33 handle_types
[sandbox::HandleTable::kTypeDesktop
] = DesktopHandle
;
34 handle_types
[sandbox::HandleTable::kTypeService
] = ServiceHandle
;
35 handle_types
[sandbox::HandleTable::kTypeMutex
] = MutexHandle
;
36 handle_types
[sandbox::HandleTable::kTypeSemaphore
] = SemaphoreHandle
;
37 handle_types
[sandbox::HandleTable::kTypeEvent
] = EventHandle
;
38 handle_types
[sandbox::HandleTable::kTypeTimer
] = TimerHandle
;
39 handle_types
[sandbox::HandleTable::kTypeNamedPipe
] = NamedPipeHandle
;
40 handle_types
[sandbox::HandleTable::kTypeJobObject
] = JobHandle
;
41 handle_types
[sandbox::HandleTable::kTypeFileMap
] = FileMapHandle
;
42 handle_types
[sandbox::HandleTable::kTypeAlpcPort
] = AlpcPortHandle
;
49 const size_t kMaxHandleNameLength
= 1024;
51 void HandleEnumerator::EnumerateHandles() {
52 sandbox::HandleTable handles
;
53 std::string process_type
=
54 CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
55 switches::kProcessType
);
56 string16 output
= ASCIIToUTF16(process_type
);
57 output
.append(ASCIIToUTF16(" process - Handles at shutdown:\n"));
58 for (sandbox::HandleTable::Iterator sys_handle
59 = handles
.HandlesForProcess(::GetCurrentProcessId());
60 sys_handle
!= handles
.end(); ++sys_handle
) {
61 HandleType current_type
= StringToHandleType(sys_handle
->Type());
62 if (!all_handles_
&& (current_type
!= ProcessHandle
&&
63 current_type
!= FileHandle
&&
64 current_type
!= DirectoryHandle
&&
65 current_type
!= KeyHandle
&&
66 current_type
!= WindowStationHandle
&&
67 current_type
!= DesktopHandle
&&
68 current_type
!= ServiceHandle
))
71 output
+= ASCIIToUTF16("[");
72 output
+= sys_handle
->Type();
73 output
+= ASCIIToUTF16("] (");
74 output
+= sys_handle
->Name();
75 output
+= ASCIIToUTF16(")\n");
76 output
+= GetAccessString(current_type
,
77 sys_handle
->handle_entry()->GrantedAccess
);
82 HandleType
StringToHandleType(const string16
& type
) {
83 static HandleTypeMap handle_types
= MakeHandleTypeMap();
84 HandleTypeMap::iterator result
= handle_types
.find(type
);
85 return result
!= handle_types
.end() ? result
->second
: OtherHandle
;
88 string16
GetAccessString(HandleType handle_type
,
91 if (access
& GENERIC_READ
)
92 output
.append(ASCIIToUTF16("\tGENERIC_READ\n"));
93 if (access
& GENERIC_WRITE
)
94 output
.append(ASCIIToUTF16("\tGENERIC_WRITE\n"));
95 if (access
& GENERIC_EXECUTE
)
96 output
.append(ASCIIToUTF16("\tGENERIC_EXECUTE\n"));
97 if (access
& GENERIC_ALL
)
98 output
.append(ASCIIToUTF16("\tGENERIC_ALL\n"));
100 output
.append(ASCIIToUTF16("\tDELETE\n"));
101 if (access
& READ_CONTROL
)
102 output
.append(ASCIIToUTF16("\tREAD_CONTROL\n"));
103 if (access
& WRITE_DAC
)
104 output
.append(ASCIIToUTF16("\tWRITE_DAC\n"));
105 if (access
& WRITE_OWNER
)
106 output
.append(ASCIIToUTF16("\tWRITE_OWNER\n"));
107 if (access
& SYNCHRONIZE
)
108 output
.append(ASCIIToUTF16("\tSYNCHRONIZE\n"));
110 switch (handle_type
) {
112 if (access
& PROCESS_CREATE_PROCESS
)
113 output
.append(ASCIIToUTF16("\tPROCESS_CREATE_PROCESS\n"));
114 if (access
& PROCESS_CREATE_THREAD
)
115 output
.append(ASCIIToUTF16("\tPROCESS_CREATE_THREAD\n"));
116 if (access
& PROCESS_DUP_HANDLE
)
117 output
.append(ASCIIToUTF16("\tPROCESS_DUP_HANDLE\n"));
118 if (access
& PROCESS_QUERY_INFORMATION
)
119 output
.append(ASCIIToUTF16("\tPROCESS_QUERY_INFORMATION\n"));
120 if (access
& PROCESS_QUERY_LIMITED_INFORMATION
)
121 output
.append(ASCIIToUTF16("\tPROCESS_QUERY_LIMITED_INFORMATION\n"));
122 if (access
& PROCESS_SET_INFORMATION
)
123 output
.append(ASCIIToUTF16("\tPROCESS_SET_INFORMATION\n"));
124 if (access
& PROCESS_SET_QUOTA
)
125 output
.append(ASCIIToUTF16("\tPROCESS_SET_QUOTA\n"));
126 if (access
& PROCESS_SUSPEND_RESUME
)
127 output
.append(ASCIIToUTF16("\tPROCESS_SUSPEND_RESUME\n"));
128 if (access
& PROCESS_TERMINATE
)
129 output
.append(ASCIIToUTF16("\tPROCESS_TERMINATE\n"));
130 if (access
& PROCESS_VM_OPERATION
)
131 output
.append(ASCIIToUTF16("\tPROCESS_VM_OPERATION\n"));
132 if (access
& PROCESS_VM_READ
)
133 output
.append(ASCIIToUTF16("\tPROCESS_VM_READ\n"));
134 if (access
& PROCESS_VM_WRITE
)
135 output
.append(ASCIIToUTF16("\tPROCESS_VM_WRITE\n"));
138 if (access
& THREAD_DIRECT_IMPERSONATION
)
139 output
.append(ASCIIToUTF16("\tTHREAD_DIRECT_IMPERSONATION\n"));
140 if (access
& THREAD_GET_CONTEXT
)
141 output
.append(ASCIIToUTF16("\tTHREAD_GET_CONTEXT\n"));
142 if (access
& THREAD_IMPERSONATE
)
143 output
.append(ASCIIToUTF16("\tTHREAD_IMPERSONATE\n"));
144 if (access
& THREAD_QUERY_INFORMATION
)
145 output
.append(ASCIIToUTF16("\tTHREAD_QUERY_INFORMATION\n"));
146 if (access
& THREAD_QUERY_LIMITED_INFORMATION
)
147 output
.append(ASCIIToUTF16("\tTHREAD_QUERY_LIMITED_INFORMATION\n"));
148 if (access
& THREAD_SET_CONTEXT
)
149 output
.append(ASCIIToUTF16("\tTHREAD_SET_CONTEXT\n"));
150 if (access
& THREAD_SET_INFORMATION
)
151 output
.append(ASCIIToUTF16("\tTHREAD_SET_INFORMATION\n"));
152 if (access
& THREAD_SET_LIMITED_INFORMATION
)
153 output
.append(ASCIIToUTF16("\tTHREAD_SET_LIMITED_INFORMATION\n"));
154 if (access
& THREAD_SET_THREAD_TOKEN
)
155 output
.append(ASCIIToUTF16("\tTHREAD_SET_THREAD_TOKEN\n"));
156 if (access
& THREAD_SUSPEND_RESUME
)
157 output
.append(ASCIIToUTF16("\tTHREAD_SUSPEND_RESUME\n"));
158 if (access
& THREAD_TERMINATE
)
159 output
.append(ASCIIToUTF16("\tTHREAD_TERMINATE\n"));
162 if (access
& FILE_APPEND_DATA
)
163 output
.append(ASCIIToUTF16("\tFILE_APPEND_DATA\n"));
164 if (access
& FILE_EXECUTE
)
165 output
.append(ASCIIToUTF16("\tFILE_EXECUTE\n"));
166 if (access
& FILE_READ_ATTRIBUTES
)
167 output
.append(ASCIIToUTF16("\tFILE_READ_ATTRIBUTES\n"));
168 if (access
& FILE_READ_DATA
)
169 output
.append(ASCIIToUTF16("\tFILE_READ_DATA\n"));
170 if (access
& FILE_READ_EA
)
171 output
.append(ASCIIToUTF16("\tFILE_READ_EA\n"));
172 if (access
& FILE_WRITE_ATTRIBUTES
)
173 output
.append(ASCIIToUTF16("\tFILE_WRITE_ATTRIBUTES\n"));
174 if (access
& FILE_WRITE_DATA
)
175 output
.append(ASCIIToUTF16("\tFILE_WRITE_DATA\n"));
176 if (access
& FILE_WRITE_EA
)
177 output
.append(ASCIIToUTF16("\tFILE_WRITE_EA\n"));
178 if (access
& FILE_WRITE_EA
)
179 output
.append(ASCIIToUTF16("\tFILE_WRITE_EA\n"));
181 case DirectoryHandle
:
182 if (access
& FILE_ADD_FILE
)
183 output
.append(ASCIIToUTF16("\tFILE_ADD_FILE\n"));
184 if (access
& FILE_ADD_SUBDIRECTORY
)
185 output
.append(ASCIIToUTF16("\tFILE_ADD_SUBDIRECTORY\n"));
186 if (access
& FILE_APPEND_DATA
)
187 output
.append(ASCIIToUTF16("\tFILE_APPEND_DATA\n"));
188 if (access
& FILE_DELETE_CHILD
)
189 output
.append(ASCIIToUTF16("\tFILE_DELETE_CHILD\n"));
190 if (access
& FILE_LIST_DIRECTORY
)
191 output
.append(ASCIIToUTF16("\tFILE_LIST_DIRECTORY\n"));
192 if (access
& FILE_READ_DATA
)
193 output
.append(ASCIIToUTF16("\tFILE_READ_DATA\n"));
194 if (access
& FILE_TRAVERSE
)
195 output
.append(ASCIIToUTF16("\tFILE_TRAVERSE\n"));
196 if (access
& FILE_WRITE_DATA
)
197 output
.append(ASCIIToUTF16("\tFILE_WRITE_DATA\n"));
200 if (access
& KEY_CREATE_LINK
)
201 output
.append(ASCIIToUTF16("\tKEY_CREATE_LINK\n"));
202 if (access
& KEY_CREATE_SUB_KEY
)
203 output
.append(ASCIIToUTF16("\tKEY_CREATE_SUB_KEY\n"));
204 if (access
& KEY_ENUMERATE_SUB_KEYS
)
205 output
.append(ASCIIToUTF16("\tKEY_ENUMERATE_SUB_KEYS\n"));
206 if (access
& KEY_EXECUTE
)
207 output
.append(ASCIIToUTF16("\tKEY_EXECUTE\n"));
208 if (access
& KEY_NOTIFY
)
209 output
.append(ASCIIToUTF16("\tKEY_NOTIFY\n"));
210 if (access
& KEY_QUERY_VALUE
)
211 output
.append(ASCIIToUTF16("\tKEY_QUERY_VALUE\n"));
212 if (access
& KEY_READ
)
213 output
.append(ASCIIToUTF16("\tKEY_READ\n"));
214 if (access
& KEY_SET_VALUE
)
215 output
.append(ASCIIToUTF16("\tKEY_SET_VALUE\n"));
216 if (access
& KEY_WOW64_32KEY
)
217 output
.append(ASCIIToUTF16("\tKEY_WOW64_32KEY\n"));
218 if (access
& KEY_WOW64_64KEY
)
219 output
.append(ASCIIToUTF16("\tKEY_WOW64_64KEY\n"));
221 case WindowStationHandle
:
222 if (access
& WINSTA_ACCESSCLIPBOARD
)
223 output
.append(ASCIIToUTF16("\tWINSTA_ACCESSCLIPBOARD\n"));
224 if (access
& WINSTA_ACCESSGLOBALATOMS
)
225 output
.append(ASCIIToUTF16("\tWINSTA_ACCESSGLOBALATOMS\n"));
226 if (access
& WINSTA_CREATEDESKTOP
)
227 output
.append(ASCIIToUTF16("\tWINSTA_CREATEDESKTOP\n"));
228 if (access
& WINSTA_ENUMDESKTOPS
)
229 output
.append(ASCIIToUTF16("\tWINSTA_ENUMDESKTOPS\n"));
230 if (access
& WINSTA_ENUMERATE
)
231 output
.append(ASCIIToUTF16("\tWINSTA_ENUMERATE\n"));
232 if (access
& WINSTA_EXITWINDOWS
)
233 output
.append(ASCIIToUTF16("\tWINSTA_EXITWINDOWS\n"));
234 if (access
& WINSTA_READATTRIBUTES
)
235 output
.append(ASCIIToUTF16("\tWINSTA_READATTRIBUTES\n"));
236 if (access
& WINSTA_READSCREEN
)
237 output
.append(ASCIIToUTF16("\tWINSTA_READSCREEN\n"));
238 if (access
& WINSTA_WRITEATTRIBUTES
)
239 output
.append(ASCIIToUTF16("\tWINSTA_WRITEATTRIBUTES\n"));
242 if (access
& DESKTOP_CREATEMENU
)
243 output
.append(ASCIIToUTF16("\tDESKTOP_CREATEMENU\n"));
244 if (access
& DESKTOP_CREATEWINDOW
)
245 output
.append(ASCIIToUTF16("\tDESKTOP_CREATEWINDOW\n"));
246 if (access
& DESKTOP_ENUMERATE
)
247 output
.append(ASCIIToUTF16("\tDESKTOP_ENUMERATE\n"));
248 if (access
& DESKTOP_HOOKCONTROL
)
249 output
.append(ASCIIToUTF16("\tDESKTOP_HOOKCONTROL\n"));
250 if (access
& DESKTOP_JOURNALPLAYBACK
)
251 output
.append(ASCIIToUTF16("\tDESKTOP_JOURNALPLAYBACK\n"));
252 if (access
& DESKTOP_JOURNALRECORD
)
253 output
.append(ASCIIToUTF16("\tDESKTOP_JOURNALRECORD\n"));
254 if (access
& DESKTOP_READOBJECTS
)
255 output
.append(ASCIIToUTF16("\tDESKTOP_READOBJECTS\n"));
256 if (access
& DESKTOP_SWITCHDESKTOP
)
257 output
.append(ASCIIToUTF16("\tDESKTOP_SWITCHDESKTOP\n"));
258 if (access
& DESKTOP_WRITEOBJECTS
)
259 output
.append(ASCIIToUTF16("\tDESKTOP_WRITEOBJECTS\n"));
262 if (access
& SC_MANAGER_CREATE_SERVICE
)
263 output
.append(ASCIIToUTF16("\tSC_MANAGER_CREATE_SERVICE\n"));
264 if (access
& SC_MANAGER_CONNECT
)
265 output
.append(ASCIIToUTF16("\tSC_MANAGER_CONNECT\n"));
266 if (access
& SC_MANAGER_ENUMERATE_SERVICE
)
267 output
.append(ASCIIToUTF16("\tSC_MANAGER_ENUMERATE_SERVICE\n"));
268 if (access
& SC_MANAGER_LOCK
)
269 output
.append(ASCIIToUTF16("\tSC_MANAGER_LOCK\n"));
270 if (access
& SC_MANAGER_MODIFY_BOOT_CONFIG
)
271 output
.append(ASCIIToUTF16("\tSC_MANAGER_MODIFY_BOOT_CONFIG\n"));
272 if (access
& SC_MANAGER_QUERY_LOCK_STATUS
)
273 output
.append(ASCIIToUTF16("\tSC_MANAGER_QUERY_LOCK_STATUS\n"));
276 if (access
& EVENT_MODIFY_STATE
)
277 output
.append(ASCIIToUTF16("\tEVENT_MODIFY_STATE\n"));
280 if (access
& MUTEX_MODIFY_STATE
)
281 output
.append(ASCIIToUTF16("\tMUTEX_MODIFY_STATE\n"));
283 case SemaphoreHandle
:
284 if (access
& SEMAPHORE_MODIFY_STATE
)
285 output
.append(ASCIIToUTF16("\tSEMAPHORE_MODIFY_STATE\n"));
288 if (access
& TIMER_MODIFY_STATE
)
289 output
.append(ASCIIToUTF16("\tTIMER_MODIFY_STATE\n"));
290 if (access
& TIMER_QUERY_STATE
)
291 output
.append(ASCIIToUTF16("\tTIMER_QUERY_STATE\n"));
293 case NamedPipeHandle
:
294 if (access
& PIPE_ACCESS_INBOUND
)
295 output
.append(ASCIIToUTF16("\tPIPE_ACCESS_INBOUND\n"));
296 if (access
& PIPE_ACCESS_OUTBOUND
)
297 output
.append(ASCIIToUTF16("\tPIPE_ACCESS_OUTBOUND\n"));
300 if (access
& JOB_OBJECT_ASSIGN_PROCESS
)
301 output
.append(ASCIIToUTF16("\tJOB_OBJECT_ASSIGN_PROCESS\n"));
302 if (access
& JOB_OBJECT_QUERY
)
303 output
.append(ASCIIToUTF16("\tJOB_OBJECT_QUERY\n"));
304 if (access
& JOB_OBJECT_SET_ATTRIBUTES
)
305 output
.append(ASCIIToUTF16("\tJOB_OBJECT_SET_ATTRIBUTES\n"));
306 if (access
& JOB_OBJECT_SET_SECURITY_ATTRIBUTES
)
307 output
.append(ASCIIToUTF16("\tJOB_OBJECT_SET_SECURITY_ATTRIBUTES\n"));
308 if (access
& JOB_OBJECT_TERMINATE
)
309 output
.append(ASCIIToUTF16("\tJOB_OBJECT_TERMINATE\n"));
312 if (access
& FILE_MAP_EXECUTE
)
313 output
.append(ASCIIToUTF16("\tFILE_MAP_EXECUTE\n"));
314 if (access
& FILE_MAP_READ
)
315 output
.append(ASCIIToUTF16("\tFILE_MAP_READ\n"));
316 if (access
& FILE_MAP_WRITE
)
317 output
.append(ASCIIToUTF16("\tFILE_MAP_WRITE\n"));
323 } // namespace content