Skip to contentSkip to conversation

libsepol/cil: Line mark cleanup and fix

PATCH In review James Carter 2 participants 12 messages Aug 16, 2021 – Aug 19, 2021 SELinux security module

lore
Share & tools
lore
#0 lore raw

v2 cover libsepol/cil: Line mark cleanup and fix

Patches 1-5 cleanup minor issues with CIL's line marks.

Patches 6-7 fix the way line mark works so that the correct line numbers will be given for nested line marks.

Patch 8 writes out line mark information when writing out the AST.

Changes in v2
In patch 6, use cil_strpool_add("1") instead of "1"
In patch 7, fix confusing usage of pre_hll_expand and pre_hll_offset; initialize pre_hll_offset, change format specifiers in cil_tree.c to use %u instead of %d.

James Carter (7):
libsepol/cil: Check syntax of src_info statement libsepol/cil: Check the token type after getting the next token libsepol/cil: Check for valid line mark type immediately libsepol/cil: Push line mark state first when processing a line mark libsepol/cil: Create common string-to-unsigned-integer functions libsepol/cil: Add line mark kind and line number to src info libsepol/cil: Report correct high-level language line numbers

 libsepol/cil/src/cil.c           |  70 ++++++++++++++++-
 libsepol/cil/src/cil_binary.c    |   9 ++-
 libsepol/cil/src/cil_build_ast.c |  77 ++++++++++--------
 libsepol/cil/src/cil_copy_ast.c  |   5 +-
 libsepol/cil/src/cil_internal.h  |   9 ++-
 libsepol/cil/src/cil_parser.c    | 129 +++++++++++++++----------------
 libsepol/cil/src/cil_tree.c      |  57 +++++++++-----
 libsepol/cil/src/cil_tree.h      |   4 +-
 8 files changed, 226 insertions(+), 134 deletions(-)
Signature · 1 line
2.31.1
#1 lore raw

PATCH v2 1/7 libsepol/cil: Check syntax of src_info statement

Every rule other than src_info has their syntax checked when building the AST. It wasn't considered necessary for src_info rules because they were expected to always be generated by the parser and aren't part of the CIL language. But there is no check preventing them from occurring in a policy and the secilc fuzzer found some bugs by using src_info rules in a policy. This caused some syntax checking to be added. Since the parse AST from secil2tree will contain src_info rules and since the goal is to be able to compile the output of secil2tree, it makes sense to check the syntax of src_info rules in the same way that all of the other rules are checked.

Check the syntax of src_info statements in the same way every other rule is checked.

---
 libsepol/cil/src/cil_build_ast.c | 24 ++++++++++++++++++++----
 1 file changed, 20 insertions(+), 4 deletions(-)
libsepol/cil/src/cil_build_ast.c +20 -4
index 9da90883..5e65a266 100644
@@ -6075,12 +6075,24 @@ void cil_destroy_mls(struct cil_mls *mls)
 
 int cil_gen_src_info(struct cil_tree_node *parse_current, struct cil_tree_node *ast_node)
 {
-	/* No need to check syntax, because this is auto generated */
+	int rc = SEPOL_ERR;
+	enum cil_syntax syntax[] = {
+		CIL_SYN_STRING,
+		CIL_SYN_STRING,
+		CIL_SYN_STRING,
+		CIL_SYN_N_LISTS | CIL_SYN_END,
+		CIL_SYN_END
+	};
+	int syntax_len = sizeof(syntax)/sizeof(*syntax);
 	struct cil_src_info *info = NULL;
 
-	if (parse_current->next == NULL || parse_current->next->next == NULL) {
-		cil_tree_log(parse_current, CIL_ERR, "Bad <src_info>");
-		return SEPOL_ERR;
+	if (parse_current == NULL || ast_node == NULL) {
+		goto exit;
+	}
+
+	rc = __cil_verify_syntax(parse_current, syntax, syntax_len);
+	if (rc != SEPOL_OK) {
+		goto exit;
 	}
 
 	cil_src_info_init(&info);
@@ -6092,6 +6104,10 @@ int cil_gen_src_info(struct cil_tree_node *parse_current, struct cil_tree_node *
 	ast_node->flavor = CIL_SRC_INFO;
 
 	return SEPOL_OK;
+
+exit:
+	cil_tree_log(parse_current, CIL_ERR, "Bad src info");
+	return rc;
 }
 
 void cil_destroy_src_info(struct cil_src_info *info)
Signature · 1 line
2.31.1
#2 lore raw

PATCH v2 2/7 libsepol/cil: Check the token type after getting the next token

In reply to James Carter

In add_hll_linemark(), cil_lexer_next() is called and the token type is not checked after the call for the expected type (SYMBOL).

Check that the token type is SYMBOL after calling cil_lexer_next().

---
 libsepol/cil/src/cil_parser.c | 4 ++++
 1 file changed, 4 insertions(+)
libsepol/cil/src/cil_parser.c +4 -0
index fb95f401..fc90caec 100644
@@ -111,6 +111,10 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 	unsigned long val;
 
 	cil_lexer_next(&tok);
+	if (tok.type != SYMBOL) {
+		cil_log(CIL_ERR, "Invalid line mark syntax\n");
+		goto exit;
+	}
 	hll_type = cil_strpool_add(tok.value);
 	if (hll_type == CIL_KEY_HLL_LME) {
 		if (cil_stack_is_empty(stack)) {
Signature · 1 line
2.31.1
#3 lore raw

PATCH v2 3/7 libsepol/cil: Check for valid line mark type immediately

In reply to James Carter

It clearer to check that the line mark type is a valid option right after getting the token.

Check that the line mark type is one of the expected values right awasy.

---
 libsepol/cil/src/cil_parser.c | 14 +++++---------
 1 file changed, 5 insertions(+), 9 deletions(-)
libsepol/cil/src/cil_parser.c +5 -9
index fc90caec..24386f60 100644
@@ -116,6 +116,10 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 		goto exit;
 	}
 	hll_type = cil_strpool_add(tok.value);
+	if (hll_type != CIL_KEY_HLL_LME && hll_type != CIL_KEY_HLL_LMS && hll_type != CIL_KEY_HLL_LMX) {
+		cil_log(CIL_ERR, "Invalid line mark syntax\n");
+		goto exit;
+	}
 	if (hll_type == CIL_KEY_HLL_LME) {
 		if (cil_stack_is_empty(stack)) {
 			cil_log(CIL_ERR, "Line mark end without start\n");
@@ -134,15 +138,6 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 		create_node(&node, *current, tok.line, *hll_lineno, CIL_KEY_SRC_HLL);
 		insert_node(node, *current);
 
-		if (hll_type == CIL_KEY_HLL_LMS) {
-			*hll_expand = 0;
-		} else if (hll_type == CIL_KEY_HLL_LMX) {
-			*hll_expand = 1;
-		} else {
-			cil_log(CIL_ERR, "Invalid line mark syntax\n");
-			goto exit;
-		}
-
 		cil_lexer_next(&tok);
 		if (tok.type != SYMBOL) {
 			cil_log(CIL_ERR, "Invalid line mark syntax\n");
@@ -161,6 +156,7 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 		}
 #endif
 		*hll_lineno = val;
+		*hll_expand = (hll_type == CIL_KEY_HLL_LMX) ? 1 : 0;
 
 		push_hll_info(stack, *hll_lineno, *hll_expand);
 
Signature · 1 line
2.31.1
#4 lore raw

PATCH v4 4/7 libsepol/cil: Push line mark state first when processing a line mark

In reply to James Carter

CIL line mark rules are used to annotate the original line and file of a rule. It is mostly used for neverallow rules that have been converted to CIL.

Pushing the current line mark state after processing a line mark section does not make sense since that information is never used. When the line mark section ends the information is just popped and discarded. It also makes pop_hll_info() more complicated than it needs to be.

Push the line mark state first and simplfy pop_hll_info().

---
 libsepol/cil/src/cil_parser.c | 22 +++++++++-------------
 1 file changed, 9 insertions(+), 13 deletions(-)
libsepol/cil/src/cil_parser.c +9 -13
index 24386f60..d36ffc49 100644
@@ -66,19 +66,15 @@ static void push_hll_info(struct cil_stack *stack, uint32_t hll_lineno, uint32_t
 static void pop_hll_info(struct cil_stack *stack, uint32_t *hll_lineno, uint32_t *hll_expand)
 {
 	struct cil_stack_item *curr = cil_stack_pop(stack);
-	struct cil_stack_item *prev = cil_stack_peek(stack);
-	struct hll_info *old;
+	struct hll_info *info;
 
-	free(curr->data);
-
-	if (!prev) {
-		*hll_lineno = 0;
-		*hll_expand = 0;
-	} else {
-		old = prev->data;
-		*hll_lineno = old->hll_lineno;
-		*hll_expand = old->hll_expand;
+	if (!curr) {
+		return;
 	}
+	info = curr->data;
+	*hll_expand = info->hll_expand;
+	*hll_lineno = info->hll_lineno;
+	free(curr->data);
 }
 
 static void create_node(struct cil_tree_node **node, struct cil_tree_node *current, uint32_t line, uint32_t hll_line, void *value)
@@ -128,6 +124,8 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 		pop_hll_info(stack, hll_lineno, hll_expand);
 		*current = (*current)->parent;
 	} else {
+		push_hll_info(stack, *hll_lineno, *hll_expand);
+
 		create_node(&node, *current, tok.line, *hll_lineno, NULL);
 		insert_node(node, *current);
 		*current = node;
@@ -158,8 +156,6 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 		*hll_lineno = val;
 		*hll_expand = (hll_type == CIL_KEY_HLL_LMX) ? 1 : 0;
 
-		push_hll_info(stack, *hll_lineno, *hll_expand);
-
 		cil_lexer_next(&tok);
 		if (tok.type != SYMBOL && tok.type != QSTRING) {
 			cil_log(CIL_ERR, "Invalid line mark syntax\n");
Signature · 1 line
2.31.1
#5 lore raw

PATCH v2 5/7 libsepol/cil: Create common string-to-unsigned-integer functions

In reply to James Carter

The functions cil_fill_integer() and cil_fill_integer64() exist in cil_build_ast.c, but these functions take a node and it would be better to have a function that can be used in add_hll_linemark() so that the common functinality is in one place.

Create cil_string_to_uint32() and cil_string_to_uint64() and use these functions in cil_fill_integer(), cil_fill_integer64(), and add_hll_linemark().

---
 libsepol/cil/src/cil.c           | 57 ++++++++++++++++++++++++++++++++
 libsepol/cil/src/cil_build_ast.c | 32 ++++--------------
 libsepol/cil/src/cil_internal.h  |  2 ++
 libsepol/cil/src/cil_parser.c    | 16 +++------
 4 files changed, 69 insertions(+), 38 deletions(-)
libsepol/cil/src/cil.c +57 -0
index d24c81c8..bdd16eb8 100644
@@ -1997,6 +1997,63 @@ exit:
 	return SEPOL_ERR;	
 }
 
+int cil_string_to_uint32(const char *string, uint32_t *value, int base)
+{
+	unsigned long val;
+	char *end = NULL;
+	int rc = SEPOL_ERR;
+
+	if (string == NULL || value  == NULL) {
+		goto exit;
+	}
+
+	errno = 0;
+	val = strtoul(string, &end, base);
+	if (errno != 0 || end == string || *end != '\0') {
+		rc = SEPOL_ERR;
+		goto exit;
+	}
+
+	/* Ensure that the value fits a 32-bit integer without triggering -Wtype-limits */
+#if ULONG_MAX > UINT32_MAX
+	if (val > UINT32_MAX) {
+		rc = SEPOL_ERR;
+		goto exit;
+	}
+#endif
+
+	*value = val;
+
+	return SEPOL_OK;
+
+exit:
+	cil_log(CIL_ERR, "Failed to create uint32_t from string\n");
+	return rc;
+}
+
+int cil_string_to_uint64(const char *string, uint64_t *value, int base)
+{
+	char *end = NULL;
+	int rc = SEPOL_ERR;
+
+	if (string == NULL || value  == NULL) {
+		goto exit;
+	}
+
+	errno = 0;
+	*value = strtoull(string, &end, base);
+	if (errno != 0 || end == string || *end != '\0') {
+		rc = SEPOL_ERR;
+		goto exit;
+	}
+
+	return SEPOL_OK;
+
+exit:
+	cil_log(CIL_ERR, "Failed to create uint64_t from string\n");
+	return rc;
+}
+
 void cil_sort_init(struct cil_sort **sort)
 {
 	*sort = cil_malloc(sizeof(**sort));
libsepol/cil/src/cil_build_ast.c +6 -26
index 5e65a266..ffbd3082 100644
@@ -5601,60 +5601,40 @@ void cil_destroy_ipaddr(struct cil_ipaddr *ipaddr)
 int cil_fill_integer(struct cil_tree_node *int_node, uint32_t *integer, int base)
 {
 	int rc = SEPOL_ERR;
-	char *endptr = NULL;
-	unsigned long val;
 
 	if (int_node == NULL || int_node->data == NULL || integer == NULL) {
 		goto exit;
 	}
 
-	errno = 0;
-	val = strtoul(int_node->data, &endptr, base);
-	if (errno != 0 || endptr == int_node->data || *endptr != '\0') {
-		rc = SEPOL_ERR;
-		goto exit;
-	}
-
-	/* Ensure that the value fits a 32-bit integer without triggering -Wtype-limits */
-#if ULONG_MAX > UINT32_MAX
-	if (val > UINT32_MAX) {
-		rc = SEPOL_ERR;
+	rc = cil_string_to_uint32(int_node->data, integer, base);
+	if (rc != SEPOL_OK) {
 		goto exit;
 	}
-#endif
-
-	*integer = val;
 
 	return SEPOL_OK;
 
 exit:
-	cil_log(CIL_ERR, "Failed to create integer from string\n");
+	cil_log(CIL_ERR, "Failed to fill 32-bit integer\n");
 	return rc;
 }
 
 int cil_fill_integer64(struct cil_tree_node *int_node, uint64_t *integer, int base)
 {
 	int rc = SEPOL_ERR;
-	char *endptr = NULL;
-	uint64_t val;
 
 	if (int_node == NULL || int_node->data == NULL || integer == NULL) {
 		goto exit;
 	}
 
-	errno = 0;
-	val = strtoull(int_node->data, &endptr, base);
-	if (errno != 0 || endptr == int_node->data || *endptr != '\0') {
-		rc = SEPOL_ERR;
+	rc = cil_string_to_uint64(int_node->data, integer, base);
+	if (rc != SEPOL_OK) {
 		goto exit;
 	}
 
-	*integer = val;
-
 	return SEPOL_OK;
 
 exit:
-	cil_log(CIL_ERR, "Failed to create integer from string\n");
+	cil_log(CIL_ERR, "Failed to fill 64-bit integer\n");
 	return rc;
 }
 
libsepol/cil/src/cil_internal.h +2 -0
index 98e303d1..b9a03a37 100644
@@ -986,6 +986,8 @@ void cil_symtab_array_init(symtab_t symtab[], const int symtab_sizes[CIL_SYM_NUM
 void cil_symtab_array_destroy(symtab_t symtab[]);
 void cil_destroy_ast_symtabs(struct cil_tree_node *root);
 int cil_get_symtab(struct cil_tree_node *ast_node, symtab_t **symtab, enum cil_sym_index sym_index);
+int cil_string_to_uint32(const char *string, uint32_t *value, int base);
+int cil_string_to_uint64(const char *string, uint64_t *value, int base);
 
 void cil_sort_init(struct cil_sort **sort);
 void cil_sort_destroy(struct cil_sort **sort);
libsepol/cil/src/cil_parser.c +4 -12
index d36ffc49..9ca1432e 100644
@@ -103,8 +103,7 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 	struct cil_tree_node *node;
 	struct token tok;
 	char *hll_file;
-	char *end = NULL;
-	unsigned long val;
+	int rc;
 
 	cil_lexer_next(&tok);
 	if (tok.type != SYMBOL) {
@@ -142,18 +141,11 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 			goto exit;
 		}
 
-		val = strtoul(tok.value, &end, 10);
-		if (errno == ERANGE || *end != '\0') {
-			cil_log(CIL_ERR, "Problem parsing line number for line mark\n");
+		rc = cil_string_to_uint32(tok.value, hll_lineno, 10);
+		if (rc != SEPOL_OK) {
 			goto exit;
 		}
-#if ULONG_MAX > UINT32_MAX
-		if (val > UINT32_MAX) {
-			cil_log(CIL_ERR, "Line mark line number > UINT32_MAX\n");
-			goto exit;
-		}
-#endif
-		*hll_lineno = val;
+
 		*hll_expand = (hll_type == CIL_KEY_HLL_LMX) ? 1 : 0;
 
 		cil_lexer_next(&tok);
Signature · 1 line
2.31.1
#6 lore raw

PATCH v2 6/7 libsepol/cil: Add line mark kind and line number to src info

In reply to James Carter

To be able to write line mark information when writing the AST, the line mark kind and line number is needed in the src info.

Instead of indicating whether the src info is for CIL or a hll, differentiate between CIL, a normal hll line mark, and an expanded hll line mark. Also include the line mark line number in the src info nodes.

---
v2:
Use cil_strpool_add("1") instead of "1" in add_cil_path()

 libsepol/cil/src/cil.c           | 13 +++++++++----
 libsepol/cil/src/cil_build_ast.c | 17 +++++++++++++++--
 libsepol/cil/src/cil_copy_ast.c  |  3 ++-
 libsepol/cil/src/cil_internal.h  |  7 +++++--
 libsepol/cil/src/cil_parser.c    | 27 +++++++++++----------------
 libsepol/cil/src/cil_tree.c      |  2 +-
 6 files changed, 43 insertions(+), 26 deletions(-)
libsepol/cil/src/cil.c +9 -4
index bdd16eb8..caec5dad 100644
@@ -220,7 +220,9 @@ char *CIL_KEY_IOCTL;
 char *CIL_KEY_UNORDERED;
 char *CIL_KEY_SRC_INFO;
 char *CIL_KEY_SRC_CIL;
-char *CIL_KEY_SRC_HLL;
+char *CIL_KEY_SRC_HLL_LMS;
+char *CIL_KEY_SRC_HLL_LMX;
+char *CIL_KEY_SRC_HLL_LME;
 
 static void cil_init_keys(void)
 {
@@ -384,8 +386,10 @@ static void cil_init_keys(void)
 	CIL_KEY_IOCTL = cil_strpool_add("ioctl");
 	CIL_KEY_UNORDERED = cil_strpool_add("unordered");
 	CIL_KEY_SRC_INFO = cil_strpool_add("<src_info>");
-	CIL_KEY_SRC_CIL = cil_strpool_add("<src_cil>");
-	CIL_KEY_SRC_HLL = cil_strpool_add("<src_hll>");
+	CIL_KEY_SRC_CIL = cil_strpool_add("cil");
+	CIL_KEY_SRC_HLL_LMS = cil_strpool_add("lms");
+	CIL_KEY_SRC_HLL_LMX = cil_strpool_add("lmx");
+	CIL_KEY_SRC_HLL_LME = cil_strpool_add("lme");
 }
 
 void cil_db_init(struct cil_db **db)
@@ -2881,6 +2885,7 @@ void cil_mls_init(struct cil_mls **mls)
 void cil_src_info_init(struct cil_src_info **info)
 {
 	*info = cil_malloc(sizeof(**info));
-	(*info)->is_cil = 0;
+	(*info)->kind = NULL;
+	(*info)->hll_line = 0;
 	(*info)->path = NULL;
 }
libsepol/cil/src/cil_build_ast.c +15 -2
index ffbd3082..a0f58b1e 100644
@@ -6060,6 +6060,7 @@ int cil_gen_src_info(struct cil_tree_node *parse_current, struct cil_tree_node *
 		CIL_SYN_STRING,
 		CIL_SYN_STRING,
 		CIL_SYN_STRING,
+		CIL_SYN_STRING,
 		CIL_SYN_N_LISTS | CIL_SYN_END,
 		CIL_SYN_END
 	};
@@ -6077,8 +6078,19 @@ int cil_gen_src_info(struct cil_tree_node *parse_current, struct cil_tree_node *
 
 	cil_src_info_init(&info);
 
-	info->is_cil = (parse_current->next->data == CIL_KEY_SRC_CIL) ? CIL_TRUE : CIL_FALSE;
-	info->path = parse_current->next->next->data;
+	info->kind = parse_current->next->data;
+	if (info->kind != CIL_KEY_SRC_CIL && info->kind != CIL_KEY_SRC_HLL_LMS && info->kind != CIL_KEY_SRC_HLL_LMX) {
+		cil_log(CIL_ERR, "Invalid src info kind\n");
+		rc = SEPOL_ERR;
+		goto exit;
+	}
+
+	rc = cil_string_to_uint32(parse_current->next->next->data, &info->hll_line, 10);
+	if (rc != SEPOL_OK) {
+		goto exit;
+	}
+
+	info->path = parse_current->next->next->next->data;
 
 	ast_node->data = info;
 	ast_node->flavor = CIL_SRC_INFO;
@@ -6087,6 +6099,7 @@ int cil_gen_src_info(struct cil_tree_node *parse_current, struct cil_tree_node *
 
 exit:
 	cil_tree_log(parse_current, CIL_ERR, "Bad src info");
+	cil_destroy_src_info(info);
 	return rc;
 }
 
libsepol/cil/src/cil_copy_ast.c +2 -1
index 9c0231f2..02b9828f 100644
@@ -1692,7 +1692,8 @@ int cil_copy_src_info(__attribute__((unused)) struct cil_db *db, void *data, voi
 
 	cil_src_info_init(&new);
 
-	new->is_cil = orig->is_cil;
+	new->kind = orig->kind;
+	new->hll_line = orig->hll_line;
 	new->path = orig->path;
 
 	*copy = new;
libsepol/cil/src/cil_internal.h +5 -2
index b9a03a37..385677d4 100644
@@ -236,7 +236,9 @@ extern char *CIL_KEY_IOCTL;
 extern char *CIL_KEY_UNORDERED;
 extern char *CIL_KEY_SRC_INFO;
 extern char *CIL_KEY_SRC_CIL;
-extern char *CIL_KEY_SRC_HLL;
+extern char *CIL_KEY_SRC_HLL_LMS;
+extern char *CIL_KEY_SRC_HLL_LMX;
+extern char *CIL_KEY_SRC_HLL_LME;
 
 /*
 	Symbol Table Array Indices
@@ -963,7 +965,8 @@ struct cil_mls {
 };
 
 struct cil_src_info {
-	int is_cil;
+	char *kind;
+	uint32_t hll_line;
 	char *path;
 };
 
libsepol/cil/src/cil_parser.c +11 -16
index 9ca1432e..cde6e5c0 100644
@@ -44,10 +44,6 @@
 
 #define CIL_PARSER_MAX_EXPR_DEPTH (0x1 << 12)
 
-char *CIL_KEY_HLL_LMS;
-char *CIL_KEY_HLL_LMX;
-char *CIL_KEY_HLL_LME;
-
 struct hll_info {
 	uint32_t hll_lineno;
 	uint32_t hll_expand;
@@ -102,7 +98,6 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 	char *hll_type;
 	struct cil_tree_node *node;
 	struct token tok;
-	char *hll_file;
 	int rc;
 
 	cil_lexer_next(&tok);
@@ -111,11 +106,11 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 		goto exit;
 	}
 	hll_type = cil_strpool_add(tok.value);
-	if (hll_type != CIL_KEY_HLL_LME && hll_type != CIL_KEY_HLL_LMS && hll_type != CIL_KEY_HLL_LMX) {
+	if (hll_type != CIL_KEY_SRC_HLL_LME && hll_type != CIL_KEY_SRC_HLL_LMS && hll_type != CIL_KEY_SRC_HLL_LMX) {
 		cil_log(CIL_ERR, "Invalid line mark syntax\n");
 		goto exit;
 	}
-	if (hll_type == CIL_KEY_HLL_LME) {
+	if (hll_type == CIL_KEY_SRC_HLL_LME) {
 		if (cil_stack_is_empty(stack)) {
 			cil_log(CIL_ERR, "Line mark end without start\n");
 			goto exit;
@@ -132,7 +127,7 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 		create_node(&node, *current, tok.line, *hll_lineno, CIL_KEY_SRC_INFO);
 		insert_node(node, *current);
 
-		create_node(&node, *current, tok.line, *hll_lineno, CIL_KEY_SRC_HLL);
+		create_node(&node, *current, tok.line, *hll_lineno, hll_type);
 		insert_node(node, *current);
 
 		cil_lexer_next(&tok);
@@ -141,12 +136,15 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 			goto exit;
 		}
 
+		create_node(&node, *current, tok.line, *hll_lineno, cil_strpool_add(tok.value));
+		insert_node(node, *current);
+
 		rc = cil_string_to_uint32(tok.value, hll_lineno, 10);
 		if (rc != SEPOL_OK) {
 			goto exit;
 		}
 
-		*hll_expand = (hll_type == CIL_KEY_HLL_LMX) ? 1 : 0;
+		*hll_expand = (hll_type == CIL_KEY_SRC_HLL_LMX) ? 1 : 0;
 
 		cil_lexer_next(&tok);
 		if (tok.type != SYMBOL && tok.type != QSTRING) {
@@ -159,9 +157,7 @@ static int add_hll_linemark(struct cil_tree_node **current, uint32_t *hll_lineno
 			tok.value = tok.value+1;
 		}
 
-		hll_file = cil_strpool_add(tok.value);
-
-		create_node(&node, *current, tok.line, *hll_lineno, hll_file);
+		create_node(&node, *current, tok.line, *hll_lineno, cil_strpool_add(tok.value));
 		insert_node(node, *current);
 	}
 
@@ -192,6 +188,9 @@ static void add_cil_path(struct cil_tree_node **current, char *path)
 	create_node(&node, *current, 0, 0, CIL_KEY_SRC_CIL);
 	insert_node(node, *current);
 
+	create_node(&node, *current, 0, 0, cil_strpool_add("1"));
+	insert_node(node, *current);
+
 	create_node(&node, *current, 0, 0, path);
 	insert_node(node, *current);
 }
@@ -211,10 +210,6 @@ int cil_parser(const char *_path, char *buffer, uint32_t size, struct cil_tree *
 	struct token tok;
 	int rc = SEPOL_OK;
 
-	CIL_KEY_HLL_LMS = cil_strpool_add("lms");
-	CIL_KEY_HLL_LMX = cil_strpool_add("lmx");
-	CIL_KEY_HLL_LME = cil_strpool_add("lme");
-
 	cil_stack_init(&stack);
 
 	cil_lexer_setup(buffer, size);
libsepol/cil/src/cil_tree.c +1 -1
index 4cf8dcc8..52b28999 100644
@@ -71,7 +71,7 @@ struct cil_tree_node *cil_tree_get_next_path(struct cil_tree_node *node, char **
 				/* AST */
 				struct cil_src_info *info = node->data;
 				*path = info->path;
-				*is_cil = info->is_cil;
+				*is_cil = (info->kind == CIL_KEY_SRC_CIL);
 				return node;
 		} else {
 			if (node->flavor == CIL_CALL) {
Signature · 1 line
2.31.1
#7 lore raw

PATCH v2 7/7 libsepol/cil: Report correct high-level language line numbers

In reply to James Carter

CIL supports specifiying the original high-level language file and line numbers when reporting errors. This is done through line marks and is mostly used to report the original Refpolicy file and line number for neverallow rules that have been converted to CIL.

As long as the line mark remain simple, everything works fine, but the wrong line numbers will be reported with more complex nextings of line marks.

Example:
;;* lms 100 file01.hll
(type t1a)
(allow t1a self (CLASS (PERM)))
;;* lmx 200 file02.hll
(type t2a)
(allow t2a self (CLASS (PERM)))
;;* lme
(type t1b)
(allow t1b self (CLASS (PERM)))
(allow bad1b self (CLASS (PERM))) ; file01.hll:101 (Should be 106) ;;* lme

The primary problem is that the tree nodes can only store one hll line number. Instead a number is needed that can be used by any number of stacked line mark sections. This number would increment line a normal line number except when in lmx sections (that have the same line number throughout the section because they represent an expansion of a line -- like the expansion of a macro call. This number can go backwards when exiting a lms section within a lmx section, because line number will increase in the lms section, but outside the lmx section, the line number did not advance.

This number is called the hll_offset and this is the value that is now stored in tree nodes instead of the hll line number. To calculate the hll line number for a rule, a search is made for an ancestor of the node that is a line mark and the line number for a lms section is the hll line number stored in the line mark, plus the hll offset of the rule, minus the hll offset of the line mark node, minus one. (hll_lineno + hll_offset_rule - hll_offset_lm - 1)

---
v2:
Fix confusing usage of pre_hll_expand and pre_hll_offset Initialize pre_hll_offset
Change format specifiers in cil_tree.c to use %u instead of %d

 libsepol/cil/src/cil_binary.c    |  9 ++--
 libsepol/cil/src/cil_build_ast.c |  4 +-
 libsepol/cil/src/cil_copy_ast.c  |  2 +-
 libsepol/cil/src/cil_parser.c    | 72 +++++++++++++++++++-------------
 libsepol/cil/src/cil_tree.c      | 57 ++++++++++++++++---------
 libsepol/cil/src/cil_tree.h      |  4 +-
 6 files changed, 90 insertions(+), 58 deletions(-)
libsepol/cil/src/cil_binary.c +5 -4
index 2b65c622..43c37fc2 100644
@@ -4480,7 +4480,8 @@ static avrule_t *__cil_init_sepol_avrule(uint32_t kind, struct cil_tree_node *no
 	avrule_t *avrule;
 	struct cil_tree_node *source_node;
 	char *source_path;
-	int is_cil;
+	char *lm_kind;
+	uint32_t hll_line;
 
 	avrule = cil_malloc(sizeof(avrule_t));
 	avrule->specified = kind;
@@ -4492,11 +4493,11 @@ static avrule_t *__cil_init_sepol_avrule(uint32_t kind, struct cil_tree_node *no
 
 	avrule->source_filename = NULL;
 	avrule->source_line = node->line;
-	source_node = cil_tree_get_next_path(node, &source_path, &is_cil);
+	source_node = cil_tree_get_next_path(node, &lm_kind, &hll_line, &source_path);
 	if (source_node) {
 		avrule->source_filename = source_path;
-		if (!is_cil) {
-			avrule->source_line = node->hll_line;
+		if (lm_kind != CIL_KEY_SRC_CIL) {
+			avrule->source_line = hll_line + node->hll_offset - source_node->hll_offset - 1;
 		}
 	}
 
libsepol/cil/src/cil_build_ast.c +2 -2
index a0f58b1e..a5afc267 100644
@@ -619,7 +619,7 @@ int cil_gen_perm_nodes(struct cil_db *db, struct cil_tree_node *current_perm, st
 		cil_tree_node_init(&new_ast);
 		new_ast->parent = ast_node;
 		new_ast->line = current_perm->line;
-		new_ast->hll_line = current_perm->hll_line;
+		new_ast->hll_offset = current_perm->hll_offset;
 
 		rc = cil_gen_perm(db, current_perm, new_ast, flavor, num_perms);
 		if (rc != SEPOL_OK) {
@@ -6203,7 +6203,7 @@ int __cil_build_ast_node_helper(struct cil_tree_node *parse_current, uint32_t *f
 
 	ast_node->parent = ast_current;
 	ast_node->line = parse_current->line;
-	ast_node->hll_line = parse_current->hll_line;
+	ast_node->hll_offset = parse_current->hll_offset;
 
 	if (parse_current->data == CIL_KEY_BLOCK) {
 		rc = cil_gen_block(db, parse_current, ast_node, 0);
libsepol/cil/src/cil_copy_ast.c +1 -1
index 02b9828f..34282a92 100644
@@ -2010,7 +2010,7 @@ int __cil_copy_node_helper(struct cil_tree_node *orig, __attribute__((unused)) u
 
 		new->parent = parent;
 		new->line = orig->line;
-		new->hll_line = orig->hll_line;
+		new->hll_offset = orig->hll_offset;
 		new->flavor = orig->flavor;
 		new->data = data;
 
libsepol/cil/src/cil_parser.c +42 -30

188 lines not on this page · raw message

libsepol/cil/src/cil_tree.c +38 -19

110 lines not on this page · raw message

libsepol/cil/src/cil_tree.h +2 -2
index f4d22071..5a98da55 100644
@@ -46,11 +46,11 @@ struct cil_tree_node {
 	struct cil_tree_node *next;		//Each element in the list points to the next element
 	enum cil_flavor flavor;
 	uint32_t line;
-	uint32_t hll_line;
+	uint32_t hll_offset;
 	void *data;
 };
 
-struct cil_tree_node *cil_tree_get_next_path(struct cil_tree_node *node, char **path, int* is_cil);
+struct cil_tree_node *cil_tree_get_next_path(struct cil_tree_node *node, char **info_kind, uint32_t *hll_line, char **path);
 char *cil_tree_get_cil_path(struct cil_tree_node *node);
 __attribute__((format (printf, 3, 4))) void cil_tree_log(struct cil_tree_node *node, enum cil_log_level lvl, const char* msg, ...);
 
Signature · 1 line
2.31.1
#8 lore raw
In reply to James Carter
Quoted 7 lines · James Carter

Patches 1-5 cleanup minor issues with CIL's line marks.

Patches 6-7 fix the way line mark works so that the correct line numbers will be given for nested line marks.

Patch 8 writes out line mark information when writing out the AST.

Hello,
Patch 8 is missing from the v2 (the series only contains 7 patches). Should the previous patch 8 ("libsepol/cil: When writing AST use line marks for src_info nodes") be also considered for review?

Thanks,
Nicolas

Quoted 26 lines

Changes in v2
In patch 6, use cil_strpool_add("1") instead of "1"
In patch 7, fix confusing usage of pre_hll_expand and pre_hll_offset; initialize pre_hll_offset, change format specifiers in cil_tree.c to use %u instead of %d.

James Carter (7):
libsepol/cil: Check syntax of src_info statement libsepol/cil: Check the token type after getting the next token libsepol/cil: Check for valid line mark type immediately libsepol/cil: Push line mark state first when processing a line mark libsepol/cil: Create common string-to-unsigned-integer functions libsepol/cil: Add line mark kind and line number to src info libsepol/cil: Report correct high-level language line numbers

 libsepol/cil/src/cil.c           |  70 ++++++++++++++++-
 libsepol/cil/src/cil_binary.c    |   9 ++-
 libsepol/cil/src/cil_build_ast.c |  77 ++++++++++--------
 libsepol/cil/src/cil_copy_ast.c  |   5 +-
 libsepol/cil/src/cil_internal.h  |   9 ++-
 libsepol/cil/src/cil_parser.c    | 129 +++++++++++++++----------------
 libsepol/cil/src/cil_tree.c      |  57 +++++++++-----
 libsepol/cil/src/cil_tree.h      |   4 +-
 8 files changed, 226 insertions(+), 134 deletions(-)

--
2.31.1

#9 lore raw
Quoted 8 lines · Nicolas Iooss
Quoted 6 lines · James Carter

Patches 1-5 cleanup minor issues with CIL's line marks.

Patches 6-7 fix the way line mark works so that the correct line numbers will be given for nested line marks.

Quoted 7 lines
Quoted 1 line

Patch 8 writes out line mark information when writing out the AST.

Hello,
Patch 8 is missing from the v2 (the series only contains 7 patches). Should the previous patch 8 ("libsepol/cil: When writing AST use line marks for src_info nodes") be also considered for review?

Yes, I got caught up in correcting patch 7 that I never applied patch 8 when updating. There are no changes to that patch. Thanks,
Jim

Quoted 30 lines

Thanks,
Nicolas

Quoted 26 lines

Changes in v2
In patch 6, use cil_strpool_add("1") instead of "1"
In patch 7, fix confusing usage of pre_hll_expand and pre_hll_offset; initialize pre_hll_offset, change format specifiers in cil_tree.c to use %u instead of %d.

James Carter (7):
libsepol/cil: Check syntax of src_info statement libsepol/cil: Check the token type after getting the next token libsepol/cil: Check for valid line mark type immediately libsepol/cil: Push line mark state first when processing a line mark libsepol/cil: Create common string-to-unsigned-integer functions libsepol/cil: Add line mark kind and line number to src info libsepol/cil: Report correct high-level language line numbers

 libsepol/cil/src/cil.c           |  70 ++++++++++++++++-
 libsepol/cil/src/cil_binary.c    |   9 ++-
 libsepol/cil/src/cil_build_ast.c |  77 ++++++++++--------
 libsepol/cil/src/cil_copy_ast.c  |   5 +-
 libsepol/cil/src/cil_internal.h  |   9 ++-
 libsepol/cil/src/cil_parser.c    | 129 +++++++++++++++----------------
 libsepol/cil/src/cil_tree.c      |  57 +++++++++-----
 libsepol/cil/src/cil_tree.h      |   4 +-
 8 files changed, 226 insertions(+), 134 deletions(-)

--
2.31.1

Acked-by
#10 lore raw
Quoted 18 lines · James Carter
Quoted 8 lines · Nicolas Iooss
Quoted 6 lines · James Carter

Patches 1-5 cleanup minor issues with CIL's line marks.

Patches 6-7 fix the way line mark works so that the correct line numbers will be given for nested line marks.

Quoted 7 lines
Quoted 1 line

Patch 8 writes out line mark information when writing out the AST.

Hello,
Patch 8 is missing from the v2 (the series only contains 7 patches). Should the previous patch 8 ("libsepol/cil: When writing AST use line marks for src_info nodes") be also considered for review?

Quoted 4 lines

Yes, I got caught up in correcting patch 7 that I never applied patch 8 when updating. There are no changes to that patch. Thanks,
Jim

OK. For all 8 patches:

Thanks,
Nicolas

Quoted 27 lines
Quoted 27 lines
Quoted 26 lines

Changes in v2
In patch 6, use cil_strpool_add("1") instead of "1"
In patch 7, fix confusing usage of pre_hll_expand and pre_hll_offset; initialize pre_hll_offset, change format specifiers in cil_tree.c to use %u instead of %d.

James Carter (7):
libsepol/cil: Check syntax of src_info statement libsepol/cil: Check the token type after getting the next token libsepol/cil: Check for valid line mark type immediately libsepol/cil: Push line mark state first when processing a line mark libsepol/cil: Create common string-to-unsigned-integer functions libsepol/cil: Add line mark kind and line number to src info libsepol/cil: Report correct high-level language line numbers

 libsepol/cil/src/cil.c           |  70 ++++++++++++++++-
 libsepol/cil/src/cil_binary.c    |   9 ++-
 libsepol/cil/src/cil_build_ast.c |  77 ++++++++++--------
 libsepol/cil/src/cil_copy_ast.c  |   5 +-
 libsepol/cil/src/cil_internal.h  |   9 ++-
 libsepol/cil/src/cil_parser.c    | 129 +++++++++++++++----------------
 libsepol/cil/src/cil_tree.c      |  57 +++++++++-----
 libsepol/cil/src/cil_tree.h      |   4 +-
 8 files changed, 226 insertions(+), 134 deletions(-)

--
2.31.1

#11 lore raw
Quoted 20 lines · Nicolas Iooss
Quoted 18 lines · James Carter
Quoted 15 lines · Nicolas Iooss
Quoted 7 lines · James Carter

Patches 1-5 cleanup minor issues with CIL's line marks.

Patches 6-7 fix the way line mark works so that the correct line numbers will be given for nested line marks.

Patch 8 writes out line mark information when writing out the AST.

Hello,
Patch 8 is missing from the v2 (the series only contains 7 patches). Should the previous patch 8 ("libsepol/cil: When writing AST use line marks for src_info nodes") be also considered for review?

Quoted 9 lines
Quoted 4 lines

Yes, I got caught up in correcting patch 7 that I never applied patch 8 when updating. There are no changes to that patch. Thanks,
Jim

OK. For all 8 patches:

Merged.
Thanks,
Jim

Quoted 31 lines

Thanks,
Nicolas

Quoted 27 lines
Quoted 27 lines
Quoted 26 lines

Changes in v2
In patch 6, use cil_strpool_add("1") instead of "1"
In patch 7, fix confusing usage of pre_hll_expand and pre_hll_offset; initialize pre_hll_offset, change format specifiers in cil_tree.c to use %u instead of %d.

James Carter (7):
libsepol/cil: Check syntax of src_info statement libsepol/cil: Check the token type after getting the next token libsepol/cil: Check for valid line mark type immediately libsepol/cil: Push line mark state first when processing a line mark libsepol/cil: Create common string-to-unsigned-integer functions libsepol/cil: Add line mark kind and line number to src info libsepol/cil: Report correct high-level language line numbers

 libsepol/cil/src/cil.c           |  70 ++++++++++++++++-
 libsepol/cil/src/cil_binary.c    |   9 ++-
 libsepol/cil/src/cil_build_ast.c |  77 ++++++++++--------
 libsepol/cil/src/cil_copy_ast.c  |   5 +-
 libsepol/cil/src/cil_internal.h  |   9 ++-
 libsepol/cil/src/cil_parser.c    | 129 +++++++++++++++----------------
 libsepol/cil/src/cil_tree.c      |  57 +++++++++-----
 libsepol/cil/src/cil_tree.h      |   4 +-
 8 files changed, 226 insertions(+), 134 deletions(-)

--
2.31.1