162306a36Sopenharmony_ci========= 262306a36Sopenharmony_ciSafeSetID 362306a36Sopenharmony_ci========= 462306a36Sopenharmony_ciSafeSetID is an LSM module that gates the setid family of syscalls to restrict 562306a36Sopenharmony_ciUID/GID transitions from a given UID/GID to only those approved by a 662306a36Sopenharmony_cisystem-wide allowlist. These restrictions also prohibit the given UIDs/GIDs 762306a36Sopenharmony_cifrom obtaining auxiliary privileges associated with CAP_SET{U/G}ID, such as 862306a36Sopenharmony_ciallowing a user to set up user namespace UID/GID mappings. 962306a36Sopenharmony_ci 1062306a36Sopenharmony_ci 1162306a36Sopenharmony_ciBackground 1262306a36Sopenharmony_ci========== 1362306a36Sopenharmony_ciIn absence of file capabilities, processes spawned on a Linux system that need 1462306a36Sopenharmony_cito switch to a different user must be spawned with CAP_SETUID privileges. 1562306a36Sopenharmony_ciCAP_SETUID is granted to programs running as root or those running as a non-root 1662306a36Sopenharmony_ciuser that have been explicitly given the CAP_SETUID runtime capability. It is 1762306a36Sopenharmony_cioften preferable to use Linux runtime capabilities rather than file 1862306a36Sopenharmony_cicapabilities, since using file capabilities to run a program with elevated 1962306a36Sopenharmony_ciprivileges opens up possible security holes since any user with access to the 2062306a36Sopenharmony_cifile can exec() that program to gain the elevated privileges. 2162306a36Sopenharmony_ci 2262306a36Sopenharmony_ciWhile it is possible to implement a tree of processes by giving full 2362306a36Sopenharmony_ciCAP_SET{U/G}ID capabilities, this is often at odds with the goals of running a 2462306a36Sopenharmony_citree of processes under non-root user(s) in the first place. Specifically, 2562306a36Sopenharmony_cisince CAP_SETUID allows changing to any user on the system, including the root 2662306a36Sopenharmony_ciuser, it is an overpowered capability for what is needed in this scenario, 2762306a36Sopenharmony_ciespecially since programs often only call setuid() to drop privileges to a 2862306a36Sopenharmony_cilesser-privileged user -- not elevate privileges. Unfortunately, there is no 2962306a36Sopenharmony_cigenerally feasible way in Linux to restrict the potential UIDs that a user can 3062306a36Sopenharmony_ciswitch to through setuid() beyond allowing a switch to any user on the system. 3162306a36Sopenharmony_ciThis SafeSetID LSM seeks to provide a solution for restricting setid 3262306a36Sopenharmony_cicapabilities in such a way. 3362306a36Sopenharmony_ci 3462306a36Sopenharmony_ciThe main use case for this LSM is to allow a non-root program to transition to 3562306a36Sopenharmony_ciother untrusted uids without full blown CAP_SETUID capabilities. The non-root 3662306a36Sopenharmony_ciprogram would still need CAP_SETUID to do any kind of transition, but the 3762306a36Sopenharmony_ciadditional restrictions imposed by this LSM would mean it is a "safer" version 3862306a36Sopenharmony_ciof CAP_SETUID since the non-root program cannot take advantage of CAP_SETUID to 3962306a36Sopenharmony_cido any unapproved actions (e.g. setuid to uid 0 or create/enter new user 4062306a36Sopenharmony_cinamespace). The higher level goal is to allow for uid-based sandboxing of system 4162306a36Sopenharmony_ciservices without having to give out CAP_SETUID all over the place just so that 4262306a36Sopenharmony_cinon-root programs can drop to even-lesser-privileged uids. This is especially 4362306a36Sopenharmony_cirelevant when one non-root daemon on the system should be allowed to spawn other 4462306a36Sopenharmony_ciprocesses as different uids, but its undesirable to give the daemon a 4562306a36Sopenharmony_cibasically-root-equivalent CAP_SETUID. 4662306a36Sopenharmony_ci 4762306a36Sopenharmony_ci 4862306a36Sopenharmony_ciOther Approaches Considered 4962306a36Sopenharmony_ci=========================== 5062306a36Sopenharmony_ci 5162306a36Sopenharmony_ciSolve this problem in userspace 5262306a36Sopenharmony_ci------------------------------- 5362306a36Sopenharmony_ciFor candidate applications that would like to have restricted setid capabilities 5462306a36Sopenharmony_cias implemented in this LSM, an alternative option would be to simply take away 5562306a36Sopenharmony_cisetid capabilities from the application completely and refactor the process 5662306a36Sopenharmony_cispawning semantics in the application (e.g. by using a privileged helper program 5762306a36Sopenharmony_cito do process spawning and UID/GID transitions). Unfortunately, there are a 5862306a36Sopenharmony_cinumber of semantics around process spawning that would be affected by this, such 5962306a36Sopenharmony_cias fork() calls where the program doesn't immediately call exec() after the 6062306a36Sopenharmony_cifork(), parent processes specifying custom environment variables or command line 6162306a36Sopenharmony_ciargs for spawned child processes, or inheritance of file handles across a 6262306a36Sopenharmony_cifork()/exec(). Because of this, as solution that uses a privileged helper in 6362306a36Sopenharmony_ciuserspace would likely be less appealing to incorporate into existing projects 6462306a36Sopenharmony_cithat rely on certain process-spawning semantics in Linux. 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ciUse user namespaces 6762306a36Sopenharmony_ci------------------- 6862306a36Sopenharmony_ciAnother possible approach would be to run a given process tree in its own user 6962306a36Sopenharmony_cinamespace and give programs in the tree setid capabilities. In this way, 7062306a36Sopenharmony_ciprograms in the tree could change to any desired UID/GID in the context of their 7162306a36Sopenharmony_ciown user namespace, and only approved UIDs/GIDs could be mapped back to the 7262306a36Sopenharmony_ciinitial system user namespace, affectively preventing privilege escalation. 7362306a36Sopenharmony_ciUnfortunately, it is not generally feasible to use user namespaces in isolation, 7462306a36Sopenharmony_ciwithout pairing them with other namespace types, which is not always an option. 7562306a36Sopenharmony_ciLinux checks for capabilities based off of the user namespace that "owns" some 7662306a36Sopenharmony_cientity. For example, Linux has the notion that network namespaces are owned by 7762306a36Sopenharmony_cithe user namespace in which they were created. A consequence of this is that 7862306a36Sopenharmony_cicapability checks for access to a given network namespace are done by checking 7962306a36Sopenharmony_ciwhether a task has the given capability in the context of the user namespace 8062306a36Sopenharmony_cithat owns the network namespace -- not necessarily the user namespace under 8162306a36Sopenharmony_ciwhich the given task runs. Therefore spawning a process in a new user namespace 8262306a36Sopenharmony_cieffectively prevents it from accessing the network namespace owned by the 8362306a36Sopenharmony_ciinitial namespace. This is a deal-breaker for any application that expects to 8462306a36Sopenharmony_ciretain the CAP_NET_ADMIN capability for the purpose of adjusting network 8562306a36Sopenharmony_ciconfigurations. Using user namespaces in isolation causes problems regarding 8662306a36Sopenharmony_ciother system interactions, including use of pid namespaces and device creation. 8762306a36Sopenharmony_ci 8862306a36Sopenharmony_ciUse an existing LSM 8962306a36Sopenharmony_ci------------------- 9062306a36Sopenharmony_ciNone of the other in-tree LSMs have the capability to gate setid transitions, or 9162306a36Sopenharmony_cieven employ the security_task_fix_setuid hook at all. SELinux says of that hook: 9262306a36Sopenharmony_ci"Since setuid only affects the current process, and since the SELinux controls 9362306a36Sopenharmony_ciare not based on the Linux identity attributes, SELinux does not need to control 9462306a36Sopenharmony_cithis operation." 9562306a36Sopenharmony_ci 9662306a36Sopenharmony_ci 9762306a36Sopenharmony_ciDirections for use 9862306a36Sopenharmony_ci================== 9962306a36Sopenharmony_ciThis LSM hooks the setid syscalls to make sure transitions are allowed if an 10062306a36Sopenharmony_ciapplicable restriction policy is in place. Policies are configured through 10162306a36Sopenharmony_cisecurityfs by writing to the safesetid/uid_allowlist_policy and 10262306a36Sopenharmony_cisafesetid/gid_allowlist_policy files at the location where securityfs is 10362306a36Sopenharmony_cimounted. The format for adding a policy is '<UID>:<UID>' or '<GID>:<GID>', 10462306a36Sopenharmony_ciusing literal numbers, and ending with a newline character such as '123:456\n'. 10562306a36Sopenharmony_ciWriting an empty string "" will flush the policy. Again, configuring a policy 10662306a36Sopenharmony_cifor a UID/GID will prevent that UID/GID from obtaining auxiliary setid 10762306a36Sopenharmony_ciprivileges, such as allowing a user to set up user namespace UID/GID mappings. 10862306a36Sopenharmony_ci 10962306a36Sopenharmony_ciNote on GID policies and setgroups() 11062306a36Sopenharmony_ci==================================== 11162306a36Sopenharmony_ciIn v5.9 we are adding support for limiting CAP_SETGID privileges as was done 11262306a36Sopenharmony_cipreviously for CAP_SETUID. However, for compatibility with common sandboxing 11362306a36Sopenharmony_cirelated code conventions in userspace, we currently allow arbitrary 11462306a36Sopenharmony_cisetgroups() calls for processes with CAP_SETGID restrictions. Until we add 11562306a36Sopenharmony_cisupport in a future release for restricting setgroups() calls, these GID 11662306a36Sopenharmony_cipolicies add no meaningful security. setgroups() restrictions will be enforced 11762306a36Sopenharmony_cionce we have the policy checking code in place, which will rely on GID policy 11862306a36Sopenharmony_ciconfiguration code added in v5.9. 119