blob: 8044fe4167994553d7c7100aaa214360d227ef6a [file] [log] [blame]
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -05001// 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 Vakulenko262be3f2014-07-30 15:25:50 -07005#include "debugd/src/sandboxed_process.h"
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -05006
Edward Hillad7de4e2017-07-05 14:45:17 -06007#include <inttypes.h>
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -07008#include <signal.h>
Eric Carusoa6f1adb2017-05-24 14:19:46 -07009#include <stdlib.h>
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -070010#include <sys/types.h>
11#include <sys/wait.h>
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -070012
Ben Chan297c3c22013-07-17 17:34:12 -070013#include <base/strings/stringprintf.h>
14
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050015namespace debugd {
16
Ben Chanaf125862017-02-08 23:11:18 -080017namespace {
18
19const size_t kMaxWaitAttempts = 3;
20const unsigned int kDelayUSec = 1000;
21
22const char kMiniJail[] = "/sbin/minijail0";
23
Lann Martin419fe712017-06-07 10:45:47 -060024// waitpid(2) with a timeout of kMaxWaitAttempts * kDelayUSec.
25bool waitpid_awhile(pid_t pid) {
26 DCHECK_GT(pid, 0);
27 for (size_t attempt = 0; attempt < kMaxWaitAttempts; ++attempt) {
28 pid_t res = waitpid(pid, nullptr, WNOHANG);
29 if (res > 0) {
30 return true;
31 }
32 if (res < 0) {
33 PLOG(ERROR) << "waitpid(" << pid << ") failed";
34 return false;
35 }
36 usleep(kDelayUSec);
37 }
38 return false;
39}
40
Ben Chanaf125862017-02-08 23:11:18 -080041} // namespace
42
43const char SandboxedProcess::kDefaultUser[] = "debugd";
44const char SandboxedProcess::kDefaultGroup[] = "debugd";
Elly Fong-Jones215b5622013-03-20 14:32:18 -040045
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050046SandboxedProcess::SandboxedProcess()
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -070047 : sandboxing_(true),
48 access_root_mount_ns_(false),
Edward Hillad7de4e2017-07-05 14:45:17 -060049 set_capabilities_(false),
David Valleau09f46642017-12-19 17:55:14 -080050 inherit_usergroups_(false),
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -070051 user_(kDefaultUser),
52 group_(kDefaultGroup) {
53}
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050054
Ben Chan297c3c22013-07-17 17:34:12 -070055// static
56bool SandboxedProcess::GetHelperPath(const std::string& relative_path,
57 std::string* full_path) {
58 // This environment variable controls the root directory for debugd helpers,
59 // which lets people develop helpers even when verified boot is on.
60 const char* helpers_dir = getenv("DEBUGD_HELPERS");
61 std::string path = base::StringPrintf(
62 "%s/%s",
63 helpers_dir ? helpers_dir : "/usr/libexec/debugd/helpers",
64 relative_path.c_str());
65
66 if (path.length() > PATH_MAX)
67 return false;
68
69 *full_path = path;
70 return true;
71}
72
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050073bool SandboxedProcess::Init() {
Jorge Lucangeli Obes75cb7ba2017-07-06 10:52:48 -040074 if (sandboxing_ && (user_.empty() || group_.empty())) {
75 // Cannot sandbox without user/group.
76 return false;
77 }
78
79 if (set_capabilities_ && (!sandboxing_ || user_ == "root")) {
80 // Restricting capabilities requires dropping root.
81 return false;
82 }
83
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070084 AddArg(kMiniJail);
85 // Enter a new mount namespace. This is done for every process to avoid
86 // affecting the original mount namespace.
87 AddArg("-v");
88
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050089 if (sandboxing_) {
Elly Fong-Jonese56a8f62013-01-23 15:50:21 -050090 if (user_ != "root") {
91 AddArg("-u");
92 AddArg(user_);
93 }
94 if (group_ != "root") {
95 AddArg("-g");
96 AddArg(group_);
97 }
David Valleau09f46642017-12-19 17:55:14 -080098 if (inherit_usergroups_) {
99 AddArg("-G");
100 }
Jorge Lucangeli Obes75cb7ba2017-07-06 10:52:48 -0400101 if (set_capabilities_) {
Edward Hillad7de4e2017-07-05 14:45:17 -0600102 AddStringOption("-c",
Jorge Lucangeli Obes75cb7ba2017-07-06 10:52:48 -0400103 base::StringPrintf("0x%" PRIx64, capabilities_mask_));
Edward Hillad7de4e2017-07-05 14:45:17 -0600104 }
105 }
106
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -0700107 if (access_root_mount_ns_) {
108 // Enter root mount namespace.
109 AddStringOption("-V", "/proc/1/ns/mnt");
110 }
111
Justin Carlson73310fb2016-10-11 16:13:26 -0700112 if (!seccomp_filter_policy_file_.empty()) {
113 AddStringOption("-S", seccomp_filter_policy_file_);
Justin Carlson187c7252016-10-28 11:03:12 -0700114
115 // Whenever we use a seccomp filter, we want no-new-privs so we can apply
116 // the policy after dropping other privs.
117 AddArg("-n");
Justin Carlson73310fb2016-10-11 16:13:26 -0700118 }
119
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -0700120 AddArg("--");
121
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500122 return true;
123}
124
125void SandboxedProcess::DisableSandbox() {
126 sandboxing_ = false;
127}
128
129void SandboxedProcess::SandboxAs(const std::string& user,
130 const std::string& group) {
131 sandboxing_ = true;
132 user_ = user;
133 group_ = group;
134}
135
David Valleau09f46642017-12-19 17:55:14 -0800136void SandboxedProcess::InheritUsergroups() {
137 inherit_usergroups_ = true;
138}
139
Edward Hillad7de4e2017-07-05 14:45:17 -0600140void SandboxedProcess::SetCapabilities(uint64_t capabilities_mask) {
141 set_capabilities_ = true;
142 capabilities_mask_ = capabilities_mask;
143}
144
Justin Carlson73310fb2016-10-11 16:13:26 -0700145void SandboxedProcess::SetSeccompFilterPolicyFile(const std::string& path) {
146 seccomp_filter_policy_file_ = path;
147}
148
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -0700149void SandboxedProcess::AllowAccessRootMountNamespace() {
150 access_root_mount_ns_ = true;
151}
152
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -0700153bool SandboxedProcess::KillProcessGroup() {
154 pid_t minijail_pid = pid();
155 if (minijail_pid == 0) {
156 LOG(ERROR) << "Process is not running";
157 return false;
158 }
159
160 // Minijail sets its process group ID equal to its PID,
161 // so we can use pid() as PGID. Check that's still the case.
162 pid_t pgid = getpgid(minijail_pid);
163 if (pgid < 0) {
164 PLOG(ERROR) << "getpgid(minijail_pid) failed";
165 return false;
166 }
167 if (pgid != minijail_pid) {
168 LOG(ERROR) << "Minijail PGID " << pgid << " is different from PID "
169 << minijail_pid;
170 return false;
171 }
172
Lann Martin419fe712017-06-07 10:45:47 -0600173 // Attempt to kill minijail gracefully with SIGINT and then SIGTERM.
174 // Note: we fall through to SIGKILLing the process group below even if this
175 // succeeds to ensure all descendents have been killed.
176 bool minijail_reaped = false;
177 for (auto sig : {SIGINT, SIGTERM}) {
178 if (kill(minijail_pid, sig) != 0) {
179 // ESRCH means the process already exited.
180 if (errno != ESRCH) {
181 PLOG(WARNING) << "failed to kill " << minijail_pid
182 << " with signal " << sig;
183 }
184 break;
185 }
186 if (waitpid_awhile(minijail_pid)) {
187 minijail_reaped = true;
188 break;
189 }
190 }
191
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -0700192 // kill(-pgid) kills every process with process group ID |pgid|.
Lann Martin419fe712017-06-07 10:45:47 -0600193 if (kill(-pgid, SIGKILL) != 0) {
194 // ESRCH means the graceful exit above caught everything.
195 if (errno != ESRCH) {
196 PLOG(ERROR) << "kill(-pgid, SIGKILL) failed";
197 return false;
198 }
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -0700199 }
200
201 // If kill(2) succeeded, we release the PID.
202 UpdatePid(0);
203
204 // We only expect to reap one process, the Minijail process.
205 // If the jailed process dies first, Minijail or init will reap it.
206 // If the Minijail process dies first, we will reap it. The jailed process
207 // will then be reaped by init.
Lann Martin419fe712017-06-07 10:45:47 -0600208 if (!minijail_reaped && !waitpid_awhile(minijail_pid)) {
209 LOG(ERROR) << "Process " << minijail_pid << " did not terminate";
210 return false;
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -0700211 }
212
Lann Martin419fe712017-06-07 10:45:47 -0600213 return true;
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -0700214}
215
Ben Chana0011d82014-05-13 00:19:29 -0700216} // namespace debugd