Blame SOURCES/0007-libsepol-cil-Exit-with-an-error-if-declaration-name-.patch

060220
From 5edd2126ad3dc30f75f0ec9f73cd609bbe432c29 Mon Sep 17 00:00:00 2001
060220
From: James Carter <jwcart2@gmail.com>
060220
Date: Thu, 8 Apr 2021 13:32:12 -0400
060220
Subject: [PATCH] libsepol/cil: Exit with an error if declaration name is a
060220
 reserved word
060220
060220
When CIL parses sets or conditional expressions, any identifier that
060220
matches an operator name will always be taken as an operator. If a
060220
declaration has the same name as an operator, then there is the
060220
possibility of causing either confusion or a syntax error if it is
060220
used in an expression. The potential for problems is much greater
060220
than any possible advantage in allowing a declaration to share the
060220
name of a reserved word.
060220
060220
Create a new function, __cil_is_reserved_name() that is called when
060220
an identifier is declared and its name is being validated. In this
060220
function, check if the declaration has the same name as a reserved
060220
word for an expression operator that can be used with the identifer's
060220
flavor and exit with an error if it does.
060220
060220
Also, move the check for types, type aliases, and type attributes
060220
matching the reserved word "self" to this new function.
060220
060220
Finally, change the name of the function __cil_verify_name() to
060220
cil_verify_name(), since this function is neither static nor a
060220
helper function.
060220
060220
Signed-off-by: James Carter <jwcart2@gmail.com>
060220
---
060220
 libsepol/cil/src/cil_build_ast.c | 28 ++---------------
060220
 libsepol/cil/src/cil_verify.c    | 52 +++++++++++++++++++++++++++++++-
060220
 libsepol/cil/src/cil_verify.h    |  2 +-
060220
 3 files changed, 54 insertions(+), 28 deletions(-)
060220
060220
diff --git a/libsepol/cil/src/cil_build_ast.c b/libsepol/cil/src/cil_build_ast.c
060220
index b90b0f60..fe7b7777 100644
060220
--- a/libsepol/cil/src/cil_build_ast.c
060220
+++ b/libsepol/cil/src/cil_build_ast.c
060220
@@ -110,7 +110,7 @@ int cil_gen_node(struct cil_db *db, struct cil_tree_node *ast_node, struct cil_s
060220
 	symtab_t *symtab = NULL;
060220
 	struct cil_symtab_datum *prev;
060220
 
060220
-	rc = __cil_verify_name((const char*)key);
060220
+	rc = cil_verify_name((const char*)key, nflavor);
060220
 	if (rc != SEPOL_OK) {
060220
 		goto exit;
060220
 	}
060220
@@ -1919,12 +1919,6 @@ int cil_gen_roleattribute(struct cil_db *db, struct cil_tree_node *parse_current
060220
 		goto exit;
060220
 	}
060220
 
060220
-	if (parse_current->next->data == CIL_KEY_SELF) {
060220
-		cil_log(CIL_ERR, "The keyword '%s' is reserved\n", CIL_KEY_SELF);
060220
-		rc = SEPOL_ERR;
060220
-		goto exit;
060220
-	}
060220
-
060220
 	cil_roleattribute_init(&attr);
060220
 
060220
 	key = parse_current->next->data;
060220
@@ -2303,12 +2297,6 @@ int cil_gen_type(struct cil_db *db, struct cil_tree_node *parse_current, struct
060220
 		goto exit;
060220
 	}
060220
 
060220
-	if (parse_current->next->data == CIL_KEY_SELF) {
060220
-		cil_log(CIL_ERR, "The keyword '%s' is reserved\n", CIL_KEY_SELF);
060220
-		rc = SEPOL_ERR;
060220
-		goto exit;
060220
-	}
060220
-
060220
 	cil_type_init(&type);
060220
 
060220
 	key = parse_current->next->data;
060220
@@ -2357,12 +2345,6 @@ int cil_gen_typeattribute(struct cil_db *db, struct cil_tree_node *parse_current
060220
 		goto exit;
060220
 	}
060220
 
060220
-	if (parse_current->next->data == CIL_KEY_SELF) {
060220
-		cil_log(CIL_ERR, "The keyword '%s' is reserved\n", CIL_KEY_SELF);
060220
-		rc = SEPOL_ERR;
060220
-		goto exit;
060220
-	}
060220
-
060220
 	cil_typeattribute_init(&attr);
060220
 
060220
 	key = parse_current->next->data;
060220
@@ -3064,12 +3046,6 @@ int cil_gen_alias(struct cil_db *db, struct cil_tree_node *parse_current, struct
060220
 		goto exit;
060220
 	}
060220
 
060220
-	if (flavor == CIL_TYPEALIAS && parse_current->next->data == CIL_KEY_SELF) {
060220
-		cil_log(CIL_ERR, "The keyword '%s' is reserved\n", CIL_KEY_SELF);
060220
-		rc = SEPOL_ERR;
060220
-		goto exit;
060220
-	}
060220
-
060220
 	cil_alias_init(&alias);
060220
 
060220
 	key = parse_current->next->data;
060220
@@ -5294,7 +5270,7 @@ int cil_gen_macro(struct cil_db *db, struct cil_tree_node *parse_current, struct
060220
 
060220
 		param->str =  current_item->cl_head->next->data;
060220
 
060220
-		rc = __cil_verify_name(param->str);
060220
+		rc = cil_verify_name(param->str, param->flavor);
060220
 		if (rc != SEPOL_OK) {
060220
 			cil_destroy_param(param);
060220
 			goto exit;
060220
diff --git a/libsepol/cil/src/cil_verify.c b/libsepol/cil/src/cil_verify.c
060220
index 3972b1e9..ea95c2cb 100644
060220
--- a/libsepol/cil/src/cil_verify.c
060220
+++ b/libsepol/cil/src/cil_verify.c
060220
@@ -47,7 +47,51 @@
060220
 
060220
 #include "cil_verify.h"
060220
 
060220
-int __cil_verify_name(const char *name)
060220
+static int __cil_is_reserved_name(const char *name, enum cil_flavor flavor)
060220
+{
060220
+	switch (flavor) {
060220
+	case CIL_BOOL:
060220
+	case CIL_TUNABLE:
060220
+		if ((name == CIL_KEY_EQ) || (name == CIL_KEY_NEQ))
060220
+			return CIL_TRUE;
060220
+		break;
060220
+	case CIL_PERM:
060220
+	case CIL_MAP_PERM:
060220
+	case CIL_USER:
060220
+	case CIL_USERATTRIBUTE:
060220
+	case CIL_ROLE:
060220
+	case CIL_ROLEATTRIBUTE:
060220
+		if (name == CIL_KEY_ALL)
060220
+			return CIL_TRUE;
060220
+		break;
060220
+	case CIL_TYPE:
060220
+	case CIL_TYPEATTRIBUTE:
060220
+	case CIL_TYPEALIAS:
060220
+		if ((name == CIL_KEY_ALL) || (name == CIL_KEY_SELF))
060220
+			return CIL_TRUE;
060220
+		break;
060220
+	case CIL_CAT:
060220
+	case CIL_CATSET:
060220
+	case CIL_CATALIAS:
060220
+	case CIL_PERMISSIONX:
060220
+		if ((name == CIL_KEY_ALL) || (name == CIL_KEY_RANGE))
060220
+			return CIL_TRUE;
060220
+		break;
060220
+	default:
060220
+		/* All of these are not used in expressions */
060220
+		return CIL_FALSE;
060220
+		break;
060220
+	}
060220
+
060220
+	/* Everything not under the default case is also checked for these */
060220
+	if ((name == CIL_KEY_AND) || (name == CIL_KEY_OR) || (name == CIL_KEY_NOT) || (name == CIL_KEY_XOR)) {
060220
+		return CIL_TRUE;
060220
+	}
060220
+
060220
+	return CIL_FALSE;
060220
+}
060220
+
060220
+int cil_verify_name(const char *name, enum cil_flavor flavor)
060220
 {
060220
 	int rc = SEPOL_ERR;
060220
 	int len;
060220
@@ -77,6 +121,12 @@ int __cil_verify_name(const char *name)
060220
 			goto exit;
060220
 		}
060220
 	}
060220
+
060220
+	if (__cil_is_reserved_name(name, flavor)) {
060220
+		cil_log(CIL_ERR, "Name %s is a reserved word\n", name);
060220
+		goto exit;
060220
+	}
060220
+
060220
 	return SEPOL_OK;
060220
 
060220
 exit:
060220
diff --git a/libsepol/cil/src/cil_verify.h b/libsepol/cil/src/cil_verify.h
060220
index bda1565f..e4b98919 100644
060220
--- a/libsepol/cil/src/cil_verify.h
060220
+++ b/libsepol/cil/src/cil_verify.h
060220
@@ -56,7 +56,7 @@ struct cil_args_verify {
060220
 	int *pass;
060220
 };
060220
 
060220
-int __cil_verify_name(const char *name);
060220
+int cil_verify_name(const char *name, enum cil_flavor flavor);
060220
 int __cil_verify_syntax(struct cil_tree_node *parse_current, enum cil_syntax s[], int len);
060220
 int cil_verify_expr_syntax(struct cil_tree_node *current, enum cil_flavor op, enum cil_flavor expr_flavor);
060220
 int cil_verify_constraint_leaf_expr_syntax(enum cil_flavor l_flavor, enum cil_flavor r_flavor, enum cil_flavor op, enum cil_flavor expr_flavor);
060220
-- 
060220
2.30.2
060220