]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blobdiff - security/security.c
perf xyarray: Fix wrong processing when closing evsel fd
[mirror_ubuntu-artful-kernel.git] / security / security.c
index 72f38cbadbb7da6bc352a395c5c97f6c747032a4..95d6b85a951c30c55b08fbd70fd6d86a487709da 100644 (file)
 #include <linux/personality.h>
 #include <linux/backing-dev.h>
 #include <linux/string.h>
-#include <linux/msg.h>
 #include <linux/prctl.h>
 #include <net/flow.h>
-#include <net/sock.h>
 
 #define MAX_LSM_EVM_XATTR      2
 
@@ -51,7 +49,7 @@ char *lsm_names;
  */
 static struct lsm_blob_sizes blob_sizes = {
 #ifdef CONFIG_SECURITY_STACKING
-       .lbs_task = SECURITY_NAME_MAX + 2,
+       .lbs_task = SECURITY_NAME_MAX + 6,
 #endif
 };
 
@@ -62,6 +60,12 @@ static __initdata char chosen_lsms[SECURITY_CHOSEN_NAMES_MAX + 1] =
 #else
        CONFIG_DEFAULT_SECURITY;
 #endif
+static __initdata char chosen_display_lsm[SECURITY_NAME_MAX + 1]
+#ifdef CONFIG_SECURITY_STACKING
+       = CONFIG_SECURITY_DEFAULT_DISPLAY_NAME
+#endif
+;
+static char default_display_lsm[SECURITY_NAME_MAX + 1];
 
 static void __init do_security_initcalls(void)
 {
@@ -111,16 +115,8 @@ int __init security_init(void)
 #ifdef CONFIG_SECURITY_LSM_DEBUG
        pr_info("LSM: cred blob size       = %d\n", blob_sizes.lbs_cred);
        pr_info("LSM: file blob size       = %d\n", blob_sizes.lbs_file);
-       pr_info("LSM: inode blob size      = %d\n", blob_sizes.lbs_inode);
-       pr_info("LSM: ipc blob size        = %d\n", blob_sizes.lbs_ipc);
-#ifdef CONFIG_KEYS
-       pr_info("LSM: key blob size        = %d\n", blob_sizes.lbs_key);
-#endif /* CONFIG_KEYS */
-       pr_info("LSM: msg_msg blob size    = %d\n", blob_sizes.lbs_msg_msg);
-       pr_info("LSM: sock blob size       = %d\n", blob_sizes.lbs_sock);
-       pr_info("LSM: superblock blob size = %d\n", blob_sizes.lbs_superblock);
        pr_info("LSM: task blob size       = %d\n", blob_sizes.lbs_task);
-#endif /* CONFIG_SECURITY_LSM_DEBUG */
+#endif
 
        return 0;
 }
@@ -135,6 +131,15 @@ static int __init choose_lsm(char *str)
 }
 __setup("security=", choose_lsm);
 
+static int __init choose_display_lsm(char *str)
+{
+       strncpy(chosen_display_lsm, str, SECURITY_NAME_MAX);
+       pr_info("LSM: command line set default display lsm %s'\n",
+               chosen_display_lsm);
+       return 1;
+}
+__setup("security.display=", choose_display_lsm);
+
 static bool match_last_lsm(const char *list, const char *lsm)
 {
        const char *last;
@@ -216,20 +221,44 @@ bool __init security_module_enable(const char *lsm, const bool stacked)
        /*
         * Module defined on the command line security=XXXX
         */
-       if (strcmp(chosen_lsms, MODULE_STACK))
-               return cmp_lsms(lsm);
-
+       if (strcmp(chosen_lsms, MODULE_STACK)) {
+               if (cmp_lsms(lsm)) {
+                       /* set to first LSM registered and then override */
+                       if (!*default_display_lsm)
+                               strcpy(default_display_lsm, lsm);
+                       else if (*chosen_display_lsm && !strcmp(chosen_display_lsm, lsm)) {
+                               strcpy(default_display_lsm, lsm);
+                               pr_info("LSM: default display lsm '%s'\n", default_display_lsm);
+                       }
+                       return true;
+               }
+               return false;
+       }
        /*
         * Module configured as stacked.
         */
+       if (stacked && !*default_display_lsm)
+               strcpy(default_display_lsm, lsm);
+       else if (stacked && *chosen_display_lsm && !strcmp(chosen_display_lsm, lsm)) {
+               strcpy(default_display_lsm, lsm);
+               pr_info("LSM: default display lsm '%s'\n", default_display_lsm);
+       }
+
        return stacked;
 #else
-       if (strcmp(lsm, chosen_lsms) == 0)
+       if (strcmp(lsm, chosen_lsms) == 0) {
+               strcpy(default_display_lsm, lsm);
                return true;
+       }
        return false;
 #endif
 }
 
+/*
+ * Keep the order of major modules for mapping secids.
+ */
+static int lsm_next_major;
+
 /**
  * security_add_hooks - Add a modules hooks to the hook lists.
  * @hooks: the hooks to add
@@ -242,8 +271,14 @@ void __init security_add_hooks(struct security_hook_list *hooks, int count,
                                char *lsm)
 {
        int i;
+       int lsm_index = lsm_next_major++;
+
+#ifdef CONFIG_SECURITY_LSM_DEBUG
+       pr_info("LSM: Security module %s gets index %d\n", lsm, lsm_index);
+#endif
        for (i = 0; i < count; i++) {
                hooks[i].lsm = lsm;
+               hooks[i].lsm_index = lsm_index;
                list_add_tail_rcu(&hooks[i].list, hooks[i].head);
        }
        if (lsm_append(lsm, &lsm_names) < 0)
@@ -336,19 +371,7 @@ void __init security_add_blobs(struct lsm_blob_sizes *needed)
 {
        lsm_set_size(&needed->lbs_cred, &blob_sizes.lbs_cred);
        lsm_set_size(&needed->lbs_file, &blob_sizes.lbs_file);
-       lsm_set_size(&needed->lbs_ipc, &blob_sizes.lbs_ipc);
-       lsm_set_size(&needed->lbs_key, &blob_sizes.lbs_key);
-       lsm_set_size(&needed->lbs_msg_msg, &blob_sizes.lbs_msg_msg);
-       lsm_set_size(&needed->lbs_sock, &blob_sizes.lbs_sock);
-       lsm_set_size(&needed->lbs_superblock, &blob_sizes.lbs_superblock);
        lsm_set_size(&needed->lbs_task, &blob_sizes.lbs_task);
-       /*
-        * The inode blob gets an rcu_head in addition to
-        * what the modules might need.
-        */
-       if (needed->lbs_inode && blob_sizes.lbs_inode == 0)
-               blob_sizes.lbs_inode = sizeof(struct rcu_head);
-       lsm_set_size(&needed->lbs_inode, &blob_sizes.lbs_inode);
 }
 
 /**
@@ -457,170 +480,12 @@ int lsm_task_alloc(struct task_struct *task)
 #ifdef CONFIG_SECURITY_STACKING
        if (current->security)
                strcpy(task->security, lsm_of_task(current));
+       else
+               strcpy(task->security, default_display_lsm);
 #endif
        return 0;
 }
 
-/**
- * lsm_inode_alloc - allocate a composite inode blob
- * @inode: the inode that needs a blob
- *
- * Allocate the inode blob for all the modules
- *
- * Returns 0, or -ENOMEM if memory can't be allocated.
- */
-int lsm_inode_alloc(struct inode *inode)
-{
-#ifdef CONFIG_SECURITY_LSM_DEBUG
-       if (inode->i_security)
-               pr_info("%s: Inbound inode blob is not NULL.\n", __func__);
-#endif
-       if (blob_sizes.lbs_inode == 0)
-               return 0;
-
-       inode->i_security = kzalloc(blob_sizes.lbs_inode, GFP_KERNEL);
-       if (inode->i_security == NULL)
-               return -ENOMEM;
-       return 0;
-}
-
-/**
- * lsm_early_inode - during initialization allocate a composite inode blob
- * @inode: the inode that needs a blob
- *
- * Allocate the inode blob for all the modules if it's not already there
- */
-void lsm_early_inode(struct inode *inode)
-{
-       int rc;
-
-       if (inode == NULL)
-               panic("%s: NULL inode.\n", __func__);
-       if (inode->i_security != NULL)
-               return;
-       rc = lsm_inode_alloc(inode);
-       if (rc)
-               panic("%s: Early inode alloc failed.\n", __func__);
-}
-
-/**
- * lsm_ipc_alloc - allocate a composite ipc blob
- * @kip: the ipc that needs a blob
- *
- * Allocate the ipc blob for all the modules
- *
- * Returns 0, or -ENOMEM if memory can't be allocated.
- */
-int lsm_ipc_alloc(struct kern_ipc_perm *kip)
-{
-#ifdef CONFIG_SECURITY_LSM_DEBUG
-       if (kip->security)
-               pr_info("%s: Inbound ipc blob is not NULL.\n", __func__);
-#endif
-       if (blob_sizes.lbs_ipc == 0)
-               return 0;
-
-       kip->security = kzalloc(blob_sizes.lbs_ipc, GFP_KERNEL);
-       if (kip->security == NULL)
-               return -ENOMEM;
-       return 0;
-}
-
-#ifdef CONFIG_KEYS
-/**
- * lsm_key_alloc - allocate a composite key blob
- * @key: the key that needs a blob
- *
- * Allocate the key blob for all the modules
- *
- * Returns 0, or -ENOMEM if memory can't be allocated.
- */
-int lsm_key_alloc(struct key *key)
-{
-#ifdef CONFIG_SECURITY_LSM_DEBUG
-       if (key->security)
-               pr_info("%s: Inbound key blob is not NULL.\n", __func__);
-#endif
-       if (blob_sizes.lbs_key == 0)
-               return 0;
-
-       key->security = kzalloc(blob_sizes.lbs_key, GFP_KERNEL);
-       if (key->security == NULL)
-               return -ENOMEM;
-       return 0;
-}
-#endif /* CONFIG_KEYS */
-
-/**
- * lsm_msg_msg_alloc - allocate a composite msg_msg blob
- * @mp: the msg_msg that needs a blob
- *
- * Allocate the ipc blob for all the modules
- *
- * Returns 0, or -ENOMEM if memory can't be allocated.
- */
-int lsm_msg_msg_alloc(struct msg_msg *mp)
-{
-#ifdef CONFIG_SECURITY_LSM_DEBUG
-       if (mp->security)
-               pr_info("%s: Inbound msg_msg blob is not NULL.\n", __func__);
-#endif
-       if (blob_sizes.lbs_msg_msg == 0)
-               return 0;
-
-       mp->security = kzalloc(blob_sizes.lbs_msg_msg, GFP_KERNEL);
-       if (mp->security == NULL)
-               return -ENOMEM;
-       return 0;
-}
-
-/**
- * lsm_sock_alloc - allocate a composite sock blob
- * @sock: the sock that needs a blob
- * @priority: allocation mode
- *
- * Allocate the sock blob for all the modules
- *
- * Returns 0, or -ENOMEM if memory can't be allocated.
- */
-int lsm_sock_alloc(struct sock *sock, gfp_t priority)
-{
-#ifdef CONFIG_SECURITY_LSM_DEBUG
-       if (sock->sk_security)
-               pr_info("%s: Inbound sock blob is not NULL.\n", __func__);
-#endif
-       if (blob_sizes.lbs_sock == 0)
-               return 0;
-
-       sock->sk_security = kzalloc(blob_sizes.lbs_sock, priority);
-       if (sock->sk_security == NULL)
-               return -ENOMEM;
-       return 0;
-}
-
-/**
- * lsm_superblock_alloc - allocate a composite superblock blob
- * @sb: the superblock that needs a blob
- *
- * Allocate the superblock blob for all the modules
- *
- * Returns 0, or -ENOMEM if memory can't be allocated.
- */
-int lsm_superblock_alloc(struct super_block *sb)
-{
-#ifdef CONFIG_SECURITY_LSM_DEBUG
-       if (sb->s_security)
-               pr_info("%s: Inbound superblock blob is not NULL.\n", __func__);
-#endif
-       if (blob_sizes.lbs_superblock == 0)
-               return 0;
-
-       sb->s_security = kzalloc(blob_sizes.lbs_superblock, GFP_KERNEL);
-       if (sb->s_security == NULL)
-               return -ENOMEM;
-       return 0;
-}
-
 /*
  * Hook list operation macros.
  *
@@ -793,18 +658,12 @@ int security_bprm_secureexec(struct linux_binprm *bprm)
 
 int security_sb_alloc(struct super_block *sb)
 {
-       int rc = lsm_superblock_alloc(sb);
-
-       if (rc)
-               return rc;
        return call_int_hook(sb_alloc_security, 0, sb);
 }
 
 void security_sb_free(struct super_block *sb)
 {
        call_void_hook(sb_free_security, sb);
-       kfree(sb->s_security);
-       sb->s_security = NULL;
 }
 
 int security_sb_copy_data(char *orig, char *copy)
@@ -878,39 +737,14 @@ EXPORT_SYMBOL(security_sb_parse_opts_str);
 
 int security_inode_alloc(struct inode *inode)
 {
-       int rc = lsm_inode_alloc(inode);
-
-       if (rc)
-               return rc;
+       inode->i_security = NULL;
        return call_int_hook(inode_alloc_security, 0, inode);
 }
 
-static void inode_free_by_rcu(struct rcu_head *head)
-{
-       /*
-        * The rcu head is at the start of the inode blob
-        */
-       kfree(head);
-}
-
 void security_inode_free(struct inode *inode)
 {
        integrity_inode_free(inode);
        call_void_hook(inode_free_security, inode);
-       /*
-        * The inode may still be referenced in a path walk and
-        * a call to security_inode_permission() can be made
-        * after inode_free_security() is called. Ideally, the VFS
-        * wouldn't do this, but fixing that is a much harder
-        * job. For now, simply free the i_security via RCU, and
-        * leave the current inode->i_security pointer intact.
-        * The inode will be freed after the RCU grace period too.
-        */
-       if (inode->i_security != NULL) {
-               call_rcu((struct rcu_head *)inode->i_security,
-                               inode_free_by_rcu);
-               inode->i_security = NULL;
-       }
 }
 
 int security_dentry_init_security(struct dentry *dentry, int mode,
@@ -1635,6 +1469,33 @@ int security_task_kill(struct task_struct *p, struct siginfo *info,
 static char *nolsm = "-default";
 #define NOLSMLEN       9
 
+static bool is_registered_lsm(const char *str, size_t size)
+{
+       struct security_hook_list *hp;
+
+       list_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
+               if (size == strlen(hp->lsm) && !strncmp(str, hp->lsm, size))
+                       return true;
+       }
+
+       return false;
+}
+
+static bool set_lsm_of_current(const char *str, size_t size)
+{
+       char *lsm = lsm_of_task(current);
+
+       if (is_registered_lsm(str, size)) {
+               strncpy(lsm, str, size);
+               lsm[size] = '\0';
+       } else if (size == NOLSMLEN && !strncmp(str, nolsm, size)) {
+               lsm[0] = '\0';
+       } else {
+               return false;
+       }
+       return true;
+}
+
 static int lsm_task_prctl(int option, unsigned long arg2, unsigned long arg3,
                                unsigned long arg4, unsigned long arg5)
 {
@@ -1667,13 +1528,9 @@ static int lsm_task_prctl(int option, unsigned long arg2, unsigned long arg3,
                if (copy_from_user(buffer, optval, len))
                        return -EFAULT;
                buffer[len] = '\0';
-               /*
-                * Trust the caller to know what lsm name(s) are available.
-                */
-               if (!strncmp(buffer, nolsm, NOLSMLEN))
-                       lsm[0] = '\0';
-               else
-                       strcpy(lsm, buffer);
+               /* verify the requested LSM is registered */
+               if (!set_lsm_of_current(buffer, len))
+                       return -ENOENT;
                break;
        default:
                return -ENOSYS;
@@ -1724,36 +1581,22 @@ void security_ipc_getsecid(struct kern_ipc_perm *ipcp, u32 *secid)
 
 int security_msg_msg_alloc(struct msg_msg *msg)
 {
-       int rc = lsm_msg_msg_alloc(msg);
-
-       if (rc)
-               return rc;
        return call_int_hook(msg_msg_alloc_security, 0, msg);
 }
 
 void security_msg_msg_free(struct msg_msg *msg)
 {
        call_void_hook(msg_msg_free_security, msg);
-       kfree(msg->security);
-       msg->security = NULL;
 }
 
 int security_msg_queue_alloc(struct msg_queue *msq)
 {
-       int rc = lsm_ipc_alloc(&msq->q_perm);
-
-       if (rc)
-               return rc;
        return call_int_hook(msg_queue_alloc_security, 0, msq);
 }
 
 void security_msg_queue_free(struct msg_queue *msq)
 {
-       struct kern_ipc_perm *kip = &msq->q_perm;
-
        call_void_hook(msg_queue_free_security, msq);
-       kfree(kip->security);
-       kip->security = NULL;
 }
 
 int security_msg_queue_associate(struct msg_queue *msq, int msqflg)
@@ -1780,20 +1623,12 @@ int security_msg_queue_msgrcv(struct msg_queue *msq, struct msg_msg *msg,
 
 int security_shm_alloc(struct shmid_kernel *shp)
 {
-       int rc = lsm_ipc_alloc(&shp->shm_perm);
-
-       if (rc)
-               return rc;
        return call_int_hook(shm_alloc_security, 0, shp);
 }
 
 void security_shm_free(struct shmid_kernel *shp)
 {
-       struct kern_ipc_perm *kip = &shp->shm_perm;
-
        call_void_hook(shm_free_security, shp);
-       kfree(kip->security);
-       kip->security = NULL;
 }
 
 int security_shm_associate(struct shmid_kernel *shp, int shmflg)
@@ -1813,20 +1648,12 @@ int security_shm_shmat(struct shmid_kernel *shp, char __user *shmaddr, int shmfl
 
 int security_sem_alloc(struct sem_array *sma)
 {
-       int rc = lsm_ipc_alloc(&sma->sem_perm);
-
-       if (rc)
-               return rc;
        return call_int_hook(sem_alloc_security, 0, sma);
 }
 
 void security_sem_free(struct sem_array *sma)
 {
-       struct kern_ipc_perm *kip = &sma->sem_perm;
-
        call_void_hook(sem_free_security, sma);
-       kfree(kip->security);
-       kip->security = NULL;
 }
 
 int security_sem_associate(struct sem_array *sma, int semflg)
@@ -1901,6 +1728,11 @@ int security_getprocattr(struct task_struct *p, const char *lsm, char *name,
                if (rc > 0)
                        return strlen(*value);
                return rc;
+       } else if (strcmp(name, "display_lsm") == 0) {
+               *value = kstrdup(current->security, GFP_KERNEL);
+               if (*value == NULL)
+                       return -ENOMEM;
+               return strlen(*value);
        }
 
        list_for_each_entry(hp, &security_hook_heads.getprocattr, list) {
@@ -1932,6 +1764,9 @@ int security_setprocattr(const char *lsm, const char *name, void *value,
        char *temp;
        char *cp;
 
+       if (!size)
+               return -EINVAL;
+
        /*
         * If lsm is NULL look at all the modules to find one
         * that processes name. If lsm is not NULL only look at
@@ -2003,6 +1838,12 @@ free_out:
                if (rc >= 0)
                        return size;
                return rc;
+       } else if (strcmp(name, "display_lsm") == 0) {
+#ifdef CONFIG_SECURITY_STACKING
+               if (set_lsm_of_current(value, size))
+                       return size;
+#endif
+               return -EINVAL;
        }
 
        list_for_each_entry(hp, &security_hook_heads.setprocattr, list) {
@@ -2203,18 +2044,12 @@ EXPORT_SYMBOL(security_socket_getpeersec_dgram);
 
 int security_sk_alloc(struct sock *sk, int family, gfp_t priority)
 {
-       int rc = lsm_sock_alloc(sk, priority);
-
-       if (rc)
-               return rc;
        return call_int_hook(sk_alloc_security, 0, sk, family, priority);
 }
 
 void security_sk_free(struct sock *sk)
 {
        call_void_hook(sk_free_security, sk);
-       kfree(sk->sk_security);
-       sk->sk_security = NULL;
 }
 
 void security_sk_clone(const struct sock *sk, struct sock *newsk)
@@ -2444,18 +2279,12 @@ EXPORT_SYMBOL(security_skb_classify_flow);
 int security_key_alloc(struct key *key, const struct cred *cred,
                       unsigned long flags)
 {
-       int rc = lsm_key_alloc(key);
-
-       if (rc)
-               return rc;
        return call_int_hook(key_alloc, 0, key, cred, flags);
 }
 
 void security_key_free(struct key *key)
 {
        call_void_hook(key_free, key);
-       kfree(key->security);
-       key->security = NULL;
 }
 
 int security_key_permission(key_ref_t key_ref,