18c2ecf20Sopenharmony_ci==================== 28c2ecf20Sopenharmony_ciCredentials in Linux 38c2ecf20Sopenharmony_ci==================== 48c2ecf20Sopenharmony_ci 58c2ecf20Sopenharmony_ciBy: David Howells <dhowells@redhat.com> 68c2ecf20Sopenharmony_ci 78c2ecf20Sopenharmony_ci.. contents:: :local: 88c2ecf20Sopenharmony_ci 98c2ecf20Sopenharmony_ciOverview 108c2ecf20Sopenharmony_ci======== 118c2ecf20Sopenharmony_ci 128c2ecf20Sopenharmony_ciThere are several parts to the security check performed by Linux when one 138c2ecf20Sopenharmony_ciobject acts upon another: 148c2ecf20Sopenharmony_ci 158c2ecf20Sopenharmony_ci 1. Objects. 168c2ecf20Sopenharmony_ci 178c2ecf20Sopenharmony_ci Objects are things in the system that may be acted upon directly by 188c2ecf20Sopenharmony_ci userspace programs. Linux has a variety of actionable objects, including: 198c2ecf20Sopenharmony_ci 208c2ecf20Sopenharmony_ci - Tasks 218c2ecf20Sopenharmony_ci - Files/inodes 228c2ecf20Sopenharmony_ci - Sockets 238c2ecf20Sopenharmony_ci - Message queues 248c2ecf20Sopenharmony_ci - Shared memory segments 258c2ecf20Sopenharmony_ci - Semaphores 268c2ecf20Sopenharmony_ci - Keys 278c2ecf20Sopenharmony_ci 288c2ecf20Sopenharmony_ci As a part of the description of all these objects there is a set of 298c2ecf20Sopenharmony_ci credentials. What's in the set depends on the type of object. 308c2ecf20Sopenharmony_ci 318c2ecf20Sopenharmony_ci 2. Object ownership. 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci Amongst the credentials of most objects, there will be a subset that 348c2ecf20Sopenharmony_ci indicates the ownership of that object. This is used for resource 358c2ecf20Sopenharmony_ci accounting and limitation (disk quotas and task rlimits for example). 368c2ecf20Sopenharmony_ci 378c2ecf20Sopenharmony_ci In a standard UNIX filesystem, for instance, this will be defined by the 388c2ecf20Sopenharmony_ci UID marked on the inode. 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_ci 3. The objective context. 418c2ecf20Sopenharmony_ci 428c2ecf20Sopenharmony_ci Also amongst the credentials of those objects, there will be a subset that 438c2ecf20Sopenharmony_ci indicates the 'objective context' of that object. This may or may not be 448c2ecf20Sopenharmony_ci the same set as in (2) - in standard UNIX files, for instance, this is the 458c2ecf20Sopenharmony_ci defined by the UID and the GID marked on the inode. 468c2ecf20Sopenharmony_ci 478c2ecf20Sopenharmony_ci The objective context is used as part of the security calculation that is 488c2ecf20Sopenharmony_ci carried out when an object is acted upon. 498c2ecf20Sopenharmony_ci 508c2ecf20Sopenharmony_ci 4. Subjects. 518c2ecf20Sopenharmony_ci 528c2ecf20Sopenharmony_ci A subject is an object that is acting upon another object. 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci Most of the objects in the system are inactive: they don't act on other 558c2ecf20Sopenharmony_ci objects within the system. Processes/tasks are the obvious exception: 568c2ecf20Sopenharmony_ci they do stuff; they access and manipulate things. 578c2ecf20Sopenharmony_ci 588c2ecf20Sopenharmony_ci Objects other than tasks may under some circumstances also be subjects. 598c2ecf20Sopenharmony_ci For instance an open file may send SIGIO to a task using the UID and EUID 608c2ecf20Sopenharmony_ci given to it by a task that called ``fcntl(F_SETOWN)`` upon it. In this case, 618c2ecf20Sopenharmony_ci the file struct will have a subjective context too. 628c2ecf20Sopenharmony_ci 638c2ecf20Sopenharmony_ci 5. The subjective context. 648c2ecf20Sopenharmony_ci 658c2ecf20Sopenharmony_ci A subject has an additional interpretation of its credentials. A subset 668c2ecf20Sopenharmony_ci of its credentials forms the 'subjective context'. The subjective context 678c2ecf20Sopenharmony_ci is used as part of the security calculation that is carried out when a 688c2ecf20Sopenharmony_ci subject acts. 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci A Linux task, for example, has the FSUID, FSGID and the supplementary 718c2ecf20Sopenharmony_ci group list for when it is acting upon a file - which are quite separate 728c2ecf20Sopenharmony_ci from the real UID and GID that normally form the objective context of the 738c2ecf20Sopenharmony_ci task. 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci 6. Actions. 768c2ecf20Sopenharmony_ci 778c2ecf20Sopenharmony_ci Linux has a number of actions available that a subject may perform upon an 788c2ecf20Sopenharmony_ci object. The set of actions available depends on the nature of the subject 798c2ecf20Sopenharmony_ci and the object. 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci Actions include reading, writing, creating and deleting files; forking or 828c2ecf20Sopenharmony_ci signalling and tracing tasks. 838c2ecf20Sopenharmony_ci 848c2ecf20Sopenharmony_ci 7. Rules, access control lists and security calculations. 858c2ecf20Sopenharmony_ci 868c2ecf20Sopenharmony_ci When a subject acts upon an object, a security calculation is made. This 878c2ecf20Sopenharmony_ci involves taking the subjective context, the objective context and the 888c2ecf20Sopenharmony_ci action, and searching one or more sets of rules to see whether the subject 898c2ecf20Sopenharmony_ci is granted or denied permission to act in the desired manner on the 908c2ecf20Sopenharmony_ci object, given those contexts. 918c2ecf20Sopenharmony_ci 928c2ecf20Sopenharmony_ci There are two main sources of rules: 938c2ecf20Sopenharmony_ci 948c2ecf20Sopenharmony_ci a. Discretionary access control (DAC): 958c2ecf20Sopenharmony_ci 968c2ecf20Sopenharmony_ci Sometimes the object will include sets of rules as part of its 978c2ecf20Sopenharmony_ci description. This is an 'Access Control List' or 'ACL'. A Linux 988c2ecf20Sopenharmony_ci file may supply more than one ACL. 998c2ecf20Sopenharmony_ci 1008c2ecf20Sopenharmony_ci A traditional UNIX file, for example, includes a permissions mask that 1018c2ecf20Sopenharmony_ci is an abbreviated ACL with three fixed classes of subject ('user', 1028c2ecf20Sopenharmony_ci 'group' and 'other'), each of which may be granted certain privileges 1038c2ecf20Sopenharmony_ci ('read', 'write' and 'execute' - whatever those map to for the object 1048c2ecf20Sopenharmony_ci in question). UNIX file permissions do not allow the arbitrary 1058c2ecf20Sopenharmony_ci specification of subjects, however, and so are of limited use. 1068c2ecf20Sopenharmony_ci 1078c2ecf20Sopenharmony_ci A Linux file might also sport a POSIX ACL. This is a list of rules 1088c2ecf20Sopenharmony_ci that grants various permissions to arbitrary subjects. 1098c2ecf20Sopenharmony_ci 1108c2ecf20Sopenharmony_ci b. Mandatory access control (MAC): 1118c2ecf20Sopenharmony_ci 1128c2ecf20Sopenharmony_ci The system as a whole may have one or more sets of rules that get 1138c2ecf20Sopenharmony_ci applied to all subjects and objects, regardless of their source. 1148c2ecf20Sopenharmony_ci SELinux and Smack are examples of this. 1158c2ecf20Sopenharmony_ci 1168c2ecf20Sopenharmony_ci In the case of SELinux and Smack, each object is given a label as part 1178c2ecf20Sopenharmony_ci of its credentials. When an action is requested, they take the 1188c2ecf20Sopenharmony_ci subject label, the object label and the action and look for a rule 1198c2ecf20Sopenharmony_ci that says that this action is either granted or denied. 1208c2ecf20Sopenharmony_ci 1218c2ecf20Sopenharmony_ci 1228c2ecf20Sopenharmony_ciTypes of Credentials 1238c2ecf20Sopenharmony_ci==================== 1248c2ecf20Sopenharmony_ci 1258c2ecf20Sopenharmony_ciThe Linux kernel supports the following types of credentials: 1268c2ecf20Sopenharmony_ci 1278c2ecf20Sopenharmony_ci 1. Traditional UNIX credentials. 1288c2ecf20Sopenharmony_ci 1298c2ecf20Sopenharmony_ci - Real User ID 1308c2ecf20Sopenharmony_ci - Real Group ID 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci The UID and GID are carried by most, if not all, Linux objects, even if in 1338c2ecf20Sopenharmony_ci some cases it has to be invented (FAT or CIFS files for example, which are 1348c2ecf20Sopenharmony_ci derived from Windows). These (mostly) define the objective context of 1358c2ecf20Sopenharmony_ci that object, with tasks being slightly different in some cases. 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci - Effective, Saved and FS User ID 1388c2ecf20Sopenharmony_ci - Effective, Saved and FS Group ID 1398c2ecf20Sopenharmony_ci - Supplementary groups 1408c2ecf20Sopenharmony_ci 1418c2ecf20Sopenharmony_ci These are additional credentials used by tasks only. Usually, an 1428c2ecf20Sopenharmony_ci EUID/EGID/GROUPS will be used as the subjective context, and real UID/GID 1438c2ecf20Sopenharmony_ci will be used as the objective. For tasks, it should be noted that this is 1448c2ecf20Sopenharmony_ci not always true. 1458c2ecf20Sopenharmony_ci 1468c2ecf20Sopenharmony_ci 2. Capabilities. 1478c2ecf20Sopenharmony_ci 1488c2ecf20Sopenharmony_ci - Set of permitted capabilities 1498c2ecf20Sopenharmony_ci - Set of inheritable capabilities 1508c2ecf20Sopenharmony_ci - Set of effective capabilities 1518c2ecf20Sopenharmony_ci - Capability bounding set 1528c2ecf20Sopenharmony_ci 1538c2ecf20Sopenharmony_ci These are only carried by tasks. They indicate superior capabilities 1548c2ecf20Sopenharmony_ci granted piecemeal to a task that an ordinary task wouldn't otherwise have. 1558c2ecf20Sopenharmony_ci These are manipulated implicitly by changes to the traditional UNIX 1568c2ecf20Sopenharmony_ci credentials, but can also be manipulated directly by the ``capset()`` 1578c2ecf20Sopenharmony_ci system call. 1588c2ecf20Sopenharmony_ci 1598c2ecf20Sopenharmony_ci The permitted capabilities are those caps that the process might grant 1608c2ecf20Sopenharmony_ci itself to its effective or permitted sets through ``capset()``. This 1618c2ecf20Sopenharmony_ci inheritable set might also be so constrained. 1628c2ecf20Sopenharmony_ci 1638c2ecf20Sopenharmony_ci The effective capabilities are the ones that a task is actually allowed to 1648c2ecf20Sopenharmony_ci make use of itself. 1658c2ecf20Sopenharmony_ci 1668c2ecf20Sopenharmony_ci The inheritable capabilities are the ones that may get passed across 1678c2ecf20Sopenharmony_ci ``execve()``. 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci The bounding set limits the capabilities that may be inherited across 1708c2ecf20Sopenharmony_ci ``execve()``, especially when a binary is executed that will execute as 1718c2ecf20Sopenharmony_ci UID 0. 1728c2ecf20Sopenharmony_ci 1738c2ecf20Sopenharmony_ci 3. Secure management flags (securebits). 1748c2ecf20Sopenharmony_ci 1758c2ecf20Sopenharmony_ci These are only carried by tasks. These govern the way the above 1768c2ecf20Sopenharmony_ci credentials are manipulated and inherited over certain operations such as 1778c2ecf20Sopenharmony_ci execve(). They aren't used directly as objective or subjective 1788c2ecf20Sopenharmony_ci credentials. 1798c2ecf20Sopenharmony_ci 1808c2ecf20Sopenharmony_ci 4. Keys and keyrings. 1818c2ecf20Sopenharmony_ci 1828c2ecf20Sopenharmony_ci These are only carried by tasks. They carry and cache security tokens 1838c2ecf20Sopenharmony_ci that don't fit into the other standard UNIX credentials. They are for 1848c2ecf20Sopenharmony_ci making such things as network filesystem keys available to the file 1858c2ecf20Sopenharmony_ci accesses performed by processes, without the necessity of ordinary 1868c2ecf20Sopenharmony_ci programs having to know about security details involved. 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci Keyrings are a special type of key. They carry sets of other keys and can 1898c2ecf20Sopenharmony_ci be searched for the desired key. Each process may subscribe to a number 1908c2ecf20Sopenharmony_ci of keyrings: 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci Per-thread keying 1938c2ecf20Sopenharmony_ci Per-process keyring 1948c2ecf20Sopenharmony_ci Per-session keyring 1958c2ecf20Sopenharmony_ci 1968c2ecf20Sopenharmony_ci When a process accesses a key, if not already present, it will normally be 1978c2ecf20Sopenharmony_ci cached on one of these keyrings for future accesses to find. 1988c2ecf20Sopenharmony_ci 1998c2ecf20Sopenharmony_ci For more information on using keys, see ``Documentation/security/keys/*``. 2008c2ecf20Sopenharmony_ci 2018c2ecf20Sopenharmony_ci 5. LSM 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_ci The Linux Security Module allows extra controls to be placed over the 2048c2ecf20Sopenharmony_ci operations that a task may do. Currently Linux supports several LSM 2058c2ecf20Sopenharmony_ci options. 2068c2ecf20Sopenharmony_ci 2078c2ecf20Sopenharmony_ci Some work by labelling the objects in a system and then applying sets of 2088c2ecf20Sopenharmony_ci rules (policies) that say what operations a task with one label may do to 2098c2ecf20Sopenharmony_ci an object with another label. 2108c2ecf20Sopenharmony_ci 2118c2ecf20Sopenharmony_ci 6. AF_KEY 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci This is a socket-based approach to credential management for networking 2148c2ecf20Sopenharmony_ci stacks [RFC 2367]. It isn't discussed by this document as it doesn't 2158c2ecf20Sopenharmony_ci interact directly with task and file credentials; rather it keeps system 2168c2ecf20Sopenharmony_ci level credentials. 2178c2ecf20Sopenharmony_ci 2188c2ecf20Sopenharmony_ci 2198c2ecf20Sopenharmony_ciWhen a file is opened, part of the opening task's subjective context is 2208c2ecf20Sopenharmony_cirecorded in the file struct created. This allows operations using that file 2218c2ecf20Sopenharmony_cistruct to use those credentials instead of the subjective context of the task 2228c2ecf20Sopenharmony_cithat issued the operation. An example of this would be a file opened on a 2238c2ecf20Sopenharmony_cinetwork filesystem where the credentials of the opened file should be presented 2248c2ecf20Sopenharmony_cito the server, regardless of who is actually doing a read or a write upon it. 2258c2ecf20Sopenharmony_ci 2268c2ecf20Sopenharmony_ci 2278c2ecf20Sopenharmony_ciFile Markings 2288c2ecf20Sopenharmony_ci============= 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ciFiles on disk or obtained over the network may have annotations that form the 2318c2ecf20Sopenharmony_ciobjective security context of that file. Depending on the type of filesystem, 2328c2ecf20Sopenharmony_cithis may include one or more of the following: 2338c2ecf20Sopenharmony_ci 2348c2ecf20Sopenharmony_ci * UNIX UID, GID, mode; 2358c2ecf20Sopenharmony_ci * Windows user ID; 2368c2ecf20Sopenharmony_ci * Access control list; 2378c2ecf20Sopenharmony_ci * LSM security label; 2388c2ecf20Sopenharmony_ci * UNIX exec privilege escalation bits (SUID/SGID); 2398c2ecf20Sopenharmony_ci * File capabilities exec privilege escalation bits. 2408c2ecf20Sopenharmony_ci 2418c2ecf20Sopenharmony_ciThese are compared to the task's subjective security context, and certain 2428c2ecf20Sopenharmony_cioperations allowed or disallowed as a result. In the case of execve(), the 2438c2ecf20Sopenharmony_ciprivilege escalation bits come into play, and may allow the resulting process 2448c2ecf20Sopenharmony_ciextra privileges, based on the annotations on the executable file. 2458c2ecf20Sopenharmony_ci 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ciTask Credentials 2488c2ecf20Sopenharmony_ci================ 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ciIn Linux, all of a task's credentials are held in (uid, gid) or through 2518c2ecf20Sopenharmony_ci(groups, keys, LSM security) a refcounted structure of type 'struct cred'. 2528c2ecf20Sopenharmony_ciEach task points to its credentials by a pointer called 'cred' in its 2538c2ecf20Sopenharmony_citask_struct. 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_ciOnce a set of credentials has been prepared and committed, it may not be 2568c2ecf20Sopenharmony_cichanged, barring the following exceptions: 2578c2ecf20Sopenharmony_ci 2588c2ecf20Sopenharmony_ci 1. its reference count may be changed; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_ci 2. the reference count on the group_info struct it points to may be changed; 2618c2ecf20Sopenharmony_ci 2628c2ecf20Sopenharmony_ci 3. the reference count on the security data it points to may be changed; 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_ci 4. the reference count on any keyrings it points to may be changed; 2658c2ecf20Sopenharmony_ci 2668c2ecf20Sopenharmony_ci 5. any keyrings it points to may be revoked, expired or have their security 2678c2ecf20Sopenharmony_ci attributes changed; and 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_ci 6. the contents of any keyrings to which it points may be changed (the whole 2708c2ecf20Sopenharmony_ci point of keyrings being a shared set of credentials, modifiable by anyone 2718c2ecf20Sopenharmony_ci with appropriate access). 2728c2ecf20Sopenharmony_ci 2738c2ecf20Sopenharmony_ciTo alter anything in the cred struct, the copy-and-replace principle must be 2748c2ecf20Sopenharmony_ciadhered to. First take a copy, then alter the copy and then use RCU to change 2758c2ecf20Sopenharmony_cithe task pointer to make it point to the new copy. There are wrappers to aid 2768c2ecf20Sopenharmony_ciwith this (see below). 2778c2ecf20Sopenharmony_ci 2788c2ecf20Sopenharmony_ciA task may only alter its _own_ credentials; it is no longer permitted for a 2798c2ecf20Sopenharmony_citask to alter another's credentials. This means the ``capset()`` system call 2808c2ecf20Sopenharmony_ciis no longer permitted to take any PID other than the one of the current 2818c2ecf20Sopenharmony_ciprocess. Also ``keyctl_instantiate()`` and ``keyctl_negate()`` functions no 2828c2ecf20Sopenharmony_cilonger permit attachment to process-specific keyrings in the requesting 2838c2ecf20Sopenharmony_ciprocess as the instantiating process may need to create them. 2848c2ecf20Sopenharmony_ci 2858c2ecf20Sopenharmony_ci 2868c2ecf20Sopenharmony_ciImmutable Credentials 2878c2ecf20Sopenharmony_ci--------------------- 2888c2ecf20Sopenharmony_ci 2898c2ecf20Sopenharmony_ciOnce a set of credentials has been made public (by calling ``commit_creds()`` 2908c2ecf20Sopenharmony_cifor example), it must be considered immutable, barring two exceptions: 2918c2ecf20Sopenharmony_ci 2928c2ecf20Sopenharmony_ci 1. The reference count may be altered. 2938c2ecf20Sopenharmony_ci 2948c2ecf20Sopenharmony_ci 2. While the keyring subscriptions of a set of credentials may not be 2958c2ecf20Sopenharmony_ci changed, the keyrings subscribed to may have their contents altered. 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_ciTo catch accidental credential alteration at compile time, struct task_struct 2988c2ecf20Sopenharmony_cihas _const_ pointers to its credential sets, as does struct file. Furthermore, 2998c2ecf20Sopenharmony_cicertain functions such as ``get_cred()`` and ``put_cred()`` operate on const 3008c2ecf20Sopenharmony_cipointers, thus rendering casts unnecessary, but require to temporarily ditch 3018c2ecf20Sopenharmony_cithe const qualification to be able to alter the reference count. 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ci 3048c2ecf20Sopenharmony_ciAccessing Task Credentials 3058c2ecf20Sopenharmony_ci-------------------------- 3068c2ecf20Sopenharmony_ci 3078c2ecf20Sopenharmony_ciA task being able to alter only its own credentials permits the current process 3088c2ecf20Sopenharmony_cito read or replace its own credentials without the need for any form of locking 3098c2ecf20Sopenharmony_ci-- which simplifies things greatly. It can just call:: 3108c2ecf20Sopenharmony_ci 3118c2ecf20Sopenharmony_ci const struct cred *current_cred() 3128c2ecf20Sopenharmony_ci 3138c2ecf20Sopenharmony_cito get a pointer to its credentials structure, and it doesn't have to release 3148c2ecf20Sopenharmony_ciit afterwards. 3158c2ecf20Sopenharmony_ci 3168c2ecf20Sopenharmony_ciThere are convenience wrappers for retrieving specific aspects of a task's 3178c2ecf20Sopenharmony_cicredentials (the value is simply returned in each case):: 3188c2ecf20Sopenharmony_ci 3198c2ecf20Sopenharmony_ci uid_t current_uid(void) Current's real UID 3208c2ecf20Sopenharmony_ci gid_t current_gid(void) Current's real GID 3218c2ecf20Sopenharmony_ci uid_t current_euid(void) Current's effective UID 3228c2ecf20Sopenharmony_ci gid_t current_egid(void) Current's effective GID 3238c2ecf20Sopenharmony_ci uid_t current_fsuid(void) Current's file access UID 3248c2ecf20Sopenharmony_ci gid_t current_fsgid(void) Current's file access GID 3258c2ecf20Sopenharmony_ci kernel_cap_t current_cap(void) Current's effective capabilities 3268c2ecf20Sopenharmony_ci struct user_struct *current_user(void) Current's user account 3278c2ecf20Sopenharmony_ci 3288c2ecf20Sopenharmony_ciThere are also convenience wrappers for retrieving specific associated pairs of 3298c2ecf20Sopenharmony_cia task's credentials:: 3308c2ecf20Sopenharmony_ci 3318c2ecf20Sopenharmony_ci void current_uid_gid(uid_t *, gid_t *); 3328c2ecf20Sopenharmony_ci void current_euid_egid(uid_t *, gid_t *); 3338c2ecf20Sopenharmony_ci void current_fsuid_fsgid(uid_t *, gid_t *); 3348c2ecf20Sopenharmony_ci 3358c2ecf20Sopenharmony_ciwhich return these pairs of values through their arguments after retrieving 3368c2ecf20Sopenharmony_cithem from the current task's credentials. 3378c2ecf20Sopenharmony_ci 3388c2ecf20Sopenharmony_ci 3398c2ecf20Sopenharmony_ciIn addition, there is a function for obtaining a reference on the current 3408c2ecf20Sopenharmony_ciprocess's current set of credentials:: 3418c2ecf20Sopenharmony_ci 3428c2ecf20Sopenharmony_ci const struct cred *get_current_cred(void); 3438c2ecf20Sopenharmony_ci 3448c2ecf20Sopenharmony_ciand functions for getting references to one of the credentials that don't 3458c2ecf20Sopenharmony_ciactually live in struct cred:: 3468c2ecf20Sopenharmony_ci 3478c2ecf20Sopenharmony_ci struct user_struct *get_current_user(void); 3488c2ecf20Sopenharmony_ci struct group_info *get_current_groups(void); 3498c2ecf20Sopenharmony_ci 3508c2ecf20Sopenharmony_ciwhich get references to the current process's user accounting structure and 3518c2ecf20Sopenharmony_cisupplementary groups list respectively. 3528c2ecf20Sopenharmony_ci 3538c2ecf20Sopenharmony_ciOnce a reference has been obtained, it must be released with ``put_cred()``, 3548c2ecf20Sopenharmony_ci``free_uid()`` or ``put_group_info()`` as appropriate. 3558c2ecf20Sopenharmony_ci 3568c2ecf20Sopenharmony_ci 3578c2ecf20Sopenharmony_ciAccessing Another Task's Credentials 3588c2ecf20Sopenharmony_ci------------------------------------ 3598c2ecf20Sopenharmony_ci 3608c2ecf20Sopenharmony_ciWhile a task may access its own credentials without the need for locking, the 3618c2ecf20Sopenharmony_cisame is not true of a task wanting to access another task's credentials. It 3628c2ecf20Sopenharmony_cimust use the RCU read lock and ``rcu_dereference()``. 3638c2ecf20Sopenharmony_ci 3648c2ecf20Sopenharmony_ciThe ``rcu_dereference()`` is wrapped by:: 3658c2ecf20Sopenharmony_ci 3668c2ecf20Sopenharmony_ci const struct cred *__task_cred(struct task_struct *task); 3678c2ecf20Sopenharmony_ci 3688c2ecf20Sopenharmony_ciThis should be used inside the RCU read lock, as in the following example:: 3698c2ecf20Sopenharmony_ci 3708c2ecf20Sopenharmony_ci void foo(struct task_struct *t, struct foo_data *f) 3718c2ecf20Sopenharmony_ci { 3728c2ecf20Sopenharmony_ci const struct cred *tcred; 3738c2ecf20Sopenharmony_ci ... 3748c2ecf20Sopenharmony_ci rcu_read_lock(); 3758c2ecf20Sopenharmony_ci tcred = __task_cred(t); 3768c2ecf20Sopenharmony_ci f->uid = tcred->uid; 3778c2ecf20Sopenharmony_ci f->gid = tcred->gid; 3788c2ecf20Sopenharmony_ci f->groups = get_group_info(tcred->groups); 3798c2ecf20Sopenharmony_ci rcu_read_unlock(); 3808c2ecf20Sopenharmony_ci ... 3818c2ecf20Sopenharmony_ci } 3828c2ecf20Sopenharmony_ci 3838c2ecf20Sopenharmony_ciShould it be necessary to hold another task's credentials for a long period of 3848c2ecf20Sopenharmony_citime, and possibly to sleep while doing so, then the caller should get a 3858c2ecf20Sopenharmony_cireference on them using:: 3868c2ecf20Sopenharmony_ci 3878c2ecf20Sopenharmony_ci const struct cred *get_task_cred(struct task_struct *task); 3888c2ecf20Sopenharmony_ci 3898c2ecf20Sopenharmony_ciThis does all the RCU magic inside of it. The caller must call put_cred() on 3908c2ecf20Sopenharmony_cithe credentials so obtained when they're finished with. 3918c2ecf20Sopenharmony_ci 3928c2ecf20Sopenharmony_ci.. note:: 3938c2ecf20Sopenharmony_ci The result of ``__task_cred()`` should not be passed directly to 3948c2ecf20Sopenharmony_ci ``get_cred()`` as this may race with ``commit_cred()``. 3958c2ecf20Sopenharmony_ci 3968c2ecf20Sopenharmony_ciThere are a couple of convenience functions to access bits of another task's 3978c2ecf20Sopenharmony_cicredentials, hiding the RCU magic from the caller:: 3988c2ecf20Sopenharmony_ci 3998c2ecf20Sopenharmony_ci uid_t task_uid(task) Task's real UID 4008c2ecf20Sopenharmony_ci uid_t task_euid(task) Task's effective UID 4018c2ecf20Sopenharmony_ci 4028c2ecf20Sopenharmony_ciIf the caller is holding the RCU read lock at the time anyway, then:: 4038c2ecf20Sopenharmony_ci 4048c2ecf20Sopenharmony_ci __task_cred(task)->uid 4058c2ecf20Sopenharmony_ci __task_cred(task)->euid 4068c2ecf20Sopenharmony_ci 4078c2ecf20Sopenharmony_cishould be used instead. Similarly, if multiple aspects of a task's credentials 4088c2ecf20Sopenharmony_cineed to be accessed, RCU read lock should be used, ``__task_cred()`` called, 4098c2ecf20Sopenharmony_cithe result stored in a temporary pointer and then the credential aspects called 4108c2ecf20Sopenharmony_cifrom that before dropping the lock. This prevents the potentially expensive 4118c2ecf20Sopenharmony_ciRCU magic from being invoked multiple times. 4128c2ecf20Sopenharmony_ci 4138c2ecf20Sopenharmony_ciShould some other single aspect of another task's credentials need to be 4148c2ecf20Sopenharmony_ciaccessed, then this can be used:: 4158c2ecf20Sopenharmony_ci 4168c2ecf20Sopenharmony_ci task_cred_xxx(task, member) 4178c2ecf20Sopenharmony_ci 4188c2ecf20Sopenharmony_ciwhere 'member' is a non-pointer member of the cred struct. For instance:: 4198c2ecf20Sopenharmony_ci 4208c2ecf20Sopenharmony_ci uid_t task_cred_xxx(task, suid); 4218c2ecf20Sopenharmony_ci 4228c2ecf20Sopenharmony_ciwill retrieve 'struct cred::suid' from the task, doing the appropriate RCU 4238c2ecf20Sopenharmony_cimagic. This may not be used for pointer members as what they point to may 4248c2ecf20Sopenharmony_cidisappear the moment the RCU read lock is dropped. 4258c2ecf20Sopenharmony_ci 4268c2ecf20Sopenharmony_ci 4278c2ecf20Sopenharmony_ciAltering Credentials 4288c2ecf20Sopenharmony_ci-------------------- 4298c2ecf20Sopenharmony_ci 4308c2ecf20Sopenharmony_ciAs previously mentioned, a task may only alter its own credentials, and may not 4318c2ecf20Sopenharmony_cialter those of another task. This means that it doesn't need to use any 4328c2ecf20Sopenharmony_cilocking to alter its own credentials. 4338c2ecf20Sopenharmony_ci 4348c2ecf20Sopenharmony_ciTo alter the current process's credentials, a function should first prepare a 4358c2ecf20Sopenharmony_cinew set of credentials by calling:: 4368c2ecf20Sopenharmony_ci 4378c2ecf20Sopenharmony_ci struct cred *prepare_creds(void); 4388c2ecf20Sopenharmony_ci 4398c2ecf20Sopenharmony_cithis locks current->cred_replace_mutex and then allocates and constructs a 4408c2ecf20Sopenharmony_ciduplicate of the current process's credentials, returning with the mutex still 4418c2ecf20Sopenharmony_ciheld if successful. It returns NULL if not successful (out of memory). 4428c2ecf20Sopenharmony_ci 4438c2ecf20Sopenharmony_ciThe mutex prevents ``ptrace()`` from altering the ptrace state of a process 4448c2ecf20Sopenharmony_ciwhile security checks on credentials construction and changing is taking place 4458c2ecf20Sopenharmony_cias the ptrace state may alter the outcome, particularly in the case of 4468c2ecf20Sopenharmony_ci``execve()``. 4478c2ecf20Sopenharmony_ci 4488c2ecf20Sopenharmony_ciThe new credentials set should be altered appropriately, and any security 4498c2ecf20Sopenharmony_cichecks and hooks done. Both the current and the proposed sets of credentials 4508c2ecf20Sopenharmony_ciare available for this purpose as current_cred() will return the current set 4518c2ecf20Sopenharmony_cistill at this point. 4528c2ecf20Sopenharmony_ci 4538c2ecf20Sopenharmony_ciWhen replacing the group list, the new list must be sorted before it 4548c2ecf20Sopenharmony_ciis added to the credential, as a binary search is used to test for 4558c2ecf20Sopenharmony_cimembership. In practice, this means groups_sort() should be 4568c2ecf20Sopenharmony_cicalled before set_groups() or set_current_groups(). 4578c2ecf20Sopenharmony_cigroups_sort() must not be called on a ``struct group_list`` which 4588c2ecf20Sopenharmony_ciis shared as it may permute elements as part of the sorting process 4598c2ecf20Sopenharmony_cieven if the array is already sorted. 4608c2ecf20Sopenharmony_ci 4618c2ecf20Sopenharmony_ciWhen the credential set is ready, it should be committed to the current process 4628c2ecf20Sopenharmony_ciby calling:: 4638c2ecf20Sopenharmony_ci 4648c2ecf20Sopenharmony_ci int commit_creds(struct cred *new); 4658c2ecf20Sopenharmony_ci 4668c2ecf20Sopenharmony_ciThis will alter various aspects of the credentials and the process, giving the 4678c2ecf20Sopenharmony_ciLSM a chance to do likewise, then it will use ``rcu_assign_pointer()`` to 4688c2ecf20Sopenharmony_ciactually commit the new credentials to ``current->cred``, it will release 4698c2ecf20Sopenharmony_ci``current->cred_replace_mutex`` to allow ``ptrace()`` to take place, and it 4708c2ecf20Sopenharmony_ciwill notify the scheduler and others of the changes. 4718c2ecf20Sopenharmony_ci 4728c2ecf20Sopenharmony_ciThis function is guaranteed to return 0, so that it can be tail-called at the 4738c2ecf20Sopenharmony_ciend of such functions as ``sys_setresuid()``. 4748c2ecf20Sopenharmony_ci 4758c2ecf20Sopenharmony_ciNote that this function consumes the caller's reference to the new credentials. 4768c2ecf20Sopenharmony_ciThe caller should _not_ call ``put_cred()`` on the new credentials afterwards. 4778c2ecf20Sopenharmony_ci 4788c2ecf20Sopenharmony_ciFurthermore, once this function has been called on a new set of credentials, 4798c2ecf20Sopenharmony_cithose credentials may _not_ be changed further. 4808c2ecf20Sopenharmony_ci 4818c2ecf20Sopenharmony_ci 4828c2ecf20Sopenharmony_ciShould the security checks fail or some other error occur after 4838c2ecf20Sopenharmony_ci``prepare_creds()`` has been called, then the following function should be 4848c2ecf20Sopenharmony_ciinvoked:: 4858c2ecf20Sopenharmony_ci 4868c2ecf20Sopenharmony_ci void abort_creds(struct cred *new); 4878c2ecf20Sopenharmony_ci 4888c2ecf20Sopenharmony_ciThis releases the lock on ``current->cred_replace_mutex`` that 4898c2ecf20Sopenharmony_ci``prepare_creds()`` got and then releases the new credentials. 4908c2ecf20Sopenharmony_ci 4918c2ecf20Sopenharmony_ci 4928c2ecf20Sopenharmony_ciA typical credentials alteration function would look something like this:: 4938c2ecf20Sopenharmony_ci 4948c2ecf20Sopenharmony_ci int alter_suid(uid_t suid) 4958c2ecf20Sopenharmony_ci { 4968c2ecf20Sopenharmony_ci struct cred *new; 4978c2ecf20Sopenharmony_ci int ret; 4988c2ecf20Sopenharmony_ci 4998c2ecf20Sopenharmony_ci new = prepare_creds(); 5008c2ecf20Sopenharmony_ci if (!new) 5018c2ecf20Sopenharmony_ci return -ENOMEM; 5028c2ecf20Sopenharmony_ci 5038c2ecf20Sopenharmony_ci new->suid = suid; 5048c2ecf20Sopenharmony_ci ret = security_alter_suid(new); 5058c2ecf20Sopenharmony_ci if (ret < 0) { 5068c2ecf20Sopenharmony_ci abort_creds(new); 5078c2ecf20Sopenharmony_ci return ret; 5088c2ecf20Sopenharmony_ci } 5098c2ecf20Sopenharmony_ci 5108c2ecf20Sopenharmony_ci return commit_creds(new); 5118c2ecf20Sopenharmony_ci } 5128c2ecf20Sopenharmony_ci 5138c2ecf20Sopenharmony_ci 5148c2ecf20Sopenharmony_ciManaging Credentials 5158c2ecf20Sopenharmony_ci-------------------- 5168c2ecf20Sopenharmony_ci 5178c2ecf20Sopenharmony_ciThere are some functions to help manage credentials: 5188c2ecf20Sopenharmony_ci 5198c2ecf20Sopenharmony_ci - ``void put_cred(const struct cred *cred);`` 5208c2ecf20Sopenharmony_ci 5218c2ecf20Sopenharmony_ci This releases a reference to the given set of credentials. If the 5228c2ecf20Sopenharmony_ci reference count reaches zero, the credentials will be scheduled for 5238c2ecf20Sopenharmony_ci destruction by the RCU system. 5248c2ecf20Sopenharmony_ci 5258c2ecf20Sopenharmony_ci - ``const struct cred *get_cred(const struct cred *cred);`` 5268c2ecf20Sopenharmony_ci 5278c2ecf20Sopenharmony_ci This gets a reference on a live set of credentials, returning a pointer to 5288c2ecf20Sopenharmony_ci that set of credentials. 5298c2ecf20Sopenharmony_ci 5308c2ecf20Sopenharmony_ci - ``struct cred *get_new_cred(struct cred *cred);`` 5318c2ecf20Sopenharmony_ci 5328c2ecf20Sopenharmony_ci This gets a reference on a set of credentials that is under construction 5338c2ecf20Sopenharmony_ci and is thus still mutable, returning a pointer to that set of credentials. 5348c2ecf20Sopenharmony_ci 5358c2ecf20Sopenharmony_ci 5368c2ecf20Sopenharmony_ciOpen File Credentials 5378c2ecf20Sopenharmony_ci===================== 5388c2ecf20Sopenharmony_ci 5398c2ecf20Sopenharmony_ciWhen a new file is opened, a reference is obtained on the opening task's 5408c2ecf20Sopenharmony_cicredentials and this is attached to the file struct as ``f_cred`` in place of 5418c2ecf20Sopenharmony_ci``f_uid`` and ``f_gid``. Code that used to access ``file->f_uid`` and 5428c2ecf20Sopenharmony_ci``file->f_gid`` should now access ``file->f_cred->fsuid`` and 5438c2ecf20Sopenharmony_ci``file->f_cred->fsgid``. 5448c2ecf20Sopenharmony_ci 5458c2ecf20Sopenharmony_ciIt is safe to access ``f_cred`` without the use of RCU or locking because the 5468c2ecf20Sopenharmony_cipointer will not change over the lifetime of the file struct, and nor will the 5478c2ecf20Sopenharmony_cicontents of the cred struct pointed to, barring the exceptions listed above 5488c2ecf20Sopenharmony_ci(see the Task Credentials section). 5498c2ecf20Sopenharmony_ci 5508c2ecf20Sopenharmony_ciTo avoid "confused deputy" privilege escalation attacks, access control checks 5518c2ecf20Sopenharmony_ciduring subsequent operations on an opened file should use these credentials 5528c2ecf20Sopenharmony_ciinstead of "current"'s credentials, as the file may have been passed to a more 5538c2ecf20Sopenharmony_ciprivileged process. 5548c2ecf20Sopenharmony_ci 5558c2ecf20Sopenharmony_ciOverriding the VFS's Use of Credentials 5568c2ecf20Sopenharmony_ci======================================= 5578c2ecf20Sopenharmony_ci 5588c2ecf20Sopenharmony_ciUnder some circumstances it is desirable to override the credentials used by 5598c2ecf20Sopenharmony_cithe VFS, and that can be done by calling into such as ``vfs_mkdir()`` with a 5608c2ecf20Sopenharmony_cidifferent set of credentials. This is done in the following places: 5618c2ecf20Sopenharmony_ci 5628c2ecf20Sopenharmony_ci * ``sys_faccessat()``. 5638c2ecf20Sopenharmony_ci * ``do_coredump()``. 5648c2ecf20Sopenharmony_ci * nfs4recover.c. 565