162306a36Sopenharmony_ci==================== 262306a36Sopenharmony_ciCredentials in Linux 362306a36Sopenharmony_ci==================== 462306a36Sopenharmony_ci 562306a36Sopenharmony_ciBy: David Howells <dhowells@redhat.com> 662306a36Sopenharmony_ci 762306a36Sopenharmony_ci.. contents:: :local: 862306a36Sopenharmony_ci 962306a36Sopenharmony_ciOverview 1062306a36Sopenharmony_ci======== 1162306a36Sopenharmony_ci 1262306a36Sopenharmony_ciThere are several parts to the security check performed by Linux when one 1362306a36Sopenharmony_ciobject acts upon another: 1462306a36Sopenharmony_ci 1562306a36Sopenharmony_ci 1. Objects. 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci Objects are things in the system that may be acted upon directly by 1862306a36Sopenharmony_ci userspace programs. Linux has a variety of actionable objects, including: 1962306a36Sopenharmony_ci 2062306a36Sopenharmony_ci - Tasks 2162306a36Sopenharmony_ci - Files/inodes 2262306a36Sopenharmony_ci - Sockets 2362306a36Sopenharmony_ci - Message queues 2462306a36Sopenharmony_ci - Shared memory segments 2562306a36Sopenharmony_ci - Semaphores 2662306a36Sopenharmony_ci - Keys 2762306a36Sopenharmony_ci 2862306a36Sopenharmony_ci As a part of the description of all these objects there is a set of 2962306a36Sopenharmony_ci credentials. What's in the set depends on the type of object. 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_ci 2. Object ownership. 3262306a36Sopenharmony_ci 3362306a36Sopenharmony_ci Amongst the credentials of most objects, there will be a subset that 3462306a36Sopenharmony_ci indicates the ownership of that object. This is used for resource 3562306a36Sopenharmony_ci accounting and limitation (disk quotas and task rlimits for example). 3662306a36Sopenharmony_ci 3762306a36Sopenharmony_ci In a standard UNIX filesystem, for instance, this will be defined by the 3862306a36Sopenharmony_ci UID marked on the inode. 3962306a36Sopenharmony_ci 4062306a36Sopenharmony_ci 3. The objective context. 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_ci Also amongst the credentials of those objects, there will be a subset that 4362306a36Sopenharmony_ci indicates the 'objective context' of that object. This may or may not be 4462306a36Sopenharmony_ci the same set as in (2) - in standard UNIX files, for instance, this is the 4562306a36Sopenharmony_ci defined by the UID and the GID marked on the inode. 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci The objective context is used as part of the security calculation that is 4862306a36Sopenharmony_ci carried out when an object is acted upon. 4962306a36Sopenharmony_ci 5062306a36Sopenharmony_ci 4. Subjects. 5162306a36Sopenharmony_ci 5262306a36Sopenharmony_ci A subject is an object that is acting upon another object. 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci Most of the objects in the system are inactive: they don't act on other 5562306a36Sopenharmony_ci objects within the system. Processes/tasks are the obvious exception: 5662306a36Sopenharmony_ci they do stuff; they access and manipulate things. 5762306a36Sopenharmony_ci 5862306a36Sopenharmony_ci Objects other than tasks may under some circumstances also be subjects. 5962306a36Sopenharmony_ci For instance an open file may send SIGIO to a task using the UID and EUID 6062306a36Sopenharmony_ci given to it by a task that called ``fcntl(F_SETOWN)`` upon it. In this case, 6162306a36Sopenharmony_ci the file struct will have a subjective context too. 6262306a36Sopenharmony_ci 6362306a36Sopenharmony_ci 5. The subjective context. 6462306a36Sopenharmony_ci 6562306a36Sopenharmony_ci A subject has an additional interpretation of its credentials. A subset 6662306a36Sopenharmony_ci of its credentials forms the 'subjective context'. The subjective context 6762306a36Sopenharmony_ci is used as part of the security calculation that is carried out when a 6862306a36Sopenharmony_ci subject acts. 6962306a36Sopenharmony_ci 7062306a36Sopenharmony_ci A Linux task, for example, has the FSUID, FSGID and the supplementary 7162306a36Sopenharmony_ci group list for when it is acting upon a file - which are quite separate 7262306a36Sopenharmony_ci from the real UID and GID that normally form the objective context of the 7362306a36Sopenharmony_ci task. 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci 6. Actions. 7662306a36Sopenharmony_ci 7762306a36Sopenharmony_ci Linux has a number of actions available that a subject may perform upon an 7862306a36Sopenharmony_ci object. The set of actions available depends on the nature of the subject 7962306a36Sopenharmony_ci and the object. 8062306a36Sopenharmony_ci 8162306a36Sopenharmony_ci Actions include reading, writing, creating and deleting files; forking or 8262306a36Sopenharmony_ci signalling and tracing tasks. 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci 7. Rules, access control lists and security calculations. 8562306a36Sopenharmony_ci 8662306a36Sopenharmony_ci When a subject acts upon an object, a security calculation is made. This 8762306a36Sopenharmony_ci involves taking the subjective context, the objective context and the 8862306a36Sopenharmony_ci action, and searching one or more sets of rules to see whether the subject 8962306a36Sopenharmony_ci is granted or denied permission to act in the desired manner on the 9062306a36Sopenharmony_ci object, given those contexts. 9162306a36Sopenharmony_ci 9262306a36Sopenharmony_ci There are two main sources of rules: 9362306a36Sopenharmony_ci 9462306a36Sopenharmony_ci a. Discretionary access control (DAC): 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci Sometimes the object will include sets of rules as part of its 9762306a36Sopenharmony_ci description. This is an 'Access Control List' or 'ACL'. A Linux 9862306a36Sopenharmony_ci file may supply more than one ACL. 9962306a36Sopenharmony_ci 10062306a36Sopenharmony_ci A traditional UNIX file, for example, includes a permissions mask that 10162306a36Sopenharmony_ci is an abbreviated ACL with three fixed classes of subject ('user', 10262306a36Sopenharmony_ci 'group' and 'other'), each of which may be granted certain privileges 10362306a36Sopenharmony_ci ('read', 'write' and 'execute' - whatever those map to for the object 10462306a36Sopenharmony_ci in question). UNIX file permissions do not allow the arbitrary 10562306a36Sopenharmony_ci specification of subjects, however, and so are of limited use. 10662306a36Sopenharmony_ci 10762306a36Sopenharmony_ci A Linux file might also sport a POSIX ACL. This is a list of rules 10862306a36Sopenharmony_ci that grants various permissions to arbitrary subjects. 10962306a36Sopenharmony_ci 11062306a36Sopenharmony_ci b. Mandatory access control (MAC): 11162306a36Sopenharmony_ci 11262306a36Sopenharmony_ci The system as a whole may have one or more sets of rules that get 11362306a36Sopenharmony_ci applied to all subjects and objects, regardless of their source. 11462306a36Sopenharmony_ci SELinux and Smack are examples of this. 11562306a36Sopenharmony_ci 11662306a36Sopenharmony_ci In the case of SELinux and Smack, each object is given a label as part 11762306a36Sopenharmony_ci of its credentials. When an action is requested, they take the 11862306a36Sopenharmony_ci subject label, the object label and the action and look for a rule 11962306a36Sopenharmony_ci that says that this action is either granted or denied. 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci 12262306a36Sopenharmony_ciTypes of Credentials 12362306a36Sopenharmony_ci==================== 12462306a36Sopenharmony_ci 12562306a36Sopenharmony_ciThe Linux kernel supports the following types of credentials: 12662306a36Sopenharmony_ci 12762306a36Sopenharmony_ci 1. Traditional UNIX credentials. 12862306a36Sopenharmony_ci 12962306a36Sopenharmony_ci - Real User ID 13062306a36Sopenharmony_ci - Real Group ID 13162306a36Sopenharmony_ci 13262306a36Sopenharmony_ci The UID and GID are carried by most, if not all, Linux objects, even if in 13362306a36Sopenharmony_ci some cases it has to be invented (FAT or CIFS files for example, which are 13462306a36Sopenharmony_ci derived from Windows). These (mostly) define the objective context of 13562306a36Sopenharmony_ci that object, with tasks being slightly different in some cases. 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci - Effective, Saved and FS User ID 13862306a36Sopenharmony_ci - Effective, Saved and FS Group ID 13962306a36Sopenharmony_ci - Supplementary groups 14062306a36Sopenharmony_ci 14162306a36Sopenharmony_ci These are additional credentials used by tasks only. Usually, an 14262306a36Sopenharmony_ci EUID/EGID/GROUPS will be used as the subjective context, and real UID/GID 14362306a36Sopenharmony_ci will be used as the objective. For tasks, it should be noted that this is 14462306a36Sopenharmony_ci not always true. 14562306a36Sopenharmony_ci 14662306a36Sopenharmony_ci 2. Capabilities. 14762306a36Sopenharmony_ci 14862306a36Sopenharmony_ci - Set of permitted capabilities 14962306a36Sopenharmony_ci - Set of inheritable capabilities 15062306a36Sopenharmony_ci - Set of effective capabilities 15162306a36Sopenharmony_ci - Capability bounding set 15262306a36Sopenharmony_ci 15362306a36Sopenharmony_ci These are only carried by tasks. They indicate superior capabilities 15462306a36Sopenharmony_ci granted piecemeal to a task that an ordinary task wouldn't otherwise have. 15562306a36Sopenharmony_ci These are manipulated implicitly by changes to the traditional UNIX 15662306a36Sopenharmony_ci credentials, but can also be manipulated directly by the ``capset()`` 15762306a36Sopenharmony_ci system call. 15862306a36Sopenharmony_ci 15962306a36Sopenharmony_ci The permitted capabilities are those caps that the process might grant 16062306a36Sopenharmony_ci itself to its effective or permitted sets through ``capset()``. This 16162306a36Sopenharmony_ci inheritable set might also be so constrained. 16262306a36Sopenharmony_ci 16362306a36Sopenharmony_ci The effective capabilities are the ones that a task is actually allowed to 16462306a36Sopenharmony_ci make use of itself. 16562306a36Sopenharmony_ci 16662306a36Sopenharmony_ci The inheritable capabilities are the ones that may get passed across 16762306a36Sopenharmony_ci ``execve()``. 16862306a36Sopenharmony_ci 16962306a36Sopenharmony_ci The bounding set limits the capabilities that may be inherited across 17062306a36Sopenharmony_ci ``execve()``, especially when a binary is executed that will execute as 17162306a36Sopenharmony_ci UID 0. 17262306a36Sopenharmony_ci 17362306a36Sopenharmony_ci 3. Secure management flags (securebits). 17462306a36Sopenharmony_ci 17562306a36Sopenharmony_ci These are only carried by tasks. These govern the way the above 17662306a36Sopenharmony_ci credentials are manipulated and inherited over certain operations such as 17762306a36Sopenharmony_ci execve(). They aren't used directly as objective or subjective 17862306a36Sopenharmony_ci credentials. 17962306a36Sopenharmony_ci 18062306a36Sopenharmony_ci 4. Keys and keyrings. 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci These are only carried by tasks. They carry and cache security tokens 18362306a36Sopenharmony_ci that don't fit into the other standard UNIX credentials. They are for 18462306a36Sopenharmony_ci making such things as network filesystem keys available to the file 18562306a36Sopenharmony_ci accesses performed by processes, without the necessity of ordinary 18662306a36Sopenharmony_ci programs having to know about security details involved. 18762306a36Sopenharmony_ci 18862306a36Sopenharmony_ci Keyrings are a special type of key. They carry sets of other keys and can 18962306a36Sopenharmony_ci be searched for the desired key. Each process may subscribe to a number 19062306a36Sopenharmony_ci of keyrings: 19162306a36Sopenharmony_ci 19262306a36Sopenharmony_ci Per-thread keying 19362306a36Sopenharmony_ci Per-process keyring 19462306a36Sopenharmony_ci Per-session keyring 19562306a36Sopenharmony_ci 19662306a36Sopenharmony_ci When a process accesses a key, if not already present, it will normally be 19762306a36Sopenharmony_ci cached on one of these keyrings for future accesses to find. 19862306a36Sopenharmony_ci 19962306a36Sopenharmony_ci For more information on using keys, see ``Documentation/security/keys/*``. 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci 5. LSM 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci The Linux Security Module allows extra controls to be placed over the 20462306a36Sopenharmony_ci operations that a task may do. Currently Linux supports several LSM 20562306a36Sopenharmony_ci options. 20662306a36Sopenharmony_ci 20762306a36Sopenharmony_ci Some work by labelling the objects in a system and then applying sets of 20862306a36Sopenharmony_ci rules (policies) that say what operations a task with one label may do to 20962306a36Sopenharmony_ci an object with another label. 21062306a36Sopenharmony_ci 21162306a36Sopenharmony_ci 6. AF_KEY 21262306a36Sopenharmony_ci 21362306a36Sopenharmony_ci This is a socket-based approach to credential management for networking 21462306a36Sopenharmony_ci stacks [RFC 2367]. It isn't discussed by this document as it doesn't 21562306a36Sopenharmony_ci interact directly with task and file credentials; rather it keeps system 21662306a36Sopenharmony_ci level credentials. 21762306a36Sopenharmony_ci 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_ciWhen a file is opened, part of the opening task's subjective context is 22062306a36Sopenharmony_cirecorded in the file struct created. This allows operations using that file 22162306a36Sopenharmony_cistruct to use those credentials instead of the subjective context of the task 22262306a36Sopenharmony_cithat issued the operation. An example of this would be a file opened on a 22362306a36Sopenharmony_cinetwork filesystem where the credentials of the opened file should be presented 22462306a36Sopenharmony_cito the server, regardless of who is actually doing a read or a write upon it. 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci 22762306a36Sopenharmony_ciFile Markings 22862306a36Sopenharmony_ci============= 22962306a36Sopenharmony_ci 23062306a36Sopenharmony_ciFiles on disk or obtained over the network may have annotations that form the 23162306a36Sopenharmony_ciobjective security context of that file. Depending on the type of filesystem, 23262306a36Sopenharmony_cithis may include one or more of the following: 23362306a36Sopenharmony_ci 23462306a36Sopenharmony_ci * UNIX UID, GID, mode; 23562306a36Sopenharmony_ci * Windows user ID; 23662306a36Sopenharmony_ci * Access control list; 23762306a36Sopenharmony_ci * LSM security label; 23862306a36Sopenharmony_ci * UNIX exec privilege escalation bits (SUID/SGID); 23962306a36Sopenharmony_ci * File capabilities exec privilege escalation bits. 24062306a36Sopenharmony_ci 24162306a36Sopenharmony_ciThese are compared to the task's subjective security context, and certain 24262306a36Sopenharmony_cioperations allowed or disallowed as a result. In the case of execve(), the 24362306a36Sopenharmony_ciprivilege escalation bits come into play, and may allow the resulting process 24462306a36Sopenharmony_ciextra privileges, based on the annotations on the executable file. 24562306a36Sopenharmony_ci 24662306a36Sopenharmony_ci 24762306a36Sopenharmony_ciTask Credentials 24862306a36Sopenharmony_ci================ 24962306a36Sopenharmony_ci 25062306a36Sopenharmony_ciIn Linux, all of a task's credentials are held in (uid, gid) or through 25162306a36Sopenharmony_ci(groups, keys, LSM security) a refcounted structure of type 'struct cred'. 25262306a36Sopenharmony_ciEach task points to its credentials by a pointer called 'cred' in its 25362306a36Sopenharmony_citask_struct. 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_ciOnce a set of credentials has been prepared and committed, it may not be 25662306a36Sopenharmony_cichanged, barring the following exceptions: 25762306a36Sopenharmony_ci 25862306a36Sopenharmony_ci 1. its reference count may be changed; 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci 2. the reference count on the group_info struct it points to may be changed; 26162306a36Sopenharmony_ci 26262306a36Sopenharmony_ci 3. the reference count on the security data it points to may be changed; 26362306a36Sopenharmony_ci 26462306a36Sopenharmony_ci 4. the reference count on any keyrings it points to may be changed; 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci 5. any keyrings it points to may be revoked, expired or have their security 26762306a36Sopenharmony_ci attributes changed; and 26862306a36Sopenharmony_ci 26962306a36Sopenharmony_ci 6. the contents of any keyrings to which it points may be changed (the whole 27062306a36Sopenharmony_ci point of keyrings being a shared set of credentials, modifiable by anyone 27162306a36Sopenharmony_ci with appropriate access). 27262306a36Sopenharmony_ci 27362306a36Sopenharmony_ciTo alter anything in the cred struct, the copy-and-replace principle must be 27462306a36Sopenharmony_ciadhered to. First take a copy, then alter the copy and then use RCU to change 27562306a36Sopenharmony_cithe task pointer to make it point to the new copy. There are wrappers to aid 27662306a36Sopenharmony_ciwith this (see below). 27762306a36Sopenharmony_ci 27862306a36Sopenharmony_ciA task may only alter its _own_ credentials; it is no longer permitted for a 27962306a36Sopenharmony_citask to alter another's credentials. This means the ``capset()`` system call 28062306a36Sopenharmony_ciis no longer permitted to take any PID other than the one of the current 28162306a36Sopenharmony_ciprocess. Also ``keyctl_instantiate()`` and ``keyctl_negate()`` functions no 28262306a36Sopenharmony_cilonger permit attachment to process-specific keyrings in the requesting 28362306a36Sopenharmony_ciprocess as the instantiating process may need to create them. 28462306a36Sopenharmony_ci 28562306a36Sopenharmony_ci 28662306a36Sopenharmony_ciImmutable Credentials 28762306a36Sopenharmony_ci--------------------- 28862306a36Sopenharmony_ci 28962306a36Sopenharmony_ciOnce a set of credentials has been made public (by calling ``commit_creds()`` 29062306a36Sopenharmony_cifor example), it must be considered immutable, barring two exceptions: 29162306a36Sopenharmony_ci 29262306a36Sopenharmony_ci 1. The reference count may be altered. 29362306a36Sopenharmony_ci 29462306a36Sopenharmony_ci 2. While the keyring subscriptions of a set of credentials may not be 29562306a36Sopenharmony_ci changed, the keyrings subscribed to may have their contents altered. 29662306a36Sopenharmony_ci 29762306a36Sopenharmony_ciTo catch accidental credential alteration at compile time, struct task_struct 29862306a36Sopenharmony_cihas _const_ pointers to its credential sets, as does struct file. Furthermore, 29962306a36Sopenharmony_cicertain functions such as ``get_cred()`` and ``put_cred()`` operate on const 30062306a36Sopenharmony_cipointers, thus rendering casts unnecessary, but require to temporarily ditch 30162306a36Sopenharmony_cithe const qualification to be able to alter the reference count. 30262306a36Sopenharmony_ci 30362306a36Sopenharmony_ci 30462306a36Sopenharmony_ciAccessing Task Credentials 30562306a36Sopenharmony_ci-------------------------- 30662306a36Sopenharmony_ci 30762306a36Sopenharmony_ciA task being able to alter only its own credentials permits the current process 30862306a36Sopenharmony_cito read or replace its own credentials without the need for any form of locking 30962306a36Sopenharmony_ci-- which simplifies things greatly. It can just call:: 31062306a36Sopenharmony_ci 31162306a36Sopenharmony_ci const struct cred *current_cred() 31262306a36Sopenharmony_ci 31362306a36Sopenharmony_cito get a pointer to its credentials structure, and it doesn't have to release 31462306a36Sopenharmony_ciit afterwards. 31562306a36Sopenharmony_ci 31662306a36Sopenharmony_ciThere are convenience wrappers for retrieving specific aspects of a task's 31762306a36Sopenharmony_cicredentials (the value is simply returned in each case):: 31862306a36Sopenharmony_ci 31962306a36Sopenharmony_ci uid_t current_uid(void) Current's real UID 32062306a36Sopenharmony_ci gid_t current_gid(void) Current's real GID 32162306a36Sopenharmony_ci uid_t current_euid(void) Current's effective UID 32262306a36Sopenharmony_ci gid_t current_egid(void) Current's effective GID 32362306a36Sopenharmony_ci uid_t current_fsuid(void) Current's file access UID 32462306a36Sopenharmony_ci gid_t current_fsgid(void) Current's file access GID 32562306a36Sopenharmony_ci kernel_cap_t current_cap(void) Current's effective capabilities 32662306a36Sopenharmony_ci struct user_struct *current_user(void) Current's user account 32762306a36Sopenharmony_ci 32862306a36Sopenharmony_ciThere are also convenience wrappers for retrieving specific associated pairs of 32962306a36Sopenharmony_cia task's credentials:: 33062306a36Sopenharmony_ci 33162306a36Sopenharmony_ci void current_uid_gid(uid_t *, gid_t *); 33262306a36Sopenharmony_ci void current_euid_egid(uid_t *, gid_t *); 33362306a36Sopenharmony_ci void current_fsuid_fsgid(uid_t *, gid_t *); 33462306a36Sopenharmony_ci 33562306a36Sopenharmony_ciwhich return these pairs of values through their arguments after retrieving 33662306a36Sopenharmony_cithem from the current task's credentials. 33762306a36Sopenharmony_ci 33862306a36Sopenharmony_ci 33962306a36Sopenharmony_ciIn addition, there is a function for obtaining a reference on the current 34062306a36Sopenharmony_ciprocess's current set of credentials:: 34162306a36Sopenharmony_ci 34262306a36Sopenharmony_ci const struct cred *get_current_cred(void); 34362306a36Sopenharmony_ci 34462306a36Sopenharmony_ciand functions for getting references to one of the credentials that don't 34562306a36Sopenharmony_ciactually live in struct cred:: 34662306a36Sopenharmony_ci 34762306a36Sopenharmony_ci struct user_struct *get_current_user(void); 34862306a36Sopenharmony_ci struct group_info *get_current_groups(void); 34962306a36Sopenharmony_ci 35062306a36Sopenharmony_ciwhich get references to the current process's user accounting structure and 35162306a36Sopenharmony_cisupplementary groups list respectively. 35262306a36Sopenharmony_ci 35362306a36Sopenharmony_ciOnce a reference has been obtained, it must be released with ``put_cred()``, 35462306a36Sopenharmony_ci``free_uid()`` or ``put_group_info()`` as appropriate. 35562306a36Sopenharmony_ci 35662306a36Sopenharmony_ci 35762306a36Sopenharmony_ciAccessing Another Task's Credentials 35862306a36Sopenharmony_ci------------------------------------ 35962306a36Sopenharmony_ci 36062306a36Sopenharmony_ciWhile a task may access its own credentials without the need for locking, the 36162306a36Sopenharmony_cisame is not true of a task wanting to access another task's credentials. It 36262306a36Sopenharmony_cimust use the RCU read lock and ``rcu_dereference()``. 36362306a36Sopenharmony_ci 36462306a36Sopenharmony_ciThe ``rcu_dereference()`` is wrapped by:: 36562306a36Sopenharmony_ci 36662306a36Sopenharmony_ci const struct cred *__task_cred(struct task_struct *task); 36762306a36Sopenharmony_ci 36862306a36Sopenharmony_ciThis should be used inside the RCU read lock, as in the following example:: 36962306a36Sopenharmony_ci 37062306a36Sopenharmony_ci void foo(struct task_struct *t, struct foo_data *f) 37162306a36Sopenharmony_ci { 37262306a36Sopenharmony_ci const struct cred *tcred; 37362306a36Sopenharmony_ci ... 37462306a36Sopenharmony_ci rcu_read_lock(); 37562306a36Sopenharmony_ci tcred = __task_cred(t); 37662306a36Sopenharmony_ci f->uid = tcred->uid; 37762306a36Sopenharmony_ci f->gid = tcred->gid; 37862306a36Sopenharmony_ci f->groups = get_group_info(tcred->groups); 37962306a36Sopenharmony_ci rcu_read_unlock(); 38062306a36Sopenharmony_ci ... 38162306a36Sopenharmony_ci } 38262306a36Sopenharmony_ci 38362306a36Sopenharmony_ciShould it be necessary to hold another task's credentials for a long period of 38462306a36Sopenharmony_citime, and possibly to sleep while doing so, then the caller should get a 38562306a36Sopenharmony_cireference on them using:: 38662306a36Sopenharmony_ci 38762306a36Sopenharmony_ci const struct cred *get_task_cred(struct task_struct *task); 38862306a36Sopenharmony_ci 38962306a36Sopenharmony_ciThis does all the RCU magic inside of it. The caller must call put_cred() on 39062306a36Sopenharmony_cithe credentials so obtained when they're finished with. 39162306a36Sopenharmony_ci 39262306a36Sopenharmony_ci.. note:: 39362306a36Sopenharmony_ci The result of ``__task_cred()`` should not be passed directly to 39462306a36Sopenharmony_ci ``get_cred()`` as this may race with ``commit_cred()``. 39562306a36Sopenharmony_ci 39662306a36Sopenharmony_ciThere are a couple of convenience functions to access bits of another task's 39762306a36Sopenharmony_cicredentials, hiding the RCU magic from the caller:: 39862306a36Sopenharmony_ci 39962306a36Sopenharmony_ci uid_t task_uid(task) Task's real UID 40062306a36Sopenharmony_ci uid_t task_euid(task) Task's effective UID 40162306a36Sopenharmony_ci 40262306a36Sopenharmony_ciIf the caller is holding the RCU read lock at the time anyway, then:: 40362306a36Sopenharmony_ci 40462306a36Sopenharmony_ci __task_cred(task)->uid 40562306a36Sopenharmony_ci __task_cred(task)->euid 40662306a36Sopenharmony_ci 40762306a36Sopenharmony_cishould be used instead. Similarly, if multiple aspects of a task's credentials 40862306a36Sopenharmony_cineed to be accessed, RCU read lock should be used, ``__task_cred()`` called, 40962306a36Sopenharmony_cithe result stored in a temporary pointer and then the credential aspects called 41062306a36Sopenharmony_cifrom that before dropping the lock. This prevents the potentially expensive 41162306a36Sopenharmony_ciRCU magic from being invoked multiple times. 41262306a36Sopenharmony_ci 41362306a36Sopenharmony_ciShould some other single aspect of another task's credentials need to be 41462306a36Sopenharmony_ciaccessed, then this can be used:: 41562306a36Sopenharmony_ci 41662306a36Sopenharmony_ci task_cred_xxx(task, member) 41762306a36Sopenharmony_ci 41862306a36Sopenharmony_ciwhere 'member' is a non-pointer member of the cred struct. For instance:: 41962306a36Sopenharmony_ci 42062306a36Sopenharmony_ci uid_t task_cred_xxx(task, suid); 42162306a36Sopenharmony_ci 42262306a36Sopenharmony_ciwill retrieve 'struct cred::suid' from the task, doing the appropriate RCU 42362306a36Sopenharmony_cimagic. This may not be used for pointer members as what they point to may 42462306a36Sopenharmony_cidisappear the moment the RCU read lock is dropped. 42562306a36Sopenharmony_ci 42662306a36Sopenharmony_ci 42762306a36Sopenharmony_ciAltering Credentials 42862306a36Sopenharmony_ci-------------------- 42962306a36Sopenharmony_ci 43062306a36Sopenharmony_ciAs previously mentioned, a task may only alter its own credentials, and may not 43162306a36Sopenharmony_cialter those of another task. This means that it doesn't need to use any 43262306a36Sopenharmony_cilocking to alter its own credentials. 43362306a36Sopenharmony_ci 43462306a36Sopenharmony_ciTo alter the current process's credentials, a function should first prepare a 43562306a36Sopenharmony_cinew set of credentials by calling:: 43662306a36Sopenharmony_ci 43762306a36Sopenharmony_ci struct cred *prepare_creds(void); 43862306a36Sopenharmony_ci 43962306a36Sopenharmony_cithis locks current->cred_replace_mutex and then allocates and constructs a 44062306a36Sopenharmony_ciduplicate of the current process's credentials, returning with the mutex still 44162306a36Sopenharmony_ciheld if successful. It returns NULL if not successful (out of memory). 44262306a36Sopenharmony_ci 44362306a36Sopenharmony_ciThe mutex prevents ``ptrace()`` from altering the ptrace state of a process 44462306a36Sopenharmony_ciwhile security checks on credentials construction and changing is taking place 44562306a36Sopenharmony_cias the ptrace state may alter the outcome, particularly in the case of 44662306a36Sopenharmony_ci``execve()``. 44762306a36Sopenharmony_ci 44862306a36Sopenharmony_ciThe new credentials set should be altered appropriately, and any security 44962306a36Sopenharmony_cichecks and hooks done. Both the current and the proposed sets of credentials 45062306a36Sopenharmony_ciare available for this purpose as current_cred() will return the current set 45162306a36Sopenharmony_cistill at this point. 45262306a36Sopenharmony_ci 45362306a36Sopenharmony_ciWhen replacing the group list, the new list must be sorted before it 45462306a36Sopenharmony_ciis added to the credential, as a binary search is used to test for 45562306a36Sopenharmony_cimembership. In practice, this means groups_sort() should be 45662306a36Sopenharmony_cicalled before set_groups() or set_current_groups(). 45762306a36Sopenharmony_cigroups_sort() must not be called on a ``struct group_list`` which 45862306a36Sopenharmony_ciis shared as it may permute elements as part of the sorting process 45962306a36Sopenharmony_cieven if the array is already sorted. 46062306a36Sopenharmony_ci 46162306a36Sopenharmony_ciWhen the credential set is ready, it should be committed to the current process 46262306a36Sopenharmony_ciby calling:: 46362306a36Sopenharmony_ci 46462306a36Sopenharmony_ci int commit_creds(struct cred *new); 46562306a36Sopenharmony_ci 46662306a36Sopenharmony_ciThis will alter various aspects of the credentials and the process, giving the 46762306a36Sopenharmony_ciLSM a chance to do likewise, then it will use ``rcu_assign_pointer()`` to 46862306a36Sopenharmony_ciactually commit the new credentials to ``current->cred``, it will release 46962306a36Sopenharmony_ci``current->cred_replace_mutex`` to allow ``ptrace()`` to take place, and it 47062306a36Sopenharmony_ciwill notify the scheduler and others of the changes. 47162306a36Sopenharmony_ci 47262306a36Sopenharmony_ciThis function is guaranteed to return 0, so that it can be tail-called at the 47362306a36Sopenharmony_ciend of such functions as ``sys_setresuid()``. 47462306a36Sopenharmony_ci 47562306a36Sopenharmony_ciNote that this function consumes the caller's reference to the new credentials. 47662306a36Sopenharmony_ciThe caller should _not_ call ``put_cred()`` on the new credentials afterwards. 47762306a36Sopenharmony_ci 47862306a36Sopenharmony_ciFurthermore, once this function has been called on a new set of credentials, 47962306a36Sopenharmony_cithose credentials may _not_ be changed further. 48062306a36Sopenharmony_ci 48162306a36Sopenharmony_ci 48262306a36Sopenharmony_ciShould the security checks fail or some other error occur after 48362306a36Sopenharmony_ci``prepare_creds()`` has been called, then the following function should be 48462306a36Sopenharmony_ciinvoked:: 48562306a36Sopenharmony_ci 48662306a36Sopenharmony_ci void abort_creds(struct cred *new); 48762306a36Sopenharmony_ci 48862306a36Sopenharmony_ciThis releases the lock on ``current->cred_replace_mutex`` that 48962306a36Sopenharmony_ci``prepare_creds()`` got and then releases the new credentials. 49062306a36Sopenharmony_ci 49162306a36Sopenharmony_ci 49262306a36Sopenharmony_ciA typical credentials alteration function would look something like this:: 49362306a36Sopenharmony_ci 49462306a36Sopenharmony_ci int alter_suid(uid_t suid) 49562306a36Sopenharmony_ci { 49662306a36Sopenharmony_ci struct cred *new; 49762306a36Sopenharmony_ci int ret; 49862306a36Sopenharmony_ci 49962306a36Sopenharmony_ci new = prepare_creds(); 50062306a36Sopenharmony_ci if (!new) 50162306a36Sopenharmony_ci return -ENOMEM; 50262306a36Sopenharmony_ci 50362306a36Sopenharmony_ci new->suid = suid; 50462306a36Sopenharmony_ci ret = security_alter_suid(new); 50562306a36Sopenharmony_ci if (ret < 0) { 50662306a36Sopenharmony_ci abort_creds(new); 50762306a36Sopenharmony_ci return ret; 50862306a36Sopenharmony_ci } 50962306a36Sopenharmony_ci 51062306a36Sopenharmony_ci return commit_creds(new); 51162306a36Sopenharmony_ci } 51262306a36Sopenharmony_ci 51362306a36Sopenharmony_ci 51462306a36Sopenharmony_ciManaging Credentials 51562306a36Sopenharmony_ci-------------------- 51662306a36Sopenharmony_ci 51762306a36Sopenharmony_ciThere are some functions to help manage credentials: 51862306a36Sopenharmony_ci 51962306a36Sopenharmony_ci - ``void put_cred(const struct cred *cred);`` 52062306a36Sopenharmony_ci 52162306a36Sopenharmony_ci This releases a reference to the given set of credentials. If the 52262306a36Sopenharmony_ci reference count reaches zero, the credentials will be scheduled for 52362306a36Sopenharmony_ci destruction by the RCU system. 52462306a36Sopenharmony_ci 52562306a36Sopenharmony_ci - ``const struct cred *get_cred(const struct cred *cred);`` 52662306a36Sopenharmony_ci 52762306a36Sopenharmony_ci This gets a reference on a live set of credentials, returning a pointer to 52862306a36Sopenharmony_ci that set of credentials. 52962306a36Sopenharmony_ci 53062306a36Sopenharmony_ci - ``struct cred *get_new_cred(struct cred *cred);`` 53162306a36Sopenharmony_ci 53262306a36Sopenharmony_ci This gets a reference on a set of credentials that is under construction 53362306a36Sopenharmony_ci and is thus still mutable, returning a pointer to that set of credentials. 53462306a36Sopenharmony_ci 53562306a36Sopenharmony_ci 53662306a36Sopenharmony_ciOpen File Credentials 53762306a36Sopenharmony_ci===================== 53862306a36Sopenharmony_ci 53962306a36Sopenharmony_ciWhen a new file is opened, a reference is obtained on the opening task's 54062306a36Sopenharmony_cicredentials and this is attached to the file struct as ``f_cred`` in place of 54162306a36Sopenharmony_ci``f_uid`` and ``f_gid``. Code that used to access ``file->f_uid`` and 54262306a36Sopenharmony_ci``file->f_gid`` should now access ``file->f_cred->fsuid`` and 54362306a36Sopenharmony_ci``file->f_cred->fsgid``. 54462306a36Sopenharmony_ci 54562306a36Sopenharmony_ciIt is safe to access ``f_cred`` without the use of RCU or locking because the 54662306a36Sopenharmony_cipointer will not change over the lifetime of the file struct, and nor will the 54762306a36Sopenharmony_cicontents of the cred struct pointed to, barring the exceptions listed above 54862306a36Sopenharmony_ci(see the Task Credentials section). 54962306a36Sopenharmony_ci 55062306a36Sopenharmony_ciTo avoid "confused deputy" privilege escalation attacks, access control checks 55162306a36Sopenharmony_ciduring subsequent operations on an opened file should use these credentials 55262306a36Sopenharmony_ciinstead of "current"'s credentials, as the file may have been passed to a more 55362306a36Sopenharmony_ciprivileged process. 55462306a36Sopenharmony_ci 55562306a36Sopenharmony_ciOverriding the VFS's Use of Credentials 55662306a36Sopenharmony_ci======================================= 55762306a36Sopenharmony_ci 55862306a36Sopenharmony_ciUnder some circumstances it is desirable to override the credentials used by 55962306a36Sopenharmony_cithe VFS, and that can be done by calling into such as ``vfs_mkdir()`` with a 56062306a36Sopenharmony_cidifferent set of credentials. This is done in the following places: 56162306a36Sopenharmony_ci 56262306a36Sopenharmony_ci * ``sys_faccessat()``. 56362306a36Sopenharmony_ci * ``do_coredump()``. 56462306a36Sopenharmony_ci * nfs4recover.c. 565