1 /*
2 * Copyright 2014 Google, Inc
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 <assert.h>
21 #include <dirent.h>
22 #include <errno.h>
23 #include <fcntl.h>
24 #include <inttypes.h>
25 #include <signal.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <sys/stat.h>
29 #include <sys/types.h>
30 #include <unistd.h>
31
32 #include <chrono>
33 #include <map>
34 #include <memory>
35 #include <mutex>
36 #include <set>
37 #include <string>
38 #include <thread>
39
40 #include <android-base/file.h>
41 #include <android-base/logging.h>
42 #include <android-base/properties.h>
43 #include <android-base/stringprintf.h>
44 #include <android-base/strings.h>
45 #include <cutils/android_filesystem_config.h>
46 #include <processgroup/processgroup.h>
47 #include <task_profiles.h>
48
49 using android::base::GetBoolProperty;
50 using android::base::StartsWith;
51 using android::base::StringPrintf;
52 using android::base::WriteStringToFile;
53
54 using namespace std::chrono_literals;
55
56 #define PROCESSGROUP_CGROUP_PROCS_FILE "/cgroup.procs"
57
CgroupGetControllerPath(const std::string & cgroup_name,std::string * path)58 bool CgroupGetControllerPath(const std::string& cgroup_name, std::string* path) {
59 auto controller = CgroupMap::GetInstance().FindController(cgroup_name);
60
61 if (!controller.HasValue()) {
62 return false;
63 }
64
65 if (path) {
66 *path = controller.path();
67 }
68
69 return true;
70 }
71
CgroupGetMemcgAppsPath(std::string * path)72 static bool CgroupGetMemcgAppsPath(std::string* path) {
73 CgroupController controller = CgroupMap::GetInstance().FindController("memory");
74
75 if (!controller.HasValue()) {
76 return false;
77 }
78
79 if (path) {
80 *path = controller.path();
81 if (controller.version() == 1) {
82 *path += "/apps";
83 }
84 }
85
86 return true;
87 }
88
CgroupGetControllerFromPath(const std::string & path,std::string * cgroup_name)89 bool CgroupGetControllerFromPath(const std::string& path, std::string* cgroup_name) {
90 auto controller = CgroupMap::GetInstance().FindControllerByPath(path);
91
92 if (!controller.HasValue()) {
93 return false;
94 }
95
96 if (cgroup_name) {
97 *cgroup_name = controller.name();
98 }
99
100 return true;
101 }
102
CgroupGetAttributePath(const std::string & attr_name,std::string * path)103 bool CgroupGetAttributePath(const std::string& attr_name, std::string* path) {
104 const TaskProfiles& tp = TaskProfiles::GetInstance();
105 const IProfileAttribute* attr = tp.GetAttribute(attr_name);
106
107 if (attr == nullptr) {
108 return false;
109 }
110
111 if (path) {
112 *path = StringPrintf("%s/%s", attr->controller()->path(), attr->file_name().c_str());
113 }
114
115 return true;
116 }
117
CgroupGetAttributePathForTask(const std::string & attr_name,int tid,std::string * path)118 bool CgroupGetAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
119 const TaskProfiles& tp = TaskProfiles::GetInstance();
120 const IProfileAttribute* attr = tp.GetAttribute(attr_name);
121
122 if (attr == nullptr) {
123 return false;
124 }
125
126 if (!attr->GetPathForTask(tid, path)) {
127 PLOG(ERROR) << "Failed to find cgroup for tid " << tid;
128 return false;
129 }
130
131 return true;
132 }
133
UsePerAppMemcg()134 bool UsePerAppMemcg() {
135 bool low_ram_device = GetBoolProperty("ro.config.low_ram", false);
136 return GetBoolProperty("ro.config.per_app_memcg", low_ram_device);
137 }
138
isMemoryCgroupSupported()139 static bool isMemoryCgroupSupported() {
140 static bool memcg_supported = CgroupMap::GetInstance().FindController("memory").IsUsable();
141
142 return memcg_supported;
143 }
144
DropTaskProfilesResourceCaching()145 void DropTaskProfilesResourceCaching() {
146 TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_TASK);
147 TaskProfiles::GetInstance().DropResourceCaching(ProfileAction::RCT_PROCESS);
148 }
149
SetProcessProfiles(uid_t uid,pid_t pid,const std::vector<std::string> & profiles)150 bool SetProcessProfiles(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
151 return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles, false);
152 }
153
SetProcessProfilesCached(uid_t uid,pid_t pid,const std::vector<std::string> & profiles)154 bool SetProcessProfilesCached(uid_t uid, pid_t pid, const std::vector<std::string>& profiles) {
155 return TaskProfiles::GetInstance().SetProcessProfiles(uid, pid, profiles, true);
156 }
157
SetTaskProfiles(int tid,const std::vector<std::string> & profiles,bool use_fd_cache)158 bool SetTaskProfiles(int tid, const std::vector<std::string>& profiles, bool use_fd_cache) {
159 return TaskProfiles::GetInstance().SetTaskProfiles(tid, profiles, use_fd_cache);
160 }
161
162 // C wrapper for SetProcessProfiles.
163 // No need to have this in the header file because this function is specifically for crosvm. Crosvm
164 // which is written in Rust has its own declaration of this foreign function and doesn't rely on the
165 // header. See
166 // https://chromium-review.googlesource.com/c/chromiumos/platform/crosvm/+/3574427/5/src/linux/android.rs#12
android_set_process_profiles(uid_t uid,pid_t pid,size_t num_profiles,const char * profiles[])167 extern "C" bool android_set_process_profiles(uid_t uid, pid_t pid, size_t num_profiles,
168 const char* profiles[]) {
169 std::vector<std::string> profiles_;
170 profiles_.reserve(num_profiles);
171 for (size_t i = 0; i < num_profiles; i++) {
172 profiles_.emplace_back(profiles[i]);
173 }
174 return SetProcessProfiles(uid, pid, profiles_);
175 }
176
ConvertUidToPath(const char * cgroup,uid_t uid)177 static std::string ConvertUidToPath(const char* cgroup, uid_t uid) {
178 return StringPrintf("%s/uid_%d", cgroup, uid);
179 }
180
ConvertUidPidToPath(const char * cgroup,uid_t uid,int pid)181 static std::string ConvertUidPidToPath(const char* cgroup, uid_t uid, int pid) {
182 return StringPrintf("%s/uid_%d/pid_%d", cgroup, uid, pid);
183 }
184
RemoveProcessGroup(const char * cgroup,uid_t uid,int pid,unsigned int retries)185 static int RemoveProcessGroup(const char* cgroup, uid_t uid, int pid, unsigned int retries) {
186 int ret = 0;
187 auto uid_pid_path = ConvertUidPidToPath(cgroup, uid, pid);
188 auto uid_path = ConvertUidToPath(cgroup, uid);
189
190 if (retries == 0) {
191 retries = 1;
192 }
193
194 while (retries--) {
195 ret = rmdir(uid_pid_path.c_str());
196 if (!ret || errno != EBUSY) break;
197 std::this_thread::sleep_for(5ms);
198 }
199
200 return ret;
201 }
202
RemoveUidProcessGroups(const std::string & uid_path,bool empty_only)203 static bool RemoveUidProcessGroups(const std::string& uid_path, bool empty_only) {
204 std::unique_ptr<DIR, decltype(&closedir)> uid(opendir(uid_path.c_str()), closedir);
205 bool empty = true;
206 if (uid != NULL) {
207 dirent* dir;
208 while ((dir = readdir(uid.get())) != nullptr) {
209 if (dir->d_type != DT_DIR) {
210 continue;
211 }
212
213 if (!StartsWith(dir->d_name, "pid_")) {
214 continue;
215 }
216
217 auto path = StringPrintf("%s/%s", uid_path.c_str(), dir->d_name);
218 if (empty_only) {
219 struct stat st;
220 auto procs_file = StringPrintf("%s/%s", path.c_str(),
221 PROCESSGROUP_CGROUP_PROCS_FILE);
222 if (stat(procs_file.c_str(), &st) == -1) {
223 PLOG(ERROR) << "Failed to get stats for " << procs_file;
224 continue;
225 }
226 if (st.st_size > 0) {
227 // skip non-empty groups
228 LOG(VERBOSE) << "Skipping non-empty group " << path;
229 empty = false;
230 continue;
231 }
232 }
233 LOG(VERBOSE) << "Removing " << path;
234 if (rmdir(path.c_str()) == -1) {
235 if (errno != EBUSY) {
236 PLOG(WARNING) << "Failed to remove " << path;
237 }
238 empty = false;
239 }
240 }
241 }
242 return empty;
243 }
244
removeAllProcessGroupsInternal(bool empty_only)245 void removeAllProcessGroupsInternal(bool empty_only) {
246 std::vector<std::string> cgroups;
247 std::string path, memcg_apps_path;
248
249 if (CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &path)) {
250 cgroups.push_back(path);
251 }
252 if (CgroupGetMemcgAppsPath(&memcg_apps_path) && memcg_apps_path != path) {
253 cgroups.push_back(memcg_apps_path);
254 }
255
256 for (std::string cgroup_root_path : cgroups) {
257 std::unique_ptr<DIR, decltype(&closedir)> root(opendir(cgroup_root_path.c_str()), closedir);
258 if (root == NULL) {
259 PLOG(ERROR) << __func__ << " failed to open " << cgroup_root_path;
260 } else {
261 dirent* dir;
262 while ((dir = readdir(root.get())) != nullptr) {
263 if (dir->d_type != DT_DIR) {
264 continue;
265 }
266
267 if (!StartsWith(dir->d_name, "uid_")) {
268 continue;
269 }
270
271 auto path = StringPrintf("%s/%s", cgroup_root_path.c_str(), dir->d_name);
272 if (!RemoveUidProcessGroups(path, empty_only)) {
273 LOG(VERBOSE) << "Skip removing " << path;
274 continue;
275 }
276 LOG(VERBOSE) << "Removing " << path;
277 if (rmdir(path.c_str()) == -1 && errno != EBUSY) {
278 PLOG(WARNING) << "Failed to remove " << path;
279 }
280 }
281 }
282 }
283 }
284
removeAllProcessGroups()285 void removeAllProcessGroups() {
286 LOG(VERBOSE) << "removeAllProcessGroups()";
287 removeAllProcessGroupsInternal(false);
288 }
289
removeAllEmptyProcessGroups()290 void removeAllEmptyProcessGroups() {
291 LOG(VERBOSE) << "removeAllEmptyProcessGroups()";
292 removeAllProcessGroupsInternal(true);
293 }
294
295 /**
296 * Process groups are primarily created by the Zygote, meaning that uid/pid groups are created by
297 * the user root. Ownership for the newly created cgroup and all of its files must thus be
298 * transferred for the user/group passed as uid/gid before system_server can properly access them.
299 */
MkdirAndChown(const std::string & path,mode_t mode,uid_t uid,gid_t gid)300 static bool MkdirAndChown(const std::string& path, mode_t mode, uid_t uid, gid_t gid) {
301 if (mkdir(path.c_str(), mode) == -1) {
302 if (errno == EEXIST) {
303 // Directory already exists and permissions have been set at the time it was created
304 return true;
305 }
306 return false;
307 }
308
309 auto dir = std::unique_ptr<DIR, decltype(&closedir)>(opendir(path.c_str()), closedir);
310
311 if (dir == NULL) {
312 PLOG(ERROR) << "opendir failed for " << path;
313 goto err;
314 }
315
316 struct dirent* dir_entry;
317 while ((dir_entry = readdir(dir.get()))) {
318 if (!strcmp("..", dir_entry->d_name)) {
319 continue;
320 }
321
322 std::string file_path = path + "/" + dir_entry->d_name;
323
324 if (lchown(file_path.c_str(), uid, gid) < 0) {
325 PLOG(ERROR) << "lchown failed for " << file_path;
326 goto err;
327 }
328
329 if (fchmodat(AT_FDCWD, file_path.c_str(), mode, AT_SYMLINK_NOFOLLOW) != 0) {
330 PLOG(ERROR) << "fchmodat failed for " << file_path;
331 goto err;
332 }
333 }
334
335 return true;
336 err:
337 int saved_errno = errno;
338 rmdir(path.c_str());
339 errno = saved_errno;
340
341 return false;
342 }
343
344 // Returns number of processes killed on success
345 // Returns 0 if there are no processes in the process cgroup left to kill
346 // Returns -1 on error
DoKillProcessGroupOnce(const char * cgroup,uid_t uid,int initialPid,int signal)347 static int DoKillProcessGroupOnce(const char* cgroup, uid_t uid, int initialPid, int signal) {
348 auto path = ConvertUidPidToPath(cgroup, uid, initialPid) + PROCESSGROUP_CGROUP_PROCS_FILE;
349 std::unique_ptr<FILE, decltype(&fclose)> fd(fopen(path.c_str(), "re"), fclose);
350 if (!fd) {
351 if (errno == ENOENT) {
352 // This happens when process is already dead
353 return 0;
354 }
355 PLOG(WARNING) << __func__ << " failed to open process cgroup uid " << uid << " pid "
356 << initialPid;
357 return -1;
358 }
359
360 // We separate all of the pids in the cgroup into those pids that are also the leaders of
361 // process groups (stored in the pgids set) and those that are not (stored in the pids set).
362 std::set<pid_t> pgids;
363 pgids.emplace(initialPid);
364 std::set<pid_t> pids;
365
366 pid_t pid;
367 int processes = 0;
368 while (fscanf(fd.get(), "%d\n", &pid) == 1 && pid >= 0) {
369 processes++;
370 if (pid == 0) {
371 // Should never happen... but if it does, trying to kill this
372 // will boomerang right back and kill us! Let's not let that happen.
373 LOG(WARNING) << "Yikes, we've been told to kill pid 0! How about we don't do that?";
374 continue;
375 }
376 pid_t pgid = getpgid(pid);
377 if (pgid == -1) PLOG(ERROR) << "getpgid(" << pid << ") failed";
378 if (pgid == pid) {
379 pgids.emplace(pid);
380 } else {
381 pids.emplace(pid);
382 }
383 }
384
385 // Erase all pids that will be killed when we kill the process groups.
386 for (auto it = pids.begin(); it != pids.end();) {
387 pid_t pgid = getpgid(*it);
388 if (pgids.count(pgid) == 1) {
389 it = pids.erase(it);
390 } else {
391 ++it;
392 }
393 }
394
395 // Kill all process groups.
396 for (const auto pgid : pgids) {
397 LOG(VERBOSE) << "Killing process group " << -pgid << " in uid " << uid
398 << " as part of process cgroup " << initialPid;
399
400 if (kill(-pgid, signal) == -1 && errno != ESRCH) {
401 PLOG(WARNING) << "kill(" << -pgid << ", " << signal << ") failed";
402 }
403 }
404
405 // Kill remaining pids.
406 for (const auto pid : pids) {
407 LOG(VERBOSE) << "Killing pid " << pid << " in uid " << uid << " as part of process cgroup "
408 << initialPid;
409
410 if (kill(pid, signal) == -1 && errno != ESRCH) {
411 PLOG(WARNING) << "kill(" << pid << ", " << signal << ") failed";
412 }
413 }
414
415 return feof(fd.get()) ? processes : -1;
416 }
417
KillProcessGroup(uid_t uid,int initialPid,int signal,int retries,int * max_processes)418 static int KillProcessGroup(uid_t uid, int initialPid, int signal, int retries,
419 int* max_processes) {
420 std::string hierarchy_root_path;
421 CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &hierarchy_root_path);
422 const char* cgroup = hierarchy_root_path.c_str();
423
424 std::chrono::steady_clock::time_point start = std::chrono::steady_clock::now();
425
426 if (max_processes != nullptr) {
427 *max_processes = 0;
428 }
429
430 int retry = retries;
431 int processes;
432 while ((processes = DoKillProcessGroupOnce(cgroup, uid, initialPid, signal)) > 0) {
433 if (max_processes != nullptr && processes > *max_processes) {
434 *max_processes = processes;
435 }
436 LOG(VERBOSE) << "Killed " << processes << " processes for processgroup " << initialPid;
437 if (retry > 0) {
438 std::this_thread::sleep_for(5ms);
439 --retry;
440 } else {
441 break;
442 }
443 }
444
445 if (processes < 0) {
446 PLOG(ERROR) << "Error encountered killing process cgroup uid " << uid << " pid "
447 << initialPid;
448 return -1;
449 }
450
451 std::chrono::steady_clock::time_point end = std::chrono::steady_clock::now();
452 auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(end - start).count();
453
454 // We only calculate the number of 'processes' when killing the processes.
455 // In the retries == 0 case, we only kill the processes once and therefore
456 // will not have waited then recalculated how many processes are remaining
457 // after the first signals have been sent.
458 // Logging anything regarding the number of 'processes' here does not make sense.
459
460 if (processes == 0) {
461 if (retries > 0) {
462 LOG(INFO) << "Successfully killed process cgroup uid " << uid << " pid " << initialPid
463 << " in " << static_cast<int>(ms) << "ms";
464 }
465
466 int err = RemoveProcessGroup(cgroup, uid, initialPid, retries);
467
468 if (isMemoryCgroupSupported() && UsePerAppMemcg()) {
469 std::string memcg_apps_path;
470 if (CgroupGetMemcgAppsPath(&memcg_apps_path) &&
471 RemoveProcessGroup(memcg_apps_path.c_str(), uid, initialPid, retries) < 0) {
472 return -1;
473 }
474 }
475
476 return err;
477 } else {
478 if (retries > 0) {
479 LOG(ERROR) << "Failed to kill process cgroup uid " << uid << " pid " << initialPid
480 << " in " << static_cast<int>(ms) << "ms, " << processes
481 << " processes remain";
482 }
483 return -1;
484 }
485 }
486
killProcessGroup(uid_t uid,int initialPid,int signal,int * max_processes)487 int killProcessGroup(uid_t uid, int initialPid, int signal, int* max_processes) {
488 return KillProcessGroup(uid, initialPid, signal, 40 /*retries*/, max_processes);
489 }
490
killProcessGroupOnce(uid_t uid,int initialPid,int signal,int * max_processes)491 int killProcessGroupOnce(uid_t uid, int initialPid, int signal, int* max_processes) {
492 return KillProcessGroup(uid, initialPid, signal, 0 /*retries*/, max_processes);
493 }
494
createProcessGroupInternal(uid_t uid,int initialPid,std::string cgroup,bool activate_controllers)495 static int createProcessGroupInternal(uid_t uid, int initialPid, std::string cgroup,
496 bool activate_controllers) {
497 auto uid_path = ConvertUidToPath(cgroup.c_str(), uid);
498
499 struct stat cgroup_stat;
500 mode_t cgroup_mode = 0750;
501 uid_t cgroup_uid = AID_SYSTEM;
502 gid_t cgroup_gid = AID_SYSTEM;
503 int ret = 0;
504
505 if (stat(cgroup.c_str(), &cgroup_stat) < 0) {
506 PLOG(ERROR) << "Failed to get stats for " << cgroup;
507 } else {
508 cgroup_mode = cgroup_stat.st_mode;
509 cgroup_uid = cgroup_stat.st_uid;
510 cgroup_gid = cgroup_stat.st_gid;
511 }
512
513 if (!MkdirAndChown(uid_path, cgroup_mode, cgroup_uid, cgroup_gid)) {
514 PLOG(ERROR) << "Failed to make and chown " << uid_path;
515 return -errno;
516 }
517 if (activate_controllers) {
518 ret = CgroupMap::GetInstance().ActivateControllers(uid_path);
519 if (ret) {
520 LOG(ERROR) << "Failed to activate controllers in " << uid_path;
521 return ret;
522 }
523 }
524
525 auto uid_pid_path = ConvertUidPidToPath(cgroup.c_str(), uid, initialPid);
526
527 if (!MkdirAndChown(uid_pid_path, cgroup_mode, cgroup_uid, cgroup_gid)) {
528 PLOG(ERROR) << "Failed to make and chown " << uid_pid_path;
529 return -errno;
530 }
531
532 auto uid_pid_procs_file = uid_pid_path + PROCESSGROUP_CGROUP_PROCS_FILE;
533
534 if (!WriteStringToFile(std::to_string(initialPid), uid_pid_procs_file)) {
535 ret = -errno;
536 PLOG(ERROR) << "Failed to write '" << initialPid << "' to " << uid_pid_procs_file;
537 }
538
539 return ret;
540 }
541
createProcessGroup(uid_t uid,int initialPid,bool memControl)542 int createProcessGroup(uid_t uid, int initialPid, bool memControl) {
543 std::string cgroup;
544
545 if (memControl && !UsePerAppMemcg()) {
546 PLOG(ERROR) << "service memory controls are used without per-process memory cgroup support";
547 return -EINVAL;
548 }
549
550 if (std::string memcg_apps_path;
551 isMemoryCgroupSupported() && UsePerAppMemcg() && CgroupGetMemcgAppsPath(&memcg_apps_path)) {
552 // Note by bvanassche: passing 'false' as fourth argument below implies that the v1
553 // hierarchy is used. It is not clear to me whether the above conditions guarantee that the
554 // v1 hierarchy is used.
555 int ret = createProcessGroupInternal(uid, initialPid, memcg_apps_path, false);
556 if (ret != 0) {
557 return ret;
558 }
559 }
560
561 CgroupGetControllerPath(CGROUPV2_CONTROLLER_NAME, &cgroup);
562 return createProcessGroupInternal(uid, initialPid, cgroup, true);
563 }
564
SetProcessGroupValue(int tid,const std::string & attr_name,int64_t value)565 static bool SetProcessGroupValue(int tid, const std::string& attr_name, int64_t value) {
566 if (!isMemoryCgroupSupported()) {
567 PLOG(ERROR) << "Memcg is not mounted.";
568 return false;
569 }
570
571 std::string path;
572 if (!CgroupGetAttributePathForTask(attr_name, tid, &path)) {
573 PLOG(ERROR) << "Failed to find attribute '" << attr_name << "'";
574 return false;
575 }
576
577 if (!WriteStringToFile(std::to_string(value), path)) {
578 PLOG(ERROR) << "Failed to write '" << value << "' to " << path;
579 return false;
580 }
581 return true;
582 }
583
setProcessGroupSwappiness(uid_t,int pid,int swappiness)584 bool setProcessGroupSwappiness(uid_t, int pid, int swappiness) {
585 return SetProcessGroupValue(pid, "MemSwappiness", swappiness);
586 }
587
setProcessGroupSoftLimit(uid_t,int pid,int64_t soft_limit_in_bytes)588 bool setProcessGroupSoftLimit(uid_t, int pid, int64_t soft_limit_in_bytes) {
589 return SetProcessGroupValue(pid, "MemSoftLimit", soft_limit_in_bytes);
590 }
591
setProcessGroupLimit(uid_t,int pid,int64_t limit_in_bytes)592 bool setProcessGroupLimit(uid_t, int pid, int64_t limit_in_bytes) {
593 return SetProcessGroupValue(pid, "MemLimit", limit_in_bytes);
594 }
595
getAttributePathForTask(const std::string & attr_name,int tid,std::string * path)596 bool getAttributePathForTask(const std::string& attr_name, int tid, std::string* path) {
597 return CgroupGetAttributePathForTask(attr_name, tid, path);
598 }
599