SELinux: Validate kernel object classes and permissions
cover SELinux: Validate kernel object classes and permissions
This replaces the object class and permission validation code in the SELinux security server. The current code validates that the object classes and permissions have not changed between policy loads. This requires a simple reboot to circumvent, and more importantly does not allow for any change in object classes or permissions upon reload (say if a new userspace object manager was added to the system).
The new logic simply verifies the values of object classes and permissions against the kernel headers in security/selinux/include. If a class or permission in the new policy that is defined in the kernel headers does not match the kernel headers, then policy will fail to load. Note that there is no longer any comparison between the old policy and the new policy.
--
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.
PATCH 1/4 SELinux: remove current object class and permission validation mechanism
Removes the current SELinux object class and permission validation code, as the current code makes it impossible to change or remove object classes and permissions on a running system. Additionally, the current code does not actually validate that the classes and permissions are correct, but instead merely validates that they do not change between policy reloads.
---
Note that this validation code is replaced in the fourth patch of this set, but I removed it completely here to make the patches more readable.
--- security/selinux/ss/services.c | 91 ----------------------------------------- 1 file changed, 91 deletions(-)
security/selinux/ss/services.c +0 -91
@@ -1018,89 +1018,6 @@ int security_change_sid(u32 ssid,return security_compute_sid (ssid, tsid, tclass,AVTAB_CHANGE , out_sid); } -/* -* Verify that each permission that is defined under the -* existing policy is still defined with the same value -* in the new policy. -*/ -static int validate_perm (void *key,void *datum,void *p) -{ -struct hashtab *h; -struct perm_datum *perdatum, *perdatum2; -int rc =0 ; - - - h = p; - perdatum = datum; - - perdatum2 =hashtab_search (h, key); -if (!perdatum2) { -printk (KERN_ERR "security: permission %s disappeared" , - (char *)key); - rc = -ENOENT ; - goto out; - } -if (perdatum->value != perdatum2->value) { -printk (KERN_ERR "security: the value of permission %s changed" , - (char *)key); - rc = -EINVAL ; - } -out: -return rc; -} - -/* -* Verify that each class that is defined under the -* existing policy is still defined with the same -* attributes in the new policy. -*/ -static int validate_class (void *key,void *datum,void *p) -{ -struct policydb *newp; -struct class_datum *cladatum, *cladatum2; -int rc; - - newp = p; - cladatum = datum; - - cladatum2 =hashtab_search (newp->p_classes.table, key); -if (!cladatum2) { -printk (KERN_ERR "security: class %s disappeared\n" , - (char *)key); - rc = -ENOENT ; - goto out; - } -if (cladatum->value != cladatum2->value) { -printk (KERN_ERR "security: the value of class %s changed\n" , - (char *)key); - rc = -EINVAL ; - goto out; - } -if ((cladatum->comdatum && !cladatum2->comdatum) || - (!cladatum->comdatum && cladatum2->comdatum)) { -printk (KERN_ERR "security: the inherits clause for the access " -"vector definition for class %s changed\n" , (char *)key); - rc = -EINVAL ; - goto out; - } -if (cladatum->comdatum) { - rc =hashtab_map (cladatum->comdatum->permissions.table, validate_perm, - cladatum2->comdatum->permissions.table); -if (rc) { -printk (" in the access vector definition for class " -"%s\n" , (char *)key); - goto out; - } - } - rc =hashtab_map (cladatum->permissions.table, validate_perm, - cladatum2->permissions.table); -if (rc) -printk (" in access vector definition for class %s\n" , - (char *)key); -out: -return rc; -} -/* Clone the SID into the new SID table. */ static int clone_sid(u32 sid, struct context *context,
@@ -1265,14 +1182,6 @@ int security_load_policy(void *data, sizsidtab_init (&newsidtab); -/* Verify that the existing classes did not change. */ -if (hashtab_map (policydb.p_classes.table, validate_class, &newpolicydb)) { -printk (KERN_ERR "security: the definition of an existing " -"class changed\n" ); - rc = -EINVAL ; - goto err; - } -/* Clone the SID table. */ sidtab_shutdown (&sidtab);if (sidtab_map (&sidtab, clone_sid, &newsidtab)) {
Signature · 4 lines
-- 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.
PATCH 2/4 SELinux: export object class and permission definitions
In reply to Chad SellersMoves the definition of the 3 structs containing object class and permission definitions from avc.c to avc_ss.h so that the security server can access them for validation on policy load. This also adds a new struct type, defined_classes_perms_t, suitable for allowing the security server to access these data structures from the avc.
--- security/selinux/avc.c | 23 +++++++++++------------ security/selinux/include/avc_ss.h | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-)
security/selinux/avc.c +11 -12
@@ -32,12 +32,7 @@#include "avc.h" #include "avc_ss.h" -static const struct av_perm_to_string -{ -u16 tclass; -u32 value; -const char *name; -} av_perm_to_string[] = { +static const struct av_perm_to_string_t av_perm_to_string[] = { #define S_(c, v, s) { c, v, s }, #include "av_perm_to_string.h" #undef S_
@@ -57,17 +52,21 @@ static const char *class_to_string[] = {#undef TE_#undef S_ -static const struct av_inherit -{ -u16 tclass; -const char **common_pts; -u32 common_base; -} av_inherit[] = { +static const struct av_inherit_t av_inherit[] = { #define S_(c, i, b) { c, common_##i##_perm_to_string, b }, #include "av_inherit.h" #undef S_ }; +const struct defined_classes_perms_t defined_classes_perms = { + av_perm_to_string, +ARRAY_SIZE (av_perm_to_string), + class_to_string, +ARRAY_SIZE (class_to_string), + av_inherit, +ARRAY_SIZE (av_inherit) +}; +#define AVC_CACHE_SLOTS 512#define AVC_DEF_CACHE_THRESHOLD 512#define AVC_CACHE_RECLAIM 16
security/selinux/include/avc_ss.h +24 -0
@@ -10,5 +10,29 @@int avc_ss_reset (u32 seqno); +struct av_perm_to_string_t +{ +u16 tclass; +u32 value; +const char *name; +}; + +struct av_inherit_t +{ +u16 tclass; +const char **common_pts; +u32 common_base; +}; + +struct defined_classes_perms_t +{ +const struct av_perm_to_string_t *av_perm_to_string; +u32 av_perm_to_string_len; +const char **class_to_string; +u32 class_to_string_len; +const struct av_inherit_t *av_inherit; +u32 av_inherit_len; +}; +#endif /* _SELINUX_AVC_SS_H_ */
Signature · 4 lines
-- 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.
Re: [PATCH 2/4] SELinux: export object class and permission definitions
Quoted 35 lines · Chad Sellers
plain text document attachment (export_classes_perms.diff)
Moves the definition of the 3 structs containing object class and permission definitions fromavc.ctoavc_ss.hso that the security server can access them for validation on policy load. This also adds a new struct type,defined_classes_perms_t, suitable for allowing the security server to access these data structures from the avc.Signed-off-by Chad Sellers <[email protected]>--- security/selinux/avc.c | 23 +++++++++++------------ security/selinux/include/avc_ss.h | 24 ++++++++++++++++++++++++ 2 files changed, 35 insertions(+), 12 deletions(-)Index:
linux-2.6-discovery/security/selinux/avc.c
===================================================================security/selinux/avc.c +1
-9@@ -32,12 +32,7 @@#include "avc.h" #include "avc_ss.h" -static const struct av_perm_to_string -{ -u16 tclass; -u32 value; -const char *name; -} av_perm_to_string[] = { +static const struct av_perm_to_string_t av_perm_to_string[] = { #define S_(c, v, s) { c, v, s }, #include "av_perm_to_string.h" #undef S_@@ -57,17 +52,21 @@ static const char *class_to_string[] = {#undef TE_#undef S_ -static const struct av_inherit -{ - u16 tclass;
security/selinux/avc.c static const char *class_to_string[] = { · line 61
Quoted 10 lines
security/selinux/avc.c +2
-3-const char **common_pts; -u32 common_base; -} av_inherit[] = { +static const struct av_inherit_t av_inherit[] = { #define S_(c, i, b) { c, common_##i##_perm_to_string, b }, #include "av_inherit.h" #undef S_ }; +const struct defined_classes_perms_t defined_classes_perms = {
- Don't use _t in struct names.
- Use a name for the variable that provides good namespacing (e.g. selinux_) but isn't so long as to induce RSI.
-
Quoted 16 lines
security/selinux/avc.c +8
-0+ av_perm_to_string, +ARRAY_SIZE (av_perm_to_string), + class_to_string, +ARRAY_SIZE (class_to_string), + av_inherit, + ARRAY_SIZE(av_inherit) +}; +#define AVC_CACHE_SLOTS 512#define AVC_DEF_CACHE_THRESHOLD 512#define AVC_CACHE_RECLAIM 16Index:
linux-2.6-discovery/security/selinux/include/avc_ss.h
===================================================================
security/selinux/include/avc_ss.h line 13
Quoted 4 lines
security/selinux/include/avc_ss.h +1
-0@@ -10,5 +10,29 @@int avc_ss_reset (u32 seqno); +struct av_perm_to_string_t
Don't rename these structs.
Quoted 7 lines
security/selinux/include/avc_ss.h +7
-0+{ +u16 tclass; +u32 value; +const char *name; +}; + +struct av_inherit_t
security/selinux/include/avc_ss.h line 30
Quoted 10 lines
security/selinux/include/avc_ss.h +10
-0+{ +u16 tclass; +const char **common_pts; +u32 common_base; +}; + +struct defined_classes_perms_t +{ +const struct av_perm_to_string_t *av_perm_to_string; +u32 av_perm_to_string_len;
Don't go overkill on field name length.
Quoted 9 lines
security/selinux/include/avc_ss.h +6
-0+const char **class_to_string; +u32 class_to_string_len; +const struct av_inherit_t *av_inherit; + u32 av_inherit_len; +}; +#endif /* _SELINUX_AVC_SS_H_ */
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.
PATCH 3/4 SELinux: ensure keys constant in hashtab_search
In reply to Chad SellersMakes the key argument passed into hashtab_search and all the functions it calls constant. These functions include hash table function pointers hash_value and keycmp. The only implementations of these currently are symhash and symcmp, which do not modify the key. The key parameter should never be changed by any of these, so it should be const. This is necessary to allow calling these functions with keys found in kernel object class and permission definitions.
--- security/selinux/ss/hashtab.c | 6 +++--- security/selinux/ss/hashtab.h | 10 +++++----- security/selinux/ss/symtab.c | 8 ++++---- 3 files changed, 12 insertions(+), 12 deletions(-)
security/selinux/ss/hashtab.c +3 -3
@@ -8,8 +8,8 @@#include <linux/errno.h> #include "hashtab.h" -struct hashtab *hashtab_create (u32 (*hash_value)(struct hashtab *h,void *key), -int (*keycmp)(struct hashtab *h,void *key1,void *key2), +struct hashtab *hashtab_create (u32 (*hash_value)(struct hashtab *h,const void *key), +int (*keycmp)(struct hashtab *h,const void *key1,const void *key2),u32 size) {struct hashtab *p;
@@ -71,7 +71,7 @@ int hashtab_insert(struct hashtab *h, voreturn 0 ; } -void *hashtab_search (struct hashtab *h,void *key) +void *hashtab_search (struct hashtab *h,const void *key) {u32 hvalue;struct hashtab_node *cur;
security/selinux/ss/hashtab.h +5 -5
@@ -22,9 +22,9 @@ struct hashtab {struct hashtab_node **htable;/* hash table */ u32 size;/* number of slots in hash table */ u32 nel;/* number of elements in hash table */ -u32 (*hash_value)(struct hashtab *h,void *key); +u32 (*hash_value)(struct hashtab *h,const void *key);/* hash function */ -int (*keycmp)(struct hashtab *h,void *key1,void *key2); +int (*keycmp)(struct hashtab *h,const void *key1,const void *key2);/* key comparison function */ };
@@ -39,8 +39,8 @@ struct hashtab_info {* Returns NULL if insufficent space is available or * the new hash table otherwise. */ -struct hashtab *hashtab_create (u32 (*hash_value)(struct hashtab *h,void *key), -int (*keycmp)(struct hashtab *h,void *key1,void *key2), +struct hashtab *hashtab_create (u32 (*hash_value)(struct hashtab *h,const void *key), +int (*keycmp)(struct hashtab *h,const void *key1,const void *key2),u32 size);/*
@@ -59,7 +59,7 @@ int hashtab_insert(struct hashtab *h, vo* Returns NULL if no entry has the specified key or * the datum of the entry otherwise. */ -void *hashtab_search (struct hashtab *h,void *k); +void *hashtab_search (struct hashtab *h,const void *k);/* * Destroys the specified hash table.
security/selinux/ss/symtab.c +4 -4
@@ -9,9 +9,9 @@#include <linux/errno.h> #include "symtab.h" -static unsigned int symhash (struct hashtab *h,void *key) +static unsigned int symhash (struct hashtab *h,const void *key) { -char *p, *keyp; +const char *p, *keyp;unsigned int size;unsigned int val;
@@ -23,9 +23,9 @@ static unsigned int symhash(struct hashtreturn val & (h->size -1 ); } -static int symcmp (struct hashtab *h,void *key1,void *key2) +static int symcmp (struct hashtab *h,const void *key1,const void *key2) { -char *keyp1, *keyp2; +const char *keyp1, *keyp2; keyp1 = key1; keyp2 = key2;
Signature · 4 lines
-- 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.
PATCH 4/4 SELinux: validate kernel object classes and permissions
In reply to Chad SellersThis is a new object class and permission validation scheme that validates against the defined kernel headers. This scheme allows extra classes and permissions that do not conflict with the kernel definitions to be added to the policy. This validation is now done for all policy loads, not just subsequent loads after the first policy load.
The implementation walks the three structrures containing the defined object class and permission values and ensures their values are the same in the policy being loaded. This includes verifying the object classes themselves, the permissions they contain, and the permissions they inherit from commons. Classes or permissions that are present in the kernel but missing from the policy cause a warning (printed to KERN_INFO) to be printed, but do not stop the policy from loading, emulating current behavior. Any other inconsistencies cause the load to fail.
--- security/selinux/ss/services.c | 159 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 1 deletion(-)
security/selinux/ss/services.c +158 -1
@@ -17,9 +17,13 @@* * Added support for NetLabel * +* Updated: Chad Sellers <[email protected]> +* +* Added validation of kernel classes and permissions +* * Copyright (C) 2006 Hewlett-Packard Development Company, L.P. * Copyright (C) 2004-2006 Trusted Computer Solutions, Inc. -* Copyright (C) 2003 - 2004 Tresys Technology, LLC +* Copyright (C) 2003 - 2004, 2006 Tresys Technology, LLC * Copyright (C) 2003 Red Hat, Inc., James Morris <[email protected]> * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by
@@ -53,6 +57,11 @@extern void selnl_notify_policyload (u32 seqno);unsigned int policydb_loaded_version; +/* +* This is declared in avc.c +*/ +extern const struct defined_classes_perms_t defined_classes_perms; +static DEFINE_RWLOCK (policy_rwlock);#define POLICY_RDLOCK read_lock(&policy_rwlock)#define POLICY_WRLOCK write_lock_irq(&policy_rwlock)
@@ -1018,6 +1027,136 @@ int security_change_sid(u32 ssid,return security_compute_sid (ssid, tsid, tclass,AVTAB_CHANGE , out_sid); } +/* +* Verify that each kernel class that is defined in the +* policy is correct +*/ +static int validate_classes (struct policydb *p) +{ +int i, j; +struct class_datum *cladatum; +struct perm_datum *perdatum; +u32 val, tmp, common_pts_len; + +for (i =1 ; i < defined_classes_perms.class_to_string_len; i++) { +if (i > p->p_classes.nprim) { +printk (KERN_INFO +"security: class %s not defined in policy\n" , + defined_classes_perms.class_to_string[i]); +continue ; + } +if (strcmp (p->p_class_val_to_name[i-1 ], + defined_classes_perms.class_to_string[i])) { +printk (KERN_ERR +"security: class number %d is incorrect, found %s but should be %s\n" , + i, p->p_class_val_to_name[i-1 ], + defined_classes_perms.class_to_string[i]); +return -EINVAL ; + } + cladatum =hashtab_search (p->p_classes.table, + defined_classes_perms.class_to_string[i]); +if (cladatum ==NULL ) { +printk (KERN_ERR "security: class %s not found in policy\n" , + defined_classes_perms.class_to_string[i]); +return -EINVAL ; + } +if (i != cladatum->value) { +printk (KERN_ERR +"security: class %s has incorrect value, found %d but should be %d\n" , + defined_classes_perms.class_to_string[i], + cladatum->value, i); +return -EINVAL ; + } + } +for (i =0 ; i < defined_classes_perms.av_perm_to_string_len; i++) { +if (defined_classes_perms.av_perm_to_string[i].tclass > p->p_classes.nprim) +continue ; + cladatum =hashtab_search (p->p_classes.table, + p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1 ]); +if (cladatum ==NULL ) { +printk (KERN_ERR +"security: class %s for permission %s not found in policy\n" , + p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1 ], + defined_classes_perms.av_perm_to_string[i].name); +return -EINVAL ; + } + val =1 << (cladatum->permissions.nprim -1 ); +if (defined_classes_perms.av_perm_to_string[i].value > val) { +printk (KERN_INFO +"security: permission %s in class %s not defined in policy\n" , + defined_classes_perms.av_perm_to_string[i].name, + p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1 ]); +continue ; + } + perdatum =hashtab_search (cladatum->permissions.table, + defined_classes_perms.av_perm_to_string[i].name); +if (perdatum ==NULL ) { +printk (KERN_ERR +"security: permission %s in class %s not found in policy\n" , + defined_classes_perms.av_perm_to_string[i].name, + p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1 ]); +return -EINVAL ; + } + val =1 << (perdatum->value -1 ); +if (val != defined_classes_perms.av_perm_to_string[i].value) { +printk (KERN_ERR +"security: permission %s in class %s has incorrect value\n" , + defined_classes_perms.av_perm_to_string[i].name, + p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1 ]); +return -EINVAL ; + } + } +for (i =0 ; i < defined_classes_perms.av_inherit_len; i++) { +if (defined_classes_perms.av_inherit[i].tclass > p->p_classes.nprim) +continue ; + cladatum =hashtab_search (p->p_classes.table, + p->p_class_val_to_name[defined_classes_perms.av_inherit[i].tclass-1 ]); +if (cladatum ==NULL ) { +printk (KERN_ERR "security: class %s not found in policy\n" , + p->p_class_val_to_name[defined_classes_perms.av_inherit[i].tclass-1 ]); +return -EINVAL ; + } +if (!cladatum->comdatum) { +printk (KERN_ERR +"security: class %s should have an inherits clause but does not\n" , + p->p_class_val_to_name[defined_classes_perms.av_inherit[i].tclass-1 ]); +return -EINVAL ; + } + tmp = defined_classes_perms.av_inherit[i].common_base; + common_pts_len =0 ; +while (!(tmp &0x01 )) { + common_pts_len++; + tmp >>=1 ; + } +for (j =0 ; j < common_pts_len; j++) { +if (j >= cladatum->comdatum->permissions.nprim) { +printk (KERN_INFO +"security: permission %s in class %s not defined in policy\n" , + defined_classes_perms.av_inherit[i].common_pts[j], + p->p_class_val_to_name[defined_classes_perms.av_inherit[i].tclass-1 ]); +continue ; + } + perdatum =hashtab_search (cladatum->comdatum->permissions.table, + defined_classes_perms.av_inherit[i].common_pts[j]); +if (perdatum ==NULL ) { +printk (KERN_ERR +"security: permission %s in class %s not found in policy\n" , + defined_classes_perms.av_inherit[i].common_pts[j], + p->p_class_val_to_name[defined_classes_perms.av_inherit[i].tclass-1 ]); +return -EINVAL ; + } +if (perdatum->value != j +1 ) { +printk (KERN_ERR +"security: permission %s in class %s has incorrect value\n" , + defined_classes_perms.av_inherit[i].common_pts[j], + p->p_class_val_to_name[defined_classes_perms.av_inherit[i].tclass-1 ]); +return -EINVAL ; + } + } + } +return 0 ; +} +/* Clone the SID into the new SID table. */ static int clone_sid(u32 sid, struct context *context,
@@ -1160,6 +1299,16 @@ int security_load_policy(void *data, sizavtab_cache_destroy ();return -EINVAL ; } +/* Verify that the kernel defined classes are correct. */ +if (validate_classes (&policydb)) { +printk (KERN_ERR +"security: the definition of a class is incorrect\n" ); +LOAD_UNLOCK ; +sidtab_destroy (&sidtab); +policydb_destroy (&policydb); +avtab_cache_destroy (); +return -EINVAL ; + } policydb_loaded_version = policydb.policyvers; ss_initialized =1 ; seqno = ++latest_granting;
@@ -1182,6 +1331,14 @@ int security_load_policy(void *data, sizsidtab_init (&newsidtab); +/* Verify that the kernel defined classes are correct. */ +if (validate_classes (&newpolicydb)) { +printk (KERN_ERR +"security: the definition of a class is incorrect\n" ); + rc = -EINVAL ; + goto err; + } +/* Clone the SID table. */ sidtab_shutdown (&sidtab);if (sidtab_map (&sidtab, clone_sid, &newsidtab)) {
Signature · 4 lines
-- 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.
Re: [PATCH 4/4] SELinux: validate kernel object classes and permissions
Quoted 19 lines · Chad Sellers
plain text document attachment (verify_kernel.diff)
This is a new object class and permission validation scheme that validates against the defined kernel headers. This scheme allows extra classes and permissions that do not conflict with the kernel definitions to be added to the policy. This validation is now done for all policy loads, not just subsequent loads after the first policy load.The implementation walks the three structrures containing the defined object class and permission values and ensures their values are the same in the policy being loaded. This includes verifying the object classes themselves, the permissions they contain, and the permissions they inherit from commons. Classes or permissions that are present in the kernel but missing from the policy cause a warning (printed to
KERN_INFO) to be printed, but do not stop the policy from loading, emulating current behavior. Any other inconsistencies cause the load to fail.Signed-off-by Chad Sellers <[email protected]>--- security/selinux/ss/services.c | 159 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 1 deletion(-)Index:
linux-2.6-discovery/security/selinux/ss/services.c
===================================================================security/selinux/ss/services.c +0
-0@@ -1018,6 +1027,136 @@ int security_change_sid(u32 ssid,return security_compute_sid (ssid, tsid, tclass,AVTAB_CHANGE , out_sid); }
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1039
Quoted 10 lines
security/selinux/ss/services.c +10
-0+/* +* Verify that each kernel class that is defined in the +* policy is correct +*/ +static int validate_classes (struct policydb *p) +{ +int i, j; +struct class_datum *cladatum; +struct perm_datum *perdatum; +u32 val, tmp, common_pts_len;
Use temporary variables to make this code readable, e.g.
const struct selinux_class_perm *kdefs = &selinux_class_perm;
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1041
Quoted 2 lines
security/selinux/ss/services.c +2
-0+ +for (i =1 ; i < defined_classes_perms.class_to_string_len; i++) {
Likewise:
for (i = 1; i < kdefs->nclasses; i++) {
const char *kname, *pname;
kname = kdefs->class_to_string[i];security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1045
Quoted 4 lines
security/selinux/ss/services.c +4
-0+if (i > p->p_classes.nprim) { +printk (KERN_INFO +"security: class %s not defined in policy\n" , + defined_classes_perms.class_to_string[i]);
kname);
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1047
Quoted 2 lines
security/selinux/ss/services.c +2
-0+continue ; + }
pname = p->p_class_val_to_name[i-1];
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1049
Quoted 2 lines
security/selinux/ss/services.c +2
-0+if (strcmp (p->p_class_val_to_name[i-1 ], + defined_classes_perms.class_to_string[i])) {
if (strcmp(pname, kname)) {security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1052
Quoted 3 lines
security/selinux/ss/services.c +3
-0+ printk(KERN_ERR + "security: class number %d is incorrect, found %s but should be %s\n", + i, p->p_class_val_to_name[i-1],
i, pname,
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1053
Quoted 1 line
security/selinux/ss/services.c +1
-0+ defined_classes_perms.class_to_string[i]);
kname);
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1062
Quoted 10 lines
security/selinux/ss/services.c +9
-0+return -EINVAL ; + } + cladatum =hashtab_search (p->p_classes.table, + defined_classes_perms.class_to_string[i]); +if (cladatum ==NULL ) { +printk (KERN_ERR "security: class %s not found in policy\n" , + defined_classes_perms.class_to_string[i]); +return -EINVAL ; + }
This seems redundant, as you checked class_val_to_name and it was generated from the classes symtab.
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1069
Quoted 7 lines
security/selinux/ss/services.c +7
-0+if (i != cladatum->value) { +printk (KERN_ERR +"security: class %s has incorrect value, found %d but should be %d\n" , + defined_classes_perms.class_to_string[i], + cladatum->value, i); +return -EINVAL ; + }
Ditto.
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1072
Quoted 3 lines
security/selinux/ss/services.c +3
-0+ } +for (i =0 ; i < defined_classes_perms.av_perm_to_string_len; i++) { +if (defined_classes_perms.av_perm_to_string[i].tclass > p->p_classes.nprim)
More opportunities for short temporary variables.
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1082
Quoted 10 lines
security/selinux/ss/services.c +10
-0+continue ; + cladatum =hashtab_search (p->p_classes.table, + p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1 ]); +if (cladatum ==NULL ) { +printk (KERN_ERR +"security: class %s for permission %s not found in policy\n" , + p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1 ], + defined_classes_perms.av_perm_to_string[i].name); +return -EINVAL ; + }
When would this occur? Seems like a BUG_ON case.
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1088
Quoted 6 lines
security/selinux/ss/services.c +6
-0+ val =1 << (cladatum->permissions.nprim -1 ); +if (defined_classes_perms.av_perm_to_string[i].value > val) { +printk (KERN_INFO +"security: permission %s in class %s not defined in policy\n" , + defined_classes_perms.av_perm_to_string[i].name, + p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1 ]);
I can't believe you typed that, and more than once too.
Quoted 11 lines
security/selinux/ss/services.c +11
-0+continue ; + } + perdatum =hashtab_search (cladatum->permissions.table, + defined_classes_perms.av_perm_to_string[i].name); +if (perdatum ==NULL ) { +printk (KERN_ERR +"security: permission %s in class %s not found in policy\n" , + defined_classes_perms.av_perm_to_string[i].name, + p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1 ]); +return -EINVAL ; + }
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.
Re: [PATCH 4/4] SELinux: validate kernel object classes and permissions
Quoted 82 lines · Stephen Smalley
Quoted 31 lines · Chad Sellers
plain text document attachment (verify_kernel.diff)
This is a new object class and permission validation scheme that validates against the defined kernel headers. This scheme allows extra classes and permissions that do not conflict with the kernel definitions to be added to the policy. This validation is now done for all policy loads, not just subsequent loads after the first policy load.The implementation walks the three structrures containing the defined object class and permission values and ensures their values are the same in the policy being loaded. This includes verifying the object classes themselves, the permissions they contain, and the permissions they inherit from commons. Classes or permissions that are present in the kernel but missing from the policy cause a warning (printed to
KERN_INFO) to be printed, but do not stop the policy from loading, emulating current behavior. Any other inconsistencies cause the load to fail.Signed-off-by Chad Sellers <[email protected]>--- security/selinux/ss/services.c | 159 ++++++++++++++++++++++++++++++++++++++++- 1 file changed, 158 insertions(+), 1 deletion(-)Index:
linux-2.6-discovery/security/selinux/ss/services.c
===================================================================security/selinux/ss/services.c +0
-0@@ -1018,6 +1027,136 @@ int security_change_sid(u32 ssid,return security_compute_sid (ssid, tsid, tclass,AVTAB_CHANGE , out_sid); }security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1039
Quoted 10 lines
security/selinux/ss/services.c +10
-0+/* +* Verify that each kernel class that is defined in the +* policy is correct +*/ +static int validate_classes (struct policydb *p) +{ +int i, j; +struct class_datum *cladatum; +struct perm_datum *perdatum; +u32 val, tmp, common_pts_len;Use temporary variables to make this code readable, e.g. const struct
selinux_class_perm*kdefs =&selinux_class_perm;security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1041
Quoted 2 lines
security/selinux/ss/services.c +2
-0+ +for (i =1 ; i < defined_classes_perms.class_to_string_len; i++) {Likewise:
for (i = 1; i <kdefs->nclasses; i++) {
const char *kname, *pname;
kname = kdefs->class_to_string[i];security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1045
Quoted 4 lines
security/selinux/ss/services.c +4
-0+if (i > p->p_classes.nprim) { +printk (KERN_INFO +"security: class %s not defined in policy\n" , + defined_classes_perms.class_to_string[i]);kname);
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1047
Quoted 2 lines
security/selinux/ss/services.c +2
-0+continue ; + }pname = p->p_class_val_to_name[i-1];
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1049
Quoted 2 lines
security/selinux/ss/services.c +2
-0+if (strcmp (p->p_class_val_to_name[i-1 ], + defined_classes_perms.class_to_string[i])) {if (
strcmp(pname, kname)) {security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1052
Quoted 4 lines
security/selinux/ss/services.c +3
-0+ printk(KERN_ERR + "security: class number %d is incorrect, found %s but should be %s\n", + i, p->p_class_val_to_name[i-1],i, pname,
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1054
Quoted 1 line
security/selinux/ss/services.c +1
-0+ defined_classes_perms.class_to_string[i]);kname);
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1062
Quoted 10 lines
security/selinux/ss/services.c +9
-0+return -EINVAL ; + } + cladatum =hashtab_search (p->p_classes.table, + defined_classes_perms.class_to_string[i]); +if (cladatum ==NULL ) { +printk (KERN_ERR "security: class %s not found in policy\n" , + defined_classes_perms.class_to_string[i]); +return -EINVAL ; + }
Quoted 3 lines
This seems redundant, as you checked
class_val_to_nameand it was generated from the classes symtab.
Yes, this would only be triggered by a broken policy. Would you rather I not worry about a broken policy at all, as I obviously don't provide complete coverage for a broken policy?
Quoted 31 lines
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1069
Quoted 8 lines
security/selinux/ss/services.c +7
-0+if (i != cladatum->value) { +printk (KERN_ERR + "security: class %s has incorrect value, found %d but should be %d\n", + defined_classes_perms.class_to_string[i], + cladatum->value, i); +return -EINVAL ; + }Ditto.
Quoted 4 lines
security/selinux/ss/services.c +3
-0+ } +for (i =0 ; i < defined_classes_perms.av_perm_to_string_len; i++) { + if (defined_classes_perms.av_perm_to_string[i].tclass >
p->p_classes.nprim)More opportunities for short temporary variables.
security/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1082
Quoted 12 lines
security/selinux/ss/services.c +3
-0+continue ; + cladatum = hashtab_search(p->p_classes.table, +p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1]);
security/selinux/ss/services.c +4
-0+if (cladatum ==NULL ) { + printk(KERN_ERR + "security: class %s for permission %s not found in policy\n", +p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1],
security/selinux/ss/services.c +3
-0+ defined_classes_perms.av_perm_to_string[i].name); +return -EINVAL ; + }
Quoted 2 lines
When would this occur? Seems like a
BUG_ONcase.
Yes, this would only occur if the headers were not consistent, so it should be a BUG_ON.
Quoted 10 lines
Quoted 7 lines
security/selinux/ss/services.c +6
-0+ val =1 << (cladatum->permissions.nprim -1 ); +if (defined_classes_perms.av_perm_to_string[i].value > val) { + printk(KERN_INFO + "security: permission %s in class %s not defined in policy\n", + defined_classes_perms.av_perm_to_string[i].name, +p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1]);
I can't believe you typed that, and more than once too.
:-)
Quoted 5 lines
Quoted 5 lines
security/selinux/ss/services.c +4
-0+continue ; + } + perdatum = hashtab_search(cladatum->permissions.table, +defined_classes_perms.av_perm_to_string[i].name);
Quoted 8 lines
security/selinux/ss/services.c
Quoted 8 lines
security/selinux/ss/services.c +5
-0+if (perdatum ==NULL ) { + printk(KERN_ERR + "security: permission %s in class %s not found in policy\n", + defined_classes_perms.av_perm_to_string[i].name, +p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1]);
security/selinux/ss/services.c +2
-0+return -EINVAL ; + }
I'll make some temporary variables, as you wish. I tend to avoid adding additional unnecessary layers of indirection, which is why this code is the way it is (I've followed too many pointers through libsemanage), but it will help a lot more than it will hurt in this case.
--
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.
Re: [PATCH 4/4] SELinux: validate kernel object classes and permissions
Quoted 7 lines · Chad Sellers
Quoted 3 lines · Stephen Smalley
This seems redundant, as you checked
class_val_to_nameand it was generated from the classes symtab.Yes, this would only be triggered by a broken policy. Would you rather I not worry about a broken policy at all, as I obviously don't provide complete coverage for a broken policy?
If there was some way for class_val_to_name and the classes symtab to get out of sync (e.g. they were independently provided from the policy image), then it would make sense in general to provide a consistency check. But since the former is generated from the latter, the only way for them to get out of sync is a bug in the kernel code. Which you can't really test for in general within the kernel itself...
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.
Re: [PATCH 4/4] SELinux: validate kernel object classes and permissions
In reply to Chad Sellerssecurity/selinux/ss/services.c int security_change_sid(u32 ssid, · line 1075
Quoted 5 lines · Chad Sellers
security/selinux/ss/services.c +5
-0+for (i =0 ; i < defined_classes_perms.av_perm_to_string_len; i++) { +if (defined_classes_perms.av_perm_to_string[i].tclass > p->p_classes.nprim) +continue ; + cladatum =hashtab_search (p->p_classes.table, + p->p_class_val_to_name[defined_classes_perms.av_perm_to_string[i].tclass-1 ]);
These lines are too long. Limit is 80 characters for the function body.
See Documentation/CodingStyle
- James
Signature · 7 lines
James Morris <[email protected]> -- 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.