xref: /titanic_52/usr/src/uts/common/os/policy.c (revision fd9ee8b58485b20072eeef1310a88ff348d5e7fa)
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 /*
22134a1f4eSCasper 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 
149134a1f4eSCasper H.S. Dik #define	FAST_BASIC_CHECK(cr, priv)	\
150134a1f4eSCasper H.S. Dik 	if (PRIV_ISASSERT(&CR_OEPRIV(cr), priv)) { \
151134a1f4eSCasper H.S. Dik 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, B_FALSE); \
152134a1f4eSCasper H.S. Dik 		return (0); \
153134a1f4eSCasper H.S. Dik 	}
154134a1f4eSCasper 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
346134a1f4eSCasper H.S. Dik priv_policy_override_set(const cred_t *cr, const priv_set_t *req, va_list ap)
347ddf7fe95Scasper {
348134a1f4eSCasper H.S. Dik 	if (CR_FLAGS(cr) & PRIV_PFEXEC)
349134a1f4eSCasper 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 
356134a1f4eSCasper H.S. Dik static int
357134a1f4eSCasper H.S. Dik priv_policy_override_set_va(const cred_t *cr, const priv_set_t *req, ...)
358134a1f4eSCasper H.S. Dik {
359134a1f4eSCasper H.S. Dik 	va_list ap;
360134a1f4eSCasper H.S. Dik 	int ret;
361134a1f4eSCasper H.S. Dik 
362134a1f4eSCasper H.S. Dik 	va_start(ap, req);
363134a1f4eSCasper H.S. Dik 	ret = priv_policy_override_set(cr, req, ap);
364134a1f4eSCasper H.S. Dik 	va_end(ap);
365134a1f4eSCasper H.S. Dik 	return (ret);
366134a1f4eSCasper H.S. Dik }
367134a1f4eSCasper 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 {
437134a1f4eSCasper 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
484134a1f4eSCasper H.S. Dik secpolicy_require_set(const cred_t *cr, const priv_set_t *req,
485134a1f4eSCasper 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;
490134a1f4eSCasper H.S. Dik 	va_list ap;
491134a1f4eSCasper 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 
498134a1f4eSCasper H.S. Dik 	va_start(ap, msg);
499134a1f4eSCasper H.S. Dik 	ret = priv_policy_override_set(cr, req, ap);
500134a1f4eSCasper H.S. Dik 	va_end(ap);
501134a1f4eSCasper 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.
567134a1f4eSCasper H.S. Dik  * When adding a new privilege which allows binding to currently privileged
568134a1f4eSCasper H.S. Dik  * ports, then you MUST also allow processes with PRIV_NET_PRIVADDR bind
569134a1f4eSCasper 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 		/*
583134a1f4eSCasper H.S. Dik 		 * NBT and SMB ports, these are normal privileged ports,
584134a1f4eSCasper H.S. Dik 		 * allow bind only if the SYS_SMB or NET_PRIVADDR privilege
585134a1f4eSCasper H.S. Dik 		 * is present.
586134a1f4eSCasper H.S. Dik 		 * Try both, if neither is present return an error for
587134a1f4eSCasper H.S. Dik 		 * priv SYS_SMB.
588da6c28aaSamw 		 */
589134a1f4eSCasper H.S. Dik 		if (PRIV_POLICY_ONLY(cr, PRIV_NET_PRIVADDR, B_FALSE))
590134a1f4eSCasper H.S. Dik 			priv = PRIV_NET_PRIVADDR;
591134a1f4eSCasper 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 
718134a1f4eSCasper H.S. Dik 		if (secpolicy_vnode_access2(cr, mvp, va.va_uid, va.va_mode,
719134a1f4eSCasper 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 
7580fbb751dSJohn Levon int
7590fbb751dSJohn Levon secpolicy_fs_allowed_mount(const char *fsname)
7600fbb751dSJohn Levon {
7610fbb751dSJohn Levon 	struct vfssw *vswp;
7620fbb751dSJohn Levon 	const char *p;
7630fbb751dSJohn Levon 	size_t len;
7640fbb751dSJohn Levon 
7650fbb751dSJohn Levon 	ASSERT(fsname != NULL);
7660fbb751dSJohn Levon 	ASSERT(fsname[0] != '\0');
7670fbb751dSJohn Levon 
7680fbb751dSJohn Levon 	if (INGLOBALZONE(curproc))
7690fbb751dSJohn Levon 		return (0);
7700fbb751dSJohn Levon 
7710fbb751dSJohn Levon 	vswp = vfs_getvfssw(fsname);
7720fbb751dSJohn Levon 	if (vswp == NULL)
7730fbb751dSJohn Levon 		return (ENOENT);
7740fbb751dSJohn Levon 
7750fbb751dSJohn Levon 	if ((vswp->vsw_flag & VSW_ZMOUNT) != 0) {
7760fbb751dSJohn Levon 		vfs_unrefvfssw(vswp);
7770fbb751dSJohn Levon 		return (0);
7780fbb751dSJohn Levon 	}
7790fbb751dSJohn Levon 
7800fbb751dSJohn Levon 	vfs_unrefvfssw(vswp);
7810fbb751dSJohn Levon 
7820fbb751dSJohn Levon 	p = curzone->zone_fs_allowed;
7830fbb751dSJohn Levon 	len = strlen(fsname);
7840fbb751dSJohn Levon 
7850fbb751dSJohn Levon 	while (p != NULL && *p != '\0') {
7860fbb751dSJohn Levon 		if (strncmp(p, fsname, len) == 0) {
7870fbb751dSJohn Levon 			char c = *(p + len);
7880fbb751dSJohn Levon 			if (c == '\0' || c == ',')
7890fbb751dSJohn Levon 				return (0);
7900fbb751dSJohn Levon 		}
7910fbb751dSJohn Levon 
7920fbb751dSJohn Levon 		/* skip to beyond the next comma */
7930fbb751dSJohn Levon 		if ((p = strchr(p, ',')) != NULL)
7940fbb751dSJohn Levon 			p++;
7950fbb751dSJohn Levon 	}
7960fbb751dSJohn Levon 
7970fbb751dSJohn Levon 	return (EPERM);
7980fbb751dSJohn Levon }
7990fbb751dSJohn Levon 
800ecd6cf80Smarks extern vnode_t *rootvp;
801ecd6cf80Smarks extern vfs_t *rootvfs;
802ecd6cf80Smarks 
803ecd6cf80Smarks int
804ecd6cf80Smarks secpolicy_fs_mount(cred_t *cr, vnode_t *mvp, struct vfs *vfsp)
805ecd6cf80Smarks {
806ecd6cf80Smarks 	boolean_t needoptchk;
807ecd6cf80Smarks 	int error;
808ecd6cf80Smarks 
809ecd6cf80Smarks 	/*
810ecd6cf80Smarks 	 * If it's a remount, get the underlying mount point,
811ecd6cf80Smarks 	 * except for the root where we use the rootvp.
812ecd6cf80Smarks 	 */
813ecd6cf80Smarks 	if ((vfsp->vfs_flag & VFS_REMOUNT) != 0) {
814ecd6cf80Smarks 		if (vfsp == rootvfs)
815ecd6cf80Smarks 			mvp = rootvp;
816ecd6cf80Smarks 		else
817ecd6cf80Smarks 			mvp = vfsp->vfs_vnodecovered;
818ecd6cf80Smarks 	}
819ecd6cf80Smarks 
820ecd6cf80Smarks 	error = secpolicy_fs_common(cr, mvp, vfsp, &needoptchk);
821ecd6cf80Smarks 
822ecd6cf80Smarks 	if (error == 0 && needoptchk) {
823ecd6cf80Smarks 		secpolicy_fs_mount_clearopts(cr, vfsp);
824ecd6cf80Smarks 	}
825ecd6cf80Smarks 
8267c478bd9Sstevel@tonic-gate 	return (error);
8277c478bd9Sstevel@tonic-gate }
8287c478bd9Sstevel@tonic-gate 
8297c478bd9Sstevel@tonic-gate /*
8307c478bd9Sstevel@tonic-gate  * Does the policy computations for "ownership" of a mount;
8317c478bd9Sstevel@tonic-gate  * here ownership is defined as the ability to "mount"
8327c478bd9Sstevel@tonic-gate  * the filesystem originally.  The rootvfs doesn't cover any
8337c478bd9Sstevel@tonic-gate  * vnodes; we attribute its ownership to the rootvp.
8347c478bd9Sstevel@tonic-gate  */
8357c478bd9Sstevel@tonic-gate static int
8367c478bd9Sstevel@tonic-gate secpolicy_fs_owner(cred_t *cr, const struct vfs *vfsp)
8377c478bd9Sstevel@tonic-gate {
8387c478bd9Sstevel@tonic-gate 	vnode_t *mvp;
8397c478bd9Sstevel@tonic-gate 
8407c478bd9Sstevel@tonic-gate 	if (vfsp == NULL)
8417c478bd9Sstevel@tonic-gate 		mvp = NULL;
8427c478bd9Sstevel@tonic-gate 	else if (vfsp == rootvfs)
8437c478bd9Sstevel@tonic-gate 		mvp = rootvp;
8447c478bd9Sstevel@tonic-gate 	else
8457c478bd9Sstevel@tonic-gate 		mvp = vfsp->vfs_vnodecovered;
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_common(cr, mvp, vfsp, NULL));
8487c478bd9Sstevel@tonic-gate }
8497c478bd9Sstevel@tonic-gate 
8507c478bd9Sstevel@tonic-gate int
8517c478bd9Sstevel@tonic-gate secpolicy_fs_unmount(cred_t *cr, struct vfs *vfsp)
8527c478bd9Sstevel@tonic-gate {
8537c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_owner(cr, vfsp));
8547c478bd9Sstevel@tonic-gate }
8557c478bd9Sstevel@tonic-gate 
8567c478bd9Sstevel@tonic-gate /*
8577c478bd9Sstevel@tonic-gate  * Quotas are a resource, but if one has the ability to mount a filesystem, he
8587c478bd9Sstevel@tonic-gate  * should be able to modify quotas on it.
8597c478bd9Sstevel@tonic-gate  */
8607c478bd9Sstevel@tonic-gate int
8617c478bd9Sstevel@tonic-gate secpolicy_fs_quota(const cred_t *cr, const vfs_t *vfsp)
8627c478bd9Sstevel@tonic-gate {
8637c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
8647c478bd9Sstevel@tonic-gate }
8657c478bd9Sstevel@tonic-gate 
8667c478bd9Sstevel@tonic-gate /*
8677c478bd9Sstevel@tonic-gate  * Exceeding minfree: also a per-mount resource constraint.
8687c478bd9Sstevel@tonic-gate  */
8697c478bd9Sstevel@tonic-gate int
8707c478bd9Sstevel@tonic-gate secpolicy_fs_minfree(const cred_t *cr, const vfs_t *vfsp)
8717c478bd9Sstevel@tonic-gate {
8727c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
8737c478bd9Sstevel@tonic-gate }
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate int
8767c478bd9Sstevel@tonic-gate secpolicy_fs_config(const cred_t *cr, const vfs_t *vfsp)
8777c478bd9Sstevel@tonic-gate {
8787c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
8797c478bd9Sstevel@tonic-gate }
8807c478bd9Sstevel@tonic-gate 
8817c478bd9Sstevel@tonic-gate /* ARGSUSED */
8827c478bd9Sstevel@tonic-gate int
8837c478bd9Sstevel@tonic-gate secpolicy_fs_linkdir(const cred_t *cr, const vfs_t *vfsp)
8847c478bd9Sstevel@tonic-gate {
8857c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_LINKDIR, B_FALSE, EPERM, NULL));
8867c478bd9Sstevel@tonic-gate }
8877c478bd9Sstevel@tonic-gate 
8887c478bd9Sstevel@tonic-gate /*
8897c478bd9Sstevel@tonic-gate  * Name:        secpolicy_vnode_access()
8907c478bd9Sstevel@tonic-gate  *
8917c478bd9Sstevel@tonic-gate  * Parameters:  Process credential
8927c478bd9Sstevel@tonic-gate  *		vnode
8937c478bd9Sstevel@tonic-gate  *		uid of owner of vnode
8947c478bd9Sstevel@tonic-gate  *		permission bits not granted to the caller when examining
8957c478bd9Sstevel@tonic-gate  *		file mode bits (i.e., when a process wants to open a
8967c478bd9Sstevel@tonic-gate  *		mode 444 file for VREAD|VWRITE, this function should be
8977c478bd9Sstevel@tonic-gate  *		called only with a VWRITE argument).
8987c478bd9Sstevel@tonic-gate  *
8997c478bd9Sstevel@tonic-gate  * Normal:      Verifies that cred has the appropriate privileges to
9007c478bd9Sstevel@tonic-gate  *              override the mode bits that were denied.
9017c478bd9Sstevel@tonic-gate  *
9027c478bd9Sstevel@tonic-gate  * Override:    file_dac_execute - if VEXEC bit was denied and vnode is
9037c478bd9Sstevel@tonic-gate  *                      not a directory.
9047c478bd9Sstevel@tonic-gate  *              file_dac_read - if VREAD bit was denied.
9057c478bd9Sstevel@tonic-gate  *              file_dac_search - if VEXEC bit was denied and vnode is
9067c478bd9Sstevel@tonic-gate  *                      a directory.
9077c478bd9Sstevel@tonic-gate  *              file_dac_write - if VWRITE bit was denied.
9087c478bd9Sstevel@tonic-gate  *
9097c478bd9Sstevel@tonic-gate  *		Root owned files are special cased to protect system
9107c478bd9Sstevel@tonic-gate  *		configuration files and such.
9117c478bd9Sstevel@tonic-gate  *
9127c478bd9Sstevel@tonic-gate  * Output:      EACCES - if privilege check fails.
9137c478bd9Sstevel@tonic-gate  */
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate int
9167c478bd9Sstevel@tonic-gate secpolicy_vnode_access(const cred_t *cr, vnode_t *vp, uid_t owner, mode_t mode)
9177c478bd9Sstevel@tonic-gate {
918ddf7fe95Scasper 	if ((mode & VREAD) && priv_policy_va(cr, PRIV_FILE_DAC_READ, B_FALSE,
919ddf7fe95Scasper 	    EACCES, NULL, KLPDARG_VNODE, vp, (char *)NULL,
920ddf7fe95Scasper 	    KLPDARG_NOMORE) != 0) {
9217c478bd9Sstevel@tonic-gate 		return (EACCES);
922ddf7fe95Scasper 	}
9237c478bd9Sstevel@tonic-gate 
9247c478bd9Sstevel@tonic-gate 	if (mode & VWRITE) {
9257c478bd9Sstevel@tonic-gate 		boolean_t allzone;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		if (owner == 0 && cr->cr_uid != 0)
9287c478bd9Sstevel@tonic-gate 			allzone = B_TRUE;
9297c478bd9Sstevel@tonic-gate 		else
9307c478bd9Sstevel@tonic-gate 			allzone = B_FALSE;
931ddf7fe95Scasper 		if (priv_policy_va(cr, PRIV_FILE_DAC_WRITE, allzone, EACCES,
932ddf7fe95Scasper 		    NULL, KLPDARG_VNODE, vp, (char *)NULL,
933ddf7fe95Scasper 		    KLPDARG_NOMORE) != 0) {
9347c478bd9Sstevel@tonic-gate 			return (EACCES);
9357c478bd9Sstevel@tonic-gate 		}
936ddf7fe95Scasper 	}
9377c478bd9Sstevel@tonic-gate 
9387c478bd9Sstevel@tonic-gate 	if (mode & VEXEC) {
9397c478bd9Sstevel@tonic-gate 		/*
9407c478bd9Sstevel@tonic-gate 		 * Directories use file_dac_search to override the execute bit.
9417c478bd9Sstevel@tonic-gate 		 */
942ddf7fe95Scasper 		int p = vp->v_type == VDIR ? PRIV_FILE_DAC_SEARCH :
943ddf7fe95Scasper 		    PRIV_FILE_DAC_EXECUTE;
9447c478bd9Sstevel@tonic-gate 
945ddf7fe95Scasper 		return (priv_policy_va(cr, p, B_FALSE, EACCES, NULL,
946ddf7fe95Scasper 		    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
9477c478bd9Sstevel@tonic-gate 	}
9487c478bd9Sstevel@tonic-gate 	return (0);
9497c478bd9Sstevel@tonic-gate }
9507c478bd9Sstevel@tonic-gate 
9517c478bd9Sstevel@tonic-gate /*
952134a1f4eSCasper H.S. Dik  * Like secpolicy_vnode_access() but we get the actual wanted mode and the
953134a1f4eSCasper H.S. Dik  * current mode of the file, not the missing bits.
954134a1f4eSCasper H.S. Dik  */
955134a1f4eSCasper H.S. Dik int
956134a1f4eSCasper H.S. Dik secpolicy_vnode_access2(const cred_t *cr, vnode_t *vp, uid_t owner,
957134a1f4eSCasper H.S. Dik     mode_t curmode, mode_t wantmode)
958134a1f4eSCasper H.S. Dik {
959134a1f4eSCasper H.S. Dik 	mode_t mode;
960134a1f4eSCasper H.S. Dik 
961134a1f4eSCasper H.S. Dik 	/* Inline the basic privileges tests. */
962134a1f4eSCasper H.S. Dik 	if ((wantmode & VREAD) &&
963134a1f4eSCasper H.S. Dik 	    !PRIV_ISASSERT(&CR_OEPRIV(cr), PRIV_FILE_READ) &&
964134a1f4eSCasper H.S. Dik 	    priv_policy_va(cr, PRIV_FILE_READ, B_FALSE, EACCES, NULL,
965134a1f4eSCasper H.S. Dik 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE) != 0) {
966134a1f4eSCasper H.S. Dik 		return (EACCES);
967134a1f4eSCasper H.S. Dik 	}
968134a1f4eSCasper H.S. Dik 
969134a1f4eSCasper H.S. Dik 	if ((wantmode & VWRITE) &&
970134a1f4eSCasper H.S. Dik 	    !PRIV_ISASSERT(&CR_OEPRIV(cr), PRIV_FILE_WRITE) &&
971134a1f4eSCasper H.S. Dik 	    priv_policy_va(cr, PRIV_FILE_WRITE, B_FALSE, EACCES, NULL,
972134a1f4eSCasper H.S. Dik 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE) != 0) {
973134a1f4eSCasper H.S. Dik 		return (EACCES);
974134a1f4eSCasper H.S. Dik 	}
975134a1f4eSCasper H.S. Dik 
976134a1f4eSCasper H.S. Dik 	mode = ~curmode & wantmode;
977134a1f4eSCasper H.S. Dik 
978134a1f4eSCasper H.S. Dik 	if (mode == 0)
979134a1f4eSCasper H.S. Dik 		return (0);
980134a1f4eSCasper H.S. Dik 
981134a1f4eSCasper H.S. Dik 	if ((mode & VREAD) && priv_policy_va(cr, PRIV_FILE_DAC_READ, B_FALSE,
982134a1f4eSCasper H.S. Dik 	    EACCES, NULL, KLPDARG_VNODE, vp, (char *)NULL,
983134a1f4eSCasper H.S. Dik 	    KLPDARG_NOMORE) != 0) {
984134a1f4eSCasper H.S. Dik 		return (EACCES);
985134a1f4eSCasper H.S. Dik 	}
986134a1f4eSCasper H.S. Dik 
987134a1f4eSCasper H.S. Dik 	if (mode & VWRITE) {
988134a1f4eSCasper H.S. Dik 		boolean_t allzone;
989134a1f4eSCasper H.S. Dik 
990134a1f4eSCasper H.S. Dik 		if (owner == 0 && cr->cr_uid != 0)
991134a1f4eSCasper H.S. Dik 			allzone = B_TRUE;
992134a1f4eSCasper H.S. Dik 		else
993134a1f4eSCasper H.S. Dik 			allzone = B_FALSE;
994134a1f4eSCasper H.S. Dik 		if (priv_policy_va(cr, PRIV_FILE_DAC_WRITE, allzone, EACCES,
995134a1f4eSCasper H.S. Dik 		    NULL, KLPDARG_VNODE, vp, (char *)NULL,
996134a1f4eSCasper H.S. Dik 		    KLPDARG_NOMORE) != 0) {
997134a1f4eSCasper H.S. Dik 			return (EACCES);
998134a1f4eSCasper H.S. Dik 		}
999134a1f4eSCasper H.S. Dik 	}
1000134a1f4eSCasper H.S. Dik 
1001134a1f4eSCasper H.S. Dik 	if (mode & VEXEC) {
1002134a1f4eSCasper H.S. Dik 		/*
1003134a1f4eSCasper H.S. Dik 		 * Directories use file_dac_search to override the execute bit.
1004134a1f4eSCasper H.S. Dik 		 */
1005134a1f4eSCasper H.S. Dik 		int p = vp->v_type == VDIR ? PRIV_FILE_DAC_SEARCH :
1006134a1f4eSCasper H.S. Dik 		    PRIV_FILE_DAC_EXECUTE;
1007134a1f4eSCasper H.S. Dik 
1008134a1f4eSCasper H.S. Dik 		return (priv_policy_va(cr, p, B_FALSE, EACCES, NULL,
1009134a1f4eSCasper H.S. Dik 		    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
1010134a1f4eSCasper H.S. Dik 	}
1011134a1f4eSCasper H.S. Dik 	return (0);
1012134a1f4eSCasper H.S. Dik }
1013134a1f4eSCasper H.S. Dik 
1014134a1f4eSCasper H.S. Dik /*
1015134a1f4eSCasper H.S. Dik  * This is a special routine for ZFS; it is used to determine whether
1016134a1f4eSCasper H.S. Dik  * any of the privileges in effect allow any form of access to the
1017134a1f4eSCasper H.S. Dik  * file.  There's no reason to audit this or any reason to record
1018134a1f4eSCasper H.S. Dik  * this.  More work is needed to do the "KPLD" stuff.
1019134a1f4eSCasper H.S. Dik  */
1020134a1f4eSCasper H.S. Dik int
1021134a1f4eSCasper H.S. Dik secpolicy_vnode_any_access(const cred_t *cr, vnode_t *vp, uid_t owner)
1022134a1f4eSCasper H.S. Dik {
1023134a1f4eSCasper H.S. Dik 	static int privs[] = {
1024134a1f4eSCasper H.S. Dik 	    PRIV_FILE_OWNER,
1025060fafacSCasper H.S. Dik 	    PRIV_FILE_CHOWN,
1026134a1f4eSCasper H.S. Dik 	    PRIV_FILE_DAC_READ,
1027134a1f4eSCasper H.S. Dik 	    PRIV_FILE_DAC_WRITE,
1028134a1f4eSCasper H.S. Dik 	    PRIV_FILE_DAC_EXECUTE,
1029134a1f4eSCasper H.S. Dik 	    PRIV_FILE_DAC_SEARCH,
1030134a1f4eSCasper H.S. Dik 	};
1031134a1f4eSCasper H.S. Dik 	int i;
1032134a1f4eSCasper H.S. Dik 
1033134a1f4eSCasper H.S. Dik 	/* Same as secpolicy_vnode_setdac */
1034134a1f4eSCasper H.S. Dik 	if (owner == cr->cr_uid)
1035134a1f4eSCasper H.S. Dik 		return (0);
1036134a1f4eSCasper H.S. Dik 
1037134a1f4eSCasper H.S. Dik 	for (i = 0; i < sizeof (privs)/sizeof (int); i++) {
1038134a1f4eSCasper H.S. Dik 		boolean_t allzone = B_FALSE;
1039134a1f4eSCasper H.S. Dik 		int priv;
1040134a1f4eSCasper H.S. Dik 
1041134a1f4eSCasper H.S. Dik 		switch (priv = privs[i]) {
1042134a1f4eSCasper H.S. Dik 		case PRIV_FILE_DAC_EXECUTE:
1043134a1f4eSCasper H.S. Dik 			if (vp->v_type == VDIR)
1044134a1f4eSCasper H.S. Dik 				continue;
1045134a1f4eSCasper H.S. Dik 			break;
1046134a1f4eSCasper H.S. Dik 		case PRIV_FILE_DAC_SEARCH:
1047134a1f4eSCasper H.S. Dik 			if (vp->v_type != VDIR)
1048134a1f4eSCasper H.S. Dik 				continue;
1049134a1f4eSCasper H.S. Dik 			break;
1050134a1f4eSCasper H.S. Dik 		case PRIV_FILE_DAC_WRITE:
1051134a1f4eSCasper H.S. Dik 		case PRIV_FILE_OWNER:
1052060fafacSCasper H.S. Dik 		case PRIV_FILE_CHOWN:
1053134a1f4eSCasper H.S. Dik 			/* We know here that if owner == 0, that cr_uid != 0 */
1054134a1f4eSCasper H.S. Dik 			allzone = owner == 0;
1055134a1f4eSCasper H.S. Dik 			break;
1056134a1f4eSCasper H.S. Dik 		}
1057134a1f4eSCasper H.S. Dik 		if (PRIV_POLICY_CHOICE(cr, priv, allzone))
1058134a1f4eSCasper H.S. Dik 			return (0);
1059134a1f4eSCasper H.S. Dik 	}
1060134a1f4eSCasper H.S. Dik 	return (EPERM);
1061134a1f4eSCasper H.S. Dik }
1062134a1f4eSCasper H.S. Dik 
1063134a1f4eSCasper H.S. Dik /*
10647c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_setid_modify()
10657c478bd9Sstevel@tonic-gate  *
10667c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can set the file setid flags.
10677c478bd9Sstevel@tonic-gate  *
10687c478bd9Sstevel@tonic-gate  * Output:	EPERM - if not privileged.
10697c478bd9Sstevel@tonic-gate  */
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate static int
10727c478bd9Sstevel@tonic-gate secpolicy_vnode_setid_modify(const cred_t *cr, uid_t owner)
10737c478bd9Sstevel@tonic-gate {
10747c478bd9Sstevel@tonic-gate 	/* If changing to suid root, must have all zone privs */
10757c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_TRUE;
10767c478bd9Sstevel@tonic-gate 
10777c478bd9Sstevel@tonic-gate 	if (owner != 0) {
10787c478bd9Sstevel@tonic-gate 		if (owner == cr->cr_uid)
10797c478bd9Sstevel@tonic-gate 			return (0);
10807c478bd9Sstevel@tonic-gate 		allzone = B_FALSE;
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_SETID, allzone, EPERM, NULL));
10837c478bd9Sstevel@tonic-gate }
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate /*
10867c478bd9Sstevel@tonic-gate  * Are we allowed to retain the set-uid/set-gid bits when
10877c478bd9Sstevel@tonic-gate  * changing ownership or when writing to a file?
10887c478bd9Sstevel@tonic-gate  * "issuid" should be true when set-uid; only in that case
10897c478bd9Sstevel@tonic-gate  * root ownership is checked (setgid is assumed).
10907c478bd9Sstevel@tonic-gate  */
10917c478bd9Sstevel@tonic-gate int
10927c478bd9Sstevel@tonic-gate secpolicy_vnode_setid_retain(const cred_t *cred, boolean_t issuidroot)
10937c478bd9Sstevel@tonic-gate {
10947c478bd9Sstevel@tonic-gate 	if (issuidroot && !HAS_ALLZONEPRIVS(cred))
10957c478bd9Sstevel@tonic-gate 		return (EPERM);
10967c478bd9Sstevel@tonic-gate 
10977c478bd9Sstevel@tonic-gate 	return (!PRIV_POLICY_CHOICE(cred, PRIV_FILE_SETID, B_FALSE));
10987c478bd9Sstevel@tonic-gate }
10997c478bd9Sstevel@tonic-gate 
11007c478bd9Sstevel@tonic-gate /*
11017c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_setids_setgids()
11027c478bd9Sstevel@tonic-gate  *
11037c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can set the file setgid flag.
11047c478bd9Sstevel@tonic-gate  *
11057c478bd9Sstevel@tonic-gate  * Output:	EPERM - if not privileged
11067c478bd9Sstevel@tonic-gate  */
11077c478bd9Sstevel@tonic-gate 
11087c478bd9Sstevel@tonic-gate int
11097c478bd9Sstevel@tonic-gate secpolicy_vnode_setids_setgids(const cred_t *cred, gid_t gid)
11107c478bd9Sstevel@tonic-gate {
11117c478bd9Sstevel@tonic-gate 	if (!groupmember(gid, cred))
11127c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cred, PRIV_FILE_SETID, B_FALSE, EPERM,
11137c478bd9Sstevel@tonic-gate 		    NULL));
11147c478bd9Sstevel@tonic-gate 	return (0);
11157c478bd9Sstevel@tonic-gate }
11167c478bd9Sstevel@tonic-gate 
11177c478bd9Sstevel@tonic-gate /*
111847def0dcSMark Shellenbaum  * Name:	secpolicy_vnode_chown
111947def0dcSMark Shellenbaum  *
112047def0dcSMark Shellenbaum  * Normal:	Determine if subject can chown owner of a file.
112147def0dcSMark Shellenbaum  *
112247def0dcSMark Shellenbaum  * Output:	EPERM - if access denied
11237c478bd9Sstevel@tonic-gate  */
112447def0dcSMark Shellenbaum 
11257c478bd9Sstevel@tonic-gate int
1126e02bc683SMark Shellenbaum secpolicy_vnode_chown(const cred_t *cred, uid_t owner)
11277c478bd9Sstevel@tonic-gate {
1128e02bc683SMark Shellenbaum 	boolean_t is_owner = (owner == crgetuid(cred));
1129e02bc683SMark Shellenbaum 	boolean_t allzone = B_FALSE;
1130e02bc683SMark Shellenbaum 	int priv;
1131e02bc683SMark Shellenbaum 
1132e02bc683SMark Shellenbaum 	if (!is_owner) {
1133e02bc683SMark Shellenbaum 		allzone = (owner == 0);
1134e02bc683SMark Shellenbaum 		priv = PRIV_FILE_CHOWN;
1135e02bc683SMark Shellenbaum 	} else {
1136e02bc683SMark Shellenbaum 		priv = HAS_PRIVILEGE(cred, PRIV_FILE_CHOWN) ?
1137e02bc683SMark Shellenbaum 		    PRIV_FILE_CHOWN : PRIV_FILE_CHOWN_SELF;
1138e02bc683SMark Shellenbaum 	}
1139e02bc683SMark Shellenbaum 
1140e02bc683SMark Shellenbaum 	return (PRIV_POLICY(cred, priv, allzone, EPERM, NULL));
114147def0dcSMark Shellenbaum }
114247def0dcSMark Shellenbaum 
114347def0dcSMark Shellenbaum /*
114447def0dcSMark Shellenbaum  * Name:	secpolicy_vnode_create_gid
114547def0dcSMark Shellenbaum  *
114647def0dcSMark Shellenbaum  * Normal:	Determine if subject can change group ownership of a file.
114747def0dcSMark Shellenbaum  *
114847def0dcSMark Shellenbaum  * Output:	EPERM - if access denied
114947def0dcSMark Shellenbaum  */
115047def0dcSMark Shellenbaum int
115147def0dcSMark Shellenbaum secpolicy_vnode_create_gid(const cred_t *cred)
115247def0dcSMark Shellenbaum {
1153e02bc683SMark Shellenbaum 	if (HAS_PRIVILEGE(cred, PRIV_FILE_CHOWN))
1154e02bc683SMark Shellenbaum 		return (PRIV_POLICY(cred, PRIV_FILE_CHOWN, B_FALSE, EPERM,
1155e02bc683SMark Shellenbaum 		    NULL));
1156e02bc683SMark Shellenbaum 	else
1157e02bc683SMark Shellenbaum 		return (PRIV_POLICY(cred, PRIV_FILE_CHOWN_SELF, B_FALSE, EPERM,
1158e02bc683SMark Shellenbaum 		    NULL));
11597c478bd9Sstevel@tonic-gate }
11607c478bd9Sstevel@tonic-gate 
11617c478bd9Sstevel@tonic-gate /*
11627c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_utime_modify()
11637c478bd9Sstevel@tonic-gate  *
11647c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can modify the utime on a file.
11657c478bd9Sstevel@tonic-gate  *
11667c478bd9Sstevel@tonic-gate  * Output:	EPERM - if access denied.
11677c478bd9Sstevel@tonic-gate  */
11687c478bd9Sstevel@tonic-gate 
11697c478bd9Sstevel@tonic-gate static int
11707c478bd9Sstevel@tonic-gate secpolicy_vnode_utime_modify(const cred_t *cred)
11717c478bd9Sstevel@tonic-gate {
11727c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_FILE_OWNER, B_FALSE, EPERM,
11737c478bd9Sstevel@tonic-gate 	    "modify file times"));
11747c478bd9Sstevel@tonic-gate }
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 
11777c478bd9Sstevel@tonic-gate /*
11787c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_setdac()
11797c478bd9Sstevel@tonic-gate  *
11807c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can modify the mode of a file.
11817c478bd9Sstevel@tonic-gate  *		allzone privilege needed when modifying root owned object.
11827c478bd9Sstevel@tonic-gate  *
11837c478bd9Sstevel@tonic-gate  * Output:	EPERM - if access denied.
11847c478bd9Sstevel@tonic-gate  */
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate int
11877c478bd9Sstevel@tonic-gate secpolicy_vnode_setdac(const cred_t *cred, uid_t owner)
11887c478bd9Sstevel@tonic-gate {
11897c478bd9Sstevel@tonic-gate 	if (owner == cred->cr_uid)
11907c478bd9Sstevel@tonic-gate 		return (0);
11917c478bd9Sstevel@tonic-gate 
11927c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_FILE_OWNER, owner == 0, EPERM, NULL));
11937c478bd9Sstevel@tonic-gate }
11947c478bd9Sstevel@tonic-gate /*
11957c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_stky_modify()
11967c478bd9Sstevel@tonic-gate  *
11977c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can make a file a "sticky".
11987c478bd9Sstevel@tonic-gate  *
11997c478bd9Sstevel@tonic-gate  * Output:	EPERM - if access denied.
12007c478bd9Sstevel@tonic-gate  */
12017c478bd9Sstevel@tonic-gate 
12027c478bd9Sstevel@tonic-gate int
12037c478bd9Sstevel@tonic-gate secpolicy_vnode_stky_modify(const cred_t *cred)
12047c478bd9Sstevel@tonic-gate {
12057c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_SYS_CONFIG, B_FALSE, EPERM,
12067c478bd9Sstevel@tonic-gate 	    "set file sticky"));
12077c478bd9Sstevel@tonic-gate }
12087c478bd9Sstevel@tonic-gate 
12097c478bd9Sstevel@tonic-gate /*
12107c478bd9Sstevel@tonic-gate  * Policy determines whether we can remove an entry from a directory,
12117c478bd9Sstevel@tonic-gate  * regardless of permission bits.
12127c478bd9Sstevel@tonic-gate  */
12137c478bd9Sstevel@tonic-gate int
12147c478bd9Sstevel@tonic-gate secpolicy_vnode_remove(const cred_t *cr)
12157c478bd9Sstevel@tonic-gate {
12167c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_OWNER, B_FALSE, EACCES,
12177c478bd9Sstevel@tonic-gate 	    "sticky directory"));
12187c478bd9Sstevel@tonic-gate }
12197c478bd9Sstevel@tonic-gate 
12207c478bd9Sstevel@tonic-gate int
12217c478bd9Sstevel@tonic-gate secpolicy_vnode_owner(const cred_t *cr, uid_t owner)
12227c478bd9Sstevel@tonic-gate {
12237c478bd9Sstevel@tonic-gate 	boolean_t allzone = (owner == 0);
12247c478bd9Sstevel@tonic-gate 
12257c478bd9Sstevel@tonic-gate 	if (owner == cr->cr_uid)
12267c478bd9Sstevel@tonic-gate 		return (0);
12277c478bd9Sstevel@tonic-gate 
12287c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_OWNER, allzone, EPERM, NULL));
12297c478bd9Sstevel@tonic-gate }
12307c478bd9Sstevel@tonic-gate 
123113f9f30eSmarks void
123213f9f30eSmarks secpolicy_setid_clear(vattr_t *vap, cred_t *cr)
123313f9f30eSmarks {
123413f9f30eSmarks 	if ((vap->va_mode & (S_ISUID | S_ISGID)) != 0 &&
123513f9f30eSmarks 	    secpolicy_vnode_setid_retain(cr,
123613f9f30eSmarks 	    (vap->va_mode & S_ISUID) != 0 &&
123713f9f30eSmarks 	    (vap->va_mask & AT_UID) != 0 && vap->va_uid == 0) != 0) {
123813f9f30eSmarks 		vap->va_mask |= AT_MODE;
123913f9f30eSmarks 		vap->va_mode &= ~(S_ISUID|S_ISGID);
124013f9f30eSmarks 	}
124113f9f30eSmarks }
124213f9f30eSmarks 
1243f92daba9Smarks int
1244f92daba9Smarks secpolicy_setid_setsticky_clear(vnode_t *vp, vattr_t *vap, const vattr_t *ovap,
1245f92daba9Smarks     cred_t *cr)
1246f92daba9Smarks {
1247f92daba9Smarks 	int error;
1248f92daba9Smarks 
1249f92daba9Smarks 	if ((vap->va_mode & S_ISUID) != 0 &&
1250f92daba9Smarks 	    (error = secpolicy_vnode_setid_modify(cr,
1251f92daba9Smarks 	    ovap->va_uid)) != 0) {
1252f92daba9Smarks 		return (error);
1253f92daba9Smarks 	}
1254f92daba9Smarks 
1255f92daba9Smarks 	/*
1256f92daba9Smarks 	 * Check privilege if attempting to set the
1257f92daba9Smarks 	 * sticky bit on a non-directory.
1258f92daba9Smarks 	 */
1259f92daba9Smarks 	if (vp->v_type != VDIR && (vap->va_mode & S_ISVTX) != 0 &&
1260f92daba9Smarks 	    secpolicy_vnode_stky_modify(cr) != 0) {
1261f92daba9Smarks 		vap->va_mode &= ~S_ISVTX;
1262f92daba9Smarks 	}
1263f92daba9Smarks 
1264f92daba9Smarks 	/*
1265f92daba9Smarks 	 * Check for privilege if attempting to set the
1266f92daba9Smarks 	 * group-id bit.
1267f92daba9Smarks 	 */
1268f92daba9Smarks 	if ((vap->va_mode & S_ISGID) != 0 &&
1269f92daba9Smarks 	    secpolicy_vnode_setids_setgids(cr, ovap->va_gid) != 0) {
1270f92daba9Smarks 		vap->va_mode &= ~S_ISGID;
1271f92daba9Smarks 	}
1272f92daba9Smarks 
1273f92daba9Smarks 	return (0);
1274f92daba9Smarks }
1275f92daba9Smarks 
1276da6c28aaSamw #define	ATTR_FLAG_PRIV(attr, value, cr)	\
1277da6c28aaSamw 	PRIV_POLICY(cr, value ? PRIV_FILE_FLAG_SET : PRIV_ALL, \
1278da6c28aaSamw 	B_FALSE, EPERM, NULL)
1279da6c28aaSamw 
1280da6c28aaSamw /*
1281da6c28aaSamw  * Check privileges for setting xvattr attributes
1282da6c28aaSamw  */
1283da6c28aaSamw int
1284da6c28aaSamw secpolicy_xvattr(xvattr_t *xvap, uid_t owner, cred_t *cr, vtype_t vtype)
1285da6c28aaSamw {
1286da6c28aaSamw 	xoptattr_t *xoap;
1287da6c28aaSamw 	int error = 0;
1288da6c28aaSamw 
1289da6c28aaSamw 	if ((xoap = xva_getxoptattr(xvap)) == NULL)
1290da6c28aaSamw 		return (EINVAL);
1291da6c28aaSamw 
1292da6c28aaSamw 	/*
1293da6c28aaSamw 	 * First process the DOS bits
1294da6c28aaSamw 	 */
1295da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
1296da6c28aaSamw 	    XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
1297da6c28aaSamw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
1298da6c28aaSamw 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM) ||
1299*fd9ee8b5Sjoyce mcintosh 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
1300*fd9ee8b5Sjoyce mcintosh 	    XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
1301*fd9ee8b5Sjoyce mcintosh 	    XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1302da6c28aaSamw 		if ((error = secpolicy_vnode_owner(cr, owner)) != 0)
1303da6c28aaSamw 			return (error);
1304da6c28aaSamw 	}
1305da6c28aaSamw 
1306da6c28aaSamw 	/*
1307da6c28aaSamw 	 * Now handle special attributes
1308da6c28aaSamw 	 */
1309da6c28aaSamw 
1310da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
1311da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_IMMUTABLE,
1312da6c28aaSamw 		    xoap->xoa_immutable, cr);
1313da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
1314da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_NOUNLINK,
1315da6c28aaSamw 		    xoap->xoa_nounlink, cr);
1316da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
1317da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_APPENDONLY,
1318da6c28aaSamw 		    xoap->xoa_appendonly, cr);
1319da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_NODUMP))
1320da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_NODUMP,
1321da6c28aaSamw 		    xoap->xoa_nodump, cr);
1322da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_OPAQUE))
1323da6c28aaSamw 		error = EPERM;
1324da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1325da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_AV_QUARANTINED,
1326da6c28aaSamw 		    xoap->xoa_av_quarantined, cr);
1327e8f97327Smarks 		if (error == 0 && vtype != VREG && xoap->xoa_av_quarantined)
1328da6c28aaSamw 			error = EINVAL;
1329da6c28aaSamw 	}
1330da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
1331da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_AV_MODIFIED,
1332da6c28aaSamw 		    xoap->xoa_av_modified, cr);
1333da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1334da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_AV_SCANSTAMP,
1335da6c28aaSamw 		    xoap->xoa_av_scanstamp, cr);
1336da6c28aaSamw 		if (error == 0 && vtype != VREG)
1337da6c28aaSamw 			error = EINVAL;
1338da6c28aaSamw 	}
1339da6c28aaSamw 	return (error);
1340da6c28aaSamw }
1341da6c28aaSamw 
13427c478bd9Sstevel@tonic-gate /*
13437c478bd9Sstevel@tonic-gate  * This function checks the policy decisions surrounding the
13447c478bd9Sstevel@tonic-gate  * vop setattr call.
13457c478bd9Sstevel@tonic-gate  *
13467c478bd9Sstevel@tonic-gate  * It should be called after sufficient locks have been established
13477c478bd9Sstevel@tonic-gate  * on the underlying data structures.  No concurrent modifications
13487c478bd9Sstevel@tonic-gate  * should be allowed.
13497c478bd9Sstevel@tonic-gate  *
13507c478bd9Sstevel@tonic-gate  * The caller must pass in unlocked version of its vaccess function
13517c478bd9Sstevel@tonic-gate  * this is required because vop_access function should lock the
13527c478bd9Sstevel@tonic-gate  * node for reading.  A three argument function should be defined
13537c478bd9Sstevel@tonic-gate  * which accepts the following argument:
13547c478bd9Sstevel@tonic-gate  * 	A pointer to the internal "node" type (inode *)
13557c478bd9Sstevel@tonic-gate  *	vnode access bits (VREAD|VWRITE|VEXEC)
13567c478bd9Sstevel@tonic-gate  *	a pointer to the credential
13577c478bd9Sstevel@tonic-gate  *
13587c478bd9Sstevel@tonic-gate  * This function makes the following policy decisions:
13597c478bd9Sstevel@tonic-gate  *
13607c478bd9Sstevel@tonic-gate  *		- change permissions
13617c478bd9Sstevel@tonic-gate  *			- permission to change file mode if not owner
13627c478bd9Sstevel@tonic-gate  *			- permission to add sticky bit to non-directory
13637c478bd9Sstevel@tonic-gate  *			- permission to add set-gid bit
13647c478bd9Sstevel@tonic-gate  *
13657c478bd9Sstevel@tonic-gate  * The ovap argument should include AT_MODE|AT_UID|AT_GID.
13667c478bd9Sstevel@tonic-gate  *
13677c478bd9Sstevel@tonic-gate  * If the vap argument does not include AT_MODE, the mode will be copied from
13687c478bd9Sstevel@tonic-gate  * ovap.  In certain situations set-uid/set-gid bits need to be removed;
13697c478bd9Sstevel@tonic-gate  * this is done by marking vap->va_mask to include AT_MODE and va_mode
13707c478bd9Sstevel@tonic-gate  * is updated to the newly computed mode.
13717c478bd9Sstevel@tonic-gate  */
13727c478bd9Sstevel@tonic-gate 
13737c478bd9Sstevel@tonic-gate int
13747c478bd9Sstevel@tonic-gate secpolicy_vnode_setattr(cred_t *cr, struct vnode *vp, struct vattr *vap,
13757c478bd9Sstevel@tonic-gate 	const struct vattr *ovap, int flags,
13767c478bd9Sstevel@tonic-gate 	int unlocked_access(void *, int, cred_t *),
13777c478bd9Sstevel@tonic-gate 	void *node)
13787c478bd9Sstevel@tonic-gate {
13797c478bd9Sstevel@tonic-gate 	int mask = vap->va_mask;
13807c478bd9Sstevel@tonic-gate 	int error = 0;
1381da6c28aaSamw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
13827c478bd9Sstevel@tonic-gate 
13837c478bd9Sstevel@tonic-gate 	if (mask & AT_SIZE) {
13847c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
13857c478bd9Sstevel@tonic-gate 			error = EISDIR;
13867c478bd9Sstevel@tonic-gate 			goto out;
13877c478bd9Sstevel@tonic-gate 		}
1388da6c28aaSamw 
1389da6c28aaSamw 		/*
1390da6c28aaSamw 		 * If ATTR_NOACLCHECK is set in the flags, then we don't
1391da6c28aaSamw 		 * perform the secondary unlocked_access() call since the
1392da6c28aaSamw 		 * ACL (if any) is being checked there.
1393da6c28aaSamw 		 */
1394da6c28aaSamw 		if (skipaclchk == B_FALSE) {
13957c478bd9Sstevel@tonic-gate 			error = unlocked_access(node, VWRITE, cr);
13967c478bd9Sstevel@tonic-gate 			if (error)
13977c478bd9Sstevel@tonic-gate 				goto out;
13987c478bd9Sstevel@tonic-gate 		}
1399da6c28aaSamw 	}
14007c478bd9Sstevel@tonic-gate 	if (mask & AT_MODE) {
14017c478bd9Sstevel@tonic-gate 		/*
14027c478bd9Sstevel@tonic-gate 		 * If not the owner of the file then check privilege
14037c478bd9Sstevel@tonic-gate 		 * for two things: the privilege to set the mode at all
14047c478bd9Sstevel@tonic-gate 		 * and, if we're setting setuid, we also need permissions
14057c478bd9Sstevel@tonic-gate 		 * to add the set-uid bit, if we're not the owner.
14067c478bd9Sstevel@tonic-gate 		 * In the specific case of creating a set-uid root
14077c478bd9Sstevel@tonic-gate 		 * file, we need even more permissions.
14087c478bd9Sstevel@tonic-gate 		 */
14097c478bd9Sstevel@tonic-gate 		if ((error = secpolicy_vnode_setdac(cr, ovap->va_uid)) != 0)
14107c478bd9Sstevel@tonic-gate 			goto out;
14117c478bd9Sstevel@tonic-gate 
1412f92daba9Smarks 		if ((error = secpolicy_setid_setsticky_clear(vp, vap,
1413f92daba9Smarks 		    ovap, cr)) != 0)
14147c478bd9Sstevel@tonic-gate 			goto out;
14157c478bd9Sstevel@tonic-gate 	} else
14167c478bd9Sstevel@tonic-gate 		vap->va_mode = ovap->va_mode;
14177c478bd9Sstevel@tonic-gate 
14187c478bd9Sstevel@tonic-gate 	if (mask & (AT_UID|AT_GID)) {
14197c478bd9Sstevel@tonic-gate 		boolean_t checkpriv = B_FALSE;
14207c478bd9Sstevel@tonic-gate 
14217c478bd9Sstevel@tonic-gate 		/*
14227c478bd9Sstevel@tonic-gate 		 * Chowning files.
14237c478bd9Sstevel@tonic-gate 		 *
14247c478bd9Sstevel@tonic-gate 		 * If you are the file owner:
14257c478bd9Sstevel@tonic-gate 		 *	chown to other uid		FILE_CHOWN_SELF
14267c478bd9Sstevel@tonic-gate 		 *	chown to gid (non-member) 	FILE_CHOWN_SELF
14277c478bd9Sstevel@tonic-gate 		 *	chown to gid (member) 		<none>
14287c478bd9Sstevel@tonic-gate 		 *
14297c478bd9Sstevel@tonic-gate 		 * Instead of PRIV_FILE_CHOWN_SELF, FILE_CHOWN is also
14307c478bd9Sstevel@tonic-gate 		 * acceptable but the first one is reported when debugging.
14317c478bd9Sstevel@tonic-gate 		 *
14327c478bd9Sstevel@tonic-gate 		 * If you are not the file owner:
14337c478bd9Sstevel@tonic-gate 		 *	chown from root			PRIV_FILE_CHOWN + zone
14347c478bd9Sstevel@tonic-gate 		 *	chown from other to any		PRIV_FILE_CHOWN
14357c478bd9Sstevel@tonic-gate 		 *
14367c478bd9Sstevel@tonic-gate 		 */
14377c478bd9Sstevel@tonic-gate 		if (cr->cr_uid != ovap->va_uid) {
14387c478bd9Sstevel@tonic-gate 			checkpriv = B_TRUE;
14397c478bd9Sstevel@tonic-gate 		} else {
14407c478bd9Sstevel@tonic-gate 			if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
14417c478bd9Sstevel@tonic-gate 			    ((mask & AT_GID) && vap->va_gid != ovap->va_gid &&
14427c478bd9Sstevel@tonic-gate 			    !groupmember(vap->va_gid, cr))) {
14437c478bd9Sstevel@tonic-gate 				checkpriv = B_TRUE;
14447c478bd9Sstevel@tonic-gate 			}
14457c478bd9Sstevel@tonic-gate 		}
14467c478bd9Sstevel@tonic-gate 		/*
14477c478bd9Sstevel@tonic-gate 		 * If necessary, check privilege to see if update can be done.
14487c478bd9Sstevel@tonic-gate 		 */
14497c478bd9Sstevel@tonic-gate 		if (checkpriv &&
1450e02bc683SMark Shellenbaum 		    (error = secpolicy_vnode_chown(cr, ovap->va_uid)) != 0) {
14517c478bd9Sstevel@tonic-gate 			goto out;
14527c478bd9Sstevel@tonic-gate 		}
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 		/*
14557c478bd9Sstevel@tonic-gate 		 * If the file has either the set UID or set GID bits
14567c478bd9Sstevel@tonic-gate 		 * set and the caller can set the bits, then leave them.
14577c478bd9Sstevel@tonic-gate 		 */
145813f9f30eSmarks 		secpolicy_setid_clear(vap, cr);
14597c478bd9Sstevel@tonic-gate 	}
14607c478bd9Sstevel@tonic-gate 	if (mask & (AT_ATIME|AT_MTIME)) {
14617c478bd9Sstevel@tonic-gate 		/*
14627c478bd9Sstevel@tonic-gate 		 * If not the file owner and not otherwise privileged,
14637c478bd9Sstevel@tonic-gate 		 * always return an error when setting the
14647c478bd9Sstevel@tonic-gate 		 * time other than the current (ATTR_UTIME flag set).
14657c478bd9Sstevel@tonic-gate 		 * If setting the current time (ATTR_UTIME not set) then
14667c478bd9Sstevel@tonic-gate 		 * unlocked_access will check permissions according to policy.
14677c478bd9Sstevel@tonic-gate 		 */
14687c478bd9Sstevel@tonic-gate 		if (cr->cr_uid != ovap->va_uid) {
14697c478bd9Sstevel@tonic-gate 			if (flags & ATTR_UTIME)
14707c478bd9Sstevel@tonic-gate 				error = secpolicy_vnode_utime_modify(cr);
1471da6c28aaSamw 			else if (skipaclchk == B_FALSE) {
14727c478bd9Sstevel@tonic-gate 				error = unlocked_access(node, VWRITE, cr);
14737c478bd9Sstevel@tonic-gate 				if (error == EACCES &&
14747c478bd9Sstevel@tonic-gate 				    secpolicy_vnode_utime_modify(cr) == 0)
14757c478bd9Sstevel@tonic-gate 					error = 0;
14767c478bd9Sstevel@tonic-gate 			}
14777c478bd9Sstevel@tonic-gate 			if (error)
14787c478bd9Sstevel@tonic-gate 				goto out;
14797c478bd9Sstevel@tonic-gate 		}
14807c478bd9Sstevel@tonic-gate 	}
1481da6c28aaSamw 
1482da6c28aaSamw 	/*
1483da6c28aaSamw 	 * Check for optional attributes here by checking the following:
1484da6c28aaSamw 	 */
1485da6c28aaSamw 	if (mask & AT_XVATTR)
1486da6c28aaSamw 		error = secpolicy_xvattr((xvattr_t *)vap, ovap->va_uid, cr,
1487da6c28aaSamw 		    vp->v_type);
14887c478bd9Sstevel@tonic-gate out:
14897c478bd9Sstevel@tonic-gate 	return (error);
14907c478bd9Sstevel@tonic-gate }
14917c478bd9Sstevel@tonic-gate 
14927c478bd9Sstevel@tonic-gate /*
14937c478bd9Sstevel@tonic-gate  * Name:	secpolicy_pcfs_modify_bootpartition()
14947c478bd9Sstevel@tonic-gate  *
14957c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can modify a pcfs boot partition.
14967c478bd9Sstevel@tonic-gate  *
14977c478bd9Sstevel@tonic-gate  * Output:	EACCES - if privilege check failed.
14987c478bd9Sstevel@tonic-gate  */
14997c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15007c478bd9Sstevel@tonic-gate int
15017c478bd9Sstevel@tonic-gate secpolicy_pcfs_modify_bootpartition(const cred_t *cred)
15027c478bd9Sstevel@tonic-gate {
15037c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_ALL, B_FALSE, EACCES,
15047c478bd9Sstevel@tonic-gate 	    "modify pcfs boot partition"));
15057c478bd9Sstevel@tonic-gate }
15067c478bd9Sstevel@tonic-gate 
15077c478bd9Sstevel@tonic-gate /*
15087c478bd9Sstevel@tonic-gate  * System V IPC routines
15097c478bd9Sstevel@tonic-gate  */
15107c478bd9Sstevel@tonic-gate int
15117c478bd9Sstevel@tonic-gate secpolicy_ipc_owner(const cred_t *cr, const struct kipc_perm *ip)
15127c478bd9Sstevel@tonic-gate {
15137c478bd9Sstevel@tonic-gate 	if (crgetzoneid(cr) != ip->ipc_zoneid ||
15147c478bd9Sstevel@tonic-gate 	    (cr->cr_uid != ip->ipc_uid && cr->cr_uid != ip->ipc_cuid)) {
15157c478bd9Sstevel@tonic-gate 		boolean_t allzone = B_FALSE;
15167c478bd9Sstevel@tonic-gate 		if (ip->ipc_uid == 0 || ip->ipc_cuid == 0)
15177c478bd9Sstevel@tonic-gate 			allzone = B_TRUE;
15187c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_OWNER, allzone, EPERM, NULL));
15197c478bd9Sstevel@tonic-gate 	}
15207c478bd9Sstevel@tonic-gate 	return (0);
15217c478bd9Sstevel@tonic-gate }
15227c478bd9Sstevel@tonic-gate 
15237c478bd9Sstevel@tonic-gate int
15247c478bd9Sstevel@tonic-gate secpolicy_ipc_config(const cred_t *cr)
15257c478bd9Sstevel@tonic-gate {
15267c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_IPC_CONFIG, B_FALSE, EPERM, NULL));
15277c478bd9Sstevel@tonic-gate }
15287c478bd9Sstevel@tonic-gate 
15297c478bd9Sstevel@tonic-gate int
15307c478bd9Sstevel@tonic-gate secpolicy_ipc_access(const cred_t *cr, const struct kipc_perm *ip, mode_t mode)
15317c478bd9Sstevel@tonic-gate {
15327c478bd9Sstevel@tonic-gate 
15337c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
15347c478bd9Sstevel@tonic-gate 
15357c478bd9Sstevel@tonic-gate 	ASSERT((mode & (MSG_R|MSG_W)) != 0);
15367c478bd9Sstevel@tonic-gate 
15377c478bd9Sstevel@tonic-gate 	if ((mode & MSG_R) &&
15387c478bd9Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_IPC_DAC_READ, allzone, EACCES, NULL) != 0)
15397c478bd9Sstevel@tonic-gate 		return (EACCES);
15407c478bd9Sstevel@tonic-gate 
15417c478bd9Sstevel@tonic-gate 	if (mode & MSG_W) {
15427c478bd9Sstevel@tonic-gate 		if (cr->cr_uid != 0 && (ip->ipc_uid == 0 || ip->ipc_cuid == 0))
15437c478bd9Sstevel@tonic-gate 			allzone = B_TRUE;
15447c478bd9Sstevel@tonic-gate 
15457c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_DAC_WRITE, allzone, EACCES,
15467c478bd9Sstevel@tonic-gate 		    NULL));
15477c478bd9Sstevel@tonic-gate 	}
15487c478bd9Sstevel@tonic-gate 	return (0);
15497c478bd9Sstevel@tonic-gate }
15507c478bd9Sstevel@tonic-gate 
15517c478bd9Sstevel@tonic-gate int
15527c478bd9Sstevel@tonic-gate secpolicy_rsm_access(const cred_t *cr, uid_t owner, mode_t mode)
15537c478bd9Sstevel@tonic-gate {
15547c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
15557c478bd9Sstevel@tonic-gate 
15567c478bd9Sstevel@tonic-gate 	ASSERT((mode & (MSG_R|MSG_W)) != 0);
15577c478bd9Sstevel@tonic-gate 
15587c478bd9Sstevel@tonic-gate 	if ((mode & MSG_R) &&
15597c478bd9Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_IPC_DAC_READ, allzone, EACCES, NULL) != 0)
15607c478bd9Sstevel@tonic-gate 		return (EACCES);
15617c478bd9Sstevel@tonic-gate 
15627c478bd9Sstevel@tonic-gate 	if (mode & MSG_W) {
15637c478bd9Sstevel@tonic-gate 		if (cr->cr_uid != 0 && owner == 0)
15647c478bd9Sstevel@tonic-gate 			allzone = B_TRUE;
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_DAC_WRITE, allzone, EACCES,
15677c478bd9Sstevel@tonic-gate 		    NULL));
15687c478bd9Sstevel@tonic-gate 	}
15697c478bd9Sstevel@tonic-gate 	return (0);
15707c478bd9Sstevel@tonic-gate }
15717c478bd9Sstevel@tonic-gate 
15727c478bd9Sstevel@tonic-gate /*
15737c478bd9Sstevel@tonic-gate  * Audit configuration.
15747c478bd9Sstevel@tonic-gate  */
15757c478bd9Sstevel@tonic-gate int
15767c478bd9Sstevel@tonic-gate secpolicy_audit_config(const cred_t *cr)
15777c478bd9Sstevel@tonic-gate {
15787c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_AUDIT, B_FALSE, EPERM, NULL));
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate /*
15827c478bd9Sstevel@tonic-gate  * Audit record generation.
15837c478bd9Sstevel@tonic-gate  */
15847c478bd9Sstevel@tonic-gate int
15857c478bd9Sstevel@tonic-gate secpolicy_audit_modify(const cred_t *cr)
15867c478bd9Sstevel@tonic-gate {
15877c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_AUDIT, B_FALSE, EPERM, NULL));
15887c478bd9Sstevel@tonic-gate }
15897c478bd9Sstevel@tonic-gate 
15907c478bd9Sstevel@tonic-gate /*
15917c478bd9Sstevel@tonic-gate  * Get audit attributes.
15927c478bd9Sstevel@tonic-gate  * Either PRIV_SYS_AUDIT or PRIV_PROC_AUDIT required; report the
15937c478bd9Sstevel@tonic-gate  * "Least" of the two privileges on error.
15947c478bd9Sstevel@tonic-gate  */
15957c478bd9Sstevel@tonic-gate int
1596134a1f4eSCasper H.S. Dik secpolicy_audit_getattr(const cred_t *cr, boolean_t checkonly)
15977c478bd9Sstevel@tonic-gate {
1598134a1f4eSCasper H.S. Dik 	int priv;
1599134a1f4eSCasper H.S. Dik 
1600134a1f4eSCasper H.S. Dik 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_AUDIT, B_FALSE))
1601134a1f4eSCasper H.S. Dik 		priv = PRIV_SYS_AUDIT;
1602134a1f4eSCasper H.S. Dik 	else
1603134a1f4eSCasper H.S. Dik 		priv = PRIV_PROC_AUDIT;
1604134a1f4eSCasper H.S. Dik 
1605134a1f4eSCasper H.S. Dik 	if (checkonly)
1606134a1f4eSCasper H.S. Dik 		return (!PRIV_POLICY_ONLY(cr, priv, B_FALSE));
1607134a1f4eSCasper H.S. Dik 	else
1608134a1f4eSCasper H.S. Dik 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
16097c478bd9Sstevel@tonic-gate }
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 
16127c478bd9Sstevel@tonic-gate /*
16137c478bd9Sstevel@tonic-gate  * Locking physical memory
16147c478bd9Sstevel@tonic-gate  */
16157c478bd9Sstevel@tonic-gate int
16167c478bd9Sstevel@tonic-gate secpolicy_lock_memory(const cred_t *cr)
16177c478bd9Sstevel@tonic-gate {
16187c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_LOCK_MEMORY, B_FALSE, EPERM, NULL));
16197c478bd9Sstevel@tonic-gate }
16207c478bd9Sstevel@tonic-gate 
16217c478bd9Sstevel@tonic-gate /*
16227c478bd9Sstevel@tonic-gate  * Accounting (both acct(2) and exacct).
16237c478bd9Sstevel@tonic-gate  */
16247c478bd9Sstevel@tonic-gate int
16257c478bd9Sstevel@tonic-gate secpolicy_acct(const cred_t *cr)
16267c478bd9Sstevel@tonic-gate {
16277c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ACCT, B_FALSE, EPERM, NULL));
16287c478bd9Sstevel@tonic-gate }
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate /*
16317c478bd9Sstevel@tonic-gate  * Is this process privileged to change its uids at will?
16327c478bd9Sstevel@tonic-gate  * Uid 0 is still considered "special" and having the SETID
16337c478bd9Sstevel@tonic-gate  * privilege is not sufficient to get uid 0.
16347c478bd9Sstevel@tonic-gate  * Files are owned by root, so the privilege would give
16357c478bd9Sstevel@tonic-gate  * full access and euid 0 is still effective.
16367c478bd9Sstevel@tonic-gate  *
16377c478bd9Sstevel@tonic-gate  * If you have the privilege and euid 0 only then do you
16387c478bd9Sstevel@tonic-gate  * get the powers of root wrt uid 0.
16397c478bd9Sstevel@tonic-gate  *
16407c478bd9Sstevel@tonic-gate  * For gid manipulations, this is should be called with an
16417c478bd9Sstevel@tonic-gate  * uid of -1.
16427c478bd9Sstevel@tonic-gate  *
16437c478bd9Sstevel@tonic-gate  */
16447c478bd9Sstevel@tonic-gate int
16457c478bd9Sstevel@tonic-gate secpolicy_allow_setid(const cred_t *cr, uid_t newuid, boolean_t checkonly)
16467c478bd9Sstevel@tonic-gate {
16477c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
16487c478bd9Sstevel@tonic-gate 
16497c478bd9Sstevel@tonic-gate 	if (newuid == 0 && cr->cr_uid != 0 && cr->cr_suid != 0 &&
16507c478bd9Sstevel@tonic-gate 	    cr->cr_ruid != 0) {
16517c478bd9Sstevel@tonic-gate 		allzone = B_TRUE;
16527c478bd9Sstevel@tonic-gate 	}
16537c478bd9Sstevel@tonic-gate 
16547c478bd9Sstevel@tonic-gate 	return (checkonly ? !PRIV_POLICY_ONLY(cr, PRIV_PROC_SETID, allzone) :
16557c478bd9Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_PROC_SETID, allzone, EPERM, NULL));
16567c478bd9Sstevel@tonic-gate }
16577c478bd9Sstevel@tonic-gate 
16587c478bd9Sstevel@tonic-gate 
16597c478bd9Sstevel@tonic-gate /*
16607c478bd9Sstevel@tonic-gate  * Acting on a different process: if the mode is for writing,
16617c478bd9Sstevel@tonic-gate  * the restrictions are more severe.  This is called after
16627c478bd9Sstevel@tonic-gate  * we've verified that the uids do not match.
16637c478bd9Sstevel@tonic-gate  */
16647c478bd9Sstevel@tonic-gate int
16657c478bd9Sstevel@tonic-gate secpolicy_proc_owner(const cred_t *scr, const cred_t *tcr, int mode)
16667c478bd9Sstevel@tonic-gate {
16677c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
16687c478bd9Sstevel@tonic-gate 
16697c478bd9Sstevel@tonic-gate 	if ((mode & VWRITE) && scr->cr_uid != 0 &&
16707c478bd9Sstevel@tonic-gate 	    (tcr->cr_uid == 0 || tcr->cr_ruid == 0 || tcr->cr_suid == 0))
16717c478bd9Sstevel@tonic-gate 		allzone = B_TRUE;
16727c478bd9Sstevel@tonic-gate 
16737c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, allzone, EPERM, NULL));
16747c478bd9Sstevel@tonic-gate }
16757c478bd9Sstevel@tonic-gate 
16767c478bd9Sstevel@tonic-gate int
16777c478bd9Sstevel@tonic-gate secpolicy_proc_access(const cred_t *scr)
16787c478bd9Sstevel@tonic-gate {
16797c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, B_FALSE, EACCES, NULL));
16807c478bd9Sstevel@tonic-gate }
16817c478bd9Sstevel@tonic-gate 
16827c478bd9Sstevel@tonic-gate int
16837c478bd9Sstevel@tonic-gate secpolicy_proc_excl_open(const cred_t *scr)
16847c478bd9Sstevel@tonic-gate {
16857c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, B_FALSE, EBUSY, NULL));
16867c478bd9Sstevel@tonic-gate }
16877c478bd9Sstevel@tonic-gate 
16887c478bd9Sstevel@tonic-gate int
16897c478bd9Sstevel@tonic-gate secpolicy_proc_zone(const cred_t *scr)
16907c478bd9Sstevel@tonic-gate {
16917c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_ZONE, B_FALSE, EPERM, NULL));
16927c478bd9Sstevel@tonic-gate }
16937c478bd9Sstevel@tonic-gate 
16947c478bd9Sstevel@tonic-gate /*
16957c478bd9Sstevel@tonic-gate  * Destroying the system
16967c478bd9Sstevel@tonic-gate  */
16977c478bd9Sstevel@tonic-gate 
16987c478bd9Sstevel@tonic-gate int
16997c478bd9Sstevel@tonic-gate secpolicy_kmdb(const cred_t *scr)
17007c478bd9Sstevel@tonic-gate {
17017c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
17027c478bd9Sstevel@tonic-gate }
17037c478bd9Sstevel@tonic-gate 
17047aec1d6eScindi int
17057aec1d6eScindi secpolicy_error_inject(const cred_t *scr)
17067aec1d6eScindi {
17077aec1d6eScindi 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
17087aec1d6eScindi }
17097aec1d6eScindi 
17107c478bd9Sstevel@tonic-gate /*
17117c478bd9Sstevel@tonic-gate  * Processor sets, cpu configuration, resource pools.
17127c478bd9Sstevel@tonic-gate  */
17137c478bd9Sstevel@tonic-gate int
17147c478bd9Sstevel@tonic-gate secpolicy_pset(const cred_t *cr)
17157c478bd9Sstevel@tonic-gate {
17167c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
17177c478bd9Sstevel@tonic-gate }
17187c478bd9Sstevel@tonic-gate 
1719bbf58fc5S /*
1720bbf58fc5S  * Processor set binding.
1721bbf58fc5S  */
1722bbf58fc5S int
1723bbf58fc5S secpolicy_pbind(const cred_t *cr)
1724bbf58fc5S {
1725bbf58fc5S 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_RES_CONFIG, B_FALSE))
1726bbf58fc5S 		return (secpolicy_pset(cr));
1727bbf58fc5S 	return (PRIV_POLICY(cr, PRIV_SYS_RES_BIND, B_FALSE, EPERM, NULL));
1728bbf58fc5S }
1729bbf58fc5S 
17307c478bd9Sstevel@tonic-gate int
17317c478bd9Sstevel@tonic-gate secpolicy_ponline(const cred_t *cr)
17327c478bd9Sstevel@tonic-gate {
17337c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
17347c478bd9Sstevel@tonic-gate }
17357c478bd9Sstevel@tonic-gate 
17367c478bd9Sstevel@tonic-gate int
17377c478bd9Sstevel@tonic-gate secpolicy_pool(const cred_t *cr)
17387c478bd9Sstevel@tonic-gate {
17397c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
17407c478bd9Sstevel@tonic-gate }
17417c478bd9Sstevel@tonic-gate 
17427c478bd9Sstevel@tonic-gate int
17437c478bd9Sstevel@tonic-gate secpolicy_blacklist(const cred_t *cr)
17447c478bd9Sstevel@tonic-gate {
17457c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
17467c478bd9Sstevel@tonic-gate }
17477c478bd9Sstevel@tonic-gate 
17487c478bd9Sstevel@tonic-gate /*
17497c478bd9Sstevel@tonic-gate  * Catch all system configuration.
17507c478bd9Sstevel@tonic-gate  */
17517c478bd9Sstevel@tonic-gate int
17527c478bd9Sstevel@tonic-gate secpolicy_sys_config(const cred_t *cr, boolean_t checkonly)
17537c478bd9Sstevel@tonic-gate {
17547c478bd9Sstevel@tonic-gate 	if (checkonly) {
17557c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_CONFIG, B_FALSE) ? 0 :
17567c478bd9Sstevel@tonic-gate 		    EPERM);
17577c478bd9Sstevel@tonic-gate 	} else {
17587c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
17597c478bd9Sstevel@tonic-gate 	}
17607c478bd9Sstevel@tonic-gate }
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate /*
17637c478bd9Sstevel@tonic-gate  * Zone administration (halt, reboot, etc.) from within zone.
17647c478bd9Sstevel@tonic-gate  */
17657c478bd9Sstevel@tonic-gate int
17667c478bd9Sstevel@tonic-gate secpolicy_zone_admin(const cred_t *cr, boolean_t checkonly)
17677c478bd9Sstevel@tonic-gate {
17687c478bd9Sstevel@tonic-gate 	if (checkonly) {
17697c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_ADMIN, B_FALSE) ? 0 :
17707c478bd9Sstevel@tonic-gate 		    EPERM);
17717c478bd9Sstevel@tonic-gate 	} else {
17727c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM,
17737c478bd9Sstevel@tonic-gate 		    NULL));
17747c478bd9Sstevel@tonic-gate 	}
17757c478bd9Sstevel@tonic-gate }
17767c478bd9Sstevel@tonic-gate 
17777c478bd9Sstevel@tonic-gate /*
17787c478bd9Sstevel@tonic-gate  * Zone configuration (create, halt, enter).
17797c478bd9Sstevel@tonic-gate  */
17807c478bd9Sstevel@tonic-gate int
17817c478bd9Sstevel@tonic-gate secpolicy_zone_config(const cred_t *cr)
17827c478bd9Sstevel@tonic-gate {
17837c478bd9Sstevel@tonic-gate 	/*
17847c478bd9Sstevel@tonic-gate 	 * Require all privileges to avoid possibility of privilege
17857c478bd9Sstevel@tonic-gate 	 * escalation.
17867c478bd9Sstevel@tonic-gate 	 */
1787134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
17887c478bd9Sstevel@tonic-gate }
17897c478bd9Sstevel@tonic-gate 
17907c478bd9Sstevel@tonic-gate /*
17917c478bd9Sstevel@tonic-gate  * Various other system configuration calls
17927c478bd9Sstevel@tonic-gate  */
17937c478bd9Sstevel@tonic-gate int
17947c478bd9Sstevel@tonic-gate secpolicy_coreadm(const cred_t *cr)
17957c478bd9Sstevel@tonic-gate {
17967c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM, NULL));
17977c478bd9Sstevel@tonic-gate }
17987c478bd9Sstevel@tonic-gate 
17997c478bd9Sstevel@tonic-gate int
18007c478bd9Sstevel@tonic-gate secpolicy_systeminfo(const cred_t *cr)
18017c478bd9Sstevel@tonic-gate {
18027c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM, NULL));
18037c478bd9Sstevel@tonic-gate }
18047c478bd9Sstevel@tonic-gate 
18057c478bd9Sstevel@tonic-gate int
18067c478bd9Sstevel@tonic-gate secpolicy_dispadm(const cred_t *cr)
18077c478bd9Sstevel@tonic-gate {
18087c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
18097c478bd9Sstevel@tonic-gate }
18107c478bd9Sstevel@tonic-gate 
18117c478bd9Sstevel@tonic-gate int
18127c478bd9Sstevel@tonic-gate secpolicy_settime(const cred_t *cr)
18137c478bd9Sstevel@tonic-gate {
18147c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_TIME, B_FALSE, EPERM, NULL));
18157c478bd9Sstevel@tonic-gate }
18167c478bd9Sstevel@tonic-gate 
18177c478bd9Sstevel@tonic-gate /*
18187c478bd9Sstevel@tonic-gate  * For realtime users: high resolution clock.
18197c478bd9Sstevel@tonic-gate  */
18207c478bd9Sstevel@tonic-gate int
18217c478bd9Sstevel@tonic-gate secpolicy_clock_highres(const cred_t *cr)
18227c478bd9Sstevel@tonic-gate {
18237c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_CLOCK_HIGHRES, B_FALSE, EPERM,
18247c478bd9Sstevel@tonic-gate 	    NULL));
18257c478bd9Sstevel@tonic-gate }
18267c478bd9Sstevel@tonic-gate 
18277c478bd9Sstevel@tonic-gate /*
18287c478bd9Sstevel@tonic-gate  * drv_priv() is documented as callable from interrupt context, not that
18297c478bd9Sstevel@tonic-gate  * anyone ever does, but still.  No debugging or auditing can be done when
18307c478bd9Sstevel@tonic-gate  * it is called from interrupt context.
18317c478bd9Sstevel@tonic-gate  * returns 0 on succes, EPERM on failure.
18327c478bd9Sstevel@tonic-gate  */
18337c478bd9Sstevel@tonic-gate int
18347c478bd9Sstevel@tonic-gate drv_priv(cred_t *cr)
18357c478bd9Sstevel@tonic-gate {
18367c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
18377c478bd9Sstevel@tonic-gate }
18387c478bd9Sstevel@tonic-gate 
18397c478bd9Sstevel@tonic-gate int
18407c478bd9Sstevel@tonic-gate secpolicy_sys_devices(const cred_t *cr)
18417c478bd9Sstevel@tonic-gate {
18427c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
18437c478bd9Sstevel@tonic-gate }
18447c478bd9Sstevel@tonic-gate 
18457c478bd9Sstevel@tonic-gate int
18467c478bd9Sstevel@tonic-gate secpolicy_excl_open(const cred_t *cr)
18477c478bd9Sstevel@tonic-gate {
18487c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EBUSY, NULL));
18497c478bd9Sstevel@tonic-gate }
18507c478bd9Sstevel@tonic-gate 
18517c478bd9Sstevel@tonic-gate int
18527c478bd9Sstevel@tonic-gate secpolicy_rctlsys(const cred_t *cr, boolean_t is_zone_rctl)
18537c478bd9Sstevel@tonic-gate {
18547c478bd9Sstevel@tonic-gate 	/* zone.* rctls can only be set from the global zone */
18557c478bd9Sstevel@tonic-gate 	if (is_zone_rctl && priv_policy_global(cr) != 0)
18567c478bd9Sstevel@tonic-gate 		return (EPERM);
18577c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
18587c478bd9Sstevel@tonic-gate }
18597c478bd9Sstevel@tonic-gate 
18607c478bd9Sstevel@tonic-gate int
18617c478bd9Sstevel@tonic-gate secpolicy_resource(const cred_t *cr)
18627c478bd9Sstevel@tonic-gate {
18637c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
18647c478bd9Sstevel@tonic-gate }
18657c478bd9Sstevel@tonic-gate 
18661c7cef2bSStan Studzinski int
18671c7cef2bSStan Studzinski secpolicy_resource_anon_mem(const cred_t *cr)
18681c7cef2bSStan Studzinski {
18691c7cef2bSStan Studzinski 	return (PRIV_POLICY_ONLY(cr, PRIV_SYS_RESOURCE, B_FALSE));
18701c7cef2bSStan Studzinski }
18711c7cef2bSStan Studzinski 
18727c478bd9Sstevel@tonic-gate /*
18737c478bd9Sstevel@tonic-gate  * Processes with a real uid of 0 escape any form of accounting, much
18747c478bd9Sstevel@tonic-gate  * like before.
18757c478bd9Sstevel@tonic-gate  */
18767c478bd9Sstevel@tonic-gate int
18777c478bd9Sstevel@tonic-gate secpolicy_newproc(const cred_t *cr)
18787c478bd9Sstevel@tonic-gate {
18797c478bd9Sstevel@tonic-gate 	if (cr->cr_ruid == 0)
18807c478bd9Sstevel@tonic-gate 		return (0);
18817c478bd9Sstevel@tonic-gate 
18827c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
18837c478bd9Sstevel@tonic-gate }
18847c478bd9Sstevel@tonic-gate 
18857c478bd9Sstevel@tonic-gate /*
18867c478bd9Sstevel@tonic-gate  * Networking
18877c478bd9Sstevel@tonic-gate  */
18887c478bd9Sstevel@tonic-gate int
18897c478bd9Sstevel@tonic-gate secpolicy_net_rawaccess(const cred_t *cr)
18907c478bd9Sstevel@tonic-gate {
18917c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_NET_RAWACCESS, B_FALSE, EACCES, NULL));
18927c478bd9Sstevel@tonic-gate }
18937c478bd9Sstevel@tonic-gate 
18940a0e9771SDarren Reed int
18950a0e9771SDarren Reed secpolicy_net_observability(const cred_t *cr)
18960a0e9771SDarren Reed {
18970a0e9771SDarren Reed 	return (PRIV_POLICY(cr, PRIV_NET_OBSERVABILITY, B_FALSE, EACCES, NULL));
18980a0e9771SDarren Reed }
18990a0e9771SDarren Reed 
19007c478bd9Sstevel@tonic-gate /*
19017c478bd9Sstevel@tonic-gate  * Need this privilege for accessing the ICMP device
19027c478bd9Sstevel@tonic-gate  */
19037c478bd9Sstevel@tonic-gate int
19047c478bd9Sstevel@tonic-gate secpolicy_net_icmpaccess(const cred_t *cr)
19057c478bd9Sstevel@tonic-gate {
19067c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_NET_ICMPACCESS, B_FALSE, EACCES, NULL));
19077c478bd9Sstevel@tonic-gate }
19087c478bd9Sstevel@tonic-gate 
19097c478bd9Sstevel@tonic-gate /*
19107c478bd9Sstevel@tonic-gate  * There are a few rare cases where the kernel generates ioctls() from
19117c478bd9Sstevel@tonic-gate  * interrupt context with a credential of kcred rather than NULL.
19127c478bd9Sstevel@tonic-gate  * In those cases, we take the safe and cheap test.
19137c478bd9Sstevel@tonic-gate  */
19147c478bd9Sstevel@tonic-gate int
19157c478bd9Sstevel@tonic-gate secpolicy_net_config(const cred_t *cr, boolean_t checkonly)
19167c478bd9Sstevel@tonic-gate {
19177c478bd9Sstevel@tonic-gate 	if (checkonly) {
19187c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE) ?
19197c478bd9Sstevel@tonic-gate 		    0 : EPERM);
19207c478bd9Sstevel@tonic-gate 	} else {
19217c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_NET_CONFIG, B_FALSE, EPERM,
19227c478bd9Sstevel@tonic-gate 		    NULL));
19237c478bd9Sstevel@tonic-gate 	}
19247c478bd9Sstevel@tonic-gate }
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate 
19277c478bd9Sstevel@tonic-gate /*
1928e6bdcbd5Sdh155122  * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_IP_CONFIG.
1929f4b3ec61Sdh155122  *
1930f4b3ec61Sdh155122  * There are a few rare cases where the kernel generates ioctls() from
1931f4b3ec61Sdh155122  * interrupt context with a credential of kcred rather than NULL.
1932f4b3ec61Sdh155122  * In those cases, we take the safe and cheap test.
1933f4b3ec61Sdh155122  */
1934f4b3ec61Sdh155122 int
1935f4b3ec61Sdh155122 secpolicy_ip_config(const cred_t *cr, boolean_t checkonly)
1936f4b3ec61Sdh155122 {
1937f4b3ec61Sdh155122 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
1938f4b3ec61Sdh155122 		return (secpolicy_net_config(cr, checkonly));
1939f4b3ec61Sdh155122 
1940f4b3ec61Sdh155122 	if (checkonly) {
1941f4b3ec61Sdh155122 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_IP_CONFIG, B_FALSE) ?
1942f4b3ec61Sdh155122 		    0 : EPERM);
1943f4b3ec61Sdh155122 	} else {
1944f4b3ec61Sdh155122 		return (PRIV_POLICY(cr, PRIV_SYS_IP_CONFIG, B_FALSE, EPERM,
1945f4b3ec61Sdh155122 		    NULL));
1946f4b3ec61Sdh155122 	}
1947f4b3ec61Sdh155122 }
1948f4b3ec61Sdh155122 
1949eae72b5bSSebastien Roy /*
1950eae72b5bSSebastien Roy  * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_DL_CONFIG.
1951eae72b5bSSebastien Roy  */
1952eae72b5bSSebastien Roy int
1953eae72b5bSSebastien Roy secpolicy_dl_config(const cred_t *cr)
1954eae72b5bSSebastien Roy {
1955eae72b5bSSebastien Roy 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
1956eae72b5bSSebastien Roy 		return (secpolicy_net_config(cr, B_FALSE));
19572b24ab6bSSebastien Roy 	return (PRIV_POLICY(cr, PRIV_SYS_DL_CONFIG, B_FALSE, EPERM, NULL));
1958eae72b5bSSebastien Roy }
1959eae72b5bSSebastien Roy 
19602b24ab6bSSebastien Roy /*
19612b24ab6bSSebastien Roy  * PRIV_SYS_DL_CONFIG is a superset of PRIV_SYS_IPTUN_CONFIG.
19622b24ab6bSSebastien Roy  */
19632b24ab6bSSebastien Roy int
19642b24ab6bSSebastien Roy secpolicy_iptun_config(const cred_t *cr)
19652b24ab6bSSebastien Roy {
19662b24ab6bSSebastien Roy 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
19672b24ab6bSSebastien Roy 		return (secpolicy_net_config(cr, B_FALSE));
19682b24ab6bSSebastien Roy 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_DL_CONFIG, B_FALSE))
19692b24ab6bSSebastien Roy 		return (secpolicy_dl_config(cr));
19702b24ab6bSSebastien Roy 	return (PRIV_POLICY(cr, PRIV_SYS_IPTUN_CONFIG, B_FALSE, EPERM, NULL));
19712b24ab6bSSebastien Roy }
1972f4b3ec61Sdh155122 
1973f4b3ec61Sdh155122 /*
1974f4b3ec61Sdh155122  * Map IP pseudo privileges to actual privileges.
1975f4b3ec61Sdh155122  * So we don't need to recompile IP when we change the privileges.
1976f4b3ec61Sdh155122  */
1977f4b3ec61Sdh155122 int
1978f4b3ec61Sdh155122 secpolicy_ip(const cred_t *cr, int netpriv, boolean_t checkonly)
1979f4b3ec61Sdh155122 {
1980f4b3ec61Sdh155122 	int priv = PRIV_ALL;
1981f4b3ec61Sdh155122 
1982f4b3ec61Sdh155122 	switch (netpriv) {
1983f4b3ec61Sdh155122 	case OP_CONFIG:
1984f4b3ec61Sdh155122 		priv = PRIV_SYS_IP_CONFIG;
1985f4b3ec61Sdh155122 		break;
1986f4b3ec61Sdh155122 	case OP_RAW:
1987f4b3ec61Sdh155122 		priv = PRIV_NET_RAWACCESS;
1988f4b3ec61Sdh155122 		break;
1989f4b3ec61Sdh155122 	case OP_PRIVPORT:
1990f4b3ec61Sdh155122 		priv = PRIV_NET_PRIVADDR;
1991f4b3ec61Sdh155122 		break;
1992f4b3ec61Sdh155122 	}
1993f4b3ec61Sdh155122 	ASSERT(priv != PRIV_ALL);
1994f4b3ec61Sdh155122 	if (checkonly)
1995f4b3ec61Sdh155122 		return (PRIV_POLICY_ONLY(cr, priv, B_FALSE) ? 0 : EPERM);
1996f4b3ec61Sdh155122 	else
1997f4b3ec61Sdh155122 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
1998f4b3ec61Sdh155122 }
1999f4b3ec61Sdh155122 
2000f4b3ec61Sdh155122 /*
20017c478bd9Sstevel@tonic-gate  * Map network pseudo privileges to actual privileges.
20027c478bd9Sstevel@tonic-gate  * So we don't need to recompile IP when we change the privileges.
20037c478bd9Sstevel@tonic-gate  */
20047c478bd9Sstevel@tonic-gate int
20057c478bd9Sstevel@tonic-gate secpolicy_net(const cred_t *cr, int netpriv, boolean_t checkonly)
20067c478bd9Sstevel@tonic-gate {
20077c478bd9Sstevel@tonic-gate 	int priv = PRIV_ALL;
20087c478bd9Sstevel@tonic-gate 
20097c478bd9Sstevel@tonic-gate 	switch (netpriv) {
20107c478bd9Sstevel@tonic-gate 	case OP_CONFIG:
20117c478bd9Sstevel@tonic-gate 		priv = PRIV_SYS_NET_CONFIG;
20127c478bd9Sstevel@tonic-gate 		break;
20137c478bd9Sstevel@tonic-gate 	case OP_RAW:
20147c478bd9Sstevel@tonic-gate 		priv = PRIV_NET_RAWACCESS;
20157c478bd9Sstevel@tonic-gate 		break;
20167c478bd9Sstevel@tonic-gate 	case OP_PRIVPORT:
20177c478bd9Sstevel@tonic-gate 		priv = PRIV_NET_PRIVADDR;
20187c478bd9Sstevel@tonic-gate 		break;
20197c478bd9Sstevel@tonic-gate 	}
20207c478bd9Sstevel@tonic-gate 	ASSERT(priv != PRIV_ALL);
20217c478bd9Sstevel@tonic-gate 	if (checkonly)
20227c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, priv, B_FALSE) ? 0 : EPERM);
20237c478bd9Sstevel@tonic-gate 	else
20247c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
20257c478bd9Sstevel@tonic-gate }
20267c478bd9Sstevel@tonic-gate 
20277c478bd9Sstevel@tonic-gate /*
20287c478bd9Sstevel@tonic-gate  * Checks for operations that are either client-only or are used by
20297c478bd9Sstevel@tonic-gate  * both clients and servers.
20307c478bd9Sstevel@tonic-gate  */
20317c478bd9Sstevel@tonic-gate int
20327c478bd9Sstevel@tonic-gate secpolicy_nfs(const cred_t *cr)
20337c478bd9Sstevel@tonic-gate {
20347c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_NFS, B_FALSE, EPERM, NULL));
20357c478bd9Sstevel@tonic-gate }
20367c478bd9Sstevel@tonic-gate 
20377c478bd9Sstevel@tonic-gate /*
20387c478bd9Sstevel@tonic-gate  * Special case for opening rpcmod: have NFS privileges or network
20397c478bd9Sstevel@tonic-gate  * config privileges.
20407c478bd9Sstevel@tonic-gate  */
20417c478bd9Sstevel@tonic-gate int
20427c478bd9Sstevel@tonic-gate secpolicy_rpcmod_open(const cred_t *cr)
20437c478bd9Sstevel@tonic-gate {
20447c478bd9Sstevel@tonic-gate 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NFS, B_FALSE))
20457c478bd9Sstevel@tonic-gate 		return (secpolicy_nfs(cr));
20467c478bd9Sstevel@tonic-gate 	else
20477c478bd9Sstevel@tonic-gate 		return (secpolicy_net_config(cr, NULL));
20487c478bd9Sstevel@tonic-gate }
20497c478bd9Sstevel@tonic-gate 
20507c478bd9Sstevel@tonic-gate int
20517c478bd9Sstevel@tonic-gate secpolicy_chroot(const cred_t *cr)
20527c478bd9Sstevel@tonic-gate {
20537c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_CHROOT, B_FALSE, EPERM, NULL));
20547c478bd9Sstevel@tonic-gate }
20557c478bd9Sstevel@tonic-gate 
20567c478bd9Sstevel@tonic-gate int
20577c478bd9Sstevel@tonic-gate secpolicy_tasksys(const cred_t *cr)
20587c478bd9Sstevel@tonic-gate {
20597c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_TASKID, B_FALSE, EPERM, NULL));
20607c478bd9Sstevel@tonic-gate }
20617c478bd9Sstevel@tonic-gate 
2062134a1f4eSCasper H.S. Dik int
2063134a1f4eSCasper H.S. Dik secpolicy_pfexec_register(const cred_t *cr)
2064134a1f4eSCasper H.S. Dik {
2065134a1f4eSCasper H.S. Dik 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_TRUE, EPERM, NULL));
2066134a1f4eSCasper H.S. Dik }
2067134a1f4eSCasper H.S. Dik 
20687c478bd9Sstevel@tonic-gate /*
20697c478bd9Sstevel@tonic-gate  * Basic privilege checks.
20707c478bd9Sstevel@tonic-gate  */
20717c478bd9Sstevel@tonic-gate int
2072ddf7fe95Scasper secpolicy_basic_exec(const cred_t *cr, vnode_t *vp)
20737c478bd9Sstevel@tonic-gate {
2074134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_PROC_EXEC);
2075134a1f4eSCasper H.S. Dik 
2076ddf7fe95Scasper 	return (priv_policy_va(cr, PRIV_PROC_EXEC, B_FALSE, EPERM, NULL,
2077ddf7fe95Scasper 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
20787c478bd9Sstevel@tonic-gate }
20797c478bd9Sstevel@tonic-gate 
20807c478bd9Sstevel@tonic-gate int
20817c478bd9Sstevel@tonic-gate secpolicy_basic_fork(const cred_t *cr)
20827c478bd9Sstevel@tonic-gate {
2083134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_PROC_FORK);
2084134a1f4eSCasper H.S. Dik 
20857c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_FORK, B_FALSE, EPERM, NULL));
20867c478bd9Sstevel@tonic-gate }
20877c478bd9Sstevel@tonic-gate 
20887c478bd9Sstevel@tonic-gate int
20897c478bd9Sstevel@tonic-gate secpolicy_basic_proc(const cred_t *cr)
20907c478bd9Sstevel@tonic-gate {
2091134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_PROC_SESSION);
2092134a1f4eSCasper H.S. Dik 
20937c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_SESSION, B_FALSE, EPERM, NULL));
20947c478bd9Sstevel@tonic-gate }
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate /*
20977c478bd9Sstevel@tonic-gate  * Slightly complicated because we don't want to trigger the policy too
20987c478bd9Sstevel@tonic-gate  * often.  First we shortcircuit access to "self" (tp == sp) or if
20997c478bd9Sstevel@tonic-gate  * we don't have the privilege but if we have permission
21007c478bd9Sstevel@tonic-gate  * just return (0) and we don't flag the privilege as needed.
21017c478bd9Sstevel@tonic-gate  * Else, we test for the privilege because we either have it or need it.
21027c478bd9Sstevel@tonic-gate  */
21037c478bd9Sstevel@tonic-gate int
21047c478bd9Sstevel@tonic-gate secpolicy_basic_procinfo(const cred_t *cr, proc_t *tp, proc_t *sp)
21057c478bd9Sstevel@tonic-gate {
21067c478bd9Sstevel@tonic-gate 	if (tp == sp ||
21077c478bd9Sstevel@tonic-gate 	    !HAS_PRIVILEGE(cr, PRIV_PROC_INFO) && prochasprocperm(tp, sp, cr)) {
21087c478bd9Sstevel@tonic-gate 		return (0);
21097c478bd9Sstevel@tonic-gate 	} else {
21107c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_PROC_INFO, B_FALSE, EPERM, NULL));
21117c478bd9Sstevel@tonic-gate 	}
21127c478bd9Sstevel@tonic-gate }
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate int
21157c478bd9Sstevel@tonic-gate secpolicy_basic_link(const cred_t *cr)
21167c478bd9Sstevel@tonic-gate {
2117134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_FILE_LINK_ANY);
2118134a1f4eSCasper H.S. Dik 
21197c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_LINK_ANY, B_FALSE, EPERM, NULL));
21207c478bd9Sstevel@tonic-gate }
21217c478bd9Sstevel@tonic-gate 
2122634e26ecSCasper H.S. Dik int
2123634e26ecSCasper H.S. Dik secpolicy_basic_net_access(const cred_t *cr)
2124634e26ecSCasper H.S. Dik {
2125134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_NET_ACCESS);
2126134a1f4eSCasper H.S. Dik 
2127634e26ecSCasper H.S. Dik 	return (PRIV_POLICY(cr, PRIV_NET_ACCESS, B_FALSE, EACCES, NULL));
2128634e26ecSCasper H.S. Dik }
2129634e26ecSCasper H.S. Dik 
2130134a1f4eSCasper H.S. Dik /* ARGSUSED */
2131134a1f4eSCasper H.S. Dik int
2132134a1f4eSCasper H.S. Dik secpolicy_basic_file_read(const cred_t *cr, vnode_t *vp, const char *pn)
2133134a1f4eSCasper H.S. Dik {
2134134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_FILE_READ);
2135134a1f4eSCasper H.S. Dik 
2136134a1f4eSCasper H.S. Dik 	return (priv_policy_va(cr, PRIV_FILE_READ, B_FALSE, EACCES, NULL,
2137134a1f4eSCasper H.S. Dik 	    KLPDARG_VNODE, vp, (char *)pn, KLPDARG_NOMORE));
2138134a1f4eSCasper H.S. Dik }
2139134a1f4eSCasper H.S. Dik 
2140134a1f4eSCasper H.S. Dik /* ARGSUSED */
2141134a1f4eSCasper H.S. Dik int
2142134a1f4eSCasper H.S. Dik secpolicy_basic_file_write(const cred_t *cr, vnode_t *vp, const char *pn)
2143134a1f4eSCasper H.S. Dik {
2144134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_FILE_WRITE);
2145134a1f4eSCasper H.S. Dik 
2146134a1f4eSCasper H.S. Dik 	return (priv_policy_va(cr, PRIV_FILE_WRITE, B_FALSE, EACCES, NULL,
2147134a1f4eSCasper H.S. Dik 	    KLPDARG_VNODE, vp, (char *)pn, KLPDARG_NOMORE));
2148134a1f4eSCasper H.S. Dik }
2149134a1f4eSCasper H.S. Dik 
21507c478bd9Sstevel@tonic-gate /*
21517c478bd9Sstevel@tonic-gate  * Additional device protection.
21527c478bd9Sstevel@tonic-gate  *
21537c478bd9Sstevel@tonic-gate  * Traditionally, a device has specific permissions on the node in
21547c478bd9Sstevel@tonic-gate  * the filesystem which govern which devices can be opened by what
21557c478bd9Sstevel@tonic-gate  * processes.  In certain cases, it is desirable to add extra
21567c478bd9Sstevel@tonic-gate  * restrictions, as writing to certain devices is identical to
21577c478bd9Sstevel@tonic-gate  * having a complete run of the system.
21587c478bd9Sstevel@tonic-gate  *
21597c478bd9Sstevel@tonic-gate  * This mechanism is called the device policy.
21607c478bd9Sstevel@tonic-gate  *
21617c478bd9Sstevel@tonic-gate  * When a device is opened, its policy entry is looked up in the
21627c478bd9Sstevel@tonic-gate  * policy cache and checked.
21637c478bd9Sstevel@tonic-gate  */
21647c478bd9Sstevel@tonic-gate int
21657c478bd9Sstevel@tonic-gate secpolicy_spec_open(const cred_t *cr, struct vnode *vp, int oflag)
21667c478bd9Sstevel@tonic-gate {
21677c478bd9Sstevel@tonic-gate 	devplcy_t *plcy;
21687c478bd9Sstevel@tonic-gate 	int err;
21697c478bd9Sstevel@tonic-gate 	struct snode *csp = VTOS(common_specvp(vp));
2170e6bdcbd5Sdh155122 	priv_set_t pset;
21717c478bd9Sstevel@tonic-gate 
21727c478bd9Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
21737c478bd9Sstevel@tonic-gate 
21747c478bd9Sstevel@tonic-gate 	if (csp->s_plcy == NULL || csp->s_plcy->dp_gen != devplcy_gen) {
21757c478bd9Sstevel@tonic-gate 		plcy = devpolicy_find(vp);
21767c478bd9Sstevel@tonic-gate 		if (csp->s_plcy)
21777c478bd9Sstevel@tonic-gate 			dpfree(csp->s_plcy);
21787c478bd9Sstevel@tonic-gate 		csp->s_plcy = plcy;
21797c478bd9Sstevel@tonic-gate 		ASSERT(plcy != NULL);
21807c478bd9Sstevel@tonic-gate 	} else
21817c478bd9Sstevel@tonic-gate 		plcy = csp->s_plcy;
21827c478bd9Sstevel@tonic-gate 
21837c478bd9Sstevel@tonic-gate 	if (plcy == nullpolicy) {
21847c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
21857c478bd9Sstevel@tonic-gate 		return (0);
21867c478bd9Sstevel@tonic-gate 	}
21877c478bd9Sstevel@tonic-gate 
21887c478bd9Sstevel@tonic-gate 	dphold(plcy);
21897c478bd9Sstevel@tonic-gate 
21907c478bd9Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
21917c478bd9Sstevel@tonic-gate 
2192e6bdcbd5Sdh155122 	if (oflag & FWRITE)
2193e6bdcbd5Sdh155122 		pset = plcy->dp_wrp;
2194e6bdcbd5Sdh155122 	else
2195e6bdcbd5Sdh155122 		pset = plcy->dp_rdp;
2196e6bdcbd5Sdh155122 	/*
2197e6bdcbd5Sdh155122 	 * Special case:
2198e6bdcbd5Sdh155122 	 * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_IP_CONFIG.
2199e6bdcbd5Sdh155122 	 * If PRIV_SYS_NET_CONFIG is present and PRIV_SYS_IP_CONFIG is
2200e6bdcbd5Sdh155122 	 * required, replace PRIV_SYS_IP_CONFIG with PRIV_SYS_NET_CONFIG
2201e6bdcbd5Sdh155122 	 * in the required privilege set before doing the check.
2202e6bdcbd5Sdh155122 	 */
2203e6bdcbd5Sdh155122 	if (priv_ismember(&pset, PRIV_SYS_IP_CONFIG) &&
2204e6bdcbd5Sdh155122 	    priv_ismember(&CR_OEPRIV(cr), PRIV_SYS_NET_CONFIG) &&
2205e6bdcbd5Sdh155122 	    !priv_ismember(&CR_OEPRIV(cr), PRIV_SYS_IP_CONFIG)) {
2206e6bdcbd5Sdh155122 		priv_delset(&pset, PRIV_SYS_IP_CONFIG);
2207e6bdcbd5Sdh155122 		priv_addset(&pset, PRIV_SYS_NET_CONFIG);
2208e6bdcbd5Sdh155122 	}
2209e6bdcbd5Sdh155122 
2210134a1f4eSCasper H.S. Dik 	err = secpolicy_require_set(cr, &pset, "devpolicy", KLPDARG_NONE);
22117c478bd9Sstevel@tonic-gate 	dpfree(plcy);
22127c478bd9Sstevel@tonic-gate 
22137c478bd9Sstevel@tonic-gate 	return (err);
22147c478bd9Sstevel@tonic-gate }
22157c478bd9Sstevel@tonic-gate 
22167c478bd9Sstevel@tonic-gate int
22177c478bd9Sstevel@tonic-gate secpolicy_modctl(const cred_t *cr, int cmd)
22187c478bd9Sstevel@tonic-gate {
22197c478bd9Sstevel@tonic-gate 	switch (cmd) {
22207c478bd9Sstevel@tonic-gate 	case MODINFO:
2221a08731ecScth 	case MODGETMAJBIND:
22227c478bd9Sstevel@tonic-gate 	case MODGETPATH:
22237c478bd9Sstevel@tonic-gate 	case MODGETPATHLEN:
22247c478bd9Sstevel@tonic-gate 	case MODGETNAME:
2225a08731ecScth 	case MODGETFBNAME:
22267c478bd9Sstevel@tonic-gate 	case MODGETDEVPOLICY:
22277c478bd9Sstevel@tonic-gate 	case MODGETDEVPOLICYBYNAME:
2228a08731ecScth 	case MODDEVT2INSTANCE:
2229a08731ecScth 	case MODSIZEOF_DEVID:
2230a08731ecScth 	case MODGETDEVID:
2231a08731ecScth 	case MODSIZEOF_MINORNAME:
2232a08731ecScth 	case MODGETMINORNAME:
2233a08731ecScth 	case MODGETDEVFSPATH_LEN:
2234a08731ecScth 	case MODGETDEVFSPATH:
2235a08731ecScth 	case MODGETDEVFSPATH_MI_LEN:
2236a08731ecScth 	case MODGETDEVFSPATH_MI:
22377c478bd9Sstevel@tonic-gate 		/* Unprivileged */
22387c478bd9Sstevel@tonic-gate 		return (0);
22397c478bd9Sstevel@tonic-gate 	case MODLOAD:
22407c478bd9Sstevel@tonic-gate 	case MODSETDEVPOLICY:
2241134a1f4eSCasper H.S. Dik 		return (secpolicy_require_set(cr, PRIV_FULLSET, NULL,
2242134a1f4eSCasper H.S. Dik 		    KLPDARG_NONE));
22437c478bd9Sstevel@tonic-gate 	default:
22447c478bd9Sstevel@tonic-gate 		return (secpolicy_sys_config(cr, B_FALSE));
22457c478bd9Sstevel@tonic-gate 	}
22467c478bd9Sstevel@tonic-gate }
22477c478bd9Sstevel@tonic-gate 
22487c478bd9Sstevel@tonic-gate int
22497c478bd9Sstevel@tonic-gate secpolicy_console(const cred_t *cr)
22507c478bd9Sstevel@tonic-gate {
22517c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
22527c478bd9Sstevel@tonic-gate }
22537c478bd9Sstevel@tonic-gate 
22547c478bd9Sstevel@tonic-gate int
22557c478bd9Sstevel@tonic-gate secpolicy_power_mgmt(const cred_t *cr)
22567c478bd9Sstevel@tonic-gate {
22577c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
22587c478bd9Sstevel@tonic-gate }
22597c478bd9Sstevel@tonic-gate 
22607c478bd9Sstevel@tonic-gate /*
22617c478bd9Sstevel@tonic-gate  * Simulate terminal input; another escalation of privileges avenue.
22627c478bd9Sstevel@tonic-gate  */
22637c478bd9Sstevel@tonic-gate 
22647c478bd9Sstevel@tonic-gate int
22657c478bd9Sstevel@tonic-gate secpolicy_sti(const cred_t *cr)
22667c478bd9Sstevel@tonic-gate {
2267134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
22687c478bd9Sstevel@tonic-gate }
22697c478bd9Sstevel@tonic-gate 
227045916cd2Sjpk boolean_t
227145916cd2Sjpk secpolicy_net_reply_equal(const cred_t *cr)
227245916cd2Sjpk {
227345916cd2Sjpk 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
227445916cd2Sjpk }
227545916cd2Sjpk 
22767c478bd9Sstevel@tonic-gate int
22777c478bd9Sstevel@tonic-gate secpolicy_swapctl(const cred_t *cr)
22787c478bd9Sstevel@tonic-gate {
22797c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
22807c478bd9Sstevel@tonic-gate }
22817c478bd9Sstevel@tonic-gate 
22827c478bd9Sstevel@tonic-gate int
22837c478bd9Sstevel@tonic-gate secpolicy_cpc_cpu(const cred_t *cr)
22847c478bd9Sstevel@tonic-gate {
22857c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CPC_CPU, B_FALSE, EACCES, NULL));
22867c478bd9Sstevel@tonic-gate }
22877c478bd9Sstevel@tonic-gate 
22887c478bd9Sstevel@tonic-gate /*
22897b209c2cSacruz  * secpolicy_contract_identity
22907b209c2cSacruz  *
22917b209c2cSacruz  * Determine if the subject may set the process contract FMRI value
22927b209c2cSacruz  */
22937b209c2cSacruz int
22947b209c2cSacruz secpolicy_contract_identity(const cred_t *cr)
22957b209c2cSacruz {
22967b209c2cSacruz 	return (PRIV_POLICY(cr, PRIV_CONTRACT_IDENTITY, B_FALSE, EPERM, NULL));
22977b209c2cSacruz }
22987b209c2cSacruz 
22997b209c2cSacruz /*
23007c478bd9Sstevel@tonic-gate  * secpolicy_contract_observer
23017c478bd9Sstevel@tonic-gate  *
23027c478bd9Sstevel@tonic-gate  * Determine if the subject may observe a specific contract's events.
23037c478bd9Sstevel@tonic-gate  */
23047c478bd9Sstevel@tonic-gate int
23057c478bd9Sstevel@tonic-gate secpolicy_contract_observer(const cred_t *cr, struct contract *ct)
23067c478bd9Sstevel@tonic-gate {
23077c478bd9Sstevel@tonic-gate 	if (contract_owned(ct, cr, B_FALSE))
23087c478bd9Sstevel@tonic-gate 		return (0);
23097c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CONTRACT_OBSERVER, B_FALSE, EPERM, NULL));
23107c478bd9Sstevel@tonic-gate }
23117c478bd9Sstevel@tonic-gate 
23127c478bd9Sstevel@tonic-gate /*
23137c478bd9Sstevel@tonic-gate  * secpolicy_contract_observer_choice
23147c478bd9Sstevel@tonic-gate  *
23157c478bd9Sstevel@tonic-gate  * Determine if the subject may observe any contract's events.  Just
23167c478bd9Sstevel@tonic-gate  * tests privilege and audits on success.
23177c478bd9Sstevel@tonic-gate  */
23187c478bd9Sstevel@tonic-gate boolean_t
23197c478bd9Sstevel@tonic-gate secpolicy_contract_observer_choice(const cred_t *cr)
23207c478bd9Sstevel@tonic-gate {
23217c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY_CHOICE(cr, PRIV_CONTRACT_OBSERVER, B_FALSE));
23227c478bd9Sstevel@tonic-gate }
23237c478bd9Sstevel@tonic-gate 
23247c478bd9Sstevel@tonic-gate /*
23257c478bd9Sstevel@tonic-gate  * secpolicy_contract_event
23267c478bd9Sstevel@tonic-gate  *
23277c478bd9Sstevel@tonic-gate  * Determine if the subject may request critical contract events or
23287c478bd9Sstevel@tonic-gate  * reliable contract event delivery.
23297c478bd9Sstevel@tonic-gate  */
23307c478bd9Sstevel@tonic-gate int
23317c478bd9Sstevel@tonic-gate secpolicy_contract_event(const cred_t *cr)
23327c478bd9Sstevel@tonic-gate {
23337c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CONTRACT_EVENT, B_FALSE, EPERM, NULL));
23347c478bd9Sstevel@tonic-gate }
23357c478bd9Sstevel@tonic-gate 
23367c478bd9Sstevel@tonic-gate /*
23377c478bd9Sstevel@tonic-gate  * secpolicy_contract_event_choice
23387c478bd9Sstevel@tonic-gate  *
23397c478bd9Sstevel@tonic-gate  * Determine if the subject may retain contract events in its critical
23407c478bd9Sstevel@tonic-gate  * set when a change in other terms would normally require a change in
23417c478bd9Sstevel@tonic-gate  * the critical set.  Just tests privilege and audits on success.
23427c478bd9Sstevel@tonic-gate  */
23437c478bd9Sstevel@tonic-gate boolean_t
23447c478bd9Sstevel@tonic-gate secpolicy_contract_event_choice(const cred_t *cr)
23457c478bd9Sstevel@tonic-gate {
23467c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY_CHOICE(cr, PRIV_CONTRACT_EVENT, B_FALSE));
23477c478bd9Sstevel@tonic-gate }
23487c478bd9Sstevel@tonic-gate 
23497c478bd9Sstevel@tonic-gate /*
2350ea8dc4b6Seschrock  * secpolicy_gart_access
23517c478bd9Sstevel@tonic-gate  *
2352ea8dc4b6Seschrock  * Determine if the subject has sufficient priveleges to make ioctls to agpgart
2353ea8dc4b6Seschrock  * device.
23547c478bd9Sstevel@tonic-gate  */
23557c478bd9Sstevel@tonic-gate int
23567c478bd9Sstevel@tonic-gate secpolicy_gart_access(const cred_t *cr)
23577c478bd9Sstevel@tonic-gate {
235826f24838Scasper 	return (PRIV_POLICY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE, EPERM, NULL));
23597c478bd9Sstevel@tonic-gate }
23607c478bd9Sstevel@tonic-gate 
23617c478bd9Sstevel@tonic-gate /*
2362ea8dc4b6Seschrock  * secpolicy_gart_map
23637c478bd9Sstevel@tonic-gate  *
2364ea8dc4b6Seschrock  * Determine if the subject has sufficient priveleges to map aperture range
2365ea8dc4b6Seschrock  * through agpgart driver.
23667c478bd9Sstevel@tonic-gate  */
23677c478bd9Sstevel@tonic-gate int
23687c478bd9Sstevel@tonic-gate secpolicy_gart_map(const cred_t *cr)
23697c478bd9Sstevel@tonic-gate {
237026f24838Scasper 	if (PRIV_POLICY_ONLY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE)) {
237126f24838Scasper 		return (PRIV_POLICY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE, EPERM,
237226f24838Scasper 		    NULL));
237326f24838Scasper 	} else {
237426f24838Scasper 		return (PRIV_POLICY(cr, PRIV_GRAPHICS_MAP, B_FALSE, EPERM,
237526f24838Scasper 		    NULL));
23767c478bd9Sstevel@tonic-gate 	}
23777c478bd9Sstevel@tonic-gate }
2378fa9e4066Sahrens 
2379fa9e4066Sahrens /*
2380ea8dc4b6Seschrock  * secpolicy_zinject
2381ea8dc4b6Seschrock  *
2382ea8dc4b6Seschrock  * Determine if the subject can inject faults in the ZFS fault injection
2383ea8dc4b6Seschrock  * framework.  Requires all privileges.
2384ea8dc4b6Seschrock  */
2385ea8dc4b6Seschrock int
2386ea8dc4b6Seschrock secpolicy_zinject(const cred_t *cr)
2387ea8dc4b6Seschrock {
2388134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
2389ea8dc4b6Seschrock }
2390ea8dc4b6Seschrock 
2391ea8dc4b6Seschrock /*
2392fa9e4066Sahrens  * secpolicy_zfs
2393fa9e4066Sahrens  *
2394ea8dc4b6Seschrock  * Determine if the subject has permission to manipulate ZFS datasets
2395ea8dc4b6Seschrock  * (not pools).  Equivalent to the SYS_MOUNT privilege.
2396fa9e4066Sahrens  */
2397fa9e4066Sahrens int
2398fa9e4066Sahrens secpolicy_zfs(const cred_t *cr)
2399fa9e4066Sahrens {
2400fa9e4066Sahrens 	return (PRIV_POLICY(cr, PRIV_SYS_MOUNT, B_FALSE, EPERM, NULL));
2401fa9e4066Sahrens }
2402f48205beScasper 
2403f48205beScasper /*
2404f48205beScasper  * secpolicy_idmap
2405f48205beScasper  *
2406f48205beScasper  * Determine if the calling process has permissions to register an SID
2407f48205beScasper  * mapping daemon and allocate ephemeral IDs.
2408f48205beScasper  */
2409f48205beScasper int
2410f48205beScasper secpolicy_idmap(const cred_t *cr)
2411f48205beScasper {
2412bda89588Sjp151216 	return (PRIV_POLICY(cr, PRIV_FILE_SETID, B_TRUE, EPERM, NULL));
2413f48205beScasper }
24142449e17fSsherrym 
24152449e17fSsherrym /*
24162449e17fSsherrym  * secpolicy_ucode_update
24172449e17fSsherrym  *
24182449e17fSsherrym  * Determine if the subject has sufficient privilege to update microcode.
24192449e17fSsherrym  */
24202449e17fSsherrym int
24212449e17fSsherrym secpolicy_ucode_update(const cred_t *scr)
24222449e17fSsherrym {
24232449e17fSsherrym 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
24242449e17fSsherrym }
2425e6bdcbd5Sdh155122 
2426e6bdcbd5Sdh155122 /*
2427e6bdcbd5Sdh155122  * secpolicy_sadopen
2428e6bdcbd5Sdh155122  *
2429e6bdcbd5Sdh155122  * Determine if the subject has sufficient privilege to access /dev/sad/admin.
2430e6bdcbd5Sdh155122  * /dev/sad/admin appear in global zone and exclusive-IP zones only.
2431e6bdcbd5Sdh155122  * In global zone, sys_config is required.
2432e6bdcbd5Sdh155122  * In exclusive-IP zones, sys_ip_config is required.
2433e6bdcbd5Sdh155122  * Note that sys_config is prohibited in non-global zones.
2434e6bdcbd5Sdh155122  */
2435e6bdcbd5Sdh155122 int
2436e6bdcbd5Sdh155122 secpolicy_sadopen(const cred_t *credp)
2437e6bdcbd5Sdh155122 {
2438e6bdcbd5Sdh155122 	priv_set_t pset;
2439e6bdcbd5Sdh155122 
2440e6bdcbd5Sdh155122 	priv_emptyset(&pset);
2441e6bdcbd5Sdh155122 
2442e6bdcbd5Sdh155122 	if (crgetzoneid(credp) == GLOBAL_ZONEID)
2443e6bdcbd5Sdh155122 		priv_addset(&pset, PRIV_SYS_CONFIG);
2444e6bdcbd5Sdh155122 	else
2445e6bdcbd5Sdh155122 		priv_addset(&pset, PRIV_SYS_IP_CONFIG);
2446e6bdcbd5Sdh155122 
2447134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(credp, &pset, "devpolicy", KLPDARG_NONE));
2448e6bdcbd5Sdh155122 }
2449da6c28aaSamw 
2450ddf7fe95Scasper 
2451ddf7fe95Scasper /*
2452ddf7fe95Scasper  * Add privileges to a particular privilege set; this is called when the
2453ddf7fe95Scasper  * current sets of privileges are not sufficient.  I.e., we should always
2454ddf7fe95Scasper  * call the policy override functions from here.
2455ddf7fe95Scasper  * What we are allowed to have is in the Observed Permitted set; so
2456ddf7fe95Scasper  * we compute the difference between that and the newset.
2457ddf7fe95Scasper  */
2458ddf7fe95Scasper int
2459ddf7fe95Scasper secpolicy_require_privs(const cred_t *cr, const priv_set_t *nset)
2460ddf7fe95Scasper {
2461ddf7fe95Scasper 	priv_set_t rqd;
2462ddf7fe95Scasper 
2463ddf7fe95Scasper 	rqd = CR_OPPRIV(cr);
2464ddf7fe95Scasper 
2465ddf7fe95Scasper 	priv_inverse(&rqd);
2466ddf7fe95Scasper 	priv_intersect(nset, &rqd);
2467ddf7fe95Scasper 
2468134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(cr, &rqd, NULL, KLPDARG_NONE));
2469ddf7fe95Scasper }
2470ddf7fe95Scasper 
2471da6c28aaSamw /*
2472da6c28aaSamw  * secpolicy_smb
2473da6c28aaSamw  *
2474da6c28aaSamw  * Determine if the cred_t has PRIV_SYS_SMB privilege, indicating
2475da6c28aaSamw  * that it has permission to access the smbsrv kernel driver.
2476da6c28aaSamw  * PRIV_POLICY checks the privilege and audits the check.
2477da6c28aaSamw  *
2478da6c28aaSamw  * Returns:
2479da6c28aaSamw  * 0       Driver access is allowed.
2480da6c28aaSamw  * EPERM   Driver access is NOT permitted.
2481da6c28aaSamw  */
2482da6c28aaSamw int
2483da6c28aaSamw secpolicy_smb(const cred_t *cr)
2484da6c28aaSamw {
2485da6c28aaSamw 	return (PRIV_POLICY(cr, PRIV_SYS_SMB, B_FALSE, EPERM, NULL));
2486da6c28aaSamw }
2487911106dfSjm199354 
2488911106dfSjm199354 /*
2489911106dfSjm199354  * secpolicy_vscan
2490911106dfSjm199354  *
2491911106dfSjm199354  * Determine if cred_t has the necessary privileges to access a file
2492911106dfSjm199354  * for virus scanning and update its extended system attributes.
2493911106dfSjm199354  * PRIV_FILE_DAC_SEARCH, PRIV_FILE_DAC_READ - file access
2494911106dfSjm199354  * PRIV_FILE_FLAG_SET - set extended system attributes
2495911106dfSjm199354  *
2496911106dfSjm199354  * PRIV_POLICY checks the privilege and audits the check.
2497911106dfSjm199354  *
2498911106dfSjm199354  * Returns:
2499911106dfSjm199354  * 0      file access for virus scanning allowed.
2500911106dfSjm199354  * EPERM  file access for virus scanning is NOT permitted.
2501911106dfSjm199354  */
2502911106dfSjm199354 int
2503911106dfSjm199354 secpolicy_vscan(const cred_t *cr)
2504911106dfSjm199354 {
2505911106dfSjm199354 	if ((PRIV_POLICY(cr, PRIV_FILE_DAC_SEARCH, B_FALSE, EPERM, NULL)) ||
2506911106dfSjm199354 	    (PRIV_POLICY(cr, PRIV_FILE_DAC_READ, B_FALSE, EPERM, NULL)) ||
2507911106dfSjm199354 	    (PRIV_POLICY(cr, PRIV_FILE_FLAG_SET, B_FALSE, EPERM, NULL))) {
2508911106dfSjm199354 		return (EPERM);
2509911106dfSjm199354 	}
2510911106dfSjm199354 
2511911106dfSjm199354 	return (0);
2512911106dfSjm199354 }
25134bff34e3Sthurlow 
25144bff34e3Sthurlow /*
25154bff34e3Sthurlow  * secpolicy_smbfs_login
25164bff34e3Sthurlow  *
25174bff34e3Sthurlow  * Determines if the caller can add and delete the smbfs login
25184bff34e3Sthurlow  * password in the the nsmb kernel module for the CIFS client.
25194bff34e3Sthurlow  *
25204bff34e3Sthurlow  * Returns:
25214bff34e3Sthurlow  * 0       access is allowed.
25224bff34e3Sthurlow  * EPERM   access is NOT allowed.
25234bff34e3Sthurlow  */
25244bff34e3Sthurlow int
25254bff34e3Sthurlow secpolicy_smbfs_login(const cred_t *cr, uid_t uid)
25264bff34e3Sthurlow {
25274bff34e3Sthurlow 	uid_t cruid = crgetruid(cr);
25284bff34e3Sthurlow 
25294bff34e3Sthurlow 	if (cruid == uid)
25304bff34e3Sthurlow 		return (0);
25314bff34e3Sthurlow 	return (PRIV_POLICY(cr, PRIV_PROC_OWNER, B_FALSE,
25324bff34e3Sthurlow 	    EPERM, NULL));
25334bff34e3Sthurlow }
2534b26a64aeSjohnlev 
2535b26a64aeSjohnlev /*
2536b26a64aeSjohnlev  * secpolicy_xvm_control
2537b26a64aeSjohnlev  *
2538b26a64aeSjohnlev  * Determines if a caller can control the xVM hypervisor and/or running
2539b26a64aeSjohnlev  * domains (x86 specific).
2540b26a64aeSjohnlev  *
2541b26a64aeSjohnlev  * Returns:
2542b26a64aeSjohnlev  * 0       access is allowed.
2543b26a64aeSjohnlev  * EPERM   access is NOT allowed.
2544b26a64aeSjohnlev  */
2545b26a64aeSjohnlev int
2546b26a64aeSjohnlev secpolicy_xvm_control(const cred_t *cr)
2547b26a64aeSjohnlev {
2548b26a64aeSjohnlev 	if (PRIV_POLICY(cr, PRIV_XVM_CONTROL, B_FALSE, EPERM, NULL))
2549b26a64aeSjohnlev 		return (EPERM);
2550b26a64aeSjohnlev 	return (0);
2551b26a64aeSjohnlev }
2552da14cebeSEric Cheng 
2553da14cebeSEric Cheng /*
2554f53eecf5SJames Carlson  * secpolicy_ppp_config
2555f53eecf5SJames Carlson  *
2556f53eecf5SJames Carlson  * Determine if the subject has sufficient privileges to configure PPP and
2557f53eecf5SJames Carlson  * PPP-related devices.
2558f53eecf5SJames Carlson  */
2559f53eecf5SJames Carlson int
2560f53eecf5SJames Carlson secpolicy_ppp_config(const cred_t *cr)
2561f53eecf5SJames Carlson {
2562f53eecf5SJames Carlson 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
2563f53eecf5SJames Carlson 		return (secpolicy_net_config(cr, B_FALSE));
2564f53eecf5SJames Carlson 	return (PRIV_POLICY(cr, PRIV_SYS_PPP_CONFIG, B_FALSE, EPERM, NULL));
2565f53eecf5SJames Carlson }
2566