permission.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  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 "internal.h"
  13. /*****************************************************************************/
  14. /*
  15. * check to see whether permission is granted to use a key in the desired way,
  16. * but permit the security modules to override
  17. */
  18. int key_task_permission(const key_ref_t key_ref,
  19. struct task_struct *context,
  20. key_perm_t perm)
  21. {
  22. struct key *key;
  23. key_perm_t kperm;
  24. int ret;
  25. key = key_ref_to_ptr(key_ref);
  26. /* use the top 8-bits of permissions for keys the caller possesses */
  27. if (is_key_possessed(key_ref)) {
  28. kperm = key->perm >> 24;
  29. goto use_these_perms;
  30. }
  31. /* use the second 8-bits of permissions for keys the caller owns */
  32. if (key->uid == context->fsuid) {
  33. kperm = key->perm >> 16;
  34. goto use_these_perms;
  35. }
  36. /* use the third 8-bits of permissions for keys the caller has a group
  37. * membership in common with */
  38. if (key->gid != -1 && key->perm & KEY_GRP_ALL) {
  39. if (key->gid == context->fsgid) {
  40. kperm = key->perm >> 8;
  41. goto use_these_perms;
  42. }
  43. task_lock(context);
  44. ret = groups_search(context->group_info, key->gid);
  45. task_unlock(context);
  46. if (ret) {
  47. kperm = key->perm >> 8;
  48. goto use_these_perms;
  49. }
  50. }
  51. /* otherwise use the least-significant 8-bits */
  52. kperm = key->perm;
  53. use_these_perms:
  54. kperm = kperm & perm & KEY_ALL;
  55. return kperm == perm;
  56. } /* end key_task_permission() */
  57. EXPORT_SYMBOL(key_task_permission);