selinux: add object class discovery
cover selinux: add object class discovery
Add support to SELinux for userland to discover object classes and their permissions. This patch set adds a class directory to the root of selinuxfs and a tree of object classes and permissions, with the value for each of the respective component.
Signature · 8 lines
Chris PeBenito Tresys Technology, LLC (410) 290-1411 x150 -- 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: add support for querying object classes and permissions from the running policy
From: Christopher J. PeBenito <[email protected]>
Add support to the SELinux security server for obtaining a list of classes, and for obtaining a list of permissions for a specified class.
--- security/selinux/include/security.h | 3 + security/selinux/ss/services.c | 93 +++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+), 0 deletions(-)
security/selinux/include/security.h +3 -0
@@ -87,6 +87,9 @@ int security_validate_transition(u32 oldsid, u32 newsid, u32 tasksid,int security_sid_mls_copy (u32 sid,u32 mls_sid,u32 *new_sid); +int security_get_classes (char ***classes,int *nclasses); +int security_get_permissions (char *class,char ***perms,int *nperms); +#define SECURITY_FS_USE_XATTR 1/* use xattr */ #define SECURITY_FS_USE_TRANS 2/* use transition SIDs, e.g. devpts/tmpfs */ #define SECURITY_FS_USE_TASK 3/* use task SIDs, e.g. pipefs/sockfs */
security/selinux/ss/services.c +93 -0
99 lines not on this page · raw message
Signature · 7 lines
1.5.1.4 -- 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: rename sel_remove_bools() for more general usage.
From: Christopher J. PeBenito <[email protected]>
sel_remove_bools() will also be used by the object class discovery, rename it for more general use.
--- security/selinux/selinuxfs.c | 9 ++++----- 1 files changed, 4 insertions(+), 5 deletions(-)
security/selinux/selinuxfs.c +4 -5
@@ -940,9 +940,8 @@ static const struct file_operations sel_commit_bools_ops = { .write = sel_commit_bools_write, }; -/* delete booleans - partial revoke() from -* fs/proc/generic.c proc_kill_inodes */ -static void sel_remove_bools (struct dentry *de) +/* partial revoke() from fs/proc/generic.c proc_kill_inodes */ +static void sel_remove_entries (struct dentry *de) {struct list_head *p, *node;struct super_block *sb = de->d_sb;
@@ -998,7 +997,7 @@ static int sel_make_bools(void)kfree (bool_pending_values); bool_pending_values =NULL ; -sel_remove_bools (dir); +sel_remove_entries (dir);if (!(page = (char *)get_zeroed_page (GFP_KERNEL )))return -ENOMEM ;
@@ -1048,7 +1047,7 @@ out:return ret; err:kfree (values); -sel_remove_bools (dir); +sel_remove_entries (dir); ret = -ENOMEM ; goto out; }
Signature · 7 lines
1.5.1.4 -- 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: change sel_make_dir() to specify inode counter.
From: Christopher J. PeBenito <[email protected]>
Specify the inode counter explicitly in sel_make_dir(), rather than always using sel_last_ino.
--- security/selinux/selinuxfs.c | 11 ++++++----- 1 files changed, 6 insertions(+), 5 deletions(-)
security/selinux/selinuxfs.c +6 -5
@@ -1293,7 +1293,8 @@ out:return ret; } -static int sel_make_dir (struct inode *dir,struct dentry *dentry) +static int sel_make_dir (struct inode *dir,struct dentry *dentry, +unsigned long *ino) {int ret =0 ;struct inode *inode;
@@ -1305,7 +1306,7 @@ static int sel_make_dir(struct inode *dir, struct dentry *dentry) } inode->i_op = &simple_dir_inode_operations; inode->i_fop = &simple_dir_operations; - inode->i_ino = ++sel_last_ino; + inode->i_ino = ++(*ino);/* directory inodes start off with i_nlink == 2 (for "." entry) */ inc_nlink (inode);d_add (dentry, inode);
@@ -1351,7 +1352,7 @@ static int sel_fill_super(struct super_block * sb, void * data, int silent) goto err; } - ret =sel_make_dir (root_inode, dentry); + ret =sel_make_dir (root_inode, dentry, &sel_last_ino);if (ret) goto err;
@@ -1384,7 +1385,7 @@ static int sel_fill_super(struct super_block * sb, void * data, int silent) goto err; } - ret =sel_make_dir (root_inode, dentry); + ret =sel_make_dir (root_inode, dentry, &sel_last_ino);if (ret) goto err;
@@ -1398,7 +1399,7 @@ static int sel_fill_super(struct super_block * sb, void * data, int silent) goto err; } - ret =sel_make_dir (root_inode, dentry); + ret =sel_make_dir (root_inode, dentry, &sel_last_ino);if (ret) goto err;
Signature · 7 lines
1.5.1.4 -- 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: add selinuxfs structure for object class discovery
From: Christopher J. PeBenito <[email protected]>
The structure is as follows (relative to selinuxfs root):
/class/file/index
/class/file/perms/read
/class/file/perms/write
...
Each class is allocated 33 inodes, 1 for the class index and 32 for permissions. Relative to SEL_CLASS_INO_OFFSET, the inode of the index file DIV 33 is the class number. The inode of the permission file % 33 is the index of the permission for that class.
--- security/selinux/selinuxfs.c | 218 ++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 218 insertions(+), 0 deletions(-)
security/selinux/selinuxfs.c +218 -0
@@ -67,6 +67,10 @@ static struct dentry *bool_dir = NULL;static int bool_num =0 ;static int *bool_pending_values =NULL ; +/* global data for classes */ +static struct dentry *class_dir =NULL ; +static unsigned long last_class_ino; +extern void selnl_notify_setenforce (int val);/* Check whether a task is allowed to use a security operation. */
@@ -106,6 +110,7 @@ static unsigned long sel_last_ino = SEL_INO_NEXT - 1;#define SEL_INITCON_INO_OFFSET 0x01000000#define SEL_BOOL_INO_OFFSET 0x02000000 +#define SEL_CLASS_INO_OFFSET 0x04000000#define SEL_INO_MASK 0x00ffffff#define TMPBUFLEN 12
@@ -237,6 +242,11 @@ static const struct file_operations sel_policyvers_ops = {/* declaration for sel_write_load */ static int sel_make_bools (void ); +static int sel_make_classes (void ); + +/* declaration for sel_make_class_dirs */ +static int sel_make_dir (struct inode *dir,struct dentry *dentry, +unsigned long *ino);static ssize_t sel_read_mls (struct file *filp,char __user *buf,size_t count,loff_t *ppos)
@@ -287,10 +297,18 @@ static ssize_t sel_write_load(struct file * file, const char __user * buf, goto out; ret =sel_make_bools (); +if (ret) { + length = ret; + goto out1; + } + + ret =sel_make_classes ();if (ret) length = ret;else length = count; + +out1:audit_log (current->audit_context,GFP_KERNEL ,AUDIT_MAC_POLICY_LOAD ,"policy loaded auid=%u" ,audit_get_loginuid (current->audit_context));
@@ -1293,6 +1311,194 @@ out:return ret; } +#define NVECTORS 32 +#define DIV (a, b) ((a) / (b) - ((a) % (b) < 0)) +static ssize_t sel_read_class (struct file * file,char __user *buf, +size_t count,loff_t *ppos) +{ +ssize_t rc, len; +char *page; +unsigned long ino = file->f_path.dentry->d_inode->i_ino; + + page = (char *)__get_free_page (GFP_KERNEL ); +if (!page) { + rc = -ENOMEM ; + goto out; + } + + len =snprintf (page,PAGE_SIZE ,"%d" ,(int )DIV (ino&SEL_INO_MASK ,NVECTORS +1 )); + rc =simple_read_from_buffer (buf,count,ppos,page,len); +free_page ((unsigned long )page); +out: +return rc; +} + +static const struct file_operations sel_class_ops = { + .read = sel_read_class, +}; + +static ssize_t sel_read_perm (struct file * file,char __user *buf, +size_t count,loff_t *ppos) +{ +ssize_t rc, len; +char *page; +unsigned long ino = file->f_path.dentry->d_inode->i_ino; + + page = (char *)__get_free_page (GFP_KERNEL ); +if (!page) { + rc = -ENOMEM ; + goto out; + } + + len =snprintf (page,PAGE_SIZE ,"%d" ,(int )(ino&SEL_INO_MASK ) % (NVECTORS +1 )); + rc =simple_read_from_buffer (buf,count,ppos,page,len); +free_page ((unsigned long )page); +out: +return rc; +} + +static const struct file_operations sel_perm_ops = { + .read = sel_read_perm, +}; + +static int sel_make_perm_files (char *objclass,int classindex,struct dentry *dir) +{ +int i, rc =0 , nperms; +char **perms; + + rc =security_get_permissions (objclass,&perms,&nperms); +if (rc) + goto out; + +for (i =0 ; i < nperms; i++) { +struct inode *inode; +struct dentry *dentry; + + dentry =d_alloc_name (dir, perms[i]); +if (!dentry) { + rc = -ENOMEM ; + goto out1; + } + + inode =sel_make_inode (dir->d_sb,S_IFREG |S_IRUGO ); +if (!inode) { + rc = -ENOMEM ; + goto out1; + } + inode->i_fop = &sel_perm_ops; + inode->i_ino = (classindex * (NVECTORS +1 ) + i +1 ) |SEL_CLASS_INO_OFFSET ; +d_add (dentry, inode); + } + +out1: +for (i =0 ; i < nperms; i++) +kfree (perms[i]); +kfree (perms); +out: +return rc; +} + +static int sel_make_class_dir_entries (char *classname,int index,struct dentry *dir) +{ +struct dentry *dentry =NULL ; +struct inode *inode =NULL ; +int rc; + + dentry =d_alloc_name (dir,"index" ); +if (!dentry) { + rc = -ENOMEM ; + goto out; + } + + inode =sel_make_inode (dir->d_sb,S_IFREG |S_IRUGO ); +if (!inode) { + rc = -ENOMEM ; + goto out; + } + + inode->i_fop = &sel_class_ops; + inode->i_ino = (index * (NVECTORS +1 )) |SEL_CLASS_INO_OFFSET ; +d_add (dentry, inode); + + dentry =d_alloc_name (dir,"perms" ); +if (!dentry) { + rc = -ENOMEM ; + goto out; + } + + rc =sel_make_dir (dir->d_inode,dentry, &last_class_ino); +if (rc) + goto out; + + rc =sel_make_perm_files (classname,index,dentry); + +out: +return rc; +} + +static void sel_remove_classes (void ) +{ +struct list_head *class_node; + +list_for_each (class_node, &class_dir->d_subdirs) { + struct dentry *class_subdir = list_entry(class_node, struct dentry, d_u.d_child); +struct list_head *class_subdir_node; + +list_for_each (class_subdir_node, &class_subdir->d_subdirs) { + struct dentry *d = list_entry(class_subdir_node, struct dentry, d_u.d_child); + +if (d->d_inode) +if (d->d_inode->i_mode &S_IFDIR ) +sel_remove_entries (d); + } + +sel_remove_entries (class_subdir); + } + +sel_remove_entries (class_dir); +} + +static int sel_make_classes (void ) +{ +int rc =0 , nclasses, i; +char **classes; + +/* delete any existing entries */ +sel_remove_classes (); + + rc =security_get_classes (&classes,&nclasses); +if (rc <0 ) + goto out; + + last_class_ino = ((nclasses+1 ) * (NVECTORS +1 ) +1 ) |SEL_CLASS_INO_OFFSET ; + +for (i =0 ; i < nclasses; i++) { +struct dentry *class_name_dir; + + class_name_dir =d_alloc_name (class_dir, classes[i]); +if (!class_name_dir) { + rc = -ENOMEM ; + goto out1; + } + + rc =sel_make_dir (class_dir->d_inode, class_name_dir, &last_class_ino); +if (rc) + goto out1; + +/* i+1 since class values are 1-indexed */ + rc =sel_make_class_dir_entries (classes[i],i+1 ,class_name_dir); +if (rc) + goto out1; + } + +out1: +for (i =0 ; i < nclasses; i++) +kfree (classes[i]); +kfree (classes); +out: +return rc; +} +static int sel_make_dir (struct inode *dir,struct dentry *dentry,unsigned long *ino) {
@@ -1407,6 +1613,18 @@ static int sel_fill_super(struct super_block * sb, void * data, int silent)if (ret) goto err; + dentry =d_alloc_name (sb->s_root,"class" ); +if (!dentry) { + ret = -ENOMEM ; + goto err; + } + + ret =sel_make_dir (root_inode, dentry, &sel_last_ino); +if (ret) + goto err; + + class_dir = dentry; + out:return ret; err:
Signature · 7 lines
1.5.1.4 -- 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: add selinuxfs structure for object class discovery
Quoted 9 lines · Christopher J. PeBenito
From: Christopher J. PeBenito <[email protected]>
The structure is as follows (relative to selinuxfs root):
/class/file/index
/class/file/perms/read
/class/file/perms/write
...
Quoted 4 lines
Each class is allocated 33 inodes, 1 for the class index and 32 for permissions. Relative to
SEL_CLASS_INO_OFFSET, the inode of the index file DIV 33 is the class number. The inode of the permission file % 33 is the index of the permission for that class.
Keep in mind that any of these may need to be 64-bit at some point, so be careful about hard-coding any 32-bit assumptions into the kernel/user API.
Quoted 1 line
+#define NVECTORS 32
Macro name is too generic.
Quoted 1 line
+#define
DIV(a, b)((a) / (b) - ((a) % (b) < 0))
Also too generic, and should be a static inline.
I think you should also make static inline wrappers:
sel_ino_to_class()sel_ino_to_perm()
to make it all clearer to others.
security/selinux/selinuxfs.c out: · line 1420
Quoted 3 lines
security/selinux/selinuxfs.c +2
-0+ inode->i_ino = (classindex * (NVECTORS +1 ) + i +1 ) |SEL_CLASS_INO_OFFSET ; + inode->i_ino = (index * (NVECTORS +1 )) |SEL_CLASS_INO_OFFSET ;
Add wrappers for these, too.
security/selinux/selinuxfs.c out: · line 1445
Quoted 7 lines
security/selinux/selinuxfs.c +7
-0+static void sel_remove_classes (void ) +{ +struct list_head *class_node; + +list_for_each (class_node, &class_dir->d_subdirs) { + struct dentry *class_subdir = list_entry(class_node, struct dentry, d_u.d_child); +struct list_head *class_subdir_node;
Coding style: max. 80 columns for body of function (similar issue elsewhere in the patches).
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.
Re: [PATCH 4/4] selinux: add selinuxfs structure for object class discovery
Quoted 11 lines · James Morris
Quoted 9 lines · Christopher J. PeBenito
From: Christopher J. PeBenito <[email protected]>
The structure is as follows (relative to selinuxfs root):
/class/file/index
/class/file/perms/read
/class/file/perms/write
...
Quoted 7 lines
Quoted 4 lines
Each class is allocated 33 inodes, 1 for the class index and 32 for permissions. Relative to
SEL_CLASS_INO_OFFSET, the inode of the index file DIV 33 is the class number. The inode of the permission file % 33 is the index of the permission for that class.Keep in mind that any of these may need to be 64-bit at some point, so be careful about hard-coding any 32-bit assumptions into the kernel/user API.
Well its 32 because the access vector is a u32, so an object class can only have 32 perms max, which is why I had the following macro. Perhaps the macro should be moved to security.h, next to the struct av_decision definition?
Quoted 3 lines
Quoted 1 line
+#define NVECTORS 32
Macro name is too generic.
Maybe something like SEL_AV_MAX or SEL_PERM_MAX?
Quoted 3 lines
Quoted 1 line
+#define
DIV(a, b)((a) / (b) - ((a) % (b) < 0))Also too generic, and should be a static inline.
Ok. I noticed it other places in the kernel, which is why I used it. What should it be called? sel_div()?
Signature · 9 lines
Chris PeBenito Tresys Technology, LLC (410) 290-1411 x150 -- 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: add selinuxfs structure for object class discovery
Quoted 4 lines · Christopher J. PeBenito
Well its 32 because the access vector is a u32, so an object class can only have 32 perms max, which is why I had the following macro. Perhaps the macro should be moved to
security.h, next to the structav_decisiondefinition?
Yes, and keep in mind that we may move to u64 sometime.
Quoted 5 lines
Quoted 3 lines
Quoted 1 line
+#define NVECTORS 32
Macro name is too generic.
Maybe something like
SEL_AV_MAXorSEL_PERM_MAX?
SEL_VEC_MAX ?
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
+#define
DIV(a, b)((a) / (b) - ((a) % (b) < 0))Also too generic, and should be a static inline.
Ok. I noticed it other places in the kernel, which is why I used it. What should it be called?
sel_div()?
Yep, should be fine.
KJ folk may be interested in finding all of these and using a common inline instead.
- 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.
Re: [PATCH 1/4] selinux: add support for querying object classes and permissions from the running policy
In reply to Christopher J. PeBenitoQuoted 13 lines · Christopher J. PeBenito
int
security_sid_mls_copy(u32 sid, u32 mls_sid, u32 *new_sid);security/selinux/include/security.h +3
-0+int security_get_classes (char ***classes,int *nclasses); +int security_get_permissions (char *class,char ***perms,int *nperms); +#define SECURITY_FS_USE_XATTR 1/* use xattr */ #define SECURITY_FS_USE_TRANS 2/* use transition SIDs, e.g. devpts/tmpfs */ #define SECURITY_FS_USE_TASK 3/* use task SIDs, e.g. pipefs/sockfs */ security/selinux/ss/services.c +0
-0
security/selinux/ss/services.c out: · line 2005
Quoted 10 lines
security/selinux/ss/services.c +7
-0@@ -1996,6 +1996,99 @@ out:return rc; } +static int get_classes_callback (void *k,void *d,void *args) +{ +struct class_datum *datum = d; +char *name = k, **classes = args; +int value = datum->value -1 ; + + classes[value] =kstrdup (name,GFP_KERNEL );
Coding style: spaces after commas (needs to be fixed throughout).
security/selinux/ss/services.c out: · line 2006
Quoted 1 line
security/selinux/ss/services.c +1
-0+
Probably don't need this blank line.
security/selinux/ss/services.c out: · line 2008
Quoted 2 lines
security/selinux/ss/services.c +2
-0+if (classes[value] ==NULL ) +return -ENOMEM ;
I'd suggest
if (!foo)
to remain consistent.
security/selinux/ss/services.c out: · line 2020
Quoted 12 lines
security/selinux/ss/services.c +12
-0+ +return 0 ; +} + +int security_get_classes (char ***classes,int *nclasses) +{ +int rc = -ENOMEM ; + +POLICY_RDLOCK ; + + *nclasses = policydb.p_classes.nprim; + *classes =kzalloc (*nclasses*sizeof (*classes),GFP_KERNEL );
Use kcalloc().
Needs to be GFP_ATOMIC because you're holding a spinlock.
Quoted 13 lines
security/selinux/ss/services.c +13
-0+if (!*classes) + goto out; + + rc =hashtab_map (policydb.p_classes.table,get_classes_callback,*classes); +if (rc <0 ) { +int i; +for (i =0 ; i < *nclasses; i++) +kfree ((*classes)[i]); +kfree (*classes); + } + +out: +POLICY_RDUNLOCK ;
security/selinux/ss/services.c out: · line 2043
Quoted 10 lines
security/selinux/ss/services.c +10
-0+return rc; +} + +static int get_permissions_callback (void *k,void *d,void *args) +{ +struct perm_datum *datum = d; +char *name = k, **perms = args; +int value = datum->value -1 ; + + perms[value] =kstrdup (name,GFP_KERNEL );
Ditto.
(Have you tested this with all of the lock debugging enabled ?)
Quoted 13 lines
security/selinux/ss/services.c +13
-0+ +if (perms[value] ==NULL ) +return -ENOMEM ; + +return 0 ; +} + +int security_get_permissions (char *class,char ***perms,int *nperms) +{ +int rc = -ENOMEM , i; +struct class_datum *match; + +POLICY_RDLOCK ;
security/selinux/ss/services.c out: · line 2066
Quoted 10 lines
security/selinux/ss/services.c +10
-0+ + match =hashtab_search (policydb.p_classes.table,class); +if (match ==NULL ) { +printk (KERN_ERR "%s: unrecognized class %s\n" ,__FUNCTION__,class); + rc = -EINVAL ; + goto out; + } + + *nperms = match->permissions.nprim; + *perms =kzalloc (*nperms*sizeof (*perms),GFP_KERNEL );
Ditto.
Quoted 29 lines
security/selinux/ss/services.c +25
-0+if (!*perms) + goto out; + +if (match->comdatum) { + rc =hashtab_map (match->comdatum->permissions.table,get_permissions_callback,*perms); +if (rc <0 ) + goto err; + } + + rc =hashtab_map (match->permissions.table,get_permissions_callback,*perms); +if (rc <0 ) + goto err; + +out: +POLICY_RDUNLOCK ; +return rc; + +err: +POLICY_RDUNLOCK ; +for (i =0 ; i < *nperms; i++) +kfree ((*perms)[i]); +kfree (*perms); +return rc; +} +struct selinux_audit_rule {u32 au_seqno;struct context au_ctxt;
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.
Re: [PATCH 1/4] selinux: add support for querying object classes and permissions from the running policy
Quoted 13 lines · James Morris
security/selinux/ss/services.c out: · line 2020
Quoted 8 lines · Christopher J. PeBenito
security/selinux/ss/services.c +8
-0+int security_get_classes (char ***classes,int *nclasses) +{ +int rc = -ENOMEM ; + +POLICY_RDLOCK ; + + *nclasses = policydb.p_classes.nprim; + *classes =kzalloc (*nclasses*sizeof (*classes),GFP_KERNEL );Use
kcalloc().Needs to be
GFP_ATOMICbecause you're holding a spinlock.
[...]
Quoted 1 line
(Have you tested this with all of the lock debugging enabled ?)
Nope, my mistake. I'm a kernel coding newbie :)
Signature · 9 lines
Chris PeBenito Tresys Technology, LLC (410) 290-1411 x150 -- 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.