PageRenderTime 27ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/include/linux/cred.h

https://gitlab.com/willemmali-linux/linux
C Header | 411 lines | 245 code | 40 blank | 126 comment | 4 complexity | 9369a9c38cb15903e0d29a03edea6568 MD5 | raw file
  1. /* Credentials management - see Documentation/security/credentials.txt
  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/init.h>
  15. #include <linux/key.h>
  16. #include <linux/selinux.h>
  17. #include <linux/atomic.h>
  18. #include <linux/uidgid.h>
  19. struct user_struct;
  20. struct cred;
  21. struct inode;
  22. /*
  23. * COW Supplementary groups list
  24. */
  25. #define NGROUPS_SMALL 32
  26. #define NGROUPS_PER_BLOCK ((unsigned int)(PAGE_SIZE / sizeof(kgid_t)))
  27. struct group_info {
  28. atomic_t usage;
  29. int ngroups;
  30. int nblocks;
  31. kgid_t small_block[NGROUPS_SMALL];
  32. kgid_t *blocks[0];
  33. };
  34. /**
  35. * get_group_info - Get a reference to a group info structure
  36. * @group_info: The group info to reference
  37. *
  38. * This gets a reference to a set of supplementary groups.
  39. *
  40. * If the caller is accessing a task's credentials, they must hold the RCU read
  41. * lock when reading.
  42. */
  43. static inline struct group_info *get_group_info(struct group_info *gi)
  44. {
  45. atomic_inc(&gi->usage);
  46. return gi;
  47. }
  48. /**
  49. * put_group_info - Release a reference to a group info structure
  50. * @group_info: The group info to release
  51. */
  52. #define put_group_info(group_info) \
  53. do { \
  54. if (atomic_dec_and_test(&(group_info)->usage)) \
  55. groups_free(group_info); \
  56. } while (0)
  57. extern struct group_info init_groups;
  58. #ifdef CONFIG_MULTIUSER
  59. extern struct group_info *groups_alloc(int);
  60. extern void groups_free(struct group_info *);
  61. extern int in_group_p(kgid_t);
  62. extern int in_egroup_p(kgid_t);
  63. #else
  64. static inline void groups_free(struct group_info *group_info)
  65. {
  66. }
  67. static inline int in_group_p(kgid_t grp)
  68. {
  69. return 1;
  70. }
  71. static inline int in_egroup_p(kgid_t grp)
  72. {
  73. return 1;
  74. }
  75. #endif
  76. extern int set_current_groups(struct group_info *);
  77. extern void set_groups(struct cred *, struct group_info *);
  78. extern int groups_search(const struct group_info *, kgid_t);
  79. extern bool may_setgroups(void);
  80. /* access the groups "array" with this macro */
  81. #define GROUP_AT(gi, i) \
  82. ((gi)->blocks[(i) / NGROUPS_PER_BLOCK][(i) % NGROUPS_PER_BLOCK])
  83. /*
  84. * The security context of a task
  85. *
  86. * The parts of the context break down into two categories:
  87. *
  88. * (1) The objective context of a task. These parts are used when some other
  89. * task is attempting to affect this one.
  90. *
  91. * (2) The subjective context. These details are used when the task is acting
  92. * upon another object, be that a file, a task, a key or whatever.
  93. *
  94. * Note that some members of this structure belong to both categories - the
  95. * LSM security pointer for instance.
  96. *
  97. * A task has two security pointers. task->real_cred points to the objective
  98. * context that defines that task's actual details. The objective part of this
  99. * context is used whenever that task is acted upon.
  100. *
  101. * task->cred points to the subjective context that defines the details of how
  102. * that task is going to act upon another object. This may be overridden
  103. * temporarily to point to another security context, but normally points to the
  104. * same context as task->real_cred.
  105. */
  106. struct cred {
  107. atomic_t usage;
  108. #ifdef CONFIG_DEBUG_CREDENTIALS
  109. atomic_t subscribers; /* number of processes subscribed */
  110. void *put_addr;
  111. unsigned magic;
  112. #define CRED_MAGIC 0x43736564
  113. #define CRED_MAGIC_DEAD 0x44656144
  114. #endif
  115. kuid_t uid; /* real UID of the task */
  116. kgid_t gid; /* real GID of the task */
  117. kuid_t suid; /* saved UID of the task */
  118. kgid_t sgid; /* saved GID of the task */
  119. kuid_t euid; /* effective UID of the task */
  120. kgid_t egid; /* effective GID of the task */
  121. kuid_t fsuid; /* UID for VFS ops */
  122. kgid_t fsgid; /* GID for VFS ops */
  123. unsigned securebits; /* SUID-less security management */
  124. kernel_cap_t cap_inheritable; /* caps our children can inherit */
  125. kernel_cap_t cap_permitted; /* caps we're permitted */
  126. kernel_cap_t cap_effective; /* caps we can actually use */
  127. kernel_cap_t cap_bset; /* capability bounding set */
  128. kernel_cap_t cap_ambient; /* Ambient capability set */
  129. #ifdef CONFIG_KEYS
  130. unsigned char jit_keyring; /* default keyring to attach requested
  131. * keys to */
  132. struct key __rcu *session_keyring; /* keyring inherited over fork */
  133. struct key *process_keyring; /* keyring private to this process */
  134. struct key *thread_keyring; /* keyring private to this thread */
  135. struct key *request_key_auth; /* assumed request_key authority */
  136. #endif
  137. #ifdef CONFIG_SECURITY
  138. void *security; /* subjective LSM security */
  139. #endif
  140. struct user_struct *user; /* real user ID subscription */
  141. struct user_namespace *user_ns; /* user_ns the caps and keyrings are relative to. */
  142. struct group_info *group_info; /* supplementary groups for euid/fsgid */
  143. struct rcu_head rcu; /* RCU deletion hook */
  144. };
  145. extern void __put_cred(struct cred *);
  146. extern void exit_creds(struct task_struct *);
  147. extern int copy_creds(struct task_struct *, unsigned long);
  148. extern const struct cred *get_task_cred(struct task_struct *);
  149. extern struct cred *cred_alloc_blank(void);
  150. extern struct cred *prepare_creds(void);
  151. extern struct cred *prepare_exec_creds(void);
  152. extern int commit_creds(struct cred *);
  153. extern void abort_creds(struct cred *);
  154. extern const struct cred *override_creds(const struct cred *);
  155. extern void revert_creds(const struct cred *);
  156. extern struct cred *prepare_kernel_cred(struct task_struct *);
  157. extern int change_create_files_as(struct cred *, struct inode *);
  158. extern int set_security_override(struct cred *, u32);
  159. extern int set_security_override_from_ctx(struct cred *, const char *);
  160. extern int set_create_files_as(struct cred *, struct inode *);
  161. extern void __init cred_init(void);
  162. /*
  163. * check for validity of credentials
  164. */
  165. #ifdef CONFIG_DEBUG_CREDENTIALS
  166. extern void __invalid_creds(const struct cred *, const char *, unsigned);
  167. extern void __validate_process_creds(struct task_struct *,
  168. const char *, unsigned);
  169. extern bool creds_are_invalid(const struct cred *cred);
  170. static inline void __validate_creds(const struct cred *cred,
  171. const char *file, unsigned line)
  172. {
  173. if (unlikely(creds_are_invalid(cred)))
  174. __invalid_creds(cred, file, line);
  175. }
  176. #define validate_creds(cred) \
  177. do { \
  178. __validate_creds((cred), __FILE__, __LINE__); \
  179. } while(0)
  180. #define validate_process_creds() \
  181. do { \
  182. __validate_process_creds(current, __FILE__, __LINE__); \
  183. } while(0)
  184. extern void validate_creds_for_do_exit(struct task_struct *);
  185. #else
  186. static inline void validate_creds(const struct cred *cred)
  187. {
  188. }
  189. static inline void validate_creds_for_do_exit(struct task_struct *tsk)
  190. {
  191. }
  192. static inline void validate_process_creds(void)
  193. {
  194. }
  195. #endif
  196. static inline bool cap_ambient_invariant_ok(const struct cred *cred)
  197. {
  198. return cap_issubset(cred->cap_ambient,
  199. cap_intersect(cred->cap_permitted,
  200. cred->cap_inheritable));
  201. }
  202. /**
  203. * get_new_cred - Get a reference on a new set of credentials
  204. * @cred: The new credentials to reference
  205. *
  206. * Get a reference on the specified set of new credentials. The caller must
  207. * release the reference.
  208. */
  209. static inline struct cred *get_new_cred(struct cred *cred)
  210. {
  211. atomic_inc(&cred->usage);
  212. return cred;
  213. }
  214. /**
  215. * get_cred - Get a reference on a set of credentials
  216. * @cred: The credentials to reference
  217. *
  218. * Get a reference on the specified set of credentials. The caller must
  219. * release the reference.
  220. *
  221. * This is used to deal with a committed set of credentials. Although the
  222. * pointer is const, this will temporarily discard the const and increment the
  223. * usage count. The purpose of this is to attempt to catch at compile time the
  224. * accidental alteration of a set of credentials that should be considered
  225. * immutable.
  226. */
  227. static inline const struct cred *get_cred(const struct cred *cred)
  228. {
  229. struct cred *nonconst_cred = (struct cred *) cred;
  230. validate_creds(cred);
  231. return get_new_cred(nonconst_cred);
  232. }
  233. /**
  234. * put_cred - Release a reference to a set of credentials
  235. * @cred: The credentials to release
  236. *
  237. * Release a reference to a set of credentials, deleting them when the last ref
  238. * is released.
  239. *
  240. * This takes a const pointer to a set of credentials because the credentials
  241. * on task_struct are attached by const pointers to prevent accidental
  242. * alteration of otherwise immutable credential sets.
  243. */
  244. static inline void put_cred(const struct cred *_cred)
  245. {
  246. struct cred *cred = (struct cred *) _cred;
  247. validate_creds(cred);
  248. if (atomic_dec_and_test(&(cred)->usage))
  249. __put_cred(cred);
  250. }
  251. /**
  252. * current_cred - Access the current task's subjective credentials
  253. *
  254. * Access the subjective credentials of the current task. RCU-safe,
  255. * since nobody else can modify it.
  256. */
  257. #define current_cred() \
  258. rcu_dereference_protected(current->cred, 1)
  259. /**
  260. * current_real_cred - Access the current task's objective credentials
  261. *
  262. * Access the objective credentials of the current task. RCU-safe,
  263. * since nobody else can modify it.
  264. */
  265. #define current_real_cred() \
  266. rcu_dereference_protected(current->real_cred, 1)
  267. /**
  268. * __task_cred - Access a task's objective credentials
  269. * @task: The task to query
  270. *
  271. * Access the objective credentials of a task. The caller must hold the RCU
  272. * readlock.
  273. *
  274. * The result of this function should not be passed directly to get_cred();
  275. * rather get_task_cred() should be used instead.
  276. */
  277. #define __task_cred(task) \
  278. rcu_dereference((task)->real_cred)
  279. /**
  280. * get_current_cred - Get the current task's subjective credentials
  281. *
  282. * Get the subjective credentials of the current task, pinning them so that
  283. * they can't go away. Accessing the current task's credentials directly is
  284. * not permitted.
  285. */
  286. #define get_current_cred() \
  287. (get_cred(current_cred()))
  288. /**
  289. * get_current_user - Get the current task's user_struct
  290. *
  291. * Get the user record of the current task, pinning it so that it can't go
  292. * away.
  293. */
  294. #define get_current_user() \
  295. ({ \
  296. struct user_struct *__u; \
  297. const struct cred *__cred; \
  298. __cred = current_cred(); \
  299. __u = get_uid(__cred->user); \
  300. __u; \
  301. })
  302. /**
  303. * get_current_groups - Get the current task's supplementary group list
  304. *
  305. * Get the supplementary group list of the current task, pinning it so that it
  306. * can't go away.
  307. */
  308. #define get_current_groups() \
  309. ({ \
  310. struct group_info *__groups; \
  311. const struct cred *__cred; \
  312. __cred = current_cred(); \
  313. __groups = get_group_info(__cred->group_info); \
  314. __groups; \
  315. })
  316. #define task_cred_xxx(task, xxx) \
  317. ({ \
  318. __typeof__(((struct cred *)NULL)->xxx) ___val; \
  319. rcu_read_lock(); \
  320. ___val = __task_cred((task))->xxx; \
  321. rcu_read_unlock(); \
  322. ___val; \
  323. })
  324. #define task_uid(task) (task_cred_xxx((task), uid))
  325. #define task_euid(task) (task_cred_xxx((task), euid))
  326. #define current_cred_xxx(xxx) \
  327. ({ \
  328. current_cred()->xxx; \
  329. })
  330. #define current_uid() (current_cred_xxx(uid))
  331. #define current_gid() (current_cred_xxx(gid))
  332. #define current_euid() (current_cred_xxx(euid))
  333. #define current_egid() (current_cred_xxx(egid))
  334. #define current_suid() (current_cred_xxx(suid))
  335. #define current_sgid() (current_cred_xxx(sgid))
  336. #define current_fsuid() (current_cred_xxx(fsuid))
  337. #define current_fsgid() (current_cred_xxx(fsgid))
  338. #define current_cap() (current_cred_xxx(cap_effective))
  339. #define current_user() (current_cred_xxx(user))
  340. #define current_security() (current_cred_xxx(security))
  341. extern struct user_namespace init_user_ns;
  342. #ifdef CONFIG_USER_NS
  343. #define current_user_ns() (current_cred_xxx(user_ns))
  344. #else
  345. static inline struct user_namespace *current_user_ns(void)
  346. {
  347. return &init_user_ns;
  348. }
  349. #endif
  350. #define current_uid_gid(_uid, _gid) \
  351. do { \
  352. const struct cred *__cred; \
  353. __cred = current_cred(); \
  354. *(_uid) = __cred->uid; \
  355. *(_gid) = __cred->gid; \
  356. } while(0)
  357. #define current_euid_egid(_euid, _egid) \
  358. do { \
  359. const struct cred *__cred; \
  360. __cred = current_cred(); \
  361. *(_euid) = __cred->euid; \
  362. *(_egid) = __cred->egid; \
  363. } while(0)
  364. #define current_fsuid_fsgid(_fsuid, _fsgid) \
  365. do { \
  366. const struct cred *__cred; \
  367. __cred = current_cred(); \
  368. *(_fsuid) = __cred->fsuid; \
  369. *(_fsgid) = __cred->fsgid; \
  370. } while(0)
  371. #endif /* _LINUX_CRED_H */