Skip to contentSkip to conversation

libsepol: check the number of elements in the avtab

PATCH In review Rahul Sandhu 2 participants 3 messages May 28 – Jun 2 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol: check the number of elements in the avtab

Instead of counting each time we semantically see an AV rule, check the number of elements in the access vector table. AV rules may collapse to nothing, for example:

(class foo (bar baz))
(classorder (foo))

(allow t self (foo (not (bar baz))))

Previously, the compiler would incorrectly count the above rule as one AV rule, when in reality it collapses down to nothing as no permissions are actually granted.

---
 libsepol/cil/src/cil_binary.c | 6 ++++++
 libsepol/cil/src/cil_post.c   | 8 --------
 libsepol/cil/src/cil_verify.c | 6 ------
 libsepol/cil/src/cil_verify.h | 1 -
 4 files changed, 6 insertions(+), 15 deletions(-)
libsepol/cil/src/cil_binary.c +6 -0
index 74ec3cd5..dca1937a 100644
@@ -5468,6 +5468,12 @@ int cil_binary_create_allocated_pdb(const struct cil_db *db, sepol_policydb_t *p
 		goto exit;
 	}
 
+	if (pdb->te_avtab.nel == 0) {
+		cil_log(CIL_ERR, "Policy must include at least one avrule\n");
+		rc = SEPOL_ERR;
+		goto exit;
+	}
+
 	rc = SEPOL_OK;
 
 exit:
libsepol/cil/src/cil_post.c +0 -8
index 2b6dad73..fa59d337 100644
@@ -2506,7 +2506,6 @@ exit:
 static int cil_post_verify(struct cil_db *db)
 {
 	int rc = SEPOL_ERR;
-	int avrule_cnt = 0;
 	int handleunknown = -1;
 	int mls = -1;
 	int nseuserdflt = 0;
@@ -2518,7 +2517,6 @@ static int cil_post_verify(struct cil_db *db)
 
 	extra_args.db = db;
 	extra_args.csymtab = &csymtab;
-	extra_args.avrule_cnt = &avrule_cnt;
 	extra_args.handleunknown = &handleunknown;
 	extra_args.mls = &mls;
 	extra_args.nseuserdflt = &nseuserdflt;
@@ -2548,12 +2546,6 @@ static int cil_post_verify(struct cil_db *db)
 		}
 	}
 
-	if (avrule_cnt == 0) {
-		cil_log(CIL_ERR, "Policy must include at least one avrule\n");
-		rc = SEPOL_ERR;
-		goto exit;
-	}
-
 	if (nseuserdflt > 1) {
 		cil_log(CIL_ERR, "Policy cannot contain more than one selinuxuserdefault, found: %d\n", nseuserdflt);
 		rc = SEPOL_ERR;
libsepol/cil/src/cil_verify.c +0 -6
index 9654d71d..d3c4fd81 100644
@@ -1621,7 +1621,6 @@ exit:
 int __cil_verify_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
 {
 	int rc = SEPOL_ERR;
-	int *avrule_cnt = 0;
 	int *handleunknown;
 	int *mls;
 	int *nseuserdflt = 0;
@@ -1635,7 +1634,6 @@ int __cil_verify_helper(struct cil_tree_node *node, uint32_t *finished, void *ex
 	}
 
 	db = args->db;
-	avrule_cnt = args->avrule_cnt;
 	handleunknown = args->handleunknown;
 	mls = args->mls;
 	nseuserdflt = args->nseuserdflt;
@@ -1671,10 +1669,6 @@ int __cil_verify_helper(struct cil_tree_node *node, uint32_t *finished, void *ex
 		case CIL_TYPE:
 			rc = __cil_verify_type(node);
 			break;
-		case CIL_AVRULE:
-			(*avrule_cnt)++;
-			rc = SEPOL_OK;
-			break;
 		case CIL_HANDLEUNKNOWN:
 			if (*handleunknown != -1) {
 				cil_log(CIL_ERR, "Policy can not have more than one handleunknown\n");
libsepol/cil/src/cil_verify.h +0 -1
index daa2f80a..f86d07ed 100644
@@ -49,7 +49,6 @@ enum cil_syntax {
 struct cil_args_verify {
 	struct cil_db *db;
 	struct cil_complex_symtab *csymtab;
-	int *avrule_cnt;
 	int *handleunknown;
 	int *mls;
 	int *nseuserdflt;
Signature · 1 line
2.54.0
Acked-by on v1 1/1
#1 lore raw
Quoted 8 lines · Rahul Sandhu

Instead of counting each time we semantically see an AV rule, check the number of elements in the access vector table. AV rules may collapse to nothing, for example:

(class foo (bar baz))
(classorder (foo))

Quoted 7 lines

(allow t self (foo (not (bar baz))))

Previously, the compiler would incorrectly count the above rule as one AV rule, when in reality it collapses down to nothing as no permissions are actually granted.

Quoted 104 lines
---
 libsepol/cil/src/cil_binary.c | 6 ++++++
 libsepol/cil/src/cil_post.c   | 8 --------
 libsepol/cil/src/cil_verify.c | 6 ------
 libsepol/cil/src/cil_verify.h | 1 -
 4 files changed, 6 insertions(+), 15 deletions(-)
libsepol/cil/src/cil_binary.c +6 -0
index 74ec3cd5..dca1937a 100644
@@ -5468,6 +5468,12 @@ int cil_binary_create_allocated_pdb(const struct cil_db *db, sepol_policydb_t *p
                goto exit;
        }
 
+       if (pdb->te_avtab.nel == 0) {
+               cil_log(CIL_ERR, "Policy must include at least one avrule\n");
+               rc = SEPOL_ERR;
+               goto exit;
+       }
+
        rc = SEPOL_OK;
 
 exit:
libsepol/cil/src/cil_post.c +0 -8
index 2b6dad73..fa59d337 100644
@@ -2506,7 +2506,6 @@ exit:
 static int cil_post_verify(struct cil_db *db)
 {
        int rc = SEPOL_ERR;
-       int avrule_cnt = 0;
        int handleunknown = -1;
        int mls = -1;
        int nseuserdflt = 0;
@@ -2518,7 +2517,6 @@ static int cil_post_verify(struct cil_db *db)
 
        extra_args.db = db;
        extra_args.csymtab = &csymtab;
-       extra_args.avrule_cnt = &avrule_cnt;
        extra_args.handleunknown = &handleunknown;
        extra_args.mls = &mls;
        extra_args.nseuserdflt = &nseuserdflt;
@@ -2548,12 +2546,6 @@ static int cil_post_verify(struct cil_db *db)
                }
        }
 
-       if (avrule_cnt == 0) {
-               cil_log(CIL_ERR, "Policy must include at least one avrule\n");
-               rc = SEPOL_ERR;
-               goto exit;
-       }
-
        if (nseuserdflt > 1) {
                cil_log(CIL_ERR, "Policy cannot contain more than one selinuxuserdefault, found: %d\n", nseuserdflt);
                rc = SEPOL_ERR;
libsepol/cil/src/cil_verify.c +0 -6
index 9654d71d..d3c4fd81 100644
@@ -1621,7 +1621,6 @@ exit:
 int __cil_verify_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
 {
        int rc = SEPOL_ERR;
-       int *avrule_cnt = 0;
        int *handleunknown;
        int *mls;
        int *nseuserdflt = 0;
@@ -1635,7 +1634,6 @@ int __cil_verify_helper(struct cil_tree_node *node, uint32_t *finished, void *ex
        }
 
        db = args->db;
-       avrule_cnt = args->avrule_cnt;
        handleunknown = args->handleunknown;
        mls = args->mls;
        nseuserdflt = args->nseuserdflt;
@@ -1671,10 +1669,6 @@ int __cil_verify_helper(struct cil_tree_node *node, uint32_t *finished, void *ex
                case CIL_TYPE:
                        rc = __cil_verify_type(node);
                        break;
-               case CIL_AVRULE:
-                       (*avrule_cnt)++;
-                       rc = SEPOL_OK;
-                       break;
                case CIL_HANDLEUNKNOWN:
                        if (*handleunknown != -1) {
                                cil_log(CIL_ERR, "Policy can not have more than one handleunknown\n");
libsepol/cil/src/cil_verify.h +0 -2
index daa2f80a..f86d07ed 100644
@@ -49,7 +49,6 @@ enum cil_syntax {
 struct cil_args_verify {
        struct cil_db *db;
        struct cil_complex_symtab *csymtab;
-       int *avrule_cnt;
        int *handleunknown;
        int *mls;
        int *nseuserdflt;
--

2.54.0

#2 lore raw
Quoted 12 lines · James Carter
Quoted 10 lines · Rahul Sandhu

Instead of counting each time we semantically see an AV rule, check the number of elements in the access vector table. AV rules may collapse to nothing, for example:

(class foo (bar baz))
(classorder (foo))

(allow t self (foo (not (bar baz))))

Quoted 8 lines
Quoted 5 lines

Previously, the compiler would incorrectly count the above rule as one AV rule, when in reality it collapses down to nothing as no permissions are actually granted.

Merged.
Thanks,
Jim

Quoted 104 lines
Quoted 104 lines
---
 libsepol/cil/src/cil_binary.c | 6 ++++++
 libsepol/cil/src/cil_post.c   | 8 --------
 libsepol/cil/src/cil_verify.c | 6 ------
 libsepol/cil/src/cil_verify.h | 1 -
 4 files changed, 6 insertions(+), 15 deletions(-)
libsepol/cil/src/cil_binary.c +6 -0
index 74ec3cd5..dca1937a 100644
@@ -5468,6 +5468,12 @@ int cil_binary_create_allocated_pdb(const struct cil_db *db, sepol_policydb_t *p
                goto exit;
        }
 
+       if (pdb->te_avtab.nel == 0) {
+               cil_log(CIL_ERR, "Policy must include at least one avrule\n");
+               rc = SEPOL_ERR;
+               goto exit;
+       }
+
        rc = SEPOL_OK;
 
 exit:
libsepol/cil/src/cil_post.c +0 -8
index 2b6dad73..fa59d337 100644
@@ -2506,7 +2506,6 @@ exit:
 static int cil_post_verify(struct cil_db *db)
 {
        int rc = SEPOL_ERR;
-       int avrule_cnt = 0;
        int handleunknown = -1;
        int mls = -1;
        int nseuserdflt = 0;
@@ -2518,7 +2517,6 @@ static int cil_post_verify(struct cil_db *db)
 
        extra_args.db = db;
        extra_args.csymtab = &csymtab;
-       extra_args.avrule_cnt = &avrule_cnt;
        extra_args.handleunknown = &handleunknown;
        extra_args.mls = &mls;
        extra_args.nseuserdflt = &nseuserdflt;
@@ -2548,12 +2546,6 @@ static int cil_post_verify(struct cil_db *db)
                }
        }
 
-       if (avrule_cnt == 0) {
-               cil_log(CIL_ERR, "Policy must include at least one avrule\n");
-               rc = SEPOL_ERR;
-               goto exit;
-       }
-
        if (nseuserdflt > 1) {
                cil_log(CIL_ERR, "Policy cannot contain more than one selinuxuserdefault, found: %d\n", nseuserdflt);
                rc = SEPOL_ERR;
libsepol/cil/src/cil_verify.c +0 -6
index 9654d71d..d3c4fd81 100644
@@ -1621,7 +1621,6 @@ exit:
 int __cil_verify_helper(struct cil_tree_node *node, uint32_t *finished, void *extra_args)
 {
        int rc = SEPOL_ERR;
-       int *avrule_cnt = 0;
        int *handleunknown;
        int *mls;
        int *nseuserdflt = 0;
@@ -1635,7 +1634,6 @@ int __cil_verify_helper(struct cil_tree_node *node, uint32_t *finished, void *ex
        }
 
        db = args->db;
-       avrule_cnt = args->avrule_cnt;
        handleunknown = args->handleunknown;
        mls = args->mls;
        nseuserdflt = args->nseuserdflt;
@@ -1671,10 +1669,6 @@ int __cil_verify_helper(struct cil_tree_node *node, uint32_t *finished, void *ex
                case CIL_TYPE:
                        rc = __cil_verify_type(node);
                        break;
-               case CIL_AVRULE:
-                       (*avrule_cnt)++;
-                       rc = SEPOL_OK;
-                       break;
                case CIL_HANDLEUNKNOWN:
                        if (*handleunknown != -1) {
                                cil_log(CIL_ERR, "Policy can not have more than one handleunknown\n");
libsepol/cil/src/cil_verify.h +0 -2
index daa2f80a..f86d07ed 100644
@@ -49,7 +49,6 @@ enum cil_syntax {
 struct cil_args_verify {
        struct cil_db *db;
        struct cil_complex_symtab *csymtab;
-       int *avrule_cnt;
        int *handleunknown;
        int *mls;
        int *nseuserdflt;
--

2.54.0