Skip to content

Commit c5d2308

Browse files
committed
- Attempt to fix abnormal cgroups
1 parent cc662fb commit c5d2308

2 files changed

Lines changed: 74 additions & 29 deletions

File tree

module/src/main/cpp/nohello.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -287,8 +287,8 @@ static void remount(const std::vector<MountInfo>& mounts) {
287287
int (*ar_unshare)(int) = nullptr;
288288

289289
static int reshare(int flags) {
290-
errno = 0;
291-
return flags == CLONE_NEWNS ? 0 : ar_unshare(flags & ~CLONE_NEWNS);
290+
errno = 0;
291+
return ar_unshare(flags & ~(CLONE_NEWNS | CLONE_NEWCGROUP));
292292
}
293293

294294
class NoHello : public zygisk::ModuleBase {
@@ -419,7 +419,7 @@ class NoHello : public zygisk::ModuleBase {
419419
}
420420
}
421421

422-
int res = unshare(CLONE_NEWNS);
422+
int res = unshare(CLONE_NEWNS | CLONE_NEWCGROUP);
423423
if (res != 0) {
424424
LOGE("#[zygisk::preSpecialize] unshare: %s", strerror(errno));
425425
// There's nothing we can do except returning
@@ -618,7 +618,7 @@ static void NoRoot(int fd) {
618618
result = forkcall(
619619
[pid]()
620620
{
621-
int res = switchnsto(pid);
621+
int res = nscg2(pid);
622622
if (!res) { // switchnsto returns true on success (0 from setns)
623623
LOGE("#[ps::Companion] Switch namespaces failed for PID %d: %s", pid, strerror(errno));
624624
return FAILURE;

module/src/main/cpp/utils.cpp

Lines changed: 70 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -158,33 +158,78 @@ static ssize_t process_vm_writev(pid_t pid,
158158
return syscall(SYS_process_vm_writev, pid, local_iov, liovcnt, remote_iov, riovcnt, flags);
159159
}
160160

161-
bool switchnsto(pid_t pid) {
162-
int fd = pidfd_open(pid, 0);
163-
if (fd != -1) {
164-
int res = setns(fd, CLONE_NEWNS);
165-
close(fd);
166-
if (!res)
167-
return true;
168-
else {
169-
LOGE("setns(procfd_open(%d, 0) -> %d, CLONE_NEWNS): %s", pid, fd, strerror(errno));
170-
goto fallback;
171-
}
172-
} else {
173-
LOGE("pidfd_open: %s", strerror(errno));
174-
}
175-
fallback:
176-
std::string path = "/proc/" + std::to_string(pid) + "/ns/mnt";
177-
fd = open(path.c_str(), O_RDONLY);
178-
if (fd != -1) {
179-
int res = setns(fd, CLONE_NEWNS);
180-
close(fd);
181-
return res == 0;
182-
} else {
183-
LOGE("open: %s", strerror(errno));
184-
}
185-
return false;
161+
bool nscg2(pid_t pid) {
162+
int pidfd = pidfd_open(pid, 0);
163+
if (pidfd != -1) {
164+
int res = -1;
165+
int mntfd = openat(pidfd, "ns/mnt", O_RDONLY);
166+
if (mntfd == -1) {
167+
LOGE("openat(pidfd_open(%d, 0) -> %d, \"ns/mnt\", O_RDONLY): %s", pid, pidfd, strerror(errno));
168+
goto fallback_mnt;
169+
}
170+
res = setns(mntfd, CLONE_NEWNS);
171+
if (res) {
172+
LOGE("setns(openat(pidfd_open(%d, 0), \"ns/mnt\") -> %d, CLONE_NEWNS): %s", pid, mntfd, strerror(errno));
173+
close(mntfd);
174+
goto fallback_mnt;
175+
}
176+
close(mntfd);
177+
178+
int cgfd = openat(pidfd, "ns/cgroup", O_RDONLY);
179+
if (cgfd == -1) {
180+
LOGE("openat(pidfd_open(%d, 0) -> %d, \"ns/cgroup\", O_RDONLY): %s", pid, pidfd, strerror(errno));
181+
goto fallback_cg;
182+
}
183+
res = setns(cgfd, CLONE_NEWCGROUP);
184+
if (res) {
185+
LOGE("setns(openat(pidfd_open(%d, 0), \"ns/cgroup\") -> %d, CLONE_NEWCGROUP): %s", pid, cgfd, strerror(errno));
186+
close(cgfd);
187+
goto fallback_cg;
188+
}
189+
close(cgfd);
190+
close(pidfd);
191+
return true;
192+
} else {
193+
LOGE("pidfd_open(%d): %s", pid, strerror(errno));
194+
}
195+
fallback_mnt:
196+
{
197+
std::string mntPath = "/proc/" + std::to_string(pid) + "/ns/mnt";
198+
int mntfd_fallback = open(mntPath.c_str(), O_RDONLY);
199+
if (mntfd_fallback != -1) {
200+
int res = setns(mntfd_fallback, CLONE_NEWNS);
201+
if (res) {
202+
LOGE("setns(open(\"%s\") -> %d, CLONE_NEWNS): %s", mntPath.c_str(), mntfd_fallback, strerror(errno));
203+
close(mntfd_fallback);
204+
return false;
205+
}
206+
close(mntfd_fallback);
207+
} else {
208+
LOGE("open(\"%s\"): %s", mntPath.c_str(), strerror(errno));
209+
return false;
210+
}
211+
}
212+
fallback_cg:
213+
{
214+
std::string cgPath = "/proc/" + std::to_string(pid) + "/ns/cgroup";
215+
int cgfd_fallback = open(cgPath.c_str(), O_RDONLY);
216+
if (cgfd_fallback != -1) {
217+
int res = setns(cgfd_fallback, CLONE_NEWCGROUP);
218+
if (res) {
219+
LOGE("setns(open(\"%s\") -> %d, CLONE_NEWCGROUP): %s", cgPath.c_str(), cgfd_fallback, strerror(errno));
220+
close(cgfd_fallback);
221+
return false;
222+
}
223+
close(cgfd_fallback);
224+
} else {
225+
LOGE("open(\"%s\"): %s", cgPath.c_str(), strerror(errno));
226+
return false;
227+
}
228+
}
229+
return true;
186230
}
187231

232+
188233
bool isuserapp(int uid) {
189234
int appid = uid % AID_USER_OFFSET;
190235
if (appid >= AID_APP_START && appid <= AID_APP_END)

0 commit comments

Comments
 (0)