Chromium Code Reviews| 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/security_key_auth_handler.h" | 5 #include "remoting/host/security_key/security_key_auth_handler.h" |
| 6 | 6 |
| 7 #include <stdint.h> | |
| 8 #include <unistd.h> | 7 #include <unistd.h> |
| 9 | 8 |
| 9 #include <cstdint> | |
| 10 #include <map> | |
| 10 #include <memory> | 11 #include <memory> |
| 12 #include <string> | |
| 13 #include <utility> | |
| 11 | 14 |
| 12 #include "base/bind.h" | 15 #include "base/bind.h" |
| 16 #include "base/callback.h" | |
| 17 #include "base/files/file_path.h" | |
| 13 #include "base/files/file_util.h" | 18 #include "base/files/file_util.h" |
| 14 #include "base/lazy_instance.h" | 19 #include "base/lazy_instance.h" |
| 20 #include "base/location.h" | |
| 15 #include "base/logging.h" | 21 #include "base/logging.h" |
| 16 #include "base/stl_util.h" | 22 #include "base/memory/ptr_util.h" |
| 23 #include "base/memory/ref_counted.h" | |
| 24 #include "base/memory/weak_ptr.h" | |
| 25 #include "base/single_thread_task_runner.h" | |
| 17 #include "base/threading/thread_checker.h" | 26 #include "base/threading/thread_checker.h" |
| 18 #include "base/threading/thread_restrictions.h" | |
| 19 #include "base/values.h" | |
| 20 #include "net/base/completion_callback.h" | 27 #include "net/base/completion_callback.h" |
| 21 #include "net/base/net_errors.h" | 28 #include "net/base/net_errors.h" |
| 22 #include "net/socket/stream_socket.h" | 29 #include "net/socket/stream_socket.h" |
| 23 #include "net/socket/unix_domain_server_socket_posix.h" | 30 #include "net/socket/unix_domain_server_socket_posix.h" |
| 24 #include "remoting/base/logging.h" | 31 #include "remoting/base/logging.h" |
| 25 #include "remoting/host/security_key/security_key_socket.h" | 32 #include "remoting/host/security_key/security_key_socket.h" |
| 26 | 33 |
| 27 namespace { | 34 namespace { |
| 28 | 35 |
| 29 const int64_t kDefaultRequestTimeoutSeconds = 60; | 36 const int64_t kDefaultRequestTimeoutSeconds = 60; |
| 30 | 37 |
| 31 // The name of the socket to listen for security key requests on. | 38 // The name of the socket to listen for security key requests on. |
| 32 base::LazyInstance<base::FilePath>::Leaky g_security_key_socket_name = | 39 base::LazyInstance<base::FilePath>::Leaky g_security_key_socket_name = |
| 33 LAZY_INSTANCE_INITIALIZER; | 40 LAZY_INSTANCE_INITIALIZER; |
| 34 | 41 |
| 42 void DeleteExistingSocketFile() { | |
| 43 // If the file already exists, a socket in use error is returned. | |
| 44 base::DeleteFile(g_security_key_socket_name.Get(), false); | |
| 45 } | |
| 46 | |
| 35 // Socket authentication function that only allows connections from callers with | 47 // Socket authentication function that only allows connections from callers with |
| 36 // the current uid. | 48 // the current uid. |
| 37 bool MatchUid(const net::UnixDomainServerSocket::Credentials& credentials) { | 49 bool MatchUid(const net::UnixDomainServerSocket::Credentials& credentials) { |
| 38 bool allowed = credentials.user_id == getuid(); | 50 bool allowed = credentials.user_id == getuid(); |
| 39 if (!allowed) | 51 if (!allowed) |
| 40 HOST_LOG << "Refused socket connection from uid " << credentials.user_id; | 52 HOST_LOG << "Refused socket connection from uid " << credentials.user_id; |
| 41 return allowed; | 53 return allowed; |
| 42 } | 54 } |
| 43 | 55 |
| 44 // Returns the command code (the first byte of the data) if it exists, or -1 if | 56 // Returns the command code (the first byte of the data) if it exists, or -1 if |
| 45 // the data is empty. | 57 // the data is empty. |
| 46 unsigned int GetCommandCode(const std::string& data) { | 58 unsigned int GetCommandCode(const std::string& data) { |
| 47 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); | 59 return data.empty() ? -1 : static_cast<unsigned int>(data[0]); |
| 48 } | 60 } |
| 49 | 61 |
| 50 } // namespace | 62 } // namespace |
| 51 | 63 |
| 52 namespace remoting { | 64 namespace remoting { |
| 53 | 65 |
| 54 class SecurityKeyAuthHandlerLinux : public SecurityKeyAuthHandler { | 66 class SecurityKeyAuthHandlerLinux : public SecurityKeyAuthHandler { |
| 55 public: | 67 public: |
| 56 SecurityKeyAuthHandlerLinux(); | 68 explicit SecurityKeyAuthHandlerLinux( |
| 69 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner); | |
| 57 ~SecurityKeyAuthHandlerLinux() override; | 70 ~SecurityKeyAuthHandlerLinux() override; |
| 58 | 71 |
| 59 private: | 72 private: |
| 60 typedef std::map<int, SecurityKeySocket*> ActiveSockets; | 73 typedef std::map<int, std::unique_ptr<SecurityKeySocket>> ActiveSockets; |
| 61 | 74 |
| 62 // SecurityKeyAuthHandler interface. | 75 // SecurityKeyAuthHandler interface. |
| 63 void CreateSecurityKeyConnection() override; | 76 void CreateSecurityKeyConnection() override; |
| 64 bool IsValidConnectionId(int security_key_connection_id) const override; | 77 bool IsValidConnectionId(int security_key_connection_id) const override; |
| 65 void SendClientResponse(int security_key_connection_id, | 78 void SendClientResponse(int security_key_connection_id, |
| 66 const std::string& response) override; | 79 const std::string& response) override; |
| 67 void SendErrorAndCloseConnection(int security_key_connection_id) override; | 80 void SendErrorAndCloseConnection(int security_key_connection_id) override; |
| 68 void SetSendMessageCallback(const SendMessageCallback& callback) override; | 81 void SetSendMessageCallback(const SendMessageCallback& callback) override; |
| 69 size_t GetActiveConnectionCountForTest() const override; | 82 size_t GetActiveConnectionCountForTest() const override; |
| 70 void SetRequestTimeoutForTest(base::TimeDelta timeout) override; | 83 void SetRequestTimeoutForTest(base::TimeDelta timeout) override; |
| 71 | 84 |
| 85 // Sets up the socket used for accepting new connections. | |
| 86 void CreateSocket(); | |
| 87 | |
| 72 // Starts listening for connection. | 88 // Starts listening for connection. |
| 73 void DoAccept(); | 89 void DoAccept(); |
| 74 | 90 |
| 75 // Called when a connection is accepted. | 91 // Called when a connection is accepted. |
| 76 void OnAccepted(int result); | 92 void OnAccepted(int result); |
| 77 | 93 |
| 78 // Called when a SecurityKeySocket has done reading. | 94 // Called when a SecurityKeySocket has done reading. |
| 79 void OnReadComplete(int security_key_connection_id); | 95 void OnReadComplete(int security_key_connection_id); |
| 80 | 96 |
| 81 // Gets an active socket iterator for |security_key_connection_id|. | 97 // Gets an active socket iterator for |security_key_connection_id|. |
| (...skipping 12 matching lines...) Expand all Loading... | |
| 94 // Socket used to listen for authorization requests. | 110 // Socket used to listen for authorization requests. |
| 95 std::unique_ptr<net::UnixDomainServerSocket> auth_socket_; | 111 std::unique_ptr<net::UnixDomainServerSocket> auth_socket_; |
| 96 | 112 |
| 97 // A temporary holder for an accepted connection. | 113 // A temporary holder for an accepted connection. |
| 98 std::unique_ptr<net::StreamSocket> accept_socket_; | 114 std::unique_ptr<net::StreamSocket> accept_socket_; |
| 99 | 115 |
| 100 // Used to pass security key extension messages to the client. | 116 // Used to pass security key extension messages to the client. |
| 101 SendMessageCallback send_message_callback_; | 117 SendMessageCallback send_message_callback_; |
| 102 | 118 |
| 103 // The last assigned security key connection id. | 119 // The last assigned security key connection id. |
| 104 int last_connection_id_; | 120 int last_connection_id_ = 0; |
| 105 | 121 |
| 106 // Sockets by connection id used to process gnubbyd requests. | 122 // Sockets by connection id used to process gnubbyd requests. |
| 107 ActiveSockets active_sockets_; | 123 ActiveSockets active_sockets_; |
| 108 | 124 |
| 125 // Used to perform File IO via Unix Domain Sockets. | |
| 126 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner_; | |
| 127 | |
| 109 // Timeout used for a request. | 128 // Timeout used for a request. |
| 110 base::TimeDelta request_timeout_; | 129 base::TimeDelta request_timeout_; |
| 111 | 130 |
| 131 base::WeakPtrFactory<SecurityKeyAuthHandlerLinux> weak_factory_; | |
| 132 | |
| 112 DISALLOW_COPY_AND_ASSIGN(SecurityKeyAuthHandlerLinux); | 133 DISALLOW_COPY_AND_ASSIGN(SecurityKeyAuthHandlerLinux); |
| 113 }; | 134 }; |
| 114 | 135 |
| 115 std::unique_ptr<SecurityKeyAuthHandler> SecurityKeyAuthHandler::Create( | 136 std::unique_ptr<SecurityKeyAuthHandler> SecurityKeyAuthHandler::Create( |
| 116 ClientSessionDetails* client_session_details, | 137 ClientSessionDetails* client_session_details, |
| 117 const SendMessageCallback& send_message_callback) { | 138 const SendMessageCallback& send_message_callback, |
| 139 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) { | |
| 118 std::unique_ptr<SecurityKeyAuthHandler> auth_handler( | 140 std::unique_ptr<SecurityKeyAuthHandler> auth_handler( |
| 119 new SecurityKeyAuthHandlerLinux()); | 141 new SecurityKeyAuthHandlerLinux(file_task_runner)); |
| 120 auth_handler->SetSendMessageCallback(send_message_callback); | 142 auth_handler->SetSendMessageCallback(send_message_callback); |
| 121 return auth_handler; | 143 return auth_handler; |
| 122 } | 144 } |
| 123 | 145 |
| 124 void SecurityKeyAuthHandler::SetSecurityKeySocketName( | 146 void SecurityKeyAuthHandler::SetSecurityKeySocketName( |
| 125 const base::FilePath& security_key_socket_name) { | 147 const base::FilePath& security_key_socket_name) { |
| 126 g_security_key_socket_name.Get() = security_key_socket_name; | 148 g_security_key_socket_name.Get() = security_key_socket_name; |
| 127 } | 149 } |
| 128 | 150 |
| 129 SecurityKeyAuthHandlerLinux::SecurityKeyAuthHandlerLinux() | 151 SecurityKeyAuthHandlerLinux::SecurityKeyAuthHandlerLinux( |
| 130 : last_connection_id_(0), | 152 scoped_refptr<base::SingleThreadTaskRunner> file_task_runner) |
| 153 : file_task_runner_(file_task_runner), | |
| 131 request_timeout_( | 154 request_timeout_( |
| 132 base::TimeDelta::FromSeconds(kDefaultRequestTimeoutSeconds)) {} | 155 base::TimeDelta::FromSeconds(kDefaultRequestTimeoutSeconds)), |
| 156 weak_factory_(this) {} | |
| 133 | 157 |
| 134 SecurityKeyAuthHandlerLinux::~SecurityKeyAuthHandlerLinux() { | 158 SecurityKeyAuthHandlerLinux::~SecurityKeyAuthHandlerLinux() {} |
| 135 STLDeleteValues(&active_sockets_); | |
| 136 } | |
| 137 | 159 |
| 138 void SecurityKeyAuthHandlerLinux::CreateSecurityKeyConnection() { | 160 void SecurityKeyAuthHandlerLinux::CreateSecurityKeyConnection() { |
| 139 DCHECK(thread_checker_.CalledOnValidThread()); | 161 DCHECK(thread_checker_.CalledOnValidThread()); |
| 140 DCHECK(!g_security_key_socket_name.Get().empty()); | 162 DCHECK(!g_security_key_socket_name.Get().empty()); |
| 141 | 163 |
| 142 { | 164 file_task_runner_->PostTaskAndReply( |
|
Sergey Ulanov
2016/07/25 18:29:55
add a comment to explain why this is necessary.
joedow
2016/07/25 22:39:25
Done.
| |
| 143 // DeleteFile() is a blocking operation, but so is creation of the unix | 165 FROM_HERE, base::Bind(&DeleteExistingSocketFile), |
|
Sergey Ulanov
2016/07/25 18:29:55
nit: here you can post task for base::DeleteFile()
joedow
2016/07/25 22:39:25
Done.
| |
| 144 // socket below. Consider moving this class to a different thread if this | 166 base::Bind(&SecurityKeyAuthHandlerLinux::CreateSocket, |
| 145 // causes any problems. See crbug.com/509807. | 167 weak_factory_.GetWeakPtr())); |
| 146 // TODO(joedow): Since this code now runs as a host extension, we should | 168 } |
| 147 // perform our IO on a separate thread: crbug.com/591739 | |
| 148 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
| 149 | 169 |
| 150 // If the file already exists, a socket in use error is returned. | 170 void SecurityKeyAuthHandlerLinux::CreateSocket() { |
| 151 base::DeleteFile(g_security_key_socket_name.Get(), false); | 171 DCHECK(thread_checker_.CalledOnValidThread()); |
| 152 } | |
| 153 | |
| 154 HOST_LOG << "Listening for security key requests on " | 172 HOST_LOG << "Listening for security key requests on " |
| 155 << g_security_key_socket_name.Get().value(); | 173 << g_security_key_socket_name.Get().value(); |
| 156 | 174 |
| 157 auth_socket_.reset( | 175 auth_socket_.reset( |
| 158 new net::UnixDomainServerSocket(base::Bind(MatchUid), false)); | 176 new net::UnixDomainServerSocket(base::Bind(MatchUid), false)); |
| 159 int rv = auth_socket_->BindAndListen(g_security_key_socket_name.Get().value(), | 177 int rv = auth_socket_->BindAndListen(g_security_key_socket_name.Get().value(), |
| 160 /*backlog=*/1); | 178 /*backlog=*/1); |
| 161 if (rv != net::OK) { | 179 if (rv != net::OK) { |
| 162 LOG(ERROR) << "Failed to open socket for auth requests: '" << rv << "'"; | 180 LOG(ERROR) << "Failed to open socket for auth requests: '" << rv << "'"; |
| 163 return; | 181 return; |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 205 size_t SecurityKeyAuthHandlerLinux::GetActiveConnectionCountForTest() const { | 223 size_t SecurityKeyAuthHandlerLinux::GetActiveConnectionCountForTest() const { |
| 206 return active_sockets_.size(); | 224 return active_sockets_.size(); |
| 207 } | 225 } |
| 208 | 226 |
| 209 void SecurityKeyAuthHandlerLinux::SetRequestTimeoutForTest( | 227 void SecurityKeyAuthHandlerLinux::SetRequestTimeoutForTest( |
| 210 base::TimeDelta timeout) { | 228 base::TimeDelta timeout) { |
| 211 request_timeout_ = timeout; | 229 request_timeout_ = timeout; |
| 212 } | 230 } |
| 213 | 231 |
| 214 void SecurityKeyAuthHandlerLinux::DoAccept() { | 232 void SecurityKeyAuthHandlerLinux::DoAccept() { |
| 233 DCHECK(thread_checker_.CalledOnValidThread()); | |
| 215 int result = auth_socket_->Accept( | 234 int result = auth_socket_->Accept( |
| 216 &accept_socket_, base::Bind(&SecurityKeyAuthHandlerLinux::OnAccepted, | 235 &accept_socket_, base::Bind(&SecurityKeyAuthHandlerLinux::OnAccepted, |
| 217 base::Unretained(this))); | 236 weak_factory_.GetWeakPtr())); |
| 218 if (result != net::ERR_IO_PENDING) | 237 if (result != net::ERR_IO_PENDING) |
| 219 OnAccepted(result); | 238 OnAccepted(result); |
| 220 } | 239 } |
| 221 | 240 |
| 222 void SecurityKeyAuthHandlerLinux::OnAccepted(int result) { | 241 void SecurityKeyAuthHandlerLinux::OnAccepted(int result) { |
| 223 DCHECK(thread_checker_.CalledOnValidThread()); | 242 DCHECK(thread_checker_.CalledOnValidThread()); |
| 224 DCHECK_NE(net::ERR_IO_PENDING, result); | 243 DCHECK_NE(net::ERR_IO_PENDING, result); |
| 225 | 244 |
| 226 if (result < 0) { | 245 if (result < 0) { |
| 227 LOG(ERROR) << "Error in accepting a new connection"; | 246 LOG(ERROR) << "Error in accepting a new connection"; |
| 228 return; | 247 return; |
| 229 } | 248 } |
| 230 | 249 |
| 231 int security_key_connection_id = ++last_connection_id_; | 250 int security_key_connection_id = ++last_connection_id_; |
| 232 SecurityKeySocket* socket = new SecurityKeySocket( | 251 SecurityKeySocket* socket = new SecurityKeySocket( |
| 233 std::move(accept_socket_), request_timeout_, | 252 std::move(accept_socket_), request_timeout_, |
| 234 base::Bind(&SecurityKeyAuthHandlerLinux::RequestTimedOut, | 253 base::Bind(&SecurityKeyAuthHandlerLinux::RequestTimedOut, |
| 235 base::Unretained(this), security_key_connection_id)); | 254 weak_factory_.GetWeakPtr(), security_key_connection_id)); |
|
Sergey Ulanov
2016/07/25 18:29:55
nit: don't really need to use weak-ptrs here becau
joedow
2016/07/25 22:39:25
Done. I was thinking this would be safer in case
| |
| 236 active_sockets_[security_key_connection_id] = socket; | 255 active_sockets_[security_key_connection_id] = base::WrapUnique(socket); |
| 237 socket->StartReadingRequest( | 256 socket->StartReadingRequest( |
| 238 base::Bind(&SecurityKeyAuthHandlerLinux::OnReadComplete, | 257 base::Bind(&SecurityKeyAuthHandlerLinux::OnReadComplete, |
| 239 base::Unretained(this), security_key_connection_id)); | 258 weak_factory_.GetWeakPtr(), security_key_connection_id)); |
| 240 | 259 |
| 241 // Continue accepting new connections. | 260 // Continue accepting new connections. |
| 242 DoAccept(); | 261 DoAccept(); |
| 243 } | 262 } |
| 244 | 263 |
| 245 void SecurityKeyAuthHandlerLinux::OnReadComplete( | 264 void SecurityKeyAuthHandlerLinux::OnReadComplete( |
| 246 int security_key_connection_id) { | 265 int security_key_connection_id) { |
| 247 DCHECK(thread_checker_.CalledOnValidThread()); | 266 DCHECK(thread_checker_.CalledOnValidThread()); |
| 248 | 267 |
| 249 ActiveSockets::const_iterator iter = | 268 ActiveSockets::const_iterator iter = |
| 250 active_sockets_.find(security_key_connection_id); | 269 active_sockets_.find(security_key_connection_id); |
| 251 DCHECK(iter != active_sockets_.end()); | 270 DCHECK(iter != active_sockets_.end()); |
| 252 std::string request_data; | 271 std::string request_data; |
| 253 if (!iter->second->GetAndClearRequestData(&request_data)) { | 272 if (!iter->second->GetAndClearRequestData(&request_data)) { |
| 254 SendErrorAndCloseActiveSocket(iter); | 273 SendErrorAndCloseActiveSocket(iter); |
| 255 return; | 274 return; |
| 256 } | 275 } |
| 257 | 276 |
| 258 HOST_LOG << "Received security key request: " << GetCommandCode(request_data); | 277 HOST_LOG << "Received security key request: " << GetCommandCode(request_data); |
| 259 send_message_callback_.Run(security_key_connection_id, request_data); | 278 send_message_callback_.Run(security_key_connection_id, request_data); |
| 260 | 279 |
| 261 iter->second->StartReadingRequest( | 280 iter->second->StartReadingRequest( |
| 262 base::Bind(&SecurityKeyAuthHandlerLinux::OnReadComplete, | 281 base::Bind(&SecurityKeyAuthHandlerLinux::OnReadComplete, |
| 263 base::Unretained(this), security_key_connection_id)); | 282 weak_factory_.GetWeakPtr(), security_key_connection_id)); |
| 264 } | 283 } |
| 265 | 284 |
| 266 SecurityKeyAuthHandlerLinux::ActiveSockets::const_iterator | 285 SecurityKeyAuthHandlerLinux::ActiveSockets::const_iterator |
| 267 SecurityKeyAuthHandlerLinux::GetSocketForConnectionId( | 286 SecurityKeyAuthHandlerLinux::GetSocketForConnectionId( |
| 268 int security_key_connection_id) const { | 287 int security_key_connection_id) const { |
| 269 return active_sockets_.find(security_key_connection_id); | 288 return active_sockets_.find(security_key_connection_id); |
| 270 } | 289 } |
| 271 | 290 |
| 272 void SecurityKeyAuthHandlerLinux::SendErrorAndCloseActiveSocket( | 291 void SecurityKeyAuthHandlerLinux::SendErrorAndCloseActiveSocket( |
| 273 const ActiveSockets::const_iterator& iter) { | 292 const ActiveSockets::const_iterator& iter) { |
| 274 iter->second->SendSshError(); | 293 iter->second->SendSshError(); |
| 275 delete iter->second; | |
| 276 active_sockets_.erase(iter); | 294 active_sockets_.erase(iter); |
| 277 } | 295 } |
| 278 | 296 |
| 279 void SecurityKeyAuthHandlerLinux::RequestTimedOut( | 297 void SecurityKeyAuthHandlerLinux::RequestTimedOut( |
| 280 int security_key_connection_id) { | 298 int security_key_connection_id) { |
| 281 HOST_LOG << "SecurityKey request timed out"; | 299 HOST_LOG << "SecurityKey request timed out"; |
| 282 ActiveSockets::const_iterator iter = | 300 ActiveSockets::const_iterator iter = |
| 283 active_sockets_.find(security_key_connection_id); | 301 active_sockets_.find(security_key_connection_id); |
| 284 if (iter != active_sockets_.end()) | 302 if (iter != active_sockets_.end()) |
| 285 SendErrorAndCloseActiveSocket(iter); | 303 SendErrorAndCloseActiveSocket(iter); |
| 286 } | 304 } |
| 287 | 305 |
| 288 } // namespace remoting | 306 } // namespace remoting |
| OLD | NEW |