Ability to allow unknown class and permissions -v4
RFC 1/1 Ability to allow unknown class and permissions -v4
I need 4+ RFC's for a small patch, I'm sure everyone is tired of this darn thing. But this is a possibly a finished kernel patch (against linus git as of today) to allow a policy flag to specify how the kernel should handle a policy which does not cover all of the potential kernel checks. The three options are
0 - allow the policy to load but deny all checks (applies to old policy) 1 - reject the policy. load return with EINVAL
2 - allow the policy to load and allow all unknown checks
Note that since the flags in old policy are all zero (outside of the MLS flag) running an old policy on a new kernel means there will be no behavioral change from how things are today. I'm still fighting with userspace and should have those patches out soon, but as of this moment I've been testing by using a hex editor on the policy.21 by hand, changing the flags, and then rebooting. (If I use any userspace tools like load_policy it is blanking the flags on me) All three options seem to be working as expected. But I do need a lot more testing especially to make sure everything works when i switch between the options on reloads.
-Eric
---
security/selinux/ss/policydb.c | 4 ++ security/selinux/ss/policydb.h | 13 +++++++ security/selinux/ss/services.c | 79 ++++++++++++++++++++++++++++++++++------ 3 files changed, 85 insertions(+), 11 deletions(-)
security/selinux/ss/policydb.c +4 -0
@@ -670,6 +670,8 @@ void policydb_destroy(struct policydb *p) }kfree (p->type_attr_map); +kfree (p->undefined_perms); +return ; }
@@ -1523,6 +1525,8 @@ int policydb_read(struct policydb *p, void *fp) goto bad; } } + p->handle_unknown =le32_to_cpu (buf[1 ]) &POLICYDB_CONFIG_UNKNOWN_MASK ; + p->handle_unknown >>=POLICYDB_CONFIG_UNKNOWN_SHIFT ; info =policydb_lookup_compat (p->policyvers);if (!info) {
security/selinux/ss/policydb.h +13 -0
@@ -242,6 +242,9 @@ struct policydb {struct ebitmap *type_attr_map;unsigned int policyvers; + +int handle_unknown; +u32 *undefined_perms; };extern void policydb_destroy (struct policydb *p);
@@ -253,6 +256,16 @@ extern int policydb_read(struct policydb *p, void *fp);#define POLICYDB_CONFIG_MLS 1 +/* the config flags related to unknown classes/perms are bits 2 and 3 */ +#define POLICYDB_CONFIG_UNKNOWN_MASK 6 +#define POLICYDB_CONFIG_UNKNOWN_SHIFT 1 + +enum policy_with_unknown_perms { +DENY_UNKNOWN =0 , +REJECT_UNKNOWN =1 , +ALLOW_UNKNOWN =2 +}; +#define OBJECT_R "object_r"#define OBJECT_R_VAL 1
security/selinux/ss/services.c +68 -11
139 lines not on this page · raw message
--
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 45 lines · Eric Paris
I need 4+ RFC's for a small patch, I'm sure everyone is tired of this darn thing. But this is a possibly a finished kernel patch (against linus git as of today) to allow a policy flag to specify how the kernel should handle a policy which does not cover all of the potential kernel checks. The three options are
0 - allow the policy to load but deny all checks (applies to old policy) 1 - reject the policy. load return with EINVAL
2 - allow the policy to load and allow all unknown checksNote that since the flags in old policy are all zero (outside of the MLS flag) running an old policy on a new kernel means there will be no behavioral change from how things are today. I'm still fighting with userspace and should have those patches out soon, but as of this moment I've been testing by using a hex editor on the policy.21 by hand, changing the flags, and then rebooting. (If I use any userspace tools like
load_policyit is blanking the flags on me) All three options seem to be working as expected. But I do need a lot more testing especially to make sure everything works when i switch between the options on reloads.(quoted diff) +0
-2-Eric --- security/selinux/ss/policydb.c | 4 ++ security/selinux/ss/policydb.h | 13 +++++++ security/selinux/ss/services.c | 79 ++++++++++++++++++++++++++++++++++------ 3 files changed, 85 insertions(+), 11 deletions(-)security/selinux/ss/services.c +7
-0@@ -322,6 +316,40 @@ static int context_struct_compute_av(struct context *scontext, avd->seqno = latest_granting;/* +* Check if the class in question is a kernel class and if it +* is defined in policy. If yes to both it will pad the allow +* for undefined perms if appropriate. +*/ +if (unlikely (!tclass)) + goto inval_class; +if (unlikely (tclass > policydb.p_classes.nprim)) {
security/selinux/ss/services.c static int context_struct_compute_av(struct context *scontext, · line 335
Quoted 10 lines
security/selinux/ss/services.c +10
-0+if (tclass > kdefs->cts_len || (policydb.handle_unknown ==DENY_UNKNOWN )) + goto inval_class; +/* +* kernel class not in policy, but we +* allow unknown, so let everything through +*/ +if (kdefs->class_to_string[tclass -1 ]) { + avd->allowed =0xffffffff ; +return 0 ; + }
Do you need to special case this vs. just filling in policydb.undefined_perms[] with ~0UL when the kernel class is not in policy and using it always? With some rearrangement of the code to avoid duplication?
Especially since you allocate the array to the number of kernel classes.
Quoted 46 lines
security/selinux/ss/services.c +28
-0+/* +* this is a 'hole' in policy where a userspace +* class exists +*/ + goto inval_class; + } +if (tclass < kdefs->cts_len && (policydb.handle_unknown ==ALLOW_UNKNOWN )) +/* +* kernel class, defined in policy, allow unknown. +* might have undefined permissions, so default +* those perms to allow +*/ + avd->allowed = policydb.undefined_perms[tclass -1 ]; + + tclass_datum = policydb.class_val_to_struct[tclass -1 ]; + +/* * If a specific type enforcement rule was defined for * this permission check, then use it. */ @@ -387,6 +415,10 @@ static int context_struct_compute_av(struct context *scontext, }return 0 ; + +inval_class: +printk (KERN_ERR "security_compute_av: unrecognized class %d\n" , tclass); +return -EINVAL ; } static int security_validtrans_handle_fail(struct context *ocontext,@@ -1054,6 +1086,13 @@ static int validate_classes(struct policydb *p)const char *def_class, *def_perm, *pol_class;struct symtab *perms; +if (p->handle_unknown ==ALLOW_UNKNOWN ) { +u32 num_classes = kdefs->cts_len; + p->undefined_perms =kcalloc (num_classes,sizeof (u32),GFP_KERNEL ); +if (!p->undefined_perms) +return -ENOMEM ; + } +for (i =1 ; i < kdefs->cts_len; i++) { def_class = kdefs->class_to_string[i];if (!def_class)
security/selinux/ss/services.c static int validate_classes(struct policydb *p) · line 1105
Quoted 5 lines
security/selinux/ss/services.c +2
-0@@ -1062,6 +1101,8 @@ static int validate_classes(struct policydb *p)printk (KERN_INFO "security: class %s not defined in policy\n" , def_class); +if (p->handle_unknown ==REJECT_UNKNOWN ) +return -EINVAL ;
e.g. set p->undefined_perms[i-1] = ~0UL; here.
Quoted 15 lines
security/selinux/ss/services.c +4
-0continue ; } pol_class = p->p_class_val_to_name[i-1 ];@@ -1087,14 +1128,22 @@ static int validate_classes(struct policydb *p)printk (KERN_INFO "security: permission %s in class %s not defined in policy\n" , def_perm, pol_class); +if (p->handle_unknown ==ALLOW_UNKNOWN ) + p->undefined_perms[class_val-1 ] |= perm_val; +else if (p->handle_unknown ==REJECT_UNKNOWN ) +return -EINVAL ;continue ; } perdatum =hashtab_search (perms->table, def_perm);if (perdatum ==NULL ) {
security/selinux/ss/services.c static int validate_classes(struct policydb *p) · line 1146
Quoted 10 lines
security/selinux/ss/services.c +6
-2-printk (KERN_ERR +printk (KERN_INFO "security: permission %s in class %s not found in policy\n" , def_perm, pol_class); -return -EINVAL ; +if (p->handle_unknown ==ALLOW_UNKNOWN ) + p->undefined_perms[class_val-1 ] |= perm_val; +else if (p->handle_unknown ==REJECT_UNKNOWN ) +return -EINVAL ; +continue ;
I think this likely has to stay fatal, and may reflect a problem with the tristate flag idea. Here we have a case where the permission value was defined in the policy but the string name was not, suggesting a disruption in the access vector definitions (e.g. re-use of the permission bit? removal or addition of earlier permissions causing displacement?).
Quoted 15 lines
security/selinux/ss/services.c +4
-0} pol_val =1 << (perdatum->value -1 );if (pol_val != perm_val) {@@ -1130,14 +1179,22 @@ static int validate_classes(struct policydb *p)printk (KERN_INFO "security: permission %s in class %s not defined in policy\n" , def_perm, pol_class); +if (p->handle_unknown ==ALLOW_UNKNOWN ) + p->undefined_perms[class_val-1 ] |= (1 << j); +else if (p->handle_unknown ==REJECT_UNKNOWN ) +return -EINVAL ;continue ; } perdatum =hashtab_search (perms->table, def_perm);if (perdatum ==NULL ) {
security/selinux/ss/services.c static int validate_classes(struct policydb *p) · line 1197
Quoted 10 lines
security/selinux/ss/services.c +6
-2-printk (KERN_ERR +printk (KERN_INFO "security: permission %s in class %s not found in policy\n" , def_perm, pol_class); -return -EINVAL ; +if (p->handle_unknown ==ALLOW_UNKNOWN ) + p->undefined_perms[class_val-1 ] |= (1 << j); +else if (p->handle_unknown ==REJECT_UNKNOWN ) +return -EINVAL ; +continue ;
Ditto.
Quoted 10 lines
security/selinux/ss/services.c +0
-1}if (perdatum->value != j +1 ) { printk(KERN_ERR --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.
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 46 lines · Stephen Smalley
Quoted 15 lines · Eric Paris
security/selinux/ss/services.c +7
-0@@ -322,6 +316,40 @@ static int context_struct_compute_av(struct context *scontext, avd->seqno = latest_granting;/* +* Check if the class in question is a kernel class and if it +* is defined in policy. If yes to both it will pad the allow +* for undefined perms if appropriate. +*/ +if (unlikely (!tclass)) + goto inval_class; +if (unlikely (tclass > policydb.p_classes.nprim)) {security/selinux/ss/services.c static int context_struct_compute_av(struct context *scontext, · line 335
Quoted 10 lines
security/selinux/ss/services.c +10
-0+if (tclass > kdefs->cts_len || (policydb.handle_unknown ==DENY_UNKNOWN )) + goto inval_class; +/* +* kernel class not in policy, but we +* allow unknown, so let everything through +*/ +if (kdefs->class_to_string[tclass -1 ]) { + avd->allowed =0xffffffff ; +return 0 ; + }Do you need to special case this vs. just filling in policydb.undefined_perms[] with ~0UL when the kernel class is not in policy and using it always? With some rearrangement of the code to avoid duplication?
Especially since you allocate the array to the number of kernel classes.Quoted 12 lines
security/selinux/ss/services.c +12
-0+/* +* this is a 'hole' in policy where a userspace +* class exists +*/ + goto inval_class; + } +if (tclass < kdefs->cts_len && (policydb.handle_unknown ==ALLOW_UNKNOWN )) +/* +* kernel class, defined in policy, allow unknown. +* might have undefined permissions, so default +* those perms to allow +*/
Quoted 8 lines
security/selinux/ss/services.c static int context_struct_compute_av(struct context *scontext, · line 355
Quoted 8 lines
security/selinux/ss/services.c +5
-0+ avd->allowed = policydb.undefined_perms[tclass -1 ]; + + tclass_datum = policydb.class_val_to_struct[tclass -1 ]; + +/* * If a specific type enforcement rule was defined for * this permission check, then use it. */
It does get rid of the special casing but it isn't much less complex. Anyone see a way to make this bit better?
-Eric
@@ -322,6 +316,38 @@ static int context_struct_compute_av(struct context *scontext,
avd->seqno = latest_granting;
/*
+ * Check for all the invalid cases.
+ * - tclass 0
+ * - tclass > policy and > kernel
+ * - tclass > policy but is a userspace class
+ * - tclass > policy but we do not allow unknowns
+ */
+ if (unlikely(!tclass))
+ goto inval_class;
+ if (unlikely(tclass > policydb.p_class.nprim))
+ if (tclass > kdefs->cts_len ||
+ !kdefs->class_to_string[tclass - 1] ||
+ policydb.handle_unknown != ALLOW_UNKOWN)
+ goto inval_class;
+
+ /*
+ * Kernel class and we ALLOW_UNKNOWN so pad the allow decision
+ * the pad will be all 1 for unknown classes.
+ */
+ if (tclass <= kdefs->cts_len && (policydb.handle_unknown == ALLOW_UNKNOWN))
+ avd->allowed = policydb.undefined_perms[tclass - 1];
+
+ /*
+ * Kernel class not in policy and we ALLOW_UNKNOWN. Since decision is
+ * completed return.
+ */
+ if (unlikely(tclass > policydb.p_class.nprim &&
+ tclass <= kdefs->cts_len))
+ return 0;
+
+ tclass_datum = policydb.class_val_to_struct[tclass - 1];
+
+ /*
* If a specific type enforcement rule was defined for
* this permission check, then use it.
*/
--
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 27 lines · Eric Paris
It does get rid of the special casing but it isn't much less complex. Anyone see a way to make this bit better?
(quoted diff) +17
-1-Eric@@ -322,6 +316,38 @@ static int context_struct_compute_av(struct context *scontext, avd->seqno = latest_granting; /* + * Check for all the invalid cases. + * - tclass 0 + * - tclass > policy and > kernel + * - tclass > policy but is a userspace class + * - tclass > policy but we do not allow unknowns + */ + if (unlikely(!tclass)) + goto inval_class; + if (unlikely(tclass > policydb.p_class.nprim)) + if (tclass > kdefs->cts_len || + !kdefs->class_to_string[tclass - 1] || + policydb.handle_unknown != ALLOW_UNKOWN) + goto inval_class; + + /* + * Kernel class and we ALLOW_UNKNOWN so pad the allow decision + * the pad will be all 1 for unknown classes.
Review static int context_struct_compute_av(struct context *scontext, · line 345
Quoted 10 lines
(quoted diff) +10
-0+ */ + if (tclass <= kdefs->cts_len && (policydb.handle_unknown == ALLOW_UNKNOWN)) + avd->allowed = policydb.undefined_perms[tclass - 1]; + + /* + * Kernel class not in policy and we ALLOW_UNKNOWN. Since decision is + * completed return. + */ + if (unlikely(tclass > policydb.p_class.nprim && + tclass <= kdefs->cts_len))
You can drop the second test (tclass <= kdefs->cts_len) or turn it into a BUG_ON, because you should only reach this point if that is true. And you must ensure that you don't go further if the first test is true regardless, as you will otherwise index beyond the end of the class_val_to_struct array.
Quoted 9 lines
security/selinux/ss/services.c +5
-0+return 0 ; + + tclass_datum = policydb.class_val_to_struct[tclass -1 ]; + +/* * If a specific type enforcement rule was defined for * this permission check, then use it. */
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.