request_key_auth.c 4.9 KB

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