Skip to contentSkip to conversation

libsepol/cil: Limit the amount of reporting for neverallow violations

PATCH In review James Carter 2 participants 7 messages Jan 14, 2022 – Feb 14, 2022 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libsepol/cil: Limit the amount of reporting for neverallow violations

When there is a neverallow violation, a search is made for all of the rules that violate the neverallow. The violating rules as well as their parents are written out to make it easier to find these rules.

If there is a lot of rules that violate a neverallow, then this amount of reporting is too much. Instead, only print out the first two rules (with their parents) that match the violated neverallow rule along with the total number of rules that violate the neverallow.

---
 libsepol/cil/src/cil_binary.c | 10 ++++++++++
 1 file changed, 10 insertions(+)
libsepol/cil/src/cil_binary.c +10 -0
index 4ac8ce8d..04a5d053 100644
@@ -4640,6 +4640,8 @@ static int __cil_print_neverallow_failure(const struct cil_db *db, struct cil_tr
 	char *neverallow_str;
 	char *allow_str;
 	enum cil_flavor avrule_flavor;
+	int num_matching = 0;
+	int count_matching = 0;
 
 	target.rule_kind = CIL_AVRULE_ALLOWED;
 	target.is_extended = cil_rule->is_extended;
@@ -4666,11 +4668,19 @@ static int __cil_print_neverallow_failure(const struct cil_db *db, struct cil_tr
 		goto exit;
 	}
 
+	cil_list_for_each(i2, matching) {
+		num_matching++;
+	}
 	cil_list_for_each(i2, matching) {
 		n2 = i2->data;
 		r2 = n2->data;
 		__cil_print_parents("    ", n2);
 		__cil_print_rule("      ", allow_str, r2);
+		count_matching++;
+		if (count_matching >= 2) {
+			cil_log(CIL_ERR, "    Only first 2 of %d matching rules shown\n", num_matching);
+			break;
+		}
 	}
 	cil_log(CIL_ERR,"\n");
 	cil_list_destroy(&matching, CIL_FALSE);
Signature · 1 line
2.31.1
#1 lore raw

PATCH 2/2 libsepol/cil: Limit the amount of reporting for context rule conflicts

When there are conflicting context rules, the location of the conflicting rules are written out. If there are many duplicates of the same context rule, there will be many pairs of conflicts written out. This hides the fact that all of the rules are the same and can make it hard to see the different conflicts.

Report all the duplicate conflicting rules together and only report the first 10 conflicts of the same rule.

Fixes problem found by oss-fuzz (#39735)

---
 libsepol/cil/src/cil_post.c | 46 +++++++++++++++++++++----------------
 1 file changed, 26 insertions(+), 20 deletions(-)
libsepol/cil/src/cil_post.c +26 -20
index 7e2c2b9a..f0520abe 100644
@@ -2280,7 +2280,8 @@ static int __cil_post_report_conflict(struct cil_tree_node *node, uint32_t *fini
 static int __cil_post_process_context_rules(struct cil_sort *sort, int (*compar)(const void *, const void *), int (*concompar)(const void *, const void *), struct cil_db *db, enum cil_flavor flavor, const char *flavor_str)
 {
 	uint32_t count = sort->count;
-	uint32_t i, j = 0, removed = 0;
+	uint32_t i = 0, j, removed = 0;
+	int conflicting = 0;
 	int rc = SEPOL_OK;
 
 	if (count < 2) {
@@ -2289,36 +2290,41 @@ static int __cil_post_process_context_rules(struct cil_sort *sort, int (*compar)
 
 	qsort(sort->array, sort->count, sizeof(sort->array), compar);
 
-	for (i=1; i<count; i++) {
+	for (j=1; j<count; j++) {
 		if (compar(&sort->array[i], &sort->array[j]) != 0) {
-			j++;
+			i++;
+			if (conflicting >= 10) {
+				cil_log(CIL_WARN, "  Only first 10 of %d conflicting rules shown\n", conflicting);
+			}
+			conflicting = 0;
 		} else {
 			removed++;
 			if (!db->multiple_decls ||
 			   concompar(&sort->array[i], &sort->array[j]) != 0) {
 				struct cil_list_item li;
 				int rc2;
-				cil_log(CIL_WARN, "Found conflicting %s rules\n",
-					flavor_str);
-				rc = SEPOL_ERR;
-				li.flavor = flavor;
-				li.data = sort->array[i];
-				rc2 = cil_tree_walk(db->ast->root,
-						    __cil_post_report_conflict,
-						    NULL, NULL, &li);
-				if (rc2 != SEPOL_OK) goto exit;
-				li.data = sort->array[j];
-				rc2 = cil_tree_walk(db->ast->root,
-						    __cil_post_report_conflict,
-						    NULL, NULL, &li);
-				if (rc2 != SEPOL_OK) goto exit;
+				conflicting++;
+				if (conflicting == 1) {
+					cil_log(CIL_WARN, "Found conflicting %s rules\n", flavor_str);
+					rc = SEPOL_ERR;
+					li.flavor = flavor;
+					li.data = sort->array[i];
+					rc2 = cil_tree_walk(db->ast->root, __cil_post_report_conflict,
+										NULL, NULL, &li);
+					if (rc2 != SEPOL_OK) goto exit;
+				}
+				if (conflicting < 10) {
+					li.data = sort->array[j];
+					rc2 = cil_tree_walk(db->ast->root, __cil_post_report_conflict,
+										NULL, NULL, &li);
+					if (rc2 != SEPOL_OK) goto exit;
+				}
 			}
 		}
-		if (i != j) {
-			sort->array[j] = sort->array[i];
+		if (i != j && !conflicting) {
+			sort->array[i] = sort->array[j];
 		}
 	}
-
 	sort->count = count - removed;
 
 exit:
Signature · 1 line
2.31.1
#2 lore raw
In reply to James Carter

Hi,

as a heavy user of neverallow / neverallowx, please don't limit this.

When adding a new neverallow rule there might quite a few types violating them, and having to rebuild the policy every 2 types would make fixing them incredibly annoying.

If you want to limit this, then please make it opt-in or add it as a command line option.

Quoted 49 lines · James Carter

When there is a neverallow violation, a search is made for all of the rules that violate the neverallow. The violating rules as well as their parents are written out to make it easier to find these rules.

If there is a lot of rules that violate a neverallow, then this amount of reporting is too much. Instead, only print out the first two rules (with their parents) that match the violated neverallow rule along with the total number of rules that violate the neverallow.

---
  libsepol/cil/src/cil_binary.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
libsepol/cil/src/cil_binary.c +10 -0
index 4ac8ce8d..04a5d053 100644
@@ -4640,6 +4640,8 @@ static int __cil_print_neverallow_failure(const struct cil_db *db, struct cil_tr
  	char *neverallow_str;
  	char *allow_str;
  	enum cil_flavor avrule_flavor;
+	int num_matching = 0;
+	int count_matching = 0;
 
  	target.rule_kind = CIL_AVRULE_ALLOWED;
  	target.is_extended = cil_rule->is_extended;
@@ -4666,11 +4668,19 @@ static int __cil_print_neverallow_failure(const struct cil_db *db, struct cil_tr
  		goto exit;
  	}
 
+	cil_list_for_each(i2, matching) {
+		num_matching++;
+	}
  	cil_list_for_each(i2, matching) {
  		n2 = i2->data;
  		r2 = n2->data;
  		__cil_print_parents("    ", n2);
  		__cil_print_rule("      ", allow_str, r2);
+		count_matching++;
+		if (count_matching >= 2) {
+			cil_log(CIL_ERR, "    Only first 2 of %d matching rules shown\n", num_matching);
+			break;
+		}
  	}
  	cil_log(CIL_ERR,"\n");
  	cil_list_destroy(&matching, CIL_FALSE);
Signature · 2 lines
bauen1
https://dn42.bauen1.xyz/
#3 lore raw
Quoted 9 lines · On Fri, Jan 14, 2022 at 2:44 PM bauen1 <[email protected]> wrote:

Hi,

as a heavy user of neverallow / neverallowx, please don't limit this.

When adding a new neverallow rule there might quite a few types violating them, and having to rebuild the policy every 2 types would make fixing them incredibly annoying.

If you want to limit this, then please make it opt-in or add it as a command line option.

I am trying to limit error messages because oss-fuzz seems to be good at creating policies that generate a lot of error messages and subsequently take a lot of time to process.

But I am not going to do that at the expense of people actually using secilc.

I was already thinking about making the amount of error reporting depending on the verbosity level. What would think of it limiting it to two by default, but unlimited at any higher verbosity level. I can even add a message to use "-v" to see all of the errors.

Jim

Quoted 54 lines · James Carter
Quoted 39 lines

When there is a neverallow violation, a search is made for all of the rules that violate the neverallow. The violating rules as well as their parents are written out to make it easier to find these rules.

If there is a lot of rules that violate a neverallow, then this amount of reporting is too much. Instead, only print out the first two rules (with their parents) that match the violated neverallow rule along with the total number of rules that violate the neverallow.

---
  libsepol/cil/src/cil_binary.c | 10 ++++++++++
  1 file changed, 10 insertions(+)
libsepol/cil/src/cil_binary.c +5 -0
index 4ac8ce8d..04a5d053 100644
@@ -4640,6 +4640,8 @@ static int __cil_print_neverallow_failure(const struct cil_db *db, struct cil_tr
      char *neverallow_str;
      char *allow_str;
      enum cil_flavor avrule_flavor;
+     int num_matching = 0;
+     int count_matching = 0;
 
      target.rule_kind = CIL_AVRULE_ALLOWED;
      target.is_extended = cil_rule->is_extended;
@@ -4666,11 +4668,19 @@ static int __cil_print_neverallow_failure(const struct cil_db *db, struct cil_tr
              goto exit;
      }
 
+     cil_list_for_each(i2, matching) {
+             num_matching++;
+     }
      cil_list_for_each(i2, matching) {
              n2 = i2->data;
              r2 = n2->data;

libsepol/cil/src/cil_binary.c static int __cil_print_neverallow_failure(const struct cil_db *db, struct cil_tr · line 4686

Quoted 10 lines
libsepol/cil/src/cil_binary.c +5 -0
              __cil_print_parents("    ", n2);
              __cil_print_rule("      ", allow_str, r2);
+             count_matching++;
+             if (count_matching >= 2) {
+                     cil_log(CIL_ERR, "    Only first 2 of %d matching rules shown\n", num_matching);
+                     break;
+             }
      }
      cil_log(CIL_ERR,"\n");
      cil_list_destroy(&matching, CIL_FALSE);

--
bauen1
https://dn42.bauen1.xyz/

#4 lore raw
Quoted 15 lines · James Carter
Quoted 9 lines · On Fri, Jan 14, 2022 at 2:44 PM bauen1 <[email protected]> wrote:

Hi,

as a heavy user of neverallow / neverallowx, please don't limit this.

When adding a new neverallow rule there might quite a few types violating them, and having to rebuild the policy every 2 types would make fixing them incredibly annoying.

If you want to limit this, then please make it opt-in or add it as a command line option.

I am trying to limit error messages because oss-fuzz seems to be good at creating policies that generate a lot of error messages and subsequently take a lot of time to process.

Quoted 6 lines

But I am not going to do that at the expense of people actually using secilc.

I was already thinking about making the amount of error reporting depending on the verbosity level. What would think of it limiting it to two by default, but unlimited at any higher verbosity level. I can even add a message to use "-v" to see all of the errors.

Thanks, something like that would be totally fine for me.

Quoted 57 lines

Jim

Quoted 54 lines · James Carter
Quoted 39 lines

When there is a neverallow violation, a search is made for all of the rules that violate the neverallow. The violating rules as well as their parents are written out to make it easier to find these rules.

If there is a lot of rules that violate a neverallow, then this amount of reporting is too much. Instead, only print out the first two rules (with their parents) that match the violated neverallow rule along with the total number of rules that violate the neverallow.

---
   libsepol/cil/src/cil_binary.c | 10 ++++++++++
   1 file changed, 10 insertions(+)
libsepol/cil/src/cil_binary.c +5 -0
index 4ac8ce8d..04a5d053 100644
@@ -4640,6 +4640,8 @@ static int __cil_print_neverallow_failure(const struct cil_db *db, struct cil_tr
       char *neverallow_str;
       char *allow_str;
       enum cil_flavor avrule_flavor;
+     int num_matching = 0;
+     int count_matching = 0;
 
       target.rule_kind = CIL_AVRULE_ALLOWED;
       target.is_extended = cil_rule->is_extended;
@@ -4666,11 +4668,19 @@ static int __cil_print_neverallow_failure(const struct cil_db *db, struct cil_tr
               goto exit;
       }
 
+     cil_list_for_each(i2, matching) {
+             num_matching++;
+     }
       cil_list_for_each(i2, matching) {
               n2 = i2->data;
               r2 = n2->data;

libsepol/cil/src/cil_binary.c static int __cil_print_neverallow_failure(const struct cil_db *db, struct cil_tr · line 4686

Quoted 10 lines
libsepol/cil/src/cil_binary.c +5 -0
               __cil_print_parents("    ", n2);
               __cil_print_rule("      ", allow_str, r2);
+             count_matching++;
+             if (count_matching >= 2) {
+                     cil_log(CIL_ERR, "    Only first 2 of %d matching rules shown\n", num_matching);
+                     break;
+             }
       }
       cil_log(CIL_ERR,"\n");
       cil_list_destroy(&matching, CIL_FALSE);

--
bauen1
https://dn42.bauen1.xyz/

Signature · 2 lines
bauen1
https://dn42.bauen1.xyz/
#5 lore raw

Hi,

Quoted 17 lines · On 1/19/22 14:04, bauen1 wrote:
Quoted 15 lines · James Carter
Quoted 9 lines · On Fri, Jan 14, 2022 at 2:44 PM bauen1 <[email protected]> wrote:

Hi,

as a heavy user of neverallow / neverallowx, please don't limit this.

When adding a new neverallow rule there might quite a few types violating them, and having to rebuild the policy every 2 types would make fixing them incredibly annoying.

If you want to limit this, then please make it opt-in or add it as a command line option.

I am trying to limit error messages because oss-fuzz seems to be good at creating policies that generate a lot of error messages and subsequently take a lot of time to process.

Quoted 9 lines
Quoted 6 lines

But I am not going to do that at the expense of people actually using secilc.

I was already thinking about making the amount of error reporting depending on the verbosity level. What would think of it limiting it to two by default, but unlimited at any higher verbosity level. I can even add a message to use "-v" to see all of the errors.

Thanks, something like that would be totally fine for me.

I've also just noticed that typebounds will only print the first 2 violations.
So if you make this depend on the verbosity level you might want to change that too, just to be consistent.

Signature · 2 lines
bauen1
https://dn42.bauen1.xyz/
#6 lore raw
Quoted 28 lines · On Fri, Feb 11, 2022 at 8:03 PM bauen1 <[email protected]> wrote:

Hi,

Quoted 24 lines · On 1/19/22 14:04, bauen1 wrote:
Quoted 21 lines · James Carter
Quoted 9 lines · On Fri, Jan 14, 2022 at 2:44 PM bauen1 <[email protected]> wrote:

Hi,

as a heavy user of neverallow / neverallowx, please don't limit this.

When adding a new neverallow rule there might quite a few types violating them, and having to rebuild the policy every 2 types would make fixing them incredibly annoying.

If you want to limit this, then please make it opt-in or add it as a command line option.

I am trying to limit error messages because oss-fuzz seems to be good at creating policies that generate a lot of error messages and subsequently take a lot of time to process.

But I am not going to do that at the expense of people actually using secilc.

I was already thinking about making the amount of error reporting depending on the verbosity level. What would think of it limiting it to two by default, but unlimited at any higher verbosity level. I can even add a message to use "-v" to see all of the errors.

Quoted 6 lines
Quoted 2 lines

Thanks, something like that would be totally fine for me.

I've also just noticed that typebounds will only print the first 2 violations.
So if you make this depend on the verbosity level you might want to change that too, just to be consistent.

Yes, I did send out a v2 that changes the typebounds error reporting to depend on the verbosity level as well. That patch set was sent out on January 19th.

Thanks,
Jim

Quoted 3 lines

--
bauen1
https://dn42.bauen1.xyz/