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 "remoting/host/win/launch_process_with_token.h" | 5 #include "remoting/host/win/launch_process_with_token.h" |
6 | 6 |
7 #include <windows.h> | 7 #include <windows.h> |
8 #include <stddef.h> | |
9 #include <winternl.h> | |
10 | 8 |
11 #include <limits> | |
12 #include <memory> | |
13 #include <utility> | 9 #include <utility> |
14 | 10 |
15 #include "base/logging.h" | 11 #include "base/logging.h" |
16 #include "base/rand_util.h" | |
17 #include "base/scoped_native_library.h" | |
18 #include "base/strings/string16.h" | |
19 #include "base/strings/stringprintf.h" | |
20 #include "base/strings/utf_string_conversions.h" | |
21 #include "base/win/scoped_handle.h" | 12 #include "base/win/scoped_handle.h" |
22 #include "base/win/scoped_process_information.h" | 13 #include "base/win/scoped_process_information.h" |
23 #include "base/win/windows_version.h" | |
24 | 14 |
25 using base::win::ScopedHandle; | 15 using base::win::ScopedHandle; |
26 | 16 |
27 namespace { | 17 namespace { |
28 | 18 |
29 const char kCreateProcessDefaultPipeNameFormat[] = | |
30 "\\\\.\\Pipe\\TerminalServer\\SystemExecSrvr\\%d"; | |
31 | |
32 // Undocumented WINSTATIONINFOCLASS value causing | |
33 // winsta!WinStationQueryInformationW() to return the name of the pipe for | |
34 // requesting cross-session process creation. | |
35 const WINSTATIONINFOCLASS kCreateProcessPipeNameClass = | |
36 static_cast<WINSTATIONINFOCLASS>(0x21); | |
37 | |
38 const int kPipeBusyWaitTimeoutMs = 2000; | |
39 const int kPipeConnectMaxAttempts = 3; | |
40 | |
41 // Terminates the process and closes process and thread handles in | |
42 // |process_information| structure. | |
43 void CloseHandlesAndTerminateProcess(PROCESS_INFORMATION* process_information) { | |
44 if (process_information->hThread) { | |
45 CloseHandle(process_information->hThread); | |
46 process_information->hThread = nullptr; | |
47 } | |
48 | |
49 if (process_information->hProcess) { | |
50 TerminateProcess(process_information->hProcess, CONTROL_C_EXIT); | |
51 CloseHandle(process_information->hProcess); | |
52 process_information->hProcess = nullptr; | |
53 } | |
54 } | |
55 | |
56 // Connects to the executor server corresponding to |session_id|. | |
57 bool ConnectToExecutionServer(uint32_t session_id, | |
58 base::win::ScopedHandle* pipe_out) { | |
59 base::string16 pipe_name; | |
60 | |
61 // Use winsta!WinStationQueryInformationW() to determine the process creation | |
62 // pipe name for the session. | |
63 base::FilePath winsta_path( | |
64 base::GetNativeLibraryName(base::UTF8ToUTF16("winsta"))); | |
65 base::ScopedNativeLibrary winsta(winsta_path); | |
66 if (winsta.is_valid()) { | |
67 PWINSTATIONQUERYINFORMATIONW win_station_query_information = | |
68 reinterpret_cast<PWINSTATIONQUERYINFORMATIONW>( | |
69 winsta.GetFunctionPointer("WinStationQueryInformationW")); | |
70 if (win_station_query_information) { | |
71 wchar_t name[MAX_PATH]; | |
72 ULONG name_length; | |
73 if (win_station_query_information(0, | |
74 session_id, | |
75 kCreateProcessPipeNameClass, | |
76 name, | |
77 sizeof(name), | |
78 &name_length)) { | |
79 pipe_name.assign(name); | |
80 } | |
81 } | |
82 } | |
83 | |
84 // Use the default pipe name if we couldn't query its name. | |
85 if (pipe_name.empty()) { | |
86 pipe_name = base::UTF8ToUTF16( | |
87 base::StringPrintf(kCreateProcessDefaultPipeNameFormat, session_id)); | |
88 } | |
89 | |
90 // Try to connect to the named pipe. | |
91 base::win::ScopedHandle pipe; | |
92 for (int i = 0; i < kPipeConnectMaxAttempts; ++i) { | |
93 pipe.Set(CreateFile(pipe_name.c_str(), | |
94 GENERIC_READ | GENERIC_WRITE, | |
95 0, | |
96 nullptr, | |
97 OPEN_EXISTING, | |
98 0, | |
99 nullptr)); | |
100 if (pipe.IsValid()) { | |
101 break; | |
102 } | |
103 | |
104 // Cannot continue retrying if error is something other than | |
105 // ERROR_PIPE_BUSY. | |
106 if (GetLastError() != ERROR_PIPE_BUSY) { | |
107 break; | |
108 } | |
109 | |
110 // Cannot continue retrying if wait on pipe fails. | |
111 if (!WaitNamedPipe(pipe_name.c_str(), kPipeBusyWaitTimeoutMs)) { | |
112 break; | |
113 } | |
114 } | |
115 | |
116 if (!pipe.IsValid()) { | |
117 PLOG(ERROR) << "Failed to connect to '" << pipe_name << "'"; | |
118 return false; | |
119 } | |
120 | |
121 *pipe_out = std::move(pipe); | |
122 return true; | |
123 } | |
124 | |
125 // Copies the process token making it a primary impersonation token. | 19 // Copies the process token making it a primary impersonation token. |
126 // The returned handle will have |desired_access| rights. | 20 // The returned handle will have |desired_access| rights. |
127 bool CopyProcessToken(DWORD desired_access, ScopedHandle* token_out) { | 21 bool CopyProcessToken(DWORD desired_access, ScopedHandle* token_out) { |
128 HANDLE temp_handle; | 22 HANDLE temp_handle; |
129 if (!OpenProcessToken(GetCurrentProcess(), | 23 if (!OpenProcessToken(GetCurrentProcess(), |
130 TOKEN_DUPLICATE | desired_access, | 24 TOKEN_DUPLICATE | desired_access, |
131 &temp_handle)) { | 25 &temp_handle)) { |
132 PLOG(ERROR) << "Failed to open process token"; | 26 PLOG(ERROR) << "Failed to open process token"; |
133 return false; | 27 return false; |
134 } | 28 } |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
170 if (!AdjustTokenPrivileges(privileged_token.Get(), FALSE, &state, 0, nullptr, | 64 if (!AdjustTokenPrivileges(privileged_token.Get(), FALSE, &state, 0, nullptr, |
171 0)) { | 65 0)) { |
172 PLOG(ERROR) << "Failed to enable SE_TCB_NAME privilege in a token"; | 66 PLOG(ERROR) << "Failed to enable SE_TCB_NAME privilege in a token"; |
173 return false; | 67 return false; |
174 } | 68 } |
175 | 69 |
176 *token_out = std::move(privileged_token); | 70 *token_out = std::move(privileged_token); |
177 return true; | 71 return true; |
178 } | 72 } |
179 | 73 |
180 // Fills the process and thread handles in the passed |process_information| | |
181 // structure and resume the process if the caller didn't want to suspend it. | |
182 bool ProcessCreateProcessResponse(DWORD creation_flags, | |
183 PROCESS_INFORMATION* process_information) { | |
184 // The execution server does not return handles to the created process and | |
185 // thread. | |
186 if (!process_information->hProcess) { | |
187 // N.B. PROCESS_ALL_ACCESS is different in XP and Vista+ versions of | |
188 // the SDK. |desired_access| below is effectively PROCESS_ALL_ACCESS from | |
189 // the XP version of the SDK. | |
190 DWORD desired_access = | |
191 STANDARD_RIGHTS_REQUIRED | | |
192 SYNCHRONIZE | | |
193 PROCESS_TERMINATE | | |
194 PROCESS_CREATE_THREAD | | |
195 PROCESS_SET_SESSIONID | | |
196 PROCESS_VM_OPERATION | | |
197 PROCESS_VM_READ | | |
198 PROCESS_VM_WRITE | | |
199 PROCESS_DUP_HANDLE | | |
200 PROCESS_CREATE_PROCESS | | |
201 PROCESS_SET_QUOTA | | |
202 PROCESS_SET_INFORMATION | | |
203 PROCESS_QUERY_INFORMATION | | |
204 PROCESS_SUSPEND_RESUME; | |
205 process_information->hProcess = | |
206 OpenProcess(desired_access, | |
207 FALSE, | |
208 process_information->dwProcessId); | |
209 if (!process_information->hProcess) { | |
210 PLOG(ERROR) << "Failed to open the process " | |
211 << process_information->dwProcessId; | |
212 return false; | |
213 } | |
214 } | |
215 | |
216 if (!process_information->hThread) { | |
217 // N.B. THREAD_ALL_ACCESS is different in XP and Vista+ versions of | |
218 // the SDK. |desired_access| below is effectively THREAD_ALL_ACCESS from | |
219 // the XP version of the SDK. | |
220 DWORD desired_access = | |
221 STANDARD_RIGHTS_REQUIRED | | |
222 SYNCHRONIZE | | |
223 THREAD_TERMINATE | | |
224 THREAD_SUSPEND_RESUME | | |
225 THREAD_GET_CONTEXT | | |
226 THREAD_SET_CONTEXT | | |
227 THREAD_QUERY_INFORMATION | | |
228 THREAD_SET_INFORMATION | | |
229 THREAD_SET_THREAD_TOKEN | | |
230 THREAD_IMPERSONATE | | |
231 THREAD_DIRECT_IMPERSONATION; | |
232 process_information->hThread = | |
233 OpenThread(desired_access, | |
234 FALSE, | |
235 process_information->dwThreadId); | |
236 if (!process_information->hThread) { | |
237 PLOG(ERROR) << "Failed to open the thread " | |
238 << process_information->dwThreadId; | |
239 return false; | |
240 } | |
241 } | |
242 | |
243 // Resume the thread if the caller didn't want to suspend the process. | |
244 if ((creation_flags & CREATE_SUSPENDED) == 0) { | |
245 if (!ResumeThread(process_information->hThread)) { | |
246 PLOG(ERROR) << "Failed to resume the thread " | |
247 << process_information->dwThreadId; | |
248 return false; | |
249 } | |
250 } | |
251 | |
252 return true; | |
253 } | |
254 | |
255 // Receives the response to a remote process create request. | |
256 bool ReceiveCreateProcessResponse( | |
257 HANDLE pipe, | |
258 PROCESS_INFORMATION* process_information_out) { | |
259 struct CreateProcessResponse { | |
260 DWORD size; | |
261 BOOL success; | |
262 DWORD last_error; | |
263 PROCESS_INFORMATION process_information; | |
264 }; | |
265 | |
266 DWORD bytes; | |
267 CreateProcessResponse response; | |
268 if (!ReadFile(pipe, &response, sizeof(response), &bytes, nullptr)) { | |
269 PLOG(ERROR) << "Failed to receive CreateProcessAsUser response"; | |
270 return false; | |
271 } | |
272 | |
273 // The server sends the data in one chunk so if we didn't received a complete | |
274 // answer something bad happend and there is no point in retrying. | |
275 if (bytes != sizeof(response)) { | |
276 SetLastError(ERROR_RECEIVE_PARTIAL); | |
277 return false; | |
278 } | |
279 | |
280 if (!response.success) { | |
281 SetLastError(response.last_error); | |
282 return false; | |
283 } | |
284 | |
285 *process_information_out = response.process_information; | |
286 return true; | |
287 } | |
288 | |
289 // Sends a remote process create request to the execution server. | |
290 bool SendCreateProcessRequest( | |
291 HANDLE pipe, | |
292 const base::FilePath::StringType& application_name, | |
293 const base::CommandLine::StringType& command_line, | |
294 DWORD creation_flags, | |
295 const base::char16* desktop_name) { | |
296 // |CreateProcessRequest| structure passes the same parameters to | |
297 // the execution server as CreateProcessAsUser() function does. Strings are | |
298 // stored as wide strings immediately after the structure. String pointers are | |
299 // represented as byte offsets to string data from the beginning of | |
300 // the structure. | |
301 struct CreateProcessRequest { | |
302 DWORD size; | |
303 DWORD process_id; | |
304 BOOL use_default_token; | |
305 HANDLE token; | |
306 LPWSTR application_name; | |
307 LPWSTR command_line; | |
308 SECURITY_ATTRIBUTES process_attributes; | |
309 SECURITY_ATTRIBUTES thread_attributes; | |
310 BOOL inherit_handles; | |
311 DWORD creation_flags; | |
312 LPVOID environment; | |
313 LPWSTR current_directory; | |
314 STARTUPINFOW startup_info; | |
315 PROCESS_INFORMATION process_information; | |
316 }; | |
317 | |
318 base::string16 desktop; | |
319 if (desktop_name) | |
320 desktop = desktop_name; | |
321 | |
322 // Allocate a large enough buffer to hold the CreateProcessRequest structure | |
323 // and three nullptr-terminated string parameters. | |
324 size_t size = sizeof(CreateProcessRequest) + sizeof(wchar_t) * | |
325 (application_name.size() + command_line.size() + desktop.size() + 3); | |
326 std::unique_ptr<char[]> buffer(new char[size]); | |
327 memset(buffer.get(), 0, size); | |
328 | |
329 // Marshal the input parameters. | |
330 CreateProcessRequest* request = | |
331 reinterpret_cast<CreateProcessRequest*>(buffer.get()); | |
332 request->size = size; | |
333 request->process_id = GetCurrentProcessId(); | |
334 request->use_default_token = TRUE; | |
335 // Always pass CREATE_SUSPENDED to avoid a race between the created process | |
336 // exiting too soon and OpenProcess() call below. | |
337 request->creation_flags = creation_flags | CREATE_SUSPENDED; | |
338 request->startup_info.cb = sizeof(request->startup_info); | |
339 | |
340 size_t buffer_offset = sizeof(CreateProcessRequest); | |
341 | |
342 request->application_name = reinterpret_cast<LPWSTR>(buffer_offset); | |
343 std::copy(application_name.begin(), | |
344 application_name.end(), | |
345 reinterpret_cast<wchar_t*>(buffer.get() + buffer_offset)); | |
346 buffer_offset += (application_name.size() + 1) * sizeof(wchar_t); | |
347 | |
348 request->command_line = reinterpret_cast<LPWSTR>(buffer_offset); | |
349 std::copy(command_line.begin(), | |
350 command_line.end(), | |
351 reinterpret_cast<wchar_t*>(buffer.get() + buffer_offset)); | |
352 buffer_offset += (command_line.size() + 1) * sizeof(wchar_t); | |
353 | |
354 request->startup_info.lpDesktop = | |
355 reinterpret_cast<LPWSTR>(buffer_offset); | |
356 std::copy(desktop.begin(), | |
357 desktop.end(), | |
358 reinterpret_cast<wchar_t*>(buffer.get() + buffer_offset)); | |
359 | |
360 // Pass the request to create a process in the target session. | |
361 DWORD bytes; | |
362 if (!WriteFile(pipe, buffer.get(), size, &bytes, nullptr)) { | |
363 PLOG(ERROR) << "Failed to send CreateProcessAsUser request"; | |
364 return false; | |
365 } | |
366 | |
367 return true; | |
368 } | |
369 | |
370 // Requests the execution server to create a process in the specified session | |
371 // using the default (i.e. Winlogon) token. This routine relies on undocumented | |
372 // OS functionality and will likely not work on anything but XP or W2K3. | |
373 bool CreateRemoteSessionProcess( | |
374 uint32_t session_id, | |
375 const base::FilePath::StringType& application_name, | |
376 const base::CommandLine::StringType& command_line, | |
377 DWORD creation_flags, | |
378 const base::char16* desktop_name, | |
379 PROCESS_INFORMATION* process_information_out) { | |
380 DCHECK_LT(base::win::GetVersion(), base::win::VERSION_VISTA); | |
381 | |
382 base::win::ScopedHandle pipe; | |
383 if (!ConnectToExecutionServer(session_id, &pipe)) | |
384 return false; | |
385 | |
386 if (!SendCreateProcessRequest(pipe.Get(), application_name, command_line, | |
387 creation_flags, desktop_name)) { | |
388 return false; | |
389 } | |
390 | |
391 PROCESS_INFORMATION process_information; | |
392 if (!ReceiveCreateProcessResponse(pipe.Get(), &process_information)) | |
393 return false; | |
394 | |
395 if (!ProcessCreateProcessResponse(creation_flags, &process_information)) { | |
396 CloseHandlesAndTerminateProcess(&process_information); | |
397 return false; | |
398 } | |
399 | |
400 *process_information_out = process_information; | |
401 return true; | |
402 } | |
403 | |
404 } // namespace | 74 } // namespace |
405 | 75 |
406 namespace remoting { | 76 namespace remoting { |
407 | 77 |
408 base::LazyInstance<base::Lock>::Leaky g_inherit_handles_lock = | 78 base::LazyInstance<base::Lock>::Leaky g_inherit_handles_lock = |
409 LAZY_INSTANCE_INITIALIZER; | 79 LAZY_INSTANCE_INITIALIZER; |
410 | 80 |
411 // Creates a copy of the current process token for the given |session_id| so | 81 // Creates a copy of the current process token for the given |session_id| so |
412 // it can be used to launch a process in that session. | 82 // it can be used to launch a process in that session. |
413 bool CreateSessionToken(uint32_t session_id, ScopedHandle* token_out) { | 83 bool CreateSessionToken(uint32_t session_id, ScopedHandle* token_out) { |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
473 const_cast<LPWSTR>(command_line.c_str()), | 143 const_cast<LPWSTR>(command_line.c_str()), |
474 process_attributes, | 144 process_attributes, |
475 thread_attributes, | 145 thread_attributes, |
476 inherit_handles, | 146 inherit_handles, |
477 creation_flags, | 147 creation_flags, |
478 nullptr, | 148 nullptr, |
479 nullptr, | 149 nullptr, |
480 &startup_info, | 150 &startup_info, |
481 &temp_process_info); | 151 &temp_process_info); |
482 | 152 |
483 // CreateProcessAsUser will fail on XP and W2K3 with ERROR_PIPE_NOT_CONNECTED | |
484 // if the user hasn't logged to the target session yet. In such a case | |
485 // we try to talk to the execution server directly emulating what | |
486 // the undocumented and not-exported advapi32!CreateRemoteSessionProcessW() | |
487 // function does. The created process will run under Winlogon'a token instead | |
488 // of |user_token|. Since Winlogon runs as SYSTEM, this suits our needs. | |
489 if (!result && | |
490 GetLastError() == ERROR_PIPE_NOT_CONNECTED && | |
491 base::win::GetVersion() < base::win::VERSION_VISTA) { | |
492 DWORD session_id; | |
493 DWORD return_length; | |
494 result = GetTokenInformation(user_token, | |
495 TokenSessionId, | |
496 &session_id, | |
497 sizeof(session_id), | |
498 &return_length); | |
499 if (result && session_id != 0) { | |
500 result = CreateRemoteSessionProcess(session_id, | |
501 application_name, | |
502 command_line, | |
503 creation_flags, | |
504 desktop_name, | |
505 &temp_process_info); | |
506 } else { | |
507 // Restore the error status returned by CreateProcessAsUser(). | |
508 result = FALSE; | |
509 SetLastError(ERROR_PIPE_NOT_CONNECTED); | |
510 } | |
511 } | |
512 | |
513 if (!result) { | 153 if (!result) { |
514 PLOG(ERROR) << "Failed to launch a process with a user token"; | 154 PLOG(ERROR) << "Failed to launch a process with a user token"; |
515 return false; | 155 return false; |
516 } | 156 } |
517 | 157 |
518 base::win::ScopedProcessInformation process_info(temp_process_info); | 158 base::win::ScopedProcessInformation process_info(temp_process_info); |
519 | 159 |
520 CHECK(process_info.IsValid()); | 160 CHECK(process_info.IsValid()); |
521 process_out->Set(process_info.TakeProcessHandle()); | 161 process_out->Set(process_info.TakeProcessHandle()); |
522 thread_out->Set(process_info.TakeThreadHandle()); | 162 thread_out->Set(process_info.TakeThreadHandle()); |
523 return true; | 163 return true; |
524 } | 164 } |
525 | 165 |
526 } // namespace remoting | 166 } // namespace remoting |
OLD | NEW |