blob: 34aa8370f6517380a8c4aa3f04d7ec6feb1d8318 [file] [log] [blame]
Vic Yange01ca4d2019-05-29 15:58:32 -07001/*
2 * Copyright (C) 2019 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "service_utils.h"
18
19#include <grp.h>
20#include <sys/mount.h>
21#include <sys/prctl.h>
22#include <sys/wait.h>
23
24#include <android-base/file.h>
25#include <android-base/logging.h>
26#include <android-base/properties.h>
27#include <android-base/stringprintf.h>
28#include <android-base/strings.h>
29#include <android-base/unique_fd.h>
30#include <processgroup/processgroup.h>
31
32#include "mount_namespace.h"
33
34using android::base::GetProperty;
35using android::base::StartsWith;
36using android::base::StringPrintf;
37using android::base::unique_fd;
38using android::base::WriteStringToFile;
39
40namespace android {
41namespace init {
42
43namespace {
44
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070045Result<void> EnterNamespace(int nstype, const char* path) {
Vic Yange01ca4d2019-05-29 15:58:32 -070046 auto fd = unique_fd{open(path, O_RDONLY | O_CLOEXEC)};
47 if (fd == -1) {
48 return ErrnoError() << "Could not open namespace at " << path;
49 }
50 if (setns(fd, nstype) == -1) {
51 return ErrnoError() << "Could not setns() namespace at " << path;
52 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070053 return {};
Vic Yange01ca4d2019-05-29 15:58:32 -070054}
55
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070056Result<void> SetUpMountNamespace(bool remount_proc, bool remount_sys) {
Vic Yange01ca4d2019-05-29 15:58:32 -070057 constexpr unsigned int kSafeFlags = MS_NODEV | MS_NOEXEC | MS_NOSUID;
58
59 // Recursively remount / as slave like zygote does so unmounting and mounting /proc
60 // doesn't interfere with the parent namespace's /proc mount. This will also
61 // prevent any other mounts/unmounts initiated by the service from interfering
62 // with the parent namespace but will still allow mount events from the parent
63 // namespace to propagate to the child.
64 if (mount("rootfs", "/", nullptr, (MS_SLAVE | MS_REC), nullptr) == -1) {
65 return ErrnoError() << "Could not remount(/) recursively as slave";
66 }
67
68 // umount() then mount() /proc and/or /sys
69 // Note that it is not sufficient to mount with MS_REMOUNT.
70 if (remount_proc) {
71 if (umount("/proc") == -1) {
72 return ErrnoError() << "Could not umount(/proc)";
73 }
74 if (mount("", "/proc", "proc", kSafeFlags, "") == -1) {
75 return ErrnoError() << "Could not mount(/proc)";
76 }
77 }
78 if (remount_sys) {
79 if (umount2("/sys", MNT_DETACH) == -1) {
80 return ErrnoError() << "Could not umount(/sys)";
81 }
82 if (mount("", "/sys", "sysfs", kSafeFlags, "") == -1) {
83 return ErrnoError() << "Could not mount(/sys)";
84 }
85 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070086 return {};
Vic Yange01ca4d2019-05-29 15:58:32 -070087}
88
Tom Cherrybbcbc2f2019-06-10 11:08:01 -070089Result<void> SetUpPidNamespace(const char* name) {
Vic Yange01ca4d2019-05-29 15:58:32 -070090 if (prctl(PR_SET_NAME, name) == -1) {
91 return ErrnoError() << "Could not set name";
92 }
93
94 pid_t child_pid = fork();
95 if (child_pid == -1) {
96 return ErrnoError() << "Could not fork init inside the PID namespace";
97 }
98
99 if (child_pid > 0) {
100 // So that we exit with the right status.
101 static int init_exitstatus = 0;
102 signal(SIGTERM, [](int) { _exit(init_exitstatus); });
103
104 pid_t waited_pid;
105 int status;
106 while ((waited_pid = wait(&status)) > 0) {
107 // This loop will end when there are no processes left inside the
108 // PID namespace or when the init process inside the PID namespace
109 // gets a signal.
110 if (waited_pid == child_pid) {
111 init_exitstatus = status;
112 }
113 }
114 if (!WIFEXITED(init_exitstatus)) {
115 _exit(EXIT_FAILURE);
116 }
117 _exit(WEXITSTATUS(init_exitstatus));
118 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700119 return {};
Vic Yange01ca4d2019-05-29 15:58:32 -0700120}
121
122void ZapStdio() {
Tom Cherry247ffbf2019-07-08 15:09:36 -0700123 auto fd = unique_fd{open("/dev/null", O_RDWR | O_CLOEXEC)};
Vic Yange01ca4d2019-05-29 15:58:32 -0700124 dup2(fd, 0);
125 dup2(fd, 1);
126 dup2(fd, 2);
Vic Yange01ca4d2019-05-29 15:58:32 -0700127}
128
129void OpenConsole(const std::string& console) {
Tom Cherry247ffbf2019-07-08 15:09:36 -0700130 auto fd = unique_fd{open(console.c_str(), O_RDWR | O_CLOEXEC)};
131 if (fd == -1) fd.reset(open("/dev/null", O_RDWR | O_CLOEXEC));
Vic Yange01ca4d2019-05-29 15:58:32 -0700132 ioctl(fd, TIOCSCTTY, 0);
133 dup2(fd, 0);
134 dup2(fd, 1);
135 dup2(fd, 2);
Vic Yange01ca4d2019-05-29 15:58:32 -0700136}
137
138} // namespace
139
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700140Result<void> EnterNamespaces(const NamespaceInfo& info, const std::string& name, bool pre_apexd) {
Vic Yange01ca4d2019-05-29 15:58:32 -0700141 for (const auto& [nstype, path] : info.namespaces_to_enter) {
142 if (auto result = EnterNamespace(nstype, path.c_str()); !result) {
143 return result;
144 }
145 }
146
147#if defined(__ANDROID__)
148 if (pre_apexd) {
149 if (!SwitchToBootstrapMountNamespaceIfNeeded()) {
150 return Error() << "could not enter into the bootstrap mount namespace";
151 }
152 }
153#endif
154
155 if (info.flags & CLONE_NEWNS) {
156 bool remount_proc = info.flags & CLONE_NEWPID;
157 bool remount_sys =
158 std::any_of(info.namespaces_to_enter.begin(), info.namespaces_to_enter.end(),
159 [](const auto& entry) { return entry.first == CLONE_NEWNET; });
160 if (auto result = SetUpMountNamespace(remount_proc, remount_sys); !result) {
161 return result;
162 }
163 }
164
165 if (info.flags & CLONE_NEWPID) {
166 // This will fork again to run an init process inside the PID namespace.
167 if (auto result = SetUpPidNamespace(name.c_str()); !result) {
168 return result;
169 }
170 }
171
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700172 return {};
Vic Yange01ca4d2019-05-29 15:58:32 -0700173}
174
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700175Result<void> SetProcessAttributes(const ProcessAttributes& attr) {
Vic Yange01ca4d2019-05-29 15:58:32 -0700176 if (attr.ioprio_class != IoSchedClass_NONE) {
177 if (android_set_ioprio(getpid(), attr.ioprio_class, attr.ioprio_pri)) {
178 PLOG(ERROR) << "failed to set pid " << getpid() << " ioprio=" << attr.ioprio_class
179 << "," << attr.ioprio_pri;
180 }
181 }
182
183 if (!attr.console.empty()) {
184 setsid();
185 OpenConsole(attr.console);
186 } else {
187 if (setpgid(0, getpid()) == -1) {
188 return ErrnoError() << "setpgid failed";
189 }
190 ZapStdio();
191 }
192
193 for (const auto& rlimit : attr.rlimits) {
194 if (setrlimit(rlimit.first, &rlimit.second) == -1) {
195 return ErrnoError() << StringPrintf(
196 "setrlimit(%d, {rlim_cur=%ld, rlim_max=%ld}) failed", rlimit.first,
197 rlimit.second.rlim_cur, rlimit.second.rlim_max);
198 }
199 }
200
201 if (attr.gid) {
202 if (setgid(attr.gid) != 0) {
203 return ErrnoError() << "setgid failed";
204 }
205 }
206 if (setgroups(attr.supp_gids.size(), const_cast<gid_t*>(&attr.supp_gids[0])) != 0) {
207 return ErrnoError() << "setgroups failed";
208 }
209 if (attr.uid) {
210 if (setuid(attr.uid) != 0) {
211 return ErrnoError() << "setuid failed";
212 }
213 }
214
215 if (attr.priority != 0) {
216 if (setpriority(PRIO_PROCESS, 0, attr.priority) != 0) {
217 return ErrnoError() << "setpriority failed";
218 }
219 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700220 return {};
Vic Yange01ca4d2019-05-29 15:58:32 -0700221}
222
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700223Result<void> WritePidToFiles(std::vector<std::string>* files) {
Vic Yange01ca4d2019-05-29 15:58:32 -0700224 // See if there were "writepid" instructions to write to files under cpuset path.
225 std::string cpuset_path;
226 if (CgroupGetControllerPath("cpuset", &cpuset_path)) {
227 auto cpuset_predicate = [&cpuset_path](const std::string& path) {
228 return StartsWith(path, cpuset_path + "/");
229 };
230 auto iter = std::find_if(files->begin(), files->end(), cpuset_predicate);
231 if (iter == files->end()) {
232 // There were no "writepid" instructions for cpusets, check if the system default
233 // cpuset is specified to be used for the process.
234 std::string default_cpuset = GetProperty("ro.cpuset.default", "");
235 if (!default_cpuset.empty()) {
236 // Make sure the cpuset name starts and ends with '/'.
237 // A single '/' means the 'root' cpuset.
238 if (default_cpuset.front() != '/') {
239 default_cpuset.insert(0, 1, '/');
240 }
241 if (default_cpuset.back() != '/') {
242 default_cpuset.push_back('/');
243 }
244 files->push_back(
245 StringPrintf("%s%stasks", cpuset_path.c_str(), default_cpuset.c_str()));
246 }
247 }
248 } else {
249 LOG(ERROR) << "cpuset cgroup controller is not mounted!";
250 }
251 std::string pid_str = std::to_string(getpid());
252 for (const auto& file : *files) {
253 if (!WriteStringToFile(pid_str, file)) {
254 return ErrnoError() << "couldn't write " << pid_str << " to " << file;
255 }
256 }
Tom Cherrybbcbc2f2019-06-10 11:08:01 -0700257 return {};
Vic Yange01ca4d2019-05-29 15:58:32 -0700258}
259
260} // namespace init
261} // namespace android