blob: e6bf8777c00ee0d65e36b091d0711c23905bc023 [file] [log] [blame]
Andreea Costinas942284d2020-01-28 16:28:40 +01001// Copyright 2020 The Chromium OS Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4#include "system-proxy/system_proxy_adaptor.h"
5
6#include <string>
7#include <utility>
8#include <vector>
9
10#include <base/location.h>
Andreea Costinase9c73592020-07-17 15:27:54 +020011#include <base/strings/stringprintf.h>
Andreea Costinas91f75352020-07-08 14:47:47 +020012#include <base/time/time.h>
Andreea Costinas942284d2020-01-28 16:28:40 +010013#include <brillo/dbus/dbus_object.h>
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010014#include <brillo/message_loops/message_loop.h>
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020015#include <chromeos/dbus/service_constants.h>
Jason Jeremy Imanadffbcb2020-08-31 13:21:36 +090016#include <chromeos/patchpanel/dbus/client.h>
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020017#include <dbus/object_proxy.h>
Andreea Costinas942284d2020-01-28 16:28:40 +010018
Andreea Costinas922fbaf2020-05-28 11:55:22 +020019#include "system-proxy/kerberos_client.h"
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010020#include "system-proxy/sandboxed_worker.h"
Andreea Costinas942284d2020-01-28 16:28:40 +010021
22namespace system_proxy {
23namespace {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010024
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020025constexpr int kProxyPort = 3128;
Andreea Costinas77b180e2020-05-12 15:17:32 +020026constexpr char kFailedToStartWorkerError[] = "Failed to start worker process";
Andreea Costinas91f75352020-07-08 14:47:47 +020027// Time delay for calling patchpanel::ConnectNamespace(). Patchpanel needs to
28// enter the network namespace of the worker process to configure it and fails
29// if it's soon after the process starts. See https://crbug.com/1095170 for
30// details.
31constexpr base::TimeDelta kConnectNamespaceDelay =
32 base::TimeDelta::FromSeconds(1);
33constexpr int kNetworkNamespaceReconnectAttempts = 3;
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020034
Andreea Costinas942284d2020-01-28 16:28:40 +010035// Serializes |proto| to a vector of bytes.
36std::vector<uint8_t> SerializeProto(
37 const google::protobuf::MessageLite& proto) {
38 std::vector<uint8_t> proto_blob(proto.ByteSizeLong());
Andreea Costinasc991e232020-06-08 20:30:58 +020039 bool result = proto.SerializeToArray(proto_blob.data(), proto_blob.size());
40 DCHECK(result);
Andreea Costinas942284d2020-01-28 16:28:40 +010041 return proto_blob;
42}
43
44// Parses a proto from an array of bytes |proto_blob|. Returns
45// ERROR_PARSE_REQUEST_FAILED on error.
46std::string DeserializeProto(const base::Location& from_here,
47 google::protobuf::MessageLite* proto,
48 const std::vector<uint8_t>& proto_blob) {
49 if (!proto->ParseFromArray(proto_blob.data(), proto_blob.size())) {
50 const std::string error_message = "Failed to parse proto message.";
51 LOG(ERROR) << from_here.ToString() << error_message;
52 return error_message;
53 }
54 return "";
55}
56} // namespace
57
58SystemProxyAdaptor::SystemProxyAdaptor(
59 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object)
60 : org::chromium::SystemProxyAdaptor(this),
Andreea Costinas91f75352020-07-08 14:47:47 +020061 netns_reconnect_attempts_available_(kNetworkNamespaceReconnectAttempts),
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010062 dbus_object_(std::move(dbus_object)),
Andreea Costinas922fbaf2020-05-28 11:55:22 +020063 weak_ptr_factory_(this) {
64 kerberos_client_ = std::make_unique<KerberosClient>(dbus_object_->GetBus());
65}
Andreea Costinas942284d2020-01-28 16:28:40 +010066
67SystemProxyAdaptor::~SystemProxyAdaptor() = default;
68
69void SystemProxyAdaptor::RegisterAsync(
70 const brillo::dbus_utils::AsyncEventSequencer::CompletionAction&
71 completion_callback) {
72 RegisterWithDBusObject(dbus_object_.get());
73 dbus_object_->RegisterAsync(completion_callback);
74}
75
Andreea Costinas77b180e2020-05-12 15:17:32 +020076std::vector<uint8_t> SystemProxyAdaptor::SetAuthenticationDetails(
77 const std::vector<uint8_t>& request_blob) {
78 LOG(INFO) << "Received set authentication details request.";
79
80 SetAuthenticationDetailsRequest request;
Andreea Costinas350e4aa2020-07-20 20:29:46 +020081 std::string error_message =
Andreea Costinas77b180e2020-05-12 15:17:32 +020082 DeserializeProto(FROM_HERE, &request, request_blob);
83
84 SetAuthenticationDetailsResponse response;
85 if (!error_message.empty()) {
86 response.set_error_message(error_message);
87 return SerializeProto(response);
88 }
89
Andreea Costinas350e4aa2020-07-20 20:29:46 +020090 if (IncludesSystemTraffic(request.traffic_type())) {
91 SetAuthenticationDetails(request, /*user_traffic=*/false, &error_message);
92 }
93 if (IncludesUserTraffic(request.traffic_type())) {
94 SetAuthenticationDetails(request, /*user_traffic=*/true, &error_message);
95 }
96 if (!error_message.empty()) {
97 response.set_error_message(error_message);
98 }
99 return SerializeProto(response);
100}
101
102void SystemProxyAdaptor::SetAuthenticationDetails(
103 SetAuthenticationDetailsRequest auth_details,
104 bool user_traffic,
105 std::string* error_message) {
106 SandboxedWorker* worker = CreateWorkerIfNeeded(user_traffic);
107 if (!worker) {
108 error_message->append(kFailedToStartWorkerError);
109 return;
Andreea Costinas77b180e2020-05-12 15:17:32 +0200110 }
111
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200112 if (auth_details.has_credentials() || auth_details.has_protection_space()) {
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200113 worker::Credentials credentials;
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200114 if (auth_details.has_protection_space()) {
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200115 worker::ProtectionSpace protection_space;
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200116 protection_space.set_origin(auth_details.protection_space().origin());
117 protection_space.set_scheme(auth_details.protection_space().scheme());
118 protection_space.set_realm(auth_details.protection_space().realm());
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200119 *credentials.mutable_protection_space() = protection_space;
120 }
Andreea Costinas77b180e2020-05-12 15:17:32 +0200121
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200122 if (auth_details.has_credentials()) {
123 if (auth_details.credentials().has_username() &&
124 auth_details.credentials().has_password()) {
125 credentials.set_username(auth_details.credentials().username());
126 credentials.set_password(auth_details.credentials().password());
127 }
128 }
129
130 brillo::MessageLoop::current()->PostTask(
131 FROM_HERE,
132 base::Bind(&SystemProxyAdaptor::SetCredentialsTask,
133 weak_ptr_factory_.GetWeakPtr(), worker, credentials));
134 }
135 if (auth_details.has_kerberos_enabled()) {
136 std::string principal_name = auth_details.has_active_principal_name()
137 ? auth_details.active_principal_name()
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200138 : std::string();
139
140 brillo::MessageLoop::current()->PostTask(
141 FROM_HERE, base::Bind(&SystemProxyAdaptor::SetKerberosEnabledTask,
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200142 weak_ptr_factory_.GetWeakPtr(), worker,
143 auth_details.kerberos_enabled(), principal_name));
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200144 }
Andreea Costinas77b180e2020-05-12 15:17:32 +0200145}
146
Andreea Costinasfc3dc7d2020-07-20 18:54:38 +0200147// TODO(acostinas, crbug.com/1109144): Deprecated in favor of |ShutDownProcess|.
Andreea Costinas942284d2020-01-28 16:28:40 +0100148std::vector<uint8_t> SystemProxyAdaptor::ShutDown() {
149 LOG(INFO) << "Received shutdown request.";
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100150
151 std::string error_message;
Andreea Costinase9c73592020-07-17 15:27:54 +0200152 if (!ResetWorker(/* user_traffic=*/false)) {
153 error_message =
154 "Failure to terminate worker process for system services traffic.";
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100155 }
156
Andreea Costinase9c73592020-07-17 15:27:54 +0200157 if (!ResetWorker(/* user_traffic=*/true)) {
158 error_message += "Failure to terminate worker process for arc traffic.";
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100159 }
160
Andreea Costinas942284d2020-01-28 16:28:40 +0100161 ShutDownResponse response;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100162 if (!error_message.empty())
163 response.set_error_message(error_message);
164
165 brillo::MessageLoop::current()->PostTask(
166 FROM_HERE, base::Bind(&SystemProxyAdaptor::ShutDownTask,
167 weak_ptr_factory_.GetWeakPtr()));
168
Andreea Costinas942284d2020-01-28 16:28:40 +0100169 return SerializeProto(response);
170}
171
Andreea Costinase9c73592020-07-17 15:27:54 +0200172std::vector<uint8_t> SystemProxyAdaptor::ClearUserCredentials(
173 const std::vector<uint8_t>& request_blob) {
174 LOG(INFO) << "Received request to clear user credentials.";
175 std::string error_message;
176 ClearUserCredentials(/*user_traffic=*/false, &error_message);
177 ClearUserCredentials(/*user_traffic=*/true, &error_message);
178
179 ClearUserCredentialsResponse response;
180 if (!error_message.empty())
181 response.set_error_message(error_message);
182 return SerializeProto(response);
183}
184
185void SystemProxyAdaptor::ClearUserCredentials(bool user_traffic,
186 std::string* error_message) {
187 SandboxedWorker* worker = GetWorker(user_traffic);
188 if (!worker) {
189 return;
190 }
191 if (!worker->ClearUserCredentials()) {
192 error_message->append(
193 base::StringPrintf("Failure to clear user credentials for worker with "
194 "pid %s. Restarting worker.",
195 std::to_string(worker->pid()).c_str()));
196 ResetWorker(user_traffic);
197 CreateWorkerIfNeeded(user_traffic);
198 }
199}
200
Andreea Costinasfc3dc7d2020-07-20 18:54:38 +0200201std::vector<uint8_t> SystemProxyAdaptor::ShutDownProcess(
202 const std::vector<uint8_t>& request_blob) {
203 LOG(INFO) << "Received shutdown request.";
204 ShutDownRequest request;
205 std::string error_message =
206 DeserializeProto(FROM_HERE, &request, request_blob);
207
208 if (IncludesSystemTraffic(request.traffic_type()) &&
209 !ResetWorker(/* user_traffic=*/false)) {
210 error_message =
211 "Failure to terminate worker process for system services traffic.";
212 }
213
214 if (IncludesUserTraffic(request.traffic_type()) &&
215 !ResetWorker(/* user_traffic=*/true)) {
216 error_message += "Failure to terminate worker process for arc traffic.";
217 }
218
219 ShutDownResponse response;
220 if (!error_message.empty())
221 response.set_error_message(error_message);
222
223 if (request.traffic_type() == TrafficOrigin::ALL) {
224 brillo::MessageLoop::current()->PostTask(
225 FROM_HERE, base::Bind(&SystemProxyAdaptor::ShutDownTask,
226 weak_ptr_factory_.GetWeakPtr()));
227 }
228 return SerializeProto(response);
229}
230
Andreea Costinas5862b102020-03-19 14:45:36 +0100231void SystemProxyAdaptor::GetChromeProxyServersAsync(
232 const std::string& target_url,
233 const brillo::http::GetChromeProxyServersCallback& callback) {
Andreea Costinasc9defae2020-04-22 10:28:35 +0200234 brillo::http::GetChromeProxyServersAsync(dbus_object_->GetBus(), target_url,
235 move(callback));
Andreea Costinas5862b102020-03-19 14:45:36 +0100236}
237
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200238bool SystemProxyAdaptor::IsLocalProxy(const std::string& proxy) {
239 if (system_services_worker_ &&
240 proxy.find(system_services_worker_->local_proxy_host_and_port()) !=
241 std::string::npos) {
242 return true;
243 }
244 return arc_worker_ && proxy.find(arc_worker_->local_proxy_host_and_port()) !=
245 std::string::npos;
246}
247
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100248std::unique_ptr<SandboxedWorker> SystemProxyAdaptor::CreateWorker() {
Andreea Costinas5862b102020-03-19 14:45:36 +0100249 return std::make_unique<SandboxedWorker>(weak_ptr_factory_.GetWeakPtr());
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100250}
251
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200252SandboxedWorker* SystemProxyAdaptor::CreateWorkerIfNeeded(bool user_traffic) {
253 SandboxedWorker* worker = GetWorker(user_traffic);
254 if (worker) {
255 // A worker for traffic indicated by |user_traffic| already exists.
256 return worker;
Andreea Costinas77b180e2020-05-12 15:17:32 +0200257 }
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200258 SetWorker(user_traffic, CreateWorker());
259 worker = GetWorker(user_traffic);
Andreea Costinas77b180e2020-05-12 15:17:32 +0200260
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200261 if (!worker->Start()) {
262 ResetWorker(user_traffic);
263 return nullptr;
Andreea Costinas77b180e2020-05-12 15:17:32 +0200264 }
265 // patchpanel_proxy is owned by |dbus_object_->bus_|.
266 dbus::ObjectProxy* patchpanel_proxy = dbus_object_->GetBus()->GetObjectProxy(
267 patchpanel::kPatchPanelServiceName,
268 dbus::ObjectPath(patchpanel::kPatchPanelServicePath));
269 patchpanel_proxy->WaitForServiceToBeAvailable(
270 base::Bind(&SystemProxyAdaptor::OnPatchpanelServiceAvailable,
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200271 weak_ptr_factory_.GetWeakPtr(), user_traffic));
272 return worker;
Andreea Costinas77b180e2020-05-12 15:17:32 +0200273}
274
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200275void SystemProxyAdaptor::SetCredentialsTask(
276 SandboxedWorker* worker, const worker::Credentials& credentials) {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100277 DCHECK(worker);
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200278 worker->SetCredentials(credentials);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100279}
280
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200281void SystemProxyAdaptor::SetKerberosEnabledTask(
282 SandboxedWorker* worker,
283 bool kerberos_enabled,
284 const std::string& principal_name) {
285 DCHECK(worker);
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200286 worker->SetKerberosEnabled(kerberos_enabled,
287 kerberos_client_->krb5_conf_path(),
288 kerberos_client_->krb5_ccache_path());
289 kerberos_client_->SetKerberosEnabled(kerberos_enabled);
290 if (kerberos_enabled) {
291 kerberos_client_->SetPrincipalName(principal_name);
292 }
293}
294
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100295void SystemProxyAdaptor::ShutDownTask() {
296 brillo::MessageLoop::current()->BreakLoop();
297}
298
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200299void SystemProxyAdaptor::SetWorker(bool user_traffic,
300 std::unique_ptr<SandboxedWorker> worker) {
301 if (user_traffic) {
302 arc_worker_ = std::move(worker);
303 } else {
304 system_services_worker_ = std::move(worker);
305 }
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100306}
307
Andreea Costinase9c73592020-07-17 15:27:54 +0200308bool SystemProxyAdaptor::ResetWorker(bool user_traffic) {
309 SandboxedWorker* worker =
310 user_traffic ? arc_worker_.get() : system_services_worker_.get();
311 if (!worker) {
312 return true;
313 }
314 if (!worker->Stop()) {
315 return false;
316 }
317 if (user_traffic) {
318 arc_worker_.reset();
319 } else {
320 system_services_worker_.reset();
321 }
322 return true;
323}
324
325SandboxedWorker* SystemProxyAdaptor::GetWorker(bool user_traffic) {
326 return user_traffic ? arc_worker_.get() : system_services_worker_.get();
327}
328
Andreea Costinasfc3dc7d2020-07-20 18:54:38 +0200329bool SystemProxyAdaptor::IncludesSystemTraffic(TrafficOrigin traffic_origin) {
330 return traffic_origin != TrafficOrigin::USER;
331}
332
333bool SystemProxyAdaptor::IncludesUserTraffic(TrafficOrigin traffic_origin) {
334 return traffic_origin != TrafficOrigin::SYSTEM;
335}
336
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200337void SystemProxyAdaptor::OnPatchpanelServiceAvailable(bool user_traffic,
338 bool is_available) {
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200339 if (!is_available) {
340 LOG(ERROR) << "Patchpanel service not available";
341 return;
342 }
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200343 ConnectNamespace(user_traffic);
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200344}
345
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200346void SystemProxyAdaptor::ConnectNamespace(bool user_traffic) {
Andreea Costinas91f75352020-07-08 14:47:47 +0200347 DCHECK_GT(netns_reconnect_attempts_available_, 0);
348 --netns_reconnect_attempts_available_;
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200349 SandboxedWorker* worker = GetWorker(user_traffic);
350 DCHECK(worker);
Andreea Costinas91f75352020-07-08 14:47:47 +0200351 // TODO(b/160736881, acostinas): Remove the delay after patchpanel
352 // implements "ip netns" to create the veth pair across network namespaces.
353 brillo::MessageLoop::current()->PostDelayedTask(
354 FROM_HERE,
355 base::Bind(&SystemProxyAdaptor::ConnectNamespaceTask,
Andreea Costinase9c73592020-07-17 15:27:54 +0200356 weak_ptr_factory_.GetWeakPtr(), worker, user_traffic),
Andreea Costinas91f75352020-07-08 14:47:47 +0200357 kConnectNamespaceDelay);
358}
359
360void SystemProxyAdaptor::ConnectNamespaceTask(SandboxedWorker* worker,
361 bool user_traffic) {
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200362 std::unique_ptr<patchpanel::Client> patchpanel_client =
363 patchpanel::Client::New();
364 if (!patchpanel_client) {
365 LOG(ERROR) << "Failed to open networking service client";
Andreea Costinas91f75352020-07-08 14:47:47 +0200366 return;
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200367 }
368
369 std::pair<base::ScopedFD, patchpanel::ConnectNamespaceResponse> result =
370 patchpanel_client->ConnectNamespace(
371 worker->pid(), "" /* outbound_ifname */, user_traffic);
372
373 if (!result.first.is_valid()) {
Andreea Costinas91f75352020-07-08 14:47:47 +0200374 LOG(ERROR) << "Failed to setup network namespace on attempt "
375 << kNetworkNamespaceReconnectAttempts -
376 netns_reconnect_attempts_available_;
377 if (netns_reconnect_attempts_available_ > 0) {
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200378 ConnectNamespace(user_traffic);
Andreea Costinas91f75352020-07-08 14:47:47 +0200379 }
380 return;
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200381 }
382
383 worker->SetNetNamespaceLifelineFd(std::move(result.first));
Andreea Costinasa89309d2020-05-08 15:51:12 +0200384 if (!worker->SetListeningAddress(result.second.host_ipv4_address(),
385 kProxyPort)) {
Andreea Costinas91f75352020-07-08 14:47:47 +0200386 return;
Andreea Costinasa89309d2020-05-08 15:51:12 +0200387 }
388 OnNamespaceConnected(worker, user_traffic);
Andreea Costinasa89309d2020-05-08 15:51:12 +0200389}
390
391void SystemProxyAdaptor::OnNamespaceConnected(SandboxedWorker* worker,
392 bool user_traffic) {
393 WorkerActiveSignalDetails details;
394 details.set_traffic_origin(user_traffic ? TrafficOrigin::USER
395 : TrafficOrigin::SYSTEM);
396 details.set_local_proxy_url(worker->local_proxy_host_and_port());
397 SendWorkerActiveSignal(SerializeProto(details));
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100398}
399
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200400void SystemProxyAdaptor::RequestAuthenticationCredentials(
Andreea Costinased9e6122020-08-12 12:06:19 +0200401 const worker::ProtectionSpace& protection_space,
402 bool bad_cached_credentials) {
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200403 AuthenticationRequiredDetails details;
404 ProtectionSpace proxy_protection_space;
405 proxy_protection_space.set_origin(protection_space.origin());
406 proxy_protection_space.set_realm(protection_space.realm());
407 proxy_protection_space.set_scheme(protection_space.scheme());
408 *details.mutable_proxy_protection_space() = proxy_protection_space;
Andreea Costinased9e6122020-08-12 12:06:19 +0200409 details.set_bad_cached_credentials(bad_cached_credentials);
Andreea Costinasdb2cbee2020-06-15 11:43:44 +0200410 SendAuthenticationRequiredSignal(SerializeProto(details));
411}
412
Andreea Costinas942284d2020-01-28 16:28:40 +0100413} // namespace system_proxy