1800c9408SRobert Watson /*- 2800c9408SRobert Watson * Copyright (c) 2006 nCircle Network Security, Inc. 36efcc2f2SRobert Watson * Copyright (c) 2009 Robert N. M. Watson 4800c9408SRobert Watson * All rights reserved. 5800c9408SRobert Watson * 6800c9408SRobert Watson * This software was developed by Robert N. M. Watson for the TrustedBSD 7800c9408SRobert Watson * Project under contract to nCircle Network Security, Inc. 8800c9408SRobert Watson * 9800c9408SRobert Watson * Redistribution and use in source and binary forms, with or without 10800c9408SRobert Watson * modification, are permitted provided that the following conditions 11800c9408SRobert Watson * are met: 12800c9408SRobert Watson * 1. Redistributions of source code must retain the above copyright 13800c9408SRobert Watson * notice, this list of conditions and the following disclaimer. 14800c9408SRobert Watson * 2. Redistributions in binary form must reproduce the above copyright 15800c9408SRobert Watson * notice, this list of conditions and the following disclaimer in the 16800c9408SRobert Watson * documentation and/or other materials provided with the distribution. 17800c9408SRobert Watson * 18800c9408SRobert Watson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 19800c9408SRobert Watson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20800c9408SRobert Watson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21800c9408SRobert Watson * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR, NCIRCLE NETWORK SECURITY, 22800c9408SRobert Watson * INC., OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 23800c9408SRobert Watson * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 24800c9408SRobert Watson * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 25800c9408SRobert Watson * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 26800c9408SRobert Watson * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 27800c9408SRobert Watson * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 28800c9408SRobert Watson * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 29800c9408SRobert Watson */ 30800c9408SRobert Watson 316efcc2f2SRobert Watson #include "opt_kdtrace.h" 32800c9408SRobert Watson #include "opt_mac.h" 33800c9408SRobert Watson 34b916b56bSRobert Watson #include <sys/cdefs.h> 35b916b56bSRobert Watson __FBSDID("$FreeBSD$"); 36b916b56bSRobert Watson 37800c9408SRobert Watson #include <sys/param.h> 38800c9408SRobert Watson #include <sys/jail.h> 39800c9408SRobert Watson #include <sys/kernel.h> 40800c9408SRobert Watson #include <sys/priv.h> 41800c9408SRobert Watson #include <sys/proc.h> 426efcc2f2SRobert Watson #include <sys/sdt.h> 43800c9408SRobert Watson #include <sys/sysctl.h> 44800c9408SRobert Watson #include <sys/systm.h> 45800c9408SRobert Watson 46800c9408SRobert Watson #include <security/mac/mac_framework.h> 47800c9408SRobert Watson 48800c9408SRobert Watson /* 49800c9408SRobert Watson * `suser_enabled' (which can be set by the security.bsd.suser_enabled 50800c9408SRobert Watson * sysctl) determines whether the system 'super-user' policy is in effect. If 51800c9408SRobert Watson * it is nonzero, an effective uid of 0 connotes special privilege, 52800c9408SRobert Watson * overriding many mandatory and discretionary protections. If it is zero, 53800c9408SRobert Watson * uid 0 is offered no special privilege in the kernel security policy. 54800c9408SRobert Watson * Setting it to zero may seriously impact the functionality of many existing 55800c9408SRobert Watson * userland programs, and should not be done without careful consideration of 56800c9408SRobert Watson * the consequences. 57800c9408SRobert Watson */ 58bc6eca24SRobert Watson static int suser_enabled = 1; 59800c9408SRobert Watson SYSCTL_INT(_security_bsd, OID_AUTO, suser_enabled, CTLFLAG_RW, 60800c9408SRobert Watson &suser_enabled, 0, "processes with uid 0 have privilege"); 61800c9408SRobert Watson TUNABLE_INT("security.bsd.suser_enabled", &suser_enabled); 62800c9408SRobert Watson 636efcc2f2SRobert Watson SDT_PROVIDER_DEFINE(priv); 646efcc2f2SRobert Watson 656efcc2f2SRobert Watson SDT_PROBE_DEFINE(priv, kernel, priv_check, priv_ok); 666efcc2f2SRobert Watson SDT_PROBE_ARGTYPE(priv, kernel, priv_check, priv_ok, 0, "int"); 676efcc2f2SRobert Watson 686efcc2f2SRobert Watson SDT_PROBE_DEFINE(priv, kernel, priv_check, priv_err); 696efcc2f2SRobert Watson SDT_PROBE_ARGTYPE(priv, kernel, priv_check, priv_err, 0, "int"); 706efcc2f2SRobert Watson 71800c9408SRobert Watson /* 72800c9408SRobert Watson * Check a credential for privilege. Lots of good reasons to deny privilege; 73800c9408SRobert Watson * only a few to grant it. 74800c9408SRobert Watson */ 75800c9408SRobert Watson int 76800c9408SRobert Watson priv_check_cred(struct ucred *cred, int priv, int flags) 77800c9408SRobert Watson { 78800c9408SRobert Watson int error; 79800c9408SRobert Watson 80800c9408SRobert Watson KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege %d", 81800c9408SRobert Watson priv)); 82800c9408SRobert Watson 837251b786SRobert Watson /* 847251b786SRobert Watson * We first evaluate policies that may deny the granting of 857251b786SRobert Watson * privilege unilaterally. 867251b786SRobert Watson */ 87800c9408SRobert Watson #ifdef MAC 88800c9408SRobert Watson error = mac_priv_check(cred, priv); 89800c9408SRobert Watson if (error) 906efcc2f2SRobert Watson goto out; 91800c9408SRobert Watson #endif 92800c9408SRobert Watson 93800c9408SRobert Watson /* 94800c9408SRobert Watson * Jail policy will restrict certain privileges that may otherwise be 95800c9408SRobert Watson * be granted. 96800c9408SRobert Watson */ 97800c9408SRobert Watson error = prison_priv_check(cred, priv); 98800c9408SRobert Watson if (error) 996efcc2f2SRobert Watson goto out; 100800c9408SRobert Watson 101800c9408SRobert Watson /* 102800c9408SRobert Watson * Having determined if privilege is restricted by various policies, 1037251b786SRobert Watson * now determine if privilege is granted. At this point, any policy 1047251b786SRobert Watson * may grant privilege. For now, we allow short-circuit boolean 1057251b786SRobert Watson * evaluation, so may not call all policies. Perhaps we should. 106800c9408SRobert Watson * 107800c9408SRobert Watson * Superuser policy grants privilege based on the effective (or in 1087251b786SRobert Watson * the case of specific privileges, real) uid being 0. We allow the 1097251b786SRobert Watson * superuser policy to be globally disabled, although this is 1107251b786SRobert Watson * currenty of limited utility. 111800c9408SRobert Watson */ 112800c9408SRobert Watson if (suser_enabled) { 1137251b786SRobert Watson switch (priv) { 1147251b786SRobert Watson case PRIV_MAXFILES: 1157251b786SRobert Watson case PRIV_MAXPROC: 1167251b786SRobert Watson case PRIV_PROC_LIMIT: 1176efcc2f2SRobert Watson if (cred->cr_ruid == 0) { 1186efcc2f2SRobert Watson error = 0; 1196efcc2f2SRobert Watson goto out; 1206efcc2f2SRobert Watson } 1217251b786SRobert Watson break; 1227251b786SRobert Watson 1237251b786SRobert Watson default: 1246efcc2f2SRobert Watson if (cred->cr_uid == 0) { 1256efcc2f2SRobert Watson error = 0; 1266efcc2f2SRobert Watson goto out; 1276efcc2f2SRobert Watson } 1287251b786SRobert Watson break; 129800c9408SRobert Watson } 130800c9408SRobert Watson } 131800c9408SRobert Watson 132800c9408SRobert Watson /* 133800c9408SRobert Watson * Now check with MAC, if enabled, to see if a policy module grants 134800c9408SRobert Watson * privilege. 135800c9408SRobert Watson */ 136800c9408SRobert Watson #ifdef MAC 1376efcc2f2SRobert Watson if (mac_priv_grant(cred, priv) == 0) { 1386efcc2f2SRobert Watson error = 0; 1396efcc2f2SRobert Watson goto out; 1406efcc2f2SRobert Watson } 141800c9408SRobert Watson #endif 1426efcc2f2SRobert Watson 1436efcc2f2SRobert Watson /* 1446efcc2f2SRobert Watson * The default is deny, so if no policies have granted it, reject 1456efcc2f2SRobert Watson * with a privilege error here. 1466efcc2f2SRobert Watson */ 1476efcc2f2SRobert Watson error = EPERM; 1486efcc2f2SRobert Watson out: 1496efcc2f2SRobert Watson if (error) { 1506efcc2f2SRobert Watson SDT_PROBE(priv, kernel, priv_check, priv_err, priv, 0, 0, 0, 1516efcc2f2SRobert Watson 0); 1526efcc2f2SRobert Watson } else { 1536efcc2f2SRobert Watson SDT_PROBE(priv, kernel, priv_check, priv_ok, priv, 0, 0, 0, 1546efcc2f2SRobert Watson 0); 1556efcc2f2SRobert Watson } 1566efcc2f2SRobert Watson return (error); 157800c9408SRobert Watson } 158800c9408SRobert Watson 159800c9408SRobert Watson int 160800c9408SRobert Watson priv_check(struct thread *td, int priv) 161800c9408SRobert Watson { 162800c9408SRobert Watson 163800c9408SRobert Watson KASSERT(td == curthread, ("priv_check: td != curthread")); 164800c9408SRobert Watson 165800c9408SRobert Watson return (priv_check_cred(td->td_ucred, priv, 0)); 166800c9408SRobert Watson } 167