1 /*
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 //#define LOG_NDEBUG 0
18 #define LOG_TAG "libprocessgroup"
19
20 #include <dirent.h>
21 #include <errno.h>
22 #include <fcntl.h>
23 #include <grp.h>
24 #include <pwd.h>
25 #include <sys/mman.h>
26 #include <sys/mount.h>
27 #include <sys/stat.h>
28 #include <sys/types.h>
29 #include <time.h>
30 #include <unistd.h>
31
32 #include <regex>
33
34 #include <android-base/file.h>
35 #include <android-base/logging.h>
36 #include <android-base/properties.h>
37 #include <android-base/stringprintf.h>
38 #include <android-base/unique_fd.h>
39 #include <android/cgrouprc.h>
40 #include <json/reader.h>
41 #include <json/value.h>
42 #include <processgroup/format/cgroup_file.h>
43 #include <processgroup/processgroup.h>
44 #include <processgroup/setup.h>
45
46 #include "cgroup_descriptor.h"
47
48 using android::base::GetUintProperty;
49 using android::base::StringPrintf;
50 using android::base::unique_fd;
51
52 namespace android {
53 namespace cgrouprc {
54
55 static constexpr const char* CGROUPS_DESC_FILE = "/etc/cgroups.json";
56 static constexpr const char* CGROUPS_DESC_VENDOR_FILE = "/vendor/etc/cgroups.json";
57
58 static constexpr const char* TEMPLATE_CGROUPS_DESC_API_FILE = "/etc/task_profiles/cgroups_%u.json";
59
ChangeDirModeAndOwner(const std::string & path,mode_t mode,const std::string & uid,const std::string & gid,bool permissive_mode=false)60 static bool ChangeDirModeAndOwner(const std::string& path, mode_t mode, const std::string& uid,
61 const std::string& gid, bool permissive_mode = false) {
62 uid_t pw_uid = -1;
63 gid_t gr_gid = -1;
64
65 if (!uid.empty()) {
66 passwd* uid_pwd = getpwnam(uid.c_str());
67 if (!uid_pwd) {
68 PLOG(ERROR) << "Unable to decode UID for '" << uid << "'";
69 return false;
70 }
71
72 pw_uid = uid_pwd->pw_uid;
73 gr_gid = -1;
74
75 if (!gid.empty()) {
76 group* gid_pwd = getgrnam(gid.c_str());
77 if (!gid_pwd) {
78 PLOG(ERROR) << "Unable to decode GID for '" << gid << "'";
79 return false;
80 }
81 gr_gid = gid_pwd->gr_gid;
82 }
83 }
84
85 auto dir = std::unique_ptr<DIR, decltype(&closedir)>(opendir(path.c_str()), closedir);
86
87 if (dir == NULL) {
88 PLOG(ERROR) << "opendir failed for " << path;
89 return false;
90 }
91
92 struct dirent* dir_entry;
93 while ((dir_entry = readdir(dir.get()))) {
94 if (!strcmp("..", dir_entry->d_name)) {
95 continue;
96 }
97
98 std::string file_path = path + "/" + dir_entry->d_name;
99
100 if (pw_uid != -1 && lchown(file_path.c_str(), pw_uid, gr_gid) < 0) {
101 PLOG(ERROR) << "lchown() failed for " << file_path;
102 return false;
103 }
104
105 if (fchmodat(AT_FDCWD, file_path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0 &&
106 (errno != EROFS || !permissive_mode)) {
107 PLOG(ERROR) << "fchmodat() failed for " << path;
108 return false;
109 }
110 }
111
112 return true;
113 }
114
Mkdir(const std::string & path,mode_t mode,const std::string & uid,const std::string & gid)115 static bool Mkdir(const std::string& path, mode_t mode, const std::string& uid,
116 const std::string& gid) {
117 bool permissive_mode = false;
118
119 if (mode == 0) {
120 /* Allow chmod to fail */
121 permissive_mode = true;
122 mode = 0755;
123 }
124
125 if (mkdir(path.c_str(), mode) != 0) {
126 // /acct is a special case when the directory already exists
127 if (errno != EEXIST) {
128 PLOG(ERROR) << "mkdir() failed for " << path;
129 return false;
130 } else {
131 permissive_mode = true;
132 }
133 }
134
135 if (uid.empty() && permissive_mode) {
136 return true;
137 }
138
139 if (!ChangeDirModeAndOwner(path, mode, uid, gid, permissive_mode)) {
140 PLOG(ERROR) << "change of ownership or mode failed for " << path;
141 return false;
142 }
143
144 return true;
145 }
146
MergeCgroupToDescriptors(std::map<std::string,CgroupDescriptor> * descriptors,const Json::Value & cgroup,const std::string & name,const std::string & root_path,int cgroups_version)147 static void MergeCgroupToDescriptors(std::map<std::string, CgroupDescriptor>* descriptors,
148 const Json::Value& cgroup, const std::string& name,
149 const std::string& root_path, int cgroups_version) {
150 std::string path;
151
152 if (!root_path.empty()) {
153 path = root_path + "/" + cgroup["Path"].asString();
154 } else {
155 path = cgroup["Path"].asString();
156 }
157
158 uint32_t controller_flags = 0;
159
160 if (cgroup["NeedsActivation"].isBool() && cgroup["NeedsActivation"].asBool()) {
161 controller_flags |= CGROUPRC_CONTROLLER_FLAG_NEEDS_ACTIVATION;
162 }
163
164 if (cgroup["Optional"].isBool() && cgroup["Optional"].asBool()) {
165 controller_flags |= CGROUPRC_CONTROLLER_FLAG_OPTIONAL;
166 }
167
168 CgroupDescriptor descriptor(
169 cgroups_version, name, path, std::strtoul(cgroup["Mode"].asString().c_str(), 0, 8),
170 cgroup["UID"].asString(), cgroup["GID"].asString(), controller_flags);
171
172 auto iter = descriptors->find(name);
173 if (iter == descriptors->end()) {
174 descriptors->emplace(name, descriptor);
175 } else {
176 iter->second = descriptor;
177 }
178 }
179
ReadDescriptorsFromFile(const std::string & file_name,std::map<std::string,CgroupDescriptor> * descriptors)180 static bool ReadDescriptorsFromFile(const std::string& file_name,
181 std::map<std::string, CgroupDescriptor>* descriptors) {
182 std::vector<CgroupDescriptor> result;
183 std::string json_doc;
184
185 if (!android::base::ReadFileToString(file_name, &json_doc)) {
186 PLOG(ERROR) << "Failed to read task profiles from " << file_name;
187 return false;
188 }
189
190 Json::CharReaderBuilder builder;
191 std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
192 Json::Value root;
193 std::string errorMessage;
194 if (!reader->parse(&*json_doc.begin(), &*json_doc.end(), &root, &errorMessage)) {
195 LOG(ERROR) << "Failed to parse cgroups description: " << errorMessage;
196 return false;
197 }
198
199 if (root.isMember("Cgroups")) {
200 const Json::Value& cgroups = root["Cgroups"];
201 for (Json::Value::ArrayIndex i = 0; i < cgroups.size(); ++i) {
202 std::string name = cgroups[i]["Controller"].asString();
203 MergeCgroupToDescriptors(descriptors, cgroups[i], name, "", 1);
204 }
205 }
206
207 if (root.isMember("Cgroups2")) {
208 const Json::Value& cgroups2 = root["Cgroups2"];
209 std::string root_path = cgroups2["Path"].asString();
210 MergeCgroupToDescriptors(descriptors, cgroups2, CGROUPV2_CONTROLLER_NAME, "", 2);
211
212 const Json::Value& childGroups = cgroups2["Controllers"];
213 for (Json::Value::ArrayIndex i = 0; i < childGroups.size(); ++i) {
214 std::string name = childGroups[i]["Controller"].asString();
215 MergeCgroupToDescriptors(descriptors, childGroups[i], name, root_path, 2);
216 }
217 }
218
219 return true;
220 }
221
ReadDescriptors(std::map<std::string,CgroupDescriptor> * descriptors)222 static bool ReadDescriptors(std::map<std::string, CgroupDescriptor>* descriptors) {
223 // load system cgroup descriptors
224 if (!ReadDescriptorsFromFile(CGROUPS_DESC_FILE, descriptors)) {
225 return false;
226 }
227
228 // load API-level specific system cgroups descriptors if available
229 unsigned int api_level = GetUintProperty<unsigned int>("ro.product.first_api_level", 0);
230 if (api_level > 0) {
231 std::string api_cgroups_path =
232 android::base::StringPrintf(TEMPLATE_CGROUPS_DESC_API_FILE, api_level);
233 if (!access(api_cgroups_path.c_str(), F_OK) || errno != ENOENT) {
234 if (!ReadDescriptorsFromFile(api_cgroups_path, descriptors)) {
235 return false;
236 }
237 }
238 }
239
240 // load vendor cgroup descriptors if the file exists
241 if (!access(CGROUPS_DESC_VENDOR_FILE, F_OK) &&
242 !ReadDescriptorsFromFile(CGROUPS_DESC_VENDOR_FILE, descriptors)) {
243 return false;
244 }
245
246 return true;
247 }
248
249 // To avoid issues in sdk_mac build
250 #if defined(__ANDROID__)
251
SetupCgroup(const CgroupDescriptor & descriptor)252 static bool SetupCgroup(const CgroupDescriptor& descriptor) {
253 const format::CgroupController* controller = descriptor.controller();
254
255 int result;
256 if (controller->version() == 2) {
257 result = 0;
258 if (!strcmp(controller->name(), CGROUPV2_CONTROLLER_NAME)) {
259 // /sys/fs/cgroup is created by cgroup2 with specific selinux permissions,
260 // try to create again in case the mount point is changed
261 if (!Mkdir(controller->path(), 0, "", "")) {
262 LOG(ERROR) << "Failed to create directory for " << controller->name() << " cgroup";
263 return false;
264 }
265
266 result = mount("none", controller->path(), "cgroup2", MS_NODEV | MS_NOEXEC | MS_NOSUID,
267 nullptr);
268
269 // selinux permissions change after mounting, so it's ok to change mode and owner now
270 if (!ChangeDirModeAndOwner(controller->path(), descriptor.mode(), descriptor.uid(),
271 descriptor.gid())) {
272 LOG(ERROR) << "Failed to create directory for " << controller->name() << " cgroup";
273 result = -1;
274 }
275 } else {
276 if (!Mkdir(controller->path(), descriptor.mode(), descriptor.uid(), descriptor.gid())) {
277 LOG(ERROR) << "Failed to create directory for " << controller->name() << " cgroup";
278 return false;
279 }
280
281 if (controller->flags() & CGROUPRC_CONTROLLER_FLAG_NEEDS_ACTIVATION) {
282 std::string str = std::string("+") + controller->name();
283 std::string path = std::string(controller->path()) + "/cgroup.subtree_control";
284
285 if (!base::WriteStringToFile(str, path)) {
286 LOG(ERROR) << "Failed to activate controller " << controller->name();
287 return false;
288 }
289 }
290 }
291 } else {
292 // mkdir <path> [mode] [owner] [group]
293 if (!Mkdir(controller->path(), descriptor.mode(), descriptor.uid(), descriptor.gid())) {
294 LOG(ERROR) << "Failed to create directory for " << controller->name() << " cgroup";
295 return false;
296 }
297
298 // Unfortunately historically cpuset controller was mounted using a mount command
299 // different from all other controllers. This results in controller attributes not
300 // to be prepended with controller name. For example this way instead of
301 // /dev/cpuset/cpuset.cpus the attribute becomes /dev/cpuset/cpus which is what
302 // the system currently expects.
303 if (!strcmp(controller->name(), "cpuset")) {
304 // mount cpuset none /dev/cpuset nodev noexec nosuid
305 result = mount("none", controller->path(), controller->name(),
306 MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr);
307 } else {
308 // mount cgroup none <path> nodev noexec nosuid <controller>
309 result = mount("none", controller->path(), "cgroup", MS_NODEV | MS_NOEXEC | MS_NOSUID,
310 controller->name());
311 }
312 }
313
314 if (result < 0) {
315 bool optional = controller->flags() & CGROUPRC_CONTROLLER_FLAG_OPTIONAL;
316
317 if (optional && errno == EINVAL) {
318 // Optional controllers are allowed to fail to mount if kernel does not support them
319 LOG(INFO) << "Optional " << controller->name() << " cgroup controller is not mounted";
320 } else {
321 PLOG(ERROR) << "Failed to mount " << controller->name() << " cgroup";
322 return false;
323 }
324 }
325
326 return true;
327 }
328
329 #else
330
331 // Stubs for non-Android targets.
SetupCgroup(const CgroupDescriptor &)332 static bool SetupCgroup(const CgroupDescriptor&) {
333 return false;
334 }
335
336 #endif
337
WriteRcFile(const std::map<std::string,CgroupDescriptor> & descriptors)338 static bool WriteRcFile(const std::map<std::string, CgroupDescriptor>& descriptors) {
339 unique_fd fd(TEMP_FAILURE_RETRY(open(CGROUPS_RC_PATH, O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC,
340 S_IRUSR | S_IRGRP | S_IROTH)));
341 if (fd < 0) {
342 PLOG(ERROR) << "open() failed for " << CGROUPS_RC_PATH;
343 return false;
344 }
345
346 format::CgroupFile fl;
347 fl.version_ = format::CgroupFile::FILE_CURR_VERSION;
348 fl.controller_count_ = descriptors.size();
349 int ret = TEMP_FAILURE_RETRY(write(fd, &fl, sizeof(fl)));
350 if (ret < 0) {
351 PLOG(ERROR) << "write() failed for " << CGROUPS_RC_PATH;
352 return false;
353 }
354
355 for (const auto& [name, descriptor] : descriptors) {
356 ret = TEMP_FAILURE_RETRY(
357 write(fd, descriptor.controller(), sizeof(format::CgroupController)));
358 if (ret < 0) {
359 PLOG(ERROR) << "write() failed for " << CGROUPS_RC_PATH;
360 return false;
361 }
362 }
363
364 return true;
365 }
366
CgroupDescriptor(uint32_t version,const std::string & name,const std::string & path,mode_t mode,const std::string & uid,const std::string & gid,uint32_t flags=0)367 CgroupDescriptor::CgroupDescriptor(uint32_t version, const std::string& name,
368 const std::string& path, mode_t mode, const std::string& uid,
369 const std::string& gid, uint32_t flags = 0)
370 : controller_(version, flags, name, path), mode_(mode), uid_(uid), gid_(gid) {}
371
set_mounted(bool mounted)372 void CgroupDescriptor::set_mounted(bool mounted) {
373 uint32_t flags = controller_.flags();
374 if (mounted) {
375 flags |= CGROUPRC_CONTROLLER_FLAG_MOUNTED;
376 } else {
377 flags &= ~CGROUPRC_CONTROLLER_FLAG_MOUNTED;
378 }
379 controller_.set_flags(flags);
380 }
381
382 } // namespace cgrouprc
383 } // namespace android
384
CgroupSetup()385 bool CgroupSetup() {
386 using namespace android::cgrouprc;
387
388 std::map<std::string, CgroupDescriptor> descriptors;
389
390 if (getpid() != 1) {
391 LOG(ERROR) << "Cgroup setup can be done only by init process";
392 return false;
393 }
394
395 // Make sure we do this only one time. No need for std::call_once because
396 // init is a single-threaded process
397 if (access(CGROUPS_RC_PATH, F_OK) == 0) {
398 LOG(WARNING) << "Attempt to call SetupCgroups more than once";
399 return true;
400 }
401
402 // load cgroups.json file
403 if (!ReadDescriptors(&descriptors)) {
404 LOG(ERROR) << "Failed to load cgroup description file";
405 return false;
406 }
407
408 // setup cgroups
409 for (auto& [name, descriptor] : descriptors) {
410 if (SetupCgroup(descriptor)) {
411 descriptor.set_mounted(true);
412 } else {
413 // issue a warning and proceed with the next cgroup
414 LOG(WARNING) << "Failed to setup " << name << " cgroup";
415 }
416 }
417
418 // mkdir <CGROUPS_RC_DIR> 0711 system system
419 if (!Mkdir(android::base::Dirname(CGROUPS_RC_PATH), 0711, "system", "system")) {
420 LOG(ERROR) << "Failed to create directory for " << CGROUPS_RC_PATH << " file";
421 return false;
422 }
423
424 // Generate <CGROUPS_RC_FILE> file which can be directly mmapped into
425 // process memory. This optimizes performance, memory usage
426 // and limits infrormation shared with unprivileged processes
427 // to the minimum subset of information from cgroups.json
428 if (!WriteRcFile(descriptors)) {
429 LOG(ERROR) << "Failed to write " << CGROUPS_RC_PATH << " file";
430 return false;
431 }
432
433 // chmod 0644 <CGROUPS_RC_PATH>
434 if (fchmodat(AT_FDCWD, CGROUPS_RC_PATH, 0644, AT_SYMLINK_NOFOLLOW) < 0) {
435 PLOG(ERROR) << "fchmodat() failed";
436 return false;
437 }
438
439 return true;
440 }
441