Merge branch 'master' of ../mmc
[pandora-kernel.git] / kernel / cred.c
index f3ca106..ff7bc07 100644 (file)
@@ -274,6 +274,7 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags)
        struct thread_group_cred *tgcred;
 #endif
        struct cred *new;
+       int ret;
 
        mutex_init(&p->cred_exec_mutex);
 
@@ -293,6 +294,12 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags)
        if (!new)
                return -ENOMEM;
 
+       if (clone_flags & CLONE_NEWUSER) {
+               ret = create_user_ns(new);
+               if (ret < 0)
+                       goto error_put;
+       }
+
 #ifdef CONFIG_KEYS
        /* new threads get their own thread keyrings if their parent already
         * had one */
@@ -309,8 +316,8 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags)
        if (!(clone_flags & CLONE_THREAD)) {
                tgcred = kmalloc(sizeof(*tgcred), GFP_KERNEL);
                if (!tgcred) {
-                       put_cred(new);
-                       return -ENOMEM;
+                       ret = -ENOMEM;
+                       goto error_put;
                }
                atomic_set(&tgcred->usage, 1);
                spin_lock_init(&tgcred->lock);
@@ -325,6 +332,10 @@ int copy_creds(struct task_struct *p, unsigned long clone_flags)
        atomic_inc(&new->user->processes);
        p->cred = p->real_cred = get_cred(new);
        return 0;
+
+error_put:
+       put_cred(new);
+       return ret;
 }
 
 /**
@@ -462,3 +473,116 @@ void __init cred_init(void)
        cred_jar = kmem_cache_create("cred_jar", sizeof(struct cred),
                                     0, SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
 }
+
+/**
+ * prepare_kernel_cred - Prepare a set of credentials for a kernel service
+ * @daemon: A userspace daemon to be used as a reference
+ *
+ * Prepare a set of credentials for a kernel service.  This can then be used to
+ * override a task's own credentials so that work can be done on behalf of that
+ * task that requires a different subjective context.
+ *
+ * @daemon is used to provide a base for the security record, but can be NULL.
+ * If @daemon is supplied, then the security data will be derived from that;
+ * otherwise they'll be set to 0 and no groups, full capabilities and no keys.
+ *
+ * The caller may change these controls afterwards if desired.
+ *
+ * Returns the new credentials or NULL if out of memory.
+ *
+ * Does not take, and does not return holding current->cred_replace_mutex.
+ */
+struct cred *prepare_kernel_cred(struct task_struct *daemon)
+{
+       const struct cred *old;
+       struct cred *new;
+
+       new = kmem_cache_alloc(cred_jar, GFP_KERNEL);
+       if (!new)
+               return NULL;
+
+       if (daemon)
+               old = get_task_cred(daemon);
+       else
+               old = get_cred(&init_cred);
+
+       get_uid(new->user);
+       get_group_info(new->group_info);
+
+#ifdef CONFIG_KEYS
+       atomic_inc(&init_tgcred.usage);
+       new->tgcred = &init_tgcred;
+       new->request_key_auth = NULL;
+       new->thread_keyring = NULL;
+       new->jit_keyring = KEY_REQKEY_DEFL_THREAD_KEYRING;
+#endif
+
+#ifdef CONFIG_SECURITY
+       new->security = NULL;
+#endif
+       if (security_prepare_creds(new, old, GFP_KERNEL) < 0)
+               goto error;
+
+       atomic_set(&new->usage, 1);
+       put_cred(old);
+       return new;
+
+error:
+       put_cred(new);
+       return NULL;
+}
+EXPORT_SYMBOL(prepare_kernel_cred);
+
+/**
+ * set_security_override - Set the security ID in a set of credentials
+ * @new: The credentials to alter
+ * @secid: The LSM security ID to set
+ *
+ * Set the LSM security ID in a set of credentials so that the subjective
+ * security is overridden when an alternative set of credentials is used.
+ */
+int set_security_override(struct cred *new, u32 secid)
+{
+       return security_kernel_act_as(new, secid);
+}
+EXPORT_SYMBOL(set_security_override);
+
+/**
+ * set_security_override_from_ctx - Set the security ID in a set of credentials
+ * @new: The credentials to alter
+ * @secctx: The LSM security context to generate the security ID from.
+ *
+ * Set the LSM security ID in a set of credentials so that the subjective
+ * security is overridden when an alternative set of credentials is used.  The
+ * security ID is specified in string form as a security context to be
+ * interpreted by the LSM.
+ */
+int set_security_override_from_ctx(struct cred *new, const char *secctx)
+{
+       u32 secid;
+       int ret;
+
+       ret = security_secctx_to_secid(secctx, strlen(secctx), &secid);
+       if (ret < 0)
+               return ret;
+
+       return set_security_override(new, secid);
+}
+EXPORT_SYMBOL(set_security_override_from_ctx);
+
+/**
+ * set_create_files_as - Set the LSM file create context in a set of credentials
+ * @new: The credentials to alter
+ * @inode: The inode to take the context from
+ *
+ * Change the LSM file creation context in a set of credentials to be the same
+ * as the object context of the specified inode, so that the new inodes have
+ * the same MAC context as that inode.
+ */
+int set_create_files_as(struct cred *new, struct inode *inode)
+{
+       new->fsuid = inode->i_uid;
+       new->fsgid = inode->i_gid;
+       return security_kernel_create_files_as(new, inode);
+}
+EXPORT_SYMBOL(set_create_files_as);