persistent.c 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /* General persistent per-UID keyrings register
  2. *
  3. * Copyright (C) 2013 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 Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #include <linux/user_namespace.h>
  12. #include <linux/cred.h>
  13. #include "internal.h"
  14. unsigned persistent_keyring_expiry = 3 * 24 * 3600; /* Expire after 3 days of non-use */
  15. /*
  16. * Create the persistent keyring register for the current user namespace.
  17. *
  18. * Called with the namespace's sem locked for writing.
  19. */
  20. static int key_create_persistent_register(struct user_namespace *ns)
  21. {
  22. struct key *reg = keyring_alloc(".persistent_register",
  23. KUIDT_INIT(0), KGIDT_INIT(0),
  24. current_cred(),
  25. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  26. KEY_USR_VIEW | KEY_USR_READ),
  27. KEY_ALLOC_NOT_IN_QUOTA, NULL, NULL);
  28. if (IS_ERR(reg))
  29. return PTR_ERR(reg);
  30. ns->persistent_keyring_register = reg;
  31. return 0;
  32. }
  33. /*
  34. * Create the persistent keyring for the specified user.
  35. *
  36. * Called with the namespace's sem locked for writing.
  37. */
  38. static key_ref_t key_create_persistent(struct user_namespace *ns, kuid_t uid,
  39. struct keyring_index_key *index_key)
  40. {
  41. struct key *persistent;
  42. key_ref_t reg_ref, persistent_ref;
  43. if (!ns->persistent_keyring_register) {
  44. long err = key_create_persistent_register(ns);
  45. if (err < 0)
  46. return ERR_PTR(err);
  47. } else {
  48. reg_ref = make_key_ref(ns->persistent_keyring_register, true);
  49. persistent_ref = find_key_to_update(reg_ref, index_key);
  50. if (persistent_ref)
  51. return persistent_ref;
  52. }
  53. persistent = keyring_alloc(index_key->description,
  54. uid, INVALID_GID, current_cred(),
  55. ((KEY_POS_ALL & ~KEY_POS_SETATTR) |
  56. KEY_USR_VIEW | KEY_USR_READ),
  57. KEY_ALLOC_NOT_IN_QUOTA, NULL,
  58. ns->persistent_keyring_register);
  59. if (IS_ERR(persistent))
  60. return ERR_CAST(persistent);
  61. return make_key_ref(persistent, true);
  62. }
  63. /*
  64. * Get the persistent keyring for a specific UID and link it to the nominated
  65. * keyring.
  66. */
  67. static long key_get_persistent(struct user_namespace *ns, kuid_t uid,
  68. key_ref_t dest_ref)
  69. {
  70. struct keyring_index_key index_key;
  71. struct key *persistent;
  72. key_ref_t reg_ref, persistent_ref;
  73. char buf[32];
  74. long ret;
  75. /* Look in the register if it exists */
  76. index_key.type = &key_type_keyring;
  77. index_key.description = buf;
  78. index_key.desc_len = sprintf(buf, "_persistent.%u", from_kuid(ns, uid));
  79. if (ns->persistent_keyring_register) {
  80. reg_ref = make_key_ref(ns->persistent_keyring_register, true);
  81. down_read(&ns->persistent_keyring_register_sem);
  82. persistent_ref = find_key_to_update(reg_ref, &index_key);
  83. up_read(&ns->persistent_keyring_register_sem);
  84. if (persistent_ref)
  85. goto found;
  86. }
  87. /* It wasn't in the register, so we'll need to create it. We might
  88. * also need to create the register.
  89. */
  90. down_write(&ns->persistent_keyring_register_sem);
  91. persistent_ref = key_create_persistent(ns, uid, &index_key);
  92. up_write(&ns->persistent_keyring_register_sem);
  93. if (!IS_ERR(persistent_ref))
  94. goto found;
  95. return PTR_ERR(persistent_ref);
  96. found:
  97. ret = key_task_permission(persistent_ref, current_cred(), KEY_NEED_LINK);
  98. if (ret == 0) {
  99. persistent = key_ref_to_ptr(persistent_ref);
  100. ret = key_link(key_ref_to_ptr(dest_ref), persistent);
  101. if (ret == 0) {
  102. key_set_timeout(persistent, persistent_keyring_expiry);
  103. ret = persistent->serial;
  104. }
  105. }
  106. key_ref_put(persistent_ref);
  107. return ret;
  108. }
  109. /*
  110. * Get the persistent keyring for a specific UID and link it to the nominated
  111. * keyring.
  112. */
  113. long keyctl_get_persistent(uid_t _uid, key_serial_t destid)
  114. {
  115. struct user_namespace *ns = current_user_ns();
  116. key_ref_t dest_ref;
  117. kuid_t uid;
  118. long ret;
  119. /* -1 indicates the current user */
  120. if (_uid == (uid_t)-1) {
  121. uid = current_uid();
  122. } else {
  123. uid = make_kuid(ns, _uid);
  124. if (!uid_valid(uid))
  125. return -EINVAL;
  126. /* You can only see your own persistent cache if you're not
  127. * sufficiently privileged.
  128. */
  129. if (!uid_eq(uid, current_uid()) &&
  130. !uid_eq(uid, current_euid()) &&
  131. !ns_capable(ns, CAP_SETUID))
  132. return -EPERM;
  133. }
  134. /* There must be a destination keyring */
  135. dest_ref = lookup_user_key(destid, KEY_LOOKUP_CREATE, KEY_NEED_WRITE);
  136. if (IS_ERR(dest_ref))
  137. return PTR_ERR(dest_ref);
  138. if (key_ref_to_ptr(dest_ref)->type != &key_type_keyring) {
  139. ret = -ENOTDIR;
  140. goto out_put_dest;
  141. }
  142. ret = key_get_persistent(ns, uid, dest_ref);
  143. out_put_dest:
  144. key_ref_put(dest_ref);
  145. return ret;
  146. }