]> nv-tegra.nvidia Code Review - linux-3.10.git/blobdiff - security/keys/user_defined.c
KEYS/DNS: Fix ____call_usermodehelper() to not lose the session keyring
[linux-3.10.git] / security / keys / user_defined.c
index c33d3614a0dbf00724fad2c6b4f9729e274e57e2..5b366d7af3c4dc17b595c67af3dbe99d5fd69df1 100644 (file)
 
 #include <linux/module.h>
 #include <linux/init.h>
-#include <linux/sched.h>
 #include <linux/slab.h>
 #include <linux/seq_file.h>
 #include <linux/err.h>
+#include <keys/user-type.h>
 #include <asm/uaccess.h>
 #include "internal.h"
 
-static int user_instantiate(struct key *key, const void *data, size_t datalen);
-static int user_duplicate(struct key *key, const struct key *source);
-static int user_update(struct key *key, const void *data, size_t datalen);
-static int user_match(const struct key *key, const void *criterion);
-static void user_destroy(struct key *key);
-static void user_describe(const struct key *user, struct seq_file *m);
-static long user_read(const struct key *key,
-                     char __user *buffer, size_t buflen);
-
 /*
  * user defined keys take an arbitrary string as the description and an
  * arbitrary blob of data as the payload
@@ -34,25 +25,20 @@ static long user_read(const struct key *key,
 struct key_type key_type_user = {
        .name           = "user",
        .instantiate    = user_instantiate,
-       .duplicate      = user_duplicate,
        .update         = user_update,
        .match          = user_match,
+       .revoke         = user_revoke,
        .destroy        = user_destroy,
        .describe       = user_describe,
        .read           = user_read,
 };
 
-struct user_key_payload {
-       struct rcu_head rcu;            /* RCU destructor */
-       unsigned short  datalen;        /* length of this data */
-       char            data[0];        /* actual data */
-};
+EXPORT_SYMBOL_GPL(key_type_user);
 
-/*****************************************************************************/
 /*
  * instantiate a user defined key
  */
-static int user_instantiate(struct key *key, const void *data, size_t datalen)
+int user_instantiate(struct key *key, const void *data, size_t datalen)
 {
        struct user_key_payload *upayload;
        int ret;
@@ -76,60 +62,17 @@ static int user_instantiate(struct key *key, const void *data, size_t datalen)
        rcu_assign_pointer(key->payload.data, upayload);
        ret = 0;
 
- error:
-       return ret;
-
-} /* end user_instantiate() */
-
-/*****************************************************************************/
-/*
- * duplicate a user defined key
- * - both keys' semaphores are locked against further modification
- * - the new key cannot yet be accessed
- */
-static int user_duplicate(struct key *key, const struct key *source)
-{
-       struct user_key_payload *upayload, *spayload;
-       int ret;
-
-       /* just copy the payload */
-       ret = -ENOMEM;
-       upayload = kmalloc(sizeof(*upayload) + source->datalen, GFP_KERNEL);
-       if (upayload) {
-               spayload = rcu_dereference(source->payload.data);
-               BUG_ON(source->datalen != spayload->datalen);
-
-               upayload->datalen = key->datalen = spayload->datalen;
-               memcpy(upayload->data, spayload->data, key->datalen);
-
-               key->payload.data = upayload;
-               ret = 0;
-       }
-
+error:
        return ret;
+}
 
-} /* end user_duplicate() */
-
-/*****************************************************************************/
-/*
- * dispose of the old data from an updated user defined key
- */
-static void user_update_rcu_disposal(struct rcu_head *rcu)
-{
-       struct user_key_payload *upayload;
-
-       upayload = container_of(rcu, struct user_key_payload, rcu);
+EXPORT_SYMBOL_GPL(user_instantiate);
 
-       kfree(upayload);
-
-} /* end user_update_rcu_disposal() */
-
-/*****************************************************************************/
 /*
  * update a user defined key
  * - the key's semaphore is write-locked
  */
-static int user_update(struct key *key, const void *data, size_t datalen)
+int user_update(struct key *key, const void *data, size_t datalen)
 {
        struct user_key_payload *upayload, *zap;
        int ret;
@@ -159,59 +102,77 @@ static int user_update(struct key *key, const void *data, size_t datalen)
                key->expiry = 0;
        }
 
-       call_rcu(&zap->rcu, user_update_rcu_disposal);
+       kfree_rcu(zap, rcu);
 
- error:
+error:
        return ret;
+}
 
-} /* end user_update() */
+EXPORT_SYMBOL_GPL(user_update);
 
-/*****************************************************************************/
 /*
  * match users on their name
  */
-static int user_match(const struct key *key, const void *description)
+int user_match(const struct key *key, const void *description)
 {
        return strcmp(key->description, description) == 0;
+}
+
+EXPORT_SYMBOL_GPL(user_match);
+
+/*
+ * dispose of the links from a revoked keyring
+ * - called with the key sem write-locked
+ */
+void user_revoke(struct key *key)
+{
+       struct user_key_payload *upayload = key->payload.data;
 
-} /* end user_match() */
+       /* clear the quota */
+       key_payload_reserve(key, 0);
+
+       if (upayload) {
+               rcu_assign_pointer(key->payload.data, NULL);
+               kfree_rcu(upayload, rcu);
+       }
+}
+
+EXPORT_SYMBOL(user_revoke);
 
-/*****************************************************************************/
 /*
- * dispose of the data dangling from the corpse of a user
+ * dispose of the data dangling from the corpse of a user key
  */
-static void user_destroy(struct key *key)
+void user_destroy(struct key *key)
 {
        struct user_key_payload *upayload = key->payload.data;
 
        kfree(upayload);
+}
 
-} /* end user_destroy() */
+EXPORT_SYMBOL_GPL(user_destroy);
 
-/*****************************************************************************/
 /*
  * describe the user key
  */
-static void user_describe(const struct key *key, struct seq_file *m)
+void user_describe(const struct key *key, struct seq_file *m)
 {
        seq_puts(m, key->description);
+       if (key_is_instantiated(key))
+               seq_printf(m, ": %u", key->datalen);
+}
 
-       seq_printf(m, ": %u", key->datalen);
-
-} /* end user_describe() */
+EXPORT_SYMBOL_GPL(user_describe);
 
-/*****************************************************************************/
 /*
  * read the key data
  * - the key's semaphore is read-locked
  */
-static long user_read(const struct key *key,
-                     char __user *buffer, size_t buflen)
+long user_read(const struct key *key, char __user *buffer, size_t buflen)
 {
        struct user_key_payload *upayload;
        long ret;
 
-       upayload = rcu_dereference(key->payload.data);
+       upayload = rcu_dereference_key(key);
        ret = upayload->datalen;
 
        /* we can return the data as is */
@@ -224,5 +185,6 @@ static long user_read(const struct key *key,
        }
 
        return ret;
+}
 
-} /* end user_read() */
+EXPORT_SYMBOL_GPL(user_read);