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 second 8-bits of permissions for keys the caller owns */
  27. if (key->uid == context->fsuid) {
  28. kperm = key->perm >> 16;
  29. goto use_these_perms;
  30. }
  31. /* use the third 8-bits of permissions for keys the caller has a group
  32. * membership in common with */
  33. if (key->gid != -1 && key->perm & KEY_GRP_ALL) {
  34. if (key->gid == context->fsgid) {
  35. kperm = key->perm >> 8;
  36. goto use_these_perms;
  37. }
  38. task_lock(context);
  39. ret = groups_search(context->group_info, key->gid);
  40. task_unlock(context);
  41. if (ret) {
  42. kperm = key->perm >> 8;
  43. goto use_these_perms;
  44. }
  45. }
  46. /* otherwise use the least-significant 8-bits */
  47. kperm = key->perm;
  48. use_these_perms:
  49. /* use the top 8-bits of permissions for keys the caller possesses
  50. * - possessor permissions are additive with other permissions
  51. */
  52. if (is_key_possessed(key_ref))
  53. kperm |= key->perm >> 24;
  54. kperm = kperm & perm & KEY_ALL;
  55. return kperm == perm;
  56. } /* end key_task_permission() */
  57. EXPORT_SYMBOL(key_task_permission);