blob: 69b8d56169fdb021f9a6bdc30e6287dfd3b4870d [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>
8#include <sys/types.h>
9#include <sys/wait.h>
10#include <unistd.h>
11
Ben Chan297c3c22013-07-17 17:34:12 -070012#include <base/strings/stringprintf.h>
13
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -070014namespace {
15const size_t kMaxWaitAttempts = 3;
16const unsigned int kDelayUSec = 1000;
17}
18
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050019namespace debugd {
20
Elly Fong-Jones215b5622013-03-20 14:32:18 -040021const char *SandboxedProcess::kDefaultUser = "debugd";
22const char *SandboxedProcess::kDefaultGroup = "debugd";
23
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050024SandboxedProcess::SandboxedProcess()
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -070025 : sandboxing_(true),
26 access_root_mount_ns_(false),
27 user_(kDefaultUser),
28 group_(kDefaultGroup) {
29}
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050030
Ricky Liang1ef73e52016-05-24 16:32:34 +080031SandboxedProcess::~SandboxedProcess() {
32 for (const auto& fd : bound_fds_)
33 close(fd);
34}
35
Ben Chan297c3c22013-07-17 17:34:12 -070036// static
37bool SandboxedProcess::GetHelperPath(const std::string& relative_path,
38 std::string* full_path) {
39 // This environment variable controls the root directory for debugd helpers,
40 // which lets people develop helpers even when verified boot is on.
41 const char* helpers_dir = getenv("DEBUGD_HELPERS");
42 std::string path = base::StringPrintf(
43 "%s/%s",
44 helpers_dir ? helpers_dir : "/usr/libexec/debugd/helpers",
45 relative_path.c_str());
46
47 if (path.length() > PATH_MAX)
48 return false;
49
50 *full_path = path;
51 return true;
52}
53
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050054bool SandboxedProcess::Init() {
55 const char *kMiniJail = "/sbin/minijail0";
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070056
57 AddArg(kMiniJail);
58 // Enter a new mount namespace. This is done for every process to avoid
59 // affecting the original mount namespace.
60 AddArg("-v");
61
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050062 if (sandboxing_) {
63 if (user_.empty() || group_.empty())
64 return false;
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -070065
Elly Fong-Jonese56a8f62013-01-23 15:50:21 -050066 if (user_ != "root") {
67 AddArg("-u");
68 AddArg(user_);
69 }
70 if (group_ != "root") {
71 AddArg("-g");
72 AddArg(group_);
73 }
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050074 }
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070075
76 if (access_root_mount_ns_) {
77 // Enter root mount namespace.
78 AddStringOption("-V", "/proc/1/ns/mnt");
79 }
80
Justin Carlson73310fb2016-10-11 16:13:26 -070081 if (!seccomp_filter_policy_file_.empty()) {
82 AddStringOption("-S", seccomp_filter_policy_file_);
83 }
84
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070085 AddArg("--");
86
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050087 return true;
88}
89
Ricky Liang1ef73e52016-05-24 16:32:34 +080090void SandboxedProcess::BindFd(int parent_fd, int child_fd) {
91 ProcessImpl::BindFd(parent_fd, child_fd);
92 bound_fds_.push_back(parent_fd);
93}
94
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050095void SandboxedProcess::DisableSandbox() {
96 sandboxing_ = false;
97}
98
99void SandboxedProcess::SandboxAs(const std::string& user,
100 const std::string& group) {
101 sandboxing_ = true;
102 user_ = user;
103 group_ = group;
104}
105
Justin Carlson73310fb2016-10-11 16:13:26 -0700106void SandboxedProcess::SetSeccompFilterPolicyFile(const std::string& path) {
107 seccomp_filter_policy_file_ = path;
108}
109
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -0700110void SandboxedProcess::AllowAccessRootMountNamespace() {
111 access_root_mount_ns_ = true;
112}
113
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -0700114bool SandboxedProcess::KillProcessGroup() {
115 pid_t minijail_pid = pid();
116 if (minijail_pid == 0) {
117 LOG(ERROR) << "Process is not running";
118 return false;
119 }
120
121 // Minijail sets its process group ID equal to its PID,
122 // so we can use pid() as PGID. Check that's still the case.
123 pid_t pgid = getpgid(minijail_pid);
124 if (pgid < 0) {
125 PLOG(ERROR) << "getpgid(minijail_pid) failed";
126 return false;
127 }
128 if (pgid != minijail_pid) {
129 LOG(ERROR) << "Minijail PGID " << pgid << " is different from PID "
130 << minijail_pid;
131 return false;
132 }
133
134 // kill(-pgid) kills every process with process group ID |pgid|.
135 if (kill(-pgid, SIGKILL) < 0) {
136 PLOG(ERROR) << "kill(-pgid, SIGKILL) failed";
137 return false;
138 }
139
140 // If kill(2) succeeded, we release the PID.
141 UpdatePid(0);
142
143 // We only expect to reap one process, the Minijail process.
144 // If the jailed process dies first, Minijail or init will reap it.
145 // If the Minijail process dies first, we will reap it. The jailed process
146 // will then be reaped by init.
147 for (size_t attempt = 0; attempt < kMaxWaitAttempts; ++attempt) {
148 int status = 0;
149 // waitpid(-pgid) waits for any child process with process group ID |pgid|.
150 pid_t waited = waitpid(-pgid, &status, WNOHANG);
151 int saved_errno = errno;
152
153 if (waited < 0) {
154 if (saved_errno == ECHILD) {
155 // Processes with PGID |pgid| don't exist, so we're done.
156 return true;
157 }
158 PLOG(ERROR) << "waitpid(-pgid) failed";
159 return false;
160 }
161
162 if (waited > 0) {
163 if (waited != minijail_pid) {
164 LOG(WARNING) << "Expecting PID " << minijail_pid << ", got PID "
165 << waited;
166 }
167 return true;
168 }
169
170 usleep(kDelayUSec);
171 }
172
173 LOG(WARNING) << "Process " << minijail_pid << " did not terminate";
174 return false;
175}
176
Ben Chana0011d82014-05-13 00:19:29 -0700177} // namespace debugd