libsepol: mark permissive types when loading a binary policy
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
@@ -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
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_PERMISSIVEis added totypdatum->flagsonly 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 asp->permissive_mapbefore the types are loaded. Use this bitmap to mark permissive types in the loaded policy.Signed-off-by Nicolas Iooss <[email protected]>
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@@ -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 {
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
@@ -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
@@ -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
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_PERMISSIVEis added totypdatum->flagsonly 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 asp->permissive_mapbefore the types are loaded. Use this bitmap to mark permissive types in the loaded policy.
Quoted 3 lines
Quoted 1 line
Signed-off-by Nicolas Iooss <[email protected]>Or we could directly use the
permissive_mapin kernel_to_cil/kernel_to_conf?
I think that this is the more natural way to do it. Jim
Quoted 24 lines
Regardless,
Acked-by Stephen Smalley <[email protected]>Quoted 19 lines
---
libsepol/src/policydb.c| 3 +++1 file changed, 3 insertions(+)libsepol/src/policydb.c +0
-0@@ -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
Nicolas Iooss
Acked-byQuoted 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_PERMISSIVEis added totypdatum->flagsonly 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 asp->permissive_mapbefore the types are loaded. Use this bitmap to mark permissive types in the loaded policy.
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
Signed-off-by Nicolas Iooss <[email protected]>Or we could directly use the
permissive_mapin 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
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_PERMISSIVEis added totypdatum->flagsonly 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 asp->permissive_mapbefore the types are loaded. Use this bitmap to mark permissive types in the loaded policy.Signed-off-by Nicolas Iooss <[email protected]>
Quoted 9 lines
Quoted 4 lines
Quoted 1 line
Or we could directly use the
permissive_mapin 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.
Acked-by Nicolas Iooss <[email protected]>
Merged my patch.
Thanks,
Jim
Quoted 4 lines
Thanks,
Nicolas
Signature · 2 lines
James Carter <[email protected]> National Security Agency