Magisk/native/jni/magiskpolicy/sepolicy.c

497 lines
12 KiB
C
Raw Normal View History

#include <stdlib.h>
#include <sepol/policydb/expand.h>
2019-02-10 09:57:51 +01:00
#include <logging.h>
2017-04-15 13:26:29 +02:00
#include "sepolicy.h"
2013-06-28 03:42:09 +02:00
2019-11-19 11:20:18 +01:00
policydb_t *magisk_policydb = NULL;
#define mpdb magisk_policydb
2019-11-19 08:04:47 +01:00
extern void *xmalloc(size_t size);
extern void *xcalloc(size_t nmemb, size_t size);
extern void *xrealloc(void *ptr, size_t size);
extern int policydb_index_decls(sepol_handle_t * handle, policydb_t * p);
2017-04-15 20:29:42 +02:00
2019-11-19 11:20:18 +01:00
// Generic hash table traversal
#define hash_for_each(node_ptr, n_slots, table, block) \
for (int __i = 0; __i < (table)->n_slots; ++__i) { \
__typeof__(*(table)->node_ptr) node; \
__typeof__(node) __next; \
for (node = (table)->node_ptr[__i]; node; node = __next) { \
__next = node->next; \
block \
} \
} \
// hashtab traversal
#define hashtab_for_each(hashtab, block) \
hash_for_each(htable, size, hashtab, block)
// avtab traversal
#define avtab_for_each(avtab, block) \
hash_for_each(htable, nslot, avtab, block)
2018-11-08 10:20:16 +01:00
static int set_attr(const char *type, int value) {
2019-11-19 11:20:18 +01:00
type_datum_t *attr = hashtab_search(mpdb->p_types.table, type);
if (!attr || attr->flavor != TYPE_ATTRIB)
return -1;
2015-06-12 12:03:58 +02:00
2019-11-19 11:20:18 +01:00
if (ebitmap_set_bit(&mpdb->type_attr_map[value - 1], attr->s.value - 1, 1))
return -1;
if (ebitmap_set_bit(&mpdb->attr_type_map[attr->s.value - 1], value - 1, 1))
return -1;
2015-06-12 12:03:58 +02:00
2019-11-19 11:20:18 +01:00
return attr->s.value;
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)
2019-11-19 11:20:18 +01:00
avtab_remove_node(&mpdb->te_avtab, node);
2018-11-29 11:42:08 +01:00
}
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) {
2019-11-19 11:20:18 +01:00
node = avtab_search_node(&mpdb->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 {
2019-11-19 11:20:18 +01:00
node = avtab_search_node(&mpdb->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 */
2019-11-19 11:20:18 +01:00
node = avtab_insert_nonunique(&mpdb->te_avtab, key, &avdatum);
2017-04-19 20:15:10 +02:00
}
return node;
}
2019-11-19 11:20:18 +01:00
static int add_avrule(avtab_key_t *key, int val, int not) {
2018-11-29 11:42:08 +01:00
avtab_ptr_t node = get_avtab_node(key, NULL);
2019-11-19 11:20:18 +01:00
if (not) {
if (val < 0)
2018-11-29 11:42:08 +01:00
node->datum.data = 0U;
2017-04-19 20:15:10 +02:00
else
2019-11-19 11:20:18 +01:00
node->datum.data &= ~(1U << (val - 1));
2017-04-19 20:15:10 +02:00
} else {
2019-11-19 11:20:18 +01:00
if (val < 0)
2018-11-29 11:42:08 +01:00
node->datum.data = ~0U;
2017-04-19 20:15:10 +02:00
else
2019-11-19 11:20:18 +01:00
node->datum.data |= 1U << (val - 1);
2017-01-31 17:51:45 +01:00
}
2019-11-19 11:20:18 +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-02-03 21:24:22 +01:00
int ret = 0;
2017-01-31 17:51:45 +01:00
if (src == NULL) {
2019-11-19 11:20:18 +01:00
hashtab_for_each(mpdb->p_types.table, {
src = node->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) {
2019-11-19 11:20:18 +01:00
hashtab_for_each(mpdb->p_types.table, {
tgt = node->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) {
2019-11-19 11:20:18 +01:00
hashtab_for_each(mpdb->p_classes.table, {
cls = node->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
int ret = 0;
if (src == NULL) {
2019-11-19 11:20:18 +01:00
hashtab_for_each(mpdb->p_types.table, {
src = node->datum;
2017-04-19 22:04:09 +02:00
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) {
2019-11-19 11:20:18 +01:00
hashtab_for_each(mpdb->p_types.table, {
tgt = node->datum;
2017-04-19 22:04:09 +02:00
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) {
2019-11-19 11:20:18 +01:00
hashtab_for_each(mpdb->p_classes.table, {
cls = node->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) {
2019-11-19 11:20:18 +01:00
symtab_datum_t *src = hashtab_search(mpdb->p_types.table, d);
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
2019-11-19 11:20:18 +01:00
type_datum_t *typedatum = xmalloc(sizeof(type_datum_t));
2017-04-19 20:15:10 +02:00
type_datum_init(typedatum);
typedatum->primary = 1;
typedatum->flavor = TYPE_TYPE;
2015-06-12 12:03:58 +02:00
uint32_t value = 0;
2019-11-19 11:20:18 +01:00
symtab_insert(mpdb, 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
2019-11-19 11:20:18 +01:00
if (ebitmap_set_bit(&mpdb->global->branch_list->declared.scope[SYM_TYPES], value - 1, 1)) {
return 1;
2015-06-12 12:03:58 +02:00
}
2019-11-19 11:20:18 +01:00
mpdb->type_attr_map = xrealloc(mpdb->type_attr_map, sizeof(ebitmap_t) * mpdb->p_types.nprim);
mpdb->attr_type_map = xrealloc(mpdb->attr_type_map, sizeof(ebitmap_t) * mpdb->p_types.nprim);
ebitmap_init(&mpdb->type_attr_map[value-1]);
ebitmap_init(&mpdb->attr_type_map[value-1]);
ebitmap_set_bit(&mpdb->type_attr_map[value-1], value-1, 1);
2015-06-12 12:03:58 +02:00
2019-11-19 11:20:18 +01:00
src = hashtab_search(mpdb->p_types.table, d);
2015-06-12 12:03:58 +02:00
if(!src)
return 1;
2015-06-12 12:03:58 +02:00
2019-11-19 11:20:18 +01:00
if(policydb_index_decls(NULL, mpdb))
return 1;
2015-06-12 12:03:58 +02:00
2019-11-19 11:20:18 +01:00
if(policydb_index_classes(mpdb))
return 1;
2015-06-12 12:03:58 +02:00
2019-11-19 11:20:18 +01:00
if(policydb_index_others(NULL, mpdb, 0))
return 1;
2015-06-12 12:03:58 +02:00
//Add the domain to all roles
2019-11-19 11:20:18 +01:00
for(unsigned i = 0; i < mpdb->p_roles.nprim; ++i) {
//Not sure all those three calls are needed
2019-11-19 11:20:18 +01:00
ebitmap_set_bit(&mpdb->role_val_to_struct[i]->types.negset, value - 1, 0);
ebitmap_set_bit(&mpdb->role_val_to_struct[i]->types.types, value - 1, 1);
type_set_expand(&mpdb->role_val_to_struct[i]->types, &mpdb->role_val_to_struct[i]->cache, mpdb, 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;
if (s == NULL) {
2019-11-19 11:20:18 +01:00
hashtab_for_each(mpdb->p_types.table, {
type = node->datum;
if (ebitmap_set_bit(&mpdb->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 {
2019-11-19 11:20:18 +01:00
type = hashtab_search(mpdb->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;
}
2019-11-19 11:20:18 +01:00
if (ebitmap_set_bit(&mpdb->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
2019-11-19 08:04:47 +01:00
int add_filename_trans(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;
2019-11-19 11:20:18 +01:00
src = hashtab_search(mpdb->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;
}
2019-11-19 11:20:18 +01:00
tgt = hashtab_search(mpdb->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;
}
2019-11-19 11:20:18 +01:00
cls = hashtab_search(mpdb->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;
}
2019-11-19 11:20:18 +01:00
def = hashtab_search(mpdb->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;
2019-11-19 11:20:18 +01:00
trans_datum = hashtab_search(mpdb->filename_trans, (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);
2019-11-19 11:20:18 +01:00
hashtab_insert(mpdb->filename_trans, (hashtab_key_t) &trans_key, trans_datum);
2017-04-19 20:15:10 +02:00
}
2017-04-19 20:15:10 +02:00
// Overwrite existing
trans_datum->otype = def->s.value;
return 0;
}
2019-11-19 11:20:18 +01:00
int add_typeattribute(const char *type, const char *attr) {
type_datum_t *domain = hashtab_search(mpdb->p_types.table, type);
if (domain == NULL) {
2019-11-19 11:20:18 +01:00
LOGW("type %s does not exist\n", type);
return 1;
}
2019-11-19 11:20:18 +01:00
int attr_id = set_attr(attr, domain->s.value);
if (attr_id < 0)
return 1;
hashtab_for_each(mpdb->p_classes.table, {
class_datum_t *cls = node->datum;
for (constraint_node_t *n = cls->constraints; n ; n = n->next) {
for (constraint_expr_t *e = n->expr; e; e = e->next) {
if (e->expr_type == CEXPR_NAMES &&
ebitmap_get_bit(&e->type_names->types, attr_id - 1)) {
ebitmap_set_bit(&e->names, domain->s.value - 1, 1);
}
}
}
2019-11-19 11:20:18 +01:00
})
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) {
2019-11-19 11:20:18 +01:00
src = hashtab_search(mpdb->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) {
2019-11-19 11:20:18 +01:00
tgt = hashtab_search(mpdb->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) {
2019-11-19 11:20:18 +01:00
cls = hashtab_search(mpdb->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) {
2019-11-19 11:20:18 +01:00
src = hashtab_search(mpdb->p_types.table, s);
2017-04-19 22:04:09 +02:00
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) {
2019-11-19 11:20:18 +01:00
tgt = hashtab_search(mpdb->p_types.table, t);
2017-04-19 22:04:09 +02:00
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) {
2019-11-19 11:20:18 +01:00
cls = hashtab_search(mpdb->p_classes.table, c);
2017-04-19 22:04:09 +02:00
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;
2019-11-19 11:20:18 +01:00
src = hashtab_search(mpdb->p_types.table, s);
if (src == NULL) {
LOGW("source type %s does not exist\n", s);
return 1;
}
2019-11-19 11:20:18 +01:00
tgt = hashtab_search(mpdb->p_types.table, t);
if (tgt == NULL) {
LOGW("target type %s does not exist\n", t);
return 1;
}
2019-11-19 11:20:18 +01:00
cls = hashtab_search(mpdb->p_classes.table, c);
if (cls == NULL) {
LOGW("class %s does not exist\n", c);
return 1;
}
2019-11-19 11:20:18 +01:00
def = hashtab_search(mpdb->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;
}
2019-11-19 11:20:18 +01:00
void strip_dontaudit() {
avtab_for_each(&mpdb->te_avtab, {
if (node->key.specified == AVTAB_AUDITDENY || node->key.specified == AVTAB_XPERMS_DONTAUDIT)
avtab_remove_node(&magisk_policydb->te_avtab, node);
})
}