You've already forked Zygisk-Assistant
mirror of
https://github.com/snake-4/Zygisk-Assistant.git
synced 2025-09-06 06:37:02 +00:00
Added root companion and logging macros
This commit is contained in:
@@ -6,10 +6,10 @@
|
|||||||
#ifndef NDEBUG
|
#ifndef NDEBUG
|
||||||
static constexpr auto TAG = "ZygiskAssistant/JNI";
|
static constexpr auto TAG = "ZygiskAssistant/JNI";
|
||||||
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
|
#define LOGD(...) __android_log_print(ANDROID_LOG_DEBUG, TAG, __VA_ARGS__)
|
||||||
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, TAG, __VA_ARGS__)
|
#define LOGW(...) __android_log_print(ANDROID_LOG_WARN, TAG, __VA_ARGS__)
|
||||||
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
|
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR, TAG, __VA_ARGS__)
|
||||||
#else
|
#else
|
||||||
#define LOGD(...)
|
#define LOGD(...)
|
||||||
#define LOGI(...)
|
#define LOGW(...)
|
||||||
#define LOGE(...)
|
#define LOGE(...)
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -1,10 +1,20 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <errno.h>
|
||||||
|
#include "logging.hpp"
|
||||||
#include "zygisk.hpp"
|
#include "zygisk.hpp"
|
||||||
|
|
||||||
#define DCL_HOOK_FUNC(ret, func, ...) \
|
#define DCL_HOOK_FUNC(ret, func, ...) \
|
||||||
ret (*old_##func)(__VA_ARGS__) = nullptr; \
|
ret (*old_##func)(__VA_ARGS__) = nullptr; \
|
||||||
ret new_##func(__VA_ARGS__)
|
ret new_##func(__VA_ARGS__)
|
||||||
|
|
||||||
|
#define ASSERT_LOG(tag, expr) if(!(expr)) { \
|
||||||
|
LOGE("%s:%d Assertion %s failed. %d:%s", tag, __LINE__, #expr, errno, std::strerror(errno)); }
|
||||||
|
|
||||||
|
#define ASSERT_EXIT(tag, expr, ret) if(!(expr)) { \
|
||||||
|
LOGE("%s:%d Assertion %s failed. %d:%s", tag, __LINE__, #expr, errno, std::strerror(errno)); \
|
||||||
|
ret; }
|
||||||
|
|
||||||
|
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);
|
||||||
|
|||||||
@@ -1,6 +1,10 @@
|
|||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
#include <sched.h>
|
#include <sched.h>
|
||||||
#include <sys/mount.h>
|
#include <sys/mount.h>
|
||||||
|
#include <grp.h>
|
||||||
|
|
||||||
|
#include <cstdint>
|
||||||
|
#include <functional>
|
||||||
|
|
||||||
#include "zygisk.hpp"
|
#include "zygisk.hpp"
|
||||||
#include "logging.hpp"
|
#include "logging.hpp"
|
||||||
@@ -13,6 +17,8 @@ using zygisk::ServerSpecializeArgs;
|
|||||||
void doUnmount();
|
void doUnmount();
|
||||||
void doRemount();
|
void doRemount();
|
||||||
|
|
||||||
|
static std::function<void()> callbackFunction = []() {};
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* [What's the purpose of this hook?]
|
* [What's the purpose of this hook?]
|
||||||
* Calling unshare twice invalidates existing FD links, which fails Zygote sanity checks.
|
* Calling unshare twice invalidates existing FD links, which fails Zygote sanity checks.
|
||||||
@@ -28,9 +34,7 @@ void doRemount();
|
|||||||
*/
|
*/
|
||||||
DCL_HOOK_FUNC(static int, unshare, int flags)
|
DCL_HOOK_FUNC(static int, unshare, int flags)
|
||||||
{
|
{
|
||||||
doUnmount();
|
callbackFunction();
|
||||||
doRemount();
|
|
||||||
|
|
||||||
// Do not allow CLONE_NEWNS.
|
// Do not allow CLONE_NEWNS.
|
||||||
flags &= ~(CLONE_NEWNS);
|
flags &= ~(CLONE_NEWNS);
|
||||||
if (!flags)
|
if (!flags)
|
||||||
@@ -68,27 +72,41 @@ public:
|
|||||||
/*
|
/*
|
||||||
* Read the comment above unshare hook.
|
* Read the comment above unshare hook.
|
||||||
*/
|
*/
|
||||||
if (unshare(CLONE_NEWNS) == -1)
|
ASSERT_EXIT("preAppSpecialize", unshare(CLONE_NEWNS) != -1, return);
|
||||||
{
|
|
||||||
LOGE("unshare(CLONE_NEWNS) returned -1: %d (%s)", errno, strerror(errno));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
/*
|
/*
|
||||||
* Mount the app mount namespace's root as MS_SLAVE, so every mount/umount from
|
* Mount the app mount namespace's root as MS_SLAVE, so every mount/umount from
|
||||||
* Zygote shared pre-specialization namespace is propagated to this one.
|
* Zygote shared pre-specialization namespace is propagated to this one.
|
||||||
*/
|
*/
|
||||||
if (mount("rootfs", "/", NULL, (MS_SLAVE | MS_REC), NULL) == -1)
|
ASSERT_EXIT("preAppSpecialize", mount("rootfs", "/", NULL, (MS_SLAVE | MS_REC), NULL) != -1, return);
|
||||||
{
|
|
||||||
LOGE("mount(\"rootfs\", \"/\", NULL, (MS_SLAVE | MS_REC), NULL) returned -1: %d (%s)", errno, strerror(errno));
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!hookPLTByName("libandroid_runtime.so", "unshare", new_unshare, &old_unshare))
|
ASSERT_EXIT("preAppSpecialize", hookPLTByName("libandroid_runtime.so", "unshare", new_unshare, &old_unshare), return);
|
||||||
|
|
||||||
|
ASSERT_LOG("preAppSpecialize", (companionFd = api->connectCompanion()) != -1);
|
||||||
|
|
||||||
|
callbackFunction = [fd = companionFd]()
|
||||||
{
|
{
|
||||||
LOGE("plt_hook_wrapper(\"libandroid_runtime.so\", \"unshare\", new_unshare, old_unshare) returned false");
|
bool result = false;
|
||||||
return;
|
if (fd != -1)
|
||||||
}
|
{
|
||||||
|
do
|
||||||
|
{
|
||||||
|
pid_t pid = getpid();
|
||||||
|
ASSERT_EXIT("invokeZygiskCompanion", write(fd, &pid, sizeof(pid)) == sizeof(pid), break);
|
||||||
|
ASSERT_EXIT("invokeZygiskCompanion", read(fd, &result, sizeof(result)) == sizeof(result), break);
|
||||||
|
} while (false);
|
||||||
|
close(fd);
|
||||||
|
}
|
||||||
|
|
||||||
|
if (result)
|
||||||
|
LOGD("Invoking the companion was successful.");
|
||||||
|
else
|
||||||
|
{
|
||||||
|
LOGW("Invoking the companion failed. Performing operations in Zygote context!");
|
||||||
|
doUnmount();
|
||||||
|
doRemount();
|
||||||
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
void preServerSpecialize(ServerSpecializeArgs *args) override
|
void preServerSpecialize(ServerSpecializeArgs *args) override
|
||||||
@@ -98,10 +116,8 @@ public:
|
|||||||
|
|
||||||
void postAppSpecialize(const AppSpecializeArgs *args) override
|
void postAppSpecialize(const AppSpecializeArgs *args) override
|
||||||
{
|
{
|
||||||
if (old_unshare != nullptr && !hookPLTByName("libandroid_runtime.so", "unshare", old_unshare))
|
if (old_unshare != nullptr)
|
||||||
{
|
ASSERT_LOG("postAppSpecialize", hookPLTByName("libandroid_runtime.so", "unshare", old_unshare));
|
||||||
LOGE("plt_hook_wrapper(\"libandroid_runtime.so\", \"unshare\", old_unshare) returned false");
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
template <typename T>
|
template <typename T>
|
||||||
@@ -111,8 +127,30 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
int companionFd = -1;
|
||||||
Api *api;
|
Api *api;
|
||||||
JNIEnv *env;
|
JNIEnv *env;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
void zygisk_companion_handler(int fd)
|
||||||
|
{
|
||||||
|
bool result = [&]() -> bool
|
||||||
|
{
|
||||||
|
pid_t pid;
|
||||||
|
ASSERT_EXIT("zygisk_companion_handler", read(fd, &pid, sizeof(pid)) == sizeof(pid), return false);
|
||||||
|
LOGD("zygisk_companion_handler received pid=%d", pid);
|
||||||
|
|
||||||
|
ASSERT_EXIT("zygisk_companion_handler", unshare(CLONE_NEWNS) != -1, return false);
|
||||||
|
ASSERT_EXIT("zygisk_companion_handler", switchMountNS(pid), return false);
|
||||||
|
|
||||||
|
doUnmount();
|
||||||
|
doRemount();
|
||||||
|
|
||||||
|
return true;
|
||||||
|
}();
|
||||||
|
|
||||||
|
ASSERT_LOG("zygisk_companion_handler", write(fd, &result, sizeof(result)) == sizeof(result));
|
||||||
|
}
|
||||||
|
|
||||||
REGISTER_ZYGISK_MODULE(ZygiskModule)
|
REGISTER_ZYGISK_MODULE(ZygiskModule)
|
||||||
|
REGISTER_ZYGISK_COMPANION(zygisk_companion_handler)
|
||||||
|
|||||||
@@ -7,6 +7,7 @@
|
|||||||
|
|
||||||
#include "mount_parser.hpp"
|
#include "mount_parser.hpp"
|
||||||
#include "logging.hpp"
|
#include "logging.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
mount_entry_t::mount_entry_t(::mntent *entry)
|
mount_entry_t::mount_entry_t(::mntent *entry)
|
||||||
: fsname(entry->mnt_fsname), dir(entry->mnt_dir), type(entry->mnt_type), freq(entry->mnt_freq), passno(entry->mnt_passno)
|
: fsname(entry->mnt_fsname), dir(entry->mnt_dir), type(entry->mnt_type), freq(entry->mnt_freq), passno(entry->mnt_passno)
|
||||||
@@ -25,12 +26,8 @@ std::vector<mount_entry_t> parseMountsFromPath(const char *path)
|
|||||||
{
|
{
|
||||||
std::vector<mount_entry_t> result;
|
std::vector<mount_entry_t> result;
|
||||||
|
|
||||||
FILE *file = setmntent(path, "r");
|
FILE *file;
|
||||||
if (file == NULL)
|
ASSERT_EXIT("parseMountsFromPath", (file = setmntent(path, "r")) != NULL, return result);
|
||||||
{
|
|
||||||
LOGE("setmntent(\"%s\", \"r\") returned NULL: %d (%s)", path, errno, strerror(errno));
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
struct mntent *entry;
|
struct mntent *entry;
|
||||||
while ((entry = getmntent(file)) != NULL)
|
while ((entry = getmntent(file)) != NULL)
|
||||||
|
|||||||
@@ -8,6 +8,7 @@
|
|||||||
#include "logging.hpp"
|
#include "logging.hpp"
|
||||||
#include "mount_parser.hpp"
|
#include "mount_parser.hpp"
|
||||||
#include "mountinfo_parser.hpp"
|
#include "mountinfo_parser.hpp"
|
||||||
|
#include "utils.hpp"
|
||||||
|
|
||||||
constexpr std::array<const char *, 4> fsname_list = {"KSU", "APatch", "magisk", "worker"};
|
constexpr std::array<const char *, 4> fsname_list = {"KSU", "APatch", "magisk", "worker"};
|
||||||
|
|
||||||
@@ -91,7 +92,7 @@ void doUnmount()
|
|||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
LOGE("umount2(\"%s\", MNT_DETACH) returned -1: %d (%s)", mountPoint.c_str(), errno, strerror(errno));
|
LOGW("umount2(\"%s\", MNT_DETACH) returned -1: %d (%s)", mountPoint.c_str(), errno, strerror(errno));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -108,14 +109,8 @@ void doRemount()
|
|||||||
// If errors=remount-ro, remount it with errors=continue
|
// If errors=remount-ro, remount it with errors=continue
|
||||||
if (options.find("errors") != options.end() && options.at("errors") == "remount-ro")
|
if (options.find("errors") != options.end() && options.at("errors") == "remount-ro")
|
||||||
{
|
{
|
||||||
if (mount(NULL, "/data", NULL, MS_REMOUNT, "errors=continue") == 0)
|
LOGD("Trying to remount: /data");
|
||||||
{
|
ASSERT_LOG("doRemount", mount(NULL, "/data", NULL, MS_REMOUNT, "errors=continue") == 0);
|
||||||
LOGD("mount(NULL, \"/data\", NULL, MS_REMOUNT, \"errors=continue\") returned 0");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
LOGE("mount(NULL, \"/data\", NULL, MS_REMOUNT, \"errors=continue\") returned -1: %d (%s)", errno, strerror(errno));
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,9 +1,14 @@
|
|||||||
#include <string>
|
#include <string>
|
||||||
|
#include <format>
|
||||||
|
#include <unistd.h>
|
||||||
|
#include <sched.h>
|
||||||
|
#include <fcntl.h>
|
||||||
|
|
||||||
#include "map_parser.hpp"
|
#include "map_parser.hpp"
|
||||||
#include "utils.hpp"
|
#include "utils.hpp"
|
||||||
#include "android_filesystem_config.h"
|
#include "android_filesystem_config.h"
|
||||||
#include "zygisk.hpp"
|
#include "zygisk.hpp"
|
||||||
|
#include "logging.hpp"
|
||||||
|
|
||||||
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)
|
||||||
{
|
{
|
||||||
@@ -27,3 +32,17 @@ int isUserAppUID(int uid)
|
|||||||
return true;
|
return true;
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool switchMountNS(int pid)
|
||||||
|
{
|
||||||
|
std::string path = std::string("/proc/") + std::to_string(pid) + "/ns/mnt";
|
||||||
|
int ret, fd;
|
||||||
|
if ((fd = open(path.c_str(), O_RDONLY)) < 0)
|
||||||
|
{
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
ret = setns(fd, 0);
|
||||||
|
close(fd);
|
||||||
|
return ret == 0;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user