user_defined.c 3.9 KB

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