key-ui.h 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /* key-ui.h: key userspace interface stuff
  2. *
  3. * Copyright (C) 2004 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. #ifndef _LINUX_KEY_UI_H
  12. #define _LINUX_KEY_UI_H
  13. #include <linux/key.h>
  14. /* the key tree */
  15. extern struct rb_root key_serial_tree;
  16. extern spinlock_t key_serial_lock;
  17. /* required permissions */
  18. #define KEY_VIEW 0x01 /* require permission to view attributes */
  19. #define KEY_READ 0x02 /* require permission to read content */
  20. #define KEY_WRITE 0x04 /* require permission to update / modify */
  21. #define KEY_SEARCH 0x08 /* require permission to search (keyring) or find (key) */
  22. #define KEY_LINK 0x10 /* require permission to link */
  23. #define KEY_ALL 0x1f /* all the above permissions */
  24. /*
  25. * the keyring payload contains a list of the keys to which the keyring is
  26. * subscribed
  27. */
  28. struct keyring_list {
  29. struct rcu_head rcu; /* RCU deletion hook */
  30. unsigned short maxkeys; /* max keys this list can hold */
  31. unsigned short nkeys; /* number of keys currently held */
  32. unsigned short delkey; /* key to be unlinked by RCU */
  33. struct key *keys[0];
  34. };
  35. /*
  36. * check to see whether permission is granted to use a key in the desired way
  37. */
  38. static inline int key_permission(const struct key *key, key_perm_t perm)
  39. {
  40. key_perm_t kperm;
  41. if (key->uid == current->fsuid)
  42. kperm = key->perm >> 16;
  43. else if (key->gid != -1 &&
  44. key->perm & KEY_GRP_ALL &&
  45. in_group_p(key->gid)
  46. )
  47. kperm = key->perm >> 8;
  48. else
  49. kperm = key->perm;
  50. kperm = kperm & perm & KEY_ALL;
  51. return kperm == perm;
  52. }
  53. /*
  54. * check to see whether permission is granted to use a key in at least one of
  55. * the desired ways
  56. */
  57. static inline int key_any_permission(const struct key *key, key_perm_t perm)
  58. {
  59. key_perm_t kperm;
  60. if (key->uid == current->fsuid)
  61. kperm = key->perm >> 16;
  62. else if (key->gid != -1 &&
  63. key->perm & KEY_GRP_ALL &&
  64. in_group_p(key->gid)
  65. )
  66. kperm = key->perm >> 8;
  67. else
  68. kperm = key->perm;
  69. kperm = kperm & perm & KEY_ALL;
  70. return kperm != 0;
  71. }
  72. static inline int key_task_groups_search(struct task_struct *tsk, gid_t gid)
  73. {
  74. int ret;
  75. task_lock(tsk);
  76. ret = groups_search(tsk->group_info, gid);
  77. task_unlock(tsk);
  78. return ret;
  79. }
  80. static inline int key_task_permission(const struct key *key,
  81. struct task_struct *context,
  82. key_perm_t perm)
  83. {
  84. key_perm_t kperm;
  85. if (key->uid == context->fsuid) {
  86. kperm = key->perm >> 16;
  87. }
  88. else if (key->gid != -1 &&
  89. key->perm & KEY_GRP_ALL && (
  90. key->gid == context->fsgid ||
  91. key_task_groups_search(context, key->gid)
  92. )
  93. ) {
  94. kperm = key->perm >> 8;
  95. }
  96. else {
  97. kperm = key->perm;
  98. }
  99. kperm = kperm & perm & KEY_ALL;
  100. return kperm == perm;
  101. }
  102. extern struct key *lookup_user_key(struct task_struct *context,
  103. key_serial_t id, int create, int partial,
  104. key_perm_t perm);
  105. extern long join_session_keyring(const char *name);
  106. extern struct key_type *key_type_lookup(const char *type);
  107. extern void key_type_put(struct key_type *ktype);
  108. #define key_negative_timeout 60 /* default timeout on a negative key's existence */
  109. #endif /* _LINUX_KEY_UI_H */