xref: /titanic_52/usr/src/uts/common/os/policy.c (revision 134a1f4e3289b54e0f980e9cf05352e419a60bee)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5ea8dc4b6Seschrock  * Common Development and Distribution License (the "License").
6ea8dc4b6Seschrock  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*134a1f4eSCasper H.S. Dik  * Copyright (c) 2003, 2010, Oracle and/or its affiliates. All rights reserved.
237c478bd9Sstevel@tonic-gate  */
247c478bd9Sstevel@tonic-gate 
257c478bd9Sstevel@tonic-gate #include <sys/types.h>
267c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
277c478bd9Sstevel@tonic-gate #include <sys/param.h>
287c478bd9Sstevel@tonic-gate #include <sys/systm.h>
297c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
307c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
317c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
327c478bd9Sstevel@tonic-gate #include <sys/stat.h>
337c478bd9Sstevel@tonic-gate #include <sys/errno.h>
347c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
357c478bd9Sstevel@tonic-gate #include <sys/user.h>
367c478bd9Sstevel@tonic-gate #include <sys/proc.h>
377c478bd9Sstevel@tonic-gate #include <sys/acct.h>
387c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h>
397c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
407c478bd9Sstevel@tonic-gate #include <sys/debug.h>
417c478bd9Sstevel@tonic-gate #include <sys/policy.h>
427c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
437c478bd9Sstevel@tonic-gate #include <sys/msg.h>
447c478bd9Sstevel@tonic-gate #include <sys/devpolicy.h>
457c478bd9Sstevel@tonic-gate #include <c2/audit.h>
467c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
47ddf7fe95Scasper #include <sys/klpd.h>
487c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
497c478bd9Sstevel@tonic-gate #include <sys/disp.h>
507c478bd9Sstevel@tonic-gate #include <sys/zone.h>
517c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
527c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
537c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
547c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
557c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
56da14cebeSEric Cheng #include <sys/dld_ioc.h>
577c478bd9Sstevel@tonic-gate 
587c478bd9Sstevel@tonic-gate /*
597c478bd9Sstevel@tonic-gate  * There are two possible layers of privilege routines and two possible
607c478bd9Sstevel@tonic-gate  * levels of secpolicy.  Plus one other we may not be interested in, so
617c478bd9Sstevel@tonic-gate  * we may need as many as 6 but no more.
627c478bd9Sstevel@tonic-gate  */
637c478bd9Sstevel@tonic-gate #define	MAXPRIVSTACK		6
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate int priv_debug = 0;
66634e26ecSCasper H.S. Dik int priv_basic_test = -1;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate /*
697c478bd9Sstevel@tonic-gate  * This file contains the majority of the policy routines.
707c478bd9Sstevel@tonic-gate  * Since the policy routines are defined by function and not
717c478bd9Sstevel@tonic-gate  * by privilege, there is quite a bit of duplication of
727c478bd9Sstevel@tonic-gate  * functions.
737c478bd9Sstevel@tonic-gate  *
74da6c28aaSamw  * The secpolicy functions must not make assumptions about
757c478bd9Sstevel@tonic-gate  * locks held or not held as any lock can be held while they're
767c478bd9Sstevel@tonic-gate  * being called.
777c478bd9Sstevel@tonic-gate  *
787c478bd9Sstevel@tonic-gate  * Credentials are read-only so no special precautions need to
797c478bd9Sstevel@tonic-gate  * be taken while locking them.
807c478bd9Sstevel@tonic-gate  *
817c478bd9Sstevel@tonic-gate  * When a new policy check needs to be added to the system the
827c478bd9Sstevel@tonic-gate  * following procedure should be followed:
837c478bd9Sstevel@tonic-gate  *
847c478bd9Sstevel@tonic-gate  *		Pick an appropriate secpolicy_*() function
857c478bd9Sstevel@tonic-gate  *			-> done if one exists.
867c478bd9Sstevel@tonic-gate  *		Create a new secpolicy function, preferably with
877c478bd9Sstevel@tonic-gate  *		a descriptive name using the standard template.
887c478bd9Sstevel@tonic-gate  *		Pick an appropriate privilege for the policy.
897c478bd9Sstevel@tonic-gate  *		If no appropraite privilege exists, define new one
907c478bd9Sstevel@tonic-gate  *		(this should be done with extreme care; in most cases
917c478bd9Sstevel@tonic-gate  *		little is gained by adding another privilege)
927c478bd9Sstevel@tonic-gate  *
937c478bd9Sstevel@tonic-gate  * WHY ROOT IS STILL SPECIAL.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * In a number of the policy functions, there are still explicit
967c478bd9Sstevel@tonic-gate  * checks for uid 0.  The rationale behind these is that many root
977c478bd9Sstevel@tonic-gate  * owned files/objects hold configuration information which can give full
987c478bd9Sstevel@tonic-gate  * privileges to the user once written to.  To prevent escalation
997c478bd9Sstevel@tonic-gate  * of privilege by allowing just a single privilege to modify root owned
1007c478bd9Sstevel@tonic-gate  * objects, we've added these root specific checks where we considered
1017c478bd9Sstevel@tonic-gate  * them necessary: modifying root owned files, changing uids to 0, etc.
1027c478bd9Sstevel@tonic-gate  *
1037c478bd9Sstevel@tonic-gate  * PRIVILEGE ESCALATION AND ZONES.
1047c478bd9Sstevel@tonic-gate  *
1057c478bd9Sstevel@tonic-gate  * A number of operations potentially allow the caller to achieve
1067c478bd9Sstevel@tonic-gate  * privileges beyond the ones normally required to perform the operation.
1077c478bd9Sstevel@tonic-gate  * For example, if allowed to create a setuid 0 executable, a process can
1087c478bd9Sstevel@tonic-gate  * gain privileges beyond PRIV_FILE_SETID.  Zones, however, place
1097c478bd9Sstevel@tonic-gate  * restrictions on the ability to gain privileges beyond those available
1107c478bd9Sstevel@tonic-gate  * within the zone through file and process manipulation.  Hence, such
1117c478bd9Sstevel@tonic-gate  * operations require that the caller have an effective set that includes
1127c478bd9Sstevel@tonic-gate  * all privileges available within the current zone, or all privileges
1137c478bd9Sstevel@tonic-gate  * if executing in the global zone.
1147c478bd9Sstevel@tonic-gate  *
1157c478bd9Sstevel@tonic-gate  * This is indicated in the priv_policy* policy checking functions
1167c478bd9Sstevel@tonic-gate  * through a combination of parameters.  The "priv" parameter indicates
1177c478bd9Sstevel@tonic-gate  * the privilege that is required, and the "allzone" parameter indicates
1187c478bd9Sstevel@tonic-gate  * whether or not all privileges in the zone are required.  In addition,
1197c478bd9Sstevel@tonic-gate  * priv can be set to PRIV_ALL to indicate that all privileges are
1207c478bd9Sstevel@tonic-gate  * required (regardless of zone).  There are three scenarios of interest:
1217c478bd9Sstevel@tonic-gate  * (1) operation requires a specific privilege
1227c478bd9Sstevel@tonic-gate  * (2) operation requires a specific privilege, and requires all
1237c478bd9Sstevel@tonic-gate  *     privileges available within the zone (or all privileges if in
1247c478bd9Sstevel@tonic-gate  *     the global zone)
1257c478bd9Sstevel@tonic-gate  * (3) operation requires all privileges, regardless of zone
1267c478bd9Sstevel@tonic-gate  *
1277c478bd9Sstevel@tonic-gate  * For (1), priv should be set to the specific privilege, and allzone
1287c478bd9Sstevel@tonic-gate  * should be set to B_FALSE.
1297c478bd9Sstevel@tonic-gate  * For (2), priv should be set to the specific privilege, and allzone
1307c478bd9Sstevel@tonic-gate  * should be set to B_TRUE.
1317c478bd9Sstevel@tonic-gate  * For (3), priv should be set to PRIV_ALL, and allzone should be set
1327c478bd9Sstevel@tonic-gate  * to B_FALSE.
1337c478bd9Sstevel@tonic-gate  *
1347c478bd9Sstevel@tonic-gate  */
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * The privileges are checked against the Effective set for
1387c478bd9Sstevel@tonic-gate  * ordinary processes and checked against the Limit set
1397c478bd9Sstevel@tonic-gate  * for euid 0 processes that haven't manipulated their privilege
1407c478bd9Sstevel@tonic-gate  * sets.
1417c478bd9Sstevel@tonic-gate  */
1427c478bd9Sstevel@tonic-gate #define	HAS_ALLPRIVS(cr)	priv_isfullset(&CR_OEPRIV(cr))
1437c478bd9Sstevel@tonic-gate #define	ZONEPRIVS(cr)		((cr)->cr_zone->zone_privset)
1447c478bd9Sstevel@tonic-gate #define	HAS_ALLZONEPRIVS(cr)	priv_issubset(ZONEPRIVS(cr), &CR_OEPRIV(cr))
1457c478bd9Sstevel@tonic-gate #define	HAS_PRIVILEGE(cr, pr)	((pr) == PRIV_ALL ? \
1467c478bd9Sstevel@tonic-gate 					HAS_ALLPRIVS(cr) : \
1477c478bd9Sstevel@tonic-gate 					PRIV_ISASSERT(&CR_OEPRIV(cr), pr))
1487c478bd9Sstevel@tonic-gate 
149*134a1f4eSCasper H.S. Dik #define	FAST_BASIC_CHECK(cr, priv)	\
150*134a1f4eSCasper H.S. Dik 	if (PRIV_ISASSERT(&CR_OEPRIV(cr), priv)) { \
151*134a1f4eSCasper H.S. Dik 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, B_FALSE); \
152*134a1f4eSCasper H.S. Dik 		return (0); \
153*134a1f4eSCasper H.S. Dik 	}
154*134a1f4eSCasper H.S. Dik 
1557c478bd9Sstevel@tonic-gate /*
156ddf7fe95Scasper  * Policy checking functions.
1577c478bd9Sstevel@tonic-gate  *
158ddf7fe95Scasper  * All of the system's policy should be implemented here.
1597c478bd9Sstevel@tonic-gate  */
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate /*
162ddf7fe95Scasper  * Private functions which take an additional va_list argument to
163ddf7fe95Scasper  * implement an object specific policy override.
164ddf7fe95Scasper  */
165ddf7fe95Scasper static int priv_policy_ap(const cred_t *, int, boolean_t, int,
166ddf7fe95Scasper     const char *, va_list);
167ddf7fe95Scasper static int priv_policy_va(const cred_t *, int, boolean_t, int,
168ddf7fe95Scasper     const char *, ...);
169ddf7fe95Scasper 
170ddf7fe95Scasper /*
1717c478bd9Sstevel@tonic-gate  * Generic policy calls
1727c478bd9Sstevel@tonic-gate  *
1737c478bd9Sstevel@tonic-gate  * The "bottom" functions of policy control
1747c478bd9Sstevel@tonic-gate  */
1757c478bd9Sstevel@tonic-gate static char *
1767c478bd9Sstevel@tonic-gate mprintf(const char *fmt, ...)
1777c478bd9Sstevel@tonic-gate {
1787c478bd9Sstevel@tonic-gate 	va_list args;
1797c478bd9Sstevel@tonic-gate 	char *buf;
1807c478bd9Sstevel@tonic-gate 	size_t len;
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
1837c478bd9Sstevel@tonic-gate 	len = vsnprintf(NULL, 0, fmt, args) + 1;
1847c478bd9Sstevel@tonic-gate 	va_end(args);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(len, KM_NOSLEEP);
1877c478bd9Sstevel@tonic-gate 
1887c478bd9Sstevel@tonic-gate 	if (buf == NULL)
1897c478bd9Sstevel@tonic-gate 		return (NULL);
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
1927c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, len, fmt, args);
1937c478bd9Sstevel@tonic-gate 	va_end(args);
1947c478bd9Sstevel@tonic-gate 
1957c478bd9Sstevel@tonic-gate 	return (buf);
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * priv_policy_errmsg()
2007c478bd9Sstevel@tonic-gate  *
2017c478bd9Sstevel@tonic-gate  * Generate an error message if privilege debugging is enabled system wide
2027c478bd9Sstevel@tonic-gate  * or for this particular process.
2037c478bd9Sstevel@tonic-gate  */
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate #define	FMTHDR	"%s[%d]: missing privilege \"%s\" (euid = %d, syscall = %d)"
2067c478bd9Sstevel@tonic-gate #define	FMTMSG	" for \"%s\""
2077c478bd9Sstevel@tonic-gate #define	FMTFUN	" needed at %s+0x%lx"
2087c478bd9Sstevel@tonic-gate 
2097c478bd9Sstevel@tonic-gate /* The maximum size privilege format: the concatenation of the above */
2107c478bd9Sstevel@tonic-gate #define	FMTMAX	FMTHDR FMTMSG FMTFUN "\n"
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate static void
2137c478bd9Sstevel@tonic-gate priv_policy_errmsg(const cred_t *cr, int priv, const char *msg)
2147c478bd9Sstevel@tonic-gate {
2157c478bd9Sstevel@tonic-gate 	struct proc *me;
2167c478bd9Sstevel@tonic-gate 	pc_t stack[MAXPRIVSTACK];
2177c478bd9Sstevel@tonic-gate 	int depth;
2187c478bd9Sstevel@tonic-gate 	int i;
2197c478bd9Sstevel@tonic-gate 	char *sym;
2207c478bd9Sstevel@tonic-gate 	ulong_t off;
2217c478bd9Sstevel@tonic-gate 	const char *pname;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	char *cmd;
2247c478bd9Sstevel@tonic-gate 	char fmt[sizeof (FMTMAX)];
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	if ((me = curproc) == &p0)
2277c478bd9Sstevel@tonic-gate 		return;
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate 	/* Privileges must be defined  */
2307c478bd9Sstevel@tonic-gate 	ASSERT(priv == PRIV_ALL || priv == PRIV_MULTIPLE ||
2317c478bd9Sstevel@tonic-gate 	    priv == PRIV_ALLZONE || priv == PRIV_GLOBAL ||
2327c478bd9Sstevel@tonic-gate 	    priv_getbynum(priv) != NULL);
2337c478bd9Sstevel@tonic-gate 
2347c478bd9Sstevel@tonic-gate 	if (priv == PRIV_ALLZONE && INGLOBALZONE(me))
2357c478bd9Sstevel@tonic-gate 		priv = PRIV_ALL;
2367c478bd9Sstevel@tonic-gate 
2377c478bd9Sstevel@tonic-gate 	if (curthread->t_pre_sys)
2387c478bd9Sstevel@tonic-gate 		ttolwp(curthread)->lwp_badpriv = (short)priv;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	if (priv_debug == 0 && (CR_FLAGS(cr) & PRIV_DEBUG) == 0)
2417c478bd9Sstevel@tonic-gate 		return;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	(void) strcpy(fmt, FMTHDR);
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	if (me->p_user.u_comm[0])
2467c478bd9Sstevel@tonic-gate 		cmd = &me->p_user.u_comm[0];
2477c478bd9Sstevel@tonic-gate 	else
2487c478bd9Sstevel@tonic-gate 		cmd = "priv_policy";
2497c478bd9Sstevel@tonic-gate 
2507c478bd9Sstevel@tonic-gate 	if (msg != NULL && *msg != '\0') {
2517c478bd9Sstevel@tonic-gate 		(void) strcat(fmt, FMTMSG);
2527c478bd9Sstevel@tonic-gate 	} else {
2537c478bd9Sstevel@tonic-gate 		(void) strcat(fmt, "%s");
2547c478bd9Sstevel@tonic-gate 		msg = "";
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	sym = NULL;
2587c478bd9Sstevel@tonic-gate 
2597c478bd9Sstevel@tonic-gate 	depth = getpcstack(stack, MAXPRIVSTACK);
2607c478bd9Sstevel@tonic-gate 
2617c478bd9Sstevel@tonic-gate 	/*
2627c478bd9Sstevel@tonic-gate 	 * Try to find the first interesting function on the stack.
2637c478bd9Sstevel@tonic-gate 	 * priv_policy* that's us, so completely uninteresting.
2647c478bd9Sstevel@tonic-gate 	 * suser(), drv_priv(), secpolicy_* are also called from
2657c478bd9Sstevel@tonic-gate 	 * too many locations to convey useful information.
2667c478bd9Sstevel@tonic-gate 	 */
2677c478bd9Sstevel@tonic-gate 	for (i = 0; i < depth; i++) {
2687c478bd9Sstevel@tonic-gate 		sym = kobj_getsymname((uintptr_t)stack[i], &off);
2697c478bd9Sstevel@tonic-gate 		if (sym != NULL &&
2707c478bd9Sstevel@tonic-gate 		    strstr(sym, "hasprocperm") == 0 &&
2717c478bd9Sstevel@tonic-gate 		    strcmp("suser", sym) != 0 &&
2727c478bd9Sstevel@tonic-gate 		    strcmp("ipcaccess", sym) != 0 &&
2737c478bd9Sstevel@tonic-gate 		    strcmp("drv_priv", sym) != 0 &&
2747c478bd9Sstevel@tonic-gate 		    strncmp("secpolicy_", sym, 10) != 0 &&
2757c478bd9Sstevel@tonic-gate 		    strncmp("priv_policy", sym, 11) != 0)
2767c478bd9Sstevel@tonic-gate 			break;
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	if (sym != NULL)
2807c478bd9Sstevel@tonic-gate 		(void) strcat(fmt, FMTFUN);
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	(void) strcat(fmt, "\n");
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate 	switch (priv) {
2857c478bd9Sstevel@tonic-gate 	case PRIV_ALL:
2867c478bd9Sstevel@tonic-gate 		pname = "ALL";
2877c478bd9Sstevel@tonic-gate 		break;
2887c478bd9Sstevel@tonic-gate 	case PRIV_MULTIPLE:
2897c478bd9Sstevel@tonic-gate 		pname = "MULTIPLE";
2907c478bd9Sstevel@tonic-gate 		break;
2917c478bd9Sstevel@tonic-gate 	case PRIV_ALLZONE:
2927c478bd9Sstevel@tonic-gate 		pname = "ZONE";
2937c478bd9Sstevel@tonic-gate 		break;
2947c478bd9Sstevel@tonic-gate 	case PRIV_GLOBAL:
2957c478bd9Sstevel@tonic-gate 		pname = "GLOBAL";
2967c478bd9Sstevel@tonic-gate 		break;
2977c478bd9Sstevel@tonic-gate 	default:
2987c478bd9Sstevel@tonic-gate 		pname = priv_getbynum(priv);
2997c478bd9Sstevel@tonic-gate 		break;
3007c478bd9Sstevel@tonic-gate 	}
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate 	if (CR_FLAGS(cr) & PRIV_DEBUG) {
3037c478bd9Sstevel@tonic-gate 		/* Remember last message, just like lwp_badpriv. */
3047c478bd9Sstevel@tonic-gate 		if (curthread->t_pdmsg != NULL) {
3057c478bd9Sstevel@tonic-gate 			kmem_free(curthread->t_pdmsg,
3067c478bd9Sstevel@tonic-gate 			    strlen(curthread->t_pdmsg) + 1);
3077c478bd9Sstevel@tonic-gate 		}
3087c478bd9Sstevel@tonic-gate 
3097c478bd9Sstevel@tonic-gate 		curthread->t_pdmsg = mprintf(fmt, cmd, me->p_pid, pname,
3107c478bd9Sstevel@tonic-gate 		    cr->cr_uid, curthread->t_sysnum, msg, sym, off);
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 		curthread->t_post_sys = 1;
313ddf7fe95Scasper 	}
314ddf7fe95Scasper 	if (priv_debug) {
3157c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, fmt, cmd, me->p_pid, pname, cr->cr_uid,
3167c478bd9Sstevel@tonic-gate 		    curthread->t_sysnum, msg, sym, off);
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate }
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate /*
321ddf7fe95Scasper  * Override the policy, if appropriate.  Return 0 if the external
322ddf7fe95Scasper  * policy engine approves.
323ddf7fe95Scasper  */
324ddf7fe95Scasper static int
325ddf7fe95Scasper priv_policy_override(const cred_t *cr, int priv, boolean_t allzone, va_list ap)
326ddf7fe95Scasper {
327ddf7fe95Scasper 	priv_set_t set;
328ddf7fe95Scasper 	int ret;
329ddf7fe95Scasper 
330ddf7fe95Scasper 	if (!(CR_FLAGS(cr) & PRIV_XPOLICY))
331ddf7fe95Scasper 		return (-1);
332ddf7fe95Scasper 
333ddf7fe95Scasper 	if (priv == PRIV_ALL) {
334ddf7fe95Scasper 		priv_fillset(&set);
335ddf7fe95Scasper 	} else if (allzone) {
336ddf7fe95Scasper 		set = *ZONEPRIVS(cr);
337ddf7fe95Scasper 	} else {
338ddf7fe95Scasper 		priv_emptyset(&set);
339ddf7fe95Scasper 		priv_addset(&set, priv);
340ddf7fe95Scasper 	}
341ddf7fe95Scasper 	ret = klpd_call(cr, &set, ap);
342ddf7fe95Scasper 	return (ret);
343ddf7fe95Scasper }
344ddf7fe95Scasper 
345ddf7fe95Scasper static int
346*134a1f4eSCasper H.S. Dik priv_policy_override_set(const cred_t *cr, const priv_set_t *req, va_list ap)
347ddf7fe95Scasper {
348*134a1f4eSCasper H.S. Dik 	if (CR_FLAGS(cr) & PRIV_PFEXEC)
349*134a1f4eSCasper H.S. Dik 		return (check_user_privs(cr, req));
350ddf7fe95Scasper 	if (CR_FLAGS(cr) & PRIV_XPOLICY) {
351ddf7fe95Scasper 		return (klpd_call(cr, req, ap));
352ddf7fe95Scasper 	}
353ddf7fe95Scasper 	return (-1);
354ddf7fe95Scasper }
355ddf7fe95Scasper 
356*134a1f4eSCasper H.S. Dik static int
357*134a1f4eSCasper H.S. Dik priv_policy_override_set_va(const cred_t *cr, const priv_set_t *req, ...)
358*134a1f4eSCasper H.S. Dik {
359*134a1f4eSCasper H.S. Dik 	va_list ap;
360*134a1f4eSCasper H.S. Dik 	int ret;
361*134a1f4eSCasper H.S. Dik 
362*134a1f4eSCasper H.S. Dik 	va_start(ap, req);
363*134a1f4eSCasper H.S. Dik 	ret = priv_policy_override_set(cr, req, ap);
364*134a1f4eSCasper H.S. Dik 	va_end(ap);
365*134a1f4eSCasper H.S. Dik 	return (ret);
366*134a1f4eSCasper H.S. Dik }
367*134a1f4eSCasper H.S. Dik 
368ddf7fe95Scasper /*
3697c478bd9Sstevel@tonic-gate  * Audit failure, log error message.
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@tonic-gate static void
3727c478bd9Sstevel@tonic-gate priv_policy_err(const cred_t *cr, int priv, boolean_t allzone, const char *msg)
3737c478bd9Sstevel@tonic-gate {
3747c478bd9Sstevel@tonic-gate 
375005d3febSMarek Pospisil 	if (AU_AUDITING())
3767c478bd9Sstevel@tonic-gate 		audit_priv(priv, allzone ? ZONEPRIVS(cr) : NULL, 0);
3777c478bd9Sstevel@tonic-gate 	DTRACE_PROBE2(priv__err, int, priv, boolean_t, allzone);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	if (priv_debug || (CR_FLAGS(cr) & PRIV_DEBUG) ||
3807c478bd9Sstevel@tonic-gate 	    curthread->t_pre_sys) {
3817c478bd9Sstevel@tonic-gate 		if (allzone && !HAS_ALLZONEPRIVS(cr)) {
3827c478bd9Sstevel@tonic-gate 			priv_policy_errmsg(cr, PRIV_ALLZONE, msg);
3837c478bd9Sstevel@tonic-gate 		} else {
3847c478bd9Sstevel@tonic-gate 			ASSERT(!HAS_PRIVILEGE(cr, priv));
3857c478bd9Sstevel@tonic-gate 			priv_policy_errmsg(cr, priv, msg);
3867c478bd9Sstevel@tonic-gate 		}
3877c478bd9Sstevel@tonic-gate 	}
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate /*
391ddf7fe95Scasper  * priv_policy_ap()
3927c478bd9Sstevel@tonic-gate  * return 0 or error.
3937c478bd9Sstevel@tonic-gate  * See block comment above for a description of "priv" and "allzone" usage.
3947c478bd9Sstevel@tonic-gate  */
395ddf7fe95Scasper static int
396ddf7fe95Scasper priv_policy_ap(const cred_t *cr, int priv, boolean_t allzone, int err,
397ddf7fe95Scasper     const char *msg, va_list ap)
3987c478bd9Sstevel@tonic-gate {
399ddf7fe95Scasper 	if ((HAS_PRIVILEGE(cr, priv) && (!allzone || HAS_ALLZONEPRIVS(cr))) ||
400ddf7fe95Scasper 	    (!servicing_interrupt() &&
401ddf7fe95Scasper 	    priv_policy_override(cr, priv, allzone, ap) == 0)) {
4027c478bd9Sstevel@tonic-gate 		if ((allzone || priv == PRIV_ALL ||
4037c478bd9Sstevel@tonic-gate 		    !PRIV_ISASSERT(priv_basic, priv)) &&
4047c478bd9Sstevel@tonic-gate 		    !servicing_interrupt()) {
405ae115bc7Smrj 			PTOU(curproc)->u_acflag |= ASU; /* Needed for SVVS */
406005d3febSMarek Pospisil 			if (AU_AUDITING())
4077c478bd9Sstevel@tonic-gate 				audit_priv(priv,
4087c478bd9Sstevel@tonic-gate 				    allzone ? ZONEPRIVS(cr) : NULL, 1);
4097c478bd9Sstevel@tonic-gate 		}
4107c478bd9Sstevel@tonic-gate 		err = 0;
4117c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, allzone);
4127c478bd9Sstevel@tonic-gate 	} else if (!servicing_interrupt()) {
4137c478bd9Sstevel@tonic-gate 		/* Failure audited in this procedure */
4147c478bd9Sstevel@tonic-gate 		priv_policy_err(cr, priv, allzone, msg);
4157c478bd9Sstevel@tonic-gate 	}
4167c478bd9Sstevel@tonic-gate 	return (err);
4177c478bd9Sstevel@tonic-gate }
4187c478bd9Sstevel@tonic-gate 
419ddf7fe95Scasper int
420ddf7fe95Scasper priv_policy_va(const cred_t *cr, int priv, boolean_t allzone, int err,
421ddf7fe95Scasper     const char *msg, ...)
422ddf7fe95Scasper {
423ddf7fe95Scasper 	int ret;
424ddf7fe95Scasper 	va_list ap;
425ddf7fe95Scasper 
426ddf7fe95Scasper 	va_start(ap, msg);
427ddf7fe95Scasper 	ret = priv_policy_ap(cr, priv, allzone, err, msg, ap);
428ddf7fe95Scasper 	va_end(ap);
429ddf7fe95Scasper 
430ddf7fe95Scasper 	return (ret);
431ddf7fe95Scasper }
432ddf7fe95Scasper 
433ddf7fe95Scasper int
434ddf7fe95Scasper priv_policy(const cred_t *cr, int priv, boolean_t allzone, int err,
435ddf7fe95Scasper     const char *msg)
436ddf7fe95Scasper {
437*134a1f4eSCasper H.S. Dik 	return (priv_policy_va(cr, priv, allzone, err, msg, KLPDARG_NONE));
438ddf7fe95Scasper }
439ddf7fe95Scasper 
4407c478bd9Sstevel@tonic-gate /*
4417c478bd9Sstevel@tonic-gate  * Return B_TRUE for sufficient privileges, B_FALSE for insufficient privileges.
4427c478bd9Sstevel@tonic-gate  */
4437c478bd9Sstevel@tonic-gate boolean_t
4447c478bd9Sstevel@tonic-gate priv_policy_choice(const cred_t *cr, int priv, boolean_t allzone)
4457c478bd9Sstevel@tonic-gate {
4467c478bd9Sstevel@tonic-gate 	boolean_t res = HAS_PRIVILEGE(cr, priv) &&
4477c478bd9Sstevel@tonic-gate 	    (!allzone || HAS_ALLZONEPRIVS(cr));
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate 	/* Audit success only */
450005d3febSMarek Pospisil 	if (res && AU_AUDITING() &&
4517c478bd9Sstevel@tonic-gate 	    (allzone || priv == PRIV_ALL || !PRIV_ISASSERT(priv_basic, priv)) &&
4527c478bd9Sstevel@tonic-gate 	    !servicing_interrupt()) {
4537c478bd9Sstevel@tonic-gate 		audit_priv(priv, allzone ? ZONEPRIVS(cr) : NULL, 1);
4547c478bd9Sstevel@tonic-gate 	}
4557c478bd9Sstevel@tonic-gate 	if (res) {
4567c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, allzone);
4577c478bd9Sstevel@tonic-gate 	} else {
4587c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(priv__err, int, priv, boolean_t, allzone);
4597c478bd9Sstevel@tonic-gate 	}
4607c478bd9Sstevel@tonic-gate 	return (res);
4617c478bd9Sstevel@tonic-gate }
4627c478bd9Sstevel@tonic-gate 
4637c478bd9Sstevel@tonic-gate /*
4647c478bd9Sstevel@tonic-gate  * Non-auditing variant of priv_policy_choice().
4657c478bd9Sstevel@tonic-gate  */
4667c478bd9Sstevel@tonic-gate boolean_t
4677c478bd9Sstevel@tonic-gate priv_policy_only(const cred_t *cr, int priv, boolean_t allzone)
4687c478bd9Sstevel@tonic-gate {
4697c478bd9Sstevel@tonic-gate 	boolean_t res = HAS_PRIVILEGE(cr, priv) &&
4707c478bd9Sstevel@tonic-gate 	    (!allzone || HAS_ALLZONEPRIVS(cr));
4717c478bd9Sstevel@tonic-gate 
4727c478bd9Sstevel@tonic-gate 	if (res) {
4737c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, allzone);
4747c478bd9Sstevel@tonic-gate 	} else {
4757c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(priv__err, int, priv, boolean_t, allzone);
4767c478bd9Sstevel@tonic-gate 	}
4777c478bd9Sstevel@tonic-gate 	return (res);
4787c478bd9Sstevel@tonic-gate }
4797c478bd9Sstevel@tonic-gate 
4807c478bd9Sstevel@tonic-gate /*
4817c478bd9Sstevel@tonic-gate  * Check whether all privileges in the required set are present.
4827c478bd9Sstevel@tonic-gate  */
4837c478bd9Sstevel@tonic-gate static int
484*134a1f4eSCasper H.S. Dik secpolicy_require_set(const cred_t *cr, const priv_set_t *req,
485*134a1f4eSCasper H.S. Dik     const char *msg, ...)
4867c478bd9Sstevel@tonic-gate {
4877c478bd9Sstevel@tonic-gate 	int priv;
4887c478bd9Sstevel@tonic-gate 	int pfound = -1;
4897c478bd9Sstevel@tonic-gate 	priv_set_t pset;
490*134a1f4eSCasper H.S. Dik 	va_list ap;
491*134a1f4eSCasper H.S. Dik 	int ret;
4927c478bd9Sstevel@tonic-gate 
4937c478bd9Sstevel@tonic-gate 	if (req == PRIV_FULLSET ? HAS_ALLPRIVS(cr) : priv_issubset(req,
4947c478bd9Sstevel@tonic-gate 	    &CR_OEPRIV(cr))) {
4957c478bd9Sstevel@tonic-gate 		return (0);
4967c478bd9Sstevel@tonic-gate 	}
4977c478bd9Sstevel@tonic-gate 
498*134a1f4eSCasper H.S. Dik 	va_start(ap, msg);
499*134a1f4eSCasper H.S. Dik 	ret = priv_policy_override_set(cr, req, ap);
500*134a1f4eSCasper H.S. Dik 	va_end(ap);
501*134a1f4eSCasper H.S. Dik 	if (ret == 0)
502ddf7fe95Scasper 		return (0);
503ddf7fe95Scasper 
5047c478bd9Sstevel@tonic-gate 	if (req == PRIV_FULLSET || priv_isfullset(req)) {
5057c478bd9Sstevel@tonic-gate 		priv_policy_err(cr, PRIV_ALL, B_FALSE, msg);
5067c478bd9Sstevel@tonic-gate 		return (EACCES);
5077c478bd9Sstevel@tonic-gate 	}
5087c478bd9Sstevel@tonic-gate 
5097c478bd9Sstevel@tonic-gate 	pset = CR_OEPRIV(cr);		/* present privileges */
5107c478bd9Sstevel@tonic-gate 	priv_inverse(&pset);		/* all non present privileges */
5117c478bd9Sstevel@tonic-gate 	priv_intersect(req, &pset);	/* the actual missing privs */
5127c478bd9Sstevel@tonic-gate 
513005d3febSMarek Pospisil 	if (AU_AUDITING())
5147c478bd9Sstevel@tonic-gate 		audit_priv(PRIV_NONE, &pset, 0);
5157c478bd9Sstevel@tonic-gate 	/*
5167c478bd9Sstevel@tonic-gate 	 * Privilege debugging; special case "one privilege in set".
5177c478bd9Sstevel@tonic-gate 	 */
5187c478bd9Sstevel@tonic-gate 	if (priv_debug || (CR_FLAGS(cr) & PRIV_DEBUG) || curthread->t_pre_sys) {
5197c478bd9Sstevel@tonic-gate 		for (priv = 0; priv < nprivs; priv++) {
5207c478bd9Sstevel@tonic-gate 			if (priv_ismember(&pset, priv)) {
5217c478bd9Sstevel@tonic-gate 				if (pfound != -1) {
5227c478bd9Sstevel@tonic-gate 					/* Multiple missing privs */
5237c478bd9Sstevel@tonic-gate 					priv_policy_errmsg(cr, PRIV_MULTIPLE,
5247c478bd9Sstevel@tonic-gate 					    msg);
5257c478bd9Sstevel@tonic-gate 					return (EACCES);
5267c478bd9Sstevel@tonic-gate 				}
5277c478bd9Sstevel@tonic-gate 				pfound = priv;
5287c478bd9Sstevel@tonic-gate 			}
5297c478bd9Sstevel@tonic-gate 		}
5307c478bd9Sstevel@tonic-gate 		ASSERT(pfound != -1);
5317c478bd9Sstevel@tonic-gate 		/* Just the one missing privilege */
5327c478bd9Sstevel@tonic-gate 		priv_policy_errmsg(cr, pfound, msg);
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate 	return (EACCES);
5367c478bd9Sstevel@tonic-gate }
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate /*
5397c478bd9Sstevel@tonic-gate  * Called when an operation requires that the caller be in the
5407c478bd9Sstevel@tonic-gate  * global zone, regardless of privilege.
5417c478bd9Sstevel@tonic-gate  */
5427c478bd9Sstevel@tonic-gate static int
5437c478bd9Sstevel@tonic-gate priv_policy_global(const cred_t *cr)
5447c478bd9Sstevel@tonic-gate {
5457c478bd9Sstevel@tonic-gate 	if (crgetzoneid(cr) == GLOBAL_ZONEID)
5467c478bd9Sstevel@tonic-gate 		return (0);	/* success */
5477c478bd9Sstevel@tonic-gate 
5487c478bd9Sstevel@tonic-gate 	if (priv_debug || (CR_FLAGS(cr) & PRIV_DEBUG) ||
5497c478bd9Sstevel@tonic-gate 	    curthread->t_pre_sys) {
5507c478bd9Sstevel@tonic-gate 		priv_policy_errmsg(cr, PRIV_GLOBAL, NULL);
5517c478bd9Sstevel@tonic-gate 	}
5527c478bd9Sstevel@tonic-gate 	return (EPERM);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /*
5567c478bd9Sstevel@tonic-gate  * Changing process priority
5577c478bd9Sstevel@tonic-gate  */
5587c478bd9Sstevel@tonic-gate int
5597c478bd9Sstevel@tonic-gate secpolicy_setpriority(const cred_t *cr)
5607c478bd9Sstevel@tonic-gate {
5617c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_PRIOCNTL, B_FALSE, EPERM, NULL));
5627c478bd9Sstevel@tonic-gate }
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate /*
5657c478bd9Sstevel@tonic-gate  * Binding to a privileged port, port must be specified in host byte
5667c478bd9Sstevel@tonic-gate  * order.
567*134a1f4eSCasper H.S. Dik  * When adding a new privilege which allows binding to currently privileged
568*134a1f4eSCasper H.S. Dik  * ports, then you MUST also allow processes with PRIV_NET_PRIVADDR bind
569*134a1f4eSCasper H.S. Dik  * to these ports because of backward compatibility.
5707c478bd9Sstevel@tonic-gate  */
5717c478bd9Sstevel@tonic-gate int
572ddf7fe95Scasper secpolicy_net_privaddr(const cred_t *cr, in_port_t port, int proto)
5737c478bd9Sstevel@tonic-gate {
574da6c28aaSamw 	char *reason;
575da6c28aaSamw 	int priv;
576da6c28aaSamw 
577da6c28aaSamw 	switch (port) {
578da6c28aaSamw 	case 137:
579da6c28aaSamw 	case 138:
580da6c28aaSamw 	case 139:
581da6c28aaSamw 	case 445:
582da6c28aaSamw 		/*
583*134a1f4eSCasper H.S. Dik 		 * NBT and SMB ports, these are normal privileged ports,
584*134a1f4eSCasper H.S. Dik 		 * allow bind only if the SYS_SMB or NET_PRIVADDR privilege
585*134a1f4eSCasper H.S. Dik 		 * is present.
586*134a1f4eSCasper H.S. Dik 		 * Try both, if neither is present return an error for
587*134a1f4eSCasper H.S. Dik 		 * priv SYS_SMB.
588da6c28aaSamw 		 */
589*134a1f4eSCasper H.S. Dik 		if (PRIV_POLICY_ONLY(cr, PRIV_NET_PRIVADDR, B_FALSE))
590*134a1f4eSCasper H.S. Dik 			priv = PRIV_NET_PRIVADDR;
591*134a1f4eSCasper H.S. Dik 		else
592da6c28aaSamw 			priv = PRIV_SYS_SMB;
593da6c28aaSamw 		reason = "NBT or SMB port";
594da6c28aaSamw 		break;
595da6c28aaSamw 
596da6c28aaSamw 	case 2049:
597da6c28aaSamw 	case 4045:
5987c478bd9Sstevel@tonic-gate 		/*
5997c478bd9Sstevel@tonic-gate 		 * NFS ports, these are extra privileged ports, allow bind
6007c478bd9Sstevel@tonic-gate 		 * only if the SYS_NFS privilege is present.
6017c478bd9Sstevel@tonic-gate 		 */
602da6c28aaSamw 		priv = PRIV_SYS_NFS;
603da6c28aaSamw 		reason = "NFS port";
604da6c28aaSamw 		break;
605da6c28aaSamw 
606da6c28aaSamw 	default:
607da6c28aaSamw 		priv = PRIV_NET_PRIVADDR;
608da6c28aaSamw 		reason = NULL;
609da6c28aaSamw 		break;
610da6c28aaSamw 
611da6c28aaSamw 	}
612da6c28aaSamw 
613ddf7fe95Scasper 	return (priv_policy_va(cr, priv, B_FALSE, EACCES, reason,
614ddf7fe95Scasper 	    KLPDARG_PORT, (int)proto, (int)port, KLPDARG_NOMORE));
6157c478bd9Sstevel@tonic-gate }
6167c478bd9Sstevel@tonic-gate 
6177c478bd9Sstevel@tonic-gate /*
61845916cd2Sjpk  * Binding to a multilevel port on a trusted (labeled) system.
61945916cd2Sjpk  */
62045916cd2Sjpk int
62145916cd2Sjpk secpolicy_net_bindmlp(const cred_t *cr)
62245916cd2Sjpk {
623ddf7fe95Scasper 	return (PRIV_POLICY(cr, PRIV_NET_BINDMLP, B_FALSE, EACCES, NULL));
62445916cd2Sjpk }
62545916cd2Sjpk 
62645916cd2Sjpk /*
62745916cd2Sjpk  * Allow a communication between a zone and an unlabeled host when their
62845916cd2Sjpk  * labels don't match.
62945916cd2Sjpk  */
63045916cd2Sjpk int
63145916cd2Sjpk secpolicy_net_mac_aware(const cred_t *cr)
63245916cd2Sjpk {
633ddf7fe95Scasper 	return (PRIV_POLICY(cr, PRIV_NET_MAC_AWARE, B_FALSE, EACCES, NULL));
63445916cd2Sjpk }
63545916cd2Sjpk 
63645916cd2Sjpk /*
6375d3b8cb7SBill Sommerfeld  * Allow a privileged process to transmit traffic without explicit labels
6385d3b8cb7SBill Sommerfeld  */
6395d3b8cb7SBill Sommerfeld int
6405d3b8cb7SBill Sommerfeld secpolicy_net_mac_implicit(const cred_t *cr)
6415d3b8cb7SBill Sommerfeld {
6425d3b8cb7SBill Sommerfeld 	return (PRIV_POLICY(cr, PRIV_NET_MAC_IMPLICIT, B_FALSE, EACCES, NULL));
6435d3b8cb7SBill Sommerfeld }
6445d3b8cb7SBill Sommerfeld 
6455d3b8cb7SBill Sommerfeld /*
6467c478bd9Sstevel@tonic-gate  * Common routine which determines whether a given credential can
6477c478bd9Sstevel@tonic-gate  * act on a given mount.
6487c478bd9Sstevel@tonic-gate  * When called through mount, the parameter needoptcheck is a pointer
6497c478bd9Sstevel@tonic-gate  * to a boolean variable which will be set to either true or false,
6507c478bd9Sstevel@tonic-gate  * depending on whether the mount policy should change the mount options.
6517c478bd9Sstevel@tonic-gate  * In all other cases, needoptcheck should be a NULL pointer.
6527c478bd9Sstevel@tonic-gate  */
6537c478bd9Sstevel@tonic-gate static int
6547c478bd9Sstevel@tonic-gate secpolicy_fs_common(cred_t *cr, vnode_t *mvp, const vfs_t *vfsp,
6557c478bd9Sstevel@tonic-gate     boolean_t *needoptcheck)
6567c478bd9Sstevel@tonic-gate {
6577c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
6587c478bd9Sstevel@tonic-gate 	boolean_t mounting = needoptcheck != NULL;
6597c478bd9Sstevel@tonic-gate 
6607c478bd9Sstevel@tonic-gate 	/*
6617c478bd9Sstevel@tonic-gate 	 * Short circuit the following cases:
6627c478bd9Sstevel@tonic-gate 	 *	vfsp == NULL or mvp == NULL (pure privilege check)
6637c478bd9Sstevel@tonic-gate 	 *	have all privileges - no further checks required
6647c478bd9Sstevel@tonic-gate 	 *	and no mount options need to be set.
6657c478bd9Sstevel@tonic-gate 	 */
6667c478bd9Sstevel@tonic-gate 	if (vfsp == NULL || mvp == NULL || HAS_ALLPRIVS(cr)) {
6677c478bd9Sstevel@tonic-gate 		if (mounting)
6687c478bd9Sstevel@tonic-gate 			*needoptcheck = B_FALSE;
6697c478bd9Sstevel@tonic-gate 
670ddf7fe95Scasper 		return (priv_policy_va(cr, PRIV_SYS_MOUNT, allzone, EPERM,
671ddf7fe95Scasper 		    NULL, KLPDARG_VNODE, mvp, (char *)NULL, KLPDARG_NOMORE));
6727c478bd9Sstevel@tonic-gate 	}
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	/*
6757c478bd9Sstevel@tonic-gate 	 * When operating on an existing mount (either we're not mounting
6767c478bd9Sstevel@tonic-gate 	 * or we're doing a remount and VFS_REMOUNT will be set), zones
6777c478bd9Sstevel@tonic-gate 	 * can operate only on mounts established by the zone itself.
6787c478bd9Sstevel@tonic-gate 	 */
6797c478bd9Sstevel@tonic-gate 	if (!mounting || (vfsp->vfs_flag & VFS_REMOUNT) != 0) {
6807c478bd9Sstevel@tonic-gate 		zoneid_t zoneid = crgetzoneid(cr);
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 		if (zoneid != GLOBAL_ZONEID &&
6837c478bd9Sstevel@tonic-gate 		    vfsp->vfs_zone->zone_id != zoneid) {
6847c478bd9Sstevel@tonic-gate 			return (EPERM);
6857c478bd9Sstevel@tonic-gate 		}
6867c478bd9Sstevel@tonic-gate 	}
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	if (mounting)
6897c478bd9Sstevel@tonic-gate 		*needoptcheck = B_TRUE;
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	/*
6927c478bd9Sstevel@tonic-gate 	 * Overlay mounts may hide important stuff; if you can't write to a
6937c478bd9Sstevel@tonic-gate 	 * mount point but would be able to mount on top of it, you can
6947c478bd9Sstevel@tonic-gate 	 * escalate your privileges.
6957c478bd9Sstevel@tonic-gate 	 * So we go about asking the same questions namefs does when it
6967c478bd9Sstevel@tonic-gate 	 * decides whether you can mount over a file or not but with the
6977c478bd9Sstevel@tonic-gate 	 * added restriction that you can only mount on top of a regular
6987c478bd9Sstevel@tonic-gate 	 * file or directory.
6997c478bd9Sstevel@tonic-gate 	 * If we have all the zone's privileges, we skip all other checks,
7007c478bd9Sstevel@tonic-gate 	 * or else we may actually get in trouble inside the automounter.
7017c478bd9Sstevel@tonic-gate 	 */
7027c478bd9Sstevel@tonic-gate 	if ((mvp->v_flag & VROOT) != 0 ||
7037c478bd9Sstevel@tonic-gate 	    (mvp->v_type != VDIR && mvp->v_type != VREG) ||
7047c478bd9Sstevel@tonic-gate 	    HAS_ALLZONEPRIVS(cr)) {
7057c478bd9Sstevel@tonic-gate 		allzone = B_TRUE;
7067c478bd9Sstevel@tonic-gate 	} else {
7077c478bd9Sstevel@tonic-gate 		vattr_t va;
7087c478bd9Sstevel@tonic-gate 		int err;
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 		va.va_mask = AT_UID|AT_MODE;
711da6c28aaSamw 		err = VOP_GETATTR(mvp, &va, 0, cr, NULL);
7127c478bd9Sstevel@tonic-gate 		if (err != 0)
7137c478bd9Sstevel@tonic-gate 			return (err);
7147c478bd9Sstevel@tonic-gate 
7157c478bd9Sstevel@tonic-gate 		if ((err = secpolicy_vnode_owner(cr, va.va_uid)) != 0)
7167c478bd9Sstevel@tonic-gate 			return (err);
7177c478bd9Sstevel@tonic-gate 
718*134a1f4eSCasper H.S. Dik 		if (secpolicy_vnode_access2(cr, mvp, va.va_uid, va.va_mode,
719*134a1f4eSCasper H.S. Dik 		    VWRITE) != 0) {
7207c478bd9Sstevel@tonic-gate 			return (EACCES);
7217c478bd9Sstevel@tonic-gate 		}
7227c478bd9Sstevel@tonic-gate 	}
723ddf7fe95Scasper 	return (priv_policy_va(cr, PRIV_SYS_MOUNT, allzone, EPERM,
724ddf7fe95Scasper 	    NULL, KLPDARG_VNODE, mvp, (char *)NULL, KLPDARG_NOMORE));
7257c478bd9Sstevel@tonic-gate }
7267c478bd9Sstevel@tonic-gate 
727ecd6cf80Smarks void
728ecd6cf80Smarks secpolicy_fs_mount_clearopts(cred_t *cr, struct vfs *vfsp)
7297c478bd9Sstevel@tonic-gate {
7307c478bd9Sstevel@tonic-gate 	boolean_t amsuper = HAS_ALLZONEPRIVS(cr);
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	/*
733ecd6cf80Smarks 	 * check; if we don't have either "nosuid" or
7347c478bd9Sstevel@tonic-gate 	 * both "nosetuid" and "nodevices", then we add
7357c478bd9Sstevel@tonic-gate 	 * "nosuid"; this depends on how the current
7367c478bd9Sstevel@tonic-gate 	 * implementation works (it first checks nosuid).  In a
7377c478bd9Sstevel@tonic-gate 	 * zone, a user with all zone privileges can mount with
7387c478bd9Sstevel@tonic-gate 	 * "setuid" but never with "devices".
7397c478bd9Sstevel@tonic-gate 	 */
7407c478bd9Sstevel@tonic-gate 	if (!vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL) &&
7417c478bd9Sstevel@tonic-gate 	    (!vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL) ||
7427c478bd9Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))) {
7437c478bd9Sstevel@tonic-gate 		if (crgetzoneid(cr) == GLOBAL_ZONEID || !amsuper)
7447c478bd9Sstevel@tonic-gate 			vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
7457c478bd9Sstevel@tonic-gate 		else
7467c478bd9Sstevel@tonic-gate 			vfs_setmntopt(vfsp, MNTOPT_NODEVICES, NULL, 0);
7477c478bd9Sstevel@tonic-gate 	}
7487c478bd9Sstevel@tonic-gate 	/*
7497c478bd9Sstevel@tonic-gate 	 * If we're not the local super user, we set the "restrict"
7507c478bd9Sstevel@tonic-gate 	 * option to indicate to automountd that this mount should
7517c478bd9Sstevel@tonic-gate 	 * be handled with care.
7527c478bd9Sstevel@tonic-gate 	 */
7537c478bd9Sstevel@tonic-gate 	if (!amsuper)
7547c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RESTRICT, NULL, 0);
7557c478bd9Sstevel@tonic-gate 
7567c478bd9Sstevel@tonic-gate }
757ecd6cf80Smarks 
758ecd6cf80Smarks extern vnode_t *rootvp;
759ecd6cf80Smarks extern vfs_t *rootvfs;
760ecd6cf80Smarks 
761ecd6cf80Smarks int
762ecd6cf80Smarks secpolicy_fs_mount(cred_t *cr, vnode_t *mvp, struct vfs *vfsp)
763ecd6cf80Smarks {
764ecd6cf80Smarks 	boolean_t needoptchk;
765ecd6cf80Smarks 	int error;
766ecd6cf80Smarks 
767ecd6cf80Smarks 	/*
768ecd6cf80Smarks 	 * If it's a remount, get the underlying mount point,
769ecd6cf80Smarks 	 * except for the root where we use the rootvp.
770ecd6cf80Smarks 	 */
771ecd6cf80Smarks 	if ((vfsp->vfs_flag & VFS_REMOUNT) != 0) {
772ecd6cf80Smarks 		if (vfsp == rootvfs)
773ecd6cf80Smarks 			mvp = rootvp;
774ecd6cf80Smarks 		else
775ecd6cf80Smarks 			mvp = vfsp->vfs_vnodecovered;
776ecd6cf80Smarks 	}
777ecd6cf80Smarks 
778ecd6cf80Smarks 	error = secpolicy_fs_common(cr, mvp, vfsp, &needoptchk);
779ecd6cf80Smarks 
780ecd6cf80Smarks 	if (error == 0 && needoptchk) {
781ecd6cf80Smarks 		secpolicy_fs_mount_clearopts(cr, vfsp);
782ecd6cf80Smarks 	}
783ecd6cf80Smarks 
7847c478bd9Sstevel@tonic-gate 	return (error);
7857c478bd9Sstevel@tonic-gate }
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate /*
7887c478bd9Sstevel@tonic-gate  * Does the policy computations for "ownership" of a mount;
7897c478bd9Sstevel@tonic-gate  * here ownership is defined as the ability to "mount"
7907c478bd9Sstevel@tonic-gate  * the filesystem originally.  The rootvfs doesn't cover any
7917c478bd9Sstevel@tonic-gate  * vnodes; we attribute its ownership to the rootvp.
7927c478bd9Sstevel@tonic-gate  */
7937c478bd9Sstevel@tonic-gate static int
7947c478bd9Sstevel@tonic-gate secpolicy_fs_owner(cred_t *cr, const struct vfs *vfsp)
7957c478bd9Sstevel@tonic-gate {
7967c478bd9Sstevel@tonic-gate 	vnode_t *mvp;
7977c478bd9Sstevel@tonic-gate 
7987c478bd9Sstevel@tonic-gate 	if (vfsp == NULL)
7997c478bd9Sstevel@tonic-gate 		mvp = NULL;
8007c478bd9Sstevel@tonic-gate 	else if (vfsp == rootvfs)
8017c478bd9Sstevel@tonic-gate 		mvp = rootvp;
8027c478bd9Sstevel@tonic-gate 	else
8037c478bd9Sstevel@tonic-gate 		mvp = vfsp->vfs_vnodecovered;
8047c478bd9Sstevel@tonic-gate 
8057c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_common(cr, mvp, vfsp, NULL));
8067c478bd9Sstevel@tonic-gate }
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate int
8097c478bd9Sstevel@tonic-gate secpolicy_fs_unmount(cred_t *cr, struct vfs *vfsp)
8107c478bd9Sstevel@tonic-gate {
8117c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_owner(cr, vfsp));
8127c478bd9Sstevel@tonic-gate }
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate /*
8157c478bd9Sstevel@tonic-gate  * Quotas are a resource, but if one has the ability to mount a filesystem, he
8167c478bd9Sstevel@tonic-gate  * should be able to modify quotas on it.
8177c478bd9Sstevel@tonic-gate  */
8187c478bd9Sstevel@tonic-gate int
8197c478bd9Sstevel@tonic-gate secpolicy_fs_quota(const cred_t *cr, const vfs_t *vfsp)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
8227c478bd9Sstevel@tonic-gate }
8237c478bd9Sstevel@tonic-gate 
8247c478bd9Sstevel@tonic-gate /*
8257c478bd9Sstevel@tonic-gate  * Exceeding minfree: also a per-mount resource constraint.
8267c478bd9Sstevel@tonic-gate  */
8277c478bd9Sstevel@tonic-gate int
8287c478bd9Sstevel@tonic-gate secpolicy_fs_minfree(const cred_t *cr, const vfs_t *vfsp)
8297c478bd9Sstevel@tonic-gate {
8307c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
8317c478bd9Sstevel@tonic-gate }
8327c478bd9Sstevel@tonic-gate 
8337c478bd9Sstevel@tonic-gate int
8347c478bd9Sstevel@tonic-gate secpolicy_fs_config(const cred_t *cr, const vfs_t *vfsp)
8357c478bd9Sstevel@tonic-gate {
8367c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
8377c478bd9Sstevel@tonic-gate }
8387c478bd9Sstevel@tonic-gate 
8397c478bd9Sstevel@tonic-gate /* ARGSUSED */
8407c478bd9Sstevel@tonic-gate int
8417c478bd9Sstevel@tonic-gate secpolicy_fs_linkdir(const cred_t *cr, const vfs_t *vfsp)
8427c478bd9Sstevel@tonic-gate {
8437c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_LINKDIR, B_FALSE, EPERM, NULL));
8447c478bd9Sstevel@tonic-gate }
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate /*
8477c478bd9Sstevel@tonic-gate  * Name:        secpolicy_vnode_access()
8487c478bd9Sstevel@tonic-gate  *
8497c478bd9Sstevel@tonic-gate  * Parameters:  Process credential
8507c478bd9Sstevel@tonic-gate  *		vnode
8517c478bd9Sstevel@tonic-gate  *		uid of owner of vnode
8527c478bd9Sstevel@tonic-gate  *		permission bits not granted to the caller when examining
8537c478bd9Sstevel@tonic-gate  *		file mode bits (i.e., when a process wants to open a
8547c478bd9Sstevel@tonic-gate  *		mode 444 file for VREAD|VWRITE, this function should be
8557c478bd9Sstevel@tonic-gate  *		called only with a VWRITE argument).
8567c478bd9Sstevel@tonic-gate  *
8577c478bd9Sstevel@tonic-gate  * Normal:      Verifies that cred has the appropriate privileges to
8587c478bd9Sstevel@tonic-gate  *              override the mode bits that were denied.
8597c478bd9Sstevel@tonic-gate  *
8607c478bd9Sstevel@tonic-gate  * Override:    file_dac_execute - if VEXEC bit was denied and vnode is
8617c478bd9Sstevel@tonic-gate  *                      not a directory.
8627c478bd9Sstevel@tonic-gate  *              file_dac_read - if VREAD bit was denied.
8637c478bd9Sstevel@tonic-gate  *              file_dac_search - if VEXEC bit was denied and vnode is
8647c478bd9Sstevel@tonic-gate  *                      a directory.
8657c478bd9Sstevel@tonic-gate  *              file_dac_write - if VWRITE bit was denied.
8667c478bd9Sstevel@tonic-gate  *
8677c478bd9Sstevel@tonic-gate  *		Root owned files are special cased to protect system
8687c478bd9Sstevel@tonic-gate  *		configuration files and such.
8697c478bd9Sstevel@tonic-gate  *
8707c478bd9Sstevel@tonic-gate  * Output:      EACCES - if privilege check fails.
8717c478bd9Sstevel@tonic-gate  */
8727c478bd9Sstevel@tonic-gate 
8737c478bd9Sstevel@tonic-gate int
8747c478bd9Sstevel@tonic-gate secpolicy_vnode_access(const cred_t *cr, vnode_t *vp, uid_t owner, mode_t mode)
8757c478bd9Sstevel@tonic-gate {
876ddf7fe95Scasper 	if ((mode & VREAD) && priv_policy_va(cr, PRIV_FILE_DAC_READ, B_FALSE,
877ddf7fe95Scasper 	    EACCES, NULL, KLPDARG_VNODE, vp, (char *)NULL,
878ddf7fe95Scasper 	    KLPDARG_NOMORE) != 0) {
8797c478bd9Sstevel@tonic-gate 		return (EACCES);
880ddf7fe95Scasper 	}
8817c478bd9Sstevel@tonic-gate 
8827c478bd9Sstevel@tonic-gate 	if (mode & VWRITE) {
8837c478bd9Sstevel@tonic-gate 		boolean_t allzone;
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 		if (owner == 0 && cr->cr_uid != 0)
8867c478bd9Sstevel@tonic-gate 			allzone = B_TRUE;
8877c478bd9Sstevel@tonic-gate 		else
8887c478bd9Sstevel@tonic-gate 			allzone = B_FALSE;
889ddf7fe95Scasper 		if (priv_policy_va(cr, PRIV_FILE_DAC_WRITE, allzone, EACCES,
890ddf7fe95Scasper 		    NULL, KLPDARG_VNODE, vp, (char *)NULL,
891ddf7fe95Scasper 		    KLPDARG_NOMORE) != 0) {
8927c478bd9Sstevel@tonic-gate 			return (EACCES);
8937c478bd9Sstevel@tonic-gate 		}
894ddf7fe95Scasper 	}
8957c478bd9Sstevel@tonic-gate 
8967c478bd9Sstevel@tonic-gate 	if (mode & VEXEC) {
8977c478bd9Sstevel@tonic-gate 		/*
8987c478bd9Sstevel@tonic-gate 		 * Directories use file_dac_search to override the execute bit.
8997c478bd9Sstevel@tonic-gate 		 */
900ddf7fe95Scasper 		int p = vp->v_type == VDIR ? PRIV_FILE_DAC_SEARCH :
901ddf7fe95Scasper 		    PRIV_FILE_DAC_EXECUTE;
9027c478bd9Sstevel@tonic-gate 
903ddf7fe95Scasper 		return (priv_policy_va(cr, p, B_FALSE, EACCES, NULL,
904ddf7fe95Scasper 		    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
9057c478bd9Sstevel@tonic-gate 	}
9067c478bd9Sstevel@tonic-gate 	return (0);
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate /*
910*134a1f4eSCasper H.S. Dik  * Like secpolicy_vnode_access() but we get the actual wanted mode and the
911*134a1f4eSCasper H.S. Dik  * current mode of the file, not the missing bits.
912*134a1f4eSCasper H.S. Dik  */
913*134a1f4eSCasper H.S. Dik int
914*134a1f4eSCasper H.S. Dik secpolicy_vnode_access2(const cred_t *cr, vnode_t *vp, uid_t owner,
915*134a1f4eSCasper H.S. Dik     mode_t curmode, mode_t wantmode)
916*134a1f4eSCasper H.S. Dik {
917*134a1f4eSCasper H.S. Dik 	mode_t mode;
918*134a1f4eSCasper H.S. Dik 
919*134a1f4eSCasper H.S. Dik 	/* Inline the basic privileges tests. */
920*134a1f4eSCasper H.S. Dik 	if ((wantmode & VREAD) &&
921*134a1f4eSCasper H.S. Dik 	    !PRIV_ISASSERT(&CR_OEPRIV(cr), PRIV_FILE_READ) &&
922*134a1f4eSCasper H.S. Dik 	    priv_policy_va(cr, PRIV_FILE_READ, B_FALSE, EACCES, NULL,
923*134a1f4eSCasper H.S. Dik 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE) != 0) {
924*134a1f4eSCasper H.S. Dik 		return (EACCES);
925*134a1f4eSCasper H.S. Dik 	}
926*134a1f4eSCasper H.S. Dik 
927*134a1f4eSCasper H.S. Dik 	if ((wantmode & VWRITE) &&
928*134a1f4eSCasper H.S. Dik 	    !PRIV_ISASSERT(&CR_OEPRIV(cr), PRIV_FILE_WRITE) &&
929*134a1f4eSCasper H.S. Dik 	    priv_policy_va(cr, PRIV_FILE_WRITE, B_FALSE, EACCES, NULL,
930*134a1f4eSCasper H.S. Dik 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE) != 0) {
931*134a1f4eSCasper H.S. Dik 		return (EACCES);
932*134a1f4eSCasper H.S. Dik 	}
933*134a1f4eSCasper H.S. Dik 
934*134a1f4eSCasper H.S. Dik 	mode = ~curmode & wantmode;
935*134a1f4eSCasper H.S. Dik 
936*134a1f4eSCasper H.S. Dik 	if (mode == 0)
937*134a1f4eSCasper H.S. Dik 		return (0);
938*134a1f4eSCasper H.S. Dik 
939*134a1f4eSCasper H.S. Dik 	if ((mode & VREAD) && priv_policy_va(cr, PRIV_FILE_DAC_READ, B_FALSE,
940*134a1f4eSCasper H.S. Dik 	    EACCES, NULL, KLPDARG_VNODE, vp, (char *)NULL,
941*134a1f4eSCasper H.S. Dik 	    KLPDARG_NOMORE) != 0) {
942*134a1f4eSCasper H.S. Dik 		return (EACCES);
943*134a1f4eSCasper H.S. Dik 	}
944*134a1f4eSCasper H.S. Dik 
945*134a1f4eSCasper H.S. Dik 	if (mode & VWRITE) {
946*134a1f4eSCasper H.S. Dik 		boolean_t allzone;
947*134a1f4eSCasper H.S. Dik 
948*134a1f4eSCasper H.S. Dik 		if (owner == 0 && cr->cr_uid != 0)
949*134a1f4eSCasper H.S. Dik 			allzone = B_TRUE;
950*134a1f4eSCasper H.S. Dik 		else
951*134a1f4eSCasper H.S. Dik 			allzone = B_FALSE;
952*134a1f4eSCasper H.S. Dik 		if (priv_policy_va(cr, PRIV_FILE_DAC_WRITE, allzone, EACCES,
953*134a1f4eSCasper H.S. Dik 		    NULL, KLPDARG_VNODE, vp, (char *)NULL,
954*134a1f4eSCasper H.S. Dik 		    KLPDARG_NOMORE) != 0) {
955*134a1f4eSCasper H.S. Dik 			return (EACCES);
956*134a1f4eSCasper H.S. Dik 		}
957*134a1f4eSCasper H.S. Dik 	}
958*134a1f4eSCasper H.S. Dik 
959*134a1f4eSCasper H.S. Dik 	if (mode & VEXEC) {
960*134a1f4eSCasper H.S. Dik 		/*
961*134a1f4eSCasper H.S. Dik 		 * Directories use file_dac_search to override the execute bit.
962*134a1f4eSCasper H.S. Dik 		 */
963*134a1f4eSCasper H.S. Dik 		int p = vp->v_type == VDIR ? PRIV_FILE_DAC_SEARCH :
964*134a1f4eSCasper H.S. Dik 		    PRIV_FILE_DAC_EXECUTE;
965*134a1f4eSCasper H.S. Dik 
966*134a1f4eSCasper H.S. Dik 		return (priv_policy_va(cr, p, B_FALSE, EACCES, NULL,
967*134a1f4eSCasper H.S. Dik 		    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
968*134a1f4eSCasper H.S. Dik 	}
969*134a1f4eSCasper H.S. Dik 	return (0);
970*134a1f4eSCasper H.S. Dik }
971*134a1f4eSCasper H.S. Dik 
972*134a1f4eSCasper H.S. Dik /*
973*134a1f4eSCasper H.S. Dik  * This is a special routine for ZFS; it is used to determine whether
974*134a1f4eSCasper H.S. Dik  * any of the privileges in effect allow any form of access to the
975*134a1f4eSCasper H.S. Dik  * file.  There's no reason to audit this or any reason to record
976*134a1f4eSCasper H.S. Dik  * this.  More work is needed to do the "KPLD" stuff.
977*134a1f4eSCasper H.S. Dik  */
978*134a1f4eSCasper H.S. Dik int
979*134a1f4eSCasper H.S. Dik secpolicy_vnode_any_access(const cred_t *cr, vnode_t *vp, uid_t owner)
980*134a1f4eSCasper H.S. Dik {
981*134a1f4eSCasper H.S. Dik 	static int privs[] = {
982*134a1f4eSCasper H.S. Dik 	    PRIV_FILE_OWNER,
983*134a1f4eSCasper H.S. Dik 	    PRIV_FILE_DAC_READ,
984*134a1f4eSCasper H.S. Dik 	    PRIV_FILE_DAC_WRITE,
985*134a1f4eSCasper H.S. Dik 	    PRIV_FILE_DAC_EXECUTE,
986*134a1f4eSCasper H.S. Dik 	    PRIV_FILE_DAC_SEARCH,
987*134a1f4eSCasper H.S. Dik 	};
988*134a1f4eSCasper H.S. Dik 	int i;
989*134a1f4eSCasper H.S. Dik 
990*134a1f4eSCasper H.S. Dik 	/* Same as secpolicy_vnode_setdac */
991*134a1f4eSCasper H.S. Dik 	if (owner == cr->cr_uid)
992*134a1f4eSCasper H.S. Dik 		return (0);
993*134a1f4eSCasper H.S. Dik 
994*134a1f4eSCasper H.S. Dik 	for (i = 0; i < sizeof (privs)/sizeof (int); i++) {
995*134a1f4eSCasper H.S. Dik 		boolean_t allzone = B_FALSE;
996*134a1f4eSCasper H.S. Dik 		int priv;
997*134a1f4eSCasper H.S. Dik 
998*134a1f4eSCasper H.S. Dik 		switch (priv = privs[i]) {
999*134a1f4eSCasper H.S. Dik 		case PRIV_FILE_DAC_EXECUTE:
1000*134a1f4eSCasper H.S. Dik 			if (vp->v_type == VDIR)
1001*134a1f4eSCasper H.S. Dik 				continue;
1002*134a1f4eSCasper H.S. Dik 			break;
1003*134a1f4eSCasper H.S. Dik 		case PRIV_FILE_DAC_SEARCH:
1004*134a1f4eSCasper H.S. Dik 			if (vp->v_type != VDIR)
1005*134a1f4eSCasper H.S. Dik 				continue;
1006*134a1f4eSCasper H.S. Dik 			break;
1007*134a1f4eSCasper H.S. Dik 		case PRIV_FILE_DAC_WRITE:
1008*134a1f4eSCasper H.S. Dik 		case PRIV_FILE_OWNER:
1009*134a1f4eSCasper H.S. Dik 			/* We know here that if owner == 0, that cr_uid != 0 */
1010*134a1f4eSCasper H.S. Dik 			allzone = owner == 0;
1011*134a1f4eSCasper H.S. Dik 			break;
1012*134a1f4eSCasper H.S. Dik 		}
1013*134a1f4eSCasper H.S. Dik 		if (PRIV_POLICY_CHOICE(cr, priv, allzone))
1014*134a1f4eSCasper H.S. Dik 			return (0);
1015*134a1f4eSCasper H.S. Dik 	}
1016*134a1f4eSCasper H.S. Dik 	return (EPERM);
1017*134a1f4eSCasper H.S. Dik }
1018*134a1f4eSCasper H.S. Dik 
1019*134a1f4eSCasper H.S. Dik /*
10207c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_setid_modify()
10217c478bd9Sstevel@tonic-gate  *
10227c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can set the file setid flags.
10237c478bd9Sstevel@tonic-gate  *
10247c478bd9Sstevel@tonic-gate  * Output:	EPERM - if not privileged.
10257c478bd9Sstevel@tonic-gate  */
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate static int
10287c478bd9Sstevel@tonic-gate secpolicy_vnode_setid_modify(const cred_t *cr, uid_t owner)
10297c478bd9Sstevel@tonic-gate {
10307c478bd9Sstevel@tonic-gate 	/* If changing to suid root, must have all zone privs */
10317c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_TRUE;
10327c478bd9Sstevel@tonic-gate 
10337c478bd9Sstevel@tonic-gate 	if (owner != 0) {
10347c478bd9Sstevel@tonic-gate 		if (owner == cr->cr_uid)
10357c478bd9Sstevel@tonic-gate 			return (0);
10367c478bd9Sstevel@tonic-gate 		allzone = B_FALSE;
10377c478bd9Sstevel@tonic-gate 	}
10387c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_SETID, allzone, EPERM, NULL));
10397c478bd9Sstevel@tonic-gate }
10407c478bd9Sstevel@tonic-gate 
10417c478bd9Sstevel@tonic-gate /*
10427c478bd9Sstevel@tonic-gate  * Are we allowed to retain the set-uid/set-gid bits when
10437c478bd9Sstevel@tonic-gate  * changing ownership or when writing to a file?
10447c478bd9Sstevel@tonic-gate  * "issuid" should be true when set-uid; only in that case
10457c478bd9Sstevel@tonic-gate  * root ownership is checked (setgid is assumed).
10467c478bd9Sstevel@tonic-gate  */
10477c478bd9Sstevel@tonic-gate int
10487c478bd9Sstevel@tonic-gate secpolicy_vnode_setid_retain(const cred_t *cred, boolean_t issuidroot)
10497c478bd9Sstevel@tonic-gate {
10507c478bd9Sstevel@tonic-gate 	if (issuidroot && !HAS_ALLZONEPRIVS(cred))
10517c478bd9Sstevel@tonic-gate 		return (EPERM);
10527c478bd9Sstevel@tonic-gate 
10537c478bd9Sstevel@tonic-gate 	return (!PRIV_POLICY_CHOICE(cred, PRIV_FILE_SETID, B_FALSE));
10547c478bd9Sstevel@tonic-gate }
10557c478bd9Sstevel@tonic-gate 
10567c478bd9Sstevel@tonic-gate /*
10577c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_setids_setgids()
10587c478bd9Sstevel@tonic-gate  *
10597c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can set the file setgid flag.
10607c478bd9Sstevel@tonic-gate  *
10617c478bd9Sstevel@tonic-gate  * Output:	EPERM - if not privileged
10627c478bd9Sstevel@tonic-gate  */
10637c478bd9Sstevel@tonic-gate 
10647c478bd9Sstevel@tonic-gate int
10657c478bd9Sstevel@tonic-gate secpolicy_vnode_setids_setgids(const cred_t *cred, gid_t gid)
10667c478bd9Sstevel@tonic-gate {
10677c478bd9Sstevel@tonic-gate 	if (!groupmember(gid, cred))
10687c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cred, PRIV_FILE_SETID, B_FALSE, EPERM,
10697c478bd9Sstevel@tonic-gate 		    NULL));
10707c478bd9Sstevel@tonic-gate 	return (0);
10717c478bd9Sstevel@tonic-gate }
10727c478bd9Sstevel@tonic-gate 
10737c478bd9Sstevel@tonic-gate /*
107447def0dcSMark Shellenbaum  * Name:	secpolicy_vnode_chown
107547def0dcSMark Shellenbaum  *
107647def0dcSMark Shellenbaum  * Normal:	Determine if subject can chown owner of a file.
107747def0dcSMark Shellenbaum  *
107847def0dcSMark Shellenbaum  * Output:	EPERM - if access denied
10797c478bd9Sstevel@tonic-gate  */
108047def0dcSMark Shellenbaum 
10817c478bd9Sstevel@tonic-gate int
1082e02bc683SMark Shellenbaum secpolicy_vnode_chown(const cred_t *cred, uid_t owner)
10837c478bd9Sstevel@tonic-gate {
1084e02bc683SMark Shellenbaum 	boolean_t is_owner = (owner == crgetuid(cred));
1085e02bc683SMark Shellenbaum 	boolean_t allzone = B_FALSE;
1086e02bc683SMark Shellenbaum 	int priv;
1087e02bc683SMark Shellenbaum 
1088e02bc683SMark Shellenbaum 	if (!is_owner) {
1089e02bc683SMark Shellenbaum 		allzone = (owner == 0);
1090e02bc683SMark Shellenbaum 		priv = PRIV_FILE_CHOWN;
1091e02bc683SMark Shellenbaum 	} else {
1092e02bc683SMark Shellenbaum 		priv = HAS_PRIVILEGE(cred, PRIV_FILE_CHOWN) ?
1093e02bc683SMark Shellenbaum 		    PRIV_FILE_CHOWN : PRIV_FILE_CHOWN_SELF;
1094e02bc683SMark Shellenbaum 	}
1095e02bc683SMark Shellenbaum 
1096e02bc683SMark Shellenbaum 	return (PRIV_POLICY(cred, priv, allzone, EPERM, NULL));
109747def0dcSMark Shellenbaum }
109847def0dcSMark Shellenbaum 
109947def0dcSMark Shellenbaum /*
110047def0dcSMark Shellenbaum  * Name:	secpolicy_vnode_create_gid
110147def0dcSMark Shellenbaum  *
110247def0dcSMark Shellenbaum  * Normal:	Determine if subject can change group ownership of a file.
110347def0dcSMark Shellenbaum  *
110447def0dcSMark Shellenbaum  * Output:	EPERM - if access denied
110547def0dcSMark Shellenbaum  */
110647def0dcSMark Shellenbaum int
110747def0dcSMark Shellenbaum secpolicy_vnode_create_gid(const cred_t *cred)
110847def0dcSMark Shellenbaum {
1109e02bc683SMark Shellenbaum 	if (HAS_PRIVILEGE(cred, PRIV_FILE_CHOWN))
1110e02bc683SMark Shellenbaum 		return (PRIV_POLICY(cred, PRIV_FILE_CHOWN, B_FALSE, EPERM,
1111e02bc683SMark Shellenbaum 		    NULL));
1112e02bc683SMark Shellenbaum 	else
1113e02bc683SMark Shellenbaum 		return (PRIV_POLICY(cred, PRIV_FILE_CHOWN_SELF, B_FALSE, EPERM,
1114e02bc683SMark Shellenbaum 		    NULL));
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate /*
11187c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_utime_modify()
11197c478bd9Sstevel@tonic-gate  *
11207c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can modify the utime on a file.
11217c478bd9Sstevel@tonic-gate  *
11227c478bd9Sstevel@tonic-gate  * Output:	EPERM - if access denied.
11237c478bd9Sstevel@tonic-gate  */
11247c478bd9Sstevel@tonic-gate 
11257c478bd9Sstevel@tonic-gate static int
11267c478bd9Sstevel@tonic-gate secpolicy_vnode_utime_modify(const cred_t *cred)
11277c478bd9Sstevel@tonic-gate {
11287c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_FILE_OWNER, B_FALSE, EPERM,
11297c478bd9Sstevel@tonic-gate 	    "modify file times"));
11307c478bd9Sstevel@tonic-gate }
11317c478bd9Sstevel@tonic-gate 
11327c478bd9Sstevel@tonic-gate 
11337c478bd9Sstevel@tonic-gate /*
11347c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_setdac()
11357c478bd9Sstevel@tonic-gate  *
11367c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can modify the mode of a file.
11377c478bd9Sstevel@tonic-gate  *		allzone privilege needed when modifying root owned object.
11387c478bd9Sstevel@tonic-gate  *
11397c478bd9Sstevel@tonic-gate  * Output:	EPERM - if access denied.
11407c478bd9Sstevel@tonic-gate  */
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate int
11437c478bd9Sstevel@tonic-gate secpolicy_vnode_setdac(const cred_t *cred, uid_t owner)
11447c478bd9Sstevel@tonic-gate {
11457c478bd9Sstevel@tonic-gate 	if (owner == cred->cr_uid)
11467c478bd9Sstevel@tonic-gate 		return (0);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_FILE_OWNER, owner == 0, EPERM, NULL));
11497c478bd9Sstevel@tonic-gate }
11507c478bd9Sstevel@tonic-gate /*
11517c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_stky_modify()
11527c478bd9Sstevel@tonic-gate  *
11537c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can make a file a "sticky".
11547c478bd9Sstevel@tonic-gate  *
11557c478bd9Sstevel@tonic-gate  * Output:	EPERM - if access denied.
11567c478bd9Sstevel@tonic-gate  */
11577c478bd9Sstevel@tonic-gate 
11587c478bd9Sstevel@tonic-gate int
11597c478bd9Sstevel@tonic-gate secpolicy_vnode_stky_modify(const cred_t *cred)
11607c478bd9Sstevel@tonic-gate {
11617c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_SYS_CONFIG, B_FALSE, EPERM,
11627c478bd9Sstevel@tonic-gate 	    "set file sticky"));
11637c478bd9Sstevel@tonic-gate }
11647c478bd9Sstevel@tonic-gate 
11657c478bd9Sstevel@tonic-gate /*
11667c478bd9Sstevel@tonic-gate  * Policy determines whether we can remove an entry from a directory,
11677c478bd9Sstevel@tonic-gate  * regardless of permission bits.
11687c478bd9Sstevel@tonic-gate  */
11697c478bd9Sstevel@tonic-gate int
11707c478bd9Sstevel@tonic-gate secpolicy_vnode_remove(const cred_t *cr)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_OWNER, B_FALSE, EACCES,
11737c478bd9Sstevel@tonic-gate 	    "sticky directory"));
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate int
11777c478bd9Sstevel@tonic-gate secpolicy_vnode_owner(const cred_t *cr, uid_t owner)
11787c478bd9Sstevel@tonic-gate {
11797c478bd9Sstevel@tonic-gate 	boolean_t allzone = (owner == 0);
11807c478bd9Sstevel@tonic-gate 
11817c478bd9Sstevel@tonic-gate 	if (owner == cr->cr_uid)
11827c478bd9Sstevel@tonic-gate 		return (0);
11837c478bd9Sstevel@tonic-gate 
11847c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_OWNER, allzone, EPERM, NULL));
11857c478bd9Sstevel@tonic-gate }
11867c478bd9Sstevel@tonic-gate 
118713f9f30eSmarks void
118813f9f30eSmarks secpolicy_setid_clear(vattr_t *vap, cred_t *cr)
118913f9f30eSmarks {
119013f9f30eSmarks 	if ((vap->va_mode & (S_ISUID | S_ISGID)) != 0 &&
119113f9f30eSmarks 	    secpolicy_vnode_setid_retain(cr,
119213f9f30eSmarks 	    (vap->va_mode & S_ISUID) != 0 &&
119313f9f30eSmarks 	    (vap->va_mask & AT_UID) != 0 && vap->va_uid == 0) != 0) {
119413f9f30eSmarks 		vap->va_mask |= AT_MODE;
119513f9f30eSmarks 		vap->va_mode &= ~(S_ISUID|S_ISGID);
119613f9f30eSmarks 	}
119713f9f30eSmarks }
119813f9f30eSmarks 
1199f92daba9Smarks int
1200f92daba9Smarks secpolicy_setid_setsticky_clear(vnode_t *vp, vattr_t *vap, const vattr_t *ovap,
1201f92daba9Smarks     cred_t *cr)
1202f92daba9Smarks {
1203f92daba9Smarks 	int error;
1204f92daba9Smarks 
1205f92daba9Smarks 	if ((vap->va_mode & S_ISUID) != 0 &&
1206f92daba9Smarks 	    (error = secpolicy_vnode_setid_modify(cr,
1207f92daba9Smarks 	    ovap->va_uid)) != 0) {
1208f92daba9Smarks 		return (error);
1209f92daba9Smarks 	}
1210f92daba9Smarks 
1211f92daba9Smarks 	/*
1212f92daba9Smarks 	 * Check privilege if attempting to set the
1213f92daba9Smarks 	 * sticky bit on a non-directory.
1214f92daba9Smarks 	 */
1215f92daba9Smarks 	if (vp->v_type != VDIR && (vap->va_mode & S_ISVTX) != 0 &&
1216f92daba9Smarks 	    secpolicy_vnode_stky_modify(cr) != 0) {
1217f92daba9Smarks 		vap->va_mode &= ~S_ISVTX;
1218f92daba9Smarks 	}
1219f92daba9Smarks 
1220f92daba9Smarks 	/*
1221f92daba9Smarks 	 * Check for privilege if attempting to set the
1222f92daba9Smarks 	 * group-id bit.
1223f92daba9Smarks 	 */
1224f92daba9Smarks 	if ((vap->va_mode & S_ISGID) != 0 &&
1225f92daba9Smarks 	    secpolicy_vnode_setids_setgids(cr, ovap->va_gid) != 0) {
1226f92daba9Smarks 		vap->va_mode &= ~S_ISGID;
1227f92daba9Smarks 	}
1228f92daba9Smarks 
1229f92daba9Smarks 	return (0);
1230f92daba9Smarks }
1231f92daba9Smarks 
1232da6c28aaSamw #define	ATTR_FLAG_PRIV(attr, value, cr)	\
1233da6c28aaSamw 	PRIV_POLICY(cr, value ? PRIV_FILE_FLAG_SET : PRIV_ALL, \
1234da6c28aaSamw 	B_FALSE, EPERM, NULL)
1235da6c28aaSamw 
1236da6c28aaSamw /*
1237da6c28aaSamw  * Check privileges for setting xvattr attributes
1238da6c28aaSamw  */
1239da6c28aaSamw int
1240da6c28aaSamw secpolicy_xvattr(xvattr_t *xvap, uid_t owner, cred_t *cr, vtype_t vtype)
1241da6c28aaSamw {
1242da6c28aaSamw 	xoptattr_t *xoap;
1243da6c28aaSamw 	int error = 0;
1244da6c28aaSamw 
1245da6c28aaSamw 	if ((xoap = xva_getxoptattr(xvap)) == NULL)
1246da6c28aaSamw 		return (EINVAL);
1247da6c28aaSamw 
1248da6c28aaSamw 	/*
1249da6c28aaSamw 	 * First process the DOS bits
1250da6c28aaSamw 	 */
1251da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
1252da6c28aaSamw 	    XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
1253da6c28aaSamw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
1254da6c28aaSamw 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM) ||
1255da6c28aaSamw 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME)) {
1256da6c28aaSamw 		if ((error = secpolicy_vnode_owner(cr, owner)) != 0)
1257da6c28aaSamw 			return (error);
1258da6c28aaSamw 	}
1259da6c28aaSamw 
1260da6c28aaSamw 	/*
1261da6c28aaSamw 	 * Now handle special attributes
1262da6c28aaSamw 	 */
1263da6c28aaSamw 
1264da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
1265da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_IMMUTABLE,
1266da6c28aaSamw 		    xoap->xoa_immutable, cr);
1267da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
1268da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_NOUNLINK,
1269da6c28aaSamw 		    xoap->xoa_nounlink, cr);
1270da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
1271da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_APPENDONLY,
1272da6c28aaSamw 		    xoap->xoa_appendonly, cr);
1273da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_NODUMP))
1274da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_NODUMP,
1275da6c28aaSamw 		    xoap->xoa_nodump, cr);
1276da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_OPAQUE))
1277da6c28aaSamw 		error = EPERM;
1278da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1279da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_AV_QUARANTINED,
1280da6c28aaSamw 		    xoap->xoa_av_quarantined, cr);
1281e8f97327Smarks 		if (error == 0 && vtype != VREG && xoap->xoa_av_quarantined)
1282da6c28aaSamw 			error = EINVAL;
1283da6c28aaSamw 	}
1284da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
1285da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_AV_MODIFIED,
1286da6c28aaSamw 		    xoap->xoa_av_modified, cr);
1287da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1288da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_AV_SCANSTAMP,
1289da6c28aaSamw 		    xoap->xoa_av_scanstamp, cr);
1290da6c28aaSamw 		if (error == 0 && vtype != VREG)
1291da6c28aaSamw 			error = EINVAL;
1292da6c28aaSamw 	}
1293da6c28aaSamw 	return (error);
1294da6c28aaSamw }
1295da6c28aaSamw 
12967c478bd9Sstevel@tonic-gate /*
12977c478bd9Sstevel@tonic-gate  * This function checks the policy decisions surrounding the
12987c478bd9Sstevel@tonic-gate  * vop setattr call.
12997c478bd9Sstevel@tonic-gate  *
13007c478bd9Sstevel@tonic-gate  * It should be called after sufficient locks have been established
13017c478bd9Sstevel@tonic-gate  * on the underlying data structures.  No concurrent modifications
13027c478bd9Sstevel@tonic-gate  * should be allowed.
13037c478bd9Sstevel@tonic-gate  *
13047c478bd9Sstevel@tonic-gate  * The caller must pass in unlocked version of its vaccess function
13057c478bd9Sstevel@tonic-gate  * this is required because vop_access function should lock the
13067c478bd9Sstevel@tonic-gate  * node for reading.  A three argument function should be defined
13077c478bd9Sstevel@tonic-gate  * which accepts the following argument:
13087c478bd9Sstevel@tonic-gate  * 	A pointer to the internal "node" type (inode *)
13097c478bd9Sstevel@tonic-gate  *	vnode access bits (VREAD|VWRITE|VEXEC)
13107c478bd9Sstevel@tonic-gate  *	a pointer to the credential
13117c478bd9Sstevel@tonic-gate  *
13127c478bd9Sstevel@tonic-gate  * This function makes the following policy decisions:
13137c478bd9Sstevel@tonic-gate  *
13147c478bd9Sstevel@tonic-gate  *		- change permissions
13157c478bd9Sstevel@tonic-gate  *			- permission to change file mode if not owner
13167c478bd9Sstevel@tonic-gate  *			- permission to add sticky bit to non-directory
13177c478bd9Sstevel@tonic-gate  *			- permission to add set-gid bit
13187c478bd9Sstevel@tonic-gate  *
13197c478bd9Sstevel@tonic-gate  * The ovap argument should include AT_MODE|AT_UID|AT_GID.
13207c478bd9Sstevel@tonic-gate  *
13217c478bd9Sstevel@tonic-gate  * If the vap argument does not include AT_MODE, the mode will be copied from
13227c478bd9Sstevel@tonic-gate  * ovap.  In certain situations set-uid/set-gid bits need to be removed;
13237c478bd9Sstevel@tonic-gate  * this is done by marking vap->va_mask to include AT_MODE and va_mode
13247c478bd9Sstevel@tonic-gate  * is updated to the newly computed mode.
13257c478bd9Sstevel@tonic-gate  */
13267c478bd9Sstevel@tonic-gate 
13277c478bd9Sstevel@tonic-gate int
13287c478bd9Sstevel@tonic-gate secpolicy_vnode_setattr(cred_t *cr, struct vnode *vp, struct vattr *vap,
13297c478bd9Sstevel@tonic-gate 	const struct vattr *ovap, int flags,
13307c478bd9Sstevel@tonic-gate 	int unlocked_access(void *, int, cred_t *),
13317c478bd9Sstevel@tonic-gate 	void *node)
13327c478bd9Sstevel@tonic-gate {
13337c478bd9Sstevel@tonic-gate 	int mask = vap->va_mask;
13347c478bd9Sstevel@tonic-gate 	int error = 0;
1335da6c28aaSamw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
13367c478bd9Sstevel@tonic-gate 
13377c478bd9Sstevel@tonic-gate 	if (mask & AT_SIZE) {
13387c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
13397c478bd9Sstevel@tonic-gate 			error = EISDIR;
13407c478bd9Sstevel@tonic-gate 			goto out;
13417c478bd9Sstevel@tonic-gate 		}
1342da6c28aaSamw 
1343da6c28aaSamw 		/*
1344da6c28aaSamw 		 * If ATTR_NOACLCHECK is set in the flags, then we don't
1345da6c28aaSamw 		 * perform the secondary unlocked_access() call since the
1346da6c28aaSamw 		 * ACL (if any) is being checked there.
1347da6c28aaSamw 		 */
1348da6c28aaSamw 		if (skipaclchk == B_FALSE) {
13497c478bd9Sstevel@tonic-gate 			error = unlocked_access(node, VWRITE, cr);
13507c478bd9Sstevel@tonic-gate 			if (error)
13517c478bd9Sstevel@tonic-gate 				goto out;
13527c478bd9Sstevel@tonic-gate 		}
1353da6c28aaSamw 	}
13547c478bd9Sstevel@tonic-gate 	if (mask & AT_MODE) {
13557c478bd9Sstevel@tonic-gate 		/*
13567c478bd9Sstevel@tonic-gate 		 * If not the owner of the file then check privilege
13577c478bd9Sstevel@tonic-gate 		 * for two things: the privilege to set the mode at all
13587c478bd9Sstevel@tonic-gate 		 * and, if we're setting setuid, we also need permissions
13597c478bd9Sstevel@tonic-gate 		 * to add the set-uid bit, if we're not the owner.
13607c478bd9Sstevel@tonic-gate 		 * In the specific case of creating a set-uid root
13617c478bd9Sstevel@tonic-gate 		 * file, we need even more permissions.
13627c478bd9Sstevel@tonic-gate 		 */
13637c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_vnode_setdac(cr, ovap->va_uid)) != 0)
13647c478bd9Sstevel@tonic-gate 			goto out;
13657c478bd9Sstevel@tonic-gate 
1366f92daba9Smarks 		if ((error = secpolicy_setid_setsticky_clear(vp, vap,
1367f92daba9Smarks 		    ovap, cr)) != 0)
13687c478bd9Sstevel@tonic-gate 			goto out;
13697c478bd9Sstevel@tonic-gate 	} else
13707c478bd9Sstevel@tonic-gate 		vap->va_mode = ovap->va_mode;
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 	if (mask & (AT_UID|AT_GID)) {
13737c478bd9Sstevel@tonic-gate 		boolean_t checkpriv = B_FALSE;
13747c478bd9Sstevel@tonic-gate 
13757c478bd9Sstevel@tonic-gate 		/*
13767c478bd9Sstevel@tonic-gate 		 * Chowning files.
13777c478bd9Sstevel@tonic-gate 		 *
13787c478bd9Sstevel@tonic-gate 		 * If you are the file owner:
13797c478bd9Sstevel@tonic-gate 		 *	chown to other uid		FILE_CHOWN_SELF
13807c478bd9Sstevel@tonic-gate 		 *	chown to gid (non-member) 	FILE_CHOWN_SELF
13817c478bd9Sstevel@tonic-gate 		 *	chown to gid (member) 		<none>
13827c478bd9Sstevel@tonic-gate 		 *
13837c478bd9Sstevel@tonic-gate 		 * Instead of PRIV_FILE_CHOWN_SELF, FILE_CHOWN is also
13847c478bd9Sstevel@tonic-gate 		 * acceptable but the first one is reported when debugging.
13857c478bd9Sstevel@tonic-gate 		 *
13867c478bd9Sstevel@tonic-gate 		 * If you are not the file owner:
13877c478bd9Sstevel@tonic-gate 		 *	chown from root			PRIV_FILE_CHOWN + zone
13887c478bd9Sstevel@tonic-gate 		 *	chown from other to any		PRIV_FILE_CHOWN
13897c478bd9Sstevel@tonic-gate 		 *
13907c478bd9Sstevel@tonic-gate 		 */
13917c478bd9Sstevel@tonic-gate 		if (cr->cr_uid != ovap->va_uid) {
13927c478bd9Sstevel@tonic-gate 			checkpriv = B_TRUE;
13937c478bd9Sstevel@tonic-gate 		} else {
13947c478bd9Sstevel@tonic-gate 			if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
13957c478bd9Sstevel@tonic-gate 			    ((mask & AT_GID) && vap->va_gid != ovap->va_gid &&
13967c478bd9Sstevel@tonic-gate 			    !groupmember(vap->va_gid, cr))) {
13977c478bd9Sstevel@tonic-gate 				checkpriv = B_TRUE;
13987c478bd9Sstevel@tonic-gate 			}
13997c478bd9Sstevel@tonic-gate 		}
14007c478bd9Sstevel@tonic-gate 		/*
14017c478bd9Sstevel@tonic-gate 		 * If necessary, check privilege to see if update can be done.
14027c478bd9Sstevel@tonic-gate 		 */
14037c478bd9Sstevel@tonic-gate 		if (checkpriv &&
1404e02bc683SMark Shellenbaum 		    (error = secpolicy_vnode_chown(cr, ovap->va_uid)) != 0) {
14057c478bd9Sstevel@tonic-gate 			goto out;
14067c478bd9Sstevel@tonic-gate 		}
14077c478bd9Sstevel@tonic-gate 
14087c478bd9Sstevel@tonic-gate 		/*
14097c478bd9Sstevel@tonic-gate 		 * If the file has either the set UID or set GID bits
14107c478bd9Sstevel@tonic-gate 		 * set and the caller can set the bits, then leave them.
14117c478bd9Sstevel@tonic-gate 		 */
141213f9f30eSmarks 		secpolicy_setid_clear(vap, cr);
14137c478bd9Sstevel@tonic-gate 	}
14147c478bd9Sstevel@tonic-gate 	if (mask & (AT_ATIME|AT_MTIME)) {
14157c478bd9Sstevel@tonic-gate 		/*
14167c478bd9Sstevel@tonic-gate 		 * If not the file owner and not otherwise privileged,
14177c478bd9Sstevel@tonic-gate 		 * always return an error when setting the
14187c478bd9Sstevel@tonic-gate 		 * time other than the current (ATTR_UTIME flag set).
14197c478bd9Sstevel@tonic-gate 		 * If setting the current time (ATTR_UTIME not set) then
14207c478bd9Sstevel@tonic-gate 		 * unlocked_access will check permissions according to policy.
14217c478bd9Sstevel@tonic-gate 		 */
14227c478bd9Sstevel@tonic-gate 		if (cr->cr_uid != ovap->va_uid) {
14237c478bd9Sstevel@tonic-gate 			if (flags & ATTR_UTIME)
14247c478bd9Sstevel@tonic-gate 				error = secpolicy_vnode_utime_modify(cr);
1425da6c28aaSamw 			else if (skipaclchk == B_FALSE) {
14267c478bd9Sstevel@tonic-gate 				error = unlocked_access(node, VWRITE, cr);
14277c478bd9Sstevel@tonic-gate 				if (error == EACCES &&
14287c478bd9Sstevel@tonic-gate 				    secpolicy_vnode_utime_modify(cr) == 0)
14297c478bd9Sstevel@tonic-gate 					error = 0;
14307c478bd9Sstevel@tonic-gate 			}
14317c478bd9Sstevel@tonic-gate 			if (error)
14327c478bd9Sstevel@tonic-gate 				goto out;
14337c478bd9Sstevel@tonic-gate 		}
14347c478bd9Sstevel@tonic-gate 	}
1435da6c28aaSamw 
1436da6c28aaSamw 	/*
1437da6c28aaSamw 	 * Check for optional attributes here by checking the following:
1438da6c28aaSamw 	 */
1439da6c28aaSamw 	if (mask & AT_XVATTR)
1440da6c28aaSamw 		error = secpolicy_xvattr((xvattr_t *)vap, ovap->va_uid, cr,
1441da6c28aaSamw 		    vp->v_type);
14427c478bd9Sstevel@tonic-gate out:
14437c478bd9Sstevel@tonic-gate 	return (error);
14447c478bd9Sstevel@tonic-gate }
14457c478bd9Sstevel@tonic-gate 
14467c478bd9Sstevel@tonic-gate /*
14477c478bd9Sstevel@tonic-gate  * Name:	secpolicy_pcfs_modify_bootpartition()
14487c478bd9Sstevel@tonic-gate  *
14497c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can modify a pcfs boot partition.
14507c478bd9Sstevel@tonic-gate  *
14517c478bd9Sstevel@tonic-gate  * Output:	EACCES - if privilege check failed.
14527c478bd9Sstevel@tonic-gate  */
14537c478bd9Sstevel@tonic-gate /*ARGSUSED*/
14547c478bd9Sstevel@tonic-gate int
14557c478bd9Sstevel@tonic-gate secpolicy_pcfs_modify_bootpartition(const cred_t *cred)
14567c478bd9Sstevel@tonic-gate {
14577c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_ALL, B_FALSE, EACCES,
14587c478bd9Sstevel@tonic-gate 	    "modify pcfs boot partition"));
14597c478bd9Sstevel@tonic-gate }
14607c478bd9Sstevel@tonic-gate 
14617c478bd9Sstevel@tonic-gate /*
14627c478bd9Sstevel@tonic-gate  * System V IPC routines
14637c478bd9Sstevel@tonic-gate  */
14647c478bd9Sstevel@tonic-gate int
14657c478bd9Sstevel@tonic-gate secpolicy_ipc_owner(const cred_t *cr, const struct kipc_perm *ip)
14667c478bd9Sstevel@tonic-gate {
14677c478bd9Sstevel@tonic-gate 	if (crgetzoneid(cr) != ip->ipc_zoneid ||
14687c478bd9Sstevel@tonic-gate 	    (cr->cr_uid != ip->ipc_uid && cr->cr_uid != ip->ipc_cuid)) {
14697c478bd9Sstevel@tonic-gate 		boolean_t allzone = B_FALSE;
14707c478bd9Sstevel@tonic-gate 		if (ip->ipc_uid == 0 || ip->ipc_cuid == 0)
14717c478bd9Sstevel@tonic-gate 			allzone = B_TRUE;
14727c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_OWNER, allzone, EPERM, NULL));
14737c478bd9Sstevel@tonic-gate 	}
14747c478bd9Sstevel@tonic-gate 	return (0);
14757c478bd9Sstevel@tonic-gate }
14767c478bd9Sstevel@tonic-gate 
14777c478bd9Sstevel@tonic-gate int
14787c478bd9Sstevel@tonic-gate secpolicy_ipc_config(const cred_t *cr)
14797c478bd9Sstevel@tonic-gate {
14807c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_IPC_CONFIG, B_FALSE, EPERM, NULL));
14817c478bd9Sstevel@tonic-gate }
14827c478bd9Sstevel@tonic-gate 
14837c478bd9Sstevel@tonic-gate int
14847c478bd9Sstevel@tonic-gate secpolicy_ipc_access(const cred_t *cr, const struct kipc_perm *ip, mode_t mode)
14857c478bd9Sstevel@tonic-gate {
14867c478bd9Sstevel@tonic-gate 
14877c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
14887c478bd9Sstevel@tonic-gate 
14897c478bd9Sstevel@tonic-gate 	ASSERT((mode & (MSG_R|MSG_W)) != 0);
14907c478bd9Sstevel@tonic-gate 
14917c478bd9Sstevel@tonic-gate 	if ((mode & MSG_R) &&
14927c478bd9Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_IPC_DAC_READ, allzone, EACCES, NULL) != 0)
14937c478bd9Sstevel@tonic-gate 		return (EACCES);
14947c478bd9Sstevel@tonic-gate 
14957c478bd9Sstevel@tonic-gate 	if (mode & MSG_W) {
14967c478bd9Sstevel@tonic-gate 		if (cr->cr_uid != 0 && (ip->ipc_uid == 0 || ip->ipc_cuid == 0))
14977c478bd9Sstevel@tonic-gate 			allzone = B_TRUE;
14987c478bd9Sstevel@tonic-gate 
14997c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_DAC_WRITE, allzone, EACCES,
15007c478bd9Sstevel@tonic-gate 		    NULL));
15017c478bd9Sstevel@tonic-gate 	}
15027c478bd9Sstevel@tonic-gate 	return (0);
15037c478bd9Sstevel@tonic-gate }
15047c478bd9Sstevel@tonic-gate 
15057c478bd9Sstevel@tonic-gate int
15067c478bd9Sstevel@tonic-gate secpolicy_rsm_access(const cred_t *cr, uid_t owner, mode_t mode)
15077c478bd9Sstevel@tonic-gate {
15087c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
15097c478bd9Sstevel@tonic-gate 
15107c478bd9Sstevel@tonic-gate 	ASSERT((mode & (MSG_R|MSG_W)) != 0);
15117c478bd9Sstevel@tonic-gate 
15127c478bd9Sstevel@tonic-gate 	if ((mode & MSG_R) &&
15137c478bd9Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_IPC_DAC_READ, allzone, EACCES, NULL) != 0)
15147c478bd9Sstevel@tonic-gate 		return (EACCES);
15157c478bd9Sstevel@tonic-gate 
15167c478bd9Sstevel@tonic-gate 	if (mode & MSG_W) {
15177c478bd9Sstevel@tonic-gate 		if (cr->cr_uid != 0 && owner == 0)
15187c478bd9Sstevel@tonic-gate 			allzone = B_TRUE;
15197c478bd9Sstevel@tonic-gate 
15207c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_DAC_WRITE, allzone, EACCES,
15217c478bd9Sstevel@tonic-gate 		    NULL));
15227c478bd9Sstevel@tonic-gate 	}
15237c478bd9Sstevel@tonic-gate 	return (0);
15247c478bd9Sstevel@tonic-gate }
15257c478bd9Sstevel@tonic-gate 
15267c478bd9Sstevel@tonic-gate /*
15277c478bd9Sstevel@tonic-gate  * Audit configuration.
15287c478bd9Sstevel@tonic-gate  */
15297c478bd9Sstevel@tonic-gate int
15307c478bd9Sstevel@tonic-gate secpolicy_audit_config(const cred_t *cr)
15317c478bd9Sstevel@tonic-gate {
15327c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_AUDIT, B_FALSE, EPERM, NULL));
15337c478bd9Sstevel@tonic-gate }
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate /*
15367c478bd9Sstevel@tonic-gate  * Audit record generation.
15377c478bd9Sstevel@tonic-gate  */
15387c478bd9Sstevel@tonic-gate int
15397c478bd9Sstevel@tonic-gate secpolicy_audit_modify(const cred_t *cr)
15407c478bd9Sstevel@tonic-gate {
15417c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_AUDIT, B_FALSE, EPERM, NULL));
15427c478bd9Sstevel@tonic-gate }
15437c478bd9Sstevel@tonic-gate 
15447c478bd9Sstevel@tonic-gate /*
15457c478bd9Sstevel@tonic-gate  * Get audit attributes.
15467c478bd9Sstevel@tonic-gate  * Either PRIV_SYS_AUDIT or PRIV_PROC_AUDIT required; report the
15477c478bd9Sstevel@tonic-gate  * "Least" of the two privileges on error.
15487c478bd9Sstevel@tonic-gate  */
15497c478bd9Sstevel@tonic-gate int
1550*134a1f4eSCasper H.S. Dik secpolicy_audit_getattr(const cred_t *cr, boolean_t checkonly)
15517c478bd9Sstevel@tonic-gate {
1552*134a1f4eSCasper H.S. Dik 	int priv;
1553*134a1f4eSCasper H.S. Dik 
1554*134a1f4eSCasper H.S. Dik 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_AUDIT, B_FALSE))
1555*134a1f4eSCasper H.S. Dik 		priv = PRIV_SYS_AUDIT;
1556*134a1f4eSCasper H.S. Dik 	else
1557*134a1f4eSCasper H.S. Dik 		priv = PRIV_PROC_AUDIT;
1558*134a1f4eSCasper H.S. Dik 
1559*134a1f4eSCasper H.S. Dik 	if (checkonly)
1560*134a1f4eSCasper H.S. Dik 		return (!PRIV_POLICY_ONLY(cr, priv, B_FALSE));
1561*134a1f4eSCasper H.S. Dik 	else
1562*134a1f4eSCasper H.S. Dik 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
15637c478bd9Sstevel@tonic-gate }
15647c478bd9Sstevel@tonic-gate 
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate /*
15677c478bd9Sstevel@tonic-gate  * Locking physical memory
15687c478bd9Sstevel@tonic-gate  */
15697c478bd9Sstevel@tonic-gate int
15707c478bd9Sstevel@tonic-gate secpolicy_lock_memory(const cred_t *cr)
15717c478bd9Sstevel@tonic-gate {
15727c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_LOCK_MEMORY, B_FALSE, EPERM, NULL));
15737c478bd9Sstevel@tonic-gate }
15747c478bd9Sstevel@tonic-gate 
15757c478bd9Sstevel@tonic-gate /*
15767c478bd9Sstevel@tonic-gate  * Accounting (both acct(2) and exacct).
15777c478bd9Sstevel@tonic-gate  */
15787c478bd9Sstevel@tonic-gate int
15797c478bd9Sstevel@tonic-gate secpolicy_acct(const cred_t *cr)
15807c478bd9Sstevel@tonic-gate {
15817c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ACCT, B_FALSE, EPERM, NULL));
15827c478bd9Sstevel@tonic-gate }
15837c478bd9Sstevel@tonic-gate 
15847c478bd9Sstevel@tonic-gate /*
15857c478bd9Sstevel@tonic-gate  * Is this process privileged to change its uids at will?
15867c478bd9Sstevel@tonic-gate  * Uid 0 is still considered "special" and having the SETID
15877c478bd9Sstevel@tonic-gate  * privilege is not sufficient to get uid 0.
15887c478bd9Sstevel@tonic-gate  * Files are owned by root, so the privilege would give
15897c478bd9Sstevel@tonic-gate  * full access and euid 0 is still effective.
15907c478bd9Sstevel@tonic-gate  *
15917c478bd9Sstevel@tonic-gate  * If you have the privilege and euid 0 only then do you
15927c478bd9Sstevel@tonic-gate  * get the powers of root wrt uid 0.
15937c478bd9Sstevel@tonic-gate  *
15947c478bd9Sstevel@tonic-gate  * For gid manipulations, this is should be called with an
15957c478bd9Sstevel@tonic-gate  * uid of -1.
15967c478bd9Sstevel@tonic-gate  *
15977c478bd9Sstevel@tonic-gate  */
15987c478bd9Sstevel@tonic-gate int
15997c478bd9Sstevel@tonic-gate secpolicy_allow_setid(const cred_t *cr, uid_t newuid, boolean_t checkonly)
16007c478bd9Sstevel@tonic-gate {
16017c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate 	if (newuid == 0 && cr->cr_uid != 0 && cr->cr_suid != 0 &&
16047c478bd9Sstevel@tonic-gate 	    cr->cr_ruid != 0) {
16057c478bd9Sstevel@tonic-gate 		allzone = B_TRUE;
16067c478bd9Sstevel@tonic-gate 	}
16077c478bd9Sstevel@tonic-gate 
16087c478bd9Sstevel@tonic-gate 	return (checkonly ? !PRIV_POLICY_ONLY(cr, PRIV_PROC_SETID, allzone) :
16097c478bd9Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_PROC_SETID, allzone, EPERM, NULL));
16107c478bd9Sstevel@tonic-gate }
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate 
16137c478bd9Sstevel@tonic-gate /*
16147c478bd9Sstevel@tonic-gate  * Acting on a different process: if the mode is for writing,
16157c478bd9Sstevel@tonic-gate  * the restrictions are more severe.  This is called after
16167c478bd9Sstevel@tonic-gate  * we've verified that the uids do not match.
16177c478bd9Sstevel@tonic-gate  */
16187c478bd9Sstevel@tonic-gate int
16197c478bd9Sstevel@tonic-gate secpolicy_proc_owner(const cred_t *scr, const cred_t *tcr, int mode)
16207c478bd9Sstevel@tonic-gate {
16217c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
16227c478bd9Sstevel@tonic-gate 
16237c478bd9Sstevel@tonic-gate 	if ((mode & VWRITE) && scr->cr_uid != 0 &&
16247c478bd9Sstevel@tonic-gate 	    (tcr->cr_uid == 0 || tcr->cr_ruid == 0 || tcr->cr_suid == 0))
16257c478bd9Sstevel@tonic-gate 		allzone = B_TRUE;
16267c478bd9Sstevel@tonic-gate 
16277c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, allzone, EPERM, NULL));
16287c478bd9Sstevel@tonic-gate }
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate int
16317c478bd9Sstevel@tonic-gate secpolicy_proc_access(const cred_t *scr)
16327c478bd9Sstevel@tonic-gate {
16337c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, B_FALSE, EACCES, NULL));
16347c478bd9Sstevel@tonic-gate }
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate int
16377c478bd9Sstevel@tonic-gate secpolicy_proc_excl_open(const cred_t *scr)
16387c478bd9Sstevel@tonic-gate {
16397c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, B_FALSE, EBUSY, NULL));
16407c478bd9Sstevel@tonic-gate }
16417c478bd9Sstevel@tonic-gate 
16427c478bd9Sstevel@tonic-gate int
16437c478bd9Sstevel@tonic-gate secpolicy_proc_zone(const cred_t *scr)
16447c478bd9Sstevel@tonic-gate {
16457c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_ZONE, B_FALSE, EPERM, NULL));
16467c478bd9Sstevel@tonic-gate }
16477c478bd9Sstevel@tonic-gate 
16487c478bd9Sstevel@tonic-gate /*
16497c478bd9Sstevel@tonic-gate  * Destroying the system
16507c478bd9Sstevel@tonic-gate  */
16517c478bd9Sstevel@tonic-gate 
16527c478bd9Sstevel@tonic-gate int
16537c478bd9Sstevel@tonic-gate secpolicy_kmdb(const cred_t *scr)
16547c478bd9Sstevel@tonic-gate {
16557c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
16567c478bd9Sstevel@tonic-gate }
16577c478bd9Sstevel@tonic-gate 
16587aec1d6eScindi int
16597aec1d6eScindi secpolicy_error_inject(const cred_t *scr)
16607aec1d6eScindi {
16617aec1d6eScindi 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
16627aec1d6eScindi }
16637aec1d6eScindi 
16647c478bd9Sstevel@tonic-gate /*
16657c478bd9Sstevel@tonic-gate  * Processor sets, cpu configuration, resource pools.
16667c478bd9Sstevel@tonic-gate  */
16677c478bd9Sstevel@tonic-gate int
16687c478bd9Sstevel@tonic-gate secpolicy_pset(const cred_t *cr)
16697c478bd9Sstevel@tonic-gate {
16707c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
16717c478bd9Sstevel@tonic-gate }
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate int
16747c478bd9Sstevel@tonic-gate secpolicy_ponline(const cred_t *cr)
16757c478bd9Sstevel@tonic-gate {
16767c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
16777c478bd9Sstevel@tonic-gate }
16787c478bd9Sstevel@tonic-gate 
16797c478bd9Sstevel@tonic-gate int
16807c478bd9Sstevel@tonic-gate secpolicy_pool(const cred_t *cr)
16817c478bd9Sstevel@tonic-gate {
16827c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
16837c478bd9Sstevel@tonic-gate }
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate int
16867c478bd9Sstevel@tonic-gate secpolicy_blacklist(const cred_t *cr)
16877c478bd9Sstevel@tonic-gate {
16887c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
16897c478bd9Sstevel@tonic-gate }
16907c478bd9Sstevel@tonic-gate 
16917c478bd9Sstevel@tonic-gate /*
16927c478bd9Sstevel@tonic-gate  * Catch all system configuration.
16937c478bd9Sstevel@tonic-gate  */
16947c478bd9Sstevel@tonic-gate int
16957c478bd9Sstevel@tonic-gate secpolicy_sys_config(const cred_t *cr, boolean_t checkonly)
16967c478bd9Sstevel@tonic-gate {
16977c478bd9Sstevel@tonic-gate 	if (checkonly) {
16987c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_CONFIG, B_FALSE) ? 0 :
16997c478bd9Sstevel@tonic-gate 		    EPERM);
17007c478bd9Sstevel@tonic-gate 	} else {
17017c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
17027c478bd9Sstevel@tonic-gate 	}
17037c478bd9Sstevel@tonic-gate }
17047c478bd9Sstevel@tonic-gate 
17057c478bd9Sstevel@tonic-gate /*
17067c478bd9Sstevel@tonic-gate  * Zone administration (halt, reboot, etc.) from within zone.
17077c478bd9Sstevel@tonic-gate  */
17087c478bd9Sstevel@tonic-gate int
17097c478bd9Sstevel@tonic-gate secpolicy_zone_admin(const cred_t *cr, boolean_t checkonly)
17107c478bd9Sstevel@tonic-gate {
17117c478bd9Sstevel@tonic-gate 	if (checkonly) {
17127c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_ADMIN, B_FALSE) ? 0 :
17137c478bd9Sstevel@tonic-gate 		    EPERM);
17147c478bd9Sstevel@tonic-gate 	} else {
17157c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM,
17167c478bd9Sstevel@tonic-gate 		    NULL));
17177c478bd9Sstevel@tonic-gate 	}
17187c478bd9Sstevel@tonic-gate }
17197c478bd9Sstevel@tonic-gate 
17207c478bd9Sstevel@tonic-gate /*
17217c478bd9Sstevel@tonic-gate  * Zone configuration (create, halt, enter).
17227c478bd9Sstevel@tonic-gate  */
17237c478bd9Sstevel@tonic-gate int
17247c478bd9Sstevel@tonic-gate secpolicy_zone_config(const cred_t *cr)
17257c478bd9Sstevel@tonic-gate {
17267c478bd9Sstevel@tonic-gate 	/*
17277c478bd9Sstevel@tonic-gate 	 * Require all privileges to avoid possibility of privilege
17287c478bd9Sstevel@tonic-gate 	 * escalation.
17297c478bd9Sstevel@tonic-gate 	 */
1730*134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
17317c478bd9Sstevel@tonic-gate }
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate /*
17347c478bd9Sstevel@tonic-gate  * Various other system configuration calls
17357c478bd9Sstevel@tonic-gate  */
17367c478bd9Sstevel@tonic-gate int
17377c478bd9Sstevel@tonic-gate secpolicy_coreadm(const cred_t *cr)
17387c478bd9Sstevel@tonic-gate {
17397c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM, NULL));
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate int
17437c478bd9Sstevel@tonic-gate secpolicy_systeminfo(const cred_t *cr)
17447c478bd9Sstevel@tonic-gate {
17457c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM, NULL));
17467c478bd9Sstevel@tonic-gate }
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate int
17497c478bd9Sstevel@tonic-gate secpolicy_dispadm(const cred_t *cr)
17507c478bd9Sstevel@tonic-gate {
17517c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
17527c478bd9Sstevel@tonic-gate }
17537c478bd9Sstevel@tonic-gate 
17547c478bd9Sstevel@tonic-gate int
17557c478bd9Sstevel@tonic-gate secpolicy_settime(const cred_t *cr)
17567c478bd9Sstevel@tonic-gate {
17577c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_TIME, B_FALSE, EPERM, NULL));
17587c478bd9Sstevel@tonic-gate }
17597c478bd9Sstevel@tonic-gate 
17607c478bd9Sstevel@tonic-gate /*
17617c478bd9Sstevel@tonic-gate  * For realtime users: high resolution clock.
17627c478bd9Sstevel@tonic-gate  */
17637c478bd9Sstevel@tonic-gate int
17647c478bd9Sstevel@tonic-gate secpolicy_clock_highres(const cred_t *cr)
17657c478bd9Sstevel@tonic-gate {
17667c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_CLOCK_HIGHRES, B_FALSE, EPERM,
17677c478bd9Sstevel@tonic-gate 	    NULL));
17687c478bd9Sstevel@tonic-gate }
17697c478bd9Sstevel@tonic-gate 
17707c478bd9Sstevel@tonic-gate /*
17717c478bd9Sstevel@tonic-gate  * drv_priv() is documented as callable from interrupt context, not that
17727c478bd9Sstevel@tonic-gate  * anyone ever does, but still.  No debugging or auditing can be done when
17737c478bd9Sstevel@tonic-gate  * it is called from interrupt context.
17747c478bd9Sstevel@tonic-gate  * returns 0 on succes, EPERM on failure.
17757c478bd9Sstevel@tonic-gate  */
17767c478bd9Sstevel@tonic-gate int
17777c478bd9Sstevel@tonic-gate drv_priv(cred_t *cr)
17787c478bd9Sstevel@tonic-gate {
17797c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
17807c478bd9Sstevel@tonic-gate }
17817c478bd9Sstevel@tonic-gate 
17827c478bd9Sstevel@tonic-gate int
17837c478bd9Sstevel@tonic-gate secpolicy_sys_devices(const cred_t *cr)
17847c478bd9Sstevel@tonic-gate {
17857c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
17867c478bd9Sstevel@tonic-gate }
17877c478bd9Sstevel@tonic-gate 
17887c478bd9Sstevel@tonic-gate int
17897c478bd9Sstevel@tonic-gate secpolicy_excl_open(const cred_t *cr)
17907c478bd9Sstevel@tonic-gate {
17917c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EBUSY, NULL));
17927c478bd9Sstevel@tonic-gate }
17937c478bd9Sstevel@tonic-gate 
17947c478bd9Sstevel@tonic-gate int
17957c478bd9Sstevel@tonic-gate secpolicy_rctlsys(const cred_t *cr, boolean_t is_zone_rctl)
17967c478bd9Sstevel@tonic-gate {
17977c478bd9Sstevel@tonic-gate 	/* zone.* rctls can only be set from the global zone */
17987c478bd9Sstevel@tonic-gate 	if (is_zone_rctl && priv_policy_global(cr) != 0)
17997c478bd9Sstevel@tonic-gate 		return (EPERM);
18007c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
18017c478bd9Sstevel@tonic-gate }
18027c478bd9Sstevel@tonic-gate 
18037c478bd9Sstevel@tonic-gate int
18047c478bd9Sstevel@tonic-gate secpolicy_resource(const cred_t *cr)
18057c478bd9Sstevel@tonic-gate {
18067c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
18077c478bd9Sstevel@tonic-gate }
18087c478bd9Sstevel@tonic-gate 
18091c7cef2bSStan Studzinski int
18101c7cef2bSStan Studzinski secpolicy_resource_anon_mem(const cred_t *cr)
18111c7cef2bSStan Studzinski {
18121c7cef2bSStan Studzinski 	return (PRIV_POLICY_ONLY(cr, PRIV_SYS_RESOURCE, B_FALSE));
18131c7cef2bSStan Studzinski }
18141c7cef2bSStan Studzinski 
18157c478bd9Sstevel@tonic-gate /*
18167c478bd9Sstevel@tonic-gate  * Processes with a real uid of 0 escape any form of accounting, much
18177c478bd9Sstevel@tonic-gate  * like before.
18187c478bd9Sstevel@tonic-gate  */
18197c478bd9Sstevel@tonic-gate int
18207c478bd9Sstevel@tonic-gate secpolicy_newproc(const cred_t *cr)
18217c478bd9Sstevel@tonic-gate {
18227c478bd9Sstevel@tonic-gate 	if (cr->cr_ruid == 0)
18237c478bd9Sstevel@tonic-gate 		return (0);
18247c478bd9Sstevel@tonic-gate 
18257c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
18267c478bd9Sstevel@tonic-gate }
18277c478bd9Sstevel@tonic-gate 
18287c478bd9Sstevel@tonic-gate /*
18297c478bd9Sstevel@tonic-gate  * Networking
18307c478bd9Sstevel@tonic-gate  */
18317c478bd9Sstevel@tonic-gate int
18327c478bd9Sstevel@tonic-gate secpolicy_net_rawaccess(const cred_t *cr)
18337c478bd9Sstevel@tonic-gate {
18347c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_NET_RAWACCESS, B_FALSE, EACCES, NULL));
18357c478bd9Sstevel@tonic-gate }
18367c478bd9Sstevel@tonic-gate 
18370a0e9771SDarren Reed int
18380a0e9771SDarren Reed secpolicy_net_observability(const cred_t *cr)
18390a0e9771SDarren Reed {
18400a0e9771SDarren Reed 	return (PRIV_POLICY(cr, PRIV_NET_OBSERVABILITY, B_FALSE, EACCES, NULL));
18410a0e9771SDarren Reed }
18420a0e9771SDarren Reed 
18437c478bd9Sstevel@tonic-gate /*
18447c478bd9Sstevel@tonic-gate  * Need this privilege for accessing the ICMP device
18457c478bd9Sstevel@tonic-gate  */
18467c478bd9Sstevel@tonic-gate int
18477c478bd9Sstevel@tonic-gate secpolicy_net_icmpaccess(const cred_t *cr)
18487c478bd9Sstevel@tonic-gate {
18497c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_NET_ICMPACCESS, B_FALSE, EACCES, NULL));
18507c478bd9Sstevel@tonic-gate }
18517c478bd9Sstevel@tonic-gate 
18527c478bd9Sstevel@tonic-gate /*
18537c478bd9Sstevel@tonic-gate  * There are a few rare cases where the kernel generates ioctls() from
18547c478bd9Sstevel@tonic-gate  * interrupt context with a credential of kcred rather than NULL.
18557c478bd9Sstevel@tonic-gate  * In those cases, we take the safe and cheap test.
18567c478bd9Sstevel@tonic-gate  */
18577c478bd9Sstevel@tonic-gate int
18587c478bd9Sstevel@tonic-gate secpolicy_net_config(const cred_t *cr, boolean_t checkonly)
18597c478bd9Sstevel@tonic-gate {
18607c478bd9Sstevel@tonic-gate 	if (checkonly) {
18617c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE) ?
18627c478bd9Sstevel@tonic-gate 		    0 : EPERM);
18637c478bd9Sstevel@tonic-gate 	} else {
18647c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_NET_CONFIG, B_FALSE, EPERM,
18657c478bd9Sstevel@tonic-gate 		    NULL));
18667c478bd9Sstevel@tonic-gate 	}
18677c478bd9Sstevel@tonic-gate }
18687c478bd9Sstevel@tonic-gate 
18697c478bd9Sstevel@tonic-gate 
18707c478bd9Sstevel@tonic-gate /*
1871e6bdcbd5Sdh155122  * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_IP_CONFIG.
1872f4b3ec61Sdh155122  *
1873f4b3ec61Sdh155122  * There are a few rare cases where the kernel generates ioctls() from
1874f4b3ec61Sdh155122  * interrupt context with a credential of kcred rather than NULL.
1875f4b3ec61Sdh155122  * In those cases, we take the safe and cheap test.
1876f4b3ec61Sdh155122  */
1877f4b3ec61Sdh155122 int
1878f4b3ec61Sdh155122 secpolicy_ip_config(const cred_t *cr, boolean_t checkonly)
1879f4b3ec61Sdh155122 {
1880f4b3ec61Sdh155122 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
1881f4b3ec61Sdh155122 		return (secpolicy_net_config(cr, checkonly));
1882f4b3ec61Sdh155122 
1883f4b3ec61Sdh155122 	if (checkonly) {
1884f4b3ec61Sdh155122 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_IP_CONFIG, B_FALSE) ?
1885f4b3ec61Sdh155122 		    0 : EPERM);
1886f4b3ec61Sdh155122 	} else {
1887f4b3ec61Sdh155122 		return (PRIV_POLICY(cr, PRIV_SYS_IP_CONFIG, B_FALSE, EPERM,
1888f4b3ec61Sdh155122 		    NULL));
1889f4b3ec61Sdh155122 	}
1890f4b3ec61Sdh155122 }
1891f4b3ec61Sdh155122 
1892eae72b5bSSebastien Roy /*
1893eae72b5bSSebastien Roy  * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_DL_CONFIG.
1894eae72b5bSSebastien Roy  */
1895eae72b5bSSebastien Roy int
1896eae72b5bSSebastien Roy secpolicy_dl_config(const cred_t *cr)
1897eae72b5bSSebastien Roy {
1898eae72b5bSSebastien Roy 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
1899eae72b5bSSebastien Roy 		return (secpolicy_net_config(cr, B_FALSE));
19002b24ab6bSSebastien Roy 	return (PRIV_POLICY(cr, PRIV_SYS_DL_CONFIG, B_FALSE, EPERM, NULL));
1901eae72b5bSSebastien Roy }
1902eae72b5bSSebastien Roy 
19032b24ab6bSSebastien Roy /*
19042b24ab6bSSebastien Roy  * PRIV_SYS_DL_CONFIG is a superset of PRIV_SYS_IPTUN_CONFIG.
19052b24ab6bSSebastien Roy  */
19062b24ab6bSSebastien Roy int
19072b24ab6bSSebastien Roy secpolicy_iptun_config(const cred_t *cr)
19082b24ab6bSSebastien Roy {
19092b24ab6bSSebastien Roy 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
19102b24ab6bSSebastien Roy 		return (secpolicy_net_config(cr, B_FALSE));
19112b24ab6bSSebastien Roy 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_DL_CONFIG, B_FALSE))
19122b24ab6bSSebastien Roy 		return (secpolicy_dl_config(cr));
19132b24ab6bSSebastien Roy 	return (PRIV_POLICY(cr, PRIV_SYS_IPTUN_CONFIG, B_FALSE, EPERM, NULL));
19142b24ab6bSSebastien Roy }
1915f4b3ec61Sdh155122 
1916f4b3ec61Sdh155122 /*
1917f4b3ec61Sdh155122  * Map IP pseudo privileges to actual privileges.
1918f4b3ec61Sdh155122  * So we don't need to recompile IP when we change the privileges.
1919f4b3ec61Sdh155122  */
1920f4b3ec61Sdh155122 int
1921f4b3ec61Sdh155122 secpolicy_ip(const cred_t *cr, int netpriv, boolean_t checkonly)
1922f4b3ec61Sdh155122 {
1923f4b3ec61Sdh155122 	int priv = PRIV_ALL;
1924f4b3ec61Sdh155122 
1925f4b3ec61Sdh155122 	switch (netpriv) {
1926f4b3ec61Sdh155122 	case OP_CONFIG:
1927f4b3ec61Sdh155122 		priv = PRIV_SYS_IP_CONFIG;
1928f4b3ec61Sdh155122 		break;
1929f4b3ec61Sdh155122 	case OP_RAW:
1930f4b3ec61Sdh155122 		priv = PRIV_NET_RAWACCESS;
1931f4b3ec61Sdh155122 		break;
1932f4b3ec61Sdh155122 	case OP_PRIVPORT:
1933f4b3ec61Sdh155122 		priv = PRIV_NET_PRIVADDR;
1934f4b3ec61Sdh155122 		break;
1935f4b3ec61Sdh155122 	}
1936f4b3ec61Sdh155122 	ASSERT(priv != PRIV_ALL);
1937f4b3ec61Sdh155122 	if (checkonly)
1938f4b3ec61Sdh155122 		return (PRIV_POLICY_ONLY(cr, priv, B_FALSE) ? 0 : EPERM);
1939f4b3ec61Sdh155122 	else
1940f4b3ec61Sdh155122 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
1941f4b3ec61Sdh155122 }
1942f4b3ec61Sdh155122 
1943f4b3ec61Sdh155122 /*
19447c478bd9Sstevel@tonic-gate  * Map network pseudo privileges to actual privileges.
19457c478bd9Sstevel@tonic-gate  * So we don't need to recompile IP when we change the privileges.
19467c478bd9Sstevel@tonic-gate  */
19477c478bd9Sstevel@tonic-gate int
19487c478bd9Sstevel@tonic-gate secpolicy_net(const cred_t *cr, int netpriv, boolean_t checkonly)
19497c478bd9Sstevel@tonic-gate {
19507c478bd9Sstevel@tonic-gate 	int priv = PRIV_ALL;
19517c478bd9Sstevel@tonic-gate 
19527c478bd9Sstevel@tonic-gate 	switch (netpriv) {
19537c478bd9Sstevel@tonic-gate 	case OP_CONFIG:
19547c478bd9Sstevel@tonic-gate 		priv = PRIV_SYS_NET_CONFIG;
19557c478bd9Sstevel@tonic-gate 		break;
19567c478bd9Sstevel@tonic-gate 	case OP_RAW:
19577c478bd9Sstevel@tonic-gate 		priv = PRIV_NET_RAWACCESS;
19587c478bd9Sstevel@tonic-gate 		break;
19597c478bd9Sstevel@tonic-gate 	case OP_PRIVPORT:
19607c478bd9Sstevel@tonic-gate 		priv = PRIV_NET_PRIVADDR;
19617c478bd9Sstevel@tonic-gate 		break;
19627c478bd9Sstevel@tonic-gate 	}
19637c478bd9Sstevel@tonic-gate 	ASSERT(priv != PRIV_ALL);
19647c478bd9Sstevel@tonic-gate 	if (checkonly)
19657c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, priv, B_FALSE) ? 0 : EPERM);
19667c478bd9Sstevel@tonic-gate 	else
19677c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
19687c478bd9Sstevel@tonic-gate }
19697c478bd9Sstevel@tonic-gate 
19707c478bd9Sstevel@tonic-gate /*
19717c478bd9Sstevel@tonic-gate  * Checks for operations that are either client-only or are used by
19727c478bd9Sstevel@tonic-gate  * both clients and servers.
19737c478bd9Sstevel@tonic-gate  */
19747c478bd9Sstevel@tonic-gate int
19757c478bd9Sstevel@tonic-gate secpolicy_nfs(const cred_t *cr)
19767c478bd9Sstevel@tonic-gate {
19777c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_NFS, B_FALSE, EPERM, NULL));
19787c478bd9Sstevel@tonic-gate }
19797c478bd9Sstevel@tonic-gate 
19807c478bd9Sstevel@tonic-gate /*
19817c478bd9Sstevel@tonic-gate  * Special case for opening rpcmod: have NFS privileges or network
19827c478bd9Sstevel@tonic-gate  * config privileges.
19837c478bd9Sstevel@tonic-gate  */
19847c478bd9Sstevel@tonic-gate int
19857c478bd9Sstevel@tonic-gate secpolicy_rpcmod_open(const cred_t *cr)
19867c478bd9Sstevel@tonic-gate {
19877c478bd9Sstevel@tonic-gate 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NFS, B_FALSE))
19887c478bd9Sstevel@tonic-gate 		return (secpolicy_nfs(cr));
19897c478bd9Sstevel@tonic-gate 	else
19907c478bd9Sstevel@tonic-gate 		return (secpolicy_net_config(cr, NULL));
19917c478bd9Sstevel@tonic-gate }
19927c478bd9Sstevel@tonic-gate 
19937c478bd9Sstevel@tonic-gate int
19947c478bd9Sstevel@tonic-gate secpolicy_chroot(const cred_t *cr)
19957c478bd9Sstevel@tonic-gate {
19967c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_CHROOT, B_FALSE, EPERM, NULL));
19977c478bd9Sstevel@tonic-gate }
19987c478bd9Sstevel@tonic-gate 
19997c478bd9Sstevel@tonic-gate int
20007c478bd9Sstevel@tonic-gate secpolicy_tasksys(const cred_t *cr)
20017c478bd9Sstevel@tonic-gate {
20027c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_TASKID, B_FALSE, EPERM, NULL));
20037c478bd9Sstevel@tonic-gate }
20047c478bd9Sstevel@tonic-gate 
2005*134a1f4eSCasper H.S. Dik int
2006*134a1f4eSCasper H.S. Dik secpolicy_pfexec_register(const cred_t *cr)
2007*134a1f4eSCasper H.S. Dik {
2008*134a1f4eSCasper H.S. Dik 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_TRUE, EPERM, NULL));
2009*134a1f4eSCasper H.S. Dik }
2010*134a1f4eSCasper H.S. Dik 
20117c478bd9Sstevel@tonic-gate /*
20127c478bd9Sstevel@tonic-gate  * Basic privilege checks.
20137c478bd9Sstevel@tonic-gate  */
20147c478bd9Sstevel@tonic-gate int
2015ddf7fe95Scasper secpolicy_basic_exec(const cred_t *cr, vnode_t *vp)
20167c478bd9Sstevel@tonic-gate {
2017*134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_PROC_EXEC);
2018*134a1f4eSCasper H.S. Dik 
2019ddf7fe95Scasper 	return (priv_policy_va(cr, PRIV_PROC_EXEC, B_FALSE, EPERM, NULL,
2020ddf7fe95Scasper 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
20217c478bd9Sstevel@tonic-gate }
20227c478bd9Sstevel@tonic-gate 
20237c478bd9Sstevel@tonic-gate int
20247c478bd9Sstevel@tonic-gate secpolicy_basic_fork(const cred_t *cr)
20257c478bd9Sstevel@tonic-gate {
2026*134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_PROC_FORK);
2027*134a1f4eSCasper H.S. Dik 
20287c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_FORK, B_FALSE, EPERM, NULL));
20297c478bd9Sstevel@tonic-gate }
20307c478bd9Sstevel@tonic-gate 
20317c478bd9Sstevel@tonic-gate int
20327c478bd9Sstevel@tonic-gate secpolicy_basic_proc(const cred_t *cr)
20337c478bd9Sstevel@tonic-gate {
2034*134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_PROC_SESSION);
2035*134a1f4eSCasper H.S. Dik 
20367c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_SESSION, B_FALSE, EPERM, NULL));
20377c478bd9Sstevel@tonic-gate }
20387c478bd9Sstevel@tonic-gate 
20397c478bd9Sstevel@tonic-gate /*
20407c478bd9Sstevel@tonic-gate  * Slightly complicated because we don't want to trigger the policy too
20417c478bd9Sstevel@tonic-gate  * often.  First we shortcircuit access to "self" (tp == sp) or if
20427c478bd9Sstevel@tonic-gate  * we don't have the privilege but if we have permission
20437c478bd9Sstevel@tonic-gate  * just return (0) and we don't flag the privilege as needed.
20447c478bd9Sstevel@tonic-gate  * Else, we test for the privilege because we either have it or need it.
20457c478bd9Sstevel@tonic-gate  */
20467c478bd9Sstevel@tonic-gate int
20477c478bd9Sstevel@tonic-gate secpolicy_basic_procinfo(const cred_t *cr, proc_t *tp, proc_t *sp)
20487c478bd9Sstevel@tonic-gate {
20497c478bd9Sstevel@tonic-gate 	if (tp == sp ||
20507c478bd9Sstevel@tonic-gate 	    !HAS_PRIVILEGE(cr, PRIV_PROC_INFO) && prochasprocperm(tp, sp, cr)) {
20517c478bd9Sstevel@tonic-gate 		return (0);
20527c478bd9Sstevel@tonic-gate 	} else {
20537c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_PROC_INFO, B_FALSE, EPERM, NULL));
20547c478bd9Sstevel@tonic-gate 	}
20557c478bd9Sstevel@tonic-gate }
20567c478bd9Sstevel@tonic-gate 
20577c478bd9Sstevel@tonic-gate int
20587c478bd9Sstevel@tonic-gate secpolicy_basic_link(const cred_t *cr)
20597c478bd9Sstevel@tonic-gate {
2060*134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_FILE_LINK_ANY);
2061*134a1f4eSCasper H.S. Dik 
20627c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_LINK_ANY, B_FALSE, EPERM, NULL));
20637c478bd9Sstevel@tonic-gate }
20647c478bd9Sstevel@tonic-gate 
2065634e26ecSCasper H.S. Dik int
2066634e26ecSCasper H.S. Dik secpolicy_basic_net_access(const cred_t *cr)
2067634e26ecSCasper H.S. Dik {
2068*134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_NET_ACCESS);
2069*134a1f4eSCasper H.S. Dik 
2070634e26ecSCasper H.S. Dik 	return (PRIV_POLICY(cr, PRIV_NET_ACCESS, B_FALSE, EACCES, NULL));
2071634e26ecSCasper H.S. Dik }
2072634e26ecSCasper H.S. Dik 
2073*134a1f4eSCasper H.S. Dik /* ARGSUSED */
2074*134a1f4eSCasper H.S. Dik int
2075*134a1f4eSCasper H.S. Dik secpolicy_basic_file_read(const cred_t *cr, vnode_t *vp, const char *pn)
2076*134a1f4eSCasper H.S. Dik {
2077*134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_FILE_READ);
2078*134a1f4eSCasper H.S. Dik 
2079*134a1f4eSCasper H.S. Dik 	return (priv_policy_va(cr, PRIV_FILE_READ, B_FALSE, EACCES, NULL,
2080*134a1f4eSCasper H.S. Dik 	    KLPDARG_VNODE, vp, (char *)pn, KLPDARG_NOMORE));
2081*134a1f4eSCasper H.S. Dik }
2082*134a1f4eSCasper H.S. Dik 
2083*134a1f4eSCasper H.S. Dik /* ARGSUSED */
2084*134a1f4eSCasper H.S. Dik int
2085*134a1f4eSCasper H.S. Dik secpolicy_basic_file_write(const cred_t *cr, vnode_t *vp, const char *pn)
2086*134a1f4eSCasper H.S. Dik {
2087*134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_FILE_WRITE);
2088*134a1f4eSCasper H.S. Dik 
2089*134a1f4eSCasper H.S. Dik 	return (priv_policy_va(cr, PRIV_FILE_WRITE, B_FALSE, EACCES, NULL,
2090*134a1f4eSCasper H.S. Dik 	    KLPDARG_VNODE, vp, (char *)pn, KLPDARG_NOMORE));
2091*134a1f4eSCasper H.S. Dik }
2092*134a1f4eSCasper H.S. Dik 
20937c478bd9Sstevel@tonic-gate /*
20947c478bd9Sstevel@tonic-gate  * Additional device protection.
20957c478bd9Sstevel@tonic-gate  *
20967c478bd9Sstevel@tonic-gate  * Traditionally, a device has specific permissions on the node in
20977c478bd9Sstevel@tonic-gate  * the filesystem which govern which devices can be opened by what
20987c478bd9Sstevel@tonic-gate  * processes.  In certain cases, it is desirable to add extra
20997c478bd9Sstevel@tonic-gate  * restrictions, as writing to certain devices is identical to
21007c478bd9Sstevel@tonic-gate  * having a complete run of the system.
21017c478bd9Sstevel@tonic-gate  *
21027c478bd9Sstevel@tonic-gate  * This mechanism is called the device policy.
21037c478bd9Sstevel@tonic-gate  *
21047c478bd9Sstevel@tonic-gate  * When a device is opened, its policy entry is looked up in the
21057c478bd9Sstevel@tonic-gate  * policy cache and checked.
21067c478bd9Sstevel@tonic-gate  */
21077c478bd9Sstevel@tonic-gate int
21087c478bd9Sstevel@tonic-gate secpolicy_spec_open(const cred_t *cr, struct vnode *vp, int oflag)
21097c478bd9Sstevel@tonic-gate {
21107c478bd9Sstevel@tonic-gate 	devplcy_t *plcy;
21117c478bd9Sstevel@tonic-gate 	int err;
21127c478bd9Sstevel@tonic-gate 	struct snode *csp = VTOS(common_specvp(vp));
2113e6bdcbd5Sdh155122 	priv_set_t pset;
21147c478bd9Sstevel@tonic-gate 
21157c478bd9Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
21167c478bd9Sstevel@tonic-gate 
21177c478bd9Sstevel@tonic-gate 	if (csp->s_plcy == NULL || csp->s_plcy->dp_gen != devplcy_gen) {
21187c478bd9Sstevel@tonic-gate 		plcy = devpolicy_find(vp);
21197c478bd9Sstevel@tonic-gate 		if (csp->s_plcy)
21207c478bd9Sstevel@tonic-gate 			dpfree(csp->s_plcy);
21217c478bd9Sstevel@tonic-gate 		csp->s_plcy = plcy;
21227c478bd9Sstevel@tonic-gate 		ASSERT(plcy != NULL);
21237c478bd9Sstevel@tonic-gate 	} else
21247c478bd9Sstevel@tonic-gate 		plcy = csp->s_plcy;
21257c478bd9Sstevel@tonic-gate 
21267c478bd9Sstevel@tonic-gate 	if (plcy == nullpolicy) {
21277c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
21287c478bd9Sstevel@tonic-gate 		return (0);
21297c478bd9Sstevel@tonic-gate 	}
21307c478bd9Sstevel@tonic-gate 
21317c478bd9Sstevel@tonic-gate 	dphold(plcy);
21327c478bd9Sstevel@tonic-gate 
21337c478bd9Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
21347c478bd9Sstevel@tonic-gate 
2135e6bdcbd5Sdh155122 	if (oflag & FWRITE)
2136e6bdcbd5Sdh155122 		pset = plcy->dp_wrp;
2137e6bdcbd5Sdh155122 	else
2138e6bdcbd5Sdh155122 		pset = plcy->dp_rdp;
2139e6bdcbd5Sdh155122 	/*
2140e6bdcbd5Sdh155122 	 * Special case:
2141e6bdcbd5Sdh155122 	 * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_IP_CONFIG.
2142e6bdcbd5Sdh155122 	 * If PRIV_SYS_NET_CONFIG is present and PRIV_SYS_IP_CONFIG is
2143e6bdcbd5Sdh155122 	 * required, replace PRIV_SYS_IP_CONFIG with PRIV_SYS_NET_CONFIG
2144e6bdcbd5Sdh155122 	 * in the required privilege set before doing the check.
2145e6bdcbd5Sdh155122 	 */
2146e6bdcbd5Sdh155122 	if (priv_ismember(&pset, PRIV_SYS_IP_CONFIG) &&
2147e6bdcbd5Sdh155122 	    priv_ismember(&CR_OEPRIV(cr), PRIV_SYS_NET_CONFIG) &&
2148e6bdcbd5Sdh155122 	    !priv_ismember(&CR_OEPRIV(cr), PRIV_SYS_IP_CONFIG)) {
2149e6bdcbd5Sdh155122 		priv_delset(&pset, PRIV_SYS_IP_CONFIG);
2150e6bdcbd5Sdh155122 		priv_addset(&pset, PRIV_SYS_NET_CONFIG);
2151e6bdcbd5Sdh155122 	}
2152e6bdcbd5Sdh155122 
2153*134a1f4eSCasper H.S. Dik 	err = secpolicy_require_set(cr, &pset, "devpolicy", KLPDARG_NONE);
21547c478bd9Sstevel@tonic-gate 	dpfree(plcy);
21557c478bd9Sstevel@tonic-gate 
21567c478bd9Sstevel@tonic-gate 	return (err);
21577c478bd9Sstevel@tonic-gate }
21587c478bd9Sstevel@tonic-gate 
21597c478bd9Sstevel@tonic-gate int
21607c478bd9Sstevel@tonic-gate secpolicy_modctl(const cred_t *cr, int cmd)
21617c478bd9Sstevel@tonic-gate {
21627c478bd9Sstevel@tonic-gate 	switch (cmd) {
21637c478bd9Sstevel@tonic-gate 	case MODINFO:
2164a08731ecScth 	case MODGETMAJBIND:
21657c478bd9Sstevel@tonic-gate 	case MODGETPATH:
21667c478bd9Sstevel@tonic-gate 	case MODGETPATHLEN:
21677c478bd9Sstevel@tonic-gate 	case MODGETNAME:
2168a08731ecScth 	case MODGETFBNAME:
21697c478bd9Sstevel@tonic-gate 	case MODGETDEVPOLICY:
21707c478bd9Sstevel@tonic-gate 	case MODGETDEVPOLICYBYNAME:
2171a08731ecScth 	case MODDEVT2INSTANCE:
2172a08731ecScth 	case MODSIZEOF_DEVID:
2173a08731ecScth 	case MODGETDEVID:
2174a08731ecScth 	case MODSIZEOF_MINORNAME:
2175a08731ecScth 	case MODGETMINORNAME:
2176a08731ecScth 	case MODGETDEVFSPATH_LEN:
2177a08731ecScth 	case MODGETDEVFSPATH:
2178a08731ecScth 	case MODGETDEVFSPATH_MI_LEN:
2179a08731ecScth 	case MODGETDEVFSPATH_MI:
21807c478bd9Sstevel@tonic-gate 		/* Unprivileged */
21817c478bd9Sstevel@tonic-gate 		return (0);
21827c478bd9Sstevel@tonic-gate 	case MODLOAD:
21837c478bd9Sstevel@tonic-gate 	case MODSETDEVPOLICY:
2184*134a1f4eSCasper H.S. Dik 		return (secpolicy_require_set(cr, PRIV_FULLSET, NULL,
2185*134a1f4eSCasper H.S. Dik 		    KLPDARG_NONE));
21867c478bd9Sstevel@tonic-gate 	default:
21877c478bd9Sstevel@tonic-gate 		return (secpolicy_sys_config(cr, B_FALSE));
21887c478bd9Sstevel@tonic-gate 	}
21897c478bd9Sstevel@tonic-gate }
21907c478bd9Sstevel@tonic-gate 
21917c478bd9Sstevel@tonic-gate int
21927c478bd9Sstevel@tonic-gate secpolicy_console(const cred_t *cr)
21937c478bd9Sstevel@tonic-gate {
21947c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
21957c478bd9Sstevel@tonic-gate }
21967c478bd9Sstevel@tonic-gate 
21977c478bd9Sstevel@tonic-gate int
21987c478bd9Sstevel@tonic-gate secpolicy_power_mgmt(const cred_t *cr)
21997c478bd9Sstevel@tonic-gate {
22007c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
22017c478bd9Sstevel@tonic-gate }
22027c478bd9Sstevel@tonic-gate 
22037c478bd9Sstevel@tonic-gate /*
22047c478bd9Sstevel@tonic-gate  * Simulate terminal input; another escalation of privileges avenue.
22057c478bd9Sstevel@tonic-gate  */
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate int
22087c478bd9Sstevel@tonic-gate secpolicy_sti(const cred_t *cr)
22097c478bd9Sstevel@tonic-gate {
2210*134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
22117c478bd9Sstevel@tonic-gate }
22127c478bd9Sstevel@tonic-gate 
221345916cd2Sjpk boolean_t
221445916cd2Sjpk secpolicy_net_reply_equal(const cred_t *cr)
221545916cd2Sjpk {
221645916cd2Sjpk 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
221745916cd2Sjpk }
221845916cd2Sjpk 
22197c478bd9Sstevel@tonic-gate int
22207c478bd9Sstevel@tonic-gate secpolicy_swapctl(const cred_t *cr)
22217c478bd9Sstevel@tonic-gate {
22227c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
22237c478bd9Sstevel@tonic-gate }
22247c478bd9Sstevel@tonic-gate 
22257c478bd9Sstevel@tonic-gate int
22267c478bd9Sstevel@tonic-gate secpolicy_cpc_cpu(const cred_t *cr)
22277c478bd9Sstevel@tonic-gate {
22287c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CPC_CPU, B_FALSE, EACCES, NULL));
22297c478bd9Sstevel@tonic-gate }
22307c478bd9Sstevel@tonic-gate 
22317c478bd9Sstevel@tonic-gate /*
22327b209c2cSacruz  * secpolicy_contract_identity
22337b209c2cSacruz  *
22347b209c2cSacruz  * Determine if the subject may set the process contract FMRI value
22357b209c2cSacruz  */
22367b209c2cSacruz int
22377b209c2cSacruz secpolicy_contract_identity(const cred_t *cr)
22387b209c2cSacruz {
22397b209c2cSacruz 	return (PRIV_POLICY(cr, PRIV_CONTRACT_IDENTITY, B_FALSE, EPERM, NULL));
22407b209c2cSacruz }
22417b209c2cSacruz 
22427b209c2cSacruz /*
22437c478bd9Sstevel@tonic-gate  * secpolicy_contract_observer
22447c478bd9Sstevel@tonic-gate  *
22457c478bd9Sstevel@tonic-gate  * Determine if the subject may observe a specific contract's events.
22467c478bd9Sstevel@tonic-gate  */
22477c478bd9Sstevel@tonic-gate int
22487c478bd9Sstevel@tonic-gate secpolicy_contract_observer(const cred_t *cr, struct contract *ct)
22497c478bd9Sstevel@tonic-gate {
22507c478bd9Sstevel@tonic-gate 	if (contract_owned(ct, cr, B_FALSE))
22517c478bd9Sstevel@tonic-gate 		return (0);
22527c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CONTRACT_OBSERVER, B_FALSE, EPERM, NULL));
22537c478bd9Sstevel@tonic-gate }
22547c478bd9Sstevel@tonic-gate 
22557c478bd9Sstevel@tonic-gate /*
22567c478bd9Sstevel@tonic-gate  * secpolicy_contract_observer_choice
22577c478bd9Sstevel@tonic-gate  *
22587c478bd9Sstevel@tonic-gate  * Determine if the subject may observe any contract's events.  Just
22597c478bd9Sstevel@tonic-gate  * tests privilege and audits on success.
22607c478bd9Sstevel@tonic-gate  */
22617c478bd9Sstevel@tonic-gate boolean_t
22627c478bd9Sstevel@tonic-gate secpolicy_contract_observer_choice(const cred_t *cr)
22637c478bd9Sstevel@tonic-gate {
22647c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY_CHOICE(cr, PRIV_CONTRACT_OBSERVER, B_FALSE));
22657c478bd9Sstevel@tonic-gate }
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate /*
22687c478bd9Sstevel@tonic-gate  * secpolicy_contract_event
22697c478bd9Sstevel@tonic-gate  *
22707c478bd9Sstevel@tonic-gate  * Determine if the subject may request critical contract events or
22717c478bd9Sstevel@tonic-gate  * reliable contract event delivery.
22727c478bd9Sstevel@tonic-gate  */
22737c478bd9Sstevel@tonic-gate int
22747c478bd9Sstevel@tonic-gate secpolicy_contract_event(const cred_t *cr)
22757c478bd9Sstevel@tonic-gate {
22767c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CONTRACT_EVENT, B_FALSE, EPERM, NULL));
22777c478bd9Sstevel@tonic-gate }
22787c478bd9Sstevel@tonic-gate 
22797c478bd9Sstevel@tonic-gate /*
22807c478bd9Sstevel@tonic-gate  * secpolicy_contract_event_choice
22817c478bd9Sstevel@tonic-gate  *
22827c478bd9Sstevel@tonic-gate  * Determine if the subject may retain contract events in its critical
22837c478bd9Sstevel@tonic-gate  * set when a change in other terms would normally require a change in
22847c478bd9Sstevel@tonic-gate  * the critical set.  Just tests privilege and audits on success.
22857c478bd9Sstevel@tonic-gate  */
22867c478bd9Sstevel@tonic-gate boolean_t
22877c478bd9Sstevel@tonic-gate secpolicy_contract_event_choice(const cred_t *cr)
22887c478bd9Sstevel@tonic-gate {
22897c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY_CHOICE(cr, PRIV_CONTRACT_EVENT, B_FALSE));
22907c478bd9Sstevel@tonic-gate }
22917c478bd9Sstevel@tonic-gate 
22927c478bd9Sstevel@tonic-gate /*
2293ea8dc4b6Seschrock  * secpolicy_gart_access
22947c478bd9Sstevel@tonic-gate  *
2295ea8dc4b6Seschrock  * Determine if the subject has sufficient priveleges to make ioctls to agpgart
2296ea8dc4b6Seschrock  * device.
22977c478bd9Sstevel@tonic-gate  */
22987c478bd9Sstevel@tonic-gate int
22997c478bd9Sstevel@tonic-gate secpolicy_gart_access(const cred_t *cr)
23007c478bd9Sstevel@tonic-gate {
230126f24838Scasper 	return (PRIV_POLICY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE, EPERM, NULL));
23027c478bd9Sstevel@tonic-gate }
23037c478bd9Sstevel@tonic-gate 
23047c478bd9Sstevel@tonic-gate /*
2305ea8dc4b6Seschrock  * secpolicy_gart_map
23067c478bd9Sstevel@tonic-gate  *
2307ea8dc4b6Seschrock  * Determine if the subject has sufficient priveleges to map aperture range
2308ea8dc4b6Seschrock  * through agpgart driver.
23097c478bd9Sstevel@tonic-gate  */
23107c478bd9Sstevel@tonic-gate int
23117c478bd9Sstevel@tonic-gate secpolicy_gart_map(const cred_t *cr)
23127c478bd9Sstevel@tonic-gate {
231326f24838Scasper 	if (PRIV_POLICY_ONLY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE)) {
231426f24838Scasper 		return (PRIV_POLICY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE, EPERM,
231526f24838Scasper 		    NULL));
231626f24838Scasper 	} else {
231726f24838Scasper 		return (PRIV_POLICY(cr, PRIV_GRAPHICS_MAP, B_FALSE, EPERM,
231826f24838Scasper 		    NULL));
23197c478bd9Sstevel@tonic-gate 	}
23207c478bd9Sstevel@tonic-gate }
2321fa9e4066Sahrens 
2322fa9e4066Sahrens /*
2323ea8dc4b6Seschrock  * secpolicy_zinject
2324ea8dc4b6Seschrock  *
2325ea8dc4b6Seschrock  * Determine if the subject can inject faults in the ZFS fault injection
2326ea8dc4b6Seschrock  * framework.  Requires all privileges.
2327ea8dc4b6Seschrock  */
2328ea8dc4b6Seschrock int
2329ea8dc4b6Seschrock secpolicy_zinject(const cred_t *cr)
2330ea8dc4b6Seschrock {
2331*134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
2332ea8dc4b6Seschrock }
2333ea8dc4b6Seschrock 
2334ea8dc4b6Seschrock /*
2335fa9e4066Sahrens  * secpolicy_zfs
2336fa9e4066Sahrens  *
2337ea8dc4b6Seschrock  * Determine if the subject has permission to manipulate ZFS datasets
2338ea8dc4b6Seschrock  * (not pools).  Equivalent to the SYS_MOUNT privilege.
2339fa9e4066Sahrens  */
2340fa9e4066Sahrens int
2341fa9e4066Sahrens secpolicy_zfs(const cred_t *cr)
2342fa9e4066Sahrens {
2343fa9e4066Sahrens 	return (PRIV_POLICY(cr, PRIV_SYS_MOUNT, B_FALSE, EPERM, NULL));
2344fa9e4066Sahrens }
2345f48205beScasper 
2346f48205beScasper /*
2347f48205beScasper  * secpolicy_idmap
2348f48205beScasper  *
2349f48205beScasper  * Determine if the calling process has permissions to register an SID
2350f48205beScasper  * mapping daemon and allocate ephemeral IDs.
2351f48205beScasper  */
2352f48205beScasper int
2353f48205beScasper secpolicy_idmap(const cred_t *cr)
2354f48205beScasper {
2355bda89588Sjp151216 	return (PRIV_POLICY(cr, PRIV_FILE_SETID, B_TRUE, EPERM, NULL));
2356f48205beScasper }
23572449e17fSsherrym 
23582449e17fSsherrym /*
23592449e17fSsherrym  * secpolicy_ucode_update
23602449e17fSsherrym  *
23612449e17fSsherrym  * Determine if the subject has sufficient privilege to update microcode.
23622449e17fSsherrym  */
23632449e17fSsherrym int
23642449e17fSsherrym secpolicy_ucode_update(const cred_t *scr)
23652449e17fSsherrym {
23662449e17fSsherrym 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
23672449e17fSsherrym }
2368e6bdcbd5Sdh155122 
2369e6bdcbd5Sdh155122 /*
2370e6bdcbd5Sdh155122  * secpolicy_sadopen
2371e6bdcbd5Sdh155122  *
2372e6bdcbd5Sdh155122  * Determine if the subject has sufficient privilege to access /dev/sad/admin.
2373e6bdcbd5Sdh155122  * /dev/sad/admin appear in global zone and exclusive-IP zones only.
2374e6bdcbd5Sdh155122  * In global zone, sys_config is required.
2375e6bdcbd5Sdh155122  * In exclusive-IP zones, sys_ip_config is required.
2376e6bdcbd5Sdh155122  * Note that sys_config is prohibited in non-global zones.
2377e6bdcbd5Sdh155122  */
2378e6bdcbd5Sdh155122 int
2379e6bdcbd5Sdh155122 secpolicy_sadopen(const cred_t *credp)
2380e6bdcbd5Sdh155122 {
2381e6bdcbd5Sdh155122 	priv_set_t pset;
2382e6bdcbd5Sdh155122 
2383e6bdcbd5Sdh155122 	priv_emptyset(&pset);
2384e6bdcbd5Sdh155122 
2385e6bdcbd5Sdh155122 	if (crgetzoneid(credp) == GLOBAL_ZONEID)
2386e6bdcbd5Sdh155122 		priv_addset(&pset, PRIV_SYS_CONFIG);
2387e6bdcbd5Sdh155122 	else
2388e6bdcbd5Sdh155122 		priv_addset(&pset, PRIV_SYS_IP_CONFIG);
2389e6bdcbd5Sdh155122 
2390*134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(credp, &pset, "devpolicy", KLPDARG_NONE));
2391e6bdcbd5Sdh155122 }
2392da6c28aaSamw 
2393ddf7fe95Scasper 
2394ddf7fe95Scasper /*
2395ddf7fe95Scasper  * Add privileges to a particular privilege set; this is called when the
2396ddf7fe95Scasper  * current sets of privileges are not sufficient.  I.e., we should always
2397ddf7fe95Scasper  * call the policy override functions from here.
2398ddf7fe95Scasper  * What we are allowed to have is in the Observed Permitted set; so
2399ddf7fe95Scasper  * we compute the difference between that and the newset.
2400ddf7fe95Scasper  */
2401ddf7fe95Scasper int
2402ddf7fe95Scasper secpolicy_require_privs(const cred_t *cr, const priv_set_t *nset)
2403ddf7fe95Scasper {
2404ddf7fe95Scasper 	priv_set_t rqd;
2405ddf7fe95Scasper 
2406ddf7fe95Scasper 	rqd = CR_OPPRIV(cr);
2407ddf7fe95Scasper 
2408ddf7fe95Scasper 	priv_inverse(&rqd);
2409ddf7fe95Scasper 	priv_intersect(nset, &rqd);
2410ddf7fe95Scasper 
2411*134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(cr, &rqd, NULL, KLPDARG_NONE));
2412ddf7fe95Scasper }
2413ddf7fe95Scasper 
2414da6c28aaSamw /*
2415da6c28aaSamw  * secpolicy_smb
2416da6c28aaSamw  *
2417da6c28aaSamw  * Determine if the cred_t has PRIV_SYS_SMB privilege, indicating
2418da6c28aaSamw  * that it has permission to access the smbsrv kernel driver.
2419da6c28aaSamw  * PRIV_POLICY checks the privilege and audits the check.
2420da6c28aaSamw  *
2421da6c28aaSamw  * Returns:
2422da6c28aaSamw  * 0       Driver access is allowed.
2423da6c28aaSamw  * EPERM   Driver access is NOT permitted.
2424da6c28aaSamw  */
2425da6c28aaSamw int
2426da6c28aaSamw secpolicy_smb(const cred_t *cr)
2427da6c28aaSamw {
2428da6c28aaSamw 	return (PRIV_POLICY(cr, PRIV_SYS_SMB, B_FALSE, EPERM, NULL));
2429da6c28aaSamw }
2430911106dfSjm199354 
2431911106dfSjm199354 /*
2432911106dfSjm199354  * secpolicy_vscan
2433911106dfSjm199354  *
2434911106dfSjm199354  * Determine if cred_t has the necessary privileges to access a file
2435911106dfSjm199354  * for virus scanning and update its extended system attributes.
2436911106dfSjm199354  * PRIV_FILE_DAC_SEARCH, PRIV_FILE_DAC_READ - file access
2437911106dfSjm199354  * PRIV_FILE_FLAG_SET - set extended system attributes
2438911106dfSjm199354  *
2439911106dfSjm199354  * PRIV_POLICY checks the privilege and audits the check.
2440911106dfSjm199354  *
2441911106dfSjm199354  * Returns:
2442911106dfSjm199354  * 0      file access for virus scanning allowed.
2443911106dfSjm199354  * EPERM  file access for virus scanning is NOT permitted.
2444911106dfSjm199354  */
2445911106dfSjm199354 int
2446911106dfSjm199354 secpolicy_vscan(const cred_t *cr)
2447911106dfSjm199354 {
2448911106dfSjm199354 	if ((PRIV_POLICY(cr, PRIV_FILE_DAC_SEARCH, B_FALSE, EPERM, NULL)) ||
2449911106dfSjm199354 	    (PRIV_POLICY(cr, PRIV_FILE_DAC_READ, B_FALSE, EPERM, NULL)) ||
2450911106dfSjm199354 	    (PRIV_POLICY(cr, PRIV_FILE_FLAG_SET, B_FALSE, EPERM, NULL))) {
2451911106dfSjm199354 		return (EPERM);
2452911106dfSjm199354 	}
2453911106dfSjm199354 
2454911106dfSjm199354 	return (0);
2455911106dfSjm199354 }
24564bff34e3Sthurlow 
24574bff34e3Sthurlow /*
24584bff34e3Sthurlow  * secpolicy_smbfs_login
24594bff34e3Sthurlow  *
24604bff34e3Sthurlow  * Determines if the caller can add and delete the smbfs login
24614bff34e3Sthurlow  * password in the the nsmb kernel module for the CIFS client.
24624bff34e3Sthurlow  *
24634bff34e3Sthurlow  * Returns:
24644bff34e3Sthurlow  * 0       access is allowed.
24654bff34e3Sthurlow  * EPERM   access is NOT allowed.
24664bff34e3Sthurlow  */
24674bff34e3Sthurlow int
24684bff34e3Sthurlow secpolicy_smbfs_login(const cred_t *cr, uid_t uid)
24694bff34e3Sthurlow {
24704bff34e3Sthurlow 	uid_t cruid = crgetruid(cr);
24714bff34e3Sthurlow 
24724bff34e3Sthurlow 	if (cruid == uid)
24734bff34e3Sthurlow 		return (0);
24744bff34e3Sthurlow 	return (PRIV_POLICY(cr, PRIV_PROC_OWNER, B_FALSE,
24754bff34e3Sthurlow 	    EPERM, NULL));
24764bff34e3Sthurlow }
2477b26a64aeSjohnlev 
2478b26a64aeSjohnlev /*
2479b26a64aeSjohnlev  * secpolicy_xvm_control
2480b26a64aeSjohnlev  *
2481b26a64aeSjohnlev  * Determines if a caller can control the xVM hypervisor and/or running
2482b26a64aeSjohnlev  * domains (x86 specific).
2483b26a64aeSjohnlev  *
2484b26a64aeSjohnlev  * Returns:
2485b26a64aeSjohnlev  * 0       access is allowed.
2486b26a64aeSjohnlev  * EPERM   access is NOT allowed.
2487b26a64aeSjohnlev  */
2488b26a64aeSjohnlev int
2489b26a64aeSjohnlev secpolicy_xvm_control(const cred_t *cr)
2490b26a64aeSjohnlev {
2491b26a64aeSjohnlev 	if (PRIV_POLICY(cr, PRIV_XVM_CONTROL, B_FALSE, EPERM, NULL))
2492b26a64aeSjohnlev 		return (EPERM);
2493b26a64aeSjohnlev 	return (0);
2494b26a64aeSjohnlev }
2495da14cebeSEric Cheng 
2496da14cebeSEric Cheng /*
2497f53eecf5SJames Carlson  * secpolicy_ppp_config
2498f53eecf5SJames Carlson  *
2499f53eecf5SJames Carlson  * Determine if the subject has sufficient privileges to configure PPP and
2500f53eecf5SJames Carlson  * PPP-related devices.
2501f53eecf5SJames Carlson  */
2502f53eecf5SJames Carlson int
2503f53eecf5SJames Carlson secpolicy_ppp_config(const cred_t *cr)
2504f53eecf5SJames Carlson {
2505f53eecf5SJames Carlson 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
2506f53eecf5SJames Carlson 		return (secpolicy_net_config(cr, B_FALSE));
2507f53eecf5SJames Carlson 	return (PRIV_POLICY(cr, PRIV_SYS_PPP_CONFIG, B_FALSE, EPERM, NULL));
2508f53eecf5SJames Carlson }
2509