permission.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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->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, key->gid);
  50. if (ret) {
  51. kperm = key->perm >> 8;
  52. goto use_these_perms;
  53. }
  54. }
  55. use_other_perms:
  56. /* otherwise use the least-significant 8-bits */
  57. kperm = key->perm;
  58. use_these_perms:
  59. /* use the top 8-bits of permissions for keys the caller possesses
  60. * - possessor permissions are additive with other permissions
  61. */
  62. if (is_key_possessed(key_ref))
  63. kperm |= key->perm >> 24;
  64. kperm = kperm & perm & KEY_ALL;
  65. if (kperm != perm)
  66. return -EACCES;
  67. /* let LSM be the final arbiter */
  68. return security_key_permission(key_ref, cred, perm);
  69. }
  70. EXPORT_SYMBOL(key_task_permission);
  71. /**
  72. * key_validate - Validate a key.
  73. * @key: The key to be validated.
  74. *
  75. * Check that a key is valid, returning 0 if the key is okay, -EKEYREVOKED if
  76. * the key's type has been removed or if the key has been revoked or
  77. * -EKEYEXPIRED if the key has expired.
  78. */
  79. int key_validate(struct key *key)
  80. {
  81. struct timespec now;
  82. int ret = 0;
  83. if (key) {
  84. /* check it's still accessible */
  85. ret = -EKEYREVOKED;
  86. if (test_bit(KEY_FLAG_REVOKED, &key->flags) ||
  87. test_bit(KEY_FLAG_DEAD, &key->flags))
  88. goto error;
  89. /* check it hasn't expired */
  90. ret = 0;
  91. if (key->expiry) {
  92. now = current_kernel_time();
  93. if (now.tv_sec >= key->expiry)
  94. ret = -EKEYEXPIRED;
  95. }
  96. }
  97. error:
  98. return ret;
  99. }
  100. EXPORT_SYMBOL(key_validate);