Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
libsepol/cil: Check for duplicate blocks, optionals, and macros
In CIL, blocks, optionals, and macros share the same symbol table so
that the targets of "in" statements can be located. Because of this,
they cannot have the same name in the same namespace, but, because
they do not show up in the final policy, they can have the same name
as long as they are in different namespaces. Unfortunately, when
copying from one namespace to another, no check was being done to see
if there was a conflict.

When copying blocks, optionals, and macros, if a datum is found in
the destination namespace, then there is a conflict with a previously
declared block, optional, or macro, so exit with an error.

Reported-by: Nicolas Iooss <nicolas.iooss@m4x.org>
Reported-by: Evgeny Vereshchagin <evvers@ya.ru>
Signed-off-by: James Carter <jwcart2@gmail.com>
  • Loading branch information
jwcart2 committed Mar 18, 2021
1 parent b839e9a commit d155b41
Showing 1 changed file with 25 additions and 64 deletions.
89 changes: 25 additions & 64 deletions libsepol/cil/src/cil_copy_ast.c
Expand Up @@ -100,16 +100,17 @@ int cil_copy_block(__attribute__((unused)) struct cil_db *db, void *data, void *
struct cil_block *orig = data;
char *key = orig->datum.name;
struct cil_symtab_datum *datum = NULL;
struct cil_block *new;

cil_symtab_get_datum(symtab, key, &datum);
if (datum == NULL) {
struct cil_block *new;
cil_block_init(&new);
*copy = new;
} else {
*copy = datum;;
if (datum != NULL) {
cil_tree_log(NODE(datum), CIL_ERR, "Re-declaration of %s %s", cil_node_to_string(NODE(datum)), key);
return SEPOL_ERR;
}

cil_block_init(&new);
*copy = new;

return SEPOL_OK;
}

Expand Down Expand Up @@ -1509,81 +1510,40 @@ int cil_copy_macro(__attribute__((unused)) struct cil_db *db, void *data, void *
struct cil_macro *orig = data;
char *key = orig->datum.name;
struct cil_symtab_datum *datum = NULL;
struct cil_macro *new;

cil_symtab_get_datum(symtab, key, &datum);
if (datum == NULL) {
struct cil_macro *new;
cil_macro_init(&new);
if (orig->params != NULL) {
cil_copy_list(orig->params, &new->params);
}

*copy = new;

} else {
struct cil_list_item *curr_orig = NULL;
struct cil_list_item *curr_new = NULL;
struct cil_param *param_orig = NULL;
struct cil_param *param_new = NULL;

if (((struct cil_macro*)datum)->params != NULL) {
curr_new = ((struct cil_macro*)datum)->params->head;
}

if (orig->params != NULL) {
curr_orig = orig->params->head;
}

if (curr_orig != NULL && curr_new != NULL) {
while (curr_orig != NULL) {
if (curr_new == NULL) {
goto exit;
}

param_orig = (struct cil_param*)curr_orig->data;
param_new = (struct cil_param*)curr_new->data;
if (param_orig->str != param_new->str) {
goto exit;
} else if (param_orig->flavor != param_new->flavor) {
goto exit;
}

curr_orig = curr_orig->next;
curr_new = curr_new->next;
}

if (curr_new != NULL) {
goto exit;
}
} else if (!(curr_orig == NULL && curr_new == NULL)) {
goto exit;
}
if (datum != NULL) {
cil_tree_log(NODE(datum), CIL_ERR, "Re-declaration of %s %s", cil_node_to_string(NODE(datum)), key);
return SEPOL_ERR;
}

*copy = datum;
cil_macro_init(&new);
if (orig->params != NULL) {
cil_copy_list(orig->params, &new->params);
}

return SEPOL_OK;
*copy = new;

exit:
cil_log(CIL_INFO, "cil_copy_macro: macro cannot be redefined\n");
return SEPOL_ERR;
return SEPOL_OK;
}

int cil_copy_optional(__attribute__((unused)) struct cil_db *db, void *data, void **copy, symtab_t *symtab)
{
struct cil_optional *orig = data;
char *key = orig->datum.name;
struct cil_symtab_datum *datum = NULL;
struct cil_optional *new;

cil_symtab_get_datum(symtab, key, &datum);
if (datum == NULL) {
struct cil_optional *new;
cil_optional_init(&new);
*copy = new;
} else {
*copy = datum;
if (datum != NULL) {
cil_tree_log(NODE(datum), CIL_ERR, "Re-declaration of %s %s", cil_node_to_string(NODE(datum)), key);
return SEPOL_ERR;
}

cil_optional_init(&new);
*copy = new;

return SEPOL_OK;
}

Expand Down Expand Up @@ -2122,6 +2082,7 @@ int __cil_copy_node_helper(struct cil_tree_node *orig, __attribute__((unused)) u
args->dest = new;
}
} else {
cil_tree_log(orig, CIL_ERR, "Problem copying %s node", cil_node_to_string(orig));
goto exit;
}

Expand Down

0 comments on commit d155b41

Please sign in to comment.