Magisk/native/jni/zygisk/deny/cli.cpp

150 lines
3.9 KiB
C++
Raw Normal View History

2021-09-12 19:40:34 +00:00
#include <sys/wait.h>
#include <sys/mount.h>
#include <utils.hpp>
#include "deny.hpp"
using namespace std;
[[noreturn]] static void usage() {
fprintf(stderr,
2022-01-25 10:04:15 +00:00
R"EOF(DenyList Config CLI
2021-09-12 19:40:34 +00:00
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
2022-01-25 10:04:15 +00:00
2021-09-12 19:40:34 +00:00
)EOF");
exit(1);
}
2021-10-20 06:46:38 +00:00
void denylist_handler(int client, const sock_cred *cred) {
2021-09-12 19:40:34 +00:00
if (client < 0) {
revert_unmount();
return;
}
2022-03-01 10:13:18 +00:00
int req = read_int(client);
int res = DenyResponse::ERROR;
2022-02-12 15:43:36 +00:00
2022-03-01 10:13:18 +00:00
if (req < 0 || req >= DenyRequest::END) {
2022-02-12 15:43:36 +00:00
goto done;
}
2021-09-12 19:40:34 +00:00
switch (req) {
2022-02-12 15:43:36 +00:00
case DenyRequest::ENFORCE:
2021-09-16 12:27:34 +00:00
res = enable_deny();
2021-09-12 19:40:34 +00:00
break;
2022-02-12 15:43:36 +00:00
case DenyRequest::DISABLE:
2021-09-12 19:40:34 +00:00
res = disable_deny();
break;
2022-02-12 15:43:36 +00:00
case DenyRequest::ADD:
2021-09-12 19:40:34 +00:00
res = add_list(client);
break;
2022-02-12 15:43:36 +00:00
case DenyRequest::REMOVE:
2021-09-12 19:40:34 +00:00
res = rm_list(client);
break;
2022-02-12 15:43:36 +00:00
case DenyRequest::LIST:
2021-09-12 19:40:34 +00:00
ls_list(client);
return;
2022-02-12 15:43:36 +00:00
case DenyRequest::STATUS:
2022-03-01 10:13:18 +00:00
res = (zygisk_enabled && denylist_enforced)
? DenyResponse::ENFORCED : DenyResponse::NOT_ENFORCED;
2021-09-12 19:40:34 +00:00
break;
2022-03-01 10:13:18 +00:00
default:
2022-02-12 15:43:36 +00:00
__builtin_unreachable();
2021-09-12 19:40:34 +00:00
}
2022-02-12 15:43:36 +00:00
done:
2022-03-01 10:13:18 +00:00
write_int(client, res);
2021-09-12 19:40:34 +00:00
close(client);
}
int denylist_cli(int argc, char **argv) {
if (argc < 2)
usage();
2022-03-01 10:13:18 +00:00
int req;
2021-09-12 19:40:34 +00:00
if (argv[1] == "enable"sv)
2022-02-12 15:43:36 +00:00
req = DenyRequest::ENFORCE;
2021-09-12 19:40:34 +00:00
else if (argv[1] == "disable"sv)
2022-02-12 15:43:36 +00:00
req = DenyRequest::DISABLE;
2021-09-12 19:40:34 +00:00
else if (argv[1] == "add"sv)
2022-02-12 15:43:36 +00:00
req = DenyRequest::ADD;
2021-09-12 19:40:34 +00:00
else if (argv[1] == "rm"sv)
2022-02-12 15:43:36 +00:00
req = DenyRequest::REMOVE;
2021-09-12 19:40:34 +00:00
else if (argv[1] == "ls"sv)
2022-02-12 15:43:36 +00:00
req = DenyRequest::LIST;
2021-09-12 19:40:34 +00:00
else if (argv[1] == "status"sv)
2022-02-12 15:43:36 +00:00
req = DenyRequest::STATUS;
2021-09-12 19:40:34 +00:00
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
2022-03-01 10:13:18 +00:00
int fd = connect_daemon(MainRequest::DENYLIST);
write_int(fd, req);
2022-02-12 15:43:36 +00:00
if (req == DenyRequest::ADD || req == DenyRequest::REMOVE) {
2021-09-12 19:40:34 +00:00
write_string(fd, argv[2]);
write_string(fd, argv[3] ? argv[3] : "");
}
// Get response
2022-03-01 10:13:18 +00:00
int res = read_int(fd);
if (res < 0 || res >= DenyResponse::END)
res = DenyResponse::ERROR;
2022-02-12 15:43:36 +00:00
switch (res) {
case DenyResponse::NOT_ENFORCED:
2021-09-12 19:40:34 +00:00
fprintf(stderr, "Denylist is not enforced\n");
goto return_code;
2022-02-12 15:43:36 +00:00
case DenyResponse::ENFORCED:
2021-09-12 19:40:34 +00:00
fprintf(stderr, "Denylist is enforced\n");
goto return_code;
2022-02-12 15:43:36 +00:00
case DenyResponse::ITEM_EXIST:
2021-09-12 19:40:34 +00:00
fprintf(stderr, "Target already exists in denylist\n");
goto return_code;
2022-02-12 15:43:36 +00:00
case DenyResponse::ITEM_NOT_EXIST:
2021-09-12 19:40:34 +00:00
fprintf(stderr, "Target does not exist in denylist\n");
goto return_code;
2022-02-12 15:43:36 +00:00
case DenyResponse::NO_NS:
2021-09-12 19:40:34 +00:00
fprintf(stderr, "The kernel does not support mount namespace\n");
goto return_code;
2022-02-12 15:43:36 +00:00
case DenyResponse::INVALID_PKG:
2021-09-12 19:40:34 +00:00
fprintf(stderr, "Invalid package / process name\n");
goto return_code;
2022-02-12 15:43:36 +00:00
case DenyResponse::ERROR:
fprintf(stderr, "deny: Daemon error\n");
return -1;
case DenyResponse::OK:
break;
2022-03-01 10:13:18 +00:00
default:
2022-02-12 15:43:36 +00:00
__builtin_unreachable();
2021-09-12 19:40:34 +00:00
}
2022-02-12 15:43:36 +00:00
if (req == DenyRequest::LIST) {
string out;
2021-09-12 19:40:34 +00:00
for (;;) {
2022-02-12 15:43:36 +00:00
read_string(fd, out);
if (out.empty())
2021-09-12 19:40:34 +00:00
break;
2022-02-12 15:43:36 +00:00
printf("%s\n", out.data());
2021-09-12 19:40:34 +00:00
}
}
return_code:
2022-02-12 15:43:36 +00:00
return req == DenyRequest::STATUS ? res != DenyResponse::ENFORCED : res != DenyResponse::OK;
2021-09-12 19:40:34 +00:00
}