blob: 0d62e8ea86305e42da002d8becf14e2407f10da5 [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
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -07007#include <signal.h>
Eric Carusoa6f1adb2017-05-24 14:19:46 -07008#include <stdlib.h>
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -07009#include <sys/types.h>
10#include <sys/wait.h>
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -070011
Ben Chan297c3c22013-07-17 17:34:12 -070012#include <base/strings/stringprintf.h>
13
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050014namespace debugd {
15
Ben Chanaf125862017-02-08 23:11:18 -080016namespace {
17
18const size_t kMaxWaitAttempts = 3;
19const unsigned int kDelayUSec = 1000;
20
21const char kMiniJail[] = "/sbin/minijail0";
22
23} // namespace
24
25const char SandboxedProcess::kDefaultUser[] = "debugd";
26const char SandboxedProcess::kDefaultGroup[] = "debugd";
Elly Fong-Jones215b5622013-03-20 14:32:18 -040027
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050028SandboxedProcess::SandboxedProcess()
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -070029 : sandboxing_(true),
30 access_root_mount_ns_(false),
31 user_(kDefaultUser),
32 group_(kDefaultGroup) {
33}
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050034
Ben Chan297c3c22013-07-17 17:34:12 -070035// static
36bool 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-Jonesd9a16cd2012-11-12 16:09:49 -050053bool SandboxedProcess::Init() {
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070054 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-Jonesd9a16cd2012-11-12 16:09:49 -050059 if (sandboxing_) {
60 if (user_.empty() || group_.empty())
61 return false;
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -070062
Elly Fong-Jonese56a8f62013-01-23 15:50:21 -050063 if (user_ != "root") {
64 AddArg("-u");
65 AddArg(user_);
66 }
67 if (group_ != "root") {
68 AddArg("-g");
69 AddArg(group_);
70 }
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050071 }
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070072
73 if (access_root_mount_ns_) {
74 // Enter root mount namespace.
75 AddStringOption("-V", "/proc/1/ns/mnt");
76 }
77
Justin Carlson73310fb2016-10-11 16:13:26 -070078 if (!seccomp_filter_policy_file_.empty()) {
79 AddStringOption("-S", seccomp_filter_policy_file_);
Justin Carlson187c7252016-10-28 11:03:12 -070080
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 Carlson73310fb2016-10-11 16:13:26 -070084 }
85
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070086 AddArg("--");
87
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050088 return true;
89}
90
91void SandboxedProcess::DisableSandbox() {
92 sandboxing_ = false;
93}
94
95void SandboxedProcess::SandboxAs(const std::string& user,
96 const std::string& group) {
97 sandboxing_ = true;
98 user_ = user;
99 group_ = group;
100}
101
Justin Carlson73310fb2016-10-11 16:13:26 -0700102void SandboxedProcess::SetSeccompFilterPolicyFile(const std::string& path) {
103 seccomp_filter_policy_file_ = path;
104}
105
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -0700106void SandboxedProcess::AllowAccessRootMountNamespace() {
107 access_root_mount_ns_ = true;
108}
109
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -0700110bool 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 Chana0011d82014-05-13 00:19:29 -0700173} // namespace debugd