Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 1 | // Copyright (c) 2012 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 | |
Alex Vakulenko | 262be3f | 2014-07-30 15:25:50 -0700 | [diff] [blame] | 5 | #include "debugd/src/sandboxed_process.h" |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 6 | |
Edward Hill | ad7de4e | 2017-07-05 14:45:17 -0600 | [diff] [blame] | 7 | #include <inttypes.h> |
Jorge Lucangeli Obes | 389a9ee | 2015-05-14 17:37:01 -0700 | [diff] [blame] | 8 | #include <signal.h> |
Eric Caruso | a6f1adb | 2017-05-24 14:19:46 -0700 | [diff] [blame] | 9 | #include <stdlib.h> |
Jorge Lucangeli Obes | 389a9ee | 2015-05-14 17:37:01 -0700 | [diff] [blame] | 10 | #include <sys/types.h> |
| 11 | #include <sys/wait.h> |
Jorge Lucangeli Obes | 389a9ee | 2015-05-14 17:37:01 -0700 | [diff] [blame] | 12 | |
Mike Frysinger | 56379d7 | 2019-02-19 16:03:03 -0500 | [diff] [blame] | 13 | #include <string> |
| 14 | #include <vector> |
| 15 | |
Qijiang Fan | 713061e | 2021-03-08 15:45:12 +0900 | [diff] [blame^] | 16 | #include <base/check_op.h> |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 17 | #include <base/strings/stringprintf.h> |
| 18 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 19 | namespace debugd { |
| 20 | |
Ben Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame] | 21 | namespace { |
| 22 | |
| 23 | const size_t kMaxWaitAttempts = 3; |
| 24 | const unsigned int kDelayUSec = 1000; |
| 25 | |
| 26 | const char kMiniJail[] = "/sbin/minijail0"; |
| 27 | |
Lann Martin | 419fe71 | 2017-06-07 10:45:47 -0600 | [diff] [blame] | 28 | // waitpid(2) with a timeout of kMaxWaitAttempts * kDelayUSec. |
| 29 | bool waitpid_awhile(pid_t pid) { |
| 30 | DCHECK_GT(pid, 0); |
| 31 | for (size_t attempt = 0; attempt < kMaxWaitAttempts; ++attempt) { |
| 32 | pid_t res = waitpid(pid, nullptr, WNOHANG); |
| 33 | if (res > 0) { |
| 34 | return true; |
| 35 | } |
| 36 | if (res < 0) { |
| 37 | PLOG(ERROR) << "waitpid(" << pid << ") failed"; |
| 38 | return false; |
| 39 | } |
| 40 | usleep(kDelayUSec); |
| 41 | } |
| 42 | return false; |
| 43 | } |
| 44 | |
Ben Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame] | 45 | } // namespace |
| 46 | |
| 47 | const char SandboxedProcess::kDefaultUser[] = "debugd"; |
| 48 | const char SandboxedProcess::kDefaultGroup[] = "debugd"; |
Elly Fong-Jones | 215b562 | 2013-03-20 14:32:18 -0400 | [diff] [blame] | 49 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 50 | SandboxedProcess::SandboxedProcess() |
Jorge Lucangeli Obes | c99a12a | 2014-09-17 16:43:40 -0700 | [diff] [blame] | 51 | : sandboxing_(true), |
| 52 | access_root_mount_ns_(false), |
Edward Hill | ad7de4e | 2017-07-05 14:45:17 -0600 | [diff] [blame] | 53 | set_capabilities_(false), |
David Valleau | 09f4664 | 2017-12-19 17:55:14 -0800 | [diff] [blame] | 54 | inherit_usergroups_(false), |
Jorge Lucangeli Obes | c99a12a | 2014-09-17 16:43:40 -0700 | [diff] [blame] | 55 | user_(kDefaultUser), |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 56 | group_(kDefaultGroup) {} |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 57 | |
Mike Frysinger | 56379d7 | 2019-02-19 16:03:03 -0500 | [diff] [blame] | 58 | bool SandboxedProcess::Init( |
| 59 | const std::vector<std::string>& minijail_extra_args) { |
Jorge Lucangeli Obes | 75cb7ba | 2017-07-06 10:52:48 -0400 | [diff] [blame] | 60 | if (sandboxing_ && (user_.empty() || group_.empty())) { |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 61 | // Cannot sandbox without user/group. |
| 62 | return false; |
Jorge Lucangeli Obes | 75cb7ba | 2017-07-06 10:52:48 -0400 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | if (set_capabilities_ && (!sandboxing_ || user_ == "root")) { |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 66 | // Restricting capabilities requires dropping root. |
| 67 | return false; |
Jorge Lucangeli Obes | 75cb7ba | 2017-07-06 10:52:48 -0400 | [diff] [blame] | 68 | } |
| 69 | |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 70 | AddArg(kMiniJail); |
| 71 | // Enter a new mount namespace. This is done for every process to avoid |
| 72 | // affecting the original mount namespace. |
| 73 | AddArg("-v"); |
| 74 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 75 | if (sandboxing_) { |
Elly Fong-Jones | e56a8f6 | 2013-01-23 15:50:21 -0500 | [diff] [blame] | 76 | if (user_ != "root") { |
| 77 | AddArg("-u"); |
| 78 | AddArg(user_); |
| 79 | } |
| 80 | if (group_ != "root") { |
| 81 | AddArg("-g"); |
| 82 | AddArg(group_); |
| 83 | } |
David Valleau | 09f4664 | 2017-12-19 17:55:14 -0800 | [diff] [blame] | 84 | if (inherit_usergroups_) { |
| 85 | AddArg("-G"); |
| 86 | } |
Jorge Lucangeli Obes | 75cb7ba | 2017-07-06 10:52:48 -0400 | [diff] [blame] | 87 | if (set_capabilities_) { |
Edward Hill | ad7de4e | 2017-07-05 14:45:17 -0600 | [diff] [blame] | 88 | AddStringOption("-c", |
Jorge Lucangeli Obes | 75cb7ba | 2017-07-06 10:52:48 -0400 | [diff] [blame] | 89 | base::StringPrintf("0x%" PRIx64, capabilities_mask_)); |
Edward Hill | ad7de4e | 2017-07-05 14:45:17 -0600 | [diff] [blame] | 90 | } |
| 91 | } |
| 92 | |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 93 | if (access_root_mount_ns_) { |
| 94 | // Enter root mount namespace. |
| 95 | AddStringOption("-V", "/proc/1/ns/mnt"); |
| 96 | } |
| 97 | |
Justin Carlson | 73310fb | 2016-10-11 16:13:26 -0700 | [diff] [blame] | 98 | if (!seccomp_filter_policy_file_.empty()) { |
| 99 | AddStringOption("-S", seccomp_filter_policy_file_); |
Justin Carlson | 187c725 | 2016-10-28 11:03:12 -0700 | [diff] [blame] | 100 | |
| 101 | // Whenever we use a seccomp filter, we want no-new-privs so we can apply |
| 102 | // the policy after dropping other privs. |
| 103 | AddArg("-n"); |
Justin Carlson | 73310fb | 2016-10-11 16:13:26 -0700 | [diff] [blame] | 104 | } |
| 105 | |
Mike Frysinger | 56379d7 | 2019-02-19 16:03:03 -0500 | [diff] [blame] | 106 | for (const auto& arg : minijail_extra_args) |
| 107 | AddArg(arg); |
| 108 | |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 109 | AddArg("--"); |
| 110 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 111 | return true; |
| 112 | } |
| 113 | |
Mike Frysinger | 56379d7 | 2019-02-19 16:03:03 -0500 | [diff] [blame] | 114 | bool SandboxedProcess::Init() { |
| 115 | return Init({}); |
| 116 | } |
| 117 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 118 | void SandboxedProcess::DisableSandbox() { |
| 119 | sandboxing_ = false; |
| 120 | } |
| 121 | |
| 122 | void SandboxedProcess::SandboxAs(const std::string& user, |
| 123 | const std::string& group) { |
| 124 | sandboxing_ = true; |
| 125 | user_ = user; |
| 126 | group_ = group; |
| 127 | } |
| 128 | |
David Valleau | 09f4664 | 2017-12-19 17:55:14 -0800 | [diff] [blame] | 129 | void SandboxedProcess::InheritUsergroups() { |
| 130 | inherit_usergroups_ = true; |
| 131 | } |
| 132 | |
Edward Hill | ad7de4e | 2017-07-05 14:45:17 -0600 | [diff] [blame] | 133 | void SandboxedProcess::SetCapabilities(uint64_t capabilities_mask) { |
| 134 | set_capabilities_ = true; |
| 135 | capabilities_mask_ = capabilities_mask; |
| 136 | } |
| 137 | |
Justin Carlson | 73310fb | 2016-10-11 16:13:26 -0700 | [diff] [blame] | 138 | void SandboxedProcess::SetSeccompFilterPolicyFile(const std::string& path) { |
| 139 | seccomp_filter_policy_file_ = path; |
| 140 | } |
| 141 | |
Jorge Lucangeli Obes | c99a12a | 2014-09-17 16:43:40 -0700 | [diff] [blame] | 142 | void SandboxedProcess::AllowAccessRootMountNamespace() { |
| 143 | access_root_mount_ns_ = true; |
| 144 | } |
| 145 | |
Jorge Lucangeli Obes | 389a9ee | 2015-05-14 17:37:01 -0700 | [diff] [blame] | 146 | bool SandboxedProcess::KillProcessGroup() { |
| 147 | pid_t minijail_pid = pid(); |
| 148 | if (minijail_pid == 0) { |
| 149 | LOG(ERROR) << "Process is not running"; |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | // Minijail sets its process group ID equal to its PID, |
| 154 | // so we can use pid() as PGID. Check that's still the case. |
| 155 | pid_t pgid = getpgid(minijail_pid); |
| 156 | if (pgid < 0) { |
| 157 | PLOG(ERROR) << "getpgid(minijail_pid) failed"; |
| 158 | return false; |
| 159 | } |
| 160 | if (pgid != minijail_pid) { |
| 161 | LOG(ERROR) << "Minijail PGID " << pgid << " is different from PID " |
| 162 | << minijail_pid; |
| 163 | return false; |
| 164 | } |
| 165 | |
Lann Martin | 419fe71 | 2017-06-07 10:45:47 -0600 | [diff] [blame] | 166 | // Attempt to kill minijail gracefully with SIGINT and then SIGTERM. |
| 167 | // Note: we fall through to SIGKILLing the process group below even if this |
| 168 | // succeeds to ensure all descendents have been killed. |
| 169 | bool minijail_reaped = false; |
| 170 | for (auto sig : {SIGINT, SIGTERM}) { |
| 171 | if (kill(minijail_pid, sig) != 0) { |
| 172 | // ESRCH means the process already exited. |
| 173 | if (errno != ESRCH) { |
Tom Hughes | d6c2d39 | 2020-08-24 18:12:11 -0700 | [diff] [blame] | 174 | PLOG(WARNING) << "failed to kill " << minijail_pid << " with signal " |
| 175 | << sig; |
Lann Martin | 419fe71 | 2017-06-07 10:45:47 -0600 | [diff] [blame] | 176 | } |
| 177 | break; |
| 178 | } |
| 179 | if (waitpid_awhile(minijail_pid)) { |
| 180 | minijail_reaped = true; |
| 181 | break; |
| 182 | } |
| 183 | } |
| 184 | |
Jorge Lucangeli Obes | 389a9ee | 2015-05-14 17:37:01 -0700 | [diff] [blame] | 185 | // kill(-pgid) kills every process with process group ID |pgid|. |
Lann Martin | 419fe71 | 2017-06-07 10:45:47 -0600 | [diff] [blame] | 186 | if (kill(-pgid, SIGKILL) != 0) { |
| 187 | // ESRCH means the graceful exit above caught everything. |
| 188 | if (errno != ESRCH) { |
| 189 | PLOG(ERROR) << "kill(-pgid, SIGKILL) failed"; |
| 190 | return false; |
| 191 | } |
Jorge Lucangeli Obes | 389a9ee | 2015-05-14 17:37:01 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | // If kill(2) succeeded, we release the PID. |
| 195 | UpdatePid(0); |
| 196 | |
| 197 | // We only expect to reap one process, the Minijail process. |
| 198 | // If the jailed process dies first, Minijail or init will reap it. |
| 199 | // If the Minijail process dies first, we will reap it. The jailed process |
| 200 | // will then be reaped by init. |
Lann Martin | 419fe71 | 2017-06-07 10:45:47 -0600 | [diff] [blame] | 201 | if (!minijail_reaped && !waitpid_awhile(minijail_pid)) { |
| 202 | LOG(ERROR) << "Process " << minijail_pid << " did not terminate"; |
| 203 | return false; |
Jorge Lucangeli Obes | 389a9ee | 2015-05-14 17:37:01 -0700 | [diff] [blame] | 204 | } |
| 205 | |
Lann Martin | 419fe71 | 2017-06-07 10:45:47 -0600 | [diff] [blame] | 206 | return true; |
Jorge Lucangeli Obes | 389a9ee | 2015-05-14 17:37:01 -0700 | [diff] [blame] | 207 | } |
| 208 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 209 | } // namespace debugd |