user_defined.c 4.9 KB

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