Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 1 | // 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 Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 12 | #include <utility> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 13 | #include <vector> |
| 14 | |
| 15 | #include <base/bind.h> |
| 16 | #include <base/callback_helpers.h> |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame] | 17 | #include <base/check.h> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 18 | #include <base/files/file_util.h> |
| 19 | #include <base/strings/string_util.h> |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 20 | #include <base/strings/stringprintf.h> |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 21 | #include <brillo/http/http_transport.h> |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 22 | #include <chromeos/patchpanel/net_util.h> |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 23 | #include <google/protobuf/repeated_field.h> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 24 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 25 | #include "system-proxy/protobuf_util.h" |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 26 | #include "system-proxy/system_proxy_adaptor.h" |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 27 | |
| 28 | namespace { |
| 29 | constexpr char kSystemProxyWorkerBin[] = "/usr/sbin/system_proxy_worker"; |
| 30 | constexpr char kSeccompFilterPath[] = |
| 31 | "/usr/share/policy/system-proxy-worker-seccomp.policy"; |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 32 | constexpr int kMaxWorkerMessageSize = 4096; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 33 | // Size of the buffer array used to read data from the worker's stderr. |
| 34 | constexpr int kWorkerBufferSize = 1024; |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 35 | constexpr char kPrefixDirect[] = "direct://"; |
| 36 | constexpr char kPrefixHttp[] = "http://"; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 37 | } // namespace |
| 38 | |
| 39 | namespace system_proxy { |
| 40 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 41 | SandboxedWorker::SandboxedWorker(base::WeakPtr<SystemProxyAdaptor> adaptor) |
Andreea Costinas | 396c1de | 2020-04-12 23:44:46 +0200 | [diff] [blame] | 42 | : jail_(minijail_new()), adaptor_(adaptor), pid_(0) {} |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 43 | |
Andreea Costinas | c9defae | 2020-04-22 10:28:35 +0200 | [diff] [blame] | 44 | bool SandboxedWorker::Start() { |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 45 | DCHECK(!IsRunning()) << "Worker is already running."; |
| 46 | |
| 47 | if (!jail_) |
Andreea Costinas | c9defae | 2020-04-22 10:28:35 +0200 | [diff] [blame] | 48 | return false; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 49 | |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 50 | minijail_namespace_pids(jail_.get()); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 51 | 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); |
Andreea Costinas | 005a5d2 | 2020-07-17 15:09:08 +0200 | [diff] [blame] | 55 | // Required to forward SIGTERM to the child process. |
| 56 | minijail_forward_signals(jail_.get()); |
| 57 | // Resets the signal mask to ensure signals are not unintentionally blocked. |
| 58 | minijail_reset_signal_mask(jail_.get()); |
| 59 | // Resets the signal handlers to the default behaviours. This is needed so |
| 60 | // that the child process terminates when receiving the SIGTERM signal. |
| 61 | minijail_reset_signal_handlers(jail_.get()); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 62 | |
| 63 | int child_stdin = -1, child_stdout = -1, child_stderr = -1; |
| 64 | |
| 65 | std::vector<char*> args_ptr; |
| 66 | |
| 67 | args_ptr.push_back(const_cast<char*>(kSystemProxyWorkerBin)); |
| 68 | args_ptr.push_back(nullptr); |
| 69 | |
| 70 | // Execute the command. |
| 71 | int res = |
| 72 | minijail_run_pid_pipes(jail_.get(), args_ptr[0], args_ptr.data(), &pid_, |
| 73 | &child_stdin, &child_stdout, &child_stderr); |
| 74 | |
| 75 | if (res != 0) { |
| 76 | LOG(ERROR) << "Failed to start sandboxed worker: " << strerror(-res); |
Andreea Costinas | c9defae | 2020-04-22 10:28:35 +0200 | [diff] [blame] | 77 | return false; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | // Make sure the pipes never block. |
| 81 | if (!base::SetNonBlocking(child_stdin)) |
| 82 | LOG(WARNING) << "Failed to set stdin non-blocking"; |
| 83 | if (!base::SetNonBlocking(child_stdout)) |
| 84 | LOG(WARNING) << "Failed to set stdout non-blocking"; |
| 85 | if (!base::SetNonBlocking(child_stderr)) |
| 86 | LOG(WARNING) << "Failed to set stderr non-blocking"; |
| 87 | |
| 88 | stdin_pipe_.reset(child_stdin); |
| 89 | stdout_pipe_.reset(child_stdout); |
| 90 | stderr_pipe_.reset(child_stderr); |
| 91 | |
| 92 | stdout_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
| 93 | stdout_pipe_.get(), |
| 94 | base::BindRepeating(&SandboxedWorker::OnMessageReceived, |
| 95 | base::Unretained(this))); |
| 96 | |
| 97 | stderr_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
Andreea Costinas | c9defae | 2020-04-22 10:28:35 +0200 | [diff] [blame] | 98 | stderr_pipe_.get(), base::BindRepeating(&SandboxedWorker::OnErrorReceived, |
| 99 | base::Unretained(this))); |
| 100 | return true; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 101 | } |
| 102 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 103 | void SandboxedWorker::SetCredentials(const worker::Credentials& credentials) { |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 104 | worker::WorkerConfigs configs; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 105 | *configs.mutable_credentials() = credentials; |
| 106 | if (!WriteProtobuf(stdin_pipe_.get(), configs)) { |
| 107 | LOG(ERROR) << "Failed to set credentials for worker " << pid_; |
| 108 | } |
| 109 | } |
| 110 | |
Andreea Costinas | c9defae | 2020-04-22 10:28:35 +0200 | [diff] [blame] | 111 | bool SandboxedWorker::SetListeningAddress(uint32_t addr, int port) { |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 112 | worker::SocketAddress address; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 113 | address.set_addr(addr); |
| 114 | address.set_port(port); |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 115 | worker::WorkerConfigs configs; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 116 | *configs.mutable_listening_address() = address; |
| 117 | |
| 118 | if (!WriteProtobuf(stdin_pipe_.get(), configs)) { |
Andreea Costinas | c9defae | 2020-04-22 10:28:35 +0200 | [diff] [blame] | 119 | LOG(ERROR) << "Failed to set local proxy address for worker " << pid_; |
| 120 | return false; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 121 | } |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 122 | local_proxy_host_and_port_ = base::StringPrintf( |
| 123 | "%s:%d", patchpanel::IPv4AddressToString(addr).c_str(), port); |
| 124 | LOG(INFO) << "Set proxy address " << local_proxy_host_and_port_ |
| 125 | << " for worker " << pid_; |
Andreea Costinas | c9defae | 2020-04-22 10:28:35 +0200 | [diff] [blame] | 126 | return true; |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 127 | } |
| 128 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 129 | bool SandboxedWorker::SetKerberosEnabled(bool enabled, |
| 130 | const std::string& krb5_conf_path, |
| 131 | const std::string& krb5_ccache_path) { |
| 132 | worker::KerberosConfig kerberos_config; |
| 133 | kerberos_config.set_enabled(enabled); |
| 134 | kerberos_config.set_krb5cc_path(krb5_ccache_path); |
| 135 | kerberos_config.set_krb5conf_path(krb5_conf_path); |
| 136 | worker::WorkerConfigs configs; |
| 137 | *configs.mutable_kerberos_config() = kerberos_config; |
| 138 | |
| 139 | if (!WriteProtobuf(stdin_pipe_.get(), configs)) { |
| 140 | LOG(ERROR) << "Failed to set kerberos enabled for worker " << pid_; |
| 141 | return false; |
| 142 | } |
| 143 | return true; |
| 144 | } |
| 145 | |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 146 | bool SandboxedWorker::ClearUserCredentials() { |
| 147 | worker::ClearUserCredentials clear_user_credentials; |
| 148 | worker::WorkerConfigs configs; |
| 149 | *configs.mutable_clear_user_credentials() = clear_user_credentials; |
| 150 | |
| 151 | if (!WriteProtobuf(stdin_pipe_.get(), configs)) { |
| 152 | LOG(ERROR) << "Failed to send request to clear user credentials for worker " |
| 153 | << pid_; |
| 154 | return false; |
| 155 | } |
| 156 | return true; |
| 157 | } |
| 158 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 159 | bool SandboxedWorker::Stop() { |
| 160 | if (is_being_terminated_) |
| 161 | return true; |
| 162 | LOG(INFO) << "Killing " << pid_; |
| 163 | is_being_terminated_ = true; |
| 164 | |
| 165 | if (kill(pid_, SIGTERM) < 0) { |
| 166 | if (errno == ESRCH) { |
| 167 | // No process or group found for pid, assume already terminated. |
| 168 | return true; |
| 169 | } |
| 170 | PLOG(ERROR) << "Failed to terminate process " << pid_; |
| 171 | return false; |
| 172 | } |
| 173 | return true; |
| 174 | } |
| 175 | |
| 176 | bool SandboxedWorker::IsRunning() { |
| 177 | return pid_ != 0 && !is_being_terminated_; |
| 178 | } |
| 179 | |
| 180 | void SandboxedWorker::OnMessageReceived() { |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 181 | worker::WorkerRequest request; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 182 | |
| 183 | if (!ReadProtobuf(stdout_pipe_.get(), &request)) { |
| 184 | LOG(ERROR) << "Failed to read request from worker " << pid_; |
| 185 | // The message is corrupted or the pipe closed, either way stop listening. |
| 186 | stdout_watcher_ = nullptr; |
| 187 | return; |
| 188 | } |
| 189 | if (request.has_log_request()) { |
| 190 | LOG(INFO) << "[worker: " << pid_ << "]" << request.log_request().message(); |
| 191 | } |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 192 | |
| 193 | if (request.has_proxy_resolution_request()) { |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 194 | const worker::ProxyResolutionRequest& proxy_request = |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 195 | request.proxy_resolution_request(); |
| 196 | |
| 197 | // This callback will always be called with at least one proxy entry. Even |
| 198 | // if the dbus call itself fails, the proxy server list will contain the |
| 199 | // direct proxy. |
| 200 | adaptor_->GetChromeProxyServersAsync( |
| 201 | proxy_request.target_url(), |
| 202 | base::BindRepeating(&SandboxedWorker::OnProxyResolved, |
| 203 | weak_ptr_factory_.GetWeakPtr(), |
| 204 | proxy_request.target_url())); |
| 205 | } |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 206 | if (request.has_auth_required_request()) { |
| 207 | const worker::AuthRequiredRequest& auth_request = |
| 208 | request.auth_required_request(); |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame] | 209 | adaptor_->RequestAuthenticationCredentials( |
| 210 | auth_request.protection_space(), auth_request.bad_cached_credentials()); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 211 | } |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 212 | } |
| 213 | |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 214 | void SandboxedWorker::SetNetNamespaceLifelineFd( |
| 215 | base::ScopedFD net_namespace_lifeline_fd) { |
| 216 | // Sanity check that only one network namespace is setup for the worker |
| 217 | // process. |
Andreea Costinas | baa1dc0 | 2020-05-20 16:25:21 +0200 | [diff] [blame] | 218 | DCHECK(!net_namespace_lifeline_fd_.is_valid()); |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 219 | net_namespace_lifeline_fd_ = std::move(net_namespace_lifeline_fd); |
| 220 | } |
| 221 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 222 | void SandboxedWorker::OnErrorReceived() { |
| 223 | std::vector<char> buf; |
| 224 | buf.resize(kWorkerBufferSize); |
| 225 | |
| 226 | std::string message; |
| 227 | std::string worker_msg = "[worker: " + std::to_string(pid_) + "] "; |
| 228 | |
| 229 | ssize_t count = kWorkerBufferSize; |
| 230 | ssize_t total_count = 0; |
| 231 | |
| 232 | while (count == kWorkerBufferSize) { |
| 233 | count = HANDLE_EINTR(read(stderr_pipe_.get(), buf.data(), buf.size())); |
| 234 | |
| 235 | if (count < 0) { |
| 236 | PLOG(ERROR) << worker_msg << "Failed to read from stdio"; |
| 237 | return; |
| 238 | } |
| 239 | |
| 240 | if (count == 0) { |
| 241 | if (!message.empty()) |
| 242 | break; // Full message was read at the first iteration. |
| 243 | |
| 244 | PLOG(INFO) << worker_msg << "Pipe closed"; |
| 245 | // Stop watching, otherwise the handler will fire forever. |
| 246 | stderr_watcher_ = nullptr; |
| 247 | } |
| 248 | |
| 249 | total_count += count; |
| 250 | if (total_count > kMaxWorkerMessageSize) { |
| 251 | LOG(ERROR) << "Failure to read message from woker: message size exceeds " |
| 252 | "maximum allowed"; |
| 253 | stderr_watcher_ = nullptr; |
| 254 | return; |
| 255 | } |
| 256 | message.append(buf.begin(), buf.begin() + count); |
| 257 | } |
| 258 | |
| 259 | LOG(ERROR) << worker_msg << message; |
| 260 | } |
| 261 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 262 | void SandboxedWorker::OnProxyResolved( |
| 263 | const std::string& target_url, |
| 264 | bool success, |
| 265 | const std::vector<std::string>& proxy_servers) { |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 266 | worker::ProxyResolutionReply reply; |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 267 | reply.set_target_url(target_url); |
| 268 | |
| 269 | // Only http and direct proxies are supported at the moment. |
| 270 | for (const auto& proxy : proxy_servers) { |
| 271 | if (base::StartsWith(proxy, kPrefixHttp, |
| 272 | base::CompareCase::INSENSITIVE_ASCII) || |
| 273 | base::StartsWith(proxy, kPrefixDirect, |
| 274 | base::CompareCase::INSENSITIVE_ASCII)) { |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 275 | // Make sure the local proxy doesn't try to connect to itself. |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 276 | if (!adaptor_->IsLocalProxy(proxy)) { |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 277 | reply.add_proxy_servers(proxy); |
| 278 | } |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 279 | } |
| 280 | } |
| 281 | |
Andreea Costinas | aae9738 | 2020-05-05 13:31:58 +0200 | [diff] [blame] | 282 | worker::WorkerConfigs configs; |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 283 | *configs.mutable_proxy_resolution_reply() = reply; |
| 284 | |
| 285 | if (!WriteProtobuf(stdin_pipe_.get(), configs)) { |
| 286 | LOG(ERROR) << "Failed to send proxy resolution reply to worker" << pid_; |
| 287 | } |
| 288 | } |
| 289 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 290 | } // namespace system_proxy |