request_key_auth.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /* Request key authorisation token key definition.
  3. *
  4. * Copyright (C) 2005 Red Hat, Inc. All Rights Reserved.
  5. * Written by David Howells ([email protected])
  6. *
  7. * See Documentation/security/keys/request-key.rst
  8. */
  9. #include <linux/sched.h>
  10. #include <linux/err.h>
  11. #include <linux/seq_file.h>
  12. #include <linux/slab.h>
  13. #include <linux/uaccess.h>
  14. #include "internal.h"
  15. #include <keys/request_key_auth-type.h>
  16. static int request_key_auth_preparse(struct key_preparsed_payload *);
  17. static void request_key_auth_free_preparse(struct key_preparsed_payload *);
  18. static int request_key_auth_instantiate(struct key *,
  19. struct key_preparsed_payload *);
  20. static void request_key_auth_describe(const struct key *, struct seq_file *);
  21. static void request_key_auth_revoke(struct key *);
  22. static void request_key_auth_destroy(struct key *);
  23. static long request_key_auth_read(const struct key *, char *, size_t);
  24. /*
  25. * The request-key authorisation key type definition.
  26. */
  27. struct key_type key_type_request_key_auth = {
  28. .name = ".request_key_auth",
  29. .def_datalen = sizeof(struct request_key_auth),
  30. .preparse = request_key_auth_preparse,
  31. .free_preparse = request_key_auth_free_preparse,
  32. .instantiate = request_key_auth_instantiate,
  33. .describe = request_key_auth_describe,
  34. .revoke = request_key_auth_revoke,
  35. .destroy = request_key_auth_destroy,
  36. .read = request_key_auth_read,
  37. };
  38. static int request_key_auth_preparse(struct key_preparsed_payload *prep)
  39. {
  40. return 0;
  41. }
  42. static void request_key_auth_free_preparse(struct key_preparsed_payload *prep)
  43. {
  44. }
  45. /*
  46. * Instantiate a request-key authorisation key.
  47. */
  48. static int request_key_auth_instantiate(struct key *key,
  49. struct key_preparsed_payload *prep)
  50. {
  51. rcu_assign_keypointer(key, (struct request_key_auth *)prep->data);
  52. return 0;
  53. }
  54. /*
  55. * Describe an authorisation token.
  56. */
  57. static void request_key_auth_describe(const struct key *key,
  58. struct seq_file *m)
  59. {
  60. struct request_key_auth *rka = dereference_key_rcu(key);
  61. if (!rka)
  62. return;
  63. seq_puts(m, "key:");
  64. seq_puts(m, key->description);
  65. if (key_is_positive(key))
  66. seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
  67. }
  68. /*
  69. * Read the callout_info data (retrieves the callout information).
  70. * - the key's semaphore is read-locked
  71. */
  72. static long request_key_auth_read(const struct key *key,
  73. char *buffer, size_t buflen)
  74. {
  75. struct request_key_auth *rka = dereference_key_locked(key);
  76. size_t datalen;
  77. long ret;
  78. if (!rka)
  79. return -EKEYREVOKED;
  80. datalen = rka->callout_len;
  81. ret = datalen;
  82. /* we can return the data as is */
  83. if (buffer && buflen > 0) {
  84. if (buflen > datalen)
  85. buflen = datalen;
  86. memcpy(buffer, rka->callout_info, buflen);
  87. }
  88. return ret;
  89. }
  90. static void free_request_key_auth(struct request_key_auth *rka)
  91. {
  92. if (!rka)
  93. return;
  94. key_put(rka->target_key);
  95. key_put(rka->dest_keyring);
  96. if (rka->cred)
  97. put_cred(rka->cred);
  98. kfree(rka->callout_info);
  99. kfree(rka);
  100. }
  101. /*
  102. * Dispose of the request_key_auth record under RCU conditions
  103. */
  104. static void request_key_auth_rcu_disposal(struct rcu_head *rcu)
  105. {
  106. struct request_key_auth *rka =
  107. container_of(rcu, struct request_key_auth, rcu);
  108. free_request_key_auth(rka);
  109. }
  110. /*
  111. * Handle revocation of an authorisation token key.
  112. *
  113. * Called with the key sem write-locked.
  114. */
  115. static void request_key_auth_revoke(struct key *key)
  116. {
  117. struct request_key_auth *rka = dereference_key_locked(key);
  118. kenter("{%d}", key->serial);
  119. rcu_assign_keypointer(key, NULL);
  120. call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
  121. }
  122. /*
  123. * Destroy an instantiation authorisation token key.
  124. */
  125. static void request_key_auth_destroy(struct key *key)
  126. {
  127. struct request_key_auth *rka = rcu_access_pointer(key->payload.rcu_data0);
  128. kenter("{%d}", key->serial);
  129. if (rka) {
  130. rcu_assign_keypointer(key, NULL);
  131. call_rcu(&rka->rcu, request_key_auth_rcu_disposal);
  132. }
  133. }
  134. /*
  135. * Create an authorisation token for /sbin/request-key or whoever to gain
  136. * access to the caller's security data.
  137. */
  138. struct key *request_key_auth_new(struct key *target, const char *op,
  139. const void *callout_info, size_t callout_len,
  140. struct key *dest_keyring)
  141. {
  142. struct request_key_auth *rka, *irka;
  143. const struct cred *cred = current_cred();
  144. struct key *authkey = NULL;
  145. char desc[20];
  146. int ret = -ENOMEM;
  147. kenter("%d,", target->serial);
  148. /* allocate a auth record */
  149. rka = kzalloc(sizeof(*rka), GFP_KERNEL);
  150. if (!rka)
  151. goto error;
  152. rka->callout_info = kmemdup(callout_info, callout_len, GFP_KERNEL);
  153. if (!rka->callout_info)
  154. goto error_free_rka;
  155. rka->callout_len = callout_len;
  156. strlcpy(rka->op, op, sizeof(rka->op));
  157. /* see if the calling process is already servicing the key request of
  158. * another process */
  159. if (cred->request_key_auth) {
  160. /* it is - use that instantiation context here too */
  161. down_read(&cred->request_key_auth->sem);
  162. /* if the auth key has been revoked, then the key we're
  163. * servicing is already instantiated */
  164. if (test_bit(KEY_FLAG_REVOKED,
  165. &cred->request_key_auth->flags)) {
  166. up_read(&cred->request_key_auth->sem);
  167. ret = -EKEYREVOKED;
  168. goto error_free_rka;
  169. }
  170. irka = cred->request_key_auth->payload.data[0];
  171. rka->cred = get_cred(irka->cred);
  172. rka->pid = irka->pid;
  173. up_read(&cred->request_key_auth->sem);
  174. }
  175. else {
  176. /* it isn't - use this process as the context */
  177. rka->cred = get_cred(cred);
  178. rka->pid = current->pid;
  179. }
  180. rka->target_key = key_get(target);
  181. rka->dest_keyring = key_get(dest_keyring);
  182. /* allocate the auth key */
  183. sprintf(desc, "%x", target->serial);
  184. authkey = key_alloc(&key_type_request_key_auth, desc,
  185. cred->fsuid, cred->fsgid, cred,
  186. KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH | KEY_POS_LINK |
  187. KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA, NULL);
  188. if (IS_ERR(authkey)) {
  189. ret = PTR_ERR(authkey);
  190. goto error_free_rka;
  191. }
  192. /* construct the auth key */
  193. ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
  194. if (ret < 0)
  195. goto error_put_authkey;
  196. kleave(" = {%d,%d}", authkey->serial, refcount_read(&authkey->usage));
  197. return authkey;
  198. error_put_authkey:
  199. key_put(authkey);
  200. error_free_rka:
  201. free_request_key_auth(rka);
  202. error:
  203. kleave("= %d", ret);
  204. return ERR_PTR(ret);
  205. }
  206. /*
  207. * Search the current process's keyrings for the authorisation key for
  208. * instantiation of a key.
  209. */
  210. struct key *key_get_instantiation_authkey(key_serial_t target_id)
  211. {
  212. char description[16];
  213. struct keyring_search_context ctx = {
  214. .index_key.type = &key_type_request_key_auth,
  215. .index_key.description = description,
  216. .cred = current_cred(),
  217. .match_data.cmp = key_default_cmp,
  218. .match_data.raw_data = description,
  219. .match_data.lookup_type = KEYRING_SEARCH_LOOKUP_DIRECT,
  220. .flags = (KEYRING_SEARCH_DO_STATE_CHECK |
  221. KEYRING_SEARCH_RECURSE),
  222. };
  223. struct key *authkey;
  224. key_ref_t authkey_ref;
  225. ctx.index_key.desc_len = sprintf(description, "%x", target_id);
  226. rcu_read_lock();
  227. authkey_ref = search_process_keyrings_rcu(&ctx);
  228. rcu_read_unlock();
  229. if (IS_ERR(authkey_ref)) {
  230. authkey = ERR_CAST(authkey_ref);
  231. if (authkey == ERR_PTR(-EAGAIN))
  232. authkey = ERR_PTR(-ENOKEY);
  233. goto error;
  234. }
  235. authkey = key_ref_to_ptr(authkey_ref);
  236. if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
  237. key_put(authkey);
  238. authkey = ERR_PTR(-EKEYREVOKED);
  239. }
  240. error:
  241. return authkey;
  242. }