OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 1 // Copyright 2016 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/security_key/gnubby_auth_handler.h" | 5 #include "remoting/host/security_key/gnubby_auth_handler.h" |
6 | 6 |
| 7 #include <cstdint> |
7 #include <map> | 8 #include <map> |
8 #include <memory> | 9 #include <memory> |
9 #include <string> | 10 #include <string> |
10 | 11 |
11 #include "base/bind.h" | 12 #include "base/bind.h" |
| 13 #include "base/location.h" |
12 #include "base/logging.h" | 14 #include "base/logging.h" |
| 15 #include "base/memory/weak_ptr.h" |
13 #include "base/strings/stringprintf.h" | 16 #include "base/strings/stringprintf.h" |
14 #include "base/strings/utf_string_conversions.h" | 17 #include "base/strings/utf_string_conversions.h" |
15 #include "base/threading/thread_checker.h" | 18 #include "base/threading/thread_checker.h" |
| 19 #include "base/threading/thread_task_runner_handle.h" |
16 #include "base/time/time.h" | 20 #include "base/time/time.h" |
17 #include "base/timer/timer.h" | 21 #include "base/timer/timer.h" |
18 #include "base/win/win_util.h" | 22 #include "base/win/win_util.h" |
19 #include "ipc/ipc_channel.h" | 23 #include "ipc/ipc_channel.h" |
20 #include "ipc/ipc_listener.h" | 24 #include "ipc/ipc_listener.h" |
21 #include "ipc/ipc_message.h" | 25 #include "ipc/ipc_message.h" |
22 #include "ipc/ipc_message_macros.h" | 26 #include "ipc/ipc_message_macros.h" |
23 #include "remoting/base/logging.h" | 27 #include "remoting/base/logging.h" |
24 #include "remoting/host/chromoting_messages.h" | 28 #include "remoting/host/chromoting_messages.h" |
| 29 #include "remoting/host/client_session_details.h" |
25 #include "remoting/host/ipc_util.h" | 30 #include "remoting/host/ipc_util.h" |
26 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" | 31 #include "remoting/host/security_key/remote_security_key_ipc_constants.h" |
27 #include "remoting/host/security_key/remote_security_key_ipc_server.h" | 32 #include "remoting/host/security_key/remote_security_key_ipc_server.h" |
28 | 33 |
29 namespace { | 34 namespace { |
30 | 35 |
31 // The timeout used to disconnect a client from the IPC Server channel if it | 36 // The timeout used to disconnect a client from the IPC Server channel if it |
32 // forgets to do so. This ensures the server channel is not blocked forever. | 37 // forgets to do so. This ensures the server channel is not blocked forever. |
33 const int kInitialRequestTimeoutSeconds = 5; | 38 const int kInitialRequestTimeoutSeconds = 5; |
34 | 39 |
(...skipping 13 matching lines...) Expand all Loading... |
48 // assigned a unique IPC channel name and connection id. The channel name is | 53 // assigned a unique IPC channel name and connection id. The channel name is |
49 // sent to the client which should disconnect the IPC server channel and | 54 // sent to the client which should disconnect the IPC server channel and |
50 // connect to the security key forwarding session IPC channel to send/receive | 55 // connect to the security key forwarding session IPC channel to send/receive |
51 // security key messages. The IPC server channel will then be reset so it can | 56 // security key messages. The IPC server channel will then be reset so it can |
52 // can service the next client/request. This system allows multiple security | 57 // can service the next client/request. This system allows multiple security |
53 // key forwarding sessions to occur concurrently. | 58 // key forwarding sessions to occur concurrently. |
54 // TODO(joedow): Update GnubbyAuthHandler impls to run on a separate IO thread | 59 // TODO(joedow): Update GnubbyAuthHandler impls to run on a separate IO thread |
55 // instead of the thread it was created on: crbug.com/591739 | 60 // instead of the thread it was created on: crbug.com/591739 |
56 class GnubbyAuthHandlerWin : public GnubbyAuthHandler, public IPC::Listener { | 61 class GnubbyAuthHandlerWin : public GnubbyAuthHandler, public IPC::Listener { |
57 public: | 62 public: |
58 GnubbyAuthHandlerWin(); | 63 explicit GnubbyAuthHandlerWin(ClientSessionDetails* client_session_details); |
59 ~GnubbyAuthHandlerWin() override; | 64 ~GnubbyAuthHandlerWin() override; |
60 | 65 |
61 private: | 66 private: |
62 typedef std::map<int, std::unique_ptr<RemoteSecurityKeyIpcServer>> | 67 typedef std::map<int, std::unique_ptr<RemoteSecurityKeyIpcServer>> |
63 ActiveChannels; | 68 ActiveChannels; |
64 | 69 |
65 // GnubbyAuthHandler interface. | 70 // GnubbyAuthHandler interface. |
66 void CreateGnubbyConnection() override; | 71 void CreateGnubbyConnection() override; |
67 bool IsValidConnectionId(int gnubby_connection_id) const override; | 72 bool IsValidConnectionId(int gnubby_connection_id) const override; |
68 void SendClientResponse(int gnubby_connection_id, | 73 void SendClientResponse(int gnubby_connection_id, |
(...skipping 24 matching lines...) Expand all Loading... |
93 | 98 |
94 // Creates a unique name based on the well-known IPC channel name. | 99 // Creates a unique name based on the well-known IPC channel name. |
95 std::string GenerateUniqueChannelName(); | 100 std::string GenerateUniqueChannelName(); |
96 | 101 |
97 // Represents the last id assigned to a new security key request IPC channel. | 102 // Represents the last id assigned to a new security key request IPC channel. |
98 int last_connection_id_ = 0; | 103 int last_connection_id_ = 0; |
99 | 104 |
100 // Sends a gnubby extension messages to the remote client when called. | 105 // Sends a gnubby extension messages to the remote client when called. |
101 SendMessageCallback send_message_callback_; | 106 SendMessageCallback send_message_callback_; |
102 | 107 |
| 108 // Interface which provides details about the client session. |
| 109 ClientSessionDetails* client_session_details_ = nullptr; |
| 110 |
103 // Tracks the IPC channel created for each security key forwarding session. | 111 // Tracks the IPC channel created for each security key forwarding session. |
104 ActiveChannels active_channels_; | 112 ActiveChannels active_channels_; |
105 | 113 |
106 // The amount of time to wait for a client to process the connection details | 114 // The amount of time to wait for a client to process the connection details |
107 // message and disconnect from the IPC server channel before disconnecting it. | 115 // message and disconnect from the IPC server channel before disconnecting it. |
108 base::TimeDelta disconnect_timeout_; | 116 base::TimeDelta disconnect_timeout_; |
109 | 117 |
110 // Used to recreate the IPC server channel if a client forgets to disconnect. | 118 // Used to recreate the IPC server channel if a client forgets to disconnect. |
111 base::OneShotTimer timer_; | 119 base::OneShotTimer timer_; |
112 | 120 |
113 // IPC Clients connect to this channel first to receive their own IPC | 121 // IPC Clients connect to this channel first to receive their own IPC |
114 // channel to start a security key forwarding session on. | 122 // channel to start a security key forwarding session on. |
115 std::unique_ptr<IPC::Channel> ipc_server_channel_; | 123 std::unique_ptr<IPC::Channel> ipc_server_channel_; |
116 | 124 |
117 // Ensures GnubbyAuthHandlerWin methods are called on the same thread. | 125 // Ensures GnubbyAuthHandlerWin methods are called on the same thread. |
118 base::ThreadChecker thread_checker_; | 126 base::ThreadChecker thread_checker_; |
119 | 127 |
| 128 base::WeakPtrFactory<GnubbyAuthHandlerWin> weak_factory_; |
| 129 |
120 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerWin); | 130 DISALLOW_COPY_AND_ASSIGN(GnubbyAuthHandlerWin); |
121 }; | 131 }; |
122 | 132 |
123 std::unique_ptr<GnubbyAuthHandler> GnubbyAuthHandler::Create( | 133 std::unique_ptr<GnubbyAuthHandler> GnubbyAuthHandler::Create( |
124 const SendMessageCallback& callback) { | 134 ClientSessionDetails* client_session_details, |
125 std::unique_ptr<GnubbyAuthHandler> auth_handler(new GnubbyAuthHandlerWin()); | 135 const SendMessageCallback& send_message_callback) { |
126 auth_handler->SetSendMessageCallback(callback); | 136 std::unique_ptr<GnubbyAuthHandler> auth_handler( |
| 137 new GnubbyAuthHandlerWin(client_session_details)); |
| 138 auth_handler->SetSendMessageCallback(send_message_callback); |
127 return auth_handler; | 139 return auth_handler; |
128 } | 140 } |
129 | 141 |
130 GnubbyAuthHandlerWin::GnubbyAuthHandlerWin() | 142 GnubbyAuthHandlerWin::GnubbyAuthHandlerWin( |
131 : disconnect_timeout_( | 143 ClientSessionDetails* client_session_details) |
132 base::TimeDelta::FromSeconds(kInitialRequestTimeoutSeconds)) {} | 144 : client_session_details_(client_session_details), |
| 145 disconnect_timeout_( |
| 146 base::TimeDelta::FromSeconds(kInitialRequestTimeoutSeconds)), |
| 147 weak_factory_(this) { |
| 148 DCHECK(client_session_details_); |
| 149 } |
133 | 150 |
134 GnubbyAuthHandlerWin::~GnubbyAuthHandlerWin() {} | 151 GnubbyAuthHandlerWin::~GnubbyAuthHandlerWin() {} |
135 | 152 |
136 void GnubbyAuthHandlerWin::CreateGnubbyConnection() { | 153 void GnubbyAuthHandlerWin::CreateGnubbyConnection() { |
137 DCHECK(thread_checker_.CalledOnValidThread()); | 154 DCHECK(thread_checker_.CalledOnValidThread()); |
138 StartIpcServerChannel(); | 155 StartIpcServerChannel(); |
139 } | 156 } |
140 | 157 |
141 bool GnubbyAuthHandlerWin::IsValidConnectionId(int connection_id) const { | 158 bool GnubbyAuthHandlerWin::IsValidConnectionId(int connection_id) const { |
142 DCHECK(thread_checker_.CalledOnValidThread()); | 159 DCHECK(thread_checker_.CalledOnValidThread()); |
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
232 return false; | 249 return false; |
233 } | 250 } |
234 | 251 |
235 void GnubbyAuthHandlerWin::OnChannelConnected(int32_t peer_pid) { | 252 void GnubbyAuthHandlerWin::OnChannelConnected(int32_t peer_pid) { |
236 DCHECK(thread_checker_.CalledOnValidThread()); | 253 DCHECK(thread_checker_.CalledOnValidThread()); |
237 | 254 |
238 timer_.Start(FROM_HERE, disconnect_timeout_, | 255 timer_.Start(FROM_HERE, disconnect_timeout_, |
239 base::Bind(&GnubbyAuthHandlerWin::OnChannelError, | 256 base::Bind(&GnubbyAuthHandlerWin::OnChannelError, |
240 base::Unretained(this))); | 257 base::Unretained(this))); |
241 | 258 |
242 // TODO(joedow): Use |peer_pid| to determine the originating session | 259 // Verify the IPC connection attempt originated from the session we are |
243 // using ProcessIdToSessionId() and verify it is the one we created. | 260 // currently remoting. We don't want to service requests from arbitrary |
244 // Tracked via crbug.com/591746 | 261 // Windows sessions. |
| 262 bool close_connection = false; |
| 263 DWORD peer_session_id; |
| 264 if (!ProcessIdToSessionId(peer_pid, &peer_session_id)) { |
| 265 PLOG(ERROR) << "ProcessIdToSessionId() failed"; |
| 266 close_connection = true; |
| 267 } else if (peer_session_id != client_session_details_->desktop_session_id()) { |
| 268 LOG(INFO) << "Ignoring connection attempt from outside remoted session."; |
| 269 close_connection = true; |
| 270 } |
| 271 if (close_connection) { |
| 272 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 273 FROM_HERE, base::Bind(&GnubbyAuthHandlerWin::OnChannelError, |
| 274 weak_factory_.GetWeakPtr())); |
| 275 return; |
| 276 } |
245 | 277 |
246 int new_connection_id = ++last_connection_id_; | 278 int new_connection_id = ++last_connection_id_; |
247 std::unique_ptr<RemoteSecurityKeyIpcServer> ipc_server( | 279 std::unique_ptr<RemoteSecurityKeyIpcServer> ipc_server( |
248 RemoteSecurityKeyIpcServer::Create( | 280 RemoteSecurityKeyIpcServer::Create( |
249 new_connection_id, disconnect_timeout_, send_message_callback_, | 281 new_connection_id, peer_session_id, disconnect_timeout_, |
| 282 send_message_callback_, |
250 base::Bind(&GnubbyAuthHandlerWin::CloseSecurityKeyRequestIpcChannel, | 283 base::Bind(&GnubbyAuthHandlerWin::CloseSecurityKeyRequestIpcChannel, |
251 base::Unretained(this), new_connection_id))); | 284 base::Unretained(this), new_connection_id))); |
252 | 285 |
253 std::string unique_channel_name = GenerateUniqueChannelName(); | 286 std::string unique_channel_name = GenerateUniqueChannelName(); |
254 if (ipc_server->CreateChannel( | 287 if (ipc_server->CreateChannel( |
255 unique_channel_name, | 288 unique_channel_name, |
256 base::TimeDelta::FromSeconds(kGnubbyRequestTimeoutSeconds))) { | 289 base::TimeDelta::FromSeconds(kGnubbyRequestTimeoutSeconds))) { |
257 active_channels_[new_connection_id] = std::move(ipc_server); | 290 active_channels_[new_connection_id] = std::move(ipc_server); |
258 ipc_server_channel_->Send( | 291 ipc_server_channel_->Send( |
259 new ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails( | 292 new ChromotingNetworkToRemoteSecurityKeyMsg_ConnectionDetails( |
260 unique_channel_name)); | 293 unique_channel_name)); |
261 } | 294 } |
262 } | 295 } |
263 | 296 |
264 void GnubbyAuthHandlerWin::OnChannelError() { | 297 void GnubbyAuthHandlerWin::OnChannelError() { |
265 DCHECK(thread_checker_.CalledOnValidThread()); | 298 DCHECK(thread_checker_.CalledOnValidThread()); |
266 | 299 |
267 // Could be an error, most likely the client disconnected though. Either way | 300 // Could be an error, most likely the client disconnected though. Either way |
268 // we should restart the server to prepare for the next connection. | 301 // we should restart the server to prepare for the next connection. |
269 RecreateIpcServerChannel(); | 302 RecreateIpcServerChannel(); |
270 } | 303 } |
271 | 304 |
272 } // namespace remoting | 305 } // namespace remoting |
OLD | NEW |