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

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