Magisk/sepolicy.c

465 lines
12 KiB
C
Raw Normal View History

2017-03-18 09:52:38 +01:00
#include "magiskpolicy.h"
2017-04-15 13:26:29 +02:00
#include "sepolicy.h"
2013-06-28 03:42:09 +02:00
2017-04-15 20:29:42 +02:00
policydb_t *policydb = NULL;
2017-01-31 17:51:45 +01:00
static void *cmalloc(size_t s) {
2013-06-28 03:42:09 +02:00
void *t = malloc(s);
if (t == NULL) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
return t;
}
2017-01-31 17:51:45 +01:00
static int get_attr(char *type, int value) {
2017-04-15 20:29:42 +02:00
type_datum_t *attr = hashtab_search(policydb->p_types.table, type);
2015-10-26 00:11:37 +01:00
if (!attr)
return 1;
2015-10-26 00:11:37 +01:00
if (attr->flavor != TYPE_ATTRIB)
return 1;
2015-10-26 00:11:37 +01:00
2017-04-15 20:29:42 +02:00
return !! ebitmap_get_bit(&policydb->attr_type_map[attr->s.value-1], value-1);
//return !! ebitmap_get_bit(&policydb->type_attr_map[value-1], attr->s.value-1);
2015-10-26 00:11:37 +01:00
}
2017-01-31 17:51:45 +01:00
static int get_attr_id(char *type) {
2017-04-15 20:29:42 +02:00
type_datum_t *attr = hashtab_search(policydb->p_types.table, type);
if (!attr)
return 1;
if (attr->flavor != TYPE_ATTRIB)
return 1;
return attr->s.value;
}
2017-01-31 17:51:45 +01:00
static int set_attr(char *type, int value) {
2017-04-15 20:29:42 +02:00
type_datum_t *attr = hashtab_search(policydb->p_types.table, type);
2015-06-12 12:03:58 +02:00
if (!attr)
return 1;
2015-06-12 12:03:58 +02:00
if (attr->flavor != TYPE_ATTRIB)
return 1;
2015-06-12 12:03:58 +02:00
2017-04-15 20:29:42 +02:00
if(ebitmap_set_bit(&policydb->type_attr_map[value-1], attr->s.value-1, 1))
return 1;
2017-04-15 20:29:42 +02:00
if(ebitmap_set_bit(&policydb->attr_type_map[attr->s.value-1], value-1, 1))
return 1;
return 0;
2015-06-12 12:03:58 +02:00
}
2017-01-31 17:51:45 +01:00
static int add_irule(int s, int t, int c, int p, int effect, int not) {
avtab_datum_t *av;
avtab_key_t key;
key.source_type = s;
key.target_type = t;
key.target_class = c;
key.specified = effect;
2017-04-15 20:29:42 +02:00
av = avtab_search(&policydb->te_avtab, &key);
2017-01-31 17:51:45 +01:00
if (av == NULL) {
av = cmalloc(sizeof(*av));
memset(av, 0, sizeof(*av));
2017-01-31 17:51:45 +01:00
av->data |= 1U << (p - 1);
2017-04-15 20:29:42 +02:00
int ret = avtab_insert(&policydb->te_avtab, &key, av);
2017-01-31 17:51:45 +01:00
if (ret) {
fprintf(stderr, "Error inserting into avtab\n");
return 1;
}
}
if(not)
av->data &= ~(1U << (p - 1));
else
av->data |= 1U << (p - 1);
return 0;
}
static int add_rule_auto(type_datum_t *src, type_datum_t *tgt, class_datum_t *cls, perm_datum_t *perm, int effect, int not) {
hashtab_ptr_t cur;
2017-02-03 21:24:22 +01:00
int ret = 0;
2017-01-31 17:51:45 +01:00
if (src == NULL) {
2017-04-15 20:29:42 +02:00
hashtab_for_each(policydb->p_types.table, &cur) {
2017-02-03 21:24:22 +01:00
src = cur->datum;
if(add_rule_auto(src, tgt, cls, perm, effect, not))
return 1;
2017-01-31 17:51:45 +01:00
}
} else if (tgt == NULL) {
2017-04-15 20:29:42 +02:00
hashtab_for_each(policydb->p_types.table, &cur) {
2017-02-03 21:24:22 +01:00
tgt = cur->datum;
if(add_rule_auto(src, tgt, cls, perm, effect, not))
return 1;
2017-01-31 17:51:45 +01:00
}
} else if (cls == NULL) {
2017-04-15 20:29:42 +02:00
hashtab_for_each(policydb->p_classes.table, &cur) {
2017-02-03 21:24:22 +01:00
cls = cur->datum;
if(add_rule_auto(src, tgt, cls, perm, effect, not))
return 1;
2017-01-31 17:51:45 +01:00
}
} else if (perm == NULL) {
2017-02-03 21:24:22 +01:00
hashtab_for_each(cls->permissions.table, &cur) {
perm = cur->datum;
if(add_rule_auto(src, tgt, cls, perm, effect, not))
return 1;
2017-01-31 17:51:45 +01:00
}
if (cls->comdatum != NULL) {
2017-02-03 21:24:22 +01:00
hashtab_for_each(cls->comdatum->permissions.table, &cur) {
perm = cur->datum;
if(add_rule_auto(src, tgt, cls, perm, effect, not))
return 1;
2017-01-31 17:51:45 +01:00
}
}
} else {
2017-02-03 21:24:22 +01:00
ebitmap_node_t *s_node, *t_node;
int i, j;
if (src->flavor == TYPE_ATTRIB) {
if (tgt->flavor == TYPE_ATTRIB) {
2017-04-15 20:29:42 +02:00
ebitmap_for_each_bit(&policydb->attr_type_map[src->s.value-1], s_node, i)
ebitmap_for_each_bit(&policydb->attr_type_map[tgt->s.value-1], t_node, j)
2017-02-03 21:24:22 +01:00
if(ebitmap_node_get_bit(s_node, i) && ebitmap_node_get_bit(t_node, j))
ret |= add_irule(i + 1, j + 1, cls->s.value, perm->s.value, effect, not);
} else {
2017-04-15 20:29:42 +02:00
ebitmap_for_each_bit(&policydb->attr_type_map[src->s.value-1], s_node, i)
2017-02-03 21:24:22 +01:00
if(ebitmap_node_get_bit(s_node, i))
ret |= add_irule(i + 1, tgt->s.value, cls->s.value, perm->s.value, effect, not);
}
} else if (tgt->flavor == TYPE_ATTRIB) {
2017-04-15 20:29:42 +02:00
ebitmap_for_each_bit(&policydb->attr_type_map[tgt->s.value-1], t_node, j)
2017-02-03 21:24:22 +01:00
if(ebitmap_node_get_bit(t_node, j))
ret |= add_irule(src->s.value, j + 1, cls->s.value, perm->s.value, effect, not);
} else
ret = add_irule(src->s.value, tgt->s.value, cls->s.value, perm->s.value, effect, not);
2017-01-31 17:51:45 +01:00
}
2017-02-03 21:24:22 +01:00
return ret;
2017-01-31 17:51:45 +01:00
}
2017-04-15 20:29:42 +02:00
int load_policydb(const char *filename) {
2017-01-31 17:51:45 +01:00
int fd;
struct stat sb;
2017-02-04 10:30:34 +01:00
struct policy_file pf;
2017-01-31 17:51:45 +01:00
void *map;
int ret;
2017-04-15 20:29:42 +02:00
if (policydb)
destroy_policydb();
policydb = cmalloc(sizeof(*policydb));
2017-01-31 17:51:45 +01:00
fd = open(filename, O_RDONLY);
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;
}
2017-02-04 10:30:34 +01:00
policy_file_init(&pf);
pf.type = PF_USE_MEMORY;
pf.data = map;
pf.len = sb.st_size;
2017-04-15 20:29:42 +02:00
if (policydb_init(policydb)) {
2017-01-31 17:51:45 +01:00
fprintf(stderr, "policydb_init: Out of memory!\n");
return 1;
}
2017-04-15 20:29:42 +02:00
ret = policydb_read(policydb, &pf, 0);
2017-01-31 17:51:45 +01:00
if (ret) {
fprintf(stderr, "error(s) encountered while parsing configuration\n");
return 1;
}
2017-04-15 20:29:42 +02:00
close(fd);
2017-01-31 17:51:45 +01:00
return 0;
}
2017-04-15 20:29:42 +02:00
int dump_policydb(const char *filename) {
2017-02-04 10:30:34 +01:00
int fd, ret;
void *data = NULL;
size_t len;
2017-04-15 20:29:42 +02:00
policydb_to_image(NULL, policydb, &data, &len);
2017-02-04 10:30:34 +01:00
if (data == NULL) {
2017-04-15 20:29:42 +02:00
fprintf(stderr, "Fail to dump policy image!");
2017-02-04 10:30:34 +01:00
return 1;
}
fd = open(filename, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH);
if (fd < 0) {
fprintf(stderr, "Can't open '%s': %s\n",
filename, strerror(errno));
return 1;
}
ret = write(fd, data, len);
close(fd);
if (ret < 0) {
fprintf(stderr, "Could not write policy to %s\n",
filename);
return 1;
}
return 0;
}
2017-04-15 20:29:42 +02:00
void destroy_policydb() {
policydb_destroy(policydb);
free(policydb);
policydb = NULL;
}
int create_domain(char *d) {
2017-04-15 20:29:42 +02:00
symtab_datum_t *src = hashtab_search(policydb->p_types.table, d);
2015-06-12 12:03:58 +02:00
if(src)
return 1;
2015-06-12 12:03:58 +02:00
type_datum_t *typdatum = (type_datum_t *) malloc(sizeof(type_datum_t));
type_datum_init(typdatum);
typdatum->primary = 1;
typdatum->flavor = TYPE_TYPE;
uint32_t value = 0;
2017-04-15 20:29:42 +02:00
int r = symtab_insert(policydb, SYM_TYPES, strdup(d), typdatum, SCOPE_DECL, 1, &value);
2015-06-12 12:03:58 +02:00
typdatum->s.value = value;
2017-04-15 20:29:42 +02:00
if (ebitmap_set_bit(&policydb->global->branch_list->declared.scope[SYM_TYPES], value - 1, 1)) {
return 1;
2015-06-12 12:03:58 +02:00
}
2017-04-15 20:29:42 +02:00
policydb->type_attr_map = realloc(policydb->type_attr_map, sizeof(ebitmap_t)*policydb->p_types.nprim);
policydb->attr_type_map = realloc(policydb->attr_type_map, sizeof(ebitmap_t)*policydb->p_types.nprim);
ebitmap_init(&policydb->type_attr_map[value-1]);
ebitmap_init(&policydb->attr_type_map[value-1]);
ebitmap_set_bit(&policydb->type_attr_map[value-1], value-1, 1);
2015-06-12 12:03:58 +02:00
//Add the domain to all roles
2017-04-15 20:29:42 +02:00
for(unsigned i=0; i<policydb->p_roles.nprim; ++i) {
2015-06-12 12:03:58 +02:00
//Not sure all those three calls are needed
2017-04-15 20:29:42 +02:00
ebitmap_set_bit(&policydb->role_val_to_struct[i]->types.negset, value-1, 0);
ebitmap_set_bit(&policydb->role_val_to_struct[i]->types.types, value-1, 1);
type_set_expand(&policydb->role_val_to_struct[i]->types, &policydb->role_val_to_struct[i]->cache, policydb, 0);
2015-06-12 12:03:58 +02:00
}
2017-04-15 20:29:42 +02:00
src = hashtab_search(policydb->p_types.table, d);
2015-06-12 12:03:58 +02:00
if(!src)
return 1;
2015-06-12 12:03:58 +02:00
2017-04-15 20:29:42 +02:00
if(policydb_index_decls(NULL, policydb))
return 1;
2015-06-12 12:03:58 +02:00
2017-04-15 20:29:42 +02:00
if(policydb_index_classes(policydb))
return 1;
2015-06-12 12:03:58 +02:00
2017-04-15 20:29:42 +02:00
if(policydb_index_others(NULL, policydb, 0))
return 1;
2015-06-12 12:03:58 +02:00
return set_attr("domain", value);
2015-06-12 12:03:58 +02:00
}
2017-02-03 21:24:22 +01:00
int set_domain_state(char* s, int state) {
type_datum_t *type;
hashtab_ptr_t cur;
if (s == NULL) {
2017-04-15 20:29:42 +02:00
hashtab_for_each(policydb->p_types.table, &cur) {
type = cur->datum;
2017-04-15 20:29:42 +02:00
if (ebitmap_set_bit(&policydb->permissive_map, type->s.value, state)) {
fprintf(stderr, "Could not set bit in permissive map\n");
return 1;
}
}
} else {
2017-04-15 20:29:42 +02:00
type = hashtab_search(policydb->p_types.table, s);
if (type == NULL) {
fprintf(stderr, "type %s does not exist\n", s);
return 1;
}
2017-04-15 20:29:42 +02:00
if (ebitmap_set_bit(&policydb->permissive_map, type->s.value, state)) {
fprintf(stderr, "Could not set bit in permissive map\n");
2013-06-28 03:42:09 +02:00
return 1;
}
2013-06-28 03:42:09 +02:00
}
2017-02-03 21:24:22 +01:00
return 0;
2013-06-28 03:42:09 +02:00
}
2015-11-03 10:52:03 +01:00
int add_transition(char *s, char *t, char *c, char *d) {
type_datum_t *src, *tgt, *def;
class_datum_t *cls;
avtab_datum_t *av;
avtab_key_t key;
2017-04-15 20:29:42 +02:00
src = hashtab_search(policydb->p_types.table, s);
if (src == NULL) {
fprintf(stderr, "source type %s does not exist\n", s);
return 1;
}
2017-04-15 20:29:42 +02:00
tgt = hashtab_search(policydb->p_types.table, t);
if (tgt == NULL) {
fprintf(stderr, "target type %s does not exist\n", t);
return 1;
}
2017-04-15 20:29:42 +02:00
cls = hashtab_search(policydb->p_classes.table, c);
if (cls == NULL) {
fprintf(stderr, "class %s does not exist\n", c);
return 1;
}
2017-04-15 20:29:42 +02:00
def = hashtab_search(policydb->p_types.table, d);
if (def == NULL) {
fprintf(stderr, "default type %s does not exist\n", d);
return 1;
}
key.source_type = src->s.value;
key.target_type = tgt->s.value;
key.target_class = cls->s.value;
key.specified = AVTAB_TRANSITION;
2017-04-15 20:29:42 +02:00
av = avtab_search(&policydb->te_avtab, &key);
if (av == NULL) {
2015-11-01 17:32:00 +01:00
av = cmalloc(sizeof(*av));
av->data = def->s.value;
2017-04-15 20:29:42 +02:00
int ret = avtab_insert(&policydb->te_avtab, &key, av);
if (ret) {
fprintf(stderr, "Error inserting into avtab\n");
return 1;
}
} else {
fprintf(stderr, "Warning, rule already defined! Won't override.\n");
fprintf(stderr, "Previous value = %d, wanted value = %d\n", av->data, def->s.value);
}
return 0;
}
2013-06-28 03:42:09 +02:00
int add_file_transition(char *s, char *t, char *c, char *d, char* filename) {
type_datum_t *src, *tgt, *def;
class_datum_t *cls;
2017-04-15 20:29:42 +02:00
src = hashtab_search(policydb->p_types.table, s);
if (src == NULL) {
fprintf(stderr, "source type %s does not exist\n", s);
return 1;
}
2017-04-15 20:29:42 +02:00
tgt = hashtab_search(policydb->p_types.table, t);
if (tgt == NULL) {
fprintf(stderr, "target type %s does not exist\n", t);
return 1;
}
2017-04-15 20:29:42 +02:00
cls = hashtab_search(policydb->p_classes.table, c);
if (cls == NULL) {
fprintf(stderr, "class %s does not exist\n", c);
return 1;
}
2017-04-15 20:29:42 +02:00
def = hashtab_search(policydb->p_types.table, d);
if (def == NULL) {
fprintf(stderr, "default type %s does not exist\n", d);
return 1;
}
2017-04-05 03:13:09 +02:00
filename_trans_t *new_trans_key = cmalloc(sizeof(*new_trans_key));
new_trans_key->stype = src->s.value;
new_trans_key->ttype = tgt->s.value;
new_trans_key->tclass = cls->s.value;
new_trans_key->name = strdup(filename);
filename_trans_datum_t *new_trans_datam = cmalloc(sizeof(*new_trans_datam));
new_trans_datam->otype = def->s.value;
2017-04-15 20:29:42 +02:00
hashtab_insert(policydb->filename_trans, (hashtab_key_t) new_trans_key, new_trans_datam);
return 0;
}
int add_typeattribute(char *domainS, char *attr) {
type_datum_t *domain;
2017-04-15 20:29:42 +02:00
domain = hashtab_search(policydb->p_types.table, domainS);
if (domain == NULL) {
fprintf(stderr, "source type %s does not exist\n", domainS);
return 1;
}
set_attr(attr, domain->s.value);
int typeId = get_attr_id(attr);
//Now let's update all constraints!
//(kernel doesn't support (yet?) type_names rules)
2017-04-15 20:29:42 +02:00
for(int i=0; i<policydb->p_classes.nprim; ++i) {
class_datum_t *cl = policydb->class_val_to_struct[i];
for(constraint_node_t *n = cl->constraints; n ; n=n->next) {
for(constraint_expr_t *e = n->expr; e; e=e->next) {
if(e->expr_type == CEXPR_NAMES) {
if(ebitmap_get_bit(&e->type_names->types, typeId-1)) {
ebitmap_set_bit(&e->names, domain->s.value-1, 1);
}
}
}
}
}
2015-10-26 00:11:37 +01:00
return 0;
}
2017-01-31 17:51:45 +01:00
int add_rule(char *s, char *t, char *c, char *p, int effect, int not) {
2016-09-13 00:34:13 +02:00
type_datum_t *src = NULL, *tgt = NULL;
class_datum_t *cls = NULL;
perm_datum_t *perm = NULL;
if (s) {
2017-04-15 20:29:42 +02:00
src = hashtab_search(policydb->p_types.table, s);
2016-09-13 00:34:13 +02:00
if (src == NULL) {
fprintf(stderr, "source type %s does not exist\n", s);
return 1;
}
}
if (t) {
2017-04-15 20:29:42 +02:00
tgt = hashtab_search(policydb->p_types.table, t);
2016-09-13 00:34:13 +02:00
if (tgt == NULL) {
fprintf(stderr, "target type %s does not exist\n", t);
return 1;
}
}
if (c) {
2017-04-15 20:29:42 +02:00
cls = hashtab_search(policydb->p_classes.table, c);
2016-09-13 00:34:13 +02:00
if (cls == NULL) {
fprintf(stderr, "class %s does not exist\n", c);
return 1;
}
}
if (p) {
if (c == NULL) {
fprintf(stderr, "No class is specified, cannot add perm [%s] \n", p);
return 1;
}
if (cls != NULL) {
perm = hashtab_search(cls->permissions.table, p);
if (perm == NULL && cls->comdatum != NULL) {
perm = hashtab_search(cls->comdatum->permissions.table, p);
}
if (perm == NULL) {
fprintf(stderr, "perm %s does not exist in class %s\n", p, c);
return 1;
}
}
}
2017-01-31 17:51:45 +01:00
return add_rule_auto(src, tgt, cls, perm, effect, not);
2016-09-13 00:34:13 +02:00
}