Magisk/native/jni/magiskpolicy/sepolicy.c

497 lines
13 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <sepol/policydb/expand.h>
2019-02-10 09:57:51 +01:00
#include <utils.h>
#include <logging.h>
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
extern int policydb_index_decls(sepol_handle_t * handle, policydb_t * p);
2017-04-15 20:29:42 +02:00
2018-11-08 10:20:16 +01:00
static int get_attr(const 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
return ebitmap_get_bit(&policydb->attr_type_map[attr->s.value - 1], value - 1) != 0;
2015-10-26 00:11:37 +01:00
}
2018-11-08 10:20:16 +01:00
static int get_attr_id(const 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;
}
2018-11-08 10:20:16 +01:00
static int set_attr(const 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
}
2018-11-29 11:42:08 +01:00
static void check_avtab_node(avtab_ptr_t node) {
int redundant = 0;
if (node->key.specified == AVTAB_AUDITDENY)
redundant = node->datum.data == ~0U;
else if (node->key.specified & AVTAB_XPERMS)
redundant = node->datum.xperms == NULL;
else
redundant = node->datum.data == 0U;
if (redundant)
avtab_remove_node(&policydb->te_avtab, node);
}
static avtab_ptr_t get_avtab_node(avtab_key_t *key, avtab_extended_perms_t *xperms) {
avtab_ptr_t node;
avtab_datum_t avdatum;
int match = 0;
/* AVTAB_XPERMS entries are not necessarily unique */
if (key->specified & AVTAB_XPERMS) {
node = avtab_search_node(&policydb->te_avtab, key);
while (node) {
if ((node->datum.xperms->specified == xperms->specified) &&
(node->datum.xperms->driver == xperms->driver)) {
match = 1;
break;
}
node = avtab_search_node_next(node, key->specified);
}
if (!match)
node = NULL;
} else {
node = avtab_search_node(&policydb->te_avtab, key);
}
2017-01-31 17:51:45 +01:00
if (!node) {
memset(&avdatum, 0, sizeof avdatum);
/*
* AUDITDENY, aka DONTAUDIT, are &= assigned, versus |= for
* others. Initialize the data accordingly.
*/
avdatum.data = key->specified == AVTAB_AUDITDENY ? ~0U : 0U;
/* this is used to get the node - insertion is actually unique */
node = avtab_insert_nonunique(&policydb->te_avtab, key, &avdatum);
2017-04-19 20:15:10 +02:00
}
return node;
}
static int add_avrule(avtab_key_t *key, int p, int not) {
2018-11-29 11:42:08 +01:00
avtab_ptr_t node = get_avtab_node(key, NULL);
// Support DONTAUDIT (AUDITDENY is inverted)
if (AVTAB_AUDITDENY == node->key.specified == !not) {
2017-04-19 20:15:10 +02:00
if (p < 0)
2018-11-29 11:42:08 +01:00
node->datum.data = 0U;
2017-04-19 20:15:10 +02:00
else
2018-11-29 11:42:08 +01:00
node->datum.data &= ~(1U << (p - 1));
2017-04-19 20:15:10 +02:00
} else {
if (p < 0)
2018-11-29 11:42:08 +01:00
node->datum.data = ~0U;
2017-04-19 20:15:10 +02:00
else
2018-11-29 11:42:08 +01:00
node->datum.data |= 1U << (p - 1);
2017-01-31 17:51:45 +01:00
}
2018-11-29 11:42:08 +01:00
check_avtab_node(node);
2017-01-31 17:51:45 +01:00
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) {
avtab_key_t key;
2017-01-31 17:51:45 +01:00
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) {
2018-12-01 09:53:58 +01:00
hashtab_for_each(policydb->p_types.table, cur, {
2017-02-03 21:24:22 +01:00
src = cur->datum;
2017-04-19 20:15:10 +02:00
ret |= add_rule_auto(src, tgt, cls, perm, effect, not);
2018-12-01 09:53:58 +01:00
})
2017-01-31 17:51:45 +01:00
} else if (tgt == NULL) {
2018-12-01 09:53:58 +01:00
hashtab_for_each(policydb->p_types.table, cur, {
2017-02-03 21:24:22 +01:00
tgt = cur->datum;
2017-04-19 20:15:10 +02:00
ret |= add_rule_auto(src, tgt, cls, perm, effect, not);
2018-12-01 09:53:58 +01:00
})
2017-01-31 17:51:45 +01:00
} else if (cls == NULL) {
2018-12-01 09:53:58 +01:00
hashtab_for_each(policydb->p_classes.table, cur, {
2017-02-03 21:24:22 +01:00
cls = cur->datum;
ret |= add_rule_auto(src, tgt, cls, perm, effect, not);
2018-12-01 09:53:58 +01:00
})
2017-01-31 17:51:45 +01:00
} else {
key.source_type = src->s.value;
key.target_type = tgt->s.value;
key.target_class = cls->s.value;
key.specified = effect;
return add_avrule(&key, perm ? perm->s.value : -1, not);
2017-04-19 22:04:09 +02:00
}
return ret;
}
#define ioctl_driver(x) (x>>8 & 0xFF)
#define ioctl_func(x) (x & 0xFF)
static int add_avxrule(avtab_key_t *key, uint16_t low, uint16_t high, int not) {
avtab_datum_t *datum;
avtab_extended_perms_t xperms;
2017-04-19 22:04:09 +02:00
memset(&xperms, 0, sizeof(xperms));
if (ioctl_driver(low) != ioctl_driver(high)) {
xperms.specified = AVTAB_XPERMS_IOCTLDRIVER;
xperms.driver = 0;
} else {
xperms.specified = AVTAB_XPERMS_IOCTLFUNCTION;
xperms.driver = ioctl_driver(low);
2017-04-19 22:04:09 +02:00
}
if (xperms.specified == AVTAB_XPERMS_IOCTLDRIVER) {
for (int i = ioctl_driver(low); i <= ioctl_driver(high); ++i) {
2017-04-19 22:04:09 +02:00
if (not)
xperm_clear(i, xperms.perms);
2017-04-19 22:04:09 +02:00
else
xperm_set(i, xperms.perms);
2017-04-19 22:04:09 +02:00
}
} else {
for (int i = ioctl_func(low); i <= ioctl_func(high); ++i) {
2017-04-19 22:04:09 +02:00
if (not)
xperm_clear(i, xperms.perms);
2017-04-19 22:04:09 +02:00
else
xperm_set(i, xperms.perms);
2017-04-19 22:04:09 +02:00
}
}
datum = &get_avtab_node(key, &xperms)->datum;
if (datum->xperms == NULL)
datum->xperms = xmalloc(sizeof(xperms));
2017-04-19 22:04:09 +02:00
memcpy(datum->xperms, &xperms, sizeof(xperms));
2017-04-19 22:04:09 +02:00
return 0;
}
static int add_xperm_rule_auto(type_datum_t *src, type_datum_t *tgt, class_datum_t *cls,
uint16_t low, uint16_t high, int effect, int not) {
avtab_key_t key;
2017-04-19 22:04:09 +02:00
hashtab_ptr_t cur;
int ret = 0;
if (src == NULL) {
2018-12-01 09:53:58 +01:00
hashtab_for_each(policydb->p_types.table, cur, {
2017-04-19 22:04:09 +02:00
src = cur->datum;
ret |= add_xperm_rule_auto(src, tgt, cls, low, high, effect, not);
2018-12-01 09:53:58 +01:00
})
2017-04-19 22:04:09 +02:00
} else if (tgt == NULL) {
2018-12-01 09:53:58 +01:00
hashtab_for_each(policydb->p_types.table, cur, {
2017-04-19 22:04:09 +02:00
tgt = cur->datum;
ret |= add_xperm_rule_auto(src, tgt, cls, low, high, effect, not);
2018-12-01 09:53:58 +01:00
})
2017-04-19 22:04:09 +02:00
} else if (cls == NULL) {
2018-12-01 09:53:58 +01:00
hashtab_for_each(policydb->p_classes.table, cur, {
2017-04-19 22:04:09 +02:00
cls = cur->datum;
ret |= add_xperm_rule_auto(src, tgt, cls, low, high, effect, not);
2018-12-01 09:53:58 +01:00
})
2017-04-19 22:04:09 +02:00
} else {
key.source_type = src->s.value;
key.target_type = tgt->s.value;
key.target_class = cls->s.value;
key.specified = effect;
return add_avxrule(&key, low, high, 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
}
2018-11-08 10:20:16 +01:00
int create_domain(const char *d) {
2017-04-15 20:29:42 +02:00
symtab_datum_t *src = hashtab_search(policydb->p_types.table, d);
2017-04-19 20:15:10 +02:00
if(src) {
2018-11-08 12:43:11 +01:00
LOGW("Domain %s already exists\n", d);
2017-08-27 20:13:36 +02:00
return 0;
2017-04-19 20:15:10 +02:00
}
2015-06-12 12:03:58 +02:00
2017-04-19 20:15:10 +02:00
type_datum_t *typedatum = (type_datum_t *) malloc(sizeof(type_datum_t));
type_datum_init(typedatum);
typedatum->primary = 1;
typedatum->flavor = TYPE_TYPE;
2015-06-12 12:03:58 +02:00
uint32_t value = 0;
symtab_insert(policydb, SYM_TYPES, strdup(d), typedatum, SCOPE_DECL, 1, &value);
2017-04-19 20:15:10 +02:00
typedatum->s.value = value;
2015-06-12 12:03:58 +02:00
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-19 20:15:10 +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);
2017-04-15 20:29:42 +02:00
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
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
//Add the domain to all roles
for(unsigned i=0; i<policydb->p_roles.nprim; ++i) {
//Not sure all those three calls are needed
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);
}
return set_attr("domain", value);
2015-06-12 12:03:58 +02:00
}
2018-11-08 10:20:16 +01:00
int set_domain_state(const char *s, int state) {
2017-02-03 21:24:22 +01:00
type_datum_t *type;
hashtab_ptr_t cur;
if (s == NULL) {
2018-12-01 09:53:58 +01: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)) {
2018-11-08 12:43:11 +01:00
LOGW("Could not set bit in permissive map\n");
return 1;
}
2018-12-01 09:53:58 +01:00
})
} else {
2017-04-15 20:29:42 +02:00
type = hashtab_search(policydb->p_types.table, s);
if (type == NULL) {
2018-11-08 12:43:11 +01:00
LOGW("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)) {
2018-11-08 12:43:11 +01:00
LOGW("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-08-27 20:13:36 +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 sepol_nametrans(const char *s, const char *t, const char *c, const char *d, const char *o) {
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) {
2018-11-08 12:43:11 +01:00
LOGW("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) {
2018-11-08 12:43:11 +01:00
LOGW("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) {
2018-11-08 12:43:11 +01:00
LOGW("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) {
2018-11-08 12:43:11 +01:00
LOGW("default type %s does not exist\n", d);
return 1;
}
2017-04-19 20:15:10 +02:00
filename_trans_t trans_key;
trans_key.stype = src->s.value;
trans_key.ttype = tgt->s.value;
trans_key.tclass = cls->s.value;
trans_key.name = (char *) o;
2017-04-05 03:13:09 +02:00
2017-04-19 20:15:10 +02:00
filename_trans_datum_t *trans_datum;
trans_datum = hashtab_search(policydb->p_types.table, (hashtab_key_t) &trans_key);
2017-04-05 03:13:09 +02:00
2017-04-19 20:15:10 +02:00
if (trans_datum == NULL) {
trans_datum = xcalloc(sizeof(*trans_datum), 1);
2017-04-19 20:15:10 +02:00
hashtab_insert(policydb->filename_trans, (hashtab_key_t) &trans_key, trans_datum);
}
2017-04-19 20:15:10 +02:00
// Overwrite existing
trans_datum->otype = def->s.value;
return 0;
}
2018-11-08 10:20:16 +01:00
int add_typeattribute(const char *domainS, const 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) {
2018-11-08 12:43:11 +01:00
LOGW("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;
}
2018-11-08 10:20:16 +01:00
int add_rule(const char *s, const char *t, const char *c, const char *p, int effect, int n) {
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) {
2018-11-08 12:43:11 +01:00
LOGW("source type %s does not exist\n", s);
2016-09-13 00:34:13 +02:00
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) {
2018-11-08 12:43:11 +01:00
LOGW("target type %s does not exist\n", t);
2016-09-13 00:34:13 +02:00
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) {
2018-11-08 12:43:11 +01:00
LOGW("class %s does not exist\n", c);
2016-09-13 00:34:13 +02:00
return 1;
}
}
if (p) {
if (c == NULL) {
2018-11-08 12:43:11 +01:00
LOGW("No class is specified, cannot add perm [%s] \n", p);
2016-09-13 00:34:13 +02:00
return 1;
}
2017-08-27 20:13:36 +02:00
2018-11-08 12:43:11 +01:00
perm = hashtab_search(cls->permissions.table, p);
if (perm == NULL && cls->comdatum != NULL) {
perm = hashtab_search(cls->comdatum->permissions.table, p);
}
if (perm == NULL) {
LOGW("perm %s does not exist in class %s\n", p, c);
return 1;
2016-09-13 00:34:13 +02:00
}
}
2018-11-08 10:20:16 +01:00
return add_rule_auto(src, tgt, cls, perm, effect, n);
2016-09-13 00:34:13 +02:00
}
2017-04-19 22:04:09 +02:00
2018-11-08 10:20:16 +01:00
int add_xperm_rule(const char *s, const char *t, const char *c, const char *range, int effect,
int n) {
2017-04-19 22:04:09 +02:00
type_datum_t *src = NULL, *tgt = NULL;
class_datum_t *cls = NULL;
if (s) {
src = hashtab_search(policydb->p_types.table, s);
if (src == NULL) {
2018-11-08 12:43:11 +01:00
LOGW("source type %s does not exist\n", s);
2017-04-19 22:04:09 +02:00
return 1;
}
}
if (t) {
tgt = hashtab_search(policydb->p_types.table, t);
if (tgt == NULL) {
2018-11-08 12:43:11 +01:00
LOGW("target type %s does not exist\n", t);
2017-04-19 22:04:09 +02:00
return 1;
}
}
if (c) {
cls = hashtab_search(policydb->p_classes.table, c);
if (cls == NULL) {
2018-11-08 12:43:11 +01:00
LOGW("class %s does not exist\n", c);
2017-04-19 22:04:09 +02:00
return 1;
}
}
uint16_t low, high;
if (range) {
if (strchr(range, '-')){
sscanf(range, "%hx-%hx", &low, &high);
} else {
sscanf(range, "%hx", &low);
high = low;
}
} else {
low = 0;
high = 0xFFFF;
}
2018-11-08 10:20:16 +01:00
return add_xperm_rule_auto(src, tgt, cls, low, high, effect, n);
2017-04-19 22:04:09 +02:00
}
int add_type_rule(const char *s, const char *t, const char *c, const char *d, int effect) {
type_datum_t *src, *tgt, *def;
class_datum_t *cls;
src = hashtab_search(policydb->p_types.table, s);
if (src == NULL) {
LOGW("source type %s does not exist\n", s);
return 1;
}
tgt = hashtab_search(policydb->p_types.table, t);
if (tgt == NULL) {
LOGW("target type %s does not exist\n", t);
return 1;
}
cls = hashtab_search(policydb->p_classes.table, c);
if (cls == NULL) {
LOGW("class %s does not exist\n", c);
return 1;
}
def = hashtab_search(policydb->p_types.table, d);
if (def == NULL) {
LOGW("default type %s does not exist\n", d);
return 1;
}
2018-11-29 11:42:08 +01:00
avtab_key_t key;
key.source_type = src->s.value;
key.target_type = tgt->s.value;
key.target_class = cls->s.value;
key.specified = effect;
2018-11-29 11:42:08 +01:00
avtab_ptr_t node = get_avtab_node(&key, NULL);
node->datum.data = def->s.value;
return 0;
}