permission.c 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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. /* use the second 8-bits of permissions for keys the caller owns */
  35. if (key->uid == cred->fsuid) {
  36. kperm = key->perm >> 16;
  37. goto use_these_perms;
  38. }
  39. /* use the third 8-bits of permissions for keys the caller has a group
  40. * membership in common with */
  41. if (key->gid != -1 && key->perm & KEY_GRP_ALL) {
  42. if (key->gid == cred->fsgid) {
  43. kperm = key->perm >> 8;
  44. goto use_these_perms;
  45. }
  46. ret = groups_search(cred->group_info, key->gid);
  47. if (ret) {
  48. kperm = key->perm >> 8;
  49. goto use_these_perms;
  50. }
  51. }
  52. /* otherwise use the least-significant 8-bits */
  53. kperm = key->perm;
  54. use_these_perms:
  55. /* use the top 8-bits of permissions for keys the caller possesses
  56. * - possessor permissions are additive with other permissions
  57. */
  58. if (is_key_possessed(key_ref))
  59. kperm |= key->perm >> 24;
  60. kperm = kperm & perm & KEY_ALL;
  61. if (kperm != perm)
  62. return -EACCES;
  63. /* let LSM be the final arbiter */
  64. return security_key_permission(key_ref, cred, perm);
  65. } /* end key_task_permission() */
  66. EXPORT_SYMBOL(key_task_permission);
  67. /*****************************************************************************/
  68. /*
  69. * validate a key
  70. */
  71. int key_validate(struct key *key)
  72. {
  73. struct timespec now;
  74. int ret = 0;
  75. if (key) {
  76. /* check it's still accessible */
  77. ret = -EKEYREVOKED;
  78. if (test_bit(KEY_FLAG_REVOKED, &key->flags) ||
  79. test_bit(KEY_FLAG_DEAD, &key->flags))
  80. goto error;
  81. /* check it hasn't expired */
  82. ret = 0;
  83. if (key->expiry) {
  84. now = current_kernel_time();
  85. if (now.tv_sec >= key->expiry)
  86. ret = -EKEYEXPIRED;
  87. }
  88. }
  89. error:
  90. return ret;
  91. } /* end key_validate() */
  92. EXPORT_SYMBOL(key_validate);