150 lines
4.0 KiB
C++
Raw Normal View History

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