2017-04-09 07:25:10 +08:00
|
|
|
#include <sys/wait.h>
|
2019-06-04 22:27:19 -07:00
|
|
|
#include <sys/mount.h>
|
2016-12-31 02:44:24 +08:00
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include <utils.hpp>
|
2019-02-10 03:57:51 -05:00
|
|
|
|
2020-03-09 01:50:30 -07:00
|
|
|
#include "magiskhide.hpp"
|
2016-12-31 02:44:24 +08:00
|
|
|
|
2021-01-12 00:07:48 -08:00
|
|
|
using namespace std;
|
2019-05-25 16:08:53 -07:00
|
|
|
|
2018-11-01 14:08:33 -04:00
|
|
|
[[noreturn]] static void usage(char *arg0) {
|
2020-12-30 22:11:24 -08:00
|
|
|
fprintf(stderr,
|
|
|
|
"MagiskHide - Hide Config CLI\n\n"
|
|
|
|
"Usage: %s [action [arguments...] ]\n\n"
|
|
|
|
"Actions:\n"
|
|
|
|
" status Return the status of magiskhide\n"
|
|
|
|
" enable Start magiskhide\n"
|
|
|
|
" disable Stop magiskhide\n"
|
|
|
|
" add PKG [PROC] Add a new target to the hide list\n"
|
|
|
|
" rm PKG [PROC] Remove target(s) from the hide list\n"
|
|
|
|
" ls Print the current hide list\n"
|
|
|
|
" exec CMDs... Execute commands in isolated mount\n"
|
|
|
|
" namespace and do all hide unmounts\n"
|
2019-05-26 02:53:28 -07:00
|
|
|
#ifdef MAGISK_DEBUG
|
2020-12-30 22:11:24 -08:00
|
|
|
" test Run process monitor test\n"
|
2019-05-26 02:53:28 -07:00
|
|
|
#endif
|
2020-12-30 22:11:24 -08:00
|
|
|
, arg0);
|
|
|
|
exit(1);
|
2017-04-09 07:25:10 +08:00
|
|
|
}
|
|
|
|
|
2021-01-12 03:28:00 -08:00
|
|
|
void magiskhide_handler(int client, ucred *cred) {
|
2020-12-30 22:11:24 -08:00
|
|
|
int req = read_int(client);
|
|
|
|
int res = DAEMON_ERROR;
|
2018-11-01 14:08:33 -04:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
switch (req) {
|
|
|
|
case STOP_MAGISKHIDE:
|
|
|
|
case ADD_HIDELIST:
|
|
|
|
case RM_HIDELIST:
|
|
|
|
case LS_HIDELIST:
|
|
|
|
if (!hide_enabled()) {
|
|
|
|
write_int(client, HIDE_NOT_ENABLED);
|
|
|
|
close(client);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
2018-11-01 14:08:33 -04:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
switch (req) {
|
|
|
|
case LAUNCH_MAGISKHIDE:
|
2021-01-11 02:19:10 -08:00
|
|
|
res = launch_magiskhide(true);
|
2020-12-30 22:11:24 -08:00
|
|
|
break;
|
|
|
|
case STOP_MAGISKHIDE:
|
|
|
|
res = stop_magiskhide();
|
|
|
|
break;
|
|
|
|
case ADD_HIDELIST:
|
|
|
|
res = add_list(client);
|
|
|
|
break;
|
|
|
|
case RM_HIDELIST:
|
|
|
|
res = rm_list(client);
|
|
|
|
break;
|
|
|
|
case LS_HIDELIST:
|
|
|
|
ls_list(client);
|
|
|
|
return;
|
|
|
|
case HIDE_STATUS:
|
|
|
|
res = hide_enabled() ? HIDE_IS_ENABLED : HIDE_NOT_ENABLED;
|
|
|
|
break;
|
2021-01-13 20:07:23 -08:00
|
|
|
#if ENABLE_INJECT
|
2021-01-12 03:28:00 -08:00
|
|
|
case REMOTE_CHECK_HIDE:
|
|
|
|
res = check_uid_map(client);
|
|
|
|
break;
|
|
|
|
case REMOTE_DO_HIDE:
|
|
|
|
kill(cred->pid, SIGSTOP);
|
|
|
|
write_int(client, 0);
|
|
|
|
hide_daemon(cred->pid);
|
|
|
|
close(client);
|
|
|
|
return;
|
2021-01-13 20:07:23 -08:00
|
|
|
#endif
|
2020-12-30 22:11:24 -08:00
|
|
|
}
|
2018-11-01 14:08:33 -04:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
write_int(client, res);
|
|
|
|
close(client);
|
2017-04-06 06:12:29 +08:00
|
|
|
}
|
2016-12-31 02:44:24 +08:00
|
|
|
|
2017-04-06 06:12:29 +08:00
|
|
|
int magiskhide_main(int argc, char *argv[]) {
|
2020-12-30 22:11:24 -08:00
|
|
|
if (argc < 2)
|
|
|
|
usage(argv[0]);
|
2018-11-16 00:37:41 -05:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
// CLI backwards compatibility
|
|
|
|
const char *opt = argv[1];
|
|
|
|
if (opt[0] == '-' && opt[1] == '-')
|
|
|
|
opt += 2;
|
2019-05-26 02:59:38 -07:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
int req;
|
|
|
|
if (opt == "enable"sv)
|
|
|
|
req = LAUNCH_MAGISKHIDE;
|
|
|
|
else if (opt == "disable"sv)
|
|
|
|
req = STOP_MAGISKHIDE;
|
|
|
|
else if (opt == "add"sv)
|
|
|
|
req = ADD_HIDELIST;
|
|
|
|
else if (opt == "rm"sv)
|
|
|
|
req = RM_HIDELIST;
|
|
|
|
else if (opt == "ls"sv)
|
|
|
|
req = LS_HIDELIST;
|
|
|
|
else if (opt == "status"sv)
|
|
|
|
req = HIDE_STATUS;
|
|
|
|
else if (opt == "exec"sv && argc > 2) {
|
|
|
|
xunshare(CLONE_NEWNS);
|
|
|
|
xmount(nullptr, "/", nullptr, MS_PRIVATE | MS_REC, nullptr);
|
|
|
|
hide_unmount();
|
|
|
|
execvp(argv[2], argv + 2);
|
|
|
|
exit(1);
|
|
|
|
}
|
2021-01-13 20:07:23 -08:00
|
|
|
#if 0 && !ENABLE_INJECT
|
2020-12-30 22:11:24 -08:00
|
|
|
else if (opt == "test"sv)
|
|
|
|
test_proc_monitor();
|
2019-05-26 02:53:28 -07:00
|
|
|
#endif
|
2020-12-30 22:11:24 -08:00
|
|
|
else
|
|
|
|
usage(argv[0]);
|
2018-11-01 14:08:33 -04:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
// Send request
|
|
|
|
int fd = connect_daemon();
|
|
|
|
write_int(fd, MAGISKHIDE);
|
|
|
|
write_int(fd, req);
|
|
|
|
if (req == ADD_HIDELIST || req == RM_HIDELIST) {
|
|
|
|
write_string(fd, argv[2]);
|
|
|
|
write_string(fd, argv[3] ? argv[3] : "");
|
|
|
|
}
|
2018-11-16 00:37:41 -05:00
|
|
|
|
2020-12-30 22:11:24 -08:00
|
|
|
// Get response
|
|
|
|
int code = read_int(fd);
|
|
|
|
switch (code) {
|
|
|
|
case DAEMON_SUCCESS:
|
|
|
|
break;
|
|
|
|
case HIDE_NOT_ENABLED:
|
|
|
|
fprintf(stderr, "MagiskHide is not enabled\n");
|
2021-01-12 00:07:48 -08:00
|
|
|
goto return_code;
|
2020-12-30 22:11:24 -08:00
|
|
|
case HIDE_IS_ENABLED:
|
|
|
|
fprintf(stderr, "MagiskHide is enabled\n");
|
2021-01-12 00:07:48 -08:00
|
|
|
goto return_code;
|
2020-12-30 22:11:24 -08:00
|
|
|
case HIDE_ITEM_EXIST:
|
|
|
|
fprintf(stderr, "Target already exists in hide list\n");
|
2021-01-12 00:07:48 -08:00
|
|
|
goto return_code;
|
2020-12-30 22:11:24 -08:00
|
|
|
case HIDE_ITEM_NOT_EXIST:
|
|
|
|
fprintf(stderr, "Target does not exist in hide list\n");
|
2021-01-12 00:07:48 -08:00
|
|
|
goto return_code;
|
2020-12-30 22:11:24 -08:00
|
|
|
case HIDE_NO_NS:
|
|
|
|
fprintf(stderr, "Your kernel doesn't support mount namespace\n");
|
2021-01-12 00:07:48 -08:00
|
|
|
goto return_code;
|
2020-12-30 22:11:24 -08:00
|
|
|
case HIDE_INVALID_PKG:
|
|
|
|
fprintf(stderr, "Invalid package / process name\n");
|
2021-01-12 00:07:48 -08:00
|
|
|
goto return_code;
|
2020-12-30 22:11:24 -08:00
|
|
|
case ROOT_REQUIRED:
|
|
|
|
fprintf(stderr, "Root is required for this operation\n");
|
2021-01-12 00:07:48 -08:00
|
|
|
goto return_code;
|
2020-12-30 22:11:24 -08:00
|
|
|
case DAEMON_ERROR:
|
|
|
|
default:
|
|
|
|
fprintf(stderr, "Daemon error\n");
|
|
|
|
return DAEMON_ERROR;
|
|
|
|
}
|
2017-09-06 02:25:40 +08:00
|
|
|
|
2021-01-12 00:07:48 -08:00
|
|
|
if (req == LS_HIDELIST) {
|
|
|
|
string res;
|
|
|
|
for (;;) {
|
|
|
|
read_string(fd, res);
|
|
|
|
if (res.empty())
|
|
|
|
break;
|
|
|
|
printf("%s\n", res.data());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return_code:
|
2020-12-30 22:11:24 -08:00
|
|
|
return req == HIDE_STATUS ? (code == HIDE_IS_ENABLED ? 0 : 1) : code != DAEMON_SUCCESS;
|
2017-04-06 06:12:29 +08:00
|
|
|
}
|
2021-01-12 03:28:00 -08:00
|
|
|
|
2021-01-13 20:07:23 -08:00
|
|
|
#if ENABLE_INJECT
|
2021-01-12 03:28:00 -08:00
|
|
|
int remote_check_hide(int uid, const char *process) {
|
|
|
|
int fd = connect_daemon();
|
|
|
|
write_int(fd, MAGISKHIDE);
|
|
|
|
write_int(fd, REMOTE_CHECK_HIDE);
|
|
|
|
write_int(fd, uid);
|
|
|
|
write_string(fd, process);
|
|
|
|
int res = read_int(fd);
|
|
|
|
close(fd);
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
void remote_request_hide() {
|
|
|
|
int fd = connect_daemon();
|
|
|
|
write_int(fd, MAGISKHIDE);
|
|
|
|
write_int(fd, REMOTE_DO_HIDE);
|
|
|
|
|
|
|
|
// Should receive SIGSTOP before reading anything
|
|
|
|
// During process stop, magiskd will cleanup our mount ns
|
|
|
|
read_int(fd);
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
}
|
2021-01-13 20:07:23 -08:00
|
|
|
#endif
|