Better parsing logic

This commit is contained in:
topjohnwu 2018-11-08 04:57:16 -05:00
parent 3d269fe8be
commit b6965105b7

View File

@ -7,9 +7,9 @@
#include <limits.h> #include <limits.h>
#include "sepolicy.h" #include "sepolicy.h"
#include "array.h"
#include "magiskpolicy.h" #include "magiskpolicy.h"
#include "magisk.h" #include "magisk.h"
#include "utils.h"
#include "flags.h" #include "flags.h"
static const char *type_msg_1 = static const char *type_msg_1 =
@ -92,20 +92,46 @@ static const char *type_msg_5 =
exit(1); exit(1);
} }
static char *parse_bracket(char *str, Array<const char *> *vec) { static int parse_bracket(char *tok, char *&stmt, Array<const char *> *vec) {
str = strchr(str, '{') + 1; if (tok == nullptr || tok[0] != '{') {
char *end = strchr(str, '}'); // Not in a bracket
if (end == nullptr) vec->push_back(tok);
return nullptr; } else {
if (stmt)
stmt[-1] = ' ';
tok = strchr(tok, '{') + 1;
char *end = strchr(tok, '}');
if (end == nullptr) // Bracket not closed
return 1;
*end = '\0'; *end = '\0';
char *cur; char *cur;
while ((cur = strtok_r(nullptr, " ", &str)) != nullptr) while ((cur = strtok_r(nullptr, " ", &tok)) != nullptr)
vec->push_back(cur); vec->push_back(cur);
return end + 1; stmt = end + 1;
}
return 0;
} }
// Pattern 1: action { source } { target } class { permission } // Pattern 1: action { source } { target } class { permission }
static int parse_pattern_1(int action, char *stmt) { static int parse_pattern_1(int action, const char *action_str, char *stmt) {
int (*action_func)(const char*, const char*, const char*, const char*);
switch (action) {
case 0:
action_func = sepol_allow;
break;
case 1:
action_func = sepol_deny;
break;
case 2:
action_func = sepol_auditallow;
break;
case 3:
action_func = sepol_auditdeny;
break;
default:
return 1;
}
int state = 0; int state = 0;
char *cur, *cls; char *cur, *cls;
Array<const char*> source, target, permission; Array<const char*> source, target, permission;
@ -130,57 +156,42 @@ static int parse_pattern_1(int action, char *stmt) {
return 1; return 1;
} }
if (vec) { if (vec && parse_bracket(cur, stmt, vec))
if (cur == nullptr || cur[0] != '{') {
vec->push_back(cur);
} else {
stmt[-1] = ' ';
stmt = parse_bracket(cur, vec);
if (stmt == nullptr)
return 1; return 1;
}
}
++state; ++state;
} }
if (state != 4) if (state != 4 || source.empty() || target.empty() || permission.empty())
return 1; return 1;
for(int i = 0; i < source.size(); ++i)
for (int j = 0; j < target.size(); ++j) for (auto src : source)
for (int k = 0; k < permission.size(); ++k) { for (auto tgt : target)
int (*action_func)(const char*, const char*, const char*, const char*); for (auto perm : permission)
const char *action_str; if (action_func(src, tgt, cls, perm))
switch (action) { fprintf(stderr, "Error in: %s %s %s %s %s\n", action_str, src, tgt, cls, perm);
case 0:
action_func = sepol_allow;
action_str = "allow";
break;
case 1:
action_func = sepol_deny;
action_str = "deny";
break;
case 2:
action_func = sepol_auditallow;
action_str = "auditallow";
break;
case 3:
action_func = sepol_auditdeny;
action_str = "auditdeny";
break;
default:
return 1;
}
if (action_func(source[i], target[j], cls, permission[k]))
fprintf(stderr, "Error in: %s %s %s %s %s\n",
action_str, source[i], target[j], cls, permission[k]);
}
return 0; return 0;
} }
// Pattern 2: action { source } { target } { class } ioctl range // Pattern 2: action { source } { target } { class } ioctl range
static int parse_pattern_2(int action, char *stmt) { static int parse_pattern_2(int action, const char *action_str, char *stmt) {
int (*action_func)(const char*, const char*, const char*, const char*);
switch (action) {
case 0:
action_func = sepol_allowxperm;
break;
case 1:
action_func = sepol_auditallowxperm;
break;
case 2:
action_func = sepol_dontauditxperm;
break;
default:
return 1;
}
int state = 0; int state = 0;
char *cur, *range; char *cur, *range;
Array<const char *> source, target, cls; Array<const char *> source, target, classes;
while ((cur = strtok_r(nullptr, " ", &stmt)) != nullptr) { while ((cur = strtok_r(nullptr, " ", &stmt)) != nullptr) {
if (cur[0] == '*') cur = ALL; if (cur[0] == '*') cur = ALL;
Array<const char *> *vec; Array<const char *> *vec;
@ -192,7 +203,7 @@ static int parse_pattern_2(int action, char *stmt) {
vec = &target; vec = &target;
break; break;
case 2: case 2:
vec = &cls; vec = &classes;
break; break;
case 3: case 3:
// Currently only support ioctl // Currently only support ioctl
@ -206,91 +217,76 @@ static int parse_pattern_2(int action, char *stmt) {
return 1; return 1;
} }
if (vec) { if (vec && parse_bracket(cur, stmt, vec))
if (cur == nullptr || cur[0] != '{') {
vec->push_back(cur);
} else {
stmt[-1] = ' ';
stmt = parse_bracket(cur, vec);
if (stmt == nullptr)
return 1; return 1;
}
}
++state; ++state;
} }
if (state != 5) return 1; if (state != 5 || source.empty() || target.empty() || classes.empty())
for(int i = 0; i < source.size(); ++i)
for (int j = 0; j < target.size(); ++j)
for (int k = 0; k < cls.size(); ++k) {
int (*action_func)(const char*, const char*, const char*, const char*);
const char *action_str;
switch (action) {
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; return 1;
}
if (action_func(source[i], target[j], cls[k], range)) for (auto src : source)
fprintf(stderr, "Error in: %s %s %s %s %s\n", for (auto tgt : target)
action_str, source[i], target[j], cls[k], range); for (auto cls : classes)
} if (action_func(src, tgt, cls, range))
fprintf(stderr, "Error in: %s %s %s %s %s\n", action_str, src, tgt, cls, range);
return 0; return 0;
} }
// Pattern 3: action { type } // Pattern 3: action { type }
static int parse_pattern_3(int action, char* stmt) { static int parse_pattern_3(int action, const char *action_str, char* stmt) {
int (*action_func)(const char*);
switch (action) {
case 0:
action_func = sepol_create;
break;
case 1:
action_func = sepol_permissive;
break;
case 2:
action_func = sepol_enforce;
break;
default:
return 1;
}
char *cur; char *cur;
Array<const char *> domains; Array<const char *> domains;
while ((cur = strtok_r(nullptr, " {}", &stmt)) != nullptr) { while ((cur = strtok_r(nullptr, " {}", &stmt)) != nullptr) {
if (cur[0] == '*') cur = ALL; if (cur[0] == '*') cur = ALL;
domains.push_back(cur); domains.push_back(cur);
} }
for (int i = 0; i < domains.size(); ++i) {
int (*action_func)(const char*); if (domains.empty())
const char *action_str;
switch (action) {
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;
default:
return 1; return 1;
}
if (action_func(domains[i])) for (auto dom : domains)
fprintf(stderr, "Error in: %s %s\n", action_str, domains[i]); if (action_func(dom))
} fprintf(stderr, "Error in: %s %s\n", action_str, dom);
return 0; return 0;
} }
// Pattern 4: action { class } { attribute } // Pattern 4: action { class } { attribute }
static int parse_pattern_4(int action, char *stmt) { static int parse_pattern_4(int action, const char *action_str, char *stmt) {
int (*action_func)(const char*, const char*);
switch (action) {
case 0:
action_func = sepol_attradd;
break;
default:
return 1;
}
int state = 0; int state = 0;
char *cur; char *cur;
Array<const char *> cls, attribute; Array<const char *> classes, attribute;
while ((cur = strtok_r(nullptr, " ", &stmt)) != nullptr) { while ((cur = strtok_r(nullptr, " ", &stmt)) != nullptr) {
if (cur[0] == '*') cur = ALL; if (cur[0] == '*') cur = ALL;
Array<const char *> *vec; Array<const char *> *vec;
switch (state) { switch (state) {
case 0: case 0:
vec = &cls; vec = &classes;
break; break;
case 1: case 1:
vec = &attribute; vec = &attribute;
@ -299,37 +295,23 @@ static int parse_pattern_4(int action, char *stmt) {
return 1; return 1;
} }
if (cur == nullptr || cur[0] != '{') { if (parse_bracket(cur, stmt, vec))
vec->push_back(cur);
} else {
stmt[-1] = ' ';
stmt = parse_bracket(cur, vec);
if (stmt == nullptr)
return 1; return 1;
}
++state; ++state;
} }
if (state != 2) return 1; if (state != 2 || classes.empty() || attribute.empty())
for(int i = 0; i < cls.size(); ++i)
for (int j = 0; j < attribute.size(); ++j) {
int (*action_func)(const char*, const char*);
const char *action_str;
switch (action) {
case 0:
action_func = sepol_attradd;
action_str = "attradd";
break;
default:
return 1; return 1;
}
if (action_func(cls[i], attribute[j])) for (auto cls : classes)
fprintf(stderr, "Error in: %s %s %s\n", action_str, cls[i], attribute[j]); for (auto attr : attribute)
} if (action_func(cls, attr))
fprintf(stderr, "Error in: %s %s %s\n", action_str, cls, attr);
return 0; return 0;
} }
// Pattern 5: action source target class default (filename) // Pattern 5: action source target class default (filename)
static int parse_pattern_5(int action, char *stmt) { static int parse_pattern_5(int action, const char *action_str, char *stmt) {
int state = 0; int state = 0;
char *cur; char *cur;
char *source, *target, *cls, *def, *filename = nullptr; char *source, *target, *cls, *def, *filename = nullptr;
@ -357,21 +339,22 @@ static int parse_pattern_5(int action, char *stmt) {
} }
if (state < 4) return 1; if (state < 4) return 1;
if (sepol_typetrans(source, target, cls, def, filename)) if (sepol_typetrans(source, target, cls, def, filename))
fprintf(stderr, "Error in: typetrans %s %s %s %s %s\n", source, target, cls, def, filename ? filename : ""); fprintf(stderr, "Error in: %s %s %s %s %s %s\n",
action_str, source, target, cls, def, filename ? filename : "");
return 0; return 0;
} }
#define add_action(name, type, num) \ #define add_action(name, type, num) \
else if (strcmp(name, action) == 0) { \ else if (strcmp(name, action) == 0) { \
if (parse_pattern_##type(num, remain)) \ if (parse_pattern_##type(num, name, remain)) \
fprintf(stderr, "Syntax error in '%s'\n\n%s\n", orig, type_msg_##type); \ fprintf(stderr, "Syntax error in '%s'\n\n%s\n", orig.c_str(), type_msg_##type); \
} }
static void parse_statement(char *statement) { static void parse_statement(char *statement) {
char *action, *remain; char *action, *remain;
// strtok will modify the origin string, duplicate the statement for error messages // strtok will modify the origin string, duplicate the statement for error messages
char *orig = strdup(statement); CharArray orig(statement);
action = strtok_r(statement, " ", &remain); action = strtok_r(statement, " ", &remain);
if (remain == nullptr) remain = &action[strlen(action)]; if (remain == nullptr) remain = &action[strlen(action)];
@ -389,9 +372,7 @@ static void parse_statement(char *statement) {
add_action("enforce", 3, 2) add_action("enforce", 3, 2)
add_action("attradd", 4, 0) add_action("attradd", 4, 0)
add_action("typetrans", 5, 0) add_action("typetrans", 5, 0)
else { fprintf(stderr, "Unknown statement: '%s'\n\n", orig); } else { fprintf(stderr, "Unknown statement: '%s'\n\n", orig.c_str()); }
free(orig);
} }
int magiskpolicy_main(int argc, char *argv[]) { int magiskpolicy_main(int argc, char *argv[]) {