Blame SOURCES/0002-checkpolicy-Do-not-automatically-upgrade-when-using-.patch

83845a
From 750cc1136d054b77e84cd55be5fbe0e8ad0174e8 Mon Sep 17 00:00:00 2001
83845a
From: James Carter <jwcart2@gmail.com>
83845a
Date: Mon, 15 Mar 2021 11:09:37 -0400
83845a
Subject: [PATCH] checkpolicy: Do not automatically upgrade when using "-b"
83845a
 flag
83845a
83845a
When reading a binary policy, do not automatically change the version
83845a
to the max policy version supported by libsepol or, if specified, the
83845a
value given using the "-c" flag.
83845a
83845a
If the binary policy version is less than or equal to version 23
83845a
(POLICYDB_VERSION_PERMISSIVE) than do not automatically upgrade the
83845a
policy and if a policy version is specified by the "-c" flag, only set
83845a
the binary policy to the specified version if it is lower than the
83845a
current version.
83845a
83845a
If the binary policy version is greater than version 23 than it should
83845a
be set to the maximum version supported by libsepol or, if specified,
83845a
the value given by the "-c" flag.
83845a
83845a
The reason for this change is that policy versions 20
83845a
(POLICYDB_VERSION_AVTAB) to 23 have a more primitive support for type
83845a
attributes where the datums are not written out, but they exist in the
83845a
type_attr_map. This means that when the binary policy is read by
83845a
libsepol, there will be gaps in the type_val_to_struct and
83845a
p_type_val_to_name arrays and policy rules can refer to those gaps.
83845a
Certain libsepol functions like sepol_kernel_policydb_to_conf() and
83845a
sepol_kernel_policydb_to_cil() do not support this behavior and need
83845a
to be able to identify these policies. Policies before version 20 do not
83845a
support attributes at all and can be handled by all libsepol functions.
83845a
83845a
Signed-off-by: James Carter <jwcart2@gmail.com>
83845a
---
83845a
 checkpolicy/checkpolicy.c | 19 +++++++++++++++----
83845a
 1 file changed, 15 insertions(+), 4 deletions(-)
83845a
83845a
diff --git a/checkpolicy/checkpolicy.c b/checkpolicy/checkpolicy.c
83845a
index 5841c5c4c196..acf1eac41559 100644
83845a
--- a/checkpolicy/checkpolicy.c
83845a
+++ b/checkpolicy/checkpolicy.c
83845a
@@ -106,7 +106,7 @@ static int handle_unknown = SEPOL_DENY_UNKNOWN;
83845a
 static const char *txtfile = "policy.conf";
83845a
 static const char *binfile = "policy";
83845a
 
83845a
-unsigned int policyvers = POLICYDB_VERSION_MAX;
83845a
+unsigned int policyvers = 0;
83845a
 
83845a
 static __attribute__((__noreturn__)) void usage(const char *progname)
83845a
 {
83845a
@@ -515,7 +515,8 @@ int main(int argc, char **argv)
83845a
 	}
83845a
 
83845a
 	if (show_version) {
83845a
-		printf("%d (compatibility range %d-%d)\n", policyvers,
83845a
+		printf("%d (compatibility range %d-%d)\n",
83845a
+			   policyvers ? policyvers : POLICYDB_VERSION_MAX ,
83845a
 		       POLICYDB_VERSION_MAX, POLICYDB_VERSION_MIN);
83845a
 		exit(0);
83845a
 	}
83845a
@@ -588,6 +589,16 @@ int main(int argc, char **argv)
83845a
 				exit(1);
83845a
 			}
83845a
 		}
83845a
+
83845a
+		if (policydbp->policyvers <= POLICYDB_VERSION_PERMISSIVE) {
83845a
+			if (policyvers > policydbp->policyvers) {
83845a
+				fprintf(stderr, "Binary policies with version <= %u cannot be upgraded\n", POLICYDB_VERSION_PERMISSIVE);
83845a
+			} else if (policyvers) {
83845a
+				policydbp->policyvers = policyvers;
83845a
+			}
83845a
+		} else {
83845a
+			policydbp->policyvers = policyvers ? policyvers : POLICYDB_VERSION_MAX;
83845a
+		}
83845a
 	} else {
83845a
 		if (conf) {
83845a
 			fprintf(stderr, "Can only generate policy.conf from binary policy\n");
83845a
@@ -629,6 +640,8 @@ int main(int argc, char **argv)
83845a
 			policydb_destroy(policydbp);
83845a
 			policydbp = &policydb;
83845a
 		}
83845a
+
83845a
+		policydbp->policyvers = policyvers ? policyvers : POLICYDB_VERSION_MAX;
83845a
 	}
83845a
 
83845a
 	if (policydb_load_isids(&policydb, &sidtab))
83845a
@@ -654,8 +667,6 @@ int main(int argc, char **argv)
83845a
 			}
83845a
 		}
83845a
 
83845a
-		policydb.policyvers = policyvers;
83845a
-
83845a
 		if (!cil) {
83845a
 			if (!conf) {
83845a
 				policydb.policy_type = POLICY_KERN;
83845a
-- 
83845a
2.32.0
83845a