request_key_auth.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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. kfree(rka);
  86. } /* end request_key_auth_destroy() */
  87. /*****************************************************************************/
  88. /*
  89. * create a session keyring to be for the invokation of /sbin/request-key and
  90. * stick an authorisation token in it
  91. */
  92. struct key *request_key_auth_new(struct key *target, struct key **_rkakey)
  93. {
  94. struct key *keyring, *rkakey = NULL;
  95. char desc[20];
  96. int ret;
  97. kenter("%d,", target->serial);
  98. /* allocate a new session keyring */
  99. sprintf(desc, "_req.%u", target->serial);
  100. keyring = keyring_alloc(desc, current->fsuid, current->fsgid, 1, NULL);
  101. if (IS_ERR(keyring)) {
  102. kleave("= %ld", PTR_ERR(keyring));
  103. return keyring;
  104. }
  105. /* allocate the auth key */
  106. sprintf(desc, "%x", target->serial);
  107. rkakey = key_alloc(&key_type_request_key_auth, desc,
  108. current->fsuid, current->fsgid,
  109. KEY_POS_VIEW | KEY_USR_VIEW, 1);
  110. if (IS_ERR(rkakey)) {
  111. key_put(keyring);
  112. kleave("= %ld", PTR_ERR(rkakey));
  113. return rkakey;
  114. }
  115. /* construct and attach to the keyring */
  116. ret = key_instantiate_and_link(rkakey, target, 0, keyring, NULL);
  117. if (ret < 0) {
  118. key_revoke(rkakey);
  119. key_put(rkakey);
  120. key_put(keyring);
  121. kleave("= %d", ret);
  122. return ERR_PTR(ret);
  123. }
  124. *_rkakey = rkakey;
  125. kleave(" = {%d} ({%d})", keyring->serial, rkakey->serial);
  126. return keyring;
  127. } /* end request_key_auth_new() */
  128. /*****************************************************************************/
  129. /*
  130. * get the authorisation key for instantiation of a specific key if attached to
  131. * the current process's keyrings
  132. * - this key is inserted into a keyring and that is set as /sbin/request-key's
  133. * session keyring
  134. * - a target_id of zero specifies any valid token
  135. */
  136. struct key *key_get_instantiation_authkey(key_serial_t target_id)
  137. {
  138. struct task_struct *tsk = current;
  139. struct key *instkey;
  140. /* we must have our own personal session keyring */
  141. if (!tsk->signal->session_keyring)
  142. return ERR_PTR(-EACCES);
  143. /* and it must contain a suitable request authorisation key
  144. * - lock RCU against session keyring changing
  145. */
  146. rcu_read_lock();
  147. instkey = keyring_search_instkey(
  148. rcu_dereference(tsk->signal->session_keyring), target_id);
  149. rcu_read_unlock();
  150. return instkey;
  151. } /* end key_get_instantiation_authkey() */