xref: /illumos-gate/usr/src/uts/common/os/policy.c (revision 705610572a32b33c0da7d60b05c6dc30597a9e5a)
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.
23993e3fafSRobert Mustacchi  * Copyright 2016 Joyent, Inc.
2448bbca81SDaniel Hoffman  * Copyright (c) 2016 by Delphix. All rights reserved.
25ad8f9d95SJoshua M. Clulow  * Copyright 2022 Oxide Computer Company
26*70561057SGordon Ross  * Copyright 2023 RackTop Systems, Inc.
277c478bd9Sstevel@tonic-gate  */
287c478bd9Sstevel@tonic-gate 
297c478bd9Sstevel@tonic-gate #include <sys/types.h>
307c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h>
317c478bd9Sstevel@tonic-gate #include <sys/param.h>
327c478bd9Sstevel@tonic-gate #include <sys/systm.h>
337c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
347c478bd9Sstevel@tonic-gate #include <sys/vnode.h>
357c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
367c478bd9Sstevel@tonic-gate #include <sys/stat.h>
377c478bd9Sstevel@tonic-gate #include <sys/errno.h>
387c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
397c478bd9Sstevel@tonic-gate #include <sys/user.h>
407c478bd9Sstevel@tonic-gate #include <sys/proc.h>
417c478bd9Sstevel@tonic-gate #include <sys/acct.h>
427c478bd9Sstevel@tonic-gate #include <sys/ipc_impl.h>
437c478bd9Sstevel@tonic-gate #include <sys/cmn_err.h>
447c478bd9Sstevel@tonic-gate #include <sys/debug.h>
457c478bd9Sstevel@tonic-gate #include <sys/policy.h>
467c478bd9Sstevel@tonic-gate #include <sys/kobj.h>
477c478bd9Sstevel@tonic-gate #include <sys/msg.h>
487c478bd9Sstevel@tonic-gate #include <sys/devpolicy.h>
497c478bd9Sstevel@tonic-gate #include <c2/audit.h>
507c478bd9Sstevel@tonic-gate #include <sys/varargs.h>
51ddf7fe95Scasper #include <sys/klpd.h>
527c478bd9Sstevel@tonic-gate #include <sys/modctl.h>
537c478bd9Sstevel@tonic-gate #include <sys/disp.h>
547c478bd9Sstevel@tonic-gate #include <sys/zone.h>
557c478bd9Sstevel@tonic-gate #include <inet/optcom.h>
567c478bd9Sstevel@tonic-gate #include <sys/sdt.h>
577c478bd9Sstevel@tonic-gate #include <sys/vfs.h>
587c478bd9Sstevel@tonic-gate #include <sys/mntent.h>
597c478bd9Sstevel@tonic-gate #include <sys/contract_impl.h>
60da14cebeSEric Cheng #include <sys/dld_ioc.h>
617c478bd9Sstevel@tonic-gate 
627c478bd9Sstevel@tonic-gate /*
637c478bd9Sstevel@tonic-gate  * There are two possible layers of privilege routines and two possible
647c478bd9Sstevel@tonic-gate  * levels of secpolicy.  Plus one other we may not be interested in, so
657c478bd9Sstevel@tonic-gate  * we may need as many as 6 but no more.
667c478bd9Sstevel@tonic-gate  */
677c478bd9Sstevel@tonic-gate #define	MAXPRIVSTACK		6
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate int priv_debug = 0;
70634e26ecSCasper H.S. Dik int priv_basic_test = -1;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate /*
73ad8f9d95SJoshua M. Clulow  * Unlinking or creating new hard links to directories was historically allowed
74ad8f9d95SJoshua M. Clulow  * in some file systems; e.g., UFS allows root users to do it, at the cost of
75ad8f9d95SJoshua M. Clulow  * almost certain file system corruption that will require fsck to fix.
76ad8f9d95SJoshua M. Clulow  *
77ad8f9d95SJoshua M. Clulow  * Most modern operating systems and file systems (e.g., ZFS) do not allow this
78ad8f9d95SJoshua M. Clulow  * behaviour anymore, and we have elected to stamp it out entirely for
79ad8f9d95SJoshua M. Clulow  * compatibility and safety reasons.  An attempt to unlink a directory will
80ad8f9d95SJoshua M. Clulow  * fail with EPERM, as described in the standard.  During this transition, one
81ad8f9d95SJoshua M. Clulow  * can turn the behaviour back on, at their own risk, with this tuneable:
82ad8f9d95SJoshua M. Clulow  */
83ad8f9d95SJoshua M. Clulow int priv_allow_linkdir = 0;
84ad8f9d95SJoshua M. Clulow 
85ad8f9d95SJoshua M. Clulow /*
867c478bd9Sstevel@tonic-gate  * This file contains the majority of the policy routines.
877c478bd9Sstevel@tonic-gate  * Since the policy routines are defined by function and not
887c478bd9Sstevel@tonic-gate  * by privilege, there is quite a bit of duplication of
897c478bd9Sstevel@tonic-gate  * functions.
907c478bd9Sstevel@tonic-gate  *
91da6c28aaSamw  * The secpolicy functions must not make assumptions about
927c478bd9Sstevel@tonic-gate  * locks held or not held as any lock can be held while they're
937c478bd9Sstevel@tonic-gate  * being called.
947c478bd9Sstevel@tonic-gate  *
957c478bd9Sstevel@tonic-gate  * Credentials are read-only so no special precautions need to
967c478bd9Sstevel@tonic-gate  * be taken while locking them.
977c478bd9Sstevel@tonic-gate  *
987c478bd9Sstevel@tonic-gate  * When a new policy check needs to be added to the system the
997c478bd9Sstevel@tonic-gate  * following procedure should be followed:
1007c478bd9Sstevel@tonic-gate  *
1017c478bd9Sstevel@tonic-gate  *		Pick an appropriate secpolicy_*() function
1027c478bd9Sstevel@tonic-gate  *			-> done if one exists.
1037c478bd9Sstevel@tonic-gate  *		Create a new secpolicy function, preferably with
1047c478bd9Sstevel@tonic-gate  *		a descriptive name using the standard template.
1057c478bd9Sstevel@tonic-gate  *		Pick an appropriate privilege for the policy.
1067c478bd9Sstevel@tonic-gate  *		If no appropraite privilege exists, define new one
1077c478bd9Sstevel@tonic-gate  *		(this should be done with extreme care; in most cases
1087c478bd9Sstevel@tonic-gate  *		little is gained by adding another privilege)
1097c478bd9Sstevel@tonic-gate  *
1107c478bd9Sstevel@tonic-gate  * WHY ROOT IS STILL SPECIAL.
1117c478bd9Sstevel@tonic-gate  *
1127c478bd9Sstevel@tonic-gate  * In a number of the policy functions, there are still explicit
1137c478bd9Sstevel@tonic-gate  * checks for uid 0.  The rationale behind these is that many root
1147c478bd9Sstevel@tonic-gate  * owned files/objects hold configuration information which can give full
1157c478bd9Sstevel@tonic-gate  * privileges to the user once written to.  To prevent escalation
1167c478bd9Sstevel@tonic-gate  * of privilege by allowing just a single privilege to modify root owned
1177c478bd9Sstevel@tonic-gate  * objects, we've added these root specific checks where we considered
1187c478bd9Sstevel@tonic-gate  * them necessary: modifying root owned files, changing uids to 0, etc.
1197c478bd9Sstevel@tonic-gate  *
1207c478bd9Sstevel@tonic-gate  * PRIVILEGE ESCALATION AND ZONES.
1217c478bd9Sstevel@tonic-gate  *
1227c478bd9Sstevel@tonic-gate  * A number of operations potentially allow the caller to achieve
1237c478bd9Sstevel@tonic-gate  * privileges beyond the ones normally required to perform the operation.
1247c478bd9Sstevel@tonic-gate  * For example, if allowed to create a setuid 0 executable, a process can
1257c478bd9Sstevel@tonic-gate  * gain privileges beyond PRIV_FILE_SETID.  Zones, however, place
1267c478bd9Sstevel@tonic-gate  * restrictions on the ability to gain privileges beyond those available
1277c478bd9Sstevel@tonic-gate  * within the zone through file and process manipulation.  Hence, such
1287c478bd9Sstevel@tonic-gate  * operations require that the caller have an effective set that includes
1297c478bd9Sstevel@tonic-gate  * all privileges available within the current zone, or all privileges
1307c478bd9Sstevel@tonic-gate  * if executing in the global zone.
1317c478bd9Sstevel@tonic-gate  *
1327c478bd9Sstevel@tonic-gate  * This is indicated in the priv_policy* policy checking functions
1337c478bd9Sstevel@tonic-gate  * through a combination of parameters.  The "priv" parameter indicates
1347c478bd9Sstevel@tonic-gate  * the privilege that is required, and the "allzone" parameter indicates
1357c478bd9Sstevel@tonic-gate  * whether or not all privileges in the zone are required.  In addition,
1367c478bd9Sstevel@tonic-gate  * priv can be set to PRIV_ALL to indicate that all privileges are
1377c478bd9Sstevel@tonic-gate  * required (regardless of zone).  There are three scenarios of interest:
1387c478bd9Sstevel@tonic-gate  * (1) operation requires a specific privilege
1397c478bd9Sstevel@tonic-gate  * (2) operation requires a specific privilege, and requires all
1407c478bd9Sstevel@tonic-gate  *     privileges available within the zone (or all privileges if in
1417c478bd9Sstevel@tonic-gate  *     the global zone)
1427c478bd9Sstevel@tonic-gate  * (3) operation requires all privileges, regardless of zone
1437c478bd9Sstevel@tonic-gate  *
1447c478bd9Sstevel@tonic-gate  * For (1), priv should be set to the specific privilege, and allzone
1457c478bd9Sstevel@tonic-gate  * should be set to B_FALSE.
1467c478bd9Sstevel@tonic-gate  * For (2), priv should be set to the specific privilege, and allzone
1477c478bd9Sstevel@tonic-gate  * should be set to B_TRUE.
1487c478bd9Sstevel@tonic-gate  * For (3), priv should be set to PRIV_ALL, and allzone should be set
1497c478bd9Sstevel@tonic-gate  * to B_FALSE.
1507c478bd9Sstevel@tonic-gate  *
1517c478bd9Sstevel@tonic-gate  */
1527c478bd9Sstevel@tonic-gate 
1537c478bd9Sstevel@tonic-gate /*
1547c478bd9Sstevel@tonic-gate  * The privileges are checked against the Effective set for
1557c478bd9Sstevel@tonic-gate  * ordinary processes and checked against the Limit set
1567c478bd9Sstevel@tonic-gate  * for euid 0 processes that haven't manipulated their privilege
1577c478bd9Sstevel@tonic-gate  * sets.
1587c478bd9Sstevel@tonic-gate  */
1597c478bd9Sstevel@tonic-gate #define	HAS_ALLPRIVS(cr)	priv_isfullset(&CR_OEPRIV(cr))
1607c478bd9Sstevel@tonic-gate #define	ZONEPRIVS(cr)		((cr)->cr_zone->zone_privset)
1617c478bd9Sstevel@tonic-gate #define	HAS_ALLZONEPRIVS(cr)	priv_issubset(ZONEPRIVS(cr), &CR_OEPRIV(cr))
1627c478bd9Sstevel@tonic-gate #define	HAS_PRIVILEGE(cr, pr)	((pr) == PRIV_ALL ? \
1637c478bd9Sstevel@tonic-gate 					HAS_ALLPRIVS(cr) : \
1647c478bd9Sstevel@tonic-gate 					PRIV_ISASSERT(&CR_OEPRIV(cr), pr))
1657c478bd9Sstevel@tonic-gate 
166134a1f4eSCasper H.S. Dik #define	FAST_BASIC_CHECK(cr, priv)	\
167134a1f4eSCasper H.S. Dik 	if (PRIV_ISASSERT(&CR_OEPRIV(cr), priv)) { \
168134a1f4eSCasper H.S. Dik 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, B_FALSE); \
169134a1f4eSCasper H.S. Dik 		return (0); \
170134a1f4eSCasper H.S. Dik 	}
171134a1f4eSCasper H.S. Dik 
1727c478bd9Sstevel@tonic-gate /*
173ddf7fe95Scasper  * Policy checking functions.
1747c478bd9Sstevel@tonic-gate  *
175ddf7fe95Scasper  * All of the system's policy should be implemented here.
1767c478bd9Sstevel@tonic-gate  */
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate /*
179ddf7fe95Scasper  * Private functions which take an additional va_list argument to
180ddf7fe95Scasper  * implement an object specific policy override.
181ddf7fe95Scasper  */
182ddf7fe95Scasper static int priv_policy_ap(const cred_t *, int, boolean_t, int,
183ddf7fe95Scasper     const char *, va_list);
184ddf7fe95Scasper static int priv_policy_va(const cred_t *, int, boolean_t, int,
185ddf7fe95Scasper     const char *, ...);
186ddf7fe95Scasper 
187ddf7fe95Scasper /*
1887c478bd9Sstevel@tonic-gate  * Generic policy calls
1897c478bd9Sstevel@tonic-gate  *
1907c478bd9Sstevel@tonic-gate  * The "bottom" functions of policy control
1917c478bd9Sstevel@tonic-gate  */
1927c478bd9Sstevel@tonic-gate static char *
mprintf(const char * fmt,...)1937c478bd9Sstevel@tonic-gate mprintf(const char *fmt, ...)
1947c478bd9Sstevel@tonic-gate {
1957c478bd9Sstevel@tonic-gate 	va_list args;
1967c478bd9Sstevel@tonic-gate 	char *buf;
1977c478bd9Sstevel@tonic-gate 	size_t len;
1987c478bd9Sstevel@tonic-gate 
1997c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
2007c478bd9Sstevel@tonic-gate 	len = vsnprintf(NULL, 0, fmt, args) + 1;
2017c478bd9Sstevel@tonic-gate 	va_end(args);
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 	buf = kmem_alloc(len, KM_NOSLEEP);
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	if (buf == NULL)
2067c478bd9Sstevel@tonic-gate 		return (NULL);
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	va_start(args, fmt);
2097c478bd9Sstevel@tonic-gate 	(void) vsnprintf(buf, len, fmt, args);
2107c478bd9Sstevel@tonic-gate 	va_end(args);
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate 	return (buf);
2137c478bd9Sstevel@tonic-gate }
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate /*
2167c478bd9Sstevel@tonic-gate  * priv_policy_errmsg()
2177c478bd9Sstevel@tonic-gate  *
2187c478bd9Sstevel@tonic-gate  * Generate an error message if privilege debugging is enabled system wide
2197c478bd9Sstevel@tonic-gate  * or for this particular process.
2207c478bd9Sstevel@tonic-gate  */
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate #define	FMTHDR	"%s[%d]: missing privilege \"%s\" (euid = %d, syscall = %d)"
2237c478bd9Sstevel@tonic-gate #define	FMTMSG	" for \"%s\""
2247c478bd9Sstevel@tonic-gate #define	FMTFUN	" needed at %s+0x%lx"
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate /* The maximum size privilege format: the concatenation of the above */
2277c478bd9Sstevel@tonic-gate #define	FMTMAX	FMTHDR FMTMSG FMTFUN "\n"
2287c478bd9Sstevel@tonic-gate 
2297c478bd9Sstevel@tonic-gate static void
priv_policy_errmsg(const cred_t * cr,int priv,const char * msg)2307c478bd9Sstevel@tonic-gate priv_policy_errmsg(const cred_t *cr, int priv, const char *msg)
2317c478bd9Sstevel@tonic-gate {
2327c478bd9Sstevel@tonic-gate 	struct proc *me;
2337c478bd9Sstevel@tonic-gate 	pc_t stack[MAXPRIVSTACK];
2347c478bd9Sstevel@tonic-gate 	int depth;
2357c478bd9Sstevel@tonic-gate 	int i;
2367c478bd9Sstevel@tonic-gate 	char *sym;
2377c478bd9Sstevel@tonic-gate 	ulong_t off;
2387c478bd9Sstevel@tonic-gate 	const char *pname;
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate 	char *cmd;
2417c478bd9Sstevel@tonic-gate 	char fmt[sizeof (FMTMAX)];
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	if ((me = curproc) == &p0)
2447c478bd9Sstevel@tonic-gate 		return;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	/* Privileges must be defined  */
2477c478bd9Sstevel@tonic-gate 	ASSERT(priv == PRIV_ALL || priv == PRIV_MULTIPLE ||
2487c478bd9Sstevel@tonic-gate 	    priv == PRIV_ALLZONE || priv == PRIV_GLOBAL ||
2497c478bd9Sstevel@tonic-gate 	    priv_getbynum(priv) != NULL);
2507c478bd9Sstevel@tonic-gate 
2517c478bd9Sstevel@tonic-gate 	if (priv == PRIV_ALLZONE && INGLOBALZONE(me))
2527c478bd9Sstevel@tonic-gate 		priv = PRIV_ALL;
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 	if (curthread->t_pre_sys)
2557c478bd9Sstevel@tonic-gate 		ttolwp(curthread)->lwp_badpriv = (short)priv;
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	if (priv_debug == 0 && (CR_FLAGS(cr) & PRIV_DEBUG) == 0)
2587c478bd9Sstevel@tonic-gate 		return;
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 	(void) strcpy(fmt, FMTHDR);
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 	if (me->p_user.u_comm[0])
2637c478bd9Sstevel@tonic-gate 		cmd = &me->p_user.u_comm[0];
2647c478bd9Sstevel@tonic-gate 	else
2657c478bd9Sstevel@tonic-gate 		cmd = "priv_policy";
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 	if (msg != NULL && *msg != '\0') {
2687c478bd9Sstevel@tonic-gate 		(void) strcat(fmt, FMTMSG);
2697c478bd9Sstevel@tonic-gate 	} else {
2707c478bd9Sstevel@tonic-gate 		(void) strcat(fmt, "%s");
2717c478bd9Sstevel@tonic-gate 		msg = "";
2727c478bd9Sstevel@tonic-gate 	}
2737c478bd9Sstevel@tonic-gate 
2747c478bd9Sstevel@tonic-gate 	sym = NULL;
2757c478bd9Sstevel@tonic-gate 
2767c478bd9Sstevel@tonic-gate 	depth = getpcstack(stack, MAXPRIVSTACK);
2777c478bd9Sstevel@tonic-gate 
2787c478bd9Sstevel@tonic-gate 	/*
2797c478bd9Sstevel@tonic-gate 	 * Try to find the first interesting function on the stack.
2807c478bd9Sstevel@tonic-gate 	 * priv_policy* that's us, so completely uninteresting.
2817c478bd9Sstevel@tonic-gate 	 * suser(), drv_priv(), secpolicy_* are also called from
2827c478bd9Sstevel@tonic-gate 	 * too many locations to convey useful information.
2837c478bd9Sstevel@tonic-gate 	 */
2847c478bd9Sstevel@tonic-gate 	for (i = 0; i < depth; i++) {
2857c478bd9Sstevel@tonic-gate 		sym = kobj_getsymname((uintptr_t)stack[i], &off);
2867c478bd9Sstevel@tonic-gate 		if (sym != NULL &&
2877c478bd9Sstevel@tonic-gate 		    strstr(sym, "hasprocperm") == 0 &&
2887c478bd9Sstevel@tonic-gate 		    strcmp("suser", sym) != 0 &&
2897c478bd9Sstevel@tonic-gate 		    strcmp("ipcaccess", sym) != 0 &&
2907c478bd9Sstevel@tonic-gate 		    strcmp("drv_priv", sym) != 0 &&
2917c478bd9Sstevel@tonic-gate 		    strncmp("secpolicy_", sym, 10) != 0 &&
2927c478bd9Sstevel@tonic-gate 		    strncmp("priv_policy", sym, 11) != 0)
2937c478bd9Sstevel@tonic-gate 			break;
2947c478bd9Sstevel@tonic-gate 	}
2957c478bd9Sstevel@tonic-gate 
2967c478bd9Sstevel@tonic-gate 	if (sym != NULL)
2977c478bd9Sstevel@tonic-gate 		(void) strcat(fmt, FMTFUN);
2987c478bd9Sstevel@tonic-gate 
2997c478bd9Sstevel@tonic-gate 	(void) strcat(fmt, "\n");
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	switch (priv) {
3027c478bd9Sstevel@tonic-gate 	case PRIV_ALL:
3037c478bd9Sstevel@tonic-gate 		pname = "ALL";
3047c478bd9Sstevel@tonic-gate 		break;
3057c478bd9Sstevel@tonic-gate 	case PRIV_MULTIPLE:
3067c478bd9Sstevel@tonic-gate 		pname = "MULTIPLE";
3077c478bd9Sstevel@tonic-gate 		break;
3087c478bd9Sstevel@tonic-gate 	case PRIV_ALLZONE:
3097c478bd9Sstevel@tonic-gate 		pname = "ZONE";
3107c478bd9Sstevel@tonic-gate 		break;
3117c478bd9Sstevel@tonic-gate 	case PRIV_GLOBAL:
3127c478bd9Sstevel@tonic-gate 		pname = "GLOBAL";
3137c478bd9Sstevel@tonic-gate 		break;
3147c478bd9Sstevel@tonic-gate 	default:
3157c478bd9Sstevel@tonic-gate 		pname = priv_getbynum(priv);
3167c478bd9Sstevel@tonic-gate 		break;
3177c478bd9Sstevel@tonic-gate 	}
3187c478bd9Sstevel@tonic-gate 
3197c478bd9Sstevel@tonic-gate 	if (CR_FLAGS(cr) & PRIV_DEBUG) {
3207c478bd9Sstevel@tonic-gate 		/* Remember last message, just like lwp_badpriv. */
3217c478bd9Sstevel@tonic-gate 		if (curthread->t_pdmsg != NULL) {
3227c478bd9Sstevel@tonic-gate 			kmem_free(curthread->t_pdmsg,
3237c478bd9Sstevel@tonic-gate 			    strlen(curthread->t_pdmsg) + 1);
3247c478bd9Sstevel@tonic-gate 		}
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 		curthread->t_pdmsg = mprintf(fmt, cmd, me->p_pid, pname,
3277c478bd9Sstevel@tonic-gate 		    cr->cr_uid, curthread->t_sysnum, msg, sym, off);
3287c478bd9Sstevel@tonic-gate 
3297c478bd9Sstevel@tonic-gate 		curthread->t_post_sys = 1;
330ddf7fe95Scasper 	}
331ddf7fe95Scasper 	if (priv_debug) {
3327c478bd9Sstevel@tonic-gate 		cmn_err(CE_NOTE, fmt, cmd, me->p_pid, pname, cr->cr_uid,
3337c478bd9Sstevel@tonic-gate 		    curthread->t_sysnum, msg, sym, off);
3347c478bd9Sstevel@tonic-gate 	}
3357c478bd9Sstevel@tonic-gate }
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate /*
338ddf7fe95Scasper  * Override the policy, if appropriate.  Return 0 if the external
339ddf7fe95Scasper  * policy engine approves.
340ddf7fe95Scasper  */
341ddf7fe95Scasper static int
priv_policy_override(const cred_t * cr,int priv,boolean_t allzone,va_list ap)342ddf7fe95Scasper priv_policy_override(const cred_t *cr, int priv, boolean_t allzone, va_list ap)
343ddf7fe95Scasper {
344ddf7fe95Scasper 	priv_set_t set;
345ddf7fe95Scasper 	int ret;
346ddf7fe95Scasper 
347ddf7fe95Scasper 	if (!(CR_FLAGS(cr) & PRIV_XPOLICY))
348ddf7fe95Scasper 		return (-1);
349ddf7fe95Scasper 
350ddf7fe95Scasper 	if (priv == PRIV_ALL) {
351ddf7fe95Scasper 		priv_fillset(&set);
352ddf7fe95Scasper 	} else if (allzone) {
353ddf7fe95Scasper 		set = *ZONEPRIVS(cr);
354ddf7fe95Scasper 	} else {
355ddf7fe95Scasper 		priv_emptyset(&set);
356ddf7fe95Scasper 		priv_addset(&set, priv);
357ddf7fe95Scasper 	}
358ddf7fe95Scasper 	ret = klpd_call(cr, &set, ap);
359ddf7fe95Scasper 	return (ret);
360ddf7fe95Scasper }
361ddf7fe95Scasper 
362ddf7fe95Scasper static int
priv_policy_override_set(const cred_t * cr,const priv_set_t * req,va_list ap)363134a1f4eSCasper H.S. Dik priv_policy_override_set(const cred_t *cr, const priv_set_t *req, va_list ap)
364ddf7fe95Scasper {
365134a1f4eSCasper H.S. Dik 	if (CR_FLAGS(cr) & PRIV_PFEXEC)
366134a1f4eSCasper H.S. Dik 		return (check_user_privs(cr, req));
367ddf7fe95Scasper 	if (CR_FLAGS(cr) & PRIV_XPOLICY) {
368ddf7fe95Scasper 		return (klpd_call(cr, req, ap));
369ddf7fe95Scasper 	}
370ddf7fe95Scasper 	return (-1);
371ddf7fe95Scasper }
372ddf7fe95Scasper 
373134a1f4eSCasper H.S. Dik static int
priv_policy_override_set_va(const cred_t * cr,const priv_set_t * req,...)374134a1f4eSCasper H.S. Dik priv_policy_override_set_va(const cred_t *cr, const priv_set_t *req, ...)
375134a1f4eSCasper H.S. Dik {
376134a1f4eSCasper H.S. Dik 	va_list ap;
377134a1f4eSCasper H.S. Dik 	int ret;
378134a1f4eSCasper H.S. Dik 
379134a1f4eSCasper H.S. Dik 	va_start(ap, req);
380134a1f4eSCasper H.S. Dik 	ret = priv_policy_override_set(cr, req, ap);
381134a1f4eSCasper H.S. Dik 	va_end(ap);
382134a1f4eSCasper H.S. Dik 	return (ret);
383134a1f4eSCasper H.S. Dik }
384134a1f4eSCasper H.S. Dik 
385ddf7fe95Scasper /*
3867c478bd9Sstevel@tonic-gate  * Audit failure, log error message.
3877c478bd9Sstevel@tonic-gate  */
3887c478bd9Sstevel@tonic-gate static void
priv_policy_err(const cred_t * cr,int priv,boolean_t allzone,const char * msg)3897c478bd9Sstevel@tonic-gate priv_policy_err(const cred_t *cr, int priv, boolean_t allzone, const char *msg)
3907c478bd9Sstevel@tonic-gate {
3917c478bd9Sstevel@tonic-gate 
392005d3febSMarek Pospisil 	if (AU_AUDITING())
3937c478bd9Sstevel@tonic-gate 		audit_priv(priv, allzone ? ZONEPRIVS(cr) : NULL, 0);
3947c478bd9Sstevel@tonic-gate 	DTRACE_PROBE2(priv__err, int, priv, boolean_t, allzone);
3957c478bd9Sstevel@tonic-gate 
3967c478bd9Sstevel@tonic-gate 	if (priv_debug || (CR_FLAGS(cr) & PRIV_DEBUG) ||
3977c478bd9Sstevel@tonic-gate 	    curthread->t_pre_sys) {
3987c478bd9Sstevel@tonic-gate 		if (allzone && !HAS_ALLZONEPRIVS(cr)) {
3997c478bd9Sstevel@tonic-gate 			priv_policy_errmsg(cr, PRIV_ALLZONE, msg);
4007c478bd9Sstevel@tonic-gate 		} else {
4017c478bd9Sstevel@tonic-gate 			ASSERT(!HAS_PRIVILEGE(cr, priv));
4027c478bd9Sstevel@tonic-gate 			priv_policy_errmsg(cr, priv, msg);
4037c478bd9Sstevel@tonic-gate 		}
4047c478bd9Sstevel@tonic-gate 	}
4057c478bd9Sstevel@tonic-gate }
4067c478bd9Sstevel@tonic-gate 
4077c478bd9Sstevel@tonic-gate /*
408ddf7fe95Scasper  * priv_policy_ap()
4097c478bd9Sstevel@tonic-gate  * return 0 or error.
4107c478bd9Sstevel@tonic-gate  * See block comment above for a description of "priv" and "allzone" usage.
4117c478bd9Sstevel@tonic-gate  */
412ddf7fe95Scasper static int
priv_policy_ap(const cred_t * cr,int priv,boolean_t allzone,int err,const char * msg,va_list ap)413ddf7fe95Scasper priv_policy_ap(const cred_t *cr, int priv, boolean_t allzone, int err,
414ddf7fe95Scasper     const char *msg, va_list ap)
4157c478bd9Sstevel@tonic-gate {
416ddf7fe95Scasper 	if ((HAS_PRIVILEGE(cr, priv) && (!allzone || HAS_ALLZONEPRIVS(cr))) ||
417ddf7fe95Scasper 	    (!servicing_interrupt() &&
418ddf7fe95Scasper 	    priv_policy_override(cr, priv, allzone, ap) == 0)) {
4197c478bd9Sstevel@tonic-gate 		if ((allzone || priv == PRIV_ALL ||
4207c478bd9Sstevel@tonic-gate 		    !PRIV_ISASSERT(priv_basic, priv)) &&
4217c478bd9Sstevel@tonic-gate 		    !servicing_interrupt()) {
422ae115bc7Smrj 			PTOU(curproc)->u_acflag |= ASU; /* Needed for SVVS */
423005d3febSMarek Pospisil 			if (AU_AUDITING())
4247c478bd9Sstevel@tonic-gate 				audit_priv(priv,
4257c478bd9Sstevel@tonic-gate 				    allzone ? ZONEPRIVS(cr) : NULL, 1);
4267c478bd9Sstevel@tonic-gate 		}
4277c478bd9Sstevel@tonic-gate 		err = 0;
4287c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, allzone);
4297c478bd9Sstevel@tonic-gate 	} else if (!servicing_interrupt()) {
4307c478bd9Sstevel@tonic-gate 		/* Failure audited in this procedure */
4317c478bd9Sstevel@tonic-gate 		priv_policy_err(cr, priv, allzone, msg);
4327c478bd9Sstevel@tonic-gate 	}
4337c478bd9Sstevel@tonic-gate 	return (err);
4347c478bd9Sstevel@tonic-gate }
4357c478bd9Sstevel@tonic-gate 
436ddf7fe95Scasper int
priv_policy_va(const cred_t * cr,int priv,boolean_t allzone,int err,const char * msg,...)437ddf7fe95Scasper priv_policy_va(const cred_t *cr, int priv, boolean_t allzone, int err,
438ddf7fe95Scasper     const char *msg, ...)
439ddf7fe95Scasper {
440ddf7fe95Scasper 	int ret;
441ddf7fe95Scasper 	va_list ap;
442ddf7fe95Scasper 
443ddf7fe95Scasper 	va_start(ap, msg);
444ddf7fe95Scasper 	ret = priv_policy_ap(cr, priv, allzone, err, msg, ap);
445ddf7fe95Scasper 	va_end(ap);
446ddf7fe95Scasper 
447ddf7fe95Scasper 	return (ret);
448ddf7fe95Scasper }
449ddf7fe95Scasper 
450ddf7fe95Scasper int
priv_policy(const cred_t * cr,int priv,boolean_t allzone,int err,const char * msg)451ddf7fe95Scasper priv_policy(const cred_t *cr, int priv, boolean_t allzone, int err,
452ddf7fe95Scasper     const char *msg)
453ddf7fe95Scasper {
454134a1f4eSCasper H.S. Dik 	return (priv_policy_va(cr, priv, allzone, err, msg, KLPDARG_NONE));
455ddf7fe95Scasper }
456ddf7fe95Scasper 
4577c478bd9Sstevel@tonic-gate /*
4587c478bd9Sstevel@tonic-gate  * Return B_TRUE for sufficient privileges, B_FALSE for insufficient privileges.
4597c478bd9Sstevel@tonic-gate  */
4607c478bd9Sstevel@tonic-gate boolean_t
priv_policy_choice(const cred_t * cr,int priv,boolean_t allzone)4617c478bd9Sstevel@tonic-gate priv_policy_choice(const cred_t *cr, int priv, boolean_t allzone)
4627c478bd9Sstevel@tonic-gate {
4637c478bd9Sstevel@tonic-gate 	boolean_t res = HAS_PRIVILEGE(cr, priv) &&
4647c478bd9Sstevel@tonic-gate 	    (!allzone || HAS_ALLZONEPRIVS(cr));
4657c478bd9Sstevel@tonic-gate 
4667c478bd9Sstevel@tonic-gate 	/* Audit success only */
467005d3febSMarek Pospisil 	if (res && AU_AUDITING() &&
4687c478bd9Sstevel@tonic-gate 	    (allzone || priv == PRIV_ALL || !PRIV_ISASSERT(priv_basic, priv)) &&
4697c478bd9Sstevel@tonic-gate 	    !servicing_interrupt()) {
4707c478bd9Sstevel@tonic-gate 		audit_priv(priv, allzone ? ZONEPRIVS(cr) : NULL, 1);
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  * Non-auditing variant of priv_policy_choice().
4827c478bd9Sstevel@tonic-gate  */
4837c478bd9Sstevel@tonic-gate boolean_t
priv_policy_only(const cred_t * cr,int priv,boolean_t allzone)4847c478bd9Sstevel@tonic-gate priv_policy_only(const cred_t *cr, int priv, boolean_t allzone)
4857c478bd9Sstevel@tonic-gate {
4867c478bd9Sstevel@tonic-gate 	boolean_t res = HAS_PRIVILEGE(cr, priv) &&
4877c478bd9Sstevel@tonic-gate 	    (!allzone || HAS_ALLZONEPRIVS(cr));
4887c478bd9Sstevel@tonic-gate 
4897c478bd9Sstevel@tonic-gate 	if (res) {
4907c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(priv__ok, int, priv, boolean_t, allzone);
4917c478bd9Sstevel@tonic-gate 	} else {
4927c478bd9Sstevel@tonic-gate 		DTRACE_PROBE2(priv__err, int, priv, boolean_t, allzone);
4937c478bd9Sstevel@tonic-gate 	}
4947c478bd9Sstevel@tonic-gate 	return (res);
4957c478bd9Sstevel@tonic-gate }
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate /*
4987c478bd9Sstevel@tonic-gate  * Check whether all privileges in the required set are present.
4997c478bd9Sstevel@tonic-gate  */
5007c478bd9Sstevel@tonic-gate static int
secpolicy_require_set(const cred_t * cr,const priv_set_t * req,const char * msg,...)501134a1f4eSCasper H.S. Dik secpolicy_require_set(const cred_t *cr, const priv_set_t *req,
502134a1f4eSCasper H.S. Dik     const char *msg, ...)
5037c478bd9Sstevel@tonic-gate {
5047c478bd9Sstevel@tonic-gate 	int priv;
5057c478bd9Sstevel@tonic-gate 	int pfound = -1;
5067c478bd9Sstevel@tonic-gate 	priv_set_t pset;
507134a1f4eSCasper H.S. Dik 	va_list ap;
508134a1f4eSCasper H.S. Dik 	int ret;
5097c478bd9Sstevel@tonic-gate 
5107c478bd9Sstevel@tonic-gate 	if (req == PRIV_FULLSET ? HAS_ALLPRIVS(cr) : priv_issubset(req,
5117c478bd9Sstevel@tonic-gate 	    &CR_OEPRIV(cr))) {
5127c478bd9Sstevel@tonic-gate 		return (0);
5137c478bd9Sstevel@tonic-gate 	}
5147c478bd9Sstevel@tonic-gate 
515134a1f4eSCasper H.S. Dik 	va_start(ap, msg);
516134a1f4eSCasper H.S. Dik 	ret = priv_policy_override_set(cr, req, ap);
517134a1f4eSCasper H.S. Dik 	va_end(ap);
518134a1f4eSCasper H.S. Dik 	if (ret == 0)
519ddf7fe95Scasper 		return (0);
520ddf7fe95Scasper 
5217c478bd9Sstevel@tonic-gate 	if (req == PRIV_FULLSET || priv_isfullset(req)) {
5227c478bd9Sstevel@tonic-gate 		priv_policy_err(cr, PRIV_ALL, B_FALSE, msg);
5237c478bd9Sstevel@tonic-gate 		return (EACCES);
5247c478bd9Sstevel@tonic-gate 	}
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 	pset = CR_OEPRIV(cr);		/* present privileges */
5277c478bd9Sstevel@tonic-gate 	priv_inverse(&pset);		/* all non present privileges */
5287c478bd9Sstevel@tonic-gate 	priv_intersect(req, &pset);	/* the actual missing privs */
5297c478bd9Sstevel@tonic-gate 
530005d3febSMarek Pospisil 	if (AU_AUDITING())
5317c478bd9Sstevel@tonic-gate 		audit_priv(PRIV_NONE, &pset, 0);
5327c478bd9Sstevel@tonic-gate 	/*
5337c478bd9Sstevel@tonic-gate 	 * Privilege debugging; special case "one privilege in set".
5347c478bd9Sstevel@tonic-gate 	 */
5357c478bd9Sstevel@tonic-gate 	if (priv_debug || (CR_FLAGS(cr) & PRIV_DEBUG) || curthread->t_pre_sys) {
5367c478bd9Sstevel@tonic-gate 		for (priv = 0; priv < nprivs; priv++) {
5377c478bd9Sstevel@tonic-gate 			if (priv_ismember(&pset, priv)) {
5387c478bd9Sstevel@tonic-gate 				if (pfound != -1) {
5397c478bd9Sstevel@tonic-gate 					/* Multiple missing privs */
5407c478bd9Sstevel@tonic-gate 					priv_policy_errmsg(cr, PRIV_MULTIPLE,
5417c478bd9Sstevel@tonic-gate 					    msg);
5427c478bd9Sstevel@tonic-gate 					return (EACCES);
5437c478bd9Sstevel@tonic-gate 				}
5447c478bd9Sstevel@tonic-gate 				pfound = priv;
5457c478bd9Sstevel@tonic-gate 			}
5467c478bd9Sstevel@tonic-gate 		}
5477c478bd9Sstevel@tonic-gate 		ASSERT(pfound != -1);
5487c478bd9Sstevel@tonic-gate 		/* Just the one missing privilege */
5497c478bd9Sstevel@tonic-gate 		priv_policy_errmsg(cr, pfound, msg);
5507c478bd9Sstevel@tonic-gate 	}
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 	return (EACCES);
5537c478bd9Sstevel@tonic-gate }
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate /*
5567c478bd9Sstevel@tonic-gate  * Called when an operation requires that the caller be in the
5577c478bd9Sstevel@tonic-gate  * global zone, regardless of privilege.
5587c478bd9Sstevel@tonic-gate  */
5597c478bd9Sstevel@tonic-gate static int
priv_policy_global(const cred_t * cr)5607c478bd9Sstevel@tonic-gate priv_policy_global(const cred_t *cr)
5617c478bd9Sstevel@tonic-gate {
5627c478bd9Sstevel@tonic-gate 	if (crgetzoneid(cr) == GLOBAL_ZONEID)
5637c478bd9Sstevel@tonic-gate 		return (0);	/* success */
5647c478bd9Sstevel@tonic-gate 
5657c478bd9Sstevel@tonic-gate 	if (priv_debug || (CR_FLAGS(cr) & PRIV_DEBUG) ||
5667c478bd9Sstevel@tonic-gate 	    curthread->t_pre_sys) {
5677c478bd9Sstevel@tonic-gate 		priv_policy_errmsg(cr, PRIV_GLOBAL, NULL);
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 	return (EPERM);
5707c478bd9Sstevel@tonic-gate }
5717c478bd9Sstevel@tonic-gate 
5727c478bd9Sstevel@tonic-gate /*
57324d819e6SJerry Jelinek  * Raising process priority
57424d819e6SJerry Jelinek  */
57524d819e6SJerry Jelinek int
secpolicy_raisepriority(const cred_t * cr)57624d819e6SJerry Jelinek secpolicy_raisepriority(const cred_t *cr)
57724d819e6SJerry Jelinek {
57824d819e6SJerry Jelinek 	if (PRIV_POLICY(cr, PRIV_PROC_PRIOUP, B_FALSE, EPERM, NULL) == 0)
57924d819e6SJerry Jelinek 		return (0);
58024d819e6SJerry Jelinek 	return (secpolicy_setpriority(cr));
58124d819e6SJerry Jelinek }
58224d819e6SJerry Jelinek 
58324d819e6SJerry Jelinek /*
58424d819e6SJerry Jelinek  * Changing process priority or scheduling class
5857c478bd9Sstevel@tonic-gate  */
5867c478bd9Sstevel@tonic-gate int
secpolicy_setpriority(const cred_t * cr)5877c478bd9Sstevel@tonic-gate secpolicy_setpriority(const cred_t *cr)
5887c478bd9Sstevel@tonic-gate {
5897c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_PRIOCNTL, B_FALSE, EPERM, NULL));
5907c478bd9Sstevel@tonic-gate }
5917c478bd9Sstevel@tonic-gate 
5927c478bd9Sstevel@tonic-gate /*
5937c478bd9Sstevel@tonic-gate  * Binding to a privileged port, port must be specified in host byte
5947c478bd9Sstevel@tonic-gate  * order.
595134a1f4eSCasper H.S. Dik  * When adding a new privilege which allows binding to currently privileged
596134a1f4eSCasper H.S. Dik  * ports, then you MUST also allow processes with PRIV_NET_PRIVADDR bind
597134a1f4eSCasper H.S. Dik  * to these ports because of backward compatibility.
5987c478bd9Sstevel@tonic-gate  */
5997c478bd9Sstevel@tonic-gate int
secpolicy_net_privaddr(const cred_t * cr,in_port_t port,int proto)600ddf7fe95Scasper secpolicy_net_privaddr(const cred_t *cr, in_port_t port, int proto)
6017c478bd9Sstevel@tonic-gate {
602da6c28aaSamw 	char *reason;
603da6c28aaSamw 	int priv;
604da6c28aaSamw 
605da6c28aaSamw 	switch (port) {
606da6c28aaSamw 	case 137:
607da6c28aaSamw 	case 138:
608da6c28aaSamw 	case 139:
609da6c28aaSamw 	case 445:
610da6c28aaSamw 		/*
611134a1f4eSCasper H.S. Dik 		 * NBT and SMB ports, these are normal privileged ports,
612134a1f4eSCasper H.S. Dik 		 * allow bind only if the SYS_SMB or NET_PRIVADDR privilege
613134a1f4eSCasper H.S. Dik 		 * is present.
614134a1f4eSCasper H.S. Dik 		 * Try both, if neither is present return an error for
615134a1f4eSCasper H.S. Dik 		 * priv SYS_SMB.
616da6c28aaSamw 		 */
617134a1f4eSCasper H.S. Dik 		if (PRIV_POLICY_ONLY(cr, PRIV_NET_PRIVADDR, B_FALSE))
618134a1f4eSCasper H.S. Dik 			priv = PRIV_NET_PRIVADDR;
619134a1f4eSCasper H.S. Dik 		else
620da6c28aaSamw 			priv = PRIV_SYS_SMB;
621da6c28aaSamw 		reason = "NBT or SMB port";
622da6c28aaSamw 		break;
623da6c28aaSamw 
624da6c28aaSamw 	case 2049:
625da6c28aaSamw 	case 4045:
6267c478bd9Sstevel@tonic-gate 		/*
6277c478bd9Sstevel@tonic-gate 		 * NFS ports, these are extra privileged ports, allow bind
6287c478bd9Sstevel@tonic-gate 		 * only if the SYS_NFS privilege is present.
6297c478bd9Sstevel@tonic-gate 		 */
630da6c28aaSamw 		priv = PRIV_SYS_NFS;
631da6c28aaSamw 		reason = "NFS port";
632da6c28aaSamw 		break;
633da6c28aaSamw 
634da6c28aaSamw 	default:
635da6c28aaSamw 		priv = PRIV_NET_PRIVADDR;
636da6c28aaSamw 		reason = NULL;
637da6c28aaSamw 		break;
638da6c28aaSamw 
639da6c28aaSamw 	}
640da6c28aaSamw 
641ddf7fe95Scasper 	return (priv_policy_va(cr, priv, B_FALSE, EACCES, reason,
642ddf7fe95Scasper 	    KLPDARG_PORT, (int)proto, (int)port, KLPDARG_NOMORE));
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate /*
64645916cd2Sjpk  * Binding to a multilevel port on a trusted (labeled) system.
64745916cd2Sjpk  */
64845916cd2Sjpk int
secpolicy_net_bindmlp(const cred_t * cr)64945916cd2Sjpk secpolicy_net_bindmlp(const cred_t *cr)
65045916cd2Sjpk {
651ddf7fe95Scasper 	return (PRIV_POLICY(cr, PRIV_NET_BINDMLP, B_FALSE, EACCES, NULL));
65245916cd2Sjpk }
65345916cd2Sjpk 
65445916cd2Sjpk /*
65545916cd2Sjpk  * Allow a communication between a zone and an unlabeled host when their
65645916cd2Sjpk  * labels don't match.
65745916cd2Sjpk  */
65845916cd2Sjpk int
secpolicy_net_mac_aware(const cred_t * cr)65945916cd2Sjpk secpolicy_net_mac_aware(const cred_t *cr)
66045916cd2Sjpk {
661ddf7fe95Scasper 	return (PRIV_POLICY(cr, PRIV_NET_MAC_AWARE, B_FALSE, EACCES, NULL));
66245916cd2Sjpk }
66345916cd2Sjpk 
66445916cd2Sjpk /*
6655d3b8cb7SBill Sommerfeld  * Allow a privileged process to transmit traffic without explicit labels
6665d3b8cb7SBill Sommerfeld  */
6675d3b8cb7SBill Sommerfeld int
secpolicy_net_mac_implicit(const cred_t * cr)6685d3b8cb7SBill Sommerfeld secpolicy_net_mac_implicit(const cred_t *cr)
6695d3b8cb7SBill Sommerfeld {
6705d3b8cb7SBill Sommerfeld 	return (PRIV_POLICY(cr, PRIV_NET_MAC_IMPLICIT, B_FALSE, EACCES, NULL));
6715d3b8cb7SBill Sommerfeld }
6725d3b8cb7SBill Sommerfeld 
6735d3b8cb7SBill Sommerfeld /*
6747c478bd9Sstevel@tonic-gate  * Common routine which determines whether a given credential can
6757c478bd9Sstevel@tonic-gate  * act on a given mount.
6767c478bd9Sstevel@tonic-gate  * When called through mount, the parameter needoptcheck is a pointer
6777c478bd9Sstevel@tonic-gate  * to a boolean variable which will be set to either true or false,
6787c478bd9Sstevel@tonic-gate  * depending on whether the mount policy should change the mount options.
6797c478bd9Sstevel@tonic-gate  * In all other cases, needoptcheck should be a NULL pointer.
6807c478bd9Sstevel@tonic-gate  */
6817c478bd9Sstevel@tonic-gate static int
secpolicy_fs_common(cred_t * cr,vnode_t * mvp,const vfs_t * vfsp,boolean_t * needoptcheck)6827c478bd9Sstevel@tonic-gate secpolicy_fs_common(cred_t *cr, vnode_t *mvp, const vfs_t *vfsp,
6837c478bd9Sstevel@tonic-gate     boolean_t *needoptcheck)
6847c478bd9Sstevel@tonic-gate {
6857c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
6867c478bd9Sstevel@tonic-gate 	boolean_t mounting = needoptcheck != NULL;
6877c478bd9Sstevel@tonic-gate 
6887c478bd9Sstevel@tonic-gate 	/*
6897c478bd9Sstevel@tonic-gate 	 * Short circuit the following cases:
6907c478bd9Sstevel@tonic-gate 	 *	vfsp == NULL or mvp == NULL (pure privilege check)
6917c478bd9Sstevel@tonic-gate 	 *	have all privileges - no further checks required
6927c478bd9Sstevel@tonic-gate 	 *	and no mount options need to be set.
6937c478bd9Sstevel@tonic-gate 	 */
6947c478bd9Sstevel@tonic-gate 	if (vfsp == NULL || mvp == NULL || HAS_ALLPRIVS(cr)) {
6957c478bd9Sstevel@tonic-gate 		if (mounting)
6967c478bd9Sstevel@tonic-gate 			*needoptcheck = B_FALSE;
6977c478bd9Sstevel@tonic-gate 
698ddf7fe95Scasper 		return (priv_policy_va(cr, PRIV_SYS_MOUNT, allzone, EPERM,
699ddf7fe95Scasper 		    NULL, KLPDARG_VNODE, mvp, (char *)NULL, KLPDARG_NOMORE));
7007c478bd9Sstevel@tonic-gate 	}
7017c478bd9Sstevel@tonic-gate 
7027c478bd9Sstevel@tonic-gate 	/*
7037c478bd9Sstevel@tonic-gate 	 * When operating on an existing mount (either we're not mounting
7047c478bd9Sstevel@tonic-gate 	 * or we're doing a remount and VFS_REMOUNT will be set), zones
7057c478bd9Sstevel@tonic-gate 	 * can operate only on mounts established by the zone itself.
7067c478bd9Sstevel@tonic-gate 	 */
7077c478bd9Sstevel@tonic-gate 	if (!mounting || (vfsp->vfs_flag & VFS_REMOUNT) != 0) {
7087c478bd9Sstevel@tonic-gate 		zoneid_t zoneid = crgetzoneid(cr);
7097c478bd9Sstevel@tonic-gate 
7107c478bd9Sstevel@tonic-gate 		if (zoneid != GLOBAL_ZONEID &&
7117c478bd9Sstevel@tonic-gate 		    vfsp->vfs_zone->zone_id != zoneid) {
7127c478bd9Sstevel@tonic-gate 			return (EPERM);
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	if (mounting)
7177c478bd9Sstevel@tonic-gate 		*needoptcheck = B_TRUE;
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate 	/*
7207c478bd9Sstevel@tonic-gate 	 * Overlay mounts may hide important stuff; if you can't write to a
7217c478bd9Sstevel@tonic-gate 	 * mount point but would be able to mount on top of it, you can
7227c478bd9Sstevel@tonic-gate 	 * escalate your privileges.
7237c478bd9Sstevel@tonic-gate 	 * So we go about asking the same questions namefs does when it
7247c478bd9Sstevel@tonic-gate 	 * decides whether you can mount over a file or not but with the
7257c478bd9Sstevel@tonic-gate 	 * added restriction that you can only mount on top of a regular
7267c478bd9Sstevel@tonic-gate 	 * file or directory.
7277c478bd9Sstevel@tonic-gate 	 * If we have all the zone's privileges, we skip all other checks,
7287c478bd9Sstevel@tonic-gate 	 * or else we may actually get in trouble inside the automounter.
7297c478bd9Sstevel@tonic-gate 	 */
7307c478bd9Sstevel@tonic-gate 	if ((mvp->v_flag & VROOT) != 0 ||
7317c478bd9Sstevel@tonic-gate 	    (mvp->v_type != VDIR && mvp->v_type != VREG) ||
7327c478bd9Sstevel@tonic-gate 	    HAS_ALLZONEPRIVS(cr)) {
7337c478bd9Sstevel@tonic-gate 		allzone = B_TRUE;
7347c478bd9Sstevel@tonic-gate 	} else {
7357c478bd9Sstevel@tonic-gate 		vattr_t va;
7367c478bd9Sstevel@tonic-gate 		int err;
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 		va.va_mask = AT_UID|AT_MODE;
739da6c28aaSamw 		err = VOP_GETATTR(mvp, &va, 0, cr, NULL);
7407c478bd9Sstevel@tonic-gate 		if (err != 0)
7417c478bd9Sstevel@tonic-gate 			return (err);
7427c478bd9Sstevel@tonic-gate 
7437c478bd9Sstevel@tonic-gate 		if ((err = secpolicy_vnode_owner(cr, va.va_uid)) != 0)
7447c478bd9Sstevel@tonic-gate 			return (err);
7457c478bd9Sstevel@tonic-gate 
746134a1f4eSCasper H.S. Dik 		if (secpolicy_vnode_access2(cr, mvp, va.va_uid, va.va_mode,
747134a1f4eSCasper H.S. Dik 		    VWRITE) != 0) {
7487c478bd9Sstevel@tonic-gate 			return (EACCES);
7497c478bd9Sstevel@tonic-gate 		}
7507c478bd9Sstevel@tonic-gate 	}
751ddf7fe95Scasper 	return (priv_policy_va(cr, PRIV_SYS_MOUNT, allzone, EPERM,
752ddf7fe95Scasper 	    NULL, KLPDARG_VNODE, mvp, (char *)NULL, KLPDARG_NOMORE));
7537c478bd9Sstevel@tonic-gate }
7547c478bd9Sstevel@tonic-gate 
755ecd6cf80Smarks void
secpolicy_fs_mount_clearopts(cred_t * cr,struct vfs * vfsp)756ecd6cf80Smarks secpolicy_fs_mount_clearopts(cred_t *cr, struct vfs *vfsp)
7577c478bd9Sstevel@tonic-gate {
7587c478bd9Sstevel@tonic-gate 	boolean_t amsuper = HAS_ALLZONEPRIVS(cr);
7597c478bd9Sstevel@tonic-gate 
7607c478bd9Sstevel@tonic-gate 	/*
761ecd6cf80Smarks 	 * check; if we don't have either "nosuid" or
7627c478bd9Sstevel@tonic-gate 	 * both "nosetuid" and "nodevices", then we add
7637c478bd9Sstevel@tonic-gate 	 * "nosuid"; this depends on how the current
7647c478bd9Sstevel@tonic-gate 	 * implementation works (it first checks nosuid).  In a
7657c478bd9Sstevel@tonic-gate 	 * zone, a user with all zone privileges can mount with
7667c478bd9Sstevel@tonic-gate 	 * "setuid" but never with "devices".
7677c478bd9Sstevel@tonic-gate 	 */
7687c478bd9Sstevel@tonic-gate 	if (!vfs_optionisset(vfsp, MNTOPT_NOSUID, NULL) &&
7697c478bd9Sstevel@tonic-gate 	    (!vfs_optionisset(vfsp, MNTOPT_NODEVICES, NULL) ||
7707c478bd9Sstevel@tonic-gate 	    !vfs_optionisset(vfsp, MNTOPT_NOSETUID, NULL))) {
7717c478bd9Sstevel@tonic-gate 		if (crgetzoneid(cr) == GLOBAL_ZONEID || !amsuper)
7727c478bd9Sstevel@tonic-gate 			vfs_setmntopt(vfsp, MNTOPT_NOSUID, NULL, 0);
7737c478bd9Sstevel@tonic-gate 		else
7747c478bd9Sstevel@tonic-gate 			vfs_setmntopt(vfsp, MNTOPT_NODEVICES, NULL, 0);
7757c478bd9Sstevel@tonic-gate 	}
7767c478bd9Sstevel@tonic-gate 	/*
7777c478bd9Sstevel@tonic-gate 	 * If we're not the local super user, we set the "restrict"
7787c478bd9Sstevel@tonic-gate 	 * option to indicate to automountd that this mount should
7797c478bd9Sstevel@tonic-gate 	 * be handled with care.
7807c478bd9Sstevel@tonic-gate 	 */
7817c478bd9Sstevel@tonic-gate 	if (!amsuper)
7827c478bd9Sstevel@tonic-gate 		vfs_setmntopt(vfsp, MNTOPT_RESTRICT, NULL, 0);
7837c478bd9Sstevel@tonic-gate 
7847c478bd9Sstevel@tonic-gate }
785ecd6cf80Smarks 
7860fbb751dSJohn Levon int
secpolicy_fs_allowed_mount(const char * fsname)7870fbb751dSJohn Levon secpolicy_fs_allowed_mount(const char *fsname)
7880fbb751dSJohn Levon {
7890fbb751dSJohn Levon 	struct vfssw *vswp;
7900fbb751dSJohn Levon 	const char *p;
7910fbb751dSJohn Levon 	size_t len;
7920fbb751dSJohn Levon 
7930fbb751dSJohn Levon 	ASSERT(fsname != NULL);
7940fbb751dSJohn Levon 	ASSERT(fsname[0] != '\0');
7950fbb751dSJohn Levon 
7960fbb751dSJohn Levon 	if (INGLOBALZONE(curproc))
7970fbb751dSJohn Levon 		return (0);
7980fbb751dSJohn Levon 
7990fbb751dSJohn Levon 	vswp = vfs_getvfssw(fsname);
8000fbb751dSJohn Levon 	if (vswp == NULL)
8010fbb751dSJohn Levon 		return (ENOENT);
8020fbb751dSJohn Levon 
8030fbb751dSJohn Levon 	if ((vswp->vsw_flag & VSW_ZMOUNT) != 0) {
8040fbb751dSJohn Levon 		vfs_unrefvfssw(vswp);
8050fbb751dSJohn Levon 		return (0);
8060fbb751dSJohn Levon 	}
8070fbb751dSJohn Levon 
8080fbb751dSJohn Levon 	vfs_unrefvfssw(vswp);
8090fbb751dSJohn Levon 
8100fbb751dSJohn Levon 	p = curzone->zone_fs_allowed;
8110fbb751dSJohn Levon 	len = strlen(fsname);
8120fbb751dSJohn Levon 
8130fbb751dSJohn Levon 	while (p != NULL && *p != '\0') {
8140fbb751dSJohn Levon 		if (strncmp(p, fsname, len) == 0) {
8150fbb751dSJohn Levon 			char c = *(p + len);
8160fbb751dSJohn Levon 			if (c == '\0' || c == ',')
8170fbb751dSJohn Levon 				return (0);
8180fbb751dSJohn Levon 		}
8190fbb751dSJohn Levon 
8200fbb751dSJohn Levon 		/* skip to beyond the next comma */
8210fbb751dSJohn Levon 		if ((p = strchr(p, ',')) != NULL)
8220fbb751dSJohn Levon 			p++;
8230fbb751dSJohn Levon 	}
8240fbb751dSJohn Levon 
8250fbb751dSJohn Levon 	return (EPERM);
8260fbb751dSJohn Levon }
8270fbb751dSJohn Levon 
828ecd6cf80Smarks extern vnode_t *rootvp;
829ecd6cf80Smarks extern vfs_t *rootvfs;
830ecd6cf80Smarks 
831ecd6cf80Smarks int
secpolicy_fs_mount(cred_t * cr,vnode_t * mvp,struct vfs * vfsp)832ecd6cf80Smarks secpolicy_fs_mount(cred_t *cr, vnode_t *mvp, struct vfs *vfsp)
833ecd6cf80Smarks {
834ecd6cf80Smarks 	boolean_t needoptchk;
835ecd6cf80Smarks 	int error;
836ecd6cf80Smarks 
837ecd6cf80Smarks 	/*
838ecd6cf80Smarks 	 * If it's a remount, get the underlying mount point,
839ecd6cf80Smarks 	 * except for the root where we use the rootvp.
840ecd6cf80Smarks 	 */
841ecd6cf80Smarks 	if ((vfsp->vfs_flag & VFS_REMOUNT) != 0) {
842ecd6cf80Smarks 		if (vfsp == rootvfs)
843ecd6cf80Smarks 			mvp = rootvp;
844ecd6cf80Smarks 		else
845ecd6cf80Smarks 			mvp = vfsp->vfs_vnodecovered;
846ecd6cf80Smarks 	}
847ecd6cf80Smarks 
848ecd6cf80Smarks 	error = secpolicy_fs_common(cr, mvp, vfsp, &needoptchk);
849ecd6cf80Smarks 
850ecd6cf80Smarks 	if (error == 0 && needoptchk) {
851ecd6cf80Smarks 		secpolicy_fs_mount_clearopts(cr, vfsp);
852ecd6cf80Smarks 	}
853ecd6cf80Smarks 
8547c478bd9Sstevel@tonic-gate 	return (error);
8557c478bd9Sstevel@tonic-gate }
8567c478bd9Sstevel@tonic-gate 
8577c478bd9Sstevel@tonic-gate /*
8587c478bd9Sstevel@tonic-gate  * Does the policy computations for "ownership" of a mount;
8597c478bd9Sstevel@tonic-gate  * here ownership is defined as the ability to "mount"
8607c478bd9Sstevel@tonic-gate  * the filesystem originally.  The rootvfs doesn't cover any
8617c478bd9Sstevel@tonic-gate  * vnodes; we attribute its ownership to the rootvp.
8627c478bd9Sstevel@tonic-gate  */
8637c478bd9Sstevel@tonic-gate static int
secpolicy_fs_owner(cred_t * cr,const struct vfs * vfsp)8647c478bd9Sstevel@tonic-gate secpolicy_fs_owner(cred_t *cr, const struct vfs *vfsp)
8657c478bd9Sstevel@tonic-gate {
8667c478bd9Sstevel@tonic-gate 	vnode_t *mvp;
8677c478bd9Sstevel@tonic-gate 
8687c478bd9Sstevel@tonic-gate 	if (vfsp == NULL)
8697c478bd9Sstevel@tonic-gate 		mvp = NULL;
8707c478bd9Sstevel@tonic-gate 	else if (vfsp == rootvfs)
8717c478bd9Sstevel@tonic-gate 		mvp = rootvp;
8727c478bd9Sstevel@tonic-gate 	else
8737c478bd9Sstevel@tonic-gate 		mvp = vfsp->vfs_vnodecovered;
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_common(cr, mvp, vfsp, NULL));
8767c478bd9Sstevel@tonic-gate }
8777c478bd9Sstevel@tonic-gate 
8787c478bd9Sstevel@tonic-gate int
secpolicy_fs_unmount(cred_t * cr,struct vfs * vfsp)8797c478bd9Sstevel@tonic-gate secpolicy_fs_unmount(cred_t *cr, struct vfs *vfsp)
8807c478bd9Sstevel@tonic-gate {
8817c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_owner(cr, vfsp));
8827c478bd9Sstevel@tonic-gate }
8837c478bd9Sstevel@tonic-gate 
8847c478bd9Sstevel@tonic-gate /*
88548bbca81SDaniel Hoffman  * Quotas are a resource, but if one has the ability to mount a filesystem,
88648bbca81SDaniel Hoffman  * they should be able to modify quotas on it.
8877c478bd9Sstevel@tonic-gate  */
8887c478bd9Sstevel@tonic-gate int
secpolicy_fs_quota(const cred_t * cr,const vfs_t * vfsp)8897c478bd9Sstevel@tonic-gate secpolicy_fs_quota(const cred_t *cr, const vfs_t *vfsp)
8907c478bd9Sstevel@tonic-gate {
8917c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
8927c478bd9Sstevel@tonic-gate }
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate /*
8957c478bd9Sstevel@tonic-gate  * Exceeding minfree: also a per-mount resource constraint.
8967c478bd9Sstevel@tonic-gate  */
8977c478bd9Sstevel@tonic-gate int
secpolicy_fs_minfree(const cred_t * cr,const vfs_t * vfsp)8987c478bd9Sstevel@tonic-gate secpolicy_fs_minfree(const cred_t *cr, const vfs_t *vfsp)
8997c478bd9Sstevel@tonic-gate {
9007c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
9017c478bd9Sstevel@tonic-gate }
9027c478bd9Sstevel@tonic-gate 
9037c478bd9Sstevel@tonic-gate int
secpolicy_fs_config(const cred_t * cr,const vfs_t * vfsp)9047c478bd9Sstevel@tonic-gate secpolicy_fs_config(const cred_t *cr, const vfs_t *vfsp)
9057c478bd9Sstevel@tonic-gate {
9067c478bd9Sstevel@tonic-gate 	return (secpolicy_fs_owner((cred_t *)cr, vfsp));
9077c478bd9Sstevel@tonic-gate }
9087c478bd9Sstevel@tonic-gate 
9097c478bd9Sstevel@tonic-gate /* ARGSUSED */
9107c478bd9Sstevel@tonic-gate int
secpolicy_fs_linkdir(const cred_t * cr,const vfs_t * vfsp)9117c478bd9Sstevel@tonic-gate secpolicy_fs_linkdir(const cred_t *cr, const vfs_t *vfsp)
9127c478bd9Sstevel@tonic-gate {
913ad8f9d95SJoshua M. Clulow 	if (priv_allow_linkdir == 0) {
914ad8f9d95SJoshua M. Clulow 		/*
915ad8f9d95SJoshua M. Clulow 		 * By default, this policy check will now always return EPERM
916ad8f9d95SJoshua M. Clulow 		 * unless overridden.
917ad8f9d95SJoshua M. Clulow 		 *
918ad8f9d95SJoshua M. Clulow 		 * We do so without triggering auditing or allowing privilege
919ad8f9d95SJoshua M. Clulow 		 * debugging for two reasons: first, we intend eventually to
920ad8f9d95SJoshua M. Clulow 		 * deprecate the PRIV_SYS_LINKDIR privilege entirely and remove
921ad8f9d95SJoshua M. Clulow 		 * the use of this policy check from the file systems; second,
922ad8f9d95SJoshua M. Clulow 		 * for privilege debugging in particular, because it would be
923ad8f9d95SJoshua M. Clulow 		 * confusing to report an unlink() failure as the result of a
924ad8f9d95SJoshua M. Clulow 		 * missing privilege when in fact we are simply no longer
925ad8f9d95SJoshua M. Clulow 		 * allowing the operation at all.
926ad8f9d95SJoshua M. Clulow 		 */
927ad8f9d95SJoshua M. Clulow 		return (EPERM);
928ad8f9d95SJoshua M. Clulow 	}
929ad8f9d95SJoshua M. Clulow 
9307c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_LINKDIR, B_FALSE, EPERM, NULL));
9317c478bd9Sstevel@tonic-gate }
9327c478bd9Sstevel@tonic-gate 
9337c478bd9Sstevel@tonic-gate /*
9347c478bd9Sstevel@tonic-gate  * Name:        secpolicy_vnode_access()
9357c478bd9Sstevel@tonic-gate  *
9367c478bd9Sstevel@tonic-gate  * Parameters:  Process credential
9377c478bd9Sstevel@tonic-gate  *		vnode
9387c478bd9Sstevel@tonic-gate  *		uid of owner of vnode
9397c478bd9Sstevel@tonic-gate  *		permission bits not granted to the caller when examining
9407c478bd9Sstevel@tonic-gate  *		file mode bits (i.e., when a process wants to open a
9417c478bd9Sstevel@tonic-gate  *		mode 444 file for VREAD|VWRITE, this function should be
9427c478bd9Sstevel@tonic-gate  *		called only with a VWRITE argument).
9437c478bd9Sstevel@tonic-gate  *
9447c478bd9Sstevel@tonic-gate  * Normal:      Verifies that cred has the appropriate privileges to
9457c478bd9Sstevel@tonic-gate  *              override the mode bits that were denied.
9467c478bd9Sstevel@tonic-gate  *
9477c478bd9Sstevel@tonic-gate  * Override:    file_dac_execute - if VEXEC bit was denied and vnode is
9487c478bd9Sstevel@tonic-gate  *                      not a directory.
9497c478bd9Sstevel@tonic-gate  *              file_dac_read - if VREAD bit was denied.
9507c478bd9Sstevel@tonic-gate  *              file_dac_search - if VEXEC bit was denied and vnode is
9517c478bd9Sstevel@tonic-gate  *                      a directory.
9527c478bd9Sstevel@tonic-gate  *              file_dac_write - if VWRITE bit was denied.
9537c478bd9Sstevel@tonic-gate  *
9547c478bd9Sstevel@tonic-gate  *		Root owned files are special cased to protect system
9557c478bd9Sstevel@tonic-gate  *		configuration files and such.
9567c478bd9Sstevel@tonic-gate  *
9577c478bd9Sstevel@tonic-gate  * Output:      EACCES - if privilege check fails.
9587c478bd9Sstevel@tonic-gate  */
9597c478bd9Sstevel@tonic-gate 
9607c478bd9Sstevel@tonic-gate int
secpolicy_vnode_access(const cred_t * cr,vnode_t * vp,uid_t owner,mode_t mode)9617c478bd9Sstevel@tonic-gate secpolicy_vnode_access(const cred_t *cr, vnode_t *vp, uid_t owner, mode_t mode)
9627c478bd9Sstevel@tonic-gate {
963ddf7fe95Scasper 	if ((mode & VREAD) && priv_policy_va(cr, PRIV_FILE_DAC_READ, B_FALSE,
964ddf7fe95Scasper 	    EACCES, NULL, KLPDARG_VNODE, vp, (char *)NULL,
965ddf7fe95Scasper 	    KLPDARG_NOMORE) != 0) {
9667c478bd9Sstevel@tonic-gate 		return (EACCES);
967ddf7fe95Scasper 	}
9687c478bd9Sstevel@tonic-gate 
9697c478bd9Sstevel@tonic-gate 	if (mode & VWRITE) {
9707c478bd9Sstevel@tonic-gate 		boolean_t allzone;
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 		if (owner == 0 && cr->cr_uid != 0)
9737c478bd9Sstevel@tonic-gate 			allzone = B_TRUE;
9747c478bd9Sstevel@tonic-gate 		else
9757c478bd9Sstevel@tonic-gate 			allzone = B_FALSE;
976ddf7fe95Scasper 		if (priv_policy_va(cr, PRIV_FILE_DAC_WRITE, allzone, EACCES,
977ddf7fe95Scasper 		    NULL, KLPDARG_VNODE, vp, (char *)NULL,
978ddf7fe95Scasper 		    KLPDARG_NOMORE) != 0) {
9797c478bd9Sstevel@tonic-gate 			return (EACCES);
9807c478bd9Sstevel@tonic-gate 		}
981ddf7fe95Scasper 	}
9827c478bd9Sstevel@tonic-gate 
9837c478bd9Sstevel@tonic-gate 	if (mode & VEXEC) {
9847c478bd9Sstevel@tonic-gate 		/*
9857c478bd9Sstevel@tonic-gate 		 * Directories use file_dac_search to override the execute bit.
9867c478bd9Sstevel@tonic-gate 		 */
987ddf7fe95Scasper 		int p = vp->v_type == VDIR ? PRIV_FILE_DAC_SEARCH :
988ddf7fe95Scasper 		    PRIV_FILE_DAC_EXECUTE;
9897c478bd9Sstevel@tonic-gate 
990ddf7fe95Scasper 		return (priv_policy_va(cr, p, B_FALSE, EACCES, NULL,
991ddf7fe95Scasper 		    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
9927c478bd9Sstevel@tonic-gate 	}
9937c478bd9Sstevel@tonic-gate 	return (0);
9947c478bd9Sstevel@tonic-gate }
9957c478bd9Sstevel@tonic-gate 
9967c478bd9Sstevel@tonic-gate /*
997134a1f4eSCasper H.S. Dik  * Like secpolicy_vnode_access() but we get the actual wanted mode and the
998134a1f4eSCasper H.S. Dik  * current mode of the file, not the missing bits.
999134a1f4eSCasper H.S. Dik  */
1000134a1f4eSCasper H.S. Dik int
secpolicy_vnode_access2(const cred_t * cr,vnode_t * vp,uid_t owner,mode_t curmode,mode_t wantmode)1001134a1f4eSCasper H.S. Dik secpolicy_vnode_access2(const cred_t *cr, vnode_t *vp, uid_t owner,
1002134a1f4eSCasper H.S. Dik     mode_t curmode, mode_t wantmode)
1003134a1f4eSCasper H.S. Dik {
1004134a1f4eSCasper H.S. Dik 	mode_t mode;
1005134a1f4eSCasper H.S. Dik 
1006134a1f4eSCasper H.S. Dik 	/* Inline the basic privileges tests. */
1007134a1f4eSCasper H.S. Dik 	if ((wantmode & VREAD) &&
1008134a1f4eSCasper H.S. Dik 	    !PRIV_ISASSERT(&CR_OEPRIV(cr), PRIV_FILE_READ) &&
1009134a1f4eSCasper H.S. Dik 	    priv_policy_va(cr, PRIV_FILE_READ, B_FALSE, EACCES, NULL,
1010134a1f4eSCasper H.S. Dik 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE) != 0) {
1011134a1f4eSCasper H.S. Dik 		return (EACCES);
1012134a1f4eSCasper H.S. Dik 	}
1013134a1f4eSCasper H.S. Dik 
1014134a1f4eSCasper H.S. Dik 	if ((wantmode & VWRITE) &&
1015134a1f4eSCasper H.S. Dik 	    !PRIV_ISASSERT(&CR_OEPRIV(cr), PRIV_FILE_WRITE) &&
1016134a1f4eSCasper H.S. Dik 	    priv_policy_va(cr, PRIV_FILE_WRITE, B_FALSE, EACCES, NULL,
1017134a1f4eSCasper H.S. Dik 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE) != 0) {
1018134a1f4eSCasper H.S. Dik 		return (EACCES);
1019134a1f4eSCasper H.S. Dik 	}
1020134a1f4eSCasper H.S. Dik 
1021134a1f4eSCasper H.S. Dik 	mode = ~curmode & wantmode;
1022134a1f4eSCasper H.S. Dik 
1023134a1f4eSCasper H.S. Dik 	if (mode == 0)
1024134a1f4eSCasper H.S. Dik 		return (0);
1025134a1f4eSCasper H.S. Dik 
1026134a1f4eSCasper H.S. Dik 	if ((mode & VREAD) && priv_policy_va(cr, PRIV_FILE_DAC_READ, B_FALSE,
1027134a1f4eSCasper H.S. Dik 	    EACCES, NULL, KLPDARG_VNODE, vp, (char *)NULL,
1028134a1f4eSCasper H.S. Dik 	    KLPDARG_NOMORE) != 0) {
1029134a1f4eSCasper H.S. Dik 		return (EACCES);
1030134a1f4eSCasper H.S. Dik 	}
1031134a1f4eSCasper H.S. Dik 
1032134a1f4eSCasper H.S. Dik 	if (mode & VWRITE) {
1033134a1f4eSCasper H.S. Dik 		boolean_t allzone;
1034134a1f4eSCasper H.S. Dik 
1035134a1f4eSCasper H.S. Dik 		if (owner == 0 && cr->cr_uid != 0)
1036134a1f4eSCasper H.S. Dik 			allzone = B_TRUE;
1037134a1f4eSCasper H.S. Dik 		else
1038134a1f4eSCasper H.S. Dik 			allzone = B_FALSE;
1039134a1f4eSCasper H.S. Dik 		if (priv_policy_va(cr, PRIV_FILE_DAC_WRITE, allzone, EACCES,
1040134a1f4eSCasper H.S. Dik 		    NULL, KLPDARG_VNODE, vp, (char *)NULL,
1041134a1f4eSCasper H.S. Dik 		    KLPDARG_NOMORE) != 0) {
1042134a1f4eSCasper H.S. Dik 			return (EACCES);
1043134a1f4eSCasper H.S. Dik 		}
1044134a1f4eSCasper H.S. Dik 	}
1045134a1f4eSCasper H.S. Dik 
1046134a1f4eSCasper H.S. Dik 	if (mode & VEXEC) {
1047134a1f4eSCasper H.S. Dik 		/*
1048134a1f4eSCasper H.S. Dik 		 * Directories use file_dac_search to override the execute bit.
1049134a1f4eSCasper H.S. Dik 		 */
1050134a1f4eSCasper H.S. Dik 		int p = vp->v_type == VDIR ? PRIV_FILE_DAC_SEARCH :
1051134a1f4eSCasper H.S. Dik 		    PRIV_FILE_DAC_EXECUTE;
1052134a1f4eSCasper H.S. Dik 
1053134a1f4eSCasper H.S. Dik 		return (priv_policy_va(cr, p, B_FALSE, EACCES, NULL,
1054134a1f4eSCasper H.S. Dik 		    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
1055134a1f4eSCasper H.S. Dik 	}
1056134a1f4eSCasper H.S. Dik 	return (0);
1057134a1f4eSCasper H.S. Dik }
1058134a1f4eSCasper H.S. Dik 
1059134a1f4eSCasper H.S. Dik /*
1060134a1f4eSCasper H.S. Dik  * This is a special routine for ZFS; it is used to determine whether
1061134a1f4eSCasper H.S. Dik  * any of the privileges in effect allow any form of access to the
1062134a1f4eSCasper H.S. Dik  * file.  There's no reason to audit this or any reason to record
1063134a1f4eSCasper H.S. Dik  * this.  More work is needed to do the "KPLD" stuff.
1064134a1f4eSCasper H.S. Dik  */
1065134a1f4eSCasper H.S. Dik int
secpolicy_vnode_any_access(const cred_t * cr,vnode_t * vp,uid_t owner)1066134a1f4eSCasper H.S. Dik secpolicy_vnode_any_access(const cred_t *cr, vnode_t *vp, uid_t owner)
1067134a1f4eSCasper H.S. Dik {
1068134a1f4eSCasper H.S. Dik 	static int privs[] = {
1069134a1f4eSCasper H.S. Dik 	    PRIV_FILE_OWNER,
1070060fafacSCasper H.S. Dik 	    PRIV_FILE_CHOWN,
1071134a1f4eSCasper H.S. Dik 	    PRIV_FILE_DAC_READ,
1072134a1f4eSCasper H.S. Dik 	    PRIV_FILE_DAC_WRITE,
1073134a1f4eSCasper H.S. Dik 	    PRIV_FILE_DAC_EXECUTE,
1074134a1f4eSCasper H.S. Dik 	    PRIV_FILE_DAC_SEARCH,
1075134a1f4eSCasper H.S. Dik 	};
1076134a1f4eSCasper H.S. Dik 	int i;
1077134a1f4eSCasper H.S. Dik 
1078134a1f4eSCasper H.S. Dik 	/* Same as secpolicy_vnode_setdac */
1079134a1f4eSCasper H.S. Dik 	if (owner == cr->cr_uid)
1080134a1f4eSCasper H.S. Dik 		return (0);
1081134a1f4eSCasper H.S. Dik 
1082134a1f4eSCasper H.S. Dik 	for (i = 0; i < sizeof (privs)/sizeof (int); i++) {
1083134a1f4eSCasper H.S. Dik 		boolean_t allzone = B_FALSE;
1084134a1f4eSCasper H.S. Dik 		int priv;
1085134a1f4eSCasper H.S. Dik 
1086134a1f4eSCasper H.S. Dik 		switch (priv = privs[i]) {
1087134a1f4eSCasper H.S. Dik 		case PRIV_FILE_DAC_EXECUTE:
1088134a1f4eSCasper H.S. Dik 			if (vp->v_type == VDIR)
1089134a1f4eSCasper H.S. Dik 				continue;
1090134a1f4eSCasper H.S. Dik 			break;
1091134a1f4eSCasper H.S. Dik 		case PRIV_FILE_DAC_SEARCH:
1092134a1f4eSCasper H.S. Dik 			if (vp->v_type != VDIR)
1093134a1f4eSCasper H.S. Dik 				continue;
1094134a1f4eSCasper H.S. Dik 			break;
1095134a1f4eSCasper H.S. Dik 		case PRIV_FILE_DAC_WRITE:
1096134a1f4eSCasper H.S. Dik 		case PRIV_FILE_OWNER:
1097060fafacSCasper H.S. Dik 		case PRIV_FILE_CHOWN:
1098134a1f4eSCasper H.S. Dik 			/* We know here that if owner == 0, that cr_uid != 0 */
1099134a1f4eSCasper H.S. Dik 			allzone = owner == 0;
1100134a1f4eSCasper H.S. Dik 			break;
1101134a1f4eSCasper H.S. Dik 		}
1102134a1f4eSCasper H.S. Dik 		if (PRIV_POLICY_CHOICE(cr, priv, allzone))
1103134a1f4eSCasper H.S. Dik 			return (0);
1104134a1f4eSCasper H.S. Dik 	}
1105134a1f4eSCasper H.S. Dik 	return (EPERM);
1106134a1f4eSCasper H.S. Dik }
1107134a1f4eSCasper H.S. Dik 
1108134a1f4eSCasper H.S. Dik /*
11097c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_setid_modify()
11107c478bd9Sstevel@tonic-gate  *
11117c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can set the file setid flags.
11127c478bd9Sstevel@tonic-gate  *
11137c478bd9Sstevel@tonic-gate  * Output:	EPERM - if not privileged.
11147c478bd9Sstevel@tonic-gate  */
11157c478bd9Sstevel@tonic-gate 
11167c478bd9Sstevel@tonic-gate static int
secpolicy_vnode_setid_modify(const cred_t * cr,uid_t owner)11177c478bd9Sstevel@tonic-gate secpolicy_vnode_setid_modify(const cred_t *cr, uid_t owner)
11187c478bd9Sstevel@tonic-gate {
11197c478bd9Sstevel@tonic-gate 	/* If changing to suid root, must have all zone privs */
11207c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_TRUE;
11217c478bd9Sstevel@tonic-gate 
11227c478bd9Sstevel@tonic-gate 	if (owner != 0) {
11237c478bd9Sstevel@tonic-gate 		if (owner == cr->cr_uid)
11247c478bd9Sstevel@tonic-gate 			return (0);
11257c478bd9Sstevel@tonic-gate 		allzone = B_FALSE;
11267c478bd9Sstevel@tonic-gate 	}
11277c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_SETID, allzone, EPERM, NULL));
11287c478bd9Sstevel@tonic-gate }
11297c478bd9Sstevel@tonic-gate 
11307c478bd9Sstevel@tonic-gate /*
11317c478bd9Sstevel@tonic-gate  * Are we allowed to retain the set-uid/set-gid bits when
11327c478bd9Sstevel@tonic-gate  * changing ownership or when writing to a file?
11337c478bd9Sstevel@tonic-gate  * "issuid" should be true when set-uid; only in that case
11347c478bd9Sstevel@tonic-gate  * root ownership is checked (setgid is assumed).
11357c478bd9Sstevel@tonic-gate  */
11367c478bd9Sstevel@tonic-gate int
secpolicy_vnode_setid_retain(const cred_t * cred,boolean_t issuidroot)11377c478bd9Sstevel@tonic-gate secpolicy_vnode_setid_retain(const cred_t *cred, boolean_t issuidroot)
11387c478bd9Sstevel@tonic-gate {
11397c478bd9Sstevel@tonic-gate 	if (issuidroot && !HAS_ALLZONEPRIVS(cred))
11407c478bd9Sstevel@tonic-gate 		return (EPERM);
11417c478bd9Sstevel@tonic-gate 
11427c478bd9Sstevel@tonic-gate 	return (!PRIV_POLICY_CHOICE(cred, PRIV_FILE_SETID, B_FALSE));
11437c478bd9Sstevel@tonic-gate }
11447c478bd9Sstevel@tonic-gate 
11457c478bd9Sstevel@tonic-gate /*
11467c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_setids_setgids()
11477c478bd9Sstevel@tonic-gate  *
11487c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can set the file setgid flag.
11497c478bd9Sstevel@tonic-gate  *
11507c478bd9Sstevel@tonic-gate  * Output:	EPERM - if not privileged
11517c478bd9Sstevel@tonic-gate  */
11527c478bd9Sstevel@tonic-gate 
11537c478bd9Sstevel@tonic-gate int
secpolicy_vnode_setids_setgids(const cred_t * cred,gid_t gid)11547c478bd9Sstevel@tonic-gate secpolicy_vnode_setids_setgids(const cred_t *cred, gid_t gid)
11557c478bd9Sstevel@tonic-gate {
11567c478bd9Sstevel@tonic-gate 	if (!groupmember(gid, cred))
11577c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cred, PRIV_FILE_SETID, B_FALSE, EPERM,
11587c478bd9Sstevel@tonic-gate 		    NULL));
11597c478bd9Sstevel@tonic-gate 	return (0);
11607c478bd9Sstevel@tonic-gate }
11617c478bd9Sstevel@tonic-gate 
11627c478bd9Sstevel@tonic-gate /*
116347def0dcSMark Shellenbaum  * Name:	secpolicy_vnode_chown
116447def0dcSMark Shellenbaum  *
116547def0dcSMark Shellenbaum  * Normal:	Determine if subject can chown owner of a file.
116647def0dcSMark Shellenbaum  *
116747def0dcSMark Shellenbaum  * Output:	EPERM - if access denied
11687c478bd9Sstevel@tonic-gate  */
116947def0dcSMark Shellenbaum 
11707c478bd9Sstevel@tonic-gate int
secpolicy_vnode_chown(const cred_t * cred,uid_t owner)1171e02bc683SMark Shellenbaum secpolicy_vnode_chown(const cred_t *cred, uid_t owner)
11727c478bd9Sstevel@tonic-gate {
1173e02bc683SMark Shellenbaum 	boolean_t is_owner = (owner == crgetuid(cred));
1174e02bc683SMark Shellenbaum 	boolean_t allzone = B_FALSE;
1175e02bc683SMark Shellenbaum 	int priv;
1176e02bc683SMark Shellenbaum 
1177e02bc683SMark Shellenbaum 	if (!is_owner) {
1178e02bc683SMark Shellenbaum 		allzone = (owner == 0);
1179e02bc683SMark Shellenbaum 		priv = PRIV_FILE_CHOWN;
1180e02bc683SMark Shellenbaum 	} else {
1181e02bc683SMark Shellenbaum 		priv = HAS_PRIVILEGE(cred, PRIV_FILE_CHOWN) ?
1182e02bc683SMark Shellenbaum 		    PRIV_FILE_CHOWN : PRIV_FILE_CHOWN_SELF;
1183e02bc683SMark Shellenbaum 	}
1184e02bc683SMark Shellenbaum 
1185e02bc683SMark Shellenbaum 	return (PRIV_POLICY(cred, priv, allzone, EPERM, NULL));
118647def0dcSMark Shellenbaum }
118747def0dcSMark Shellenbaum 
118847def0dcSMark Shellenbaum /*
118947def0dcSMark Shellenbaum  * Name:	secpolicy_vnode_create_gid
119047def0dcSMark Shellenbaum  *
119147def0dcSMark Shellenbaum  * Normal:	Determine if subject can change group ownership of a file.
119247def0dcSMark Shellenbaum  *
119347def0dcSMark Shellenbaum  * Output:	EPERM - if access denied
119447def0dcSMark Shellenbaum  */
119547def0dcSMark Shellenbaum int
secpolicy_vnode_create_gid(const cred_t * cred)119647def0dcSMark Shellenbaum secpolicy_vnode_create_gid(const cred_t *cred)
119747def0dcSMark Shellenbaum {
1198e02bc683SMark Shellenbaum 	if (HAS_PRIVILEGE(cred, PRIV_FILE_CHOWN))
1199e02bc683SMark Shellenbaum 		return (PRIV_POLICY(cred, PRIV_FILE_CHOWN, B_FALSE, EPERM,
1200e02bc683SMark Shellenbaum 		    NULL));
1201e02bc683SMark Shellenbaum 	else
1202e02bc683SMark Shellenbaum 		return (PRIV_POLICY(cred, PRIV_FILE_CHOWN_SELF, B_FALSE, EPERM,
1203e02bc683SMark Shellenbaum 		    NULL));
12047c478bd9Sstevel@tonic-gate }
12057c478bd9Sstevel@tonic-gate 
12067c478bd9Sstevel@tonic-gate /*
12077c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_utime_modify()
12087c478bd9Sstevel@tonic-gate  *
12097c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can modify the utime on a file.
12107c478bd9Sstevel@tonic-gate  *
12117c478bd9Sstevel@tonic-gate  * Output:	EPERM - if access denied.
12127c478bd9Sstevel@tonic-gate  */
12137c478bd9Sstevel@tonic-gate 
12147c478bd9Sstevel@tonic-gate static int
secpolicy_vnode_utime_modify(const cred_t * cred)12157c478bd9Sstevel@tonic-gate secpolicy_vnode_utime_modify(const cred_t *cred)
12167c478bd9Sstevel@tonic-gate {
12177c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_FILE_OWNER, B_FALSE, EPERM,
12187c478bd9Sstevel@tonic-gate 	    "modify file times"));
12197c478bd9Sstevel@tonic-gate }
12207c478bd9Sstevel@tonic-gate 
12217c478bd9Sstevel@tonic-gate 
12227c478bd9Sstevel@tonic-gate /*
12237c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_setdac()
12247c478bd9Sstevel@tonic-gate  *
12257c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can modify the mode of a file.
12267c478bd9Sstevel@tonic-gate  *		allzone privilege needed when modifying root owned object.
12277c478bd9Sstevel@tonic-gate  *
12287c478bd9Sstevel@tonic-gate  * Output:	EPERM - if access denied.
12297c478bd9Sstevel@tonic-gate  */
12307c478bd9Sstevel@tonic-gate 
12317c478bd9Sstevel@tonic-gate int
secpolicy_vnode_setdac(const cred_t * cred,uid_t owner)12327c478bd9Sstevel@tonic-gate secpolicy_vnode_setdac(const cred_t *cred, uid_t owner)
12337c478bd9Sstevel@tonic-gate {
1234*70561057SGordon Ross 	boolean_t allzone = (owner == 0);
1235*70561057SGordon Ross 
12367c478bd9Sstevel@tonic-gate 	if (owner == cred->cr_uid)
12377c478bd9Sstevel@tonic-gate 		return (0);
12387c478bd9Sstevel@tonic-gate 
1239*70561057SGordon Ross 	return (PRIV_POLICY(cred, PRIV_FILE_OWNER, allzone, EPERM, NULL));
12407c478bd9Sstevel@tonic-gate }
1241*70561057SGordon Ross 
1242*70561057SGordon Ross /*
1243*70561057SGordon Ross  * Name:	secpolicy_vnode_setdac3()
1244*70561057SGordon Ross  *
1245*70561057SGordon Ross  * Normal:	Variant of secpolicy_vnode_setdac() that conditionally
1246*70561057SGordon Ross  *		grants implicit rights to the owner of a file.
1247*70561057SGordon Ross  *		allzone privilege needed when modifying root owned object.
1248*70561057SGordon Ross  *
1249*70561057SGordon Ross  * Output:	EPERM - if access denied.
1250*70561057SGordon Ross  */
1251*70561057SGordon Ross 
1252*70561057SGordon Ross int
secpolicy_vnode_setdac3(const cred_t * cred,uid_t owner,boolean_t owner_implicit_rights)1253*70561057SGordon Ross secpolicy_vnode_setdac3(const cred_t *cred, uid_t owner,
1254*70561057SGordon Ross     boolean_t owner_implicit_rights)
1255*70561057SGordon Ross {
1256*70561057SGordon Ross 	boolean_t allzone = (owner == 0);
1257*70561057SGordon Ross 
1258*70561057SGordon Ross 	if (owner_implicit_rights && owner == cred->cr_uid)
1259*70561057SGordon Ross 		return (0);
1260*70561057SGordon Ross 
1261*70561057SGordon Ross 	return (PRIV_POLICY(cred, PRIV_FILE_OWNER, allzone, EPERM, NULL));
1262*70561057SGordon Ross }
1263*70561057SGordon Ross 
12647c478bd9Sstevel@tonic-gate /*
12657c478bd9Sstevel@tonic-gate  * Name:	secpolicy_vnode_stky_modify()
12667c478bd9Sstevel@tonic-gate  *
12677c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can make a file a "sticky".
12687c478bd9Sstevel@tonic-gate  *
12697c478bd9Sstevel@tonic-gate  * Output:	EPERM - if access denied.
12707c478bd9Sstevel@tonic-gate  */
12717c478bd9Sstevel@tonic-gate 
12727c478bd9Sstevel@tonic-gate int
secpolicy_vnode_stky_modify(const cred_t * cred)12737c478bd9Sstevel@tonic-gate secpolicy_vnode_stky_modify(const cred_t *cred)
12747c478bd9Sstevel@tonic-gate {
12757c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_SYS_CONFIG, B_FALSE, EPERM,
12767c478bd9Sstevel@tonic-gate 	    "set file sticky"));
12777c478bd9Sstevel@tonic-gate }
12787c478bd9Sstevel@tonic-gate 
12797c478bd9Sstevel@tonic-gate /*
12807c478bd9Sstevel@tonic-gate  * Policy determines whether we can remove an entry from a directory,
12817c478bd9Sstevel@tonic-gate  * regardless of permission bits.
12827c478bd9Sstevel@tonic-gate  */
12837c478bd9Sstevel@tonic-gate int
secpolicy_vnode_remove(const cred_t * cr)12847c478bd9Sstevel@tonic-gate secpolicy_vnode_remove(const cred_t *cr)
12857c478bd9Sstevel@tonic-gate {
12867c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_OWNER, B_FALSE, EACCES,
12877c478bd9Sstevel@tonic-gate 	    "sticky directory"));
12887c478bd9Sstevel@tonic-gate }
12897c478bd9Sstevel@tonic-gate 
12907c478bd9Sstevel@tonic-gate int
secpolicy_vnode_owner(const cred_t * cr,uid_t owner)12917c478bd9Sstevel@tonic-gate secpolicy_vnode_owner(const cred_t *cr, uid_t owner)
12927c478bd9Sstevel@tonic-gate {
12937c478bd9Sstevel@tonic-gate 	boolean_t allzone = (owner == 0);
12947c478bd9Sstevel@tonic-gate 
12957c478bd9Sstevel@tonic-gate 	if (owner == cr->cr_uid)
12967c478bd9Sstevel@tonic-gate 		return (0);
12977c478bd9Sstevel@tonic-gate 
12987c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_OWNER, allzone, EPERM, NULL));
12997c478bd9Sstevel@tonic-gate }
13007c478bd9Sstevel@tonic-gate 
130113f9f30eSmarks void
secpolicy_setid_clear(vattr_t * vap,cred_t * cr)130213f9f30eSmarks secpolicy_setid_clear(vattr_t *vap, cred_t *cr)
130313f9f30eSmarks {
130413f9f30eSmarks 	if ((vap->va_mode & (S_ISUID | S_ISGID)) != 0 &&
130513f9f30eSmarks 	    secpolicy_vnode_setid_retain(cr,
130613f9f30eSmarks 	    (vap->va_mode & S_ISUID) != 0 &&
130713f9f30eSmarks 	    (vap->va_mask & AT_UID) != 0 && vap->va_uid == 0) != 0) {
130813f9f30eSmarks 		vap->va_mask |= AT_MODE;
130913f9f30eSmarks 		vap->va_mode &= ~(S_ISUID|S_ISGID);
131013f9f30eSmarks 	}
131113f9f30eSmarks }
131213f9f30eSmarks 
1313f92daba9Smarks int
secpolicy_setid_setsticky_clear(vnode_t * vp,vattr_t * vap,const vattr_t * ovap,cred_t * cr)1314f92daba9Smarks secpolicy_setid_setsticky_clear(vnode_t *vp, vattr_t *vap, const vattr_t *ovap,
1315f92daba9Smarks     cred_t *cr)
1316f92daba9Smarks {
1317f92daba9Smarks 	int error;
1318f92daba9Smarks 
1319f92daba9Smarks 	if ((vap->va_mode & S_ISUID) != 0 &&
1320f92daba9Smarks 	    (error = secpolicy_vnode_setid_modify(cr,
1321f92daba9Smarks 	    ovap->va_uid)) != 0) {
1322f92daba9Smarks 		return (error);
1323f92daba9Smarks 	}
1324f92daba9Smarks 
1325f92daba9Smarks 	/*
1326f92daba9Smarks 	 * Check privilege if attempting to set the
1327f92daba9Smarks 	 * sticky bit on a non-directory.
1328f92daba9Smarks 	 */
1329f92daba9Smarks 	if (vp->v_type != VDIR && (vap->va_mode & S_ISVTX) != 0 &&
1330f92daba9Smarks 	    secpolicy_vnode_stky_modify(cr) != 0) {
1331f92daba9Smarks 		vap->va_mode &= ~S_ISVTX;
1332f92daba9Smarks 	}
1333f92daba9Smarks 
1334f92daba9Smarks 	/*
1335f92daba9Smarks 	 * Check for privilege if attempting to set the
1336f92daba9Smarks 	 * group-id bit.
1337f92daba9Smarks 	 */
1338f92daba9Smarks 	if ((vap->va_mode & S_ISGID) != 0 &&
1339f92daba9Smarks 	    secpolicy_vnode_setids_setgids(cr, ovap->va_gid) != 0) {
1340f92daba9Smarks 		vap->va_mode &= ~S_ISGID;
1341f92daba9Smarks 	}
1342f92daba9Smarks 
1343f92daba9Smarks 	return (0);
1344f92daba9Smarks }
1345f92daba9Smarks 
1346da6c28aaSamw #define	ATTR_FLAG_PRIV(attr, value, cr)	\
1347da6c28aaSamw 	PRIV_POLICY(cr, value ? PRIV_FILE_FLAG_SET : PRIV_ALL, \
1348da6c28aaSamw 	B_FALSE, EPERM, NULL)
1349da6c28aaSamw 
1350da6c28aaSamw /*
1351da6c28aaSamw  * Check privileges for setting xvattr attributes
1352da6c28aaSamw  */
1353da6c28aaSamw int
secpolicy_xvattr(xvattr_t * xvap,uid_t owner,cred_t * cr,vtype_t vtype)1354da6c28aaSamw secpolicy_xvattr(xvattr_t *xvap, uid_t owner, cred_t *cr, vtype_t vtype)
1355da6c28aaSamw {
1356da6c28aaSamw 	xoptattr_t *xoap;
1357da6c28aaSamw 	int error = 0;
1358da6c28aaSamw 
1359da6c28aaSamw 	if ((xoap = xva_getxoptattr(xvap)) == NULL)
1360da6c28aaSamw 		return (EINVAL);
1361da6c28aaSamw 
1362da6c28aaSamw 	/*
1363da6c28aaSamw 	 * First process the DOS bits
1364da6c28aaSamw 	 */
1365da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_ARCHIVE) ||
1366da6c28aaSamw 	    XVA_ISSET_REQ(xvap, XAT_HIDDEN) ||
1367da6c28aaSamw 	    XVA_ISSET_REQ(xvap, XAT_READONLY) ||
1368da6c28aaSamw 	    XVA_ISSET_REQ(xvap, XAT_SYSTEM) ||
1369fd9ee8b5Sjoyce mcintosh 	    XVA_ISSET_REQ(xvap, XAT_CREATETIME) ||
1370fd9ee8b5Sjoyce mcintosh 	    XVA_ISSET_REQ(xvap, XAT_OFFLINE) ||
1371fd9ee8b5Sjoyce mcintosh 	    XVA_ISSET_REQ(xvap, XAT_SPARSE)) {
1372da6c28aaSamw 		if ((error = secpolicy_vnode_owner(cr, owner)) != 0)
1373da6c28aaSamw 			return (error);
1374da6c28aaSamw 	}
1375da6c28aaSamw 
1376da6c28aaSamw 	/*
1377da6c28aaSamw 	 * Now handle special attributes
1378da6c28aaSamw 	 */
1379da6c28aaSamw 
1380da6c28aaSamw 	if (XVA_ISSET_REQ(xvap, XAT_IMMUTABLE))
1381da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_IMMUTABLE,
1382da6c28aaSamw 		    xoap->xoa_immutable, cr);
1383da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_NOUNLINK))
1384da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_NOUNLINK,
1385da6c28aaSamw 		    xoap->xoa_nounlink, cr);
1386da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_APPENDONLY))
1387da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_APPENDONLY,
1388da6c28aaSamw 		    xoap->xoa_appendonly, cr);
1389da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_NODUMP))
1390da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_NODUMP,
1391da6c28aaSamw 		    xoap->xoa_nodump, cr);
1392da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_OPAQUE))
1393da6c28aaSamw 		error = EPERM;
1394da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_QUARANTINED)) {
1395da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_AV_QUARANTINED,
1396da6c28aaSamw 		    xoap->xoa_av_quarantined, cr);
1397e8f97327Smarks 		if (error == 0 && vtype != VREG && xoap->xoa_av_quarantined)
1398da6c28aaSamw 			error = EINVAL;
1399da6c28aaSamw 	}
1400da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_MODIFIED))
1401da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_AV_MODIFIED,
1402da6c28aaSamw 		    xoap->xoa_av_modified, cr);
1403da6c28aaSamw 	if (error == 0 && XVA_ISSET_REQ(xvap, XAT_AV_SCANSTAMP)) {
1404da6c28aaSamw 		error = ATTR_FLAG_PRIV(XAT_AV_SCANSTAMP,
1405da6c28aaSamw 		    xoap->xoa_av_scanstamp, cr);
1406da6c28aaSamw 		if (error == 0 && vtype != VREG)
1407da6c28aaSamw 			error = EINVAL;
1408da6c28aaSamw 	}
1409da6c28aaSamw 	return (error);
1410da6c28aaSamw }
1411da6c28aaSamw 
14127c478bd9Sstevel@tonic-gate /*
14137c478bd9Sstevel@tonic-gate  * This function checks the policy decisions surrounding the
14147c478bd9Sstevel@tonic-gate  * vop setattr call.
14157c478bd9Sstevel@tonic-gate  *
14167c478bd9Sstevel@tonic-gate  * It should be called after sufficient locks have been established
14177c478bd9Sstevel@tonic-gate  * on the underlying data structures.  No concurrent modifications
14187c478bd9Sstevel@tonic-gate  * should be allowed.
14197c478bd9Sstevel@tonic-gate  *
14207c478bd9Sstevel@tonic-gate  * The caller must pass in unlocked version of its vaccess function
14217c478bd9Sstevel@tonic-gate  * this is required because vop_access function should lock the
14227c478bd9Sstevel@tonic-gate  * node for reading.  A three argument function should be defined
14237c478bd9Sstevel@tonic-gate  * which accepts the following argument:
14247c478bd9Sstevel@tonic-gate  *	A pointer to the internal "node" type (inode *)
14257c478bd9Sstevel@tonic-gate  *	vnode access bits (VREAD|VWRITE|VEXEC)
14267c478bd9Sstevel@tonic-gate  *	a pointer to the credential
14277c478bd9Sstevel@tonic-gate  *
14287c478bd9Sstevel@tonic-gate  * This function makes the following policy decisions:
14297c478bd9Sstevel@tonic-gate  *
14307c478bd9Sstevel@tonic-gate  *		- change permissions
14317c478bd9Sstevel@tonic-gate  *			- permission to change file mode if not owner
14327c478bd9Sstevel@tonic-gate  *			- permission to add sticky bit to non-directory
14337c478bd9Sstevel@tonic-gate  *			- permission to add set-gid bit
14347c478bd9Sstevel@tonic-gate  *
14357c478bd9Sstevel@tonic-gate  * The ovap argument should include AT_MODE|AT_UID|AT_GID.
14367c478bd9Sstevel@tonic-gate  *
14377c478bd9Sstevel@tonic-gate  * If the vap argument does not include AT_MODE, the mode will be copied from
14387c478bd9Sstevel@tonic-gate  * ovap.  In certain situations set-uid/set-gid bits need to be removed;
14397c478bd9Sstevel@tonic-gate  * this is done by marking vap->va_mask to include AT_MODE and va_mode
14407c478bd9Sstevel@tonic-gate  * is updated to the newly computed mode.
14417c478bd9Sstevel@tonic-gate  */
14427c478bd9Sstevel@tonic-gate 
14437c478bd9Sstevel@tonic-gate int
secpolicy_vnode_setattr(cred_t * cr,struct vnode * vp,struct vattr * vap,const struct vattr * ovap,int flags,int unlocked_access (void *,int,cred_t *),void * node)14447c478bd9Sstevel@tonic-gate secpolicy_vnode_setattr(cred_t *cr, struct vnode *vp, struct vattr *vap,
14457c478bd9Sstevel@tonic-gate     const struct vattr *ovap, int flags,
14467c478bd9Sstevel@tonic-gate     int unlocked_access(void *, int, cred_t *),
14477c478bd9Sstevel@tonic-gate     void *node)
14487c478bd9Sstevel@tonic-gate {
14497c478bd9Sstevel@tonic-gate 	int mask = vap->va_mask;
14507c478bd9Sstevel@tonic-gate 	int error = 0;
1451da6c28aaSamw 	boolean_t skipaclchk = (flags & ATTR_NOACLCHECK) ? B_TRUE : B_FALSE;
1452*70561057SGordon Ross 	boolean_t implicit = (flags & ATTR_NOIMPLICIT) ? B_FALSE : B_TRUE;
14537c478bd9Sstevel@tonic-gate 
14547c478bd9Sstevel@tonic-gate 	if (mask & AT_SIZE) {
14557c478bd9Sstevel@tonic-gate 		if (vp->v_type == VDIR) {
14567c478bd9Sstevel@tonic-gate 			error = EISDIR;
14577c478bd9Sstevel@tonic-gate 			goto out;
14587c478bd9Sstevel@tonic-gate 		}
1459da6c28aaSamw 
1460da6c28aaSamw 		/*
1461da6c28aaSamw 		 * If ATTR_NOACLCHECK is set in the flags, then we don't
1462da6c28aaSamw 		 * perform the secondary unlocked_access() call since the
1463da6c28aaSamw 		 * ACL (if any) is being checked there.
1464da6c28aaSamw 		 */
1465da6c28aaSamw 		if (skipaclchk == B_FALSE) {
14667c478bd9Sstevel@tonic-gate 			error = unlocked_access(node, VWRITE, cr);
14677c478bd9Sstevel@tonic-gate 			if (error)
14687c478bd9Sstevel@tonic-gate 				goto out;
14697c478bd9Sstevel@tonic-gate 		}
1470da6c28aaSamw 	}
14717c478bd9Sstevel@tonic-gate 	if (mask & AT_MODE) {
14727c478bd9Sstevel@tonic-gate 		/*
14737c478bd9Sstevel@tonic-gate 		 * If not the owner of the file then check privilege
14747c478bd9Sstevel@tonic-gate 		 * for two things: the privilege to set the mode at all
14757c478bd9Sstevel@tonic-gate 		 * and, if we're setting setuid, we also need permissions
14767c478bd9Sstevel@tonic-gate 		 * to add the set-uid bit, if we're not the owner.
14777c478bd9Sstevel@tonic-gate 		 * In the specific case of creating a set-uid root
14787c478bd9Sstevel@tonic-gate 		 * file, we need even more permissions.
14797c478bd9Sstevel@tonic-gate 		 */
1480*70561057SGordon Ross 		error = secpolicy_vnode_setdac3(cr, ovap->va_uid, implicit);
1481*70561057SGordon Ross 		if (error != 0)
14827c478bd9Sstevel@tonic-gate 			goto out;
14837c478bd9Sstevel@tonic-gate 
1484f92daba9Smarks 		if ((error = secpolicy_setid_setsticky_clear(vp, vap,
1485f92daba9Smarks 		    ovap, cr)) != 0)
14867c478bd9Sstevel@tonic-gate 			goto out;
14877c478bd9Sstevel@tonic-gate 	} else
14887c478bd9Sstevel@tonic-gate 		vap->va_mode = ovap->va_mode;
14897c478bd9Sstevel@tonic-gate 
14907c478bd9Sstevel@tonic-gate 	if (mask & (AT_UID|AT_GID)) {
14917c478bd9Sstevel@tonic-gate 		boolean_t checkpriv = B_FALSE;
14927c478bd9Sstevel@tonic-gate 
14937c478bd9Sstevel@tonic-gate 		/*
14947c478bd9Sstevel@tonic-gate 		 * Chowning files.
14957c478bd9Sstevel@tonic-gate 		 *
14967c478bd9Sstevel@tonic-gate 		 * If you are the file owner:
14977c478bd9Sstevel@tonic-gate 		 *	chown to other uid		FILE_CHOWN_SELF
14987c478bd9Sstevel@tonic-gate 		 *	chown to gid (non-member)	FILE_CHOWN_SELF
14997c478bd9Sstevel@tonic-gate 		 *	chown to gid (member)		<none>
15007c478bd9Sstevel@tonic-gate 		 *
15017c478bd9Sstevel@tonic-gate 		 * Instead of PRIV_FILE_CHOWN_SELF, FILE_CHOWN is also
15027c478bd9Sstevel@tonic-gate 		 * acceptable but the first one is reported when debugging.
15037c478bd9Sstevel@tonic-gate 		 *
15047c478bd9Sstevel@tonic-gate 		 * If you are not the file owner:
15057c478bd9Sstevel@tonic-gate 		 *	chown from root			PRIV_FILE_CHOWN + zone
15067c478bd9Sstevel@tonic-gate 		 *	chown from other to any		PRIV_FILE_CHOWN
15077c478bd9Sstevel@tonic-gate 		 *
15087c478bd9Sstevel@tonic-gate 		 */
1509*70561057SGordon Ross 		if (!implicit) {
1510*70561057SGordon Ross 			checkpriv = B_TRUE;
1511*70561057SGordon Ross 		} else if (cr->cr_uid != ovap->va_uid) {
15127c478bd9Sstevel@tonic-gate 			checkpriv = B_TRUE;
15137c478bd9Sstevel@tonic-gate 		} else {
15147c478bd9Sstevel@tonic-gate 			if (((mask & AT_UID) && vap->va_uid != ovap->va_uid) ||
15157c478bd9Sstevel@tonic-gate 			    ((mask & AT_GID) && vap->va_gid != ovap->va_gid &&
15167c478bd9Sstevel@tonic-gate 			    !groupmember(vap->va_gid, cr))) {
15177c478bd9Sstevel@tonic-gate 				checkpriv = B_TRUE;
15187c478bd9Sstevel@tonic-gate 			}
15197c478bd9Sstevel@tonic-gate 		}
15207c478bd9Sstevel@tonic-gate 		/*
15217c478bd9Sstevel@tonic-gate 		 * If necessary, check privilege to see if update can be done.
15227c478bd9Sstevel@tonic-gate 		 */
15237c478bd9Sstevel@tonic-gate 		if (checkpriv &&
1524e02bc683SMark Shellenbaum 		    (error = secpolicy_vnode_chown(cr, ovap->va_uid)) != 0) {
15257c478bd9Sstevel@tonic-gate 			goto out;
15267c478bd9Sstevel@tonic-gate 		}
15277c478bd9Sstevel@tonic-gate 
15287c478bd9Sstevel@tonic-gate 		/*
15297c478bd9Sstevel@tonic-gate 		 * If the file has either the set UID or set GID bits
15307c478bd9Sstevel@tonic-gate 		 * set and the caller can set the bits, then leave them.
15317c478bd9Sstevel@tonic-gate 		 */
153213f9f30eSmarks 		secpolicy_setid_clear(vap, cr);
15337c478bd9Sstevel@tonic-gate 	}
15347c478bd9Sstevel@tonic-gate 	if (mask & (AT_ATIME|AT_MTIME)) {
15357c478bd9Sstevel@tonic-gate 		/*
15367c478bd9Sstevel@tonic-gate 		 * If not the file owner and not otherwise privileged,
15377c478bd9Sstevel@tonic-gate 		 * always return an error when setting the
15387c478bd9Sstevel@tonic-gate 		 * time other than the current (ATTR_UTIME flag set).
15397c478bd9Sstevel@tonic-gate 		 * If setting the current time (ATTR_UTIME not set) then
15407c478bd9Sstevel@tonic-gate 		 * unlocked_access will check permissions according to policy.
15417c478bd9Sstevel@tonic-gate 		 */
15427c478bd9Sstevel@tonic-gate 		if (cr->cr_uid != ovap->va_uid) {
15437c478bd9Sstevel@tonic-gate 			if (flags & ATTR_UTIME)
15447c478bd9Sstevel@tonic-gate 				error = secpolicy_vnode_utime_modify(cr);
1545da6c28aaSamw 			else if (skipaclchk == B_FALSE) {
15467c478bd9Sstevel@tonic-gate 				error = unlocked_access(node, VWRITE, cr);
15477c478bd9Sstevel@tonic-gate 				if (error == EACCES &&
15487c478bd9Sstevel@tonic-gate 				    secpolicy_vnode_utime_modify(cr) == 0)
15497c478bd9Sstevel@tonic-gate 					error = 0;
15507c478bd9Sstevel@tonic-gate 			}
15517c478bd9Sstevel@tonic-gate 			if (error)
15527c478bd9Sstevel@tonic-gate 				goto out;
15537c478bd9Sstevel@tonic-gate 		}
15547c478bd9Sstevel@tonic-gate 	}
1555da6c28aaSamw 
1556da6c28aaSamw 	/*
1557da6c28aaSamw 	 * Check for optional attributes here by checking the following:
1558da6c28aaSamw 	 */
1559da6c28aaSamw 	if (mask & AT_XVATTR)
1560da6c28aaSamw 		error = secpolicy_xvattr((xvattr_t *)vap, ovap->va_uid, cr,
1561da6c28aaSamw 		    vp->v_type);
15627c478bd9Sstevel@tonic-gate out:
15637c478bd9Sstevel@tonic-gate 	return (error);
15647c478bd9Sstevel@tonic-gate }
15657c478bd9Sstevel@tonic-gate 
15667c478bd9Sstevel@tonic-gate /*
15677c478bd9Sstevel@tonic-gate  * Name:	secpolicy_pcfs_modify_bootpartition()
15687c478bd9Sstevel@tonic-gate  *
15697c478bd9Sstevel@tonic-gate  * Normal:	verify that subject can modify a pcfs boot partition.
15707c478bd9Sstevel@tonic-gate  *
15717c478bd9Sstevel@tonic-gate  * Output:	EACCES - if privilege check failed.
15727c478bd9Sstevel@tonic-gate  */
15737c478bd9Sstevel@tonic-gate /*ARGSUSED*/
15747c478bd9Sstevel@tonic-gate int
secpolicy_pcfs_modify_bootpartition(const cred_t * cred)15757c478bd9Sstevel@tonic-gate secpolicy_pcfs_modify_bootpartition(const cred_t *cred)
15767c478bd9Sstevel@tonic-gate {
15777c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cred, PRIV_ALL, B_FALSE, EACCES,
15787c478bd9Sstevel@tonic-gate 	    "modify pcfs boot partition"));
15797c478bd9Sstevel@tonic-gate }
15807c478bd9Sstevel@tonic-gate 
15817c478bd9Sstevel@tonic-gate /*
15827c478bd9Sstevel@tonic-gate  * System V IPC routines
15837c478bd9Sstevel@tonic-gate  */
15847c478bd9Sstevel@tonic-gate int
secpolicy_ipc_owner(const cred_t * cr,const struct kipc_perm * ip)15857c478bd9Sstevel@tonic-gate secpolicy_ipc_owner(const cred_t *cr, const struct kipc_perm *ip)
15867c478bd9Sstevel@tonic-gate {
15877c478bd9Sstevel@tonic-gate 	if (crgetzoneid(cr) != ip->ipc_zoneid ||
15887c478bd9Sstevel@tonic-gate 	    (cr->cr_uid != ip->ipc_uid && cr->cr_uid != ip->ipc_cuid)) {
15897c478bd9Sstevel@tonic-gate 		boolean_t allzone = B_FALSE;
15907c478bd9Sstevel@tonic-gate 		if (ip->ipc_uid == 0 || ip->ipc_cuid == 0)
15917c478bd9Sstevel@tonic-gate 			allzone = B_TRUE;
15927c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_OWNER, allzone, EPERM, NULL));
15937c478bd9Sstevel@tonic-gate 	}
15947c478bd9Sstevel@tonic-gate 	return (0);
15957c478bd9Sstevel@tonic-gate }
15967c478bd9Sstevel@tonic-gate 
15977c478bd9Sstevel@tonic-gate int
secpolicy_ipc_config(const cred_t * cr)15987c478bd9Sstevel@tonic-gate secpolicy_ipc_config(const cred_t *cr)
15997c478bd9Sstevel@tonic-gate {
16007c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_IPC_CONFIG, B_FALSE, EPERM, NULL));
16017c478bd9Sstevel@tonic-gate }
16027c478bd9Sstevel@tonic-gate 
16037c478bd9Sstevel@tonic-gate int
secpolicy_ipc_access(const cred_t * cr,const struct kipc_perm * ip,mode_t mode)16047c478bd9Sstevel@tonic-gate secpolicy_ipc_access(const cred_t *cr, const struct kipc_perm *ip, mode_t mode)
16057c478bd9Sstevel@tonic-gate {
16067c478bd9Sstevel@tonic-gate 
16077c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
16087c478bd9Sstevel@tonic-gate 
16097c478bd9Sstevel@tonic-gate 	ASSERT((mode & (MSG_R|MSG_W)) != 0);
16107c478bd9Sstevel@tonic-gate 
16117c478bd9Sstevel@tonic-gate 	if ((mode & MSG_R) &&
16127c478bd9Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_IPC_DAC_READ, allzone, EACCES, NULL) != 0)
16137c478bd9Sstevel@tonic-gate 		return (EACCES);
16147c478bd9Sstevel@tonic-gate 
16157c478bd9Sstevel@tonic-gate 	if (mode & MSG_W) {
16167c478bd9Sstevel@tonic-gate 		if (cr->cr_uid != 0 && (ip->ipc_uid == 0 || ip->ipc_cuid == 0))
16177c478bd9Sstevel@tonic-gate 			allzone = B_TRUE;
16187c478bd9Sstevel@tonic-gate 
16197c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_DAC_WRITE, allzone, EACCES,
16207c478bd9Sstevel@tonic-gate 		    NULL));
16217c478bd9Sstevel@tonic-gate 	}
16227c478bd9Sstevel@tonic-gate 	return (0);
16237c478bd9Sstevel@tonic-gate }
16247c478bd9Sstevel@tonic-gate 
16257c478bd9Sstevel@tonic-gate int
secpolicy_rsm_access(const cred_t * cr,uid_t owner,mode_t mode)16267c478bd9Sstevel@tonic-gate secpolicy_rsm_access(const cred_t *cr, uid_t owner, mode_t mode)
16277c478bd9Sstevel@tonic-gate {
16287c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
16297c478bd9Sstevel@tonic-gate 
16307c478bd9Sstevel@tonic-gate 	ASSERT((mode & (MSG_R|MSG_W)) != 0);
16317c478bd9Sstevel@tonic-gate 
16327c478bd9Sstevel@tonic-gate 	if ((mode & MSG_R) &&
16337c478bd9Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_IPC_DAC_READ, allzone, EACCES, NULL) != 0)
16347c478bd9Sstevel@tonic-gate 		return (EACCES);
16357c478bd9Sstevel@tonic-gate 
16367c478bd9Sstevel@tonic-gate 	if (mode & MSG_W) {
16377c478bd9Sstevel@tonic-gate 		if (cr->cr_uid != 0 && owner == 0)
16387c478bd9Sstevel@tonic-gate 			allzone = B_TRUE;
16397c478bd9Sstevel@tonic-gate 
16407c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_IPC_DAC_WRITE, allzone, EACCES,
16417c478bd9Sstevel@tonic-gate 		    NULL));
16427c478bd9Sstevel@tonic-gate 	}
16437c478bd9Sstevel@tonic-gate 	return (0);
16447c478bd9Sstevel@tonic-gate }
16457c478bd9Sstevel@tonic-gate 
16467c478bd9Sstevel@tonic-gate /*
16477c478bd9Sstevel@tonic-gate  * Audit configuration.
16487c478bd9Sstevel@tonic-gate  */
16497c478bd9Sstevel@tonic-gate int
secpolicy_audit_config(const cred_t * cr)16507c478bd9Sstevel@tonic-gate secpolicy_audit_config(const cred_t *cr)
16517c478bd9Sstevel@tonic-gate {
16527c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_AUDIT, B_FALSE, EPERM, NULL));
16537c478bd9Sstevel@tonic-gate }
16547c478bd9Sstevel@tonic-gate 
16557c478bd9Sstevel@tonic-gate /*
16567c478bd9Sstevel@tonic-gate  * Audit record generation.
16577c478bd9Sstevel@tonic-gate  */
16587c478bd9Sstevel@tonic-gate int
secpolicy_audit_modify(const cred_t * cr)16597c478bd9Sstevel@tonic-gate secpolicy_audit_modify(const cred_t *cr)
16607c478bd9Sstevel@tonic-gate {
16617c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_AUDIT, B_FALSE, EPERM, NULL));
16627c478bd9Sstevel@tonic-gate }
16637c478bd9Sstevel@tonic-gate 
16647c478bd9Sstevel@tonic-gate /*
16657c478bd9Sstevel@tonic-gate  * Get audit attributes.
16667c478bd9Sstevel@tonic-gate  * Either PRIV_SYS_AUDIT or PRIV_PROC_AUDIT required; report the
16677c478bd9Sstevel@tonic-gate  * "Least" of the two privileges on error.
16687c478bd9Sstevel@tonic-gate  */
16697c478bd9Sstevel@tonic-gate int
secpolicy_audit_getattr(const cred_t * cr,boolean_t checkonly)1670134a1f4eSCasper H.S. Dik secpolicy_audit_getattr(const cred_t *cr, boolean_t checkonly)
16717c478bd9Sstevel@tonic-gate {
1672134a1f4eSCasper H.S. Dik 	int priv;
1673134a1f4eSCasper H.S. Dik 
1674134a1f4eSCasper H.S. Dik 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_AUDIT, B_FALSE))
1675134a1f4eSCasper H.S. Dik 		priv = PRIV_SYS_AUDIT;
1676134a1f4eSCasper H.S. Dik 	else
1677134a1f4eSCasper H.S. Dik 		priv = PRIV_PROC_AUDIT;
1678134a1f4eSCasper H.S. Dik 
1679134a1f4eSCasper H.S. Dik 	if (checkonly)
1680134a1f4eSCasper H.S. Dik 		return (!PRIV_POLICY_ONLY(cr, priv, B_FALSE));
1681134a1f4eSCasper H.S. Dik 	else
1682134a1f4eSCasper H.S. Dik 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
16837c478bd9Sstevel@tonic-gate }
16847c478bd9Sstevel@tonic-gate 
16857c478bd9Sstevel@tonic-gate 
16867c478bd9Sstevel@tonic-gate /*
16877c478bd9Sstevel@tonic-gate  * Locking physical memory
16887c478bd9Sstevel@tonic-gate  */
16897c478bd9Sstevel@tonic-gate int
secpolicy_lock_memory(const cred_t * cr)16907c478bd9Sstevel@tonic-gate secpolicy_lock_memory(const cred_t *cr)
16917c478bd9Sstevel@tonic-gate {
16927c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_LOCK_MEMORY, B_FALSE, EPERM, NULL));
16937c478bd9Sstevel@tonic-gate }
16947c478bd9Sstevel@tonic-gate 
16957c478bd9Sstevel@tonic-gate /*
16967c478bd9Sstevel@tonic-gate  * Accounting (both acct(2) and exacct).
16977c478bd9Sstevel@tonic-gate  */
16987c478bd9Sstevel@tonic-gate int
secpolicy_acct(const cred_t * cr)16997c478bd9Sstevel@tonic-gate secpolicy_acct(const cred_t *cr)
17007c478bd9Sstevel@tonic-gate {
17017c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ACCT, B_FALSE, EPERM, NULL));
17027c478bd9Sstevel@tonic-gate }
17037c478bd9Sstevel@tonic-gate 
17047c478bd9Sstevel@tonic-gate /*
17057c478bd9Sstevel@tonic-gate  * Is this process privileged to change its uids at will?
17067c478bd9Sstevel@tonic-gate  * Uid 0 is still considered "special" and having the SETID
17077c478bd9Sstevel@tonic-gate  * privilege is not sufficient to get uid 0.
17087c478bd9Sstevel@tonic-gate  * Files are owned by root, so the privilege would give
17097c478bd9Sstevel@tonic-gate  * full access and euid 0 is still effective.
17107c478bd9Sstevel@tonic-gate  *
17117c478bd9Sstevel@tonic-gate  * If you have the privilege and euid 0 only then do you
17127c478bd9Sstevel@tonic-gate  * get the powers of root wrt uid 0.
17137c478bd9Sstevel@tonic-gate  *
17147c478bd9Sstevel@tonic-gate  * For gid manipulations, this is should be called with an
17157c478bd9Sstevel@tonic-gate  * uid of -1.
17167c478bd9Sstevel@tonic-gate  *
17177c478bd9Sstevel@tonic-gate  */
17187c478bd9Sstevel@tonic-gate int
secpolicy_allow_setid(const cred_t * cr,uid_t newuid,boolean_t checkonly)17197c478bd9Sstevel@tonic-gate secpolicy_allow_setid(const cred_t *cr, uid_t newuid, boolean_t checkonly)
17207c478bd9Sstevel@tonic-gate {
17217c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
17227c478bd9Sstevel@tonic-gate 
17237c478bd9Sstevel@tonic-gate 	if (newuid == 0 && cr->cr_uid != 0 && cr->cr_suid != 0 &&
17247c478bd9Sstevel@tonic-gate 	    cr->cr_ruid != 0) {
17257c478bd9Sstevel@tonic-gate 		allzone = B_TRUE;
17267c478bd9Sstevel@tonic-gate 	}
17277c478bd9Sstevel@tonic-gate 
17287c478bd9Sstevel@tonic-gate 	return (checkonly ? !PRIV_POLICY_ONLY(cr, PRIV_PROC_SETID, allzone) :
17297c478bd9Sstevel@tonic-gate 	    PRIV_POLICY(cr, PRIV_PROC_SETID, allzone, EPERM, NULL));
17307c478bd9Sstevel@tonic-gate }
17317c478bd9Sstevel@tonic-gate 
17327c478bd9Sstevel@tonic-gate 
17337c478bd9Sstevel@tonic-gate /*
17347c478bd9Sstevel@tonic-gate  * Acting on a different process: if the mode is for writing,
17357c478bd9Sstevel@tonic-gate  * the restrictions are more severe.  This is called after
17367c478bd9Sstevel@tonic-gate  * we've verified that the uids do not match.
17377c478bd9Sstevel@tonic-gate  */
17387c478bd9Sstevel@tonic-gate int
secpolicy_proc_owner(const cred_t * scr,const cred_t * tcr,int mode)17397c478bd9Sstevel@tonic-gate secpolicy_proc_owner(const cred_t *scr, const cred_t *tcr, int mode)
17407c478bd9Sstevel@tonic-gate {
17417c478bd9Sstevel@tonic-gate 	boolean_t allzone = B_FALSE;
17427c478bd9Sstevel@tonic-gate 
17437c478bd9Sstevel@tonic-gate 	if ((mode & VWRITE) && scr->cr_uid != 0 &&
17447c478bd9Sstevel@tonic-gate 	    (tcr->cr_uid == 0 || tcr->cr_ruid == 0 || tcr->cr_suid == 0))
17457c478bd9Sstevel@tonic-gate 		allzone = B_TRUE;
17467c478bd9Sstevel@tonic-gate 
17477c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, allzone, EPERM, NULL));
17487c478bd9Sstevel@tonic-gate }
17497c478bd9Sstevel@tonic-gate 
17507c478bd9Sstevel@tonic-gate int
secpolicy_proc_access(const cred_t * scr)17517c478bd9Sstevel@tonic-gate secpolicy_proc_access(const cred_t *scr)
17527c478bd9Sstevel@tonic-gate {
17537c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, B_FALSE, EACCES, NULL));
17547c478bd9Sstevel@tonic-gate }
17557c478bd9Sstevel@tonic-gate 
17567c478bd9Sstevel@tonic-gate int
secpolicy_proc_excl_open(const cred_t * scr)17577c478bd9Sstevel@tonic-gate secpolicy_proc_excl_open(const cred_t *scr)
17587c478bd9Sstevel@tonic-gate {
17597c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_OWNER, B_FALSE, EBUSY, NULL));
17607c478bd9Sstevel@tonic-gate }
17617c478bd9Sstevel@tonic-gate 
17627c478bd9Sstevel@tonic-gate int
secpolicy_proc_zone(const cred_t * scr)17637c478bd9Sstevel@tonic-gate secpolicy_proc_zone(const cred_t *scr)
17647c478bd9Sstevel@tonic-gate {
17657c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_PROC_ZONE, B_FALSE, EPERM, NULL));
17667c478bd9Sstevel@tonic-gate }
17677c478bd9Sstevel@tonic-gate 
17687c478bd9Sstevel@tonic-gate /*
17697c478bd9Sstevel@tonic-gate  * Destroying the system
17707c478bd9Sstevel@tonic-gate  */
17717c478bd9Sstevel@tonic-gate 
17727c478bd9Sstevel@tonic-gate int
secpolicy_kmdb(const cred_t * scr)17737c478bd9Sstevel@tonic-gate secpolicy_kmdb(const cred_t *scr)
17747c478bd9Sstevel@tonic-gate {
17757c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
17767c478bd9Sstevel@tonic-gate }
17777c478bd9Sstevel@tonic-gate 
17787aec1d6eScindi int
secpolicy_error_inject(const cred_t * scr)17797aec1d6eScindi secpolicy_error_inject(const cred_t *scr)
17807aec1d6eScindi {
17817aec1d6eScindi 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
17827aec1d6eScindi }
17837aec1d6eScindi 
17847c478bd9Sstevel@tonic-gate /*
17857c478bd9Sstevel@tonic-gate  * Processor sets, cpu configuration, resource pools.
17867c478bd9Sstevel@tonic-gate  */
17877c478bd9Sstevel@tonic-gate int
secpolicy_pset(const cred_t * cr)17887c478bd9Sstevel@tonic-gate secpolicy_pset(const cred_t *cr)
17897c478bd9Sstevel@tonic-gate {
17907c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
1791d2a70789SRichard Lowe }
1792d2a70789SRichard Lowe 
1793d2a70789SRichard Lowe /* Process security flags */
1794d2a70789SRichard Lowe int
secpolicy_psecflags(const cred_t * cr,proc_t * tp,proc_t * sp)1795d2a70789SRichard Lowe secpolicy_psecflags(const cred_t *cr, proc_t *tp, proc_t *sp)
1796d2a70789SRichard Lowe {
1797d2a70789SRichard Lowe 	if (PRIV_POLICY(cr, PRIV_PROC_SECFLAGS, B_FALSE, EPERM, NULL) != 0)
1798d2a70789SRichard Lowe 		return (EPERM);
1799d2a70789SRichard Lowe 
1800d2a70789SRichard Lowe 	if (!prochasprocperm(tp, sp, cr))
1801d2a70789SRichard Lowe 		return (EPERM);
1802d2a70789SRichard Lowe 
1803d2a70789SRichard Lowe 	return (0);
18047c478bd9Sstevel@tonic-gate }
18057c478bd9Sstevel@tonic-gate 
1806bbf58fc5S /*
1807bbf58fc5S  * Processor set binding.
1808bbf58fc5S  */
1809bbf58fc5S int
secpolicy_pbind(const cred_t * cr)1810bbf58fc5S secpolicy_pbind(const cred_t *cr)
1811bbf58fc5S {
1812bbf58fc5S 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_RES_CONFIG, B_FALSE))
1813bbf58fc5S 		return (secpolicy_pset(cr));
1814bbf58fc5S 	return (PRIV_POLICY(cr, PRIV_SYS_RES_BIND, B_FALSE, EPERM, NULL));
1815bbf58fc5S }
1816bbf58fc5S 
18177c478bd9Sstevel@tonic-gate int
secpolicy_ponline(const cred_t * cr)18187c478bd9Sstevel@tonic-gate secpolicy_ponline(const cred_t *cr)
18197c478bd9Sstevel@tonic-gate {
18207c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
18217c478bd9Sstevel@tonic-gate }
18227c478bd9Sstevel@tonic-gate 
18237c478bd9Sstevel@tonic-gate int
secpolicy_pool(const cred_t * cr)18247c478bd9Sstevel@tonic-gate secpolicy_pool(const cred_t *cr)
18257c478bd9Sstevel@tonic-gate {
18267c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
18277c478bd9Sstevel@tonic-gate }
18287c478bd9Sstevel@tonic-gate 
18297c478bd9Sstevel@tonic-gate int
secpolicy_blacklist(const cred_t * cr)18307c478bd9Sstevel@tonic-gate secpolicy_blacklist(const cred_t *cr)
18317c478bd9Sstevel@tonic-gate {
18327c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RES_CONFIG, B_FALSE, EPERM, NULL));
18337c478bd9Sstevel@tonic-gate }
18347c478bd9Sstevel@tonic-gate 
18357c478bd9Sstevel@tonic-gate /*
18367c478bd9Sstevel@tonic-gate  * Catch all system configuration.
18377c478bd9Sstevel@tonic-gate  */
18387c478bd9Sstevel@tonic-gate int
secpolicy_sys_config(const cred_t * cr,boolean_t checkonly)18397c478bd9Sstevel@tonic-gate secpolicy_sys_config(const cred_t *cr, boolean_t checkonly)
18407c478bd9Sstevel@tonic-gate {
18417c478bd9Sstevel@tonic-gate 	if (checkonly) {
18427c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_CONFIG, B_FALSE) ? 0 :
18437c478bd9Sstevel@tonic-gate 		    EPERM);
18447c478bd9Sstevel@tonic-gate 	} else {
18457c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
18467c478bd9Sstevel@tonic-gate 	}
18477c478bd9Sstevel@tonic-gate }
18487c478bd9Sstevel@tonic-gate 
18497c478bd9Sstevel@tonic-gate /*
18507c478bd9Sstevel@tonic-gate  * Zone administration (halt, reboot, etc.) from within zone.
18517c478bd9Sstevel@tonic-gate  */
18527c478bd9Sstevel@tonic-gate int
secpolicy_zone_admin(const cred_t * cr,boolean_t checkonly)18537c478bd9Sstevel@tonic-gate secpolicy_zone_admin(const cred_t *cr, boolean_t checkonly)
18547c478bd9Sstevel@tonic-gate {
18557c478bd9Sstevel@tonic-gate 	if (checkonly) {
18567c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_ADMIN, B_FALSE) ? 0 :
18577c478bd9Sstevel@tonic-gate 		    EPERM);
18587c478bd9Sstevel@tonic-gate 	} else {
18597c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM,
18607c478bd9Sstevel@tonic-gate 		    NULL));
18617c478bd9Sstevel@tonic-gate 	}
18627c478bd9Sstevel@tonic-gate }
18637c478bd9Sstevel@tonic-gate 
18647c478bd9Sstevel@tonic-gate /*
18657c478bd9Sstevel@tonic-gate  * Zone configuration (create, halt, enter).
18667c478bd9Sstevel@tonic-gate  */
18677c478bd9Sstevel@tonic-gate int
secpolicy_zone_config(const cred_t * cr)18687c478bd9Sstevel@tonic-gate secpolicy_zone_config(const cred_t *cr)
18697c478bd9Sstevel@tonic-gate {
18707c478bd9Sstevel@tonic-gate 	/*
18717c478bd9Sstevel@tonic-gate 	 * Require all privileges to avoid possibility of privilege
18727c478bd9Sstevel@tonic-gate 	 * escalation.
18737c478bd9Sstevel@tonic-gate 	 */
1874134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
18757c478bd9Sstevel@tonic-gate }
18767c478bd9Sstevel@tonic-gate 
18777c478bd9Sstevel@tonic-gate /*
18787c478bd9Sstevel@tonic-gate  * Various other system configuration calls
18797c478bd9Sstevel@tonic-gate  */
18807c478bd9Sstevel@tonic-gate int
secpolicy_coreadm(const cred_t * cr)18817c478bd9Sstevel@tonic-gate secpolicy_coreadm(const cred_t *cr)
18827c478bd9Sstevel@tonic-gate {
18837c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM, NULL));
18847c478bd9Sstevel@tonic-gate }
18857c478bd9Sstevel@tonic-gate 
18867c478bd9Sstevel@tonic-gate int
secpolicy_systeminfo(const cred_t * cr)18877c478bd9Sstevel@tonic-gate secpolicy_systeminfo(const cred_t *cr)
18887c478bd9Sstevel@tonic-gate {
18897c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_FALSE, EPERM, NULL));
18907c478bd9Sstevel@tonic-gate }
18917c478bd9Sstevel@tonic-gate 
18927c478bd9Sstevel@tonic-gate int
secpolicy_dispadm(const cred_t * cr)18937c478bd9Sstevel@tonic-gate secpolicy_dispadm(const cred_t *cr)
18947c478bd9Sstevel@tonic-gate {
18957c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
18967c478bd9Sstevel@tonic-gate }
18977c478bd9Sstevel@tonic-gate 
18987c478bd9Sstevel@tonic-gate int
secpolicy_settime(const cred_t * cr)18997c478bd9Sstevel@tonic-gate secpolicy_settime(const cred_t *cr)
19007c478bd9Sstevel@tonic-gate {
19017c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_TIME, B_FALSE, EPERM, NULL));
19027c478bd9Sstevel@tonic-gate }
19037c478bd9Sstevel@tonic-gate 
19047c478bd9Sstevel@tonic-gate /*
19057c478bd9Sstevel@tonic-gate  * For realtime users: high resolution clock.
19067c478bd9Sstevel@tonic-gate  */
19077c478bd9Sstevel@tonic-gate int
secpolicy_clock_highres(const cred_t * cr)19087c478bd9Sstevel@tonic-gate secpolicy_clock_highres(const cred_t *cr)
19097c478bd9Sstevel@tonic-gate {
19107c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_CLOCK_HIGHRES, B_FALSE, EPERM,
19117c478bd9Sstevel@tonic-gate 	    NULL));
19127c478bd9Sstevel@tonic-gate }
19137c478bd9Sstevel@tonic-gate 
19147c478bd9Sstevel@tonic-gate /*
19157c478bd9Sstevel@tonic-gate  * drv_priv() is documented as callable from interrupt context, not that
19167c478bd9Sstevel@tonic-gate  * anyone ever does, but still.  No debugging or auditing can be done when
19177c478bd9Sstevel@tonic-gate  * it is called from interrupt context.
19187c478bd9Sstevel@tonic-gate  * returns 0 on succes, EPERM on failure.
19197c478bd9Sstevel@tonic-gate  */
19207c478bd9Sstevel@tonic-gate int
drv_priv(cred_t * cr)19217c478bd9Sstevel@tonic-gate drv_priv(cred_t *cr)
19227c478bd9Sstevel@tonic-gate {
19237c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
19247c478bd9Sstevel@tonic-gate }
19257c478bd9Sstevel@tonic-gate 
19267c478bd9Sstevel@tonic-gate int
secpolicy_sys_devices(const cred_t * cr)19277c478bd9Sstevel@tonic-gate secpolicy_sys_devices(const cred_t *cr)
19287c478bd9Sstevel@tonic-gate {
19297c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
19307c478bd9Sstevel@tonic-gate }
19317c478bd9Sstevel@tonic-gate 
19327c478bd9Sstevel@tonic-gate int
secpolicy_excl_open(const cred_t * cr)19337c478bd9Sstevel@tonic-gate secpolicy_excl_open(const cred_t *cr)
19347c478bd9Sstevel@tonic-gate {
19357c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EBUSY, NULL));
19367c478bd9Sstevel@tonic-gate }
19377c478bd9Sstevel@tonic-gate 
19387c478bd9Sstevel@tonic-gate int
secpolicy_rctlsys(const cred_t * cr,boolean_t is_zone_rctl)19397c478bd9Sstevel@tonic-gate secpolicy_rctlsys(const cred_t *cr, boolean_t is_zone_rctl)
19407c478bd9Sstevel@tonic-gate {
19417c478bd9Sstevel@tonic-gate 	/* zone.* rctls can only be set from the global zone */
19427c478bd9Sstevel@tonic-gate 	if (is_zone_rctl && priv_policy_global(cr) != 0)
19437c478bd9Sstevel@tonic-gate 		return (EPERM);
19447c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
19457c478bd9Sstevel@tonic-gate }
19467c478bd9Sstevel@tonic-gate 
19477c478bd9Sstevel@tonic-gate int
secpolicy_resource(const cred_t * cr)19487c478bd9Sstevel@tonic-gate secpolicy_resource(const cred_t *cr)
19497c478bd9Sstevel@tonic-gate {
19507c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
19517c478bd9Sstevel@tonic-gate }
19527c478bd9Sstevel@tonic-gate 
19531c7cef2bSStan Studzinski int
secpolicy_resource_anon_mem(const cred_t * cr)19541c7cef2bSStan Studzinski secpolicy_resource_anon_mem(const cred_t *cr)
19551c7cef2bSStan Studzinski {
19561c7cef2bSStan Studzinski 	return (PRIV_POLICY_ONLY(cr, PRIV_SYS_RESOURCE, B_FALSE));
19571c7cef2bSStan Studzinski }
19581c7cef2bSStan Studzinski 
19597c478bd9Sstevel@tonic-gate /*
19607c478bd9Sstevel@tonic-gate  * Processes with a real uid of 0 escape any form of accounting, much
19617c478bd9Sstevel@tonic-gate  * like before.
19627c478bd9Sstevel@tonic-gate  */
19637c478bd9Sstevel@tonic-gate int
secpolicy_newproc(const cred_t * cr)19647c478bd9Sstevel@tonic-gate secpolicy_newproc(const cred_t *cr)
19657c478bd9Sstevel@tonic-gate {
19667c478bd9Sstevel@tonic-gate 	if (cr->cr_ruid == 0)
19677c478bd9Sstevel@tonic-gate 		return (0);
19687c478bd9Sstevel@tonic-gate 
19697c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_RESOURCE, B_FALSE, EPERM, NULL));
19707c478bd9Sstevel@tonic-gate }
19717c478bd9Sstevel@tonic-gate 
19727c478bd9Sstevel@tonic-gate /*
19737c478bd9Sstevel@tonic-gate  * Networking
19747c478bd9Sstevel@tonic-gate  */
19757c478bd9Sstevel@tonic-gate int
secpolicy_net_rawaccess(const cred_t * cr)19767c478bd9Sstevel@tonic-gate secpolicy_net_rawaccess(const cred_t *cr)
19777c478bd9Sstevel@tonic-gate {
19787c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_NET_RAWACCESS, B_FALSE, EACCES, NULL));
19797c478bd9Sstevel@tonic-gate }
19807c478bd9Sstevel@tonic-gate 
19810a0e9771SDarren Reed int
secpolicy_net_observability(const cred_t * cr)19820a0e9771SDarren Reed secpolicy_net_observability(const cred_t *cr)
19830a0e9771SDarren Reed {
19840a0e9771SDarren Reed 	return (PRIV_POLICY(cr, PRIV_NET_OBSERVABILITY, B_FALSE, EACCES, NULL));
19850a0e9771SDarren Reed }
19860a0e9771SDarren Reed 
19877c478bd9Sstevel@tonic-gate /*
19887c478bd9Sstevel@tonic-gate  * Need this privilege for accessing the ICMP device
19897c478bd9Sstevel@tonic-gate  */
19907c478bd9Sstevel@tonic-gate int
secpolicy_net_icmpaccess(const cred_t * cr)19917c478bd9Sstevel@tonic-gate secpolicy_net_icmpaccess(const cred_t *cr)
19927c478bd9Sstevel@tonic-gate {
19937c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_NET_ICMPACCESS, B_FALSE, EACCES, NULL));
19947c478bd9Sstevel@tonic-gate }
19957c478bd9Sstevel@tonic-gate 
19967c478bd9Sstevel@tonic-gate /*
19977c478bd9Sstevel@tonic-gate  * There are a few rare cases where the kernel generates ioctls() from
19987c478bd9Sstevel@tonic-gate  * interrupt context with a credential of kcred rather than NULL.
19997c478bd9Sstevel@tonic-gate  * In those cases, we take the safe and cheap test.
20007c478bd9Sstevel@tonic-gate  */
20017c478bd9Sstevel@tonic-gate int
secpolicy_net_config(const cred_t * cr,boolean_t checkonly)20027c478bd9Sstevel@tonic-gate secpolicy_net_config(const cred_t *cr, boolean_t checkonly)
20037c478bd9Sstevel@tonic-gate {
20047c478bd9Sstevel@tonic-gate 	if (checkonly) {
20057c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE) ?
20067c478bd9Sstevel@tonic-gate 		    0 : EPERM);
20077c478bd9Sstevel@tonic-gate 	} else {
20087c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_SYS_NET_CONFIG, B_FALSE, EPERM,
20097c478bd9Sstevel@tonic-gate 		    NULL));
20107c478bd9Sstevel@tonic-gate 	}
20117c478bd9Sstevel@tonic-gate }
20127c478bd9Sstevel@tonic-gate 
20137c478bd9Sstevel@tonic-gate 
20147c478bd9Sstevel@tonic-gate /*
2015e6bdcbd5Sdh155122  * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_IP_CONFIG.
2016f4b3ec61Sdh155122  *
2017f4b3ec61Sdh155122  * There are a few rare cases where the kernel generates ioctls() from
2018f4b3ec61Sdh155122  * interrupt context with a credential of kcred rather than NULL.
2019f4b3ec61Sdh155122  * In those cases, we take the safe and cheap test.
2020f4b3ec61Sdh155122  */
2021f4b3ec61Sdh155122 int
secpolicy_ip_config(const cred_t * cr,boolean_t checkonly)2022f4b3ec61Sdh155122 secpolicy_ip_config(const cred_t *cr, boolean_t checkonly)
2023f4b3ec61Sdh155122 {
2024f4b3ec61Sdh155122 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
2025f4b3ec61Sdh155122 		return (secpolicy_net_config(cr, checkonly));
2026f4b3ec61Sdh155122 
2027f4b3ec61Sdh155122 	if (checkonly) {
2028f4b3ec61Sdh155122 		return (PRIV_POLICY_ONLY(cr, PRIV_SYS_IP_CONFIG, B_FALSE) ?
2029f4b3ec61Sdh155122 		    0 : EPERM);
2030f4b3ec61Sdh155122 	} else {
2031f4b3ec61Sdh155122 		return (PRIV_POLICY(cr, PRIV_SYS_IP_CONFIG, B_FALSE, EPERM,
2032f4b3ec61Sdh155122 		    NULL));
2033f4b3ec61Sdh155122 	}
2034f4b3ec61Sdh155122 }
2035f4b3ec61Sdh155122 
2036eae72b5bSSebastien Roy /*
2037eae72b5bSSebastien Roy  * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_DL_CONFIG.
2038eae72b5bSSebastien Roy  */
2039eae72b5bSSebastien Roy int
secpolicy_dl_config(const cred_t * cr)2040eae72b5bSSebastien Roy secpolicy_dl_config(const cred_t *cr)
2041eae72b5bSSebastien Roy {
2042eae72b5bSSebastien Roy 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
2043eae72b5bSSebastien Roy 		return (secpolicy_net_config(cr, B_FALSE));
20442b24ab6bSSebastien Roy 	return (PRIV_POLICY(cr, PRIV_SYS_DL_CONFIG, B_FALSE, EPERM, NULL));
2045eae72b5bSSebastien Roy }
2046eae72b5bSSebastien Roy 
20472b24ab6bSSebastien Roy /*
20482b24ab6bSSebastien Roy  * PRIV_SYS_DL_CONFIG is a superset of PRIV_SYS_IPTUN_CONFIG.
20492b24ab6bSSebastien Roy  */
20502b24ab6bSSebastien Roy int
secpolicy_iptun_config(const cred_t * cr)20512b24ab6bSSebastien Roy secpolicy_iptun_config(const cred_t *cr)
20522b24ab6bSSebastien Roy {
20532b24ab6bSSebastien Roy 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
20542b24ab6bSSebastien Roy 		return (secpolicy_net_config(cr, B_FALSE));
20552b24ab6bSSebastien Roy 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_DL_CONFIG, B_FALSE))
20562b24ab6bSSebastien Roy 		return (secpolicy_dl_config(cr));
20572b24ab6bSSebastien Roy 	return (PRIV_POLICY(cr, PRIV_SYS_IPTUN_CONFIG, B_FALSE, EPERM, NULL));
20582b24ab6bSSebastien Roy }
2059f4b3ec61Sdh155122 
2060f4b3ec61Sdh155122 /*
2061f4b3ec61Sdh155122  * Map IP pseudo privileges to actual privileges.
2062f4b3ec61Sdh155122  * So we don't need to recompile IP when we change the privileges.
2063f4b3ec61Sdh155122  */
2064f4b3ec61Sdh155122 int
secpolicy_ip(const cred_t * cr,int netpriv,boolean_t checkonly)2065f4b3ec61Sdh155122 secpolicy_ip(const cred_t *cr, int netpriv, boolean_t checkonly)
2066f4b3ec61Sdh155122 {
2067f4b3ec61Sdh155122 	int priv = PRIV_ALL;
2068f4b3ec61Sdh155122 
2069f4b3ec61Sdh155122 	switch (netpriv) {
2070f4b3ec61Sdh155122 	case OP_CONFIG:
2071f4b3ec61Sdh155122 		priv = PRIV_SYS_IP_CONFIG;
2072f4b3ec61Sdh155122 		break;
2073f4b3ec61Sdh155122 	case OP_RAW:
2074f4b3ec61Sdh155122 		priv = PRIV_NET_RAWACCESS;
2075f4b3ec61Sdh155122 		break;
2076f4b3ec61Sdh155122 	case OP_PRIVPORT:
2077f4b3ec61Sdh155122 		priv = PRIV_NET_PRIVADDR;
2078f4b3ec61Sdh155122 		break;
2079f4b3ec61Sdh155122 	}
2080f4b3ec61Sdh155122 	ASSERT(priv != PRIV_ALL);
2081f4b3ec61Sdh155122 	if (checkonly)
2082f4b3ec61Sdh155122 		return (PRIV_POLICY_ONLY(cr, priv, B_FALSE) ? 0 : EPERM);
2083f4b3ec61Sdh155122 	else
2084f4b3ec61Sdh155122 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
2085f4b3ec61Sdh155122 }
2086f4b3ec61Sdh155122 
2087f4b3ec61Sdh155122 /*
20887c478bd9Sstevel@tonic-gate  * Map network pseudo privileges to actual privileges.
20897c478bd9Sstevel@tonic-gate  * So we don't need to recompile IP when we change the privileges.
20907c478bd9Sstevel@tonic-gate  */
20917c478bd9Sstevel@tonic-gate int
secpolicy_net(const cred_t * cr,int netpriv,boolean_t checkonly)20927c478bd9Sstevel@tonic-gate secpolicy_net(const cred_t *cr, int netpriv, boolean_t checkonly)
20937c478bd9Sstevel@tonic-gate {
20947c478bd9Sstevel@tonic-gate 	int priv = PRIV_ALL;
20957c478bd9Sstevel@tonic-gate 
20967c478bd9Sstevel@tonic-gate 	switch (netpriv) {
20977c478bd9Sstevel@tonic-gate 	case OP_CONFIG:
20987c478bd9Sstevel@tonic-gate 		priv = PRIV_SYS_NET_CONFIG;
20997c478bd9Sstevel@tonic-gate 		break;
21007c478bd9Sstevel@tonic-gate 	case OP_RAW:
21017c478bd9Sstevel@tonic-gate 		priv = PRIV_NET_RAWACCESS;
21027c478bd9Sstevel@tonic-gate 		break;
21037c478bd9Sstevel@tonic-gate 	case OP_PRIVPORT:
21047c478bd9Sstevel@tonic-gate 		priv = PRIV_NET_PRIVADDR;
21057c478bd9Sstevel@tonic-gate 		break;
21067c478bd9Sstevel@tonic-gate 	}
21077c478bd9Sstevel@tonic-gate 	ASSERT(priv != PRIV_ALL);
21087c478bd9Sstevel@tonic-gate 	if (checkonly)
21097c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY_ONLY(cr, priv, B_FALSE) ? 0 : EPERM);
21107c478bd9Sstevel@tonic-gate 	else
21117c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, priv, B_FALSE, EPERM, NULL));
21127c478bd9Sstevel@tonic-gate }
21137c478bd9Sstevel@tonic-gate 
21147c478bd9Sstevel@tonic-gate /*
21157c478bd9Sstevel@tonic-gate  * Checks for operations that are either client-only or are used by
21167c478bd9Sstevel@tonic-gate  * both clients and servers.
21177c478bd9Sstevel@tonic-gate  */
21187c478bd9Sstevel@tonic-gate int
secpolicy_nfs(const cred_t * cr)21197c478bd9Sstevel@tonic-gate secpolicy_nfs(const cred_t *cr)
21207c478bd9Sstevel@tonic-gate {
21217c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_NFS, B_FALSE, EPERM, NULL));
21227c478bd9Sstevel@tonic-gate }
21237c478bd9Sstevel@tonic-gate 
21247c478bd9Sstevel@tonic-gate /*
21257c478bd9Sstevel@tonic-gate  * Special case for opening rpcmod: have NFS privileges or network
21267c478bd9Sstevel@tonic-gate  * config privileges.
21277c478bd9Sstevel@tonic-gate  */
21287c478bd9Sstevel@tonic-gate int
secpolicy_rpcmod_open(const cred_t * cr)21297c478bd9Sstevel@tonic-gate secpolicy_rpcmod_open(const cred_t *cr)
21307c478bd9Sstevel@tonic-gate {
21317c478bd9Sstevel@tonic-gate 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NFS, B_FALSE))
21327c478bd9Sstevel@tonic-gate 		return (secpolicy_nfs(cr));
21337c478bd9Sstevel@tonic-gate 	else
21347e12ceb3SToomas Soome 		return (secpolicy_net_config(cr, B_FALSE));
21357c478bd9Sstevel@tonic-gate }
21367c478bd9Sstevel@tonic-gate 
21377c478bd9Sstevel@tonic-gate int
secpolicy_chroot(const cred_t * cr)21387c478bd9Sstevel@tonic-gate secpolicy_chroot(const cred_t *cr)
21397c478bd9Sstevel@tonic-gate {
21407c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_CHROOT, B_FALSE, EPERM, NULL));
21417c478bd9Sstevel@tonic-gate }
21427c478bd9Sstevel@tonic-gate 
21437c478bd9Sstevel@tonic-gate int
secpolicy_tasksys(const cred_t * cr)21447c478bd9Sstevel@tonic-gate secpolicy_tasksys(const cred_t *cr)
21457c478bd9Sstevel@tonic-gate {
21467c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_TASKID, B_FALSE, EPERM, NULL));
21477c478bd9Sstevel@tonic-gate }
21487c478bd9Sstevel@tonic-gate 
2149134a1f4eSCasper H.S. Dik int
secpolicy_meminfo(const cred_t * cr)215037294019SJerry Jelinek secpolicy_meminfo(const cred_t *cr)
215137294019SJerry Jelinek {
215237294019SJerry Jelinek 	return (PRIV_POLICY(cr, PRIV_PROC_MEMINFO, B_FALSE, EPERM, NULL));
215337294019SJerry Jelinek }
215437294019SJerry Jelinek 
215537294019SJerry Jelinek int
secpolicy_pfexec_register(const cred_t * cr)2156134a1f4eSCasper H.S. Dik secpolicy_pfexec_register(const cred_t *cr)
2157134a1f4eSCasper H.S. Dik {
2158134a1f4eSCasper H.S. Dik 	return (PRIV_POLICY(cr, PRIV_SYS_ADMIN, B_TRUE, EPERM, NULL));
2159134a1f4eSCasper H.S. Dik }
2160134a1f4eSCasper H.S. Dik 
21617c478bd9Sstevel@tonic-gate /*
21627c478bd9Sstevel@tonic-gate  * Basic privilege checks.
21637c478bd9Sstevel@tonic-gate  */
21647c478bd9Sstevel@tonic-gate int
secpolicy_basic_exec(const cred_t * cr,vnode_t * vp)2165ddf7fe95Scasper secpolicy_basic_exec(const cred_t *cr, vnode_t *vp)
21667c478bd9Sstevel@tonic-gate {
2167134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_PROC_EXEC);
2168134a1f4eSCasper H.S. Dik 
2169ddf7fe95Scasper 	return (priv_policy_va(cr, PRIV_PROC_EXEC, B_FALSE, EPERM, NULL,
2170ddf7fe95Scasper 	    KLPDARG_VNODE, vp, (char *)NULL, KLPDARG_NOMORE));
21717c478bd9Sstevel@tonic-gate }
21727c478bd9Sstevel@tonic-gate 
21737c478bd9Sstevel@tonic-gate int
secpolicy_basic_fork(const cred_t * cr)21747c478bd9Sstevel@tonic-gate secpolicy_basic_fork(const cred_t *cr)
21757c478bd9Sstevel@tonic-gate {
2176134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_PROC_FORK);
2177134a1f4eSCasper H.S. Dik 
21787c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_FORK, B_FALSE, EPERM, NULL));
21797c478bd9Sstevel@tonic-gate }
21807c478bd9Sstevel@tonic-gate 
21817c478bd9Sstevel@tonic-gate int
secpolicy_basic_proc(const cred_t * cr)21827c478bd9Sstevel@tonic-gate secpolicy_basic_proc(const cred_t *cr)
21837c478bd9Sstevel@tonic-gate {
2184134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_PROC_SESSION);
2185134a1f4eSCasper H.S. Dik 
21867c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_PROC_SESSION, B_FALSE, EPERM, NULL));
21877c478bd9Sstevel@tonic-gate }
21887c478bd9Sstevel@tonic-gate 
21897c478bd9Sstevel@tonic-gate /*
21907c478bd9Sstevel@tonic-gate  * Slightly complicated because we don't want to trigger the policy too
21917c478bd9Sstevel@tonic-gate  * often.  First we shortcircuit access to "self" (tp == sp) or if
21927c478bd9Sstevel@tonic-gate  * we don't have the privilege but if we have permission
21937c478bd9Sstevel@tonic-gate  * just return (0) and we don't flag the privilege as needed.
21947c478bd9Sstevel@tonic-gate  * Else, we test for the privilege because we either have it or need it.
21957c478bd9Sstevel@tonic-gate  */
21967c478bd9Sstevel@tonic-gate int
secpolicy_basic_procinfo(const cred_t * cr,proc_t * tp,proc_t * sp)21977c478bd9Sstevel@tonic-gate secpolicy_basic_procinfo(const cred_t *cr, proc_t *tp, proc_t *sp)
21987c478bd9Sstevel@tonic-gate {
21997c478bd9Sstevel@tonic-gate 	if (tp == sp ||
22007c478bd9Sstevel@tonic-gate 	    !HAS_PRIVILEGE(cr, PRIV_PROC_INFO) && prochasprocperm(tp, sp, cr)) {
22017c478bd9Sstevel@tonic-gate 		return (0);
22027c478bd9Sstevel@tonic-gate 	} else {
22037c478bd9Sstevel@tonic-gate 		return (PRIV_POLICY(cr, PRIV_PROC_INFO, B_FALSE, EPERM, NULL));
22047c478bd9Sstevel@tonic-gate 	}
22057c478bd9Sstevel@tonic-gate }
22067c478bd9Sstevel@tonic-gate 
22077c478bd9Sstevel@tonic-gate int
secpolicy_basic_link(const cred_t * cr)22087c478bd9Sstevel@tonic-gate secpolicy_basic_link(const cred_t *cr)
22097c478bd9Sstevel@tonic-gate {
2210134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_FILE_LINK_ANY);
2211134a1f4eSCasper H.S. Dik 
22127c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_FILE_LINK_ANY, B_FALSE, EPERM, NULL));
22137c478bd9Sstevel@tonic-gate }
22147c478bd9Sstevel@tonic-gate 
2215634e26ecSCasper H.S. Dik int
secpolicy_basic_net_access(const cred_t * cr)2216634e26ecSCasper H.S. Dik secpolicy_basic_net_access(const cred_t *cr)
2217634e26ecSCasper H.S. Dik {
2218134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_NET_ACCESS);
2219134a1f4eSCasper H.S. Dik 
2220634e26ecSCasper H.S. Dik 	return (PRIV_POLICY(cr, PRIV_NET_ACCESS, B_FALSE, EACCES, NULL));
2221634e26ecSCasper H.S. Dik }
2222634e26ecSCasper H.S. Dik 
2223134a1f4eSCasper H.S. Dik /* ARGSUSED */
2224134a1f4eSCasper H.S. Dik int
secpolicy_basic_file_read(const cred_t * cr,vnode_t * vp,const char * pn)2225134a1f4eSCasper H.S. Dik secpolicy_basic_file_read(const cred_t *cr, vnode_t *vp, const char *pn)
2226134a1f4eSCasper H.S. Dik {
2227134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_FILE_READ);
2228134a1f4eSCasper H.S. Dik 
2229134a1f4eSCasper H.S. Dik 	return (priv_policy_va(cr, PRIV_FILE_READ, B_FALSE, EACCES, NULL,
2230134a1f4eSCasper H.S. Dik 	    KLPDARG_VNODE, vp, (char *)pn, KLPDARG_NOMORE));
2231134a1f4eSCasper H.S. Dik }
2232134a1f4eSCasper H.S. Dik 
2233134a1f4eSCasper H.S. Dik /* ARGSUSED */
2234134a1f4eSCasper H.S. Dik int
secpolicy_basic_file_write(const cred_t * cr,vnode_t * vp,const char * pn)2235134a1f4eSCasper H.S. Dik secpolicy_basic_file_write(const cred_t *cr, vnode_t *vp, const char *pn)
2236134a1f4eSCasper H.S. Dik {
2237134a1f4eSCasper H.S. Dik 	FAST_BASIC_CHECK(cr, PRIV_FILE_WRITE);
2238134a1f4eSCasper H.S. Dik 
2239134a1f4eSCasper H.S. Dik 	return (priv_policy_va(cr, PRIV_FILE_WRITE, B_FALSE, EACCES, NULL,
2240134a1f4eSCasper H.S. Dik 	    KLPDARG_VNODE, vp, (char *)pn, KLPDARG_NOMORE));
2241134a1f4eSCasper H.S. Dik }
2242134a1f4eSCasper H.S. Dik 
22437c478bd9Sstevel@tonic-gate /*
22447c478bd9Sstevel@tonic-gate  * Additional device protection.
22457c478bd9Sstevel@tonic-gate  *
22467c478bd9Sstevel@tonic-gate  * Traditionally, a device has specific permissions on the node in
22477c478bd9Sstevel@tonic-gate  * the filesystem which govern which devices can be opened by what
22487c478bd9Sstevel@tonic-gate  * processes.  In certain cases, it is desirable to add extra
22497c478bd9Sstevel@tonic-gate  * restrictions, as writing to certain devices is identical to
22507c478bd9Sstevel@tonic-gate  * having a complete run of the system.
22517c478bd9Sstevel@tonic-gate  *
22527c478bd9Sstevel@tonic-gate  * This mechanism is called the device policy.
22537c478bd9Sstevel@tonic-gate  *
22547c478bd9Sstevel@tonic-gate  * When a device is opened, its policy entry is looked up in the
22557c478bd9Sstevel@tonic-gate  * policy cache and checked.
22567c478bd9Sstevel@tonic-gate  */
22577c478bd9Sstevel@tonic-gate int
secpolicy_spec_open(const cred_t * cr,struct vnode * vp,int oflag)22587c478bd9Sstevel@tonic-gate secpolicy_spec_open(const cred_t *cr, struct vnode *vp, int oflag)
22597c478bd9Sstevel@tonic-gate {
22607c478bd9Sstevel@tonic-gate 	devplcy_t *plcy;
22617c478bd9Sstevel@tonic-gate 	int err;
22627c478bd9Sstevel@tonic-gate 	struct snode *csp = VTOS(common_specvp(vp));
2263e6bdcbd5Sdh155122 	priv_set_t pset;
22647c478bd9Sstevel@tonic-gate 
22657c478bd9Sstevel@tonic-gate 	mutex_enter(&csp->s_lock);
22667c478bd9Sstevel@tonic-gate 
22677c478bd9Sstevel@tonic-gate 	if (csp->s_plcy == NULL || csp->s_plcy->dp_gen != devplcy_gen) {
22687c478bd9Sstevel@tonic-gate 		plcy = devpolicy_find(vp);
22697c478bd9Sstevel@tonic-gate 		if (csp->s_plcy)
22707c478bd9Sstevel@tonic-gate 			dpfree(csp->s_plcy);
22717c478bd9Sstevel@tonic-gate 		csp->s_plcy = plcy;
22727c478bd9Sstevel@tonic-gate 		ASSERT(plcy != NULL);
22737c478bd9Sstevel@tonic-gate 	} else
22747c478bd9Sstevel@tonic-gate 		plcy = csp->s_plcy;
22757c478bd9Sstevel@tonic-gate 
22767c478bd9Sstevel@tonic-gate 	if (plcy == nullpolicy) {
22777c478bd9Sstevel@tonic-gate 		mutex_exit(&csp->s_lock);
22787c478bd9Sstevel@tonic-gate 		return (0);
22797c478bd9Sstevel@tonic-gate 	}
22807c478bd9Sstevel@tonic-gate 
22817c478bd9Sstevel@tonic-gate 	dphold(plcy);
22827c478bd9Sstevel@tonic-gate 
22837c478bd9Sstevel@tonic-gate 	mutex_exit(&csp->s_lock);
22847c478bd9Sstevel@tonic-gate 
2285e6bdcbd5Sdh155122 	if (oflag & FWRITE)
2286e6bdcbd5Sdh155122 		pset = plcy->dp_wrp;
2287e6bdcbd5Sdh155122 	else
2288e6bdcbd5Sdh155122 		pset = plcy->dp_rdp;
2289e6bdcbd5Sdh155122 	/*
2290e6bdcbd5Sdh155122 	 * Special case:
2291e6bdcbd5Sdh155122 	 * PRIV_SYS_NET_CONFIG is a superset of PRIV_SYS_IP_CONFIG.
2292e6bdcbd5Sdh155122 	 * If PRIV_SYS_NET_CONFIG is present and PRIV_SYS_IP_CONFIG is
2293e6bdcbd5Sdh155122 	 * required, replace PRIV_SYS_IP_CONFIG with PRIV_SYS_NET_CONFIG
2294e6bdcbd5Sdh155122 	 * in the required privilege set before doing the check.
2295e6bdcbd5Sdh155122 	 */
2296e6bdcbd5Sdh155122 	if (priv_ismember(&pset, PRIV_SYS_IP_CONFIG) &&
2297e6bdcbd5Sdh155122 	    priv_ismember(&CR_OEPRIV(cr), PRIV_SYS_NET_CONFIG) &&
2298e6bdcbd5Sdh155122 	    !priv_ismember(&CR_OEPRIV(cr), PRIV_SYS_IP_CONFIG)) {
2299e6bdcbd5Sdh155122 		priv_delset(&pset, PRIV_SYS_IP_CONFIG);
2300e6bdcbd5Sdh155122 		priv_addset(&pset, PRIV_SYS_NET_CONFIG);
2301e6bdcbd5Sdh155122 	}
2302e6bdcbd5Sdh155122 
2303134a1f4eSCasper H.S. Dik 	err = secpolicy_require_set(cr, &pset, "devpolicy", KLPDARG_NONE);
23047c478bd9Sstevel@tonic-gate 	dpfree(plcy);
23057c478bd9Sstevel@tonic-gate 
23067c478bd9Sstevel@tonic-gate 	return (err);
23077c478bd9Sstevel@tonic-gate }
23087c478bd9Sstevel@tonic-gate 
23097c478bd9Sstevel@tonic-gate int
secpolicy_modctl(const cred_t * cr,int cmd)23107c478bd9Sstevel@tonic-gate secpolicy_modctl(const cred_t *cr, int cmd)
23117c478bd9Sstevel@tonic-gate {
23127c478bd9Sstevel@tonic-gate 	switch (cmd) {
23137c478bd9Sstevel@tonic-gate 	case MODINFO:
2314a08731ecScth 	case MODGETMAJBIND:
23157c478bd9Sstevel@tonic-gate 	case MODGETPATH:
23167c478bd9Sstevel@tonic-gate 	case MODGETPATHLEN:
23177c478bd9Sstevel@tonic-gate 	case MODGETNAME:
2318a08731ecScth 	case MODGETFBNAME:
23197c478bd9Sstevel@tonic-gate 	case MODGETDEVPOLICY:
23207c478bd9Sstevel@tonic-gate 	case MODGETDEVPOLICYBYNAME:
2321a08731ecScth 	case MODDEVT2INSTANCE:
2322a08731ecScth 	case MODSIZEOF_DEVID:
2323a08731ecScth 	case MODGETDEVID:
2324a08731ecScth 	case MODSIZEOF_MINORNAME:
2325a08731ecScth 	case MODGETMINORNAME:
2326a08731ecScth 	case MODGETDEVFSPATH_LEN:
2327a08731ecScth 	case MODGETDEVFSPATH:
2328a08731ecScth 	case MODGETDEVFSPATH_MI_LEN:
2329a08731ecScth 	case MODGETDEVFSPATH_MI:
23307c478bd9Sstevel@tonic-gate 		/* Unprivileged */
23317c478bd9Sstevel@tonic-gate 		return (0);
23327c478bd9Sstevel@tonic-gate 	case MODLOAD:
23337c478bd9Sstevel@tonic-gate 	case MODSETDEVPOLICY:
2334134a1f4eSCasper H.S. Dik 		return (secpolicy_require_set(cr, PRIV_FULLSET, NULL,
2335134a1f4eSCasper H.S. Dik 		    KLPDARG_NONE));
23367c478bd9Sstevel@tonic-gate 	default:
23377c478bd9Sstevel@tonic-gate 		return (secpolicy_sys_config(cr, B_FALSE));
23387c478bd9Sstevel@tonic-gate 	}
23397c478bd9Sstevel@tonic-gate }
23407c478bd9Sstevel@tonic-gate 
23417c478bd9Sstevel@tonic-gate int
secpolicy_console(const cred_t * cr)23427c478bd9Sstevel@tonic-gate secpolicy_console(const cred_t *cr)
23437c478bd9Sstevel@tonic-gate {
23447c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
23457c478bd9Sstevel@tonic-gate }
23467c478bd9Sstevel@tonic-gate 
23477c478bd9Sstevel@tonic-gate int
secpolicy_power_mgmt(const cred_t * cr)23487c478bd9Sstevel@tonic-gate secpolicy_power_mgmt(const cred_t *cr)
23497c478bd9Sstevel@tonic-gate {
23507c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_DEVICES, B_FALSE, EPERM, NULL));
23517c478bd9Sstevel@tonic-gate }
23527c478bd9Sstevel@tonic-gate 
23537c478bd9Sstevel@tonic-gate /*
23547c478bd9Sstevel@tonic-gate  * Simulate terminal input; another escalation of privileges avenue.
23557c478bd9Sstevel@tonic-gate  */
23567c478bd9Sstevel@tonic-gate 
23577c478bd9Sstevel@tonic-gate int
secpolicy_sti(const cred_t * cr)23587c478bd9Sstevel@tonic-gate secpolicy_sti(const cred_t *cr)
23597c478bd9Sstevel@tonic-gate {
2360134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
23617c478bd9Sstevel@tonic-gate }
23627c478bd9Sstevel@tonic-gate 
236345916cd2Sjpk boolean_t
secpolicy_net_reply_equal(const cred_t * cr)236445916cd2Sjpk secpolicy_net_reply_equal(const cred_t *cr)
236545916cd2Sjpk {
236645916cd2Sjpk 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
236745916cd2Sjpk }
236845916cd2Sjpk 
23697c478bd9Sstevel@tonic-gate int
secpolicy_swapctl(const cred_t * cr)23707c478bd9Sstevel@tonic-gate secpolicy_swapctl(const cred_t *cr)
23717c478bd9Sstevel@tonic-gate {
23727c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_SYS_CONFIG, B_FALSE, EPERM, NULL));
23737c478bd9Sstevel@tonic-gate }
23747c478bd9Sstevel@tonic-gate 
23757c478bd9Sstevel@tonic-gate int
secpolicy_cpc_cpu(const cred_t * cr)23767c478bd9Sstevel@tonic-gate secpolicy_cpc_cpu(const cred_t *cr)
23777c478bd9Sstevel@tonic-gate {
23787c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CPC_CPU, B_FALSE, EACCES, NULL));
23797c478bd9Sstevel@tonic-gate }
23807c478bd9Sstevel@tonic-gate 
23817c478bd9Sstevel@tonic-gate /*
23827b209c2cSacruz  * secpolicy_contract_identity
23837b209c2cSacruz  *
23847b209c2cSacruz  * Determine if the subject may set the process contract FMRI value
23857b209c2cSacruz  */
23867b209c2cSacruz int
secpolicy_contract_identity(const cred_t * cr)23877b209c2cSacruz secpolicy_contract_identity(const cred_t *cr)
23887b209c2cSacruz {
23897b209c2cSacruz 	return (PRIV_POLICY(cr, PRIV_CONTRACT_IDENTITY, B_FALSE, EPERM, NULL));
23907b209c2cSacruz }
23917b209c2cSacruz 
23927b209c2cSacruz /*
23937c478bd9Sstevel@tonic-gate  * secpolicy_contract_observer
23947c478bd9Sstevel@tonic-gate  *
23957c478bd9Sstevel@tonic-gate  * Determine if the subject may observe a specific contract's events.
23967c478bd9Sstevel@tonic-gate  */
23977c478bd9Sstevel@tonic-gate int
secpolicy_contract_observer(const cred_t * cr,struct contract * ct)23987c478bd9Sstevel@tonic-gate secpolicy_contract_observer(const cred_t *cr, struct contract *ct)
23997c478bd9Sstevel@tonic-gate {
24007c478bd9Sstevel@tonic-gate 	if (contract_owned(ct, cr, B_FALSE))
24017c478bd9Sstevel@tonic-gate 		return (0);
24027c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CONTRACT_OBSERVER, B_FALSE, EPERM, NULL));
24037c478bd9Sstevel@tonic-gate }
24047c478bd9Sstevel@tonic-gate 
24057c478bd9Sstevel@tonic-gate /*
24067c478bd9Sstevel@tonic-gate  * secpolicy_contract_observer_choice
24077c478bd9Sstevel@tonic-gate  *
24087c478bd9Sstevel@tonic-gate  * Determine if the subject may observe any contract's events.  Just
24097c478bd9Sstevel@tonic-gate  * tests privilege and audits on success.
24107c478bd9Sstevel@tonic-gate  */
24117c478bd9Sstevel@tonic-gate boolean_t
secpolicy_contract_observer_choice(const cred_t * cr)24127c478bd9Sstevel@tonic-gate secpolicy_contract_observer_choice(const cred_t *cr)
24137c478bd9Sstevel@tonic-gate {
24147c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY_CHOICE(cr, PRIV_CONTRACT_OBSERVER, B_FALSE));
24157c478bd9Sstevel@tonic-gate }
24167c478bd9Sstevel@tonic-gate 
24177c478bd9Sstevel@tonic-gate /*
24187c478bd9Sstevel@tonic-gate  * secpolicy_contract_event
24197c478bd9Sstevel@tonic-gate  *
24207c478bd9Sstevel@tonic-gate  * Determine if the subject may request critical contract events or
24217c478bd9Sstevel@tonic-gate  * reliable contract event delivery.
24227c478bd9Sstevel@tonic-gate  */
24237c478bd9Sstevel@tonic-gate int
secpolicy_contract_event(const cred_t * cr)24247c478bd9Sstevel@tonic-gate secpolicy_contract_event(const cred_t *cr)
24257c478bd9Sstevel@tonic-gate {
24267c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY(cr, PRIV_CONTRACT_EVENT, B_FALSE, EPERM, NULL));
24277c478bd9Sstevel@tonic-gate }
24287c478bd9Sstevel@tonic-gate 
24297c478bd9Sstevel@tonic-gate /*
24307c478bd9Sstevel@tonic-gate  * secpolicy_contract_event_choice
24317c478bd9Sstevel@tonic-gate  *
24327c478bd9Sstevel@tonic-gate  * Determine if the subject may retain contract events in its critical
24337c478bd9Sstevel@tonic-gate  * set when a change in other terms would normally require a change in
24347c478bd9Sstevel@tonic-gate  * the critical set.  Just tests privilege and audits on success.
24357c478bd9Sstevel@tonic-gate  */
24367c478bd9Sstevel@tonic-gate boolean_t
secpolicy_contract_event_choice(const cred_t * cr)24377c478bd9Sstevel@tonic-gate secpolicy_contract_event_choice(const cred_t *cr)
24387c478bd9Sstevel@tonic-gate {
24397c478bd9Sstevel@tonic-gate 	return (PRIV_POLICY_CHOICE(cr, PRIV_CONTRACT_EVENT, B_FALSE));
24407c478bd9Sstevel@tonic-gate }
24417c478bd9Sstevel@tonic-gate 
24427c478bd9Sstevel@tonic-gate /*
2443ea8dc4b6Seschrock  * secpolicy_gart_access
24447c478bd9Sstevel@tonic-gate  *
2445ea8dc4b6Seschrock  * Determine if the subject has sufficient priveleges to make ioctls to agpgart
2446ea8dc4b6Seschrock  * device.
24477c478bd9Sstevel@tonic-gate  */
24487c478bd9Sstevel@tonic-gate int
secpolicy_gart_access(const cred_t * cr)24497c478bd9Sstevel@tonic-gate secpolicy_gart_access(const cred_t *cr)
24507c478bd9Sstevel@tonic-gate {
245126f24838Scasper 	return (PRIV_POLICY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE, EPERM, NULL));
24527c478bd9Sstevel@tonic-gate }
24537c478bd9Sstevel@tonic-gate 
24547c478bd9Sstevel@tonic-gate /*
2455ea8dc4b6Seschrock  * secpolicy_gart_map
24567c478bd9Sstevel@tonic-gate  *
2457ea8dc4b6Seschrock  * Determine if the subject has sufficient priveleges to map aperture range
2458ea8dc4b6Seschrock  * through agpgart driver.
24597c478bd9Sstevel@tonic-gate  */
24607c478bd9Sstevel@tonic-gate int
secpolicy_gart_map(const cred_t * cr)24617c478bd9Sstevel@tonic-gate secpolicy_gart_map(const cred_t *cr)
24627c478bd9Sstevel@tonic-gate {
246326f24838Scasper 	if (PRIV_POLICY_ONLY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE)) {
246426f24838Scasper 		return (PRIV_POLICY(cr, PRIV_GRAPHICS_ACCESS, B_FALSE, EPERM,
246526f24838Scasper 		    NULL));
246626f24838Scasper 	} else {
246726f24838Scasper 		return (PRIV_POLICY(cr, PRIV_GRAPHICS_MAP, B_FALSE, EPERM,
246826f24838Scasper 		    NULL));
24697c478bd9Sstevel@tonic-gate 	}
24707c478bd9Sstevel@tonic-gate }
2471fa9e4066Sahrens 
2472fa9e4066Sahrens /*
2473047043c2SRobert Mustacchi  * secpolicy_hwmanip
2474993e3fafSRobert Mustacchi  *
2475047043c2SRobert Mustacchi  * Determine if the subject can observe and manipulate a hardware device with a
2476047043c2SRobert Mustacchi  * dangerous blunt hammer, often suggests they can do something destructive.
2477047043c2SRobert Mustacchi  * Requires all privileges.
2478993e3fafSRobert Mustacchi  */
2479993e3fafSRobert Mustacchi int
secpolicy_hwmanip(const cred_t * cr)2480047043c2SRobert Mustacchi secpolicy_hwmanip(const cred_t *cr)
2481993e3fafSRobert Mustacchi {
2482993e3fafSRobert Mustacchi 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
2483993e3fafSRobert Mustacchi }
2484993e3fafSRobert Mustacchi 
2485993e3fafSRobert Mustacchi /*
2486ea8dc4b6Seschrock  * secpolicy_zinject
2487ea8dc4b6Seschrock  *
2488ea8dc4b6Seschrock  * Determine if the subject can inject faults in the ZFS fault injection
2489ea8dc4b6Seschrock  * framework.  Requires all privileges.
2490ea8dc4b6Seschrock  */
2491ea8dc4b6Seschrock int
secpolicy_zinject(const cred_t * cr)2492ea8dc4b6Seschrock secpolicy_zinject(const cred_t *cr)
2493ea8dc4b6Seschrock {
2494134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(cr, PRIV_FULLSET, NULL, KLPDARG_NONE));
2495ea8dc4b6Seschrock }
2496ea8dc4b6Seschrock 
2497ea8dc4b6Seschrock /*
2498fa9e4066Sahrens  * secpolicy_zfs
2499fa9e4066Sahrens  *
2500ea8dc4b6Seschrock  * Determine if the subject has permission to manipulate ZFS datasets
2501ea8dc4b6Seschrock  * (not pools).  Equivalent to the SYS_MOUNT privilege.
2502fa9e4066Sahrens  */
2503fa9e4066Sahrens int
secpolicy_zfs(const cred_t * cr)2504fa9e4066Sahrens secpolicy_zfs(const cred_t *cr)
2505fa9e4066Sahrens {
2506fa9e4066Sahrens 	return (PRIV_POLICY(cr, PRIV_SYS_MOUNT, B_FALSE, EPERM, NULL));
2507fa9e4066Sahrens }
2508f48205beScasper 
2509f48205beScasper /*
2510f48205beScasper  * secpolicy_idmap
2511f48205beScasper  *
2512f48205beScasper  * Determine if the calling process has permissions to register an SID
2513f48205beScasper  * mapping daemon and allocate ephemeral IDs.
2514f48205beScasper  */
2515f48205beScasper int
secpolicy_idmap(const cred_t * cr)2516f48205beScasper secpolicy_idmap(const cred_t *cr)
2517f48205beScasper {
2518bda89588Sjp151216 	return (PRIV_POLICY(cr, PRIV_FILE_SETID, B_TRUE, EPERM, NULL));
2519f48205beScasper }
25202449e17fSsherrym 
25212449e17fSsherrym /*
25222449e17fSsherrym  * secpolicy_ucode_update
25232449e17fSsherrym  *
25242449e17fSsherrym  * Determine if the subject has sufficient privilege to update microcode.
25252449e17fSsherrym  */
25262449e17fSsherrym int
secpolicy_ucode_update(const cred_t * scr)25272449e17fSsherrym secpolicy_ucode_update(const cred_t *scr)
25282449e17fSsherrym {
25292449e17fSsherrym 	return (PRIV_POLICY(scr, PRIV_ALL, B_FALSE, EPERM, NULL));
25302449e17fSsherrym }
2531e6bdcbd5Sdh155122 
2532e6bdcbd5Sdh155122 /*
2533e6bdcbd5Sdh155122  * secpolicy_sadopen
2534e6bdcbd5Sdh155122  *
2535e6bdcbd5Sdh155122  * Determine if the subject has sufficient privilege to access /dev/sad/admin.
2536e6bdcbd5Sdh155122  * /dev/sad/admin appear in global zone and exclusive-IP zones only.
2537e6bdcbd5Sdh155122  * In global zone, sys_config is required.
2538e6bdcbd5Sdh155122  * In exclusive-IP zones, sys_ip_config is required.
2539e6bdcbd5Sdh155122  * Note that sys_config is prohibited in non-global zones.
2540e6bdcbd5Sdh155122  */
2541e6bdcbd5Sdh155122 int
secpolicy_sadopen(const cred_t * credp)2542e6bdcbd5Sdh155122 secpolicy_sadopen(const cred_t *credp)
2543e6bdcbd5Sdh155122 {
2544e6bdcbd5Sdh155122 	priv_set_t pset;
2545e6bdcbd5Sdh155122 
2546e6bdcbd5Sdh155122 	priv_emptyset(&pset);
2547e6bdcbd5Sdh155122 
2548e6bdcbd5Sdh155122 	if (crgetzoneid(credp) == GLOBAL_ZONEID)
2549e6bdcbd5Sdh155122 		priv_addset(&pset, PRIV_SYS_CONFIG);
2550e6bdcbd5Sdh155122 	else
2551e6bdcbd5Sdh155122 		priv_addset(&pset, PRIV_SYS_IP_CONFIG);
2552e6bdcbd5Sdh155122 
2553134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(credp, &pset, "devpolicy", KLPDARG_NONE));
2554e6bdcbd5Sdh155122 }
2555da6c28aaSamw 
2556ddf7fe95Scasper 
2557ddf7fe95Scasper /*
2558ddf7fe95Scasper  * Add privileges to a particular privilege set; this is called when the
2559ddf7fe95Scasper  * current sets of privileges are not sufficient.  I.e., we should always
2560ddf7fe95Scasper  * call the policy override functions from here.
2561ddf7fe95Scasper  * What we are allowed to have is in the Observed Permitted set; so
2562ddf7fe95Scasper  * we compute the difference between that and the newset.
2563ddf7fe95Scasper  */
2564ddf7fe95Scasper int
secpolicy_require_privs(const cred_t * cr,const priv_set_t * nset)2565ddf7fe95Scasper secpolicy_require_privs(const cred_t *cr, const priv_set_t *nset)
2566ddf7fe95Scasper {
2567ddf7fe95Scasper 	priv_set_t rqd;
2568ddf7fe95Scasper 
2569ddf7fe95Scasper 	rqd = CR_OPPRIV(cr);
2570ddf7fe95Scasper 
2571ddf7fe95Scasper 	priv_inverse(&rqd);
2572ddf7fe95Scasper 	priv_intersect(nset, &rqd);
2573ddf7fe95Scasper 
2574134a1f4eSCasper H.S. Dik 	return (secpolicy_require_set(cr, &rqd, NULL, KLPDARG_NONE));
2575ddf7fe95Scasper }
2576ddf7fe95Scasper 
2577da6c28aaSamw /*
2578da6c28aaSamw  * secpolicy_smb
2579da6c28aaSamw  *
2580da6c28aaSamw  * Determine if the cred_t has PRIV_SYS_SMB privilege, indicating
2581da6c28aaSamw  * that it has permission to access the smbsrv kernel driver.
2582da6c28aaSamw  * PRIV_POLICY checks the privilege and audits the check.
2583da6c28aaSamw  *
2584da6c28aaSamw  * Returns:
2585da6c28aaSamw  * 0       Driver access is allowed.
2586da6c28aaSamw  * EPERM   Driver access is NOT permitted.
2587da6c28aaSamw  */
2588da6c28aaSamw int
secpolicy_smb(const cred_t * cr)2589da6c28aaSamw secpolicy_smb(const cred_t *cr)
2590da6c28aaSamw {
2591da6c28aaSamw 	return (PRIV_POLICY(cr, PRIV_SYS_SMB, B_FALSE, EPERM, NULL));
2592da6c28aaSamw }
2593911106dfSjm199354 
2594911106dfSjm199354 /*
2595911106dfSjm199354  * secpolicy_vscan
2596911106dfSjm199354  *
2597911106dfSjm199354  * Determine if cred_t has the necessary privileges to access a file
2598911106dfSjm199354  * for virus scanning and update its extended system attributes.
2599911106dfSjm199354  * PRIV_FILE_DAC_SEARCH, PRIV_FILE_DAC_READ - file access
2600911106dfSjm199354  * PRIV_FILE_FLAG_SET - set extended system attributes
2601911106dfSjm199354  *
2602911106dfSjm199354  * PRIV_POLICY checks the privilege and audits the check.
2603911106dfSjm199354  *
2604911106dfSjm199354  * Returns:
2605911106dfSjm199354  * 0      file access for virus scanning allowed.
2606911106dfSjm199354  * EPERM  file access for virus scanning is NOT permitted.
2607911106dfSjm199354  */
2608911106dfSjm199354 int
secpolicy_vscan(const cred_t * cr)2609911106dfSjm199354 secpolicy_vscan(const cred_t *cr)
2610911106dfSjm199354 {
2611911106dfSjm199354 	if ((PRIV_POLICY(cr, PRIV_FILE_DAC_SEARCH, B_FALSE, EPERM, NULL)) ||
2612911106dfSjm199354 	    (PRIV_POLICY(cr, PRIV_FILE_DAC_READ, B_FALSE, EPERM, NULL)) ||
2613911106dfSjm199354 	    (PRIV_POLICY(cr, PRIV_FILE_FLAG_SET, B_FALSE, EPERM, NULL))) {
2614911106dfSjm199354 		return (EPERM);
2615911106dfSjm199354 	}
2616911106dfSjm199354 
2617911106dfSjm199354 	return (0);
2618911106dfSjm199354 }
26194bff34e3Sthurlow 
26204bff34e3Sthurlow /*
26214bff34e3Sthurlow  * secpolicy_smbfs_login
26224bff34e3Sthurlow  *
26234bff34e3Sthurlow  * Determines if the caller can add and delete the smbfs login
26244bff34e3Sthurlow  * password in the the nsmb kernel module for the CIFS client.
26254bff34e3Sthurlow  *
26264bff34e3Sthurlow  * Returns:
26274bff34e3Sthurlow  * 0       access is allowed.
26284bff34e3Sthurlow  * EPERM   access is NOT allowed.
26294bff34e3Sthurlow  */
26304bff34e3Sthurlow int
secpolicy_smbfs_login(const cred_t * cr,uid_t uid)26314bff34e3Sthurlow secpolicy_smbfs_login(const cred_t *cr, uid_t uid)
26324bff34e3Sthurlow {
26334bff34e3Sthurlow 	uid_t cruid = crgetruid(cr);
26344bff34e3Sthurlow 
26354bff34e3Sthurlow 	if (cruid == uid)
26364bff34e3Sthurlow 		return (0);
26374bff34e3Sthurlow 	return (PRIV_POLICY(cr, PRIV_PROC_OWNER, B_FALSE,
26384bff34e3Sthurlow 	    EPERM, NULL));
26394bff34e3Sthurlow }
2640b26a64aeSjohnlev 
2641b26a64aeSjohnlev /*
2642b26a64aeSjohnlev  * secpolicy_xvm_control
2643b26a64aeSjohnlev  *
2644b26a64aeSjohnlev  * Determines if a caller can control the xVM hypervisor and/or running
2645b26a64aeSjohnlev  * domains (x86 specific).
2646b26a64aeSjohnlev  *
2647b26a64aeSjohnlev  * Returns:
2648b26a64aeSjohnlev  * 0       access is allowed.
2649b26a64aeSjohnlev  * EPERM   access is NOT allowed.
2650b26a64aeSjohnlev  */
2651b26a64aeSjohnlev int
secpolicy_xvm_control(const cred_t * cr)2652b26a64aeSjohnlev secpolicy_xvm_control(const cred_t *cr)
2653b26a64aeSjohnlev {
2654b26a64aeSjohnlev 	if (PRIV_POLICY(cr, PRIV_XVM_CONTROL, B_FALSE, EPERM, NULL))
2655b26a64aeSjohnlev 		return (EPERM);
2656b26a64aeSjohnlev 	return (0);
2657b26a64aeSjohnlev }
2658da14cebeSEric Cheng 
2659da14cebeSEric Cheng /*
2660f53eecf5SJames Carlson  * secpolicy_ppp_config
2661f53eecf5SJames Carlson  *
2662f53eecf5SJames Carlson  * Determine if the subject has sufficient privileges to configure PPP and
2663f53eecf5SJames Carlson  * PPP-related devices.
2664f53eecf5SJames Carlson  */
2665f53eecf5SJames Carlson int
secpolicy_ppp_config(const cred_t * cr)2666f53eecf5SJames Carlson secpolicy_ppp_config(const cred_t *cr)
2667f53eecf5SJames Carlson {
2668f53eecf5SJames Carlson 	if (PRIV_POLICY_ONLY(cr, PRIV_SYS_NET_CONFIG, B_FALSE))
2669f53eecf5SJames Carlson 		return (secpolicy_net_config(cr, B_FALSE));
2670f53eecf5SJames Carlson 	return (PRIV_POLICY(cr, PRIV_SYS_PPP_CONFIG, B_FALSE, EPERM, NULL));
2671f53eecf5SJames Carlson }
2672