Use magic macros

This commit is contained in:
topjohnwu 2018-12-01 03:53:58 -05:00
parent 9b170f2b4f
commit 2a2e1236fc
3 changed files with 34 additions and 35 deletions

View File

@ -171,10 +171,11 @@ void sepol_magisk_rules() {
sepol_allow("update_engine", "adb_data_file", "dir", ALL); sepol_allow("update_engine", "adb_data_file", "dir", ALL);
// Remove all dontaudit // Remove all dontaudit
for_each_avtab_node([](auto p) -> void { avtab_ptr_t av;
if (p->key.specified == AVTAB_AUDITDENY || p->key.specified == AVTAB_XPERMS_DONTAUDIT) avtab_for_each(&policydb->te_avtab, av, {
avtab_remove_node(&policydb->te_avtab, p); if (av->key.specified == AVTAB_AUDITDENY || av->key.specified == AVTAB_XPERMS_DONTAUDIT)
}); avtab_remove_node(&policydb->te_avtab, av);
})
log_cb.w = bak; log_cb.w = bak;
} }

View File

@ -138,20 +138,20 @@ static int add_rule_auto(type_datum_t *src, type_datum_t *tgt, class_datum_t *cl
int ret = 0; int ret = 0;
if (src == NULL) { if (src == NULL) {
hashtab_for_each(policydb->p_types.table, &cur) { hashtab_for_each(policydb->p_types.table, cur, {
src = cur->datum; src = cur->datum;
ret |= add_rule_auto(src, tgt, cls, perm, effect, not); ret |= add_rule_auto(src, tgt, cls, perm, effect, not);
} })
} else if (tgt == NULL) { } else if (tgt == NULL) {
hashtab_for_each(policydb->p_types.table, &cur) { hashtab_for_each(policydb->p_types.table, cur, {
tgt = cur->datum; tgt = cur->datum;
ret |= add_rule_auto(src, tgt, cls, perm, effect, not); ret |= add_rule_auto(src, tgt, cls, perm, effect, not);
} })
} else if (cls == NULL) { } else if (cls == NULL) {
hashtab_for_each(policydb->p_classes.table, &cur) { hashtab_for_each(policydb->p_classes.table, cur, {
cls = cur->datum; cls = cur->datum;
ret |= add_rule_auto(src, tgt, cls, perm, effect, not); ret |= add_rule_auto(src, tgt, cls, perm, effect, not);
} })
} else { } else {
key.source_type = src->s.value; key.source_type = src->s.value;
key.target_type = tgt->s.value; key.target_type = tgt->s.value;
@ -210,20 +210,20 @@ static int add_xperm_rule_auto(type_datum_t *src, type_datum_t *tgt, class_datum
int ret = 0; int ret = 0;
if (src == NULL) { if (src == NULL) {
hashtab_for_each(policydb->p_types.table, &cur) { hashtab_for_each(policydb->p_types.table, cur, {
src = cur->datum; src = cur->datum;
ret |= add_xperm_rule_auto(src, tgt, cls, low, high, effect, not); ret |= add_xperm_rule_auto(src, tgt, cls, low, high, effect, not);
} })
} else if (tgt == NULL) { } else if (tgt == NULL) {
hashtab_for_each(policydb->p_types.table, &cur) { hashtab_for_each(policydb->p_types.table, cur, {
tgt = cur->datum; tgt = cur->datum;
ret |= add_xperm_rule_auto(src, tgt, cls, low, high, effect, not); ret |= add_xperm_rule_auto(src, tgt, cls, low, high, effect, not);
} })
} else if (cls == NULL) { } else if (cls == NULL) {
hashtab_for_each(policydb->p_classes.table, &cur) { hashtab_for_each(policydb->p_classes.table, cur, {
cls = cur->datum; cls = cur->datum;
ret |= add_xperm_rule_auto(src, tgt, cls, low, high, effect, not); ret |= add_xperm_rule_auto(src, tgt, cls, low, high, effect, not);
} })
} else { } else {
key.source_type = src->s.value; key.source_type = src->s.value;
key.target_type = tgt->s.value; key.target_type = tgt->s.value;
@ -407,28 +407,17 @@ int create_domain(const char *d) {
return set_attr("domain", value); return set_attr("domain", value);
} }
void for_each_avtab_node(void (*callback)(avtab_ptr_t)) {
avtab_ptr_t cur, next;
for (int i = 0; i < policydb->te_avtab.nslot; ++i) {
for (cur = policydb->te_avtab.htable[i]; cur; cur = next) {
// cur could be removed after callback
next = cur->next;
callback(cur);
}
}
}
int set_domain_state(const char *s, int state) { int set_domain_state(const char *s, int state) {
type_datum_t *type; type_datum_t *type;
hashtab_ptr_t cur; hashtab_ptr_t cur;
if (s == NULL) { if (s == NULL) {
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)) {
LOGW("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) {

View File

@ -13,13 +13,22 @@ extern "C" {
// Global policydb // Global policydb
extern policydb_t *policydb; extern policydb_t *policydb;
// hashtab traversal macro // General hash table traversal
#define hashtab_for_each(table, ptr) \ #define hash_for_each(table, slots, tab, cur, block) \
for (int _i = 0; _i < table->size; ++_i) \ for (int __i = 0; __i < (tab)->slots; ++__i) { \
for (*ptr = table->htable[_i]; *ptr != NULL; *ptr = (*ptr)->next) __typeof__(cur) __next; \
for (cur = (tab)->table[__i]; cur; cur = __next) { \
__next = cur->next; \
block \
} \
} \
// hashtab traversal
#define hashtab_for_each(hashtab, cur, block) hash_for_each(htable, size, hashtab, cur, block)
// avtab traversal
#define avtab_for_each(avtab, cur, block) hash_for_each(htable, nslot, avtab, cur, block)
// sepolicy manipulation functions
void for_each_avtab_node(void (*callback)(avtab_ptr_t));
int create_domain(const char *d); int create_domain(const char *d);
int set_domain_state(const char *s, int state); int set_domain_state(const char *s, int state);
int add_typeattribute(const char *domainS, const char *attr); int add_typeattribute(const char *domainS, const char *attr);