xref: /onnv-gate/usr/src/uts/common/os/policy.c (revision 12273:63678502e95e)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
51544Seschrock  * Common Development and Distribution License (the "License").
61544Seschrock  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
22*12273SCasper.Dik@Sun.COM  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
230Sstevel@tonic-gate  */
240Sstevel@tonic-gate 
250Sstevel@tonic-gate #include <sys/types.h>
260Sstevel@tonic-gate #include <sys/sysmacros.h>
270Sstevel@tonic-gate #include <sys/param.h>
280Sstevel@tonic-gate #include <sys/systm.h>
290Sstevel@tonic-gate #include <sys/cred_impl.h>
300Sstevel@tonic-gate #include <sys/vnode.h>
310Sstevel@tonic-gate #include <sys/vfs.h>
320Sstevel@tonic-gate #include <sys/stat.h>
330Sstevel@tonic-gate #include <sys/errno.h>
340Sstevel@tonic-gate #include <sys/kmem.h>
350Sstevel@tonic-gate #include <sys/user.h>
360Sstevel@tonic-gate #include <sys/proc.h>
370Sstevel@tonic-gate #include <sys/acct.h>
380Sstevel@tonic-gate #include <sys/ipc_impl.h>
390Sstevel@tonic-gate #include <sys/cmn_err.h>
400Sstevel@tonic-gate #include <sys/debug.h>
410Sstevel@tonic-gate #include <sys/policy.h>
420Sstevel@tonic-gate #include <sys/kobj.h>
430Sstevel@tonic-gate #include <sys/msg.h>
440Sstevel@tonic-gate #include <sys/devpolicy.h>
450Sstevel@tonic-gate #include <c2/audit.h>
460Sstevel@tonic-gate #include <sys/varargs.h>
476134Scasper #include <sys/klpd.h>
480Sstevel@tonic-gate #include <sys/modctl.h>
490Sstevel@tonic-gate #include <sys/disp.h>
500Sstevel@tonic-gate #include <sys/zone.h>
510Sstevel@tonic-gate #include <inet/optcom.h>
520Sstevel@tonic-gate #include <sys/sdt.h>
530Sstevel@tonic-gate #include <sys/vfs.h>
540Sstevel@tonic-gate #include <sys/mntent.h>
550Sstevel@tonic-gate #include <sys/contract_impl.h>
568275SEric Cheng #include <sys/dld_ioc.h>
570Sstevel@tonic-gate 
580Sstevel@tonic-gate /*
590Sstevel@tonic-gate  * There are two possible layers of privilege routines and two possible
600Sstevel@tonic-gate  * levels of secpolicy.  Plus one other we may not be interested in, so
610Sstevel@tonic-gate  * we may need as many as 6 but no more.
620Sstevel@tonic-gate  */
630Sstevel@tonic-gate #define	MAXPRIVSTACK		6
640Sstevel@tonic-gate 
650Sstevel@tonic-gate int priv_debug = 0;
6611537SCasper.Dik@Sun.COM int priv_basic_test = -1;
670Sstevel@tonic-gate 
680Sstevel@tonic-gate /*
690Sstevel@tonic-gate  * This file contains the majority of the policy routines.
700Sstevel@tonic-gate  * Since the policy routines are defined by function and not
710Sstevel@tonic-gate  * by privilege, there is quite a bit of duplication of
720Sstevel@tonic-gate  * functions.
730Sstevel@tonic-gate  *
745331Samw  * The secpolicy functions must not make assumptions about
750Sstevel@tonic-gate  * locks held or not held as any lock can be held while they're
760Sstevel@tonic-gate  * being called.
770Sstevel@tonic-gate  *
780Sstevel@tonic-gate  * Credentials are read-only so no special precautions need to
790Sstevel@tonic-gate  * be taken while locking them.
800Sstevel@tonic-gate  *
810Sstevel@tonic-gate  * When a new policy check needs to be added to the system the
820Sstevel@tonic-gate  * following procedure should be followed:
830Sstevel@tonic-gate  *
840Sstevel@tonic-gate  *		Pick an appropriate secpolicy_*() function
850Sstevel@tonic-gate  *			-> done if one exists.
860Sstevel@tonic-gate  *		Create a new secpolicy function, preferably with
870Sstevel@tonic-gate  *		a descriptive name using the standard template.
880Sstevel@tonic-gate  *		Pick an appropriate privilege for the policy.
890Sstevel@tonic-gate  *		If no appropraite privilege exists, define new one
900Sstevel@tonic-gate  *		(this should be done with extreme care; in most cases
910Sstevel@tonic-gate  *		little is gained by adding another privilege)
920Sstevel@tonic-gate  *
930Sstevel@tonic-gate  * WHY ROOT IS STILL SPECIAL.
940Sstevel@tonic-gate  *
950Sstevel@tonic-gate  * In a number of the policy functions, there are still explicit
960Sstevel@tonic-gate  * checks for uid 0.  The rationale behind these is that many root
970Sstevel@tonic-gate  * owned files/objects hold configuration information which can give full
980Sstevel@tonic-gate  * privileges to the user once written to.  To prevent escalation
990Sstevel@tonic-gate  * of privilege by allowing just a single privilege to modify root owned
1000Sstevel@tonic-gate  * objects, we've added these root specific checks where we considered
1010Sstevel@tonic-gate  * them necessary: modifying root owned files, changing uids to 0, etc.
1020Sstevel@tonic-gate  *
1030Sstevel@tonic-gate  * PRIVILEGE ESCALATION AND ZONES.
1040Sstevel@tonic-gate  *
1050Sstevel@tonic-gate  * A number of operations potentially allow the caller to achieve
1060Sstevel@tonic-gate  * privileges beyond the ones normally required to perform the operation.
1070Sstevel@tonic-gate  * For example, if allowed to create a setuid 0 executable, a process can
1080Sstevel@tonic-gate  * gain privileges beyond PRIV_FILE_SETID.  Zones, however, place
1090Sstevel@tonic-gate  * restrictions on the ability to gain privileges beyond those available
1100Sstevel@tonic-gate  * within the zone through file and process manipulation.  Hence, such
1110Sstevel@tonic-gate  * operations require that the caller have an effective set that includes
1120Sstevel@tonic-gate  * all privileges available within the current zone, or all privileges
1130Sstevel@tonic-gate  * if executing in the global zone.
1140Sstevel@tonic-gate  *
1150Sstevel@tonic-gate  * This is indicated in the priv_policy* policy checking functions
1160Sstevel@tonic-gate  * through a combination of parameters.  The "priv" parameter indicates
1170Sstevel@tonic-gate  * the privilege that is required, and the "allzone" parameter indicates
1180Sstevel@tonic-gate  * whether or not all privileges in the zone are required.  In addition,
1190Sstevel@tonic-gate  * priv can be set to PRIV_ALL to indicate that all privileges are
1200Sstevel@tonic-gate  * required (regardless of zone).  There are three scenarios of interest:
1210Sstevel@tonic-gate  * (1) operation requires a specific privilege
1220Sstevel@tonic-gate  * (2) operation requires a specific privilege, and requires all
1230Sstevel@tonic-gate  *     privileges available within the zone (or all privileges if in
1240Sstevel@tonic-gate  *     the global zone)
1250Sstevel@tonic-gate  * (3) operation requires all privileges, regardless of zone
1260Sstevel@tonic-gate  *
1270Sstevel@tonic-gate  * For (1), priv should be set to the specific privilege, and allzone
1280Sstevel@tonic-gate  * should be set to B_FALSE.
1290Sstevel@tonic-gate  * For (2), priv should be set to the specific privilege, and allzone
1300Sstevel@tonic-gate  * should be set to B_TRUE.
1310Sstevel@tonic-gate  * For (3), priv should be set to PRIV_ALL, and allzone should be set
1320Sstevel@tonic-gate  * to B_FALSE.
1330Sstevel@tonic-gate  *
1340Sstevel@tonic-gate  */
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate /*
1370Sstevel@tonic-gate  * The privileges are checked against the Effective set for
1380Sstevel@tonic-gate  * ordinary processes and checked against the Limit set
1390Sstevel@tonic-gate  * for euid 0 processes that haven't manipulated their privilege
1400Sstevel@tonic-gate  * sets.
1410Sstevel@tonic-gate  */
1420Sstevel@tonic-gate #define	HAS_ALLPRIVS(cr)	priv_isfullset(&CR_OEPRIV(cr))
1430Sstevel@tonic-gate #define	ZONEPRIVS(cr)		((cr)->cr_zone->zone_privset)
1440Sstevel@tonic-gate #define	HAS_ALLZONEPRIVS(cr)	priv_issubset(ZONEPRIVS(cr), &CR_OEPRIV(cr))
1450Sstevel@tonic-gate #define	HAS_PRIVILEGE(cr, pr)	((pr) == PRIV_ALL ? \
1460Sstevel@tonic-gate 					HAS_ALLPRIVS(cr) : \
1470Sstevel@tonic-gate 					PRIV_ISASSERT(&CR_OEPRIV(cr), pr))
1480Sstevel@tonic-gate 
149*12273SCasper.Dik@Sun.COM #define	FAST_BASIC_CHECK(cr, priv)	\
150*12273SCasper.Dik@Sun.COM 	if (PRIV_ISASSERT(&CR_OEPRIV(cr), priv)) { \
151*12273SCasper.Dik@Sun.COM 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, B_FALSE); \
152*12273SCasper.Dik@Sun.COM 		return (0); \
153*12273SCasper.Dik@Sun.COM 	}
154*12273SCasper.Dik@Sun.COM 
1550Sstevel@tonic-gate /*
1566134Scasper  * Policy checking functions.
1570Sstevel@tonic-gate  *
1586134Scasper  * All of the system's policy should be implemented here.
1590Sstevel@tonic-gate  */
1600Sstevel@tonic-gate 
1610Sstevel@tonic-gate /*
1626134Scasper  * Private functions which take an additional va_list argument to
1636134Scasper  * implement an object specific policy override.
1646134Scasper  */
1656134Scasper static int priv_policy_ap(const cred_t *, int, boolean_t, int,
1666134Scasper     const char *, va_list);
1676134Scasper static int priv_policy_va(const cred_t *, int, boolean_t, int,
1686134Scasper     const char *, ...);
1696134Scasper 
1706134Scasper /*
1710Sstevel@tonic-gate  * Generic policy calls
1720Sstevel@tonic-gate  *
1730Sstevel@tonic-gate  * The "bottom" functions of policy control
1740Sstevel@tonic-gate  */
1750Sstevel@tonic-gate static char *
1760Sstevel@tonic-gate mprintf(const char *fmt, ...)
1770Sstevel@tonic-gate {
1780Sstevel@tonic-gate 	va_list args;
1790Sstevel@tonic-gate 	char *buf;
1800Sstevel@tonic-gate 	size_t len;
1810Sstevel@tonic-gate 
1820Sstevel@tonic-gate 	va_start(args, fmt);
1830Sstevel@tonic-gate 	len = vsnprintf(NULL, 0, fmt, args) + 1;
1840Sstevel@tonic-gate 	va_end(args);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	buf = kmem_alloc(len, KM_NOSLEEP);
1870Sstevel@tonic-gate 
1880Sstevel@tonic-gate 	if (buf == NULL)
1890Sstevel@tonic-gate 		return (NULL);
1900Sstevel@tonic-gate 
1910Sstevel@tonic-gate 	va_start(args, fmt);
1920Sstevel@tonic-gate 	(void) vsnprintf(buf, len, fmt, args);
1930Sstevel@tonic-gate 	va_end(args);
1940Sstevel@tonic-gate 
1950Sstevel@tonic-gate 	return (buf);
1960Sstevel@tonic-gate }
1970Sstevel@tonic-gate 
1980Sstevel@tonic-gate /*
1990Sstevel@tonic-gate  * priv_policy_errmsg()
2000Sstevel@tonic-gate  *
2010Sstevel@tonic-gate  * Generate an error message if privilege debugging is enabled system wide
2020Sstevel@tonic-gate  * or for this particular process.
2030Sstevel@tonic-gate  */
2040Sstevel@tonic-gate 
2050Sstevel@tonic-gate #define	FMTHDR	"%s[%d]: missing privilege \"%s\" (euid = %d, syscall = %d)"
2060Sstevel@tonic-gate #define	FMTMSG	" for \"%s\""
2070Sstevel@tonic-gate #define	FMTFUN	" needed at %s+0x%lx"
2080Sstevel@tonic-gate 
2090Sstevel@tonic-gate /* The maximum size privilege format: the concatenation of the above */
2100Sstevel@tonic-gate #define	FMTMAX	FMTHDR FMTMSG FMTFUN "\n"
2110Sstevel@tonic-gate 
2120Sstevel@tonic-gate static void
2130Sstevel@tonic-gate priv_policy_errmsg(const cred_t *cr, int priv, const char *msg)
2140Sstevel@tonic-gate {
2150Sstevel@tonic-gate 	struct proc *me;
2160Sstevel@tonic-gate 	pc_t stack[MAXPRIVSTACK];
2170Sstevel@tonic-gate 	int depth;
2180Sstevel@tonic-gate 	int i;
2190Sstevel@tonic-gate 	char *sym;
2200Sstevel@tonic-gate 	ulong_t off;
2210Sstevel@tonic-gate 	const char *pname;
2220Sstevel@tonic-gate 
2230Sstevel@tonic-gate 	char *cmd;
2240Sstevel@tonic-gate 	char fmt[sizeof (FMTMAX)];
2250Sstevel@tonic-gate 
2260Sstevel@tonic-gate 	if ((me = curproc) == &p0)
2270Sstevel@tonic-gate 		return;
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	/* Privileges must be defined  */
2300Sstevel@tonic-gate 	ASSERT(priv == PRIV_ALL || priv == PRIV_MULTIPLE ||
2310Sstevel@tonic-gate 	    priv == PRIV_ALLZONE || priv == PRIV_GLOBAL ||
2320Sstevel@tonic-gate 	    priv_getbynum(priv) != NULL);
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	if (priv == PRIV_ALLZONE && INGLOBALZONE(me))
2350Sstevel@tonic-gate 		priv = PRIV_ALL;
2360Sstevel@tonic-gate 
2370Sstevel@tonic-gate 	if (curthread->t_pre_sys)
2380Sstevel@tonic-gate 		ttolwp(curthread)->lwp_badpriv = (short)priv;
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (priv_debug == 0 && (CR_FLAGS(cr) & PRIV_DEBUG) == 0)
2410Sstevel@tonic-gate 		return;
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	(void) strcpy(fmt, FMTHDR);
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if (me->p_user.u_comm[0])
2460Sstevel@tonic-gate 		cmd = &me->p_user.u_comm[0];
2470Sstevel@tonic-gate 	else
2480Sstevel@tonic-gate 		cmd = "priv_policy";
2490Sstevel@tonic-gate 
2500Sstevel@tonic-gate 	if (msg != NULL && *msg != '\0') {
2510Sstevel@tonic-gate 		(void) strcat(fmt, FMTMSG);
2520Sstevel@tonic-gate 	} else {
2530Sstevel@tonic-gate 		(void) strcat(fmt, "%s");
2540Sstevel@tonic-gate 		msg = "";
2550Sstevel@tonic-gate 	}
2560Sstevel@tonic-gate 
2570Sstevel@tonic-gate 	sym = NULL;
2580Sstevel@tonic-gate 
2590Sstevel@tonic-gate 	depth = getpcstack(stack, MAXPRIVSTACK);
2600Sstevel@tonic-gate 
2610Sstevel@tonic-gate 	/*
2620Sstevel@tonic-gate 	 * Try to find the first interesting function on the stack.
2630Sstevel@tonic-gate 	 * priv_policy* that's us, so completely uninteresting.
2640Sstevel@tonic-gate 	 * suser(), drv_priv(), secpolicy_* are also called from
2650Sstevel@tonic-gate 	 * too many locations to convey useful information.
2660Sstevel@tonic-gate 	 */
2670Sstevel@tonic-gate 	for (i = 0; i < depth; i++) {
2680Sstevel@tonic-gate 		sym = kobj_getsymname((uintptr_t)stack[i], &off);
2690Sstevel@tonic-gate 		if (sym != NULL &&
2700Sstevel@tonic-gate 		    strstr(sym, "hasprocperm") == 0 &&
2710Sstevel@tonic-gate 		    strcmp("suser", sym) != 0 &&
2720Sstevel@tonic-gate 		    strcmp("ipcaccess", sym) != 0 &&
2730Sstevel@tonic-gate 		    strcmp("drv_priv", sym) != 0 &&
2740Sstevel@tonic-gate 		    strncmp("secpolicy_", sym, 10) != 0 &&
2750Sstevel@tonic-gate 		    strncmp("priv_policy", sym, 11) != 0)
2760Sstevel@tonic-gate 			break;
2770Sstevel@tonic-gate 	}
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	if (sym != NULL)
2800Sstevel@tonic-gate 		(void) strcat(fmt, FMTFUN);
2810Sstevel@tonic-gate 
2820Sstevel@tonic-gate 	(void) strcat(fmt, "\n");
2830Sstevel@tonic-gate 
2840Sstevel@tonic-gate 	switch (priv) {
2850Sstevel@tonic-gate 	case PRIV_ALL:
2860Sstevel@tonic-gate 		pname = "ALL";
2870Sstevel@tonic-gate 		break;
2880Sstevel@tonic-gate 	case PRIV_MULTIPLE:
2890Sstevel@tonic-gate 		pname = "MULTIPLE";
2900Sstevel@tonic-gate 		break;
2910Sstevel@tonic-gate 	case PRIV_ALLZONE:
2920Sstevel@tonic-gate 		pname = "ZONE";
2930Sstevel@tonic-gate 		break;
2940Sstevel@tonic-gate 	case PRIV_GLOBAL:
2950Sstevel@tonic-gate 		pname = "GLOBAL";
2960Sstevel@tonic-gate 		break;
2970Sstevel@tonic-gate 	default:
2980Sstevel@tonic-gate 		pname = priv_getbynum(priv);
2990Sstevel@tonic-gate 		break;
3000Sstevel@tonic-gate 	}
3010Sstevel@tonic-gate 
3020Sstevel@tonic-gate 	if (CR_FLAGS(cr) & PRIV_DEBUG) {
3030Sstevel@tonic-gate 		/* Remember last message, just like lwp_badpriv. */
3040Sstevel@tonic-gate 		if (curthread->t_pdmsg != NULL) {
3050Sstevel@tonic-gate 			kmem_free(curthread->t_pdmsg,
3060Sstevel@tonic-gate 			    strlen(curthread->t_pdmsg) + 1);
3070Sstevel@tonic-gate 		}
3080Sstevel@tonic-gate 
3090Sstevel@tonic-gate 		curthread->t_pdmsg = mprintf(fmt, cmd, me->p_pid, pname,
3104543Smarks 		    cr->cr_uid, curthread->t_sysnum, msg, sym, off);
3110Sstevel@tonic-gate 
3120Sstevel@tonic-gate 		curthread->t_post_sys = 1;
3136134Scasper 	}
3146134Scasper 	if (priv_debug) {
3150Sstevel@tonic-gate 		cmn_err(CE_NOTE, fmt, cmd, me->p_pid, pname, cr->cr_uid,
3160Sstevel@tonic-gate 		    curthread->t_sysnum, msg, sym, off);
3170Sstevel@tonic-gate 	}
3180Sstevel@tonic-gate }
3190Sstevel@tonic-gate 
3200Sstevel@tonic-gate /*
3216134Scasper  * Override the policy, if appropriate.  Return 0 if the external
3226134Scasper  * policy engine approves.
3236134Scasper  */
3246134Scasper static int
3256134Scasper priv_policy_override(const cred_t *cr, int priv, boolean_t allzone, va_list ap)
3266134Scasper {
3276134Scasper 	priv_set_t set;
3286134Scasper 	int ret;
3296134Scasper 
3306134Scasper 	if (!(CR_FLAGS(cr) & PRIV_XPOLICY))
3316134Scasper 		return (-1);
3326134Scasper 
3336134Scasper 	if (priv == PRIV_ALL) {
3346134Scasper 		priv_fillset(&set);
3356134Scasper 	} else if (allzone) {
3366134Scasper 		set = *ZONEPRIVS(cr);
3376134Scasper 	} else {
3386134Scasper 		priv_emptyset(&set);
3396134Scasper 		priv_addset(&set, priv);
3406134Scasper 	}
3416134Scasper 	ret = klpd_call(cr, &set, ap);
3426134Scasper 	return (ret);
3436134Scasper }
3446134Scasper 
3456134Scasper static int
346*12273SCasper.Dik@Sun.COM priv_policy_override_set(const cred_t *cr, const priv_set_t *req, va_list ap)
3476134Scasper {
348*12273SCasper.Dik@Sun.COM 	if (CR_FLAGS(cr) & PRIV_PFEXEC)
349*12273SCasper.Dik@Sun.COM 		return (check_user_privs(cr, req));
3506134Scasper 	if (CR_FLAGS(cr) & PRIV_XPOLICY) {
3516134Scasper 		return (klpd_call(cr, req, ap));
3526134Scasper 	}
3536134Scasper 	return (-1);
3546134Scasper }
3556134Scasper 
356*12273SCasper.Dik@Sun.COM static int
357*12273SCasper.Dik@Sun.COM priv_policy_override_set_va(const cred_t *cr, const priv_set_t *req, ...)
358*12273SCasper.Dik@Sun.COM {
359*12273SCasper.Dik@Sun.COM 	va_list ap;
360*12273SCasper.Dik@Sun.COM 	int ret;
361*12273SCasper.Dik@Sun.COM 
362*12273SCasper.Dik@Sun.COM 	va_start(ap, req);
363*12273SCasper.Dik@Sun.COM 	ret = priv_policy_override_set(cr, req, ap);
364*12273SCasper.Dik@Sun.COM 	va_end(ap);
365*12273SCasper.Dik@Sun.COM 	return (ret);
366*12273SCasper.Dik@Sun.COM }
367*12273SCasper.Dik@Sun.COM 
3686134Scasper /*
3690Sstevel@tonic-gate  * Audit failure, log error message.
3700Sstevel@tonic-gate  */
3710Sstevel@tonic-gate static void
3720Sstevel@tonic-gate priv_policy_err(const cred_t *cr, int priv, boolean_t allzone, const char *msg)
3730Sstevel@tonic-gate {
3740Sstevel@tonic-gate 
37511861SMarek.Pospisil@Sun.COM 	if (AU_AUDITING())
3760Sstevel@tonic-gate 		audit_priv(priv, allzone ? ZONEPRIVS(cr) : NULL, 0);
3770Sstevel@tonic-gate 	DTRACE_PROBE2(priv__err, int, priv, boolean_t, allzone);
3780Sstevel@tonic-gate 
3790Sstevel@tonic-gate 	if (priv_debug || (CR_FLAGS(cr) & PRIV_DEBUG) ||
3800Sstevel@tonic-gate 	    curthread->t_pre_sys) {
3810Sstevel@tonic-gate 		if (allzone && !HAS_ALLZONEPRIVS(cr)) {
3820Sstevel@tonic-gate 			priv_policy_errmsg(cr, PRIV_ALLZONE, msg);
3830Sstevel@tonic-gate 		} else {
3840Sstevel@tonic-gate 			ASSERT(!HAS_PRIVILEGE(cr, priv));
3850Sstevel@tonic-gate 			priv_policy_errmsg(cr, priv, msg);
3860Sstevel@tonic-gate 		}
3870Sstevel@tonic-gate 	}
3880Sstevel@tonic-gate }
3890Sstevel@tonic-gate 
3900Sstevel@tonic-gate /*
3916134Scasper  * priv_policy_ap()
3920Sstevel@tonic-gate  * return 0 or error.
3930Sstevel@tonic-gate  * See block comment above for a description of "priv" and "allzone" usage.
3940Sstevel@tonic-gate  */
3956134Scasper static int
3966134Scasper priv_policy_ap(const cred_t *cr, int priv, boolean_t allzone, int err,
3976134Scasper     const char *msg, va_list ap)
3980Sstevel@tonic-gate {
3996134Scasper 	if ((HAS_PRIVILEGE(cr, priv) && (!allzone || HAS_ALLZONEPRIVS(cr))) ||
4006134Scasper 	    (!servicing_interrupt() &&
4016134Scasper 	    priv_policy_override(cr, priv, allzone, ap) == 0)) {
4020Sstevel@tonic-gate 		if ((allzone || priv == PRIV_ALL ||
4030Sstevel@tonic-gate 		    !PRIV_ISASSERT(priv_basic, priv)) &&
4040Sstevel@tonic-gate 		    !servicing_interrupt()) {
4053446Smrj 			PTOU(curproc)->u_acflag |= ASU; /* Needed for SVVS */
40611861SMarek.Pospisil@Sun.COM 			if (AU_AUDITING())
4070Sstevel@tonic-gate 				audit_priv(priv,
4080Sstevel@tonic-gate 				    allzone ? ZONEPRIVS(cr) : NULL, 1);
4090Sstevel@tonic-gate 		}
4100Sstevel@tonic-gate 		err = 0;
4110Sstevel@tonic-gate 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, allzone);
4120Sstevel@tonic-gate 	} else if (!servicing_interrupt()) {
4130Sstevel@tonic-gate 		/* Failure audited in this procedure */
4140Sstevel@tonic-gate 		priv_policy_err(cr, priv, allzone, msg);
4150Sstevel@tonic-gate 	}
4166134Scasper 	return (err);
4176134Scasper }
4180Sstevel@tonic-gate 
4196134Scasper int
4206134Scasper priv_policy_va(const cred_t *cr, int priv, boolean_t allzone, int err,
4216134Scasper     const char *msg, ...)
4226134Scasper {
4236134Scasper 	int ret;
4246134Scasper 	va_list ap;
4256134Scasper 
4266134Scasper 	va_start(ap, msg);
4276134Scasper 	ret = priv_policy_ap(cr, priv, allzone, err, msg, ap);
4286134Scasper 	va_end(ap);
4296134Scasper 
4306134Scasper 	return (ret);
4316134Scasper }
4326134Scasper 
4336134Scasper int
4346134Scasper priv_policy(const cred_t *cr, int priv, boolean_t allzone, int err,
4356134Scasper     const char *msg)
4366134Scasper {
437*12273SCasper.Dik@Sun.COM 	return (priv_policy_va(cr, priv, allzone, err, msg, KLPDARG_NONE));
4380Sstevel@tonic-gate }
4390Sstevel@tonic-gate 
4400Sstevel@tonic-gate /*
4410Sstevel@tonic-gate  * Return B_TRUE for sufficient privileges, B_FALSE for insufficient privileges.
4420Sstevel@tonic-gate  */
4430Sstevel@tonic-gate boolean_t
4440Sstevel@tonic-gate priv_policy_choice(const cred_t *cr, int priv, boolean_t allzone)
4450Sstevel@tonic-gate {
4460Sstevel@tonic-gate 	boolean_t res = HAS_PRIVILEGE(cr, priv) &&
4470Sstevel@tonic-gate 	    (!allzone || HAS_ALLZONEPRIVS(cr));
4480Sstevel@tonic-gate 
4490Sstevel@tonic-gate 	/* Audit success only */
45011861SMarek.Pospisil@Sun.COM 	if (res && AU_AUDITING() &&
4510Sstevel@tonic-gate 	    (allzone || priv == PRIV_ALL || !PRIV_ISASSERT(priv_basic, priv)) &&
4520Sstevel@tonic-gate 	    !servicing_interrupt()) {
4530Sstevel@tonic-gate 		audit_priv(priv, allzone ? ZONEPRIVS(cr) : NULL, 1);
4540Sstevel@tonic-gate 	}
4550Sstevel@tonic-gate 	if (res) {
4560Sstevel@tonic-gate 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, allzone);
4570Sstevel@tonic-gate 	} else {
4580Sstevel@tonic-gate 		DTRACE_PROBE2(priv__err, int, priv, boolean_t, allzone);
4590Sstevel@tonic-gate 	}
4600Sstevel@tonic-gate 	return (res);
4610Sstevel@tonic-gate }
4620Sstevel@tonic-gate 
4630Sstevel@tonic-gate /*
4640Sstevel@tonic-gate  * Non-auditing variant of priv_policy_choice().
4650Sstevel@tonic-gate  */
4660Sstevel@tonic-gate boolean_t
4670Sstevel@tonic-gate priv_policy_only(const cred_t *cr, int priv, boolean_t allzone)
4680Sstevel@tonic-gate {
4690Sstevel@tonic-gate 	boolean_t res = HAS_PRIVILEGE(cr, priv) &&
4700Sstevel@tonic-gate 	    (!allzone || HAS_ALLZONEPRIVS(cr));
4710Sstevel@tonic-gate 
4720Sstevel@tonic-gate 	if (res) {
4730Sstevel@tonic-gate 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, allzone);
4740Sstevel@tonic-gate 	} else {
4750Sstevel@tonic-gate 		DTRACE_PROBE2(priv__err, int, priv, boolean_t, allzone);
4760Sstevel@tonic-gate 	}
4770Sstevel@tonic-gate 	return (res);
4780Sstevel@tonic-gate }
4790Sstevel@tonic-gate 
4800Sstevel@tonic-gate /*
4810Sstevel@tonic-gate  * Check whether all privileges in the required set are present.
4820Sstevel@tonic-gate  */
4830Sstevel@tonic-gate static int
484*12273SCasper.Dik@Sun.COM secpolicy_require_set(const cred_t *cr, const priv_set_t *req,
485*12273SCasper.Dik@Sun.COM     const char *msg, ...)
4860Sstevel@tonic-gate {
4870Sstevel@tonic-gate 	int priv;
4880Sstevel@tonic-gate 	int pfound = -1;
4890Sstevel@tonic-gate 	priv_set_t pset;
490*12273SCasper.Dik@Sun.COM 	va_list ap;
491*12273SCasper.Dik@Sun.COM 	int ret;
4920Sstevel@tonic-gate 
4930Sstevel@tonic-gate 	if (req == PRIV_FULLSET ? HAS_ALLPRIVS(cr) : priv_issubset(req,
4944543Smarks 	    &CR_OEPRIV(cr))) {
4950Sstevel@tonic-gate 		return (0);
4960Sstevel@tonic-gate 	}
4970Sstevel@tonic-gate 
498*12273SCasper.Dik@Sun.COM 	va_start(ap, msg);
499*12273SCasper.Dik@Sun.COM 	ret = priv_policy_override_set(cr, req, ap);
500*12273SCasper.Dik@Sun.COM 	va_end(ap);
501*12273SCasper.Dik@Sun.COM 	if (ret == 0)
5026134Scasper 		return (0);
5036134Scasper 
5040Sstevel@tonic-gate 	if (req == PRIV_FULLSET || priv_isfullset(req)) {
5050Sstevel@tonic-gate 		priv_policy_err(cr, PRIV_ALL, B_FALSE, msg);
5060Sstevel@tonic-gate 		return (EACCES);
5070Sstevel@tonic-gate 	}
5080Sstevel@tonic-gate 
5090Sstevel@tonic-gate 	pset = CR_OEPRIV(cr);		/* present privileges */
5100Sstevel@tonic-gate 	priv_inverse(&pset);		/* all non present privileges */
5110Sstevel@tonic-gate 	priv_intersect(req, &pset);	/* the actual missing privs */
5120Sstevel@tonic-gate 
51311861SMarek.Pospisil@Sun.COM 	if (AU_AUDITING())
5140Sstevel@tonic-gate 		audit_priv(PRIV_NONE, &pset, 0);
5150Sstevel@tonic-gate 	/*
5160Sstevel@tonic-gate 	 * Privilege debugging; special case "one privilege in set".
5170Sstevel@tonic-gate 	 */
5180Sstevel@tonic-gate 	if (priv_debug || (CR_FLAGS(cr) & PRIV_DEBUG) || curthread->t_pre_sys) {
5190Sstevel@tonic-gate 		for (priv = 0; priv < nprivs; priv++) {
5200Sstevel@tonic-gate 			if (priv_ismember(&pset, priv)) {
5210Sstevel@tonic-gate 				if (pfound != -1) {
5220Sstevel@tonic-gate 					/* Multiple missing privs */
5230Sstevel@tonic-gate 					priv_policy_errmsg(cr, PRIV_MULTIPLE,
5244543Smarks 					    msg);
5250Sstevel@tonic-gate 					return (EACCES);
5260Sstevel@tonic-gate 				}
5270Sstevel@tonic-gate 				pfound = priv;
5280Sstevel@tonic-gate 			}
5290Sstevel@tonic-gate 		}
5300Sstevel@tonic-gate 		ASSERT(pfound != -1);
5310Sstevel@tonic-gate 		/* Just the one missing privilege */
5320Sstevel@tonic-gate 		priv_policy_errmsg(cr, pfound, msg);
5330Sstevel@tonic-gate 	}
5340Sstevel@tonic-gate 
5350Sstevel@tonic-gate 	return (EACCES);
5360Sstevel@tonic-gate }
5370Sstevel@tonic-gate 
5380Sstevel@tonic-gate /*
5390Sstevel@tonic-gate  * Called when an operation requires that the caller be in the
5400Sstevel@tonic-gate  * global zone, regardless of privilege.
5410Sstevel@tonic-gate  */
5420Sstevel@tonic-gate static int
5430Sstevel@tonic-gate priv_policy_global(const cred_t *cr)
5440Sstevel@tonic-gate {
5450Sstevel@tonic-gate 	if (crgetzoneid(cr) == GLOBAL_ZONEID)
5460Sstevel@tonic-gate 		return (0);	/* success */
5470Sstevel@tonic-gate 
5480Sstevel@tonic-gate 	if (priv_debug || (CR_FLAGS(cr) & PRIV_DEBUG) ||
5490Sstevel@tonic-gate 	    curthread->t_pre_sys) {
5500Sstevel@tonic-gate 		priv_policy_errmsg(cr, PRIV_GLOBAL, NULL);
5510Sstevel@tonic-gate 	}
5520Sstevel@tonic-gate 	return (EPERM);
5530Sstevel@tonic-gate }
5540Sstevel@tonic-gate 
5550Sstevel@tonic-gate /*
5560Sstevel@tonic-gate  * Changing process priority
5570Sstevel@tonic-gate  */
5580Sstevel@tonic-gate int
5590Sstevel@tonic-gate secpolicy_setpriority(const cred_t *cr)
5600Sstevel@tonic-gate {
5610Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_PRIOCNTL, B_FALSE, EPERM, NULL));
5620Sstevel@tonic-gate }
5630Sstevel@tonic-gate 
5640Sstevel@tonic-gate /*
5650Sstevel@tonic-gate  * Binding to a privileged port, port must be specified in host byte
5660Sstevel@tonic-gate  * order.
567*12273SCasper.Dik@Sun.COM  * When adding a new privilege which allows binding to currently privileged
568*12273SCasper.Dik@Sun.COM  * ports, then you MUST also allow processes with PRIV_NET_PRIVADDR bind
569*12273SCasper.Dik@Sun.COM  * to these ports because of backward compatibility.
5700Sstevel@tonic-gate  */
5710Sstevel@tonic-gate int
5726134Scasper secpolicy_net_privaddr(const cred_t *cr, in_port_t port, int proto)
5730Sstevel@tonic-gate {
5745331Samw 	char *reason;
5755331Samw 	int priv;
5765331Samw 
5775331Samw 	switch (port) {
5785331Samw 	case 137:
5795331Samw 	case 138:
5805331Samw 	case 139:
5815331Samw 	case 445:
5825331Samw 		/*
583*12273SCasper.Dik@Sun.COM 		 * NBT and SMB ports, these are normal privileged ports,
584*12273SCasper.Dik@Sun.COM 		 * allow bind only if the SYS_SMB or NET_PRIVADDR privilege
585*12273SCasper.Dik@Sun.COM 		 * is present.
586*12273SCasper.Dik@Sun.COM 		 * Try both, if neither is present return an error for
587*12273SCasper.Dik@Sun.COM 		 * priv SYS_SMB.
5885331Samw 		 */
589*12273SCasper.Dik@Sun.COM 		if (PRIV_POLICY_ONLY(cr, PRIV_NET_PRIVADDR, B_FALSE))
590*12273SCasper.Dik@Sun.COM 			priv = PRIV_NET_PRIVADDR;
591*12273SCasper.Dik@Sun.COM 		else
592*12273SCasper.Dik@Sun.COM 			priv = PRIV_SYS_SMB;
5935331Samw 		reason = "NBT or SMB port";
5945331Samw 		break;
5955331Samw 
5965331Samw 	case 2049:
5975331Samw 	case 4045:
5985331Samw 		/*
5995331Samw 		 * NFS ports, these are extra privileged ports, allow bind
6005331Samw 		 * only if the SYS_NFS privilege is present.
6015331Samw 		 */
6025331Samw 		priv = PRIV_SYS_NFS;
6035331Samw 		reason = "NFS port";
6045331Samw 		break;
6055331Samw 
6065331Samw 	default:
6075331Samw 		priv = PRIV_NET_PRIVADDR;
6085331Samw 		reason = NULL;
6095331Samw 		break;
6105331Samw 
6115331Samw 	}
6125331Samw 
6136134Scasper 	return (priv_policy_va(cr, priv, B_FALSE, EACCES, reason,
6146134Scasper 	    KLPDARG_PORT, (int)proto, (int)port, KLPDARG_NOMORE));
6150Sstevel@tonic-gate }
6160Sstevel@tonic-gate 
6170Sstevel@tonic-gate /*
6181676Sjpk  * Binding to a multilevel port on a trusted (labeled) system.
6191676Sjpk  */
6201676Sjpk int
6211676Sjpk secpolicy_net_bindmlp(const cred_t *cr)
6221676Sjpk {
6236134Scasper 	return (PRIV_POLICY(cr, PRIV_NET_BINDMLP, B_FALSE, EACCES, NULL));
6241676Sjpk }
6251676Sjpk 
6261676Sjpk /*
6271676Sjpk  * Allow a communication between a zone and an unlabeled host when their
6281676Sjpk  * labels don't match.
6291676Sjpk  */
6301676Sjpk int
6311676Sjpk secpolicy_net_mac_aware(const cred_t *cr)
6321676Sjpk {
6336134Scasper 	return (PRIV_POLICY(cr, PRIV_NET_MAC_AWARE, B_FALSE, EACCES, NULL));
6341676Sjpk }
6351676Sjpk 
6361676Sjpk /*
63710934Ssommerfeld@sun.com  * Allow a privileged process to transmit traffic without explicit labels
63810934Ssommerfeld@sun.com  */
63910934Ssommerfeld@sun.com int
64010934Ssommerfeld@sun.com secpolicy_net_mac_implicit(const cred_t *cr)
64110934Ssommerfeld@sun.com {
64210934Ssommerfeld@sun.com 	return (PRIV_POLICY(cr, PRIV_NET_MAC_IMPLICIT, B_FALSE, EACCES, NULL));
64310934Ssommerfeld@sun.com }
64410934Ssommerfeld@sun.com 
64510934Ssommerfeld@sun.com /*
6460Sstevel@tonic-gate  * Common routine which determines whether a given credential can
6470Sstevel@tonic-gate  * act on a given mount.
6480Sstevel@tonic-gate  * When called through mount, the parameter needoptcheck is a pointer
6490Sstevel@tonic-gate  * to a boolean variable which will be set to either true or false,
6500Sstevel@tonic-gate  * depending on whether the mount policy should change the mount options.
6510Sstevel@tonic-gate  * In all other cases, needoptcheck should be a NULL pointer.
6520Sstevel@tonic-gate  */
6530Sstevel@tonic-gate static int
6540Sstevel@tonic-gate secpolicy_fs_common(cred_t *cr, vnode_t *mvp, const vfs_t *vfsp,
6550Sstevel@tonic-gate     boolean_t *needoptcheck)
6560Sstevel@tonic-gate {
6570Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
6580Sstevel@tonic-gate 	boolean_t mounting = needoptcheck != NULL;
6590Sstevel@tonic-gate 
6600Sstevel@tonic-gate 	/*
6610Sstevel@tonic-gate 	 * Short circuit the following cases:
6620Sstevel@tonic-gate 	 *	vfsp == NULL or mvp == NULL (pure privilege check)
6630Sstevel@tonic-gate 	 *	have all privileges - no further checks required
6640Sstevel@tonic-gate 	 *	and no mount options need to be set.
6650Sstevel@tonic-gate 	 */
6660Sstevel@tonic-gate 	if (vfsp == NULL || mvp == NULL || HAS_ALLPRIVS(cr)) {
6670Sstevel@tonic-gate 		if (mounting)
6680Sstevel@tonic-gate 			*needoptcheck = B_FALSE;
6690Sstevel@tonic-gate 
6706134Scasper 		return (priv_policy_va(cr, PRIV_SYS_MOUNT, allzone, EPERM,
6716134Scasper 		    NULL, KLPDARG_VNODE, mvp, (char *)NULL, KLPDARG_NOMORE));
6720Sstevel@tonic-gate 	}
6730Sstevel@tonic-gate 
6740Sstevel@tonic-gate 	/*
6750Sstevel@tonic-gate 	 * When operating on an existing mount (either we're not mounting
6760Sstevel@tonic-gate 	 * or we're doing a remount and VFS_REMOUNT will be set), zones
6770Sstevel@tonic-gate 	 * can operate only on mounts established by the zone itself.
6780Sstevel@tonic-gate 	 */
6790Sstevel@tonic-gate 	if (!mounting || (vfsp->vfs_flag & VFS_REMOUNT) != 0) {
6800Sstevel@tonic-gate 		zoneid_t zoneid = crgetzoneid(cr);
6810Sstevel@tonic-gate 
6820Sstevel@tonic-gate 		if (zoneid != GLOBAL_ZONEID &&
6830Sstevel@tonic-gate 		    vfsp->vfs_zone->zone_id != zoneid) {
6840Sstevel@tonic-gate 			return (EPERM);
6850Sstevel@tonic-gate 		}
6860Sstevel@tonic-gate 	}
6870Sstevel@tonic-gate 
6880Sstevel@tonic-gate 	if (mounting)
6890Sstevel@tonic-gate 		*needoptcheck = B_TRUE;
6900Sstevel@tonic-gate 
6910Sstevel@tonic-gate 	/*
6920Sstevel@tonic-gate 	 * Overlay mounts may hide important stuff; if you can't write to a
6930Sstevel@tonic-gate 	 * mount point but would be able to mount on top of it, you can
6940Sstevel@tonic-gate 	 * escalate your privileges.
6950Sstevel@tonic-gate 	 * So we go about asking the same questions namefs does when it
6960Sstevel@tonic-gate 	 * decides whether you can mount over a file or not but with the
6970Sstevel@tonic-gate 	 * added restriction that you can only mount on top of a regular
6980Sstevel@tonic-gate 	 * file or directory.
6990Sstevel@tonic-gate 	 * If we have all the zone's privileges, we skip all other checks,
7000Sstevel@tonic-gate 	 * or else we may actually get in trouble inside the automounter.
7010Sstevel@tonic-gate 	 */
7020Sstevel@tonic-gate 	if ((mvp->v_flag & VROOT) != 0 ||
7030Sstevel@tonic-gate 	    (mvp->v_type != VDIR && mvp->v_type != VREG) ||
7040Sstevel@tonic-gate 	    HAS_ALLZONEPRIVS(cr)) {
7050Sstevel@tonic-gate 		allzone = B_TRUE;
7060Sstevel@tonic-gate 	} else {
7070Sstevel@tonic-gate 		vattr_t va;
7080Sstevel@tonic-gate 		int err;
7090Sstevel@tonic-gate 
7100Sstevel@tonic-gate 		va.va_mask = AT_UID|AT_MODE;
7115331Samw 		err = VOP_GETATTR(mvp, &va, 0, cr, NULL);
7120Sstevel@tonic-gate 		if (err != 0)
7130Sstevel@tonic-gate 			return (err);
7140Sstevel@tonic-gate 
7150Sstevel@tonic-gate 		if ((err = secpolicy_vnode_owner(cr, va.va_uid)) != 0)
7160Sstevel@tonic-gate 			return (err);
7170Sstevel@tonic-gate 
718*12273SCasper.Dik@Sun.COM 		if (secpolicy_vnode_access2(cr, mvp, va.va_uid, va.va_mode,
719*12273SCasper.Dik@Sun.COM 		    VWRITE) != 0) {
7200Sstevel@tonic-gate 			return (EACCES);
7210Sstevel@tonic-gate 		}
7220Sstevel@tonic-gate 	}
7236134Scasper 	return (priv_policy_va(cr, PRIV_SYS_MOUNT, allzone, EPERM,
7246134Scasper 	    NULL, KLPDARG_VNODE, mvp, (char *)NULL, KLPDARG_NOMORE));
7250Sstevel@tonic-gate }
7260Sstevel@tonic-gate 
7274543Smarks void
7284543Smarks secpolicy_fs_mount_clearopts(cred_t *cr, struct vfs *vfsp)
7294543Smarks {
7304543Smarks 	boolean_t amsuper = HAS_ALLZONEPRIVS(cr);
7314543Smarks 
7324543Smarks 	/*
7334543Smarks 	 * check; if we don't have either "nosuid" or
7344543Smarks 	 * both "nosetuid" and "nodevices", then we add
7354543Smarks 	 * "nosuid"; this depends on how the current
7364543Smarks 	 * implementation works (it first checks nosuid).  In a
7374543Smarks 	 * zone, a user with all zone privileges can mount with
7384543Smarks 	 * "setuid" but never with "devices".
7394543Smarks 	 */
7404543Smarks 	if (!vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL) &&
7414543Smarks 	    (!vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL) ||
7424543Smarks 	    !vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))) {
7434543Smarks 		if (crgetzoneid(cr) == GLOBAL_ZONEID || !amsuper)
7444543Smarks 			vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
7454543Smarks 		else
7464543Smarks 			vfs_setmntopt(vfsp, MNTOPT_NODEVICES, NULL, 0);
7474543Smarks 	}
7484543Smarks 	/*
7494543Smarks 	 * If we're not the local super user, we set the "restrict"
7504543Smarks 	 * option to indicate to automountd that this mount should
7514543Smarks 	 * be handled with care.
7524543Smarks 	 */
7534543Smarks 	if (!amsuper)
7544543Smarks 		vfs_setmntopt(vfsp, MNTOPT_RESTRICT, NULL, 0);
7554543Smarks 
7564543Smarks }
7574543Smarks 
758148Scasper extern vnode_t *rootvp;
759148Scasper extern vfs_t *rootvfs;
760148Scasper 
7610Sstevel@tonic-gate int
7620Sstevel@tonic-gate secpolicy_fs_mount(cred_t *cr, vnode_t *mvp, struct vfs *vfsp)
7630Sstevel@tonic-gate {
7640Sstevel@tonic-gate 	boolean_t needoptchk;
7650Sstevel@tonic-gate 	int error;
7660Sstevel@tonic-gate 
767148Scasper 	/*
768148Scasper 	 * If it's a remount, get the underlying mount point,
769148Scasper 	 * except for the root where we use the rootvp.
770148Scasper 	 */
771148Scasper 	if ((vfsp->vfs_flag & VFS_REMOUNT) != 0) {
772148Scasper 		if (vfsp == rootvfs)
773148Scasper 			mvp = rootvp;
774148Scasper 		else
775148Scasper 			mvp = vfsp->vfs_vnodecovered;
776148Scasper 	}
777148Scasper 
7780Sstevel@tonic-gate 	error = secpolicy_fs_common(cr, mvp, vfsp, &needoptchk);
7790Sstevel@tonic-gate 
7800Sstevel@tonic-gate 	if (error == 0 && needoptchk) {
7814543Smarks 		secpolicy_fs_mount_clearopts(cr, vfsp);
7824543Smarks 	}
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate 	return (error);
7850Sstevel@tonic-gate }
7860Sstevel@tonic-gate 
7870Sstevel@tonic-gate /*
7880Sstevel@tonic-gate  * Does the policy computations for "ownership" of a mount;
7890Sstevel@tonic-gate  * here ownership is defined as the ability to "mount"
7900Sstevel@tonic-gate  * the filesystem originally.  The rootvfs doesn't cover any
7910Sstevel@tonic-gate  * vnodes; we attribute its ownership to the rootvp.
7920Sstevel@tonic-gate  */
7930Sstevel@tonic-gate static int
7940Sstevel@tonic-gate secpolicy_fs_owner(cred_t *cr, const struct vfs *vfsp)
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	vnode_t *mvp;
7970Sstevel@tonic-gate 
7980Sstevel@tonic-gate 	if (vfsp == NULL)
7990Sstevel@tonic-gate 		mvp = NULL;
8000Sstevel@tonic-gate 	else if (vfsp == rootvfs)
8010Sstevel@tonic-gate 		mvp = rootvp;
8020Sstevel@tonic-gate 	else
8030Sstevel@tonic-gate 		mvp = vfsp->vfs_vnodecovered;
8040Sstevel@tonic-gate 
8050Sstevel@tonic-gate 	return (secpolicy_fs_common(cr, mvp, vfsp, NULL));
8060Sstevel@tonic-gate }
8070Sstevel@tonic-gate 
8080Sstevel@tonic-gate int
8090Sstevel@tonic-gate secpolicy_fs_unmount(cred_t *cr, struct vfs *vfsp)
8100Sstevel@tonic-gate {
8110Sstevel@tonic-gate 	return (secpolicy_fs_owner(cr, vfsp));
8120Sstevel@tonic-gate }
8130Sstevel@tonic-gate 
8140Sstevel@tonic-gate /*
8150Sstevel@tonic-gate  * Quotas are a resource, but if one has the ability to mount a filesystem, he
8160Sstevel@tonic-gate  * should be able to modify quotas on it.
8170Sstevel@tonic-gate  */
8180Sstevel@tonic-gate int
8190Sstevel@tonic-gate secpolicy_fs_quota(const cred_t *cr, const vfs_t *vfsp)
8200Sstevel@tonic-gate {
8210Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
8220Sstevel@tonic-gate }
8230Sstevel@tonic-gate 
8240Sstevel@tonic-gate /*
8250Sstevel@tonic-gate  * Exceeding minfree: also a per-mount resource constraint.
8260Sstevel@tonic-gate  */
8270Sstevel@tonic-gate int
8280Sstevel@tonic-gate secpolicy_fs_minfree(const cred_t *cr, const vfs_t *vfsp)
8290Sstevel@tonic-gate {
8300Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
8310Sstevel@tonic-gate }
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate int
8340Sstevel@tonic-gate secpolicy_fs_config(const cred_t *cr, const vfs_t *vfsp)
8350Sstevel@tonic-gate {
8360Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
8370Sstevel@tonic-gate }
8380Sstevel@tonic-gate 
8390Sstevel@tonic-gate /* ARGSUSED */
8400Sstevel@tonic-gate int
8410Sstevel@tonic-gate secpolicy_fs_linkdir(const cred_t *cr, const vfs_t *vfsp)
8420Sstevel@tonic-gate {
8430Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_LINKDIR, B_FALSE, EPERM, NULL));
8440Sstevel@tonic-gate }
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate /*
8470Sstevel@tonic-gate  * Name:        secpolicy_vnode_access()
8480Sstevel@tonic-gate  *
8490Sstevel@tonic-gate  * Parameters:  Process credential
8500Sstevel@tonic-gate  *		vnode
8510Sstevel@tonic-gate  *		uid of owner of vnode
8520Sstevel@tonic-gate  *		permission bits not granted to the caller when examining
8530Sstevel@tonic-gate  *		file mode bits (i.e., when a process wants to open a
8540Sstevel@tonic-gate  *		mode 444 file for VREAD|VWRITE, this function should be
8550Sstevel@tonic-gate  *		called only with a VWRITE argument).
8560Sstevel@tonic-gate  *
8570Sstevel@tonic-gate  * Normal:      Verifies that cred has the appropriate privileges to
8580Sstevel@tonic-gate  *              override the mode bits that were denied.
8590Sstevel@tonic-gate  *
8600Sstevel@tonic-gate  * Override:    file_dac_execute - if VEXEC bit was denied and vnode is
8610Sstevel@tonic-gate  *                      not a directory.
8620Sstevel@tonic-gate  *              file_dac_read - if VREAD bit was denied.
8630Sstevel@tonic-gate  *              file_dac_search - if VEXEC bit was denied and vnode is
8640Sstevel@tonic-gate  *                      a directory.
8650Sstevel@tonic-gate  *              file_dac_write - if VWRITE bit was denied.
8660Sstevel@tonic-gate  *
8670Sstevel@tonic-gate  *		Root owned files are special cased to protect system
8680Sstevel@tonic-gate  *		configuration files and such.
8690Sstevel@tonic-gate  *
8700Sstevel@tonic-gate  * Output:      EACCES - if privilege check fails.
8710Sstevel@tonic-gate  */
8720Sstevel@tonic-gate 
8730Sstevel@tonic-gate int
8740Sstevel@tonic-gate secpolicy_vnode_access(const cred_t *cr, vnode_t *vp, uid_t owner, mode_t mode)
8750Sstevel@tonic-gate {
8766134Scasper 	if ((mode & VREAD) && priv_policy_va(cr, PRIV_FILE_DAC_READ, B_FALSE,
8776134Scasper 	    EACCES, NULL, KLPDARG_VNODE, vp, (char *)NULL,
8786134Scasper 	    KLPDARG_NOMORE) != 0) {
8790Sstevel@tonic-gate 		return (EACCES);
8806134Scasper 	}
8810Sstevel@tonic-gate 
8820Sstevel@tonic-gate 	if (mode & VWRITE) {
8830Sstevel@tonic-gate 		boolean_t allzone;
8840Sstevel@tonic-gate 
8850Sstevel@tonic-gate 		if (owner == 0 && cr->cr_uid != 0)
8860Sstevel@tonic-gate 			allzone = B_TRUE;
8870Sstevel@tonic-gate 		else
8880Sstevel@tonic-gate 			allzone = B_FALSE;
8896134Scasper 		if (priv_policy_va(cr, PRIV_FILE_DAC_WRITE, allzone, EACCES,
8906134Scasper 		    NULL, KLPDARG_VNODE, vp, (char *)NULL,
8916134Scasper 		    KLPDARG_NOMORE) != 0) {
8920Sstevel@tonic-gate 			return (EACCES);
8936134Scasper 		}
8940Sstevel@tonic-gate 	}
8950Sstevel@tonic-gate 
8960Sstevel@tonic-gate 	if (mode & VEXEC) {
8970Sstevel@tonic-gate 		/*
8980Sstevel@tonic-gate 		 * Directories use file_dac_search to override the execute bit.
8990Sstevel@tonic-gate 		 */
9006134Scasper 		int p = vp->v_type == VDIR ? PRIV_FILE_DAC_SEARCH :
9016134Scasper 		    PRIV_FILE_DAC_EXECUTE;
9020Sstevel@tonic-gate 
9036134Scasper 		return (priv_policy_va(cr, p, B_FALSE, EACCES, NULL,
9046134Scasper 		    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
9050Sstevel@tonic-gate 	}
9060Sstevel@tonic-gate 	return (0);
9070Sstevel@tonic-gate }
9080Sstevel@tonic-gate 
9090Sstevel@tonic-gate /*
910*12273SCasper.Dik@Sun.COM  * Like secpolicy_vnode_access() but we get the actual wanted mode and the
911*12273SCasper.Dik@Sun.COM  * current mode of the file, not the missing bits.
912*12273SCasper.Dik@Sun.COM  */
913*12273SCasper.Dik@Sun.COM int
914*12273SCasper.Dik@Sun.COM secpolicy_vnode_access2(const cred_t *cr, vnode_t *vp, uid_t owner,
915*12273SCasper.Dik@Sun.COM     mode_t curmode, mode_t wantmode)
916*12273SCasper.Dik@Sun.COM {
917*12273SCasper.Dik@Sun.COM 	mode_t mode;
918*12273SCasper.Dik@Sun.COM 
919*12273SCasper.Dik@Sun.COM 	/* Inline the basic privileges tests. */
920*12273SCasper.Dik@Sun.COM 	if ((wantmode & VREAD) &&
921*12273SCasper.Dik@Sun.COM 	    !PRIV_ISASSERT(&CR_OEPRIV(cr), PRIV_FILE_READ) &&
922*12273SCasper.Dik@Sun.COM 	    priv_policy_va(cr, PRIV_FILE_READ, B_FALSE, EACCES, NULL,
923*12273SCasper.Dik@Sun.COM 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE) != 0) {
924*12273SCasper.Dik@Sun.COM 		return (EACCES);
925*12273SCasper.Dik@Sun.COM 	}
926*12273SCasper.Dik@Sun.COM 
927*12273SCasper.Dik@Sun.COM 	if ((wantmode & VWRITE) &&
928*12273SCasper.Dik@Sun.COM 	    !PRIV_ISASSERT(&CR_OEPRIV(cr), PRIV_FILE_WRITE) &&
929*12273SCasper.Dik@Sun.COM 	    priv_policy_va(cr, PRIV_FILE_WRITE, B_FALSE, EACCES, NULL,
930*12273SCasper.Dik@Sun.COM 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE) != 0) {
931*12273SCasper.Dik@Sun.COM 		return (EACCES);
932*12273SCasper.Dik@Sun.COM 	}
933*12273SCasper.Dik@Sun.COM 
934*12273SCasper.Dik@Sun.COM 	mode = ~curmode & wantmode;
935*12273SCasper.Dik@Sun.COM 
936*12273SCasper.Dik@Sun.COM 	if (mode == 0)
937*12273SCasper.Dik@Sun.COM 		return (0);
938*12273SCasper.Dik@Sun.COM 
939*12273SCasper.Dik@Sun.COM 	if ((mode & VREAD) && priv_policy_va(cr, PRIV_FILE_DAC_READ, B_FALSE,
940*12273SCasper.Dik@Sun.COM 	    EACCES, NULL, KLPDARG_VNODE, vp, (char *)NULL,
941*12273SCasper.Dik@Sun.COM 	    KLPDARG_NOMORE) != 0) {
942*12273SCasper.Dik@Sun.COM 		return (EACCES);
943*12273SCasper.Dik@Sun.COM 	}
944*12273SCasper.Dik@Sun.COM 
945*12273SCasper.Dik@Sun.COM 	if (mode & VWRITE) {
946*12273SCasper.Dik@Sun.COM 		boolean_t allzone;
947*12273SCasper.Dik@Sun.COM 
948*12273SCasper.Dik@Sun.COM 		if (owner == 0 && cr->cr_uid != 0)
949*12273SCasper.Dik@Sun.COM 			allzone = B_TRUE;
950*12273SCasper.Dik@Sun.COM 		else
951*12273SCasper.Dik@Sun.COM 			allzone = B_FALSE;
952*12273SCasper.Dik@Sun.COM 		if (priv_policy_va(cr, PRIV_FILE_DAC_WRITE, allzone, EACCES,
953*12273SCasper.Dik@Sun.COM 		    NULL, KLPDARG_VNODE, vp, (char *)NULL,
954*12273SCasper.Dik@Sun.COM 		    KLPDARG_NOMORE) != 0) {
955*12273SCasper.Dik@Sun.COM 			return (EACCES);
956*12273SCasper.Dik@Sun.COM 		}
957*12273SCasper.Dik@Sun.COM 	}
958*12273SCasper.Dik@Sun.COM 
959*12273SCasper.Dik@Sun.COM 	if (mode & VEXEC) {
960*12273SCasper.Dik@Sun.COM 		/*
961*12273SCasper.Dik@Sun.COM 		 * Directories use file_dac_search to override the execute bit.
962*12273SCasper.Dik@Sun.COM 		 */
963*12273SCasper.Dik@Sun.COM 		int p = vp->v_type == VDIR ? PRIV_FILE_DAC_SEARCH :
964*12273SCasper.Dik@Sun.COM 		    PRIV_FILE_DAC_EXECUTE;
965*12273SCasper.Dik@Sun.COM 
966*12273SCasper.Dik@Sun.COM 		return (priv_policy_va(cr, p, B_FALSE, EACCES, NULL,
967*12273SCasper.Dik@Sun.COM 		    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
968*12273SCasper.Dik@Sun.COM 	}
969*12273SCasper.Dik@Sun.COM 	return (0);
970*12273SCasper.Dik@Sun.COM }
971*12273SCasper.Dik@Sun.COM 
972*12273SCasper.Dik@Sun.COM /*
973*12273SCasper.Dik@Sun.COM  * This is a special routine for ZFS; it is used to determine whether
974*12273SCasper.Dik@Sun.COM  * any of the privileges in effect allow any form of access to the
975*12273SCasper.Dik@Sun.COM  * file.  There's no reason to audit this or any reason to record
976*12273SCasper.Dik@Sun.COM  * this.  More work is needed to do the "KPLD" stuff.
977*12273SCasper.Dik@Sun.COM  */
978*12273SCasper.Dik@Sun.COM int
979*12273SCasper.Dik@Sun.COM secpolicy_vnode_any_access(const cred_t *cr, vnode_t *vp, uid_t owner)
980*12273SCasper.Dik@Sun.COM {
981*12273SCasper.Dik@Sun.COM 	static int privs[] = {
982*12273SCasper.Dik@Sun.COM 	    PRIV_FILE_OWNER,
983*12273SCasper.Dik@Sun.COM 	    PRIV_FILE_DAC_READ,
984*12273SCasper.Dik@Sun.COM 	    PRIV_FILE_DAC_WRITE,
985*12273SCasper.Dik@Sun.COM 	    PRIV_FILE_DAC_EXECUTE,
986*12273SCasper.Dik@Sun.COM 	    PRIV_FILE_DAC_SEARCH,
987*12273SCasper.Dik@Sun.COM 	};
988*12273SCasper.Dik@Sun.COM 	int i;
989*12273SCasper.Dik@Sun.COM 
990*12273SCasper.Dik@Sun.COM 	/* Same as secpolicy_vnode_setdac */
991*12273SCasper.Dik@Sun.COM 	if (owner == cr->cr_uid)
992*12273SCasper.Dik@Sun.COM 		return (0);
993*12273SCasper.Dik@Sun.COM 
994*12273SCasper.Dik@Sun.COM 	for (i = 0; i < sizeof (privs)/sizeof (int); i++) {
995*12273SCasper.Dik@Sun.COM 		boolean_t allzone = B_FALSE;
996*12273SCasper.Dik@Sun.COM 		int priv;
997*12273SCasper.Dik@Sun.COM 
998*12273SCasper.Dik@Sun.COM 		switch (priv = privs[i]) {
999*12273SCasper.Dik@Sun.COM 		case PRIV_FILE_DAC_EXECUTE:
1000*12273SCasper.Dik@Sun.COM 			if (vp->v_type == VDIR)
1001*12273SCasper.Dik@Sun.COM 				continue;
1002*12273SCasper.Dik@Sun.COM 			break;
1003*12273SCasper.Dik@Sun.COM 		case PRIV_FILE_DAC_SEARCH:
1004*12273SCasper.Dik@Sun.COM 			if (vp->v_type != VDIR)
1005*12273SCasper.Dik@Sun.COM 				continue;
1006*12273SCasper.Dik@Sun.COM 			break;
1007*12273SCasper.Dik@Sun.COM 		case PRIV_FILE_DAC_WRITE:
1008*12273SCasper.Dik@Sun.COM 		case PRIV_FILE_OWNER:
1009*12273SCasper.Dik@Sun.COM 			/* We know here that if owner == 0, that cr_uid != 0 */
1010*12273SCasper.Dik@Sun.COM 			allzone = owner == 0;
1011*12273SCasper.Dik@Sun.COM 			break;
1012*12273SCasper.Dik@Sun.COM 		}
1013*12273SCasper.Dik@Sun.COM 		if (PRIV_POLICY_CHOICE(cr, priv, allzone))
1014*12273SCasper.Dik@Sun.COM 			return (0);
1015*12273SCasper.Dik@Sun.COM 	}
1016*12273SCasper.Dik@Sun.COM 	return (EPERM);
1017*12273SCasper.Dik@Sun.COM }
1018*12273SCasper.Dik@Sun.COM 
1019*12273SCasper.Dik@Sun.COM /*
10200Sstevel@tonic-gate  * Name:	secpolicy_vnode_setid_modify()
10210Sstevel@tonic-gate  *
10220Sstevel@tonic-gate  * Normal:	verify that subject can set the file setid flags.
10230Sstevel@tonic-gate  *
10240Sstevel@tonic-gate  * Output:	EPERM - if not privileged.
10250Sstevel@tonic-gate  */
10260Sstevel@tonic-gate 
10270Sstevel@tonic-gate static int
10280Sstevel@tonic-gate secpolicy_vnode_setid_modify(const cred_t *cr, uid_t owner)
10290Sstevel@tonic-gate {
10300Sstevel@tonic-gate 	/* If changing to suid root, must have all zone privs */
10310Sstevel@tonic-gate 	boolean_t allzone = B_TRUE;
10320Sstevel@tonic-gate 
10330Sstevel@tonic-gate 	if (owner != 0) {
10340Sstevel@tonic-gate 		if (owner == cr->cr_uid)
10350Sstevel@tonic-gate 			return (0);
10360Sstevel@tonic-gate 		allzone = B_FALSE;
10370Sstevel@tonic-gate 	}
10380Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_SETID, allzone, EPERM, NULL));
10390Sstevel@tonic-gate }
10400Sstevel@tonic-gate 
10410Sstevel@tonic-gate /*
10420Sstevel@tonic-gate  * Are we allowed to retain the set-uid/set-gid bits when
10430Sstevel@tonic-gate  * changing ownership or when writing to a file?
10440Sstevel@tonic-gate  * "issuid" should be true when set-uid; only in that case
10450Sstevel@tonic-gate  * root ownership is checked (setgid is assumed).
10460Sstevel@tonic-gate  */
10470Sstevel@tonic-gate int
10480Sstevel@tonic-gate secpolicy_vnode_setid_retain(const cred_t *cred, boolean_t issuidroot)
10490Sstevel@tonic-gate {
10500Sstevel@tonic-gate 	if (issuidroot && !HAS_ALLZONEPRIVS(cred))
10510Sstevel@tonic-gate 		return (EPERM);
10520Sstevel@tonic-gate 
10530Sstevel@tonic-gate 	return (!PRIV_POLICY_CHOICE(cred, PRIV_FILE_SETID, B_FALSE));
10540Sstevel@tonic-gate }
10550Sstevel@tonic-gate 
10560Sstevel@tonic-gate /*
10570Sstevel@tonic-gate  * Name:	secpolicy_vnode_setids_setgids()
10580Sstevel@tonic-gate  *
10590Sstevel@tonic-gate  * Normal:	verify that subject can set the file setgid flag.
10600Sstevel@tonic-gate  *
10610Sstevel@tonic-gate  * Output:	EPERM - if not privileged
10620Sstevel@tonic-gate  */
10630Sstevel@tonic-gate 
10640Sstevel@tonic-gate int
10650Sstevel@tonic-gate secpolicy_vnode_setids_setgids(const cred_t *cred, gid_t gid)
10660Sstevel@tonic-gate {
10670Sstevel@tonic-gate 	if (!groupmember(gid, cred))
10680Sstevel@tonic-gate 		return (PRIV_POLICY(cred, PRIV_FILE_SETID, B_FALSE, EPERM,
10690Sstevel@tonic-gate 		    NULL));
10700Sstevel@tonic-gate 	return (0);
10710Sstevel@tonic-gate }
10720Sstevel@tonic-gate 
10730Sstevel@tonic-gate /*
10747624SMark.Shellenbaum@Sun.COM  * Name:	secpolicy_vnode_chown
10757624SMark.Shellenbaum@Sun.COM  *
10767624SMark.Shellenbaum@Sun.COM  * Normal:	Determine if subject can chown owner of a file.
10777624SMark.Shellenbaum@Sun.COM  *
10787624SMark.Shellenbaum@Sun.COM  * Output:	EPERM - if access denied
10797624SMark.Shellenbaum@Sun.COM  */
10807624SMark.Shellenbaum@Sun.COM 
10817624SMark.Shellenbaum@Sun.COM int
10829866SMark.Shellenbaum@Sun.COM secpolicy_vnode_chown(const cred_t *cred, uid_t owner)
10837624SMark.Shellenbaum@Sun.COM {
10849866SMark.Shellenbaum@Sun.COM 	boolean_t is_owner = (owner == crgetuid(cred));
10859866SMark.Shellenbaum@Sun.COM 	boolean_t allzone = B_FALSE;
10869866SMark.Shellenbaum@Sun.COM 	int priv;
10879866SMark.Shellenbaum@Sun.COM 
10889866SMark.Shellenbaum@Sun.COM 	if (!is_owner) {
10899866SMark.Shellenbaum@Sun.COM 		allzone = (owner == 0);
10909866SMark.Shellenbaum@Sun.COM 		priv = PRIV_FILE_CHOWN;
10919866SMark.Shellenbaum@Sun.COM 	} else {
10929866SMark.Shellenbaum@Sun.COM 		priv = HAS_PRIVILEGE(cred, PRIV_FILE_CHOWN) ?
10939866SMark.Shellenbaum@Sun.COM 		    PRIV_FILE_CHOWN : PRIV_FILE_CHOWN_SELF;
10949866SMark.Shellenbaum@Sun.COM 	}
10959866SMark.Shellenbaum@Sun.COM 
10969866SMark.Shellenbaum@Sun.COM 	return (PRIV_POLICY(cred, priv, allzone, EPERM, NULL));
10977624SMark.Shellenbaum@Sun.COM }
10987624SMark.Shellenbaum@Sun.COM 
10997624SMark.Shellenbaum@Sun.COM /*
11007624SMark.Shellenbaum@Sun.COM  * Name:	secpolicy_vnode_create_gid
11017624SMark.Shellenbaum@Sun.COM  *
11027624SMark.Shellenbaum@Sun.COM  * Normal:	Determine if subject can change group ownership of a file.
11037624SMark.Shellenbaum@Sun.COM  *
11047624SMark.Shellenbaum@Sun.COM  * Output:	EPERM - if access denied
11050Sstevel@tonic-gate  */
11060Sstevel@tonic-gate int
11070Sstevel@tonic-gate secpolicy_vnode_create_gid(const cred_t *cred)
11080Sstevel@tonic-gate {
11099866SMark.Shellenbaum@Sun.COM 	if (HAS_PRIVILEGE(cred, PRIV_FILE_CHOWN))
11109866SMark.Shellenbaum@Sun.COM 		return (PRIV_POLICY(cred, PRIV_FILE_CHOWN, B_FALSE, EPERM,
11119866SMark.Shellenbaum@Sun.COM 		    NULL));
11129866SMark.Shellenbaum@Sun.COM 	else
11139866SMark.Shellenbaum@Sun.COM 		return (PRIV_POLICY(cred, PRIV_FILE_CHOWN_SELF, B_FALSE, EPERM,
11149866SMark.Shellenbaum@Sun.COM 		    NULL));
11150Sstevel@tonic-gate }
11160Sstevel@tonic-gate 
11170Sstevel@tonic-gate /*
11180Sstevel@tonic-gate  * Name:	secpolicy_vnode_utime_modify()
11190Sstevel@tonic-gate  *
11200Sstevel@tonic-gate  * Normal:	verify that subject can modify the utime on a file.
11210Sstevel@tonic-gate  *
11220Sstevel@tonic-gate  * Output:	EPERM - if access denied.
11230Sstevel@tonic-gate  */
11240Sstevel@tonic-gate 
11250Sstevel@tonic-gate static int
11260Sstevel@tonic-gate secpolicy_vnode_utime_modify(const cred_t *cred)
11270Sstevel@tonic-gate {
11280Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_FILE_OWNER, B_FALSE, EPERM,
11290Sstevel@tonic-gate 	    "modify file times"));
11300Sstevel@tonic-gate }
11310Sstevel@tonic-gate 
11320Sstevel@tonic-gate 
11330Sstevel@tonic-gate /*
11340Sstevel@tonic-gate  * Name:	secpolicy_vnode_setdac()
11350Sstevel@tonic-gate  *
11360Sstevel@tonic-gate  * Normal:	verify that subject can modify the mode of a file.
11370Sstevel@tonic-gate  *		allzone privilege needed when modifying root owned object.
11380Sstevel@tonic-gate  *
11390Sstevel@tonic-gate  * Output:	EPERM - if access denied.
11400Sstevel@tonic-gate  */
11410Sstevel@tonic-gate 
11420Sstevel@tonic-gate int
11430Sstevel@tonic-gate secpolicy_vnode_setdac(const cred_t *cred, uid_t owner)
11440Sstevel@tonic-gate {
11450Sstevel@tonic-gate 	if (owner == cred->cr_uid)
11460Sstevel@tonic-gate 		return (0);
11470Sstevel@tonic-gate 
11480Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_FILE_OWNER, owner == 0, EPERM, NULL));
11490Sstevel@tonic-gate }
11500Sstevel@tonic-gate /*
11510Sstevel@tonic-gate  * Name:	secpolicy_vnode_stky_modify()
11520Sstevel@tonic-gate  *
11530Sstevel@tonic-gate  * Normal:	verify that subject can make a file a "sticky".
11540Sstevel@tonic-gate  *
11550Sstevel@tonic-gate  * Output:	EPERM - if access denied.
11560Sstevel@tonic-gate  */
11570Sstevel@tonic-gate 
11580Sstevel@tonic-gate int
11590Sstevel@tonic-gate secpolicy_vnode_stky_modify(const cred_t *cred)
11600Sstevel@tonic-gate {
11610Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_SYS_CONFIG, B_FALSE, EPERM,
11620Sstevel@tonic-gate 	    "set file sticky"));
11630Sstevel@tonic-gate }
11640Sstevel@tonic-gate 
11650Sstevel@tonic-gate /*
11660Sstevel@tonic-gate  * Policy determines whether we can remove an entry from a directory,
11670Sstevel@tonic-gate  * regardless of permission bits.
11680Sstevel@tonic-gate  */
11690Sstevel@tonic-gate int
11700Sstevel@tonic-gate secpolicy_vnode_remove(const cred_t *cr)
11710Sstevel@tonic-gate {
11720Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_OWNER, B_FALSE, EACCES,
11730Sstevel@tonic-gate 	    "sticky directory"));
11740Sstevel@tonic-gate }
11750Sstevel@tonic-gate 
11760Sstevel@tonic-gate int
11770Sstevel@tonic-gate secpolicy_vnode_owner(const cred_t *cr, uid_t owner)
11780Sstevel@tonic-gate {
11790Sstevel@tonic-gate 	boolean_t allzone = (owner == 0);
11800Sstevel@tonic-gate 
11810Sstevel@tonic-gate 	if (owner == cr->cr_uid)
11820Sstevel@tonic-gate 		return (0);
11830Sstevel@tonic-gate 
11840Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_OWNER, allzone, EPERM, NULL));
11850Sstevel@tonic-gate }
11860Sstevel@tonic-gate 
11871115Smarks void
11881115Smarks secpolicy_setid_clear(vattr_t *vap, cred_t *cr)
11891115Smarks {
11901115Smarks 	if ((vap->va_mode & (S_ISUID | S_ISGID)) != 0 &&
11911115Smarks 	    secpolicy_vnode_setid_retain(cr,
11921115Smarks 	    (vap->va_mode & S_ISUID) != 0 &&
11931115Smarks 	    (vap->va_mask & AT_UID) != 0 && vap->va_uid == 0) != 0) {
11941115Smarks 		vap->va_mask |= AT_MODE;
11951115Smarks 		vap->va_mode &= ~(S_ISUID|S_ISGID);
11961115Smarks 	}
11971115Smarks }
11981115Smarks 
11992796Smarks int
12002796Smarks secpolicy_setid_setsticky_clear(vnode_t *vp, vattr_t *vap, const vattr_t *ovap,
12012796Smarks     cred_t *cr)
12022796Smarks {
12032796Smarks 	int error;
12042796Smarks 
12052796Smarks 	if ((vap->va_mode & S_ISUID) != 0 &&
12062796Smarks 	    (error = secpolicy_vnode_setid_modify(cr,
12072796Smarks 	    ovap->va_uid)) != 0) {
12082796Smarks 		return (error);
12092796Smarks 	}
12102796Smarks 
12112796Smarks 	/*
12122796Smarks 	 * Check privilege if attempting to set the
12132796Smarks 	 * sticky bit on a non-directory.
12142796Smarks 	 */
12152796Smarks 	if (vp->v_type != VDIR && (vap->va_mode & S_ISVTX) != 0 &&
12162796Smarks 	    secpolicy_vnode_stky_modify(cr) != 0) {
12174543Smarks 		vap->va_mode &= ~S_ISVTX;
12182796Smarks 	}
12192796Smarks 
12202796Smarks 	/*
12212796Smarks 	 * Check for privilege if attempting to set the
12222796Smarks 	 * group-id bit.
12232796Smarks 	 */
12242796Smarks 	if ((vap->va_mode & S_ISGID) != 0 &&
12252796Smarks 	    secpolicy_vnode_setids_setgids(cr, ovap->va_gid) != 0) {
12264543Smarks 		vap->va_mode &= ~S_ISGID;
12272796Smarks 	}
12282796Smarks 
12292796Smarks 	return (0);
12302796Smarks }
12312796Smarks 
12325331Samw #define	ATTR_FLAG_PRIV(attr, value, cr)	\
12335331Samw 	PRIV_POLICY(cr, value ? PRIV_FILE_FLAG_SET : PRIV_ALL, \
12345331Samw 	B_FALSE, EPERM, NULL)
12355331Samw 
12365331Samw /*
12375331Samw  * Check privileges for setting xvattr attributes
12385331Samw  */
12395331Samw int
12405331Samw secpolicy_xvattr(xvattr_t *xvap, uid_t owner, cred_t *cr, vtype_t vtype)
12415331Samw {
12425331Samw 	xoptattr_t *xoap;
12435331Samw 	int error = 0;
12445331Samw 
12455331Samw 	if ((xoap = xva_getxoptattr(xvap)) == NULL)
12465331Samw 		return (EINVAL);
12475331Samw 
12485331Samw 	/*
12495331Samw 	 * First process the DOS bits
12505331Samw 	 */
12515331Samw 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
12525331Samw 	    XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
12535331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
12545331Samw 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM) ||
12555331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
12565331Samw 		if ((error = secpolicy_vnode_owner(cr, owner)) != 0)
12575331Samw 			return (error);
12585331Samw 	}
12595331Samw 
12605331Samw 	/*
12615331Samw 	 * Now handle special attributes
12625331Samw 	 */
12635331Samw 
12645331Samw 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
12655331Samw 		error = ATTR_FLAG_PRIV(XAT_IMMUTABLE,
12665331Samw 		    xoap->xoa_immutable, cr);
12675331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
12685331Samw 		error = ATTR_FLAG_PRIV(XAT_NOUNLINK,
12695331Samw 		    xoap->xoa_nounlink, cr);
12705331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
12715331Samw 		error = ATTR_FLAG_PRIV(XAT_APPENDONLY,
12725331Samw 		    xoap->xoa_appendonly, cr);
12735331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_NODUMP))
12745331Samw 		error = ATTR_FLAG_PRIV(XAT_NODUMP,
12755331Samw 		    xoap->xoa_nodump, cr);
12765331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_OPAQUE))
12775331Samw 		error = EPERM;
12785331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
12795331Samw 		error = ATTR_FLAG_PRIV(XAT_AV_QUARANTINED,
12805331Samw 		    xoap->xoa_av_quarantined, cr);
12815545Smarks 		if (error == 0 && vtype != VREG && xoap->xoa_av_quarantined)
12825331Samw 			error = EINVAL;
12835331Samw 	}
12845331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
12855331Samw 		error = ATTR_FLAG_PRIV(XAT_AV_MODIFIED,
12865331Samw 		    xoap->xoa_av_modified, cr);
12875331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
12885331Samw 		error = ATTR_FLAG_PRIV(XAT_AV_SCANSTAMP,
12895331Samw 		    xoap->xoa_av_scanstamp, cr);
12905331Samw 		if (error == 0 && vtype != VREG)
12915331Samw 			error = EINVAL;
12925331Samw 	}
12935331Samw 	return (error);
12945331Samw }
12955331Samw 
12960Sstevel@tonic-gate /*
12970Sstevel@tonic-gate  * This function checks the policy decisions surrounding the
12980Sstevel@tonic-gate  * vop setattr call.
12990Sstevel@tonic-gate  *
13000Sstevel@tonic-gate  * It should be called after sufficient locks have been established
13010Sstevel@tonic-gate  * on the underlying data structures.  No concurrent modifications
13020Sstevel@tonic-gate  * should be allowed.
13030Sstevel@tonic-gate  *
13040Sstevel@tonic-gate  * The caller must pass in unlocked version of its vaccess function
13050Sstevel@tonic-gate  * this is required because vop_access function should lock the
13060Sstevel@tonic-gate  * node for reading.  A three argument function should be defined
13070Sstevel@tonic-gate  * which accepts the following argument:
13080Sstevel@tonic-gate  * 	A pointer to the internal "node" type (inode *)
13090Sstevel@tonic-gate  *	vnode access bits (VREAD|VWRITE|VEXEC)
13100Sstevel@tonic-gate  *	a pointer to the credential
13110Sstevel@tonic-gate  *
13120Sstevel@tonic-gate  * This function makes the following policy decisions:
13130Sstevel@tonic-gate  *
13140Sstevel@tonic-gate  *		- change permissions
13150Sstevel@tonic-gate  *			- permission to change file mode if not owner
13160Sstevel@tonic-gate  *			- permission to add sticky bit to non-directory
13170Sstevel@tonic-gate  *			- permission to add set-gid bit
13180Sstevel@tonic-gate  *
13190Sstevel@tonic-gate  * The ovap argument should include AT_MODE|AT_UID|AT_GID.
13200Sstevel@tonic-gate  *
13210Sstevel@tonic-gate  * If the vap argument does not include AT_MODE, the mode will be copied from
13220Sstevel@tonic-gate  * ovap.  In certain situations set-uid/set-gid bits need to be removed;
13230Sstevel@tonic-gate  * this is done by marking vap->va_mask to include AT_MODE and va_mode
13240Sstevel@tonic-gate  * is updated to the newly computed mode.
13250Sstevel@tonic-gate  */
13260Sstevel@tonic-gate 
13270Sstevel@tonic-gate int
13280Sstevel@tonic-gate secpolicy_vnode_setattr(cred_t *cr, struct vnode *vp, struct vattr *vap,
13290Sstevel@tonic-gate 	const struct vattr *ovap, int flags,
13300Sstevel@tonic-gate 	int unlocked_access(void *, int, cred_t *),
13310Sstevel@tonic-gate 	void *node)
13320Sstevel@tonic-gate {
13330Sstevel@tonic-gate 	int mask = vap->va_mask;
13340Sstevel@tonic-gate 	int error = 0;
13355331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
13360Sstevel@tonic-gate 
13370Sstevel@tonic-gate 	if (mask & AT_SIZE) {
13380Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
13390Sstevel@tonic-gate 			error = EISDIR;
13400Sstevel@tonic-gate 			goto out;
13410Sstevel@tonic-gate 		}
13425331Samw 
13435331Samw 		/*
13445331Samw 		 * If ATTR_NOACLCHECK is set in the flags, then we don't
13455331Samw 		 * perform the secondary unlocked_access() call since the
13465331Samw 		 * ACL (if any) is being checked there.
13475331Samw 		 */
13485331Samw 		if (skipaclchk == B_FALSE) {
13495331Samw 			error = unlocked_access(node, VWRITE, cr);
13505331Samw 			if (error)
13515331Samw 				goto out;
13525331Samw 		}
13530Sstevel@tonic-gate 	}
13540Sstevel@tonic-gate 	if (mask & AT_MODE) {
13550Sstevel@tonic-gate 		/*
13560Sstevel@tonic-gate 		 * If not the owner of the file then check privilege
13570Sstevel@tonic-gate 		 * for two things: the privilege to set the mode at all
13580Sstevel@tonic-gate 		 * and, if we're setting setuid, we also need permissions
13590Sstevel@tonic-gate 		 * to add the set-uid bit, if we're not the owner.
13600Sstevel@tonic-gate 		 * In the specific case of creating a set-uid root
13610Sstevel@tonic-gate 		 * file, we need even more permissions.
13620Sstevel@tonic-gate 		 */
13630Sstevel@tonic-gate 		if ((error = secpolicy_vnode_setdac(cr, ovap->va_uid)) != 0)
13640Sstevel@tonic-gate 			goto out;
13650Sstevel@tonic-gate 
13662796Smarks 		if ((error = secpolicy_setid_setsticky_clear(vp, vap,
13672796Smarks 		    ovap, cr)) != 0)
13680Sstevel@tonic-gate 			goto out;
13690Sstevel@tonic-gate 	} else
13700Sstevel@tonic-gate 		vap->va_mode = ovap->va_mode;
13710Sstevel@tonic-gate 
13720Sstevel@tonic-gate 	if (mask & (AT_UID|AT_GID)) {
13730Sstevel@tonic-gate 		boolean_t checkpriv = B_FALSE;
13740Sstevel@tonic-gate 
13750Sstevel@tonic-gate 		/*
13760Sstevel@tonic-gate 		 * Chowning files.
13770Sstevel@tonic-gate 		 *
13780Sstevel@tonic-gate 		 * If you are the file owner:
13790Sstevel@tonic-gate 		 *	chown to other uid		FILE_CHOWN_SELF
13800Sstevel@tonic-gate 		 *	chown to gid (non-member) 	FILE_CHOWN_SELF
13810Sstevel@tonic-gate 		 *	chown to gid (member) 		<none>
13820Sstevel@tonic-gate 		 *
13830Sstevel@tonic-gate 		 * Instead of PRIV_FILE_CHOWN_SELF, FILE_CHOWN is also
13840Sstevel@tonic-gate 		 * acceptable but the first one is reported when debugging.
13850Sstevel@tonic-gate 		 *
13860Sstevel@tonic-gate 		 * If you are not the file owner:
13870Sstevel@tonic-gate 		 *	chown from root			PRIV_FILE_CHOWN + zone
13880Sstevel@tonic-gate 		 *	chown from other to any		PRIV_FILE_CHOWN
13890Sstevel@tonic-gate 		 *
13900Sstevel@tonic-gate 		 */
13910Sstevel@tonic-gate 		if (cr->cr_uid != ovap->va_uid) {
13920Sstevel@tonic-gate 			checkpriv = B_TRUE;
13930Sstevel@tonic-gate 		} else {
13940Sstevel@tonic-gate 			if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
13950Sstevel@tonic-gate 			    ((mask & AT_GID) && vap->va_gid != ovap->va_gid &&
13960Sstevel@tonic-gate 			    !groupmember(vap->va_gid, cr))) {
13970Sstevel@tonic-gate 				checkpriv = B_TRUE;
13980Sstevel@tonic-gate 			}
13990Sstevel@tonic-gate 		}
14000Sstevel@tonic-gate 		/*
14010Sstevel@tonic-gate 		 * If necessary, check privilege to see if update can be done.
14020Sstevel@tonic-gate 		 */
14030Sstevel@tonic-gate 		if (checkpriv &&
14049866SMark.Shellenbaum@Sun.COM 		    (error = secpolicy_vnode_chown(cr, ovap->va_uid)) != 0) {
14050Sstevel@tonic-gate 			goto out;
14060Sstevel@tonic-gate 		}
14070Sstevel@tonic-gate 
14080Sstevel@tonic-gate 		/*
14090Sstevel@tonic-gate 		 * If the file has either the set UID or set GID bits
14100Sstevel@tonic-gate 		 * set and the caller can set the bits, then leave them.
14110Sstevel@tonic-gate 		 */
14121115Smarks 		secpolicy_setid_clear(vap, cr);
14130Sstevel@tonic-gate 	}
14140Sstevel@tonic-gate 	if (mask & (AT_ATIME|AT_MTIME)) {
14150Sstevel@tonic-gate 		/*
14160Sstevel@tonic-gate 		 * If not the file owner and not otherwise privileged,
14170Sstevel@tonic-gate 		 * always return an error when setting the
14180Sstevel@tonic-gate 		 * time other than the current (ATTR_UTIME flag set).
14190Sstevel@tonic-gate 		 * If setting the current time (ATTR_UTIME not set) then
14200Sstevel@tonic-gate 		 * unlocked_access will check permissions according to policy.
14210Sstevel@tonic-gate 		 */
14220Sstevel@tonic-gate 		if (cr->cr_uid != ovap->va_uid) {
14230Sstevel@tonic-gate 			if (flags & ATTR_UTIME)
14240Sstevel@tonic-gate 				error = secpolicy_vnode_utime_modify(cr);
14255331Samw 			else if (skipaclchk == B_FALSE) {
14260Sstevel@tonic-gate 				error = unlocked_access(node, VWRITE, cr);
14270Sstevel@tonic-gate 				if (error == EACCES &&
14280Sstevel@tonic-gate 				    secpolicy_vnode_utime_modify(cr) == 0)
14290Sstevel@tonic-gate 					error = 0;
14300Sstevel@tonic-gate 			}
14310Sstevel@tonic-gate 			if (error)
14320Sstevel@tonic-gate 				goto out;
14330Sstevel@tonic-gate 		}
14340Sstevel@tonic-gate 	}
14355331Samw 
14365331Samw 	/*
14375331Samw 	 * Check for optional attributes here by checking the following:
14385331Samw 	 */
14395331Samw 	if (mask & AT_XVATTR)
14405331Samw 		error = secpolicy_xvattr((xvattr_t *)vap, ovap->va_uid, cr,
14415331Samw 		    vp->v_type);
14420Sstevel@tonic-gate out:
14430Sstevel@tonic-gate 	return (error);
14440Sstevel@tonic-gate }
14450Sstevel@tonic-gate 
14460Sstevel@tonic-gate /*
14470Sstevel@tonic-gate  * Name:	secpolicy_pcfs_modify_bootpartition()
14480Sstevel@tonic-gate  *
14490Sstevel@tonic-gate  * Normal:	verify that subject can modify a pcfs boot partition.
14500Sstevel@tonic-gate  *
14510Sstevel@tonic-gate  * Output:	EACCES - if privilege check failed.
14520Sstevel@tonic-gate  */
14530Sstevel@tonic-gate /*ARGSUSED*/
14540Sstevel@tonic-gate int
14550Sstevel@tonic-gate secpolicy_pcfs_modify_bootpartition(const cred_t *cred)
14560Sstevel@tonic-gate {
14570Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_ALL, B_FALSE, EACCES,
14580Sstevel@tonic-gate 	    "modify pcfs boot partition"));
14590Sstevel@tonic-gate }
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate /*
14620Sstevel@tonic-gate  * System V IPC routines
14630Sstevel@tonic-gate  */
14640Sstevel@tonic-gate int
14650Sstevel@tonic-gate secpolicy_ipc_owner(const cred_t *cr, const struct kipc_perm *ip)
14660Sstevel@tonic-gate {
14670Sstevel@tonic-gate 	if (crgetzoneid(cr) != ip->ipc_zoneid ||
14680Sstevel@tonic-gate 	    (cr->cr_uid != ip->ipc_uid && cr->cr_uid != ip->ipc_cuid)) {
14690Sstevel@tonic-gate 		boolean_t allzone = B_FALSE;
14700Sstevel@tonic-gate 		if (ip->ipc_uid == 0 || ip->ipc_cuid == 0)
14710Sstevel@tonic-gate 			allzone = B_TRUE;
14720Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_OWNER, allzone, EPERM, NULL));
14730Sstevel@tonic-gate 	}
14740Sstevel@tonic-gate 	return (0);
14750Sstevel@tonic-gate }
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate int
14780Sstevel@tonic-gate secpolicy_ipc_config(const cred_t *cr)
14790Sstevel@tonic-gate {
14800Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_IPC_CONFIG, B_FALSE, EPERM, NULL));
14810Sstevel@tonic-gate }
14820Sstevel@tonic-gate 
14830Sstevel@tonic-gate int
14840Sstevel@tonic-gate secpolicy_ipc_access(const cred_t *cr, const struct kipc_perm *ip, mode_t mode)
14850Sstevel@tonic-gate {
14860Sstevel@tonic-gate 
14870Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
14880Sstevel@tonic-gate 
14890Sstevel@tonic-gate 	ASSERT((mode & (MSG_R|MSG_W)) != 0);
14900Sstevel@tonic-gate 
14910Sstevel@tonic-gate 	if ((mode & MSG_R) &&
14920Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_IPC_DAC_READ, allzone, EACCES, NULL) != 0)
14930Sstevel@tonic-gate 		return (EACCES);
14940Sstevel@tonic-gate 
14950Sstevel@tonic-gate 	if (mode & MSG_W) {
14960Sstevel@tonic-gate 		if (cr->cr_uid != 0 && (ip->ipc_uid == 0 || ip->ipc_cuid == 0))
14970Sstevel@tonic-gate 			allzone = B_TRUE;
14980Sstevel@tonic-gate 
14990Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_DAC_WRITE, allzone, EACCES,
15000Sstevel@tonic-gate 		    NULL));
15010Sstevel@tonic-gate 	}
15020Sstevel@tonic-gate 	return (0);
15030Sstevel@tonic-gate }
15040Sstevel@tonic-gate 
15050Sstevel@tonic-gate int
15060Sstevel@tonic-gate secpolicy_rsm_access(const cred_t *cr, uid_t owner, mode_t mode)
15070Sstevel@tonic-gate {
15080Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate 	ASSERT((mode & (MSG_R|MSG_W)) != 0);
15110Sstevel@tonic-gate 
15120Sstevel@tonic-gate 	if ((mode & MSG_R) &&
15130Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_IPC_DAC_READ, allzone, EACCES, NULL) != 0)
15140Sstevel@tonic-gate 		return (EACCES);
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate 	if (mode & MSG_W) {
15170Sstevel@tonic-gate 		if (cr->cr_uid != 0 && owner == 0)
15180Sstevel@tonic-gate 			allzone = B_TRUE;
15190Sstevel@tonic-gate 
15200Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_DAC_WRITE, allzone, EACCES,
15210Sstevel@tonic-gate 		    NULL));
15220Sstevel@tonic-gate 	}
15230Sstevel@tonic-gate 	return (0);
15240Sstevel@tonic-gate }
15250Sstevel@tonic-gate 
15260Sstevel@tonic-gate /*
15270Sstevel@tonic-gate  * Audit configuration.
15280Sstevel@tonic-gate  */
15290Sstevel@tonic-gate int
15300Sstevel@tonic-gate secpolicy_audit_config(const cred_t *cr)
15310Sstevel@tonic-gate {
15320Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_AUDIT, B_FALSE, EPERM, NULL));
15330Sstevel@tonic-gate }
15340Sstevel@tonic-gate 
15350Sstevel@tonic-gate /*
15360Sstevel@tonic-gate  * Audit record generation.
15370Sstevel@tonic-gate  */
15380Sstevel@tonic-gate int
15390Sstevel@tonic-gate secpolicy_audit_modify(const cred_t *cr)
15400Sstevel@tonic-gate {
15410Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_AUDIT, B_FALSE, EPERM, NULL));
15420Sstevel@tonic-gate }
15430Sstevel@tonic-gate 
15440Sstevel@tonic-gate /*
15450Sstevel@tonic-gate  * Get audit attributes.
15460Sstevel@tonic-gate  * Either PRIV_SYS_AUDIT or PRIV_PROC_AUDIT required; report the
15470Sstevel@tonic-gate  * "Least" of the two privileges on error.
15480Sstevel@tonic-gate  */
15490Sstevel@tonic-gate int
1550*12273SCasper.Dik@Sun.COM secpolicy_audit_getattr(const cred_t *cr, boolean_t checkonly)
15510Sstevel@tonic-gate {
1552*12273SCasper.Dik@Sun.COM 	int priv;
1553*12273SCasper.Dik@Sun.COM 
1554*12273SCasper.Dik@Sun.COM 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_AUDIT, B_FALSE))
1555*12273SCasper.Dik@Sun.COM 		priv = PRIV_SYS_AUDIT;
1556*12273SCasper.Dik@Sun.COM 	else
1557*12273SCasper.Dik@Sun.COM 		priv = PRIV_PROC_AUDIT;
1558*12273SCasper.Dik@Sun.COM 
1559*12273SCasper.Dik@Sun.COM 	if (checkonly)
1560*12273SCasper.Dik@Sun.COM 		return (!PRIV_POLICY_ONLY(cr, priv, B_FALSE));
1561*12273SCasper.Dik@Sun.COM 	else
1562*12273SCasper.Dik@Sun.COM 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
15630Sstevel@tonic-gate }
15640Sstevel@tonic-gate 
15650Sstevel@tonic-gate 
15660Sstevel@tonic-gate /*
15670Sstevel@tonic-gate  * Locking physical memory
15680Sstevel@tonic-gate  */
15690Sstevel@tonic-gate int
15700Sstevel@tonic-gate secpolicy_lock_memory(const cred_t *cr)
15710Sstevel@tonic-gate {
15720Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_LOCK_MEMORY, B_FALSE, EPERM, NULL));
15730Sstevel@tonic-gate }
15740Sstevel@tonic-gate 
15750Sstevel@tonic-gate /*
15760Sstevel@tonic-gate  * Accounting (both acct(2) and exacct).
15770Sstevel@tonic-gate  */
15780Sstevel@tonic-gate int
15790Sstevel@tonic-gate secpolicy_acct(const cred_t *cr)
15800Sstevel@tonic-gate {
15810Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ACCT, B_FALSE, EPERM, NULL));
15820Sstevel@tonic-gate }
15830Sstevel@tonic-gate 
15840Sstevel@tonic-gate /*
15850Sstevel@tonic-gate  * Is this process privileged to change its uids at will?
15860Sstevel@tonic-gate  * Uid 0 is still considered "special" and having the SETID
15870Sstevel@tonic-gate  * privilege is not sufficient to get uid 0.
15880Sstevel@tonic-gate  * Files are owned by root, so the privilege would give
15890Sstevel@tonic-gate  * full access and euid 0 is still effective.
15900Sstevel@tonic-gate  *
15910Sstevel@tonic-gate  * If you have the privilege and euid 0 only then do you
15920Sstevel@tonic-gate  * get the powers of root wrt uid 0.
15930Sstevel@tonic-gate  *
15940Sstevel@tonic-gate  * For gid manipulations, this is should be called with an
15950Sstevel@tonic-gate  * uid of -1.
15960Sstevel@tonic-gate  *
15970Sstevel@tonic-gate  */
15980Sstevel@tonic-gate int
15990Sstevel@tonic-gate secpolicy_allow_setid(const cred_t *cr, uid_t newuid, boolean_t checkonly)
16000Sstevel@tonic-gate {
16010Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
16020Sstevel@tonic-gate 
16030Sstevel@tonic-gate 	if (newuid == 0 && cr->cr_uid != 0 && cr->cr_suid != 0 &&
16040Sstevel@tonic-gate 	    cr->cr_ruid != 0) {
16050Sstevel@tonic-gate 		allzone = B_TRUE;
16060Sstevel@tonic-gate 	}
16070Sstevel@tonic-gate 
16080Sstevel@tonic-gate 	return (checkonly ? !PRIV_POLICY_ONLY(cr, PRIV_PROC_SETID, allzone) :
16090Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_PROC_SETID, allzone, EPERM, NULL));
16100Sstevel@tonic-gate }
16110Sstevel@tonic-gate 
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate /*
16140Sstevel@tonic-gate  * Acting on a different process: if the mode is for writing,
16150Sstevel@tonic-gate  * the restrictions are more severe.  This is called after
16160Sstevel@tonic-gate  * we've verified that the uids do not match.
16170Sstevel@tonic-gate  */
16180Sstevel@tonic-gate int
16190Sstevel@tonic-gate secpolicy_proc_owner(const cred_t *scr, const cred_t *tcr, int mode)
16200Sstevel@tonic-gate {
16210Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
16220Sstevel@tonic-gate 
16230Sstevel@tonic-gate 	if ((mode & VWRITE) && scr->cr_uid != 0 &&
16240Sstevel@tonic-gate 	    (tcr->cr_uid == 0 || tcr->cr_ruid == 0 || tcr->cr_suid == 0))
16250Sstevel@tonic-gate 		allzone = B_TRUE;
16260Sstevel@tonic-gate 
16270Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, allzone, EPERM, NULL));
16280Sstevel@tonic-gate }
16290Sstevel@tonic-gate 
16300Sstevel@tonic-gate int
16310Sstevel@tonic-gate secpolicy_proc_access(const cred_t *scr)
16320Sstevel@tonic-gate {
16330Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, B_FALSE, EACCES, NULL));
16340Sstevel@tonic-gate }
16350Sstevel@tonic-gate 
16360Sstevel@tonic-gate int
16370Sstevel@tonic-gate secpolicy_proc_excl_open(const cred_t *scr)
16380Sstevel@tonic-gate {
16390Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, B_FALSE, EBUSY, NULL));
16400Sstevel@tonic-gate }
16410Sstevel@tonic-gate 
16420Sstevel@tonic-gate int
16430Sstevel@tonic-gate secpolicy_proc_zone(const cred_t *scr)
16440Sstevel@tonic-gate {
16450Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_ZONE, B_FALSE, EPERM, NULL));
16460Sstevel@tonic-gate }
16470Sstevel@tonic-gate 
16480Sstevel@tonic-gate /*
16490Sstevel@tonic-gate  * Destroying the system
16500Sstevel@tonic-gate  */
16510Sstevel@tonic-gate 
16520Sstevel@tonic-gate int
16530Sstevel@tonic-gate secpolicy_kmdb(const cred_t *scr)
16540Sstevel@tonic-gate {
16550Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
16560Sstevel@tonic-gate }
16570Sstevel@tonic-gate 
16581414Scindi int
16591414Scindi secpolicy_error_inject(const cred_t *scr)
16601414Scindi {
16611414Scindi 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
16621414Scindi }
16631414Scindi 
16640Sstevel@tonic-gate /*
16650Sstevel@tonic-gate  * Processor sets, cpu configuration, resource pools.
16660Sstevel@tonic-gate  */
16670Sstevel@tonic-gate int
16680Sstevel@tonic-gate secpolicy_pset(const cred_t *cr)
16690Sstevel@tonic-gate {
16700Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
16710Sstevel@tonic-gate }
16720Sstevel@tonic-gate 
16730Sstevel@tonic-gate int
16740Sstevel@tonic-gate secpolicy_ponline(const cred_t *cr)
16750Sstevel@tonic-gate {
16760Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
16770Sstevel@tonic-gate }
16780Sstevel@tonic-gate 
16790Sstevel@tonic-gate int
16800Sstevel@tonic-gate secpolicy_pool(const cred_t *cr)
16810Sstevel@tonic-gate {
16820Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
16830Sstevel@tonic-gate }
16840Sstevel@tonic-gate 
16850Sstevel@tonic-gate int
16860Sstevel@tonic-gate secpolicy_blacklist(const cred_t *cr)
16870Sstevel@tonic-gate {
16880Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
16890Sstevel@tonic-gate }
16900Sstevel@tonic-gate 
16910Sstevel@tonic-gate /*
16920Sstevel@tonic-gate  * Catch all system configuration.
16930Sstevel@tonic-gate  */
16940Sstevel@tonic-gate int
16950Sstevel@tonic-gate secpolicy_sys_config(const cred_t *cr, boolean_t checkonly)
16960Sstevel@tonic-gate {
16970Sstevel@tonic-gate 	if (checkonly) {
16980Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_CONFIG, B_FALSE) ? 0 :
16990Sstevel@tonic-gate 		    EPERM);
17000Sstevel@tonic-gate 	} else {
17010Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
17020Sstevel@tonic-gate 	}
17030Sstevel@tonic-gate }
17040Sstevel@tonic-gate 
17050Sstevel@tonic-gate /*
17060Sstevel@tonic-gate  * Zone administration (halt, reboot, etc.) from within zone.
17070Sstevel@tonic-gate  */
17080Sstevel@tonic-gate int
17090Sstevel@tonic-gate secpolicy_zone_admin(const cred_t *cr, boolean_t checkonly)
17100Sstevel@tonic-gate {
17110Sstevel@tonic-gate 	if (checkonly) {
17120Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_ADMIN, B_FALSE) ? 0 :
17130Sstevel@tonic-gate 		    EPERM);
17140Sstevel@tonic-gate 	} else {
17150Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM,
17160Sstevel@tonic-gate 		    NULL));
17170Sstevel@tonic-gate 	}
17180Sstevel@tonic-gate }
17190Sstevel@tonic-gate 
17200Sstevel@tonic-gate /*
17210Sstevel@tonic-gate  * Zone configuration (create, halt, enter).
17220Sstevel@tonic-gate  */
17230Sstevel@tonic-gate int
17240Sstevel@tonic-gate secpolicy_zone_config(const cred_t *cr)
17250Sstevel@tonic-gate {
17260Sstevel@tonic-gate 	/*
17270Sstevel@tonic-gate 	 * Require all privileges to avoid possibility of privilege
17280Sstevel@tonic-gate 	 * escalation.
17290Sstevel@tonic-gate 	 */
1730*12273SCasper.Dik@Sun.COM 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
17310Sstevel@tonic-gate }
17320Sstevel@tonic-gate 
17330Sstevel@tonic-gate /*
17340Sstevel@tonic-gate  * Various other system configuration calls
17350Sstevel@tonic-gate  */
17360Sstevel@tonic-gate int
17370Sstevel@tonic-gate secpolicy_coreadm(const cred_t *cr)
17380Sstevel@tonic-gate {
17390Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM, NULL));
17400Sstevel@tonic-gate }
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate int
17430Sstevel@tonic-gate secpolicy_systeminfo(const cred_t *cr)
17440Sstevel@tonic-gate {
17450Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM, NULL));
17460Sstevel@tonic-gate }
17470Sstevel@tonic-gate 
17480Sstevel@tonic-gate int
17490Sstevel@tonic-gate secpolicy_dispadm(const cred_t *cr)
17500Sstevel@tonic-gate {
17510Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
17520Sstevel@tonic-gate }
17530Sstevel@tonic-gate 
17540Sstevel@tonic-gate int
17550Sstevel@tonic-gate secpolicy_settime(const cred_t *cr)
17560Sstevel@tonic-gate {
17570Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_TIME, B_FALSE, EPERM, NULL));
17580Sstevel@tonic-gate }
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate /*
17610Sstevel@tonic-gate  * For realtime users: high resolution clock.
17620Sstevel@tonic-gate  */
17630Sstevel@tonic-gate int
17640Sstevel@tonic-gate secpolicy_clock_highres(const cred_t *cr)
17650Sstevel@tonic-gate {
17660Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_CLOCK_HIGHRES, B_FALSE, EPERM,
17670Sstevel@tonic-gate 	    NULL));
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate /*
17710Sstevel@tonic-gate  * drv_priv() is documented as callable from interrupt context, not that
17720Sstevel@tonic-gate  * anyone ever does, but still.  No debugging or auditing can be done when
17730Sstevel@tonic-gate  * it is called from interrupt context.
17740Sstevel@tonic-gate  * returns 0 on succes, EPERM on failure.
17750Sstevel@tonic-gate  */
17760Sstevel@tonic-gate int
17770Sstevel@tonic-gate drv_priv(cred_t *cr)
17780Sstevel@tonic-gate {
17790Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
17800Sstevel@tonic-gate }
17810Sstevel@tonic-gate 
17820Sstevel@tonic-gate int
17830Sstevel@tonic-gate secpolicy_sys_devices(const cred_t *cr)
17840Sstevel@tonic-gate {
17850Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
17860Sstevel@tonic-gate }
17870Sstevel@tonic-gate 
17880Sstevel@tonic-gate int
17890Sstevel@tonic-gate secpolicy_excl_open(const cred_t *cr)
17900Sstevel@tonic-gate {
17910Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EBUSY, NULL));
17920Sstevel@tonic-gate }
17930Sstevel@tonic-gate 
17940Sstevel@tonic-gate int
17950Sstevel@tonic-gate secpolicy_rctlsys(const cred_t *cr, boolean_t is_zone_rctl)
17960Sstevel@tonic-gate {
17970Sstevel@tonic-gate 	/* zone.* rctls can only be set from the global zone */
17980Sstevel@tonic-gate 	if (is_zone_rctl && priv_policy_global(cr) != 0)
17990Sstevel@tonic-gate 		return (EPERM);
18000Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
18010Sstevel@tonic-gate }
18020Sstevel@tonic-gate 
18030Sstevel@tonic-gate int
18040Sstevel@tonic-gate secpolicy_resource(const cred_t *cr)
18050Sstevel@tonic-gate {
18060Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
18070Sstevel@tonic-gate }
18080Sstevel@tonic-gate 
180910154SStan.Studzinski@Sun.COM int
181010154SStan.Studzinski@Sun.COM secpolicy_resource_anon_mem(const cred_t *cr)
181110154SStan.Studzinski@Sun.COM {
181210154SStan.Studzinski@Sun.COM 	return (PRIV_POLICY_ONLY(cr, PRIV_SYS_RESOURCE, B_FALSE));
181310154SStan.Studzinski@Sun.COM }
181410154SStan.Studzinski@Sun.COM 
18150Sstevel@tonic-gate /*
18160Sstevel@tonic-gate  * Processes with a real uid of 0 escape any form of accounting, much
18170Sstevel@tonic-gate  * like before.
18180Sstevel@tonic-gate  */
18190Sstevel@tonic-gate int
18200Sstevel@tonic-gate secpolicy_newproc(const cred_t *cr)
18210Sstevel@tonic-gate {
18220Sstevel@tonic-gate 	if (cr->cr_ruid == 0)
18230Sstevel@tonic-gate 		return (0);
18240Sstevel@tonic-gate 
18250Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
18260Sstevel@tonic-gate }
18270Sstevel@tonic-gate 
18280Sstevel@tonic-gate /*
18290Sstevel@tonic-gate  * Networking
18300Sstevel@tonic-gate  */
18310Sstevel@tonic-gate int
18320Sstevel@tonic-gate secpolicy_net_rawaccess(const cred_t *cr)
18330Sstevel@tonic-gate {
18340Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_NET_RAWACCESS, B_FALSE, EACCES, NULL));
18350Sstevel@tonic-gate }
18360Sstevel@tonic-gate 
183710639SDarren.Reed@Sun.COM int
183810639SDarren.Reed@Sun.COM secpolicy_net_observability(const cred_t *cr)
183910639SDarren.Reed@Sun.COM {
184010639SDarren.Reed@Sun.COM 	return (PRIV_POLICY(cr, PRIV_NET_OBSERVABILITY, B_FALSE, EACCES, NULL));
184110639SDarren.Reed@Sun.COM }
184210639SDarren.Reed@Sun.COM 
18430Sstevel@tonic-gate /*
18440Sstevel@tonic-gate  * Need this privilege for accessing the ICMP device
18450Sstevel@tonic-gate  */
18460Sstevel@tonic-gate int
18470Sstevel@tonic-gate secpolicy_net_icmpaccess(const cred_t *cr)
18480Sstevel@tonic-gate {
18490Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_NET_ICMPACCESS, B_FALSE, EACCES, NULL));
18500Sstevel@tonic-gate }
18510Sstevel@tonic-gate 
18520Sstevel@tonic-gate /*
18530Sstevel@tonic-gate  * There are a few rare cases where the kernel generates ioctls() from
18540Sstevel@tonic-gate  * interrupt context with a credential of kcred rather than NULL.
18550Sstevel@tonic-gate  * In those cases, we take the safe and cheap test.
18560Sstevel@tonic-gate  */
18570Sstevel@tonic-gate int
18580Sstevel@tonic-gate secpolicy_net_config(const cred_t *cr, boolean_t checkonly)
18590Sstevel@tonic-gate {
18600Sstevel@tonic-gate 	if (checkonly) {
18610Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE) ?
18620Sstevel@tonic-gate 		    0 : EPERM);
18630Sstevel@tonic-gate 	} else {
18640Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_NET_CONFIG, B_FALSE, EPERM,
18650Sstevel@tonic-gate 		    NULL));
18660Sstevel@tonic-gate 	}
18670Sstevel@tonic-gate }
18680Sstevel@tonic-gate 
18690Sstevel@tonic-gate 
18700Sstevel@tonic-gate /*
18714962Sdh155122  * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_IP_CONFIG.
18723448Sdh155122  *
18733448Sdh155122  * There are a few rare cases where the kernel generates ioctls() from
18743448Sdh155122  * interrupt context with a credential of kcred rather than NULL.
18753448Sdh155122  * In those cases, we take the safe and cheap test.
18763448Sdh155122  */
18773448Sdh155122 int
18783448Sdh155122 secpolicy_ip_config(const cred_t *cr, boolean_t checkonly)
18793448Sdh155122 {
18803448Sdh155122 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
18813448Sdh155122 		return (secpolicy_net_config(cr, checkonly));
18823448Sdh155122 
18833448Sdh155122 	if (checkonly) {
18843448Sdh155122 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_IP_CONFIG, B_FALSE) ?
18853448Sdh155122 		    0 : EPERM);
18863448Sdh155122 	} else {
18873448Sdh155122 		return (PRIV_POLICY(cr, PRIV_SYS_IP_CONFIG, B_FALSE, EPERM,
18883448Sdh155122 		    NULL));
18893448Sdh155122 	}
18903448Sdh155122 }
18913448Sdh155122 
18927408SSebastien.Roy@Sun.COM /*
18937408SSebastien.Roy@Sun.COM  * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_DL_CONFIG.
18947408SSebastien.Roy@Sun.COM  */
18957408SSebastien.Roy@Sun.COM int
18967408SSebastien.Roy@Sun.COM secpolicy_dl_config(const cred_t *cr)
18977408SSebastien.Roy@Sun.COM {
18987408SSebastien.Roy@Sun.COM 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
18997408SSebastien.Roy@Sun.COM 		return (secpolicy_net_config(cr, B_FALSE));
190010616SSebastien.Roy@Sun.COM 	return (PRIV_POLICY(cr, PRIV_SYS_DL_CONFIG, B_FALSE, EPERM, NULL));
19017408SSebastien.Roy@Sun.COM }
19027408SSebastien.Roy@Sun.COM 
190310616SSebastien.Roy@Sun.COM /*
190410616SSebastien.Roy@Sun.COM  * PRIV_SYS_DL_CONFIG is a superset of PRIV_SYS_IPTUN_CONFIG.
190510616SSebastien.Roy@Sun.COM  */
190610616SSebastien.Roy@Sun.COM int
190710616SSebastien.Roy@Sun.COM secpolicy_iptun_config(const cred_t *cr)
190810616SSebastien.Roy@Sun.COM {
190910616SSebastien.Roy@Sun.COM 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
191010616SSebastien.Roy@Sun.COM 		return (secpolicy_net_config(cr, B_FALSE));
191110616SSebastien.Roy@Sun.COM 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_DL_CONFIG, B_FALSE))
191210616SSebastien.Roy@Sun.COM 		return (secpolicy_dl_config(cr));
191310616SSebastien.Roy@Sun.COM 	return (PRIV_POLICY(cr, PRIV_SYS_IPTUN_CONFIG, B_FALSE, EPERM, NULL));
191410616SSebastien.Roy@Sun.COM }
19153448Sdh155122 
19163448Sdh155122 /*
19173448Sdh155122  * Map IP pseudo privileges to actual privileges.
19183448Sdh155122  * So we don't need to recompile IP when we change the privileges.
19193448Sdh155122  */
19203448Sdh155122 int
19213448Sdh155122 secpolicy_ip(const cred_t *cr, int netpriv, boolean_t checkonly)
19223448Sdh155122 {
19233448Sdh155122 	int priv = PRIV_ALL;
19243448Sdh155122 
19253448Sdh155122 	switch (netpriv) {
19263448Sdh155122 	case OP_CONFIG:
19273448Sdh155122 		priv = PRIV_SYS_IP_CONFIG;
19283448Sdh155122 		break;
19293448Sdh155122 	case OP_RAW:
19303448Sdh155122 		priv = PRIV_NET_RAWACCESS;
19313448Sdh155122 		break;
19323448Sdh155122 	case OP_PRIVPORT:
19333448Sdh155122 		priv = PRIV_NET_PRIVADDR;
19343448Sdh155122 		break;
19353448Sdh155122 	}
19363448Sdh155122 	ASSERT(priv != PRIV_ALL);
19373448Sdh155122 	if (checkonly)
19383448Sdh155122 		return (PRIV_POLICY_ONLY(cr, priv, B_FALSE) ? 0 : EPERM);
19393448Sdh155122 	else
19403448Sdh155122 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
19413448Sdh155122 }
19423448Sdh155122 
19433448Sdh155122 /*
19440Sstevel@tonic-gate  * Map network pseudo privileges to actual privileges.
19450Sstevel@tonic-gate  * So we don't need to recompile IP when we change the privileges.
19460Sstevel@tonic-gate  */
19470Sstevel@tonic-gate int
19480Sstevel@tonic-gate secpolicy_net(const cred_t *cr, int netpriv, boolean_t checkonly)
19490Sstevel@tonic-gate {
19500Sstevel@tonic-gate 	int priv = PRIV_ALL;
19510Sstevel@tonic-gate 
19520Sstevel@tonic-gate 	switch (netpriv) {
19530Sstevel@tonic-gate 	case OP_CONFIG:
19540Sstevel@tonic-gate 		priv = PRIV_SYS_NET_CONFIG;
19550Sstevel@tonic-gate 		break;
19560Sstevel@tonic-gate 	case OP_RAW:
19570Sstevel@tonic-gate 		priv = PRIV_NET_RAWACCESS;
19580Sstevel@tonic-gate 		break;
19590Sstevel@tonic-gate 	case OP_PRIVPORT:
19600Sstevel@tonic-gate 		priv = PRIV_NET_PRIVADDR;
19610Sstevel@tonic-gate 		break;
19620Sstevel@tonic-gate 	}
19630Sstevel@tonic-gate 	ASSERT(priv != PRIV_ALL);
19640Sstevel@tonic-gate 	if (checkonly)
19650Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, priv, B_FALSE) ? 0 : EPERM);
19660Sstevel@tonic-gate 	else
19670Sstevel@tonic-gate 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
19680Sstevel@tonic-gate }
19690Sstevel@tonic-gate 
19700Sstevel@tonic-gate /*
19710Sstevel@tonic-gate  * Checks for operations that are either client-only or are used by
19720Sstevel@tonic-gate  * both clients and servers.
19730Sstevel@tonic-gate  */
19740Sstevel@tonic-gate int
19750Sstevel@tonic-gate secpolicy_nfs(const cred_t *cr)
19760Sstevel@tonic-gate {
19770Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_NFS, B_FALSE, EPERM, NULL));
19780Sstevel@tonic-gate }
19790Sstevel@tonic-gate 
19800Sstevel@tonic-gate /*
19810Sstevel@tonic-gate  * Special case for opening rpcmod: have NFS privileges or network
19820Sstevel@tonic-gate  * config privileges.
19830Sstevel@tonic-gate  */
19840Sstevel@tonic-gate int
19850Sstevel@tonic-gate secpolicy_rpcmod_open(const cred_t *cr)
19860Sstevel@tonic-gate {
19870Sstevel@tonic-gate 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NFS, B_FALSE))
19880Sstevel@tonic-gate 		return (secpolicy_nfs(cr));
19890Sstevel@tonic-gate 	else
19900Sstevel@tonic-gate 		return (secpolicy_net_config(cr, NULL));
19910Sstevel@tonic-gate }
19920Sstevel@tonic-gate 
19930Sstevel@tonic-gate int
19940Sstevel@tonic-gate secpolicy_chroot(const cred_t *cr)
19950Sstevel@tonic-gate {
19960Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_CHROOT, B_FALSE, EPERM, NULL));
19970Sstevel@tonic-gate }
19980Sstevel@tonic-gate 
19990Sstevel@tonic-gate int
20000Sstevel@tonic-gate secpolicy_tasksys(const cred_t *cr)
20010Sstevel@tonic-gate {
20020Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_TASKID, B_FALSE, EPERM, NULL));
20030Sstevel@tonic-gate }
20040Sstevel@tonic-gate 
2005*12273SCasper.Dik@Sun.COM int
2006*12273SCasper.Dik@Sun.COM secpolicy_pfexec_register(const cred_t *cr)
2007*12273SCasper.Dik@Sun.COM {
2008*12273SCasper.Dik@Sun.COM 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_TRUE, EPERM, NULL));
2009*12273SCasper.Dik@Sun.COM }
2010*12273SCasper.Dik@Sun.COM 
20110Sstevel@tonic-gate /*
20120Sstevel@tonic-gate  * Basic privilege checks.
20130Sstevel@tonic-gate  */
20140Sstevel@tonic-gate int
20156134Scasper secpolicy_basic_exec(const cred_t *cr, vnode_t *vp)
20160Sstevel@tonic-gate {
2017*12273SCasper.Dik@Sun.COM 	FAST_BASIC_CHECK(cr, PRIV_PROC_EXEC);
2018*12273SCasper.Dik@Sun.COM 
20196134Scasper 	return (priv_policy_va(cr, PRIV_PROC_EXEC, B_FALSE, EPERM, NULL,
20206134Scasper 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
20210Sstevel@tonic-gate }
20220Sstevel@tonic-gate 
20230Sstevel@tonic-gate int
20240Sstevel@tonic-gate secpolicy_basic_fork(const cred_t *cr)
20250Sstevel@tonic-gate {
2026*12273SCasper.Dik@Sun.COM 	FAST_BASIC_CHECK(cr, PRIV_PROC_FORK);
2027*12273SCasper.Dik@Sun.COM 
20280Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_FORK, B_FALSE, EPERM, NULL));
20290Sstevel@tonic-gate }
20300Sstevel@tonic-gate 
20310Sstevel@tonic-gate int
20320Sstevel@tonic-gate secpolicy_basic_proc(const cred_t *cr)
20330Sstevel@tonic-gate {
2034*12273SCasper.Dik@Sun.COM 	FAST_BASIC_CHECK(cr, PRIV_PROC_SESSION);
2035*12273SCasper.Dik@Sun.COM 
20360Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_SESSION, B_FALSE, EPERM, NULL));
20370Sstevel@tonic-gate }
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate /*
20400Sstevel@tonic-gate  * Slightly complicated because we don't want to trigger the policy too
20410Sstevel@tonic-gate  * often.  First we shortcircuit access to "self" (tp == sp) or if
20420Sstevel@tonic-gate  * we don't have the privilege but if we have permission
20430Sstevel@tonic-gate  * just return (0) and we don't flag the privilege as needed.
20440Sstevel@tonic-gate  * Else, we test for the privilege because we either have it or need it.
20450Sstevel@tonic-gate  */
20460Sstevel@tonic-gate int
20470Sstevel@tonic-gate secpolicy_basic_procinfo(const cred_t *cr, proc_t *tp, proc_t *sp)
20480Sstevel@tonic-gate {
20490Sstevel@tonic-gate 	if (tp == sp ||
20500Sstevel@tonic-gate 	    !HAS_PRIVILEGE(cr, PRIV_PROC_INFO) && prochasprocperm(tp, sp, cr)) {
20510Sstevel@tonic-gate 		return (0);
20520Sstevel@tonic-gate 	} else {
20530Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_PROC_INFO, B_FALSE, EPERM, NULL));
20540Sstevel@tonic-gate 	}
20550Sstevel@tonic-gate }
20560Sstevel@tonic-gate 
20570Sstevel@tonic-gate int
20580Sstevel@tonic-gate secpolicy_basic_link(const cred_t *cr)
20590Sstevel@tonic-gate {
2060*12273SCasper.Dik@Sun.COM 	FAST_BASIC_CHECK(cr, PRIV_FILE_LINK_ANY);
2061*12273SCasper.Dik@Sun.COM 
20620Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_LINK_ANY, B_FALSE, EPERM, NULL));
20630Sstevel@tonic-gate }
20640Sstevel@tonic-gate 
206511537SCasper.Dik@Sun.COM int
206611537SCasper.Dik@Sun.COM secpolicy_basic_net_access(const cred_t *cr)
206711537SCasper.Dik@Sun.COM {
2068*12273SCasper.Dik@Sun.COM 	FAST_BASIC_CHECK(cr, PRIV_NET_ACCESS);
2069*12273SCasper.Dik@Sun.COM 
207011537SCasper.Dik@Sun.COM 	return (PRIV_POLICY(cr, PRIV_NET_ACCESS, B_FALSE, EACCES, NULL));
207111537SCasper.Dik@Sun.COM }
207211537SCasper.Dik@Sun.COM 
2073*12273SCasper.Dik@Sun.COM /* ARGSUSED */
2074*12273SCasper.Dik@Sun.COM int
2075*12273SCasper.Dik@Sun.COM secpolicy_basic_file_read(const cred_t *cr, vnode_t *vp, const char *pn)
2076*12273SCasper.Dik@Sun.COM {
2077*12273SCasper.Dik@Sun.COM 	FAST_BASIC_CHECK(cr, PRIV_FILE_READ);
2078*12273SCasper.Dik@Sun.COM 
2079*12273SCasper.Dik@Sun.COM 	return (priv_policy_va(cr, PRIV_FILE_READ, B_FALSE, EACCES, NULL,
2080*12273SCasper.Dik@Sun.COM 	    KLPDARG_VNODE, vp, (char *)pn, KLPDARG_NOMORE));
2081*12273SCasper.Dik@Sun.COM }
2082*12273SCasper.Dik@Sun.COM 
2083*12273SCasper.Dik@Sun.COM /* ARGSUSED */
2084*12273SCasper.Dik@Sun.COM int
2085*12273SCasper.Dik@Sun.COM secpolicy_basic_file_write(const cred_t *cr, vnode_t *vp, const char *pn)
2086*12273SCasper.Dik@Sun.COM {
2087*12273SCasper.Dik@Sun.COM 	FAST_BASIC_CHECK(cr, PRIV_FILE_WRITE);
2088*12273SCasper.Dik@Sun.COM 
2089*12273SCasper.Dik@Sun.COM 	return (priv_policy_va(cr, PRIV_FILE_WRITE, B_FALSE, EACCES, NULL,
2090*12273SCasper.Dik@Sun.COM 	    KLPDARG_VNODE, vp, (char *)pn, KLPDARG_NOMORE));
2091*12273SCasper.Dik@Sun.COM }
2092*12273SCasper.Dik@Sun.COM 
20930Sstevel@tonic-gate /*
20940Sstevel@tonic-gate  * Additional device protection.
20950Sstevel@tonic-gate  *
20960Sstevel@tonic-gate  * Traditionally, a device has specific permissions on the node in
20970Sstevel@tonic-gate  * the filesystem which govern which devices can be opened by what
20980Sstevel@tonic-gate  * processes.  In certain cases, it is desirable to add extra
20990Sstevel@tonic-gate  * restrictions, as writing to certain devices is identical to
21000Sstevel@tonic-gate  * having a complete run of the system.
21010Sstevel@tonic-gate  *
21020Sstevel@tonic-gate  * This mechanism is called the device policy.
21030Sstevel@tonic-gate  *
21040Sstevel@tonic-gate  * When a device is opened, its policy entry is looked up in the
21050Sstevel@tonic-gate  * policy cache and checked.
21060Sstevel@tonic-gate  */
21070Sstevel@tonic-gate int
21080Sstevel@tonic-gate secpolicy_spec_open(const cred_t *cr, struct vnode *vp, int oflag)
21090Sstevel@tonic-gate {
21100Sstevel@tonic-gate 	devplcy_t *plcy;
21110Sstevel@tonic-gate 	int err;
21120Sstevel@tonic-gate 	struct snode *csp = VTOS(common_specvp(vp));
21134962Sdh155122 	priv_set_t pset;
21140Sstevel@tonic-gate 
21150Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
21160Sstevel@tonic-gate 
21170Sstevel@tonic-gate 	if (csp->s_plcy == NULL || csp->s_plcy->dp_gen != devplcy_gen) {
21180Sstevel@tonic-gate 		plcy = devpolicy_find(vp);
21190Sstevel@tonic-gate 		if (csp->s_plcy)
21200Sstevel@tonic-gate 			dpfree(csp->s_plcy);
21210Sstevel@tonic-gate 		csp->s_plcy = plcy;
21220Sstevel@tonic-gate 		ASSERT(plcy != NULL);
21230Sstevel@tonic-gate 	} else
21240Sstevel@tonic-gate 		plcy = csp->s_plcy;
21250Sstevel@tonic-gate 
21260Sstevel@tonic-gate 	if (plcy == nullpolicy) {
21270Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
21280Sstevel@tonic-gate 		return (0);
21290Sstevel@tonic-gate 	}
21300Sstevel@tonic-gate 
21310Sstevel@tonic-gate 	dphold(plcy);
21320Sstevel@tonic-gate 
21330Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
21340Sstevel@tonic-gate 
21354962Sdh155122 	if (oflag & FWRITE)
21364962Sdh155122 		pset = plcy->dp_wrp;
21374962Sdh155122 	else
21384962Sdh155122 		pset = plcy->dp_rdp;
21394962Sdh155122 	/*
21404962Sdh155122 	 * Special case:
21414962Sdh155122 	 * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_IP_CONFIG.
21424962Sdh155122 	 * If PRIV_SYS_NET_CONFIG is present and PRIV_SYS_IP_CONFIG is
21434962Sdh155122 	 * required, replace PRIV_SYS_IP_CONFIG with PRIV_SYS_NET_CONFIG
21444962Sdh155122 	 * in the required privilege set before doing the check.
21454962Sdh155122 	 */
21464962Sdh155122 	if (priv_ismember(&pset, PRIV_SYS_IP_CONFIG) &&
21474962Sdh155122 	    priv_ismember(&CR_OEPRIV(cr), PRIV_SYS_NET_CONFIG) &&
21484962Sdh155122 	    !priv_ismember(&CR_OEPRIV(cr), PRIV_SYS_IP_CONFIG)) {
21494962Sdh155122 		priv_delset(&pset, PRIV_SYS_IP_CONFIG);
21504962Sdh155122 		priv_addset(&pset, PRIV_SYS_NET_CONFIG);
21514962Sdh155122 	}
21524962Sdh155122 
2153*12273SCasper.Dik@Sun.COM 	err = secpolicy_require_set(cr, &pset, "devpolicy", KLPDARG_NONE);
21540Sstevel@tonic-gate 	dpfree(plcy);
21550Sstevel@tonic-gate 
21560Sstevel@tonic-gate 	return (err);
21570Sstevel@tonic-gate }
21580Sstevel@tonic-gate 
21590Sstevel@tonic-gate int
21600Sstevel@tonic-gate secpolicy_modctl(const cred_t *cr, int cmd)
21610Sstevel@tonic-gate {
21620Sstevel@tonic-gate 	switch (cmd) {
21630Sstevel@tonic-gate 	case MODINFO:
21642723Scth 	case MODGETMAJBIND:
21650Sstevel@tonic-gate 	case MODGETPATH:
21660Sstevel@tonic-gate 	case MODGETPATHLEN:
21672723Scth 	case MODGETNAME:
21680Sstevel@tonic-gate 	case MODGETFBNAME:
21690Sstevel@tonic-gate 	case MODGETDEVPOLICY:
21700Sstevel@tonic-gate 	case MODGETDEVPOLICYBYNAME:
21712723Scth 	case MODDEVT2INSTANCE:
21722723Scth 	case MODSIZEOF_DEVID:
21732723Scth 	case MODGETDEVID:
21742723Scth 	case MODSIZEOF_MINORNAME:
21752723Scth 	case MODGETMINORNAME:
21762723Scth 	case MODGETDEVFSPATH_LEN:
21772723Scth 	case MODGETDEVFSPATH:
21782723Scth 	case MODGETDEVFSPATH_MI_LEN:
21792723Scth 	case MODGETDEVFSPATH_MI:
21800Sstevel@tonic-gate 		/* Unprivileged */
21810Sstevel@tonic-gate 		return (0);
21820Sstevel@tonic-gate 	case MODLOAD:
21830Sstevel@tonic-gate 	case MODSETDEVPOLICY:
2184*12273SCasper.Dik@Sun.COM 		return (secpolicy_require_set(cr, PRIV_FULLSET, NULL,
2185*12273SCasper.Dik@Sun.COM 		    KLPDARG_NONE));
21860Sstevel@tonic-gate 	default:
21870Sstevel@tonic-gate 		return (secpolicy_sys_config(cr, B_FALSE));
21880Sstevel@tonic-gate 	}
21890Sstevel@tonic-gate }
21900Sstevel@tonic-gate 
21910Sstevel@tonic-gate int
21920Sstevel@tonic-gate secpolicy_console(const cred_t *cr)
21930Sstevel@tonic-gate {
21940Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
21950Sstevel@tonic-gate }
21960Sstevel@tonic-gate 
21970Sstevel@tonic-gate int
21980Sstevel@tonic-gate secpolicy_power_mgmt(const cred_t *cr)
21990Sstevel@tonic-gate {
22000Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
22010Sstevel@tonic-gate }
22020Sstevel@tonic-gate 
22030Sstevel@tonic-gate /*
22040Sstevel@tonic-gate  * Simulate terminal input; another escalation of privileges avenue.
22050Sstevel@tonic-gate  */
22060Sstevel@tonic-gate 
22070Sstevel@tonic-gate int
22080Sstevel@tonic-gate secpolicy_sti(const cred_t *cr)
22090Sstevel@tonic-gate {
2210*12273SCasper.Dik@Sun.COM 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
22110Sstevel@tonic-gate }
22120Sstevel@tonic-gate 
22131676Sjpk boolean_t
22141676Sjpk secpolicy_net_reply_equal(const cred_t *cr)
22151676Sjpk {
22161676Sjpk 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
22171676Sjpk }
22181676Sjpk 
22190Sstevel@tonic-gate int
22200Sstevel@tonic-gate secpolicy_swapctl(const cred_t *cr)
22210Sstevel@tonic-gate {
22220Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
22230Sstevel@tonic-gate }
22240Sstevel@tonic-gate 
22250Sstevel@tonic-gate int
22260Sstevel@tonic-gate secpolicy_cpc_cpu(const cred_t *cr)
22270Sstevel@tonic-gate {
22280Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CPC_CPU, B_FALSE, EACCES, NULL));
22290Sstevel@tonic-gate }
22300Sstevel@tonic-gate 
22310Sstevel@tonic-gate /*
22326073Sacruz  * secpolicy_contract_identity
22336073Sacruz  *
22346073Sacruz  * Determine if the subject may set the process contract FMRI value
22356073Sacruz  */
22366073Sacruz int
22376073Sacruz secpolicy_contract_identity(const cred_t *cr)
22386073Sacruz {
22396073Sacruz 	return (PRIV_POLICY(cr, PRIV_CONTRACT_IDENTITY, B_FALSE, EPERM, NULL));
22406073Sacruz }
22416073Sacruz 
22426073Sacruz /*
22430Sstevel@tonic-gate  * secpolicy_contract_observer
22440Sstevel@tonic-gate  *
22450Sstevel@tonic-gate  * Determine if the subject may observe a specific contract's events.
22460Sstevel@tonic-gate  */
22470Sstevel@tonic-gate int
22480Sstevel@tonic-gate secpolicy_contract_observer(const cred_t *cr, struct contract *ct)
22490Sstevel@tonic-gate {
22500Sstevel@tonic-gate 	if (contract_owned(ct, cr, B_FALSE))
22510Sstevel@tonic-gate 		return (0);
22520Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CONTRACT_OBSERVER, B_FALSE, EPERM, NULL));
22530Sstevel@tonic-gate }
22540Sstevel@tonic-gate 
22550Sstevel@tonic-gate /*
22560Sstevel@tonic-gate  * secpolicy_contract_observer_choice
22570Sstevel@tonic-gate  *
22580Sstevel@tonic-gate  * Determine if the subject may observe any contract's events.  Just
22590Sstevel@tonic-gate  * tests privilege and audits on success.
22600Sstevel@tonic-gate  */
22610Sstevel@tonic-gate boolean_t
22620Sstevel@tonic-gate secpolicy_contract_observer_choice(const cred_t *cr)
22630Sstevel@tonic-gate {
22640Sstevel@tonic-gate 	return (PRIV_POLICY_CHOICE(cr, PRIV_CONTRACT_OBSERVER, B_FALSE));
22650Sstevel@tonic-gate }
22660Sstevel@tonic-gate 
22670Sstevel@tonic-gate /*
22680Sstevel@tonic-gate  * secpolicy_contract_event
22690Sstevel@tonic-gate  *
22700Sstevel@tonic-gate  * Determine if the subject may request critical contract events or
22710Sstevel@tonic-gate  * reliable contract event delivery.
22720Sstevel@tonic-gate  */
22730Sstevel@tonic-gate int
22740Sstevel@tonic-gate secpolicy_contract_event(const cred_t *cr)
22750Sstevel@tonic-gate {
22760Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CONTRACT_EVENT, B_FALSE, EPERM, NULL));
22770Sstevel@tonic-gate }
22780Sstevel@tonic-gate 
22790Sstevel@tonic-gate /*
22800Sstevel@tonic-gate  * secpolicy_contract_event_choice
22810Sstevel@tonic-gate  *
22820Sstevel@tonic-gate  * Determine if the subject may retain contract events in its critical
22830Sstevel@tonic-gate  * set when a change in other terms would normally require a change in
22840Sstevel@tonic-gate  * the critical set.  Just tests privilege and audits on success.
22850Sstevel@tonic-gate  */
22860Sstevel@tonic-gate boolean_t
22870Sstevel@tonic-gate secpolicy_contract_event_choice(const cred_t *cr)
22880Sstevel@tonic-gate {
22890Sstevel@tonic-gate 	return (PRIV_POLICY_CHOICE(cr, PRIV_CONTRACT_EVENT, B_FALSE));
22900Sstevel@tonic-gate }
22910Sstevel@tonic-gate 
22920Sstevel@tonic-gate /*
22931544Seschrock  * secpolicy_gart_access
22940Sstevel@tonic-gate  *
22951544Seschrock  * Determine if the subject has sufficient priveleges to make ioctls to agpgart
22961544Seschrock  * device.
22970Sstevel@tonic-gate  */
22980Sstevel@tonic-gate int
22990Sstevel@tonic-gate secpolicy_gart_access(const cred_t *cr)
23000Sstevel@tonic-gate {
23011862Scasper 	return (PRIV_POLICY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE, EPERM, NULL));
23020Sstevel@tonic-gate }
23030Sstevel@tonic-gate 
23040Sstevel@tonic-gate /*
23051544Seschrock  * secpolicy_gart_map
23060Sstevel@tonic-gate  *
23071544Seschrock  * Determine if the subject has sufficient priveleges to map aperture range
23081544Seschrock  * through agpgart driver.
23090Sstevel@tonic-gate  */
23100Sstevel@tonic-gate int
23110Sstevel@tonic-gate secpolicy_gart_map(const cred_t *cr)
23120Sstevel@tonic-gate {
23131862Scasper 	if (PRIV_POLICY_ONLY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE)) {
23141862Scasper 		return (PRIV_POLICY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE, EPERM,
23151862Scasper 		    NULL));
23161862Scasper 	} else {
23171862Scasper 		return (PRIV_POLICY(cr, PRIV_GRAPHICS_MAP, B_FALSE, EPERM,
23181862Scasper 		    NULL));
23190Sstevel@tonic-gate 	}
23200Sstevel@tonic-gate }
2321789Sahrens 
2322789Sahrens /*
23231544Seschrock  * secpolicy_zinject
23241544Seschrock  *
23251544Seschrock  * Determine if the subject can inject faults in the ZFS fault injection
23261544Seschrock  * framework.  Requires all privileges.
23271544Seschrock  */
23281544Seschrock int
23291544Seschrock secpolicy_zinject(const cred_t *cr)
23301544Seschrock {
2331*12273SCasper.Dik@Sun.COM 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
23321544Seschrock }
23331544Seschrock 
23341544Seschrock /*
2335789Sahrens  * secpolicy_zfs
2336789Sahrens  *
23371544Seschrock  * Determine if the subject has permission to manipulate ZFS datasets
23381544Seschrock  * (not pools).  Equivalent to the SYS_MOUNT privilege.
2339789Sahrens  */
2340789Sahrens int
2341789Sahrens secpolicy_zfs(const cred_t *cr)
2342789Sahrens {
2343789Sahrens 	return (PRIV_POLICY(cr, PRIV_SYS_MOUNT, B_FALSE, EPERM, NULL));
2344789Sahrens }
23454321Scasper 
23464321Scasper /*
23474321Scasper  * secpolicy_idmap
23484321Scasper  *
23494321Scasper  * Determine if the calling process has permissions to register an SID
23504321Scasper  * mapping daemon and allocate ephemeral IDs.
23514321Scasper  */
23524321Scasper int
23534321Scasper secpolicy_idmap(const cred_t *cr)
23544321Scasper {
23555771Sjp151216 	return (PRIV_POLICY(cr, PRIV_FILE_SETID, B_TRUE, EPERM, NULL));
23564321Scasper }
23574581Ssherrym 
23584581Ssherrym /*
23594581Ssherrym  * secpolicy_ucode_update
23604581Ssherrym  *
23614581Ssherrym  * Determine if the subject has sufficient privilege to update microcode.
23624581Ssherrym  */
23634581Ssherrym int
23644581Ssherrym secpolicy_ucode_update(const cred_t *scr)
23654581Ssherrym {
23664581Ssherrym 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
23674581Ssherrym }
23684962Sdh155122 
23694962Sdh155122 /*
23704962Sdh155122  * secpolicy_sadopen
23714962Sdh155122  *
23724962Sdh155122  * Determine if the subject has sufficient privilege to access /dev/sad/admin.
23734962Sdh155122  * /dev/sad/admin appear in global zone and exclusive-IP zones only.
23744962Sdh155122  * In global zone, sys_config is required.
23754962Sdh155122  * In exclusive-IP zones, sys_ip_config is required.
23764962Sdh155122  * Note that sys_config is prohibited in non-global zones.
23774962Sdh155122  */
23784962Sdh155122 int
23794962Sdh155122 secpolicy_sadopen(const cred_t *credp)
23804962Sdh155122 {
23814962Sdh155122 	priv_set_t pset;
23824962Sdh155122 
23834962Sdh155122 	priv_emptyset(&pset);
23844962Sdh155122 
23854962Sdh155122 	if (crgetzoneid(credp) == GLOBAL_ZONEID)
23864962Sdh155122 		priv_addset(&pset, PRIV_SYS_CONFIG);
23874962Sdh155122 	else
23884962Sdh155122 		priv_addset(&pset, PRIV_SYS_IP_CONFIG);
23894962Sdh155122 
2390*12273SCasper.Dik@Sun.COM 	return (secpolicy_require_set(credp, &pset, "devpolicy", KLPDARG_NONE));
23914962Sdh155122 }
23925331Samw 
23936134Scasper 
23946134Scasper /*
23956134Scasper  * Add privileges to a particular privilege set; this is called when the
23966134Scasper  * current sets of privileges are not sufficient.  I.e., we should always
23976134Scasper  * call the policy override functions from here.
23986134Scasper  * What we are allowed to have is in the Observed Permitted set; so
23996134Scasper  * we compute the difference between that and the newset.
24006134Scasper  */
24016134Scasper int
24026134Scasper secpolicy_require_privs(const cred_t *cr, const priv_set_t *nset)
24036134Scasper {
24046134Scasper 	priv_set_t rqd;
24056134Scasper 
24066134Scasper 	rqd = CR_OPPRIV(cr);
24076134Scasper 
24086134Scasper 	priv_inverse(&rqd);
24096134Scasper 	priv_intersect(nset, &rqd);
24106134Scasper 
2411*12273SCasper.Dik@Sun.COM 	return (secpolicy_require_set(cr, &rqd, NULL, KLPDARG_NONE));
24126134Scasper }
24136134Scasper 
24145331Samw /*
24155331Samw  * secpolicy_smb
24165331Samw  *
24175331Samw  * Determine if the cred_t has PRIV_SYS_SMB privilege, indicating
24185331Samw  * that it has permission to access the smbsrv kernel driver.
24195331Samw  * PRIV_POLICY checks the privilege and audits the check.
24205331Samw  *
24215331Samw  * Returns:
24225331Samw  * 0       Driver access is allowed.
24235331Samw  * EPERM   Driver access is NOT permitted.
24245331Samw  */
24255331Samw int
24265331Samw secpolicy_smb(const cred_t *cr)
24275331Samw {
24285331Samw 	return (PRIV_POLICY(cr, PRIV_SYS_SMB, B_FALSE, EPERM, NULL));
24295331Samw }
24305440Sjm199354 
24315440Sjm199354 /*
24325440Sjm199354  * secpolicy_vscan
24335440Sjm199354  *
24345440Sjm199354  * Determine if cred_t has the necessary privileges to access a file
24355440Sjm199354  * for virus scanning and update its extended system attributes.
24365440Sjm199354  * PRIV_FILE_DAC_SEARCH, PRIV_FILE_DAC_READ - file access
24375440Sjm199354  * PRIV_FILE_FLAG_SET - set extended system attributes
24385440Sjm199354  *
24395440Sjm199354  * PRIV_POLICY checks the privilege and audits the check.
24405440Sjm199354  *
24415440Sjm199354  * Returns:
24425440Sjm199354  * 0      file access for virus scanning allowed.
24435440Sjm199354  * EPERM  file access for virus scanning is NOT permitted.
24445440Sjm199354  */
24455440Sjm199354 int
24465440Sjm199354 secpolicy_vscan(const cred_t *cr)
24475440Sjm199354 {
24485440Sjm199354 	if ((PRIV_POLICY(cr, PRIV_FILE_DAC_SEARCH, B_FALSE, EPERM, NULL)) ||
24495440Sjm199354 	    (PRIV_POLICY(cr, PRIV_FILE_DAC_READ, B_FALSE, EPERM, NULL)) ||
24505440Sjm199354 	    (PRIV_POLICY(cr, PRIV_FILE_FLAG_SET, B_FALSE, EPERM, NULL))) {
24515440Sjm199354 		return (EPERM);
24525440Sjm199354 	}
24535440Sjm199354 
24545440Sjm199354 	return (0);
24555440Sjm199354 }
24566007Sthurlow 
24576007Sthurlow /*
24586007Sthurlow  * secpolicy_smbfs_login
24596007Sthurlow  *
24606007Sthurlow  * Determines if the caller can add and delete the smbfs login
24616007Sthurlow  * password in the the nsmb kernel module for the CIFS client.
24626007Sthurlow  *
24636007Sthurlow  * Returns:
24646007Sthurlow  * 0       access is allowed.
24656007Sthurlow  * EPERM   access is NOT allowed.
24666007Sthurlow  */
24676007Sthurlow int
24686007Sthurlow secpolicy_smbfs_login(const cred_t *cr, uid_t uid)
24696007Sthurlow {
24706007Sthurlow 	uid_t cruid = crgetruid(cr);
24716007Sthurlow 
24726007Sthurlow 	if (cruid == uid)
24736007Sthurlow 		return (0);
24746007Sthurlow 	return (PRIV_POLICY(cr, PRIV_PROC_OWNER, B_FALSE,
24756007Sthurlow 	    EPERM, NULL));
24766007Sthurlow }
24776784Sjohnlev 
24786784Sjohnlev /*
24796784Sjohnlev  * secpolicy_xvm_control
24806784Sjohnlev  *
24816784Sjohnlev  * Determines if a caller can control the xVM hypervisor and/or running
24826784Sjohnlev  * domains (x86 specific).
24836784Sjohnlev  *
24846784Sjohnlev  * Returns:
24856784Sjohnlev  * 0       access is allowed.
24866784Sjohnlev  * EPERM   access is NOT allowed.
24876784Sjohnlev  */
24886784Sjohnlev int
24896784Sjohnlev secpolicy_xvm_control(const cred_t *cr)
24906784Sjohnlev {
24916784Sjohnlev 	if (PRIV_POLICY(cr, PRIV_XVM_CONTROL, B_FALSE, EPERM, NULL))
24926784Sjohnlev 		return (EPERM);
24936784Sjohnlev 	return (0);
24946784Sjohnlev }
24958275SEric Cheng 
24968275SEric Cheng /*
24979751Sjames.d.carlson@sun.com  * secpolicy_ppp_config
24989751Sjames.d.carlson@sun.com  *
24999751Sjames.d.carlson@sun.com  * Determine if the subject has sufficient privileges to configure PPP and
25009751Sjames.d.carlson@sun.com  * PPP-related devices.
25019751Sjames.d.carlson@sun.com  */
25029751Sjames.d.carlson@sun.com int
25039751Sjames.d.carlson@sun.com secpolicy_ppp_config(const cred_t *cr)
25049751Sjames.d.carlson@sun.com {
25059751Sjames.d.carlson@sun.com 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
25069751Sjames.d.carlson@sun.com 		return (secpolicy_net_config(cr, B_FALSE));
25079751Sjames.d.carlson@sun.com 	return (PRIV_POLICY(cr, PRIV_SYS_PPP_CONFIG, B_FALSE, EPERM, NULL));
25089751Sjames.d.carlson@sun.com }
2509