xref: /onnv-gate/usr/src/uts/common/os/policy.c (revision 6134:27ee74117a16)
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 /*
225771Sjp151216  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
230Sstevel@tonic-gate  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/sysmacros.h>
300Sstevel@tonic-gate #include <sys/param.h>
310Sstevel@tonic-gate #include <sys/systm.h>
320Sstevel@tonic-gate #include <sys/cred_impl.h>
330Sstevel@tonic-gate #include <sys/vnode.h>
340Sstevel@tonic-gate #include <sys/vfs.h>
350Sstevel@tonic-gate #include <sys/stat.h>
360Sstevel@tonic-gate #include <sys/errno.h>
370Sstevel@tonic-gate #include <sys/kmem.h>
380Sstevel@tonic-gate #include <sys/user.h>
390Sstevel@tonic-gate #include <sys/proc.h>
400Sstevel@tonic-gate #include <sys/acct.h>
410Sstevel@tonic-gate #include <sys/ipc_impl.h>
420Sstevel@tonic-gate #include <sys/cmn_err.h>
430Sstevel@tonic-gate #include <sys/debug.h>
440Sstevel@tonic-gate #include <sys/policy.h>
450Sstevel@tonic-gate #include <sys/kobj.h>
460Sstevel@tonic-gate #include <sys/msg.h>
470Sstevel@tonic-gate #include <sys/devpolicy.h>
480Sstevel@tonic-gate #include <c2/audit.h>
490Sstevel@tonic-gate #include <sys/varargs.h>
50*6134Scasper #include <sys/klpd.h>
510Sstevel@tonic-gate #include <sys/modctl.h>
520Sstevel@tonic-gate #include <sys/disp.h>
530Sstevel@tonic-gate #include <sys/zone.h>
540Sstevel@tonic-gate #include <inet/optcom.h>
550Sstevel@tonic-gate #include <sys/sdt.h>
560Sstevel@tonic-gate #include <sys/vfs.h>
570Sstevel@tonic-gate #include <sys/mntent.h>
580Sstevel@tonic-gate #include <sys/contract_impl.h>
590Sstevel@tonic-gate 
600Sstevel@tonic-gate /*
610Sstevel@tonic-gate  * There are two possible layers of privilege routines and two possible
620Sstevel@tonic-gate  * levels of secpolicy.  Plus one other we may not be interested in, so
630Sstevel@tonic-gate  * we may need as many as 6 but no more.
640Sstevel@tonic-gate  */
650Sstevel@tonic-gate #define	MAXPRIVSTACK		6
660Sstevel@tonic-gate 
670Sstevel@tonic-gate int priv_debug = 0;
680Sstevel@tonic-gate 
690Sstevel@tonic-gate /*
700Sstevel@tonic-gate  * This file contains the majority of the policy routines.
710Sstevel@tonic-gate  * Since the policy routines are defined by function and not
720Sstevel@tonic-gate  * by privilege, there is quite a bit of duplication of
730Sstevel@tonic-gate  * functions.
740Sstevel@tonic-gate  *
755331Samw  * The secpolicy functions must not make assumptions about
760Sstevel@tonic-gate  * locks held or not held as any lock can be held while they're
770Sstevel@tonic-gate  * being called.
780Sstevel@tonic-gate  *
790Sstevel@tonic-gate  * Credentials are read-only so no special precautions need to
800Sstevel@tonic-gate  * be taken while locking them.
810Sstevel@tonic-gate  *
820Sstevel@tonic-gate  * When a new policy check needs to be added to the system the
830Sstevel@tonic-gate  * following procedure should be followed:
840Sstevel@tonic-gate  *
850Sstevel@tonic-gate  *		Pick an appropriate secpolicy_*() function
860Sstevel@tonic-gate  *			-> done if one exists.
870Sstevel@tonic-gate  *		Create a new secpolicy function, preferably with
880Sstevel@tonic-gate  *		a descriptive name using the standard template.
890Sstevel@tonic-gate  *		Pick an appropriate privilege for the policy.
900Sstevel@tonic-gate  *		If no appropraite privilege exists, define new one
910Sstevel@tonic-gate  *		(this should be done with extreme care; in most cases
920Sstevel@tonic-gate  *		little is gained by adding another privilege)
930Sstevel@tonic-gate  *
940Sstevel@tonic-gate  * WHY ROOT IS STILL SPECIAL.
950Sstevel@tonic-gate  *
960Sstevel@tonic-gate  * In a number of the policy functions, there are still explicit
970Sstevel@tonic-gate  * checks for uid 0.  The rationale behind these is that many root
980Sstevel@tonic-gate  * owned files/objects hold configuration information which can give full
990Sstevel@tonic-gate  * privileges to the user once written to.  To prevent escalation
1000Sstevel@tonic-gate  * of privilege by allowing just a single privilege to modify root owned
1010Sstevel@tonic-gate  * objects, we've added these root specific checks where we considered
1020Sstevel@tonic-gate  * them necessary: modifying root owned files, changing uids to 0, etc.
1030Sstevel@tonic-gate  *
1040Sstevel@tonic-gate  * PRIVILEGE ESCALATION AND ZONES.
1050Sstevel@tonic-gate  *
1060Sstevel@tonic-gate  * A number of operations potentially allow the caller to achieve
1070Sstevel@tonic-gate  * privileges beyond the ones normally required to perform the operation.
1080Sstevel@tonic-gate  * For example, if allowed to create a setuid 0 executable, a process can
1090Sstevel@tonic-gate  * gain privileges beyond PRIV_FILE_SETID.  Zones, however, place
1100Sstevel@tonic-gate  * restrictions on the ability to gain privileges beyond those available
1110Sstevel@tonic-gate  * within the zone through file and process manipulation.  Hence, such
1120Sstevel@tonic-gate  * operations require that the caller have an effective set that includes
1130Sstevel@tonic-gate  * all privileges available within the current zone, or all privileges
1140Sstevel@tonic-gate  * if executing in the global zone.
1150Sstevel@tonic-gate  *
1160Sstevel@tonic-gate  * This is indicated in the priv_policy* policy checking functions
1170Sstevel@tonic-gate  * through a combination of parameters.  The "priv" parameter indicates
1180Sstevel@tonic-gate  * the privilege that is required, and the "allzone" parameter indicates
1190Sstevel@tonic-gate  * whether or not all privileges in the zone are required.  In addition,
1200Sstevel@tonic-gate  * priv can be set to PRIV_ALL to indicate that all privileges are
1210Sstevel@tonic-gate  * required (regardless of zone).  There are three scenarios of interest:
1220Sstevel@tonic-gate  * (1) operation requires a specific privilege
1230Sstevel@tonic-gate  * (2) operation requires a specific privilege, and requires all
1240Sstevel@tonic-gate  *     privileges available within the zone (or all privileges if in
1250Sstevel@tonic-gate  *     the global zone)
1260Sstevel@tonic-gate  * (3) operation requires all privileges, regardless of zone
1270Sstevel@tonic-gate  *
1280Sstevel@tonic-gate  * For (1), priv should be set to the specific privilege, and allzone
1290Sstevel@tonic-gate  * should be set to B_FALSE.
1300Sstevel@tonic-gate  * For (2), priv should be set to the specific privilege, and allzone
1310Sstevel@tonic-gate  * should be set to B_TRUE.
1320Sstevel@tonic-gate  * For (3), priv should be set to PRIV_ALL, and allzone should be set
1330Sstevel@tonic-gate  * to B_FALSE.
1340Sstevel@tonic-gate  *
1350Sstevel@tonic-gate  */
1360Sstevel@tonic-gate 
1370Sstevel@tonic-gate /*
1380Sstevel@tonic-gate  * The privileges are checked against the Effective set for
1390Sstevel@tonic-gate  * ordinary processes and checked against the Limit set
1400Sstevel@tonic-gate  * for euid 0 processes that haven't manipulated their privilege
1410Sstevel@tonic-gate  * sets.
1420Sstevel@tonic-gate  */
1430Sstevel@tonic-gate #define	HAS_ALLPRIVS(cr)	priv_isfullset(&CR_OEPRIV(cr))
1440Sstevel@tonic-gate #define	ZONEPRIVS(cr)		((cr)->cr_zone->zone_privset)
1450Sstevel@tonic-gate #define	HAS_ALLZONEPRIVS(cr)	priv_issubset(ZONEPRIVS(cr), &CR_OEPRIV(cr))
1460Sstevel@tonic-gate #define	HAS_PRIVILEGE(cr, pr)	((pr) == PRIV_ALL ? \
1470Sstevel@tonic-gate 					HAS_ALLPRIVS(cr) : \
1480Sstevel@tonic-gate 					PRIV_ISASSERT(&CR_OEPRIV(cr), pr))
1490Sstevel@tonic-gate 
1500Sstevel@tonic-gate /*
151*6134Scasper  * Policy checking functions.
1520Sstevel@tonic-gate  *
153*6134Scasper  * All of the system's policy should be implemented here.
1540Sstevel@tonic-gate  */
1550Sstevel@tonic-gate 
1560Sstevel@tonic-gate /*
157*6134Scasper  * Private functions which take an additional va_list argument to
158*6134Scasper  * implement an object specific policy override.
159*6134Scasper  */
160*6134Scasper static int priv_policy_ap(const cred_t *, int, boolean_t, int,
161*6134Scasper     const char *, va_list);
162*6134Scasper static int priv_policy_va(const cred_t *, int, boolean_t, int,
163*6134Scasper     const char *, ...);
164*6134Scasper 
165*6134Scasper /*
1660Sstevel@tonic-gate  * Generic policy calls
1670Sstevel@tonic-gate  *
1680Sstevel@tonic-gate  * The "bottom" functions of policy control
1690Sstevel@tonic-gate  */
1700Sstevel@tonic-gate static char *
1710Sstevel@tonic-gate mprintf(const char *fmt, ...)
1720Sstevel@tonic-gate {
1730Sstevel@tonic-gate 	va_list args;
1740Sstevel@tonic-gate 	char *buf;
1750Sstevel@tonic-gate 	size_t len;
1760Sstevel@tonic-gate 
1770Sstevel@tonic-gate 	va_start(args, fmt);
1780Sstevel@tonic-gate 	len = vsnprintf(NULL, 0, fmt, args) + 1;
1790Sstevel@tonic-gate 	va_end(args);
1800Sstevel@tonic-gate 
1810Sstevel@tonic-gate 	buf = kmem_alloc(len, KM_NOSLEEP);
1820Sstevel@tonic-gate 
1830Sstevel@tonic-gate 	if (buf == NULL)
1840Sstevel@tonic-gate 		return (NULL);
1850Sstevel@tonic-gate 
1860Sstevel@tonic-gate 	va_start(args, fmt);
1870Sstevel@tonic-gate 	(void) vsnprintf(buf, len, fmt, args);
1880Sstevel@tonic-gate 	va_end(args);
1890Sstevel@tonic-gate 
1900Sstevel@tonic-gate 	return (buf);
1910Sstevel@tonic-gate }
1920Sstevel@tonic-gate 
1930Sstevel@tonic-gate /*
1940Sstevel@tonic-gate  * priv_policy_errmsg()
1950Sstevel@tonic-gate  *
1960Sstevel@tonic-gate  * Generate an error message if privilege debugging is enabled system wide
1970Sstevel@tonic-gate  * or for this particular process.
1980Sstevel@tonic-gate  */
1990Sstevel@tonic-gate 
2000Sstevel@tonic-gate #define	FMTHDR	"%s[%d]: missing privilege \"%s\" (euid = %d, syscall = %d)"
2010Sstevel@tonic-gate #define	FMTMSG	" for \"%s\""
2020Sstevel@tonic-gate #define	FMTFUN	" needed at %s+0x%lx"
2030Sstevel@tonic-gate 
2040Sstevel@tonic-gate /* The maximum size privilege format: the concatenation of the above */
2050Sstevel@tonic-gate #define	FMTMAX	FMTHDR FMTMSG FMTFUN "\n"
2060Sstevel@tonic-gate 
2070Sstevel@tonic-gate static void
2080Sstevel@tonic-gate priv_policy_errmsg(const cred_t *cr, int priv, const char *msg)
2090Sstevel@tonic-gate {
2100Sstevel@tonic-gate 	struct proc *me;
2110Sstevel@tonic-gate 	pc_t stack[MAXPRIVSTACK];
2120Sstevel@tonic-gate 	int depth;
2130Sstevel@tonic-gate 	int i;
2140Sstevel@tonic-gate 	char *sym;
2150Sstevel@tonic-gate 	ulong_t off;
2160Sstevel@tonic-gate 	const char *pname;
2170Sstevel@tonic-gate 
2180Sstevel@tonic-gate 	char *cmd;
2190Sstevel@tonic-gate 	char fmt[sizeof (FMTMAX)];
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 	if ((me = curproc) == &p0)
2220Sstevel@tonic-gate 		return;
2230Sstevel@tonic-gate 
2240Sstevel@tonic-gate 	/* Privileges must be defined  */
2250Sstevel@tonic-gate 	ASSERT(priv == PRIV_ALL || priv == PRIV_MULTIPLE ||
2260Sstevel@tonic-gate 	    priv == PRIV_ALLZONE || priv == PRIV_GLOBAL ||
2270Sstevel@tonic-gate 	    priv_getbynum(priv) != NULL);
2280Sstevel@tonic-gate 
2290Sstevel@tonic-gate 	if (priv == PRIV_ALLZONE && INGLOBALZONE(me))
2300Sstevel@tonic-gate 		priv = PRIV_ALL;
2310Sstevel@tonic-gate 
2320Sstevel@tonic-gate 	if (curthread->t_pre_sys)
2330Sstevel@tonic-gate 		ttolwp(curthread)->lwp_badpriv = (short)priv;
2340Sstevel@tonic-gate 
2350Sstevel@tonic-gate 	if (priv_debug == 0 && (CR_FLAGS(cr) & PRIV_DEBUG) == 0)
2360Sstevel@tonic-gate 		return;
2370Sstevel@tonic-gate 
2380Sstevel@tonic-gate 	(void) strcpy(fmt, FMTHDR);
2390Sstevel@tonic-gate 
2400Sstevel@tonic-gate 	if (me->p_user.u_comm[0])
2410Sstevel@tonic-gate 		cmd = &me->p_user.u_comm[0];
2420Sstevel@tonic-gate 	else
2430Sstevel@tonic-gate 		cmd = "priv_policy";
2440Sstevel@tonic-gate 
2450Sstevel@tonic-gate 	if (msg != NULL && *msg != '\0') {
2460Sstevel@tonic-gate 		(void) strcat(fmt, FMTMSG);
2470Sstevel@tonic-gate 	} else {
2480Sstevel@tonic-gate 		(void) strcat(fmt, "%s");
2490Sstevel@tonic-gate 		msg = "";
2500Sstevel@tonic-gate 	}
2510Sstevel@tonic-gate 
2520Sstevel@tonic-gate 	sym = NULL;
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 	depth = getpcstack(stack, MAXPRIVSTACK);
2550Sstevel@tonic-gate 
2560Sstevel@tonic-gate 	/*
2570Sstevel@tonic-gate 	 * Try to find the first interesting function on the stack.
2580Sstevel@tonic-gate 	 * priv_policy* that's us, so completely uninteresting.
2590Sstevel@tonic-gate 	 * suser(), drv_priv(), secpolicy_* are also called from
2600Sstevel@tonic-gate 	 * too many locations to convey useful information.
2610Sstevel@tonic-gate 	 */
2620Sstevel@tonic-gate 	for (i = 0; i < depth; i++) {
2630Sstevel@tonic-gate 		sym = kobj_getsymname((uintptr_t)stack[i], &off);
2640Sstevel@tonic-gate 		if (sym != NULL &&
2650Sstevel@tonic-gate 		    strstr(sym, "hasprocperm") == 0 &&
2660Sstevel@tonic-gate 		    strcmp("suser", sym) != 0 &&
2670Sstevel@tonic-gate 		    strcmp("ipcaccess", sym) != 0 &&
2680Sstevel@tonic-gate 		    strcmp("drv_priv", sym) != 0 &&
2690Sstevel@tonic-gate 		    strncmp("secpolicy_", sym, 10) != 0 &&
2700Sstevel@tonic-gate 		    strncmp("priv_policy", sym, 11) != 0)
2710Sstevel@tonic-gate 			break;
2720Sstevel@tonic-gate 	}
2730Sstevel@tonic-gate 
2740Sstevel@tonic-gate 	if (sym != NULL)
2750Sstevel@tonic-gate 		(void) strcat(fmt, FMTFUN);
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	(void) strcat(fmt, "\n");
2780Sstevel@tonic-gate 
2790Sstevel@tonic-gate 	switch (priv) {
2800Sstevel@tonic-gate 	case PRIV_ALL:
2810Sstevel@tonic-gate 		pname = "ALL";
2820Sstevel@tonic-gate 		break;
2830Sstevel@tonic-gate 	case PRIV_MULTIPLE:
2840Sstevel@tonic-gate 		pname = "MULTIPLE";
2850Sstevel@tonic-gate 		break;
2860Sstevel@tonic-gate 	case PRIV_ALLZONE:
2870Sstevel@tonic-gate 		pname = "ZONE";
2880Sstevel@tonic-gate 		break;
2890Sstevel@tonic-gate 	case PRIV_GLOBAL:
2900Sstevel@tonic-gate 		pname = "GLOBAL";
2910Sstevel@tonic-gate 		break;
2920Sstevel@tonic-gate 	default:
2930Sstevel@tonic-gate 		pname = priv_getbynum(priv);
2940Sstevel@tonic-gate 		break;
2950Sstevel@tonic-gate 	}
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	if (CR_FLAGS(cr) & PRIV_DEBUG) {
2980Sstevel@tonic-gate 		/* Remember last message, just like lwp_badpriv. */
2990Sstevel@tonic-gate 		if (curthread->t_pdmsg != NULL) {
3000Sstevel@tonic-gate 			kmem_free(curthread->t_pdmsg,
3010Sstevel@tonic-gate 			    strlen(curthread->t_pdmsg) + 1);
3020Sstevel@tonic-gate 		}
3030Sstevel@tonic-gate 
3040Sstevel@tonic-gate 		curthread->t_pdmsg = mprintf(fmt, cmd, me->p_pid, pname,
3054543Smarks 		    cr->cr_uid, curthread->t_sysnum, msg, sym, off);
3060Sstevel@tonic-gate 
3070Sstevel@tonic-gate 		curthread->t_post_sys = 1;
308*6134Scasper 	}
309*6134Scasper 	if (priv_debug) {
3100Sstevel@tonic-gate 		cmn_err(CE_NOTE, fmt, cmd, me->p_pid, pname, cr->cr_uid,
3110Sstevel@tonic-gate 		    curthread->t_sysnum, msg, sym, off);
3120Sstevel@tonic-gate 	}
3130Sstevel@tonic-gate }
3140Sstevel@tonic-gate 
3150Sstevel@tonic-gate /*
316*6134Scasper  * Override the policy, if appropriate.  Return 0 if the external
317*6134Scasper  * policy engine approves.
318*6134Scasper  */
319*6134Scasper static int
320*6134Scasper priv_policy_override(const cred_t *cr, int priv, boolean_t allzone, va_list ap)
321*6134Scasper {
322*6134Scasper 	priv_set_t set;
323*6134Scasper 	int ret;
324*6134Scasper 
325*6134Scasper 	if (!(CR_FLAGS(cr) & PRIV_XPOLICY))
326*6134Scasper 		return (-1);
327*6134Scasper 
328*6134Scasper 	if (priv == PRIV_ALL) {
329*6134Scasper 		priv_fillset(&set);
330*6134Scasper 	} else if (allzone) {
331*6134Scasper 		set = *ZONEPRIVS(cr);
332*6134Scasper 	} else {
333*6134Scasper 		priv_emptyset(&set);
334*6134Scasper 		priv_addset(&set, priv);
335*6134Scasper 	}
336*6134Scasper 	ret = klpd_call(cr, &set, ap);
337*6134Scasper 	return (ret);
338*6134Scasper }
339*6134Scasper 
340*6134Scasper static int
341*6134Scasper priv_policy_override_set(const cred_t *cr, const priv_set_t *req, ...)
342*6134Scasper {
343*6134Scasper 	va_list ap;
344*6134Scasper 
345*6134Scasper 	if (CR_FLAGS(cr) & PRIV_XPOLICY) {
346*6134Scasper 		va_start(ap, req);
347*6134Scasper 		return (klpd_call(cr, req, ap));
348*6134Scasper 	}
349*6134Scasper 	return (-1);
350*6134Scasper }
351*6134Scasper 
352*6134Scasper /*
3530Sstevel@tonic-gate  * Audit failure, log error message.
3540Sstevel@tonic-gate  */
3550Sstevel@tonic-gate static void
3560Sstevel@tonic-gate priv_policy_err(const cred_t *cr, int priv, boolean_t allzone, const char *msg)
3570Sstevel@tonic-gate {
3580Sstevel@tonic-gate 
3590Sstevel@tonic-gate 	if (audit_active)
3600Sstevel@tonic-gate 		audit_priv(priv, allzone ? ZONEPRIVS(cr) : NULL, 0);
3610Sstevel@tonic-gate 	DTRACE_PROBE2(priv__err, int, priv, boolean_t, allzone);
3620Sstevel@tonic-gate 
3630Sstevel@tonic-gate 	if (priv_debug || (CR_FLAGS(cr) & PRIV_DEBUG) ||
3640Sstevel@tonic-gate 	    curthread->t_pre_sys) {
3650Sstevel@tonic-gate 		if (allzone && !HAS_ALLZONEPRIVS(cr)) {
3660Sstevel@tonic-gate 			priv_policy_errmsg(cr, PRIV_ALLZONE, msg);
3670Sstevel@tonic-gate 		} else {
3680Sstevel@tonic-gate 			ASSERT(!HAS_PRIVILEGE(cr, priv));
3690Sstevel@tonic-gate 			priv_policy_errmsg(cr, priv, msg);
3700Sstevel@tonic-gate 		}
3710Sstevel@tonic-gate 	}
3720Sstevel@tonic-gate }
3730Sstevel@tonic-gate 
3740Sstevel@tonic-gate /*
375*6134Scasper  * priv_policy_ap()
3760Sstevel@tonic-gate  * return 0 or error.
3770Sstevel@tonic-gate  * See block comment above for a description of "priv" and "allzone" usage.
3780Sstevel@tonic-gate  */
379*6134Scasper static int
380*6134Scasper priv_policy_ap(const cred_t *cr, int priv, boolean_t allzone, int err,
381*6134Scasper     const char *msg, va_list ap)
3820Sstevel@tonic-gate {
383*6134Scasper 	if ((HAS_PRIVILEGE(cr, priv) && (!allzone || HAS_ALLZONEPRIVS(cr))) ||
384*6134Scasper 	    (!servicing_interrupt() &&
385*6134Scasper 	    priv_policy_override(cr, priv, allzone, ap) == 0)) {
3860Sstevel@tonic-gate 		if ((allzone || priv == PRIV_ALL ||
3870Sstevel@tonic-gate 		    !PRIV_ISASSERT(priv_basic, priv)) &&
3880Sstevel@tonic-gate 		    !servicing_interrupt()) {
3893446Smrj 			PTOU(curproc)->u_acflag |= ASU; /* Needed for SVVS */
3900Sstevel@tonic-gate 			if (audit_active)
3910Sstevel@tonic-gate 				audit_priv(priv,
3920Sstevel@tonic-gate 				    allzone ? ZONEPRIVS(cr) : NULL, 1);
3930Sstevel@tonic-gate 		}
3940Sstevel@tonic-gate 		err = 0;
3950Sstevel@tonic-gate 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, allzone);
3960Sstevel@tonic-gate 	} else if (!servicing_interrupt()) {
3970Sstevel@tonic-gate 		/* Failure audited in this procedure */
3980Sstevel@tonic-gate 		priv_policy_err(cr, priv, allzone, msg);
3990Sstevel@tonic-gate 	}
400*6134Scasper 	return (err);
401*6134Scasper }
4020Sstevel@tonic-gate 
403*6134Scasper int
404*6134Scasper priv_policy_va(const cred_t *cr, int priv, boolean_t allzone, int err,
405*6134Scasper     const char *msg, ...)
406*6134Scasper {
407*6134Scasper 	int ret;
408*6134Scasper 	va_list ap;
409*6134Scasper 
410*6134Scasper 	va_start(ap, msg);
411*6134Scasper 	ret = priv_policy_ap(cr, priv, allzone, err, msg, ap);
412*6134Scasper 	va_end(ap);
413*6134Scasper 
414*6134Scasper 	return (ret);
415*6134Scasper }
416*6134Scasper 
417*6134Scasper int
418*6134Scasper priv_policy(const cred_t *cr, int priv, boolean_t allzone, int err,
419*6134Scasper     const char *msg)
420*6134Scasper {
421*6134Scasper 	return (priv_policy_va(cr, priv, allzone, err, msg, KLPDARG_NOMORE));
4220Sstevel@tonic-gate }
4230Sstevel@tonic-gate 
4240Sstevel@tonic-gate /*
4250Sstevel@tonic-gate  * Return B_TRUE for sufficient privileges, B_FALSE for insufficient privileges.
4260Sstevel@tonic-gate  */
4270Sstevel@tonic-gate boolean_t
4280Sstevel@tonic-gate priv_policy_choice(const cred_t *cr, int priv, boolean_t allzone)
4290Sstevel@tonic-gate {
4300Sstevel@tonic-gate 	boolean_t res = HAS_PRIVILEGE(cr, priv) &&
4310Sstevel@tonic-gate 	    (!allzone || HAS_ALLZONEPRIVS(cr));
4320Sstevel@tonic-gate 
4330Sstevel@tonic-gate 	/* Audit success only */
4340Sstevel@tonic-gate 	if (res && audit_active &&
4350Sstevel@tonic-gate 	    (allzone || priv == PRIV_ALL || !PRIV_ISASSERT(priv_basic, priv)) &&
4360Sstevel@tonic-gate 	    !servicing_interrupt()) {
4370Sstevel@tonic-gate 		audit_priv(priv, allzone ? ZONEPRIVS(cr) : NULL, 1);
4380Sstevel@tonic-gate 	}
4390Sstevel@tonic-gate 	if (res) {
4400Sstevel@tonic-gate 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, allzone);
4410Sstevel@tonic-gate 	} else {
4420Sstevel@tonic-gate 		DTRACE_PROBE2(priv__err, int, priv, boolean_t, allzone);
4430Sstevel@tonic-gate 	}
4440Sstevel@tonic-gate 	return (res);
4450Sstevel@tonic-gate }
4460Sstevel@tonic-gate 
4470Sstevel@tonic-gate /*
4480Sstevel@tonic-gate  * Non-auditing variant of priv_policy_choice().
4490Sstevel@tonic-gate  */
4500Sstevel@tonic-gate boolean_t
4510Sstevel@tonic-gate priv_policy_only(const cred_t *cr, int priv, boolean_t allzone)
4520Sstevel@tonic-gate {
4530Sstevel@tonic-gate 	boolean_t res = HAS_PRIVILEGE(cr, priv) &&
4540Sstevel@tonic-gate 	    (!allzone || HAS_ALLZONEPRIVS(cr));
4550Sstevel@tonic-gate 
4560Sstevel@tonic-gate 	if (res) {
4570Sstevel@tonic-gate 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, allzone);
4580Sstevel@tonic-gate 	} else {
4590Sstevel@tonic-gate 		DTRACE_PROBE2(priv__err, int, priv, boolean_t, allzone);
4600Sstevel@tonic-gate 	}
4610Sstevel@tonic-gate 	return (res);
4620Sstevel@tonic-gate }
4630Sstevel@tonic-gate 
4640Sstevel@tonic-gate /*
4650Sstevel@tonic-gate  * Check whether all privileges in the required set are present.
4660Sstevel@tonic-gate  */
4670Sstevel@tonic-gate static int
4680Sstevel@tonic-gate secpolicy_require_set(const cred_t *cr, const priv_set_t *req, const char *msg)
4690Sstevel@tonic-gate {
4700Sstevel@tonic-gate 	int priv;
4710Sstevel@tonic-gate 	int pfound = -1;
4720Sstevel@tonic-gate 	priv_set_t pset;
4730Sstevel@tonic-gate 
4740Sstevel@tonic-gate 	if (req == PRIV_FULLSET ? HAS_ALLPRIVS(cr) : priv_issubset(req,
4754543Smarks 	    &CR_OEPRIV(cr))) {
4760Sstevel@tonic-gate 		return (0);
4770Sstevel@tonic-gate 	}
4780Sstevel@tonic-gate 
479*6134Scasper 	if (priv_policy_override_set(cr, req, KLPDARG_NOMORE) == 0)
480*6134Scasper 		return (0);
481*6134Scasper 
4820Sstevel@tonic-gate 	if (req == PRIV_FULLSET || priv_isfullset(req)) {
4830Sstevel@tonic-gate 		priv_policy_err(cr, PRIV_ALL, B_FALSE, msg);
4840Sstevel@tonic-gate 		return (EACCES);
4850Sstevel@tonic-gate 	}
4860Sstevel@tonic-gate 
4870Sstevel@tonic-gate 	pset = CR_OEPRIV(cr);		/* present privileges */
4880Sstevel@tonic-gate 	priv_inverse(&pset);		/* all non present privileges */
4890Sstevel@tonic-gate 	priv_intersect(req, &pset);	/* the actual missing privs */
4900Sstevel@tonic-gate 
4910Sstevel@tonic-gate 	if (audit_active)
4920Sstevel@tonic-gate 		audit_priv(PRIV_NONE, &pset, 0);
4930Sstevel@tonic-gate 	/*
4940Sstevel@tonic-gate 	 * Privilege debugging; special case "one privilege in set".
4950Sstevel@tonic-gate 	 */
4960Sstevel@tonic-gate 	if (priv_debug || (CR_FLAGS(cr) & PRIV_DEBUG) || curthread->t_pre_sys) {
4970Sstevel@tonic-gate 		for (priv = 0; priv < nprivs; priv++) {
4980Sstevel@tonic-gate 			if (priv_ismember(&pset, priv)) {
4990Sstevel@tonic-gate 				if (pfound != -1) {
5000Sstevel@tonic-gate 					/* Multiple missing privs */
5010Sstevel@tonic-gate 					priv_policy_errmsg(cr, PRIV_MULTIPLE,
5024543Smarks 					    msg);
5030Sstevel@tonic-gate 					return (EACCES);
5040Sstevel@tonic-gate 				}
5050Sstevel@tonic-gate 				pfound = priv;
5060Sstevel@tonic-gate 			}
5070Sstevel@tonic-gate 		}
5080Sstevel@tonic-gate 		ASSERT(pfound != -1);
5090Sstevel@tonic-gate 		/* Just the one missing privilege */
5100Sstevel@tonic-gate 		priv_policy_errmsg(cr, pfound, msg);
5110Sstevel@tonic-gate 	}
5120Sstevel@tonic-gate 
5130Sstevel@tonic-gate 	return (EACCES);
5140Sstevel@tonic-gate }
5150Sstevel@tonic-gate 
5160Sstevel@tonic-gate /*
5170Sstevel@tonic-gate  * Called when an operation requires that the caller be in the
5180Sstevel@tonic-gate  * global zone, regardless of privilege.
5190Sstevel@tonic-gate  */
5200Sstevel@tonic-gate static int
5210Sstevel@tonic-gate priv_policy_global(const cred_t *cr)
5220Sstevel@tonic-gate {
5230Sstevel@tonic-gate 	if (crgetzoneid(cr) == GLOBAL_ZONEID)
5240Sstevel@tonic-gate 		return (0);	/* success */
5250Sstevel@tonic-gate 
5260Sstevel@tonic-gate 	if (priv_debug || (CR_FLAGS(cr) & PRIV_DEBUG) ||
5270Sstevel@tonic-gate 	    curthread->t_pre_sys) {
5280Sstevel@tonic-gate 		priv_policy_errmsg(cr, PRIV_GLOBAL, NULL);
5290Sstevel@tonic-gate 	}
5300Sstevel@tonic-gate 	return (EPERM);
5310Sstevel@tonic-gate }
5320Sstevel@tonic-gate 
5330Sstevel@tonic-gate /*
5340Sstevel@tonic-gate  * Changing process priority
5350Sstevel@tonic-gate  */
5360Sstevel@tonic-gate int
5370Sstevel@tonic-gate secpolicy_setpriority(const cred_t *cr)
5380Sstevel@tonic-gate {
5390Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_PRIOCNTL, B_FALSE, EPERM, NULL));
5400Sstevel@tonic-gate }
5410Sstevel@tonic-gate 
5420Sstevel@tonic-gate /*
5430Sstevel@tonic-gate  * Binding to a privileged port, port must be specified in host byte
5440Sstevel@tonic-gate  * order.
5450Sstevel@tonic-gate  */
5460Sstevel@tonic-gate int
547*6134Scasper secpolicy_net_privaddr(const cred_t *cr, in_port_t port, int proto)
5480Sstevel@tonic-gate {
5495331Samw 	char *reason;
5505331Samw 	int priv;
5515331Samw 
5525331Samw 	switch (port) {
5535331Samw 	case 137:
5545331Samw 	case 138:
5555331Samw 	case 139:
5565331Samw 	case 445:
5575331Samw 		/*
5585331Samw 		 * NBT and SMB ports, these are extra privileged ports,
5595331Samw 		 * allow bind only if the SYS_SMB privilege is present.
5605331Samw 		 */
5615331Samw 		priv = PRIV_SYS_SMB;
5625331Samw 		reason = "NBT or SMB port";
5635331Samw 		break;
5645331Samw 
5655331Samw 	case 2049:
5665331Samw 	case 4045:
5675331Samw 		/*
5685331Samw 		 * NFS ports, these are extra privileged ports, allow bind
5695331Samw 		 * only if the SYS_NFS privilege is present.
5705331Samw 		 */
5715331Samw 		priv = PRIV_SYS_NFS;
5725331Samw 		reason = "NFS port";
5735331Samw 		break;
5745331Samw 
5755331Samw 	default:
5765331Samw 		priv = PRIV_NET_PRIVADDR;
5775331Samw 		reason = NULL;
5785331Samw 		break;
5795331Samw 
5805331Samw 	}
5815331Samw 
582*6134Scasper 	return (priv_policy_va(cr, priv, B_FALSE, EACCES, reason,
583*6134Scasper 	    KLPDARG_PORT, (int)proto, (int)port, KLPDARG_NOMORE));
5840Sstevel@tonic-gate }
5850Sstevel@tonic-gate 
5860Sstevel@tonic-gate /*
5871676Sjpk  * Binding to a multilevel port on a trusted (labeled) system.
5881676Sjpk  */
5891676Sjpk int
5901676Sjpk secpolicy_net_bindmlp(const cred_t *cr)
5911676Sjpk {
592*6134Scasper 	return (PRIV_POLICY(cr, PRIV_NET_BINDMLP, B_FALSE, EACCES, NULL));
5931676Sjpk }
5941676Sjpk 
5951676Sjpk /*
5961676Sjpk  * Allow a communication between a zone and an unlabeled host when their
5971676Sjpk  * labels don't match.
5981676Sjpk  */
5991676Sjpk int
6001676Sjpk secpolicy_net_mac_aware(const cred_t *cr)
6011676Sjpk {
602*6134Scasper 	return (PRIV_POLICY(cr, PRIV_NET_MAC_AWARE, B_FALSE, EACCES, NULL));
6031676Sjpk }
6041676Sjpk 
6051676Sjpk /*
6060Sstevel@tonic-gate  * Common routine which determines whether a given credential can
6070Sstevel@tonic-gate  * act on a given mount.
6080Sstevel@tonic-gate  * When called through mount, the parameter needoptcheck is a pointer
6090Sstevel@tonic-gate  * to a boolean variable which will be set to either true or false,
6100Sstevel@tonic-gate  * depending on whether the mount policy should change the mount options.
6110Sstevel@tonic-gate  * In all other cases, needoptcheck should be a NULL pointer.
6120Sstevel@tonic-gate  */
6130Sstevel@tonic-gate static int
6140Sstevel@tonic-gate secpolicy_fs_common(cred_t *cr, vnode_t *mvp, const vfs_t *vfsp,
6150Sstevel@tonic-gate     boolean_t *needoptcheck)
6160Sstevel@tonic-gate {
6170Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
6180Sstevel@tonic-gate 	boolean_t mounting = needoptcheck != NULL;
6190Sstevel@tonic-gate 
6200Sstevel@tonic-gate 	/*
6210Sstevel@tonic-gate 	 * Short circuit the following cases:
6220Sstevel@tonic-gate 	 *	vfsp == NULL or mvp == NULL (pure privilege check)
6230Sstevel@tonic-gate 	 *	have all privileges - no further checks required
6240Sstevel@tonic-gate 	 *	and no mount options need to be set.
6250Sstevel@tonic-gate 	 */
6260Sstevel@tonic-gate 	if (vfsp == NULL || mvp == NULL || HAS_ALLPRIVS(cr)) {
6270Sstevel@tonic-gate 		if (mounting)
6280Sstevel@tonic-gate 			*needoptcheck = B_FALSE;
6290Sstevel@tonic-gate 
630*6134Scasper 		return (priv_policy_va(cr, PRIV_SYS_MOUNT, allzone, EPERM,
631*6134Scasper 		    NULL, KLPDARG_VNODE, mvp, (char *)NULL, KLPDARG_NOMORE));
6320Sstevel@tonic-gate 	}
6330Sstevel@tonic-gate 
6340Sstevel@tonic-gate 	/*
6350Sstevel@tonic-gate 	 * When operating on an existing mount (either we're not mounting
6360Sstevel@tonic-gate 	 * or we're doing a remount and VFS_REMOUNT will be set), zones
6370Sstevel@tonic-gate 	 * can operate only on mounts established by the zone itself.
6380Sstevel@tonic-gate 	 */
6390Sstevel@tonic-gate 	if (!mounting || (vfsp->vfs_flag & VFS_REMOUNT) != 0) {
6400Sstevel@tonic-gate 		zoneid_t zoneid = crgetzoneid(cr);
6410Sstevel@tonic-gate 
6420Sstevel@tonic-gate 		if (zoneid != GLOBAL_ZONEID &&
6430Sstevel@tonic-gate 		    vfsp->vfs_zone->zone_id != zoneid) {
6440Sstevel@tonic-gate 			return (EPERM);
6450Sstevel@tonic-gate 		}
6460Sstevel@tonic-gate 	}
6470Sstevel@tonic-gate 
6480Sstevel@tonic-gate 	if (mounting)
6490Sstevel@tonic-gate 		*needoptcheck = B_TRUE;
6500Sstevel@tonic-gate 
6510Sstevel@tonic-gate 	/*
6520Sstevel@tonic-gate 	 * Overlay mounts may hide important stuff; if you can't write to a
6530Sstevel@tonic-gate 	 * mount point but would be able to mount on top of it, you can
6540Sstevel@tonic-gate 	 * escalate your privileges.
6550Sstevel@tonic-gate 	 * So we go about asking the same questions namefs does when it
6560Sstevel@tonic-gate 	 * decides whether you can mount over a file or not but with the
6570Sstevel@tonic-gate 	 * added restriction that you can only mount on top of a regular
6580Sstevel@tonic-gate 	 * file or directory.
6590Sstevel@tonic-gate 	 * If we have all the zone's privileges, we skip all other checks,
6600Sstevel@tonic-gate 	 * or else we may actually get in trouble inside the automounter.
6610Sstevel@tonic-gate 	 */
6620Sstevel@tonic-gate 	if ((mvp->v_flag & VROOT) != 0 ||
6630Sstevel@tonic-gate 	    (mvp->v_type != VDIR && mvp->v_type != VREG) ||
6640Sstevel@tonic-gate 	    HAS_ALLZONEPRIVS(cr)) {
6650Sstevel@tonic-gate 		allzone = B_TRUE;
6660Sstevel@tonic-gate 	} else {
6670Sstevel@tonic-gate 		vattr_t va;
6680Sstevel@tonic-gate 		int err;
6690Sstevel@tonic-gate 
6700Sstevel@tonic-gate 		va.va_mask = AT_UID|AT_MODE;
6715331Samw 		err = VOP_GETATTR(mvp, &va, 0, cr, NULL);
6720Sstevel@tonic-gate 		if (err != 0)
6730Sstevel@tonic-gate 			return (err);
6740Sstevel@tonic-gate 
6750Sstevel@tonic-gate 		if ((err = secpolicy_vnode_owner(cr, va.va_uid)) != 0)
6760Sstevel@tonic-gate 			return (err);
6770Sstevel@tonic-gate 
6780Sstevel@tonic-gate 		if ((va.va_mode & VWRITE) == 0 &&
6790Sstevel@tonic-gate 		    secpolicy_vnode_access(cr, mvp, va.va_uid, VWRITE) != 0) {
6800Sstevel@tonic-gate 			return (EACCES);
6810Sstevel@tonic-gate 		}
6820Sstevel@tonic-gate 	}
683*6134Scasper 	return (priv_policy_va(cr, PRIV_SYS_MOUNT, allzone, EPERM,
684*6134Scasper 	    NULL, KLPDARG_VNODE, mvp, (char *)NULL, KLPDARG_NOMORE));
6850Sstevel@tonic-gate }
6860Sstevel@tonic-gate 
6874543Smarks void
6884543Smarks secpolicy_fs_mount_clearopts(cred_t *cr, struct vfs *vfsp)
6894543Smarks {
6904543Smarks 	boolean_t amsuper = HAS_ALLZONEPRIVS(cr);
6914543Smarks 
6924543Smarks 	/*
6934543Smarks 	 * check; if we don't have either "nosuid" or
6944543Smarks 	 * both "nosetuid" and "nodevices", then we add
6954543Smarks 	 * "nosuid"; this depends on how the current
6964543Smarks 	 * implementation works (it first checks nosuid).  In a
6974543Smarks 	 * zone, a user with all zone privileges can mount with
6984543Smarks 	 * "setuid" but never with "devices".
6994543Smarks 	 */
7004543Smarks 	if (!vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL) &&
7014543Smarks 	    (!vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL) ||
7024543Smarks 	    !vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))) {
7034543Smarks 		if (crgetzoneid(cr) == GLOBAL_ZONEID || !amsuper)
7044543Smarks 			vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
7054543Smarks 		else
7064543Smarks 			vfs_setmntopt(vfsp, MNTOPT_NODEVICES, NULL, 0);
7074543Smarks 	}
7084543Smarks 	/*
7094543Smarks 	 * If we're not the local super user, we set the "restrict"
7104543Smarks 	 * option to indicate to automountd that this mount should
7114543Smarks 	 * be handled with care.
7124543Smarks 	 */
7134543Smarks 	if (!amsuper)
7144543Smarks 		vfs_setmntopt(vfsp, MNTOPT_RESTRICT, NULL, 0);
7154543Smarks 
7164543Smarks }
7174543Smarks 
718148Scasper extern vnode_t *rootvp;
719148Scasper extern vfs_t *rootvfs;
720148Scasper 
7210Sstevel@tonic-gate int
7220Sstevel@tonic-gate secpolicy_fs_mount(cred_t *cr, vnode_t *mvp, struct vfs *vfsp)
7230Sstevel@tonic-gate {
7240Sstevel@tonic-gate 	boolean_t needoptchk;
7250Sstevel@tonic-gate 	int error;
7260Sstevel@tonic-gate 
727148Scasper 	/*
728148Scasper 	 * If it's a remount, get the underlying mount point,
729148Scasper 	 * except for the root where we use the rootvp.
730148Scasper 	 */
731148Scasper 	if ((vfsp->vfs_flag & VFS_REMOUNT) != 0) {
732148Scasper 		if (vfsp == rootvfs)
733148Scasper 			mvp = rootvp;
734148Scasper 		else
735148Scasper 			mvp = vfsp->vfs_vnodecovered;
736148Scasper 	}
737148Scasper 
7380Sstevel@tonic-gate 	error = secpolicy_fs_common(cr, mvp, vfsp, &needoptchk);
7390Sstevel@tonic-gate 
7400Sstevel@tonic-gate 	if (error == 0 && needoptchk) {
7414543Smarks 		secpolicy_fs_mount_clearopts(cr, vfsp);
7424543Smarks 	}
7430Sstevel@tonic-gate 
7440Sstevel@tonic-gate 	return (error);
7450Sstevel@tonic-gate }
7460Sstevel@tonic-gate 
7470Sstevel@tonic-gate /*
7480Sstevel@tonic-gate  * Does the policy computations for "ownership" of a mount;
7490Sstevel@tonic-gate  * here ownership is defined as the ability to "mount"
7500Sstevel@tonic-gate  * the filesystem originally.  The rootvfs doesn't cover any
7510Sstevel@tonic-gate  * vnodes; we attribute its ownership to the rootvp.
7520Sstevel@tonic-gate  */
7530Sstevel@tonic-gate static int
7540Sstevel@tonic-gate secpolicy_fs_owner(cred_t *cr, const struct vfs *vfsp)
7550Sstevel@tonic-gate {
7560Sstevel@tonic-gate 	vnode_t *mvp;
7570Sstevel@tonic-gate 
7580Sstevel@tonic-gate 	if (vfsp == NULL)
7590Sstevel@tonic-gate 		mvp = NULL;
7600Sstevel@tonic-gate 	else if (vfsp == rootvfs)
7610Sstevel@tonic-gate 		mvp = rootvp;
7620Sstevel@tonic-gate 	else
7630Sstevel@tonic-gate 		mvp = vfsp->vfs_vnodecovered;
7640Sstevel@tonic-gate 
7650Sstevel@tonic-gate 	return (secpolicy_fs_common(cr, mvp, vfsp, NULL));
7660Sstevel@tonic-gate }
7670Sstevel@tonic-gate 
7680Sstevel@tonic-gate int
7690Sstevel@tonic-gate secpolicy_fs_unmount(cred_t *cr, struct vfs *vfsp)
7700Sstevel@tonic-gate {
7710Sstevel@tonic-gate 	return (secpolicy_fs_owner(cr, vfsp));
7720Sstevel@tonic-gate }
7730Sstevel@tonic-gate 
7740Sstevel@tonic-gate /*
7750Sstevel@tonic-gate  * Quotas are a resource, but if one has the ability to mount a filesystem, he
7760Sstevel@tonic-gate  * should be able to modify quotas on it.
7770Sstevel@tonic-gate  */
7780Sstevel@tonic-gate int
7790Sstevel@tonic-gate secpolicy_fs_quota(const cred_t *cr, const vfs_t *vfsp)
7800Sstevel@tonic-gate {
7810Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
7820Sstevel@tonic-gate }
7830Sstevel@tonic-gate 
7840Sstevel@tonic-gate /*
7850Sstevel@tonic-gate  * Exceeding minfree: also a per-mount resource constraint.
7860Sstevel@tonic-gate  */
7870Sstevel@tonic-gate int
7880Sstevel@tonic-gate secpolicy_fs_minfree(const cred_t *cr, const vfs_t *vfsp)
7890Sstevel@tonic-gate {
7900Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
7910Sstevel@tonic-gate }
7920Sstevel@tonic-gate 
7930Sstevel@tonic-gate int
7940Sstevel@tonic-gate secpolicy_fs_config(const cred_t *cr, const vfs_t *vfsp)
7950Sstevel@tonic-gate {
7960Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
7970Sstevel@tonic-gate }
7980Sstevel@tonic-gate 
7990Sstevel@tonic-gate /* ARGSUSED */
8000Sstevel@tonic-gate int
8010Sstevel@tonic-gate secpolicy_fs_linkdir(const cred_t *cr, const vfs_t *vfsp)
8020Sstevel@tonic-gate {
8030Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_LINKDIR, B_FALSE, EPERM, NULL));
8040Sstevel@tonic-gate }
8050Sstevel@tonic-gate 
8060Sstevel@tonic-gate /*
8070Sstevel@tonic-gate  * Name:        secpolicy_vnode_access()
8080Sstevel@tonic-gate  *
8090Sstevel@tonic-gate  * Parameters:  Process credential
8100Sstevel@tonic-gate  *		vnode
8110Sstevel@tonic-gate  *		uid of owner of vnode
8120Sstevel@tonic-gate  *		permission bits not granted to the caller when examining
8130Sstevel@tonic-gate  *		file mode bits (i.e., when a process wants to open a
8140Sstevel@tonic-gate  *		mode 444 file for VREAD|VWRITE, this function should be
8150Sstevel@tonic-gate  *		called only with a VWRITE argument).
8160Sstevel@tonic-gate  *
8170Sstevel@tonic-gate  * Normal:      Verifies that cred has the appropriate privileges to
8180Sstevel@tonic-gate  *              override the mode bits that were denied.
8190Sstevel@tonic-gate  *
8200Sstevel@tonic-gate  * Override:    file_dac_execute - if VEXEC bit was denied and vnode is
8210Sstevel@tonic-gate  *                      not a directory.
8220Sstevel@tonic-gate  *              file_dac_read - if VREAD bit was denied.
8230Sstevel@tonic-gate  *              file_dac_search - if VEXEC bit was denied and vnode is
8240Sstevel@tonic-gate  *                      a directory.
8250Sstevel@tonic-gate  *              file_dac_write - if VWRITE bit was denied.
8260Sstevel@tonic-gate  *
8270Sstevel@tonic-gate  *		Root owned files are special cased to protect system
8280Sstevel@tonic-gate  *		configuration files and such.
8290Sstevel@tonic-gate  *
8300Sstevel@tonic-gate  * Output:      EACCES - if privilege check fails.
8310Sstevel@tonic-gate  */
8320Sstevel@tonic-gate 
8330Sstevel@tonic-gate /* ARGSUSED */
8340Sstevel@tonic-gate int
8350Sstevel@tonic-gate secpolicy_vnode_access(const cred_t *cr, vnode_t *vp, uid_t owner, mode_t mode)
8360Sstevel@tonic-gate {
837*6134Scasper 	if ((mode & VREAD) && priv_policy_va(cr, PRIV_FILE_DAC_READ, B_FALSE,
838*6134Scasper 	    EACCES, NULL, KLPDARG_VNODE, vp, (char *)NULL,
839*6134Scasper 	    KLPDARG_NOMORE) != 0) {
8400Sstevel@tonic-gate 		return (EACCES);
841*6134Scasper 	}
8420Sstevel@tonic-gate 
8430Sstevel@tonic-gate 	if (mode & VWRITE) {
8440Sstevel@tonic-gate 		boolean_t allzone;
8450Sstevel@tonic-gate 
8460Sstevel@tonic-gate 		if (owner == 0 && cr->cr_uid != 0)
8470Sstevel@tonic-gate 			allzone = B_TRUE;
8480Sstevel@tonic-gate 		else
8490Sstevel@tonic-gate 			allzone = B_FALSE;
850*6134Scasper 		if (priv_policy_va(cr, PRIV_FILE_DAC_WRITE, allzone, EACCES,
851*6134Scasper 		    NULL, KLPDARG_VNODE, vp, (char *)NULL,
852*6134Scasper 		    KLPDARG_NOMORE) != 0) {
8530Sstevel@tonic-gate 			return (EACCES);
854*6134Scasper 		}
8550Sstevel@tonic-gate 	}
8560Sstevel@tonic-gate 
8570Sstevel@tonic-gate 	if (mode & VEXEC) {
8580Sstevel@tonic-gate 		/*
8590Sstevel@tonic-gate 		 * Directories use file_dac_search to override the execute bit.
8600Sstevel@tonic-gate 		 */
861*6134Scasper 		int p = vp->v_type == VDIR ? PRIV_FILE_DAC_SEARCH :
862*6134Scasper 		    PRIV_FILE_DAC_EXECUTE;
8630Sstevel@tonic-gate 
864*6134Scasper 		return (priv_policy_va(cr, p, B_FALSE, EACCES, NULL,
865*6134Scasper 		    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
8660Sstevel@tonic-gate 	}
8670Sstevel@tonic-gate 	return (0);
8680Sstevel@tonic-gate }
8690Sstevel@tonic-gate 
8700Sstevel@tonic-gate /*
8710Sstevel@tonic-gate  * Name:	secpolicy_vnode_setid_modify()
8720Sstevel@tonic-gate  *
8730Sstevel@tonic-gate  * Normal:	verify that subject can set the file setid flags.
8740Sstevel@tonic-gate  *
8750Sstevel@tonic-gate  * Output:	EPERM - if not privileged.
8760Sstevel@tonic-gate  */
8770Sstevel@tonic-gate 
8780Sstevel@tonic-gate static int
8790Sstevel@tonic-gate secpolicy_vnode_setid_modify(const cred_t *cr, uid_t owner)
8800Sstevel@tonic-gate {
8810Sstevel@tonic-gate 	/* If changing to suid root, must have all zone privs */
8820Sstevel@tonic-gate 	boolean_t allzone = B_TRUE;
8830Sstevel@tonic-gate 
8840Sstevel@tonic-gate 	if (owner != 0) {
8850Sstevel@tonic-gate 		if (owner == cr->cr_uid)
8860Sstevel@tonic-gate 			return (0);
8870Sstevel@tonic-gate 		allzone = B_FALSE;
8880Sstevel@tonic-gate 	}
8890Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_SETID, allzone, EPERM, NULL));
8900Sstevel@tonic-gate }
8910Sstevel@tonic-gate 
8920Sstevel@tonic-gate /*
8930Sstevel@tonic-gate  * Are we allowed to retain the set-uid/set-gid bits when
8940Sstevel@tonic-gate  * changing ownership or when writing to a file?
8950Sstevel@tonic-gate  * "issuid" should be true when set-uid; only in that case
8960Sstevel@tonic-gate  * root ownership is checked (setgid is assumed).
8970Sstevel@tonic-gate  */
8980Sstevel@tonic-gate int
8990Sstevel@tonic-gate secpolicy_vnode_setid_retain(const cred_t *cred, boolean_t issuidroot)
9000Sstevel@tonic-gate {
9010Sstevel@tonic-gate 	if (issuidroot && !HAS_ALLZONEPRIVS(cred))
9020Sstevel@tonic-gate 		return (EPERM);
9030Sstevel@tonic-gate 
9040Sstevel@tonic-gate 	return (!PRIV_POLICY_CHOICE(cred, PRIV_FILE_SETID, B_FALSE));
9050Sstevel@tonic-gate }
9060Sstevel@tonic-gate 
9070Sstevel@tonic-gate /*
9080Sstevel@tonic-gate  * Name:	secpolicy_vnode_setids_setgids()
9090Sstevel@tonic-gate  *
9100Sstevel@tonic-gate  * Normal:	verify that subject can set the file setgid flag.
9110Sstevel@tonic-gate  *
9120Sstevel@tonic-gate  * Output:	EPERM - if not privileged
9130Sstevel@tonic-gate  */
9140Sstevel@tonic-gate 
9150Sstevel@tonic-gate int
9160Sstevel@tonic-gate secpolicy_vnode_setids_setgids(const cred_t *cred, gid_t gid)
9170Sstevel@tonic-gate {
9180Sstevel@tonic-gate 	if (!groupmember(gid, cred))
9190Sstevel@tonic-gate 		return (PRIV_POLICY(cred, PRIV_FILE_SETID, B_FALSE, EPERM,
9200Sstevel@tonic-gate 		    NULL));
9210Sstevel@tonic-gate 	return (0);
9220Sstevel@tonic-gate }
9230Sstevel@tonic-gate 
9240Sstevel@tonic-gate /*
9250Sstevel@tonic-gate  * Create a file with a group different than any of the groups allowed:
9260Sstevel@tonic-gate  * the group of the directory the file is created in, the effective
9270Sstevel@tonic-gate  * group or any of the supplementary groups.
9280Sstevel@tonic-gate  */
9290Sstevel@tonic-gate int
9300Sstevel@tonic-gate secpolicy_vnode_create_gid(const cred_t *cred)
9310Sstevel@tonic-gate {
9320Sstevel@tonic-gate 	if (HAS_PRIVILEGE(cred, PRIV_FILE_CHOWN))
9330Sstevel@tonic-gate 		return (PRIV_POLICY(cred, PRIV_FILE_CHOWN, B_FALSE, EPERM,
9340Sstevel@tonic-gate 		    NULL));
9350Sstevel@tonic-gate 	else
9360Sstevel@tonic-gate 		return (PRIV_POLICY(cred, PRIV_FILE_CHOWN_SELF, B_FALSE, EPERM,
9370Sstevel@tonic-gate 		    NULL));
9380Sstevel@tonic-gate }
9390Sstevel@tonic-gate 
9400Sstevel@tonic-gate /*
9410Sstevel@tonic-gate  * Name:	secpolicy_vnode_utime_modify()
9420Sstevel@tonic-gate  *
9430Sstevel@tonic-gate  * Normal:	verify that subject can modify the utime on a file.
9440Sstevel@tonic-gate  *
9450Sstevel@tonic-gate  * Output:	EPERM - if access denied.
9460Sstevel@tonic-gate  */
9470Sstevel@tonic-gate 
9480Sstevel@tonic-gate static int
9490Sstevel@tonic-gate secpolicy_vnode_utime_modify(const cred_t *cred)
9500Sstevel@tonic-gate {
9510Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_FILE_OWNER, B_FALSE, EPERM,
9520Sstevel@tonic-gate 	    "modify file times"));
9530Sstevel@tonic-gate }
9540Sstevel@tonic-gate 
9550Sstevel@tonic-gate 
9560Sstevel@tonic-gate /*
9570Sstevel@tonic-gate  * Name:	secpolicy_vnode_setdac()
9580Sstevel@tonic-gate  *
9590Sstevel@tonic-gate  * Normal:	verify that subject can modify the mode of a file.
9600Sstevel@tonic-gate  *		allzone privilege needed when modifying root owned object.
9610Sstevel@tonic-gate  *
9620Sstevel@tonic-gate  * Output:	EPERM - if access denied.
9630Sstevel@tonic-gate  */
9640Sstevel@tonic-gate 
9650Sstevel@tonic-gate int
9660Sstevel@tonic-gate secpolicy_vnode_setdac(const cred_t *cred, uid_t owner)
9670Sstevel@tonic-gate {
9680Sstevel@tonic-gate 	if (owner == cred->cr_uid)
9690Sstevel@tonic-gate 		return (0);
9700Sstevel@tonic-gate 
9710Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_FILE_OWNER, owner == 0, EPERM, NULL));
9720Sstevel@tonic-gate }
9730Sstevel@tonic-gate /*
9740Sstevel@tonic-gate  * Name:	secpolicy_vnode_stky_modify()
9750Sstevel@tonic-gate  *
9760Sstevel@tonic-gate  * Normal:	verify that subject can make a file a "sticky".
9770Sstevel@tonic-gate  *
9780Sstevel@tonic-gate  * Output:	EPERM - if access denied.
9790Sstevel@tonic-gate  */
9800Sstevel@tonic-gate 
9810Sstevel@tonic-gate int
9820Sstevel@tonic-gate secpolicy_vnode_stky_modify(const cred_t *cred)
9830Sstevel@tonic-gate {
9840Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_SYS_CONFIG, B_FALSE, EPERM,
9850Sstevel@tonic-gate 	    "set file sticky"));
9860Sstevel@tonic-gate }
9870Sstevel@tonic-gate 
9880Sstevel@tonic-gate /*
9890Sstevel@tonic-gate  * Policy determines whether we can remove an entry from a directory,
9900Sstevel@tonic-gate  * regardless of permission bits.
9910Sstevel@tonic-gate  */
9920Sstevel@tonic-gate int
9930Sstevel@tonic-gate secpolicy_vnode_remove(const cred_t *cr)
9940Sstevel@tonic-gate {
9950Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_OWNER, B_FALSE, EACCES,
9960Sstevel@tonic-gate 	    "sticky directory"));
9970Sstevel@tonic-gate }
9980Sstevel@tonic-gate 
9990Sstevel@tonic-gate int
10000Sstevel@tonic-gate secpolicy_vnode_owner(const cred_t *cr, uid_t owner)
10010Sstevel@tonic-gate {
10020Sstevel@tonic-gate 	boolean_t allzone = (owner == 0);
10030Sstevel@tonic-gate 
10040Sstevel@tonic-gate 	if (owner == cr->cr_uid)
10050Sstevel@tonic-gate 		return (0);
10060Sstevel@tonic-gate 
10070Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_OWNER, allzone, EPERM, NULL));
10080Sstevel@tonic-gate }
10090Sstevel@tonic-gate 
10101115Smarks void
10111115Smarks secpolicy_setid_clear(vattr_t *vap, cred_t *cr)
10121115Smarks {
10131115Smarks 	if ((vap->va_mode & (S_ISUID | S_ISGID)) != 0 &&
10141115Smarks 	    secpolicy_vnode_setid_retain(cr,
10151115Smarks 	    (vap->va_mode & S_ISUID) != 0 &&
10161115Smarks 	    (vap->va_mask & AT_UID) != 0 && vap->va_uid == 0) != 0) {
10171115Smarks 		vap->va_mask |= AT_MODE;
10181115Smarks 		vap->va_mode &= ~(S_ISUID|S_ISGID);
10191115Smarks 	}
10201115Smarks }
10211115Smarks 
10222796Smarks int
10232796Smarks secpolicy_setid_setsticky_clear(vnode_t *vp, vattr_t *vap, const vattr_t *ovap,
10242796Smarks     cred_t *cr)
10252796Smarks {
10262796Smarks 	int error;
10272796Smarks 
10282796Smarks 	if ((vap->va_mode & S_ISUID) != 0 &&
10292796Smarks 	    (error = secpolicy_vnode_setid_modify(cr,
10302796Smarks 	    ovap->va_uid)) != 0) {
10312796Smarks 		return (error);
10322796Smarks 	}
10332796Smarks 
10342796Smarks 	/*
10352796Smarks 	 * Check privilege if attempting to set the
10362796Smarks 	 * sticky bit on a non-directory.
10372796Smarks 	 */
10382796Smarks 	if (vp->v_type != VDIR && (vap->va_mode & S_ISVTX) != 0 &&
10392796Smarks 	    secpolicy_vnode_stky_modify(cr) != 0) {
10404543Smarks 		vap->va_mode &= ~S_ISVTX;
10412796Smarks 	}
10422796Smarks 
10432796Smarks 	/*
10442796Smarks 	 * Check for privilege if attempting to set the
10452796Smarks 	 * group-id bit.
10462796Smarks 	 */
10472796Smarks 	if ((vap->va_mode & S_ISGID) != 0 &&
10482796Smarks 	    secpolicy_vnode_setids_setgids(cr, ovap->va_gid) != 0) {
10494543Smarks 		vap->va_mode &= ~S_ISGID;
10502796Smarks 	}
10512796Smarks 
10522796Smarks 	return (0);
10532796Smarks }
10542796Smarks 
10555331Samw #define	ATTR_FLAG_PRIV(attr, value, cr)	\
10565331Samw 	PRIV_POLICY(cr, value ? PRIV_FILE_FLAG_SET : PRIV_ALL, \
10575331Samw 	B_FALSE, EPERM, NULL)
10585331Samw 
10595331Samw /*
10605331Samw  * Check privileges for setting xvattr attributes
10615331Samw  */
10625331Samw int
10635331Samw secpolicy_xvattr(xvattr_t *xvap, uid_t owner, cred_t *cr, vtype_t vtype)
10645331Samw {
10655331Samw 	xoptattr_t *xoap;
10665331Samw 	int error = 0;
10675331Samw 
10685331Samw 	if ((xoap = xva_getxoptattr(xvap)) == NULL)
10695331Samw 		return (EINVAL);
10705331Samw 
10715331Samw 	/*
10725331Samw 	 * First process the DOS bits
10735331Samw 	 */
10745331Samw 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
10755331Samw 	    XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
10765331Samw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
10775331Samw 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM) ||
10785331Samw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
10795331Samw 		if ((error = secpolicy_vnode_owner(cr, owner)) != 0)
10805331Samw 			return (error);
10815331Samw 	}
10825331Samw 
10835331Samw 	/*
10845331Samw 	 * Now handle special attributes
10855331Samw 	 */
10865331Samw 
10875331Samw 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
10885331Samw 		error = ATTR_FLAG_PRIV(XAT_IMMUTABLE,
10895331Samw 		    xoap->xoa_immutable, cr);
10905331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
10915331Samw 		error = ATTR_FLAG_PRIV(XAT_NOUNLINK,
10925331Samw 		    xoap->xoa_nounlink, cr);
10935331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
10945331Samw 		error = ATTR_FLAG_PRIV(XAT_APPENDONLY,
10955331Samw 		    xoap->xoa_appendonly, cr);
10965331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_NODUMP))
10975331Samw 		error = ATTR_FLAG_PRIV(XAT_NODUMP,
10985331Samw 		    xoap->xoa_nodump, cr);
10995331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_OPAQUE))
11005331Samw 		error = EPERM;
11015331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
11025331Samw 		error = ATTR_FLAG_PRIV(XAT_AV_QUARANTINED,
11035331Samw 		    xoap->xoa_av_quarantined, cr);
11045545Smarks 		if (error == 0 && vtype != VREG && xoap->xoa_av_quarantined)
11055331Samw 			error = EINVAL;
11065331Samw 	}
11075331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
11085331Samw 		error = ATTR_FLAG_PRIV(XAT_AV_MODIFIED,
11095331Samw 		    xoap->xoa_av_modified, cr);
11105331Samw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
11115331Samw 		error = ATTR_FLAG_PRIV(XAT_AV_SCANSTAMP,
11125331Samw 		    xoap->xoa_av_scanstamp, cr);
11135331Samw 		if (error == 0 && vtype != VREG)
11145331Samw 			error = EINVAL;
11155331Samw 	}
11165331Samw 	return (error);
11175331Samw }
11185331Samw 
11190Sstevel@tonic-gate /*
11200Sstevel@tonic-gate  * This function checks the policy decisions surrounding the
11210Sstevel@tonic-gate  * vop setattr call.
11220Sstevel@tonic-gate  *
11230Sstevel@tonic-gate  * It should be called after sufficient locks have been established
11240Sstevel@tonic-gate  * on the underlying data structures.  No concurrent modifications
11250Sstevel@tonic-gate  * should be allowed.
11260Sstevel@tonic-gate  *
11270Sstevel@tonic-gate  * The caller must pass in unlocked version of its vaccess function
11280Sstevel@tonic-gate  * this is required because vop_access function should lock the
11290Sstevel@tonic-gate  * node for reading.  A three argument function should be defined
11300Sstevel@tonic-gate  * which accepts the following argument:
11310Sstevel@tonic-gate  * 	A pointer to the internal "node" type (inode *)
11320Sstevel@tonic-gate  *	vnode access bits (VREAD|VWRITE|VEXEC)
11330Sstevel@tonic-gate  *	a pointer to the credential
11340Sstevel@tonic-gate  *
11350Sstevel@tonic-gate  * This function makes the following policy decisions:
11360Sstevel@tonic-gate  *
11370Sstevel@tonic-gate  *		- change permissions
11380Sstevel@tonic-gate  *			- permission to change file mode if not owner
11390Sstevel@tonic-gate  *			- permission to add sticky bit to non-directory
11400Sstevel@tonic-gate  *			- permission to add set-gid bit
11410Sstevel@tonic-gate  *
11420Sstevel@tonic-gate  * The ovap argument should include AT_MODE|AT_UID|AT_GID.
11430Sstevel@tonic-gate  *
11440Sstevel@tonic-gate  * If the vap argument does not include AT_MODE, the mode will be copied from
11450Sstevel@tonic-gate  * ovap.  In certain situations set-uid/set-gid bits need to be removed;
11460Sstevel@tonic-gate  * this is done by marking vap->va_mask to include AT_MODE and va_mode
11470Sstevel@tonic-gate  * is updated to the newly computed mode.
11480Sstevel@tonic-gate  */
11490Sstevel@tonic-gate 
11500Sstevel@tonic-gate int
11510Sstevel@tonic-gate secpolicy_vnode_setattr(cred_t *cr, struct vnode *vp, struct vattr *vap,
11520Sstevel@tonic-gate 	const struct vattr *ovap, int flags,
11530Sstevel@tonic-gate 	int unlocked_access(void *, int, cred_t *),
11540Sstevel@tonic-gate 	void *node)
11550Sstevel@tonic-gate {
11560Sstevel@tonic-gate 	int mask = vap->va_mask;
11570Sstevel@tonic-gate 	int error = 0;
11585331Samw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
11590Sstevel@tonic-gate 
11600Sstevel@tonic-gate 	if (mask & AT_SIZE) {
11610Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
11620Sstevel@tonic-gate 			error = EISDIR;
11630Sstevel@tonic-gate 			goto out;
11640Sstevel@tonic-gate 		}
11655331Samw 
11665331Samw 		/*
11675331Samw 		 * If ATTR_NOACLCHECK is set in the flags, then we don't
11685331Samw 		 * perform the secondary unlocked_access() call since the
11695331Samw 		 * ACL (if any) is being checked there.
11705331Samw 		 */
11715331Samw 		if (skipaclchk == B_FALSE) {
11725331Samw 			error = unlocked_access(node, VWRITE, cr);
11735331Samw 			if (error)
11745331Samw 				goto out;
11755331Samw 		}
11760Sstevel@tonic-gate 	}
11770Sstevel@tonic-gate 	if (mask & AT_MODE) {
11780Sstevel@tonic-gate 		/*
11790Sstevel@tonic-gate 		 * If not the owner of the file then check privilege
11800Sstevel@tonic-gate 		 * for two things: the privilege to set the mode at all
11810Sstevel@tonic-gate 		 * and, if we're setting setuid, we also need permissions
11820Sstevel@tonic-gate 		 * to add the set-uid bit, if we're not the owner.
11830Sstevel@tonic-gate 		 * In the specific case of creating a set-uid root
11840Sstevel@tonic-gate 		 * file, we need even more permissions.
11850Sstevel@tonic-gate 		 */
11860Sstevel@tonic-gate 		if ((error = secpolicy_vnode_setdac(cr, ovap->va_uid)) != 0)
11870Sstevel@tonic-gate 			goto out;
11880Sstevel@tonic-gate 
11892796Smarks 		if ((error = secpolicy_setid_setsticky_clear(vp, vap,
11902796Smarks 		    ovap, cr)) != 0)
11910Sstevel@tonic-gate 			goto out;
11920Sstevel@tonic-gate 	} else
11930Sstevel@tonic-gate 		vap->va_mode = ovap->va_mode;
11940Sstevel@tonic-gate 
11950Sstevel@tonic-gate 	if (mask & (AT_UID|AT_GID)) {
11960Sstevel@tonic-gate 		boolean_t checkpriv = B_FALSE;
11970Sstevel@tonic-gate 		int priv;
11980Sstevel@tonic-gate 		boolean_t allzone = B_FALSE;
11990Sstevel@tonic-gate 
12000Sstevel@tonic-gate 		/*
12010Sstevel@tonic-gate 		 * Chowning files.
12020Sstevel@tonic-gate 		 *
12030Sstevel@tonic-gate 		 * If you are the file owner:
12040Sstevel@tonic-gate 		 *	chown to other uid		FILE_CHOWN_SELF
12050Sstevel@tonic-gate 		 *	chown to gid (non-member) 	FILE_CHOWN_SELF
12060Sstevel@tonic-gate 		 *	chown to gid (member) 		<none>
12070Sstevel@tonic-gate 		 *
12080Sstevel@tonic-gate 		 * Instead of PRIV_FILE_CHOWN_SELF, FILE_CHOWN is also
12090Sstevel@tonic-gate 		 * acceptable but the first one is reported when debugging.
12100Sstevel@tonic-gate 		 *
12110Sstevel@tonic-gate 		 * If you are not the file owner:
12120Sstevel@tonic-gate 		 *	chown from root			PRIV_FILE_CHOWN + zone
12130Sstevel@tonic-gate 		 *	chown from other to any		PRIV_FILE_CHOWN
12140Sstevel@tonic-gate 		 *
12150Sstevel@tonic-gate 		 */
12160Sstevel@tonic-gate 		if (cr->cr_uid != ovap->va_uid) {
12170Sstevel@tonic-gate 			checkpriv = B_TRUE;
12180Sstevel@tonic-gate 			allzone = (ovap->va_uid == 0);
12190Sstevel@tonic-gate 			priv = PRIV_FILE_CHOWN;
12200Sstevel@tonic-gate 		} else {
12210Sstevel@tonic-gate 			if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
12220Sstevel@tonic-gate 			    ((mask & AT_GID) && vap->va_gid != ovap->va_gid &&
12230Sstevel@tonic-gate 			    !groupmember(vap->va_gid, cr))) {
12240Sstevel@tonic-gate 				checkpriv = B_TRUE;
12250Sstevel@tonic-gate 				priv = HAS_PRIVILEGE(cr, PRIV_FILE_CHOWN) ?
12260Sstevel@tonic-gate 				    PRIV_FILE_CHOWN : PRIV_FILE_CHOWN_SELF;
12270Sstevel@tonic-gate 			}
12280Sstevel@tonic-gate 		}
12290Sstevel@tonic-gate 		/*
12300Sstevel@tonic-gate 		 * If necessary, check privilege to see if update can be done.
12310Sstevel@tonic-gate 		 */
12320Sstevel@tonic-gate 		if (checkpriv &&
12330Sstevel@tonic-gate 		    (error = PRIV_POLICY(cr, priv, allzone, EPERM, NULL))
12340Sstevel@tonic-gate 		    != 0) {
12350Sstevel@tonic-gate 			goto out;
12360Sstevel@tonic-gate 		}
12370Sstevel@tonic-gate 
12380Sstevel@tonic-gate 		/*
12390Sstevel@tonic-gate 		 * If the file has either the set UID or set GID bits
12400Sstevel@tonic-gate 		 * set and the caller can set the bits, then leave them.
12410Sstevel@tonic-gate 		 */
12421115Smarks 		secpolicy_setid_clear(vap, cr);
12430Sstevel@tonic-gate 	}
12440Sstevel@tonic-gate 	if (mask & (AT_ATIME|AT_MTIME)) {
12450Sstevel@tonic-gate 		/*
12460Sstevel@tonic-gate 		 * If not the file owner and not otherwise privileged,
12470Sstevel@tonic-gate 		 * always return an error when setting the
12480Sstevel@tonic-gate 		 * time other than the current (ATTR_UTIME flag set).
12490Sstevel@tonic-gate 		 * If setting the current time (ATTR_UTIME not set) then
12500Sstevel@tonic-gate 		 * unlocked_access will check permissions according to policy.
12510Sstevel@tonic-gate 		 */
12520Sstevel@tonic-gate 		if (cr->cr_uid != ovap->va_uid) {
12530Sstevel@tonic-gate 			if (flags & ATTR_UTIME)
12540Sstevel@tonic-gate 				error = secpolicy_vnode_utime_modify(cr);
12555331Samw 			else if (skipaclchk == B_FALSE) {
12560Sstevel@tonic-gate 				error = unlocked_access(node, VWRITE, cr);
12570Sstevel@tonic-gate 				if (error == EACCES &&
12580Sstevel@tonic-gate 				    secpolicy_vnode_utime_modify(cr) == 0)
12590Sstevel@tonic-gate 					error = 0;
12600Sstevel@tonic-gate 			}
12610Sstevel@tonic-gate 			if (error)
12620Sstevel@tonic-gate 				goto out;
12630Sstevel@tonic-gate 		}
12640Sstevel@tonic-gate 	}
12655331Samw 
12665331Samw 	/*
12675331Samw 	 * Check for optional attributes here by checking the following:
12685331Samw 	 */
12695331Samw 	if (mask & AT_XVATTR)
12705331Samw 		error = secpolicy_xvattr((xvattr_t *)vap, ovap->va_uid, cr,
12715331Samw 		    vp->v_type);
12720Sstevel@tonic-gate out:
12730Sstevel@tonic-gate 	return (error);
12740Sstevel@tonic-gate }
12750Sstevel@tonic-gate 
12760Sstevel@tonic-gate /*
12770Sstevel@tonic-gate  * Name:	secpolicy_pcfs_modify_bootpartition()
12780Sstevel@tonic-gate  *
12790Sstevel@tonic-gate  * Normal:	verify that subject can modify a pcfs boot partition.
12800Sstevel@tonic-gate  *
12810Sstevel@tonic-gate  * Output:	EACCES - if privilege check failed.
12820Sstevel@tonic-gate  */
12830Sstevel@tonic-gate /*ARGSUSED*/
12840Sstevel@tonic-gate int
12850Sstevel@tonic-gate secpolicy_pcfs_modify_bootpartition(const cred_t *cred)
12860Sstevel@tonic-gate {
12870Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_ALL, B_FALSE, EACCES,
12880Sstevel@tonic-gate 	    "modify pcfs boot partition"));
12890Sstevel@tonic-gate }
12900Sstevel@tonic-gate 
12910Sstevel@tonic-gate /*
12920Sstevel@tonic-gate  * System V IPC routines
12930Sstevel@tonic-gate  */
12940Sstevel@tonic-gate int
12950Sstevel@tonic-gate secpolicy_ipc_owner(const cred_t *cr, const struct kipc_perm *ip)
12960Sstevel@tonic-gate {
12970Sstevel@tonic-gate 	if (crgetzoneid(cr) != ip->ipc_zoneid ||
12980Sstevel@tonic-gate 	    (cr->cr_uid != ip->ipc_uid && cr->cr_uid != ip->ipc_cuid)) {
12990Sstevel@tonic-gate 		boolean_t allzone = B_FALSE;
13000Sstevel@tonic-gate 		if (ip->ipc_uid == 0 || ip->ipc_cuid == 0)
13010Sstevel@tonic-gate 			allzone = B_TRUE;
13020Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_OWNER, allzone, EPERM, NULL));
13030Sstevel@tonic-gate 	}
13040Sstevel@tonic-gate 	return (0);
13050Sstevel@tonic-gate }
13060Sstevel@tonic-gate 
13070Sstevel@tonic-gate int
13080Sstevel@tonic-gate secpolicy_ipc_config(const cred_t *cr)
13090Sstevel@tonic-gate {
13100Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_IPC_CONFIG, B_FALSE, EPERM, NULL));
13110Sstevel@tonic-gate }
13120Sstevel@tonic-gate 
13130Sstevel@tonic-gate int
13140Sstevel@tonic-gate secpolicy_ipc_access(const cred_t *cr, const struct kipc_perm *ip, mode_t mode)
13150Sstevel@tonic-gate {
13160Sstevel@tonic-gate 
13170Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
13180Sstevel@tonic-gate 
13190Sstevel@tonic-gate 	ASSERT((mode & (MSG_R|MSG_W)) != 0);
13200Sstevel@tonic-gate 
13210Sstevel@tonic-gate 	if ((mode & MSG_R) &&
13220Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_IPC_DAC_READ, allzone, EACCES, NULL) != 0)
13230Sstevel@tonic-gate 		return (EACCES);
13240Sstevel@tonic-gate 
13250Sstevel@tonic-gate 	if (mode & MSG_W) {
13260Sstevel@tonic-gate 		if (cr->cr_uid != 0 && (ip->ipc_uid == 0 || ip->ipc_cuid == 0))
13270Sstevel@tonic-gate 			allzone = B_TRUE;
13280Sstevel@tonic-gate 
13290Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_DAC_WRITE, allzone, EACCES,
13300Sstevel@tonic-gate 		    NULL));
13310Sstevel@tonic-gate 	}
13320Sstevel@tonic-gate 	return (0);
13330Sstevel@tonic-gate }
13340Sstevel@tonic-gate 
13350Sstevel@tonic-gate int
13360Sstevel@tonic-gate secpolicy_rsm_access(const cred_t *cr, uid_t owner, mode_t mode)
13370Sstevel@tonic-gate {
13380Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
13390Sstevel@tonic-gate 
13400Sstevel@tonic-gate 	ASSERT((mode & (MSG_R|MSG_W)) != 0);
13410Sstevel@tonic-gate 
13420Sstevel@tonic-gate 	if ((mode & MSG_R) &&
13430Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_IPC_DAC_READ, allzone, EACCES, NULL) != 0)
13440Sstevel@tonic-gate 		return (EACCES);
13450Sstevel@tonic-gate 
13460Sstevel@tonic-gate 	if (mode & MSG_W) {
13470Sstevel@tonic-gate 		if (cr->cr_uid != 0 && owner == 0)
13480Sstevel@tonic-gate 			allzone = B_TRUE;
13490Sstevel@tonic-gate 
13500Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_DAC_WRITE, allzone, EACCES,
13510Sstevel@tonic-gate 		    NULL));
13520Sstevel@tonic-gate 	}
13530Sstevel@tonic-gate 	return (0);
13540Sstevel@tonic-gate }
13550Sstevel@tonic-gate 
13560Sstevel@tonic-gate /*
13570Sstevel@tonic-gate  * Audit configuration.
13580Sstevel@tonic-gate  */
13590Sstevel@tonic-gate int
13600Sstevel@tonic-gate secpolicy_audit_config(const cred_t *cr)
13610Sstevel@tonic-gate {
13620Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_AUDIT, B_FALSE, EPERM, NULL));
13630Sstevel@tonic-gate }
13640Sstevel@tonic-gate 
13650Sstevel@tonic-gate /*
13660Sstevel@tonic-gate  * Audit record generation.
13670Sstevel@tonic-gate  */
13680Sstevel@tonic-gate int
13690Sstevel@tonic-gate secpolicy_audit_modify(const cred_t *cr)
13700Sstevel@tonic-gate {
13710Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_AUDIT, B_FALSE, EPERM, NULL));
13720Sstevel@tonic-gate }
13730Sstevel@tonic-gate 
13740Sstevel@tonic-gate /*
13750Sstevel@tonic-gate  * Get audit attributes.
13760Sstevel@tonic-gate  * Either PRIV_SYS_AUDIT or PRIV_PROC_AUDIT required; report the
13770Sstevel@tonic-gate  * "Least" of the two privileges on error.
13780Sstevel@tonic-gate  */
13790Sstevel@tonic-gate int
13800Sstevel@tonic-gate secpolicy_audit_getattr(const cred_t *cr)
13810Sstevel@tonic-gate {
13820Sstevel@tonic-gate 	if (!PRIV_POLICY_ONLY(cr, PRIV_SYS_AUDIT, B_FALSE)) {
13830Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_PROC_AUDIT, B_FALSE, EPERM,
13840Sstevel@tonic-gate 		    NULL));
13850Sstevel@tonic-gate 	} else {
13860Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_AUDIT, B_FALSE, EPERM, NULL));
13870Sstevel@tonic-gate 	}
13880Sstevel@tonic-gate }
13890Sstevel@tonic-gate 
13900Sstevel@tonic-gate 
13910Sstevel@tonic-gate /*
13920Sstevel@tonic-gate  * Locking physical memory
13930Sstevel@tonic-gate  */
13940Sstevel@tonic-gate int
13950Sstevel@tonic-gate secpolicy_lock_memory(const cred_t *cr)
13960Sstevel@tonic-gate {
13970Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_LOCK_MEMORY, B_FALSE, EPERM, NULL));
13980Sstevel@tonic-gate }
13990Sstevel@tonic-gate 
14000Sstevel@tonic-gate /*
14010Sstevel@tonic-gate  * Accounting (both acct(2) and exacct).
14020Sstevel@tonic-gate  */
14030Sstevel@tonic-gate int
14040Sstevel@tonic-gate secpolicy_acct(const cred_t *cr)
14050Sstevel@tonic-gate {
14060Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ACCT, B_FALSE, EPERM, NULL));
14070Sstevel@tonic-gate }
14080Sstevel@tonic-gate 
14090Sstevel@tonic-gate /*
14100Sstevel@tonic-gate  * Is this process privileged to change its uids at will?
14110Sstevel@tonic-gate  * Uid 0 is still considered "special" and having the SETID
14120Sstevel@tonic-gate  * privilege is not sufficient to get uid 0.
14130Sstevel@tonic-gate  * Files are owned by root, so the privilege would give
14140Sstevel@tonic-gate  * full access and euid 0 is still effective.
14150Sstevel@tonic-gate  *
14160Sstevel@tonic-gate  * If you have the privilege and euid 0 only then do you
14170Sstevel@tonic-gate  * get the powers of root wrt uid 0.
14180Sstevel@tonic-gate  *
14190Sstevel@tonic-gate  * For gid manipulations, this is should be called with an
14200Sstevel@tonic-gate  * uid of -1.
14210Sstevel@tonic-gate  *
14220Sstevel@tonic-gate  */
14230Sstevel@tonic-gate int
14240Sstevel@tonic-gate secpolicy_allow_setid(const cred_t *cr, uid_t newuid, boolean_t checkonly)
14250Sstevel@tonic-gate {
14260Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
14270Sstevel@tonic-gate 
14280Sstevel@tonic-gate 	if (newuid == 0 && cr->cr_uid != 0 && cr->cr_suid != 0 &&
14290Sstevel@tonic-gate 	    cr->cr_ruid != 0) {
14300Sstevel@tonic-gate 		allzone = B_TRUE;
14310Sstevel@tonic-gate 	}
14320Sstevel@tonic-gate 
14330Sstevel@tonic-gate 	return (checkonly ? !PRIV_POLICY_ONLY(cr, PRIV_PROC_SETID, allzone) :
14340Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_PROC_SETID, allzone, EPERM, NULL));
14350Sstevel@tonic-gate }
14360Sstevel@tonic-gate 
14370Sstevel@tonic-gate 
14380Sstevel@tonic-gate /*
14390Sstevel@tonic-gate  * Acting on a different process: if the mode is for writing,
14400Sstevel@tonic-gate  * the restrictions are more severe.  This is called after
14410Sstevel@tonic-gate  * we've verified that the uids do not match.
14420Sstevel@tonic-gate  */
14430Sstevel@tonic-gate int
14440Sstevel@tonic-gate secpolicy_proc_owner(const cred_t *scr, const cred_t *tcr, int mode)
14450Sstevel@tonic-gate {
14460Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
14470Sstevel@tonic-gate 
14480Sstevel@tonic-gate 	if ((mode & VWRITE) && scr->cr_uid != 0 &&
14490Sstevel@tonic-gate 	    (tcr->cr_uid == 0 || tcr->cr_ruid == 0 || tcr->cr_suid == 0))
14500Sstevel@tonic-gate 		allzone = B_TRUE;
14510Sstevel@tonic-gate 
14520Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, allzone, EPERM, NULL));
14530Sstevel@tonic-gate }
14540Sstevel@tonic-gate 
14550Sstevel@tonic-gate int
14560Sstevel@tonic-gate secpolicy_proc_access(const cred_t *scr)
14570Sstevel@tonic-gate {
14580Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, B_FALSE, EACCES, NULL));
14590Sstevel@tonic-gate }
14600Sstevel@tonic-gate 
14610Sstevel@tonic-gate int
14620Sstevel@tonic-gate secpolicy_proc_excl_open(const cred_t *scr)
14630Sstevel@tonic-gate {
14640Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, B_FALSE, EBUSY, NULL));
14650Sstevel@tonic-gate }
14660Sstevel@tonic-gate 
14670Sstevel@tonic-gate int
14680Sstevel@tonic-gate secpolicy_proc_zone(const cred_t *scr)
14690Sstevel@tonic-gate {
14700Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_ZONE, B_FALSE, EPERM, NULL));
14710Sstevel@tonic-gate }
14720Sstevel@tonic-gate 
14730Sstevel@tonic-gate /*
14740Sstevel@tonic-gate  * Destroying the system
14750Sstevel@tonic-gate  */
14760Sstevel@tonic-gate 
14770Sstevel@tonic-gate int
14780Sstevel@tonic-gate secpolicy_kmdb(const cred_t *scr)
14790Sstevel@tonic-gate {
14800Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
14810Sstevel@tonic-gate }
14820Sstevel@tonic-gate 
14831414Scindi int
14841414Scindi secpolicy_error_inject(const cred_t *scr)
14851414Scindi {
14861414Scindi 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
14871414Scindi }
14881414Scindi 
14890Sstevel@tonic-gate /*
14900Sstevel@tonic-gate  * Processor sets, cpu configuration, resource pools.
14910Sstevel@tonic-gate  */
14920Sstevel@tonic-gate int
14930Sstevel@tonic-gate secpolicy_pset(const cred_t *cr)
14940Sstevel@tonic-gate {
14950Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
14960Sstevel@tonic-gate }
14970Sstevel@tonic-gate 
14980Sstevel@tonic-gate int
14990Sstevel@tonic-gate secpolicy_ponline(const cred_t *cr)
15000Sstevel@tonic-gate {
15010Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
15020Sstevel@tonic-gate }
15030Sstevel@tonic-gate 
15040Sstevel@tonic-gate int
15050Sstevel@tonic-gate secpolicy_pool(const cred_t *cr)
15060Sstevel@tonic-gate {
15070Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
15080Sstevel@tonic-gate }
15090Sstevel@tonic-gate 
15100Sstevel@tonic-gate int
15110Sstevel@tonic-gate secpolicy_blacklist(const cred_t *cr)
15120Sstevel@tonic-gate {
15130Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
15140Sstevel@tonic-gate }
15150Sstevel@tonic-gate 
15160Sstevel@tonic-gate /*
15170Sstevel@tonic-gate  * Catch all system configuration.
15180Sstevel@tonic-gate  */
15190Sstevel@tonic-gate int
15200Sstevel@tonic-gate secpolicy_sys_config(const cred_t *cr, boolean_t checkonly)
15210Sstevel@tonic-gate {
15220Sstevel@tonic-gate 	if (checkonly) {
15230Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_CONFIG, B_FALSE) ? 0 :
15240Sstevel@tonic-gate 		    EPERM);
15250Sstevel@tonic-gate 	} else {
15260Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
15270Sstevel@tonic-gate 	}
15280Sstevel@tonic-gate }
15290Sstevel@tonic-gate 
15300Sstevel@tonic-gate /*
15310Sstevel@tonic-gate  * Zone administration (halt, reboot, etc.) from within zone.
15320Sstevel@tonic-gate  */
15330Sstevel@tonic-gate int
15340Sstevel@tonic-gate secpolicy_zone_admin(const cred_t *cr, boolean_t checkonly)
15350Sstevel@tonic-gate {
15360Sstevel@tonic-gate 	if (checkonly) {
15370Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_ADMIN, B_FALSE) ? 0 :
15380Sstevel@tonic-gate 		    EPERM);
15390Sstevel@tonic-gate 	} else {
15400Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM,
15410Sstevel@tonic-gate 		    NULL));
15420Sstevel@tonic-gate 	}
15430Sstevel@tonic-gate }
15440Sstevel@tonic-gate 
15450Sstevel@tonic-gate /*
15460Sstevel@tonic-gate  * Zone configuration (create, halt, enter).
15470Sstevel@tonic-gate  */
15480Sstevel@tonic-gate int
15490Sstevel@tonic-gate secpolicy_zone_config(const cred_t *cr)
15500Sstevel@tonic-gate {
15510Sstevel@tonic-gate 	/*
15520Sstevel@tonic-gate 	 * Require all privileges to avoid possibility of privilege
15530Sstevel@tonic-gate 	 * escalation.
15540Sstevel@tonic-gate 	 */
15550Sstevel@tonic-gate 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL));
15560Sstevel@tonic-gate }
15570Sstevel@tonic-gate 
15580Sstevel@tonic-gate /*
15590Sstevel@tonic-gate  * Various other system configuration calls
15600Sstevel@tonic-gate  */
15610Sstevel@tonic-gate int
15620Sstevel@tonic-gate secpolicy_coreadm(const cred_t *cr)
15630Sstevel@tonic-gate {
15640Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM, NULL));
15650Sstevel@tonic-gate }
15660Sstevel@tonic-gate 
15670Sstevel@tonic-gate int
15680Sstevel@tonic-gate secpolicy_systeminfo(const cred_t *cr)
15690Sstevel@tonic-gate {
15700Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM, NULL));
15710Sstevel@tonic-gate }
15720Sstevel@tonic-gate 
15730Sstevel@tonic-gate int
15740Sstevel@tonic-gate secpolicy_dispadm(const cred_t *cr)
15750Sstevel@tonic-gate {
15760Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
15770Sstevel@tonic-gate }
15780Sstevel@tonic-gate 
15790Sstevel@tonic-gate int
15800Sstevel@tonic-gate secpolicy_settime(const cred_t *cr)
15810Sstevel@tonic-gate {
15820Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_TIME, B_FALSE, EPERM, NULL));
15830Sstevel@tonic-gate }
15840Sstevel@tonic-gate 
15850Sstevel@tonic-gate /*
15860Sstevel@tonic-gate  * For realtime users: high resolution clock.
15870Sstevel@tonic-gate  */
15880Sstevel@tonic-gate int
15890Sstevel@tonic-gate secpolicy_clock_highres(const cred_t *cr)
15900Sstevel@tonic-gate {
15910Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_CLOCK_HIGHRES, B_FALSE, EPERM,
15920Sstevel@tonic-gate 	    NULL));
15930Sstevel@tonic-gate }
15940Sstevel@tonic-gate 
15950Sstevel@tonic-gate /*
15960Sstevel@tonic-gate  * drv_priv() is documented as callable from interrupt context, not that
15970Sstevel@tonic-gate  * anyone ever does, but still.  No debugging or auditing can be done when
15980Sstevel@tonic-gate  * it is called from interrupt context.
15990Sstevel@tonic-gate  * returns 0 on succes, EPERM on failure.
16000Sstevel@tonic-gate  */
16010Sstevel@tonic-gate int
16020Sstevel@tonic-gate drv_priv(cred_t *cr)
16030Sstevel@tonic-gate {
16040Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
16050Sstevel@tonic-gate }
16060Sstevel@tonic-gate 
16070Sstevel@tonic-gate int
16080Sstevel@tonic-gate secpolicy_sys_devices(const cred_t *cr)
16090Sstevel@tonic-gate {
16100Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
16110Sstevel@tonic-gate }
16120Sstevel@tonic-gate 
16130Sstevel@tonic-gate int
16140Sstevel@tonic-gate secpolicy_excl_open(const cred_t *cr)
16150Sstevel@tonic-gate {
16160Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EBUSY, NULL));
16170Sstevel@tonic-gate }
16180Sstevel@tonic-gate 
16190Sstevel@tonic-gate int
16200Sstevel@tonic-gate secpolicy_rctlsys(const cred_t *cr, boolean_t is_zone_rctl)
16210Sstevel@tonic-gate {
16220Sstevel@tonic-gate 	/* zone.* rctls can only be set from the global zone */
16230Sstevel@tonic-gate 	if (is_zone_rctl && priv_policy_global(cr) != 0)
16240Sstevel@tonic-gate 		return (EPERM);
16250Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
16260Sstevel@tonic-gate }
16270Sstevel@tonic-gate 
16280Sstevel@tonic-gate int
16290Sstevel@tonic-gate secpolicy_resource(const cred_t *cr)
16300Sstevel@tonic-gate {
16310Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
16320Sstevel@tonic-gate }
16330Sstevel@tonic-gate 
16340Sstevel@tonic-gate /*
16350Sstevel@tonic-gate  * Processes with a real uid of 0 escape any form of accounting, much
16360Sstevel@tonic-gate  * like before.
16370Sstevel@tonic-gate  */
16380Sstevel@tonic-gate int
16390Sstevel@tonic-gate secpolicy_newproc(const cred_t *cr)
16400Sstevel@tonic-gate {
16410Sstevel@tonic-gate 	if (cr->cr_ruid == 0)
16420Sstevel@tonic-gate 		return (0);
16430Sstevel@tonic-gate 
16440Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
16450Sstevel@tonic-gate }
16460Sstevel@tonic-gate 
16470Sstevel@tonic-gate /*
16480Sstevel@tonic-gate  * Networking
16490Sstevel@tonic-gate  */
16500Sstevel@tonic-gate int
16510Sstevel@tonic-gate secpolicy_net_rawaccess(const cred_t *cr)
16520Sstevel@tonic-gate {
16530Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_NET_RAWACCESS, B_FALSE, EACCES, NULL));
16540Sstevel@tonic-gate }
16550Sstevel@tonic-gate 
16560Sstevel@tonic-gate /*
16570Sstevel@tonic-gate  * Need this privilege for accessing the ICMP device
16580Sstevel@tonic-gate  */
16590Sstevel@tonic-gate int
16600Sstevel@tonic-gate secpolicy_net_icmpaccess(const cred_t *cr)
16610Sstevel@tonic-gate {
16620Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_NET_ICMPACCESS, B_FALSE, EACCES, NULL));
16630Sstevel@tonic-gate }
16640Sstevel@tonic-gate 
16650Sstevel@tonic-gate /*
16660Sstevel@tonic-gate  * There are a few rare cases where the kernel generates ioctls() from
16670Sstevel@tonic-gate  * interrupt context with a credential of kcred rather than NULL.
16680Sstevel@tonic-gate  * In those cases, we take the safe and cheap test.
16690Sstevel@tonic-gate  */
16700Sstevel@tonic-gate int
16710Sstevel@tonic-gate secpolicy_net_config(const cred_t *cr, boolean_t checkonly)
16720Sstevel@tonic-gate {
16730Sstevel@tonic-gate 	if (checkonly) {
16740Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE) ?
16750Sstevel@tonic-gate 		    0 : EPERM);
16760Sstevel@tonic-gate 	} else {
16770Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_NET_CONFIG, B_FALSE, EPERM,
16780Sstevel@tonic-gate 		    NULL));
16790Sstevel@tonic-gate 	}
16800Sstevel@tonic-gate }
16810Sstevel@tonic-gate 
16820Sstevel@tonic-gate 
16830Sstevel@tonic-gate /*
16844962Sdh155122  * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_IP_CONFIG.
16853448Sdh155122  *
16863448Sdh155122  * There are a few rare cases where the kernel generates ioctls() from
16873448Sdh155122  * interrupt context with a credential of kcred rather than NULL.
16883448Sdh155122  * In those cases, we take the safe and cheap test.
16893448Sdh155122  */
16903448Sdh155122 int
16913448Sdh155122 secpolicy_ip_config(const cred_t *cr, boolean_t checkonly)
16923448Sdh155122 {
16933448Sdh155122 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
16943448Sdh155122 		return (secpolicy_net_config(cr, checkonly));
16953448Sdh155122 
16963448Sdh155122 	if (checkonly) {
16973448Sdh155122 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_IP_CONFIG, B_FALSE) ?
16983448Sdh155122 		    0 : EPERM);
16993448Sdh155122 	} else {
17003448Sdh155122 		return (PRIV_POLICY(cr, PRIV_SYS_IP_CONFIG, B_FALSE, EPERM,
17013448Sdh155122 		    NULL));
17023448Sdh155122 	}
17033448Sdh155122 }
17043448Sdh155122 
17053448Sdh155122 
17063448Sdh155122 /*
17073448Sdh155122  * Map IP pseudo privileges to actual privileges.
17083448Sdh155122  * So we don't need to recompile IP when we change the privileges.
17093448Sdh155122  */
17103448Sdh155122 int
17113448Sdh155122 secpolicy_ip(const cred_t *cr, int netpriv, boolean_t checkonly)
17123448Sdh155122 {
17133448Sdh155122 	int priv = PRIV_ALL;
17143448Sdh155122 
17153448Sdh155122 	switch (netpriv) {
17163448Sdh155122 	case OP_CONFIG:
17173448Sdh155122 		priv = PRIV_SYS_IP_CONFIG;
17183448Sdh155122 		break;
17193448Sdh155122 	case OP_RAW:
17203448Sdh155122 		priv = PRIV_NET_RAWACCESS;
17213448Sdh155122 		break;
17223448Sdh155122 	case OP_PRIVPORT:
17233448Sdh155122 		priv = PRIV_NET_PRIVADDR;
17243448Sdh155122 		break;
17253448Sdh155122 	}
17263448Sdh155122 	ASSERT(priv != PRIV_ALL);
17273448Sdh155122 	if (checkonly)
17283448Sdh155122 		return (PRIV_POLICY_ONLY(cr, priv, B_FALSE) ? 0 : EPERM);
17293448Sdh155122 	else
17303448Sdh155122 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
17313448Sdh155122 }
17323448Sdh155122 
17333448Sdh155122 /*
17340Sstevel@tonic-gate  * Map network pseudo privileges to actual privileges.
17350Sstevel@tonic-gate  * So we don't need to recompile IP when we change the privileges.
17360Sstevel@tonic-gate  */
17370Sstevel@tonic-gate int
17380Sstevel@tonic-gate secpolicy_net(const cred_t *cr, int netpriv, boolean_t checkonly)
17390Sstevel@tonic-gate {
17400Sstevel@tonic-gate 	int priv = PRIV_ALL;
17410Sstevel@tonic-gate 
17420Sstevel@tonic-gate 	switch (netpriv) {
17430Sstevel@tonic-gate 	case OP_CONFIG:
17440Sstevel@tonic-gate 		priv = PRIV_SYS_NET_CONFIG;
17450Sstevel@tonic-gate 		break;
17460Sstevel@tonic-gate 	case OP_RAW:
17470Sstevel@tonic-gate 		priv = PRIV_NET_RAWACCESS;
17480Sstevel@tonic-gate 		break;
17490Sstevel@tonic-gate 	case OP_PRIVPORT:
17500Sstevel@tonic-gate 		priv = PRIV_NET_PRIVADDR;
17510Sstevel@tonic-gate 		break;
17520Sstevel@tonic-gate 	}
17530Sstevel@tonic-gate 	ASSERT(priv != PRIV_ALL);
17540Sstevel@tonic-gate 	if (checkonly)
17550Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, priv, B_FALSE) ? 0 : EPERM);
17560Sstevel@tonic-gate 	else
17570Sstevel@tonic-gate 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
17580Sstevel@tonic-gate }
17590Sstevel@tonic-gate 
17600Sstevel@tonic-gate /*
17610Sstevel@tonic-gate  * Checks for operations that are either client-only or are used by
17620Sstevel@tonic-gate  * both clients and servers.
17630Sstevel@tonic-gate  */
17640Sstevel@tonic-gate int
17650Sstevel@tonic-gate secpolicy_nfs(const cred_t *cr)
17660Sstevel@tonic-gate {
17670Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_NFS, B_FALSE, EPERM, NULL));
17680Sstevel@tonic-gate }
17690Sstevel@tonic-gate 
17700Sstevel@tonic-gate /*
17710Sstevel@tonic-gate  * Special case for opening rpcmod: have NFS privileges or network
17720Sstevel@tonic-gate  * config privileges.
17730Sstevel@tonic-gate  */
17740Sstevel@tonic-gate int
17750Sstevel@tonic-gate secpolicy_rpcmod_open(const cred_t *cr)
17760Sstevel@tonic-gate {
17770Sstevel@tonic-gate 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NFS, B_FALSE))
17780Sstevel@tonic-gate 		return (secpolicy_nfs(cr));
17790Sstevel@tonic-gate 	else
17800Sstevel@tonic-gate 		return (secpolicy_net_config(cr, NULL));
17810Sstevel@tonic-gate }
17820Sstevel@tonic-gate 
17830Sstevel@tonic-gate int
17840Sstevel@tonic-gate secpolicy_chroot(const cred_t *cr)
17850Sstevel@tonic-gate {
17860Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_CHROOT, B_FALSE, EPERM, NULL));
17870Sstevel@tonic-gate }
17880Sstevel@tonic-gate 
17890Sstevel@tonic-gate int
17900Sstevel@tonic-gate secpolicy_tasksys(const cred_t *cr)
17910Sstevel@tonic-gate {
17920Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_TASKID, B_FALSE, EPERM, NULL));
17930Sstevel@tonic-gate }
17940Sstevel@tonic-gate 
17950Sstevel@tonic-gate /*
17960Sstevel@tonic-gate  * Basic privilege checks.
17970Sstevel@tonic-gate  */
17980Sstevel@tonic-gate int
1799*6134Scasper secpolicy_basic_exec(const cred_t *cr, vnode_t *vp)
18000Sstevel@tonic-gate {
1801*6134Scasper 	return (priv_policy_va(cr, PRIV_PROC_EXEC, B_FALSE, EPERM, NULL,
1802*6134Scasper 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
18030Sstevel@tonic-gate }
18040Sstevel@tonic-gate 
18050Sstevel@tonic-gate int
18060Sstevel@tonic-gate secpolicy_basic_fork(const cred_t *cr)
18070Sstevel@tonic-gate {
18080Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_FORK, B_FALSE, EPERM, NULL));
18090Sstevel@tonic-gate }
18100Sstevel@tonic-gate 
18110Sstevel@tonic-gate int
18120Sstevel@tonic-gate secpolicy_basic_proc(const cred_t *cr)
18130Sstevel@tonic-gate {
18140Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_SESSION, B_FALSE, EPERM, NULL));
18150Sstevel@tonic-gate }
18160Sstevel@tonic-gate 
18170Sstevel@tonic-gate /*
18180Sstevel@tonic-gate  * Slightly complicated because we don't want to trigger the policy too
18190Sstevel@tonic-gate  * often.  First we shortcircuit access to "self" (tp == sp) or if
18200Sstevel@tonic-gate  * we don't have the privilege but if we have permission
18210Sstevel@tonic-gate  * just return (0) and we don't flag the privilege as needed.
18220Sstevel@tonic-gate  * Else, we test for the privilege because we either have it or need it.
18230Sstevel@tonic-gate  */
18240Sstevel@tonic-gate int
18250Sstevel@tonic-gate secpolicy_basic_procinfo(const cred_t *cr, proc_t *tp, proc_t *sp)
18260Sstevel@tonic-gate {
18270Sstevel@tonic-gate 	if (tp == sp ||
18280Sstevel@tonic-gate 	    !HAS_PRIVILEGE(cr, PRIV_PROC_INFO) && prochasprocperm(tp, sp, cr)) {
18290Sstevel@tonic-gate 		return (0);
18300Sstevel@tonic-gate 	} else {
18310Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_PROC_INFO, B_FALSE, EPERM, NULL));
18320Sstevel@tonic-gate 	}
18330Sstevel@tonic-gate }
18340Sstevel@tonic-gate 
18350Sstevel@tonic-gate int
18360Sstevel@tonic-gate secpolicy_basic_link(const cred_t *cr)
18370Sstevel@tonic-gate {
18380Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_LINK_ANY, B_FALSE, EPERM, NULL));
18390Sstevel@tonic-gate }
18400Sstevel@tonic-gate 
18410Sstevel@tonic-gate /*
18420Sstevel@tonic-gate  * Additional device protection.
18430Sstevel@tonic-gate  *
18440Sstevel@tonic-gate  * Traditionally, a device has specific permissions on the node in
18450Sstevel@tonic-gate  * the filesystem which govern which devices can be opened by what
18460Sstevel@tonic-gate  * processes.  In certain cases, it is desirable to add extra
18470Sstevel@tonic-gate  * restrictions, as writing to certain devices is identical to
18480Sstevel@tonic-gate  * having a complete run of the system.
18490Sstevel@tonic-gate  *
18500Sstevel@tonic-gate  * This mechanism is called the device policy.
18510Sstevel@tonic-gate  *
18520Sstevel@tonic-gate  * When a device is opened, its policy entry is looked up in the
18530Sstevel@tonic-gate  * policy cache and checked.
18540Sstevel@tonic-gate  */
18550Sstevel@tonic-gate int
18560Sstevel@tonic-gate secpolicy_spec_open(const cred_t *cr, struct vnode *vp, int oflag)
18570Sstevel@tonic-gate {
18580Sstevel@tonic-gate 	devplcy_t *plcy;
18590Sstevel@tonic-gate 	int err;
18600Sstevel@tonic-gate 	struct snode *csp = VTOS(common_specvp(vp));
18614962Sdh155122 	priv_set_t pset;
18620Sstevel@tonic-gate 
18630Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
18640Sstevel@tonic-gate 
18650Sstevel@tonic-gate 	if (csp->s_plcy == NULL || csp->s_plcy->dp_gen != devplcy_gen) {
18660Sstevel@tonic-gate 		plcy = devpolicy_find(vp);
18670Sstevel@tonic-gate 		if (csp->s_plcy)
18680Sstevel@tonic-gate 			dpfree(csp->s_plcy);
18690Sstevel@tonic-gate 		csp->s_plcy = plcy;
18700Sstevel@tonic-gate 		ASSERT(plcy != NULL);
18710Sstevel@tonic-gate 	} else
18720Sstevel@tonic-gate 		plcy = csp->s_plcy;
18730Sstevel@tonic-gate 
18740Sstevel@tonic-gate 	if (plcy == nullpolicy) {
18750Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
18760Sstevel@tonic-gate 		return (0);
18770Sstevel@tonic-gate 	}
18780Sstevel@tonic-gate 
18790Sstevel@tonic-gate 	dphold(plcy);
18800Sstevel@tonic-gate 
18810Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
18820Sstevel@tonic-gate 
18834962Sdh155122 	if (oflag & FWRITE)
18844962Sdh155122 		pset = plcy->dp_wrp;
18854962Sdh155122 	else
18864962Sdh155122 		pset = plcy->dp_rdp;
18874962Sdh155122 	/*
18884962Sdh155122 	 * Special case:
18894962Sdh155122 	 * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_IP_CONFIG.
18904962Sdh155122 	 * If PRIV_SYS_NET_CONFIG is present and PRIV_SYS_IP_CONFIG is
18914962Sdh155122 	 * required, replace PRIV_SYS_IP_CONFIG with PRIV_SYS_NET_CONFIG
18924962Sdh155122 	 * in the required privilege set before doing the check.
18934962Sdh155122 	 */
18944962Sdh155122 	if (priv_ismember(&pset, PRIV_SYS_IP_CONFIG) &&
18954962Sdh155122 	    priv_ismember(&CR_OEPRIV(cr), PRIV_SYS_NET_CONFIG) &&
18964962Sdh155122 	    !priv_ismember(&CR_OEPRIV(cr), PRIV_SYS_IP_CONFIG)) {
18974962Sdh155122 		priv_delset(&pset, PRIV_SYS_IP_CONFIG);
18984962Sdh155122 		priv_addset(&pset, PRIV_SYS_NET_CONFIG);
18994962Sdh155122 	}
19004962Sdh155122 
19014962Sdh155122 	err = secpolicy_require_set(cr, &pset, "devpolicy");
19020Sstevel@tonic-gate 	dpfree(plcy);
19030Sstevel@tonic-gate 
19040Sstevel@tonic-gate 	return (err);
19050Sstevel@tonic-gate }
19060Sstevel@tonic-gate 
19070Sstevel@tonic-gate int
19080Sstevel@tonic-gate secpolicy_modctl(const cred_t *cr, int cmd)
19090Sstevel@tonic-gate {
19100Sstevel@tonic-gate 	switch (cmd) {
19110Sstevel@tonic-gate 	case MODINFO:
19122723Scth 	case MODGETMAJBIND:
19130Sstevel@tonic-gate 	case MODGETPATH:
19140Sstevel@tonic-gate 	case MODGETPATHLEN:
19152723Scth 	case MODGETNAME:
19160Sstevel@tonic-gate 	case MODGETFBNAME:
19170Sstevel@tonic-gate 	case MODGETDEVPOLICY:
19180Sstevel@tonic-gate 	case MODGETDEVPOLICYBYNAME:
19192723Scth 	case MODDEVT2INSTANCE:
19202723Scth 	case MODSIZEOF_DEVID:
19212723Scth 	case MODGETDEVID:
19222723Scth 	case MODSIZEOF_MINORNAME:
19232723Scth 	case MODGETMINORNAME:
19242723Scth 	case MODGETDEVFSPATH_LEN:
19252723Scth 	case MODGETDEVFSPATH:
19262723Scth 	case MODGETDEVFSPATH_MI_LEN:
19272723Scth 	case MODGETDEVFSPATH_MI:
19280Sstevel@tonic-gate 		/* Unprivileged */
19290Sstevel@tonic-gate 		return (0);
19300Sstevel@tonic-gate 	case MODLOAD:
19310Sstevel@tonic-gate 	case MODSETDEVPOLICY:
19320Sstevel@tonic-gate 		return (secpolicy_require_set(cr, PRIV_FULLSET, NULL));
19330Sstevel@tonic-gate 	default:
19340Sstevel@tonic-gate 		return (secpolicy_sys_config(cr, B_FALSE));
19350Sstevel@tonic-gate 	}
19360Sstevel@tonic-gate }
19370Sstevel@tonic-gate 
19380Sstevel@tonic-gate int
19390Sstevel@tonic-gate secpolicy_console(const cred_t *cr)
19400Sstevel@tonic-gate {
19410Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
19420Sstevel@tonic-gate }
19430Sstevel@tonic-gate 
19440Sstevel@tonic-gate int
19450Sstevel@tonic-gate secpolicy_power_mgmt(const cred_t *cr)
19460Sstevel@tonic-gate {
19470Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
19480Sstevel@tonic-gate }
19490Sstevel@tonic-gate 
19500Sstevel@tonic-gate /*
19510Sstevel@tonic-gate  * Simulate terminal input; another escalation of privileges avenue.
19520Sstevel@tonic-gate  */
19530Sstevel@tonic-gate 
19540Sstevel@tonic-gate int
19550Sstevel@tonic-gate secpolicy_sti(const cred_t *cr)
19560Sstevel@tonic-gate {
19570Sstevel@tonic-gate 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL));
19580Sstevel@tonic-gate }
19590Sstevel@tonic-gate 
19601676Sjpk boolean_t
19611676Sjpk secpolicy_net_reply_equal(const cred_t *cr)
19621676Sjpk {
19631676Sjpk 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
19641676Sjpk }
19651676Sjpk 
19660Sstevel@tonic-gate int
19670Sstevel@tonic-gate secpolicy_swapctl(const cred_t *cr)
19680Sstevel@tonic-gate {
19690Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
19700Sstevel@tonic-gate }
19710Sstevel@tonic-gate 
19720Sstevel@tonic-gate int
19730Sstevel@tonic-gate secpolicy_cpc_cpu(const cred_t *cr)
19740Sstevel@tonic-gate {
19750Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CPC_CPU, B_FALSE, EACCES, NULL));
19760Sstevel@tonic-gate }
19770Sstevel@tonic-gate 
19780Sstevel@tonic-gate /*
19796073Sacruz  * secpolicy_contract_identity
19806073Sacruz  *
19816073Sacruz  * Determine if the subject may set the process contract FMRI value
19826073Sacruz  */
19836073Sacruz int
19846073Sacruz secpolicy_contract_identity(const cred_t *cr)
19856073Sacruz {
19866073Sacruz 	return (PRIV_POLICY(cr, PRIV_CONTRACT_IDENTITY, B_FALSE, EPERM, NULL));
19876073Sacruz }
19886073Sacruz 
19896073Sacruz /*
19900Sstevel@tonic-gate  * secpolicy_contract_observer
19910Sstevel@tonic-gate  *
19920Sstevel@tonic-gate  * Determine if the subject may observe a specific contract's events.
19930Sstevel@tonic-gate  */
19940Sstevel@tonic-gate int
19950Sstevel@tonic-gate secpolicy_contract_observer(const cred_t *cr, struct contract *ct)
19960Sstevel@tonic-gate {
19970Sstevel@tonic-gate 	if (contract_owned(ct, cr, B_FALSE))
19980Sstevel@tonic-gate 		return (0);
19990Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CONTRACT_OBSERVER, B_FALSE, EPERM, NULL));
20000Sstevel@tonic-gate }
20010Sstevel@tonic-gate 
20020Sstevel@tonic-gate /*
20030Sstevel@tonic-gate  * secpolicy_contract_observer_choice
20040Sstevel@tonic-gate  *
20050Sstevel@tonic-gate  * Determine if the subject may observe any contract's events.  Just
20060Sstevel@tonic-gate  * tests privilege and audits on success.
20070Sstevel@tonic-gate  */
20080Sstevel@tonic-gate boolean_t
20090Sstevel@tonic-gate secpolicy_contract_observer_choice(const cred_t *cr)
20100Sstevel@tonic-gate {
20110Sstevel@tonic-gate 	return (PRIV_POLICY_CHOICE(cr, PRIV_CONTRACT_OBSERVER, B_FALSE));
20120Sstevel@tonic-gate }
20130Sstevel@tonic-gate 
20140Sstevel@tonic-gate /*
20150Sstevel@tonic-gate  * secpolicy_contract_event
20160Sstevel@tonic-gate  *
20170Sstevel@tonic-gate  * Determine if the subject may request critical contract events or
20180Sstevel@tonic-gate  * reliable contract event delivery.
20190Sstevel@tonic-gate  */
20200Sstevel@tonic-gate int
20210Sstevel@tonic-gate secpolicy_contract_event(const cred_t *cr)
20220Sstevel@tonic-gate {
20230Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CONTRACT_EVENT, B_FALSE, EPERM, NULL));
20240Sstevel@tonic-gate }
20250Sstevel@tonic-gate 
20260Sstevel@tonic-gate /*
20270Sstevel@tonic-gate  * secpolicy_contract_event_choice
20280Sstevel@tonic-gate  *
20290Sstevel@tonic-gate  * Determine if the subject may retain contract events in its critical
20300Sstevel@tonic-gate  * set when a change in other terms would normally require a change in
20310Sstevel@tonic-gate  * the critical set.  Just tests privilege and audits on success.
20320Sstevel@tonic-gate  */
20330Sstevel@tonic-gate boolean_t
20340Sstevel@tonic-gate secpolicy_contract_event_choice(const cred_t *cr)
20350Sstevel@tonic-gate {
20360Sstevel@tonic-gate 	return (PRIV_POLICY_CHOICE(cr, PRIV_CONTRACT_EVENT, B_FALSE));
20370Sstevel@tonic-gate }
20380Sstevel@tonic-gate 
20390Sstevel@tonic-gate /*
20401544Seschrock  * secpolicy_gart_access
20410Sstevel@tonic-gate  *
20421544Seschrock  * Determine if the subject has sufficient priveleges to make ioctls to agpgart
20431544Seschrock  * device.
20440Sstevel@tonic-gate  */
20450Sstevel@tonic-gate int
20460Sstevel@tonic-gate secpolicy_gart_access(const cred_t *cr)
20470Sstevel@tonic-gate {
20481862Scasper 	return (PRIV_POLICY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE, EPERM, NULL));
20490Sstevel@tonic-gate }
20500Sstevel@tonic-gate 
20510Sstevel@tonic-gate /*
20521544Seschrock  * secpolicy_gart_map
20530Sstevel@tonic-gate  *
20541544Seschrock  * Determine if the subject has sufficient priveleges to map aperture range
20551544Seschrock  * through agpgart driver.
20560Sstevel@tonic-gate  */
20570Sstevel@tonic-gate int
20580Sstevel@tonic-gate secpolicy_gart_map(const cred_t *cr)
20590Sstevel@tonic-gate {
20601862Scasper 	if (PRIV_POLICY_ONLY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE)) {
20611862Scasper 		return (PRIV_POLICY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE, EPERM,
20621862Scasper 		    NULL));
20631862Scasper 	} else {
20641862Scasper 		return (PRIV_POLICY(cr, PRIV_GRAPHICS_MAP, B_FALSE, EPERM,
20651862Scasper 		    NULL));
20660Sstevel@tonic-gate 	}
20670Sstevel@tonic-gate }
2068789Sahrens 
2069789Sahrens /*
20701544Seschrock  * secpolicy_zinject
20711544Seschrock  *
20721544Seschrock  * Determine if the subject can inject faults in the ZFS fault injection
20731544Seschrock  * framework.  Requires all privileges.
20741544Seschrock  */
20751544Seschrock int
20761544Seschrock secpolicy_zinject(const cred_t *cr)
20771544Seschrock {
20781544Seschrock 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL));
20791544Seschrock }
20801544Seschrock 
20811544Seschrock /*
2082789Sahrens  * secpolicy_zfs
2083789Sahrens  *
20841544Seschrock  * Determine if the subject has permission to manipulate ZFS datasets
20851544Seschrock  * (not pools).  Equivalent to the SYS_MOUNT privilege.
2086789Sahrens  */
2087789Sahrens int
2088789Sahrens secpolicy_zfs(const cred_t *cr)
2089789Sahrens {
2090789Sahrens 	return (PRIV_POLICY(cr, PRIV_SYS_MOUNT, B_FALSE, EPERM, NULL));
2091789Sahrens }
20924321Scasper 
20934321Scasper /*
20944321Scasper  * secpolicy_idmap
20954321Scasper  *
20964321Scasper  * Determine if the calling process has permissions to register an SID
20974321Scasper  * mapping daemon and allocate ephemeral IDs.
20984321Scasper  */
20994321Scasper int
21004321Scasper secpolicy_idmap(const cred_t *cr)
21014321Scasper {
21025771Sjp151216 	return (PRIV_POLICY(cr, PRIV_FILE_SETID, B_TRUE, EPERM, NULL));
21034321Scasper }
21044581Ssherrym 
21054581Ssherrym /*
21064581Ssherrym  * secpolicy_ucode_update
21074581Ssherrym  *
21084581Ssherrym  * Determine if the subject has sufficient privilege to update microcode.
21094581Ssherrym  */
21104581Ssherrym int
21114581Ssherrym secpolicy_ucode_update(const cred_t *scr)
21124581Ssherrym {
21134581Ssherrym 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
21144581Ssherrym }
21154962Sdh155122 
21164962Sdh155122 /*
21174962Sdh155122  * secpolicy_sadopen
21184962Sdh155122  *
21194962Sdh155122  * Determine if the subject has sufficient privilege to access /dev/sad/admin.
21204962Sdh155122  * /dev/sad/admin appear in global zone and exclusive-IP zones only.
21214962Sdh155122  * In global zone, sys_config is required.
21224962Sdh155122  * In exclusive-IP zones, sys_ip_config is required.
21234962Sdh155122  * Note that sys_config is prohibited in non-global zones.
21244962Sdh155122  */
21254962Sdh155122 int
21264962Sdh155122 secpolicy_sadopen(const cred_t *credp)
21274962Sdh155122 {
21284962Sdh155122 	priv_set_t pset;
21294962Sdh155122 
21304962Sdh155122 	priv_emptyset(&pset);
21314962Sdh155122 
21324962Sdh155122 	if (crgetzoneid(credp) == GLOBAL_ZONEID)
21334962Sdh155122 		priv_addset(&pset, PRIV_SYS_CONFIG);
21344962Sdh155122 	else
21354962Sdh155122 		priv_addset(&pset, PRIV_SYS_IP_CONFIG);
21364962Sdh155122 
21374962Sdh155122 	return (secpolicy_require_set(credp, &pset, "devpolicy"));
21384962Sdh155122 }
21395331Samw 
2140*6134Scasper 
2141*6134Scasper /*
2142*6134Scasper  * Add privileges to a particular privilege set; this is called when the
2143*6134Scasper  * current sets of privileges are not sufficient.  I.e., we should always
2144*6134Scasper  * call the policy override functions from here.
2145*6134Scasper  * What we are allowed to have is in the Observed Permitted set; so
2146*6134Scasper  * we compute the difference between that and the newset.
2147*6134Scasper  */
2148*6134Scasper int
2149*6134Scasper secpolicy_require_privs(const cred_t *cr, const priv_set_t *nset)
2150*6134Scasper {
2151*6134Scasper 	priv_set_t rqd;
2152*6134Scasper 
2153*6134Scasper 	rqd = CR_OPPRIV(cr);
2154*6134Scasper 
2155*6134Scasper 	priv_inverse(&rqd);
2156*6134Scasper 	priv_intersect(nset, &rqd);
2157*6134Scasper 
2158*6134Scasper 	return (secpolicy_require_set(cr, &rqd, NULL));
2159*6134Scasper }
2160*6134Scasper 
21615331Samw /*
21625331Samw  * secpolicy_smb
21635331Samw  *
21645331Samw  * Determine if the cred_t has PRIV_SYS_SMB privilege, indicating
21655331Samw  * that it has permission to access the smbsrv kernel driver.
21665331Samw  * PRIV_POLICY checks the privilege and audits the check.
21675331Samw  *
21685331Samw  * Returns:
21695331Samw  * 0       Driver access is allowed.
21705331Samw  * EPERM   Driver access is NOT permitted.
21715331Samw  */
21725331Samw int
21735331Samw secpolicy_smb(const cred_t *cr)
21745331Samw {
21755331Samw 	return (PRIV_POLICY(cr, PRIV_SYS_SMB, B_FALSE, EPERM, NULL));
21765331Samw }
21775440Sjm199354 
21785440Sjm199354 /*
21795440Sjm199354  * secpolicy_vscan
21805440Sjm199354  *
21815440Sjm199354  * Determine if cred_t has the necessary privileges to access a file
21825440Sjm199354  * for virus scanning and update its extended system attributes.
21835440Sjm199354  * PRIV_FILE_DAC_SEARCH, PRIV_FILE_DAC_READ - file access
21845440Sjm199354  * PRIV_FILE_FLAG_SET - set extended system attributes
21855440Sjm199354  *
21865440Sjm199354  * PRIV_POLICY checks the privilege and audits the check.
21875440Sjm199354  *
21885440Sjm199354  * Returns:
21895440Sjm199354  * 0      file access for virus scanning allowed.
21905440Sjm199354  * EPERM  file access for virus scanning is NOT permitted.
21915440Sjm199354  */
21925440Sjm199354 int
21935440Sjm199354 secpolicy_vscan(const cred_t *cr)
21945440Sjm199354 {
21955440Sjm199354 	if ((PRIV_POLICY(cr, PRIV_FILE_DAC_SEARCH, B_FALSE, EPERM, NULL)) ||
21965440Sjm199354 	    (PRIV_POLICY(cr, PRIV_FILE_DAC_READ, B_FALSE, EPERM, NULL)) ||
21975440Sjm199354 	    (PRIV_POLICY(cr, PRIV_FILE_FLAG_SET, B_FALSE, EPERM, NULL))) {
21985440Sjm199354 		return (EPERM);
21995440Sjm199354 	}
22005440Sjm199354 
22015440Sjm199354 	return (0);
22025440Sjm199354 }
22036007Sthurlow 
22046007Sthurlow /*
22056007Sthurlow  * secpolicy_smbfs_login
22066007Sthurlow  *
22076007Sthurlow  * Determines if the caller can add and delete the smbfs login
22086007Sthurlow  * password in the the nsmb kernel module for the CIFS client.
22096007Sthurlow  *
22106007Sthurlow  * Returns:
22116007Sthurlow  * 0       access is allowed.
22126007Sthurlow  * EPERM   access is NOT allowed.
22136007Sthurlow  */
22146007Sthurlow int
22156007Sthurlow secpolicy_smbfs_login(const cred_t *cr, uid_t uid)
22166007Sthurlow {
22176007Sthurlow 	uid_t cruid = crgetruid(cr);
22186007Sthurlow 
22196007Sthurlow 	if (cruid == uid)
22206007Sthurlow 		return (0);
22216007Sthurlow 	return (PRIV_POLICY(cr, PRIV_PROC_OWNER, B_FALSE,
22226007Sthurlow 	    EPERM, NULL));
22236007Sthurlow }
2224