Skip to contentSkip to conversation

libsepol,checkpolicy,secilc: split min/max policydb versions between SELinux and Xen

PATCH In review Stephen Smalley 1 participant 3 messages Sep 8 – Sep 9 SELinux security module

lore
Share & tools
lore

Stephen Smalley

author maintainer
#0 lore raw

PATCH 1/2 libsepol,checkpolicy,secilc: split min/max policydb versions between SELinux and Xen

checkpolicy and secilc default the output policy version to POLICYDB_VERSION_MAX (currently 35) regardless of the target platform, and check any manually specified policy version against the POLICYDB_VERSION_MIN/MAX again regardless of the target platform. As a consequence, checkpolicy or secilc with -t xen and no -c 30 option produces a version 35 policy for the Xen target that cannot be used by Xen.

Add POLICYDB_XEN_VERSION_{MIN,MAX} to policydb.h and, in both front ends, resolve the default policyvers and any -c checks against the range for the selected target after option parsing (so option order does not matter).

---
 checkpolicy/checkpolicy.c                  | 41 ++++++++++++----------
 libsepol/include/sepol/policydb/policydb.h |  8 ++++-
 secilc/secilc.c                            | 28 +++++++++------
 3 files changed, 47 insertions(+), 30 deletions(-)
checkpolicy/checkpolicy.c +22 -19
index b5a6da3a..1ab9598b 100644
@@ -60,6 +60,7 @@
 
 #include <ctype.h>
 #include <getopt.h>
+#include <limits.h>
 #include <unistd.h>
 #include <stdlib.h>
 #include <sys/types.h>
@@ -393,6 +394,8 @@ int main(int argc, char **argv)
 	struct val_to_name v;
 	int ret, ch, fd, target = SEPOL_TARGET_SELINUX;
 	unsigned int policyvers = 0;
+	unsigned int policyvers_min = POLICYDB_VERSION_MIN;
+	unsigned int policyvers_max = POLICYDB_VERSION_MAX;
 	unsigned int nel, uret;
 	struct stat sb;
 	void *map;
@@ -488,22 +491,13 @@ int main(int argc, char **argv)
 			long int n;
 			errno = 0;
 			n = strtol(optarg, NULL, 10);
-			if (errno) {
+			if (errno || n < 1 || n > INT_MAX) {
 				fprintf(stderr,
 					"Invalid policyvers specified: %s\n",
 					optarg);
 				usage(argv[0]);
 				exit(1);
 			}
-			if (n < POLICYDB_VERSION_MIN ||
-			    n > POLICYDB_VERSION_MAX) {
-				fprintf(stderr,
-					"policyvers value %ld not in range %d-%d\n",
-					n, POLICYDB_VERSION_MIN,
-					POLICYDB_VERSION_MAX);
-				usage(argv[0]);
-				exit(1);
-			}
 			policyvers = n;
 			break;
 		}
@@ -519,10 +513,22 @@ int main(int argc, char **argv)
 		}
 	}
 
+	if (target == SEPOL_TARGET_XEN) {
+		policyvers_min = POLICYDB_XEN_VERSION_MIN;
+		policyvers_max = POLICYDB_XEN_VERSION_MAX;
+	}
+
+	if (!policyvers)
+		policyvers = policyvers_max;
+	else if (policyvers < policyvers_min || policyvers > policyvers_max) {
+		fprintf(stderr, "policyvers value %u not in range %u-%u\n",
+			policyvers, policyvers_min, policyvers_max);
+		usage(argv[0]);
+	}
+
 	if (show_version) {
-		printf("%d (compatibility range %d-%d)\n",
-		       policyvers ? policyvers : POLICYDB_VERSION_MAX,
-		       POLICYDB_VERSION_MAX, POLICYDB_VERSION_MIN);
+		printf("%u (compatibility range %u-%u)\n", policyvers,
+		       policyvers_max, policyvers_min);
 		exit(0);
 	}
 
@@ -605,8 +611,7 @@ int main(int argc, char **argv)
 			}
 		}
 
-		policydbp->policyvers = policyvers ? policyvers :
-						     POLICYDB_VERSION_MAX;
+		policydbp->policyvers = policyvers;
 	} else {
 		if (conf) {
 			fprintf(stderr,
@@ -622,8 +627,7 @@ int main(int argc, char **argv)
 		/* Let sepol know if we are dealing with MLS support */
 		parse_policy.mls = mlspol;
 		parse_policy.handle_unknown = handle_unknown;
-		parse_policy.policyvers = policyvers ? policyvers :
-						       POLICYDB_VERSION_MAX;
+		parse_policy.policyvers = policyvers;
 
 		policydbp = &parse_policy;
 
@@ -651,8 +655,7 @@ int main(int argc, char **argv)
 					"Error while expanding policy\n");
 				exit(1);
 			}
-			policydb.policyvers = policyvers ? policyvers :
-							   POLICYDB_VERSION_MAX;
+			policydb.policyvers = policyvers;
 			policydb_destroy(policydbp);
 			policydbp = &policydb;
 		}
libsepol/include/sepol/policydb/policydb.h +7 -1
index 03831fc2..e116188c 100644
@@ -760,10 +760,16 @@ extern int policydb_set_target_platform(policydb_t *p, int platform);
 	34 /* extended permissions in conditional policies */
 #define POLICYDB_VERSION_NEVERAUDIT 35 /* neveraudit domains */
 
-/* Range of policy versions we understand*/
+/* Range of SELinux policy versions we understand*/
 #define POLICYDB_VERSION_MIN POLICYDB_VERSION_XPERMS_IOCTL
 #define POLICYDB_VERSION_MAX POLICYDB_VERSION_NEVERAUDIT
 
+/*
+ * Range of Xen policy versions we understand.
+ */
+#define POLICYDB_XEN_VERSION_MIN POLICYDB_VERSION_XEN_DEVICETREE
+#define POLICYDB_XEN_VERSION_MAX POLICYDB_VERSION_XEN_DEVICETREE
+
 /* Module versions and specific changes*/
 #define MOD_POLICYDB_VERSION_XPERMS_IOCTL 18
 #define MOD_POLICYDB_VERSION_INFINIBAND 19
secilc/secilc.c +18 -10
index 54a15767..cc528432 100644
@@ -100,7 +100,9 @@ int main(int argc, char *argv[])
 	int preserve_tunables = 0;
 	int qualified_names = 0;
 	int handle_unknown = -1;
-	int policyvers = POLICYDB_VERSION_MAX;
+	int policyvers = 0;
+	int policyvers_min = POLICYDB_VERSION_MIN;
+	int policyvers_max = POLICYDB_VERSION_MAX;
 	int attrs_expand_generated = 0;
 	int attrs_expand_size = -1;
 	int optimize = 0;
@@ -165,19 +167,12 @@ int main(int argc, char *argv[])
 			char *endptr = NULL;
 			errno = 0;
 			policyvers = strtol(optarg, &endptr, 10);
-			if (errno != 0 || endptr == optarg || *endptr != '\0') {
+			if (errno != 0 || endptr == optarg || *endptr != '\0' ||
+			    policyvers < 1) {
 				fprintf(stderr, "Bad policy version: %s\n",
 					optarg);
 				usage(argv[0]);
 			}
-			if (policyvers > POLICYDB_VERSION_MAX ||
-			    policyvers < POLICYDB_VERSION_MIN) {
-				fprintf(stderr,
-					"Policy version must be between %d and %d\n",
-					POLICYDB_VERSION_MIN,
-					POLICYDB_VERSION_MAX);
-				usage(argv[0]);
-			}
 			break;
 		}
 		case 'U':
@@ -252,6 +247,19 @@ int main(int argc, char *argv[])
 		usage(argv[0]);
 	}
 
+	if (target == SEPOL_TARGET_XEN) {
+		policyvers_min = POLICYDB_XEN_VERSION_MIN;
+		policyvers_max = POLICYDB_XEN_VERSION_MAX;
+	}
+
+	if (!policyvers)
+		policyvers = policyvers_max;
+	else if (policyvers < policyvers_min || policyvers > policyvers_max) {
+		fprintf(stderr, "Policy version must be between %d and %d\n",
+			policyvers_min, policyvers_max);
+		usage(argv[0]);
+	}
+
 	cil_set_log_level(log_level);
 
 	cil_db_init(&db);
Signature · 1 line
2.55.0
author maintainer
#1 lore raw

PATCH 2/2 libsepol: only read/write the neveraudit map for SELinux policies

The neveraudit type map added at POLICYDB_VERSION_NEVERAUDIT is a SELinux-only feature, so fix the checking in policydb_read() and policydb_write() to correctly check and handle the case where a different target platform (e.g. Xen) is selected. While we are here, replace the ebitmap_for_each_positive_bit() loop with the ebitmap_is_empty() helper.

---
 libsepol/src/policydb.c |  3 ++-
 libsepol/src/write.c    | 15 ++++++++-------
 2 files changed, 10 insertions(+), 8 deletions(-)
libsepol/src/policydb.c +2 -1
index e243c3ca..bd52a107 100644
@@ -3954,7 +3954,8 @@ int policydb_read(policydb_t *p, struct policy_file *fp, unsigned verbose)
 	}
 
 	if (p->policyvers >= POLICYDB_VERSION_NEVERAUDIT &&
-	    p->policy_type == POLICY_KERN) {
+	    p->policy_type == POLICY_KERN &&
+	    p->target_platform == SEPOL_TARGET_SELINUX) {
 		if (ebitmap_read(&p->neveraudit_map, fp))
 			goto bad;
 	}
libsepol/src/write.c +8 -7
index 975b3ef2..e08fd7a7 100644
@@ -1938,17 +1938,18 @@ int policydb_write(policydb_t *p, struct policy_file *fp)
 		if (ebitmap_write(&p->permissive_map, fp) == -1)
 			return POLICYDB_ERROR;
 
-		if (p->policyvers < POLICYDB_VERSION_NEVERAUDIT) {
-			ebitmap_node_t *tnode;
-
-			ebitmap_for_each_positive_bit(&p->neveraudit_map, tnode,
-						      i) {
+		if (p->target_platform != SEPOL_TARGET_SELINUX) {
+			if (!ebitmap_is_empty(&p->neveraudit_map))
+				WARN(fp->handle,
+				     "Warning! Target platform %s does not "
+				     "support neveraudit types, but some were defined",
+				     policydb_target_strings[p->target_platform]);
+		} else if (p->policyvers < POLICYDB_VERSION_NEVERAUDIT) {
+			if (!ebitmap_is_empty(&p->neveraudit_map))
 				WARN(fp->handle,
 				     "Warning! Policy version %d cannot "
 				     "support neveraudit types, but some were defined",
 				     p->policyvers);
-				break;
-			}
 		} else if (ebitmap_write(&p->neveraudit_map, fp) == -1)
 			return POLICYDB_ERROR;
 	}
Signature · 1 line
2.55.0
author maintainer
#2 lore raw
In reply to Stephen Smalley
Quoted 9 lines · Stephen Smalley

checkpolicy and secilc default the output policy version to POLICYDB_VERSION_MAX (currently 35) regardless of the target platform, and check any manually specified policy version against the POLICYDB_VERSION_MIN/MAX again regardless of the target platform. As a consequence, checkpolicy or secilc with -t xen and no -c 30 option produces a version 35 policy for the Xen target that cannot be used by Xen.

Quoted 7 lines

Add POLICYDB_XEN_VERSION_{MIN,MAX} to policydb.h and, in both front ends, resolve the default policyvers and any -c checks against the range for the selected target after option parsing (so option order does not matter).

This series has been merged.