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