persistent.c 4.3 KB

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