cred.h 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. /* Credentials management
  2. *
  3. * Copyright (C) 2008 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 Licence
  8. * as published by the Free Software Foundation; either version
  9. * 2 of the Licence, or (at your option) any later version.
  10. */
  11. #ifndef _LINUX_CRED_H
  12. #define _LINUX_CRED_H
  13. #include <linux/capability.h>
  14. #include <linux/key.h>
  15. #include <asm/atomic.h>
  16. struct user_struct;
  17. struct cred;
  18. /*
  19. * COW Supplementary groups list
  20. */
  21. #define NGROUPS_SMALL 32
  22. #define NGROUPS_PER_BLOCK ((unsigned int)(PAGE_SIZE / sizeof(gid_t)))
  23. struct group_info {
  24. atomic_t usage;
  25. int ngroups;
  26. int nblocks;
  27. gid_t small_block[NGROUPS_SMALL];
  28. gid_t *blocks[0];
  29. };
  30. /**
  31. * get_group_info - Get a reference to a group info structure
  32. * @group_info: The group info to reference
  33. *
  34. * This must be called with the owning task locked (via task_lock()) when task
  35. * != current. The reason being that the vast majority of callers are looking
  36. * at current->group_info, which can not be changed except by the current task.
  37. * Changing current->group_info requires the task lock, too.
  38. */
  39. #define get_group_info(group_info) \
  40. do { \
  41. atomic_inc(&(group_info)->usage); \
  42. } while (0)
  43. /**
  44. * put_group_info - Release a reference to a group info structure
  45. * @group_info: The group info to release
  46. */
  47. #define put_group_info(group_info) \
  48. do { \
  49. if (atomic_dec_and_test(&(group_info)->usage)) \
  50. groups_free(group_info); \
  51. } while (0)
  52. extern struct group_info *groups_alloc(int);
  53. extern void groups_free(struct group_info *);
  54. extern int set_current_groups(struct group_info *);
  55. extern int set_groups(struct cred *, struct group_info *);
  56. extern int groups_search(struct group_info *, gid_t);
  57. /* access the groups "array" with this macro */
  58. #define GROUP_AT(gi, i) \
  59. ((gi)->blocks[(i) / NGROUPS_PER_BLOCK][(i) % NGROUPS_PER_BLOCK])
  60. extern int in_group_p(gid_t);
  61. extern int in_egroup_p(gid_t);
  62. /*
  63. * The security context of a task
  64. *
  65. * The parts of the context break down into two categories:
  66. *
  67. * (1) The objective context of a task. These parts are used when some other
  68. * task is attempting to affect this one.
  69. *
  70. * (2) The subjective context. These details are used when the task is acting
  71. * upon another object, be that a file, a task, a key or whatever.
  72. *
  73. * Note that some members of this structure belong to both categories - the
  74. * LSM security pointer for instance.
  75. *
  76. * A task has two security pointers. task->real_cred points to the objective
  77. * context that defines that task's actual details. The objective part of this
  78. * context is used whenever that task is acted upon.
  79. *
  80. * task->cred points to the subjective context that defines the details of how
  81. * that task is going to act upon another object. This may be overridden
  82. * temporarily to point to another security context, but normally points to the
  83. * same context as task->real_cred.
  84. */
  85. struct cred {
  86. atomic_t usage;
  87. uid_t uid; /* real UID of the task */
  88. gid_t gid; /* real GID of the task */
  89. uid_t suid; /* saved UID of the task */
  90. gid_t sgid; /* saved GID of the task */
  91. uid_t euid; /* effective UID of the task */
  92. gid_t egid; /* effective GID of the task */
  93. uid_t fsuid; /* UID for VFS ops */
  94. gid_t fsgid; /* GID for VFS ops */
  95. unsigned securebits; /* SUID-less security management */
  96. kernel_cap_t cap_inheritable; /* caps our children can inherit */
  97. kernel_cap_t cap_permitted; /* caps we're permitted */
  98. kernel_cap_t cap_effective; /* caps we can actually use */
  99. kernel_cap_t cap_bset; /* capability bounding set */
  100. #ifdef CONFIG_KEYS
  101. unsigned char jit_keyring; /* default keyring to attach requested
  102. * keys to */
  103. struct key *thread_keyring; /* keyring private to this thread */
  104. struct key *request_key_auth; /* assumed request_key authority */
  105. #endif
  106. #ifdef CONFIG_SECURITY
  107. void *security; /* subjective LSM security */
  108. #endif
  109. struct user_struct *user; /* real user ID subscription */
  110. struct group_info *group_info; /* supplementary groups for euid/fsgid */
  111. struct rcu_head rcu; /* RCU deletion hook */
  112. spinlock_t lock; /* lock for pointer changes */
  113. };
  114. #define get_current_user() (get_uid(current->cred->user))
  115. #define task_uid(task) ((task)->cred->uid)
  116. #define task_gid(task) ((task)->cred->gid)
  117. #define task_euid(task) ((task)->cred->euid)
  118. #define task_egid(task) ((task)->cred->egid)
  119. #define current_uid() (current->cred->uid)
  120. #define current_gid() (current->cred->gid)
  121. #define current_euid() (current->cred->euid)
  122. #define current_egid() (current->cred->egid)
  123. #define current_suid() (current->cred->suid)
  124. #define current_sgid() (current->cred->sgid)
  125. #define current_fsuid() (current->cred->fsuid)
  126. #define current_fsgid() (current->cred->fsgid)
  127. #define current_cap() (current->cred->cap_effective)
  128. #define current_uid_gid(_uid, _gid) \
  129. do { \
  130. *(_uid) = current->cred->uid; \
  131. *(_gid) = current->cred->gid; \
  132. } while(0)
  133. #define current_euid_egid(_uid, _gid) \
  134. do { \
  135. *(_uid) = current->cred->euid; \
  136. *(_gid) = current->cred->egid; \
  137. } while(0)
  138. #define current_fsuid_fsgid(_uid, _gid) \
  139. do { \
  140. *(_uid) = current->cred->fsuid; \
  141. *(_gid) = current->cred->fsgid; \
  142. } while(0)
  143. extern void __put_cred(struct cred *);
  144. extern int copy_creds(struct task_struct *, unsigned long);
  145. /**
  146. * get_cred - Get a reference on a set of credentials
  147. * @cred: The credentials to reference
  148. *
  149. * Get a reference on the specified set of credentials. The caller must
  150. * release the reference.
  151. */
  152. static inline struct cred *get_cred(struct cred *cred)
  153. {
  154. atomic_inc(&cred->usage);
  155. return cred;
  156. }
  157. /**
  158. * put_cred - Release a reference to a set of credentials
  159. * @cred: The credentials to release
  160. *
  161. * Release a reference to a set of credentials, deleting them when the last ref
  162. * is released.
  163. */
  164. static inline void put_cred(struct cred *cred)
  165. {
  166. if (atomic_dec_and_test(&(cred)->usage))
  167. __put_cred(cred);
  168. }
  169. #endif /* _LINUX_CRED_H */