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 31b916b56bSRobert Watson #include <sys/cdefs.h> 32b916b56bSRobert Watson __FBSDID("$FreeBSD$"); 33b916b56bSRobert 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> 396efcc2f2SRobert Watson #include <sys/sdt.h> 40800c9408SRobert Watson #include <sys/sysctl.h> 41800c9408SRobert Watson #include <sys/systm.h> 42800c9408SRobert Watson 43800c9408SRobert Watson #include <security/mac/mac_framework.h> 44800c9408SRobert Watson 45800c9408SRobert Watson /* 46800c9408SRobert Watson * `suser_enabled' (which can be set by the security.bsd.suser_enabled 47800c9408SRobert Watson * sysctl) determines whether the system 'super-user' policy is in effect. If 48800c9408SRobert Watson * it is nonzero, an effective uid of 0 connotes special privilege, 49800c9408SRobert Watson * overriding many mandatory and discretionary protections. If it is zero, 50800c9408SRobert Watson * uid 0 is offered no special privilege in the kernel security policy. 51800c9408SRobert Watson * Setting it to zero may seriously impact the functionality of many existing 52800c9408SRobert Watson * userland programs, and should not be done without careful consideration of 53800c9408SRobert Watson * the consequences. 54800c9408SRobert Watson */ 55bc6eca24SRobert Watson static int suser_enabled = 1; 56*af3b2549SHans Petter Selasky SYSCTL_INT(_security_bsd, OID_AUTO, suser_enabled, CTLFLAG_RWTUN, 57800c9408SRobert Watson &suser_enabled, 0, "processes with uid 0 have privilege"); 58800c9408SRobert Watson 595eb0d283SAndrey Zonov static int unprivileged_mlock = 1; 60*af3b2549SHans Petter Selasky SYSCTL_INT(_security_bsd, OID_AUTO, unprivileged_mlock, CTLFLAG_RWTUN, 615eb0d283SAndrey Zonov &unprivileged_mlock, 0, "Allow non-root users to call mlock(2)"); 625eb0d283SAndrey Zonov 636efcc2f2SRobert Watson SDT_PROVIDER_DEFINE(priv); 64d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(priv, kernel, priv_check, priv__ok, "int"); 65d9fae5abSAndriy Gapon SDT_PROBE_DEFINE1(priv, kernel, priv_check, priv__err, "int"); 666efcc2f2SRobert Watson 67800c9408SRobert Watson /* 68800c9408SRobert Watson * Check a credential for privilege. Lots of good reasons to deny privilege; 69800c9408SRobert Watson * only a few to grant it. 70800c9408SRobert Watson */ 71800c9408SRobert Watson int 72800c9408SRobert Watson priv_check_cred(struct ucred *cred, int priv, int flags) 73800c9408SRobert Watson { 74800c9408SRobert Watson int error; 75800c9408SRobert Watson 76800c9408SRobert Watson KASSERT(PRIV_VALID(priv), ("priv_check_cred: invalid privilege %d", 77800c9408SRobert Watson priv)); 78800c9408SRobert Watson 797251b786SRobert Watson /* 807251b786SRobert Watson * We first evaluate policies that may deny the granting of 817251b786SRobert Watson * privilege unilaterally. 827251b786SRobert Watson */ 83800c9408SRobert Watson #ifdef MAC 84800c9408SRobert Watson error = mac_priv_check(cred, priv); 85800c9408SRobert Watson if (error) 866efcc2f2SRobert Watson goto out; 87800c9408SRobert Watson #endif 88800c9408SRobert Watson 89800c9408SRobert Watson /* 90800c9408SRobert Watson * Jail policy will restrict certain privileges that may otherwise be 91800c9408SRobert Watson * be granted. 92800c9408SRobert Watson */ 93800c9408SRobert Watson error = prison_priv_check(cred, priv); 94800c9408SRobert Watson if (error) 956efcc2f2SRobert Watson goto out; 96800c9408SRobert Watson 975eb0d283SAndrey Zonov if (unprivileged_mlock) { 985eb0d283SAndrey Zonov /* 995eb0d283SAndrey Zonov * Allow unprivileged users to call mlock(2)/munlock(2) and 1005eb0d283SAndrey Zonov * mlockall(2)/munlockall(2). 1015eb0d283SAndrey Zonov */ 1025eb0d283SAndrey Zonov switch (priv) { 1035eb0d283SAndrey Zonov case PRIV_VM_MLOCK: 1045eb0d283SAndrey Zonov case PRIV_VM_MUNLOCK: 1055eb0d283SAndrey Zonov error = 0; 1065eb0d283SAndrey Zonov goto out; 1075eb0d283SAndrey Zonov } 1085eb0d283SAndrey Zonov } 1095eb0d283SAndrey Zonov 110800c9408SRobert Watson /* 111800c9408SRobert Watson * Having determined if privilege is restricted by various policies, 1127251b786SRobert Watson * now determine if privilege is granted. At this point, any policy 1137251b786SRobert Watson * may grant privilege. For now, we allow short-circuit boolean 1147251b786SRobert Watson * evaluation, so may not call all policies. Perhaps we should. 115800c9408SRobert Watson * 116800c9408SRobert Watson * Superuser policy grants privilege based on the effective (or in 1177251b786SRobert Watson * the case of specific privileges, real) uid being 0. We allow the 1187251b786SRobert Watson * superuser policy to be globally disabled, although this is 1197251b786SRobert Watson * currenty of limited utility. 120800c9408SRobert Watson */ 121800c9408SRobert Watson if (suser_enabled) { 1227251b786SRobert Watson switch (priv) { 1237251b786SRobert Watson case PRIV_MAXFILES: 1247251b786SRobert Watson case PRIV_MAXPROC: 1257251b786SRobert Watson case PRIV_PROC_LIMIT: 1266efcc2f2SRobert Watson if (cred->cr_ruid == 0) { 1276efcc2f2SRobert Watson error = 0; 1286efcc2f2SRobert Watson goto out; 1296efcc2f2SRobert Watson } 1307251b786SRobert Watson break; 1317251b786SRobert Watson default: 1326efcc2f2SRobert Watson if (cred->cr_uid == 0) { 1336efcc2f2SRobert Watson error = 0; 1346efcc2f2SRobert Watson goto out; 1356efcc2f2SRobert Watson } 1367251b786SRobert Watson break; 137800c9408SRobert Watson } 138800c9408SRobert Watson } 139800c9408SRobert Watson 140800c9408SRobert Watson /* 1411e7df843SJamie Gritton * Writes to kernel/physical memory are a typical root-only operation, 1421e7df843SJamie Gritton * but non-root users are expected to be able to read it (provided they 1431e7df843SJamie Gritton * have permission to access /dev/[k]mem). 144c71e3362SJamie Gritton */ 145c71e3362SJamie Gritton if (priv == PRIV_KMEM_READ) { 146c71e3362SJamie Gritton error = 0; 147c71e3362SJamie Gritton goto out; 148c71e3362SJamie Gritton } 149c71e3362SJamie Gritton 150c71e3362SJamie Gritton /* 151800c9408SRobert Watson * Now check with MAC, if enabled, to see if a policy module grants 152800c9408SRobert Watson * privilege. 153800c9408SRobert Watson */ 154800c9408SRobert Watson #ifdef MAC 1556efcc2f2SRobert Watson if (mac_priv_grant(cred, priv) == 0) { 1566efcc2f2SRobert Watson error = 0; 1576efcc2f2SRobert Watson goto out; 1586efcc2f2SRobert Watson } 159800c9408SRobert Watson #endif 1606efcc2f2SRobert Watson 1616efcc2f2SRobert Watson /* 1626efcc2f2SRobert Watson * The default is deny, so if no policies have granted it, reject 1636efcc2f2SRobert Watson * with a privilege error here. 1646efcc2f2SRobert Watson */ 1656efcc2f2SRobert Watson error = EPERM; 1666efcc2f2SRobert Watson out: 1679b1040a5SPawel Jakub Dawidek if (error) 168d9fae5abSAndriy Gapon SDT_PROBE1(priv, kernel, priv_check, priv__err, priv); 1699b1040a5SPawel Jakub Dawidek else 170d9fae5abSAndriy Gapon SDT_PROBE1(priv, kernel, priv_check, priv__ok, priv); 1716efcc2f2SRobert Watson return (error); 172800c9408SRobert Watson } 173800c9408SRobert Watson 174800c9408SRobert Watson int 175800c9408SRobert Watson priv_check(struct thread *td, int priv) 176800c9408SRobert Watson { 177800c9408SRobert Watson 178800c9408SRobert Watson KASSERT(td == curthread, ("priv_check: td != curthread")); 179800c9408SRobert Watson 180800c9408SRobert Watson return (priv_check_cred(td->td_ucred, priv, 0)); 181800c9408SRobert Watson } 182