user_defined.c 5.8 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/sched.h>
  14. #include <linux/slab.h>
  15. #include <linux/seq_file.h>
  16. #include <linux/err.h>
  17. #include <asm/uaccess.h>
  18. #include "internal.h"
  19. static int user_instantiate(struct key *key, const void *data, size_t datalen);
  20. static int user_duplicate(struct key *key, const struct key *source);
  21. static int user_update(struct key *key, const void *data, size_t datalen);
  22. static int user_match(const struct key *key, const void *criterion);
  23. static void user_destroy(struct key *key);
  24. static void user_describe(const struct key *user, struct seq_file *m);
  25. static long user_read(const struct key *key,
  26. char __user *buffer, size_t buflen);
  27. /*
  28. * user defined keys take an arbitrary string as the description and an
  29. * arbitrary blob of data as the payload
  30. */
  31. struct key_type key_type_user = {
  32. .name = "user",
  33. .instantiate = user_instantiate,
  34. .duplicate = user_duplicate,
  35. .update = user_update,
  36. .match = user_match,
  37. .destroy = user_destroy,
  38. .describe = user_describe,
  39. .read = user_read,
  40. };
  41. struct user_key_payload {
  42. struct rcu_head rcu; /* RCU destructor */
  43. unsigned short datalen; /* length of this data */
  44. char data[0]; /* actual data */
  45. };
  46. EXPORT_SYMBOL_GPL(key_type_user);
  47. /*****************************************************************************/
  48. /*
  49. * instantiate a user defined key
  50. */
  51. static int user_instantiate(struct key *key, const void *data, size_t datalen)
  52. {
  53. struct user_key_payload *upayload;
  54. int ret;
  55. ret = -EINVAL;
  56. if (datalen <= 0 || datalen > 32767 || !data)
  57. goto error;
  58. ret = key_payload_reserve(key, datalen);
  59. if (ret < 0)
  60. goto error;
  61. ret = -ENOMEM;
  62. upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
  63. if (!upayload)
  64. goto error;
  65. /* attach the data */
  66. upayload->datalen = datalen;
  67. memcpy(upayload->data, data, datalen);
  68. rcu_assign_pointer(key->payload.data, upayload);
  69. ret = 0;
  70. error:
  71. return ret;
  72. } /* end user_instantiate() */
  73. /*****************************************************************************/
  74. /*
  75. * duplicate a user defined key
  76. * - both keys' semaphores are locked against further modification
  77. * - the new key cannot yet be accessed
  78. */
  79. static int user_duplicate(struct key *key, const struct key *source)
  80. {
  81. struct user_key_payload *upayload, *spayload;
  82. int ret;
  83. /* just copy the payload */
  84. ret = -ENOMEM;
  85. upayload = kmalloc(sizeof(*upayload) + source->datalen, GFP_KERNEL);
  86. if (upayload) {
  87. spayload = rcu_dereference(source->payload.data);
  88. BUG_ON(source->datalen != spayload->datalen);
  89. upayload->datalen = key->datalen = spayload->datalen;
  90. memcpy(upayload->data, spayload->data, key->datalen);
  91. key->payload.data = upayload;
  92. ret = 0;
  93. }
  94. return ret;
  95. } /* end user_duplicate() */
  96. /*****************************************************************************/
  97. /*
  98. * dispose of the old data from an updated user defined key
  99. */
  100. static void user_update_rcu_disposal(struct rcu_head *rcu)
  101. {
  102. struct user_key_payload *upayload;
  103. upayload = container_of(rcu, struct user_key_payload, rcu);
  104. kfree(upayload);
  105. } /* end user_update_rcu_disposal() */
  106. /*****************************************************************************/
  107. /*
  108. * update a user defined key
  109. * - the key's semaphore is write-locked
  110. */
  111. static int user_update(struct key *key, const void *data, size_t datalen)
  112. {
  113. struct user_key_payload *upayload, *zap;
  114. int ret;
  115. ret = -EINVAL;
  116. if (datalen <= 0 || datalen > 32767 || !data)
  117. goto error;
  118. /* construct a replacement payload */
  119. ret = -ENOMEM;
  120. upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
  121. if (!upayload)
  122. goto error;
  123. upayload->datalen = datalen;
  124. memcpy(upayload->data, data, datalen);
  125. /* check the quota and attach the new data */
  126. zap = upayload;
  127. ret = key_payload_reserve(key, datalen);
  128. if (ret == 0) {
  129. /* attach the new data, displacing the old */
  130. zap = key->payload.data;
  131. rcu_assign_pointer(key->payload.data, upayload);
  132. key->expiry = 0;
  133. }
  134. call_rcu(&zap->rcu, user_update_rcu_disposal);
  135. error:
  136. return ret;
  137. } /* end user_update() */
  138. /*****************************************************************************/
  139. /*
  140. * match users on their name
  141. */
  142. static int user_match(const struct key *key, const void *description)
  143. {
  144. return strcmp(key->description, description) == 0;
  145. } /* end user_match() */
  146. /*****************************************************************************/
  147. /*
  148. * dispose of the data dangling from the corpse of a user
  149. */
  150. static void user_destroy(struct key *key)
  151. {
  152. struct user_key_payload *upayload = key->payload.data;
  153. kfree(upayload);
  154. } /* end user_destroy() */
  155. /*****************************************************************************/
  156. /*
  157. * describe the user key
  158. */
  159. static void user_describe(const struct key *key, struct seq_file *m)
  160. {
  161. seq_puts(m, key->description);
  162. seq_printf(m, ": %u", key->datalen);
  163. } /* end user_describe() */
  164. /*****************************************************************************/
  165. /*
  166. * read the key data
  167. * - the key's semaphore is read-locked
  168. */
  169. static long user_read(const struct key *key,
  170. char __user *buffer, size_t buflen)
  171. {
  172. struct user_key_payload *upayload;
  173. long ret;
  174. upayload = rcu_dereference(key->payload.data);
  175. ret = upayload->datalen;
  176. /* we can return the data as is */
  177. if (buffer && buflen > 0) {
  178. if (buflen > upayload->datalen)
  179. buflen = upayload->datalen;
  180. if (copy_to_user(buffer, upayload->data, buflen) != 0)
  181. ret = -EFAULT;
  182. }
  183. return ret;
  184. } /* end user_read() */