Skip to contentSkip to conversation

libsepol: mark permissive types when loading a binary policy

PATCH In review Nicolas Iooss 3 participants 6 messages Nov 5, 2018 – Nov 9, 2018 SELinux security module

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 libsepol: mark permissive types when loading a binary policy

When using checkpolicy to read a binary policy, permissive types are not written in the output file. In order to reproduce this issue, a test policy can be written from minimal.cil with the following commands:

    $ cd secilc/test/
    $ cp minimum.cil my_policy.cil
    $ echo '(typepermissive TYPE)' >> my_policy.cil
    $ secilc my_policy.cil
    $ checkpolicy -bC -o /dev/stdout policy.31

# There is no "(typepermissive TYPE)" in checkpolicy output.

This is because TYPE_FLAGS_PERMISSIVE is added to typdatum->flags only when loading a module, which uses the permissive flag in the type properties. A kernel policy defines permissive types in a dedicated bitmap, which gets loaded as p->permissive_map before the types are loaded. Use this bitmap to mark permissive types in the loaded policy.

---
 libsepol/src/policydb.c | 3 +++
 1 file changed, 3 insertions(+)
libsepol/src/policydb.c +3 -0
index 96176d805d59..f2beaf041fc7 100644
@@ -2469,6 +2469,9 @@ static int type_read(policydb_t * p, hashtab_t h, struct policy_file *fp)
 		if (properties & TYPEDATUM_PROPERTY_PERMISSIVE
 		    && p->policy_type != POLICY_KERN)
 			typdatum->flags |= TYPE_FLAGS_PERMISSIVE;
+		if (p->policy_type == POLICY_KERN
+		    && ebitmap_get_bit(&p->permissive_map, typdatum->s.value))
+			typdatum->flags |= TYPE_FLAGS_PERMISSIVE;
 
 		typdatum->bounds = le32_to_cpu(buf[++pos]);
 	} else {
Signature · 1 line
2.19.1
Acked-by on v1 1/1
#1 lore raw
Quoted 12 lines · Nicolas Iooss

When using checkpolicy to read a binary policy, permissive types are not written in the output file. In order to reproduce this issue, a test policy can be written from minimal.cil with the following commands:

     $ cd secilc/test/
     $ cp minimum.cil my_policy.cil
     $ echo '(typepermissive TYPE)' >> my_policy.cil
     $ secilc my_policy.cil
     $ checkpolicy -bC -o /dev/stdout policy.31

# There is no "(typepermissive TYPE)" in checkpolicy output.

Quoted 7 lines

This is because TYPE_FLAGS_PERMISSIVE is added to typdatum->flags only when loading a module, which uses the permissive flag in the type properties. A kernel policy defines permissive types in a dedicated bitmap, which gets loaded as p->permissive_map before the types are loaded. Use this bitmap to mark permissive types in the loaded policy.

Or we could directly use the permissive_map in kernel_to_cil/kernel_to_conf? Regardless,

Quoted 19 lines
---
  libsepol/src/policydb.c | 3 +++
  1 file changed, 3 insertions(+)
libsepol/src/policydb.c +3 -0
index 96176d805d59..f2beaf041fc7 100644
@@ -2469,6 +2469,9 @@ static int type_read(policydb_t * p, hashtab_t h, struct policy_file *fp)
  		if (properties & TYPEDATUM_PROPERTY_PERMISSIVE
  		    && p->policy_type != POLICY_KERN)
  			typdatum->flags |= TYPE_FLAGS_PERMISSIVE;
+		if (p->policy_type == POLICY_KERN
+		    && ebitmap_get_bit(&p->permissive_map, typdatum->s.value))
+			typdatum->flags |= TYPE_FLAGS_PERMISSIVE;
 
  		typdatum->bounds = le32_to_cpu(buf[++pos]);
  	} else {
#2 lore raw

PATCH 1/1 libsepol: mark permissive types when loading a binary policy

Nicolas Iooss reports:
When using checkpolicy to read a binary policy, permissive types are not written in the output file. In order to reproduce this issue, a test policy can be written from minimal.cil with the following commands:

    $ cd secilc/test/
    $ cp minimum.cil my_policy.cil
    $ echo '(typepermissive TYPE)' >> my_policy.cil
    $ secilc my_policy.cil
    $ checkpolicy -bC -o /dev/stdout policy.31

# There is no "(typepermissive TYPE)" in checkpolicy output.

This is because TYPE_FLAGS_PERMISSIVE is added to typdatum->flags only when loading a module, which uses the permissive flag in the type properties. A kernel policy defines permissive types in a dedicated bitmap, which gets loaded as p->permissive_map before the types are loaded.

The solution is to use the permissive_map bitmap instead of relying on the flags field of the struct type_datum when writing out CIL or policy.conf policy from a binary.

---
 libsepol/src/kernel_to_cil.c  | 14 ++++++--------
 libsepol/src/kernel_to_conf.c | 14 ++++++--------
 2 files changed, 12 insertions(+), 16 deletions(-)
libsepol/src/kernel_to_cil.c +6 -8
index 2c12ae9b..63e4d489 100644
@@ -1534,9 +1534,9 @@ exit:
 
 static int write_type_permissive_rules_to_cil(FILE *out, struct policydb *pdb)
 {
-	type_datum_t *type;
 	struct strs *strs;
 	char *name;
+	struct ebitmap_node *node;
 	unsigned i, num;
 	int rc = 0;
 
@@ -1545,13 +1545,11 @@ static int write_type_permissive_rules_to_cil(FILE *out, struct policydb *pdb)
 		goto exit;
 	}
 
-	for (i=0; i < pdb->p_types.nprim; i++) {
-		type = pdb->type_val_to_struct[i];
-		if (type->flavor == TYPE_TYPE && (type->flags & TYPE_FLAGS_PERMISSIVE)) {
-			rc = strs_add(strs, pdb->p_type_val_to_name[i]);
-			if (rc != 0) {
-				goto exit;
-			}
+	ebitmap_for_each_bit(&pdb->permissive_map, node, i) {
+		if (!ebitmap_get_bit(&pdb->permissive_map, i)) continue;
+		rc = strs_add(strs, pdb->p_type_val_to_name[i-1]);
+		if (rc != 0) {
+			goto exit;
 		}
 	}
 
libsepol/src/kernel_to_conf.c +6 -8
index 219a2489..215d940c 100644
@@ -1579,9 +1579,9 @@ exit:
 
 static int write_type_permissive_rules_to_conf(FILE *out, struct policydb *pdb)
 {
-	type_datum_t *type;
 	struct strs *strs;
 	char *name;
+	struct ebitmap_node *node;
 	unsigned i, num;
 	int rc = 0;
 
@@ -1590,13 +1590,11 @@ static int write_type_permissive_rules_to_conf(FILE *out, struct policydb *pdb)
 		goto exit;
 	}
 
-	for (i=0; i < pdb->p_types.nprim; i++) {
-		type = pdb->type_val_to_struct[i];
-		if (type->flavor == TYPE_TYPE && (type->flags & TYPE_FLAGS_PERMISSIVE)) {
-			rc = strs_add(strs, pdb->p_type_val_to_name[i]);
-			if (rc != 0) {
-				goto exit;
-			}
+	ebitmap_for_each_bit(&pdb->permissive_map, node, i) {
+		if (!ebitmap_get_bit(&pdb->permissive_map, i)) continue;
+		rc = strs_add(strs, pdb->p_type_val_to_name[i-1]);
+		if (rc != 0) {
+			goto exit;
 		}
 	}
 
Signature · 1 line
2.17.2
#3 lore raw
In reply to Stephen Smalley
Quoted 19 lines · Stephen Smalley
Quoted 18 lines · Nicolas Iooss

When using checkpolicy to read a binary policy, permissive types are not written in the output file. In order to reproduce this issue, a test policy can be written from minimal.cil with the following commands:

     $ cd secilc/test/
     $ cp minimum.cil my_policy.cil
     $ echo '(typepermissive TYPE)' >> my_policy.cil
     $ secilc my_policy.cil
     $ checkpolicy -bC -o /dev/stdout policy.31

     # There is no "(typepermissive TYPE)" in checkpolicy output.

This is because TYPE_FLAGS_PERMISSIVE is added to typdatum->flags only when loading a module, which uses the permissive flag in the type properties. A kernel policy defines permissive types in a dedicated bitmap, which gets loaded as p->permissive_map before the types are loaded. Use this bitmap to mark permissive types in the loaded policy.

Quoted 3 lines
Quoted 1 line

Or we could directly use the permissive_map in kernel_to_cil/kernel_to_conf?

I think that this is the more natural way to do it. Jim

Quoted 24 lines

Regardless,

Quoted 19 lines

---
  libsepol/src/policydb.c | 3 +++

  1 file changed, 3 insertions(+)
libsepol/src/policydb.c +0 -0
index 96176d805d59..f2beaf041fc7 100644
@@ -2469,6 +2469,9 @@ static int type_read(policydb_t * p, hashtab_t h, struct 
 policy_file *fp)

          if (properties & TYPEDATUM_PROPERTY_PERMISSIVE
              && p->policy_type != POLICY_KERN) typdatum->flags |= TYPE_FLAGS_PERMISSIVE;
+        if (p->policy_type == POLICY_KERN
+            && ebitmap_get_bit(&p->permissive_map, typdatum->s.value)) +            typdatum->flags |= TYPE_FLAGS_PERMISSIVE; typdatum->bounds = le32_to_cpu(buf[++pos]);
      } else {

Signature · 2 lines
James Carter <[email protected]>
National Security Agency
author Acked-by
#4 lore raw
Quoted 21 lines · On Tue, Nov 6, 2018 at 8:19 PM jwcart2 <[email protected]> wrote:
Quoted 19 lines · Stephen Smalley
Quoted 18 lines · Nicolas Iooss

When using checkpolicy to read a binary policy, permissive types are not written in the output file. In order to reproduce this issue, a test policy can be written from minimal.cil with the following commands:

     $ cd secilc/test/
     $ cp minimum.cil my_policy.cil
     $ echo '(typepermissive TYPE)' >> my_policy.cil
     $ secilc my_policy.cil
     $ checkpolicy -bC -o /dev/stdout policy.31

# There is no "(typepermissive TYPE)" in checkpolicy output.

This is because TYPE_FLAGS_PERMISSIVE is added to typdatum->flags only when loading a module, which uses the permissive flag in the type properties. A kernel policy defines permissive types in a dedicated bitmap, which gets loaded as p->permissive_map before the types are loaded. Use this bitmap to mark permissive types in the loaded policy.

Quoted 6 lines
Quoted 3 lines
Quoted 1 line

Or we could directly use the permissive_map in kernel_to_cil/kernel_to_conf?

I think that this is the more natural way to do it. Jim

I agree. Please drop my patch and keep yours.

Thanks,
Nicolas

#5 lore raw
Quoted 24 lines · Nicolas Iooss
Quoted 23 lines · On Tue, Nov 6, 2018 at 8:19 PM jwcart2 <[email protected]> wrote:
Quoted 21 lines · Stephen Smalley
Quoted 19 lines · Nicolas Iooss

When using checkpolicy to read a binary policy, permissive types are not written in the output file. In order to reproduce this issue, a test policy can be written from minimal.cil with the following commands:

      $ cd secilc/test/
      $ cp minimum.cil my_policy.cil
      $ echo '(typepermissive TYPE)' >> my_policy.cil
      $ secilc my_policy.cil
      $ checkpolicy -bC -o /dev/stdout policy.31

# There is no "(typepermissive TYPE)" in checkpolicy output.

This is because TYPE_FLAGS_PERMISSIVE is added to typdatum->flags only when loading a module, which uses the permissive flag in the type properties. A kernel policy defines permissive types in a dedicated bitmap, which gets loaded as p->permissive_map before the types are loaded. Use this bitmap to mark permissive types in the loaded policy.

Quoted 9 lines
Quoted 4 lines
Quoted 1 line

Or we could directly use the permissive_map in kernel_to_cil/kernel_to_conf?

I think that this is the more natural way to do it. Jim

I agree. Please drop my patch and keep yours.

Merged my patch.
Thanks,
Jim

Quoted 4 lines

Thanks,
Nicolas

Signature · 2 lines
James Carter <[email protected]>
National Security Agency