libsepol: support for permissive types
PATCH 1/1 libsepol: support for permissive types
This patch adds support for permissive types.
In the kernel policy format the permissive types are in a bitmap referenced by the type value.
In the module policy format a new field in the type_datum_t called 'flags' was added. The only currently defined flag is TYPE_FLAGS_PERMISSIVE.
Checkpolicy can set the permissive flag on the type_datum_t in question and that flag will persist on disk. It will be OR'd at link time against the type in the base policy. At expand time we build the bit array the kernel uses.
---
include/sepol/policydb/policydb.h +8 -2
@@ -143,6 +143,8 @@ typedef struct type_datum {#define TYPE_ALIAS 2/* alias in modular policy */ uint32_t flavor;ebitmap_t types;/* types with this attribute */ +#define TYPE_FLAGS_PERMISSIVE 0x01 +uint32_t flags; } type_datum_t;/* User attributes */
@@ -470,6 +472,8 @@ typedef struct policydb {ebitmap_t policycaps; +ebitmap_t permissive_map; +unsigned policyvers;unsigned handle_unknown;
@@ -587,10 +591,11 @@ extern int policydb_write(struct policyd#define POLICYDB_VERSION_AVTAB 20#define POLICYDB_VERSION_RANGETRANS 21#define POLICYDB_VERSION_POLCAP 22 +#define POLICYDB_VERSION_PERMISSIVE 23/* Range of policy versions we understand*/ #define POLICYDB_VERSION_MIN POLICYDB_VERSION_BASE -#define POLICYDB_VERSION_MAX POLICYDB_VERSION_POLCAP +#define POLICYDB_VERSION_MAX POLICYDB_VERSION_PERMISSIVE/* Module versions and specific changes*/ #define MOD_POLICYDB_VERSION_BASE 4
@@ -599,9 +604,10 @@ extern int policydb_write(struct policyd#define MOD_POLICYDB_VERSION_RANGETRANS 6#define MOD_POLICYDB_VERSION_MLS_USERS 6#define MOD_POLICYDB_VERSION_POLCAP 7 +#define MOD_POLICYDB_VERSION_PERMISSIVE 8#define MOD_POLICYDB_VERSION_MIN MOD_POLICYDB_VERSION_BASE -#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_POLCAP +#define MOD_POLICYDB_VERSION_MAX MOD_POLICYDB_VERSION_PERMISSIVE#define POLICYDB_CONFIG_MLS 1
src/expand.c +10 -0
@@ -92,6 +92,7 @@ static int type_copy_callback(hashtab_kememset (new_type,0 ,sizeof (type_datum_t)); new_type->flavor = type->flavor; + new_type->flags = type->flags; new_type->s.value = ++state->out->p_types.nprim;if (new_type->s.value >UINT16_MAX ) {free (new_id);
@@ -112,6 +113,9 @@ static int type_copy_callback(hashtab_kereturn -1 ; } +if (new_type->flags &TYPE_FLAGS_PERMISSIVE ) +ebitmap_set_bit (&state->out->permissive_map, new_type->s.value,1 ); +return 0 ; }
@@ -480,6 +484,8 @@ static int alias_copy_callback(hashtab_kelse assert (0 );/* unreachable */ + new_alias->flags = alias->flags; + ret =hashtab_insert (state->out->p_types.table, (hashtab_key_t ) new_id, (hashtab_datum_t ) new_alias);
@@ -492,6 +498,10 @@ static int alias_copy_callback(hashtab_k } state->typemap[alias->s.value -1 ] = new_alias->s.value; + +if (new_alias->flags &TYPE_FLAGS_PERMISSIVE ) +ebitmap_set_bit (&state->out->permissive_map, new_alias->s.value,1 ); +return 0 ; }
src/link.c +8 -0
@@ -405,6 +405,8 @@ static int type_copy_callback(hashtab_ke state->cur_mod_name, id);return -1 ; } +/* permissive should pass to the base type */ + base_type->flags |= (type->flags &TYPE_FLAGS_PERMISSIVE ); }else {if (state->verbose)INFO (state->handle,"copying type %s" , id);
@@ -418,6 +420,7 @@ static int type_copy_callback(hashtab_ke goto cleanup; } new_type->primary = type->primary; + new_type->flags = type->flags; new_type->flavor = type->flavor;/* for attributes, the writing of new_type->types is done in type_fix_callback() */
@@ -441,6 +444,7 @@ static int type_copy_callback(hashtab_ke } new_type->primary = type->primary; new_type->flavor = type->flavor; + new_type->flags = type->flags; new_type->s.value = base_type->s.value;if ((new_id =strdup (id)) ==NULL ) { goto cleanup;
@@ -702,6 +706,8 @@ static int alias_copy_callback(hashtab_kreturn -1 ; } + target_type->flags |= (type->flags &TYPE_FLAGS_PERMISSIVE ); + base_type =hashtab_search (state->base->p_types.table, id);if (base_type ==NULL ) {if (state->verbose)
@@ -713,6 +719,7 @@ static int alias_copy_callback(hashtab_k }/* the linked copy always has TYPE_ALIAS style aliases */ new_type->primary = target_type->s.value; + new_type->flags = target_type->flags; new_type->flavor =TYPE_ALIAS ; new_type->s.value = state->base->p_types.nprim +1 ;if ((new_id =strdup (id)) ==NULL ) {
@@ -747,6 +754,7 @@ static int alias_copy_callback(hashtab_k base_type->flavor =TYPE_ALIAS ; base_type->primary = target_type->s.value; + base_type->flags |= (target_type->flags &TYPE_FLAGS_PERMISSIVE ); }/* the aliases map points from its value to its primary so when this module
src/policydb.c +42 -8
104 lines not on this page · raw message
src/write.c +8 -0
@@ -959,6 +959,8 @@ static int type_write(hashtab_key_t key, buf[items++] =cpu_to_le32 (typdatum->primary);if (p->policy_type !=POLICY_KERN ) { buf[items++] =cpu_to_le32 (typdatum->flavor); +if (p->policyvers >=MOD_POLICYDB_VERSION_PERMISSIVE ) + buf[items++] =cpu_to_le32 (typdatum->flags); } items2 =put_entry (buf,sizeof (uint32_t ), items, fp);if (items != items2)
@@ -1618,6 +1620,12 @@ int policydb_write(policydb_t * p, strucreturn POLICYDB_ERROR ; } +if (p->policyvers >=POLICYDB_VERSION_PERMISSIVE && + p->policy_type ==POLICY_KERN ) { +if (ebitmap_write (&p->permissive_map, fp) ==-1 ) +return POLICYDB_ERROR ; + } + num_syms = info->sym_num;for (i =0 ; i < num_syms; i++) { buf[0 ] =cpu_to_le32 (p->symtab[i].nprim);
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 17 lines · Eric Paris
This patch adds support for permissive types.
In the kernel policy format the permissive types are in a bitmap referenced by the type value.
In the module policy format a new field in the
type_datum_tcalled 'flags' was added. The only currently defined flag isTYPE_FLAGS_PERMISSIVE.Checkpolicy can set the permissive flag on the
type_datum_tin question and that flag will persist on disk. It will be OR'd at link time against the type in the base policy. At expand time we build the bit array the kernel uses.Signed-off-by Eric Paris <[email protected]>---
<snip>
src/expand.c static int type_copy_callback(hashtab_ke · line 117
Quoted 6 lines
src/expand.c +2
-0@@ -112,6 +113,9 @@ static int type_copy_callback(hashtab_kereturn -1 ; } +if (new_type->flags &TYPE_FLAGS_PERMISSIVE ) +ebitmap_set_bit (&state->out->permissive_map, new_type->s.value,1 );
ebitmap_set_bit() can fail due to memory allocation failure, so you need to check for it.
Somewhere we also need to clearly document that the permissive map is _not_ using zero-based indexing for the types, unlike other ebitmaps in policy.
src/expand.c static int alias_copy_callback(hashtab_k · line 503
Quoted 7 lines
src/expand.c +3
-0@@ -492,6 +498,10 @@ static int alias_copy_callback(hashtab_k } state->typemap[alias->s.value -1 ] = new_alias->s.value; + +if (new_alias->flags &TYPE_FLAGS_PERMISSIVE ) +ebitmap_set_bit (&state->out->permissive_map, new_alias->s.value,1 );
Ditto.
Have you tested the handling of aliases, e.g. declare a type alias to a type in one module, then mark that type alias as permissive in another module, and confirm proper propagation of the permissive flag?
Quoted 15 lines
diff -Naupr
libsepol-2.0.23/src/write.clibsepol-2.0.23.new/src/write.csrc/write.c +2
-0@@ -959,6 +959,8 @@ static int type_write(hashtab_key_t key, buf[items++] =cpu_to_le32 (typdatum->primary);if (p->policy_type !=POLICY_KERN ) { buf[items++] =cpu_to_le32 (typdatum->flavor); +if (p->policyvers >=MOD_POLICYDB_VERSION_PERMISSIVE ) + buf[items++] =cpu_to_le32 (typdatum->flags); } items2 =put_entry (buf,sizeof (uint32_t ), items, fp);if (items != items2)@@ -1618,6 +1620,12 @@ int policydb_write(policydb_t * p, strucreturn POLICYDB_ERROR ; }
src/write.c int policydb_write(policydb_t * p, struc · line 1631
Quoted 10 lines
src/write.c +6
-0+if (p->policyvers >=POLICYDB_VERSION_PERMISSIVE && + p->policy_type ==POLICY_KERN ) { +if (ebitmap_write (&p->permissive_map, fp) ==-1 ) +return POLICYDB_ERROR ; + } + num_syms = info->sym_num;for (i =0 ; i < num_syms; i++) { buf[0 ] =cpu_to_le32 (p->symtab[i].nprim);
So in the case of automatic policy downgrade by libselinux load policy logic, we will silently shed any permissive maps. Likely need a warning if the permissive map has any bits set at all to warn the user that he isn't going to get what he expected.
Signature · 8 lines
Stephen Smalley National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 24 lines · Stephen Smalley
Quoted 17 lines · Eric Paris
This patch adds support for permissive types.
In the kernel policy format the permissive types are in a bitmap referenced by the type value.
In the module policy format a new field in the
type_datum_tcalled 'flags' was added. The only currently defined flag isTYPE_FLAGS_PERMISSIVE.Checkpolicy can set the permissive flag on the
type_datum_tin question and that flag will persist on disk. It will be OR'd at link time against the type in the base policy. At expand time we build the bit array the kernel uses.Signed-off-by Eric Paris <[email protected]>---
<snip>
Quoted 4 lines
src/expand.c +0
-0@@ -112,6 +113,9 @@ static int type_copy_callback(hashtab_kereturn -1 ; }
Quoted 5 lines
src/expand.c static int type_copy_callback(hashtab_ke · line 117
Quoted 2 lines
src/expand.c +2
-0+if (new_type->flags &TYPE_FLAGS_PERMISSIVE ) +ebitmap_set_bit (&state->out->permissive_map, new_type->s.value,1 );
ebitmap_set_bit()can fail due to memory allocation failure, so you need to check for it.
sure.
Quoted 4 lines
Somewhere we also need to clearly document that the permissive map is _not_ using zero-based indexing for the types, unlike other ebitmaps in policy.
any idea where to document? Would you prefer I just move all of this to type-1 to keep consistancy? Probably some way to use max_type_number+1 for global if I/we ever do it....
Quoted 11 lines
src/expand.c static int alias_copy_callback(hashtab_k · line 503
Quoted 8 lines
src/expand.c +3
-0@@ -492,6 +498,10 @@ static int alias_copy_callback(hashtab_k } state->typemap[alias->s.value -1 ] = new_alias->s.value; + +if (new_alias->flags &TYPE_FLAGS_PERMISSIVE ) +ebitmap_set_bit (&state->out->permissive_map, new_alias->s.value,1 );Ditto.
Quoted 3 lines
Have you tested the handling of aliases, e.g. declare a type alias to a type in one module, then mark that type alias as permissive in another module, and confirm proper propagation of the permissive flag?
I tested declare a type in base and then in a module require that base type, alias to that base type, and then permissive the alias. Is there a different between that and actually declaring the type in another module?
Quoted 16 lines
Quoted 16 lines
diff -Naupr
libsepol-2.0.23/src/write.clibsepol-2.0.23.new/src/write.csrc/write.c +2
-0@@ -959,6 +959,8 @@ static int type_write(hashtab_key_t key, buf[items++] =cpu_to_le32 (typdatum->primary);if (p->policy_type !=POLICY_KERN ) { buf[items++] =cpu_to_le32 (typdatum->flavor); +if (p->policyvers >=MOD_POLICYDB_VERSION_PERMISSIVE ) + buf[items++] =cpu_to_le32 (typdatum->flags); } items2 =put_entry (buf,sizeof (uint32_t ), items, fp);if (items != items2)@@ -1618,6 +1620,12 @@ int policydb_write(policydb_t * p, strucreturn POLICYDB_ERROR ; }
Quoted 14 lines
src/write.c int policydb_write(policydb_t * p, struc · line 1631
Quoted 9 lines
src/write.c +6
-0+if (p->policyvers >=POLICYDB_VERSION_PERMISSIVE && + p->policy_type ==POLICY_KERN ) { +if (ebitmap_write (&p->permissive_map, fp) ==-1 ) +return POLICYDB_ERROR ; + } + num_syms = info->sym_num;for (i =0 ; i < num_syms; i++) { buf[0 ] =cpu_to_le32 (p->symtab[i].nprim);So in the case of automatic policy downgrade by libselinux load policy logic, we will silently shed any permissive maps. Likely need a warning if the permissive map has any bits set at all to warn the user that he isn't going to get what he expected.
So just some print statements? I'll look for examples....
-Eric
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 34 lines · Eric Paris
Quoted 24 lines · Stephen Smalley
Quoted 17 lines · Eric Paris
This patch adds support for permissive types.
In the kernel policy format the permissive types are in a bitmap referenced by the type value.
In the module policy format a new field in the
type_datum_tcalled 'flags' was added. The only currently defined flag isTYPE_FLAGS_PERMISSIVE.Checkpolicy can set the permissive flag on the
type_datum_tin question and that flag will persist on disk. It will be OR'd at link time against the type in the base policy. At expand time we build the bit array the kernel uses.Signed-off-by Eric Paris <[email protected]>---
<snip>
Quoted 4 lines
src/expand.c +0
-0@@ -112,6 +113,9 @@ static int type_copy_callback(hashtab_kereturn -1 ; }Quoted 5 lines
src/expand.c static int type_copy_callback(hashtab_ke · line 117
Quoted 2 lines
src/expand.c +2
-0+if (new_type->flags &TYPE_FLAGS_PERMISSIVE ) +ebitmap_set_bit (&state->out->permissive_map, new_type->s.value,1 );
ebitmap_set_bit()can fail due to memory allocation failure, so you need to check for it.sure.
Quoted 1 line
Quoted 7 lines
Quoted 3 lines
Somewhere we also need to clearly document that the permissive map is _not_ using zero-based indexing for the types, unlike other ebitmaps in policy.
any idea where to document? Would you prefer I just move all of this to type-1 to keep consistancy? Probably some way to use max_type_number+1 for global if I/we ever do it....
Just a comment above the field in policydb.h would suffice.
Quoted 12 lines
Quoted 12 lines
src/expand.c static int alias_copy_callback(hashtab_k · line 503
Quoted 9 lines
src/expand.c +3
-0@@ -492,6 +498,10 @@ static int alias_copy_callback(hashtab_k } state->typemap[alias->s.value -1 ] = new_alias->s.value; + +if (new_alias->flags &TYPE_FLAGS_PERMISSIVE ) +ebitmap_set_bit (&state->out->permissive_map, new_alias->s.value,1 );Ditto.
Quoted 8 lines
Quoted 3 lines
Have you tested the handling of aliases, e.g. declare a type alias to a type in one module, then mark that type alias as permissive in another module, and confirm proper propagation of the permissive flag?
I tested declare a type in base and then in a module require that base type, alias to that base type, and then permissive the alias. Is there a different between that and actually declaring the type in another module?
The interesting case is when you declare the type and alias in one module, then use that alias as if it were a type in another module. Then checkmodule doesn't know that it is dealing with an alias while compiling the latter module, and the link/expand logic has to properly deal with it. Same issue occurs for attributes.
Quoted 33 lines
Quoted 18 lines
Quoted 18 lines
diff -Naupr
libsepol-2.0.23/src/write.clibsepol-2.0.23.new/src/write.csrc/write.c +2
-0@@ -959,6 +959,8 @@ static int type_write(hashtab_key_t key, buf[items++] =cpu_to_le32 (typdatum->primary);if (p->policy_type !=POLICY_KERN ) { buf[items++] =cpu_to_le32 (typdatum->flavor); +if (p->policyvers >=MOD_POLICYDB_VERSION_PERMISSIVE ) + buf[items++] =cpu_to_le32 (typdatum->flags); } items2 =put_entry (buf,sizeof (uint32_t ), items, fp);if (items != items2)@@ -1618,6 +1620,12 @@ int policydb_write(policydb_t * p, strucreturn POLICYDB_ERROR ; }Quoted 14 lines
src/write.c int policydb_write(policydb_t * p, struc · line 1631
Quoted 9 lines
src/write.c +6
-0+if (p->policyvers >=POLICYDB_VERSION_PERMISSIVE && + p->policy_type ==POLICY_KERN ) { +if (ebitmap_write (&p->permissive_map, fp) ==-1 ) +return POLICYDB_ERROR ; + } + num_syms = info->sym_num;for (i =0 ; i < num_syms; i++) { buf[0 ] =cpu_to_le32 (p->symtab[i].nprim);So in the case of automatic policy downgrade by libselinux load policy logic, we will silently shed any permissive maps. Likely need a warning if the permissive map has any bits set at all to warn the user that he isn't going to get what he expected.
Quoted 1 line
So just some print statements? I'll look for examples....
Something like:WARN(fp->handle, "Warning! Policy version %d cannot support permissive types", p->policyvers);
Signature · 8 lines
Stephen Smalley National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 13 lines · Stephen Smalley
Quoted 11 lines · Eric Paris
Quoted 5 lines · Stephen Smalley
On Tue, 2008-03-11 at 09:59 -0400, Eric Paris wrote:
Have you tested the handling of aliases, e.g. declare a type alias to a type in one module, then mark that type alias as permissive in another module, and confirm proper propagation of the permissive flag?
I tested declare a type in base and then in a module require that base type, alias to that base type, and then permissive the alias. Is there a different between that and actually declaring the type in another module?
Quoted 5 lines
The interesting case is when you declare the type and alias in one module, then use that alias as if it were a type in another module. Then checkmodule doesn't know that it is dealing with an alias while compiling the latter module, and the link/expand logic has to properly deal with it. Same issue occurs for attributes.
Let me know if this is what you wanted tested:
******
module define_alias 1.0;
require {
type type1_t;
}type type_t;
typealias type_t alias alias_t;
*****
module perm_alias 1.0;
require {
type alias_t;
}permissive alias_t;
******
[root@dhcp231-146 perm]# semodule -s tmp -l
No modules.
[root@dhcp231-146 perm]# semodule -s tmp -i define_alias.pp [root@dhcp231-146 perm]# semodule -s tmp -i perm_alias.pp [root@dhcp231-146 perm]# semodule -s tmp -ldefine_alias 1.0perm_alias 1.0
******
[root@dhcp231-146 perm]# sedispol /etc/selinux/tmp/policy/policy.23 Reading policy...
libsepol.policydb_index_others: security: 1 users, 2 roles, 4 types, 1 bools libsepol.policydb_index_others: security: 2 sens, 2 cats libsepol.policydb_index_others: security: 1 classes, 1 rules, 0 cond rules binary policy file loaded
[snip]
Command ('m' for menu): p
permissive sids:
type_t
Command ('m' for menu):
*******
Looks like it worked to me.
-Eric
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 25 lines · Eric Paris
Quoted 13 lines · Stephen Smalley
Quoted 11 lines · Eric Paris
Quoted 5 lines · Stephen Smalley
On Tue, 2008-03-11 at 09:59 -0400, Eric Paris wrote:
Have you tested the handling of aliases, e.g. declare a type alias to a type in one module, then mark that type alias as permissive in another module, and confirm proper propagation of the permissive flag?
I tested declare a type in base and then in a module require that base type, alias to that base type, and then permissive the alias. Is there a different between that and actually declaring the type in another module?
Quoted 5 lines
The interesting case is when you declare the type and alias in one module, then use that alias as if it were a type in another module. Then checkmodule doesn't know that it is dealing with an alias while compiling the latter module, and the link/expand logic has to properly deal with it. Same issue occurs for attributes.
Let me know if this is what you wanted tested:
******
moduledefine_alias1.0;
Quoted 6 lines
require { type type1_t; }type
type_t;
typealiastype_taliasalias_t;
Not sure what that require is for - remnant of earlier test?
Quoted 26 lines
*****
moduleperm_alias1.0;require { type alias_t; }permissive
alias_t;
******
[root@dhcp231-146 perm]# semodule -s tmp -l
No modules.
[root@dhcp231-146 perm]# semodule -s tmp -i define_alias.pp [root@dhcp231-146 perm]# semodule -s tmp -i perm_alias.pp [root@dhcp231-146 perm]# semodule -s tmp -ldefine_alias1.0perm_alias1.0
******
[root@dhcp231-146 perm]# sedispol/etc/selinux/tmp/policy/policy.23Reading policy...
libsepol.policydb_index_others: security: 1 users, 2 roles, 4 types, 1 bools libsepol.policydb_index_others: security: 2 sens, 2 cats libsepol.policydb_index_others: security: 1 classes, 1 rules, 0 cond rules binary policy file loaded[snip]
Quoted 8 lines
Command ('m' for menu): p
permissive sids:
type_tCommand ('m' for menu):
*******Looks like it worked to me.
Yes, looks ok.
Signature · 8 lines
Stephen Smalley National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 5 lines · Stephen Smalley
Quoted 3 lines · Eric Paris
******
moduledefine_alias1.0;
Quoted 8 lines
Quoted 6 lines
require { type type1_t; }type
type_t;
typealiastype_taliasalias_t;Not sure what that require is for - remnant of earlier test?
I couldn't get a module to compile without a requires. So I just threw one in, type1_t is just one of the 2 types I had in my base module.
-Eric
--
This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.
Quoted 10 lines · Eric Paris
Quoted 9 lines · Stephen Smalley
Quoted 7 lines · Eric Paris
******
moduledefine_alias1.0;require { type type1_t; }
Quoted 7 lines
Quoted 4 lines
Quoted 2 lines
type
type_t;
typealiastype_taliasalias_t;Not sure what that require is for - remnant of earlier test?
I couldn't get a module to compile without a requires. So I just threw one in,
type1_tis just one of the 2 types I had in my base module.
Ok - limitation of checkmodule then. Most policy modules automatically pick up at least requires on kernel classes/perms via policy_module() interface.
Signature · 8 lines
Stephen Smalley National Security Agency -- This message was distributed to subscribers of the selinux mailing list. If you no longer wish to subscribe, send mail to [email protected] with the words "unsubscribe selinux" without quotes as the message.