| 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 // This file implements the Windows service controlling Me2Me host processes | 5 // This file implements the Windows service controlling Me2Me host processes |
| 6 // running within user sessions. | 6 // running within user sessions. |
| 7 | 7 |
| 8 #include "remoting/host/wts_session_process_launcher_win.h" | 8 #include "remoting/host/wts_session_process_launcher_win.h" |
| 9 | 9 |
| 10 #include <windows.h> | 10 #include <windows.h> |
| 11 #include <sddl.h> | 11 #include <sddl.h> |
| 12 #include <limits> | 12 #include <limits> |
| 13 | 13 |
| 14 #include "base/base_switches.h" | 14 #include "base/base_switches.h" |
| 15 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/bind_helpers.h" | 16 #include "base/bind_helpers.h" |
| 17 #include "base/command_line.h" | 17 #include "base/command_line.h" |
| 18 #include "base/logging.h" | 18 #include "base/logging.h" |
| 19 #include "base/message_loop_proxy.h" | 19 #include "base/message_loop_proxy.h" |
| 20 #include "base/process_util.h" | 20 #include "base/process_util.h" |
| 21 #include "base/rand_util.h" | 21 #include "base/rand_util.h" |
| 22 #include "base/string16.h" | |
| 23 #include "base/stringize_macros.h" | |
| 24 #include "base/stringprintf.h" | 22 #include "base/stringprintf.h" |
| 25 #include "base/win/scoped_handle.h" | 23 #include "base/win/scoped_handle.h" |
| 26 #include "base/win/scoped_process_information.h" | 24 #include "base/win/scoped_process_information.h" |
| 27 #include "ipc/ipc_channel_proxy.h" | 25 #include "ipc/ipc_channel_proxy.h" |
| 28 #include "ipc/ipc_message.h" | 26 #include "ipc/ipc_message.h" |
| 29 #include "ipc/ipc_message_macros.h" | 27 #include "ipc/ipc_message_macros.h" |
| 30 #include "remoting/host/constants.h" | 28 #include "remoting/host/constants.h" |
| 31 #include "remoting/host/chromoting_messages.h" | 29 #include "remoting/host/chromoting_messages.h" |
| 32 #include "remoting/host/sas_injector.h" | 30 #include "remoting/host/sas_injector.h" |
| 33 #include "remoting/host/wts_console_monitor_win.h" | 31 #include "remoting/host/wts_console_monitor_win.h" |
| 34 | 32 |
| 35 using base::win::ScopedHandle; | 33 using base::win::ScopedHandle; |
| 36 using base::TimeDelta; | 34 using base::TimeDelta; |
| 37 | 35 |
| 38 namespace { | 36 namespace { |
| 39 | 37 |
| 40 // The minimum and maximum delays between attempts to inject host process into | 38 // The minimum and maximum delays between attempts to inject host process into |
| 41 // a session. | 39 // a session. |
| 42 const int kMaxLaunchDelaySeconds = 60; | 40 const int kMaxLaunchDelaySeconds = 60; |
| 43 const int kMinLaunchDelaySeconds = 1; | 41 const int kMinLaunchDelaySeconds = 1; |
| 44 | 42 |
| 45 // Name of the default session desktop. | 43 // Name of the default session desktop. |
| 46 char16 kDefaultDesktopName[] = TO_L_STRING("winsta0\\default"); | 44 wchar_t kDefaultDesktopName[] = L"winsta0\\default"; |
| 47 | 45 |
| 48 // Match the pipe name prefix used by Chrome IPC channels. | 46 // Match the pipe name prefix used by Chrome IPC channels. |
| 49 const char16 kChromePipeNamePrefix[] = TO_L_STRING("\\\\.\\pipe\\chrome."); | 47 const wchar_t kChromePipeNamePrefix[] = L"\\\\.\\pipe\\chrome."; |
| 50 | 48 |
| 51 // The IPC channel name is passed to the host in the command line. | 49 // The IPC channel name is passed to the host in the command line. |
| 52 const char kChromotingIpcSwitchName[] = "chromoting-ipc"; | 50 const char kChromotingIpcSwitchName[] = "chromoting-ipc"; |
| 53 | 51 |
| 54 // The command line parameters that should be copied from the service's command | 52 // The command line parameters that should be copied from the service's command |
| 55 // line to the host process. | 53 // line to the host process. |
| 56 const char* kCopiedSwitchNames[] = { | 54 const char* kCopiedSwitchNames[] = { |
| 57 "auth-config", "host-config", switches::kV, switches::kVModule }; | 55 "auth-config", "host-config", switches::kV, switches::kVModule }; |
| 58 | 56 |
| 59 // The security descriptor of the Chromoting IPC channel. It gives full access | 57 // The security descriptor of the Chromoting IPC channel. It gives full access |
| 60 // to LocalSystem and denies access by anyone else. | 58 // to LocalSystem and denies access by anyone else. |
| 61 const char16 kChromotingChannelSecurityDescriptor[] = | 59 const wchar_t kChromotingChannelSecurityDescriptor[] = |
| 62 TO_L_STRING("O:SYG:SYD:(A;;GA;;;SY)"); | 60 L"O:SYG:SYD:(A;;GA;;;SY)"; |
| 63 | 61 |
| 64 // Takes the process token and makes a copy of it. The returned handle will have | 62 // Takes the process token and makes a copy of it. The returned handle will have |
| 65 // |desired_access| rights. | 63 // |desired_access| rights. |
| 66 bool CopyProcessToken(DWORD desired_access, | 64 bool CopyProcessToken(DWORD desired_access, |
| 67 ScopedHandle* token_out) { | 65 ScopedHandle* token_out) { |
| 68 | 66 |
| 69 HANDLE handle; | 67 HANDLE handle; |
| 70 if (!OpenProcessToken(GetCurrentProcess(), | 68 if (!OpenProcessToken(GetCurrentProcess(), |
| 71 TOKEN_DUPLICATE | desired_access, | 69 TOKEN_DUPLICATE | desired_access, |
| 72 &handle)) { | 70 &handle)) { |
| (...skipping 69 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 142 "Failed to change session ID of a token"; | 140 "Failed to change session ID of a token"; |
| 143 return false; | 141 return false; |
| 144 } | 142 } |
| 145 | 143 |
| 146 token_out->Set(session_token.Take()); | 144 token_out->Set(session_token.Take()); |
| 147 return true; | 145 return true; |
| 148 } | 146 } |
| 149 | 147 |
| 150 // Generates random channel ID. | 148 // Generates random channel ID. |
| 151 // N.B. Stolen from src/content/common/child_process_host_impl.cc | 149 // N.B. Stolen from src/content/common/child_process_host_impl.cc |
| 152 string16 GenerateRandomChannelId(void* instance) { | 150 std::wstring GenerateRandomChannelId(void* instance) { |
| 153 return base::StringPrintf(TO_L_STRING("%d.%p.%d"), | 151 return base::StringPrintf(L"%d.%p.%d", |
| 154 base::GetCurrentProcId(), instance, | 152 base::GetCurrentProcId(), instance, |
| 155 base::RandInt(0, std::numeric_limits<int>::max())); | 153 base::RandInt(0, std::numeric_limits<int>::max())); |
| 156 } | 154 } |
| 157 | 155 |
| 158 // Creates the server end of the Chromoting IPC channel. | 156 // Creates the server end of the Chromoting IPC channel. |
| 159 // N.B. This code is based on IPC::Channel's implementation. | 157 // N.B. This code is based on IPC::Channel's implementation. |
| 160 bool CreatePipeForIpcChannel(void* instance, | 158 bool CreatePipeForIpcChannel(void* instance, |
| 161 string16* channel_name_out, | 159 std::wstring* channel_name_out, |
| 162 ScopedHandle* pipe_out) { | 160 ScopedHandle* pipe_out) { |
| 163 // Create security descriptor for the channel. | 161 // Create security descriptor for the channel. |
| 164 SECURITY_ATTRIBUTES security_attributes; | 162 SECURITY_ATTRIBUTES security_attributes; |
| 165 security_attributes.nLength = sizeof(security_attributes); | 163 security_attributes.nLength = sizeof(security_attributes); |
| 166 security_attributes.bInheritHandle = FALSE; | 164 security_attributes.bInheritHandle = FALSE; |
| 167 | 165 |
| 168 ULONG security_descriptor_length = 0; | 166 ULONG security_descriptor_length = 0; |
| 169 if (!ConvertStringSecurityDescriptorToSecurityDescriptorW( | 167 if (!ConvertStringSecurityDescriptorToSecurityDescriptorW( |
| 170 kChromotingChannelSecurityDescriptor, | 168 kChromotingChannelSecurityDescriptor, |
| 171 SDDL_REVISION_1, | 169 SDDL_REVISION_1, |
| 172 reinterpret_cast<PSECURITY_DESCRIPTOR*>( | 170 reinterpret_cast<PSECURITY_DESCRIPTOR*>( |
| 173 &security_attributes.lpSecurityDescriptor), | 171 &security_attributes.lpSecurityDescriptor), |
| 174 &security_descriptor_length)) { | 172 &security_descriptor_length)) { |
| 175 LOG_GETLASTERROR(ERROR) << | 173 LOG_GETLASTERROR(ERROR) << |
| 176 "Failed to create a security descriptor for the Chromoting IPC channel"; | 174 "Failed to create a security descriptor for the Chromoting IPC channel"; |
| 177 return false; | 175 return false; |
| 178 } | 176 } |
| 179 | 177 |
| 180 // Generate a random channel name. | 178 // Generate a random channel name. |
| 181 string16 channel_name(GenerateRandomChannelId(instance)); | 179 std::wstring channel_name(GenerateRandomChannelId(instance)); |
| 182 | 180 |
| 183 // Convert it to the pipe name. | 181 // Convert it to the pipe name. |
| 184 string16 pipe_name(kChromePipeNamePrefix); | 182 std::wstring pipe_name(kChromePipeNamePrefix); |
| 185 pipe_name.append(channel_name); | 183 pipe_name.append(channel_name); |
| 186 | 184 |
| 187 // Create the server end of the pipe. This code should match the code in | 185 // Create the server end of the pipe. This code should match the code in |
| 188 // IPC::Channel with exception of passing a non-default security descriptor. | 186 // IPC::Channel with exception of passing a non-default security descriptor. |
| 189 HANDLE pipe = CreateNamedPipeW(pipe_name.c_str(), | 187 HANDLE pipe = CreateNamedPipeW(pipe_name.c_str(), |
| 190 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | | 188 PIPE_ACCESS_DUPLEX | FILE_FLAG_OVERLAPPED | |
| 191 FILE_FLAG_FIRST_PIPE_INSTANCE, | 189 FILE_FLAG_FIRST_PIPE_INSTANCE, |
| 192 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, | 190 PIPE_TYPE_BYTE | PIPE_READMODE_BYTE, |
| 193 1, | 191 1, |
| 194 IPC::Channel::kReadBufferSize, | 192 IPC::Channel::kReadBufferSize, |
| 195 IPC::Channel::kReadBufferSize, | 193 IPC::Channel::kReadBufferSize, |
| 196 5000, | 194 5000, |
| 197 &security_attributes); | 195 &security_attributes); |
| 198 if (pipe == INVALID_HANDLE_VALUE) { | 196 if (pipe == INVALID_HANDLE_VALUE) { |
| 199 LOG_GETLASTERROR(ERROR) << | 197 LOG_GETLASTERROR(ERROR) << |
| 200 "Failed to create the server end of the Chromoting IPC channel"; | 198 "Failed to create the server end of the Chromoting IPC channel"; |
| 201 LocalFree(security_attributes.lpSecurityDescriptor); | 199 LocalFree(security_attributes.lpSecurityDescriptor); |
| 202 return false; | 200 return false; |
| 203 } | 201 } |
| 204 | 202 |
| 205 LocalFree(security_attributes.lpSecurityDescriptor); | 203 LocalFree(security_attributes.lpSecurityDescriptor); |
| 206 | 204 |
| 207 *channel_name_out = channel_name; | 205 *channel_name_out = channel_name; |
| 208 pipe_out->Set(pipe); | 206 pipe_out->Set(pipe); |
| 209 return true; | 207 return true; |
| 210 } | 208 } |
| 211 | 209 |
| 212 // Launches |binary| in the security context of the supplied |user_token|. | 210 // Launches |binary| in the security context of the supplied |user_token|. |
| 213 bool LaunchProcessAsUser(const FilePath& binary, | 211 bool LaunchProcessAsUser(const FilePath& binary, |
| 214 const string16& command_line, | 212 const std::wstring& command_line, |
| 215 HANDLE user_token, | 213 HANDLE user_token, |
| 216 base::Process* process_out) { | 214 base::Process* process_out) { |
| 217 string16 application_name = binary.value(); | 215 std::wstring application_name = binary.value(); |
| 218 | 216 |
| 219 base::win::ScopedProcessInformation process_info; | 217 base::win::ScopedProcessInformation process_info; |
| 220 STARTUPINFOW startup_info; | 218 STARTUPINFOW startup_info; |
| 221 | 219 |
| 222 memset(&startup_info, 0, sizeof(startup_info)); | 220 memset(&startup_info, 0, sizeof(startup_info)); |
| 223 startup_info.cb = sizeof(startup_info); | 221 startup_info.cb = sizeof(startup_info); |
| 224 startup_info.lpDesktop = kDefaultDesktopName; | 222 startup_info.lpDesktop = kDefaultDesktopName; |
| 225 | 223 |
| 226 if (!CreateProcessAsUserW(user_token, | 224 if (!CreateProcessAsUserW(user_token, |
| 227 application_name.c_str(), | 225 application_name.c_str(), |
| (...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 274 void WtsSessionProcessLauncher::LaunchProcess() { | 272 void WtsSessionProcessLauncher::LaunchProcess() { |
| 275 DCHECK(main_message_loop_->BelongsToCurrentThread()); | 273 DCHECK(main_message_loop_->BelongsToCurrentThread()); |
| 276 DCHECK(state_ == StateStarting); | 274 DCHECK(state_ == StateStarting); |
| 277 DCHECK(!timer_.IsRunning()); | 275 DCHECK(!timer_.IsRunning()); |
| 278 DCHECK(process_.handle() == NULL); | 276 DCHECK(process_.handle() == NULL); |
| 279 DCHECK(process_watcher_.GetWatchedObject() == NULL); | 277 DCHECK(process_watcher_.GetWatchedObject() == NULL); |
| 280 DCHECK(chromoting_channel_.get() == NULL); | 278 DCHECK(chromoting_channel_.get() == NULL); |
| 281 | 279 |
| 282 launch_time_ = base::Time::Now(); | 280 launch_time_ = base::Time::Now(); |
| 283 | 281 |
| 284 string16 channel_name; | 282 std::wstring channel_name; |
| 285 ScopedHandle pipe; | 283 ScopedHandle pipe; |
| 286 if (CreatePipeForIpcChannel(this, &channel_name, &pipe)) { | 284 if (CreatePipeForIpcChannel(this, &channel_name, &pipe)) { |
| 287 // Wrap the pipe into an IPC channel. | 285 // Wrap the pipe into an IPC channel. |
| 288 chromoting_channel_.reset(new IPC::ChannelProxy( | 286 chromoting_channel_.reset(new IPC::ChannelProxy( |
| 289 IPC::ChannelHandle(pipe.Get()), | 287 IPC::ChannelHandle(pipe.Get()), |
| 290 IPC::Channel::MODE_SERVER, | 288 IPC::Channel::MODE_SERVER, |
| 291 this, | 289 this, |
| 292 ipc_message_loop_)); | 290 ipc_message_loop_)); |
| 293 | 291 |
| 294 // Create the host process command line passing the name of the IPC channel | 292 // Create the host process command line passing the name of the IPC channel |
| (...skipping 193 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 488 process_watcher_.StopWatching(); | 486 process_watcher_.StopWatching(); |
| 489 process_.Terminate(0); | 487 process_.Terminate(0); |
| 490 process_.Close(); | 488 process_.Close(); |
| 491 chromoting_channel_.reset(); | 489 chromoting_channel_.reset(); |
| 492 state_ = StateDetached; | 490 state_ = StateDetached; |
| 493 break; | 491 break; |
| 494 } | 492 } |
| 495 } | 493 } |
| 496 | 494 |
| 497 } // namespace remoting | 495 } // namespace remoting |
| OLD | NEW |