Skip to contentSkip to conversation

libsepol: bound category values in mls_semantic_level_expand

PATCH In review Stephen Smalley 1 participant 3 messages Jun 17 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 libsepol: bound category values in mls_semantic_level_expand

From: netliomax25-code <[email protected]>

1. mls_semantic_level_expand() checks the sensitivity against p_levels.nprim but never bounds the per-category low/high values against p_cats.nprim.

2. For a base module the user MLS range and default level are read in semantic form with no bounds check, and policydb_read() expands them through policydb_index_others() -> policydb_user_cache() before policydb_validate() runs, so a crafted base policy with a category value past the declared count reads p_cat_val_to_name[] out of bounds, both in the "Category range is not valid" message and in the p_cat_val_to_name[i] reference inside the loop.

3. Reject cat->high > p_cats.nprim and guard the error-message indices with the same upper bound, matching the existing sensitivity check just above.

Confirmed under ASAN: a semantic level with a category low/high beyond the declared category count reports a heap-buffer-overflow read in mls_semantic_level_expand before the patch, and is rejected cleanly with an error after it.

---
 libsepol/src/expand.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
libsepol/src/expand.c +6 -4
index 7162f80a..0bd61c82 100644
@@ -1128,11 +1128,13 @@ int mls_semantic_level_expand(mls_semantic_level_t *sl, mls_level_t *l,
 		return -1;
 	}
 	for (cat = sl->cat; cat; cat = cat->next) {
-		if (!cat->low || cat->low > cat->high) {
+		if (!cat->low || cat->low > cat->high ||
+		    cat->high > p->p_cats.nprim) {
 			ERR(h, "Category range is not valid %s.%s",
-			    cat->low > 0 ? p->p_cat_val_to_name[cat->low - 1] :
-					   "Invalid",
-			    cat->high > 0 ?
+			    (cat->low && cat->low <= p->p_cats.nprim) ?
+				    p->p_cat_val_to_name[cat->low - 1] :
+				    "Invalid",
+			    (cat->high && cat->high <= p->p_cats.nprim) ?
 				    p->p_cat_val_to_name[cat->high - 1] :
 				    "Invalid");
 			return -1;
Signature · 1 line
2.54.0
author maintainer Acked-by on v1 1/1
#1 lore raw
Quoted 19 lines · Stephen Smalley

From: netliomax25-code <[email protected]>

1. mls_semantic_level_expand() checks the sensitivity against p_levels.nprim but never bounds the per-category low/high values against p_cats.nprim.

2. For a base module the user MLS range and default level are read in semantic form with no bounds check, and policydb_read() expands them through policydb_index_others() -> policydb_user_cache() before policydb_validate() runs, so a crafted base policy with a category value past the declared count reads p_cat_val_to_name[] out of bounds, both in the "Category range is not valid" message and in the p_cat_val_to_name[i] reference inside the loop.

3. Reject cat->high > p_cats.nprim and guard the error-message indices with the same upper bound, matching the existing sensitivity check just above.

Quoted 7 lines

Confirmed under ASAN: a semantic level with a category low/high beyond the declared category count reports a heap-buffer-overflow read in mls_semantic_level_expand before the patch, and is rejected cleanly with an error after it.

Quoted 29 lines
---
 libsepol/src/expand.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
libsepol/src/expand.c +6 -5
index 7162f80a..0bd61c82 100644
@@ -1128,11 +1128,13 @@ int mls_semantic_level_expand(mls_semantic_level_t *sl, mls_level_t *l,
                return -1;
        }
        for (cat = sl->cat; cat; cat = cat->next) {
-               if (!cat->low || cat->low > cat->high) {
+               if (!cat->low || cat->low > cat->high ||
+                   cat->high > p->p_cats.nprim) {
                        ERR(h, "Category range is not valid %s.%s",
-                           cat->low > 0 ? p->p_cat_val_to_name[cat->low - 1] :
-                                          "Invalid",
-                           cat->high > 0 ?
+                           (cat->low && cat->low <= p->p_cats.nprim) ?
+                                   p->p_cat_val_to_name[cat->low - 1] :
+                                   "Invalid",
+                           (cat->high && cat->high <= p->p_cats.nprim) ?
                                    p->p_cat_val_to_name[cat->high - 1] :
                                    "Invalid");
                        return -1;
--

2.54.0

author maintainer
#2 lore raw
Quoted 27 lines · Stephen Smalley
Quoted 24 lines · Stephen Smalley

From: netliomax25-code <[email protected]>

1. mls_semantic_level_expand() checks the sensitivity against p_levels.nprim but never bounds the per-category low/high values against p_cats.nprim.

2. For a base module the user MLS range and default level are read in semantic form with no bounds check, and policydb_read() expands them through policydb_index_others() -> policydb_user_cache() before policydb_validate() runs, so a crafted base policy with a category value past the declared count reads p_cat_val_to_name[] out of bounds, both in the "Category range is not valid" message and in the p_cat_val_to_name[i] reference inside the loop.

3. Reject cat->high > p_cats.nprim and guard the error-message indices with the same upper bound, matching the existing sensitivity check just above.

Confirmed under ASAN: a semantic level with a category low/high beyond the declared category count reports a heap-buffer-overflow read in mls_semantic_level_expand before the patch, and is rejected cleanly with an error after it.

Quoted 4 lines
Quoted 2 lines

Merged.

Quoted 30 lines
Quoted 30 lines
---
 libsepol/src/expand.c | 10 ++++++----
 1 file changed, 6 insertions(+), 4 deletions(-)
libsepol/src/expand.c +6 -5
index 7162f80a..0bd61c82 100644
@@ -1128,11 +1128,13 @@ int mls_semantic_level_expand(mls_semantic_level_t *sl, mls_level_t *l,
                return -1;
        }
        for (cat = sl->cat; cat; cat = cat->next) {
-               if (!cat->low || cat->low > cat->high) {
+               if (!cat->low || cat->low > cat->high ||
+                   cat->high > p->p_cats.nprim) {
                        ERR(h, "Category range is not valid %s.%s",
-                           cat->low > 0 ? p->p_cat_val_to_name[cat->low - 1] :
-                                          "Invalid",
-                           cat->high > 0 ?
+                           (cat->low && cat->low <= p->p_cats.nprim) ?
+                                   p->p_cat_val_to_name[cat->low - 1] :
+                                   "Invalid",
+                           (cat->high && cat->high <= p->p_cats.nprim) ?
                                    p->p_cat_val_to_name[cat->high - 1] :
                                    "Invalid");
                        return -1;
--

2.54.0