Chromium Code Reviews| 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 <winternl.h> | 8 #include <winternl.h> |
| 9 | 9 |
| 10 #include "base/logging.h" | 10 #include "base/logging.h" |
| (...skipping 188 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 199 (application_name.size() + command_line.size() + desktop_name.size() + 3); | 199 (application_name.size() + command_line.size() + desktop_name.size() + 3); |
| 200 scoped_array<char> buffer(new char[size]); | 200 scoped_array<char> buffer(new char[size]); |
| 201 memset(buffer.get(), 0, size); | 201 memset(buffer.get(), 0, size); |
| 202 | 202 |
| 203 // Marshal the input parameters. | 203 // Marshal the input parameters. |
| 204 CreateProcessRequest* request = | 204 CreateProcessRequest* request = |
| 205 reinterpret_cast<CreateProcessRequest*>(buffer.get()); | 205 reinterpret_cast<CreateProcessRequest*>(buffer.get()); |
| 206 request->size = size; | 206 request->size = size; |
| 207 request->process_id = GetCurrentProcessId(); | 207 request->process_id = GetCurrentProcessId(); |
| 208 request->use_default_token = TRUE; | 208 request->use_default_token = TRUE; |
| 209 request->creation_flags = creation_flags; | 209 // Always pass CREATE_SUSPENDED to avoid a race between the created process |
| 210 // exition too soon and OpenProcess() call below. | |
|
simonmorris
2012/10/04 23:32:44
exition -> exiting
alexeypa (please no reviews)
2012/10/05 18:30:00
Done.
| |
| 211 request->creation_flags = creation_flags | CREATE_SUSPENDED; | |
| 210 request->startup_info.cb = sizeof(request->startup_info); | 212 request->startup_info.cb = sizeof(request->startup_info); |
| 211 | 213 |
| 212 size_t buffer_offset = sizeof(CreateProcessRequest); | 214 size_t buffer_offset = sizeof(CreateProcessRequest); |
| 213 | 215 |
| 214 request->application_name = reinterpret_cast<LPWSTR>(buffer_offset); | 216 request->application_name = reinterpret_cast<LPWSTR>(buffer_offset); |
| 215 std::copy(application_name.begin(), | 217 std::copy(application_name.begin(), |
| 216 application_name.end(), | 218 application_name.end(), |
| 217 reinterpret_cast<wchar_t*>(buffer.get() + buffer_offset)); | 219 reinterpret_cast<wchar_t*>(buffer.get() + buffer_offset)); |
| 218 buffer_offset += (application_name.size() + 1) * sizeof(wchar_t); | 220 buffer_offset += (application_name.size() + 1) * sizeof(wchar_t); |
| 219 | 221 |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 return false; | 259 return false; |
| 258 } | 260 } |
| 259 | 261 |
| 260 if (!response.success) { | 262 if (!response.success) { |
| 261 SetLastError(response.last_error); | 263 SetLastError(response.last_error); |
| 262 return false; | 264 return false; |
| 263 } | 265 } |
| 264 | 266 |
| 265 // The execution server does not return handles to the created process and | 267 // The execution server does not return handles to the created process and |
| 266 // thread. | 268 // thread. |
| 269 bool success = true; | |
| 267 if (response.process_information.hProcess == NULL) { | 270 if (response.process_information.hProcess == NULL) { |
|
simonmorris
2012/10/04 23:32:44
Maybe change this to !response..., for consistency
alexeypa (please no reviews)
2012/10/05 18:30:00
Done.
| |
| 268 // N.B. PROCESS_ALL_ACCESS is different in XP and Vista+ versions of | 271 // N.B. PROCESS_ALL_ACCESS is different in XP and Vista+ versions of |
| 269 // the SDK. |desired_access| below is effectively PROCESS_ALL_ACCESS from | 272 // the SDK. |desired_access| below is effectively PROCESS_ALL_ACCESS from |
| 270 // the XP version of the SDK. | 273 // the XP version of the SDK. |
| 271 DWORD desired_access = | 274 DWORD desired_access = |
| 272 STANDARD_RIGHTS_REQUIRED | | 275 STANDARD_RIGHTS_REQUIRED | |
| 273 SYNCHRONIZE | | 276 SYNCHRONIZE | |
| 274 PROCESS_TERMINATE | | 277 PROCESS_TERMINATE | |
| 275 PROCESS_CREATE_THREAD | | 278 PROCESS_CREATE_THREAD | |
| 276 PROCESS_SET_SESSIONID | | 279 PROCESS_SET_SESSIONID | |
| 277 PROCESS_VM_OPERATION | | 280 PROCESS_VM_OPERATION | |
| 278 PROCESS_VM_READ | | 281 PROCESS_VM_READ | |
| 279 PROCESS_VM_WRITE | | 282 PROCESS_VM_WRITE | |
| 280 PROCESS_DUP_HANDLE | | 283 PROCESS_DUP_HANDLE | |
| 281 PROCESS_CREATE_PROCESS | | 284 PROCESS_CREATE_PROCESS | |
| 282 PROCESS_SET_QUOTA | | 285 PROCESS_SET_QUOTA | |
| 283 PROCESS_SET_INFORMATION | | 286 PROCESS_SET_INFORMATION | |
| 284 PROCESS_QUERY_INFORMATION | | 287 PROCESS_QUERY_INFORMATION | |
| 285 PROCESS_SUSPEND_RESUME; | 288 PROCESS_SUSPEND_RESUME; |
| 286 response.process_information.hProcess = | 289 response.process_information.hProcess = |
| 287 OpenProcess(desired_access, | 290 OpenProcess(desired_access, |
| 288 FALSE, | 291 FALSE, |
| 289 response.process_information.dwProcessId); | 292 response.process_information.dwProcessId); |
| 290 if (!response.process_information.hProcess) { | 293 if (!response.process_information.hProcess) { |
| 291 LOG_GETLASTERROR(ERROR) << "Failed to open process " | 294 LOG_GETLASTERROR(ERROR) << "Failed to open the process " |
| 292 << response.process_information.dwProcessId; | 295 << response.process_information.dwProcessId; |
| 293 return false; | 296 success = false; |
| 294 } | 297 } |
| 295 } | 298 } |
| 296 | 299 |
| 297 *process_information_out = response.process_information; | 300 if (success && response.process_information.hThread == NULL) { |
| 298 return true; | 301 // N.B. THREAD_ALL_ACCESS is different in XP and Vista+ versions of |
| 302 // the SDK. |desired_access| below is effectively THREAD_ALL_ACCESS from | |
| 303 // the XP version of the SDK. | |
| 304 DWORD desired_access = | |
| 305 STANDARD_RIGHTS_REQUIRED | | |
| 306 SYNCHRONIZE | | |
| 307 THREAD_TERMINATE | | |
| 308 THREAD_SUSPEND_RESUME | | |
| 309 THREAD_GET_CONTEXT | | |
| 310 THREAD_SET_CONTEXT | | |
| 311 THREAD_QUERY_INFORMATION | | |
| 312 THREAD_SET_INFORMATION | | |
| 313 THREAD_SET_THREAD_TOKEN | | |
| 314 THREAD_IMPERSONATE | | |
| 315 THREAD_DIRECT_IMPERSONATION; | |
| 316 response.process_information.hThread = | |
| 317 OpenThread(desired_access, | |
| 318 FALSE, | |
| 319 response.process_information.dwThreadId); | |
| 320 if (!response.process_information.hThread) { | |
| 321 LOG_GETLASTERROR(ERROR) << "Failed to open the thread " | |
| 322 << response.process_information.dwThreadId; | |
| 323 success = false; | |
| 324 } | |
| 325 } | |
| 326 | |
| 327 // Resume the thread if the caller didn't want to suspend the process. | |
| 328 if (success && (creation_flags & CREATE_SUSPENDED) == 0) { | |
| 329 if (!ResumeThread(response.process_information.hThread)) { | |
| 330 LOG_GETLASTERROR(ERROR) << "Failed to resume the thread " | |
| 331 << response.process_information.dwThreadId; | |
| 332 success = false; | |
| 333 } | |
| 334 } | |
| 335 | |
| 336 if (success) { | |
| 337 *process_information_out = response.process_information; | |
| 338 return true; | |
| 339 } else { | |
| 340 if (response.process_information.hThread != NULL) | |
| 341 CloseHandle(response.process_information.hThread); | |
| 342 | |
| 343 if (response.process_information.hProcess != NULL) { | |
| 344 TerminateProcess(response.process_information.hProcess, CONTROL_C_EXIT); | |
| 345 CloseHandle(response.process_information.hProcess); | |
| 346 } | |
|
simonmorris
2012/10/04 23:32:44
Consider moving the preceding 7 lines to a separat
alexeypa (please no reviews)
2012/10/05 18:30:00
Done. I've split CreateRemoteSessionProcess() into
| |
| 347 return false; | |
| 348 } | |
| 299 } | 349 } |
| 300 | 350 |
| 301 } // namespace | 351 } // namespace |
| 302 | 352 |
| 303 namespace remoting { | 353 namespace remoting { |
| 304 | 354 |
| 305 // Creates a copy of the current process token for the given |session_id| so | 355 // Creates a copy of the current process token for the given |session_id| so |
| 306 // it can be used to launch a process in that session. | 356 // it can be used to launch a process in that session. |
| 307 bool CreateSessionToken(uint32 session_id, ScopedHandle* token_out) { | 357 bool CreateSessionToken(uint32 session_id, ScopedHandle* token_out) { |
| 308 ScopedHandle session_token; | 358 ScopedHandle session_token; |
| (...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 408 return false; | 458 return false; |
| 409 } | 459 } |
| 410 | 460 |
| 411 CHECK(process_info.IsValid()); | 461 CHECK(process_info.IsValid()); |
| 412 process_out->Set(process_info.TakeProcessHandle()); | 462 process_out->Set(process_info.TakeProcessHandle()); |
| 413 thread_out->Set(process_info.TakeThreadHandle()); | 463 thread_out->Set(process_info.TakeThreadHandle()); |
| 414 return true; | 464 return true; |
| 415 } | 465 } |
| 416 | 466 |
| 417 } // namespace remoting | 467 } // namespace remoting |
| OLD | NEW |