blob: d6851bf67d4e53b48494aa00052073c39996e5bb [file] [log] [blame]
Andreea Costinasc7d5ad02020-03-09 09:41:51 +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
5#include "system-proxy/sandboxed_worker.h"
6
7#include <inttypes.h>
8#include <stdlib.h>
9#include <sys/types.h>
10
11#include <string>
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020012#include <utility>
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010013#include <vector>
14
15#include <base/bind.h>
16#include <base/callback_helpers.h>
17#include <base/files/file_util.h>
18#include <base/strings/string_util.h>
Andreea Costinasa89309d2020-05-08 15:51:12 +020019#include <base/strings/stringprintf.h>
Andreea Costinas5862b102020-03-19 14:45:36 +010020#include <brillo/http/http_transport.h>
Andreea Costinasa89309d2020-05-08 15:51:12 +020021#include <chromeos/patchpanel/net_util.h>
Andreea Costinas5862b102020-03-19 14:45:36 +010022#include <google/protobuf/repeated_field.h>
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010023
24#include "bindings/worker_common.pb.h"
25#include "system-proxy/protobuf_util.h"
Andreea Costinas5862b102020-03-19 14:45:36 +010026#include "system-proxy/system_proxy_adaptor.h"
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010027
28namespace {
29constexpr char kSystemProxyWorkerBin[] = "/usr/sbin/system_proxy_worker";
30constexpr char kSeccompFilterPath[] =
31 "/usr/share/policy/system-proxy-worker-seccomp.policy";
32constexpr int kMaxWorkerMessageSize = 2048;
33// Size of the buffer array used to read data from the worker's stderr.
34constexpr int kWorkerBufferSize = 1024;
Andreea Costinas5862b102020-03-19 14:45:36 +010035constexpr char kPrefixDirect[] = "direct://";
36constexpr char kPrefixHttp[] = "http://";
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010037} // namespace
38
39namespace system_proxy {
40
Andreea Costinas5862b102020-03-19 14:45:36 +010041SandboxedWorker::SandboxedWorker(base::WeakPtr<SystemProxyAdaptor> adaptor)
Andreea Costinas396c1de2020-04-12 23:44:46 +020042 : jail_(minijail_new()), adaptor_(adaptor), pid_(0) {}
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010043
Andreea Costinasc9defae2020-04-22 10:28:35 +020044bool SandboxedWorker::Start() {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010045 DCHECK(!IsRunning()) << "Worker is already running.";
46
47 if (!jail_)
Andreea Costinasc9defae2020-04-22 10:28:35 +020048 return false;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010049
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020050 minijail_namespace_pids(jail_.get());
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010051 minijail_namespace_net(jail_.get());
52 minijail_no_new_privs(jail_.get());
53 minijail_use_seccomp_filter(jail_.get());
54 minijail_parse_seccomp_filters(jail_.get(), kSeccompFilterPath);
55
56 int child_stdin = -1, child_stdout = -1, child_stderr = -1;
57
58 std::vector<char*> args_ptr;
59
60 args_ptr.push_back(const_cast<char*>(kSystemProxyWorkerBin));
61 args_ptr.push_back(nullptr);
62
63 // Execute the command.
64 int res =
65 minijail_run_pid_pipes(jail_.get(), args_ptr[0], args_ptr.data(), &pid_,
66 &child_stdin, &child_stdout, &child_stderr);
67
68 if (res != 0) {
69 LOG(ERROR) << "Failed to start sandboxed worker: " << strerror(-res);
Andreea Costinasc9defae2020-04-22 10:28:35 +020070 return false;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010071 }
72
73 // Make sure the pipes never block.
74 if (!base::SetNonBlocking(child_stdin))
75 LOG(WARNING) << "Failed to set stdin non-blocking";
76 if (!base::SetNonBlocking(child_stdout))
77 LOG(WARNING) << "Failed to set stdout non-blocking";
78 if (!base::SetNonBlocking(child_stderr))
79 LOG(WARNING) << "Failed to set stderr non-blocking";
80
81 stdin_pipe_.reset(child_stdin);
82 stdout_pipe_.reset(child_stdout);
83 stderr_pipe_.reset(child_stderr);
84
85 stdout_watcher_ = base::FileDescriptorWatcher::WatchReadable(
86 stdout_pipe_.get(),
87 base::BindRepeating(&SandboxedWorker::OnMessageReceived,
88 base::Unretained(this)));
89
90 stderr_watcher_ = base::FileDescriptorWatcher::WatchReadable(
Andreea Costinasc9defae2020-04-22 10:28:35 +020091 stderr_pipe_.get(), base::BindRepeating(&SandboxedWorker::OnErrorReceived,
92 base::Unretained(this)));
93 return true;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010094}
95
Andreea Costinas41e06442020-03-09 09:41:51 +010096void SandboxedWorker::SetUsernameAndPassword(const std::string& username,
97 const std::string& password) {
Andreea Costinasaae97382020-05-05 13:31:58 +020098 worker::Credentials credentials;
Andreea Costinas41e06442020-03-09 09:41:51 +010099 credentials.set_username(username);
100 credentials.set_password(password);
Andreea Costinasaae97382020-05-05 13:31:58 +0200101 worker::WorkerConfigs configs;
Andreea Costinas41e06442020-03-09 09:41:51 +0100102 *configs.mutable_credentials() = credentials;
103 if (!WriteProtobuf(stdin_pipe_.get(), configs)) {
104 LOG(ERROR) << "Failed to set credentials for worker " << pid_;
105 }
106}
107
Andreea Costinasc9defae2020-04-22 10:28:35 +0200108bool SandboxedWorker::SetListeningAddress(uint32_t addr, int port) {
Andreea Costinasaae97382020-05-05 13:31:58 +0200109 worker::SocketAddress address;
Andreea Costinas41e06442020-03-09 09:41:51 +0100110 address.set_addr(addr);
111 address.set_port(port);
Andreea Costinasaae97382020-05-05 13:31:58 +0200112 worker::WorkerConfigs configs;
Andreea Costinas41e06442020-03-09 09:41:51 +0100113 *configs.mutable_listening_address() = address;
114
115 if (!WriteProtobuf(stdin_pipe_.get(), configs)) {
Andreea Costinasc9defae2020-04-22 10:28:35 +0200116 LOG(ERROR) << "Failed to set local proxy address for worker " << pid_;
117 return false;
Andreea Costinas41e06442020-03-09 09:41:51 +0100118 }
Andreea Costinasa89309d2020-05-08 15:51:12 +0200119 local_proxy_host_and_port_ = base::StringPrintf(
120 "%s:%d", patchpanel::IPv4AddressToString(addr).c_str(), port);
121 LOG(INFO) << "Set proxy address " << local_proxy_host_and_port_
122 << " for worker " << pid_;
Andreea Costinasc9defae2020-04-22 10:28:35 +0200123 return true;
Andreea Costinas41e06442020-03-09 09:41:51 +0100124}
125
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200126bool SandboxedWorker::SetKerberosEnabled(bool enabled,
127 const std::string& krb5_conf_path,
128 const std::string& krb5_ccache_path) {
129 worker::KerberosConfig kerberos_config;
130 kerberos_config.set_enabled(enabled);
131 kerberos_config.set_krb5cc_path(krb5_ccache_path);
132 kerberos_config.set_krb5conf_path(krb5_conf_path);
133 worker::WorkerConfigs configs;
134 *configs.mutable_kerberos_config() = kerberos_config;
135
136 if (!WriteProtobuf(stdin_pipe_.get(), configs)) {
137 LOG(ERROR) << "Failed to set kerberos enabled for worker " << pid_;
138 return false;
139 }
140 return true;
141}
142
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100143bool SandboxedWorker::Stop() {
144 if (is_being_terminated_)
145 return true;
146 LOG(INFO) << "Killing " << pid_;
147 is_being_terminated_ = true;
148
149 if (kill(pid_, SIGTERM) < 0) {
150 if (errno == ESRCH) {
151 // No process or group found for pid, assume already terminated.
152 return true;
153 }
154 PLOG(ERROR) << "Failed to terminate process " << pid_;
155 return false;
156 }
157 return true;
158}
159
160bool SandboxedWorker::IsRunning() {
161 return pid_ != 0 && !is_being_terminated_;
162}
163
164void SandboxedWorker::OnMessageReceived() {
Andreea Costinasaae97382020-05-05 13:31:58 +0200165 worker::WorkerRequest request;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100166
167 if (!ReadProtobuf(stdout_pipe_.get(), &request)) {
168 LOG(ERROR) << "Failed to read request from worker " << pid_;
169 // The message is corrupted or the pipe closed, either way stop listening.
170 stdout_watcher_ = nullptr;
171 return;
172 }
173 if (request.has_log_request()) {
174 LOG(INFO) << "[worker: " << pid_ << "]" << request.log_request().message();
175 }
Andreea Costinas5862b102020-03-19 14:45:36 +0100176
177 if (request.has_proxy_resolution_request()) {
Andreea Costinasaae97382020-05-05 13:31:58 +0200178 const worker::ProxyResolutionRequest& proxy_request =
Andreea Costinas5862b102020-03-19 14:45:36 +0100179 request.proxy_resolution_request();
180
181 // This callback will always be called with at least one proxy entry. Even
182 // if the dbus call itself fails, the proxy server list will contain the
183 // direct proxy.
184 adaptor_->GetChromeProxyServersAsync(
185 proxy_request.target_url(),
186 base::BindRepeating(&SandboxedWorker::OnProxyResolved,
187 weak_ptr_factory_.GetWeakPtr(),
188 proxy_request.target_url()));
189 }
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100190}
191
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200192void SandboxedWorker::SetNetNamespaceLifelineFd(
193 base::ScopedFD net_namespace_lifeline_fd) {
194 // Sanity check that only one network namespace is setup for the worker
195 // process.
Andreea Costinasbaa1dc02020-05-20 16:25:21 +0200196 DCHECK(!net_namespace_lifeline_fd_.is_valid());
Andreea Costinasedb7c8e2020-04-22 10:58:04 +0200197 net_namespace_lifeline_fd_ = std::move(net_namespace_lifeline_fd);
198}
199
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100200void SandboxedWorker::OnErrorReceived() {
201 std::vector<char> buf;
202 buf.resize(kWorkerBufferSize);
203
204 std::string message;
205 std::string worker_msg = "[worker: " + std::to_string(pid_) + "] ";
206
207 ssize_t count = kWorkerBufferSize;
208 ssize_t total_count = 0;
209
210 while (count == kWorkerBufferSize) {
211 count = HANDLE_EINTR(read(stderr_pipe_.get(), buf.data(), buf.size()));
212
213 if (count < 0) {
214 PLOG(ERROR) << worker_msg << "Failed to read from stdio";
215 return;
216 }
217
218 if (count == 0) {
219 if (!message.empty())
220 break; // Full message was read at the first iteration.
221
222 PLOG(INFO) << worker_msg << "Pipe closed";
223 // Stop watching, otherwise the handler will fire forever.
224 stderr_watcher_ = nullptr;
225 }
226
227 total_count += count;
228 if (total_count > kMaxWorkerMessageSize) {
229 LOG(ERROR) << "Failure to read message from woker: message size exceeds "
230 "maximum allowed";
231 stderr_watcher_ = nullptr;
232 return;
233 }
234 message.append(buf.begin(), buf.begin() + count);
235 }
236
237 LOG(ERROR) << worker_msg << message;
238}
239
Andreea Costinas5862b102020-03-19 14:45:36 +0100240void SandboxedWorker::OnProxyResolved(
241 const std::string& target_url,
242 bool success,
243 const std::vector<std::string>& proxy_servers) {
Andreea Costinasaae97382020-05-05 13:31:58 +0200244 worker::ProxyResolutionReply reply;
Andreea Costinas5862b102020-03-19 14:45:36 +0100245 reply.set_target_url(target_url);
246
247 // Only http and direct proxies are supported at the moment.
248 for (const auto& proxy : proxy_servers) {
249 if (base::StartsWith(proxy, kPrefixHttp,
250 base::CompareCase::INSENSITIVE_ASCII) ||
251 base::StartsWith(proxy, kPrefixDirect,
252 base::CompareCase::INSENSITIVE_ASCII)) {
Andreea Costinasa89309d2020-05-08 15:51:12 +0200253 // Make sure the local proxy doesn't try to connect to itself.
254 if (proxy.find(local_proxy_host_and_port()) == std::string::npos) {
255 reply.add_proxy_servers(proxy);
256 }
Andreea Costinas5862b102020-03-19 14:45:36 +0100257 }
258 }
259
Andreea Costinasaae97382020-05-05 13:31:58 +0200260 worker::WorkerConfigs configs;
Andreea Costinas5862b102020-03-19 14:45:36 +0100261 *configs.mutable_proxy_resolution_reply() = reply;
262
263 if (!WriteProtobuf(stdin_pipe_.get(), configs)) {
264 LOG(ERROR) << "Failed to send proxy resolution reply to worker" << pid_;
265 }
266}
267
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100268} // namespace system_proxy