selinux: add brief info to policydb
Subsystems (2)
PATCH v4 1/2 selinux: add brief info to policydb
Add policybrief field to struct policydb. It holds a brief info of the policydb, made of colon separated name and value pairs that give information about how the policy is applied in the security module(s).
Note that the ordering of the fields in the string may change.
Policy brief is computed every time the policy is loaded, and when enforce or checkreqprot are changed.
Add security_policy_brief hook to give access to policy brief to the rest of the kernel. Lustre client makes use of this information to detect changes to the policy, and forward it to Lustre servers. Depending on how the policy is enforced on Lustre client side, Lustre servers can refuse connection.
--- include/linux/lsm_hooks.h | 17 +++++++ include/linux/security.h | 7 +++ security/security.c | 8 ++++ security/selinux/hooks.c | 7 +++ security/selinux/include/security.h | 2 + security/selinux/selinuxfs.c | 2 + security/selinux/ss/policydb.c | 88 +++++++++++++++++++++++++++++++++++++ security/selinux/ss/policydb.h | 3 ++ security/selinux/ss/services.c | 68 ++++++++++++++++++++++++++++ 9 files changed, 202 insertions(+)
include/linux/lsm_hooks.h +17 -0
@@ -1331,6 +1331,21 @@* @inode we wish to get the security context of. * @ctx is a pointer in which to place the allocated security context. * @ctxlen points to the place to put the length of @ctx. +* +* Security hooks for policy brief +* +* @policy_brief: +* +* Returns a string containing a brief info of the policydb. The string +* contains colon separated name and value pairs that give information +* about how the policy is applied in the security module(s). +* Note that the ordering of the fields in the string may change. +* +* @brief: pointer to buffer holding brief +* @len: in: brief buffer length if no alloc, out: brief string len +* @alloc: whether to allocate buffer for brief or not +* On success 0 is returned , or negative value on error. +* * This is the main security structure. */
@@ -1562,6 +1577,7 @@int (*inode_setsecctx)(struct dentry *dentry,void *ctx,u32 ctxlen);int (*inode_getsecctx)(struct inode *inode,void **ctx,u32 *ctxlen); +int (*policy_brief)(char **brief,size_t *len,bool alloc);#ifdef CONFIG_SECURITY_NETWORK int (*unix_stream_connect)(struct sock *sock,struct sock *other,struct sock *newsk);
@@ -1806,6 +1822,7 @@ struct security_hook_heads {struct list_head inode_notifysecctx;struct list_head inode_setsecctx;struct list_head inode_getsecctx; +struct list_head policy_brief;#ifdef CONFIG_SECURITY_NETWORK struct list_head unix_stream_connect;struct list_head unix_may_send;
include/linux/security.h +7 -0
@@ -376,6 +376,8 @@ int security_sem_semop(struct sem_array *sma, struct sembuf *sops,int security_inode_notifysecctx (struct inode *inode,void *ctx,u32 ctxlen);int security_inode_setsecctx (struct dentry *dentry,void *ctx,u32 ctxlen);int security_inode_getsecctx (struct inode *inode,void **ctx,u32 *ctxlen); + +int security_policy_brief (char **brief,size_t *len,bool alloc);#else /* CONFIG_SECURITY */ struct security_mnt_opts { };
@@ -1159,6 +1161,11 @@ static inline int security_inode_getsecctx(struct inode *inode, void **ctx, u32 {return -EOPNOTSUPP ; } + +static inline int security_policy_brief (char **brief,size_t *len,bool alloc) +{ +return -EOPNOTSUPP ; +}#endif /* CONFIG_SECURITY */ #ifdef CONFIG_SECURITY_NETWORK
security/security.c +8 -0
@@ -1269,6 +1269,12 @@ int security_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) }EXPORT_SYMBOL (security_inode_getsecctx); +int security_policy_brief (char **brief,size_t *len,bool alloc) +{ +return call_int_hook (policy_brief, -EOPNOTSUPP , brief, len, alloc); +} +EXPORT_SYMBOL (security_policy_brief); +#ifdef CONFIG_SECURITY_NETWORK int security_unix_stream_connect (struct sock *sock,struct sock *other,struct sock *newsk)
@@ -1868,6 +1874,8 @@ struct security_hook_heads security_hook_heads __lsm_ro_after_init = { LIST_HEAD_INIT(security_hook_heads.inode_setsecctx), .inode_getsecctx = LIST_HEAD_INIT(security_hook_heads.inode_getsecctx), + .policy_brief = +LIST_HEAD_INIT (security_hook_heads.policy_brief), #ifdef CONFIG_SECURITY_NETWORK .unix_stream_connect = LIST_HEAD_INIT(security_hook_heads.unix_stream_connect),
security/selinux/hooks.c +7 -0
@@ -6063,6 +6063,11 @@ static int selinux_inode_getsecctx(struct inode *inode, void **ctx, u32 *ctxlen) *ctxlen = len;return 0 ; } + +static int selinux_policy_brief (char **brief,size_t *len,bool alloc) +{ +return security_policydb_brief (brief, len, alloc); +}#ifdef CONFIG_KEYS static int selinux_key_alloc(struct key *k, const struct cred *cred,
@@ -6277,6 +6282,8 @@ static int selinux_key_getsecurity(struct key *key, char **_buffer)LSM_HOOK_INIT (inode_setsecctx, selinux_inode_setsecctx),LSM_HOOK_INIT (inode_getsecctx, selinux_inode_getsecctx), +LSM_HOOK_INIT (policy_brief, selinux_policy_brief), +LSM_HOOK_INIT (unix_stream_connect, selinux_socket_unix_stream_connect), LSM_HOOK_INIT(unix_may_send, selinux_socket_unix_may_send),
security/selinux/include/security.h +2 -0
@@ -97,6 +97,8 @@ enum {int security_load_policy (void *data,size_t len);int security_read_policy (void **data,size_t *len);size_t security_policydb_len (void ); +int security_policydb_brief (char **brief,size_t *len,bool alloc); +void security_policydb_update_info (u32 requested);int security_policycap_supported (unsigned int req_cap);
security/selinux/selinuxfs.c +2 -0
@@ -159,6 +159,7 @@ static ssize_t sel_write_enforce(struct file *file, const char __user *buf,from_kuid (&init_user_ns,audit_get_loginuid (current)), audit_get_sessionid(current)); selinux_enforcing = new_value; +security_policydb_update_info (SECURITY__SETENFORCE );if (selinux_enforcing)avc_ss_reset (0 );selnl_notify_setenforce (selinux_enforcing);
@@ -621,6 +622,7 @@ static ssize_t sel_write_checkreqprot(struct file *file, const char __user *buf, goto out; selinux_checkreqprot = new_value ?1 :0 ; +security_policydb_update_info (SECURITY__SETCHECKREQPROT ); length = count; out:kfree (page);
security/selinux/ss/policydb.c +88 -0
122 lines not on this page · raw message
security/selinux/ss/policydb.h +3 -0
@@ -293,6 +293,8 @@ struct policydb {size_t len;unsigned int policyvers; +/* summary computed on the policy */ +unsigned char *policybrief; unsigned int reject_unknown : 1; unsigned int allow_unknown : 1;
@@ -309,6 +311,7 @@ struct policydb {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); +extern unsigned int policybrief_len;#define PERM_SYMTAB_SIZE 32
security/selinux/ss/services.c +68 -0
74 lines not on this page · raw message
Signature · 6 lines
1.8.3.1 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
PATCH v4 2/2 selinux: expose policy brief via selinuxfs
Expose policy brief via selinuxfs.
--- security/selinux/selinuxfs.c | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+)
security/selinux/selinuxfs.c +26 -0
@@ -99,6 +99,7 @@ enum sel_inos {SEL_STATUS ,/* export current status using mmap() */ SEL_POLICY ,/* allow userspace to read the in kernel policy */ SEL_VALIDATE_TRANS ,/* compute validatetrans decision */ +SEL_POLICYBRIEF ,/* return policy summary */ SEL_INO_NEXT, /* The next inode number to use */ };
@@ -314,6 +315,29 @@ static ssize_t sel_read_policyvers(struct file *filp, char __user *buf, .llseek = generic_file_llseek, }; +static ssize_t sel_read_policybrief (struct file *filp,char __user *buf, +size_t count,loff_t *ppos) +{ +char *tmpbuf; +size_t len; +ssize_t rc; + + rc =security_policydb_brief (&tmpbuf, &len, true); +if (rc) +return rc; + + rc =simple_read_from_buffer (buf, count, ppos, tmpbuf, len); + +kfree (tmpbuf); + +return rc; +} + +static const struct file_operations sel_policybrief_ops = { + .read = sel_read_policybrief, + .llseek = generic_file_llseek, +}; +/* declaration for sel_write_load */ static int sel_make_bools (void );static int sel_make_classes (void );
@@ -1827,6 +1851,8 @@ static int sel_fill_super(struct super_block *sb, void *data, int silent) [SEL_POLICY] = {"policy", &sel_policy_ops, S_IRUGO}, [SEL_VALIDATE_TRANS ] = {"validatetrans" , &sel_transition_ops, S_IWUGO}, + [SEL_POLICYBRIEF ] = {"policybrief" , &sel_policybrief_ops, +S_IRUGO }, /* last one */ {""} }; ret =simple_fill_super (sb,SELINUX_MAGIC , selinux_files);
Signature · 6 lines
1.8.3.1 -- To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org More majordomo info at http://vger.kernel.org/majordomo-info.html
Quoted 74 lines · Sebastien Buisson
Add policybrief field to struct policydb. It holds a brief info of the policydb, made of colon separated name and value pairs that give information about how the policy is applied in the security module(s).
Note that the ordering of the fields in the string may change.Policy brief is computed every time the policy is loaded, and when enforce or checkreqprot are changed.
Add
security_policy_briefhook to give access to policy brief to the rest of the kernel. Lustre client makes use of this information to detect changes to the policy, and forward it to Lustre servers. Depending on how the policy is enforced on Lustre client side, Lustre servers can refuse connection.Signed-off-by Sebastien Buisson <[email protected]>---
include/linux/lsm_hooks.h | 17 +++++++
include/linux/security.h | 7 +++
security/security.c | 8 ++++
security/selinux/hooks.c | 7 +++
security/selinux/include/security.h | 2 +
security/selinux/selinuxfs.c | 2 +
security/selinux/ss/policydb.c | 88
+++++++++++++++++++++++++++++++++++++
security/selinux/ss/policydb.h | 3 ++
security/selinux/ss/services.c | 68
++++++++++++++++++++++++++++9 files changed, 202 insertions(+)security/selinux/ss/policydb.c +1
-0@@ -32,13 +32,20 @@ #include <linux/errno.h> #include <linux/audit.h> #include <linux/flex_array.h> +#include <crypto/hash.h> #include "security.h"
#include "policydb.h"
#include "conditional.h"
#include "mls.h"
+#include "objsec.h"
#include "services.h"security/selinux/ss/policydb.c +5
-0+static unsigned int policybrief_hash_size; +static struct crypto_shash *policybrief_tfm; +static const char policybrief_hash_alg[] ="sha256" ; +unsigned int policybrief_len; +#define
_DEBUG_HASHES
#ifdefDEBUG_HASHESsecurity/selinux/ss/policydb.c +2
-0@@ -879,6 +886,8 @@ void policydb_destroy(struct policydb *p) ebitmap_destroy(&p->filename_trans_ttypes); ebitmap_destroy(&p->policycaps); ebitmap_destroy(&p->permissive_map); + +kfree (p->policybrief); }
/*security/selinux/ss/policydb.c +0
-0@@ -2220,6 +2229,52 @@ static int ocontext_read(struct policydb *p, struct policydb_compat_info *info, }
/*
+ * Compute summary of a policy database binary representation file,
+ * and store it into a policy database structure.
+ */
security/selinux/ss/policydb.c static int ocontext_read(struct policydb *p, struct policydb_compat_info *info, · line 2242
Quoted 8 lines
security/selinux/ss/policydb.c +8
-0+static int policydb_brief (struct policydb *policydb,void *ptr) +{ +struct policy_file *fp = ptr; +u8 *hashval; +int rc =0 ; + +if (policydb->policybrief) +return -EINVAL ;
Should this be a BUG_ON()?
security/selinux/ss/policydb.c static int ocontext_read(struct policydb *p, struct policydb_compat_info *info, · line 2249
Quoted 7 lines
security/selinux/ss/policydb.c +7
-0+ + hashval =kmalloc (policybrief_hash_size,GFP_KERNEL ); +if (hashval ==NULL ) +return -ENOMEM ; + + { +SHASH_DESC_ON_STACK (desc, policybrief_tfm);
You should be able to move this up to the parent scope now that policybrief_tfm is allocated at init. No need for a nested block then.
Quoted 76 lines
security/selinux/ss/policydb.c +3
-0+ desc->tfm = policybrief_tfm; + desc->flags =0 ; + rc = crypto_shash_digest(desc, fp->data, fp->len,hashval);
security/selinux/ss/policydb.c +2
-0+ if (rc) { + printk(KERN_ERR "Failed crypto_shash_digest:%d\n", rc);
security/selinux/ss/policydb.c +6
-0+ goto out_free; + } + } + +/* policy brief is in the form: +* selinux(enforce=<0 or 1>:checkreqprot=<0 or 1>:<hashalg>=<checksum>)
security/selinux/ss/policydb.c +3
-0+*/ + policydb->policybrief = kasprintf(GFP_KERNEL, + "selinux(enforce=%d:checkreqprot=%d:%s=%*phN)",
security/selinux/ss/policydb.c +4
-0+ selinux_enforcing, + selinux_checkreqprot, + policybrief_hash_alg, + policybrief_hash_size,hashval);
security/selinux/ss/policydb.c +10
-0+if (policydb->policybrief ==NULL ) + rc = -ENOMEM ; + +out_free: +kfree (hashval); + +return rc; +} + +/* * Read the configuration data from a policy database binary
* representation file into a policy database structure. */security/selinux/ss/policydb.c +0
-0@@ -2238,6 +2293,11 @@ int policydb_read(struct policydb *p, void *fp) if (rc) return rc;security/selinux/ss/policydb.c +5
-0+/* Compute summary of policy, and store it in policydb */ + rc =policydb_brief (p, fp); +if (rc) + goto bad; +/* Read the magic number and string length. */ rc =
next_entry(buf, fp, sizeof(u32) * 2); if (rc)security/selinux/ss/policydb.c +0
-0@@ -3456,3 +3516,31 @@ int policydb_write(struct policydb *p, void *fp)
return 0;
}security/selinux/ss/policydb.c +17
-0+ +static int __init init_policybrief_hash (void ) +{ +struct crypto_shash *tfm; + +if (!selinux_enabled) +return 0 ; + + tfm =crypto_alloc_shash (policybrief_hash_alg,0 ,0 ); +if (IS_ERR (tfm)) { +printk (KERN_ERR "Failed to alloc crypto hash %s\n" , + policybrief_hash_alg); +return PTR_ERR (tfm); + } + + policybrief_tfm = tfm; + policybrief_hash_size =
crypto_shash_digestsize(policybrief_tfm);security/selinux/ss/policydb.c +3
-0+ +/* policy brief is in the form: +* selinux(enforce=<0 or 1>:checkreqprot=<0 or 1>:<hashalg>=<checksum>)
security/selinux/ss/policydb.c int policydb_write(struct policydb *p, void *fp) · line 3541
Quoted 3 lines
security/selinux/ss/policydb.c +3
-0+*/ + policybrief_len =35 +strlen (policybrief_hash_alg) + +2 *policybrief_hash_size;
Might as well include the + 1 for the terminating NUL here?
Quoted 22 lines
security/selinux/ss/policydb.c +5
-0+ +return 0 ; +} + +late_initcall (init_policybrief_hash);security/selinux/ss/services.c +0
-0@@ -2170,6 +2170,74 @@ size_t security_policydb_len(void) }
/**
+ *security_policydb_brief- Get policydb brief
+ * @brief: pointer to buffer holding brief
+ * @len: in: brief buffer length if no alloc, out: brief string len
+ * @alloc: whether to allocate buffer for brief or not
+ *
+ * On success 0 is returned , or negative value on error.
+ **/
security/selinux/ss/services.c size_t security_policydb_len(void) · line 2186
Quoted 7 lines
security/selinux/ss/services.c +7
-0+int security_policydb_brief (char **brief,size_t *len,bool alloc) +{ +if (!ss_initialized || brief ==NULL ) +return -EINVAL ; + +if (alloc) { +/* *brief must be kfreed by caller in this case */
What's the point in putting such a comment here in the code; it is essentially interface documentation?
security/selinux/ss/services.c size_t security_policydb_len(void) · line 2191
Quoted 5 lines
security/selinux/ss/services.c +5
-0+ *brief =kzalloc (policybrief_len +1 ,GFP_KERNEL ); +/* +* if !alloc, caller must pass a buffer that +* can hold policybrief_len+1 chars +*/
Ditto. And why not just include the + 1 in the policybrief_len in the first place?
security/selinux/ss/services.c size_t security_policydb_len(void) · line 2194
Quoted 3 lines
security/selinux/ss/services.c +3
-0+ }else if (*len < policybrief_len +1 ) { +/* put in *len the string size we need to write */ + *len = policybrief_len;
The caller needs to allocate a buffer of policybrief_len+1, so might as well include the +1 in the length we return to the caller. Less opportunity for error.
security/selinux/ss/services.c size_t security_policydb_len(void) · line 2203
Quoted 9 lines
security/selinux/ss/services.c +9
-0+return -ENAMETOOLONG ; + } + +if (*brief ==NULL ) +return -ENOMEM ; + +read_lock (&policy_rwlock); + *len = policybrief_len; +strncpy (*brief, policydb.policybrief, *len);
Why can't we just do a simple strcpy() here?
Quoted 41 lines
security/selinux/ss/services.c +9
-0+read_unlock (&policy_rwlock); + +return 0 ; +} + +void security_policydb_update_info (u32 requested) +{ +/* policy brief is in the form: +* selinux(enforce=<0 or 1>:checkreqprot=<0 or 1>:<hashalg>=<checksum>)
security/selinux/ss/services.c +28
-0+*/ +char enforce[] ="enforce=" ; +char checkreqprot[] ="checkreqprot=" ; +char *p, *str; +int val; + +if (!ss_initialized) +return ; + +if (requested ==SECURITY__SETENFORCE ) { + str = enforce; + val = selinux_enforcing; + }else if (requested ==SECURITY__SETCHECKREQPROT ) { + str = checkreqprot; + val = selinux_checkreqprot; + } + +/* update global policydb, needs write lock */ +write_lock_irq (&policy_rwlock); + p =strstr (policydb.policybrief, str); +if (p) { + p +=strlen (str); + *p ='0' + val; + } +write_unlock_irq (&policy_rwlock); +} + +/** *
security_port_sid- Obtain the SID for a port.
* @protocol: protocol number
* @port: port number
Quoted 5 lines
Add
security_policy_briefhook to give access to policy brief to the rest of the kernel. Lustre client makes use of this information to detect changes to the policy, and forward it to Lustre servers. Depending on how the policy is enforced on Lustre client side, Lustre servers can refuse connection.
NAK for any core kernel hooks for code that is purely in staging.
--
To unsubscribe from this list: send the line "unsubscribe linux-security-module" in the body of a message to majordomo at vger.kernel.org
More majordomo info at http://vger.kernel.org/majordomo-info.html