1800c9408SRobert Watson /*- 2800c9408SRobert Watson * Copyright (c) 2006 nCircle Network Security, Inc. 3800c9408SRobert Watson * All rights reserved. 4800c9408SRobert Watson * 5800c9408SRobert Watson * This software was developed by Robert N. M. Watson for the TrustedBSD 6800c9408SRobert Watson * Project under contract to nCircle Network Security, Inc. 7800c9408SRobert Watson * 8800c9408SRobert Watson * Redistribution and use in source and binary forms, with or without 9800c9408SRobert Watson * modification, are permitted provided that the following conditions 10800c9408SRobert Watson * are met: 11800c9408SRobert Watson * 1. Redistributions of source code must retain the above copyright 12800c9408SRobert Watson * notice, this list of conditions and the following disclaimer. 13800c9408SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 14800c9408SRobert Watson * notice, this list of conditions and the following disclaimer in the 15800c9408SRobert Watson * documentation and/or other materials provided with the distribution. 16800c9408SRobert Watson * 17800c9408SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 18800c9408SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 19800c9408SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 20800c9408SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY, 21800c9408SRobert Watson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 22800c9408SRobert Watson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 23800c9408SRobert Watson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24800c9408SRobert Watson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 25800c9408SRobert Watson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 26800c9408SRobert Watson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27800c9408SRobert Watson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28800c9408SRobert Watson * 29800c9408SRobert Watson * $FreeBSD$ 30800c9408SRobert Watson */ 31800c9408SRobert Watson 32800c9408SRobert Watson #include "opt_mac.h" 33800c9408SRobert Watson 34800c9408SRobert Watson #include <sys/param.h> 35800c9408SRobert Watson #include <sys/jail.h> 36800c9408SRobert Watson #include <sys/kernel.h> 37800c9408SRobert Watson #include <sys/priv.h> 38800c9408SRobert Watson #include <sys/proc.h> 39800c9408SRobert Watson #include <sys/sysctl.h> 40800c9408SRobert Watson #include <sys/systm.h> 41800c9408SRobert Watson 42800c9408SRobert Watson #include <security/mac/mac_framework.h> 43800c9408SRobert Watson 44800c9408SRobert Watson /* 45800c9408SRobert Watson * `suser_enabled' (which can be set by the security.bsd.suser_enabled 46800c9408SRobert Watson * sysctl) determines whether the system 'super-user' policy is in effect. If 47800c9408SRobert Watson * it is nonzero, an effective uid of 0 connotes special privilege, 48800c9408SRobert Watson * overriding many mandatory and discretionary protections. If it is zero, 49800c9408SRobert Watson * uid 0 is offered no special privilege in the kernel security policy. 50800c9408SRobert Watson * Setting it to zero may seriously impact the functionality of many existing 51800c9408SRobert Watson * userland programs, and should not be done without careful consideration of 52800c9408SRobert Watson * the consequences. 53800c9408SRobert Watson */ 54800c9408SRobert Watson int suser_enabled = 1; 55800c9408SRobert Watson SYSCTL_INT(_security_bsd, OID_AUTO, suser_enabled, CTLFLAG_RW, 56800c9408SRobert Watson &suser_enabled, 0, "processes with uid 0 have privilege"); 57800c9408SRobert Watson TUNABLE_INT("security.bsd.suser_enabled", &suser_enabled); 58800c9408SRobert Watson 59800c9408SRobert Watson /* 60800c9408SRobert Watson * Check a credential for privilege. Lots of good reasons to deny privilege; 61800c9408SRobert Watson * only a few to grant it. 62800c9408SRobert Watson */ 63800c9408SRobert Watson int 64800c9408SRobert Watson priv_check_cred(struct ucred *cred, int priv, int flags) 65800c9408SRobert Watson { 66800c9408SRobert Watson int error; 67800c9408SRobert Watson 68800c9408SRobert Watson KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege %d", 69800c9408SRobert Watson priv)); 70800c9408SRobert Watson 717251b786SRobert Watson /* 727251b786SRobert Watson * We first evaluate policies that may deny the granting of 737251b786SRobert Watson * privilege unilaterally. 747251b786SRobert Watson */ 75800c9408SRobert Watson #ifdef MAC 76800c9408SRobert Watson error = mac_priv_check(cred, priv); 77800c9408SRobert Watson if (error) 78800c9408SRobert Watson return (error); 79800c9408SRobert Watson #endif 80800c9408SRobert Watson 81800c9408SRobert Watson /* 82800c9408SRobert Watson * Jail policy will restrict certain privileges that may otherwise be 83800c9408SRobert Watson * be granted. 84800c9408SRobert Watson */ 85800c9408SRobert Watson error = prison_priv_check(cred, priv); 86800c9408SRobert Watson if (error) 87800c9408SRobert Watson return (error); 88800c9408SRobert Watson 89800c9408SRobert Watson /* 90800c9408SRobert Watson * Having determined if privilege is restricted by various policies, 917251b786SRobert Watson * now determine if privilege is granted. At this point, any policy 927251b786SRobert Watson * may grant privilege. For now, we allow short-circuit boolean 937251b786SRobert Watson * evaluation, so may not call all policies. Perhaps we should. 94800c9408SRobert Watson * 95800c9408SRobert Watson * Superuser policy grants privilege based on the effective (or in 967251b786SRobert Watson * the case of specific privileges, real) uid being 0. We allow the 977251b786SRobert Watson * superuser policy to be globally disabled, although this is 987251b786SRobert Watson * currenty of limited utility. 99800c9408SRobert Watson */ 100800c9408SRobert Watson if (suser_enabled) { 1017251b786SRobert Watson switch (priv) { 1027251b786SRobert Watson case PRIV_MAXFILES: 1037251b786SRobert Watson case PRIV_MAXPROC: 1047251b786SRobert Watson case PRIV_PROC_LIMIT: 105800c9408SRobert Watson if (cred->cr_ruid == 0) 106800c9408SRobert Watson return (0); 1077251b786SRobert Watson break; 1087251b786SRobert Watson 1097251b786SRobert Watson default: 110800c9408SRobert Watson if (cred->cr_uid == 0) 111800c9408SRobert Watson return (0); 1127251b786SRobert Watson break; 113800c9408SRobert Watson } 114800c9408SRobert Watson } 115800c9408SRobert Watson 116800c9408SRobert Watson /* 117800c9408SRobert Watson * Now check with MAC, if enabled, to see if a policy module grants 118800c9408SRobert Watson * privilege. 119800c9408SRobert Watson */ 120800c9408SRobert Watson #ifdef MAC 121800c9408SRobert Watson if (mac_priv_grant(cred, priv) == 0) 122800c9408SRobert Watson return (0); 123800c9408SRobert Watson #endif 124800c9408SRobert Watson return (EPERM); 125800c9408SRobert Watson } 126800c9408SRobert Watson 127800c9408SRobert Watson int 128800c9408SRobert Watson priv_check(struct thread *td, int priv) 129800c9408SRobert Watson { 130800c9408SRobert Watson 131800c9408SRobert Watson KASSERT(td == curthread, ("priv_check: td != curthread")); 132800c9408SRobert Watson 133800c9408SRobert Watson return (priv_check_cred(td->td_ucred, priv, 0)); 134800c9408SRobert Watson } 135800c9408SRobert Watson 136800c9408SRobert Watson /* 137800c9408SRobert Watson * Historical suser() wrapper functions, which now simply request PRIV_ROOT. 138800c9408SRobert Watson * These will be removed in the near future, and exist solely because 139800c9408SRobert Watson * the kernel and modules are not yet fully adapted to the new model. 140800c9408SRobert Watson */ 141800c9408SRobert Watson int 142800c9408SRobert Watson suser_cred(struct ucred *cred, int flags) 143800c9408SRobert Watson { 144800c9408SRobert Watson 145800c9408SRobert Watson return (priv_check_cred(cred, PRIV_ROOT, flags)); 146800c9408SRobert Watson } 147800c9408SRobert Watson 148800c9408SRobert Watson int 149800c9408SRobert Watson suser(struct thread *td) 150800c9408SRobert Watson { 151800c9408SRobert Watson 152800c9408SRobert Watson KASSERT(td == curthread, ("suser: td != curthread")); 153800c9408SRobert Watson 154800c9408SRobert Watson return (suser_cred(td->td_ucred, 0)); 155800c9408SRobert Watson } 156