permission.c 2.5 KB

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