blob: 4a181c1f17ed0a78aec5a3da7540d93c37844880 [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
Mike Frysinger56379d72019-02-19 16:03:03 -050013#include <string>
14#include <vector>
15
Ben Chan297c3c22013-07-17 17:34:12 -070016#include <base/strings/stringprintf.h>
17
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050018namespace debugd {
19
Ben Chanaf125862017-02-08 23:11:18 -080020namespace {
21
22const size_t kMaxWaitAttempts = 3;
23const unsigned int kDelayUSec = 1000;
24
25const char kMiniJail[] = "/sbin/minijail0";
26
Lann Martin419fe712017-06-07 10:45:47 -060027// waitpid(2) with a timeout of kMaxWaitAttempts * kDelayUSec.
28bool 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 Chanaf125862017-02-08 23:11:18 -080044} // namespace
45
46const char SandboxedProcess::kDefaultUser[] = "debugd";
47const char SandboxedProcess::kDefaultGroup[] = "debugd";
Elly Fong-Jones215b5622013-03-20 14:32:18 -040048
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050049SandboxedProcess::SandboxedProcess()
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -070050 : sandboxing_(true),
51 access_root_mount_ns_(false),
Edward Hillad7de4e2017-07-05 14:45:17 -060052 set_capabilities_(false),
David Valleau09f46642017-12-19 17:55:14 -080053 inherit_usergroups_(false),
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -070054 user_(kDefaultUser),
Tom Hughesd6c2d392020-08-24 18:12:11 -070055 group_(kDefaultGroup) {}
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050056
Mike Frysinger56379d72019-02-19 16:03:03 -050057bool SandboxedProcess::Init(
58 const std::vector<std::string>& minijail_extra_args) {
Jorge Lucangeli Obes75cb7ba2017-07-06 10:52:48 -040059 if (sandboxing_ && (user_.empty() || group_.empty())) {
Tom Hughesd6c2d392020-08-24 18:12:11 -070060 // Cannot sandbox without user/group.
61 return false;
Jorge Lucangeli Obes75cb7ba2017-07-06 10:52:48 -040062 }
63
64 if (set_capabilities_ && (!sandboxing_ || user_ == "root")) {
Tom Hughesd6c2d392020-08-24 18:12:11 -070065 // Restricting capabilities requires dropping root.
66 return false;
Jorge Lucangeli Obes75cb7ba2017-07-06 10:52:48 -040067 }
68
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070069 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-Jonesd9a16cd2012-11-12 16:09:49 -050074 if (sandboxing_) {
Elly Fong-Jonese56a8f62013-01-23 15:50:21 -050075 if (user_ != "root") {
76 AddArg("-u");
77 AddArg(user_);
78 }
79 if (group_ != "root") {
80 AddArg("-g");
81 AddArg(group_);
82 }
David Valleau09f46642017-12-19 17:55:14 -080083 if (inherit_usergroups_) {
84 AddArg("-G");
85 }
Jorge Lucangeli Obes75cb7ba2017-07-06 10:52:48 -040086 if (set_capabilities_) {
Edward Hillad7de4e2017-07-05 14:45:17 -060087 AddStringOption("-c",
Jorge Lucangeli Obes75cb7ba2017-07-06 10:52:48 -040088 base::StringPrintf("0x%" PRIx64, capabilities_mask_));
Edward Hillad7de4e2017-07-05 14:45:17 -060089 }
90 }
91
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070092 if (access_root_mount_ns_) {
93 // Enter root mount namespace.
94 AddStringOption("-V", "/proc/1/ns/mnt");
95 }
96
Justin Carlson73310fb2016-10-11 16:13:26 -070097 if (!seccomp_filter_policy_file_.empty()) {
98 AddStringOption("-S", seccomp_filter_policy_file_);
Justin Carlson187c7252016-10-28 11:03:12 -070099
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 Carlson73310fb2016-10-11 16:13:26 -0700103 }
104
Mike Frysinger56379d72019-02-19 16:03:03 -0500105 for (const auto& arg : minijail_extra_args)
106 AddArg(arg);
107
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -0700108 AddArg("--");
109
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500110 return true;
111}
112
Mike Frysinger56379d72019-02-19 16:03:03 -0500113bool SandboxedProcess::Init() {
114 return Init({});
115}
116
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -0500117void SandboxedProcess::DisableSandbox() {
118 sandboxing_ = false;
119}
120
121void SandboxedProcess::SandboxAs(const std::string& user,
122 const std::string& group) {
123 sandboxing_ = true;
124 user_ = user;
125 group_ = group;
126}
127
David Valleau09f46642017-12-19 17:55:14 -0800128void SandboxedProcess::InheritUsergroups() {
129 inherit_usergroups_ = true;
130}
131
Edward Hillad7de4e2017-07-05 14:45:17 -0600132void SandboxedProcess::SetCapabilities(uint64_t capabilities_mask) {
133 set_capabilities_ = true;
134 capabilities_mask_ = capabilities_mask;
135}
136
Justin Carlson73310fb2016-10-11 16:13:26 -0700137void SandboxedProcess::SetSeccompFilterPolicyFile(const std::string& path) {
138 seccomp_filter_policy_file_ = path;
139}
140
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -0700141void SandboxedProcess::AllowAccessRootMountNamespace() {
142 access_root_mount_ns_ = true;
143}
144
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -0700145bool 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 Martin419fe712017-06-07 10:45:47 -0600165 // 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 Hughesd6c2d392020-08-24 18:12:11 -0700173 PLOG(WARNING) << "failed to kill " << minijail_pid << " with signal "
174 << sig;
Lann Martin419fe712017-06-07 10:45:47 -0600175 }
176 break;
177 }
178 if (waitpid_awhile(minijail_pid)) {
179 minijail_reaped = true;
180 break;
181 }
182 }
183
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -0700184 // kill(-pgid) kills every process with process group ID |pgid|.
Lann Martin419fe712017-06-07 10:45:47 -0600185 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 Obes389a9ee2015-05-14 17:37:01 -0700191 }
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 Martin419fe712017-06-07 10:45:47 -0600200 if (!minijail_reaped && !waitpid_awhile(minijail_pid)) {
201 LOG(ERROR) << "Process " << minijail_pid << " did not terminate";
202 return false;
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -0700203 }
204
Lann Martin419fe712017-06-07 10:45:47 -0600205 return true;
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -0700206}
207
Ben Chana0011d82014-05-13 00:19:29 -0700208} // namespace debugd