user_defined.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* user_defined.c: user defined key type
  2. *
  3. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  4. * Written by David Howells (dhowells@redhat.com)
  5. *
  6. * This program is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU General Public License
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the License, or (at your option) any later version.
  10. */
  11. #include <linux/module.h>
  12. #include <linux/init.h>
  13. #include <linux/slab.h>
  14. #include <linux/seq_file.h>
  15. #include <linux/err.h>
  16. #include <keys/user-type.h>
  17. #include <linux/uaccess.h>
  18. #include "internal.h"
  19. static int logon_vet_description(const char *desc);
  20. /*
  21. * user defined keys take an arbitrary string as the description and an
  22. * arbitrary blob of data as the payload
  23. */
  24. struct key_type key_type_user = {
  25. .name = "user",
  26. .preparse = user_preparse,
  27. .free_preparse = user_free_preparse,
  28. .instantiate = generic_key_instantiate,
  29. .update = user_update,
  30. .revoke = user_revoke,
  31. .destroy = user_destroy,
  32. .describe = user_describe,
  33. .read = user_read,
  34. };
  35. EXPORT_SYMBOL_GPL(key_type_user);
  36. /*
  37. * This key type is essentially the same as key_type_user, but it does
  38. * not define a .read op. This is suitable for storing username and
  39. * password pairs in the keyring that you do not want to be readable
  40. * from userspace.
  41. */
  42. struct key_type key_type_logon = {
  43. .name = "logon",
  44. .preparse = user_preparse,
  45. .free_preparse = user_free_preparse,
  46. .instantiate = generic_key_instantiate,
  47. .update = user_update,
  48. .revoke = user_revoke,
  49. .destroy = user_destroy,
  50. .describe = user_describe,
  51. .vet_description = logon_vet_description,
  52. };
  53. EXPORT_SYMBOL_GPL(key_type_logon);
  54. /*
  55. * Preparse a user defined key payload
  56. */
  57. int user_preparse(struct key_preparsed_payload *prep)
  58. {
  59. struct user_key_payload *upayload;
  60. size_t datalen = prep->datalen;
  61. if (datalen <= 0 || datalen > 32767 || !prep->data)
  62. return -EINVAL;
  63. upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
  64. if (!upayload)
  65. return -ENOMEM;
  66. /* attach the data */
  67. prep->quotalen = datalen;
  68. prep->payload.data[0] = upayload;
  69. upayload->datalen = datalen;
  70. memcpy(upayload->data, prep->data, datalen);
  71. return 0;
  72. }
  73. EXPORT_SYMBOL_GPL(user_preparse);
  74. /*
  75. * Free a preparse of a user defined key payload
  76. */
  77. void user_free_preparse(struct key_preparsed_payload *prep)
  78. {
  79. kzfree(prep->payload.data[0]);
  80. }
  81. EXPORT_SYMBOL_GPL(user_free_preparse);
  82. static void user_free_payload_rcu(struct rcu_head *head)
  83. {
  84. struct user_key_payload *payload;
  85. payload = container_of(head, struct user_key_payload, rcu);
  86. kzfree(payload);
  87. }
  88. /*
  89. * update a user defined key
  90. * - the key's semaphore is write-locked
  91. */
  92. int user_update(struct key *key, struct key_preparsed_payload *prep)
  93. {
  94. struct user_key_payload *zap = NULL;
  95. int ret;
  96. /* check the quota and attach the new data */
  97. ret = key_payload_reserve(key, prep->datalen);
  98. if (ret < 0)
  99. return ret;
  100. /* attach the new data, displacing the old */
  101. key->expiry = prep->expiry;
  102. if (key_is_positive(key))
  103. zap = dereference_key_locked(key);
  104. rcu_assign_keypointer(key, prep->payload.data[0]);
  105. prep->payload.data[0] = NULL;
  106. if (zap)
  107. call_rcu(&zap->rcu, user_free_payload_rcu);
  108. return ret;
  109. }
  110. EXPORT_SYMBOL_GPL(user_update);
  111. /*
  112. * dispose of the links from a revoked keyring
  113. * - called with the key sem write-locked
  114. */
  115. void user_revoke(struct key *key)
  116. {
  117. struct user_key_payload *upayload = user_key_payload_locked(key);
  118. /* clear the quota */
  119. key_payload_reserve(key, 0);
  120. if (upayload) {
  121. rcu_assign_keypointer(key, NULL);
  122. call_rcu(&upayload->rcu, user_free_payload_rcu);
  123. }
  124. }
  125. EXPORT_SYMBOL(user_revoke);
  126. /*
  127. * dispose of the data dangling from the corpse of a user key
  128. */
  129. void user_destroy(struct key *key)
  130. {
  131. struct user_key_payload *upayload = key->payload.data[0];
  132. kzfree(upayload);
  133. }
  134. EXPORT_SYMBOL_GPL(user_destroy);
  135. /*
  136. * describe the user key
  137. */
  138. void user_describe(const struct key *key, struct seq_file *m)
  139. {
  140. seq_puts(m, key->description);
  141. if (key_is_positive(key))
  142. seq_printf(m, ": %u", key->datalen);
  143. }
  144. EXPORT_SYMBOL_GPL(user_describe);
  145. /*
  146. * read the key data
  147. * - the key's semaphore is read-locked
  148. */
  149. long user_read(const struct key *key, char __user *buffer, size_t buflen)
  150. {
  151. const struct user_key_payload *upayload;
  152. long ret;
  153. upayload = user_key_payload_locked(key);
  154. ret = upayload->datalen;
  155. /* we can return the data as is */
  156. if (buffer && buflen > 0) {
  157. if (buflen > upayload->datalen)
  158. buflen = upayload->datalen;
  159. if (copy_to_user(buffer, upayload->data, buflen) != 0)
  160. ret = -EFAULT;
  161. }
  162. return ret;
  163. }
  164. EXPORT_SYMBOL_GPL(user_read);
  165. /* Vet the description for a "logon" key */
  166. static int logon_vet_description(const char *desc)
  167. {
  168. char *p;
  169. /* require a "qualified" description string */
  170. p = strchr(desc, ':');
  171. if (!p)
  172. return -EINVAL;
  173. /* also reject description with ':' as first char */
  174. if (p == desc)
  175. return -EINVAL;
  176. return 0;
  177. }