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 33b916b56bSRobert Watson #include <sys/cdefs.h> 34b916b56bSRobert Watson __FBSDID("$FreeBSD$"); 35b916b56bSRobert Watson 36800c9408SRobert Watson #include <sys/param.h> 37800c9408SRobert Watson #include <sys/jail.h> 38800c9408SRobert Watson #include <sys/kernel.h> 39800c9408SRobert Watson #include <sys/priv.h> 40800c9408SRobert Watson #include <sys/proc.h> 416efcc2f2SRobert Watson #include <sys/sdt.h> 42800c9408SRobert Watson #include <sys/sysctl.h> 43800c9408SRobert Watson #include <sys/systm.h> 44800c9408SRobert Watson 45800c9408SRobert Watson #include <security/mac/mac_framework.h> 46800c9408SRobert Watson 47800c9408SRobert Watson /* 48800c9408SRobert Watson * `suser_enabled' (which can be set by the security.bsd.suser_enabled 49800c9408SRobert Watson * sysctl) determines whether the system 'super-user' policy is in effect. If 50800c9408SRobert Watson * it is nonzero, an effective uid of 0 connotes special privilege, 51800c9408SRobert Watson * overriding many mandatory and discretionary protections. If it is zero, 52800c9408SRobert Watson * uid 0 is offered no special privilege in the kernel security policy. 53800c9408SRobert Watson * Setting it to zero may seriously impact the functionality of many existing 54800c9408SRobert Watson * userland programs, and should not be done without careful consideration of 55800c9408SRobert Watson * the consequences. 56800c9408SRobert Watson */ 57bc6eca24SRobert Watson static int suser_enabled = 1; 58800c9408SRobert Watson SYSCTL_INT(_security_bsd, OID_AUTO, suser_enabled, CTLFLAG_RW, 59800c9408SRobert Watson &suser_enabled, 0, "processes with uid 0 have privilege"); 60800c9408SRobert Watson TUNABLE_INT("security.bsd.suser_enabled", &suser_enabled); 61800c9408SRobert Watson 625eb0d283SAndrey Zonov static int unprivileged_mlock = 1; 635eb0d283SAndrey Zonov SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_mlock, CTLFLAG_RW|CTLFLAG_TUN, 645eb0d283SAndrey Zonov &unprivileged_mlock, 0, "Allow non-root users to call mlock(2)"); 655eb0d283SAndrey Zonov TUNABLE_INT("security.bsd.unprivileged_mlock", &unprivileged_mlock); 665eb0d283SAndrey Zonov 676efcc2f2SRobert Watson SDT_PROVIDER_DEFINE(priv); 6879856499SRui Paulo SDT_PROBE_DEFINE1(priv, kernel, priv_check, priv_ok, priv-ok, "int"); 6979856499SRui Paulo SDT_PROBE_DEFINE1(priv, kernel, priv_check, priv_err, priv-err, "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 1015eb0d283SAndrey Zonov if (unprivileged_mlock) { 1025eb0d283SAndrey Zonov /* 1035eb0d283SAndrey Zonov * Allow unprivileged users to call mlock(2)/munlock(2) and 1045eb0d283SAndrey Zonov * mlockall(2)/munlockall(2). 1055eb0d283SAndrey Zonov */ 1065eb0d283SAndrey Zonov switch (priv) { 1075eb0d283SAndrey Zonov case PRIV_VM_MLOCK: 1085eb0d283SAndrey Zonov case PRIV_VM_MUNLOCK: 1095eb0d283SAndrey Zonov error = 0; 1105eb0d283SAndrey Zonov goto out; 1115eb0d283SAndrey Zonov } 1125eb0d283SAndrey Zonov } 1135eb0d283SAndrey Zonov 114800c9408SRobert Watson /* 115800c9408SRobert Watson * Having determined if privilege is restricted by various policies, 1167251b786SRobert Watson * now determine if privilege is granted. At this point, any policy 1177251b786SRobert Watson * may grant privilege. For now, we allow short-circuit boolean 1187251b786SRobert Watson * evaluation, so may not call all policies. Perhaps we should. 119800c9408SRobert Watson * 120800c9408SRobert Watson * Superuser policy grants privilege based on the effective (or in 1217251b786SRobert Watson * the case of specific privileges, real) uid being 0. We allow the 1227251b786SRobert Watson * superuser policy to be globally disabled, although this is 1237251b786SRobert Watson * currenty of limited utility. 124800c9408SRobert Watson */ 125800c9408SRobert Watson if (suser_enabled) { 1267251b786SRobert Watson switch (priv) { 1277251b786SRobert Watson case PRIV_MAXFILES: 1287251b786SRobert Watson case PRIV_MAXPROC: 1297251b786SRobert Watson case PRIV_PROC_LIMIT: 1306efcc2f2SRobert Watson if (cred->cr_ruid == 0) { 1316efcc2f2SRobert Watson error = 0; 1326efcc2f2SRobert Watson goto out; 1336efcc2f2SRobert Watson } 1347251b786SRobert Watson break; 1357251b786SRobert Watson 1367251b786SRobert Watson default: 1376efcc2f2SRobert Watson if (cred->cr_uid == 0) { 1386efcc2f2SRobert Watson error = 0; 1396efcc2f2SRobert Watson goto out; 1406efcc2f2SRobert Watson } 1417251b786SRobert Watson break; 142800c9408SRobert Watson } 143800c9408SRobert Watson } 144800c9408SRobert Watson 145800c9408SRobert Watson /* 146800c9408SRobert Watson * Now check with MAC, if enabled, to see if a policy module grants 147800c9408SRobert Watson * privilege. 148800c9408SRobert Watson */ 149800c9408SRobert Watson #ifdef MAC 1506efcc2f2SRobert Watson if (mac_priv_grant(cred, priv) == 0) { 1516efcc2f2SRobert Watson error = 0; 1526efcc2f2SRobert Watson goto out; 1536efcc2f2SRobert Watson } 154800c9408SRobert Watson #endif 1556efcc2f2SRobert Watson 1566efcc2f2SRobert Watson /* 1576efcc2f2SRobert Watson * The default is deny, so if no policies have granted it, reject 1586efcc2f2SRobert Watson * with a privilege error here. 1596efcc2f2SRobert Watson */ 1606efcc2f2SRobert Watson error = EPERM; 1616efcc2f2SRobert Watson out: 1629b1040a5SPawel Jakub Dawidek if (error) 1639b1040a5SPawel Jakub Dawidek SDT_PROBE1(priv, kernel, priv_check, priv_err, priv); 1649b1040a5SPawel Jakub Dawidek else 1659b1040a5SPawel Jakub Dawidek SDT_PROBE1(priv, kernel, priv_check, priv_ok, priv); 1666efcc2f2SRobert Watson return (error); 167800c9408SRobert Watson } 168800c9408SRobert Watson 169800c9408SRobert Watson int 170800c9408SRobert Watson priv_check(struct thread *td, int priv) 171800c9408SRobert Watson { 172800c9408SRobert Watson 173800c9408SRobert Watson KASSERT(td == curthread, ("priv_check: td != curthread")); 174800c9408SRobert Watson 175800c9408SRobert Watson return (priv_check_cred(td->td_ucred, priv, 0)); 176800c9408SRobert Watson } 177