Skip to contentSkip to conversation

libsepol,checkpolicy: update remaining policyvers comparisons to correctly handle Xen policies

PATCH In review Stephen Smalley 1 participant 2 messages Sep 9 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/1 libsepol,checkpolicy: update remaining policyvers comparisons to correctly handle Xen policies

Even after the previous two patches there were several residual policy version comparisons that failed to properly account for the difference between SELinux and Xen policies. These differences will only matter if/when Xen policy versions > 30 are introduced or if/when the minimum SELinux policy version is raised above 30 but we should go ahead and fix them now. The remaining policyvers comparisons that do not check target platform are only for modular policies, which are unused by Xen.

---
This patch depends on the previous two that introduce the split min/max policydb versions between SELinux and Xen.

 checkpolicy/fuzz/checkpolicy-fuzzer.c      |  8 +++++---
 libsepol/include/sepol/policydb/policydb.h | 11 ++++++-----
 libsepol/src/policydb.c                    | 18 +++++++++++++-----
 libsepol/src/policydb_validate.c           |  9 +++++++--
 libsepol/src/write.c                       | 12 +++++++-----
 5 files changed, 38 insertions(+), 20 deletions(-)
checkpolicy/fuzz/checkpolicy-fuzzer.c +5 -3
index f816d7b7..3465c28e 100644
@@ -197,11 +197,13 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
 	static_assert(0x7F - 'A' >= POLICYDB_VERSION_MAX,
 		      "Max policy version should be representable");
 	policyvers = data[2] - 'A';
-	if (policyvers < POLICYDB_VERSION_MIN ||
-	    policyvers > POLICYDB_VERSION_MAX)
+	if (platform == SEPOL_TARGET_SELINUX &&
+	    (policyvers < POLICYDB_VERSION_MIN ||
+	     policyvers > POLICYDB_VERSION_MAX))
 		return 0;
 	if (platform == SEPOL_TARGET_XEN &&
-	    policyvers != POLICYDB_VERSION_XEN_DEVICETREE)
+	    (policyvers < POLICYDB_XEN_VERSION_MIN ||
+	     policyvers > POLICYDB_XEN_VERSION_MAX))
 		return 0;
 #if VERBOSE
 	printf("policyvers=%d\n", policyvers);
libsepol/include/sepol/policydb/policydb.h +6 -5
index e116188c..bf948d90 100644
@@ -788,11 +788,12 @@ extern int policydb_set_target_platform(policydb_t *p, int platform);
 
 /* TODO: add other features here */
 
-#define policydb_has_cond_xperms_feature(p)                   \
-	(((p)->policy_type == POLICY_KERN &&                  \
-	  (p)->policyvers >= POLICYDB_VERSION_COND_XPERMS) || \
-	 ((p)->policy_type != POLICY_KERN &&                  \
-	  (p)->policyvers >= MOD_POLICYDB_VERSION_COND_XPERMS))
+#define policydb_has_cond_xperms_feature(p)                    \
+	((p)->target_platform == SEPOL_TARGET_SELINUX &&       \
+	 (((p)->policy_type == POLICY_KERN &&                  \
+	   (p)->policyvers >= POLICYDB_VERSION_COND_XPERMS) || \
+	  ((p)->policy_type != POLICY_KERN &&                  \
+	   (p)->policyvers >= MOD_POLICYDB_VERSION_COND_XPERMS)))
 
 /* the config flags related to unknown classes/perms are bits 2 and 3 */
 #define DENY_UNKNOWN SEPOL_DENY_UNKNOWN
libsepol/src/policydb.c +13 -5
index bd52a107..551bdad9 100644
@@ -2517,7 +2517,8 @@ static int filename_trans_read(policydb_t *p, struct policy_file *fp)
 		return -1;
 	nel = le32_to_cpu(buf[0]);
 
-	if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
+	if (p->target_platform == SEPOL_TARGET_SELINUX &&
+	    p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
 		for (i = 0; i < nel; i++) {
 			rc = filename_trans_read_one_compat(p, fp);
 			if (rc < 0)
@@ -3868,13 +3869,20 @@ int policydb_read(policydb_t *p, struct policy_file *fp, unsigned verbose)
 
 	r_policyvers = buf[bufindex];
 	if (policy_type == POLICY_KERN) {
-		if (r_policyvers < POLICYDB_VERSION_MIN ||
-		    r_policyvers > POLICYDB_VERSION_MAX) {
+		unsigned int policyvers_min = POLICYDB_VERSION_MIN;
+		unsigned int policyvers_max = POLICYDB_VERSION_MAX;
+
+		if (p->target_platform == SEPOL_TARGET_XEN) {
+			policyvers_min = POLICYDB_XEN_VERSION_MIN;
+			policyvers_max = POLICYDB_XEN_VERSION_MAX;
+		}
+
+		if (r_policyvers < policyvers_min ||
+		    r_policyvers > policyvers_max) {
 			ERR(fp->handle,
 			    "policydb version %d does not match "
 			    "my version range %d-%d",
-			    buf[bufindex], POLICYDB_VERSION_MIN,
-			    POLICYDB_VERSION_MAX);
+			    buf[bufindex], policyvers_min, policyvers_max);
 			return POLICYDB_ERROR;
 		}
 	} else if (policy_type == POLICY_BASE || policy_type == POLICY_MOD) {
libsepol/src/policydb_validate.c +7 -2
index be2cab05..8fe168f6 100644
@@ -2080,8 +2080,13 @@ static int validate_properties(sepol_handle_t *handle, const policydb_t *p)
 {
 	switch (p->policy_type) {
 	case POLICY_KERN:
-		if (p->policyvers < POLICYDB_VERSION_MIN ||
-		    p->policyvers > POLICYDB_VERSION_MAX)
+		if (p->target_platform == SEPOL_TARGET_SELINUX &&
+		    (p->policyvers < POLICYDB_VERSION_MIN ||
+		     p->policyvers > POLICYDB_VERSION_MAX))
+			goto bad;
+		if (p->target_platform == SEPOL_TARGET_XEN &&
+		    (p->policyvers < POLICYDB_XEN_VERSION_MIN ||
+		     p->policyvers > POLICYDB_XEN_VERSION_MAX))
 			goto bad;
 		break;
 	case POLICY_BASE:
libsepol/src/write.c +7 -5
index e08fd7a7..f2a7463a 100644
@@ -45,10 +45,11 @@
 #include "private.h"
 #include "mls.h"
 
-#define glblub_version                                 \
-	((p->policy_type == POLICY_KERN &&             \
-	  p->policyvers >= POLICYDB_VERSION_GLBLUB) || \
-	 (p->policy_type == POLICY_BASE &&             \
+#define glblub_version                                  \
+	((p->target_platform == SEPOL_TARGET_SELINUX && \
+	  p->policy_type == POLICY_KERN &&              \
+	  p->policyvers >= POLICYDB_VERSION_GLBLUB) ||  \
+	 (p->policy_type == POLICY_BASE &&              \
 	  p->policyvers >= MOD_POLICYDB_VERSION_GLBLUB))
 
 struct policy_data {
@@ -477,7 +478,8 @@ static int filename_trans_write(struct policydb *p, void *fp)
 	uint32_t buf[1];
 	int rc;
 
-	if (p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
+	if (p->target_platform == SEPOL_TARGET_SELINUX &&
+	    p->policyvers < POLICYDB_VERSION_COMP_FTRANS) {
 		buf[0] = cpu_to_le32(p->filename_trans_count);
 		items = put_entry(buf, sizeof(uint32_t), 1, fp);
 		if (items != 1)
Signature · 1 line
2.55.0
author maintainer
#1 lore raw
Quoted 11 lines · Stephen Smalley

Even after the previous two patches there were several residual policy version comparisons that failed to properly account for the difference between SELinux and Xen policies. These differences will only matter if/when Xen policy versions > 30 are introduced or if/when the minimum SELinux policy version is raised above 30 but we should go ahead and fix them now. The remaining policyvers comparisons that do not check target platform are only for modular policies, which are unused by Xen.

This has been merged.