KEYS: Provide a generic instantiation function
authorDavid Howells <dhowells@redhat.com>
Fri, 18 Jul 2014 17:56:34 +0000 (18:56 +0100)
committerDavid Howells <dhowells@redhat.com>
Fri, 18 Jul 2014 17:56:34 +0000 (18:56 +0100)
Provide a generic instantiation function for key types that use the preparse
hook.  This makes it easier to prereserve key quota before keyrings get locked
to retain the new key.

Signed-off-by: David Howells <dhowells@redhat.com>
Acked-by: Steve Dickson <steved@redhat.com>
Acked-by: Jeff Layton <jlayton@primarydata.com>
Reviewed-by: Sage Weil <sage@redhat.com>
crypto/asymmetric_keys/asymmetric_type.c
include/linux/key-type.h
security/keys/key.c

index b77eb53047882ad26de8de4892e8568a4b792326..c1fe0fcee8e352bf9e94ad2f10808df07b1dc4c8 100644 (file)
@@ -163,29 +163,6 @@ static void asymmetric_key_free_preparse(struct key_preparsed_payload *prep)
        kfree(prep->description);
 }
 
-/*
- * Instantiate a asymmetric_key defined key.  The key was preparsed, so we just
- * have to transfer the data here.
- */
-static int asymmetric_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
-{
-       int ret;
-
-       pr_devel("==>%s()\n", __func__);
-
-       ret = key_payload_reserve(key, prep->quotalen);
-       if (ret == 0) {
-               key->type_data.p[0] = prep->type_data[0];
-               key->type_data.p[1] = prep->type_data[1];
-               key->payload.data = prep->payload;
-               prep->type_data[0] = NULL;
-               prep->type_data[1] = NULL;
-               prep->payload = NULL;
-       }
-       pr_devel("<==%s() = %d\n", __func__, ret);
-       return ret;
-}
-
 /*
  * dispose of the data dangling from the corpse of a asymmetric key
  */
@@ -205,7 +182,7 @@ struct key_type key_type_asymmetric = {
        .name           = "asymmetric",
        .preparse       = asymmetric_key_preparse,
        .free_preparse  = asymmetric_key_free_preparse,
-       .instantiate    = asymmetric_key_instantiate,
+       .instantiate    = generic_key_instantiate,
        .match          = asymmetric_key_match,
        .destroy        = asymmetric_key_destroy,
        .describe       = asymmetric_key_describe,
index a74c3a84dfdd05223cc0613c9445bdc396e1afe9..88503dca2a572d39591ffecb1fc9384bd14cf138 100644 (file)
@@ -159,5 +159,7 @@ static inline int key_negate_and_link(struct key *key,
        return key_reject_and_link(key, timeout, ENOKEY, keyring, instkey);
 }
 
+extern int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep);
+
 #endif /* CONFIG_KEYS */
 #endif /* _LINUX_KEY_TYPE_H */
index 2048a110e7f18f0bd1e78006a88dc7bc675c9994..7c9acbf106b6fc3a8192def4e753e0bd1ce278ad 100644 (file)
@@ -1023,6 +1023,36 @@ void key_invalidate(struct key *key)
 }
 EXPORT_SYMBOL(key_invalidate);
 
+/**
+ * generic_key_instantiate - Simple instantiation of a key from preparsed data
+ * @key: The key to be instantiated
+ * @prep: The preparsed data to load.
+ *
+ * Instantiate a key from preparsed data.  We assume we can just copy the data
+ * in directly and clear the old pointers.
+ *
+ * This can be pointed to directly by the key type instantiate op pointer.
+ */
+int generic_key_instantiate(struct key *key, struct key_preparsed_payload *prep)
+{
+       int ret;
+
+       pr_devel("==>%s()\n", __func__);
+
+       ret = key_payload_reserve(key, prep->quotalen);
+       if (ret == 0) {
+               key->type_data.p[0] = prep->type_data[0];
+               key->type_data.p[1] = prep->type_data[1];
+               rcu_assign_keypointer(key, prep->payload);
+               prep->type_data[0] = NULL;
+               prep->type_data[1] = NULL;
+               prep->payload = NULL;
+       }
+       pr_devel("<==%s() = %d\n", __func__, ret);
+       return ret;
+}
+EXPORT_SYMBOL(generic_key_instantiate);
+
 /**
  * register_key_type - Register a type of key.
  * @ktype: The new key type.