Skip to contentSkip to conversation

checkpolicy: Do not automatically upgrade when using "-b" flag

PATCH Superseded James Carter 2 participants 5 messages Mar 11, 2021 – Mar 15, 2021 SELinux security module

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 checkpolicy: Do not automatically upgrade when using "-b" flag

When reading a binary policy, do not automatically change the version to the max policy version supported by libsepol or, if specified, the value given using the "-c" flag.

If the binary policy version is less than or equal to version 23 (POLICYDB_VERSION_PERMISSIVE) than do not automatically upgrade the policy and if a policy version is specified by the "-c" flag, only set the binary policy to the specified version if it is lower than the current version.

If the binary policy version is greater than version 23 than it should be set to the maximum version supported by libsepol or, if specified, the value given by the "-c" flag.

The reason for this change is that policy versions 20 (POLICYDB_VERSION_AVTAB) to 23 have a more primitive support for type attributes where the datums are not written out, but they exist in the type_attr_map. This means that when the binary policy is read by libsepol, there will be gaps in the type_val_to_struct and p_type_val_to_name arrays and policy rules can refer to those gaps. Certain libsepol functions like sepol_kernel_policydb_to_conf() and sepol_kernel_policydb_to_cil() do not support this behavior and need to be able to identify these policies. Policies before version 20 do not support attributes at all and can be handled by all libsepol functions.

---
 checkpolicy/checkpolicy.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
checkpolicy/checkpolicy.c +13 -3
index 5841c5c4..e7b225b8 100644
@@ -106,7 +106,7 @@ static int handle_unknown = SEPOL_DENY_UNKNOWN;
 static const char *txtfile = "policy.conf";
 static const char *binfile = "policy";
 
-unsigned int policyvers = POLICYDB_VERSION_MAX;
+unsigned int policyvers = 0;
 
 static __attribute__((__noreturn__)) void usage(const char *progname)
 {
@@ -588,6 +588,16 @@ int main(int argc, char **argv)
 				exit(1);
 			}
 		}
+
+		if (policydbp->policyvers <= POLICYDB_VERSION_PERMISSIVE) {
+			if (policyvers > policydbp->policyvers) {
+				fprintf(stderr, "Binary policies with version <= %u cannot be upgraded\n", POLICYDB_VERSION_PERMISSIVE);
+			} else if (policyvers) {
+				policydbp->policyvers = policyvers;
+			}
+		} else {
+			policydbp->policyvers = policyvers ? policyvers : POLICYDB_VERSION_MAX;
+		}
 	} else {
 		if (conf) {
 			fprintf(stderr, "Can only generate policy.conf from binary policy\n");
@@ -629,6 +639,8 @@ int main(int argc, char **argv)
 			policydb_destroy(policydbp);
 			policydbp = &policydb;
 		}
+
+		policydbp->policyvers = policyvers ? policyvers : POLICYDB_VERSION_MAX;
 	}
 
 	if (policydb_load_isids(&policydb, &sidtab))
@@ -654,8 +666,6 @@ int main(int argc, char **argv)
 			}
 		}
 
-		policydb.policyvers = policyvers;
-
 		if (!cil) {
 			if (!conf) {
 				policydb.policy_type = POLICY_KERN;
Signature · 1 line
2.26.2
#1 lore raw

PATCH 2/2 libsepol: Check kernel to CIL and Conf functions for supported versions

For policy versions between 20 and 23, attributes exist in the policy, but only in the type_attr_map. This means that there are gaps in both the type_val_to_struct and p_type_val_to_name arrays and policy rules can refer to those gaps which can lead to NULL dereferences when using sepol_kernel_policydb_to_conf() and sepol_kernel_policydb_to_cil().

This can be seen with the following policy:
class CLASS1
sid SID1
class CLASS1 { PERM1 }
attribute TYPE_ATTR1;
type TYPE1;
typeattribute TYPE1 TYPE_ATTR1;
allow TYPE_ATTR1 self : CLASS1 PERM1;
role ROLE1;
role ROLE1 types TYPE1;
user USER1 roles ROLE1;
sid SID1 USER1:ROLE1:TYPE1

Compile the policy:
checkpolicy -c 23 -o policy.bin policy.conf
Converting back to a policy.conf causes a segfault:
checkpolicy -F -b -o policy.bin.conf policy.bin

Have both sepol_kernel_policydb_to_conf() and sepol_kernel_policydb_to_cil() exit with an error if the kernel policy version is between 20 and 23.

---
 libsepol/src/kernel_to_cil.c  | 12 ++++++++++++
 libsepol/src/kernel_to_conf.c | 12 ++++++++++++
 2 files changed, 24 insertions(+)
libsepol/src/kernel_to_cil.c +12 -0
index a146ac51..edfebeaf 100644
@@ -3164,6 +3164,18 @@ int sepol_kernel_policydb_to_cil(FILE *out, struct policydb *pdb)
 		goto exit;
 	}
 
+	if (pdb->policyvers >= POLICYDB_VERSION_AVTAB && pdb->policyvers <= POLICYDB_VERSION_PERMISSIVE) {
+		/*
+		 * For policy versions between 20 and 23, attributes exist in the policy,
+		 * but only in the type_attr_map. This means that there are gaps in both
+		 * the type_val_to_struct and p_type_val_to_name arrays and policy rules
+		 * can refer to those gaps.
+		 */
+		sepol_log_err("Writing policy versions between 20 and 23 as CIL is not supported");
+		rc = -1;
+		goto exit;
+	}
+
 	rc = constraint_rules_to_strs(pdb, mls_constraints, non_mls_constraints);
 	if (rc != 0) {
 		goto exit;
libsepol/src/kernel_to_conf.c +12 -0
index a22f196d..ea58a026 100644
@@ -3041,6 +3041,18 @@ int sepol_kernel_policydb_to_conf(FILE *out, struct policydb *pdb)
 		goto exit;
 	}
 
+	if (pdb->policyvers >= POLICYDB_VERSION_AVTAB && pdb->policyvers <= POLICYDB_VERSION_PERMISSIVE) {
+		/*
+		 * For policy versions between 20 and 23, attributes exist in the policy,
+		 * but only in the type_attr_map. This means that there are gaps in both
+		 * the type_val_to_struct and p_type_val_to_name arrays and policy rules
+		 * can refer to those gaps.
+		 */
+		sepol_log_err("Writing policy versions between 20 and 23 as a policy.conf is not supported");
+		rc = -1;
+		goto exit;
+	}
+
 	rc = constraint_rules_to_strs(pdb, mls_constraints, non_mls_constraints);
 	if (rc != 0) {
 		goto exit;
Signature · 1 line
2.26.2
#2 lore raw

Re: [PATCH 2/2] libsepol: Check kernel to CIL and Conf functions for supported versions

Quoted 26 lines · James Carter

For policy versions between 20 and 23, attributes exist in the policy, but only in the type_attr_map. This means that there are gaps in both the type_val_to_struct and p_type_val_to_name arrays and policy rules can refer to those gaps which can lead to NULL dereferences when using sepol_kernel_policydb_to_conf() and sepol_kernel_policydb_to_cil().

This can be seen with the following policy:
class CLASS1
sid SID1
class CLASS1 { PERM1 }
attribute TYPE_ATTR1;
type TYPE1;
typeattribute TYPE1 TYPE_ATTR1;
allow TYPE_ATTR1 self : CLASS1 PERM1;
role ROLE1;
role ROLE1 types TYPE1;
user USER1 roles ROLE1;
sid SID1 USER1:ROLE1:TYPE1

Compile the policy:
checkpolicy -c 23 -o policy.bin policy.conf
Converting back to a policy.conf causes a segfault:
checkpolicy -F -b -o policy.bin.conf policy.bin

Quoted 5 lines

Have both sepol_kernel_policydb_to_conf() and sepol_kernel_policydb_to_cil() exit with an error if the kernel policy version is between 20 and 23.

For this patch: Acked-by: Nicolas Iooss <[email protected]>

(for the first one, I reported a regression in "checkpolicy -V")

Thanks,
Nicolas

Quoted 54 lines
---
 libsepol/src/kernel_to_cil.c  | 12 ++++++++++++
 libsepol/src/kernel_to_conf.c | 12 ++++++++++++
 2 files changed, 24 insertions(+)
libsepol/src/kernel_to_cil.c +12 -0
index a146ac51..edfebeaf 100644
@@ -3164,6 +3164,18 @@ int sepol_kernel_policydb_to_cil(FILE *out, struct policydb *pdb)
                goto exit;
        }
 
+       if (pdb->policyvers >= POLICYDB_VERSION_AVTAB && pdb->policyvers <= POLICYDB_VERSION_PERMISSIVE) {
+               /*
+                * For policy versions between 20 and 23, attributes exist in the policy,
+                * but only in the type_attr_map. This means that there are gaps in both
+                * the type_val_to_struct and p_type_val_to_name arrays and policy rules
+                * can refer to those gaps.
+                */
+               sepol_log_err("Writing policy versions between 20 and 23 as CIL is not supported");
+               rc = -1;
+               goto exit;
+       }
+
        rc = constraint_rules_to_strs(pdb, mls_constraints, non_mls_constraints);
        if (rc != 0) {
                goto exit;
libsepol/src/kernel_to_conf.c +12 -1
index a22f196d..ea58a026 100644
@@ -3041,6 +3041,18 @@ int sepol_kernel_policydb_to_conf(FILE *out, struct policydb *pdb)
                goto exit;
        }
 
+       if (pdb->policyvers >= POLICYDB_VERSION_AVTAB && pdb->policyvers <= POLICYDB_VERSION_PERMISSIVE) {
+               /*
+                * For policy versions between 20 and 23, attributes exist in the policy,
+                * but only in the type_attr_map. This means that there are gaps in both
+                * the type_val_to_struct and p_type_val_to_name arrays and policy rules
+                * can refer to those gaps.
+                */
+               sepol_log_err("Writing policy versions between 20 and 23 as a policy.conf is not supported");
+               rc = -1;
+               goto exit;
+       }
+
        rc = constraint_rules_to_strs(pdb, mls_constraints, non_mls_constraints);
        if (rc != 0) {
                goto exit;
--

2.26.2

#3 lore raw
In reply to James Carter
Quoted 36 lines · James Carter

When reading a binary policy, do not automatically change the version to the max policy version supported by libsepol or, if specified, the value given using the "-c" flag.

If the binary policy version is less than or equal to version 23 (POLICYDB_VERSION_PERMISSIVE) than do not automatically upgrade the policy and if a policy version is specified by the "-c" flag, only set the binary policy to the specified version if it is lower than the current version.

If the binary policy version is greater than version 23 than it should be set to the maximum version supported by libsepol or, if specified, the value given by the "-c" flag.

The reason for this change is that policy versions 20 (POLICYDB_VERSION_AVTAB) to 23 have a more primitive support for type attributes where the datums are not written out, but they exist in the type_attr_map. This means that when the binary policy is read by libsepol, there will be gaps in the type_val_to_struct and p_type_val_to_name arrays and policy rules can refer to those gaps. Certain libsepol functions like sepol_kernel_policydb_to_conf() and sepol_kernel_policydb_to_cil() do not support this behavior and need to be able to identify these policies. Policies before version 20 do not support attributes at all and can be handled by all libsepol functions.

---
 checkpolicy/checkpolicy.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
checkpolicy/checkpolicy.c +0 -0
index 5841c5c4..e7b225b8 100644

checkpolicy/checkpolicy.c static int handle_unknown = SEPOL_DENY_UNKNOWN; · line 109

Quoted 5 lines
checkpolicy/checkpolicy.c +1 -1
@@ -106,7 +106,7 @@ static int handle_unknown = SEPOL_DENY_UNKNOWN;
 static const char *txtfile = "policy.conf";
 static const char *binfile = "policy";
 
-unsigned int policyvers = POLICYDB_VERSION_MAX;
+unsigned int policyvers = 0;

Hi,
This change breaks "make test" when testing secilc. This is because secilc/Makefile uses "checkpolicy -V" to compute a policy version:

POL_VERS = $(shell $(CHECKPOLICY) -V | cut -f 1 -d ' ')

Before your patch:

$ checkpolicy -V
33 (compatibility range 33-15)

After:

$ checkpolicy -V
0 (compatibility range 33-15)

.. which makes secilc/Makefile try using "secilc -c 0 ...", which does not work.

Should "policyvers ? policyvers : POLICYDB_VERSION_MAX" also be used when "checkpolicy -V" displays version information?

Thanks,
Nicolas

Quoted 41 lines
checkpolicy/checkpolicy.c +12 -3
 static __attribute__((__noreturn__)) void usage(const char *progname)
 {
@@ -588,6 +588,16 @@ int main(int argc, char **argv)
                                exit(1);
                        }
                }
+
+               if (policydbp->policyvers <= POLICYDB_VERSION_PERMISSIVE) {
+                       if (policyvers > policydbp->policyvers) {
+                               fprintf(stderr, "Binary policies with version <= %u cannot be upgraded\n", POLICYDB_VERSION_PERMISSIVE);
+                       } else if (policyvers) {
+                               policydbp->policyvers = policyvers;
+                       }
+               } else {
+                       policydbp->policyvers = policyvers ? policyvers : POLICYDB_VERSION_MAX;
+               }
        } else {
                if (conf) {
                        fprintf(stderr, "Can only generate policy.conf from binary policy\n");
@@ -629,6 +639,8 @@ int main(int argc, char **argv)
                        policydb_destroy(policydbp);
                        policydbp = &policydb;
                }
+
+               policydbp->policyvers = policyvers ? policyvers : POLICYDB_VERSION_MAX;
        }
 
        if (policydb_load_isids(&policydb, &sidtab))
@@ -654,8 +666,6 @@ int main(int argc, char **argv)
                        }
                }
 
-               policydb.policyvers = policyvers;
-
                if (!cil) {
                        if (!conf) {
                                policydb.policy_type = POLICY_KERN;
--

2.26.2

#4 lore raw
Quoted 57 lines · Nicolas Iooss
Quoted 36 lines · James Carter

When reading a binary policy, do not automatically change the version to the max policy version supported by libsepol or, if specified, the value given using the "-c" flag.

If the binary policy version is less than or equal to version 23 (POLICYDB_VERSION_PERMISSIVE) than do not automatically upgrade the policy and if a policy version is specified by the "-c" flag, only set the binary policy to the specified version if it is lower than the current version.

If the binary policy version is greater than version 23 than it should be set to the maximum version supported by libsepol or, if specified, the value given by the "-c" flag.

The reason for this change is that policy versions 20 (POLICYDB_VERSION_AVTAB) to 23 have a more primitive support for type attributes where the datums are not written out, but they exist in the type_attr_map. This means that when the binary policy is read by libsepol, there will be gaps in the type_val_to_struct and p_type_val_to_name arrays and policy rules can refer to those gaps. Certain libsepol functions like sepol_kernel_policydb_to_conf() and sepol_kernel_policydb_to_cil() do not support this behavior and need to be able to identify these policies. Policies before version 20 do not support attributes at all and can be handled by all libsepol functions.

---
 checkpolicy/checkpolicy.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)
checkpolicy/checkpolicy.c +0 -0
index 5841c5c4..e7b225b8 100644

checkpolicy/checkpolicy.c static int handle_unknown = SEPOL_DENY_UNKNOWN; · line 109

Quoted 5 lines
checkpolicy/checkpolicy.c +1 -1
@@ -106,7 +106,7 @@ static int handle_unknown = SEPOL_DENY_UNKNOWN;
 static const char *txtfile = "policy.conf";
 static const char *binfile = "policy";
 
-unsigned int policyvers = POLICYDB_VERSION_MAX;
+unsigned int policyvers = 0;

Hi,
This change breaks "make test" when testing secilc. This is because secilc/Makefile uses "checkpolicy -V" to compute a policy version:

POL_VERS = $(shell $(CHECKPOLICY) -V | cut -f 1 -d ' ')

Before your patch:

$ checkpolicy -V
33 (compatibility range 33-15)

After:

Quoted 8 lines
$ checkpolicy -V
0 (compatibility range 33-15)

.. which makes secilc/Makefile try using "secilc -c 0 ...", which does not work.

Should "policyvers ? policyvers : POLICYDB_VERSION_MAX" also be used when "checkpolicy -V" displays version information?

Yes it should. I'll send an updated patch.
Thanks,
Jim

Quoted 45 lines

Thanks,
Nicolas

Quoted 41 lines
checkpolicy/checkpolicy.c +12 -3
 static __attribute__((__noreturn__)) void usage(const char *progname)
 {
@@ -588,6 +588,16 @@ int main(int argc, char **argv)
                                exit(1);
                        }
                }
+
+               if (policydbp->policyvers <= POLICYDB_VERSION_PERMISSIVE) {
+                       if (policyvers > policydbp->policyvers) {
+                               fprintf(stderr, "Binary policies with version <= %u cannot be upgraded\n", POLICYDB_VERSION_PERMISSIVE);
+                       } else if (policyvers) {
+                               policydbp->policyvers = policyvers;
+                       }
+               } else {
+                       policydbp->policyvers = policyvers ? policyvers : POLICYDB_VERSION_MAX;
+               }
        } else {
                if (conf) {
                        fprintf(stderr, "Can only generate policy.conf from binary policy\n");
@@ -629,6 +639,8 @@ int main(int argc, char **argv)
                        policydb_destroy(policydbp);
                        policydbp = &policydb;
                }
+
+               policydbp->policyvers = policyvers ? policyvers : POLICYDB_VERSION_MAX;
        }
 
        if (policydb_load_isids(&policydb, &sidtab))
@@ -654,8 +666,6 @@ int main(int argc, char **argv)
                        }
                }
 
-               policydb.policyvers = policyvers;
-
                if (!cil) {
                        if (!conf) {
                                policydb.policy_type = POLICY_KERN;
--

2.26.2