user_defined.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  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 <asm/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. .def_lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
  27. .preparse = user_preparse,
  28. .free_preparse = user_free_preparse,
  29. .instantiate = generic_key_instantiate,
  30. .update = user_update,
  31. .match = user_match,
  32. .revoke = user_revoke,
  33. .destroy = user_destroy,
  34. .describe = user_describe,
  35. .read = user_read,
  36. };
  37. EXPORT_SYMBOL_GPL(key_type_user);
  38. /*
  39. * This key type is essentially the same as key_type_user, but it does
  40. * not define a .read op. This is suitable for storing username and
  41. * password pairs in the keyring that you do not want to be readable
  42. * from userspace.
  43. */
  44. struct key_type key_type_logon = {
  45. .name = "logon",
  46. .def_lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
  47. .preparse = user_preparse,
  48. .free_preparse = user_free_preparse,
  49. .instantiate = generic_key_instantiate,
  50. .update = user_update,
  51. .match = user_match,
  52. .revoke = user_revoke,
  53. .destroy = user_destroy,
  54. .describe = user_describe,
  55. .vet_description = logon_vet_description,
  56. };
  57. EXPORT_SYMBOL_GPL(key_type_logon);
  58. /*
  59. * Preparse a user defined key payload
  60. */
  61. int user_preparse(struct key_preparsed_payload *prep)
  62. {
  63. struct user_key_payload *upayload;
  64. size_t datalen = prep->datalen;
  65. if (datalen <= 0 || datalen > 32767 || !prep->data)
  66. return -EINVAL;
  67. upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
  68. if (!upayload)
  69. return -ENOMEM;
  70. /* attach the data */
  71. prep->quotalen = datalen;
  72. prep->payload[0] = upayload;
  73. upayload->datalen = datalen;
  74. memcpy(upayload->data, prep->data, datalen);
  75. return 0;
  76. }
  77. EXPORT_SYMBOL_GPL(user_preparse);
  78. /*
  79. * Free a preparse of a user defined key payload
  80. */
  81. void user_free_preparse(struct key_preparsed_payload *prep)
  82. {
  83. kfree(prep->payload[0]);
  84. }
  85. EXPORT_SYMBOL_GPL(user_free_preparse);
  86. /*
  87. * update a user defined key
  88. * - the key's semaphore is write-locked
  89. */
  90. int user_update(struct key *key, struct key_preparsed_payload *prep)
  91. {
  92. struct user_key_payload *upayload, *zap;
  93. size_t datalen = prep->datalen;
  94. int ret;
  95. ret = -EINVAL;
  96. if (datalen <= 0 || datalen > 32767 || !prep->data)
  97. goto error;
  98. /* construct a replacement payload */
  99. ret = -ENOMEM;
  100. upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
  101. if (!upayload)
  102. goto error;
  103. upayload->datalen = datalen;
  104. memcpy(upayload->data, prep->data, datalen);
  105. /* check the quota and attach the new data */
  106. zap = upayload;
  107. ret = key_payload_reserve(key, datalen);
  108. if (ret == 0) {
  109. /* attach the new data, displacing the old */
  110. zap = key->payload.data;
  111. rcu_assign_keypointer(key, upayload);
  112. key->expiry = 0;
  113. }
  114. if (zap)
  115. kfree_rcu(zap, rcu);
  116. error:
  117. return ret;
  118. }
  119. EXPORT_SYMBOL_GPL(user_update);
  120. /*
  121. * match users on their name
  122. */
  123. int user_match(const struct key *key, const void *description)
  124. {
  125. return strcmp(key->description, description) == 0;
  126. }
  127. EXPORT_SYMBOL_GPL(user_match);
  128. /*
  129. * dispose of the links from a revoked keyring
  130. * - called with the key sem write-locked
  131. */
  132. void user_revoke(struct key *key)
  133. {
  134. struct user_key_payload *upayload = key->payload.data;
  135. /* clear the quota */
  136. key_payload_reserve(key, 0);
  137. if (upayload) {
  138. rcu_assign_keypointer(key, NULL);
  139. kfree_rcu(upayload, rcu);
  140. }
  141. }
  142. EXPORT_SYMBOL(user_revoke);
  143. /*
  144. * dispose of the data dangling from the corpse of a user key
  145. */
  146. void user_destroy(struct key *key)
  147. {
  148. struct user_key_payload *upayload = key->payload.data;
  149. kfree(upayload);
  150. }
  151. EXPORT_SYMBOL_GPL(user_destroy);
  152. /*
  153. * describe the user key
  154. */
  155. void user_describe(const struct key *key, struct seq_file *m)
  156. {
  157. seq_puts(m, key->description);
  158. if (key_is_instantiated(key))
  159. seq_printf(m, ": %u", key->datalen);
  160. }
  161. EXPORT_SYMBOL_GPL(user_describe);
  162. /*
  163. * read the key data
  164. * - the key's semaphore is read-locked
  165. */
  166. long user_read(const struct key *key, char __user *buffer, size_t buflen)
  167. {
  168. struct user_key_payload *upayload;
  169. long ret;
  170. upayload = rcu_dereference_key(key);
  171. ret = upayload->datalen;
  172. /* we can return the data as is */
  173. if (buffer && buflen > 0) {
  174. if (buflen > upayload->datalen)
  175. buflen = upayload->datalen;
  176. if (copy_to_user(buffer, upayload->data, buflen) != 0)
  177. ret = -EFAULT;
  178. }
  179. return ret;
  180. }
  181. EXPORT_SYMBOL_GPL(user_read);
  182. /* Vet the description for a "logon" key */
  183. static int logon_vet_description(const char *desc)
  184. {
  185. char *p;
  186. /* require a "qualified" description string */
  187. p = strchr(desc, ':');
  188. if (!p)
  189. return -EINVAL;
  190. /* also reject description with ':' as first char */
  191. if (p == desc)
  192. return -EINVAL;
  193. return 0;
  194. }