user_defined.c 5.1 KB

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