1*4882a593Smuzhiyun========= 2*4882a593SmuzhiyunSafeSetID 3*4882a593Smuzhiyun========= 4*4882a593SmuzhiyunSafeSetID is an LSM module that gates the setid family of syscalls to restrict 5*4882a593SmuzhiyunUID/GID transitions from a given UID/GID to only those approved by a 6*4882a593Smuzhiyunsystem-wide allowlist. These restrictions also prohibit the given UIDs/GIDs 7*4882a593Smuzhiyunfrom obtaining auxiliary privileges associated with CAP_SET{U/G}ID, such as 8*4882a593Smuzhiyunallowing a user to set up user namespace UID/GID mappings. 9*4882a593Smuzhiyun 10*4882a593Smuzhiyun 11*4882a593SmuzhiyunBackground 12*4882a593Smuzhiyun========== 13*4882a593SmuzhiyunIn absence of file capabilities, processes spawned on a Linux system that need 14*4882a593Smuzhiyunto switch to a different user must be spawned with CAP_SETUID privileges. 15*4882a593SmuzhiyunCAP_SETUID is granted to programs running as root or those running as a non-root 16*4882a593Smuzhiyunuser that have been explicitly given the CAP_SETUID runtime capability. It is 17*4882a593Smuzhiyunoften preferable to use Linux runtime capabilities rather than file 18*4882a593Smuzhiyuncapabilities, since using file capabilities to run a program with elevated 19*4882a593Smuzhiyunprivileges opens up possible security holes since any user with access to the 20*4882a593Smuzhiyunfile can exec() that program to gain the elevated privileges. 21*4882a593Smuzhiyun 22*4882a593SmuzhiyunWhile it is possible to implement a tree of processes by giving full 23*4882a593SmuzhiyunCAP_SET{U/G}ID capabilities, this is often at odds with the goals of running a 24*4882a593Smuzhiyuntree of processes under non-root user(s) in the first place. Specifically, 25*4882a593Smuzhiyunsince CAP_SETUID allows changing to any user on the system, including the root 26*4882a593Smuzhiyunuser, it is an overpowered capability for what is needed in this scenario, 27*4882a593Smuzhiyunespecially since programs often only call setuid() to drop privileges to a 28*4882a593Smuzhiyunlesser-privileged user -- not elevate privileges. Unfortunately, there is no 29*4882a593Smuzhiyungenerally feasible way in Linux to restrict the potential UIDs that a user can 30*4882a593Smuzhiyunswitch to through setuid() beyond allowing a switch to any user on the system. 31*4882a593SmuzhiyunThis SafeSetID LSM seeks to provide a solution for restricting setid 32*4882a593Smuzhiyuncapabilities in such a way. 33*4882a593Smuzhiyun 34*4882a593SmuzhiyunThe main use case for this LSM is to allow a non-root program to transition to 35*4882a593Smuzhiyunother untrusted uids without full blown CAP_SETUID capabilities. The non-root 36*4882a593Smuzhiyunprogram would still need CAP_SETUID to do any kind of transition, but the 37*4882a593Smuzhiyunadditional restrictions imposed by this LSM would mean it is a "safer" version 38*4882a593Smuzhiyunof CAP_SETUID since the non-root program cannot take advantage of CAP_SETUID to 39*4882a593Smuzhiyundo any unapproved actions (e.g. setuid to uid 0 or create/enter new user 40*4882a593Smuzhiyunnamespace). The higher level goal is to allow for uid-based sandboxing of system 41*4882a593Smuzhiyunservices without having to give out CAP_SETUID all over the place just so that 42*4882a593Smuzhiyunnon-root programs can drop to even-lesser-privileged uids. This is especially 43*4882a593Smuzhiyunrelevant when one non-root daemon on the system should be allowed to spawn other 44*4882a593Smuzhiyunprocesses as different uids, but its undesirable to give the daemon a 45*4882a593Smuzhiyunbasically-root-equivalent CAP_SETUID. 46*4882a593Smuzhiyun 47*4882a593Smuzhiyun 48*4882a593SmuzhiyunOther Approaches Considered 49*4882a593Smuzhiyun=========================== 50*4882a593Smuzhiyun 51*4882a593SmuzhiyunSolve this problem in userspace 52*4882a593Smuzhiyun------------------------------- 53*4882a593SmuzhiyunFor candidate applications that would like to have restricted setid capabilities 54*4882a593Smuzhiyunas implemented in this LSM, an alternative option would be to simply take away 55*4882a593Smuzhiyunsetid capabilities from the application completely and refactor the process 56*4882a593Smuzhiyunspawning semantics in the application (e.g. by using a privileged helper program 57*4882a593Smuzhiyunto do process spawning and UID/GID transitions). Unfortunately, there are a 58*4882a593Smuzhiyunnumber of semantics around process spawning that would be affected by this, such 59*4882a593Smuzhiyunas fork() calls where the program doesn't immediately call exec() after the 60*4882a593Smuzhiyunfork(), parent processes specifying custom environment variables or command line 61*4882a593Smuzhiyunargs for spawned child processes, or inheritance of file handles across a 62*4882a593Smuzhiyunfork()/exec(). Because of this, as solution that uses a privileged helper in 63*4882a593Smuzhiyunuserspace would likely be less appealing to incorporate into existing projects 64*4882a593Smuzhiyunthat rely on certain process-spawning semantics in Linux. 65*4882a593Smuzhiyun 66*4882a593SmuzhiyunUse user namespaces 67*4882a593Smuzhiyun------------------- 68*4882a593SmuzhiyunAnother possible approach would be to run a given process tree in its own user 69*4882a593Smuzhiyunnamespace and give programs in the tree setid capabilities. In this way, 70*4882a593Smuzhiyunprograms in the tree could change to any desired UID/GID in the context of their 71*4882a593Smuzhiyunown user namespace, and only approved UIDs/GIDs could be mapped back to the 72*4882a593Smuzhiyuninitial system user namespace, affectively preventing privilege escalation. 73*4882a593SmuzhiyunUnfortunately, it is not generally feasible to use user namespaces in isolation, 74*4882a593Smuzhiyunwithout pairing them with other namespace types, which is not always an option. 75*4882a593SmuzhiyunLinux checks for capabilities based off of the user namespace that "owns" some 76*4882a593Smuzhiyunentity. For example, Linux has the notion that network namespaces are owned by 77*4882a593Smuzhiyunthe user namespace in which they were created. A consequence of this is that 78*4882a593Smuzhiyuncapability checks for access to a given network namespace are done by checking 79*4882a593Smuzhiyunwhether a task has the given capability in the context of the user namespace 80*4882a593Smuzhiyunthat owns the network namespace -- not necessarily the user namespace under 81*4882a593Smuzhiyunwhich the given task runs. Therefore spawning a process in a new user namespace 82*4882a593Smuzhiyuneffectively prevents it from accessing the network namespace owned by the 83*4882a593Smuzhiyuninitial namespace. This is a deal-breaker for any application that expects to 84*4882a593Smuzhiyunretain the CAP_NET_ADMIN capability for the purpose of adjusting network 85*4882a593Smuzhiyunconfigurations. Using user namespaces in isolation causes problems regarding 86*4882a593Smuzhiyunother system interactions, including use of pid namespaces and device creation. 87*4882a593Smuzhiyun 88*4882a593SmuzhiyunUse an existing LSM 89*4882a593Smuzhiyun------------------- 90*4882a593SmuzhiyunNone of the other in-tree LSMs have the capability to gate setid transitions, or 91*4882a593Smuzhiyuneven employ the security_task_fix_setuid hook at all. SELinux says of that hook: 92*4882a593Smuzhiyun"Since setuid only affects the current process, and since the SELinux controls 93*4882a593Smuzhiyunare not based on the Linux identity attributes, SELinux does not need to control 94*4882a593Smuzhiyunthis operation." 95*4882a593Smuzhiyun 96*4882a593Smuzhiyun 97*4882a593SmuzhiyunDirections for use 98*4882a593Smuzhiyun================== 99*4882a593SmuzhiyunThis LSM hooks the setid syscalls to make sure transitions are allowed if an 100*4882a593Smuzhiyunapplicable restriction policy is in place. Policies are configured through 101*4882a593Smuzhiyunsecurityfs by writing to the safesetid/uid_allowlist_policy and 102*4882a593Smuzhiyunsafesetid/gid_allowlist_policy files at the location where securityfs is 103*4882a593Smuzhiyunmounted. The format for adding a policy is '<UID>:<UID>' or '<GID>:<GID>', 104*4882a593Smuzhiyunusing literal numbers, and ending with a newline character such as '123:456\n'. 105*4882a593SmuzhiyunWriting an empty string "" will flush the policy. Again, configuring a policy 106*4882a593Smuzhiyunfor a UID/GID will prevent that UID/GID from obtaining auxiliary setid 107*4882a593Smuzhiyunprivileges, such as allowing a user to set up user namespace UID/GID mappings. 108*4882a593Smuzhiyun 109*4882a593SmuzhiyunNote on GID policies and setgroups() 110*4882a593Smuzhiyun==================================== 111*4882a593SmuzhiyunIn v5.9 we are adding support for limiting CAP_SETGID privileges as was done 112*4882a593Smuzhiyunpreviously for CAP_SETUID. However, for compatibility with common sandboxing 113*4882a593Smuzhiyunrelated code conventions in userspace, we currently allow arbitrary 114*4882a593Smuzhiyunsetgroups() calls for processes with CAP_SETGID restrictions. Until we add 115*4882a593Smuzhiyunsupport in a future release for restricting setgroups() calls, these GID 116*4882a593Smuzhiyunpolicies add no meaningful security. setgroups() restrictions will be enforced 117*4882a593Smuzhiyunonce we have the policy checking code in place, which will rely on GID policy 118*4882a593Smuzhiyunconfiguration code added in v5.9. 119