Magisk/native/jni/magiskpolicy/magiskpolicy.c

501 lines
13 KiB
C
Raw Normal View History

2017-04-15 19:26:29 +08:00
/* magiskpolicy.c - Main function for policy patching
*
* Includes all the parsing logic for the policy statements
*/
2017-02-01 00:51:45 +08:00
#include <stdio.h>
2018-09-27 00:09:59 -04:00
#include <limits.h>
2017-04-15 19:26:29 +08:00
#include "sepolicy.h"
#include "vector.h"
#include "magiskpolicy.h"
2017-11-23 23:45:50 +08:00
#include "magisk.h"
2017-04-15 19:26:29 +08:00
2017-02-04 17:30:34 +08:00
static int syntax_err = 0;
static char err_msg[ARG_MAX];
static void statements() {
2017-08-15 00:42:32 +08:00
fprintf(stderr,
2017-08-17 00:57:38 +08:00
"One policy statement should be treated as one parameter;\n"
"this means a full policy statement should be enclosed in quotes;\n"
"multiple policy statements can be provided in a single command\n"
2017-08-15 00:42:32 +08:00
"\n"
"The statements has a format of \"<action> [args...]\"\n"
"Use '*' in args to represent every possible match.\n"
"Collections wrapped in curly brackets can also be used as args.\n"
"\n"
"Supported policy statements:\n"
"\n"
"Type 1:\n"
"\"<action> source-class target-class permission-class permission\"\n"
"Action: allow, deny, auditallow, auditdeny\n"
"\n"
"Type 2:\n"
"\"<action> source-class target-class permission-class ioctl range\"\n"
"Action: allowxperm, auditallowxperm, dontauditxperm\n"
2017-03-18 16:52:38 +08:00
"\n"
2017-08-15 00:42:32 +08:00
"Type 3:\n"
"\"<action> class\"\n"
"Action: create, permissive, enforcing\n"
"\n"
"Type 4:\n"
"\"attradd class attribute\"\n"
"\n"
"Type 5:\n"
2017-04-20 04:04:09 +08:00
"\"typetrans source-class target-class permission-class default-class (optional: object-name)\"\n"
2017-08-15 00:42:32 +08:00
"\n"
"Notes:\n"
"- typetrans does not support the all match '*' syntax\n"
"- permission-class cannot be collections\n"
"- source-class and target-class can also be attributes\n"
"\n"
"Example: allow { source1 source2 } { target1 target2 } permission-class *\n"
2017-03-18 16:52:38 +08:00
"Will be expanded to:\n"
2017-08-15 00:42:32 +08:00
"\n"
"allow source1 target1 permission-class { all-permissions }\n"
"allow source1 target2 permission-class { all-permissions }\n"
"allow source2 target1 permission-class { all-permissions }\n"
"allow source2 target2 permission-class { all-permissions }\n"
2017-03-18 16:52:38 +08:00
"\n"
);
2017-02-04 17:30:34 +08:00
}
static void usage(char *arg0) {
2017-03-30 02:24:16 +08:00
fprintf(stderr,
"MagiskPolicy v" xstr(MAGISK_VERSION) "(" xstr(MAGISK_VER_CODE) ") (by topjohnwu)\n\n"
"Usage: %s [--options...] [policy statements...]\n"
"\n"
2017-03-30 02:24:16 +08:00
"Options:\n"
" --live directly apply sepolicy live\n"
" --magisk inject built-in rules for a minimal\n"
" Magisk selinux environment\n"
" --load FILE load policies from FILE\n"
" --compile-split compile and load split cil policies\n"
" from system and vendor just like init\n"
" --save FILE save policies to FILE\n"
"\n"
"If neither --load or --compile-split is specified, it will load\n"
"from current live policies (" SELINUX_POLICY ")\n"
"\n"
2017-03-30 02:24:16 +08:00
, arg0);
2017-02-04 17:30:34 +08:00
statements();
2017-02-01 00:51:45 +08:00
exit(1);
}
2017-02-04 01:58:15 +08:00
// Pattern 1: action { source } { target } class { permission }
static int parse_pattern_1(int action, char* statement) {
int state = 0, in_bracket = 0;
char *tok, *class, *saveptr;
2017-08-28 02:13:36 +08:00
struct vector source, target, permission;
2017-02-04 01:58:15 +08:00
vec_init(&source);
vec_init(&target);
vec_init(&permission);
tok = strtok_r(statement, " ", &saveptr);
2017-02-04 01:58:15 +08:00
while (tok != NULL) {
if (tok[0] == '{') {
if (in_bracket || state == 2) return 1;
in_bracket = 1;
if (tok[1]) {
++tok;
continue;
2017-02-01 00:51:45 +08:00
}
2017-02-04 01:58:15 +08:00
} else if (tok[strlen(tok) - 1] == '}') {
if (!in_bracket || state == 2) return 1;
in_bracket = 0;
if (strlen(tok) - 1) {
tok[strlen(tok) - 1] = '\0';
continue;
}
} else {
if (tok[0] == '*') tok = ALL;
2017-08-28 02:13:36 +08:00
struct vector *vec;
2017-02-04 01:58:15 +08:00
switch (state) {
2017-08-28 02:13:36 +08:00
case 0:
vec = &source;
break;
case 1:
vec = &target;
break;
case 2:
vec = NULL;
class = tok;
break;
case 3:
vec = &permission;
break;
default:
return 1;
}
vec_push_back(vec, tok);
}
if (!in_bracket) ++state;
tok = strtok_r(NULL, " ", &saveptr);
}
if (state != 4) return 1;
for(int i = 0; i < source.size; ++i)
for (int j = 0; j < target.size; ++j)
for (int k = 0; k < permission.size; ++k) {
int (*action_func)(char*, char*, char*, char*);
char *action_str;
switch (action) {
2017-02-04 01:58:15 +08:00
case 0:
2017-08-28 02:13:36 +08:00
action_func = sepol_allow;
action_str = "allow";
2017-02-04 01:58:15 +08:00
break;
case 1:
2017-08-28 02:13:36 +08:00
action_func = sepol_deny;
action_str = "deny";
2017-02-04 01:58:15 +08:00
break;
case 2:
2017-08-28 02:13:36 +08:00
action_func = sepol_auditallow;
action_str = "auditallow";
2017-02-04 01:58:15 +08:00
break;
case 3:
2017-08-28 02:13:36 +08:00
action_func = sepol_auditdeny;
action_str = "auditdeny";
2017-02-04 01:58:15 +08:00
break;
default:
return 1;
}
2017-08-28 02:13:36 +08:00
if (action_func(source.data[i], target.data[j], class, permission.data[k]))
fprintf(stderr, "Error in: %s %s %s %s %s\n",
2017-11-23 23:45:50 +08:00
action_str, (char *) source.data[i], (char *) target.data[j], class, (char *) permission.data[k]);
2017-08-28 02:13:36 +08:00
}
2017-02-04 01:58:15 +08:00
vec_destroy(&source);
vec_destroy(&target);
vec_destroy(&permission);
return 0;
}
2017-02-01 00:51:45 +08:00
// Pattern 2: action { class } { attribute }
2017-02-04 17:30:34 +08:00
static int parse_pattern_2(int action, char* statement) {
int state = 0, in_bracket = 0;
char *tok, *saveptr;
2017-08-28 02:13:36 +08:00
struct vector class, attribute;
vec_init(&class);
vec_init(&attribute);
tok = strtok_r(statement, " ", &saveptr);
while (tok != NULL) {
if (tok[0] == '{') {
if (in_bracket) return 1;
in_bracket = 1;
if (tok[1]) {
++tok;
continue;
}
} else if (tok[strlen(tok) - 1] == '}') {
if (!in_bracket) return 1;
in_bracket = 0;
if (strlen(tok) - 1) {
tok[strlen(tok) - 1] = '\0';
continue;
}
} else {
if (tok[0] == '*') tok = ALL;
2017-08-28 02:13:36 +08:00
struct vector *vec;
switch (state) {
2017-08-28 02:13:36 +08:00
case 0:
vec = &class;
break;
case 1:
vec = &attribute;
break;
default:
return 1;
}
2017-08-28 02:13:36 +08:00
vec_push_back(vec, tok);
}
if (!in_bracket) ++state;
tok = strtok_r(NULL, " ", &saveptr);
}
if (state != 2) return 1;
for(int i = 0; i < class.size; ++i)
2017-08-28 02:13:36 +08:00
for (int j = 0; j < attribute.size; ++j) {
int (*action_func)(char*, char*);
char *action_str;
switch (action) {
case 0:
2017-08-28 02:13:36 +08:00
action_func = sepol_attradd;
action_str = "attradd";
break;
default:
return 1;
}
2017-08-28 02:13:36 +08:00
if (action_func(class.data[i], attribute.data[j]))
fprintf(stderr, "Error in: %s %s %s\n",
2017-11-23 23:45:50 +08:00
action_str, (char *) class.data[i], (char *) attribute.data[j]);
2017-08-28 02:13:36 +08:00
}
vec_destroy(&class);
vec_destroy(&attribute);
return 0;
}
// Pattern 3: action { type }
2017-02-04 17:30:34 +08:00
static int parse_pattern_3(int action, char* statement) {
char *tok, *saveptr;
2017-04-05 06:00:42 +08:00
struct vector classes;
vec_init(&classes);
tok = strtok_r(statement, " {}", &saveptr);
while (tok != NULL) {
if (tok[0] == '*') tok = ALL;
vec_push_back(&classes, tok);
tok = strtok_r(NULL, " {}", &saveptr);
}
for (int i = 0; i < classes.size; ++i) {
2017-08-28 02:13:36 +08:00
int (*action_func)(char*);
char *action_str;
switch (action) {
2017-08-28 02:13:36 +08:00
case 0:
action_func = sepol_create;
action_str = "create";
break;
case 1:
action_func = sepol_permissive;
action_str = "permissive";
break;
case 2:
action_func = sepol_enforce;
action_str = "enforce";
break;
}
2017-08-28 02:13:36 +08:00
if (action_func(classes.data[i]))
2017-11-23 23:45:50 +08:00
fprintf(stderr, "Error in: %s %s\n", action_str, (char *) classes.data[i]);
}
vec_destroy(&classes);
return 0;
}
// Pattern 4: action source target class default (filename)
2017-02-04 17:30:34 +08:00
static int parse_pattern_4(int action, char* statement) {
int state = 0;
char *tok, *saveptr;
char *source, *target, *class, *def, *filename = NULL;
tok = strtok_r(statement, " ", &saveptr);
while (tok != NULL) {
switch(state) {
2017-08-28 02:13:36 +08:00
case 0:
source = tok;
break;
case 1:
target = tok;
break;
case 2:
class = tok;
break;
case 3:
def = tok;
break;
case 4:
filename = tok;
break;
default:
return 1;
}
tok = strtok_r(NULL, " ", &saveptr);
++state;
}
if (state < 4) return 1;
2017-04-15 19:26:29 +08:00
if (sepol_typetrans(source, target, class, def, filename))
fprintf(stderr, "Error in: typetrans %s %s %s %s %s\n", source, target, class, def, filename ? filename : "");
return 0;
}
2017-04-20 04:04:09 +08:00
// Pattern 5: action { source } { target } { class } ioctl range
static int parse_pattern_5(int action, char* statement) {
int state = 0, in_bracket = 0;
char *tok, *range, *saveptr;
2017-08-28 02:13:36 +08:00
struct vector source, target, class;
2017-04-20 04:04:09 +08:00
vec_init(&source);
vec_init(&target);
vec_init(&class);
tok = strtok_r(statement, " ", &saveptr);
while (tok != NULL) {
if (tok[0] == '{') {
if (in_bracket || state == 3 || state == 4) return 1;
in_bracket = 1;
if (tok[1]) {
++tok;
continue;
}
} else if (tok[strlen(tok) - 1] == '}') {
if (!in_bracket || state == 3 || state == 4) return 1;
in_bracket = 0;
if (strlen(tok) - 1) {
tok[strlen(tok) - 1] = '\0';
continue;
}
} else {
if (tok[0] == '*') tok = ALL;
2017-08-28 02:13:36 +08:00
struct vector *vec;
2017-04-20 04:04:09 +08:00
switch (state) {
2017-08-28 02:13:36 +08:00
case 0:
vec = &source;
break;
case 1:
vec = &target;
break;
case 2:
vec = &class;
break;
case 3:
// Should always be ioctl
vec = NULL;
break;
case 4:
vec = NULL;
range = tok;
break;
default:
return 1;
2017-04-20 04:04:09 +08:00
}
2017-08-28 02:13:36 +08:00
vec_push_back(vec, tok);
2017-04-20 04:04:09 +08:00
}
if (!in_bracket) ++state;
tok = strtok_r(NULL, " ", &saveptr);
}
if (state != 5) return 1;
for(int i = 0; i < source.size; ++i)
for (int j = 0; j < target.size; ++j)
2017-08-28 02:13:36 +08:00
for (int k = 0; k < class.size; ++k) {
int (*action_func)(char*, char*, char*, char*);
char *action_str;
2017-04-20 04:04:09 +08:00
switch (action) {
2017-08-28 02:13:36 +08:00
case 0:
action_func = sepol_allowxperm;
action_str = "allowxperm";
break;
case 1:
action_func = sepol_auditallowxperm;
action_str = "auditallowxperm";
break;
case 2:
action_func = sepol_dontauditxperm;
action_str = "dontauditxperm";
break;
default:
return 1;
2017-04-20 04:04:09 +08:00
}
2017-08-28 02:13:36 +08:00
if (action_func(source.data[i], target.data[j], class.data[k], range))
fprintf(stderr, "Error in: %s %s %s %s %s\n",
2017-11-23 23:45:50 +08:00
action_str, (char *) source.data[i], (char *) target.data[j], (char *) class.data[k], range);
2017-08-28 02:13:36 +08:00
}
2017-04-20 04:04:09 +08:00
vec_destroy(&source);
vec_destroy(&target);
vec_destroy(&class);
return 0;
}
2017-02-04 17:30:34 +08:00
static void syntax_error_msg() {
fprintf(stderr, "Syntax error in \"%s\"\n", err_msg);
syntax_err = 1;
}
2017-04-05 06:00:42 +08:00
int magiskpolicy_main(int argc, char *argv[]) {
2018-09-27 03:11:10 -04:00
cmdline_logging();
char *outfile = NULL, *tok, *saveptr;
int magisk = 0;
2017-02-04 01:58:15 +08:00
struct vector rules;
vec_init(&rules);
if (argc < 2) usage(argv[0]);
for (int i = 1; i < argc; ++i) {
if (argv[i][0] == '-' && argv[i][1] == '-') {
if (strcmp(argv[i] + 2, "live") == 0)
outfile = SELINUX_LOAD;
else if (strcmp(argv[i] + 2, "magisk") == 0)
magisk = 1;
else if (strcmp(argv[i] + 2, "load") == 0) {
if (i + 1 >= argc)
usage(argv[0]);
if (load_policydb(argv[i + 1])) {
fprintf(stderr, "Cannot load policy from %s\n", argv[i + 1]);
return 1;
}
++i;
} else if (strcmp(argv[i] + 2, "compile-split") == 0) {
if (compile_split_cil()) {
fprintf(stderr, "Cannot compile split cil\n");
return 1;
}
} else if (strcmp(argv[i] + 2, "save") == 0) {
if (i + 1 >= argc)
usage(argv[0]);
2017-02-04 01:58:15 +08:00
outfile = argv[i + 1];
++i;
} else {
2017-02-04 01:58:15 +08:00
usage(argv[0]);
}
} else {
2017-02-04 01:58:15 +08:00
vec_push_back(&rules, argv[i]);
}
2017-02-04 01:58:15 +08:00
}
2017-02-01 00:51:45 +08:00
// Use current policy if nothing is loaded
if(policydb == NULL && load_policydb(SELINUX_POLICY)) {
fprintf(stderr, "Cannot load policy from " SELINUX_POLICY "\n");
2017-02-01 00:51:45 +08:00
return 1;
}
if (magisk)
sepol_magisk_rules();
2017-02-04 01:58:15 +08:00
for (int i = 0; i < rules.size; ++i) {
// Since strtok will modify the origin string, copy the policy for error messages
2017-02-04 17:30:34 +08:00
strcpy(err_msg, rules.data[i]);
tok = strtok_r(rules.data[i], " ", &saveptr);
2017-02-04 01:58:15 +08:00
if (strcmp(tok, "allow") == 0) {
if (parse_pattern_1(0, rules.data[i] + strlen(tok) + 1))
2017-02-04 17:30:34 +08:00
syntax_error_msg();
2017-02-04 01:58:15 +08:00
} else if (strcmp(tok, "deny") == 0) {
if (parse_pattern_1(1, rules.data[i] + strlen(tok) + 1))
2017-02-04 17:30:34 +08:00
syntax_error_msg();
2017-02-04 01:58:15 +08:00
} else if (strcmp(tok, "auditallow") == 0) {
if (parse_pattern_1(2, rules.data[i] + strlen(tok) + 1))
2017-02-04 17:30:34 +08:00
syntax_error_msg();
2017-02-04 01:58:15 +08:00
} else if (strcmp(tok, "auditdeny") == 0) {
if (parse_pattern_1(3, rules.data[i] + strlen(tok) + 1))
2017-02-04 17:30:34 +08:00
syntax_error_msg();
} else if (strcmp(tok, "attradd") == 0) {
if (parse_pattern_2(0, rules.data[i] + strlen(tok) + 1))
2017-02-04 17:30:34 +08:00
syntax_error_msg();
} else if (strcmp(tok, "create") == 0) {
if (parse_pattern_3(0, rules.data[i] + strlen(tok) + 1))
2017-02-04 17:30:34 +08:00
syntax_error_msg();
} else if (strcmp(tok, "permissive") == 0) {
if (parse_pattern_3(1, rules.data[i] + strlen(tok) + 1))
2017-02-04 17:30:34 +08:00
syntax_error_msg();
} else if (strcmp(tok, "enforce") == 0) {
if (parse_pattern_3(2, rules.data[i] + strlen(tok) + 1))
2017-02-04 17:30:34 +08:00
syntax_error_msg();
} else if (strcmp(tok, "typetrans") == 0) {
if (parse_pattern_4(0, rules.data[i] + strlen(tok) + 1))
2017-02-04 17:30:34 +08:00
syntax_error_msg();
2017-04-20 04:04:09 +08:00
} else if (strcmp(tok, "allowxperm") == 0) {
if (parse_pattern_5(0, rules.data[i] + strlen(tok) + 1))
syntax_error_msg();
} else if (strcmp(tok, "auditallowxperm") == 0) {
if (parse_pattern_5(1, rules.data[i] + strlen(tok) + 1))
syntax_error_msg();
} else if (strcmp(tok, "dontauditxperm") == 0) {
if (parse_pattern_5(2, rules.data[i] + strlen(tok) + 1))
syntax_error_msg();
} else {
2017-02-04 17:30:34 +08:00
syntax_error_msg();
2017-02-01 00:51:45 +08:00
}
2017-02-04 01:58:15 +08:00
}
2017-02-01 00:51:45 +08:00
2017-02-04 17:30:34 +08:00
if (syntax_err)
statements();
2017-02-01 00:51:45 +08:00
2017-02-04 17:30:34 +08:00
vec_destroy(&rules);
2017-02-01 00:51:45 +08:00
if (outfile && dump_policydb(outfile)) {
fprintf(stderr, "Cannot dump policy to %s\n", outfile);
return 1;
}
2017-02-01 00:51:45 +08:00
2017-04-16 02:29:42 +08:00
destroy_policydb();
2017-02-01 00:51:45 +08:00
return 0;
}