Changes to companion fork

This commit is contained in:
snake-4
2024-05-07 05:42:57 +02:00
parent 6530a5a011
commit 9c06b69c42
3 changed files with 25 additions and 33 deletions

View File

@@ -35,5 +35,5 @@ namespace Utils
bool switchMountNS(int pid); bool switchMountNS(int pid);
int isUserAppUID(int uid); int isUserAppUID(int uid);
bool hookPLTByName(zygisk::Api *api, const std::string &libName, const std::string &symbolName, void *hookFunc, void **origFunc); bool hookPLTByName(zygisk::Api *api, const std::string &libName, const std::string &symbolName, void *hookFunc, void **origFunc);
int executeLambdaInFork(const std::function<void()> &lambda); int forkAndInvoke(const std::function<int()> &lambda);
} }

View File

@@ -154,24 +154,21 @@ private:
}; };
void zygisk_companion_handler(int fd) void zygisk_companion_handler(int fd)
{
bool result = [&]() -> bool
{ {
pid_t pid; pid_t pid;
ASSERT_DO(zygisk_companion_handler, read(fd, &pid, sizeof(pid)) == sizeof(pid), return false); ASSERT_DO(zygisk_companion_handler, read(fd, &pid, sizeof(pid)) == sizeof(pid), return);
ASSERT_DO(zygisk_companion_handler, unshare(CLONE_NEWNS) != -1, return false);
ASSERT_DO(zygisk_companion_handler, Utils::switchMountNS(pid), return false);
LOGD("zygisk_companion_handler processing namespace of pid=%d", pid); LOGD("zygisk_companion_handler processing namespace of pid=%d", pid);
// setns mount namespace is not effective until a fork(?) // setns requires the caller to be single-threaded
return WIFEXITED(Utils::executeLambdaInFork( bool result = WIFEXITED(Utils::forkAndInvoke(
[]() [pid]()
{ {
ASSERT_DO(zygisk_companion_handler, Utils::switchMountNS(pid), return 1);
doUnmount(); doUnmount();
doRemount(); doRemount();
doMrProp(); doMrProp();
return 0;
})); }));
}();
ASSERT_LOG(zygisk_companion_handler, write(fd, &result, sizeof(result)) == sizeof(result)); ASSERT_LOG(zygisk_companion_handler, write(fd, &result, sizeof(result)) == sizeof(result));
} }

View File

@@ -56,7 +56,7 @@ bool Utils::switchMountNS(int pid)
{ {
std::string path = "/proc/" + std::to_string(pid) + "/ns/mnt"; std::string path = "/proc/" + std::to_string(pid) + "/ns/mnt";
int ret, fd; int ret, fd;
if ((fd = open(path.c_str(), O_RDONLY)) < 0) if ((fd = open(path.c_str(), O_RDONLY | O_CLOEXEC)) < 0)
{ {
return false; return false;
} }
@@ -66,22 +66,17 @@ bool Utils::switchMountNS(int pid)
return ret == 0; return ret == 0;
} }
int Utils::executeLambdaInFork(const std::function<void()> &lambda) int Utils::forkAndInvoke(const std::function<int()> &lambda)
{ {
pid_t pid = fork(); pid_t pid = fork();
ASSERT_DO(executeLambdaInFork, pid != -1, return -1); if (pid == -1)
return -1;
if (pid == 0) // Child process
exit(lambda());
if (pid == 0)
{
// Child process
lambda();
exit(EXIT_SUCCESS);
}
else
{
// Parent process // Parent process
int status = -1; int status = -1;
waitpid(pid, &status, 0); waitpid(pid, &status, 0);
return status; return status;
} }
}