persistent.c 4.5 KB

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