blob: d09481181890f43cf8e60b2af629a858ab28728f [file] [log] [blame]
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -08001/*
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 <errno.h>
21#include <fcntl.h>
Suren Baghdasaryane3ad8882019-02-06 13:25:29 -080022#include <grp.h>
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080023#include <pwd.h>
24#include <sys/mman.h>
25#include <sys/mount.h>
26#include <sys/stat.h>
27#include <sys/types.h>
28#include <time.h>
29#include <unistd.h>
30
31#include <regex>
32
33#include <android-base/file.h>
34#include <android-base/logging.h>
35#include <android-base/properties.h>
36#include <android-base/stringprintf.h>
37#include <android-base/unique_fd.h>
38#include <cgroup_map.h>
39#include <json/reader.h>
40#include <json/value.h>
41#include <processgroup/processgroup.h>
42
43using android::base::GetBoolProperty;
44using android::base::StringPrintf;
45using android::base::unique_fd;
46
47static constexpr const char* CGROUPS_DESC_FILE = "/etc/cgroups.json";
48
49static constexpr const char* CGROUP_PROCS_FILE = "/cgroup.procs";
50static constexpr const char* CGROUP_TASKS_FILE = "/tasks";
51static constexpr const char* CGROUP_TASKS_FILE_V2 = "/cgroup.tasks";
52
53static bool Mkdir(const std::string& path, mode_t mode, const std::string& uid,
54 const std::string& gid) {
55 if (mode == 0) {
56 mode = 0755;
57 }
58
59 if (mkdir(path.c_str(), mode) != 0) {
60 /* chmod in case the directory already exists */
61 if (errno == EEXIST) {
62 if (fchmodat(AT_FDCWD, path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) {
63 // /acct is a special case when the directory already exists
64 // TODO: check if file mode is already what we want instead of using EROFS
65 if (errno != EROFS) {
66 PLOG(ERROR) << "fchmodat() failed for " << path;
67 return false;
68 }
69 }
70 } else {
71 PLOG(ERROR) << "mkdir() failed for " << path;
72 return false;
73 }
74 }
75
Suren Baghdasaryane3ad8882019-02-06 13:25:29 -080076 if (uid.empty()) {
77 return true;
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080078 }
79
Suren Baghdasaryane3ad8882019-02-06 13:25:29 -080080 passwd* uid_pwd = getpwnam(uid.c_str());
81 if (!uid_pwd) {
82 PLOG(ERROR) << "Unable to decode UID for '" << uid << "'";
83 return false;
84 }
85
86 uid_t pw_uid = uid_pwd->pw_uid;
87 gid_t gr_gid = -1;
88 if (!gid.empty()) {
89 group* gid_pwd = getgrnam(gid.c_str());
90 if (!gid_pwd) {
91 PLOG(ERROR) << "Unable to decode GID for '" << gid << "'";
92 return false;
93 }
94 gr_gid = gid_pwd->gr_gid;
95 }
96
97 if (lchown(path.c_str(), pw_uid, gr_gid) < 0) {
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -080098 PLOG(ERROR) << "lchown() failed for " << path;
99 return false;
100 }
101
102 /* chown may have cleared S_ISUID and S_ISGID, chmod again */
103 if (mode & (S_ISUID | S_ISGID)) {
104 if (fchmodat(AT_FDCWD, path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) {
105 PLOG(ERROR) << "fchmodat() failed for " << path;
106 return false;
107 }
108 }
109
110 return true;
111}
112
113static bool ReadDescriptors(std::map<std::string, CgroupDescriptor>* descriptors) {
114 std::vector<CgroupDescriptor> result;
115 std::string json_doc;
116
117 if (!android::base::ReadFileToString(CGROUPS_DESC_FILE, &json_doc)) {
118 LOG(ERROR) << "Failed to read task profiles from " << CGROUPS_DESC_FILE;
119 return false;
120 }
121
122 Json::Reader reader;
123 Json::Value root;
124 if (!reader.parse(json_doc, root)) {
125 LOG(ERROR) << "Failed to parse cgroups description: " << reader.getFormattedErrorMessages();
126 return false;
127 }
128
Suren Baghdasaryand032a922019-02-12 18:20:38 -0800129 if (root.isMember("Cgroups")) {
130 const Json::Value& cgroups = root["Cgroups"];
131 for (Json::Value::ArrayIndex i = 0; i < cgroups.size(); ++i) {
132 std::string name = cgroups[i]["Controller"].asString();
133 descriptors->emplace(std::make_pair(
134 name,
135 CgroupDescriptor(1, name, cgroups[i]["Path"].asString(),
136 std::strtoul(cgroups[i]["Mode"].asString().c_str(), 0, 8),
137 cgroups[i]["UID"].asString(), cgroups[i]["GID"].asString())));
138 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800139 }
140
Suren Baghdasaryand032a922019-02-12 18:20:38 -0800141 if (root.isMember("Cgroups2")) {
142 const Json::Value& cgroups2 = root["Cgroups2"];
143 descriptors->emplace(std::make_pair(
144 CGROUPV2_CONTROLLER_NAME,
145 CgroupDescriptor(2, CGROUPV2_CONTROLLER_NAME, cgroups2["Path"].asString(),
146 std::strtoul(cgroups2["Mode"].asString().c_str(), 0, 8),
147 cgroups2["UID"].asString(), cgroups2["GID"].asString())));
148 }
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800149
150 return true;
151}
152
Suren Baghdasaryanff25a5f2019-02-02 23:12:01 -0800153// To avoid issues in sdk_mac build
154#if defined(__ANDROID__)
155
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800156static bool SetupCgroup(const CgroupDescriptor& descriptor) {
157 const CgroupController* controller = descriptor.controller();
158
159 // mkdir <path> [mode] [owner] [group]
160 if (!Mkdir(controller->path(), descriptor.mode(), descriptor.uid(), descriptor.gid())) {
161 PLOG(ERROR) << "Failed to create directory for " << controller->name() << " cgroup";
162 return false;
163 }
164
165 int result;
166 if (controller->version() == 2) {
167 result = mount("none", controller->path(), "cgroup2", MS_NODEV | MS_NOEXEC | MS_NOSUID,
168 nullptr);
169 } else {
170 // Unfortunately historically cpuset controller was mounted using a mount command
171 // different from all other controllers. This results in controller attributes not
172 // to be prepended with controller name. For example this way instead of
173 // /dev/cpuset/cpuset.cpus the attribute becomes /dev/cpuset/cpus which is what
174 // the system currently expects.
175 if (!strcmp(controller->name(), "cpuset")) {
176 // mount cpuset none /dev/cpuset nodev noexec nosuid
177 result = mount("none", controller->path(), controller->name(),
178 MS_NODEV | MS_NOEXEC | MS_NOSUID, nullptr);
179 } else {
180 // mount cgroup none <path> nodev noexec nosuid <controller>
181 result = mount("none", controller->path(), "cgroup", MS_NODEV | MS_NOEXEC | MS_NOSUID,
182 controller->name());
183 }
184 }
185
186 if (result < 0) {
187 PLOG(ERROR) << "Failed to mount " << controller->name() << " cgroup";
188 return false;
189 }
190
191 return true;
192}
193
Suren Baghdasaryanff25a5f2019-02-02 23:12:01 -0800194#else
195
196// Stubs for non-Android targets.
197static bool SetupCgroup(const CgroupDescriptor&) {
198 return false;
199}
200
201#endif
202
Suren Baghdasaryan82b72a52018-12-21 11:41:50 -0800203static bool WriteRcFile(const std::map<std::string, CgroupDescriptor>& descriptors) {
204 std::string cgroup_rc_path = StringPrintf("%s/%s", CGROUPS_RC_DIR, CgroupMap::CGROUPS_RC_FILE);
205 unique_fd fd(TEMP_FAILURE_RETRY(open(cgroup_rc_path.c_str(),
206 O_CREAT | O_WRONLY | O_TRUNC | O_CLOEXEC,
207 S_IRUSR | S_IRGRP | S_IROTH)));
208 if (fd < 0) {
209 PLOG(ERROR) << "open() failed for " << cgroup_rc_path;
210 return false;
211 }
212
213 CgroupFile fl;
214 fl.version_ = CgroupFile::FILE_CURR_VERSION;
215 fl.controller_count_ = descriptors.size();
216 int ret = TEMP_FAILURE_RETRY(write(fd, &fl, sizeof(fl)));
217 if (ret < 0) {
218 PLOG(ERROR) << "write() failed for " << cgroup_rc_path;
219 return false;
220 }
221
222 for (const auto& [name, descriptor] : descriptors) {
223 ret = TEMP_FAILURE_RETRY(write(fd, descriptor.controller(), sizeof(CgroupController)));
224 if (ret < 0) {
225 PLOG(ERROR) << "write() failed for " << cgroup_rc_path;
226 return false;
227 }
228 }
229
230 return true;
231}
232
233CgroupController::CgroupController(uint32_t version, const std::string& name,
234 const std::string& path) {
235 version_ = version;
236 strncpy(name_, name.c_str(), sizeof(name_) - 1);
237 name_[sizeof(name_) - 1] = '\0';
238 strncpy(path_, path.c_str(), sizeof(path_) - 1);
239 path_[sizeof(path_) - 1] = '\0';
240}
241
242std::string CgroupController::GetTasksFilePath(const std::string& path) const {
243 std::string tasks_path = path_;
244
245 if (!path.empty()) {
246 tasks_path += "/" + path;
247 }
248 return (version_ == 1) ? tasks_path + CGROUP_TASKS_FILE : tasks_path + CGROUP_TASKS_FILE_V2;
249}
250
251std::string CgroupController::GetProcsFilePath(const std::string& path, uid_t uid,
252 pid_t pid) const {
253 std::string proc_path(path_);
254 proc_path.append("/").append(path);
255 proc_path = regex_replace(proc_path, std::regex("<uid>"), std::to_string(uid));
256 proc_path = regex_replace(proc_path, std::regex("<pid>"), std::to_string(pid));
257
258 return proc_path.append(CGROUP_PROCS_FILE);
259}
260
261bool CgroupController::GetTaskGroup(int tid, std::string* group) const {
262 std::string file_name = StringPrintf("/proc/%d/cgroup", tid);
263 std::string content;
264 if (!android::base::ReadFileToString(file_name, &content)) {
265 LOG(ERROR) << "Failed to read " << file_name;
266 return false;
267 }
268
269 // if group is null and tid exists return early because
270 // user is not interested in cgroup membership
271 if (group == nullptr) {
272 return true;
273 }
274
275 std::string cg_tag = StringPrintf(":%s:", name_);
276 size_t start_pos = content.find(cg_tag);
277 if (start_pos == std::string::npos) {
278 return false;
279 }
280
281 start_pos += cg_tag.length() + 1; // skip '/'
282 size_t end_pos = content.find('\n', start_pos);
283 if (end_pos == std::string::npos) {
284 *group = content.substr(start_pos, std::string::npos);
285 } else {
286 *group = content.substr(start_pos, end_pos - start_pos);
287 }
288
289 return true;
290}
291
292CgroupDescriptor::CgroupDescriptor(uint32_t version, const std::string& name,
293 const std::string& path, mode_t mode, const std::string& uid,
294 const std::string& gid)
295 : controller_(version, name, path), mode_(mode), uid_(uid), gid_(gid) {}
296
297CgroupMap::CgroupMap() : cg_file_data_(nullptr), cg_file_size_(0) {
298 if (!LoadRcFile()) {
299 PLOG(ERROR) << "CgroupMap::LoadRcFile called for [" << getpid() << "] failed";
300 }
301}
302
303CgroupMap::~CgroupMap() {
304 if (cg_file_data_) {
305 munmap(cg_file_data_, cg_file_size_);
306 cg_file_data_ = nullptr;
307 cg_file_size_ = 0;
308 }
309}
310
311CgroupMap& CgroupMap::GetInstance() {
312 static CgroupMap instance;
313 return instance;
314}
315
316bool CgroupMap::LoadRcFile() {
317 struct stat sb;
318
319 if (cg_file_data_) {
320 // Data already initialized
321 return true;
322 }
323
324 std::string cgroup_rc_path = StringPrintf("%s/%s", CGROUPS_RC_DIR, CGROUPS_RC_FILE);
325 unique_fd fd(TEMP_FAILURE_RETRY(open(cgroup_rc_path.c_str(), O_RDONLY | O_CLOEXEC)));
326 if (fd < 0) {
327 PLOG(ERROR) << "open() failed for " << cgroup_rc_path;
328 return false;
329 }
330
331 if (fstat(fd, &sb) < 0) {
332 PLOG(ERROR) << "fstat() failed for " << cgroup_rc_path;
333 return false;
334 }
335
336 cg_file_size_ = sb.st_size;
337 if (cg_file_size_ < sizeof(CgroupFile)) {
338 PLOG(ERROR) << "Invalid file format " << cgroup_rc_path;
339 return false;
340 }
341
342 cg_file_data_ = (CgroupFile*)mmap(nullptr, cg_file_size_, PROT_READ, MAP_SHARED, fd, 0);
343 if (cg_file_data_ == MAP_FAILED) {
344 PLOG(ERROR) << "Failed to mmap " << cgroup_rc_path;
345 return false;
346 }
347
348 if (cg_file_data_->version_ != CgroupFile::FILE_CURR_VERSION) {
349 PLOG(ERROR) << cgroup_rc_path << " file version mismatch";
350 return false;
351 }
352
353 return true;
354}
355
356void CgroupMap::Print() {
357 LOG(INFO) << "File version = " << cg_file_data_->version_;
358 LOG(INFO) << "File controller count = " << cg_file_data_->controller_count_;
359
360 LOG(INFO) << "Mounted cgroups:";
361 CgroupController* controller = (CgroupController*)(cg_file_data_ + 1);
362 for (int i = 0; i < cg_file_data_->controller_count_; i++, controller++) {
363 LOG(INFO) << "\t" << controller->name() << " ver " << controller->version() << " path "
364 << controller->path();
365 }
366}
367
368bool CgroupMap::SetupCgroups() {
369 std::map<std::string, CgroupDescriptor> descriptors;
370
371 // load cgroups.json file
372 if (!ReadDescriptors(&descriptors)) {
373 PLOG(ERROR) << "Failed to load cgroup description file";
374 return false;
375 }
376
377 // setup cgroups
378 for (const auto& [name, descriptor] : descriptors) {
379 if (!SetupCgroup(descriptor)) {
380 // issue a warning and proceed with the next cgroup
381 // TODO: mark the descriptor as invalid and skip it in WriteRcFile()
382 LOG(WARNING) << "Failed to setup " << name << " cgroup";
383 }
384 }
385
386 // mkdir <CGROUPS_RC_DIR> 0711 system system
387 if (!Mkdir(CGROUPS_RC_DIR, 0711, "system", "system")) {
388 PLOG(ERROR) << "Failed to create directory for <CGROUPS_RC_FILE> file";
389 return false;
390 }
391
392 // Generate <CGROUPS_RC_FILE> file which can be directly mmapped into
393 // process memory. This optimizes performance, memory usage
394 // and limits infrormation shared with unprivileged processes
395 // to the minimum subset of information from cgroups.json
396 if (!WriteRcFile(descriptors)) {
397 LOG(ERROR) << "Failed to write " << CGROUPS_RC_FILE << " file";
398 return false;
399 }
400
401 std::string cgroup_rc_path = StringPrintf("%s/%s", CGROUPS_RC_DIR, CGROUPS_RC_FILE);
402 // chmod 0644 <cgroup_rc_path>
403 if (fchmodat(AT_FDCWD, cgroup_rc_path.c_str(), 0644, AT_SYMLINK_NOFOLLOW) < 0) {
404 LOG(ERROR) << "fchmodat() failed";
405 return false;
406 }
407
408 return true;
409}
410
411const CgroupController* CgroupMap::FindController(const std::string& name) const {
412 if (!cg_file_data_) {
413 return nullptr;
414 }
415
416 // skip the file header to get to the first controller
417 CgroupController* controller = (CgroupController*)(cg_file_data_ + 1);
418 for (int i = 0; i < cg_file_data_->controller_count_; i++, controller++) {
419 if (name == controller->name()) {
420 return controller;
421 }
422 }
423
424 return nullptr;
425}