Skip to content

Commit

Permalink
libsepol: Clean up scope handling
Browse files Browse the repository at this point in the history
Currently, when checking if an identifier is enabled, each scope in
the decl_ids list is checked. This means that if any block that
requires the identifier is enabled, then the identifier will be treated
as being declared.

Now, declarations will be kept at the end of the decl_ids list and
when checking if an identifier is enabled, only the last scope will
be checked (Except for roles and users which allow multiple declarations,
they will have to keep the old behavior.)

Signed-off-by: James Carter <[email protected]>
  • Loading branch information
jwcart2 committed Jun 1, 2017
1 parent 800f6b2 commit c8a7edc
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 4 deletions.
23 changes: 19 additions & 4 deletions libsepol/src/avrule_block.c
Original file line number Diff line number Diff line change
Expand Up @@ -156,20 +156,35 @@ int is_id_enabled(char *id, policydb_t * p, int symbol_table)
{
scope_datum_t *scope =
(scope_datum_t *) hashtab_search(p->scope[symbol_table].table, id);
uint32_t i;
avrule_decl_t *decl;
uint32_t len = scope->decl_ids_len;

if (scope == NULL) {
return 0;
}
if (scope->scope != SCOPE_DECL) {
return 0;
}
for (i = 0; i < scope->decl_ids_len; i++) {
avrule_decl_t *decl =
p->decl_val_to_struct[scope->decl_ids[i] - 1];

if (len < 1) {
return 0;
}

if (symbol_table == SYM_ROLES || symbol_table == SYM_USERS) {
uint32_t i;
for (i = 0; i < len; i++) {
decl = p->decl_val_to_struct[scope->decl_ids[i] - 1];
if (decl != NULL && decl->enabled) {
return 1;
}
}
} else {
decl = p->decl_val_to_struct[scope->decl_ids[len-1] - 1];
if (decl != NULL && decl->enabled) {
return 1;
}
}

return 0;
}

Expand Down
13 changes: 13 additions & 0 deletions libsepol/src/policydb.c
Original file line number Diff line number Diff line change
Expand Up @@ -1720,6 +1720,19 @@ int symtab_insert(policydb_t * pol, uint32_t sym,
return -ENOMEM;
}

if (scope_datum->scope == SCOPE_DECL && scope == SCOPE_REQ) {
/* Need to keep the decl at the end of the list */
uint32_t len, tmp;
len = scope_datum->decl_ids_len;
if (len < 2) {
/* This should be impossible here */
return -1;
}
tmp = scope_datum->decl_ids[len-2];
scope_datum->decl_ids[len-2] = scope_datum->decl_ids[len-1];
scope_datum->decl_ids[len-1] = tmp;
}

return retval;
}

Expand Down

0 comments on commit c8a7edc

Please sign in to comment.