user_defined.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* user_defined.c: user defined key type
  3. *
  4. * Copyright (C) 2004 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells (dhowells@redhat.com)
  6. */
  7. #include <linux/export.h>
  8. #include <linux/init.h>
  9. #include <linux/slab.h>
  10. #include <linux/seq_file.h>
  11. #include <linux/err.h>
  12. #include <keys/user-type.h>
  13. #include <linux/uaccess.h>
  14. #include "internal.h"
  15. static int logon_vet_description(const char *desc);
  16. /*
  17. * user defined keys take an arbitrary string as the description and an
  18. * arbitrary blob of data as the payload
  19. */
  20. struct key_type key_type_user = {
  21. .name = "user",
  22. .preparse = user_preparse,
  23. .free_preparse = user_free_preparse,
  24. .instantiate = generic_key_instantiate,
  25. .update = user_update,
  26. .revoke = user_revoke,
  27. .destroy = user_destroy,
  28. .describe = user_describe,
  29. .read = user_read,
  30. };
  31. EXPORT_SYMBOL_GPL(key_type_user);
  32. /*
  33. * This key type is essentially the same as key_type_user, but it does
  34. * not define a .read op. This is suitable for storing username and
  35. * password pairs in the keyring that you do not want to be readable
  36. * from userspace.
  37. */
  38. struct key_type key_type_logon = {
  39. .name = "logon",
  40. .preparse = user_preparse,
  41. .free_preparse = user_free_preparse,
  42. .instantiate = generic_key_instantiate,
  43. .update = user_update,
  44. .revoke = user_revoke,
  45. .destroy = user_destroy,
  46. .describe = user_describe,
  47. .vet_description = logon_vet_description,
  48. };
  49. EXPORT_SYMBOL_GPL(key_type_logon);
  50. /*
  51. * Preparse a user defined key payload
  52. */
  53. int user_preparse(struct key_preparsed_payload *prep)
  54. {
  55. struct user_key_payload *upayload;
  56. size_t datalen = prep->datalen;
  57. if (datalen <= 0 || datalen > 32767 || !prep->data)
  58. return -EINVAL;
  59. upayload = kmalloc(sizeof(*upayload) + datalen, GFP_KERNEL);
  60. if (!upayload)
  61. return -ENOMEM;
  62. /* attach the data */
  63. prep->quotalen = datalen;
  64. prep->payload.data[0] = upayload;
  65. upayload->datalen = datalen;
  66. memcpy(upayload->data, prep->data, datalen);
  67. return 0;
  68. }
  69. EXPORT_SYMBOL_GPL(user_preparse);
  70. /*
  71. * Free a preparse of a user defined key payload
  72. */
  73. void user_free_preparse(struct key_preparsed_payload *prep)
  74. {
  75. kfree_sensitive(prep->payload.data[0]);
  76. }
  77. EXPORT_SYMBOL_GPL(user_free_preparse);
  78. static void user_free_payload_rcu(struct rcu_head *head)
  79. {
  80. struct user_key_payload *payload;
  81. payload = container_of(head, struct user_key_payload, rcu);
  82. kfree_sensitive(payload);
  83. }
  84. /*
  85. * update a user defined key
  86. * - the key's semaphore is write-locked
  87. */
  88. int user_update(struct key *key, struct key_preparsed_payload *prep)
  89. {
  90. struct user_key_payload *zap = NULL;
  91. int ret;
  92. /* check the quota and attach the new data */
  93. ret = key_payload_reserve(key, prep->datalen);
  94. if (ret < 0)
  95. return ret;
  96. /* attach the new data, displacing the old */
  97. key->expiry = prep->expiry;
  98. if (key_is_positive(key))
  99. zap = dereference_key_locked(key);
  100. rcu_assign_keypointer(key, prep->payload.data[0]);
  101. prep->payload.data[0] = NULL;
  102. if (zap)
  103. call_rcu(&zap->rcu, user_free_payload_rcu);
  104. return ret;
  105. }
  106. EXPORT_SYMBOL_GPL(user_update);
  107. /*
  108. * dispose of the links from a revoked keyring
  109. * - called with the key sem write-locked
  110. */
  111. void user_revoke(struct key *key)
  112. {
  113. struct user_key_payload *upayload = user_key_payload_locked(key);
  114. /* clear the quota */
  115. key_payload_reserve(key, 0);
  116. if (upayload) {
  117. rcu_assign_keypointer(key, NULL);
  118. call_rcu(&upayload->rcu, user_free_payload_rcu);
  119. }
  120. }
  121. EXPORT_SYMBOL(user_revoke);
  122. /*
  123. * dispose of the data dangling from the corpse of a user key
  124. */
  125. void user_destroy(struct key *key)
  126. {
  127. struct user_key_payload *upayload = key->payload.data[0];
  128. kfree_sensitive(upayload);
  129. }
  130. EXPORT_SYMBOL_GPL(user_destroy);
  131. /*
  132. * describe the user key
  133. */
  134. void user_describe(const struct key *key, struct seq_file *m)
  135. {
  136. seq_puts(m, key->description);
  137. if (key_is_positive(key))
  138. seq_printf(m, ": %u", key->datalen);
  139. }
  140. EXPORT_SYMBOL_GPL(user_describe);
  141. /*
  142. * read the key data
  143. * - the key's semaphore is read-locked
  144. */
  145. long user_read(const struct key *key, char *buffer, size_t buflen)
  146. {
  147. const struct user_key_payload *upayload;
  148. long ret;
  149. upayload = user_key_payload_locked(key);
  150. ret = upayload->datalen;
  151. /* we can return the data as is */
  152. if (buffer && buflen > 0) {
  153. if (buflen > upayload->datalen)
  154. buflen = upayload->datalen;
  155. memcpy(buffer, upayload->data, buflen);
  156. }
  157. return ret;
  158. }
  159. EXPORT_SYMBOL_GPL(user_read);
  160. /* Vet the description for a "logon" key */
  161. static int logon_vet_description(const char *desc)
  162. {
  163. char *p;
  164. /* require a "qualified" description string */
  165. p = strchr(desc, ':');
  166. if (!p)
  167. return -EINVAL;
  168. /* also reject description with ':' as first char */
  169. if (p == desc)
  170. return -EINVAL;
  171. return 0;
  172. }