request_key_auth.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225
  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 <asm/uaccess.h>
  18. #include "internal.h"
  19. static int request_key_auth_instantiate(struct key *, const void *, size_t);
  20. static void request_key_auth_describe(const struct key *, struct seq_file *);
  21. static void request_key_auth_destroy(struct key *);
  22. static long request_key_auth_read(const struct key *, char __user *, size_t);
  23. /*
  24. * the request-key authorisation key type definition
  25. */
  26. struct key_type key_type_request_key_auth = {
  27. .name = ".request_key_auth",
  28. .def_datalen = sizeof(struct request_key_auth),
  29. .instantiate = request_key_auth_instantiate,
  30. .describe = request_key_auth_describe,
  31. .destroy = request_key_auth_destroy,
  32. .read = request_key_auth_read,
  33. };
  34. /*****************************************************************************/
  35. /*
  36. * instantiate a request-key authorisation key
  37. */
  38. static int request_key_auth_instantiate(struct key *key,
  39. const void *data,
  40. size_t datalen)
  41. {
  42. key->payload.data = (struct request_key_auth *) data;
  43. return 0;
  44. } /* end request_key_auth_instantiate() */
  45. /*****************************************************************************/
  46. /*
  47. * reading a request-key authorisation key retrieves the callout information
  48. */
  49. static void request_key_auth_describe(const struct key *key,
  50. struct seq_file *m)
  51. {
  52. struct request_key_auth *rka = key->payload.data;
  53. seq_puts(m, "key:");
  54. seq_puts(m, key->description);
  55. seq_printf(m, " pid:%d ci:%zu", rka->pid, strlen(rka->callout_info));
  56. } /* end request_key_auth_describe() */
  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 = strlen(rka->callout_info);
  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. } /* end request_key_auth_read() */
  79. /*****************************************************************************/
  80. /*
  81. * destroy an instantiation authorisation token key
  82. */
  83. static void request_key_auth_destroy(struct key *key)
  84. {
  85. struct request_key_auth *rka = key->payload.data;
  86. kenter("{%d}", key->serial);
  87. key_put(rka->target_key);
  88. kfree(rka);
  89. } /* end request_key_auth_destroy() */
  90. /*****************************************************************************/
  91. /*
  92. * create an authorisation token for /sbin/request-key or whoever to gain
  93. * access to the caller's security data
  94. */
  95. struct key *request_key_auth_new(struct key *target, const char *callout_info)
  96. {
  97. struct request_key_auth *rka, *irka;
  98. struct key *authkey = NULL;
  99. char desc[20];
  100. int ret;
  101. kenter("%d,", target->serial);
  102. /* allocate a auth record */
  103. rka = kmalloc(sizeof(*rka), GFP_KERNEL);
  104. if (!rka) {
  105. kleave(" = -ENOMEM");
  106. return ERR_PTR(-ENOMEM);
  107. }
  108. /* see if the calling process is already servicing the key request of
  109. * another process */
  110. if (current->request_key_auth) {
  111. /* it is - use that instantiation context here too */
  112. irka = current->request_key_auth->payload.data;
  113. rka->context = irka->context;
  114. rka->pid = irka->pid;
  115. }
  116. else {
  117. /* it isn't - use this process as the context */
  118. rka->context = current;
  119. rka->pid = current->pid;
  120. }
  121. rka->target_key = key_get(target);
  122. rka->callout_info = callout_info;
  123. /* allocate the auth key */
  124. sprintf(desc, "%x", target->serial);
  125. authkey = key_alloc(&key_type_request_key_auth, desc,
  126. current->fsuid, current->fsgid,
  127. KEY_POS_VIEW | KEY_POS_READ | KEY_POS_SEARCH |
  128. KEY_USR_VIEW, 1);
  129. if (IS_ERR(authkey)) {
  130. ret = PTR_ERR(authkey);
  131. goto error_alloc;
  132. }
  133. /* construct and attach to the keyring */
  134. ret = key_instantiate_and_link(authkey, rka, 0, NULL, NULL);
  135. if (ret < 0)
  136. goto error_inst;
  137. kleave(" = {%d})", authkey->serial);
  138. return authkey;
  139. error_inst:
  140. key_revoke(authkey);
  141. key_put(authkey);
  142. error_alloc:
  143. key_put(rka->target_key);
  144. kfree(rka);
  145. kleave("= %d", ret);
  146. return ERR_PTR(ret);
  147. } /* end request_key_auth_new() */
  148. /*****************************************************************************/
  149. /*
  150. * see if an authorisation key is associated with a particular key
  151. */
  152. static int key_get_instantiation_authkey_match(const struct key *key,
  153. const void *_id)
  154. {
  155. struct request_key_auth *rka = key->payload.data;
  156. key_serial_t id = (key_serial_t)(unsigned long) _id;
  157. return rka->target_key->serial == id;
  158. } /* end key_get_instantiation_authkey_match() */
  159. /*****************************************************************************/
  160. /*
  161. * get the authorisation key for instantiation of a specific key if attached to
  162. * the current process's keyrings
  163. * - this key is inserted into a keyring and that is set as /sbin/request-key's
  164. * session keyring
  165. * - a target_id of zero specifies any valid token
  166. */
  167. struct key *key_get_instantiation_authkey(key_serial_t target_id)
  168. {
  169. struct key *authkey;
  170. key_ref_t authkey_ref;
  171. authkey_ref = search_process_keyrings(
  172. &key_type_request_key_auth,
  173. (void *) (unsigned long) target_id,
  174. key_get_instantiation_authkey_match,
  175. current);
  176. if (IS_ERR(authkey_ref)) {
  177. authkey = ERR_PTR(PTR_ERR(authkey_ref));
  178. goto error;
  179. }
  180. authkey = key_ref_to_ptr(authkey_ref);
  181. if (test_bit(KEY_FLAG_REVOKED, &authkey->flags)) {
  182. key_put(authkey);
  183. authkey = ERR_PTR(-EKEYREVOKED);
  184. }
  185. error:
  186. return authkey;
  187. } /* end key_get_instantiation_authkey() */