user_defined.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  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. kfree_rcu(zap, rcu);
  88. error:
  89. return ret;
  90. }
  91. EXPORT_SYMBOL_GPL(user_update);
  92. /*
  93. * match users on their name
  94. */
  95. int user_match(const struct key *key, const void *description)
  96. {
  97. return strcmp(key->description, description) == 0;
  98. }
  99. EXPORT_SYMBOL_GPL(user_match);
  100. /*
  101. * dispose of the links from a revoked keyring
  102. * - called with the key sem write-locked
  103. */
  104. void user_revoke(struct key *key)
  105. {
  106. struct user_key_payload *upayload = key->payload.data;
  107. /* clear the quota */
  108. key_payload_reserve(key, 0);
  109. if (upayload) {
  110. rcu_assign_pointer(key->payload.data, NULL);
  111. kfree_rcu(upayload, rcu);
  112. }
  113. }
  114. EXPORT_SYMBOL(user_revoke);
  115. /*
  116. * dispose of the data dangling from the corpse of a user key
  117. */
  118. void user_destroy(struct key *key)
  119. {
  120. struct user_key_payload *upayload = key->payload.data;
  121. kfree(upayload);
  122. }
  123. EXPORT_SYMBOL_GPL(user_destroy);
  124. /*
  125. * describe the user key
  126. */
  127. void user_describe(const struct key *key, struct seq_file *m)
  128. {
  129. seq_puts(m, key->description);
  130. if (key_is_instantiated(key))
  131. seq_printf(m, ": %u", key->datalen);
  132. }
  133. EXPORT_SYMBOL_GPL(user_describe);
  134. /*
  135. * read the key data
  136. * - the key's semaphore is read-locked
  137. */
  138. long user_read(const struct key *key, char __user *buffer, size_t buflen)
  139. {
  140. struct user_key_payload *upayload;
  141. long ret;
  142. upayload = rcu_dereference_key(key);
  143. ret = upayload->datalen;
  144. /* we can return the data as is */
  145. if (buffer && buflen > 0) {
  146. if (buflen > upayload->datalen)
  147. buflen = upayload->datalen;
  148. if (copy_to_user(buffer, upayload->data, buflen) != 0)
  149. ret = -EFAULT;
  150. }
  151. return ret;
  152. }
  153. EXPORT_SYMBOL_GPL(user_read);