request_key_auth.c 6.6 KB

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