Magisk/native/jni/zygisk/entry.cpp

363 lines
10 KiB
C++
Raw Normal View History

#include <libgen.h>
#include <dlfcn.h>
#include <sys/mount.h>
#include <sys/sendfile.h>
2021-01-06 23:41:37 -08:00
#include <sys/prctl.h>
2021-08-21 03:52:59 -07:00
#include <android/log.h>
#include <utils.hpp>
2021-08-18 03:44:32 -07:00
#include <daemon.hpp>
#include <magisk.hpp>
2021-09-18 02:38:53 -07:00
#include <db.hpp>
2021-10-05 03:53:11 -07:00
#include "zygisk.hpp"
2021-09-18 14:50:11 -07:00
#include "deny/deny.hpp"
using namespace std;
static void *self_handle = nullptr;
2021-08-21 03:52:59 -07:00
static int zygisk_log(int prio, const char *fmt, va_list ap);
#define zlog(prio) [](auto fmt, auto ap){ return zygisk_log(ANDROID_LOG_##prio, fmt, ap); }
static void zygisk_logging() {
log_cb.d = zlog(DEBUG);
log_cb.i = zlog(INFO);
log_cb.w = zlog(WARN);
log_cb.e = zlog(ERROR);
log_cb.ex = nop_ex;
}
2021-01-08 00:53:24 -08:00
void self_unload() {
2021-10-14 02:13:23 -07:00
ZLOGD("Request to self unload\n");
2021-09-22 02:46:07 -07:00
// If unhooking failed, do not unload or else it will cause SIGSEGV
2021-01-08 00:53:24 -08:00
if (!unhook_functions())
return;
new_daemon_thread(reinterpret_cast<thread_entry>(&dlclose), self_handle);
}
2021-09-22 02:46:07 -07:00
static void unload_first_stage(int, siginfo_t *info, void *) {
auto path = static_cast<char *>(info->si_value.sival_ptr);
2021-08-18 03:44:32 -07:00
unmap_all(path);
2021-09-22 02:46:07 -07:00
free(path);
struct sigaction action{};
action.sa_handler = SIG_DFL;
sigaction(SIGUSR1, &action, nullptr);
}
2021-09-18 05:11:10 -07:00
// Make sure /proc/self/environ is sanitized
// Filter env and reset MM_ENV_END
2021-01-06 23:41:37 -08:00
static void sanitize_environ() {
2021-09-18 05:11:10 -07:00
char *cur = environ[0];
2021-01-06 23:41:37 -08:00
for (int i = 0; environ[i]; ++i) {
2021-09-22 00:14:22 -07:00
// Copy all env onto the original stack
2021-09-18 05:11:10 -07:00
int len = strlen(environ[i]);
memmove(cur, environ[i], len + 1);
environ[i] = cur;
cur += len + 1;
2021-01-06 23:41:37 -08:00
}
2021-09-18 05:11:10 -07:00
prctl(PR_SET_MM, PR_SET_MM_ENV_END, cur, 0, 0);
2021-01-06 23:41:37 -08:00
}
__attribute__((destructor))
2021-09-22 00:14:22 -07:00
static void zygisk_cleanup_wait() {
2021-09-22 02:52:33 -07:00
if (self_handle) {
// Wait 10us to make sure none of our code is executing
timespec ts = { .tv_sec = 0, .tv_nsec = 10000L };
nanosleep(&ts, nullptr);
}
}
2021-09-22 00:14:22 -07:00
#define SECOND_STAGE_PTR "ZYGISK_PTR"
static void second_stage_entry(void *handle, char *path) {
2021-09-22 00:14:22 -07:00
self_handle = handle;
unsetenv(INJECT_ENV_2);
unsetenv(SECOND_STAGE_PTR);
zygisk_logging();
2021-10-14 02:13:23 -07:00
ZLOGD("inject 2nd stage\n");
2021-09-22 00:14:22 -07:00
hook_functions();
// Register signal handler to unload 1st stage
struct sigaction action{};
action.sa_sigaction = unload_first_stage;
sigaction(SIGUSR1, &action, nullptr);
// Schedule to unload 1st stage 10us later
timer_t timer;
sigevent_t event{};
event.sigev_notify = SIGEV_SIGNAL;
event.sigev_signo = SIGUSR1;
event.sigev_value.sival_ptr = path;
timer_create(CLOCK_MONOTONIC, &event, &timer);
itimerspec time{};
time.it_value.tv_nsec = 10000L;
timer_settime(&timer, 0, &time, nullptr);
2021-09-22 00:14:22 -07:00
}
static void first_stage_entry() {
android_logging();
2021-10-14 02:13:23 -07:00
ZLOGD("inject 1st stage\n");
2021-09-22 00:14:22 -07:00
char *ld = getenv("LD_PRELOAD");
char *path;
if (char *c = strrchr(ld, ':')) {
*c = '\0';
setenv("LD_PRELOAD", ld, 1); // Restore original LD_PRELOAD
path = strdup(c + 1);
} else {
unsetenv("LD_PRELOAD");
path = strdup(ld);
}
unsetenv(INJECT_ENV_1);
sanitize_environ();
// Update path to 2nd stage lib
*(strrchr(path, '.') - 1) = '2';
2021-09-22 00:14:22 -07:00
// Load second stage
setenv(INJECT_ENV_2, "1", 1);
void *handle = dlopen(path, RTLD_LAZY);
remap_all(path);
2021-08-18 03:44:32 -07:00
2021-09-22 00:14:22 -07:00
// Revert path to 1st stage lib
*(strrchr(path, '.') - 1) = '1';
// Run second stage entry
char *env = getenv(SECOND_STAGE_PTR);
decltype(&second_stage_entry) second_stage;
sscanf(env, "%p", &second_stage);
second_stage(handle, path);
2021-09-22 00:14:22 -07:00
}
__attribute__((constructor))
static void zygisk_init() {
if (getenv(INJECT_ENV_2)) {
// Return function pointer to first stage
char buf[128];
snprintf(buf, sizeof(buf), "%p", &second_stage_entry);
setenv(SECOND_STAGE_PTR, buf, 1);
2021-08-18 03:44:32 -07:00
} else if (getenv(INJECT_ENV_1)) {
2021-09-22 00:14:22 -07:00
first_stage_entry();
}
}
2021-08-19 04:55:17 -07:00
// Start code for magiskd IPC
int app_process_main(int argc, char *argv[]) {
2021-08-21 03:52:59 -07:00
android_logging();
2021-08-18 03:44:32 -07:00
if (int fd = connect_daemon(); fd >= 0) {
write_int(fd, ZYGISK_REQUEST);
write_int(fd, ZYGISK_SETUP);
if (read_int(fd) == 0) {
string path = read_string(fd);
string lib = path + ".1.so";
if (char *ld = getenv("LD_PRELOAD")) {
char env[256];
sprintf(env, "%s:%s", ld, lib.data());
setenv("LD_PRELOAD", env, 1);
} else {
setenv("LD_PRELOAD", lib.data(), 1);
}
setenv(INJECT_ENV_1, "1", 1);
}
close(fd);
}
// Execute real app_process
2021-08-18 03:44:32 -07:00
char buf[256];
xreadlink("/proc/self/exe", buf, sizeof(buf));
xumount2("/proc/self/exe", MNT_DETACH);
execve(buf, argv, environ);
2021-01-06 23:41:37 -08:00
return 1;
}
2021-08-18 03:44:32 -07:00
2021-08-21 03:52:59 -07:00
static int zygisk_log(int prio, const char *fmt, va_list ap) {
// If we don't have log pipe set, ask magiskd for it
// This could happen multiple times in zygote because it was closed to prevent crashing
if (logd_fd < 0) {
// Change logging temporarily to prevent infinite recursion and stack overflow
android_logging();
if (int fd = connect_daemon(); fd >= 0) {
write_int(fd, ZYGISK_REQUEST);
write_int(fd, ZYGISK_GET_LOG_PIPE);
if (read_int(fd) == 0) {
logd_fd = recv_fd(fd);
}
close(fd);
}
zygisk_logging();
}
sigset_t mask;
sigset_t orig_mask;
bool sig = false;
// Make sure SIGPIPE won't crash zygote
if (logd_fd >= 0) {
sig = true;
sigemptyset(&mask);
sigaddset(&mask, SIGPIPE);
pthread_sigmask(SIG_BLOCK, &mask, &orig_mask);
}
int ret = magisk_log(prio, fmt, ap);
if (sig) {
timespec ts{};
sigtimedwait(&mask, nullptr, &ts);
pthread_sigmask(SIG_SETMASK, &orig_mask, nullptr);
}
return ret;
}
2021-10-13 04:52:02 -07:00
std::vector<int> remote_get_info(int uid, const char *process, AppInfo *info) {
vector<int> fds;
2021-08-19 04:55:17 -07:00
if (int fd = connect_daemon(); fd >= 0) {
write_int(fd, ZYGISK_REQUEST);
2021-10-13 04:52:02 -07:00
write_int(fd, ZYGISK_GET_INFO);
2021-09-18 02:38:53 -07:00
write_int(fd, uid);
write_string(fd, process);
xxread(fd, info, sizeof(*info));
2021-10-13 04:52:02 -07:00
if (!info->on_denylist) {
fds = recv_fds(fd);
}
2021-08-19 04:55:17 -07:00
close(fd);
}
2021-10-13 04:52:02 -07:00
return fds;
2021-08-19 04:55:17 -07:00
}
int remote_request_unmount() {
if (int fd = connect_daemon(); fd >= 0) {
write_int(fd, ZYGISK_REQUEST);
write_int(fd, ZYGISK_UNMOUNT);
int ret = read_int(fd);
close(fd);
return ret;
}
return DAEMON_ERROR;
}
2021-08-18 03:44:32 -07:00
// The following code runs in magiskd
2021-10-23 14:38:30 -07:00
static bool get_exe(int pid, char *buf, size_t sz) {
snprintf(buf, sz, "/proc/%d/exe", pid);
return xreadlink(buf, buf, sz) > 0;
}
2021-10-19 23:46:38 -07:00
static void setup_files(int client, const sock_cred *cred) {
2021-08-22 02:11:48 -07:00
LOGD("zygisk: setup files for pid=[%d]\n", cred->pid);
2021-08-18 03:44:32 -07:00
2021-08-22 02:11:48 -07:00
char buf[256];
2021-10-23 14:38:30 -07:00
if (!get_exe(cred->pid, buf, sizeof(buf))) {
2021-08-18 03:44:32 -07:00
write_int(client, 1);
return;
}
write_int(client, 0);
2021-09-15 01:59:43 -07:00
string path = MAGISKTMP + "/" ZYGISKBIN "/zygisk." + basename(buf);
2021-08-18 03:44:32 -07:00
cp_afc(buf, (path + ".1.so").data());
cp_afc(buf, (path + ".2.so").data());
write_string(client, path);
}
2021-09-20 05:47:15 -07:00
int cached_manager_app_id = -1;
static time_t last_modified = 0;
2021-10-19 23:46:38 -07:00
static void get_process_info(int client, const sock_cred *cred) {
2021-09-18 02:38:53 -07:00
AppInfo info{};
2021-08-19 04:55:17 -07:00
int uid = read_int(client);
string process = read_string(client);
2021-09-20 05:47:15 -07:00
2021-10-13 04:52:02 -07:00
// This function is called on every single zygote process specialization,
2021-09-20 05:47:15 -07:00
// so performance is critical. get_manager_app_id() is expensive as it goes
// through a SQLite query and potentially multiple filesystem stats, so we
// really want to cache the app ID value. Check the last modify timestamp of
// packages.xml and only re-fetch the manager app ID if something changed since
// we last checked. Granularity in seconds is good enough.
// If denylist is enabled, inotify will invalidate the app ID cache for us.
// In this case, we can skip the timestamp check all together.
2021-10-13 04:52:02 -07:00
if (uid != 1000) {
int manager_app_id = cached_manager_app_id;
2021-09-20 05:47:15 -07:00
2021-10-13 04:52:02 -07:00
// Denylist not enabled, check packages.xml timestamp
if (!denylist_enabled && manager_app_id > 0) {
struct stat st{};
stat("/data/system/packages.xml", &st);
if (st.st_atim.tv_sec > last_modified) {
manager_app_id = -1;
last_modified = st.st_atim.tv_sec;
}
2021-09-20 05:47:15 -07:00
}
2021-10-13 04:52:02 -07:00
if (manager_app_id < 0) {
manager_app_id = get_manager_app_id();
cached_manager_app_id = manager_app_id;
}
2021-09-20 05:47:15 -07:00
2021-10-13 04:52:02 -07:00
if (to_app_id(uid) == manager_app_id) {
info.is_magisk_app = true;
} else if (denylist_enabled) {
info.on_denylist = is_deny_target(uid, process);
}
2021-09-18 02:38:53 -07:00
}
2021-10-13 04:52:02 -07:00
2021-09-18 02:38:53 -07:00
xwrite(client, &info, sizeof(info));
2021-10-13 04:52:02 -07:00
if (!info.on_denylist) {
char buf[256];
2021-10-23 14:38:30 -07:00
get_exe(cred->pid, buf, sizeof(buf));
2021-10-13 04:52:02 -07:00
vector<int> fds = zygisk_module_fds(str_ends(buf, "64"));
send_fds(client, fds.data(), fds.size());
}
2021-08-19 04:55:17 -07:00
}
2021-10-19 23:46:38 -07:00
static void do_unmount(int client, const sock_cred *cred) {
2021-09-16 05:27:34 -07:00
if (denylist_enabled) {
2021-09-12 12:40:34 -07:00
LOGD("zygisk: cleanup mount namespace for pid=[%d]\n", cred->pid);
revert_daemon(cred->pid, client);
2021-08-19 04:55:17 -07:00
} else {
2021-09-12 12:40:34 -07:00
write_int(client, DENY_NOT_ENFORCED);
2021-08-19 04:55:17 -07:00
}
}
2021-08-21 03:52:59 -07:00
static void send_log_pipe(int fd) {
// There is race condition here, but we can't really do much about it...
2021-08-22 02:11:48 -07:00
if (logd_fd >= 0) {
2021-08-21 03:52:59 -07:00
write_int(fd, 0);
send_fd(fd, logd_fd);
} else {
write_int(fd, 1);
}
}
2021-10-19 23:46:38 -07:00
void zygisk_handler(int client, const sock_cred *cred) {
2021-08-18 03:44:32 -07:00
int code = read_int(client);
2021-10-23 14:38:30 -07:00
char buf[256];
2021-08-18 03:44:32 -07:00
switch (code) {
case ZYGISK_SETUP:
setup_files(client, cred);
break;
2021-10-13 04:52:02 -07:00
case ZYGISK_GET_INFO:
get_process_info(client, cred);
2021-08-19 04:55:17 -07:00
break;
case ZYGISK_UNMOUNT:
do_unmount(client, cred);
break;
2021-08-21 03:52:59 -07:00
case ZYGISK_GET_LOG_PIPE:
send_log_pipe(client);
break;
2021-10-23 14:38:30 -07:00
case ZYGISK_CONNECT_COMPANION:
get_exe(cred->pid, buf, sizeof(buf));
connect_companion(client, str_ends(buf, "64"));
2021-10-17 04:36:18 -07:00
break;
2021-08-18 03:44:32 -07:00
}
close(client);
}