Skip to contentSkip to conversation

libsepol: Write CIL default MLS rules on separate lines

PATCH Superseded James Carter 2 participants 5 messages May 21, 2020 – May 22, 2020 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/3 libsepol: Write CIL default MLS rules on separate lines

When converting a non-MLS kernel binary policy to CIL, write the CIL default MLS rules (since CIL requires at least one sensitivity, and sensitivityorder statements) on separate lines.

This improves the readability of the resulting CIL policy.

---
 libsepol/src/kernel_to_cil.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
libsepol/src/kernel_to_cil.c +3 -3
index ede78a20..cca77251 100644
@@ -777,9 +777,9 @@ exit:
 
 static void write_default_mls_level(FILE *out)
 {
-	sepol_printf(out, "(sensitivity s0)");
-	sepol_printf(out, "(sensitivityorder (s0))");
-	sepol_printf(out, "(level %s (s0))", DEFAULT_LEVEL);
+	sepol_printf(out, "(sensitivity s0)\n");
+	sepol_printf(out, "(sensitivityorder (s0))\n");
+	sepol_printf(out, "(level %s (s0))\n", DEFAULT_LEVEL);
 }
 
 static int map_sensitivity_aliases_to_strs(char *key, void *data, void *args)
Signature · 1 line
2.25.4
#1 lore raw

PATCH 2/3 libsepol: Improve writing CIL sensitivity rules

Improves writing of CIL sensitivity rules when converting MLS kernel policy to CIL. No changes to functionality, but eliminate useless checks for sensitivity aliases when using the p_sens_val_to_name array, find the actual number of aliases before allocating memory, and skip the sensitivity alias rules if there are no aliases.

---
 libsepol/src/kernel_to_cil.c | 59 ++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 30 deletions(-)
libsepol/src/kernel_to_cil.c +29 -30
index cca77251..6103c1a6 100644
@@ -782,6 +782,17 @@ static void write_default_mls_level(FILE *out)
 	sepol_printf(out, "(level %s (s0))\n", DEFAULT_LEVEL);
 }
 
+static int map_count_sensitivity_aliases(char *key, void *data, void *args)
+{
+	level_datum_t *sens = data;
+	unsigned *count = args;
+
+	if (sens->isalias)
+		(*count)++;
+
+	return SEPOL_OK;
+}
+
 static int map_sensitivity_aliases_to_strs(char *key, void *data, void *args)
 {
 	level_datum_t *sens = data;
@@ -799,26 +810,13 @@ static int write_sensitivity_rules_to_cil(FILE *out, struct policydb *pdb)
 {
 	level_datum_t *level;
 	char *prev, *name, *actual;
-	struct strs *strs;
-	unsigned i, num;
+	struct strs *strs = NULL;
+	unsigned i, num = 0;
 	int rc = 0;
 
-	rc = strs_init(&strs, pdb->p_levels.nprim);
-	if (rc != 0) {
-		goto exit;
-	}
-
 	/* sensitivities */
 	for (i=0; i < pdb->p_levels.nprim; i++) {
 		name = pdb->p_sens_val_to_name[i];
-		if (!name) continue;
-		level = hashtab_search(pdb->p_levels.table, name);
-		if (!level) {
-			rc = -1;
-			goto exit;
-		}
-		if (level->isalias) continue;
-
 		sepol_printf(out, "(sensitivity %s)\n", name);
 	}
 
@@ -827,14 +825,6 @@ static int write_sensitivity_rules_to_cil(FILE *out, struct policydb *pdb)
 	prev = NULL;
 	for (i=0; i < pdb->p_levels.nprim; i++) {
 		name = pdb->p_sens_val_to_name[i];
-		if (!name) continue;
-		level = hashtab_search(pdb->p_levels.table, name);
-		if (!level) {
-			rc = -1;
-			goto exit;
-		}
-		if (level->isalias) continue;
-
 		if (prev) {
 			sepol_printf(out, "%s ", prev);
 		}
@@ -845,6 +835,22 @@ static int write_sensitivity_rules_to_cil(FILE *out, struct policydb *pdb)
 	}
 	sepol_printf(out, "))\n");
 
+	rc = hashtab_map(pdb->p_levels.table, map_count_sensitivity_aliases, &num);
+	if (rc != 0) {
+		goto exit;
+	}
+
+	if (num == 0) {
+		/* No aliases, so skip sensitivity alias rules */
+		rc = 0;
+		goto exit;
+	}
+
+	rc = strs_init(&strs, num);
+	if (rc != 0) {
+		goto exit;
+	}
+
 	rc = hashtab_map(pdb->p_levels.table, map_sensitivity_aliases_to_strs, strs);
 	if (rc != 0) {
 		goto exit;
@@ -852,16 +858,9 @@ static int write_sensitivity_rules_to_cil(FILE *out, struct policydb *pdb)
 
 	strs_sort(strs);
 
-	num = strs_num_items(strs);
-
 	/* sensitivity aliases */
 	for (i=0; i < num; i++) {
 		name = strs_read_at_index(strs, i);
-		level = hashtab_search(pdb->p_levels.table, name);
-		if (!level) {
-			rc = -1;
-			goto exit;
-		}
 		sepol_printf(out, "(sensitivityalias %s)\n", name);
 	}
 
Signature · 1 line
2.25.4
#2 lore raw

Re: [PATCH 2/3] libsepol: Improve writing CIL sensitivity rules

Quoted 20 lines · James Carter

Improves writing of CIL sensitivity rules when converting MLS kernel policy to CIL. No changes to functionality, but eliminate useless checks for sensitivity aliases when using the p_sens_val_to_name array, find the actual number of aliases before allocating memory, and skip the sensitivity alias rules if there are no aliases.

---
 libsepol/src/kernel_to_cil.c | 59 ++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 30 deletions(-)
libsepol/src/kernel_to_cil.c +0 -0
index cca77251..6103c1a6 100644
@@ -782,6 +782,17 @@ static void write_default_mls_level(FILE *out)
        sepol_printf(out, "(level %s (s0))\n", DEFAULT_LEVEL);
 }
 

libsepol/src/kernel_to_cil.c static void write_default_mls_level(FILE *out) · line 794

Quoted 10 lines
libsepol/src/kernel_to_cil.c +10 -0
+static int map_count_sensitivity_aliases(char *key, void *data, void *args)
+{
+       level_datum_t *sens = data;
+       unsigned *count = args;
+
+       if (sens->isalias)
+               (*count)++;
+
+       return SEPOL_OK;
+}

Hello,
This patch looks good to me, but building fails because of an unused parameter (here and in other patches),
https://travis-ci.org/github/fishilico/selinux/jobs/689760790#L2029 :

kernel_to_cil.c: In function ‘map_count_sensitivity_aliases’:
kernel_to_cil.c:785:48: error: unused parameter ‘key’ [-Werror=unused-parameter]
static int map_count_sensitivity_aliases(char *key, void *data, void *args)

                                                ^~~
kernel_to_cil.c: In function ‘map_count_category_aliases’:
kernel_to_cil.c:889:45: error: unused parameter ‘key’ [-Werror=unused-parameter]
 static int map_count_category_aliases(char *key, void *data, void *args)
                                             ^~~

kernel_to_cil.c: In function ‘map_count_type_aliases’:
kernel_to_cil.c:1368:41: error: unused parameter ‘key’ [-Werror=unused-parameter]
static int map_count_type_aliases(char *key, void *data, void *args)

                                        ^~~

Other functions use __attribute__((unused)) so you could probably use it too in these functions.

Thanks,
Nicolas

Quoted 91 lines
libsepol/src/kernel_to_cil.c +19 -31
+
 static int map_sensitivity_aliases_to_strs(char *key, void *data, void *args)
 {
        level_datum_t *sens = data;
@@ -799,26 +810,13 @@ static int write_sensitivity_rules_to_cil(FILE *out, struct policydb *pdb)
 {
        level_datum_t *level;
        char *prev, *name, *actual;
-       struct strs *strs;
-       unsigned i, num;
+       struct strs *strs = NULL;
+       unsigned i, num = 0;
        int rc = 0;
 
-       rc = strs_init(&strs, pdb->p_levels.nprim);
-       if (rc != 0) {
-               goto exit;
-       }
-
        /* sensitivities */
        for (i=0; i < pdb->p_levels.nprim; i++) {
                name = pdb->p_sens_val_to_name[i];
-               if (!name) continue;
-               level = hashtab_search(pdb->p_levels.table, name);
-               if (!level) {
-                       rc = -1;
-                       goto exit;
-               }
-               if (level->isalias) continue;
-
                sepol_printf(out, "(sensitivity %s)\n", name);
        }
 
@@ -827,14 +825,6 @@ static int write_sensitivity_rules_to_cil(FILE *out, struct policydb *pdb)
        prev = NULL;
        for (i=0; i < pdb->p_levels.nprim; i++) {
                name = pdb->p_sens_val_to_name[i];
-               if (!name) continue;
-               level = hashtab_search(pdb->p_levels.table, name);
-               if (!level) {
-                       rc = -1;
-                       goto exit;
-               }
-               if (level->isalias) continue;
-
                if (prev) {
                        sepol_printf(out, "%s ", prev);
                }
@@ -845,6 +835,22 @@ static int write_sensitivity_rules_to_cil(FILE *out, struct policydb *pdb)
        }
        sepol_printf(out, "))\n");
 
+       rc = hashtab_map(pdb->p_levels.table, map_count_sensitivity_aliases, &num);
+       if (rc != 0) {
+               goto exit;
+       }
+
+       if (num == 0) {
+               /* No aliases, so skip sensitivity alias rules */
+               rc = 0;
+               goto exit;
+       }
+
+       rc = strs_init(&strs, num);
+       if (rc != 0) {
+               goto exit;
+       }
+
        rc = hashtab_map(pdb->p_levels.table, map_sensitivity_aliases_to_strs, strs);
        if (rc != 0) {
                goto exit;
@@ -852,16 +858,9 @@ static int write_sensitivity_rules_to_cil(FILE *out, struct policydb *pdb)
 
        strs_sort(strs);
 
-       num = strs_num_items(strs);
-
        /* sensitivity aliases */
        for (i=0; i < num; i++) {
                name = strs_read_at_index(strs, i);
-               level = hashtab_search(pdb->p_levels.table, name);
-               if (!level) {
-                       rc = -1;
-                       goto exit;
-               }
                sepol_printf(out, "(sensitivityalias %s)\n", name);
        }
 
--

2.25.4

#3 lore raw

Re: [PATCH 2/3] libsepol: Improve writing CIL sensitivity rules

Quoted 46 lines · Nicolas Iooss
Quoted 20 lines · James Carter

Improves writing of CIL sensitivity rules when converting MLS kernel policy to CIL. No changes to functionality, but eliminate useless checks for sensitivity aliases when using the p_sens_val_to_name array, find the actual number of aliases before allocating memory, and skip the sensitivity alias rules if there are no aliases.

---
 libsepol/src/kernel_to_cil.c | 59 ++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 30 deletions(-)
libsepol/src/kernel_to_cil.c +0 -0
index cca77251..6103c1a6 100644
@@ -782,6 +782,17 @@ static void write_default_mls_level(FILE *out)
        sepol_printf(out, "(level %s (s0))\n", DEFAULT_LEVEL);
 }
 

libsepol/src/kernel_to_cil.c static void write_default_mls_level(FILE *out) · line 794

Quoted 10 lines
libsepol/src/kernel_to_cil.c +10 -0
+static int map_count_sensitivity_aliases(char *key, void *data, void *args)
+{
+       level_datum_t *sens = data;
+       unsigned *count = args;
+
+       if (sens->isalias)
+               (*count)++;
+
+       return SEPOL_OK;
+}

Hello,
This patch looks good to me, but building fails because of an unused parameter (here and in other patches),
https://travis-ci.org/github/fishilico/selinux/jobs/689760790#L2029 :

kernel_to_cil.c: In function ‘map_count_sensitivity_aliases’:
kernel_to_cil.c:785:48: error: unused parameter ‘key’ [-Werror=unused-parameter]
static int map_count_sensitivity_aliases(char *key, void *data, void *args)

                                                ^~~
kernel_to_cil.c: In function ‘map_count_category_aliases’:
kernel_to_cil.c:889:45: error: unused parameter ‘key’ [-Werror=unused-parameter]
 static int map_count_category_aliases(char *key, void *data, void *args)
                                             ^~~
Quoted 9 lines

kernel_to_cil.c: In function ‘map_count_type_aliases’:
kernel_to_cil.c:1368:41: error: unused parameter ‘key’ [-Werror=unused-parameter]
static int map_count_type_aliases(char *key, void *data, void *args)

                                        ^~~

Other functions use __attribute__((unused)) so you could probably use it too in these functions.

Thanks, I'll update these patches and resend.

Jim

Quoted 95 lines

Thanks,
Nicolas

Quoted 91 lines
libsepol/src/kernel_to_cil.c +19 -31
+
 static int map_sensitivity_aliases_to_strs(char *key, void *data, void *args)
 {
        level_datum_t *sens = data;
@@ -799,26 +810,13 @@ static int write_sensitivity_rules_to_cil(FILE *out, struct policydb *pdb)
 {
        level_datum_t *level;
        char *prev, *name, *actual;
-       struct strs *strs;
-       unsigned i, num;
+       struct strs *strs = NULL;
+       unsigned i, num = 0;
        int rc = 0;
 
-       rc = strs_init(&strs, pdb->p_levels.nprim);
-       if (rc != 0) {
-               goto exit;
-       }
-
        /* sensitivities */
        for (i=0; i < pdb->p_levels.nprim; i++) {
                name = pdb->p_sens_val_to_name[i];
-               if (!name) continue;
-               level = hashtab_search(pdb->p_levels.table, name);
-               if (!level) {
-                       rc = -1;
-                       goto exit;
-               }
-               if (level->isalias) continue;
-
                sepol_printf(out, "(sensitivity %s)\n", name);
        }
 
@@ -827,14 +825,6 @@ static int write_sensitivity_rules_to_cil(FILE *out, struct policydb *pdb)
        prev = NULL;
        for (i=0; i < pdb->p_levels.nprim; i++) {
                name = pdb->p_sens_val_to_name[i];
-               if (!name) continue;
-               level = hashtab_search(pdb->p_levels.table, name);
-               if (!level) {
-                       rc = -1;
-                       goto exit;
-               }
-               if (level->isalias) continue;
-
                if (prev) {
                        sepol_printf(out, "%s ", prev);
                }
@@ -845,6 +835,22 @@ static int write_sensitivity_rules_to_cil(FILE *out, struct policydb *pdb)
        }
        sepol_printf(out, "))\n");
 
+       rc = hashtab_map(pdb->p_levels.table, map_count_sensitivity_aliases, &num);
+       if (rc != 0) {
+               goto exit;
+       }
+
+       if (num == 0) {
+               /* No aliases, so skip sensitivity alias rules */
+               rc = 0;
+               goto exit;
+       }
+
+       rc = strs_init(&strs, num);
+       if (rc != 0) {
+               goto exit;
+       }
+
        rc = hashtab_map(pdb->p_levels.table, map_sensitivity_aliases_to_strs, strs);
        if (rc != 0) {
                goto exit;
@@ -852,16 +858,9 @@ static int write_sensitivity_rules_to_cil(FILE *out, struct policydb *pdb)
 
        strs_sort(strs);
 
-       num = strs_num_items(strs);
-
        /* sensitivity aliases */
        for (i=0; i < num; i++) {
                name = strs_read_at_index(strs, i);
-               level = hashtab_search(pdb->p_levels.table, name);
-               if (!level) {
-                       rc = -1;
-                       goto exit;
-               }
                sepol_printf(out, "(sensitivityalias %s)\n", name);
        }
 
--

2.25.4

#4 lore raw

PATCH 3/3 libsepol: Improve writing CIL category rules

In reply to James Carter

Improves writing of CIL category rules when converting MLS kernel policy to CIL. No changes to functionality, but eliminate useless checks for category aliases when using the p_cat_val_to_name array, find the actual number of aliases before allocating memory, and skip the category alias rules if there are no aliases.

---
 libsepol/src/kernel_to_cil.c | 59 ++++++++++++++++++------------------
 1 file changed, 29 insertions(+), 30 deletions(-)
libsepol/src/kernel_to_cil.c +29 -30
index 6103c1a6..319f3641 100644
@@ -886,6 +886,17 @@ exit:
 	return rc;
 }
 
+static int map_count_category_aliases(char *key, void *data, void *args)
+{
+	cat_datum_t *cat = data;
+	unsigned *count = args;
+
+	if (cat->isalias)
+		(*count)++;
+
+	return SEPOL_OK;
+}
+
 static int map_category_aliases_to_strs(char *key, void *data, void *args)
 {
 	cat_datum_t *cat = data;
@@ -903,26 +914,13 @@ static int write_category_rules_to_cil(FILE *out, struct policydb *pdb)
 {
 	cat_datum_t *cat;
 	char *prev, *name, *actual;
-	struct strs *strs;
-	unsigned i, num;
+	struct strs *strs = NULL;
+	unsigned i, num = 0;
 	int rc = 0;
 
-	rc = strs_init(&strs, pdb->p_levels.nprim);
-	if (rc != 0) {
-		goto exit;
-	}
-
 	/* categories */
 	for (i=0; i < pdb->p_cats.nprim; i++) {
 		name = pdb->p_cat_val_to_name[i];
-		if (!name) continue;
-		cat = hashtab_search(pdb->p_cats.table, name);
-		if (!cat) {
-			rc = -1;
-			goto exit;
-		}
-		if (cat->isalias) continue;
-
 		sepol_printf(out, "(category %s)\n", name);
 	}
 
@@ -931,14 +929,6 @@ static int write_category_rules_to_cil(FILE *out, struct policydb *pdb)
 	prev = NULL;
 	for (i=0; i < pdb->p_cats.nprim; i++) {
 		name = pdb->p_cat_val_to_name[i];
-		if (!name) continue;
-		cat = hashtab_search(pdb->p_cats.table, name);
-		if (!cat) {
-			rc = -1;
-			goto exit;
-		}
-		if (cat->isalias) continue;
-
 		if (prev) {
 			sepol_printf(out, "%s ", prev);
 		}
@@ -949,6 +939,22 @@ static int write_category_rules_to_cil(FILE *out, struct policydb *pdb)
 	}
 	sepol_printf(out, "))\n");
 
+	rc = hashtab_map(pdb->p_cats.table, map_count_category_aliases, &num);
+	if (rc != 0) {
+		goto exit;
+	}
+
+	if (num == 0) {
+		/* No aliases, so skip category alias rules */
+		rc = 0;
+		goto exit;
+	}
+
+	rc = strs_init(&strs, num);
+	if (rc != 0) {
+		goto exit;
+	}
+
 	rc = hashtab_map(pdb->p_cats.table, map_category_aliases_to_strs, strs);
 	if (rc != 0) {
 		goto exit;
@@ -956,16 +962,9 @@ static int write_category_rules_to_cil(FILE *out, struct policydb *pdb)
 
 	strs_sort(strs);
 
-	num = strs_num_items(strs);
-
 	/* category aliases */
 	for (i=0; i < num; i++) {
 		name = strs_read_at_index(strs, i);
-		cat = hashtab_search(pdb->p_cats.table, name);
-		if (!cat) {
-			rc = -1;
-			goto exit;
-		}
 		sepol_printf(out, "(categoryalias %s)\n", name);
 	}
 
Signature · 1 line
2.25.4