Magisk/native/jni/magiskpolicy/magiskpolicy.cpp

126 lines
3.0 KiB
C++
Raw Normal View History

2018-11-08 09:20:16 +00:00
#include <stdio.h>
#include <limits.h>
2020-03-09 08:50:30 +00:00
#include <logging.hpp>
#include <utils.hpp>
2019-02-10 08:57:51 +00:00
#include <flags.h>
2020-03-09 08:50:30 +00:00
#include <magiskpolicy.hpp>
2019-02-10 08:57:51 +00:00
2020-05-23 07:18:25 +00:00
#include "sepolicy.hpp"
2019-12-09 09:14:30 +00:00
using namespace std::literals;
2018-11-08 09:20:16 +00:00
[[noreturn]] static void usage(char *arg0) {
fprintf(stderr,
2020-03-23 08:17:13 +00:00
NAME_WITH_VER(MagiskPolicy) R"EOF(
2019-12-09 09:14:30 +00:00
Usage: %s [--options...] [policy statements...]
Options:
--help show help message for policy statements
--load FILE load policies from FILE
--load-split load from precompiled sepolicy or compile
split policies
--compile-split compile split cil policies
--save FILE save policies to FILE
--live directly apply sepolicy live
--magisk inject built-in rules for a minimal
Magisk selinux environment
--apply FILE apply rules from FILE, read and parsed
line by line as policy statements
If neither --load or --compile-split is specified, it will load
from current live policies (/sys/fs/selinux/policy)
)EOF", arg0);
2018-11-08 09:20:16 +00:00
exit(1);
}
int magiskpolicy_main(int argc, char *argv[]) {
2018-11-08 11:43:11 +00:00
cmdline_logging();
2019-12-09 09:14:30 +00:00
const char *out_file = nullptr;
const char *rule_file = nullptr;
2020-05-21 13:48:02 +00:00
sepolicy *sepol = nullptr;
2019-12-09 09:14:30 +00:00
bool magisk = false;
bool live = false;
2018-11-08 09:20:16 +00:00
if (argc < 2) usage(argv[0]);
int i = 1;
for (; i < argc; ++i) {
// Parse options
if (argv[i][0] == '-' && argv[i][1] == '-') {
2019-12-09 09:14:30 +00:00
auto option = argv[i] + 2;
if (option == "live"sv)
2018-11-08 09:20:16 +00:00
live = true;
2019-12-09 09:14:30 +00:00
else if (option == "magisk"sv)
2018-11-08 09:20:16 +00:00
magisk = true;
2019-12-09 09:14:30 +00:00
else if (option == "load"sv) {
2018-11-08 09:20:16 +00:00
if (argv[i + 1] == nullptr)
usage(argv[0]);
2020-05-21 13:48:02 +00:00
sepol = sepolicy::from_file(argv[i + 1]);
if (!sepol) {
2018-11-08 09:20:16 +00:00
fprintf(stderr, "Cannot load policy from %s\n", argv[i + 1]);
return 1;
}
++i;
2019-12-09 09:14:30 +00:00
} else if (option == "load-split"sv) {
2020-05-21 13:48:02 +00:00
sepol = sepolicy::from_split();
if (!sepol) {
fprintf(stderr, "Cannot load split cil\n");
return 1;
}
2019-12-09 09:14:30 +00:00
} else if (option == "compile-split"sv) {
2020-05-21 13:48:02 +00:00
sepol = sepolicy::compile_split();
if (!sepol) {
2018-11-08 09:20:16 +00:00
fprintf(stderr, "Cannot compile split cil\n");
return 1;
}
2019-12-09 09:14:30 +00:00
} else if (option == "save"sv) {
if (argv[i + 1] == nullptr)
usage(argv[0]);
out_file = argv[i + 1];
++i;
} else if (option == "apply"sv) {
2018-11-08 09:20:16 +00:00
if (argv[i + 1] == nullptr)
usage(argv[0]);
2019-12-09 09:14:30 +00:00
rule_file = argv[i + 1];
2018-11-08 09:20:16 +00:00
++i;
2019-12-09 09:14:30 +00:00
} else if (option == "help"sv) {
statement_help();
2018-11-08 09:20:16 +00:00
} else {
usage(argv[0]);
}
} else {
break;
}
}
// Use current policy if nothing is loaded
2020-05-21 13:48:02 +00:00
if (sepol == nullptr && !(sepol = sepolicy::from_file(SELINUX_POLICY))) {
2018-11-08 09:20:16 +00:00
fprintf(stderr, "Cannot load policy from " SELINUX_POLICY "\n");
return 1;
}
if (magisk)
2020-05-21 13:48:02 +00:00
sepol->magisk_rules();
2018-11-08 09:20:16 +00:00
2019-12-09 09:14:30 +00:00
if (rule_file)
2020-05-21 13:48:02 +00:00
sepol->load_rule_file(rule_file);
2019-12-09 09:14:30 +00:00
for (; i < argc; ++i)
2020-05-21 13:48:02 +00:00
sepol->parse_statement(argv[i]);
2020-05-21 13:48:02 +00:00
if (live && sepol->to_file(SELINUX_LOAD)) {
2018-11-08 09:20:16 +00:00
fprintf(stderr, "Cannot apply policy\n");
return 1;
}
2020-05-21 13:48:02 +00:00
if (out_file && sepol->to_file(out_file)) {
2019-12-09 09:14:30 +00:00
fprintf(stderr, "Cannot dump policy to %s\n", out_file);
2018-11-08 09:20:16 +00:00
return 1;
}
2020-05-21 13:48:02 +00:00
delete sepol;
2018-11-08 09:20:16 +00:00
return 0;
}