blob: a18083d2b60ee21f530927c430c1ca66f98812ba [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
Ben Chan297c3c22013-07-17 17:34:12 -070031// static
32bool SandboxedProcess::GetHelperPath(const std::string& relative_path,
33 std::string* full_path) {
34 // This environment variable controls the root directory for debugd helpers,
35 // which lets people develop helpers even when verified boot is on.
36 const char* helpers_dir = getenv("DEBUGD_HELPERS");
37 std::string path = base::StringPrintf(
38 "%s/%s",
39 helpers_dir ? helpers_dir : "/usr/libexec/debugd/helpers",
40 relative_path.c_str());
41
42 if (path.length() > PATH_MAX)
43 return false;
44
45 *full_path = path;
46 return true;
47}
48
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050049bool SandboxedProcess::Init() {
50 const char *kMiniJail = "/sbin/minijail0";
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070051
52 AddArg(kMiniJail);
53 // Enter a new mount namespace. This is done for every process to avoid
54 // affecting the original mount namespace.
55 AddArg("-v");
56
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050057 if (sandboxing_) {
58 if (user_.empty() || group_.empty())
59 return false;
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -070060
Elly Fong-Jonese56a8f62013-01-23 15:50:21 -050061 if (user_ != "root") {
62 AddArg("-u");
63 AddArg(user_);
64 }
65 if (group_ != "root") {
66 AddArg("-g");
67 AddArg(group_);
68 }
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050069 }
Jorge Lucangeli Obes623f8ca2014-09-18 10:50:06 -070070
71 if (access_root_mount_ns_) {
72 // Enter root mount namespace.
73 AddStringOption("-V", "/proc/1/ns/mnt");
74 }
75
76 AddArg("--");
77
Elly Fong-Jonesd9a16cd2012-11-12 16:09:49 -050078 return true;
79}
80
81void SandboxedProcess::DisableSandbox() {
82 sandboxing_ = false;
83}
84
85void SandboxedProcess::SandboxAs(const std::string& user,
86 const std::string& group) {
87 sandboxing_ = true;
88 user_ = user;
89 group_ = group;
90}
91
Jorge Lucangeli Obesc99a12a2014-09-17 16:43:40 -070092void SandboxedProcess::AllowAccessRootMountNamespace() {
93 access_root_mount_ns_ = true;
94}
95
Jorge Lucangeli Obes389a9ee2015-05-14 17:37:01 -070096bool SandboxedProcess::KillProcessGroup() {
97 pid_t minijail_pid = pid();
98 if (minijail_pid == 0) {
99 LOG(ERROR) << "Process is not running";
100 return false;
101 }
102
103 // Minijail sets its process group ID equal to its PID,
104 // so we can use pid() as PGID. Check that's still the case.
105 pid_t pgid = getpgid(minijail_pid);
106 if (pgid < 0) {
107 PLOG(ERROR) << "getpgid(minijail_pid) failed";
108 return false;
109 }
110 if (pgid != minijail_pid) {
111 LOG(ERROR) << "Minijail PGID " << pgid << " is different from PID "
112 << minijail_pid;
113 return false;
114 }
115
116 // kill(-pgid) kills every process with process group ID |pgid|.
117 if (kill(-pgid, SIGKILL) < 0) {
118 PLOG(ERROR) << "kill(-pgid, SIGKILL) failed";
119 return false;
120 }
121
122 // If kill(2) succeeded, we release the PID.
123 UpdatePid(0);
124
125 // We only expect to reap one process, the Minijail process.
126 // If the jailed process dies first, Minijail or init will reap it.
127 // If the Minijail process dies first, we will reap it. The jailed process
128 // will then be reaped by init.
129 for (size_t attempt = 0; attempt < kMaxWaitAttempts; ++attempt) {
130 int status = 0;
131 // waitpid(-pgid) waits for any child process with process group ID |pgid|.
132 pid_t waited = waitpid(-pgid, &status, WNOHANG);
133 int saved_errno = errno;
134
135 if (waited < 0) {
136 if (saved_errno == ECHILD) {
137 // Processes with PGID |pgid| don't exist, so we're done.
138 return true;
139 }
140 PLOG(ERROR) << "waitpid(-pgid) failed";
141 return false;
142 }
143
144 if (waited > 0) {
145 if (waited != minijail_pid) {
146 LOG(WARNING) << "Expecting PID " << minijail_pid << ", got PID "
147 << waited;
148 }
149 return true;
150 }
151
152 usleep(kDelayUSec);
153 }
154
155 LOG(WARNING) << "Process " << minijail_pid << " did not terminate";
156 return false;
157}
158
Ben Chana0011d82014-05-13 00:19:29 -0700159} // namespace debugd