2018-07-20 23:12:22 +02:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sepol/policydb/expand.h>
|
|
|
|
|
2020-03-09 09:50:30 +01:00
|
|
|
#include <logging.hpp>
|
2019-02-10 09:57:51 +01:00
|
|
|
|
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);
|
2020-02-01 18:16:42 +01:00
|
|
|
|
|
|
|
// Internal libsepol APIs
|
2018-07-20 23:12:22 +02:00
|
|
|
extern int policydb_index_decls(sepol_handle_t * handle, policydb_t * p);
|
2020-02-01 18:16:42 +01:00
|
|
|
extern int context_from_string(
|
|
|
|
sepol_handle_t * handle,
|
|
|
|
const policydb_t * policydb,
|
|
|
|
context_struct_t ** cptr,
|
|
|
|
const char *con_str, size_t con_str_len);
|
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) \
|
2020-02-01 18:16:42 +01:00
|
|
|
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 \
|
2019-11-19 11:20:18 +01:00
|
|
|
} \
|
2020-02-01 18:16:42 +01:00
|
|
|
}
|
2019-11-19 11:20:18 +01:00
|
|
|
|
|
|
|
// 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)
|
2015-11-01 17:39:06 +01:00
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2018-11-29 09:46:29 +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);
|
2018-11-29 09:46:29 +01:00
|
|
|
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);
|
2018-11-29 09:46:29 +01:00
|
|
|
}
|
2017-01-31 17:51:45 +01:00
|
|
|
|
2018-11-29 09:46:29 +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
|
|
|
}
|
|
|
|
|
2018-11-29 09:46:29 +01: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;
|
|
|
|
}
|
|
|
|
|
2018-11-29 09:46:29 +01:00
|
|
|
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;
|
2018-11-29 09:46:29 +01: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 {
|
2018-11-29 09:46:29 +01:00
|
|
|
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)
|
|
|
|
|
2018-11-29 09:46:29 +01:00
|
|
|
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
|
|
|
|
2018-11-29 09:46:29 +01: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
|
|
|
}
|
|
|
|
|
2018-11-29 09:46:29 +01: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)
|
2018-11-29 09:46:29 +01:00
|
|
|
xperm_clear(i, xperms.perms);
|
2017-04-19 22:04:09 +02:00
|
|
|
else
|
2018-11-29 09:46:29 +01:00
|
|
|
xperm_set(i, xperms.perms);
|
2017-04-19 22:04:09 +02:00
|
|
|
}
|
|
|
|
} else {
|
2018-11-29 09:46:29 +01:00
|
|
|
for (int i = ioctl_func(low); i <= ioctl_func(high); ++i) {
|
2017-04-19 22:04:09 +02:00
|
|
|
if (not)
|
2018-11-29 09:46:29 +01:00
|
|
|
xperm_clear(i, xperms.perms);
|
2017-04-19 22:04:09 +02:00
|
|
|
else
|
2018-11-29 09:46:29 +01:00
|
|
|
xperm_set(i, xperms.perms);
|
2017-04-19 22:04:09 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-29 09:46:29 +01: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
|
|
|
|
2018-11-29 09:46:29 +01: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) {
|
2018-11-29 09:46:29 +01:00
|
|
|
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;
|
2018-11-29 09:46:29 +01: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 {
|
2018-11-29 09:46:29 +01:00
|
|
|
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)) {
|
2017-02-03 23:36:15 +01:00
|
|
|
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)
|
2017-02-03 23:36:15 +01:00
|
|
|
return 1;
|
2015-06-12 12:03:58 +02:00
|
|
|
|
2019-11-19 11:20:18 +01:00
|
|
|
if(policydb_index_decls(NULL, mpdb))
|
2017-02-03 23:36:15 +01:00
|
|
|
return 1;
|
2015-06-12 12:03:58 +02:00
|
|
|
|
2019-11-19 11:20:18 +01:00
|
|
|
if(policydb_index_classes(mpdb))
|
2017-02-03 23:36:15 +01:00
|
|
|
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))
|
2017-02-03 23:36:15 +01:00
|
|
|
return 1;
|
2015-06-12 12:03:58 +02:00
|
|
|
|
2017-07-13 04:12:54 +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) {
|
2017-07-13 04:12:54 +02:00
|
|
|
//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);
|
2017-07-13 04:12:54 +02:00
|
|
|
}
|
|
|
|
|
2017-02-03 23:36:15 +01:00
|
|
|
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;
|
2017-02-03 23:36:15 +01:00
|
|
|
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");
|
2017-02-03 23:36:15 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2018-12-01 09:53:58 +01:00
|
|
|
})
|
2017-02-03 23:36:15 +01:00
|
|
|
} else {
|
2019-11-19 11:20:18 +01:00
|
|
|
type = hashtab_search(mpdb->p_types.table, s);
|
2017-02-03 23:36:15 +01:00
|
|
|
if (type == NULL) {
|
2018-11-08 12:43:11 +01:00
|
|
|
LOGW("type %s does not exist\n", s);
|
|
|
|
return 1;
|
2017-02-03 23:36:15 +01:00
|
|
|
}
|
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;
|
2017-02-03 23:36:15 +01:00
|
|
|
}
|
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) {
|
2017-02-03 23:36:15 +01:00
|
|
|
type_datum_t *src, *tgt, *def;
|
2015-11-01 17:38:32 +01:00
|
|
|
class_datum_t *cls;
|
|
|
|
|
2019-11-19 11:20:18 +01:00
|
|
|
src = hashtab_search(mpdb->p_types.table, s);
|
2015-11-01 17:38:32 +01:00
|
|
|
if (src == NULL) {
|
2018-11-08 12:43:11 +01:00
|
|
|
LOGW("source type %s does not exist\n", s);
|
2015-11-01 17:38:32 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2019-11-19 11:20:18 +01:00
|
|
|
tgt = hashtab_search(mpdb->p_types.table, t);
|
2015-11-01 17:38:32 +01:00
|
|
|
if (tgt == NULL) {
|
2018-11-08 12:43:11 +01:00
|
|
|
LOGW("target type %s does not exist\n", t);
|
2015-11-01 17:38:32 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2019-11-19 11:20:18 +01:00
|
|
|
cls = hashtab_search(mpdb->p_classes.table, c);
|
2015-11-01 17:38:32 +01:00
|
|
|
if (cls == NULL) {
|
2018-11-08 12:43:11 +01:00
|
|
|
LOGW("class %s does not exist\n", c);
|
2015-11-01 17:38:32 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2019-11-19 11:20:18 +01:00
|
|
|
def = hashtab_search(mpdb->p_types.table, d);
|
2017-02-03 23:36:15 +01:00
|
|
|
if (def == NULL) {
|
2018-11-08 12:43:11 +01:00
|
|
|
LOGW("default type %s does not exist\n", d);
|
2015-11-01 17:38:32 +01:00
|
|
|
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;
|
2018-11-29 09:46:29 +01:00
|
|
|
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) {
|
2018-11-29 09:46:29 +01:00
|
|
|
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
|
|
|
}
|
2015-11-01 17:38:32 +01:00
|
|
|
|
2017-04-19 20:15:10 +02:00
|
|
|
// Overwrite existing
|
|
|
|
trans_datum->otype = def->s.value;
|
2015-11-01 17:38:32 +01:00
|
|
|
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);
|
2015-10-25 18:10:06 +01:00
|
|
|
if (domain == NULL) {
|
2019-11-19 11:20:18 +01:00
|
|
|
LOGW("type %s does not exist\n", type);
|
2015-10-25 18:10:06 +01:00
|
|
|
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);
|
2015-11-01 17:39:06 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-11-19 11:20:18 +01:00
|
|
|
})
|
|
|
|
|
2015-10-26 00:11:37 +01:00
|
|
|
return 0;
|
2015-10-25 18:10:06 +01:00
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2018-11-29 09:46:29 +01: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);
|
2018-11-29 09:46:29 +01:00
|
|
|
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);
|
2018-11-29 09:46:29 +01:00
|
|
|
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);
|
2018-11-29 09:46:29 +01:00
|
|
|
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);
|
2018-11-29 09:46:29 +01:00
|
|
|
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;
|
2018-11-29 09:46:29 +01:00
|
|
|
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;
|
2018-11-29 09:46:29 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-11-19 11:20:18 +01:00
|
|
|
|
2020-02-01 18:16:42 +01:00
|
|
|
int add_genfscon(const char *name, const char *path, const char *context) {
|
|
|
|
// First try to create context
|
|
|
|
context_struct_t *ctx;
|
|
|
|
if (context_from_string(NULL, mpdb, &ctx, context, strlen(context))) {
|
|
|
|
LOGW("Failed to create context from string [%s]\n", context);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate genfs context
|
|
|
|
ocontext_t *newc = xcalloc(sizeof(*newc), 1);
|
|
|
|
newc->u.name = strdup(path);
|
|
|
|
memcpy(&newc->context[0], ctx, sizeof(*ctx));
|
|
|
|
free(ctx);
|
|
|
|
|
|
|
|
// Find or allocate genfs
|
|
|
|
genfs_t *last_gen = NULL;
|
|
|
|
genfs_t *newfs = NULL;
|
|
|
|
for (genfs_t *node = mpdb->genfs; node; node = node->next) {
|
|
|
|
if (strcmp(node->fstype, name) == 0) {
|
|
|
|
newfs = node;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
last_gen = node;
|
|
|
|
}
|
|
|
|
if (newfs == NULL) {
|
|
|
|
newfs = xcalloc(sizeof(*newfs), 1);
|
|
|
|
newfs->fstype = strdup(name);
|
|
|
|
// Insert
|
|
|
|
if (last_gen)
|
|
|
|
last_gen->next = newfs;
|
|
|
|
else
|
|
|
|
mpdb->genfs = newfs;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Insert or replace genfs context
|
|
|
|
ocontext_t *last_ctx = NULL;
|
|
|
|
for (ocontext_t *node = newfs->head; node; node = node->next) {
|
|
|
|
if (strcmp(node->u.name, path) == 0) {
|
|
|
|
// Unlink
|
|
|
|
if (last_ctx)
|
|
|
|
last_ctx->next = node->next;
|
|
|
|
else
|
|
|
|
newfs->head = NULL;
|
|
|
|
// Destroy old node
|
|
|
|
free(node->u.name);
|
|
|
|
context_destroy(&node->context[0]);
|
|
|
|
free(node);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
last_ctx = node;
|
|
|
|
}
|
|
|
|
// Insert
|
|
|
|
if (last_ctx)
|
|
|
|
last_ctx->next = newc;
|
|
|
|
else
|
|
|
|
newfs->head = newc;
|
|
|
|
|
|
|
|
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);
|
|
|
|
})
|
|
|
|
}
|