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> |
| 12 | #include <vector> |
| 13 | |
| 14 | #include <base/bind.h> |
| 15 | #include <base/callback_helpers.h> |
| 16 | #include <base/files/file_util.h> |
| 17 | #include <base/strings/string_util.h> |
| 18 | |
| 19 | #include "bindings/worker_common.pb.h" |
| 20 | #include "system-proxy/protobuf_util.h" |
| 21 | |
| 22 | namespace { |
| 23 | constexpr char kSystemProxyWorkerBin[] = "/usr/sbin/system_proxy_worker"; |
| 24 | constexpr char kSeccompFilterPath[] = |
| 25 | "/usr/share/policy/system-proxy-worker-seccomp.policy"; |
| 26 | constexpr int kMaxWorkerMessageSize = 2048; |
| 27 | // Size of the buffer array used to read data from the worker's stderr. |
| 28 | constexpr int kWorkerBufferSize = 1024; |
| 29 | |
| 30 | } // namespace |
| 31 | |
| 32 | namespace system_proxy { |
| 33 | |
| 34 | SandboxedWorker::SandboxedWorker() : jail_(minijail_new()) {} |
| 35 | |
| 36 | void SandboxedWorker::Start() { |
| 37 | DCHECK(!IsRunning()) << "Worker is already running."; |
| 38 | |
| 39 | if (!jail_) |
| 40 | return; |
| 41 | |
| 42 | minijail_namespace_net(jail_.get()); |
| 43 | minijail_no_new_privs(jail_.get()); |
| 44 | minijail_use_seccomp_filter(jail_.get()); |
| 45 | minijail_parse_seccomp_filters(jail_.get(), kSeccompFilterPath); |
| 46 | |
| 47 | int child_stdin = -1, child_stdout = -1, child_stderr = -1; |
| 48 | |
| 49 | std::vector<char*> args_ptr; |
| 50 | |
| 51 | args_ptr.push_back(const_cast<char*>(kSystemProxyWorkerBin)); |
| 52 | args_ptr.push_back(nullptr); |
| 53 | |
| 54 | // Execute the command. |
| 55 | int res = |
| 56 | minijail_run_pid_pipes(jail_.get(), args_ptr[0], args_ptr.data(), &pid_, |
| 57 | &child_stdin, &child_stdout, &child_stderr); |
| 58 | |
| 59 | if (res != 0) { |
| 60 | LOG(ERROR) << "Failed to start sandboxed worker: " << strerror(-res); |
| 61 | return; |
| 62 | } |
| 63 | |
| 64 | // Make sure the pipes never block. |
| 65 | if (!base::SetNonBlocking(child_stdin)) |
| 66 | LOG(WARNING) << "Failed to set stdin non-blocking"; |
| 67 | if (!base::SetNonBlocking(child_stdout)) |
| 68 | LOG(WARNING) << "Failed to set stdout non-blocking"; |
| 69 | if (!base::SetNonBlocking(child_stderr)) |
| 70 | LOG(WARNING) << "Failed to set stderr non-blocking"; |
| 71 | |
| 72 | stdin_pipe_.reset(child_stdin); |
| 73 | stdout_pipe_.reset(child_stdout); |
| 74 | stderr_pipe_.reset(child_stderr); |
| 75 | |
| 76 | stdout_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
| 77 | stdout_pipe_.get(), |
| 78 | base::BindRepeating(&SandboxedWorker::OnMessageReceived, |
| 79 | base::Unretained(this))); |
| 80 | |
| 81 | stderr_watcher_ = base::FileDescriptorWatcher::WatchReadable( |
| 82 | stderr_pipe_.get(), |
| 83 | base::BindRepeating(&SandboxedWorker::OnMessageReceived, |
| 84 | base::Unretained(this))); |
| 85 | } |
| 86 | |
| 87 | bool SandboxedWorker::Stop() { |
| 88 | if (is_being_terminated_) |
| 89 | return true; |
| 90 | LOG(INFO) << "Killing " << pid_; |
| 91 | is_being_terminated_ = true; |
| 92 | |
| 93 | if (kill(pid_, SIGTERM) < 0) { |
| 94 | if (errno == ESRCH) { |
| 95 | // No process or group found for pid, assume already terminated. |
| 96 | return true; |
| 97 | } |
| 98 | PLOG(ERROR) << "Failed to terminate process " << pid_; |
| 99 | return false; |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
| 104 | bool SandboxedWorker::IsRunning() { |
| 105 | return pid_ != 0 && !is_being_terminated_; |
| 106 | } |
| 107 | |
| 108 | void SandboxedWorker::OnMessageReceived() { |
| 109 | WorkerRequest request; |
| 110 | |
| 111 | if (!ReadProtobuf(stdout_pipe_.get(), &request)) { |
| 112 | LOG(ERROR) << "Failed to read request from worker " << pid_; |
| 113 | // The message is corrupted or the pipe closed, either way stop listening. |
| 114 | stdout_watcher_ = nullptr; |
| 115 | return; |
| 116 | } |
| 117 | if (request.has_log_request()) { |
| 118 | LOG(INFO) << "[worker: " << pid_ << "]" << request.log_request().message(); |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | void SandboxedWorker::OnErrorReceived() { |
| 123 | std::vector<char> buf; |
| 124 | buf.resize(kWorkerBufferSize); |
| 125 | |
| 126 | std::string message; |
| 127 | std::string worker_msg = "[worker: " + std::to_string(pid_) + "] "; |
| 128 | |
| 129 | ssize_t count = kWorkerBufferSize; |
| 130 | ssize_t total_count = 0; |
| 131 | |
| 132 | while (count == kWorkerBufferSize) { |
| 133 | count = HANDLE_EINTR(read(stderr_pipe_.get(), buf.data(), buf.size())); |
| 134 | |
| 135 | if (count < 0) { |
| 136 | PLOG(ERROR) << worker_msg << "Failed to read from stdio"; |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | if (count == 0) { |
| 141 | if (!message.empty()) |
| 142 | break; // Full message was read at the first iteration. |
| 143 | |
| 144 | PLOG(INFO) << worker_msg << "Pipe closed"; |
| 145 | // Stop watching, otherwise the handler will fire forever. |
| 146 | stderr_watcher_ = nullptr; |
| 147 | } |
| 148 | |
| 149 | total_count += count; |
| 150 | if (total_count > kMaxWorkerMessageSize) { |
| 151 | LOG(ERROR) << "Failure to read message from woker: message size exceeds " |
| 152 | "maximum allowed"; |
| 153 | stderr_watcher_ = nullptr; |
| 154 | return; |
| 155 | } |
| 156 | message.append(buf.begin(), buf.begin() + count); |
| 157 | } |
| 158 | |
| 159 | LOG(ERROR) << worker_msg << message; |
| 160 | } |
| 161 | |
| 162 | } // namespace system_proxy |