user_defined.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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. .instantiate = user_instantiate,
  28. .update = user_update,
  29. .match = user_match,
  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. .def_lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
  45. .instantiate = user_instantiate,
  46. .update = user_update,
  47. .match = user_match,
  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. * instantiate a user defined key
  56. */
  57. int user_instantiate(struct key *key, struct key_preparsed_payload *prep)
  58. {
  59. struct user_key_payload *upayload;
  60. size_t datalen = prep->datalen;
  61. int ret;
  62. ret = -EINVAL;
  63. if (datalen <= 0 || datalen > 32767 || !prep->data)
  64. goto error;
  65. ret = key_payload_reserve(key, datalen);
  66. if (ret < 0)
  67. goto error;
  68. ret = -ENOMEM;
  69. upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
  70. if (!upayload)
  71. goto error;
  72. /* attach the data */
  73. upayload->datalen = datalen;
  74. memcpy(upayload->data, prep->data, datalen);
  75. rcu_assign_keypointer(key, upayload);
  76. ret = 0;
  77. error:
  78. return ret;
  79. }
  80. EXPORT_SYMBOL_GPL(user_instantiate);
  81. /*
  82. * update a user defined key
  83. * - the key's semaphore is write-locked
  84. */
  85. int user_update(struct key *key, struct key_preparsed_payload *prep)
  86. {
  87. struct user_key_payload *upayload, *zap;
  88. size_t datalen = prep->datalen;
  89. int ret;
  90. ret = -EINVAL;
  91. if (datalen <= 0 || datalen > 32767 || !prep->data)
  92. goto error;
  93. /* construct a replacement payload */
  94. ret = -ENOMEM;
  95. upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
  96. if (!upayload)
  97. goto error;
  98. upayload->datalen = datalen;
  99. memcpy(upayload->data, prep->data, datalen);
  100. /* check the quota and attach the new data */
  101. zap = upayload;
  102. ret = key_payload_reserve(key, datalen);
  103. if (ret == 0) {
  104. /* attach the new data, displacing the old */
  105. zap = key->payload.data;
  106. rcu_assign_keypointer(key, upayload);
  107. key->expiry = 0;
  108. }
  109. if (zap)
  110. kfree_rcu(zap, rcu);
  111. error:
  112. return ret;
  113. }
  114. EXPORT_SYMBOL_GPL(user_update);
  115. /*
  116. * match users on their name
  117. */
  118. int user_match(const struct key *key, const void *description)
  119. {
  120. return strcmp(key->description, description) == 0;
  121. }
  122. EXPORT_SYMBOL_GPL(user_match);
  123. /*
  124. * dispose of the links from a revoked keyring
  125. * - called with the key sem write-locked
  126. */
  127. void user_revoke(struct key *key)
  128. {
  129. struct user_key_payload *upayload = key->payload.data;
  130. /* clear the quota */
  131. key_payload_reserve(key, 0);
  132. if (upayload) {
  133. rcu_assign_keypointer(key, NULL);
  134. kfree_rcu(upayload, rcu);
  135. }
  136. }
  137. EXPORT_SYMBOL(user_revoke);
  138. /*
  139. * dispose of the data dangling from the corpse of a user key
  140. */
  141. void user_destroy(struct key *key)
  142. {
  143. struct user_key_payload *upayload = key->payload.data;
  144. kfree(upayload);
  145. }
  146. EXPORT_SYMBOL_GPL(user_destroy);
  147. /*
  148. * describe the user key
  149. */
  150. void user_describe(const struct key *key, struct seq_file *m)
  151. {
  152. seq_puts(m, key->description);
  153. if (key_is_instantiated(key))
  154. seq_printf(m, ": %u", key->datalen);
  155. }
  156. EXPORT_SYMBOL_GPL(user_describe);
  157. /*
  158. * read the key data
  159. * - the key's semaphore is read-locked
  160. */
  161. long user_read(const struct key *key, char __user *buffer, size_t buflen)
  162. {
  163. struct user_key_payload *upayload;
  164. long ret;
  165. upayload = rcu_dereference_key(key);
  166. ret = upayload->datalen;
  167. /* we can return the data as is */
  168. if (buffer && buflen > 0) {
  169. if (buflen > upayload->datalen)
  170. buflen = upayload->datalen;
  171. if (copy_to_user(buffer, upayload->data, buflen) != 0)
  172. ret = -EFAULT;
  173. }
  174. return ret;
  175. }
  176. EXPORT_SYMBOL_GPL(user_read);
  177. /* Vet the description for a "logon" key */
  178. static int logon_vet_description(const char *desc)
  179. {
  180. char *p;
  181. /* require a "qualified" description string */
  182. p = strchr(desc, ':');
  183. if (!p)
  184. return -EINVAL;
  185. /* also reject description with ':' as first char */
  186. if (p == desc)
  187. return -EINVAL;
  188. return 0;
  189. }