From 58849f28a8d5db957bd168ef84176c16a73893b8 Mon Sep 17 00:00:00 2001 From: topjohnwu Date: Fri, 5 May 2017 16:13:26 +0800 Subject: [PATCH] Add daemon response code --- jni/daemon/daemon.c | 26 +++++++++++++++++++++++--- jni/daemon/daemon.h | 12 ++++++++++++ jni/magiskhide/list_manager.c | 12 ++++++------ jni/magiskhide/magiskhide.c | 19 ++++++++++--------- jni/magiskhide/magiskhide.h | 9 --------- jni/su | 2 +- jni/utils/misc.c | 6 ++++++ jni/utils/utils.h | 1 + 8 files changed, 59 insertions(+), 28 deletions(-) diff --git a/jni/daemon/daemon.c b/jni/daemon/daemon.c index e07da923c..98269b3e4 100644 --- a/jni/daemon/daemon.c +++ b/jni/daemon/daemon.c @@ -32,6 +32,27 @@ static void *request_handler(void *args) { int client = *((int *) args); free(args); client_request req = read_int(client); + + struct ucred credentials; + get_client_cred(client, &credentials); + + switch (req) { + case LAUNCH_MAGISKHIDE: + case STOP_MAGISKHIDE: + case ADD_HIDELIST: + case RM_HIDELIST: + case POST_FS: + case POST_FS_DATA: + case LATE_START: + if (credentials.uid != 0) { + write_int(client, ROOT_REQUIRED); + close(client); + return NULL; + } + default: + break; + } + switch (req) { case LAUNCH_MAGISKHIDE: launch_magiskhide(client); @@ -65,9 +86,8 @@ static void *request_handler(void *args) { case LATE_START: late_start(client); break; - case TEST: - // test(client); - break; + default: + close(client); } return NULL; } diff --git a/jni/daemon/daemon.h b/jni/daemon/daemon.h index 7b2db07da..5288d6aed 100644 --- a/jni/daemon/daemon.h +++ b/jni/daemon/daemon.h @@ -10,6 +10,7 @@ extern pthread_t sepol_patch; // Commands require connecting to daemon typedef enum { + DO_NOTHING = 0, LAUNCH_MAGISKHIDE, STOP_MAGISKHIDE, ADD_HIDELIST, @@ -23,6 +24,17 @@ typedef enum { TEST } client_request; +// Return codes for daemon +typedef enum { + DAEMON_ERROR = -1, + DAEMON_SUCCESS = 0, + ROOT_REQUIRED, + HIDE_IS_ENABLED, + HIDE_NOT_ENABLED, + HIDE_ITEM_EXIST, + HIDE_ITEM_NOT_EXIST, +} daemon_response; + // daemon.c void start_daemon(); diff --git a/jni/magiskhide/list_manager.c b/jni/magiskhide/list_manager.c index 75b2636a5..683920a66 100644 --- a/jni/magiskhide/list_manager.c +++ b/jni/magiskhide/list_manager.c @@ -19,7 +19,7 @@ int add_list(char *proc) { char *line; struct vector *new_list = xmalloc(sizeof(*new_list)); if (new_list == NULL) - return HIDE_ERROR; + return DAEMON_ERROR; vec_init(new_list); vec_for_each(hide_list, line) { @@ -47,10 +47,10 @@ int add_list(char *proc) { pthread_mutex_lock(&file_lock); if (vector_to_file(HIDELIST, hide_list)) { pthread_mutex_unlock(&file_lock); - return HIDE_ERROR; + return DAEMON_ERROR; } pthread_mutex_unlock(&file_lock); - return HIDE_SUCCESS; + return DAEMON_SUCCESS; } int rm_list(char *proc) { @@ -59,7 +59,7 @@ int rm_list(char *proc) { return HIDE_NOT_ENABLED; } - hide_ret ret = HIDE_ERROR; + daemon_response ret = DAEMON_ERROR; char *line; int do_rm = 0; struct vector *new_list = xmalloc(sizeof(*new_list)); @@ -87,10 +87,10 @@ int rm_list(char *proc) { hide_list = new_list; pthread_mutex_unlock(&hide_lock); - ret = HIDE_SUCCESS; + ret = DAEMON_SUCCESS; pthread_mutex_lock(&file_lock); if (vector_to_file(HIDELIST, hide_list)) - ret = HIDE_ERROR; + ret = DAEMON_ERROR; pthread_mutex_unlock(&file_lock); } else { ret = HIDE_ITEM_NOT_EXIST; diff --git a/jni/magiskhide/magiskhide.c b/jni/magiskhide/magiskhide.c index 07a979c4c..bef800455 100644 --- a/jni/magiskhide/magiskhide.c +++ b/jni/magiskhide/magiskhide.c @@ -1,8 +1,6 @@ /* magiskhide.c - initialize the environment for Magiskhide */ -// TODO: Functions to modify hiding list - #include #include #include @@ -88,7 +86,7 @@ void launch_magiskhide(int client) { pthread_mutex_init(&hide_lock, NULL); pthread_mutex_init(&file_lock, NULL); - write_int(client, HIDE_SUCCESS); + write_int(client, DAEMON_SUCCESS); close(client); // Get thread reference @@ -99,7 +97,7 @@ void launch_magiskhide(int client) { error: hideEnabled = 0; - write_int(client, HIDE_ERROR); + write_int(client, DAEMON_ERROR); close(client); if (hide_pid != -1) { int kill = -1; @@ -125,7 +123,7 @@ void stop_magiskhide(int client) { setprop("persist.magisk.hide", "0"); pthread_kill(proc_monitor_thread, SIGUSR1); - write_int(client, HIDE_SUCCESS); + write_int(client, DAEMON_SUCCESS); close(client); } @@ -133,7 +131,7 @@ int magiskhide_main(int argc, char *argv[]) { if (argc < 2) { usage(argv[0]); } - client_request req; + client_request req = DO_NOTHING; if (strcmp(argv[1], "--enable") == 0) { req = LAUNCH_MAGISKHIDE; } else if (strcmp(argv[1], "--disable") == 0) { @@ -156,13 +154,16 @@ int magiskhide_main(int argc, char *argv[]) { if (req == ADD_HIDELIST || req == RM_HIDELIST) { write_string(fd, argv[2]); } - hide_ret code = read_int(fd); + daemon_response code = read_int(fd); close(fd); switch (code) { - case HIDE_ERROR: + case DAEMON_ERROR: fprintf(stderr, "Error occured in daemon...\n"); break; - case HIDE_SUCCESS: + case DAEMON_SUCCESS: + break; + case ROOT_REQUIRED: + fprintf(stderr, "Root is required for this operation\n"); break; case HIDE_NOT_ENABLED: fprintf(stderr, "Magisk hide is not enabled yet\n"); diff --git a/jni/magiskhide/magiskhide.h b/jni/magiskhide/magiskhide.h index 96c80820c..f36d4a23f 100644 --- a/jni/magiskhide/magiskhide.h +++ b/jni/magiskhide/magiskhide.h @@ -6,15 +6,6 @@ #define HIDELIST "/magisk/.core/magiskhide/hidelist" #define DUMMYPATH "/dev/magisk/dummy" -typedef enum { - HIDE_ERROR = -1, - HIDE_SUCCESS = 0, - HIDE_IS_ENABLED, - HIDE_NOT_ENABLED, - HIDE_ITEM_EXIST, - HIDE_ITEM_NOT_EXIST -} hide_ret; - // Kill process void kill_proc(int pid); diff --git a/jni/su b/jni/su index 9120f71f1..9a1043574 160000 --- a/jni/su +++ b/jni/su @@ -1 +1 @@ -Subproject commit 9120f71f197e3cabf68c55154e30501679fb3aa8 +Subproject commit 9a1043574a0109e98872bc6e3fe2dcd2f406849a diff --git a/jni/utils/misc.c b/jni/utils/misc.c index d2dffce08..73898f5c7 100644 --- a/jni/utils/misc.c +++ b/jni/utils/misc.c @@ -386,3 +386,9 @@ void clone_attr(const char *source, const char *target) { lsetfilecon(target, con); free(con); } + +void get_client_cred(int fd, struct ucred *cred) { + socklen_t ucred_length = sizeof(*cred); + if(getsockopt(fd, SOL_SOCKET, SO_PEERCRED, cred, &ucred_length)) + PLOGE("getsockopt"); +} diff --git a/jni/utils/utils.h b/jni/utils/utils.h index 12ce637f4..c407b4869 100644 --- a/jni/utils/utils.h +++ b/jni/utils/utils.h @@ -89,5 +89,6 @@ int cp_afc(const char *source, const char *target); int clone_dir(const char *source, const char *target); int rm_rf(const char *target); void clone_attr(const char *source, const char *target); +void get_client_cred(int fd, struct ucred *cred); #endif