Skip to contentSkip to conversation

libsepol: validate some object contexts

PATCH In review Christian Göttsche 2 participants 7 messages May 12, 2023 – Jun 5, 2023 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/5 libsepol: validate some object contexts

Ensure various object context entries have a name, since they are duplicated via strdup(3), and the order for ports and memory regions is valid.

---
 libsepol/src/policydb_validate.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
libsepol/src/policydb_validate.c +30 -0
index 301aa200..e0d290ff 100644
@@ -1149,6 +1149,8 @@ static int validate_ocontexts(sepol_handle_t *handle, const policydb_t *p, valid
 				case OCON_NETIF:
 					if (validate_context(&octx->context[1], flavors, p->mls))
 						goto bad;
+					if (!octx->u.name)
+						goto bad;
 					break;
 				case OCON_PORT:
 					if (octx->u.port.low_port > octx->u.port.high_port)
@@ -1163,6 +1165,34 @@ static int validate_ocontexts(sepol_handle_t *handle, const policydb_t *p, valid
 					default:
 						goto bad;
 					}
+					if (!octx->u.name)
+						goto bad;
+					break;
+				case OCON_IBPKEY:
+					if (octx->u.ibpkey.low_pkey > octx->u.ibpkey.high_pkey)
+						goto bad;
+					break;
+				case OCON_IBENDPORT:
+					if (!octx->u.ibendport.dev_name)
+						goto bad;
+					break;
+				}
+			} else if (p->target_platform == SEPOL_TARGET_XEN) {
+				switch(i) {
+				case OCON_XEN_IOPORT:
+					if (octx->u.ioport.low_ioport > octx->u.ioport.high_ioport)
+						goto bad;
+					break;
+				case OCON_XEN_IOMEM:
+					if (octx->u.iomem.low_iomem > octx->u.iomem.high_iomem)
+						goto bad;
+					if (p->policyvers < POLICYDB_VERSION_XEN_DEVICETREE && octx->u.iomem.high_iomem > 0xFFFFFFFFULL)
+						goto bad;
+					break;
+				case OCON_XEN_DEVICETREE:
+					if (!octx->u.name)
+						goto bad;
+					break;
 				}
 			}
 		}
Signature · 1 line
2.40.1
#1 lore raw

PATCH 2/5 libsepol: validate old style range trans classes

For old style range transition rules the class defaults to process. However the policy might not declare the process class leading to setting a wrong bit later on via:

    if (ebitmap_set_bit(&rtr->tclasses, rt->target_class - 1, 1))

UBSAN report:

policydb.c:3684:56: runtime error: unsigned integer overflow: 0 - 1 cannot be represented in type 'uint32_t' (aka 'unsigned int')

---
 libsepol/src/policydb.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
libsepol/src/policydb.c +2 -2
index b79c19b9..605d290a 100644
@@ -3650,10 +3650,10 @@ static int range_read(policydb_t * p, struct policy_file *fp)
 			if (rc < 0)
 				goto err;
 			rt->target_class = le32_to_cpu(buf[0]);
-			if (!value_isvalid(rt->target_class, p->p_classes.nprim))
-				goto err;
 		} else
 			rt->target_class = p->process_class;
+		if (!value_isvalid(rt->target_class, p->p_classes.nprim))
+			goto err;
 		r = calloc(1, sizeof(*r));
 		if (!r)
 			goto err;
Signature · 1 line
2.40.1
#2 lore raw

PATCH 3/5 libsepol: validate: check low category is not bigger than high

In reply to Christian Göttsche
---
 libsepol/src/policydb_validate.c | 2 ++
 1 file changed, 2 insertions(+)
libsepol/src/policydb_validate.c +2 -0
index e0d290ff..b34f83ec 100644
@@ -545,6 +545,8 @@ static int validate_mls_semantic_cat(const mls_semantic_cat_t *cat, const valida
 			goto bad;
 		if (validate_value(cat->high, cats))
 			goto bad;
+		if (cat->low > cat->high)
+			goto bad;
 	}
 
 	return 0;
Signature · 1 line
2.40.1
#3 lore raw

PATCH 4/5 libsepol: validate: reject XEN policy with xperm rules

In reply to Christian Göttsche

XEN policies with extended permissions are not supported, e.g. writing them will fail (see write.c:avrule_write()).

---
 libsepol/src/policydb_validate.c | 4 ++++
 1 file changed, 4 insertions(+)
libsepol/src/policydb_validate.c +4 -0
index b34f83ec..3540f34a 100644
@@ -799,6 +799,8 @@ static int validate_avtab_key(const avtab_key_t *key, int conditional, const pol
 	case AVTAB_XPERMS_ALLOWED:
 	case AVTAB_XPERMS_AUDITALLOW:
 	case AVTAB_XPERMS_DONTAUDIT:
+		if (p->target_platform != SEPOL_TARGET_SELINUX)
+			goto bad;
 		if (conditional)
 			goto bad;
 		break;
@@ -910,6 +912,8 @@ static int validate_avrules(sepol_handle_t *handle, const avrule_t *avrule, int
 		}
 
 		if (avrule->specified & AVRULE_XPERMS) {
+			if (p->target_platform != SEPOL_TARGET_SELINUX)
+				goto bad;
 			if (!avrule->xperms)
 				goto bad;
 			switch (avrule->xperms->specified) {
Signature · 1 line
2.40.1
#4 lore raw

PATCH 5/5 libsepol: expand: skip invalid cat

In reply to Christian Göttsche

Bail out on expanding levels with invalid low category.

UBSAN report:

expand.c:952:21: runtime error: unsigned integer overflow: 0 - 1 cannot be represented in type 'uint32_t' (aka 'unsigned int')

---
 libsepol/src/expand.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
libsepol/src/expand.c +1 -1
index c08d3a35..8795229a 100644
@@ -943,7 +943,7 @@ 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->high) {
+		if (!cat->low || cat->low > cat->high) {
 			ERR(h, "Category range is not valid %s.%s",
 			    p->p_cat_val_to_name[cat->low - 1],
 			    p->p_cat_val_to_name[cat->high - 1]);
Signature · 1 line
2.40.1
Acked-by on v1 1/5
#5 lore raw
In reply to Christian Göttsche
Quoted 6 lines · Christian Göttsche

Ensure various object context entries have a name, since they are duplicated via strdup(3), and the order for ports and memory regions is valid.

For these five patches:

Quoted 55 lines
---
 libsepol/src/policydb_validate.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
libsepol/src/policydb_validate.c +30 -1
index 301aa200..e0d290ff 100644
@@ -1149,6 +1149,8 @@ static int validate_ocontexts(sepol_handle_t *handle, const policydb_t *p, valid
                                case OCON_NETIF:
                                        if (validate_context(&octx->context[1], flavors, p->mls))
                                                goto bad;
+                                       if (!octx->u.name)
+                                               goto bad;
                                        break;
                                case OCON_PORT:
                                        if (octx->u.port.low_port > octx->u.port.high_port)
@@ -1163,6 +1165,34 @@ static int validate_ocontexts(sepol_handle_t *handle, const policydb_t *p, valid
                                        default:
                                                goto bad;
                                        }
+                                       if (!octx->u.name)
+                                               goto bad;
+                                       break;
+                               case OCON_IBPKEY:
+                                       if (octx->u.ibpkey.low_pkey > octx->u.ibpkey.high_pkey)
+                                               goto bad;
+                                       break;
+                               case OCON_IBENDPORT:
+                                       if (!octx->u.ibendport.dev_name)
+                                               goto bad;
+                                       break;
+                               }
+                       } else if (p->target_platform == SEPOL_TARGET_XEN) {
+                               switch(i) {
+                               case OCON_XEN_IOPORT:
+                                       if (octx->u.ioport.low_ioport > octx->u.ioport.high_ioport)
+                                               goto bad;
+                                       break;
+                               case OCON_XEN_IOMEM:
+                                       if (octx->u.iomem.low_iomem > octx->u.iomem.high_iomem)
+                                               goto bad;
+                                       if (p->policyvers < POLICYDB_VERSION_XEN_DEVICETREE && octx->u.iomem.high_iomem > 0xFFFFFFFFULL)
+                                               goto bad;
+                                       break;
+                               case OCON_XEN_DEVICETREE:
+                                       if (!octx->u.name)
+                                               goto bad;
+                                       break;
                                }
                        }
                }
--

2.40.1

#6 lore raw
Quoted 4 lines · James Carter
Quoted 1 line · Christian Göttsche
Quoted 9 lines
Quoted 5 lines

Ensure various object context entries have a name, since they are duplicated via strdup(3), and the order for ports and memory regions is valid.

For these five patches:

These five patches have been merged.
Thanks,
Jim

Quoted 55 lines
Quoted 55 lines
---
 libsepol/src/policydb_validate.c | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
libsepol/src/policydb_validate.c +30 -1
index 301aa200..e0d290ff 100644
@@ -1149,6 +1149,8 @@ static int validate_ocontexts(sepol_handle_t *handle, const policydb_t *p, valid
                                case OCON_NETIF:
                                        if (validate_context(&octx->context[1], flavors, p->mls))
                                                goto bad;
+                                       if (!octx->u.name)
+                                               goto bad;
                                        break;
                                case OCON_PORT:
                                        if (octx->u.port.low_port > octx->u.port.high_port)
@@ -1163,6 +1165,34 @@ static int validate_ocontexts(sepol_handle_t *handle, const policydb_t *p, valid
                                        default:
                                                goto bad;
                                        }
+                                       if (!octx->u.name)
+                                               goto bad;
+                                       break;
+                               case OCON_IBPKEY:
+                                       if (octx->u.ibpkey.low_pkey > octx->u.ibpkey.high_pkey)
+                                               goto bad;
+                                       break;
+                               case OCON_IBENDPORT:
+                                       if (!octx->u.ibendport.dev_name)
+                                               goto bad;
+                                       break;
+                               }
+                       } else if (p->target_platform == SEPOL_TARGET_XEN) {
+                               switch(i) {
+                               case OCON_XEN_IOPORT:
+                                       if (octx->u.ioport.low_ioport > octx->u.ioport.high_ioport)
+                                               goto bad;
+                                       break;
+                               case OCON_XEN_IOMEM:
+                                       if (octx->u.iomem.low_iomem > octx->u.iomem.high_iomem)
+                                               goto bad;
+                                       if (p->policyvers < POLICYDB_VERSION_XEN_DEVICETREE && octx->u.iomem.high_iomem > 0xFFFFFFFFULL)
+                                               goto bad;
+                                       break;
+                               case OCON_XEN_DEVICETREE:
+                                       if (!octx->u.name)
+                                               goto bad;
+                                       break;
                                }
                        }
                }
--

2.40.1