permission.c 2.8 KB

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