request_key_auth.c 7.5 KB

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