user_defined.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  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. /*
  20. * user defined keys take an arbitrary string as the description and an
  21. * arbitrary blob of data as the payload
  22. */
  23. struct key_type key_type_user = {
  24. .name = "user",
  25. .instantiate = user_instantiate,
  26. .update = user_update,
  27. .match = user_match,
  28. .revoke = user_revoke,
  29. .destroy = user_destroy,
  30. .describe = user_describe,
  31. .read = user_read,
  32. };
  33. EXPORT_SYMBOL_GPL(key_type_user);
  34. /*****************************************************************************/
  35. /*
  36. * instantiate a user defined key
  37. */
  38. int user_instantiate(struct key *key, const void *data, size_t datalen)
  39. {
  40. struct user_key_payload *upayload;
  41. int ret;
  42. ret = -EINVAL;
  43. if (datalen <= 0 || datalen > 32767 || !data)
  44. goto error;
  45. ret = key_payload_reserve(key, datalen);
  46. if (ret < 0)
  47. goto error;
  48. ret = -ENOMEM;
  49. upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
  50. if (!upayload)
  51. goto error;
  52. /* attach the data */
  53. upayload->datalen = datalen;
  54. memcpy(upayload->data, data, datalen);
  55. rcu_assign_pointer(key->payload.data, upayload);
  56. ret = 0;
  57. error:
  58. return ret;
  59. } /* end user_instantiate() */
  60. EXPORT_SYMBOL_GPL(user_instantiate);
  61. /*****************************************************************************/
  62. /*
  63. * dispose of the old data from an updated user defined key
  64. */
  65. static void user_update_rcu_disposal(struct rcu_head *rcu)
  66. {
  67. struct user_key_payload *upayload;
  68. upayload = container_of(rcu, struct user_key_payload, rcu);
  69. kfree(upayload);
  70. } /* end user_update_rcu_disposal() */
  71. /*****************************************************************************/
  72. /*
  73. * update a user defined key
  74. * - the key's semaphore is write-locked
  75. */
  76. int user_update(struct key *key, const void *data, size_t datalen)
  77. {
  78. struct user_key_payload *upayload, *zap;
  79. int ret;
  80. ret = -EINVAL;
  81. if (datalen <= 0 || datalen > 32767 || !data)
  82. goto error;
  83. /* construct a replacement payload */
  84. ret = -ENOMEM;
  85. upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
  86. if (!upayload)
  87. goto error;
  88. upayload->datalen = datalen;
  89. memcpy(upayload->data, data, datalen);
  90. /* check the quota and attach the new data */
  91. zap = upayload;
  92. ret = key_payload_reserve(key, datalen);
  93. if (ret == 0) {
  94. /* attach the new data, displacing the old */
  95. zap = key->payload.data;
  96. rcu_assign_pointer(key->payload.data, upayload);
  97. key->expiry = 0;
  98. }
  99. call_rcu(&zap->rcu, user_update_rcu_disposal);
  100. error:
  101. return ret;
  102. } /* end user_update() */
  103. EXPORT_SYMBOL_GPL(user_update);
  104. /*****************************************************************************/
  105. /*
  106. * match users on their name
  107. */
  108. int user_match(const struct key *key, const void *description)
  109. {
  110. return strcmp(key->description, description) == 0;
  111. } /* end user_match() */
  112. EXPORT_SYMBOL_GPL(user_match);
  113. /*****************************************************************************/
  114. /*
  115. * dispose of the links from a revoked keyring
  116. * - called with the key sem write-locked
  117. */
  118. void user_revoke(struct key *key)
  119. {
  120. struct user_key_payload *upayload = key->payload.data;
  121. /* clear the quota */
  122. key_payload_reserve(key, 0);
  123. if (upayload) {
  124. rcu_assign_pointer(key->payload.data, NULL);
  125. call_rcu(&upayload->rcu, user_update_rcu_disposal);
  126. }
  127. } /* end user_revoke() */
  128. EXPORT_SYMBOL(user_revoke);
  129. /*****************************************************************************/
  130. /*
  131. * dispose of the data dangling from the corpse of a user key
  132. */
  133. void user_destroy(struct key *key)
  134. {
  135. struct user_key_payload *upayload = key->payload.data;
  136. kfree(upayload);
  137. } /* end user_destroy() */
  138. EXPORT_SYMBOL_GPL(user_destroy);
  139. /*****************************************************************************/
  140. /*
  141. * describe the user key
  142. */
  143. void user_describe(const struct key *key, struct seq_file *m)
  144. {
  145. seq_puts(m, key->description);
  146. seq_printf(m, ": %u", key->datalen);
  147. } /* end user_describe() */
  148. EXPORT_SYMBOL_GPL(user_describe);
  149. /*****************************************************************************/
  150. /*
  151. * read the key data
  152. * - the key's semaphore is read-locked
  153. */
  154. long user_read(const struct key *key, char __user *buffer, size_t buflen)
  155. {
  156. struct user_key_payload *upayload;
  157. long ret;
  158. upayload = rcu_dereference(key->payload.data);
  159. ret = upayload->datalen;
  160. /* we can return the data as is */
  161. if (buffer && buflen > 0) {
  162. if (buflen > upayload->datalen)
  163. buflen = upayload->datalen;
  164. if (copy_to_user(buffer, upayload->data, buflen) != 0)
  165. ret = -EFAULT;
  166. }
  167. return ret;
  168. } /* end user_read() */
  169. EXPORT_SYMBOL_GPL(user_read);