credentials.txt 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582
  1. ====================
  2. CREDENTIALS IN LINUX
  3. ====================
  4. By: David Howells <dhowells@redhat.com>
  5. Contents:
  6. (*) Overview.
  7. (*) Types of credentials.
  8. (*) File markings.
  9. (*) Task credentials.
  10. - Immutable credentials.
  11. - Accessing task credentials.
  12. - Accessing another task's credentials.
  13. - Altering credentials.
  14. - Managing credentials.
  15. (*) Open file credentials.
  16. (*) Overriding the VFS's use of credentials.
  17. ========
  18. OVERVIEW
  19. ========
  20. There are several parts to the security check performed by Linux when one
  21. object acts upon another:
  22. (1) Objects.
  23. Objects are things in the system that may be acted upon directly by
  24. userspace programs. Linux has a variety of actionable objects, including:
  25. - Tasks
  26. - Files/inodes
  27. - Sockets
  28. - Message queues
  29. - Shared memory segments
  30. - Semaphores
  31. - Keys
  32. As a part of the description of all these objects there is a set of
  33. credentials. What's in the set depends on the type of object.
  34. (2) Object ownership.
  35. Amongst the credentials of most objects, there will be a subset that
  36. indicates the ownership of that object. This is used for resource
  37. accounting and limitation (disk quotas and task rlimits for example).
  38. In a standard UNIX filesystem, for instance, this will be defined by the
  39. UID marked on the inode.
  40. (3) The objective context.
  41. Also amongst the credentials of those objects, there will be a subset that
  42. indicates the 'objective context' of that object. This may or may not be
  43. the same set as in (2) - in standard UNIX files, for instance, this is the
  44. defined by the UID and the GID marked on the inode.
  45. The objective context is used as part of the security calculation that is
  46. carried out when an object is acted upon.
  47. (4) Subjects.
  48. A subject is an object that is acting upon another object.
  49. Most of the objects in the system are inactive: they don't act on other
  50. objects within the system. Processes/tasks are the obvious exception:
  51. they do stuff; they access and manipulate things.
  52. Objects other than tasks may under some circumstances also be subjects.
  53. For instance an open file may send SIGIO to a task using the UID and EUID
  54. given to it by a task that called fcntl(F_SETOWN) upon it. In this case,
  55. the file struct will have a subjective context too.
  56. (5) The subjective context.
  57. A subject has an additional interpretation of its credentials. A subset
  58. of its credentials forms the 'subjective context'. The subjective context
  59. is used as part of the security calculation that is carried out when a
  60. subject acts.
  61. A Linux task, for example, has the FSUID, FSGID and the supplementary
  62. group list for when it is acting upon a file - which are quite separate
  63. from the real UID and GID that normally form the objective context of the
  64. task.
  65. (6) Actions.
  66. Linux has a number of actions available that a subject may perform upon an
  67. object. The set of actions available depends on the nature of the subject
  68. and the object.
  69. Actions include reading, writing, creating and deleting files; forking or
  70. signalling and tracing tasks.
  71. (7) Rules, access control lists and security calculations.
  72. When a subject acts upon an object, a security calculation is made. This
  73. involves taking the subjective context, the objective context and the
  74. action, and searching one or more sets of rules to see whether the subject
  75. is granted or denied permission to act in the desired manner on the
  76. object, given those contexts.
  77. There are two main sources of rules:
  78. (a) Discretionary access control (DAC):
  79. Sometimes the object will include sets of rules as part of its
  80. description. This is an 'Access Control List' or 'ACL'. A Linux
  81. file may supply more than one ACL.
  82. A traditional UNIX file, for example, includes a permissions mask that
  83. is an abbreviated ACL with three fixed classes of subject ('user',
  84. 'group' and 'other'), each of which may be granted certain privileges
  85. ('read', 'write' and 'execute' - whatever those map to for the object
  86. in question). UNIX file permissions do not allow the arbitrary
  87. specification of subjects, however, and so are of limited use.
  88. A Linux file might also sport a POSIX ACL. This is a list of rules
  89. that grants various permissions to arbitrary subjects.
  90. (b) Mandatory access control (MAC):
  91. The system as a whole may have one or more sets of rules that get
  92. applied to all subjects and objects, regardless of their source.
  93. SELinux and Smack are examples of this.
  94. In the case of SELinux and Smack, each object is given a label as part
  95. of its credentials. When an action is requested, they take the
  96. subject label, the object label and the action and look for a rule
  97. that says that this action is either granted or denied.
  98. ====================
  99. TYPES OF CREDENTIALS
  100. ====================
  101. The Linux kernel supports the following types of credentials:
  102. (1) Traditional UNIX credentials.
  103. Real User ID
  104. Real Group ID
  105. The UID and GID are carried by most, if not all, Linux objects, even if in
  106. some cases it has to be invented (FAT or CIFS files for example, which are
  107. derived from Windows). These (mostly) define the objective context of
  108. that object, with tasks being slightly different in some cases.
  109. Effective, Saved and FS User ID
  110. Effective, Saved and FS Group ID
  111. Supplementary groups
  112. These are additional credentials used by tasks only. Usually, an
  113. EUID/EGID/GROUPS will be used as the subjective context, and real UID/GID
  114. will be used as the objective. For tasks, it should be noted that this is
  115. not always true.
  116. (2) Capabilities.
  117. Set of permitted capabilities
  118. Set of inheritable capabilities
  119. Set of effective capabilities
  120. Capability bounding set
  121. These are only carried by tasks. They indicate superior capabilities
  122. granted piecemeal to a task that an ordinary task wouldn't otherwise have.
  123. These are manipulated implicitly by changes to the traditional UNIX
  124. credentials, but can also be manipulated directly by the capset() system
  125. call.
  126. The permitted capabilities are those caps that the process might grant
  127. itself to its effective or permitted sets through capset(). This
  128. inheritable set might also be so constrained.
  129. The effective capabilities are the ones that a task is actually allowed to
  130. make use of itself.
  131. The inheritable capabilities are the ones that may get passed across
  132. execve().
  133. The bounding set limits the capabilities that may be inherited across
  134. execve(), especially when a binary is executed that will execute as UID 0.
  135. (3) Secure management flags (securebits).
  136. These are only carried by tasks. These govern the way the above
  137. credentials are manipulated and inherited over certain operations such as
  138. execve(). They aren't used directly as objective or subjective
  139. credentials.
  140. (4) Keys and keyrings.
  141. These are only carried by tasks. They carry and cache security tokens
  142. that don't fit into the other standard UNIX credentials. They are for
  143. making such things as network filesystem keys available to the file
  144. accesses performed by processes, without the necessity of ordinary
  145. programs having to know about security details involved.
  146. Keyrings are a special type of key. They carry sets of other keys and can
  147. be searched for the desired key. Each process may subscribe to a number
  148. of keyrings:
  149. Per-thread keying
  150. Per-process keyring
  151. Per-session keyring
  152. When a process accesses a key, if not already present, it will normally be
  153. cached on one of these keyrings for future accesses to find.
  154. For more information on using keys, see Documentation/security/keys.txt.
  155. (5) LSM
  156. The Linux Security Module allows extra controls to be placed over the
  157. operations that a task may do. Currently Linux supports several LSM
  158. options.
  159. Some work by labelling the objects in a system and then applying sets of
  160. rules (policies) that say what operations a task with one label may do to
  161. an object with another label.
  162. (6) AF_KEY
  163. This is a socket-based approach to credential management for networking
  164. stacks [RFC 2367]. It isn't discussed by this document as it doesn't
  165. interact directly with task and file credentials; rather it keeps system
  166. level credentials.
  167. When a file is opened, part of the opening task's subjective context is
  168. recorded in the file struct created. This allows operations using that file
  169. struct to use those credentials instead of the subjective context of the task
  170. that issued the operation. An example of this would be a file opened on a
  171. network filesystem where the credentials of the opened file should be presented
  172. to the server, regardless of who is actually doing a read or a write upon it.
  173. =============
  174. FILE MARKINGS
  175. =============
  176. Files on disk or obtained over the network may have annotations that form the
  177. objective security context of that file. Depending on the type of filesystem,
  178. this may include one or more of the following:
  179. (*) UNIX UID, GID, mode;
  180. (*) Windows user ID;
  181. (*) Access control list;
  182. (*) LSM security label;
  183. (*) UNIX exec privilege escalation bits (SUID/SGID);
  184. (*) File capabilities exec privilege escalation bits.
  185. These are compared to the task's subjective security context, and certain
  186. operations allowed or disallowed as a result. In the case of execve(), the
  187. privilege escalation bits come into play, and may allow the resulting process
  188. extra privileges, based on the annotations on the executable file.
  189. ================
  190. TASK CREDENTIALS
  191. ================
  192. In Linux, all of a task's credentials are held in (uid, gid) or through
  193. (groups, keys, LSM security) a refcounted structure of type 'struct cred'.
  194. Each task points to its credentials by a pointer called 'cred' in its
  195. task_struct.
  196. Once a set of credentials has been prepared and committed, it may not be
  197. changed, barring the following exceptions:
  198. (1) its reference count may be changed;
  199. (2) the reference count on the group_info struct it points to may be changed;
  200. (3) the reference count on the security data it points to may be changed;
  201. (4) the reference count on any keyrings it points to may be changed;
  202. (5) any keyrings it points to may be revoked, expired or have their security
  203. attributes changed; and
  204. (6) the contents of any keyrings to which it points may be changed (the whole
  205. point of keyrings being a shared set of credentials, modifiable by anyone
  206. with appropriate access).
  207. To alter anything in the cred struct, the copy-and-replace principle must be
  208. adhered to. First take a copy, then alter the copy and then use RCU to change
  209. the task pointer to make it point to the new copy. There are wrappers to aid
  210. with this (see below).
  211. A task may only alter its _own_ credentials; it is no longer permitted for a
  212. task to alter another's credentials. This means the capset() system call is no
  213. longer permitted to take any PID other than the one of the current process.
  214. Also keyctl_instantiate() and keyctl_negate() functions no longer permit
  215. attachment to process-specific keyrings in the requesting process as the
  216. instantiating process may need to create them.
  217. IMMUTABLE CREDENTIALS
  218. ---------------------
  219. Once a set of credentials has been made public (by calling commit_creds() for
  220. example), it must be considered immutable, barring two exceptions:
  221. (1) The reference count may be altered.
  222. (2) Whilst the keyring subscriptions of a set of credentials may not be
  223. changed, the keyrings subscribed to may have their contents altered.
  224. To catch accidental credential alteration at compile time, struct task_struct
  225. has _const_ pointers to its credential sets, as does struct file. Furthermore,
  226. certain functions such as get_cred() and put_cred() operate on const pointers,
  227. thus rendering casts unnecessary, but require to temporarily ditch the const
  228. qualification to be able to alter the reference count.
  229. ACCESSING TASK CREDENTIALS
  230. --------------------------
  231. A task being able to alter only its own credentials permits the current process
  232. to read or replace its own credentials without the need for any form of locking
  233. - which simplifies things greatly. It can just call:
  234. const struct cred *current_cred()
  235. to get a pointer to its credentials structure, and it doesn't have to release
  236. it afterwards.
  237. There are convenience wrappers for retrieving specific aspects of a task's
  238. credentials (the value is simply returned in each case):
  239. uid_t current_uid(void) Current's real UID
  240. gid_t current_gid(void) Current's real GID
  241. uid_t current_euid(void) Current's effective UID
  242. gid_t current_egid(void) Current's effective GID
  243. uid_t current_fsuid(void) Current's file access UID
  244. gid_t current_fsgid(void) Current's file access GID
  245. kernel_cap_t current_cap(void) Current's effective capabilities
  246. void *current_security(void) Current's LSM security pointer
  247. struct user_struct *current_user(void) Current's user account
  248. There are also convenience wrappers for retrieving specific associated pairs of
  249. a task's credentials:
  250. void current_uid_gid(uid_t *, gid_t *);
  251. void current_euid_egid(uid_t *, gid_t *);
  252. void current_fsuid_fsgid(uid_t *, gid_t *);
  253. which return these pairs of values through their arguments after retrieving
  254. them from the current task's credentials.
  255. In addition, there is a function for obtaining a reference on the current
  256. process's current set of credentials:
  257. const struct cred *get_current_cred(void);
  258. and functions for getting references to one of the credentials that don't
  259. actually live in struct cred:
  260. struct user_struct *get_current_user(void);
  261. struct group_info *get_current_groups(void);
  262. which get references to the current process's user accounting structure and
  263. supplementary groups list respectively.
  264. Once a reference has been obtained, it must be released with put_cred(),
  265. free_uid() or put_group_info() as appropriate.
  266. ACCESSING ANOTHER TASK'S CREDENTIALS
  267. ------------------------------------
  268. Whilst a task may access its own credentials without the need for locking, the
  269. same is not true of a task wanting to access another task's credentials. It
  270. must use the RCU read lock and rcu_dereference().
  271. The rcu_dereference() is wrapped by:
  272. const struct cred *__task_cred(struct task_struct *task);
  273. This should be used inside the RCU read lock, as in the following example:
  274. void foo(struct task_struct *t, struct foo_data *f)
  275. {
  276. const struct cred *tcred;
  277. ...
  278. rcu_read_lock();
  279. tcred = __task_cred(t);
  280. f->uid = tcred->uid;
  281. f->gid = tcred->gid;
  282. f->groups = get_group_info(tcred->groups);
  283. rcu_read_unlock();
  284. ...
  285. }
  286. Should it be necessary to hold another task's credentials for a long period of
  287. time, and possibly to sleep whilst doing so, then the caller should get a
  288. reference on them using:
  289. const struct cred *get_task_cred(struct task_struct *task);
  290. This does all the RCU magic inside of it. The caller must call put_cred() on
  291. the credentials so obtained when they're finished with.
  292. [*] Note: The result of __task_cred() should not be passed directly to
  293. get_cred() as this may race with commit_cred().
  294. There are a couple of convenience functions to access bits of another task's
  295. credentials, hiding the RCU magic from the caller:
  296. uid_t task_uid(task) Task's real UID
  297. uid_t task_euid(task) Task's effective UID
  298. If the caller is holding the RCU read lock at the time anyway, then:
  299. __task_cred(task)->uid
  300. __task_cred(task)->euid
  301. should be used instead. Similarly, if multiple aspects of a task's credentials
  302. need to be accessed, RCU read lock should be used, __task_cred() called, the
  303. result stored in a temporary pointer and then the credential aspects called
  304. from that before dropping the lock. This prevents the potentially expensive
  305. RCU magic from being invoked multiple times.
  306. Should some other single aspect of another task's credentials need to be
  307. accessed, then this can be used:
  308. task_cred_xxx(task, member)
  309. where 'member' is a non-pointer member of the cred struct. For instance:
  310. uid_t task_cred_xxx(task, suid);
  311. will retrieve 'struct cred::suid' from the task, doing the appropriate RCU
  312. magic. This may not be used for pointer members as what they point to may
  313. disappear the moment the RCU read lock is dropped.
  314. ALTERING CREDENTIALS
  315. --------------------
  316. As previously mentioned, a task may only alter its own credentials, and may not
  317. alter those of another task. This means that it doesn't need to use any
  318. locking to alter its own credentials.
  319. To alter the current process's credentials, a function should first prepare a
  320. new set of credentials by calling:
  321. struct cred *prepare_creds(void);
  322. this locks current->cred_replace_mutex and then allocates and constructs a
  323. duplicate of the current process's credentials, returning with the mutex still
  324. held if successful. It returns NULL if not successful (out of memory).
  325. The mutex prevents ptrace() from altering the ptrace state of a process whilst
  326. security checks on credentials construction and changing is taking place as
  327. the ptrace state may alter the outcome, particularly in the case of execve().
  328. The new credentials set should be altered appropriately, and any security
  329. checks and hooks done. Both the current and the proposed sets of credentials
  330. are available for this purpose as current_cred() will return the current set
  331. still at this point.
  332. When the credential set is ready, it should be committed to the current process
  333. by calling:
  334. int commit_creds(struct cred *new);
  335. This will alter various aspects of the credentials and the process, giving the
  336. LSM a chance to do likewise, then it will use rcu_assign_pointer() to actually
  337. commit the new credentials to current->cred, it will release
  338. current->cred_replace_mutex to allow ptrace() to take place, and it will notify
  339. the scheduler and others of the changes.
  340. This function is guaranteed to return 0, so that it can be tail-called at the
  341. end of such functions as sys_setresuid().
  342. Note that this function consumes the caller's reference to the new credentials.
  343. The caller should _not_ call put_cred() on the new credentials afterwards.
  344. Furthermore, once this function has been called on a new set of credentials,
  345. those credentials may _not_ be changed further.
  346. Should the security checks fail or some other error occur after prepare_creds()
  347. has been called, then the following function should be invoked:
  348. void abort_creds(struct cred *new);
  349. This releases the lock on current->cred_replace_mutex that prepare_creds() got
  350. and then releases the new credentials.
  351. A typical credentials alteration function would look something like this:
  352. int alter_suid(uid_t suid)
  353. {
  354. struct cred *new;
  355. int ret;
  356. new = prepare_creds();
  357. if (!new)
  358. return -ENOMEM;
  359. new->suid = suid;
  360. ret = security_alter_suid(new);
  361. if (ret < 0) {
  362. abort_creds(new);
  363. return ret;
  364. }
  365. return commit_creds(new);
  366. }
  367. MANAGING CREDENTIALS
  368. --------------------
  369. There are some functions to help manage credentials:
  370. (*) void put_cred(const struct cred *cred);
  371. This releases a reference to the given set of credentials. If the
  372. reference count reaches zero, the credentials will be scheduled for
  373. destruction by the RCU system.
  374. (*) const struct cred *get_cred(const struct cred *cred);
  375. This gets a reference on a live set of credentials, returning a pointer to
  376. that set of credentials.
  377. (*) struct cred *get_new_cred(struct cred *cred);
  378. This gets a reference on a set of credentials that is under construction
  379. and is thus still mutable, returning a pointer to that set of credentials.
  380. =====================
  381. OPEN FILE CREDENTIALS
  382. =====================
  383. When a new file is opened, a reference is obtained on the opening task's
  384. credentials and this is attached to the file struct as 'f_cred' in place of
  385. 'f_uid' and 'f_gid'. Code that used to access file->f_uid and file->f_gid
  386. should now access file->f_cred->fsuid and file->f_cred->fsgid.
  387. It is safe to access f_cred without the use of RCU or locking because the
  388. pointer will not change over the lifetime of the file struct, and nor will the
  389. contents of the cred struct pointed to, barring the exceptions listed above
  390. (see the Task Credentials section).
  391. =======================================
  392. OVERRIDING THE VFS'S USE OF CREDENTIALS
  393. =======================================
  394. Under some circumstances it is desirable to override the credentials used by
  395. the VFS, and that can be done by calling into such as vfs_mkdir() with a
  396. different set of credentials. This is done in the following places:
  397. (*) sys_faccessat().
  398. (*) do_coredump().
  399. (*) nfs4recover.c.