mirror of
https://github.com/topjohnwu/Magisk.git
synced 2025-12-13 10:21:51 +00:00
Restructure project files
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <magisk.hpp>
|
||||
#include <consts.hpp>
|
||||
#include <selinux.hpp>
|
||||
#include <base.hpp>
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <magisk.hpp>
|
||||
#include <consts.hpp>
|
||||
#include <selinux.hpp>
|
||||
#include <base.hpp>
|
||||
|
||||
|
||||
@@ -6,14 +6,12 @@
|
||||
#include <set>
|
||||
#include <string>
|
||||
|
||||
#include <magisk.hpp>
|
||||
#include <consts.hpp>
|
||||
#include <db.hpp>
|
||||
#include <base.hpp>
|
||||
#include <daemon.hpp>
|
||||
#include <core.hpp>
|
||||
#include <selinux.hpp>
|
||||
|
||||
#include "core.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Boot stage state
|
||||
|
||||
@@ -1,30 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "core-rs.hpp"
|
||||
#include "resetprop/resetprop.hpp"
|
||||
|
||||
extern bool RECOVERY_MODE;
|
||||
extern std::atomic<ino_t> pkg_xml_ino;
|
||||
|
||||
std::string find_preinit_device();
|
||||
void unlock_blocks();
|
||||
void reboot();
|
||||
|
||||
// Module stuffs
|
||||
void handle_modules();
|
||||
void load_modules();
|
||||
void disable_modules();
|
||||
void remove_modules();
|
||||
void exec_module_scripts(const char *stage);
|
||||
|
||||
// Scripting
|
||||
void exec_script(const char *script);
|
||||
void exec_common_scripts(const char *stage);
|
||||
void exec_module_scripts(const char *stage, const std::vector<std::string_view> &modules);
|
||||
void install_apk(const char *apk);
|
||||
void uninstall_pkg(const char *pkg);
|
||||
void clear_pkg(const char *pkg, int user_id);
|
||||
[[noreturn]] void install_module(const char *file);
|
||||
@@ -3,15 +3,13 @@
|
||||
#include <sys/un.h>
|
||||
#include <sys/mount.h>
|
||||
|
||||
#include <magisk.hpp>
|
||||
#include <consts.hpp>
|
||||
#include <base.hpp>
|
||||
#include <daemon.hpp>
|
||||
#include <core.hpp>
|
||||
#include <selinux.hpp>
|
||||
#include <db.hpp>
|
||||
#include <flags.h>
|
||||
|
||||
#include "core.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
int SDK_INT = -1;
|
||||
|
||||
@@ -2,12 +2,10 @@
|
||||
#include <dlfcn.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <magisk.hpp>
|
||||
#include <db.hpp>
|
||||
#include <socket.hpp>
|
||||
#include <consts.hpp>
|
||||
#include <base.hpp>
|
||||
|
||||
#include "core.hpp"
|
||||
#include <db.hpp>
|
||||
#include <core.hpp>
|
||||
|
||||
#define DB_VERSION 12
|
||||
|
||||
|
||||
146
native/src/core/deny/cli.cpp
Normal file
146
native/src/core/deny/cli.cpp
Normal file
@@ -0,0 +1,146 @@
|
||||
#include <sys/wait.h>
|
||||
#include <sys/mount.h>
|
||||
|
||||
#include <consts.hpp>
|
||||
#include <base.hpp>
|
||||
|
||||
#include "deny.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
[[noreturn]] static void usage() {
|
||||
fprintf(stderr,
|
||||
R"EOF(DenyList Config CLI
|
||||
|
||||
Usage: magisk --denylist [action [arguments...] ]
|
||||
Actions:
|
||||
status Return the enforcement status
|
||||
enable Enable denylist enforcement
|
||||
disable Disable denylist enforcement
|
||||
add PKG [PROC] Add a new target to the denylist
|
||||
rm PKG [PROC] Remove target(s) from the denylist
|
||||
ls Print the current denylist
|
||||
exec CMDs... Execute commands in isolated mount
|
||||
namespace and do all unmounts
|
||||
|
||||
)EOF");
|
||||
exit(1);
|
||||
}
|
||||
|
||||
void denylist_handler(int client, const sock_cred *cred) {
|
||||
if (client < 0) {
|
||||
revert_unmount();
|
||||
return;
|
||||
}
|
||||
|
||||
int req = read_int(client);
|
||||
int res = DenyResponse::ERROR;
|
||||
|
||||
switch (req) {
|
||||
case DenyRequest::ENFORCE:
|
||||
res = enable_deny();
|
||||
break;
|
||||
case DenyRequest::DISABLE:
|
||||
res = disable_deny();
|
||||
break;
|
||||
case DenyRequest::ADD:
|
||||
res = add_list(client);
|
||||
break;
|
||||
case DenyRequest::REMOVE:
|
||||
res = rm_list(client);
|
||||
break;
|
||||
case DenyRequest::LIST:
|
||||
ls_list(client);
|
||||
return;
|
||||
case DenyRequest::STATUS:
|
||||
res = (zygisk_enabled && denylist_enforced)
|
||||
? DenyResponse::ENFORCED : DenyResponse::NOT_ENFORCED;
|
||||
break;
|
||||
default:
|
||||
// Unknown request code
|
||||
break;
|
||||
}
|
||||
write_int(client, res);
|
||||
close(client);
|
||||
}
|
||||
|
||||
int denylist_cli(int argc, char **argv) {
|
||||
if (argc < 2)
|
||||
usage();
|
||||
|
||||
int req;
|
||||
if (argv[1] == "enable"sv)
|
||||
req = DenyRequest::ENFORCE;
|
||||
else if (argv[1] == "disable"sv)
|
||||
req = DenyRequest::DISABLE;
|
||||
else if (argv[1] == "add"sv)
|
||||
req = DenyRequest::ADD;
|
||||
else if (argv[1] == "rm"sv)
|
||||
req = DenyRequest::REMOVE;
|
||||
else if (argv[1] == "ls"sv)
|
||||
req = DenyRequest::LIST;
|
||||
else if (argv[1] == "status"sv)
|
||||
req = DenyRequest::STATUS;
|
||||
else if (argv[1] == "exec"sv && argc > 2) {
|
||||
xunshare(CLONE_NEWNS);
|
||||
xmount(nullptr, "/", nullptr, MS_PRIVATE | MS_REC, nullptr);
|
||||
revert_unmount();
|
||||
execvp(argv[2], argv + 2);
|
||||
exit(1);
|
||||
} else {
|
||||
usage();
|
||||
}
|
||||
|
||||
// Send request
|
||||
int fd = connect_daemon(MainRequest::DENYLIST);
|
||||
write_int(fd, req);
|
||||
if (req == DenyRequest::ADD || req == DenyRequest::REMOVE) {
|
||||
write_string(fd, argv[2]);
|
||||
write_string(fd, argv[3] ? argv[3] : "");
|
||||
}
|
||||
|
||||
// Get response
|
||||
int res = read_int(fd);
|
||||
if (res < 0 || res >= DenyResponse::END)
|
||||
res = DenyResponse::ERROR;
|
||||
switch (res) {
|
||||
case DenyResponse::NOT_ENFORCED:
|
||||
fprintf(stderr, "Denylist is not enforced\n");
|
||||
goto return_code;
|
||||
case DenyResponse::ENFORCED:
|
||||
fprintf(stderr, "Denylist is enforced\n");
|
||||
goto return_code;
|
||||
case DenyResponse::ITEM_EXIST:
|
||||
fprintf(stderr, "Target already exists in denylist\n");
|
||||
goto return_code;
|
||||
case DenyResponse::ITEM_NOT_EXIST:
|
||||
fprintf(stderr, "Target does not exist in denylist\n");
|
||||
goto return_code;
|
||||
case DenyResponse::NO_NS:
|
||||
fprintf(stderr, "The kernel does not support mount namespace\n");
|
||||
goto return_code;
|
||||
case DenyResponse::INVALID_PKG:
|
||||
fprintf(stderr, "Invalid package / process name\n");
|
||||
goto return_code;
|
||||
case DenyResponse::ERROR:
|
||||
fprintf(stderr, "deny: Daemon error\n");
|
||||
return -1;
|
||||
case DenyResponse::OK:
|
||||
break;
|
||||
default:
|
||||
__builtin_unreachable();
|
||||
}
|
||||
|
||||
if (req == DenyRequest::LIST) {
|
||||
string out;
|
||||
for (;;) {
|
||||
read_string(fd, out);
|
||||
if (out.empty())
|
||||
break;
|
||||
printf("%s\n", out.data());
|
||||
}
|
||||
}
|
||||
|
||||
return_code:
|
||||
return req == DenyRequest::STATUS ? res != DenyResponse::ENFORCED : res != DenyResponse::OK;
|
||||
}
|
||||
46
native/src/core/deny/deny.hpp
Normal file
46
native/src/core/deny/deny.hpp
Normal file
@@ -0,0 +1,46 @@
|
||||
#pragma once
|
||||
|
||||
#include <pthread.h>
|
||||
#include <string_view>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <atomic>
|
||||
|
||||
#include <core.hpp>
|
||||
|
||||
#define ISOLATED_MAGIC "isolated"
|
||||
|
||||
namespace DenyRequest {
|
||||
enum : int {
|
||||
ENFORCE,
|
||||
DISABLE,
|
||||
ADD,
|
||||
REMOVE,
|
||||
LIST,
|
||||
STATUS,
|
||||
|
||||
END
|
||||
};
|
||||
}
|
||||
|
||||
namespace DenyResponse {
|
||||
enum : int {
|
||||
OK,
|
||||
ENFORCED,
|
||||
NOT_ENFORCED,
|
||||
ITEM_EXIST,
|
||||
ITEM_NOT_EXIST,
|
||||
INVALID_PKG,
|
||||
NO_NS,
|
||||
ERROR,
|
||||
|
||||
END
|
||||
};
|
||||
}
|
||||
|
||||
// CLI entries
|
||||
int enable_deny();
|
||||
int disable_deny();
|
||||
int add_list(int client);
|
||||
int rm_list(int client);
|
||||
void ls_list(int client);
|
||||
42
native/src/core/deny/revert.cpp
Normal file
42
native/src/core/deny/revert.cpp
Normal file
@@ -0,0 +1,42 @@
|
||||
#include <set>
|
||||
#include <sys/mount.h>
|
||||
|
||||
#include <consts.hpp>
|
||||
#include <base.hpp>
|
||||
#include <core.hpp>
|
||||
|
||||
#include "deny.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
static void lazy_unmount(const char* mountpoint) {
|
||||
if (umount2(mountpoint, MNT_DETACH) != -1)
|
||||
LOGD("denylist: Unmounted (%s)\n", mountpoint);
|
||||
}
|
||||
|
||||
void revert_unmount() {
|
||||
set<string> targets;
|
||||
|
||||
// Unmount dummy skeletons and MAGISKTMP
|
||||
// since mirror nodes are always mounted under skeleton, we don't have to specifically unmount
|
||||
for (auto &info: parse_mount_info("self")) {
|
||||
if (info.source == "magisk" || info.source == "worker" || // magisktmp tmpfs
|
||||
info.root.starts_with("/adb/modules")) { // bind mount from data partition
|
||||
targets.insert(info.target);
|
||||
}
|
||||
}
|
||||
|
||||
if (targets.empty()) return;
|
||||
|
||||
auto last_target = *targets.cbegin() + '/';
|
||||
for (auto iter = next(targets.cbegin()); iter != targets.cend();) {
|
||||
if (iter->starts_with(last_target)) {
|
||||
iter = targets.erase(iter);
|
||||
} else {
|
||||
last_target = *iter++ + '/';
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &s : targets)
|
||||
lazy_unmount(s.data());
|
||||
}
|
||||
417
native/src/core/deny/utils.cpp
Normal file
417
native/src/core/deny/utils.cpp
Normal file
@@ -0,0 +1,417 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <sys/inotify.h>
|
||||
#include <unistd.h>
|
||||
#include <fcntl.h>
|
||||
#include <dirent.h>
|
||||
#include <set>
|
||||
|
||||
#include <consts.hpp>
|
||||
#include <base.hpp>
|
||||
#include <db.hpp>
|
||||
#include <core.hpp>
|
||||
|
||||
#include "deny.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
atomic_flag skip_pkg_rescan;
|
||||
|
||||
// For the following data structures:
|
||||
// If package name == ISOLATED_MAGIC, or app ID == -1, it means isolated service
|
||||
|
||||
// Package name -> list of process names
|
||||
static unique_ptr<map<string, set<string, StringCmp>, StringCmp>> pkg_to_procs_;
|
||||
#define pkg_to_procs (*pkg_to_procs_)
|
||||
|
||||
// app ID -> list of pkg names (string_view points to a pkg_to_procs key)
|
||||
static unique_ptr<map<int, set<string_view>>> app_id_to_pkgs_;
|
||||
#define app_id_to_pkgs (*app_id_to_pkgs_)
|
||||
|
||||
// Locks the data structures above
|
||||
static pthread_mutex_t data_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
|
||||
atomic<bool> denylist_enforced = false;
|
||||
|
||||
#define do_kill (zygisk_enabled && denylist_enforced)
|
||||
|
||||
static void rescan_apps() {
|
||||
LOGD("denylist: rescanning apps\n");
|
||||
|
||||
app_id_to_pkgs.clear();
|
||||
|
||||
auto data_dir = xopen_dir(APP_DATA_DIR);
|
||||
if (!data_dir)
|
||||
return;
|
||||
dirent *entry;
|
||||
while ((entry = xreaddir(data_dir.get()))) {
|
||||
// For each user
|
||||
int dfd = xopenat(dirfd(data_dir.get()), entry->d_name, O_RDONLY);
|
||||
if (auto dir = xopen_dir(dfd)) {
|
||||
while ((entry = xreaddir(dir.get()))) {
|
||||
struct stat st{};
|
||||
// For each package
|
||||
if (xfstatat(dfd, entry->d_name, &st, 0))
|
||||
continue;
|
||||
int app_id = to_app_id(st.st_uid);
|
||||
if (auto it = pkg_to_procs.find(entry->d_name); it != pkg_to_procs.end()) {
|
||||
app_id_to_pkgs[app_id].insert(it->first);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
close(dfd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void update_pkg_uid(const string &pkg, bool remove) {
|
||||
auto data_dir = xopen_dir(APP_DATA_DIR);
|
||||
if (!data_dir)
|
||||
return;
|
||||
dirent *entry;
|
||||
struct stat st{};
|
||||
char buf[PATH_MAX] = {0};
|
||||
// For each user
|
||||
while ((entry = xreaddir(data_dir.get()))) {
|
||||
ssprintf(buf, sizeof(buf), "%s/%s", entry->d_name, pkg.data());
|
||||
if (fstatat(dirfd(data_dir.get()), buf, &st, 0) == 0) {
|
||||
int app_id = to_app_id(st.st_uid);
|
||||
if (remove) {
|
||||
if (auto it = app_id_to_pkgs.find(app_id); it != app_id_to_pkgs.end()) {
|
||||
it->second.erase(pkg);
|
||||
if (it->second.empty()) {
|
||||
app_id_to_pkgs.erase(it);
|
||||
}
|
||||
}
|
||||
} else {
|
||||
app_id_to_pkgs[app_id].insert(pkg);
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Leave /proc fd opened as we're going to read from it repeatedly
|
||||
static DIR *procfp;
|
||||
|
||||
template<class F>
|
||||
static void crawl_procfs(const F &fn) {
|
||||
rewinddir(procfp);
|
||||
dirent *dp;
|
||||
int pid;
|
||||
while ((dp = readdir(procfp))) {
|
||||
pid = parse_int(dp->d_name);
|
||||
if (pid > 0 && !fn(pid))
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
static inline bool str_eql(string_view a, string_view b) { return a == b; }
|
||||
|
||||
template<bool str_op(string_view, string_view) = &str_eql>
|
||||
static bool proc_name_match(int pid, string_view name) {
|
||||
char buf[4019];
|
||||
sprintf(buf, "/proc/%d/cmdline", pid);
|
||||
if (auto fp = open_file(buf, "re")) {
|
||||
fgets(buf, sizeof(buf), fp.get());
|
||||
if (str_op(buf, name)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool proc_context_match(int pid, string_view context) {
|
||||
char buf[PATH_MAX];
|
||||
sprintf(buf, "/proc/%d/attr/current", pid);
|
||||
if (auto fp = open_file(buf, "re")) {
|
||||
fgets(buf, sizeof(buf), fp.get());
|
||||
if (str_starts(buf, context)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
template<bool matcher(int, string_view) = &proc_name_match>
|
||||
static void kill_process(const char *name, bool multi = false) {
|
||||
crawl_procfs([=](int pid) -> bool {
|
||||
if (matcher(pid, name)) {
|
||||
kill(pid, SIGKILL);
|
||||
LOGD("denylist: kill PID=[%d] (%s)\n", pid, name);
|
||||
return multi;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}
|
||||
|
||||
static bool validate(const char *pkg, const char *proc) {
|
||||
bool pkg_valid = false;
|
||||
bool proc_valid = true;
|
||||
|
||||
if (str_eql(pkg, ISOLATED_MAGIC)) {
|
||||
pkg_valid = true;
|
||||
for (char c; (c = *proc); ++proc) {
|
||||
if (isalnum(c) || c == '_' || c == '.')
|
||||
continue;
|
||||
if (c == ':')
|
||||
break;
|
||||
proc_valid = false;
|
||||
break;
|
||||
}
|
||||
} else {
|
||||
for (char c; (c = *pkg); ++pkg) {
|
||||
if (isalnum(c) || c == '_')
|
||||
continue;
|
||||
if (c == '.') {
|
||||
pkg_valid = true;
|
||||
continue;
|
||||
}
|
||||
pkg_valid = false;
|
||||
break;
|
||||
}
|
||||
|
||||
for (char c; (c = *proc); ++proc) {
|
||||
if (isalnum(c) || c == '_' || c == ':' || c == '.')
|
||||
continue;
|
||||
proc_valid = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return pkg_valid && proc_valid;
|
||||
}
|
||||
|
||||
static bool add_hide_set(const char *pkg, const char *proc) {
|
||||
auto p = pkg_to_procs[pkg].emplace(proc);
|
||||
if (!p.second)
|
||||
return false;
|
||||
LOGI("denylist add: [%s/%s]\n", pkg, proc);
|
||||
if (!do_kill)
|
||||
return true;
|
||||
if (str_eql(pkg, ISOLATED_MAGIC)) {
|
||||
// Kill all matching isolated processes
|
||||
kill_process<&proc_name_match<str_starts>>(proc, true);
|
||||
} else {
|
||||
kill_process(proc);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
static void clear_data() {
|
||||
pkg_to_procs_.reset(nullptr);
|
||||
app_id_to_pkgs_.reset(nullptr);
|
||||
}
|
||||
|
||||
static bool ensure_data() {
|
||||
if (pkg_to_procs_)
|
||||
return true;
|
||||
|
||||
LOGI("denylist: initializing internal data structures\n");
|
||||
|
||||
default_new(pkg_to_procs_);
|
||||
char *err = db_exec("SELECT * FROM denylist", [](db_row &row) -> bool {
|
||||
add_hide_set(row["package_name"].data(), row["process"].data());
|
||||
return true;
|
||||
});
|
||||
db_err_cmd(err, goto error)
|
||||
|
||||
default_new(app_id_to_pkgs_);
|
||||
rescan_apps();
|
||||
|
||||
return true;
|
||||
|
||||
error:
|
||||
clear_data();
|
||||
return false;
|
||||
}
|
||||
|
||||
static int add_list(const char *pkg, const char *proc) {
|
||||
if (proc[0] == '\0')
|
||||
proc = pkg;
|
||||
|
||||
if (!validate(pkg, proc))
|
||||
return DenyResponse::INVALID_PKG;
|
||||
|
||||
{
|
||||
mutex_guard lock(data_lock);
|
||||
if (!ensure_data())
|
||||
return DenyResponse::ERROR;
|
||||
if (!add_hide_set(pkg, proc))
|
||||
return DenyResponse::ITEM_EXIST;
|
||||
auto it = pkg_to_procs.find(pkg);
|
||||
update_pkg_uid(it->first, false);
|
||||
}
|
||||
|
||||
// Add to database
|
||||
char sql[4096];
|
||||
ssprintf(sql, sizeof(sql),
|
||||
"INSERT INTO denylist (package_name, process) VALUES('%s', '%s')", pkg, proc);
|
||||
char *err = db_exec(sql);
|
||||
db_err_cmd(err, return DenyResponse::ERROR)
|
||||
return DenyResponse::OK;
|
||||
}
|
||||
|
||||
int add_list(int client) {
|
||||
string pkg = read_string(client);
|
||||
string proc = read_string(client);
|
||||
return add_list(pkg.data(), proc.data());
|
||||
}
|
||||
|
||||
static int rm_list(const char *pkg, const char *proc) {
|
||||
{
|
||||
mutex_guard lock(data_lock);
|
||||
if (!ensure_data())
|
||||
return DenyResponse::ERROR;
|
||||
|
||||
bool remove = false;
|
||||
|
||||
auto it = pkg_to_procs.find(pkg);
|
||||
if (it != pkg_to_procs.end()) {
|
||||
if (proc[0] == '\0') {
|
||||
update_pkg_uid(it->first, true);
|
||||
pkg_to_procs.erase(it);
|
||||
remove = true;
|
||||
LOGI("denylist rm: [%s]\n", pkg);
|
||||
} else if (it->second.erase(proc) != 0) {
|
||||
remove = true;
|
||||
LOGI("denylist rm: [%s/%s]\n", pkg, proc);
|
||||
if (it->second.empty()) {
|
||||
update_pkg_uid(it->first, true);
|
||||
pkg_to_procs.erase(it);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!remove)
|
||||
return DenyResponse::ITEM_NOT_EXIST;
|
||||
}
|
||||
|
||||
char sql[4096];
|
||||
if (proc[0] == '\0')
|
||||
ssprintf(sql, sizeof(sql), "DELETE FROM denylist WHERE package_name='%s'", pkg);
|
||||
else
|
||||
ssprintf(sql, sizeof(sql),
|
||||
"DELETE FROM denylist WHERE package_name='%s' AND process='%s'", pkg, proc);
|
||||
char *err = db_exec(sql);
|
||||
db_err_cmd(err, return DenyResponse::ERROR)
|
||||
return DenyResponse::OK;
|
||||
}
|
||||
|
||||
int rm_list(int client) {
|
||||
string pkg = read_string(client);
|
||||
string proc = read_string(client);
|
||||
return rm_list(pkg.data(), proc.data());
|
||||
}
|
||||
|
||||
void ls_list(int client) {
|
||||
{
|
||||
mutex_guard lock(data_lock);
|
||||
if (!ensure_data()) {
|
||||
write_int(client, static_cast<int>(DenyResponse::ERROR));
|
||||
return;
|
||||
}
|
||||
|
||||
write_int(client,static_cast<int>(DenyResponse::OK));
|
||||
|
||||
for (const auto &[pkg, procs] : pkg_to_procs) {
|
||||
for (const auto &proc : procs) {
|
||||
write_int(client, pkg.size() + proc.size() + 1);
|
||||
xwrite(client, pkg.data(), pkg.size());
|
||||
xwrite(client, "|", 1);
|
||||
xwrite(client, proc.data(), proc.size());
|
||||
}
|
||||
}
|
||||
}
|
||||
write_int(client, 0);
|
||||
close(client);
|
||||
}
|
||||
|
||||
static void update_deny_config() {
|
||||
char sql[64];
|
||||
sprintf(sql, "REPLACE INTO settings (key,value) VALUES('%s',%d)",
|
||||
DB_SETTING_KEYS[DENYLIST_CONFIG], denylist_enforced.load());
|
||||
char *err = db_exec(sql);
|
||||
db_err(err);
|
||||
}
|
||||
|
||||
int enable_deny() {
|
||||
if (denylist_enforced) {
|
||||
return DenyResponse::OK;
|
||||
} else {
|
||||
mutex_guard lock(data_lock);
|
||||
|
||||
if (access("/proc/self/ns/mnt", F_OK) != 0) {
|
||||
LOGW("The kernel does not support mount namespace\n");
|
||||
return DenyResponse::NO_NS;
|
||||
}
|
||||
|
||||
if (procfp == nullptr && (procfp = opendir("/proc")) == nullptr)
|
||||
return DenyResponse::ERROR;
|
||||
|
||||
LOGI("* Enable DenyList\n");
|
||||
|
||||
denylist_enforced = true;
|
||||
|
||||
if (!ensure_data()) {
|
||||
denylist_enforced = false;
|
||||
return DenyResponse::ERROR;
|
||||
}
|
||||
|
||||
// On Android Q+, also kill blastula pool and all app zygotes
|
||||
if (SDK_INT >= 29 && zygisk_enabled) {
|
||||
kill_process("usap32", true);
|
||||
kill_process("usap64", true);
|
||||
kill_process<&proc_context_match>("u:r:app_zygote:s0", true);
|
||||
}
|
||||
}
|
||||
|
||||
update_deny_config();
|
||||
return DenyResponse::OK;
|
||||
}
|
||||
|
||||
int disable_deny() {
|
||||
if (denylist_enforced) {
|
||||
denylist_enforced = false;
|
||||
LOGI("* Disable DenyList\n");
|
||||
}
|
||||
update_deny_config();
|
||||
return DenyResponse::OK;
|
||||
}
|
||||
|
||||
void initialize_denylist() {
|
||||
if (!denylist_enforced) {
|
||||
db_settings dbs;
|
||||
get_db_settings(dbs, DENYLIST_CONFIG);
|
||||
if (dbs[DENYLIST_CONFIG])
|
||||
enable_deny();
|
||||
}
|
||||
}
|
||||
|
||||
bool is_deny_target(int uid, string_view process) {
|
||||
mutex_guard lock(data_lock);
|
||||
if (!ensure_data())
|
||||
return false;
|
||||
|
||||
if (!skip_pkg_rescan.test_and_set())
|
||||
rescan_apps();
|
||||
|
||||
int app_id = to_app_id(uid);
|
||||
if (app_id >= 90000) {
|
||||
if (auto it = pkg_to_procs.find(ISOLATED_MAGIC); it != pkg_to_procs.end()) {
|
||||
for (const auto &s : it->second) {
|
||||
if (str_starts(process, s))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
} else {
|
||||
auto it = app_id_to_pkgs.find(app_id);
|
||||
if (it == app_id_to_pkgs.end())
|
||||
return false;
|
||||
for (const auto &pkg : it->second) {
|
||||
if (pkg_to_procs.find(pkg)->second.count(process))
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
126
native/src/core/include/core.hpp
Normal file
126
native/src/core/include/core.hpp
Normal file
@@ -0,0 +1,126 @@
|
||||
#pragma once
|
||||
|
||||
#include <pthread.h>
|
||||
#include <poll.h>
|
||||
#include <string>
|
||||
#include <limits>
|
||||
#include <atomic>
|
||||
#include <functional>
|
||||
|
||||
#include "socket.hpp"
|
||||
#include "../core-rs.hpp"
|
||||
|
||||
#define AID_ROOT 0
|
||||
#define AID_SHELL 2000
|
||||
#define AID_APP_START 10000
|
||||
#define AID_APP_END 19999
|
||||
#define AID_USER_OFFSET 100000
|
||||
|
||||
#define to_app_id(uid) (uid % AID_USER_OFFSET)
|
||||
#define to_user_id(uid) (uid / AID_USER_OFFSET)
|
||||
|
||||
// Daemon command codes
|
||||
namespace MainRequest {
|
||||
enum : int {
|
||||
START_DAEMON,
|
||||
CHECK_VERSION,
|
||||
CHECK_VERSION_CODE,
|
||||
STOP_DAEMON,
|
||||
|
||||
_SYNC_BARRIER_,
|
||||
|
||||
SUPERUSER,
|
||||
ZYGOTE_RESTART,
|
||||
DENYLIST,
|
||||
SQLITE_CMD,
|
||||
REMOVE_MODULES,
|
||||
ZYGISK,
|
||||
|
||||
_STAGE_BARRIER_,
|
||||
|
||||
POST_FS_DATA,
|
||||
LATE_START,
|
||||
BOOT_COMPLETE,
|
||||
|
||||
END,
|
||||
};
|
||||
}
|
||||
|
||||
// Return codes for daemon
|
||||
namespace MainResponse {
|
||||
enum : int {
|
||||
ERROR = -1,
|
||||
OK = 0,
|
||||
ROOT_REQUIRED,
|
||||
ACCESS_DENIED,
|
||||
END
|
||||
};
|
||||
}
|
||||
|
||||
struct module_info {
|
||||
std::string name;
|
||||
int z32 = -1;
|
||||
#if defined(__LP64__)
|
||||
int z64 = -1;
|
||||
#endif
|
||||
};
|
||||
|
||||
extern bool RECOVERY_MODE;
|
||||
extern bool zygisk_enabled;
|
||||
extern std::vector<module_info> *module_list;
|
||||
|
||||
void reset_zygisk(bool restore);
|
||||
extern "C" const char *get_magisk_tmp();
|
||||
int connect_daemon(int req, bool create = false);
|
||||
std::string find_preinit_device();
|
||||
void unlock_blocks();
|
||||
void reboot();
|
||||
|
||||
// Poll control
|
||||
using poll_callback = void(*)(pollfd*);
|
||||
void register_poll(const pollfd *pfd, poll_callback callback);
|
||||
void unregister_poll(int fd, bool auto_close);
|
||||
void clear_poll();
|
||||
|
||||
// Thread pool
|
||||
void exec_task(std::function<void()> &&task);
|
||||
|
||||
// Daemon handlers
|
||||
void boot_stage_handler(int client, int code);
|
||||
void denylist_handler(int client, const sock_cred *cred);
|
||||
void su_daemon_handler(int client, const sock_cred *cred);
|
||||
void zygisk_handler(int client, const sock_cred *cred);
|
||||
|
||||
// Package
|
||||
extern std::atomic<ino_t> pkg_xml_ino;
|
||||
void preserve_stub_apk();
|
||||
void check_pkg_refresh();
|
||||
std::vector<bool> get_app_no_list();
|
||||
// Call check_pkg_refresh() before calling get_manager(...)
|
||||
// to make sure the package state is invalidated!
|
||||
int get_manager(int user_id = 0, std::string *pkg = nullptr, bool install = false);
|
||||
void prune_su_access();
|
||||
|
||||
// Module stuffs
|
||||
void handle_modules();
|
||||
void load_modules();
|
||||
void disable_modules();
|
||||
void remove_modules();
|
||||
void exec_module_scripts(const char *stage);
|
||||
|
||||
// Scripting
|
||||
void exec_script(const char *script);
|
||||
void exec_common_scripts(const char *stage);
|
||||
void exec_module_scripts(const char *stage, const std::vector<std::string_view> &modules);
|
||||
void install_apk(const char *apk);
|
||||
void uninstall_pkg(const char *pkg);
|
||||
void clear_pkg(const char *pkg, int user_id);
|
||||
[[noreturn]] void install_module(const char *file);
|
||||
|
||||
// Denylist
|
||||
extern std::atomic_flag skip_pkg_rescan;
|
||||
extern std::atomic<bool> denylist_enforced;
|
||||
int denylist_cli(int argc, char **argv);
|
||||
void initialize_denylist();
|
||||
bool is_deny_target(int uid, std::string_view process);
|
||||
void revert_unmount();
|
||||
134
native/src/core/include/db.hpp
Normal file
134
native/src/core/include/db.hpp
Normal file
@@ -0,0 +1,134 @@
|
||||
#pragma once
|
||||
|
||||
#include <sys/stat.h>
|
||||
#include <map>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <functional>
|
||||
|
||||
template <class T, size_t N>
|
||||
class db_dict {
|
||||
public:
|
||||
T& operator [](std::string_view key) {
|
||||
return data[get_idx(key)];
|
||||
}
|
||||
|
||||
const T& operator [](std::string_view key) const {
|
||||
return data[get_idx(key)];
|
||||
}
|
||||
|
||||
T& operator [](int key) {
|
||||
return data[key];
|
||||
}
|
||||
|
||||
const T& operator [](int key) const {
|
||||
return data[key];
|
||||
}
|
||||
|
||||
protected:
|
||||
T data[N + 1];
|
||||
virtual int get_idx(std::string_view key) const = 0;
|
||||
};
|
||||
|
||||
/***************
|
||||
* DB Settings *
|
||||
***************/
|
||||
|
||||
constexpr const char *DB_SETTING_KEYS[] = {
|
||||
"root_access",
|
||||
"multiuser_mode",
|
||||
"mnt_ns",
|
||||
"denylist",
|
||||
"zygisk"
|
||||
};
|
||||
|
||||
// Settings key indices
|
||||
enum {
|
||||
ROOT_ACCESS = 0,
|
||||
SU_MULTIUSER_MODE,
|
||||
SU_MNT_NS,
|
||||
DENYLIST_CONFIG,
|
||||
ZYGISK_CONFIG
|
||||
};
|
||||
|
||||
// Values for root_access
|
||||
enum {
|
||||
ROOT_ACCESS_DISABLED = 0,
|
||||
ROOT_ACCESS_APPS_ONLY,
|
||||
ROOT_ACCESS_ADB_ONLY,
|
||||
ROOT_ACCESS_APPS_AND_ADB
|
||||
};
|
||||
|
||||
// Values for multiuser_mode
|
||||
enum {
|
||||
MULTIUSER_MODE_OWNER_ONLY = 0,
|
||||
MULTIUSER_MODE_OWNER_MANAGED,
|
||||
MULTIUSER_MODE_USER
|
||||
};
|
||||
|
||||
// Values for mnt_ns
|
||||
enum {
|
||||
NAMESPACE_MODE_GLOBAL = 0,
|
||||
NAMESPACE_MODE_REQUESTER,
|
||||
NAMESPACE_MODE_ISOLATE
|
||||
};
|
||||
|
||||
class db_settings : public db_dict<int, std::size(DB_SETTING_KEYS)> {
|
||||
public:
|
||||
db_settings();
|
||||
protected:
|
||||
int get_idx(std::string_view key) const override;
|
||||
};
|
||||
|
||||
/**************
|
||||
* DB Strings *
|
||||
**************/
|
||||
|
||||
constexpr const char *DB_STRING_KEYS[] = { "requester" };
|
||||
|
||||
// Strings keys indices
|
||||
enum {
|
||||
SU_MANAGER = 0
|
||||
};
|
||||
|
||||
class db_strings : public db_dict<std::string, std::size(DB_STRING_KEYS)> {
|
||||
protected:
|
||||
int get_idx(std::string_view key) const override;
|
||||
};
|
||||
|
||||
/*************
|
||||
* SU Access *
|
||||
*************/
|
||||
|
||||
typedef enum {
|
||||
QUERY = 0,
|
||||
DENY = 1,
|
||||
ALLOW = 2,
|
||||
} policy_t;
|
||||
|
||||
struct su_access {
|
||||
policy_t policy;
|
||||
int log;
|
||||
int notify;
|
||||
};
|
||||
|
||||
#define DEFAULT_SU_ACCESS { QUERY, 1, 1 }
|
||||
#define SILENT_SU_ACCESS { ALLOW, 0, 0 }
|
||||
#define NO_SU_ACCESS { DENY, 0, 0 }
|
||||
|
||||
/********************
|
||||
* Public Functions *
|
||||
********************/
|
||||
|
||||
using db_row = std::map<std::string_view, std::string_view>;
|
||||
using db_row_cb = std::function<bool(db_row&)>;
|
||||
|
||||
int get_db_settings(db_settings &cfg, int key = -1);
|
||||
int get_db_strings(db_strings &str, int key = -1);
|
||||
void rm_db_strings(int key);
|
||||
void exec_sql(int client);
|
||||
char *db_exec(const char *sql);
|
||||
char *db_exec(const char *sql, const db_row_cb &fn);
|
||||
bool db_err(char *e);
|
||||
|
||||
#define db_err_cmd(e, cmd) if (db_err(e)) { cmd; }
|
||||
@@ -4,9 +4,6 @@
|
||||
#include <map>
|
||||
#include <cxx.h>
|
||||
|
||||
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
||||
#include <api/_system_properties.h>
|
||||
|
||||
struct prop_cb {
|
||||
virtual void exec(const char *name, const char *value) = 0;
|
||||
};
|
||||
16
native/src/core/include/selinux.hpp
Normal file
16
native/src/core/include/selinux.hpp
Normal file
@@ -0,0 +1,16 @@
|
||||
#pragma once
|
||||
|
||||
#include <base.hpp>
|
||||
|
||||
int setcon(const char *con);
|
||||
int getfilecon(const char *path, byte_data con);
|
||||
int lgetfilecon(const char *path, byte_data con);
|
||||
int fgetfilecon(int fd, byte_data con);
|
||||
int setfilecon(const char *path, const char *con);
|
||||
int lsetfilecon(const char *path, const char *con);
|
||||
int fsetfilecon(int fd, const char *con);
|
||||
int getfilecon_at(int dirfd, const char *name, byte_data con);
|
||||
void setfilecon_at(int dirfd, const char *name, const char *con);
|
||||
|
||||
void restorecon();
|
||||
void restore_tmpcon();
|
||||
38
native/src/core/include/socket.hpp
Normal file
38
native/src/core/include/socket.hpp
Normal file
@@ -0,0 +1,38 @@
|
||||
#pragma once
|
||||
|
||||
#include <sys/un.h>
|
||||
#include <sys/socket.h>
|
||||
#include <string_view>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
struct sock_cred : public ucred {
|
||||
std::string context;
|
||||
};
|
||||
|
||||
bool get_client_cred(int fd, sock_cred *cred);
|
||||
std::vector<int> recv_fds(int sockfd);
|
||||
int recv_fd(int sockfd);
|
||||
int send_fds(int sockfd, const int *fds, int cnt);
|
||||
int send_fd(int sockfd, int fd);
|
||||
int read_int(int fd);
|
||||
int read_int_be(int fd);
|
||||
void write_int(int fd, int val);
|
||||
void write_int_be(int fd, int val);
|
||||
std::string read_string(int fd);
|
||||
bool read_string(int fd, std::string &str);
|
||||
void write_string(int fd, std::string_view str);
|
||||
|
||||
template<typename T> requires(std::is_trivially_copyable_v<T>)
|
||||
void write_vector(int fd, const std::vector<T> &vec) {
|
||||
write_int(fd, static_cast<int>(vec.size()));
|
||||
xwrite(fd, vec.data(), vec.size() * sizeof(T));
|
||||
}
|
||||
|
||||
template<typename T> requires(std::is_trivially_copyable_v<T>)
|
||||
bool read_vector(int fd, std::vector<T> &vec) {
|
||||
int size = read_int(fd);
|
||||
if (size == -1) return false;
|
||||
vec.resize(size);
|
||||
return xread(fd, vec.data(), size * sizeof(T)) == size * sizeof(T);
|
||||
}
|
||||
@@ -19,7 +19,7 @@ mod resetprop;
|
||||
#[cxx::bridge]
|
||||
pub mod ffi {
|
||||
extern "C++" {
|
||||
include!("resetprop/resetprop.hpp");
|
||||
include!("include/resetprop.hpp");
|
||||
|
||||
#[cxx_name = "prop_cb"]
|
||||
type PropCb;
|
||||
|
||||
@@ -2,13 +2,11 @@
|
||||
#include <libgen.h>
|
||||
|
||||
#include <base.hpp>
|
||||
#include <magisk.hpp>
|
||||
#include <daemon.hpp>
|
||||
#include <consts.hpp>
|
||||
#include <core.hpp>
|
||||
#include <selinux.hpp>
|
||||
#include <flags.h>
|
||||
|
||||
#include "core.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
[[noreturn]] static void usage() {
|
||||
|
||||
@@ -5,11 +5,10 @@
|
||||
#include <utility>
|
||||
|
||||
#include <base.hpp>
|
||||
#include <magisk.hpp>
|
||||
#include <daemon.hpp>
|
||||
#include <consts.hpp>
|
||||
#include <core.hpp>
|
||||
#include <selinux.hpp>
|
||||
|
||||
#include "core.hpp"
|
||||
#include "node.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
#include <base.hpp>
|
||||
#include <magisk.hpp>
|
||||
#include <daemon.hpp>
|
||||
#include <consts.hpp>
|
||||
#include <core.hpp>
|
||||
#include <db.hpp>
|
||||
#include <flags.h>
|
||||
|
||||
#include "core.hpp"
|
||||
|
||||
using namespace std;
|
||||
using rust::Vec;
|
||||
|
||||
|
||||
@@ -4,9 +4,11 @@
|
||||
#include <map>
|
||||
|
||||
#include <base.hpp>
|
||||
#include <core.hpp>
|
||||
#include <resetprop.hpp>
|
||||
|
||||
#include "resetprop.hpp"
|
||||
#include "../core-rs.hpp"
|
||||
#define _REALLY_INCLUDE_SYS__SYSTEM_PROPERTIES_H_
|
||||
#include <api/_system_properties.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
@@ -2,12 +2,10 @@
|
||||
#include <vector>
|
||||
#include <sys/wait.h>
|
||||
|
||||
#include <magisk.hpp>
|
||||
#include <consts.hpp>
|
||||
#include <base.hpp>
|
||||
#include <selinux.hpp>
|
||||
#include <daemon.hpp>
|
||||
|
||||
#include "core.hpp"
|
||||
#include <core.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
#include <sys/syscall.h>
|
||||
#include <sys/xattr.h>
|
||||
|
||||
#include <magisk.hpp>
|
||||
#include <consts.hpp>
|
||||
#include <base.hpp>
|
||||
#include <selinux.hpp>
|
||||
#include <daemon.hpp>
|
||||
#include <core.hpp>
|
||||
#include <flags.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
@@ -3,7 +3,7 @@
|
||||
|
||||
#include <base.hpp>
|
||||
#include <selinux.hpp>
|
||||
#include <magisk.hpp>
|
||||
#include <consts.hpp>
|
||||
|
||||
#include "su.hpp"
|
||||
|
||||
|
||||
@@ -13,7 +13,7 @@
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
|
||||
#include <magisk.hpp>
|
||||
#include <consts.hpp>
|
||||
#include <base.hpp>
|
||||
#include <flags.h>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <memory>
|
||||
|
||||
#include <db.hpp>
|
||||
#include <daemon.hpp>
|
||||
#include <core.hpp>
|
||||
|
||||
#define DEFAULT_SHELL "/system/bin/sh"
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <sys/wait.h>
|
||||
#include <sys/mount.h>
|
||||
|
||||
#include <magisk.hpp>
|
||||
#include <consts.hpp>
|
||||
#include <base.hpp>
|
||||
#include <selinux.hpp>
|
||||
|
||||
|
||||
@@ -2,7 +2,7 @@
|
||||
|
||||
#include <base.hpp>
|
||||
|
||||
#include <daemon.hpp>
|
||||
#include <core.hpp>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
395
native/src/core/zygisk/api.hpp
Normal file
395
native/src/core/zygisk/api.hpp
Normal file
@@ -0,0 +1,395 @@
|
||||
/* Copyright 2022-2023 John "topjohnwu" Wu
|
||||
*
|
||||
* Permission to use, copy, modify, and/or distribute this software for any
|
||||
* purpose with or without fee is hereby granted.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
|
||||
* REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
|
||||
* AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
|
||||
* INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
|
||||
* LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
|
||||
* OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
|
||||
* PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
// This is the public API for Zygisk modules.
|
||||
// DO NOT MODIFY ANY CODE IN THIS HEADER.
|
||||
|
||||
// WARNING: this file may contain changes that are not finalized.
|
||||
// Always use the following published header for development:
|
||||
// https://github.com/topjohnwu/zygisk-module-sample/blob/master/module/jni/zygisk.hpp
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#define ZYGISK_API_VERSION 4
|
||||
|
||||
/*
|
||||
|
||||
***************
|
||||
* Introduction
|
||||
***************
|
||||
|
||||
On Android, all app processes are forked from a special daemon called "Zygote".
|
||||
For each new app process, zygote will fork a new process and perform "specialization".
|
||||
This specialization operation enforces the Android security sandbox on the newly forked
|
||||
process to make sure that 3rd party application code is only loaded after it is being
|
||||
restricted within a sandbox.
|
||||
|
||||
On Android, there is also this special process called "system_server". This single
|
||||
process hosts a significant portion of system services, which controls how the
|
||||
Android operating system and apps interact with each other.
|
||||
|
||||
The Zygisk framework provides a way to allow developers to build modules and run custom
|
||||
code before and after system_server and any app processes' specialization.
|
||||
This enable developers to inject code and alter the behavior of system_server and app processes.
|
||||
|
||||
Please note that modules will only be loaded after zygote has forked the child process.
|
||||
THIS MEANS ALL OF YOUR CODE RUNS IN THE APP/SYSTEM_SERVER PROCESS, NOT THE ZYGOTE DAEMON!
|
||||
|
||||
*********************
|
||||
* Development Guide
|
||||
*********************
|
||||
|
||||
Define a class and inherit zygisk::ModuleBase to implement the functionality of your module.
|
||||
Use the macro REGISTER_ZYGISK_MODULE(className) to register that class to Zygisk.
|
||||
|
||||
Example code:
|
||||
|
||||
static jint (*orig_logger_entry_max)(JNIEnv *env);
|
||||
static jint my_logger_entry_max(JNIEnv *env) { return orig_logger_entry_max(env); }
|
||||
|
||||
class ExampleModule : public zygisk::ModuleBase {
|
||||
public:
|
||||
void onLoad(zygisk::Api *api, JNIEnv *env) override {
|
||||
this->api = api;
|
||||
this->env = env;
|
||||
}
|
||||
void preAppSpecialize(zygisk::AppSpecializeArgs *args) override {
|
||||
JNINativeMethod methods[] = {
|
||||
{ "logger_entry_max_payload_native", "()I", (void*) my_logger_entry_max },
|
||||
};
|
||||
api->hookJniNativeMethods(env, "android/util/Log", methods, 1);
|
||||
*(void **) &orig_logger_entry_max = methods[0].fnPtr;
|
||||
}
|
||||
private:
|
||||
zygisk::Api *api;
|
||||
JNIEnv *env;
|
||||
};
|
||||
|
||||
REGISTER_ZYGISK_MODULE(ExampleModule)
|
||||
|
||||
-----------------------------------------------------------------------------------------
|
||||
|
||||
Since your module class's code runs with either Zygote's privilege in pre[XXX]Specialize,
|
||||
or runs in the sandbox of the target process in post[XXX]Specialize, the code in your class
|
||||
never runs in a true superuser environment.
|
||||
|
||||
If your module require access to superuser permissions, you can create and register
|
||||
a root companion handler function. This function runs in a separate root companion
|
||||
daemon process, and an Unix domain socket is provided to allow you to perform IPC between
|
||||
your target process and the root companion process.
|
||||
|
||||
Example code:
|
||||
|
||||
static void example_handler(int socket) { ... }
|
||||
|
||||
REGISTER_ZYGISK_COMPANION(example_handler)
|
||||
|
||||
*/
|
||||
|
||||
namespace zygisk {
|
||||
|
||||
struct Api;
|
||||
struct AppSpecializeArgs;
|
||||
struct ServerSpecializeArgs;
|
||||
|
||||
class ModuleBase {
|
||||
public:
|
||||
|
||||
// This method is called as soon as the module is loaded into the target process.
|
||||
// A Zygisk API handle will be passed as an argument.
|
||||
virtual void onLoad([[maybe_unused]] Api *api, [[maybe_unused]] JNIEnv *env) {}
|
||||
|
||||
// This method is called before the app process is specialized.
|
||||
// At this point, the process just got forked from zygote, but no app specific specialization
|
||||
// is applied. This means that the process does not have any sandbox restrictions and
|
||||
// still runs with the same privilege of zygote.
|
||||
//
|
||||
// All the arguments that will be sent and used for app specialization is passed as a single
|
||||
// AppSpecializeArgs object. You can read and overwrite these arguments to change how the app
|
||||
// process will be specialized.
|
||||
//
|
||||
// If you need to run some operations as superuser, you can call Api::connectCompanion() to
|
||||
// get a socket to do IPC calls with a root companion process.
|
||||
// See Api::connectCompanion() for more info.
|
||||
virtual void preAppSpecialize([[maybe_unused]] AppSpecializeArgs *args) {}
|
||||
|
||||
// This method is called after the app process is specialized.
|
||||
// At this point, the process has all sandbox restrictions enabled for this application.
|
||||
// This means that this method runs with the same privilege of the app's own code.
|
||||
virtual void postAppSpecialize([[maybe_unused]] const AppSpecializeArgs *args) {}
|
||||
|
||||
// This method is called before the system server process is specialized.
|
||||
// See preAppSpecialize(args) for more info.
|
||||
virtual void preServerSpecialize([[maybe_unused]] ServerSpecializeArgs *args) {}
|
||||
|
||||
// This method is called after the system server process is specialized.
|
||||
// At this point, the process runs with the privilege of system_server.
|
||||
virtual void postServerSpecialize([[maybe_unused]] const ServerSpecializeArgs *args) {}
|
||||
};
|
||||
|
||||
struct AppSpecializeArgs {
|
||||
// Required arguments. These arguments are guaranteed to exist on all Android versions.
|
||||
jint &uid;
|
||||
jint &gid;
|
||||
jintArray &gids;
|
||||
jint &runtime_flags;
|
||||
jobjectArray &rlimits;
|
||||
jint &mount_external;
|
||||
jstring &se_info;
|
||||
jstring &nice_name;
|
||||
jstring &instruction_set;
|
||||
jstring &app_data_dir;
|
||||
|
||||
// Optional arguments. Please check whether the pointer is null before de-referencing
|
||||
jintArray *const fds_to_ignore;
|
||||
jboolean *const is_child_zygote;
|
||||
jboolean *const is_top_app;
|
||||
jobjectArray *const pkg_data_info_list;
|
||||
jobjectArray *const whitelisted_data_info_list;
|
||||
jboolean *const mount_data_dirs;
|
||||
jboolean *const mount_storage_dirs;
|
||||
|
||||
AppSpecializeArgs() = delete;
|
||||
};
|
||||
|
||||
struct ServerSpecializeArgs {
|
||||
jint &uid;
|
||||
jint &gid;
|
||||
jintArray &gids;
|
||||
jint &runtime_flags;
|
||||
jlong &permitted_capabilities;
|
||||
jlong &effective_capabilities;
|
||||
|
||||
ServerSpecializeArgs() = delete;
|
||||
};
|
||||
|
||||
namespace internal {
|
||||
struct api_table;
|
||||
template <class T> void entry_impl(api_table *, JNIEnv *);
|
||||
}
|
||||
|
||||
// These values are used in Api::setOption(Option)
|
||||
enum Option : int {
|
||||
// Force Magisk's denylist unmount routines to run on this process.
|
||||
//
|
||||
// Setting this option only makes sense in preAppSpecialize.
|
||||
// The actual unmounting happens during app process specialization.
|
||||
//
|
||||
// Set this option to force all Magisk and modules' files to be unmounted from the
|
||||
// mount namespace of the process, regardless of the denylist enforcement status.
|
||||
FORCE_DENYLIST_UNMOUNT = 0,
|
||||
|
||||
// When this option is set, your module's library will be dlclose-ed after post[XXX]Specialize.
|
||||
// Be aware that after dlclose-ing your module, all of your code will be unmapped from memory.
|
||||
// YOU MUST NOT ENABLE THIS OPTION AFTER HOOKING ANY FUNCTIONS IN THE PROCESS.
|
||||
DLCLOSE_MODULE_LIBRARY = 1,
|
||||
};
|
||||
|
||||
// Bit masks of the return value of Api::getFlags()
|
||||
enum StateFlag : uint32_t {
|
||||
// The user has granted root access to the current process
|
||||
PROCESS_GRANTED_ROOT = (1u << 0),
|
||||
|
||||
// The current process was added on the denylist
|
||||
PROCESS_ON_DENYLIST = (1u << 1),
|
||||
};
|
||||
|
||||
// All API methods will stop working after post[XXX]Specialize as Zygisk will be unloaded
|
||||
// from the specialized process afterwards.
|
||||
struct Api {
|
||||
|
||||
// Connect to a root companion process and get a Unix domain socket for IPC.
|
||||
//
|
||||
// This API only works in the pre[XXX]Specialize methods due to SELinux restrictions.
|
||||
//
|
||||
// The pre[XXX]Specialize methods run with the same privilege of zygote.
|
||||
// If you would like to do some operations with superuser permissions, register a handler
|
||||
// function that would be called in the root process with REGISTER_ZYGISK_COMPANION(func).
|
||||
// Another good use case for a companion process is that if you want to share some resources
|
||||
// across multiple processes, hold the resources in the companion process and pass it over.
|
||||
//
|
||||
// The root companion process is ABI aware; that is, when calling this method from a 32-bit
|
||||
// process, you will be connected to a 32-bit companion process, and vice versa for 64-bit.
|
||||
//
|
||||
// Returns a file descriptor to a socket that is connected to the socket passed to your
|
||||
// module's companion request handler. Returns -1 if the connection attempt failed.
|
||||
int connectCompanion();
|
||||
|
||||
// Get the file descriptor of the root folder of the current module.
|
||||
//
|
||||
// This API only works in the pre[XXX]Specialize methods.
|
||||
// Accessing the directory returned is only possible in the pre[XXX]Specialize methods
|
||||
// or in the root companion process (assuming that you sent the fd over the socket).
|
||||
// Both restrictions are due to SELinux and UID.
|
||||
//
|
||||
// Returns -1 if errors occurred.
|
||||
int getModuleDir();
|
||||
|
||||
// Set various options for your module.
|
||||
// Please note that this method accepts one single option at a time.
|
||||
// Check zygisk::Option for the full list of options available.
|
||||
void setOption(Option opt);
|
||||
|
||||
// Get information about the current process.
|
||||
// Returns bitwise-or'd zygisk::StateFlag values.
|
||||
uint32_t getFlags();
|
||||
|
||||
// Exempt the provided file descriptor from being automatically closed.
|
||||
//
|
||||
// This API only make sense in preAppSpecialize; calling this method in any other situation
|
||||
// is either a no-op (returns true) or an error (returns false).
|
||||
//
|
||||
// When false is returned, the provided file descriptor will eventually be closed by zygote.
|
||||
bool exemptFd(int fd);
|
||||
|
||||
// Hook JNI native methods for a class
|
||||
//
|
||||
// Lookup all registered JNI native methods and replace it with your own methods.
|
||||
// The original function pointer will be saved in each JNINativeMethod's fnPtr.
|
||||
// If no matching class, method name, or signature is found, that specific JNINativeMethod.fnPtr
|
||||
// will be set to nullptr.
|
||||
void hookJniNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods, int numMethods);
|
||||
|
||||
// Hook functions in the PLT (Procedure Linkage Table) of ELFs loaded in memory.
|
||||
//
|
||||
// Parsing /proc/[PID]/maps will give you the memory map of a process. As an example:
|
||||
//
|
||||
// <address> <perms> <offset> <dev> <inode> <pathname>
|
||||
// 56b4346000-56b4347000 r-xp 00002000 fe:00 235 /system/bin/app_process64
|
||||
// (More details: https://man7.org/linux/man-pages/man5/proc.5.html)
|
||||
//
|
||||
// The `dev` and `inode` pair uniquely identifies a file being mapped into memory.
|
||||
// For matching ELFs loaded in memory, replace function `symbol` with `newFunc`.
|
||||
// If `oldFunc` is not nullptr, the original function pointer will be saved to `oldFunc`.
|
||||
void pltHookRegister(dev_t dev, ino_t inode, const char *symbol, void *newFunc, void **oldFunc);
|
||||
|
||||
// Commit all the hooks that was previously registered.
|
||||
// Returns false if an error occurred.
|
||||
bool pltHookCommit();
|
||||
|
||||
private:
|
||||
internal::api_table *tbl;
|
||||
template <class T> friend void internal::entry_impl(internal::api_table *, JNIEnv *);
|
||||
};
|
||||
|
||||
// Register a class as a Zygisk module
|
||||
|
||||
#define REGISTER_ZYGISK_MODULE(clazz) \
|
||||
void zygisk_module_entry(zygisk::internal::api_table *table, JNIEnv *env) { \
|
||||
zygisk::internal::entry_impl<clazz>(table, env); \
|
||||
}
|
||||
|
||||
// Register a root companion request handler function for your module
|
||||
//
|
||||
// The function runs in a superuser daemon process and handles a root companion request from
|
||||
// your module running in a target process. The function has to accept an integer value,
|
||||
// which is a Unix domain socket that is connected to the target process.
|
||||
// See Api::connectCompanion() for more info.
|
||||
//
|
||||
// NOTE: the function can run concurrently on multiple threads.
|
||||
// Be aware of race conditions if you have globally shared resources.
|
||||
|
||||
#define REGISTER_ZYGISK_COMPANION(func) \
|
||||
void zygisk_companion_entry(int client) { func(client); }
|
||||
|
||||
/*********************************************************
|
||||
* The following is internal ABI implementation detail.
|
||||
* You do not have to understand what it is doing.
|
||||
*********************************************************/
|
||||
|
||||
namespace internal {
|
||||
|
||||
struct module_abi {
|
||||
long api_version;
|
||||
ModuleBase *impl;
|
||||
|
||||
void (*preAppSpecialize)(ModuleBase *, AppSpecializeArgs *);
|
||||
void (*postAppSpecialize)(ModuleBase *, const AppSpecializeArgs *);
|
||||
void (*preServerSpecialize)(ModuleBase *, ServerSpecializeArgs *);
|
||||
void (*postServerSpecialize)(ModuleBase *, const ServerSpecializeArgs *);
|
||||
|
||||
module_abi(ModuleBase *module) : api_version(ZYGISK_API_VERSION), impl(module) {
|
||||
preAppSpecialize = [](auto m, auto args) { m->preAppSpecialize(args); };
|
||||
postAppSpecialize = [](auto m, auto args) { m->postAppSpecialize(args); };
|
||||
preServerSpecialize = [](auto m, auto args) { m->preServerSpecialize(args); };
|
||||
postServerSpecialize = [](auto m, auto args) { m->postServerSpecialize(args); };
|
||||
}
|
||||
};
|
||||
|
||||
struct api_table {
|
||||
// Base
|
||||
void *impl;
|
||||
bool (*registerModule)(api_table *, module_abi *);
|
||||
|
||||
void (*hookJniNativeMethods)(JNIEnv *, const char *, JNINativeMethod *, int);
|
||||
void (*pltHookRegister)(dev_t, ino_t, const char *, void *, void **);
|
||||
bool (*exemptFd)(int);
|
||||
bool (*pltHookCommit)();
|
||||
int (*connectCompanion)(void * /* impl */);
|
||||
void (*setOption)(void * /* impl */, Option);
|
||||
int (*getModuleDir)(void * /* impl */);
|
||||
uint32_t (*getFlags)(void * /* impl */);
|
||||
};
|
||||
|
||||
template <class T>
|
||||
void entry_impl(api_table *table, JNIEnv *env) {
|
||||
static Api api;
|
||||
api.tbl = table;
|
||||
static T module;
|
||||
ModuleBase *m = &module;
|
||||
static module_abi abi(m);
|
||||
if (!table->registerModule(table, &abi)) return;
|
||||
m->onLoad(&api, env);
|
||||
}
|
||||
|
||||
} // namespace internal
|
||||
|
||||
inline int Api::connectCompanion() {
|
||||
return tbl->connectCompanion ? tbl->connectCompanion(tbl->impl) : -1;
|
||||
}
|
||||
inline int Api::getModuleDir() {
|
||||
return tbl->getModuleDir ? tbl->getModuleDir(tbl->impl) : -1;
|
||||
}
|
||||
inline void Api::setOption(Option opt) {
|
||||
if (tbl->setOption) tbl->setOption(tbl->impl, opt);
|
||||
}
|
||||
inline uint32_t Api::getFlags() {
|
||||
return tbl->getFlags ? tbl->getFlags(tbl->impl) : 0;
|
||||
}
|
||||
inline bool Api::exemptFd(int fd) {
|
||||
return tbl->exemptFd != nullptr && tbl->exemptFd(fd);
|
||||
}
|
||||
inline void Api::hookJniNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods, int numMethods) {
|
||||
if (tbl->hookJniNativeMethods) tbl->hookJniNativeMethods(env, className, methods, numMethods);
|
||||
}
|
||||
inline void Api::pltHookRegister(dev_t dev, ino_t inode, const char *symbol, void *newFunc, void **oldFunc) {
|
||||
if (tbl->pltHookRegister) tbl->pltHookRegister(dev, inode, symbol, newFunc, oldFunc);
|
||||
}
|
||||
inline bool Api::pltHookCommit() {
|
||||
return tbl->pltHookCommit != nullptr && tbl->pltHookCommit();
|
||||
}
|
||||
|
||||
} // namespace zygisk
|
||||
|
||||
extern "C" {
|
||||
|
||||
[[gnu::visibility("default"), maybe_unused]]
|
||||
void zygisk_module_entry(zygisk::internal::api_table *, JNIEnv *);
|
||||
|
||||
[[gnu::visibility("default"), maybe_unused]]
|
||||
void zygisk_companion_entry(int);
|
||||
|
||||
} // extern "C"
|
||||
205
native/src/core/zygisk/entry.cpp
Normal file
205
native/src/core/zygisk/entry.cpp
Normal file
@@ -0,0 +1,205 @@
|
||||
#include <libgen.h>
|
||||
#include <dlfcn.h>
|
||||
#include <sys/prctl.h>
|
||||
#include <sys/mount.h>
|
||||
#include <android/log.h>
|
||||
#include <android/dlext.h>
|
||||
|
||||
#include <base.hpp>
|
||||
#include <consts.hpp>
|
||||
|
||||
#include "zygisk.hpp"
|
||||
#include "module.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
void *self_handle = nullptr;
|
||||
|
||||
extern "C" [[maybe_unused]] void zygisk_inject_entry(void *handle) {
|
||||
self_handle = handle;
|
||||
zygisk_logging();
|
||||
hook_functions();
|
||||
ZLOGD("load success\n");
|
||||
}
|
||||
|
||||
// The following code runs in zygote/app process
|
||||
|
||||
static inline bool should_load_modules(uint32_t flags) {
|
||||
return (flags & UNMOUNT_MASK) != UNMOUNT_MASK &&
|
||||
(flags & PROCESS_IS_MAGISK_APP) != PROCESS_IS_MAGISK_APP;
|
||||
}
|
||||
|
||||
int remote_get_info(int uid, const char *process, uint32_t *flags, vector<int> &fds) {
|
||||
if (int fd = zygisk_request(ZygiskRequest::GET_INFO); fd >= 0) {
|
||||
write_int(fd, uid);
|
||||
write_string(fd, process);
|
||||
xxread(fd, flags, sizeof(*flags));
|
||||
if (should_load_modules(*flags)) {
|
||||
fds = recv_fds(fd);
|
||||
}
|
||||
return fd;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
// The following code runs in magiskd
|
||||
|
||||
static vector<int> get_module_fds(bool is_64_bit) {
|
||||
vector<int> fds;
|
||||
// All fds passed to send_fds have to be valid file descriptors.
|
||||
// To workaround this issue, send over STDOUT_FILENO as an indicator of an
|
||||
// invalid fd as it will always be /dev/null in magiskd
|
||||
if (is_64_bit) {
|
||||
#if defined(__LP64__)
|
||||
std::transform(module_list->begin(), module_list->end(), std::back_inserter(fds),
|
||||
[](const module_info &info) { return info.z64 < 0 ? STDOUT_FILENO : info.z64; });
|
||||
#endif
|
||||
} else {
|
||||
std::transform(module_list->begin(), module_list->end(), std::back_inserter(fds),
|
||||
[](const module_info &info) { return info.z32 < 0 ? STDOUT_FILENO : info.z32; });
|
||||
}
|
||||
return fds;
|
||||
}
|
||||
|
||||
static bool get_exe(int pid, char *buf, size_t sz) {
|
||||
char exe[128];
|
||||
if (ssprintf(exe, sizeof(exe), "/proc/%d/exe", pid) < 0)
|
||||
return false;
|
||||
return xreadlink(exe, buf, sz) > 0;
|
||||
}
|
||||
|
||||
static pthread_mutex_t zygiskd_lock = PTHREAD_MUTEX_INITIALIZER;
|
||||
static int zygiskd_sockets[] = { -1, -1 };
|
||||
#define zygiskd_socket zygiskd_sockets[is_64_bit]
|
||||
|
||||
static void connect_companion(int client, bool is_64_bit) {
|
||||
mutex_guard g(zygiskd_lock);
|
||||
|
||||
if (zygiskd_socket >= 0) {
|
||||
// Make sure the socket is still valid
|
||||
pollfd pfd = { zygiskd_socket, 0, 0 };
|
||||
poll(&pfd, 1, 0);
|
||||
if (pfd.revents) {
|
||||
// Any revent means error
|
||||
close(zygiskd_socket);
|
||||
zygiskd_socket = -1;
|
||||
}
|
||||
}
|
||||
if (zygiskd_socket < 0) {
|
||||
int fds[2];
|
||||
socketpair(AF_UNIX, SOCK_STREAM | SOCK_CLOEXEC, 0, fds);
|
||||
zygiskd_socket = fds[0];
|
||||
if (fork_dont_care() == 0) {
|
||||
char exe[64];
|
||||
ssprintf(exe, sizeof(exe), "%s/magisk%s", get_magisk_tmp(), (is_64_bit ? "64" : "32"));
|
||||
// This fd has to survive exec
|
||||
fcntl(fds[1], F_SETFD, 0);
|
||||
char buf[16];
|
||||
ssprintf(buf, sizeof(buf), "%d", fds[1]);
|
||||
execl(exe, "", "zygisk", "companion", buf, (char *) nullptr);
|
||||
exit(-1);
|
||||
}
|
||||
close(fds[1]);
|
||||
vector<int> module_fds = get_module_fds(is_64_bit);
|
||||
send_fds(zygiskd_socket, module_fds.data(), module_fds.size());
|
||||
// Wait for ack
|
||||
if (read_int(zygiskd_socket) != 0) {
|
||||
LOGE("zygiskd startup error\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
send_fd(zygiskd_socket, client);
|
||||
}
|
||||
|
||||
extern bool uid_granted_root(int uid);
|
||||
static void get_process_info(int client, const sock_cred *cred) {
|
||||
int uid = read_int(client);
|
||||
string process = read_string(client);
|
||||
|
||||
uint32_t flags = 0;
|
||||
|
||||
check_pkg_refresh();
|
||||
if (is_deny_target(uid, process)) {
|
||||
flags |= PROCESS_ON_DENYLIST;
|
||||
}
|
||||
int manager_app_id = get_manager();
|
||||
if (to_app_id(uid) == manager_app_id) {
|
||||
flags |= PROCESS_IS_MAGISK_APP;
|
||||
}
|
||||
if (denylist_enforced) {
|
||||
flags |= DENYLIST_ENFORCING;
|
||||
}
|
||||
if (uid_granted_root(uid)) {
|
||||
flags |= PROCESS_GRANTED_ROOT;
|
||||
}
|
||||
|
||||
xwrite(client, &flags, sizeof(flags));
|
||||
|
||||
if (should_load_modules(flags)) {
|
||||
char buf[256];
|
||||
if (!get_exe(cred->pid, buf, sizeof(buf))) {
|
||||
LOGW("zygisk: remote process %d probably died, abort\n", cred->pid);
|
||||
send_fd(client, -1);
|
||||
return;
|
||||
}
|
||||
vector<int> fds = get_module_fds(str_ends(buf, "64"));
|
||||
send_fds(client, fds.data(), fds.size());
|
||||
}
|
||||
|
||||
if (uid != 1000 || process != "system_server")
|
||||
return;
|
||||
|
||||
// Collect module status from system_server
|
||||
int slots = read_int(client);
|
||||
dynamic_bitset bits;
|
||||
for (int i = 0; i < slots; ++i) {
|
||||
dynamic_bitset::slot_type l = 0;
|
||||
xxread(client, &l, sizeof(l));
|
||||
bits.emplace_back(l);
|
||||
}
|
||||
for (int id = 0; id < module_list->size(); ++id) {
|
||||
if (!as_const(bits)[id]) {
|
||||
// Either not a zygisk module, or incompatible
|
||||
char buf[4096];
|
||||
ssprintf(buf, sizeof(buf), MODULEROOT "/%s/zygisk",
|
||||
module_list->operator[](id).name.data());
|
||||
if (int dirfd = open(buf, O_RDONLY | O_CLOEXEC); dirfd >= 0) {
|
||||
close(xopenat(dirfd, "unloaded", O_CREAT | O_RDONLY, 0644));
|
||||
close(dirfd);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
static void get_moddir(int client) {
|
||||
int id = read_int(client);
|
||||
char buf[4096];
|
||||
ssprintf(buf, sizeof(buf), MODULEROOT "/%s", module_list->operator[](id).name.data());
|
||||
int dfd = xopen(buf, O_RDONLY | O_CLOEXEC);
|
||||
send_fd(client, dfd);
|
||||
close(dfd);
|
||||
}
|
||||
|
||||
void zygisk_handler(int client, const sock_cred *cred) {
|
||||
int code = read_int(client);
|
||||
char buf[256];
|
||||
switch (code) {
|
||||
case ZygiskRequest::GET_INFO:
|
||||
get_process_info(client, cred);
|
||||
break;
|
||||
case ZygiskRequest::CONNECT_COMPANION:
|
||||
if (get_exe(cred->pid, buf, sizeof(buf))) {
|
||||
connect_companion(client, str_ends(buf, "64"));
|
||||
} else {
|
||||
LOGW("zygisk: remote process %d probably died, abort\n", cred->pid);
|
||||
}
|
||||
break;
|
||||
case ZygiskRequest::GET_MODDIR:
|
||||
get_moddir(client);
|
||||
break;
|
||||
default:
|
||||
// Unknown code
|
||||
break;
|
||||
}
|
||||
close(client);
|
||||
}
|
||||
246
native/src/core/zygisk/gen_jni_hooks.py
Executable file
246
native/src/core/zygisk/gen_jni_hooks.py
Executable file
@@ -0,0 +1,246 @@
|
||||
#!/usr/bin/env python3
|
||||
|
||||
primitives = ['jint', 'jboolean', 'jlong']
|
||||
|
||||
class JType:
|
||||
def __init__(self, cpp, jni):
|
||||
self.cpp = cpp
|
||||
self.jni = jni
|
||||
|
||||
|
||||
class JArray(JType):
|
||||
def __init__(self, type):
|
||||
if type.cpp in primitives:
|
||||
name = type.cpp + 'Array'
|
||||
else:
|
||||
name = 'jobjectArray'
|
||||
super().__init__(name, '[' + type.jni)
|
||||
|
||||
|
||||
class Argument:
|
||||
def __init__(self, name, type, set_arg = False):
|
||||
self.name = name
|
||||
self.type = type
|
||||
self.set_arg = set_arg
|
||||
|
||||
def cpp(self):
|
||||
return f'{self.type.cpp} {self.name}'
|
||||
|
||||
# Args we don't care, give it an auto generated name
|
||||
class Anon(Argument):
|
||||
cnt = 0
|
||||
def __init__(self, type):
|
||||
super().__init__(f'_{Anon.cnt}', type)
|
||||
Anon.cnt += 1
|
||||
|
||||
class Return:
|
||||
def __init__(self, value, type):
|
||||
self.value = value
|
||||
self.type = type
|
||||
|
||||
class Method:
|
||||
def __init__(self, name, ret, args):
|
||||
self.name = name
|
||||
self.ret = ret
|
||||
self.args = args
|
||||
|
||||
def cpp(self):
|
||||
return ', '.join(map(lambda x: x.cpp(), self.args))
|
||||
|
||||
def name_list(self):
|
||||
return ', '.join(map(lambda x: x.name, self.args))
|
||||
|
||||
def jni(self):
|
||||
args = ''.join(map(lambda x: x.type.jni, self.args))
|
||||
return f'({args}){self.ret.type.jni}'
|
||||
|
||||
def body(self):
|
||||
return ''
|
||||
|
||||
class JNIHook(Method):
|
||||
def __init__(self, ver, ret, args):
|
||||
name = f'{self.base_name()}_{ver}'
|
||||
super().__init__(name, ret, args)
|
||||
|
||||
def base_name(self):
|
||||
return ''
|
||||
|
||||
def orig_method(self):
|
||||
return f'reinterpret_cast<decltype(&{self.name})>({self.base_name()}_orig)'
|
||||
|
||||
def ind(i):
|
||||
return '\n' + ' ' * i
|
||||
|
||||
# Common types
|
||||
jint = JType('jint', 'I')
|
||||
jintArray = JArray(jint)
|
||||
jstring = JType('jstring', 'Ljava/lang/String;')
|
||||
jboolean = JType('jboolean', 'Z')
|
||||
jlong = JType('jlong', 'J')
|
||||
void = JType('void', 'V')
|
||||
|
||||
class ForkAndSpec(JNIHook):
|
||||
def __init__(self, ver, args):
|
||||
super().__init__(ver, Return('ctx.pid', jint), args)
|
||||
|
||||
def base_name(self):
|
||||
return 'nativeForkAndSpecialize'
|
||||
|
||||
def init_args(self):
|
||||
return 'AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);'
|
||||
|
||||
def body(self):
|
||||
decl = ''
|
||||
decl += ind(1) + self.init_args()
|
||||
for a in self.args:
|
||||
if a.set_arg:
|
||||
decl += ind(1) + f'args.{a.name} = &{a.name};'
|
||||
decl += ind(1) + 'HookContext ctx(env, &args);'
|
||||
decl += ind(1) + f'ctx.{self.base_name()}_pre();'
|
||||
decl += ind(1) + self.orig_method() + '('
|
||||
decl += ind(2) + f'env, clazz, {self.name_list()}'
|
||||
decl += ind(1) + ');'
|
||||
decl += ind(1) + f'ctx.{self.base_name()}_post();'
|
||||
return decl
|
||||
|
||||
class SpecApp(ForkAndSpec):
|
||||
def __init__(self, ver, args):
|
||||
super().__init__(ver, args)
|
||||
self.ret = Return('', void)
|
||||
|
||||
def base_name(self):
|
||||
return 'nativeSpecializeAppProcess'
|
||||
|
||||
class ForkServer(ForkAndSpec):
|
||||
def base_name(self):
|
||||
return 'nativeForkSystemServer'
|
||||
|
||||
def init_args(self):
|
||||
return 'ServerSpecializeArgs_v1 args(uid, gid, gids, runtime_flags, permitted_capabilities, effective_capabilities);'
|
||||
|
||||
# Common args
|
||||
uid = Argument('uid', jint)
|
||||
gid = Argument('gid', jint)
|
||||
gids = Argument('gids', jintArray)
|
||||
runtime_flags = Argument('runtime_flags', jint)
|
||||
rlimits = Argument('rlimits', JArray(jintArray))
|
||||
mount_external = Argument('mount_external', jint)
|
||||
se_info = Argument('se_info', jstring)
|
||||
nice_name = Argument('nice_name', jstring)
|
||||
fds_to_close = Argument('fds_to_close', jintArray)
|
||||
instruction_set = Argument('instruction_set', jstring)
|
||||
app_data_dir = Argument('app_data_dir', jstring)
|
||||
|
||||
# o
|
||||
fds_to_ignore = Argument('fds_to_ignore', jintArray, True)
|
||||
|
||||
# p
|
||||
is_child_zygote = Argument('is_child_zygote', jboolean, True)
|
||||
|
||||
# q_alt
|
||||
is_top_app = Argument('is_top_app', jboolean, True)
|
||||
|
||||
# r
|
||||
pkg_data_info_list = Argument('pkg_data_info_list', JArray(jstring), True)
|
||||
whitelisted_data_info_list = Argument('whitelisted_data_info_list', JArray(jstring), True)
|
||||
mount_data_dirs = Argument('mount_data_dirs', jboolean, True)
|
||||
mount_storage_dirs = Argument('mount_storage_dirs', jboolean, True)
|
||||
|
||||
# server
|
||||
permitted_capabilities = Argument('permitted_capabilities', jlong)
|
||||
effective_capabilities = Argument('effective_capabilities', jlong)
|
||||
|
||||
# Method definitions
|
||||
fas_l = ForkAndSpec('l', [uid, gid, gids, runtime_flags, rlimits, mount_external,
|
||||
se_info, nice_name, fds_to_close, instruction_set, app_data_dir])
|
||||
|
||||
fas_o = ForkAndSpec('o', [uid, gid, gids, runtime_flags, rlimits, mount_external,
|
||||
se_info, nice_name, fds_to_close, fds_to_ignore, instruction_set, app_data_dir])
|
||||
|
||||
fas_p = ForkAndSpec('p', [uid, gid, gids, runtime_flags, rlimits, mount_external, se_info,
|
||||
nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir])
|
||||
|
||||
fas_q_alt = ForkAndSpec('q_alt', [uid, gid, gids, runtime_flags, rlimits, mount_external, se_info,
|
||||
nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir, is_top_app])
|
||||
|
||||
fas_r = ForkAndSpec('r', [uid, gid, gids, runtime_flags, rlimits, mount_external, se_info,
|
||||
nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir, is_top_app,
|
||||
pkg_data_info_list, whitelisted_data_info_list, mount_data_dirs, mount_storage_dirs])
|
||||
|
||||
fas_samsung_m = ForkAndSpec('samsung_m', [uid, gid, gids, runtime_flags, rlimits, mount_external,
|
||||
se_info, Anon(jint), Anon(jint), nice_name, fds_to_close, instruction_set, app_data_dir])
|
||||
|
||||
fas_samsung_n = ForkAndSpec('samsung_n', [uid, gid, gids, runtime_flags, rlimits, mount_external,
|
||||
se_info, Anon(jint), Anon(jint), nice_name, fds_to_close, instruction_set, app_data_dir, Anon(jint)])
|
||||
|
||||
fas_samsung_o = ForkAndSpec('samsung_o', [uid, gid, gids, runtime_flags, rlimits, mount_external,
|
||||
se_info, Anon(jint), Anon(jint), nice_name, fds_to_close, fds_to_ignore, instruction_set, app_data_dir])
|
||||
|
||||
fas_samsung_p = ForkAndSpec('samsung_p', [uid, gid, gids, runtime_flags, rlimits, mount_external,
|
||||
se_info, Anon(jint), Anon(jint), nice_name, fds_to_close, fds_to_ignore, is_child_zygote,
|
||||
instruction_set, app_data_dir])
|
||||
|
||||
spec_q = SpecApp('q', [uid, gid, gids, runtime_flags, rlimits, mount_external, se_info,
|
||||
nice_name, is_child_zygote, instruction_set, app_data_dir])
|
||||
|
||||
spec_q_alt = SpecApp('q_alt', [uid, gid, gids, runtime_flags, rlimits, mount_external, se_info,
|
||||
nice_name, is_child_zygote, instruction_set, app_data_dir, is_top_app])
|
||||
|
||||
spec_r = SpecApp('r', [uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name,
|
||||
is_child_zygote, instruction_set, app_data_dir, is_top_app, pkg_data_info_list,
|
||||
whitelisted_data_info_list, mount_data_dirs, mount_storage_dirs])
|
||||
|
||||
spec_samsung_q = SpecApp('samsung_q', [uid, gid, gids, runtime_flags, rlimits, mount_external,
|
||||
se_info, Anon(jint), Anon(jint), nice_name, is_child_zygote, instruction_set, app_data_dir])
|
||||
|
||||
server_l = ForkServer('l', [uid, gid, gids, runtime_flags, rlimits,
|
||||
permitted_capabilities, effective_capabilities])
|
||||
|
||||
server_samsung_q = ForkServer('samsung_q', [uid, gid, gids, runtime_flags, Anon(jint), Anon(jint), rlimits,
|
||||
permitted_capabilities, effective_capabilities])
|
||||
|
||||
hook_map = {}
|
||||
|
||||
def gen_jni_def(clz, methods):
|
||||
if clz not in hook_map:
|
||||
hook_map[clz] = []
|
||||
|
||||
decl = ''
|
||||
for m in methods:
|
||||
decl += ind(0) + f'[[clang::no_stack_protector]] {m.ret.type.cpp} {m.name}(JNIEnv *env, jclass clazz, {m.cpp()}) {{'
|
||||
decl += m.body()
|
||||
if m.ret.value:
|
||||
decl += ind(1) + f'return {m.ret.value};'
|
||||
decl += ind(0) + '}'
|
||||
|
||||
decl += ind(0) + f'std::array {m.base_name()}_methods = {{'
|
||||
for m in methods:
|
||||
decl += ind(1) + 'JNINativeMethod {'
|
||||
decl += ind(2) + f'"{m.base_name()}",'
|
||||
decl += ind(2) + f'"{m.jni()}",'
|
||||
decl += ind(2) + f'(void *) &{m.name}'
|
||||
decl += ind(1) + '},'
|
||||
decl += ind(0) + '};'
|
||||
decl = ind(0) + f'void *{m.base_name()}_orig = nullptr;' + decl
|
||||
decl += ind(0)
|
||||
|
||||
hook_map[clz].append(m.base_name())
|
||||
|
||||
return decl
|
||||
|
||||
with open('jni_hooks.hpp', 'w') as f:
|
||||
f.write('// Generated by gen_jni_hooks.py\n')
|
||||
f.write('\nnamespace {\n')
|
||||
|
||||
zygote = 'com/android/internal/os/Zygote'
|
||||
|
||||
methods = [fas_l, fas_o, fas_p, fas_q_alt, fas_r, fas_samsung_m, fas_samsung_n, fas_samsung_o, fas_samsung_p]
|
||||
f.write(gen_jni_def(zygote, methods))
|
||||
|
||||
methods = [spec_q, spec_q_alt, spec_r, spec_samsung_q]
|
||||
f.write(gen_jni_def(zygote, methods))
|
||||
|
||||
methods = [server_l, server_samsung_q]
|
||||
f.write(gen_jni_def(zygote, methods))
|
||||
|
||||
f.write('\n} // namespace\n')
|
||||
990
native/src/core/zygisk/hook.cpp
Normal file
990
native/src/core/zygisk/hook.cpp
Normal file
@@ -0,0 +1,990 @@
|
||||
#include <android/dlext.h>
|
||||
#include <sys/mount.h>
|
||||
#include <dlfcn.h>
|
||||
#include <regex.h>
|
||||
#include <unwind.h>
|
||||
#include <bitset>
|
||||
#include <list>
|
||||
|
||||
#include <lsplt.hpp>
|
||||
|
||||
#include <base.hpp>
|
||||
#include <consts.hpp>
|
||||
|
||||
#include "zygisk.hpp"
|
||||
#include "module.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
// Extreme verbose logging
|
||||
#define ZLOGV(...) ZLOGD(__VA_ARGS__)
|
||||
//#define ZLOGV(...) (void*)0
|
||||
|
||||
static void hook_unloader();
|
||||
static void unhook_functions();
|
||||
static void hook_jni_env();
|
||||
static void reload_native_bridge(const string &nb);
|
||||
|
||||
namespace {
|
||||
|
||||
enum {
|
||||
POST_SPECIALIZE,
|
||||
APP_FORK_AND_SPECIALIZE,
|
||||
APP_SPECIALIZE,
|
||||
SERVER_FORK_AND_SPECIALIZE,
|
||||
DO_REVERT_UNMOUNT,
|
||||
SKIP_CLOSE_LOG_PIPE,
|
||||
|
||||
FLAG_MAX
|
||||
};
|
||||
|
||||
#define MAX_FD_SIZE 1024
|
||||
|
||||
// Global variables
|
||||
vector<tuple<dev_t, ino_t, const char *, void **>> *plt_hook_list;
|
||||
map<string, vector<JNINativeMethod>, StringCmp> *jni_hook_list;
|
||||
bool should_unmap_zygisk = false;
|
||||
|
||||
// Current context
|
||||
HookContext *g_ctx;
|
||||
const JNINativeInterface *old_functions = nullptr;
|
||||
JNINativeInterface *new_functions = nullptr;
|
||||
const NativeBridgeRuntimeCallbacks *runtime_callbacks = nullptr;
|
||||
|
||||
#define DCL_PRE_POST(name) \
|
||||
void name##_pre(); \
|
||||
void name##_post();
|
||||
|
||||
struct HookContext {
|
||||
JNIEnv *env;
|
||||
union {
|
||||
void *ptr;
|
||||
AppSpecializeArgs_v3 *app;
|
||||
ServerSpecializeArgs_v1 *server;
|
||||
} args;
|
||||
|
||||
const char *process;
|
||||
list<ZygiskModule> modules;
|
||||
|
||||
int pid;
|
||||
bitset<FLAG_MAX> flags;
|
||||
uint32_t info_flags;
|
||||
bitset<MAX_FD_SIZE> allowed_fds;
|
||||
vector<int> exempted_fds;
|
||||
|
||||
struct RegisterInfo {
|
||||
regex_t regex;
|
||||
string symbol;
|
||||
void *callback;
|
||||
void **backup;
|
||||
};
|
||||
|
||||
struct IgnoreInfo {
|
||||
regex_t regex;
|
||||
string symbol;
|
||||
};
|
||||
|
||||
pthread_mutex_t hook_info_lock;
|
||||
vector<RegisterInfo> register_info;
|
||||
vector<IgnoreInfo> ignore_info;
|
||||
|
||||
HookContext(JNIEnv *env, void *args) :
|
||||
env(env), args{args}, process(nullptr), pid(-1), info_flags(0),
|
||||
hook_info_lock(PTHREAD_MUTEX_INITIALIZER) { g_ctx = this; }
|
||||
|
||||
~HookContext();
|
||||
|
||||
void run_modules_pre(const vector<int> &fds);
|
||||
void run_modules_post();
|
||||
DCL_PRE_POST(fork)
|
||||
DCL_PRE_POST(app_specialize)
|
||||
DCL_PRE_POST(server_specialize)
|
||||
DCL_PRE_POST(nativeForkAndSpecialize)
|
||||
DCL_PRE_POST(nativeSpecializeAppProcess)
|
||||
DCL_PRE_POST(nativeForkSystemServer)
|
||||
|
||||
void sanitize_fds();
|
||||
bool exempt_fd(int fd);
|
||||
bool is_child() const { return pid <= 0; }
|
||||
bool can_exempt_fd() const { return flags[APP_FORK_AND_SPECIALIZE] && args.app->fds_to_ignore; }
|
||||
|
||||
// Compatibility shim
|
||||
void plt_hook_register(const char *regex, const char *symbol, void *fn, void **backup);
|
||||
void plt_hook_exclude(const char *regex, const char *symbol);
|
||||
void plt_hook_process_regex();
|
||||
|
||||
bool plt_hook_commit();
|
||||
};
|
||||
|
||||
#undef DCL_PRE_POST
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
|
||||
#define DCL_HOOK_FUNC(ret, func, ...) \
|
||||
ret (*old_##func)(__VA_ARGS__); \
|
||||
ret new_##func(__VA_ARGS__)
|
||||
|
||||
DCL_HOOK_FUNC(void, androidSetCreateThreadFunc, void *func) {
|
||||
ZLOGD("androidSetCreateThreadFunc\n");
|
||||
hook_jni_env();
|
||||
old_androidSetCreateThreadFunc(func);
|
||||
}
|
||||
|
||||
// Skip actual fork and return cached result if applicable
|
||||
DCL_HOOK_FUNC(int, fork) {
|
||||
return (g_ctx && g_ctx->pid >= 0) ? g_ctx->pid : old_fork();
|
||||
}
|
||||
|
||||
// Unmount stuffs in the process's private mount namespace
|
||||
DCL_HOOK_FUNC(int, unshare, int flags) {
|
||||
int res = old_unshare(flags);
|
||||
if (g_ctx && (flags & CLONE_NEWNS) != 0 && res == 0 &&
|
||||
// For some unknown reason, unmounting app_process in SysUI can break.
|
||||
// This is reproducible on the official AVD running API 26 and 27.
|
||||
// Simply avoid doing any unmounts for SysUI to avoid potential issues.
|
||||
(g_ctx->info_flags & PROCESS_IS_SYS_UI) == 0) {
|
||||
if (g_ctx->flags[DO_REVERT_UNMOUNT]) {
|
||||
revert_unmount();
|
||||
}
|
||||
// Restore errno back to 0
|
||||
errno = 0;
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
// This is the last moment before the secontext of the process changes
|
||||
DCL_HOOK_FUNC(int, selinux_android_setcontext,
|
||||
uid_t uid, bool isSystemServer, const char *seinfo, const char *pkgname) {
|
||||
// Pre-fetch logd before secontext transition
|
||||
zygisk_get_logd();
|
||||
return old_selinux_android_setcontext(uid, isSystemServer, seinfo, pkgname);
|
||||
}
|
||||
|
||||
// Close file descriptors to prevent crashing
|
||||
DCL_HOOK_FUNC(void, android_log_close) {
|
||||
if (g_ctx == nullptr || !g_ctx->flags[SKIP_CLOSE_LOG_PIPE]) {
|
||||
// This happens during forks like nativeForkApp, nativeForkUsap,
|
||||
// nativeForkSystemServer, and nativeForkAndSpecialize.
|
||||
zygisk_close_logd();
|
||||
}
|
||||
old_android_log_close();
|
||||
}
|
||||
|
||||
// We cannot directly call `dlclose` to unload ourselves, otherwise when `dlclose` returns,
|
||||
// it will return to our code which has been unmapped, causing segmentation fault.
|
||||
// Instead, we hook `pthread_attr_destroy` which will be called when VM daemon threads start.
|
||||
DCL_HOOK_FUNC(int, pthread_attr_destroy, void *target) {
|
||||
int res = old_pthread_attr_destroy((pthread_attr_t *)target);
|
||||
|
||||
// Only perform unloading on the main thread
|
||||
if (gettid() != getpid())
|
||||
return res;
|
||||
|
||||
ZLOGV("pthread_attr_destroy\n");
|
||||
if (should_unmap_zygisk) {
|
||||
unhook_functions();
|
||||
if (should_unmap_zygisk) {
|
||||
// Because both `pthread_attr_destroy` and `dlclose` have the same function signature,
|
||||
// we can use `musttail` to let the compiler reuse our stack frame and thus
|
||||
// `dlclose` will directly return to the caller of `pthread_attr_destroy`.
|
||||
[[clang::musttail]] return dlclose(self_handle);
|
||||
}
|
||||
}
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
// it should be safe to assume all dlclose's in libnativebridge are for zygisk_loader
|
||||
DCL_HOOK_FUNC(int, dlclose, void *handle) {
|
||||
static bool kDone = false;
|
||||
if (!kDone) {
|
||||
ZLOGV("dlclose zygisk_loader\n");
|
||||
kDone = true;
|
||||
reload_native_bridge(get_prop(NBPROP));
|
||||
}
|
||||
[[clang::musttail]] return old_dlclose(handle);
|
||||
}
|
||||
|
||||
#undef DCL_HOOK_FUNC
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
|
||||
void hookJniNativeMethods(JNIEnv *env, const char *clz, JNINativeMethod *methods, int numMethods) {
|
||||
jclass clazz;
|
||||
if (!runtime_callbacks || !env || !clz || !(clazz = env->FindClass(clz))) {
|
||||
for (auto i = 0; i < numMethods; ++i) {
|
||||
methods[i].fnPtr = nullptr;
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
// Backup existing methods
|
||||
auto total = runtime_callbacks->getNativeMethodCount(env, clazz);
|
||||
vector<JNINativeMethod> old_methods(total);
|
||||
runtime_callbacks->getNativeMethods(env, clazz, old_methods.data(), total);
|
||||
|
||||
// Replace the method
|
||||
for (auto i = 0; i < numMethods; ++i) {
|
||||
auto &method = methods[i];
|
||||
auto res = env->RegisterNatives(clazz, &method, 1);
|
||||
// It's normal that the method is not found
|
||||
if (res == JNI_ERR || env->ExceptionCheck()) {
|
||||
auto exception = env->ExceptionOccurred();
|
||||
if (exception) env->DeleteLocalRef(exception);
|
||||
env->ExceptionClear();
|
||||
method.fnPtr = nullptr;
|
||||
} else {
|
||||
// Find the old function pointer and return to caller
|
||||
for (const auto &old_method : old_methods) {
|
||||
if (strcmp(method.name, old_method.name) == 0 &&
|
||||
strcmp(method.signature, old_method.signature) == 0) {
|
||||
ZLOGD("replace %s#%s%s %p -> %p\n", clz,
|
||||
method.name, method.signature, old_method.fnPtr, method.fnPtr);
|
||||
method.fnPtr = old_method.fnPtr;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ZygiskModule::ZygiskModule(int id, void *handle, void *entry)
|
||||
: id(id), handle(handle), entry{entry}, api{}, mod{nullptr} {
|
||||
// Make sure all pointers are null
|
||||
memset(&api, 0, sizeof(api));
|
||||
api.base.impl = this;
|
||||
api.base.registerModule = &ZygiskModule::RegisterModuleImpl;
|
||||
}
|
||||
|
||||
bool ZygiskModule::RegisterModuleImpl(ApiTable *api, long *module) {
|
||||
if (api == nullptr || module == nullptr)
|
||||
return false;
|
||||
|
||||
long api_version = *module;
|
||||
// Unsupported version
|
||||
if (api_version > ZYGISK_API_VERSION)
|
||||
return false;
|
||||
|
||||
// Set the actual module_abi*
|
||||
api->base.impl->mod = { module };
|
||||
|
||||
// Fill in API accordingly with module API version
|
||||
if (api_version >= 1) {
|
||||
api->v1.hookJniNativeMethods = hookJniNativeMethods;
|
||||
api->v1.pltHookRegister = [](auto a, auto b, auto c, auto d) {
|
||||
if (g_ctx) g_ctx->plt_hook_register(a, b, c, d);
|
||||
};
|
||||
api->v1.pltHookExclude = [](auto a, auto b) {
|
||||
if (g_ctx) g_ctx->plt_hook_exclude(a, b);
|
||||
};
|
||||
api->v1.pltHookCommit = []() { return g_ctx && g_ctx->plt_hook_commit(); };
|
||||
api->v1.connectCompanion = [](ZygiskModule *m) { return m->connectCompanion(); };
|
||||
api->v1.setOption = [](ZygiskModule *m, auto opt) { m->setOption(opt); };
|
||||
}
|
||||
if (api_version >= 2) {
|
||||
api->v2.getModuleDir = [](ZygiskModule *m) { return m->getModuleDir(); };
|
||||
api->v2.getFlags = [](auto) { return ZygiskModule::getFlags(); };
|
||||
}
|
||||
if (api_version >= 4) {
|
||||
api->v4.pltHookCommit = lsplt::CommitHook;
|
||||
api->v4.pltHookRegister = [](dev_t dev, ino_t inode, const char *symbol, void *fn, void **backup) {
|
||||
if (dev == 0 || inode == 0 || symbol == nullptr || fn == nullptr)
|
||||
return;
|
||||
lsplt::RegisterHook(dev, inode, symbol, fn, backup);
|
||||
};
|
||||
api->v4.exemptFd = [](int fd) { return g_ctx && g_ctx->exempt_fd(fd); };
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void HookContext::plt_hook_register(const char *regex, const char *symbol, void *fn, void **backup) {
|
||||
if (regex == nullptr || symbol == nullptr || fn == nullptr)
|
||||
return;
|
||||
regex_t re;
|
||||
if (regcomp(&re, regex, REG_NOSUB) != 0)
|
||||
return;
|
||||
mutex_guard lock(hook_info_lock);
|
||||
register_info.emplace_back(RegisterInfo{re, symbol, fn, backup});
|
||||
}
|
||||
|
||||
void HookContext::plt_hook_exclude(const char *regex, const char *symbol) {
|
||||
if (!regex) return;
|
||||
regex_t re;
|
||||
if (regcomp(&re, regex, REG_NOSUB) != 0)
|
||||
return;
|
||||
mutex_guard lock(hook_info_lock);
|
||||
ignore_info.emplace_back(IgnoreInfo{re, symbol ?: ""});
|
||||
}
|
||||
|
||||
void HookContext::plt_hook_process_regex() {
|
||||
if (register_info.empty())
|
||||
return;
|
||||
for (auto &map : lsplt::MapInfo::Scan()) {
|
||||
if (map.offset != 0 || !map.is_private || !(map.perms & PROT_READ)) continue;
|
||||
for (auto ®: register_info) {
|
||||
if (regexec(®.regex, map.path.data(), 0, nullptr, 0) != 0)
|
||||
continue;
|
||||
bool ignored = false;
|
||||
for (auto &ign: ignore_info) {
|
||||
if (regexec(&ign.regex, map.path.data(), 0, nullptr, 0) != 0)
|
||||
continue;
|
||||
if (ign.symbol.empty() || ign.symbol == reg.symbol) {
|
||||
ignored = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!ignored) {
|
||||
lsplt::RegisterHook(map.dev, map.inode, reg.symbol, reg.callback, reg.backup);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
bool HookContext::plt_hook_commit() {
|
||||
{
|
||||
mutex_guard lock(hook_info_lock);
|
||||
plt_hook_process_regex();
|
||||
register_info.clear();
|
||||
ignore_info.clear();
|
||||
}
|
||||
return lsplt::CommitHook();
|
||||
}
|
||||
|
||||
bool ZygiskModule::valid() const {
|
||||
if (mod.api_version == nullptr)
|
||||
return false;
|
||||
switch (*mod.api_version) {
|
||||
case 4:
|
||||
case 3:
|
||||
case 2:
|
||||
case 1:
|
||||
return mod.v1->impl && mod.v1->preAppSpecialize && mod.v1->postAppSpecialize &&
|
||||
mod.v1->preServerSpecialize && mod.v1->postServerSpecialize;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
int ZygiskModule::connectCompanion() const {
|
||||
if (int fd = zygisk_request(ZygiskRequest::CONNECT_COMPANION); fd >= 0) {
|
||||
write_int(fd, id);
|
||||
return fd;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
int ZygiskModule::getModuleDir() const {
|
||||
if (int fd = zygisk_request(ZygiskRequest::GET_MODDIR); fd >= 0) {
|
||||
write_int(fd, id);
|
||||
int dfd = recv_fd(fd);
|
||||
close(fd);
|
||||
return dfd;
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
void ZygiskModule::setOption(zygisk::Option opt) {
|
||||
if (g_ctx == nullptr)
|
||||
return;
|
||||
switch (opt) {
|
||||
case zygisk::FORCE_DENYLIST_UNMOUNT:
|
||||
g_ctx->flags[DO_REVERT_UNMOUNT] = true;
|
||||
break;
|
||||
case zygisk::DLCLOSE_MODULE_LIBRARY:
|
||||
unload = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t ZygiskModule::getFlags() {
|
||||
return g_ctx ? (g_ctx->info_flags & ~PRIVATE_MASK) : 0;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
|
||||
int sigmask(int how, int signum) {
|
||||
sigset_t set;
|
||||
sigemptyset(&set);
|
||||
sigaddset(&set, signum);
|
||||
return sigprocmask(how, &set, nullptr);
|
||||
}
|
||||
|
||||
void HookContext::fork_pre() {
|
||||
// Do our own fork before loading any 3rd party code
|
||||
// First block SIGCHLD, unblock after original fork is done
|
||||
sigmask(SIG_BLOCK, SIGCHLD);
|
||||
pid = old_fork();
|
||||
|
||||
if (!is_child())
|
||||
return;
|
||||
|
||||
// Record all open fds
|
||||
auto dir = xopen_dir("/proc/self/fd");
|
||||
for (dirent *entry; (entry = xreaddir(dir.get()));) {
|
||||
int fd = parse_int(entry->d_name);
|
||||
if (fd < 0 || fd >= MAX_FD_SIZE) {
|
||||
close(fd);
|
||||
continue;
|
||||
}
|
||||
allowed_fds[fd] = true;
|
||||
}
|
||||
// The dirfd will be closed once out of scope
|
||||
allowed_fds[dirfd(dir.get())] = false;
|
||||
// logd_fd should be handled separately
|
||||
if (int fd = zygisk_get_logd(); fd >= 0) {
|
||||
allowed_fds[fd] = false;
|
||||
}
|
||||
}
|
||||
|
||||
void HookContext::fork_post() {
|
||||
// Unblock SIGCHLD in case the original method didn't
|
||||
sigmask(SIG_UNBLOCK, SIGCHLD);
|
||||
}
|
||||
|
||||
void HookContext::sanitize_fds() {
|
||||
zygisk_close_logd();
|
||||
|
||||
if (!is_child()) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (can_exempt_fd() && !exempted_fds.empty()) {
|
||||
auto update_fd_array = [&](int old_len) -> jintArray {
|
||||
jintArray array = env->NewIntArray(static_cast<int>(old_len + exempted_fds.size()));
|
||||
if (array == nullptr)
|
||||
return nullptr;
|
||||
|
||||
env->SetIntArrayRegion(
|
||||
array, old_len, static_cast<int>(exempted_fds.size()), exempted_fds.data());
|
||||
for (int fd : exempted_fds) {
|
||||
if (fd >= 0 && fd < MAX_FD_SIZE) {
|
||||
allowed_fds[fd] = true;
|
||||
}
|
||||
}
|
||||
*args.app->fds_to_ignore = array;
|
||||
return array;
|
||||
};
|
||||
|
||||
if (jintArray fdsToIgnore = *args.app->fds_to_ignore) {
|
||||
int *arr = env->GetIntArrayElements(fdsToIgnore, nullptr);
|
||||
int len = env->GetArrayLength(fdsToIgnore);
|
||||
for (int i = 0; i < len; ++i) {
|
||||
int fd = arr[i];
|
||||
if (fd >= 0 && fd < MAX_FD_SIZE) {
|
||||
allowed_fds[fd] = true;
|
||||
}
|
||||
}
|
||||
if (jintArray newFdList = update_fd_array(len)) {
|
||||
env->SetIntArrayRegion(newFdList, 0, len, arr);
|
||||
}
|
||||
env->ReleaseIntArrayElements(fdsToIgnore, arr, JNI_ABORT);
|
||||
} else {
|
||||
update_fd_array(0);
|
||||
}
|
||||
}
|
||||
|
||||
// Close all forbidden fds to prevent crashing
|
||||
auto dir = xopen_dir("/proc/self/fd");
|
||||
int dfd = dirfd(dir.get());
|
||||
for (dirent *entry; (entry = xreaddir(dir.get()));) {
|
||||
int fd = parse_int(entry->d_name);
|
||||
if ((fd < 0 || fd >= MAX_FD_SIZE || !allowed_fds[fd]) && fd != dfd) {
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HookContext::run_modules_pre(const vector<int> &fds) {
|
||||
for (int i = 0; i < fds.size(); ++i) {
|
||||
struct stat s{};
|
||||
if (fstat(fds[i], &s) != 0 || !S_ISREG(s.st_mode)) {
|
||||
close(fds[i]);
|
||||
continue;
|
||||
}
|
||||
android_dlextinfo info {
|
||||
.flags = ANDROID_DLEXT_USE_LIBRARY_FD,
|
||||
.library_fd = fds[i],
|
||||
};
|
||||
if (void *h = android_dlopen_ext("/jit-cache", RTLD_LAZY, &info)) {
|
||||
if (void *e = dlsym(h, "zygisk_module_entry")) {
|
||||
modules.emplace_back(i, h, e);
|
||||
}
|
||||
} else if (g_ctx->flags[SERVER_FORK_AND_SPECIALIZE]) {
|
||||
ZLOGW("Failed to dlopen zygisk module: %s\n", dlerror());
|
||||
}
|
||||
close(fds[i]);
|
||||
}
|
||||
|
||||
for (auto it = modules.begin(); it != modules.end();) {
|
||||
it->onLoad(env);
|
||||
if (it->valid()) {
|
||||
++it;
|
||||
} else {
|
||||
it = modules.erase(it);
|
||||
}
|
||||
}
|
||||
|
||||
for (auto &m : modules) {
|
||||
if (flags[APP_SPECIALIZE]) {
|
||||
m.preAppSpecialize(args.app);
|
||||
} else if (flags[SERVER_FORK_AND_SPECIALIZE]) {
|
||||
m.preServerSpecialize(args.server);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void HookContext::run_modules_post() {
|
||||
flags[POST_SPECIALIZE] = true;
|
||||
for (const auto &m : modules) {
|
||||
if (flags[APP_SPECIALIZE]) {
|
||||
m.postAppSpecialize(args.app);
|
||||
} else if (flags[SERVER_FORK_AND_SPECIALIZE]) {
|
||||
m.postServerSpecialize(args.server);
|
||||
}
|
||||
m.tryUnload();
|
||||
}
|
||||
}
|
||||
|
||||
void HookContext::app_specialize_pre() {
|
||||
flags[APP_SPECIALIZE] = true;
|
||||
|
||||
vector<int> module_fds;
|
||||
int fd = remote_get_info(args.app->uid, process, &info_flags, module_fds);
|
||||
if (args.app->app_data_dir) {
|
||||
const auto *app_data_dir = env->GetStringUTFChars(args.app->app_data_dir, nullptr);
|
||||
if (std::string_view(app_data_dir).ends_with("/com.android.systemui")) {
|
||||
info_flags |= PROCESS_IS_SYS_UI;
|
||||
}
|
||||
env->ReleaseStringUTFChars(args.app->app_data_dir, app_data_dir);
|
||||
}
|
||||
if ((info_flags & UNMOUNT_MASK) == UNMOUNT_MASK) {
|
||||
ZLOGI("[%s] is on the denylist\n", process);
|
||||
flags[DO_REVERT_UNMOUNT] = true;
|
||||
} else if (fd >= 0) {
|
||||
run_modules_pre(module_fds);
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
|
||||
void HookContext::app_specialize_post() {
|
||||
run_modules_post();
|
||||
if (info_flags & PROCESS_IS_MAGISK_APP) {
|
||||
setenv("ZYGISK_ENABLED", "1", 1);
|
||||
}
|
||||
|
||||
// Cleanups
|
||||
env->ReleaseStringUTFChars(args.app->nice_name, process);
|
||||
}
|
||||
|
||||
void HookContext::server_specialize_pre() {
|
||||
vector<int> module_fds;
|
||||
int fd = remote_get_info(1000, "system_server", &info_flags, module_fds);
|
||||
if (fd >= 0) {
|
||||
if (module_fds.empty()) {
|
||||
write_int(fd, 0);
|
||||
} else {
|
||||
run_modules_pre(module_fds);
|
||||
|
||||
// Send the bitset of module status back to magiskd from system_server
|
||||
dynamic_bitset bits;
|
||||
for (const auto &m : modules)
|
||||
bits[m.getId()] = true;
|
||||
write_int(fd, static_cast<int>(bits.slots()));
|
||||
for (int i = 0; i < bits.slots(); ++i) {
|
||||
auto l = bits.get_slot(i);
|
||||
xwrite(fd, &l, sizeof(l));
|
||||
}
|
||||
}
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
void HookContext::server_specialize_post() {
|
||||
run_modules_post();
|
||||
}
|
||||
|
||||
HookContext::~HookContext() {
|
||||
// This global pointer points to a variable on the stack.
|
||||
// Set this to nullptr to prevent leaking local variable.
|
||||
// This also disables most plt hooked functions.
|
||||
g_ctx = nullptr;
|
||||
|
||||
if (!is_child())
|
||||
return;
|
||||
|
||||
zygisk_close_logd();
|
||||
android_logging();
|
||||
|
||||
should_unmap_zygisk = true;
|
||||
|
||||
// Unhook JNI methods
|
||||
for (const auto &[clz, methods] : *jni_hook_list) {
|
||||
if (!methods.empty() && env->RegisterNatives(
|
||||
env->FindClass(clz.data()), methods.data(),
|
||||
static_cast<int>(methods.size())) != 0) {
|
||||
ZLOGE("Failed to restore JNI hook of class [%s]\n", clz.data());
|
||||
should_unmap_zygisk = false;
|
||||
}
|
||||
}
|
||||
delete jni_hook_list;
|
||||
jni_hook_list = nullptr;
|
||||
|
||||
// Strip out all API function pointers
|
||||
for (auto &m : modules) {
|
||||
m.clearApi();
|
||||
}
|
||||
|
||||
hook_unloader();
|
||||
}
|
||||
|
||||
bool HookContext::exempt_fd(int fd) {
|
||||
if (flags[POST_SPECIALIZE] || flags[SKIP_CLOSE_LOG_PIPE])
|
||||
return true;
|
||||
if (!can_exempt_fd())
|
||||
return false;
|
||||
exempted_fds.push_back(fd);
|
||||
return true;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
|
||||
void HookContext::nativeSpecializeAppProcess_pre() {
|
||||
process = env->GetStringUTFChars(args.app->nice_name, nullptr);
|
||||
ZLOGV("pre specialize [%s]\n", process);
|
||||
// App specialize does not check FD
|
||||
flags[SKIP_CLOSE_LOG_PIPE] = true;
|
||||
app_specialize_pre();
|
||||
}
|
||||
|
||||
void HookContext::nativeSpecializeAppProcess_post() {
|
||||
ZLOGV("post specialize [%s]\n", process);
|
||||
app_specialize_post();
|
||||
}
|
||||
|
||||
void HookContext::nativeForkSystemServer_pre() {
|
||||
ZLOGV("pre forkSystemServer\n");
|
||||
flags[SERVER_FORK_AND_SPECIALIZE] = true;
|
||||
|
||||
fork_pre();
|
||||
if (is_child()) {
|
||||
server_specialize_pre();
|
||||
}
|
||||
sanitize_fds();
|
||||
}
|
||||
|
||||
void HookContext::nativeForkSystemServer_post() {
|
||||
if (is_child()) {
|
||||
ZLOGV("post forkSystemServer\n");
|
||||
server_specialize_post();
|
||||
}
|
||||
fork_post();
|
||||
}
|
||||
|
||||
void HookContext::nativeForkAndSpecialize_pre() {
|
||||
process = env->GetStringUTFChars(args.app->nice_name, nullptr);
|
||||
ZLOGV("pre forkAndSpecialize [%s]\n", process);
|
||||
flags[APP_FORK_AND_SPECIALIZE] = true;
|
||||
|
||||
fork_pre();
|
||||
if (is_child()) {
|
||||
app_specialize_pre();
|
||||
}
|
||||
sanitize_fds();
|
||||
}
|
||||
|
||||
void HookContext::nativeForkAndSpecialize_post() {
|
||||
if (is_child()) {
|
||||
ZLOGV("post forkAndSpecialize [%s]\n", process);
|
||||
app_specialize_post();
|
||||
}
|
||||
fork_post();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
|
||||
inline void *unwind_get_region_start(_Unwind_Context *ctx) {
|
||||
auto fp = _Unwind_GetRegionStart(ctx);
|
||||
#if defined(__arm__)
|
||||
// On arm32, we need to check if the pc is in thumb mode,
|
||||
// if so, we need to set the lowest bit of fp to 1
|
||||
auto pc = _Unwind_GetGR(ctx, 15); // r15 is pc
|
||||
if (pc & 1) {
|
||||
// Thumb mode
|
||||
fp |= 1;
|
||||
}
|
||||
#endif
|
||||
return reinterpret_cast<void *>(fp);
|
||||
}
|
||||
|
||||
// As we use NativeBridgeRuntimeCallbacks to reload native bridge and to hook jni functions,
|
||||
// we need to find it by the native bridge's unwind context.
|
||||
// For abis that use registers to pass arguments, i.e. arm32, arm64, x86_64, the registers are
|
||||
// caller-saved, and they are not preserved in the unwind context. However, they will be saved
|
||||
// into the callee-saved registers, so we will search the callee-saved registers for the second
|
||||
// argument, which is the pointer to NativeBridgeRuntimeCallbacks.
|
||||
// For x86, whose abi uses stack to pass arguments, we can directly get the pointer to
|
||||
// NativeBridgeRuntimeCallbacks from the stack.
|
||||
static const NativeBridgeRuntimeCallbacks* find_runtime_callbacks(struct _Unwind_Context *ctx) {
|
||||
// Find the writable memory region of libart.so, where the NativeBridgeRuntimeCallbacks is located.
|
||||
auto [start, end] = []()-> tuple<uintptr_t, uintptr_t> {
|
||||
for (const auto &map : lsplt::MapInfo::Scan()) {
|
||||
if (map.path.ends_with("/libart.so") && map.perms == (PROT_WRITE | PROT_READ)) {
|
||||
ZLOGV("libart.so: start=%p, end=%p\n",
|
||||
reinterpret_cast<void *>(map.start), reinterpret_cast<void *>(map.end));
|
||||
return {map.start, map.end};
|
||||
}
|
||||
}
|
||||
return {0, 0};
|
||||
}();
|
||||
#if defined(__aarch64__)
|
||||
// r19-r28 are callee-saved registers
|
||||
for (int i = 19; i <= 28; ++i) {
|
||||
auto val = static_cast<uintptr_t>(_Unwind_GetGR(ctx, i));
|
||||
ZLOGV("r%d = %p\n", i, reinterpret_cast<void *>(val));
|
||||
if (val >= start && val < end)
|
||||
return reinterpret_cast<const NativeBridgeRuntimeCallbacks*>(val);
|
||||
}
|
||||
#elif defined(__arm__)
|
||||
// r4-r10 are callee-saved registers
|
||||
for (int i = 4; i <= 10; ++i) {
|
||||
auto val = static_cast<uintptr_t>(_Unwind_GetGR(ctx, i));
|
||||
ZLOGV("r%d = %p\n", i, reinterpret_cast<void *>(val));
|
||||
if (val >= start && val < end)
|
||||
return reinterpret_cast<const NativeBridgeRuntimeCallbacks*>(val);
|
||||
}
|
||||
#elif defined(__i386__)
|
||||
// get ebp, which points to the bottom of the stack frame
|
||||
auto ebp = static_cast<uintptr_t>(_Unwind_GetGR(ctx, 5));
|
||||
// 1 pointer size above ebp is the old ebp
|
||||
// 2 pointer sizes above ebp is the return address
|
||||
// 3 pointer sizes above ebp is the 2nd arg
|
||||
auto val = *reinterpret_cast<uintptr_t *>(ebp + 3 * sizeof(void *));
|
||||
ZLOGV("ebp + 3 * ptr_size = %p\n", reinterpret_cast<void *>(val));
|
||||
if (val >= start && val < end)
|
||||
return reinterpret_cast<const NativeBridgeRuntimeCallbacks*>(val);
|
||||
#elif defined(__x86_64__)
|
||||
// r12-r15 and rbx are callee-saved registers, but the compiler is likely to use them reversely
|
||||
for (int i : {3, 15, 14, 13, 12}) {
|
||||
auto val = static_cast<uintptr_t>(_Unwind_GetGR(ctx, i));
|
||||
ZLOGV("r%d = %p\n", i, reinterpret_cast<void *>(val));
|
||||
if (val >= start && val < end)
|
||||
return reinterpret_cast<const NativeBridgeRuntimeCallbacks*>(val);
|
||||
}
|
||||
#else
|
||||
#error "Unsupported architecture"
|
||||
#endif
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
static void reload_native_bridge(const string &nb) {
|
||||
// Use unwind to find the address of android::LoadNativeBridge and NativeBridgeRuntimeCallbacks
|
||||
bool (*load_native_bridge)(const char *nb_library_filename,
|
||||
const NativeBridgeRuntimeCallbacks *runtime_cbs) = nullptr;
|
||||
_Unwind_Backtrace(+[](struct _Unwind_Context *ctx, void *arg) -> _Unwind_Reason_Code {
|
||||
void *fp = unwind_get_region_start(ctx);
|
||||
Dl_info info{};
|
||||
dladdr(fp, &info);
|
||||
ZLOGV("backtrace: %p %s\n", fp, info.dli_fname ? info.dli_fname : "???");
|
||||
if (info.dli_fname && std::string_view(info.dli_fname).ends_with("/libnativebridge.so")) {
|
||||
*reinterpret_cast<void **>(arg) = fp;
|
||||
runtime_callbacks = find_runtime_callbacks(ctx);
|
||||
ZLOGD("cbs: %p\n", runtime_callbacks);
|
||||
return _URC_END_OF_STACK;
|
||||
}
|
||||
return _URC_NO_REASON;
|
||||
}, &load_native_bridge);
|
||||
auto len = sizeof(ZYGISKLDR) - 1;
|
||||
if (nb.size() > len) {
|
||||
load_native_bridge(nb.data() + len, runtime_callbacks);
|
||||
}
|
||||
}
|
||||
|
||||
static void hook_register(dev_t dev, ino_t inode, const char *symbol, void *new_func, void **old_func) {
|
||||
if (!lsplt::RegisterHook(dev, inode, symbol, new_func, old_func)) {
|
||||
ZLOGE("Failed to register plt_hook \"%s\"\n", symbol);
|
||||
return;
|
||||
}
|
||||
plt_hook_list->emplace_back(dev, inode, symbol, old_func);
|
||||
}
|
||||
|
||||
static void hook_commit() {
|
||||
if (!lsplt::CommitHook())
|
||||
ZLOGE("plt_hook failed\n");
|
||||
}
|
||||
|
||||
#define PLT_HOOK_REGISTER_SYM(DEV, INODE, SYM, NAME) \
|
||||
hook_register(DEV, INODE, SYM, \
|
||||
reinterpret_cast<void *>(new_##NAME), reinterpret_cast<void **>(&old_##NAME))
|
||||
|
||||
#define PLT_HOOK_REGISTER(DEV, INODE, NAME) \
|
||||
PLT_HOOK_REGISTER_SYM(DEV, INODE, #NAME, NAME)
|
||||
|
||||
void hook_functions() {
|
||||
default_new(plt_hook_list);
|
||||
default_new(jni_hook_list);
|
||||
|
||||
ino_t android_runtime_inode = 0;
|
||||
dev_t android_runtime_dev = 0;
|
||||
ino_t native_bridge_inode = 0;
|
||||
dev_t native_bridge_dev = 0;
|
||||
|
||||
for (auto &map : lsplt::MapInfo::Scan()) {
|
||||
if (map.path.ends_with("/libandroid_runtime.so")) {
|
||||
android_runtime_inode = map.inode;
|
||||
android_runtime_dev = map.dev;
|
||||
} else if (map.path.ends_with("/libnativebridge.so")) {
|
||||
native_bridge_inode = map.inode;
|
||||
native_bridge_dev = map.dev;
|
||||
}
|
||||
}
|
||||
|
||||
PLT_HOOK_REGISTER(native_bridge_dev, native_bridge_inode, dlclose);
|
||||
PLT_HOOK_REGISTER(android_runtime_dev, android_runtime_inode, fork);
|
||||
PLT_HOOK_REGISTER(android_runtime_dev, android_runtime_inode, unshare);
|
||||
PLT_HOOK_REGISTER(android_runtime_dev, android_runtime_inode, androidSetCreateThreadFunc);
|
||||
PLT_HOOK_REGISTER(android_runtime_dev, android_runtime_inode, selinux_android_setcontext);
|
||||
PLT_HOOK_REGISTER_SYM(android_runtime_dev, android_runtime_inode, "__android_log_close", android_log_close);
|
||||
hook_commit();
|
||||
|
||||
// Remove unhooked methods
|
||||
plt_hook_list->erase(
|
||||
std::remove_if(plt_hook_list->begin(), plt_hook_list->end(),
|
||||
[](auto &t) { return *std::get<3>(t) == nullptr;}),
|
||||
plt_hook_list->end());
|
||||
}
|
||||
|
||||
static void hook_unloader() {
|
||||
ino_t art_inode = 0;
|
||||
dev_t art_dev = 0;
|
||||
|
||||
for (auto &map : lsplt::MapInfo::Scan()) {
|
||||
if (map.path.ends_with("/libart.so")) {
|
||||
art_inode = map.inode;
|
||||
art_dev = map.dev;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
PLT_HOOK_REGISTER(art_dev, art_inode, pthread_attr_destroy);
|
||||
hook_commit();
|
||||
}
|
||||
|
||||
static void unhook_functions() {
|
||||
// Unhook plt_hook
|
||||
for (const auto &[dev, inode, sym, old_func] : *plt_hook_list) {
|
||||
if (!lsplt::RegisterHook(dev, inode, sym, *old_func, nullptr)) {
|
||||
ZLOGE("Failed to register plt_hook [%s]\n", sym);
|
||||
should_unmap_zygisk = false;
|
||||
}
|
||||
}
|
||||
delete plt_hook_list;
|
||||
plt_hook_list = nullptr;
|
||||
if (!lsplt::CommitHook()) {
|
||||
ZLOGE("Failed to restore plt_hook\n");
|
||||
should_unmap_zygisk = false;
|
||||
}
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------
|
||||
|
||||
// JNI method hook definitions, auto generated
|
||||
#include "jni_hooks.hpp"
|
||||
|
||||
static string get_class_name(JNIEnv *env, jclass clazz) {
|
||||
static auto class_getName = env->GetMethodID(
|
||||
env->FindClass("java/lang/Class"), "getName", "()Ljava/lang/String;");
|
||||
auto nameRef = (jstring) env->CallObjectMethod(clazz, class_getName);
|
||||
const char *name = env->GetStringUTFChars(nameRef, nullptr);
|
||||
string className(name);
|
||||
env->ReleaseStringUTFChars(nameRef, name);
|
||||
std::replace(className.begin(), className.end(), '.', '/');
|
||||
return className;
|
||||
}
|
||||
|
||||
static void replace_jni_methods(
|
||||
vector<JNINativeMethod> &methods,
|
||||
JNINativeMethod *hook_methods, size_t hook_methods_size,
|
||||
void **orig_function) {
|
||||
for (auto &method : methods) {
|
||||
if (strcmp(method.name, hook_methods[0].name) == 0) {
|
||||
for (auto i = 0; i < hook_methods_size; ++i) {
|
||||
const auto &hook = hook_methods[i];
|
||||
if (strcmp(method.signature, hook.signature) == 0) {
|
||||
*orig_function = method.fnPtr;
|
||||
method.fnPtr = hook.fnPtr;
|
||||
ZLOGI("replace %s\n", method.name);
|
||||
return;
|
||||
}
|
||||
}
|
||||
ZLOGE("unknown signature of %s%s\n", method.name, method.signature);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#define HOOK_JNI(method) \
|
||||
replace_jni_methods(newMethods, method##_methods.data(), method##_methods.size(), &method##_orig)
|
||||
|
||||
static jint env_RegisterNatives(
|
||||
JNIEnv *env, jclass clazz, const JNINativeMethod *methods, jint numMethods) {
|
||||
auto className = get_class_name(env, clazz);
|
||||
if (className == "com/android/internal/os/Zygote") {
|
||||
// Restore JNIEnv as we no longer need to replace anything
|
||||
env->functions = old_functions;
|
||||
delete new_functions;
|
||||
new_functions = nullptr;
|
||||
|
||||
vector<JNINativeMethod> newMethods(methods, methods + numMethods);
|
||||
HOOK_JNI(nativeForkAndSpecialize);
|
||||
HOOK_JNI(nativeSpecializeAppProcess);
|
||||
HOOK_JNI(nativeForkSystemServer);
|
||||
return old_functions->RegisterNatives(env, clazz, newMethods.data(), numMethods);
|
||||
} else {
|
||||
return old_functions->RegisterNatives(env, clazz, methods, numMethods);
|
||||
}
|
||||
}
|
||||
|
||||
static void hook_jni_env() {
|
||||
using method_sig = jint(*)(JavaVM **, jsize, jsize *);
|
||||
auto get_created_vms = reinterpret_cast<method_sig>(
|
||||
dlsym(RTLD_DEFAULT, "JNI_GetCreatedJavaVMs"));
|
||||
if (!get_created_vms) {
|
||||
for (auto &map: lsplt::MapInfo::Scan()) {
|
||||
if (!map.path.ends_with("/libnativehelper.so")) continue;
|
||||
void *h = dlopen(map.path.data(), RTLD_LAZY);
|
||||
if (!h) {
|
||||
ZLOGW("Cannot dlopen libnativehelper.so: %s\n", dlerror());
|
||||
break;
|
||||
}
|
||||
get_created_vms = reinterpret_cast<method_sig>(dlsym(h, "JNI_GetCreatedJavaVMs"));
|
||||
dlclose(h);
|
||||
break;
|
||||
}
|
||||
if (!get_created_vms) {
|
||||
ZLOGW("JNI_GetCreatedJavaVMs not found\n");
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
JavaVM *vm = nullptr;
|
||||
jsize num = 0;
|
||||
jint res = get_created_vms(&vm, 1, &num);
|
||||
if (res != JNI_OK || vm == nullptr) {
|
||||
ZLOGW("JavaVM not found\n");
|
||||
return;
|
||||
}
|
||||
JNIEnv *env = nullptr;
|
||||
res = vm->GetEnv(reinterpret_cast<void **>(&env), JNI_VERSION_1_6);
|
||||
if (res != JNI_OK || env == nullptr) {
|
||||
ZLOGW("JNIEnv not found\n");
|
||||
return;
|
||||
}
|
||||
|
||||
// Replace the function table in JNIEnv to hook RegisterNatives
|
||||
default_new(new_functions);
|
||||
memcpy(new_functions, env->functions, sizeof(*new_functions));
|
||||
new_functions->RegisterNatives = &env_RegisterNatives;
|
||||
old_functions = env->functions;
|
||||
env->functions = new_functions;
|
||||
}
|
||||
264
native/src/core/zygisk/jni_hooks.hpp
Normal file
264
native/src/core/zygisk/jni_hooks.hpp
Normal file
@@ -0,0 +1,264 @@
|
||||
// Generated by gen_jni_hooks.py
|
||||
|
||||
namespace {
|
||||
|
||||
void *nativeForkAndSpecialize_orig = nullptr;
|
||||
[[clang::no_stack_protector]] jint nativeForkAndSpecialize_l(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jstring nice_name, jintArray fds_to_close, jstring instruction_set, jstring app_data_dir) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeForkAndSpecialize_pre();
|
||||
reinterpret_cast<decltype(&nativeForkAndSpecialize_l)>(nativeForkAndSpecialize_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, fds_to_close, instruction_set, app_data_dir
|
||||
);
|
||||
ctx.nativeForkAndSpecialize_post();
|
||||
return ctx.pid;
|
||||
}
|
||||
[[clang::no_stack_protector]] jint nativeForkAndSpecialize_o(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jstring nice_name, jintArray fds_to_close, jintArray fds_to_ignore, jstring instruction_set, jstring app_data_dir) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
args.fds_to_ignore = &fds_to_ignore;
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeForkAndSpecialize_pre();
|
||||
reinterpret_cast<decltype(&nativeForkAndSpecialize_o)>(nativeForkAndSpecialize_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, fds_to_close, fds_to_ignore, instruction_set, app_data_dir
|
||||
);
|
||||
ctx.nativeForkAndSpecialize_post();
|
||||
return ctx.pid;
|
||||
}
|
||||
[[clang::no_stack_protector]] jint nativeForkAndSpecialize_p(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jstring nice_name, jintArray fds_to_close, jintArray fds_to_ignore, jboolean is_child_zygote, jstring instruction_set, jstring app_data_dir) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
args.fds_to_ignore = &fds_to_ignore;
|
||||
args.is_child_zygote = &is_child_zygote;
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeForkAndSpecialize_pre();
|
||||
reinterpret_cast<decltype(&nativeForkAndSpecialize_p)>(nativeForkAndSpecialize_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir
|
||||
);
|
||||
ctx.nativeForkAndSpecialize_post();
|
||||
return ctx.pid;
|
||||
}
|
||||
[[clang::no_stack_protector]] jint nativeForkAndSpecialize_q_alt(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jstring nice_name, jintArray fds_to_close, jintArray fds_to_ignore, jboolean is_child_zygote, jstring instruction_set, jstring app_data_dir, jboolean is_top_app) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
args.fds_to_ignore = &fds_to_ignore;
|
||||
args.is_child_zygote = &is_child_zygote;
|
||||
args.is_top_app = &is_top_app;
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeForkAndSpecialize_pre();
|
||||
reinterpret_cast<decltype(&nativeForkAndSpecialize_q_alt)>(nativeForkAndSpecialize_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir, is_top_app
|
||||
);
|
||||
ctx.nativeForkAndSpecialize_post();
|
||||
return ctx.pid;
|
||||
}
|
||||
[[clang::no_stack_protector]] jint nativeForkAndSpecialize_r(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jstring nice_name, jintArray fds_to_close, jintArray fds_to_ignore, jboolean is_child_zygote, jstring instruction_set, jstring app_data_dir, jboolean is_top_app, jobjectArray pkg_data_info_list, jobjectArray whitelisted_data_info_list, jboolean mount_data_dirs, jboolean mount_storage_dirs) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
args.fds_to_ignore = &fds_to_ignore;
|
||||
args.is_child_zygote = &is_child_zygote;
|
||||
args.is_top_app = &is_top_app;
|
||||
args.pkg_data_info_list = &pkg_data_info_list;
|
||||
args.whitelisted_data_info_list = &whitelisted_data_info_list;
|
||||
args.mount_data_dirs = &mount_data_dirs;
|
||||
args.mount_storage_dirs = &mount_storage_dirs;
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeForkAndSpecialize_pre();
|
||||
reinterpret_cast<decltype(&nativeForkAndSpecialize_r)>(nativeForkAndSpecialize_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir, is_top_app, pkg_data_info_list, whitelisted_data_info_list, mount_data_dirs, mount_storage_dirs
|
||||
);
|
||||
ctx.nativeForkAndSpecialize_post();
|
||||
return ctx.pid;
|
||||
}
|
||||
[[clang::no_stack_protector]] jint nativeForkAndSpecialize_samsung_m(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jint _0, jint _1, jstring nice_name, jintArray fds_to_close, jstring instruction_set, jstring app_data_dir) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeForkAndSpecialize_pre();
|
||||
reinterpret_cast<decltype(&nativeForkAndSpecialize_samsung_m)>(nativeForkAndSpecialize_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, _0, _1, nice_name, fds_to_close, instruction_set, app_data_dir
|
||||
);
|
||||
ctx.nativeForkAndSpecialize_post();
|
||||
return ctx.pid;
|
||||
}
|
||||
[[clang::no_stack_protector]] jint nativeForkAndSpecialize_samsung_n(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jint _2, jint _3, jstring nice_name, jintArray fds_to_close, jstring instruction_set, jstring app_data_dir, jint _4) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeForkAndSpecialize_pre();
|
||||
reinterpret_cast<decltype(&nativeForkAndSpecialize_samsung_n)>(nativeForkAndSpecialize_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, _2, _3, nice_name, fds_to_close, instruction_set, app_data_dir, _4
|
||||
);
|
||||
ctx.nativeForkAndSpecialize_post();
|
||||
return ctx.pid;
|
||||
}
|
||||
[[clang::no_stack_protector]] jint nativeForkAndSpecialize_samsung_o(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jint _5, jint _6, jstring nice_name, jintArray fds_to_close, jintArray fds_to_ignore, jstring instruction_set, jstring app_data_dir) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
args.fds_to_ignore = &fds_to_ignore;
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeForkAndSpecialize_pre();
|
||||
reinterpret_cast<decltype(&nativeForkAndSpecialize_samsung_o)>(nativeForkAndSpecialize_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, _5, _6, nice_name, fds_to_close, fds_to_ignore, instruction_set, app_data_dir
|
||||
);
|
||||
ctx.nativeForkAndSpecialize_post();
|
||||
return ctx.pid;
|
||||
}
|
||||
[[clang::no_stack_protector]] jint nativeForkAndSpecialize_samsung_p(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jint _7, jint _8, jstring nice_name, jintArray fds_to_close, jintArray fds_to_ignore, jboolean is_child_zygote, jstring instruction_set, jstring app_data_dir) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
args.fds_to_ignore = &fds_to_ignore;
|
||||
args.is_child_zygote = &is_child_zygote;
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeForkAndSpecialize_pre();
|
||||
reinterpret_cast<decltype(&nativeForkAndSpecialize_samsung_p)>(nativeForkAndSpecialize_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, _7, _8, nice_name, fds_to_close, fds_to_ignore, is_child_zygote, instruction_set, app_data_dir
|
||||
);
|
||||
ctx.nativeForkAndSpecialize_post();
|
||||
return ctx.pid;
|
||||
}
|
||||
std::array nativeForkAndSpecialize_methods = {
|
||||
JNINativeMethod {
|
||||
"nativeForkAndSpecialize",
|
||||
"(II[II[[IILjava/lang/String;Ljava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I",
|
||||
(void *) &nativeForkAndSpecialize_l
|
||||
},
|
||||
JNINativeMethod {
|
||||
"nativeForkAndSpecialize",
|
||||
"(II[II[[IILjava/lang/String;Ljava/lang/String;[I[ILjava/lang/String;Ljava/lang/String;)I",
|
||||
(void *) &nativeForkAndSpecialize_o
|
||||
},
|
||||
JNINativeMethod {
|
||||
"nativeForkAndSpecialize",
|
||||
"(II[II[[IILjava/lang/String;Ljava/lang/String;[I[IZLjava/lang/String;Ljava/lang/String;)I",
|
||||
(void *) &nativeForkAndSpecialize_p
|
||||
},
|
||||
JNINativeMethod {
|
||||
"nativeForkAndSpecialize",
|
||||
"(II[II[[IILjava/lang/String;Ljava/lang/String;[I[IZLjava/lang/String;Ljava/lang/String;Z)I",
|
||||
(void *) &nativeForkAndSpecialize_q_alt
|
||||
},
|
||||
JNINativeMethod {
|
||||
"nativeForkAndSpecialize",
|
||||
"(II[II[[IILjava/lang/String;Ljava/lang/String;[I[IZLjava/lang/String;Ljava/lang/String;Z[Ljava/lang/String;[Ljava/lang/String;ZZ)I",
|
||||
(void *) &nativeForkAndSpecialize_r
|
||||
},
|
||||
JNINativeMethod {
|
||||
"nativeForkAndSpecialize",
|
||||
"(II[II[[IILjava/lang/String;IILjava/lang/String;[ILjava/lang/String;Ljava/lang/String;)I",
|
||||
(void *) &nativeForkAndSpecialize_samsung_m
|
||||
},
|
||||
JNINativeMethod {
|
||||
"nativeForkAndSpecialize",
|
||||
"(II[II[[IILjava/lang/String;IILjava/lang/String;[ILjava/lang/String;Ljava/lang/String;I)I",
|
||||
(void *) &nativeForkAndSpecialize_samsung_n
|
||||
},
|
||||
JNINativeMethod {
|
||||
"nativeForkAndSpecialize",
|
||||
"(II[II[[IILjava/lang/String;IILjava/lang/String;[I[ILjava/lang/String;Ljava/lang/String;)I",
|
||||
(void *) &nativeForkAndSpecialize_samsung_o
|
||||
},
|
||||
JNINativeMethod {
|
||||
"nativeForkAndSpecialize",
|
||||
"(II[II[[IILjava/lang/String;IILjava/lang/String;[I[IZLjava/lang/String;Ljava/lang/String;)I",
|
||||
(void *) &nativeForkAndSpecialize_samsung_p
|
||||
},
|
||||
};
|
||||
|
||||
void *nativeSpecializeAppProcess_orig = nullptr;
|
||||
[[clang::no_stack_protector]] void nativeSpecializeAppProcess_q(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jstring nice_name, jboolean is_child_zygote, jstring instruction_set, jstring app_data_dir) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
args.is_child_zygote = &is_child_zygote;
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeSpecializeAppProcess_pre();
|
||||
reinterpret_cast<decltype(&nativeSpecializeAppProcess_q)>(nativeSpecializeAppProcess_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, is_child_zygote, instruction_set, app_data_dir
|
||||
);
|
||||
ctx.nativeSpecializeAppProcess_post();
|
||||
}
|
||||
[[clang::no_stack_protector]] void nativeSpecializeAppProcess_q_alt(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jstring nice_name, jboolean is_child_zygote, jstring instruction_set, jstring app_data_dir, jboolean is_top_app) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
args.is_child_zygote = &is_child_zygote;
|
||||
args.is_top_app = &is_top_app;
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeSpecializeAppProcess_pre();
|
||||
reinterpret_cast<decltype(&nativeSpecializeAppProcess_q_alt)>(nativeSpecializeAppProcess_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, is_child_zygote, instruction_set, app_data_dir, is_top_app
|
||||
);
|
||||
ctx.nativeSpecializeAppProcess_post();
|
||||
}
|
||||
[[clang::no_stack_protector]] void nativeSpecializeAppProcess_r(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jstring nice_name, jboolean is_child_zygote, jstring instruction_set, jstring app_data_dir, jboolean is_top_app, jobjectArray pkg_data_info_list, jobjectArray whitelisted_data_info_list, jboolean mount_data_dirs, jboolean mount_storage_dirs) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
args.is_child_zygote = &is_child_zygote;
|
||||
args.is_top_app = &is_top_app;
|
||||
args.pkg_data_info_list = &pkg_data_info_list;
|
||||
args.whitelisted_data_info_list = &whitelisted_data_info_list;
|
||||
args.mount_data_dirs = &mount_data_dirs;
|
||||
args.mount_storage_dirs = &mount_storage_dirs;
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeSpecializeAppProcess_pre();
|
||||
reinterpret_cast<decltype(&nativeSpecializeAppProcess_r)>(nativeSpecializeAppProcess_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, is_child_zygote, instruction_set, app_data_dir, is_top_app, pkg_data_info_list, whitelisted_data_info_list, mount_data_dirs, mount_storage_dirs
|
||||
);
|
||||
ctx.nativeSpecializeAppProcess_post();
|
||||
}
|
||||
[[clang::no_stack_protector]] void nativeSpecializeAppProcess_samsung_q(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jint mount_external, jstring se_info, jint _9, jint _10, jstring nice_name, jboolean is_child_zygote, jstring instruction_set, jstring app_data_dir) {
|
||||
AppSpecializeArgs_v3 args(uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, nice_name, instruction_set, app_data_dir);
|
||||
args.is_child_zygote = &is_child_zygote;
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeSpecializeAppProcess_pre();
|
||||
reinterpret_cast<decltype(&nativeSpecializeAppProcess_samsung_q)>(nativeSpecializeAppProcess_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, mount_external, se_info, _9, _10, nice_name, is_child_zygote, instruction_set, app_data_dir
|
||||
);
|
||||
ctx.nativeSpecializeAppProcess_post();
|
||||
}
|
||||
std::array nativeSpecializeAppProcess_methods = {
|
||||
JNINativeMethod {
|
||||
"nativeSpecializeAppProcess",
|
||||
"(II[II[[IILjava/lang/String;Ljava/lang/String;ZLjava/lang/String;Ljava/lang/String;)V",
|
||||
(void *) &nativeSpecializeAppProcess_q
|
||||
},
|
||||
JNINativeMethod {
|
||||
"nativeSpecializeAppProcess",
|
||||
"(II[II[[IILjava/lang/String;Ljava/lang/String;ZLjava/lang/String;Ljava/lang/String;Z)V",
|
||||
(void *) &nativeSpecializeAppProcess_q_alt
|
||||
},
|
||||
JNINativeMethod {
|
||||
"nativeSpecializeAppProcess",
|
||||
"(II[II[[IILjava/lang/String;Ljava/lang/String;ZLjava/lang/String;Ljava/lang/String;Z[Ljava/lang/String;[Ljava/lang/String;ZZ)V",
|
||||
(void *) &nativeSpecializeAppProcess_r
|
||||
},
|
||||
JNINativeMethod {
|
||||
"nativeSpecializeAppProcess",
|
||||
"(II[II[[IILjava/lang/String;IILjava/lang/String;ZLjava/lang/String;Ljava/lang/String;)V",
|
||||
(void *) &nativeSpecializeAppProcess_samsung_q
|
||||
},
|
||||
};
|
||||
|
||||
void *nativeForkSystemServer_orig = nullptr;
|
||||
[[clang::no_stack_protector]] jint nativeForkSystemServer_l(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jobjectArray rlimits, jlong permitted_capabilities, jlong effective_capabilities) {
|
||||
ServerSpecializeArgs_v1 args(uid, gid, gids, runtime_flags, permitted_capabilities, effective_capabilities);
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeForkSystemServer_pre();
|
||||
reinterpret_cast<decltype(&nativeForkSystemServer_l)>(nativeForkSystemServer_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, rlimits, permitted_capabilities, effective_capabilities
|
||||
);
|
||||
ctx.nativeForkSystemServer_post();
|
||||
return ctx.pid;
|
||||
}
|
||||
[[clang::no_stack_protector]] jint nativeForkSystemServer_samsung_q(JNIEnv *env, jclass clazz, jint uid, jint gid, jintArray gids, jint runtime_flags, jint _11, jint _12, jobjectArray rlimits, jlong permitted_capabilities, jlong effective_capabilities) {
|
||||
ServerSpecializeArgs_v1 args(uid, gid, gids, runtime_flags, permitted_capabilities, effective_capabilities);
|
||||
HookContext ctx(env, &args);
|
||||
ctx.nativeForkSystemServer_pre();
|
||||
reinterpret_cast<decltype(&nativeForkSystemServer_samsung_q)>(nativeForkSystemServer_orig)(
|
||||
env, clazz, uid, gid, gids, runtime_flags, _11, _12, rlimits, permitted_capabilities, effective_capabilities
|
||||
);
|
||||
ctx.nativeForkSystemServer_post();
|
||||
return ctx.pid;
|
||||
}
|
||||
std::array nativeForkSystemServer_methods = {
|
||||
JNINativeMethod {
|
||||
"nativeForkSystemServer",
|
||||
"(II[II[[IJJ)I",
|
||||
(void *) &nativeForkSystemServer_l
|
||||
},
|
||||
JNINativeMethod {
|
||||
"nativeForkSystemServer",
|
||||
"(II[IIII[[IJJ)I",
|
||||
(void *) &nativeForkSystemServer_samsung_q
|
||||
},
|
||||
};
|
||||
|
||||
} // namespace
|
||||
96
native/src/core/zygisk/main.cpp
Normal file
96
native/src/core/zygisk/main.cpp
Normal file
@@ -0,0 +1,96 @@
|
||||
#include <sys/mount.h>
|
||||
#include <android/dlext.h>
|
||||
#include <dlfcn.h>
|
||||
|
||||
#include <consts.hpp>
|
||||
#include <base.hpp>
|
||||
#include <core.hpp>
|
||||
#include <selinux.hpp>
|
||||
|
||||
#include "zygisk.hpp"
|
||||
|
||||
using namespace std;
|
||||
|
||||
static void zygiskd(int socket) {
|
||||
if (getuid() != 0 || fcntl(socket, F_GETFD) < 0)
|
||||
exit(-1);
|
||||
|
||||
#if defined(__LP64__)
|
||||
set_nice_name("zygiskd64");
|
||||
LOGI("* Launching zygiskd64\n");
|
||||
#else
|
||||
set_nice_name("zygiskd32");
|
||||
LOGI("* Launching zygiskd32\n");
|
||||
#endif
|
||||
|
||||
// Load modules
|
||||
using comp_entry = void(*)(int);
|
||||
vector<comp_entry> modules;
|
||||
{
|
||||
vector<int> module_fds = recv_fds(socket);
|
||||
for (int fd : module_fds) {
|
||||
comp_entry entry = nullptr;
|
||||
struct stat s{};
|
||||
if (fstat(fd, &s) == 0 && S_ISREG(s.st_mode)) {
|
||||
android_dlextinfo info {
|
||||
.flags = ANDROID_DLEXT_USE_LIBRARY_FD,
|
||||
.library_fd = fd,
|
||||
};
|
||||
if (void *h = android_dlopen_ext("/jit-cache", RTLD_LAZY, &info)) {
|
||||
*(void **) &entry = dlsym(h, "zygisk_companion_entry");
|
||||
} else {
|
||||
LOGW("Failed to dlopen zygisk module: %s\n", dlerror());
|
||||
}
|
||||
}
|
||||
modules.push_back(entry);
|
||||
close(fd);
|
||||
}
|
||||
}
|
||||
|
||||
// ack
|
||||
write_int(socket, 0);
|
||||
|
||||
// Start accepting requests
|
||||
pollfd pfd = { socket, POLLIN, 0 };
|
||||
for (;;) {
|
||||
poll(&pfd, 1, -1);
|
||||
if (pfd.revents && !(pfd.revents & POLLIN)) {
|
||||
// Something bad happened in magiskd, terminate zygiskd
|
||||
exit(0);
|
||||
}
|
||||
int client = recv_fd(socket);
|
||||
if (client < 0) {
|
||||
// Something bad happened in magiskd, terminate zygiskd
|
||||
exit(0);
|
||||
}
|
||||
int module_id = read_int(client);
|
||||
if (module_id >= 0 && module_id < modules.size() && modules[module_id]) {
|
||||
exec_task([=, entry = modules[module_id]] {
|
||||
struct stat s1;
|
||||
fstat(client, &s1);
|
||||
entry(client);
|
||||
// Only close client if it is the same file so we don't
|
||||
// accidentally close a re-used file descriptor.
|
||||
// This check is required because the module companion
|
||||
// handler could've closed the file descriptor already.
|
||||
if (struct stat s2; fstat(client, &s2) == 0) {
|
||||
if (s1.st_dev == s2.st_dev && s1.st_ino == s2.st_ino) {
|
||||
close(client);
|
||||
}
|
||||
}
|
||||
});
|
||||
} else {
|
||||
close(client);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Entrypoint where we need to re-exec ourselves
|
||||
// This should only ever be called internally
|
||||
int zygisk_main(int argc, char *argv[]) {
|
||||
android_logging();
|
||||
if (argc == 3 && argv[1] == "companion"sv) {
|
||||
zygiskd(parse_int(argv[2]));
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
221
native/src/core/zygisk/module.hpp
Normal file
221
native/src/core/zygisk/module.hpp
Normal file
@@ -0,0 +1,221 @@
|
||||
#pragma once
|
||||
|
||||
#include "api.hpp"
|
||||
|
||||
namespace {
|
||||
|
||||
struct HookContext;
|
||||
struct ZygiskModule;
|
||||
|
||||
struct AppSpecializeArgs_v1;
|
||||
using AppSpecializeArgs_v2 = AppSpecializeArgs_v1;
|
||||
struct AppSpecializeArgs_v3;
|
||||
using AppSpecializeArgs_v4 = AppSpecializeArgs_v3;
|
||||
|
||||
struct module_abi_v1;
|
||||
using module_abi_v2 = module_abi_v1;
|
||||
using module_abi_v3 = module_abi_v1;
|
||||
using module_abi_v4 = module_abi_v1;
|
||||
|
||||
struct api_abi_v1;
|
||||
struct api_abi_v2;
|
||||
using api_abi_v3 = api_abi_v2;
|
||||
struct api_abi_v4;
|
||||
|
||||
union ApiTable;
|
||||
|
||||
struct AppSpecializeArgs_v3 {
|
||||
jint &uid;
|
||||
jint &gid;
|
||||
jintArray &gids;
|
||||
jint &runtime_flags;
|
||||
jobjectArray &rlimits;
|
||||
jint &mount_external;
|
||||
jstring &se_info;
|
||||
jstring &nice_name;
|
||||
jstring &instruction_set;
|
||||
jstring &app_data_dir;
|
||||
|
||||
jintArray *fds_to_ignore = nullptr;
|
||||
jboolean *is_child_zygote = nullptr;
|
||||
jboolean *is_top_app = nullptr;
|
||||
jobjectArray *pkg_data_info_list = nullptr;
|
||||
jobjectArray *whitelisted_data_info_list = nullptr;
|
||||
jboolean *mount_data_dirs = nullptr;
|
||||
jboolean *mount_storage_dirs = nullptr;
|
||||
|
||||
AppSpecializeArgs_v3(
|
||||
jint &uid, jint &gid, jintArray &gids, jint &runtime_flags,
|
||||
jobjectArray &rlimits, jint &mount_external, jstring &se_info, jstring &nice_name,
|
||||
jstring &instruction_set, jstring &app_data_dir) :
|
||||
uid(uid), gid(gid), gids(gids), runtime_flags(runtime_flags), rlimits(rlimits),
|
||||
mount_external(mount_external), se_info(se_info), nice_name(nice_name),
|
||||
instruction_set(instruction_set), app_data_dir(app_data_dir) {}
|
||||
};
|
||||
|
||||
struct AppSpecializeArgs_v1 {
|
||||
jint &uid;
|
||||
jint &gid;
|
||||
jintArray &gids;
|
||||
jint &runtime_flags;
|
||||
jint &mount_external;
|
||||
jstring &se_info;
|
||||
jstring &nice_name;
|
||||
jstring &instruction_set;
|
||||
jstring &app_data_dir;
|
||||
|
||||
jboolean *const is_child_zygote;
|
||||
jboolean *const is_top_app;
|
||||
jobjectArray *const pkg_data_info_list;
|
||||
jobjectArray *const whitelisted_data_info_list;
|
||||
jboolean *const mount_data_dirs;
|
||||
jboolean *const mount_storage_dirs;
|
||||
|
||||
AppSpecializeArgs_v1(const AppSpecializeArgs_v3 *v3) :
|
||||
uid(v3->uid), gid(v3->gid), gids(v3->gids), runtime_flags(v3->runtime_flags),
|
||||
mount_external(v3->mount_external), se_info(v3->se_info), nice_name(v3->nice_name),
|
||||
instruction_set(v3->instruction_set), app_data_dir(v3->app_data_dir),
|
||||
is_child_zygote(v3->is_child_zygote), is_top_app(v3->is_top_app),
|
||||
pkg_data_info_list(v3->pkg_data_info_list),
|
||||
whitelisted_data_info_list(v3->whitelisted_data_info_list),
|
||||
mount_data_dirs(v3->mount_data_dirs), mount_storage_dirs(v3->mount_storage_dirs) {}
|
||||
};
|
||||
|
||||
struct ServerSpecializeArgs_v1 {
|
||||
jint &uid;
|
||||
jint &gid;
|
||||
jintArray &gids;
|
||||
jint &runtime_flags;
|
||||
jlong &permitted_capabilities;
|
||||
jlong &effective_capabilities;
|
||||
|
||||
ServerSpecializeArgs_v1(
|
||||
jint &uid, jint &gid, jintArray &gids, jint &runtime_flags,
|
||||
jlong &permitted_capabilities, jlong &effective_capabilities) :
|
||||
uid(uid), gid(gid), gids(gids), runtime_flags(runtime_flags),
|
||||
permitted_capabilities(permitted_capabilities),
|
||||
effective_capabilities(effective_capabilities) {}
|
||||
};
|
||||
|
||||
struct module_abi_v1 {
|
||||
long api_version;
|
||||
void *impl;
|
||||
void (*preAppSpecialize)(void *, void *);
|
||||
void (*postAppSpecialize)(void *, const void *);
|
||||
void (*preServerSpecialize)(void *, void *);
|
||||
void (*postServerSpecialize)(void *, const void *);
|
||||
};
|
||||
|
||||
enum : uint32_t {
|
||||
PROCESS_GRANTED_ROOT = zygisk::StateFlag::PROCESS_GRANTED_ROOT,
|
||||
PROCESS_ON_DENYLIST = zygisk::StateFlag::PROCESS_ON_DENYLIST,
|
||||
|
||||
PROCESS_IS_SYS_UI = (1u << 29),
|
||||
DENYLIST_ENFORCING = (1u << 30),
|
||||
PROCESS_IS_MAGISK_APP = (1u << 31),
|
||||
|
||||
UNMOUNT_MASK = (PROCESS_ON_DENYLIST | DENYLIST_ENFORCING),
|
||||
PRIVATE_MASK = (PROCESS_IS_SYS_UI | DENYLIST_ENFORCING | PROCESS_IS_MAGISK_APP)
|
||||
};
|
||||
|
||||
struct api_abi_base {
|
||||
ZygiskModule *impl;
|
||||
bool (*registerModule)(ApiTable *, long *);
|
||||
};
|
||||
|
||||
struct api_abi_v1 : public api_abi_base {
|
||||
/* 0 */ void (*hookJniNativeMethods)(JNIEnv *, const char *, JNINativeMethod *, int);
|
||||
/* 1 */ void (*pltHookRegister)(const char *, const char *, void *, void **);
|
||||
/* 2 */ void (*pltHookExclude)(const char *, const char *);
|
||||
/* 3 */ bool (*pltHookCommit)();
|
||||
/* 4 */ int (*connectCompanion)(ZygiskModule *);
|
||||
/* 5 */ void (*setOption)(ZygiskModule *, zygisk::Option);
|
||||
};
|
||||
|
||||
struct api_abi_v2 : public api_abi_v1 {
|
||||
/* 6 */ int (*getModuleDir)(ZygiskModule *);
|
||||
/* 7 */ uint32_t (*getFlags)(ZygiskModule *);
|
||||
};
|
||||
|
||||
struct api_abi_v4 : public api_abi_base {
|
||||
/* 0 */ void (*hookJniNativeMethods)(JNIEnv *, const char *, JNINativeMethod *, int);
|
||||
/* 1 */ void (*pltHookRegister)(dev_t, ino_t, const char *, void *, void **);
|
||||
/* 2 */ bool (*exemptFd)(int);
|
||||
/* 3 */ bool (*pltHookCommit)();
|
||||
/* 4 */ int (*connectCompanion)(ZygiskModule *);
|
||||
/* 5 */ void (*setOption)(ZygiskModule *, zygisk::Option);
|
||||
/* 6 */ int (*getModuleDir)(ZygiskModule *);
|
||||
/* 7 */ uint32_t (*getFlags)(ZygiskModule *);
|
||||
};
|
||||
|
||||
union ApiTable {
|
||||
api_abi_base base;
|
||||
api_abi_v1 v1;
|
||||
api_abi_v2 v2;
|
||||
api_abi_v4 v4;
|
||||
};
|
||||
|
||||
#define call_app(method) \
|
||||
switch (*mod.api_version) { \
|
||||
case 1: \
|
||||
case 2: { \
|
||||
AppSpecializeArgs_v1 a(args); \
|
||||
mod.v1->method(mod.v1->impl, &a); \
|
||||
break; \
|
||||
} \
|
||||
case 3: \
|
||||
case 4: \
|
||||
mod.v1->method(mod.v1->impl, args);\
|
||||
break; \
|
||||
}
|
||||
|
||||
struct ZygiskModule {
|
||||
|
||||
void onLoad(void *env) {
|
||||
entry.fn(&api, env);
|
||||
}
|
||||
void preAppSpecialize(AppSpecializeArgs_v3 *args) const {
|
||||
call_app(preAppSpecialize)
|
||||
}
|
||||
void postAppSpecialize(const AppSpecializeArgs_v3 *args) const {
|
||||
call_app(postAppSpecialize)
|
||||
}
|
||||
void preServerSpecialize(ServerSpecializeArgs_v1 *args) const {
|
||||
mod.v1->preServerSpecialize(mod.v1->impl, args);
|
||||
}
|
||||
void postServerSpecialize(const ServerSpecializeArgs_v1 *args) const {
|
||||
mod.v1->postServerSpecialize(mod.v1->impl, args);
|
||||
}
|
||||
|
||||
bool valid() const;
|
||||
int connectCompanion() const;
|
||||
int getModuleDir() const;
|
||||
void setOption(zygisk::Option opt);
|
||||
static uint32_t getFlags();
|
||||
void tryUnload() const { if (unload) dlclose(handle); }
|
||||
void clearApi() { memset(&api, 0, sizeof(api)); }
|
||||
int getId() const { return id; }
|
||||
|
||||
ZygiskModule(int id, void *handle, void *entry);
|
||||
|
||||
static bool RegisterModuleImpl(ApiTable *api, long *module);
|
||||
|
||||
private:
|
||||
const int id;
|
||||
bool unload = false;
|
||||
|
||||
void * const handle;
|
||||
union {
|
||||
void * const ptr;
|
||||
void (* const fn)(void *, void *);
|
||||
} entry;
|
||||
|
||||
ApiTable api;
|
||||
|
||||
union {
|
||||
long *api_version;
|
||||
module_abi_v1 *v1;
|
||||
} mod;
|
||||
};
|
||||
|
||||
} // namespace
|
||||
29
native/src/core/zygisk/native_bridge.cpp
Normal file
29
native/src/core/zygisk/native_bridge.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <android/dlext.h>
|
||||
#include <dlfcn.h>
|
||||
#include <unwind.h>
|
||||
|
||||
#include <consts.hpp>
|
||||
#include <core.hpp>
|
||||
|
||||
#include "zygisk.hpp"
|
||||
|
||||
static bool is_compatible_with(uint32_t) {
|
||||
auto name = get_prop(NBPROP);
|
||||
android_dlextinfo info = {
|
||||
.flags = ANDROID_DLEXT_FORCE_LOAD
|
||||
};
|
||||
void *handle = android_dlopen_ext(name.data(), RTLD_LAZY, &info);
|
||||
if (handle) {
|
||||
auto entry = reinterpret_cast<void (*)(void *)>(dlsym(handle, "zygisk_inject_entry"));
|
||||
if (entry) {
|
||||
entry(handle);
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
extern "C" [[maybe_unused]] NativeBridgeCallbacks NativeBridgeItf{
|
||||
.version = 2,
|
||||
.padding = {},
|
||||
.isCompatibleWith = &is_compatible_with,
|
||||
};
|
||||
56
native/src/core/zygisk/zygisk.hpp
Normal file
56
native/src/core/zygisk/zygisk.hpp
Normal file
@@ -0,0 +1,56 @@
|
||||
#pragma once
|
||||
|
||||
#include <sys/mman.h>
|
||||
#include <stdint.h>
|
||||
#include <jni.h>
|
||||
#include <vector>
|
||||
#include <core.hpp>
|
||||
|
||||
namespace ZygiskRequest {
|
||||
enum : int {
|
||||
GET_INFO,
|
||||
CONNECT_COMPANION,
|
||||
GET_MODDIR,
|
||||
END
|
||||
};
|
||||
}
|
||||
|
||||
#if defined(__LP64__)
|
||||
#define ZLOGD(...) LOGD("zygisk64: " __VA_ARGS__)
|
||||
#define ZLOGE(...) LOGE("zygisk64: " __VA_ARGS__)
|
||||
#define ZLOGI(...) LOGI("zygisk64: " __VA_ARGS__)
|
||||
#define ZLOGW(...) LOGW("zygisk64: " __VA_ARGS__)
|
||||
#else
|
||||
#define ZLOGD(...) LOGD("zygisk32: " __VA_ARGS__)
|
||||
#define ZLOGE(...) LOGE("zygisk32: " __VA_ARGS__)
|
||||
#define ZLOGI(...) LOGI("zygisk32: " __VA_ARGS__)
|
||||
#define ZLOGW(...) LOGW("zygisk32: " __VA_ARGS__)
|
||||
#endif
|
||||
|
||||
extern void *self_handle;
|
||||
|
||||
void hook_functions();
|
||||
int remote_get_info(int uid, const char *process, uint32_t *flags, std::vector<int> &fds);
|
||||
|
||||
inline int zygisk_request(int req) {
|
||||
int fd = connect_daemon(MainRequest::ZYGISK);
|
||||
if (fd < 0) return fd;
|
||||
write_int(fd, req);
|
||||
return fd;
|
||||
}
|
||||
|
||||
// The reference of the following structs
|
||||
// https://cs.android.com/android/platform/superproject/main/+/main:art/libnativebridge/include/nativebridge/native_bridge.h
|
||||
|
||||
struct NativeBridgeRuntimeCallbacks {
|
||||
const char* (*getMethodShorty)(JNIEnv* env, jmethodID mid);
|
||||
uint32_t (*getNativeMethodCount)(JNIEnv* env, jclass clazz);
|
||||
uint32_t (*getNativeMethods)(JNIEnv* env, jclass clazz, JNINativeMethod* methods,
|
||||
uint32_t method_count);
|
||||
};
|
||||
|
||||
struct NativeBridgeCallbacks {
|
||||
uint32_t version;
|
||||
void *padding[5];
|
||||
bool (*isCompatibleWith)(uint32_t);
|
||||
};
|
||||
Reference in New Issue
Block a user