Blame SOURCES/0016-libsepol-cil-Check-for-duplicate-blocks-optionals-an.patch

71cd55
From d155b410d4bbc90d28f361b966f0429598da8188 Mon Sep 17 00:00:00 2001
71cd55
From: James Carter <jwcart2@gmail.com>
71cd55
Date: Tue, 16 Mar 2021 10:26:28 -0400
71cd55
Subject: [PATCH] libsepol/cil: Check for duplicate blocks, optionals, and
71cd55
 macros
71cd55
71cd55
In CIL, blocks, optionals, and macros share the same symbol table so
71cd55
that the targets of "in" statements can be located. Because of this,
71cd55
they cannot have the same name in the same namespace, but, because
71cd55
they do not show up in the final policy, they can have the same name
71cd55
as long as they are in different namespaces. Unfortunately, when
71cd55
copying from one namespace to another, no check was being done to see
71cd55
if there was a conflict.
71cd55
71cd55
When copying blocks, optionals, and macros, if a datum is found in
71cd55
the destination namespace, then there is a conflict with a previously
71cd55
declared block, optional, or macro, so exit with an error.
71cd55
71cd55
Reported-by: Nicolas Iooss <nicolas.iooss@m4x.org>
71cd55
Reported-by: Evgeny Vereshchagin <evvers@ya.ru>
71cd55
Signed-off-by: James Carter <jwcart2@gmail.com>
71cd55
---
71cd55
 libsepol/cil/src/cil_copy_ast.c | 89 +++++++++------------------------
71cd55
 1 file changed, 25 insertions(+), 64 deletions(-)
71cd55
71cd55
diff --git a/libsepol/cil/src/cil_copy_ast.c b/libsepol/cil/src/cil_copy_ast.c
71cd55
index c9aada9db348..ed96786115d3 100644
71cd55
--- a/libsepol/cil/src/cil_copy_ast.c
71cd55
+++ b/libsepol/cil/src/cil_copy_ast.c
71cd55
@@ -100,16 +100,17 @@ int cil_copy_block(__attribute__((unused)) struct cil_db *db, void *data, void *
71cd55
 	struct cil_block *orig = data;
71cd55
 	char *key = orig->datum.name;
71cd55
 	struct cil_symtab_datum *datum = NULL;
71cd55
+	struct cil_block *new;
71cd55
 
71cd55
 	cil_symtab_get_datum(symtab, key, &datum);
71cd55
-	if (datum == NULL) {
71cd55
-		struct cil_block *new;
71cd55
-		cil_block_init(&new;;
71cd55
-		*copy = new;
71cd55
-	} else {
71cd55
-		*copy = datum;;
71cd55
+	if (datum != NULL) {
71cd55
+		cil_tree_log(NODE(datum), CIL_ERR, "Re-declaration of %s %s", cil_node_to_string(NODE(datum)), key);
71cd55
+		return SEPOL_ERR;
71cd55
 	}
71cd55
 
71cd55
+	cil_block_init(&new;;
71cd55
+	*copy = new;
71cd55
+
71cd55
 	return SEPOL_OK;
71cd55
 }
71cd55
 
71cd55
@@ -1509,64 +1510,22 @@ int cil_copy_macro(__attribute__((unused)) struct cil_db *db, void *data, void *
71cd55
 	struct cil_macro *orig = data;
71cd55
 	char *key = orig->datum.name;
71cd55
 	struct cil_symtab_datum *datum = NULL;
71cd55
+	struct cil_macro *new;
71cd55
 
71cd55
 	cil_symtab_get_datum(symtab, key, &datum);
71cd55
-	if (datum == NULL) {
71cd55
-		struct cil_macro *new;
71cd55
-		cil_macro_init(&new;;
71cd55
-		if (orig->params != NULL) {
71cd55
-			cil_copy_list(orig->params, &new->params);
71cd55
-		}
71cd55
-
71cd55
-		*copy = new;
71cd55
-
71cd55
-	} else {
71cd55
-		struct cil_list_item *curr_orig = NULL;
71cd55
-		struct cil_list_item *curr_new = NULL;
71cd55
-		struct cil_param *param_orig = NULL;
71cd55
-		struct cil_param *param_new = NULL;
71cd55
-
71cd55
-		if (((struct cil_macro*)datum)->params != NULL) {
71cd55
-			curr_new = ((struct cil_macro*)datum)->params->head;
71cd55
-		}
71cd55
-
71cd55
-		if (orig->params != NULL) {
71cd55
-			curr_orig = orig->params->head;
71cd55
-		}
71cd55
-
71cd55
-		if (curr_orig != NULL && curr_new != NULL) {
71cd55
-			while (curr_orig != NULL) {
71cd55
-				if (curr_new == NULL) {
71cd55
-					goto exit;
71cd55
-				}
71cd55
-
71cd55
-				param_orig = (struct cil_param*)curr_orig->data;
71cd55
-				param_new = (struct cil_param*)curr_new->data;
71cd55
-				if (param_orig->str != param_new->str) {
71cd55
-					goto exit;
71cd55
-				} else if (param_orig->flavor != param_new->flavor) {
71cd55
-					goto exit;
71cd55
-				}
71cd55
-
71cd55
-				curr_orig = curr_orig->next;
71cd55
-				curr_new = curr_new->next;
71cd55
-			}
71cd55
-
71cd55
-			if (curr_new != NULL) {
71cd55
-				goto exit;
71cd55
-			}
71cd55
-		} else if (!(curr_orig == NULL && curr_new == NULL)) {
71cd55
-			goto exit;
71cd55
-		}
71cd55
+	if (datum != NULL) {
71cd55
+		cil_tree_log(NODE(datum), CIL_ERR, "Re-declaration of %s %s", cil_node_to_string(NODE(datum)), key);
71cd55
+		return SEPOL_ERR;
71cd55
+	}
71cd55
 
71cd55
-		*copy = datum;
71cd55
+	cil_macro_init(&new;;
71cd55
+	if (orig->params != NULL) {
71cd55
+		cil_copy_list(orig->params, &new->params);
71cd55
 	}
71cd55
 
71cd55
-	return SEPOL_OK;
71cd55
+	*copy = new;
71cd55
 
71cd55
-exit:
71cd55
-	cil_log(CIL_INFO, "cil_copy_macro: macro cannot be redefined\n");
71cd55
-	return SEPOL_ERR;
71cd55
+	return SEPOL_OK;
71cd55
 }
71cd55
 
71cd55
 int cil_copy_optional(__attribute__((unused)) struct cil_db *db, void *data, void **copy, symtab_t *symtab)
71cd55
@@ -1574,16 +1533,17 @@ int cil_copy_optional(__attribute__((unused)) struct cil_db *db, void *data, voi
71cd55
 	struct cil_optional *orig = data;
71cd55
 	char *key = orig->datum.name;
71cd55
 	struct cil_symtab_datum *datum = NULL;
71cd55
+	struct cil_optional *new;
71cd55
 
71cd55
 	cil_symtab_get_datum(symtab, key, &datum);
71cd55
-	if (datum == NULL) {
71cd55
-		struct cil_optional *new;
71cd55
-		cil_optional_init(&new;;
71cd55
-		*copy = new;
71cd55
-	} else {
71cd55
-		*copy = datum;
71cd55
+	if (datum != NULL) {
71cd55
+		cil_tree_log(NODE(datum), CIL_ERR, "Re-declaration of %s %s", cil_node_to_string(NODE(datum)), key);
71cd55
+		return SEPOL_ERR;
71cd55
 	}
71cd55
 
71cd55
+	cil_optional_init(&new;;
71cd55
+	*copy = new;
71cd55
+
71cd55
 	return SEPOL_OK;
71cd55
 }
71cd55
 
71cd55
@@ -2122,6 +2082,7 @@ int __cil_copy_node_helper(struct cil_tree_node *orig, __attribute__((unused)) u
71cd55
 			args->dest = new;
71cd55
 		}
71cd55
 	} else {
71cd55
+		cil_tree_log(orig, CIL_ERR, "Problem copying %s node", cil_node_to_string(orig));
71cd55
 		goto exit;
71cd55
 	}
71cd55
 
71cd55
-- 
71cd55
2.32.0
71cd55