PageRenderTime 57ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/kernel-source/security/keys/request_key_auth.c

https://gitlab.com/karrei/inel-imx6-kernel
C | 262 lines | 173 code | 38 blank | 51 comment | 19 complexity | a61f07e376f3ac99d424c9194b98e558 MD5 | raw file
  1. /* Request key authorisation token key definition.
  2. *
  3. * Copyright (C) 2005 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. * See Documentation/security/keys-request-key.txt
  12. */
  13. #include <linux/module.h>
  14. #include <linux/sched.h>
  15. #include <linux/err.h>
  16. #include <linux/seq_file.h>
  17. #include <linux/slab.h>
  18. #include <asm/uaccess.h>
  19. #include "internal.h"
  20. #include <keys/user-type.h>
  21. static int request_key_auth_instantiate(struct key *,
  22. struct key_preparsed_payload *);
  23. static void request_key_auth_describe(const struct key *, struct seq_file *);
  24. static void request_key_auth_revoke(struct key *);
  25. static void request_key_auth_destroy(struct key *);
  26. static long request_key_auth_read(const struct key *, char __user *, size_t);
  27. /*
  28. * The request-key authorisation key type definition.
  29. */
  30. struct key_type key_type_request_key_auth = {
  31. .name = ".request_key_auth",
  32. .def_datalen = sizeof(struct request_key_auth),
  33. .instantiate = request_key_auth_instantiate,
  34. .describe = request_key_auth_describe,
  35. .revoke = request_key_auth_revoke,
  36. .destroy = request_key_auth_destroy,
  37. .read = request_key_auth_read,
  38. };
  39. /*
  40. * Instantiate a request-key authorisation key.
  41. */
  42. static int request_key_auth_instantiate(struct key *key,
  43. struct key_preparsed_payload *prep)
  44. {
  45. key->payload.data = (struct request_key_auth *)prep->data;
  46. return 0;
  47. }
  48. /*
  49. * Describe an authorisation token.
  50. */
  51. static void request_key_auth_describe(const struct key *key,
  52. struct seq_file *m)
  53. {
  54. struct request_key_auth *rka = key->payload.data;
  55. seq_puts(m, "key:");
  56. seq_puts(m, key->description);
  57. if (key_is_instantiated(key))
  58. seq_printf(m, " pid:%d ci:%zu", rka->pid, rka->callout_len);
  59. }
  60. /*
  61. * Read the callout_info data (retrieves the callout information).
  62. * - the key's semaphore is read-locked
  63. */
  64. static long request_key_auth_read(const struct key *key,
  65. char __user *buffer, size_t buflen)
  66. {
  67. struct request_key_auth *rka = key->payload.data;
  68. size_t datalen;
  69. long ret;
  70. datalen = rka->callout_len;
  71. ret = datalen;
  72. /* we can return the data as is */
  73. if (buffer && buflen > 0) {
  74. if (buflen > datalen)
  75. buflen = datalen;
  76. if (copy_to_user(buffer, rka->callout_info, buflen) != 0)
  77. ret = -EFAULT;
  78. }
  79. return ret;
  80. }
  81. /*
  82. * Handle revocation of an authorisation token key.
  83. *
  84. * Called with the key sem write-locked.
  85. */
  86. static void request_key_auth_revoke(struct key *key)
  87. {
  88. struct request_key_auth *rka = key->payload.data;
  89. kenter("{%d}", key->serial);
  90. if (rka->cred) {
  91. put_cred(rka->cred);
  92. rka->cred = NULL;
  93. }
  94. }
  95. /*
  96. * Destroy an instantiation authorisation token key.
  97. */
  98. static void request_key_auth_destroy(struct key *key)
  99. {
  100. struct request_key_auth *rka = key->payload.data;
  101. kenter("{%d}", key->serial);
  102. if (rka->cred) {
  103. put_cred(rka->cred);
  104. rka->cred = NULL;
  105. }
  106. key_put(rka->target_key);
  107. key_put(rka->dest_keyring);
  108. kfree(rka->callout_info);
  109. kfree(rka);
  110. }
  111. /*
  112. * Create an authorisation token for /sbin/request-key or whoever to gain
  113. * access to the caller's security data.
  114. */
  115. struct key *request_key_auth_new(struct key *target, const void *callout_info,
  116. size_t callout_len, struct key *dest_keyring)
  117. {
  118. struct request_key_auth *rka, *irka;
  119. const struct cred *cred = current->cred;
  120. struct key *authkey = NULL;
  121. char desc[20];
  122. int ret;
  123. kenter("%d,", target->serial);
  124. /* allocate a auth record */
  125. rka = kmalloc(sizeof(*rka), GFP_KERNEL);
  126. if (!rka) {
  127. kleave(" = -ENOMEM");
  128. return ERR_PTR(-ENOMEM);
  129. }
  130. rka->callout_info = kmalloc(callout_len, GFP_KERNEL);
  131. if (!rka->callout_info) {
  132. kleave(" = -ENOMEM");
  133. kfree(rka);
  134. return ERR_PTR(-ENOMEM);
  135. }
  136. /* see if the calling process is already servicing the key request of
  137. * another process */
  138. if (cred->request_key_auth) {
  139. /* it is - use that instantiation context here too */
  140. down_read(&cred->request_key_auth->sem);
  141. /* if the auth key has been revoked, then the key we're
  142. * servicing is already instantiated */
  143. if (test_bit(KEY_FLAG_REVOKED, &cred->request_key_auth->flags))
  144. goto auth_key_revoked;
  145. irka = cred->request_key_auth->payload.data;
  146. rka->cred = get_cred(irka->cred);
  147. rka->pid = irka->pid;
  148. up_read(&cred->request_key_auth->sem);
  149. }
  150. else {
  151. /* it isn't - use this process as the context */
  152. rka->cred = get_cred(cred);
  153. rka->pid = current->pid;
  154. }
  155. rka->target_key = key_get(target);
  156. rka->dest_keyring = key_get(dest_keyring);
  157. memcpy(rka->callout_info, callout_info, callout_len);
  158. rka->callout_len = callout_len;
  159. /* allocate the auth key */
  160. sprintf(desc, "%x", target->serial);
  161. authkey = key_alloc(&key_type_request_key_auth, desc,
  162. cred->fsuid, cred->fsgid, cred,
  163. KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
  164. KEY_USR_VIEW, KEY_ALLOC_NOT_IN_QUOTA);
  165. if (IS_ERR(authkey)) {
  166. ret = PTR_ERR(authkey);
  167. goto error_alloc;
  168. }
  169. /* construct the auth key */
  170. ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
  171. if (ret < 0)
  172. goto error_inst;
  173. kleave(" = {%d,%d}", authkey->serial, atomic_read(&authkey->usage));
  174. return authkey;
  175. auth_key_revoked:
  176. up_read(&cred->request_key_auth->sem);
  177. kfree(rka->callout_info);
  178. kfree(rka);
  179. kleave("= -EKEYREVOKED");
  180. return ERR_PTR(-EKEYREVOKED);
  181. error_inst:
  182. key_revoke(authkey);
  183. key_put(authkey);
  184. error_alloc:
  185. key_put(rka->target_key);
  186. key_put(rka->dest_keyring);
  187. kfree(rka->callout_info);
  188. kfree(rka);
  189. kleave("= %d", ret);
  190. return ERR_PTR(ret);
  191. }
  192. /*
  193. * Search the current process's keyrings for the authorisation key for
  194. * instantiation of a key.
  195. */
  196. struct key *key_get_instantiation_authkey(key_serial_t target_id)
  197. {
  198. char description[16];
  199. struct keyring_search_context ctx = {
  200. .index_key.type = &key_type_request_key_auth,
  201. .index_key.description = description,
  202. .cred = current_cred(),
  203. .match = user_match,
  204. .match_data = description,
  205. .flags = KEYRING_SEARCH_LOOKUP_DIRECT,
  206. };
  207. struct key *authkey;
  208. key_ref_t authkey_ref;
  209. sprintf(description, "%x", target_id);
  210. authkey_ref = search_process_keyrings(&ctx);
  211. if (IS_ERR(authkey_ref)) {
  212. authkey = ERR_CAST(authkey_ref);
  213. if (authkey == ERR_PTR(-EAGAIN))
  214. authkey = ERR_PTR(-ENOKEY);
  215. goto error;
  216. }
  217. authkey = key_ref_to_ptr(authkey_ref);
  218. if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
  219. key_put(authkey);
  220. authkey = ERR_PTR(-EKEYREVOKED);
  221. }
  222. error:
  223. return authkey;
  224. }