request_key_auth.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 "internal.h"
  18. static int request_key_auth_instantiate(struct key *, const void *, size_t);
  19. static void request_key_auth_describe(const struct key *, struct seq_file *);
  20. static void request_key_auth_destroy(struct key *);
  21. /*
  22. * the request-key authorisation key type definition
  23. */
  24. struct key_type key_type_request_key_auth = {
  25. .name = ".request_key_auth",
  26. .def_datalen = sizeof(struct request_key_auth),
  27. .instantiate = request_key_auth_instantiate,
  28. .describe = request_key_auth_describe,
  29. .destroy = request_key_auth_destroy,
  30. };
  31. /*****************************************************************************/
  32. /*
  33. * instantiate a request-key authorisation record
  34. */
  35. static int request_key_auth_instantiate(struct key *key,
  36. const void *data,
  37. size_t datalen)
  38. {
  39. struct request_key_auth *rka, *irka;
  40. struct key *instkey;
  41. int ret;
  42. ret = -ENOMEM;
  43. rka = kmalloc(sizeof(*rka), GFP_KERNEL);
  44. if (rka) {
  45. /* see if the calling process is already servicing the key
  46. * request of another process */
  47. instkey = key_get_instantiation_authkey(0);
  48. if (!IS_ERR(instkey)) {
  49. /* it is - use that instantiation context here too */
  50. irka = instkey->payload.data;
  51. rka->context = irka->context;
  52. rka->pid = irka->pid;
  53. key_put(instkey);
  54. }
  55. else {
  56. /* it isn't - use this process as the context */
  57. rka->context = current;
  58. rka->pid = current->pid;
  59. }
  60. rka->target_key = key_get((struct key *) data);
  61. key->payload.data = rka;
  62. ret = 0;
  63. }
  64. return ret;
  65. } /* end request_key_auth_instantiate() */
  66. /*****************************************************************************/
  67. /*
  68. *
  69. */
  70. static void request_key_auth_describe(const struct key *key,
  71. struct seq_file *m)
  72. {
  73. struct request_key_auth *rka = key->payload.data;
  74. seq_puts(m, "key:");
  75. seq_puts(m, key->description);
  76. seq_printf(m, " pid:%d", rka->pid);
  77. } /* end request_key_auth_describe() */
  78. /*****************************************************************************/
  79. /*
  80. * destroy an instantiation authorisation token key
  81. */
  82. static void request_key_auth_destroy(struct key *key)
  83. {
  84. struct request_key_auth *rka = key->payload.data;
  85. kenter("{%d}", key->serial);
  86. key_put(rka->target_key);
  87. kfree(rka);
  88. } /* end request_key_auth_destroy() */
  89. /*****************************************************************************/
  90. /*
  91. * create a session keyring to be for the invokation of /sbin/request-key and
  92. * stick an authorisation token in it
  93. */
  94. struct key *request_key_auth_new(struct key *target, struct key **_rkakey)
  95. {
  96. struct key *keyring, *rkakey = NULL;
  97. char desc[20];
  98. int ret;
  99. kenter("%d,", target->serial);
  100. /* allocate a new session keyring */
  101. sprintf(desc, "_req.%u", target->serial);
  102. keyring = keyring_alloc(desc, current->fsuid, current->fsgid, 1, NULL);
  103. if (IS_ERR(keyring)) {
  104. kleave("= %ld", PTR_ERR(keyring));
  105. return keyring;
  106. }
  107. /* allocate the auth key */
  108. sprintf(desc, "%x", target->serial);
  109. rkakey = key_alloc(&key_type_request_key_auth, desc,
  110. current->fsuid, current->fsgid,
  111. KEY_POS_VIEW | KEY_USR_VIEW, 1);
  112. if (IS_ERR(rkakey)) {
  113. key_put(keyring);
  114. kleave("= %ld", PTR_ERR(rkakey));
  115. return rkakey;
  116. }
  117. /* construct and attach to the keyring */
  118. ret = key_instantiate_and_link(rkakey, target, 0, keyring, NULL);
  119. if (ret < 0) {
  120. key_revoke(rkakey);
  121. key_put(rkakey);
  122. key_put(keyring);
  123. kleave("= %d", ret);
  124. return ERR_PTR(ret);
  125. }
  126. *_rkakey = rkakey;
  127. kleave(" = {%d} ({%d})", keyring->serial, rkakey->serial);
  128. return keyring;
  129. } /* end request_key_auth_new() */
  130. /*****************************************************************************/
  131. /*
  132. * get the authorisation key for instantiation of a specific key if attached to
  133. * the current process's keyrings
  134. * - this key is inserted into a keyring and that is set as /sbin/request-key's
  135. * session keyring
  136. * - a target_id of zero specifies any valid token
  137. */
  138. struct key *key_get_instantiation_authkey(key_serial_t target_id)
  139. {
  140. struct task_struct *tsk = current;
  141. struct key *instkey;
  142. /* we must have our own personal session keyring */
  143. if (!tsk->signal->session_keyring)
  144. return ERR_PTR(-EACCES);
  145. /* and it must contain a suitable request authorisation key
  146. * - lock RCU against session keyring changing
  147. */
  148. rcu_read_lock();
  149. instkey = keyring_search_instkey(
  150. rcu_dereference(tsk->signal->session_keyring), target_id);
  151. rcu_read_unlock();
  152. return instkey;
  153. } /* end key_get_instantiation_authkey() */