user_defined.c 4.5 KB

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