blob: 80438e82ff5ba41c1875a4d819deb5ef21820a6a [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
81 AddArg("--");
82
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050083 return true;
84}
85
Ricky Liang1ef73e52016-05-24 16:32:34 +080086void SandboxedProcess::BindFd(int parent_fd, int child_fd) {
87 ProcessImpl::BindFd(parent_fd, child_fd);
88 bound_fds_.push_back(parent_fd);
89}
90
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050091void 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
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -0700102void SandboxedProcess::AllowAccessRootMountNamespace() {
103 access_root_mount_ns_ = true;
104}
105
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -0700106bool SandboxedProcess::KillProcessGroup() {
107 pid_t minijail_pid = pid();
108 if (minijail_pid == 0) {
109 LOG(ERROR) << "Process is not running";
110 return false;
111 }
112
113 // Minijail sets its process group ID equal to its PID,
114 // so we can use pid() as PGID. Check that's still the case.
115 pid_t pgid = getpgid(minijail_pid);
116 if (pgid < 0) {
117 PLOG(ERROR) << "getpgid(minijail_pid) failed";
118 return false;
119 }
120 if (pgid != minijail_pid) {
121 LOG(ERROR) << "Minijail PGID " << pgid << " is different from PID "
122 << minijail_pid;
123 return false;
124 }
125
126 // kill(-pgid) kills every process with process group ID |pgid|.
127 if (kill(-pgid, SIGKILL) < 0) {
128 PLOG(ERROR) << "kill(-pgid, SIGKILL) failed";
129 return false;
130 }
131
132 // If kill(2) succeeded, we release the PID.
133 UpdatePid(0);
134
135 // We only expect to reap one process, the Minijail process.
136 // If the jailed process dies first, Minijail or init will reap it.
137 // If the Minijail process dies first, we will reap it. The jailed process
138 // will then be reaped by init.
139 for (size_t attempt = 0; attempt < kMaxWaitAttempts; ++attempt) {
140 int status = 0;
141 // waitpid(-pgid) waits for any child process with process group ID |pgid|.
142 pid_t waited = waitpid(-pgid, &status, WNOHANG);
143 int saved_errno = errno;
144
145 if (waited < 0) {
146 if (saved_errno == ECHILD) {
147 // Processes with PGID |pgid| don't exist, so we're done.
148 return true;
149 }
150 PLOG(ERROR) << "waitpid(-pgid) failed";
151 return false;
152 }
153
154 if (waited > 0) {
155 if (waited != minijail_pid) {
156 LOG(WARNING) << "Expecting PID " << minijail_pid << ", got PID "
157 << waited;
158 }
159 return true;
160 }
161
162 usleep(kDelayUSec);
163 }
164
165 LOG(WARNING) << "Process " << minijail_pid << " did not terminate";
166 return false;
167}
168
Ben Chana0011d82014-05-13 00:19:29 -0700169} // namespace debugd