SELinux: allow userspace to read policy back out of the kernel
PATCH 1/1 SELinux: allow userspace to read policy back out of the kernel
There is interest in being able to see what the actual policy is that was loaded into the kernel. The patch creates a new selinuxfs file /selinux/policy which can be read by userspace. The actual policy that is loaded into the kernel will be written back out to userspace.
---
security/selinux/include/classmap.h | 2 security/selinux/include/security.h | 2 security/selinux/selinuxfs.c | 95 ++++ security/selinux/ss/avtab.c | 42 ++ security/selinux/ss/avtab.h | 2 security/selinux/ss/conditional.c | 123 +++++ security/selinux/ss/conditional.h | 2 security/selinux/ss/ebitmap.c | 81 +++ security/selinux/ss/ebitmap.h | 1 security/selinux/ss/policydb.c | 841 +++++++++++++++++++++++++++++++++++ security/selinux/ss/policydb.h | 20 + security/selinux/ss/services.c | 44 ++ 12 files changed, 1252 insertions(+), 3 deletions(-)
security/selinux/include/classmap.h +1 -1
@@ -17,7 +17,7 @@ struct security_class_mapping secclass_map[] = { {"compute_av" ,"compute_create" ,"compute_member" ,"check_context" ,"load_policy" ,"compute_relabel" ,"compute_user" ,"setenforce" ,"setbool" ,"setsecparam" , - "setcheckreqprot", NULL } }, + "setcheckreqprot", "read_policy", NULL } }, { "process", { "fork", "transition", "sigchld", "sigkill", "sigstop", "signull", "signal", "ptrace", "getsched", "setsched",
security/selinux/include/security.h +2 -0
@@ -82,6 +82,8 @@ extern int selinux_policycap_openperm;int security_mls_enabled (void );int security_load_policy (void *data,size_t len); +int security_read_policy (void **data,ssize_t *len); +size_t security_policydb_len (void );int security_policycap_supported (unsigned int req_cap);
security/selinux/selinuxfs.c +95 -0
119 lines not on this page · raw message
security/selinux/ss/avtab.c +42 -0
48 lines not on this page · raw message
security/selinux/ss/avtab.h +2 -0
@@ -71,6 +71,8 @@ int avtab_read_item(struct avtab *a, void *fp, struct policydb *pol, void *p);int avtab_read (struct avtab *a,void *fp,struct policydb *pol); +int avtab_write_item (struct policydb *p,struct avtab_node *cur,void *fp); +int avtab_write (struct policydb *p,struct avtab *a,void *fp);struct avtab_node *avtab_insert_nonunique (struct avtab *h,struct avtab_key *key,struct avtab_datum *datum);
security/selinux/ss/conditional.c +123 -0
129 lines not on this page · raw message
security/selinux/ss/conditional.h +2 -0
@@ -69,6 +69,8 @@ int cond_index_bool(void *key, void *datum, void *datap);int cond_read_bool (struct policydb *p,struct hashtab *h,void *fp);int cond_read_list (struct policydb *p,void *fp); +int cond_write_bool (void *key,void *datum,void *ptr); +int cond_write_list (struct policydb *p,struct cond_node *list,void *fp);void cond_compute_av (struct avtab *ctab,struct avtab_key *key,struct av_decision *avd);
security/selinux/ss/ebitmap.c +79 -2
98 lines not on this page · raw message
security/selinux/ss/ebitmap.h +1 -0
@@ -123,6 +123,7 @@ int ebitmap_get_bit(struct ebitmap *e, unsigned long bit);int ebitmap_set_bit (struct ebitmap *e,unsigned long bit,int value);void ebitmap_destroy (struct ebitmap *e);int ebitmap_read (struct ebitmap *e,void *fp); +int ebitmap_write (struct ebitmap *e,void *fp);#ifdef CONFIG_NETLABEL int ebitmap_netlbl_export(struct ebitmap *ebmap,
security/selinux/ss/policydb.c +841 -0
850 lines not on this page · raw message
security/selinux/ss/policydb.h +20 -0
@@ -254,6 +254,9 @@ struct policydb {struct ebitmap permissive_map; +/* length of this policy when it was loaded */ +size_t len; +unsigned int policyvers; unsigned int reject_unknown : 1;
@@ -270,6 +273,7 @@ extern int policydb_class_isvalid(struct policydb *p, unsigned int class);extern int policydb_type_isvalid (struct policydb *p,unsigned int type);extern int policydb_role_isvalid (struct policydb *p,unsigned int role);extern int policydb_read (struct policydb *p,void *fp); +extern int policydb_write (struct policydb *p,void *fp);#define PERM_SYMTAB_SIZE 32
@@ -290,6 +294,11 @@ struct policy_file {size_t len; }; +struct policy_data { +struct policydb *p; +void *fp; +}; +static inline int next_entry (void *buf,struct policy_file *fp,size_t bytes) {if (bytes > fp->len)
@@ -301,6 +310,17 @@ static inline int next_entry(void *buf, struct policy_file *fp, size_t bytes)return 0 ; } +static inline int put_entry (void *buf,size_t bytes,int num,struct policy_file *fp) +{ +size_t len = bytes * num; + +memcpy (fp->data, buf, len); + fp->data += len; + fp->len -= len; + +return 0 ; +} +extern u16 string_to_security_class (struct policydb *p,const char *name);extern u32 string_to_av_perm (struct policydb *p,u16 tclass,const char *name);
security/selinux/ss/services.c +44 -0
65 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 28 lines · Eric Paris
There is interest in being able to see what the actual policy is that was loaded into the kernel. The patch creates a new selinuxfs file /selinux/policy which can be read by userspace. The actual policy that is loaded into the kernel will be written back out to userspace.
Signed-off-by Eric Paris <[email protected]>security/selinux/selinuxfs.c +13
-0@@ -296,6 +299,97 @@ static const struct file_operations sel_mls_ops = { .llseek = generic_file_llseek, }; +struct policy_load_memory { +size_t len; +void *data; +}; + +static int sel_open_policy (struct inode *inode,struct file *filp) +{ +struct policy_load_memory *plm =NULL ; +int rc; + +BUG_ON (filp->private_data); + +mutex_lock (&sel_mutex);
security/selinux/selinuxfs.c static const struct file_operations sel_mls_ops = { · line 324
Quoted 10 lines
security/selinux/selinuxfs.c +10
-0+ + rc =task_has_security (current,SECURITY__READ_POLICY ); +if (rc) + goto err; + + rc = -EBUSY ; +if (policy_opened) + goto err; + + policy_opened =1 ;
Either don't set this flag until you've successfully completed security_read_policy(), or clear it on the err path. Otherwise a failed attempt to open will leave it in an un-openable state. Since you are taking sel_mutex around the entire operation, you can defer setting the flag until you're done without risking another one starting.
Quoted 46 lines
security/selinux/selinuxfs.c +29
-0+ + rc = -ENOMEM ; + plm =kzalloc (sizeof (*plm),GFP_KERNEL ); +if (!plm) + goto err; + +if (i_size_read (inode) !=security_policydb_len ()) { +mutex_lock (&inode->i_mutex); +i_size_write (inode,security_policydb_len ()); +mutex_unlock (&inode->i_mutex); + } + + rc =security_read_policy (&plm->data, &plm->len); +if (rc) + goto err; + + filp->private_data = plm; + +mutex_unlock (&sel_mutex); + +return 0 ; +err: +mutex_unlock (&sel_mutex); + +if (plm) +vfree (plm->data); +kfree (plm); +return rc; +}security/selinux/ss/services.c +8
-0@@ -3129,3 +3136,40 @@ netlbl_sid_to_secattr_failure: return rc; }#endif /* CONFIG_NETLABEL */ + +/** +* security_read_policy - read the policy. +* @data: binary policy data +* @len: length of data in bytes +* +*/ +int security_read_policy (void **data,ssize_t *len)
security/selinux/ss/services.c netlbl_sid_to_secattr_failure: · line 3156
Quoted 10 lines
security/selinux/ss/services.c +10
-0+{ +int rc; +struct policy_file fp; + +if (!ss_initialized) +return -EINVAL ; + +read_lock (&policy_rwlock); + *len =security_policydb_len (); +read_unlock (&policy_rwlock);
I don't think there is any benefit to taking the read-lock since you are just reading a single word. And if the read-lock was needed, then it ought to be taken inside of security_policydb_len() rather than in the caller so that it is properly taken by all callers.
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 37 lines · Stephen Smalley
Quoted 36 lines · Eric Paris
There is interest in being able to see what the actual policy is that was loaded into the kernel. The patch creates a new selinuxfs file /selinux/policy which can be read by userspace. The actual policy that is loaded into the kernel will be written back out to userspace.
Signed-off-by Eric Paris <[email protected]>security/selinux/selinuxfs.c +21
-0@@ -296,6 +299,97 @@ static const struct file_operations sel_mls_ops = { .llseek = generic_file_llseek, }; +struct policy_load_memory { +size_t len; +void *data; +}; + +static int sel_open_policy (struct inode *inode,struct file *filp) +{ +struct policy_load_memory *plm =NULL ; +int rc; + +BUG_ON (filp->private_data); + +mutex_lock (&sel_mutex); + + rc =task_has_security (current,SECURITY__READ_POLICY ); +if (rc) + goto err; + + rc = -EBUSY ; +if (policy_opened) + goto err;
Quoted 8 lines
security/selinux/selinuxfs.c static const struct file_operations sel_mls_ops = { · line 324
Quoted 2 lines
security/selinux/selinuxfs.c +2
-0+ + policy_opened =1 ;Either don't set this flag until you've successfully completed
security_read_policy(), or clear it on the err path. Otherwise a failed attempt to open will leave it in an un-openable state. Since you are takingsel_mutexaround the entire operation, you can defer setting the flag until you're done without risking another one starting.
deferred.
Quoted 8 lines
Quoted 8 lines
security/selinux/selinuxfs.c +8
-0+int security_read_policy (void **data,ssize_t *len) +{ +int rc; +struct policy_file fp; + +if (!ss_initialized) +return -EINVAL ; +
Quoted 8 lines
security/selinux/selinuxfs.c static const struct file_operations sel_mls_ops = { · line 335
Quoted 3 lines
security/selinux/selinuxfs.c +3
-0+read_lock (&policy_rwlock); + *len =security_policydb_len (); +read_unlock (&policy_rwlock);I don't think there is any benefit to taking the read-lock since you are just reading a single word. And if the read-lock was needed, then it ought to be taken inside of
security_policydb_len()rather than in the caller so that it is properly taken by all callers.
I decided to push it down to security_policydb_len() since I want to make sure we keep locking around the static policydb variable as best we can. (In case I ever revive my/KaiGai? 's series which makes it a pointer)
-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 6 lines · Eric Paris
There is interest in being able to see what the actual policy is that was loaded into the kernel. The patch creates a new selinuxfs file /selinux/policy which can be read by userspace. The actual policy that is loaded into the kernel will be written back out to userspace.
Signed-off-by Eric Paris <[email protected]>
Oh, and we still need to change the rangetr_cmp definition to deterministically order the range transition list so that we can get a consistent result.
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.