Adjust logging in magiskpolicy

This commit is contained in:
topjohnwu 2018-11-08 06:43:11 -05:00
parent 07718b994a
commit d3947d2cfa
3 changed files with 47 additions and 74 deletions

View File

@ -376,6 +376,7 @@ static void parse_statement(char *statement) {
} }
int magiskpolicy_main(int argc, char *argv[]) { int magiskpolicy_main(int argc, char *argv[]) {
cmdline_logging();
const char *outfile = nullptr; const char *outfile = nullptr;
bool magisk = false, live = false; bool magisk = false, live = false;

View File

@ -22,6 +22,7 @@
#include "utils.h" #include "utils.h"
#include "magiskpolicy.h" #include "magiskpolicy.h"
#include "sepolicy.h" #include "sepolicy.h"
#include "logging.h"
policydb_t *policydb = NULL; policydb_t *policydb = NULL;
extern int policydb_index_decls(sepol_handle_t * handle, policydb_t * p); extern int policydb_index_decls(sepol_handle_t * handle, policydb_t * p);
@ -29,7 +30,7 @@ extern int policydb_index_decls(sepol_handle_t * handle, policydb_t * p);
static void *cmalloc(size_t s) { static void *cmalloc(size_t s) {
void *t = calloc(s, 1); void *t = calloc(s, 1);
if (t == NULL) { if (t == NULL) {
fprintf(stderr, "Out of memory\n"); LOGE("Out of memory\n");
exit(1); exit(1);
} }
return t; return t;
@ -103,7 +104,7 @@ static int __add_rule(int s, int t, int c, int p, int effect, int not) {
if (new_rule) { if (new_rule) {
if (avtab_insert(&policydb->te_avtab, &key, av)) { if (avtab_insert(&policydb->te_avtab, &key, av)) {
fprintf(stderr, "Error inserting into avtab\n"); LOGW("Error inserting into avtab\n");
return 1; return 1;
} }
free(av); free(av);
@ -182,7 +183,7 @@ static int __add_xperm_rule(int s, int t, int c, uint16_t low, uint16_t high, in
if (new_rule) { if (new_rule) {
if (avtab_insert(&policydb->te_avtab, &key, av)) { if (avtab_insert(&policydb->te_avtab, &key, av)) {
fprintf(stderr, "Error inserting into avtab\n"); LOGW("Error inserting into avtab\n");
return 1; return 1;
} }
free(av); free(av);
@ -218,10 +219,9 @@ static int add_xperm_rule_auto(type_datum_t *src, type_datum_t *tgt, class_datum
} }
int load_policydb(const char *filename) { int load_policydb(const char *filename) {
int fd;
struct stat sb;
struct policy_file pf; struct policy_file pf;
void *map; void *map;
size_t size;
int ret; int ret;
if (policydb) if (policydb)
@ -229,41 +229,23 @@ int load_policydb(const char *filename) {
policydb = cmalloc(sizeof(*policydb)); policydb = cmalloc(sizeof(*policydb));
fd = open(filename, O_RDONLY); mmap_ro(filename, &map, &size);
if (fd < 0) {
fprintf(stderr, "Can't open '%s': %s\n",
filename, strerror(errno));
return 1;
}
if (fstat(fd, &sb) < 0) {
fprintf(stderr, "Can't stat '%s': %s\n",
filename, strerror(errno));
return 1;
}
map = mmap(NULL, sb.st_size, PROT_READ | PROT_WRITE, MAP_PRIVATE,
fd, 0);
if (map == MAP_FAILED) {
fprintf(stderr, "Can't mmap '%s': %s\n",
filename, strerror(errno));
return 1;
}
policy_file_init(&pf); policy_file_init(&pf);
pf.type = PF_USE_MEMORY; pf.type = PF_USE_MEMORY;
pf.data = map; pf.data = map;
pf.len = sb.st_size; pf.len = size;
if (policydb_init(policydb)) { if (policydb_init(policydb)) {
fprintf(stderr, "policydb_init: Out of memory!\n"); LOGE("policydb_init: Out of memory!\n");
return 1; return 1;
} }
ret = policydb_read(policydb, &pf, 0); ret = policydb_read(policydb, &pf, 0);
if (ret) { if (ret) {
fprintf(stderr, "error(s) encountered while parsing configuration\n"); LOGE("error(s) encountered while parsing configuration\n");
return 1; return 1;
} }
munmap(map, sb.st_size); munmap(map, size);
close(fd);
return 0; return 0;
} }
@ -290,7 +272,7 @@ int compile_split_cil() {
mmap_ro(SPLIT_PLAT_CIL, &addr, &size); mmap_ro(SPLIT_PLAT_CIL, &addr, &size);
if (cil_add_file(db, SPLIT_PLAT_CIL, addr, size)) if (cil_add_file(db, SPLIT_PLAT_CIL, addr, size))
return 1; return 1;
fprintf(stderr, "cil_add[%s]\n", SPLIT_PLAT_CIL); LOGD("cil_add[%s]\n", SPLIT_PLAT_CIL);
munmap(addr, size); munmap(addr, size);
// mapping // mapping
@ -301,7 +283,7 @@ int compile_split_cil() {
mmap_ro(path, &addr, &size); mmap_ro(path, &addr, &size);
if (cil_add_file(db, path, addr, size)) if (cil_add_file(db, path, addr, size))
return 1; return 1;
fprintf(stderr, "cil_add[%s]\n", path); LOGD("cil_add[%s]\n", path);
munmap(addr, size); munmap(addr, size);
close(fd); close(fd);
@ -315,7 +297,7 @@ int compile_split_cil() {
mmap_ro(path, &addr, &size); mmap_ro(path, &addr, &size);
if (cil_add_file(db, path, addr, size)) if (cil_add_file(db, path, addr, size))
return 1; return 1;
fprintf(stderr, "cil_add[%s]\n", path); LOGD("cil_add[%s]\n", path);
munmap(addr, size); munmap(addr, size);
} }
} }
@ -337,23 +319,19 @@ int dump_policydb(const char *filename) {
size_t len; size_t len;
policydb_to_image(NULL, policydb, &data, &len); policydb_to_image(NULL, policydb, &data, &len);
if (data == NULL) { if (data == NULL) {
fprintf(stderr, "Fail to dump policy image!"); LOGE("Fail to dump policy image!");
return 1; return 1;
} }
fd = creat(filename, 0644); fd = creat(filename, 0644);
if (fd < 0) { if (fd < 0) {
fprintf(stderr, "Can't open '%s': %s\n", LOGE("Can't open '%s': %s\n", filename, strerror(errno));
filename, strerror(errno));
return 1; return 1;
} }
ret = write(fd, data, len); ret = xwrite(fd, data, len);
close(fd); close(fd);
if (ret < 0) { if (ret < 0)
fprintf(stderr, "Could not write policy to %s\n",
filename);
return 1; return 1;
}
return 0; return 0;
} }
@ -366,7 +344,7 @@ void destroy_policydb() {
int create_domain(const char *d) { int create_domain(const char *d) {
symtab_datum_t *src = hashtab_search(policydb->p_types.table, d); symtab_datum_t *src = hashtab_search(policydb->p_types.table, d);
if(src) { if(src) {
fprintf(stderr, "Domain %s already exists\n", d); LOGW("Domain %s already exists\n", d);
return 0; return 0;
} }
@ -420,18 +398,18 @@ int set_domain_state(const char *s, int state) {
hashtab_for_each(policydb->p_types.table, &cur) { hashtab_for_each(policydb->p_types.table, &cur) {
type = cur->datum; type = cur->datum;
if (ebitmap_set_bit(&policydb->permissive_map, type->s.value, state)) { if (ebitmap_set_bit(&policydb->permissive_map, type->s.value, state)) {
fprintf(stderr, "Could not set bit in permissive map\n"); LOGW("Could not set bit in permissive map\n");
return 1; return 1;
} }
} }
} else { } else {
type = hashtab_search(policydb->p_types.table, s); type = hashtab_search(policydb->p_types.table, s);
if (type == NULL) { if (type == NULL) {
fprintf(stderr, "type %s does not exist\n", s); LOGW("type %s does not exist\n", s);
return 1; return 1;
} }
if (ebitmap_set_bit(&policydb->permissive_map, type->s.value, state)) { if (ebitmap_set_bit(&policydb->permissive_map, type->s.value, state)) {
fprintf(stderr, "Could not set bit in permissive map\n"); LOGW("Could not set bit in permissive map\n");
return 1; return 1;
} }
} }
@ -449,22 +427,22 @@ int add_transition(const char *s, const char *t, const char *c, const char *d) {
src = hashtab_search(policydb->p_types.table, s); src = hashtab_search(policydb->p_types.table, s);
if (src == NULL) { if (src == NULL) {
fprintf(stderr, "source type %s does not exist\n", s); LOGW("source type %s does not exist\n", s);
return 1; return 1;
} }
tgt = hashtab_search(policydb->p_types.table, t); tgt = hashtab_search(policydb->p_types.table, t);
if (tgt == NULL) { if (tgt == NULL) {
fprintf(stderr, "target type %s does not exist\n", t); LOGW("target type %s does not exist\n", t);
return 1; return 1;
} }
cls = hashtab_search(policydb->p_classes.table, c); cls = hashtab_search(policydb->p_classes.table, c);
if (cls == NULL) { if (cls == NULL) {
fprintf(stderr, "class %s does not exist\n", c); LOGW("class %s does not exist\n", c);
return 1; return 1;
} }
def = hashtab_search(policydb->p_types.table, d); def = hashtab_search(policydb->p_types.table, d);
if (def == NULL) { if (def == NULL) {
fprintf(stderr, "default type %s does not exist\n", d); LOGW("default type %s does not exist\n", d);
return 1; return 1;
} }
@ -482,7 +460,7 @@ int add_transition(const char *s, const char *t, const char *c, const char *d) {
if (new_rule) { if (new_rule) {
if (avtab_insert(&policydb->te_avtab, &key, av)) { if (avtab_insert(&policydb->te_avtab, &key, av)) {
fprintf(stderr, "Error inserting into avtab\n"); LOGW("Error inserting into avtab\n");
return 1; return 1;
} }
free(av); free(av);
@ -497,22 +475,22 @@ int add_file_transition(const char *s, const char *t, const char *c, const char
src = hashtab_search(policydb->p_types.table, s); src = hashtab_search(policydb->p_types.table, s);
if (src == NULL) { if (src == NULL) {
fprintf(stderr, "source type %s does not exist\n", s); LOGW("source type %s does not exist\n", s);
return 1; return 1;
} }
tgt = hashtab_search(policydb->p_types.table, t); tgt = hashtab_search(policydb->p_types.table, t);
if (tgt == NULL) { if (tgt == NULL) {
fprintf(stderr, "target type %s does not exist\n", t); LOGW("target type %s does not exist\n", t);
return 1; return 1;
} }
cls = hashtab_search(policydb->p_classes.table, c); cls = hashtab_search(policydb->p_classes.table, c);
if (cls == NULL) { if (cls == NULL) {
fprintf(stderr, "class %s does not exist\n", c); LOGW("class %s does not exist\n", c);
return 1; return 1;
} }
def = hashtab_search(policydb->p_types.table, d); def = hashtab_search(policydb->p_types.table, d);
if (def == NULL) { if (def == NULL) {
fprintf(stderr, "default type %s does not exist\n", d); LOGW("default type %s does not exist\n", d);
return 1; return 1;
} }
@ -540,7 +518,7 @@ int add_typeattribute(const char *domainS, const char *attr) {
domain = hashtab_search(policydb->p_types.table, domainS); domain = hashtab_search(policydb->p_types.table, domainS);
if (domain == NULL) { if (domain == NULL) {
fprintf(stderr, "source type %s does not exist\n", domainS); LOGW("source type %s does not exist\n", domainS);
return 1; return 1;
} }
@ -572,7 +550,7 @@ int add_rule(const char *s, const char *t, const char *c, const char *p, int eff
if (s) { if (s) {
src = hashtab_search(policydb->p_types.table, s); src = hashtab_search(policydb->p_types.table, s);
if (src == NULL) { if (src == NULL) {
fprintf(stderr, "source type %s does not exist\n", s); LOGW("source type %s does not exist\n", s);
return 1; return 1;
} }
} }
@ -580,7 +558,7 @@ int add_rule(const char *s, const char *t, const char *c, const char *p, int eff
if (t) { if (t) {
tgt = hashtab_search(policydb->p_types.table, t); tgt = hashtab_search(policydb->p_types.table, t);
if (tgt == NULL) { if (tgt == NULL) {
fprintf(stderr, "target type %s does not exist\n", t); LOGW("target type %s does not exist\n", t);
return 1; return 1;
} }
} }
@ -588,28 +566,26 @@ int add_rule(const char *s, const char *t, const char *c, const char *p, int eff
if (c) { if (c) {
cls = hashtab_search(policydb->p_classes.table, c); cls = hashtab_search(policydb->p_classes.table, c);
if (cls == NULL) { if (cls == NULL) {
fprintf(stderr, "class %s does not exist\n", c); LOGW("class %s does not exist\n", c);
return 1; return 1;
} }
} }
if (p) { if (p) {
if (c == NULL) { if (c == NULL) {
fprintf(stderr, "No class is specified, cannot add perm [%s] \n", p); LOGW("No class is specified, cannot add perm [%s] \n", p);
return 1; return 1;
} }
if (cls != NULL) {
perm = hashtab_search(cls->permissions.table, p); perm = hashtab_search(cls->permissions.table, p);
if (perm == NULL && cls->comdatum != NULL) { if (perm == NULL && cls->comdatum != NULL) {
perm = hashtab_search(cls->comdatum->permissions.table, p); perm = hashtab_search(cls->comdatum->permissions.table, p);
} }
if (perm == NULL) { if (perm == NULL) {
fprintf(stderr, "perm %s does not exist in class %s\n", p, c); LOGW("perm %s does not exist in class %s\n", p, c);
return 1; return 1;
} }
} }
}
return add_rule_auto(src, tgt, cls, perm, effect, n); return add_rule_auto(src, tgt, cls, perm, effect, n);
} }
@ -621,7 +597,7 @@ int add_xperm_rule(const char *s, const char *t, const char *c, const char *rang
if (s) { if (s) {
src = hashtab_search(policydb->p_types.table, s); src = hashtab_search(policydb->p_types.table, s);
if (src == NULL) { if (src == NULL) {
fprintf(stderr, "source type %s does not exist\n", s); LOGW("source type %s does not exist\n", s);
return 1; return 1;
} }
} }
@ -629,7 +605,7 @@ int add_xperm_rule(const char *s, const char *t, const char *c, const char *rang
if (t) { if (t) {
tgt = hashtab_search(policydb->p_types.table, t); tgt = hashtab_search(policydb->p_types.table, t);
if (tgt == NULL) { if (tgt == NULL) {
fprintf(stderr, "target type %s does not exist\n", t); LOGW("target type %s does not exist\n", t);
return 1; return 1;
} }
} }
@ -637,7 +613,7 @@ int add_xperm_rule(const char *s, const char *t, const char *c, const char *rang
if (c) { if (c) {
cls = hashtab_search(policydb->p_classes.table, c); cls = hashtab_search(policydb->p_classes.table, c);
if (cls == NULL) { if (cls == NULL) {
fprintf(stderr, "class %s does not exist\n", c); LOGW("class %s does not exist\n", c);
return 1; return 1;
} }
} }

View File

@ -333,10 +333,6 @@ int main(int argc, char *argv[]) {
return dump_magiskrc(argv[3], 0755); return dump_magiskrc(argv[3], 0755);
} }
#ifdef MAGISK_DEBUG
log_cb.d = vprintf;
#endif
// Prevent file descriptor confusion // Prevent file descriptor confusion
mknod("/null", S_IFCHR | 0666, makedev(1, 3)); mknod("/null", S_IFCHR | 0666, makedev(1, 3));
int null = open("/null", O_RDWR | O_CLOEXEC); int null = open("/null", O_RDWR | O_CLOEXEC);