From b693d13b931de4aeab2e0538c7d8210599b0ee03 Mon Sep 17 00:00:00 2001 From: vvb2060 Date: Sat, 27 Feb 2021 23:40:55 +0800 Subject: [PATCH] Proper implementation of cgroup migration https://www.kernel.org/doc/Documentation/admin-guide/cgroup-v1/cgroups.rst https://www.kernel.org/doc/Documentation/admin-guide/cgroup-v2.rst --- native/jni/core/daemon.cpp | 41 ++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 17 deletions(-) diff --git a/native/jni/core/daemon.cpp b/native/jni/core/daemon.cpp index 711f72426..afac413dd 100644 --- a/native/jni/core/daemon.cpp +++ b/native/jni/core/daemon.cpp @@ -148,7 +148,22 @@ shortcut: static void magisk_logging(); -static void daemon_entry(int ppid) { +static int switch_cgroup(const char *cgroup, int pid) { + char buf[32]; + snprintf(buf, sizeof(buf), "%s/cgroup.procs", cgroup); + int fd = open(buf, O_WRONLY | O_APPEND | O_CLOEXEC); + if (fd == -1) + return -1; + snprintf(buf, sizeof(buf), "%d\n", pid); + if (xwrite(fd, buf, strlen(buf)) == -1) { + close(fd); + return -1; + } + close(fd); + return 0; +} + +[[noreturn]] static void daemon_entry() { magisk_logging(); int fd = xopen("/dev/null", O_WRONLY); @@ -166,22 +181,15 @@ static void daemon_entry(int ppid) { LOGI(NAME_WITH_VER(Magisk) " daemon started\n"); - // Make sure ppid is not in acct - char src[64], dest[64]; - sprintf(src, "/acct/uid_0/pid_%d", ppid); - if (access(src, F_OK) == 0) { - sprintf(dest, "/acct/uid_0/pid_%d", getpid()); - rename(src, dest); - } - sprintf(src, "/sys/fs/cgroup/uid_0/pid_%d", ppid); - if (access(src, F_OK) == 0) { - sprintf(dest, "/sys/fs/cgroup/uid_0/pid_%d", getpid()); - rename(src, dest); - } + // Escape from cgroup + int pid = getpid(); + if (switch_cgroup("/acct", pid) && switch_cgroup("/sys/fs/cgroup", pid)) + LOGW("Can't switch cgroup\n"); // Get self stat - xreadlink("/proc/self/exe", src, sizeof(src)); - MAGISKTMP = dirname(src); + char buf[64]; + xreadlink("/proc/self/exe", buf, sizeof(buf)); + MAGISKTMP = dirname(buf); xstat("/proc/self/exe", &self_st); // Get API level @@ -253,11 +261,10 @@ int connect_daemon(bool create) { exit(1); } - int ppid = getpid(); LOGD("client: launching new main daemon process\n"); if (fork_dont_care() == 0) { close(fd); - daemon_entry(ppid); + daemon_entry(); } while (connect(fd, (struct sockaddr*) &sun, len))