Guard all injection features behind a global flag

This commit is contained in:
topjohnwu 2021-01-13 20:07:23 -08:00
parent 1860e5d133
commit e8ba671fc2
6 changed files with 35 additions and 16 deletions

View File

@ -4,11 +4,14 @@ LOCAL_PATH := $(call my-dir)
# Binaries
########################
# Global toggle for the WIP zygote injection features
ENABLE_INJECT := 0
ifdef B_MAGISK
include $(CLEAR_VARS)
LOCAL_MODULE := magisk
LOCAL_STATIC_LIBRARIES := libnanopb libsystemproperties libutils libxhook
LOCAL_STATIC_LIBRARIES := libnanopb libsystemproperties libutils
LOCAL_C_INCLUDES := jni/include
LOCAL_SRC_FILES := \
@ -22,7 +25,6 @@ LOCAL_SRC_FILES := \
core/restorecon.cpp \
core/module.cpp \
magiskhide/magiskhide.cpp \
magiskhide/proc_monitor.cpp \
magiskhide/hide_utils.cpp \
magiskhide/hide_policy.cpp \
resetprop/persist_properties.cpp \
@ -30,12 +32,21 @@ LOCAL_SRC_FILES := \
su/su.cpp \
su/connect.cpp \
su/pts.cpp \
su/su_daemon.cpp \
su/su_daemon.cpp
LOCAL_LDLIBS := -llog
LOCAL_CPPFLAGS := -DENABLE_INJECT=$(ENABLE_INJECT)
ifeq ($(ENABLE_INJECT),1)
LOCAL_STATIC_LIBRARIES += libxhook
LOCAL_SRC_FILES += \
inject/entry.cpp \
inject/utils.cpp \
inject/hook.cpp
else
LOCAL_SRC_FILES += magiskhide/proc_monitor.cpp
endif
LOCAL_LDLIBS := -llog
include $(BUILD_EXECUTABLE)
endif

View File

@ -20,10 +20,11 @@ static int call_applet(int argc, char *argv[]) {
return (*applet_main[i])(argc, argv);
}
}
#if ENABLE_INJECT
if (str_starts(base, "app_process")) {
return app_process_main(argc, argv);
}
#endif
fprintf(stderr, "%s: applet not found\n", base.data());
return 1;
}

View File

@ -51,6 +51,8 @@ void su_daemon_handler(int client, ucred *credential);
void auto_start_magiskhide(bool late_props);
int stop_magiskhide();
#if ENABLE_INJECT
// For injected process to access daemon
int remote_check_hide(int uid, const char *process);
void remote_request_hide();
#endif

View File

@ -20,7 +20,7 @@ map<int, vector<string_view>> uid_proc_map; /* uid -> list of process */
// Locks the variables above
pthread_mutex_t hide_state_lock = PTHREAD_MUTEX_INITIALIZER;
#if ENABLE_PTRACE_MONITOR
#if !ENABLE_INJECT
static pthread_t monitor_thread;
#endif
@ -290,7 +290,7 @@ int launch_magiskhide(bool late_props) {
if (late_props)
hide_late_sensitive_props();
#if ENABLE_PTRACE_MONITOR
#if !ENABLE_INJECT
// Start monitoring
if (new_daemon_thread(&proc_monitor))
return DAEMON_ERROR;
@ -313,7 +313,7 @@ int stop_magiskhide() {
LOGI("* Disable MagiskHide\n");
uid_proc_map.clear();
hide_set.clear();
#if ENABLE_PTRACE_MONITOR
#if !ENABLE_INJECT
pthread_kill(monitor_thread, SIGTERMTHRD);
#endif
}
@ -325,7 +325,7 @@ int stop_magiskhide() {
void auto_start_magiskhide(bool late_props) {
if (hide_enabled()) {
#if ENABLE_PTRACE_MONITOR
#if !ENABLE_INJECT
pthread_kill(monitor_thread, SIGALRM);
#endif
hide_late_sensitive_props();
@ -337,7 +337,7 @@ void auto_start_magiskhide(bool late_props) {
}
}
#if ENABLE_PTRACE_MONITOR
#if !ENABLE_INJECT
void test_proc_monitor() {
if (procfp == nullptr && (procfp = opendir("/proc")) == nullptr)
exit(1);
@ -345,6 +345,7 @@ void test_proc_monitor() {
}
#endif
#if ENABLE_INJECT
int check_uid_map(int client) {
mutex_guard lock(hide_state_lock);
@ -377,3 +378,4 @@ int check_uid_map(int client) {
return 0;
}
#endif

View File

@ -62,6 +62,7 @@ void magiskhide_handler(int client, ucred *cred) {
case HIDE_STATUS:
res = hide_enabled() ? HIDE_IS_ENABLED : HIDE_NOT_ENABLED;
break;
#if ENABLE_INJECT
case REMOTE_CHECK_HIDE:
res = check_uid_map(client);
break;
@ -71,6 +72,7 @@ void magiskhide_handler(int client, ucred *cred) {
hide_daemon(cred->pid);
close(client);
return;
#endif
}
write_int(client, res);
@ -106,7 +108,7 @@ int magiskhide_main(int argc, char *argv[]) {
execvp(argv[2], argv + 2);
exit(1);
}
#if 0 && ENABLE_PTRACE_MONITOR
#if 0 && !ENABLE_INJECT
else if (opt == "test"sv)
test_proc_monitor();
#endif
@ -168,6 +170,7 @@ return_code:
return req == HIDE_STATUS ? (code == HIDE_IS_ENABLED ? 0 : 1) : code != DAEMON_SUCCESS;
}
#if ENABLE_INJECT
int remote_check_hide(int uid, const char *process) {
int fd = connect_daemon();
write_int(fd, MAGISKHIDE);
@ -190,3 +193,4 @@ void remote_request_hide() {
close(fd);
}
#endif

View File

@ -14,21 +14,20 @@
#define SIGTERMTHRD SIGUSR1
#define ISOLATED_MAGIC "isolated"
// Global toggle for ptrace monitor
#define ENABLE_PTRACE_MONITOR 0
// CLI entries
int launch_magiskhide(bool late_props);
int stop_magiskhide();
int add_list(int client);
int rm_list(int client);
void ls_list(int client);
int check_uid_map(int client);
#if ENABLE_PTRACE_MONITOR
#if !ENABLE_INJECT
// Process monitoring
[[noreturn]] void proc_monitor();
[[noreturn]] void test_proc_monitor();
#else
// Response whether target process should be hidden
int check_uid_map(int client);
#endif
// Utility functions