permission.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /* Key permission checking
  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/security.h>
  13. #include "internal.h"
  14. /**
  15. * key_task_permission - Check a key can be used
  16. * @key_ref: The key to check.
  17. * @cred: The credentials to use.
  18. * @perm: The permissions to check for.
  19. *
  20. * Check to see whether permission is granted to use a key in the desired way,
  21. * but permit the security modules to override.
  22. *
  23. * The caller must hold either a ref on cred or must hold the RCU readlock.
  24. *
  25. * Returns 0 if successful, -EACCES if access is denied based on the
  26. * permissions bits or the LSM check.
  27. */
  28. int key_task_permission(const key_ref_t key_ref, const struct cred *cred,
  29. key_perm_t perm)
  30. {
  31. struct key *key;
  32. key_perm_t kperm;
  33. int ret;
  34. key = key_ref_to_ptr(key_ref);
  35. if (key->user->user_ns != cred->user_ns)
  36. goto use_other_perms;
  37. /* use the second 8-bits of permissions for keys the caller owns */
  38. if (key->uid == cred->fsuid) {
  39. kperm = key->perm >> 16;
  40. goto use_these_perms;
  41. }
  42. /* use the third 8-bits of permissions for keys the caller has a group
  43. * membership in common with */
  44. if (key->gid != -1 && key->perm & KEY_GRP_ALL) {
  45. if (key->gid == cred->fsgid) {
  46. kperm = key->perm >> 8;
  47. goto use_these_perms;
  48. }
  49. ret = groups_search(cred->group_info,
  50. make_kgid(current_user_ns(), key->gid));
  51. if (ret) {
  52. kperm = key->perm >> 8;
  53. goto use_these_perms;
  54. }
  55. }
  56. use_other_perms:
  57. /* otherwise use the least-significant 8-bits */
  58. kperm = key->perm;
  59. use_these_perms:
  60. /* use the top 8-bits of permissions for keys the caller possesses
  61. * - possessor permissions are additive with other permissions
  62. */
  63. if (is_key_possessed(key_ref))
  64. kperm |= key->perm >> 24;
  65. kperm = kperm & perm & KEY_ALL;
  66. if (kperm != perm)
  67. return -EACCES;
  68. /* let LSM be the final arbiter */
  69. return security_key_permission(key_ref, cred, perm);
  70. }
  71. EXPORT_SYMBOL(key_task_permission);
  72. /**
  73. * key_validate - Validate a key.
  74. * @key: The key to be validated.
  75. *
  76. * Check that a key is valid, returning 0 if the key is okay, -EKEYREVOKED if
  77. * the key's type has been removed or if the key has been revoked or
  78. * -EKEYEXPIRED if the key has expired.
  79. */
  80. int key_validate(struct key *key)
  81. {
  82. struct timespec now;
  83. int ret = 0;
  84. if (key) {
  85. /* check it's still accessible */
  86. ret = -EKEYREVOKED;
  87. if (test_bit(KEY_FLAG_REVOKED, &key->flags) ||
  88. test_bit(KEY_FLAG_DEAD, &key->flags))
  89. goto error;
  90. /* check it hasn't expired */
  91. ret = 0;
  92. if (key->expiry) {
  93. now = current_kernel_time();
  94. if (now.tv_sec >= key->expiry)
  95. ret = -EKEYEXPIRED;
  96. }
  97. }
  98. error:
  99. return ret;
  100. }
  101. EXPORT_SYMBOL(key_validate);