Several improvments and fixes

This commit is contained in:
topjohnwu 2017-04-20 02:15:10 +08:00
parent ca79e58ab9
commit 272eb37e9a

View File

@ -4,7 +4,7 @@
policydb_t *policydb = NULL; policydb_t *policydb = NULL;
static void *cmalloc(size_t s) { static void *cmalloc(size_t s) {
void *t = malloc(s); void *t = calloc(s, 1);
if (t == NULL) { if (t == NULL) {
fprintf(stderr, "Out of memory\n"); fprintf(stderr, "Out of memory\n");
exit(1); exit(1);
@ -21,7 +21,6 @@ static int get_attr(char *type, int value) {
return 1; return 1;
return !! ebitmap_get_bit(&policydb->attr_type_map[attr->s.value-1], value-1); 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);
} }
static int get_attr_id(char *type) { static int get_attr_id(char *type) {
@ -52,30 +51,41 @@ static int set_attr(char *type, int value) {
} }
static int add_irule(int s, int t, int c, int p, int effect, int not) { static int add_irule(int s, int t, int c, int p, int effect, int not) {
avtab_datum_t *av;
avtab_key_t key; avtab_key_t key;
avtab_datum_t *av;
int new_rule = 0;
key.source_type = s; key.source_type = s;
key.target_type = t; key.target_type = t;
key.target_class = c; key.target_class = c;
key.specified = effect; key.specified = effect;
av = avtab_search(&policydb->te_avtab, &key);
av = avtab_search(&policydb->te_avtab, &key);
if (av == NULL) { if (av == NULL) {
av = cmalloc(sizeof(*av)); av = cmalloc(sizeof(*av));
memset(av, 0, sizeof(*av)); new_rule = 1;
av->data |= 1U << (p - 1); }
int ret = avtab_insert(&policydb->te_avtab, &key, av);
if (ret) { if(not) {
if (p < 0)
av->data = 0U;
else
av->data &= ~(1U << (p - 1));
} else {
if (p < 0)
av->data = ~0U;
else
av->data |= 1U << (p - 1);
}
if (new_rule) {
if (avtab_insert(&policydb->te_avtab, &key, av)) {
fprintf(stderr, "Error inserting into avtab\n"); fprintf(stderr, "Error inserting into avtab\n");
return 1; return 1;
} }
free(av);
} }
if(not)
av->data &= ~(1U << (p - 1));
else
av->data |= 1U << (p - 1);
return 0; return 0;
} }
@ -86,56 +96,20 @@ static int add_rule_auto(type_datum_t *src, type_datum_t *tgt, class_datum_t *cl
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;
if(add_rule_auto(src, tgt, cls, perm, effect, not)) ret |= add_rule_auto(src, tgt, cls, perm, effect, not);
return 1;
} }
} 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;
if(add_rule_auto(src, tgt, cls, perm, effect, not)) ret |= add_rule_auto(src, tgt, cls, perm, effect, not);
return 1;
} }
} 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;
if(add_rule_auto(src, tgt, cls, perm, effect, not)) ret |= add_irule(src->s.value, tgt->s.value, cls->s.value, -1, effect, not);
return 1;
}
} else if (perm == NULL) {
hashtab_for_each(cls->permissions.table, &cur) {
perm = cur->datum;
if(add_rule_auto(src, tgt, cls, perm, effect, not))
return 1;
}
if (cls->comdatum != NULL) {
hashtab_for_each(cls->comdatum->permissions.table, &cur) {
perm = cur->datum;
if(add_rule_auto(src, tgt, cls, perm, effect, not))
return 1;
}
} }
} else { } else {
ebitmap_node_t *s_node, *t_node; return add_irule(src->s.value, tgt->s.value, cls->s.value, perm ? perm->s.value : -1, effect, not);
int i, j;
if (src->flavor == TYPE_ATTRIB) {
if (tgt->flavor == TYPE_ATTRIB) {
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)
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 {
ebitmap_for_each_bit(&policydb->attr_type_map[src->s.value-1], s_node, i)
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) {
ebitmap_for_each_bit(&policydb->attr_type_map[tgt->s.value-1], t_node, j)
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);
} }
return ret; return ret;
} }
@ -225,24 +199,26 @@ void destroy_policydb() {
int create_domain(char *d) { int create_domain(char *d) {
symtab_datum_t *src = hashtab_search(policydb->p_types.table, d); symtab_datum_t *src = hashtab_search(policydb->p_types.table, d);
if(src) if(src) {
fprintf(stderr, "Domain %s already exists\n", d);
return 1; return 1;
}
type_datum_t *typdatum = (type_datum_t *) malloc(sizeof(type_datum_t)); type_datum_t *typedatum = (type_datum_t *) malloc(sizeof(type_datum_t));
type_datum_init(typdatum); type_datum_init(typedatum);
typdatum->primary = 1; typedatum->primary = 1;
typdatum->flavor = TYPE_TYPE; typedatum->flavor = TYPE_TYPE;
uint32_t value = 0; uint32_t value = 0;
int r = symtab_insert(policydb, SYM_TYPES, strdup(d), typdatum, SCOPE_DECL, 1, &value); int r = symtab_insert(policydb, SYM_TYPES, strdup(d), typedatum, SCOPE_DECL, 1, &value);
typdatum->s.value = value; typedatum->s.value = value;
if (ebitmap_set_bit(&policydb->global->branch_list->declared.scope[SYM_TYPES], value - 1, 1)) { if (ebitmap_set_bit(&policydb->global->branch_list->declared.scope[SYM_TYPES], value - 1, 1)) {
return 1; return 1;
} }
policydb->type_attr_map = realloc(policydb->type_attr_map, sizeof(ebitmap_t)*policydb->p_types.nprim); 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); 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->type_attr_map[value-1]);
ebitmap_init(&policydb->attr_type_map[value-1]); ebitmap_init(&policydb->attr_type_map[value-1]);
ebitmap_set_bit(&policydb->type_attr_map[value-1], value-1, 1); ebitmap_set_bit(&policydb->type_attr_map[value-1], value-1, 1);
@ -301,8 +277,9 @@ int add_transition(char *s, char *t, char *c, char *d) {
type_datum_t *src, *tgt, *def; type_datum_t *src, *tgt, *def;
class_datum_t *cls; class_datum_t *cls;
avtab_datum_t *av;
avtab_key_t key; avtab_key_t key;
avtab_datum_t *av;
int new_rule = 0;
src = hashtab_search(policydb->p_types.table, s); src = hashtab_search(policydb->p_types.table, s);
if (src == NULL) { if (src == NULL) {
@ -333,17 +310,18 @@ int add_transition(char *s, char *t, char *c, char *d) {
if (av == NULL) { if (av == NULL) {
av = cmalloc(sizeof(*av)); av = cmalloc(sizeof(*av));
av->data = def->s.value; new_rule = 1;
int ret = avtab_insert(&policydb->te_avtab, &key, av); }
if (ret) {
av->data = def->s.value;
if (new_rule) {
if (avtab_insert(&policydb->te_avtab, &key, av)) {
fprintf(stderr, "Error inserting into avtab\n"); fprintf(stderr, "Error inserting into avtab\n");
return 1; return 1;
} }
} else { free(av);
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; return 0;
} }
@ -372,17 +350,22 @@ int add_file_transition(char *s, char *t, char *c, char *d, char* filename) {
return 1; return 1;
} }
filename_trans_t *new_trans_key = cmalloc(sizeof(*new_trans_key)); filename_trans_t trans_key;
new_trans_key->stype = src->s.value; trans_key.stype = src->s.value;
new_trans_key->ttype = tgt->s.value; trans_key.ttype = tgt->s.value;
new_trans_key->tclass = cls->s.value; trans_key.tclass = cls->s.value;
new_trans_key->name = strdup(filename); trans_key.name = filename;
filename_trans_datum_t *new_trans_datam = cmalloc(sizeof(*new_trans_datam)); filename_trans_datum_t *trans_datum;
new_trans_datam->otype = def->s.value; trans_datum = hashtab_search(policydb->p_types.table, (hashtab_key_t) &trans_key);
hashtab_insert(policydb->filename_trans, (hashtab_key_t) new_trans_key, new_trans_datam); if (trans_datum == NULL) {
trans_datum = cmalloc(sizeof(*trans_datum));
hashtab_insert(policydb->filename_trans, (hashtab_key_t) &trans_key, trans_datum);
}
// Overwrite existing
trans_datum->otype = def->s.value;
return 0; return 0;
} }