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 | |
Jorge Lucangeli Obes | 389a9ee | 2015-05-14 17:37:01 -0700 | [diff] [blame] | 7 | #include <signal.h> |
Eric Caruso | a6f1adb | 2017-05-24 14:19:46 -0700 | [diff] [blame^] | 8 | #include <stdlib.h> |
Jorge Lucangeli Obes | 389a9ee | 2015-05-14 17:37:01 -0700 | [diff] [blame] | 9 | #include <sys/types.h> |
| 10 | #include <sys/wait.h> |
Jorge Lucangeli Obes | 389a9ee | 2015-05-14 17:37:01 -0700 | [diff] [blame] | 11 | |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 12 | #include <base/strings/stringprintf.h> |
| 13 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 14 | namespace debugd { |
| 15 | |
Ben Chan | af12586 | 2017-02-08 23:11:18 -0800 | [diff] [blame] | 16 | namespace { |
| 17 | |
| 18 | const size_t kMaxWaitAttempts = 3; |
| 19 | const unsigned int kDelayUSec = 1000; |
| 20 | |
| 21 | const char kMiniJail[] = "/sbin/minijail0"; |
| 22 | |
| 23 | } // namespace |
| 24 | |
| 25 | const char SandboxedProcess::kDefaultUser[] = "debugd"; |
| 26 | const char SandboxedProcess::kDefaultGroup[] = "debugd"; |
Elly Fong-Jones | 215b562 | 2013-03-20 14:32:18 -0400 | [diff] [blame] | 27 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 28 | SandboxedProcess::SandboxedProcess() |
Jorge Lucangeli Obes | c99a12a | 2014-09-17 16:43:40 -0700 | [diff] [blame] | 29 | : sandboxing_(true), |
| 30 | access_root_mount_ns_(false), |
| 31 | user_(kDefaultUser), |
| 32 | group_(kDefaultGroup) { |
| 33 | } |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 34 | |
Ben Chan | 297c3c2 | 2013-07-17 17:34:12 -0700 | [diff] [blame] | 35 | // static |
| 36 | bool SandboxedProcess::GetHelperPath(const std::string& relative_path, |
| 37 | std::string* full_path) { |
| 38 | // This environment variable controls the root directory for debugd helpers, |
| 39 | // which lets people develop helpers even when verified boot is on. |
| 40 | const char* helpers_dir = getenv("DEBUGD_HELPERS"); |
| 41 | std::string path = base::StringPrintf( |
| 42 | "%s/%s", |
| 43 | helpers_dir ? helpers_dir : "/usr/libexec/debugd/helpers", |
| 44 | relative_path.c_str()); |
| 45 | |
| 46 | if (path.length() > PATH_MAX) |
| 47 | return false; |
| 48 | |
| 49 | *full_path = path; |
| 50 | return true; |
| 51 | } |
| 52 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 53 | bool SandboxedProcess::Init() { |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 54 | AddArg(kMiniJail); |
| 55 | // Enter a new mount namespace. This is done for every process to avoid |
| 56 | // affecting the original mount namespace. |
| 57 | AddArg("-v"); |
| 58 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 59 | if (sandboxing_) { |
| 60 | if (user_.empty() || group_.empty()) |
| 61 | return false; |
Jorge Lucangeli Obes | c99a12a | 2014-09-17 16:43:40 -0700 | [diff] [blame] | 62 | |
Elly Fong-Jones | e56a8f6 | 2013-01-23 15:50:21 -0500 | [diff] [blame] | 63 | if (user_ != "root") { |
| 64 | AddArg("-u"); |
| 65 | AddArg(user_); |
| 66 | } |
| 67 | if (group_ != "root") { |
| 68 | AddArg("-g"); |
| 69 | AddArg(group_); |
| 70 | } |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 71 | } |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 72 | |
| 73 | if (access_root_mount_ns_) { |
| 74 | // Enter root mount namespace. |
| 75 | AddStringOption("-V", "/proc/1/ns/mnt"); |
| 76 | } |
| 77 | |
Justin Carlson | 73310fb | 2016-10-11 16:13:26 -0700 | [diff] [blame] | 78 | if (!seccomp_filter_policy_file_.empty()) { |
| 79 | AddStringOption("-S", seccomp_filter_policy_file_); |
Justin Carlson | 187c725 | 2016-10-28 11:03:12 -0700 | [diff] [blame] | 80 | |
| 81 | // Whenever we use a seccomp filter, we want no-new-privs so we can apply |
| 82 | // the policy after dropping other privs. |
| 83 | AddArg("-n"); |
Justin Carlson | 73310fb | 2016-10-11 16:13:26 -0700 | [diff] [blame] | 84 | } |
| 85 | |
Jorge Lucangeli Obes | 623f8ca | 2014-09-18 10:50:06 -0700 | [diff] [blame] | 86 | AddArg("--"); |
| 87 | |
Elly Fong-Jones | d9a16cd | 2012-11-12 16:09:49 -0500 | [diff] [blame] | 88 | return true; |
| 89 | } |
| 90 | |
| 91 | void SandboxedProcess::DisableSandbox() { |
| 92 | sandboxing_ = false; |
| 93 | } |
| 94 | |
| 95 | void SandboxedProcess::SandboxAs(const std::string& user, |
| 96 | const std::string& group) { |
| 97 | sandboxing_ = true; |
| 98 | user_ = user; |
| 99 | group_ = group; |
| 100 | } |
| 101 | |
Justin Carlson | 73310fb | 2016-10-11 16:13:26 -0700 | [diff] [blame] | 102 | void SandboxedProcess::SetSeccompFilterPolicyFile(const std::string& path) { |
| 103 | seccomp_filter_policy_file_ = path; |
| 104 | } |
| 105 | |
Jorge Lucangeli Obes | c99a12a | 2014-09-17 16:43:40 -0700 | [diff] [blame] | 106 | void SandboxedProcess::AllowAccessRootMountNamespace() { |
| 107 | access_root_mount_ns_ = true; |
| 108 | } |
| 109 | |
Jorge Lucangeli Obes | 389a9ee | 2015-05-14 17:37:01 -0700 | [diff] [blame] | 110 | bool SandboxedProcess::KillProcessGroup() { |
| 111 | pid_t minijail_pid = pid(); |
| 112 | if (minijail_pid == 0) { |
| 113 | LOG(ERROR) << "Process is not running"; |
| 114 | return false; |
| 115 | } |
| 116 | |
| 117 | // Minijail sets its process group ID equal to its PID, |
| 118 | // so we can use pid() as PGID. Check that's still the case. |
| 119 | pid_t pgid = getpgid(minijail_pid); |
| 120 | if (pgid < 0) { |
| 121 | PLOG(ERROR) << "getpgid(minijail_pid) failed"; |
| 122 | return false; |
| 123 | } |
| 124 | if (pgid != minijail_pid) { |
| 125 | LOG(ERROR) << "Minijail PGID " << pgid << " is different from PID " |
| 126 | << minijail_pid; |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | // kill(-pgid) kills every process with process group ID |pgid|. |
| 131 | if (kill(-pgid, SIGKILL) < 0) { |
| 132 | PLOG(ERROR) << "kill(-pgid, SIGKILL) failed"; |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | // If kill(2) succeeded, we release the PID. |
| 137 | UpdatePid(0); |
| 138 | |
| 139 | // We only expect to reap one process, the Minijail process. |
| 140 | // If the jailed process dies first, Minijail or init will reap it. |
| 141 | // If the Minijail process dies first, we will reap it. The jailed process |
| 142 | // will then be reaped by init. |
| 143 | for (size_t attempt = 0; attempt < kMaxWaitAttempts; ++attempt) { |
| 144 | int status = 0; |
| 145 | // waitpid(-pgid) waits for any child process with process group ID |pgid|. |
| 146 | pid_t waited = waitpid(-pgid, &status, WNOHANG); |
| 147 | int saved_errno = errno; |
| 148 | |
| 149 | if (waited < 0) { |
| 150 | if (saved_errno == ECHILD) { |
| 151 | // Processes with PGID |pgid| don't exist, so we're done. |
| 152 | return true; |
| 153 | } |
| 154 | PLOG(ERROR) << "waitpid(-pgid) failed"; |
| 155 | return false; |
| 156 | } |
| 157 | |
| 158 | if (waited > 0) { |
| 159 | if (waited != minijail_pid) { |
| 160 | LOG(WARNING) << "Expecting PID " << minijail_pid << ", got PID " |
| 161 | << waited; |
| 162 | } |
| 163 | return true; |
| 164 | } |
| 165 | |
| 166 | usleep(kDelayUSec); |
| 167 | } |
| 168 | |
| 169 | LOG(WARNING) << "Process " << minijail_pid << " did not terminate"; |
| 170 | return false; |
| 171 | } |
| 172 | |
Ben Chan | a0011d8 | 2014-05-13 00:19:29 -0700 | [diff] [blame] | 173 | } // namespace debugd |