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" |
| 11 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 12 #include "base/scoped_native_library.h" | 12 #include "base/scoped_native_library.h" |
| 13 #include "base/stringprintf.h" | 13 #include "base/stringprintf.h" |
| 14 #include "base/utf_string_conversions.h" | 14 #include "base/utf_string_conversions.h" |
| 15 #include "base/win/scoped_handle.h" | |
| 16 #include "base/win/scoped_process_information.h" | 15 #include "base/win/scoped_process_information.h" |
| 17 #include "base/win/windows_version.h" | 16 #include "base/win/windows_version.h" |
| 18 | 17 |
| 19 using base::win::ScopedHandle; | 18 using base::win::ScopedHandle; |
| 20 | 19 |
| 21 namespace { | 20 namespace { |
| 22 | 21 |
| 23 const wchar_t kCreateProcessDefaultPipeNameFormat[] = | 22 const wchar_t kCreateProcessDefaultPipeNameFormat[] = |
| 24 L"\\\\.\\Pipe\\TerminalServer\\SystemExecSrvr\\%d"; | 23 L"\\\\.\\Pipe\\TerminalServer\\SystemExecSrvr\\%d"; |
| 25 | 24 |
| 26 // Undocumented WINSTATIONINFOCLASS value causing | 25 // Undocumented WINSTATIONINFOCLASS value causing |
| 27 // winsta!WinStationQueryInformationW() to return the name of the pipe for | 26 // winsta!WinStationQueryInformationW() to return the name of the pipe for |
| 28 // requesting cross-session process creation. | 27 // requesting cross-session process creation. |
| 29 const WINSTATIONINFOCLASS kCreateProcessPipeNameClass = | 28 const WINSTATIONINFOCLASS kCreateProcessPipeNameClass = |
| 30 static_cast<WINSTATIONINFOCLASS>(0x21); | 29 static_cast<WINSTATIONINFOCLASS>(0x21); |
| 31 | 30 |
| 32 const int kPipeBusyWaitTimeoutMs = 2000; | 31 const int kPipeBusyWaitTimeoutMs = 2000; |
| 33 const int kPipeConnectMaxAttempts = 3; | 32 const int kPipeConnectMaxAttempts = 3; |
| 34 | 33 |
| 35 // The minimum and maximum delays between attempts to inject host process into | 34 // The minimum and maximum delays between attempts to inject host process into |
| 36 // a session. | 35 // a session. |
| 37 const int kMaxLaunchDelaySeconds = 60; | 36 const int kMaxLaunchDelaySeconds = 60; |
| 38 const int kMinLaunchDelaySeconds = 1; | 37 const int kMinLaunchDelaySeconds = 1; |
| 39 | 38 |
| 40 // Name of the default session desktop. | 39 // Name of the default session desktop. |
| 41 wchar_t kDefaultDesktopName[] = L"winsta0\\default"; | 40 wchar_t kDefaultDesktopName[] = L"winsta0\\default"; |
| 42 | 41 |
| 42 // Takes the process token and makes a copy of it. The returned handle will have | |
| 43 // |desired_access| rights. | |
|
Wez
2012/08/03 20:56:06
nit: Clarify that we make an impersonation token f
alexeypa (please no reviews)
2012/08/03 21:40:39
Done.
| |
| 44 bool CopyProcessToken(DWORD desired_access, ScopedHandle* token_out) { | |
| 45 HANDLE handle; | |
|
Wez
2012/08/03 20:56:06
Why not use ScopedHandle::Receive() to avoid the u
alexeypa (please no reviews)
2012/08/03 21:40:39
Good point. I missed this piece.
| |
| 46 if (!OpenProcessToken(GetCurrentProcess(), | |
| 47 TOKEN_DUPLICATE | desired_access, | |
| 48 &handle)) { | |
| 49 LOG_GETLASTERROR(ERROR) << "Failed to open process token"; | |
| 50 return false; | |
| 51 } | |
| 52 | |
| 53 ScopedHandle process_token(handle); | |
| 54 | |
| 55 if (!DuplicateTokenEx(process_token, | |
| 56 desired_access, | |
| 57 NULL, | |
| 58 SecurityImpersonation, | |
| 59 TokenPrimary, | |
| 60 &handle)) { | |
| 61 LOG_GETLASTERROR(ERROR) << "Failed to duplicate the process token"; | |
| 62 return false; | |
| 63 } | |
| 64 | |
| 65 token_out->Set(handle); | |
| 66 return true; | |
| 67 } | |
| 68 | |
| 69 // Creates a copy of the current process with SE_TCB_NAME privilege enabled. | |
| 70 bool CreatePrivilegedToken(ScopedHandle* token_out) { | |
|
Wez
2012/08/03 20:56:06
nit: Why not have CreatePrivilegedToken() accept t
alexeypa (please no reviews)
2012/08/03 21:40:39
The main reason is that it is not going to be used
| |
| 71 ScopedHandle privileged_token; | |
| 72 DWORD desired_access = TOKEN_ADJUST_PRIVILEGES | TOKEN_IMPERSONATE | | |
| 73 TOKEN_DUPLICATE | TOKEN_QUERY; | |
| 74 if (!CopyProcessToken(desired_access, &privileged_token)) { | |
| 75 return false; | |
| 76 } | |
| 77 | |
| 78 // Get the LUID for the SE_TCB_NAME privilege. | |
| 79 TOKEN_PRIVILEGES state; | |
| 80 state.PrivilegeCount = 1; | |
| 81 state.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED; | |
| 82 if (!LookupPrivilegeValue(NULL, SE_TCB_NAME, &state.Privileges[0].Luid)) { | |
| 83 LOG_GETLASTERROR(ERROR) << | |
| 84 "Failed to lookup the LUID for the SE_TCB_NAME privilege"; | |
| 85 return false; | |
| 86 } | |
| 87 | |
| 88 // Enable the SE_TCB_NAME privilege. | |
| 89 if (!AdjustTokenPrivileges(privileged_token, FALSE, &state, 0, NULL, 0)) { | |
| 90 LOG_GETLASTERROR(ERROR) << | |
| 91 "Failed to enable SE_TCB_NAME privilege in a token"; | |
| 92 return false; | |
| 93 } | |
| 94 | |
| 95 token_out->Set(privileged_token.Take()); | |
| 96 return true; | |
| 97 } | |
| 98 | |
| 43 // Requests the execution server to create a process in the specified session | 99 // Requests the execution server to create a process in the specified session |
| 44 // using the default (i.e. Winlogon) token. This routine relies on undocumented | 100 // using the default (i.e. Winlogon) token. This routine relies on undocumented |
| 45 // OS functionality and will likely not work on anything but XP or W2K3. | 101 // OS functionality and will likely not work on anything but XP or W2K3. |
| 46 bool CreateRemoteSessionProcess( | 102 bool CreateRemoteSessionProcess( |
| 47 uint32 session_id, | 103 uint32 session_id, |
| 48 const std::wstring& application_name, | 104 const std::wstring& application_name, |
| 49 const std::wstring& command_line, | 105 const std::wstring& command_line, |
| 50 PROCESS_INFORMATION* process_information_out) | 106 PROCESS_INFORMATION* process_information_out) |
| 51 { | 107 { |
| 52 DCHECK(base::win::GetVersion() == base::win::VERSION_XP); | 108 DCHECK(base::win::GetVersion() == base::win::VERSION_XP); |
| (...skipping 183 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 236 } | 292 } |
| 237 | 293 |
| 238 *process_information_out = response.process_information; | 294 *process_information_out = response.process_information; |
| 239 return true; | 295 return true; |
| 240 } | 296 } |
| 241 | 297 |
| 242 } // namespace | 298 } // namespace |
| 243 | 299 |
| 244 namespace remoting { | 300 namespace remoting { |
| 245 | 301 |
| 302 // Creates a copy of the current process token for the given |session_id| so | |
| 303 // it can be used to launch a process in that session. | |
| 304 bool CreateSessionToken(uint32 session_id, ScopedHandle* token_out) { | |
| 305 ScopedHandle session_token; | |
| 306 DWORD desired_access = TOKEN_ADJUST_DEFAULT | TOKEN_ADJUST_SESSIONID | | |
| 307 TOKEN_ASSIGN_PRIMARY | TOKEN_DUPLICATE | TOKEN_QUERY; | |
| 308 if (!CopyProcessToken(desired_access, &session_token)) { | |
| 309 return false; | |
| 310 } | |
| 311 | |
| 312 // Temporarily enable the SE_TCB_NAME privilege. | |
|
Wez
2012/08/03 20:56:06
nit: Update comment to indicate _why_ we're doing
alexeypa (please no reviews)
2012/08/03 21:40:39
Done.
| |
| 313 ScopedHandle privileged_token; | |
| 314 if (!CreatePrivilegedToken(&privileged_token)) { | |
| 315 return false; | |
| 316 } | |
| 317 | |
|
Wez
2012/08/03 20:56:06
nit: Lose the blank line; it implies that the impe
alexeypa (please no reviews)
2012/08/03 21:40:39
Done.
| |
| 318 if (!ImpersonateLoggedOnUser(privileged_token)) { | |
| 319 LOG_GETLASTERROR(ERROR) << | |
| 320 "Failed to impersonate the privileged token"; | |
| 321 return false; | |
| 322 } | |
| 323 | |
| 324 // Change the session ID of the token. | |
| 325 DWORD new_session_id = session_id; | |
| 326 if (!SetTokenInformation(session_token, | |
| 327 TokenSessionId, | |
| 328 &new_session_id, | |
| 329 sizeof(new_session_id))) { | |
| 330 LOG_GETLASTERROR(ERROR) << "Failed to change session ID of a token"; | |
| 331 | |
| 332 // Revert to the default token. | |
| 333 CHECK(RevertToSelf()); | |
| 334 return false; | |
| 335 } | |
| 336 | |
| 337 // Revert to the default token. | |
| 338 CHECK(RevertToSelf()); | |
| 339 | |
| 340 token_out->Set(session_token.Take()); | |
| 341 return true; | |
| 342 } | |
| 343 | |
| 246 bool LaunchProcessWithToken(const FilePath& binary, | 344 bool LaunchProcessWithToken(const FilePath& binary, |
| 247 const std::wstring& command_line, | 345 const std::wstring& command_line, |
| 248 HANDLE user_token, | 346 HANDLE user_token, |
| 249 base::Process* process_out) { | 347 base::Process* process_out) { |
| 250 std::wstring application_name = binary.value(); | 348 std::wstring application_name = binary.value(); |
| 251 | 349 |
| 252 base::win::ScopedProcessInformation process_info; | 350 base::win::ScopedProcessInformation process_info; |
| 253 STARTUPINFOW startup_info; | 351 STARTUPINFOW startup_info; |
| 254 | 352 |
| 255 memset(&startup_info, 0, sizeof(startup_info)); | 353 memset(&startup_info, 0, sizeof(startup_info)); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 301 "Failed to launch a process with a user token"; | 399 "Failed to launch a process with a user token"; |
| 302 return false; | 400 return false; |
| 303 } | 401 } |
| 304 | 402 |
| 305 CHECK(process_info.IsValid()); | 403 CHECK(process_info.IsValid()); |
| 306 process_out->set_handle(process_info.TakeProcessHandle()); | 404 process_out->set_handle(process_info.TakeProcessHandle()); |
| 307 return true; | 405 return true; |
| 308 } | 406 } |
| 309 | 407 |
| 310 } // namespace remoting | 408 } // namespace remoting |
| OLD | NEW |