xref: /titanic_53/usr/src/uts/common/os/priv.c (revision 30cbf0d206aed9037082f81dba96b4013bc5f836)
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
5*30cbf0d2SCasper H.S. Dik  * Common Development and Distribution License (the "License").
6*30cbf0d2SCasper H.S. Dik  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
22*30cbf0d2SCasper H.S. Dik  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Privilege implementation.
287c478bd9Sstevel@tonic-gate  *
297c478bd9Sstevel@tonic-gate  * This file provides the infrastructure for privilege sets and limits
307c478bd9Sstevel@tonic-gate  * the number of files that requires to include <sys/cred_impl.h> and/or
317c478bd9Sstevel@tonic-gate  * <sys/priv_impl.h>.
327c478bd9Sstevel@tonic-gate  *
337c478bd9Sstevel@tonic-gate  * The Solaris privilege mechanism has been designed in a
347c478bd9Sstevel@tonic-gate  * future proof manner.  While the kernel may use fixed size arrays
357c478bd9Sstevel@tonic-gate  * and fixed bitmasks and bit values, the representation of those
367c478bd9Sstevel@tonic-gate  * is kernel private.  All external interfaces as well as K-to-K interfaces
377c478bd9Sstevel@tonic-gate  * have been constructed in a manner to provide the maximum flexibility.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * There can be X privilege sets each containing Y 32 bit words.
407c478bd9Sstevel@tonic-gate  * <X, Y> are constant for a kernel invocation.
417c478bd9Sstevel@tonic-gate  *
427c478bd9Sstevel@tonic-gate  * As a consequence, all privilege set manipulation happens in functions
437c478bd9Sstevel@tonic-gate  * below.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  */
467c478bd9Sstevel@tonic-gate 
477c478bd9Sstevel@tonic-gate #include <sys/systm.h>
487c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
497c478bd9Sstevel@tonic-gate #include <sys/kmem.h>
507c478bd9Sstevel@tonic-gate #include <sys/sunddi.h>
517c478bd9Sstevel@tonic-gate #include <sys/errno.h>
527c478bd9Sstevel@tonic-gate #include <sys/debug.h>
537c478bd9Sstevel@tonic-gate #include <sys/priv_impl.h>
547c478bd9Sstevel@tonic-gate #include <sys/procfs.h>
557c478bd9Sstevel@tonic-gate #include <sys/policy.h>
567c478bd9Sstevel@tonic-gate #include <sys/cred_impl.h>
577c478bd9Sstevel@tonic-gate #include <sys/devpolicy.h>
587c478bd9Sstevel@tonic-gate #include <sys/atomic.h>
597c478bd9Sstevel@tonic-gate 
607c478bd9Sstevel@tonic-gate /*
617c478bd9Sstevel@tonic-gate  * Privilege name to number mapping table consists in the generated
627c478bd9Sstevel@tonic-gate  * priv_const.c file.  This lock protects against updates of the privilege
637c478bd9Sstevel@tonic-gate  * names and counts; all other priv_info fields are read-only.
647c478bd9Sstevel@tonic-gate  * The actual protected values are:
657c478bd9Sstevel@tonic-gate  *	global variable nprivs
667c478bd9Sstevel@tonic-gate  *	the priv_max field
677c478bd9Sstevel@tonic-gate  *	the priv_names field
687c478bd9Sstevel@tonic-gate  *	the priv names info item (cnt/strings)
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate krwlock_t privinfo_lock;
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate static boolean_t priv_valid(const cred_t *);
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate priv_set_t priv_fullset;	/* set of all privileges */
757c478bd9Sstevel@tonic-gate priv_set_t priv_unsafe;	/* unsafe to exec set-uid root if these are not in L */
767c478bd9Sstevel@tonic-gate 
777c478bd9Sstevel@tonic-gate /*
787c478bd9Sstevel@tonic-gate  * Privilege initialization functions.
797c478bd9Sstevel@tonic-gate  * Called from common/os/cred.c when cred_init is called.
807c478bd9Sstevel@tonic-gate  */
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate void
837c478bd9Sstevel@tonic-gate priv_init(void)
847c478bd9Sstevel@tonic-gate {
857c478bd9Sstevel@tonic-gate 	rw_init(&privinfo_lock, NULL, RW_DRIVER, NULL);
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate 	PRIV_BASIC_ASSERT(priv_basic);
887c478bd9Sstevel@tonic-gate 	PRIV_UNSAFE_ASSERT(&priv_unsafe);
897c478bd9Sstevel@tonic-gate 	priv_fillset(&priv_fullset);
907c478bd9Sstevel@tonic-gate 
917c478bd9Sstevel@tonic-gate 	devpolicy_init();
927c478bd9Sstevel@tonic-gate }
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /* Utility functions: privilege sets as opaque data types */
957c478bd9Sstevel@tonic-gate 
967c478bd9Sstevel@tonic-gate /*
977c478bd9Sstevel@tonic-gate  * Guts of prgetprivsize.
987c478bd9Sstevel@tonic-gate  */
997c478bd9Sstevel@tonic-gate int
1007c478bd9Sstevel@tonic-gate priv_prgetprivsize(prpriv_t *tmpl)
1017c478bd9Sstevel@tonic-gate {
1027c478bd9Sstevel@tonic-gate 	return (sizeof (prpriv_t) +
1037c478bd9Sstevel@tonic-gate 	    PRIV_SETBYTES - sizeof (priv_chunk_t) +
1047c478bd9Sstevel@tonic-gate 	    (tmpl ? tmpl->pr_infosize : priv_info->priv_infosize));
1057c478bd9Sstevel@tonic-gate }
1067c478bd9Sstevel@tonic-gate 
1077c478bd9Sstevel@tonic-gate /*
1087c478bd9Sstevel@tonic-gate  * Guts of prgetpriv.
1097c478bd9Sstevel@tonic-gate  */
1107c478bd9Sstevel@tonic-gate void
1117c478bd9Sstevel@tonic-gate cred2prpriv(const cred_t *cp, prpriv_t *pr)
1127c478bd9Sstevel@tonic-gate {
1137c478bd9Sstevel@tonic-gate 	priv_set_t *psa;
1147c478bd9Sstevel@tonic-gate 	int i;
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	pr->pr_nsets = PRIV_NSET;
1177c478bd9Sstevel@tonic-gate 	pr->pr_setsize = PRIV_SETSIZE;
1187c478bd9Sstevel@tonic-gate 	pr->pr_infosize = priv_info->priv_infosize;
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate 	psa = (priv_set_t *)pr->pr_sets;
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_NSET; i++)
1237c478bd9Sstevel@tonic-gate 		psa[i] = *priv_getset(cp, i);
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	priv_getinfo(cp, (char *)pr + PRIV_PRPRIV_INFO_OFFSET(pr));
1267c478bd9Sstevel@tonic-gate }
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate /*
1297c478bd9Sstevel@tonic-gate  * Guts of pr_spriv:
1307c478bd9Sstevel@tonic-gate  *
1317c478bd9Sstevel@tonic-gate  * Set the privileges of a process.
1327c478bd9Sstevel@tonic-gate  *
1337c478bd9Sstevel@tonic-gate  * In order to set the privileges, the setting process will need to
1347c478bd9Sstevel@tonic-gate  * have those privileges in its effective set in order to prevent
1357c478bd9Sstevel@tonic-gate  * specially privileged processes to easily gain additional privileges.
1367c478bd9Sstevel@tonic-gate  * Pre-existing privileges can be retained.  To change any privileges,
1377c478bd9Sstevel@tonic-gate  * PRIV_PROC_OWNER needs to be asserted.
1387c478bd9Sstevel@tonic-gate  *
1397c478bd9Sstevel@tonic-gate  * In formula:
1407c478bd9Sstevel@tonic-gate  *
1417c478bd9Sstevel@tonic-gate  *	S' <= S || S' <= S + Ea
1427c478bd9Sstevel@tonic-gate  *
1437c478bd9Sstevel@tonic-gate  * the new set must either be subset of the old set or a subset of
1447c478bd9Sstevel@tonic-gate  * the oldset merged with the effective set of the acting process; or just:
1457c478bd9Sstevel@tonic-gate  *
1467c478bd9Sstevel@tonic-gate  *	S' <= S + Ea
1477c478bd9Sstevel@tonic-gate  *
1487c478bd9Sstevel@tonic-gate  * It's not legal to grow the limit set this way.
1497c478bd9Sstevel@tonic-gate  *
1507c478bd9Sstevel@tonic-gate  */
1517c478bd9Sstevel@tonic-gate int
1527c478bd9Sstevel@tonic-gate priv_pr_spriv(proc_t *p, prpriv_t *prpriv, const cred_t *cr)
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate 	cred_t *oldcred;
1557c478bd9Sstevel@tonic-gate 	cred_t *newcred;
1567c478bd9Sstevel@tonic-gate 	int i;
1577c478bd9Sstevel@tonic-gate 	int err = EPERM;
1587c478bd9Sstevel@tonic-gate 	cred_priv_t *cp, *ocp;
1597c478bd9Sstevel@tonic-gate 	priv_set_t eset;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 	ASSERT(MUTEX_HELD(&p->p_lock));
1627c478bd9Sstevel@tonic-gate 
1637c478bd9Sstevel@tonic-gate 	/*
1647c478bd9Sstevel@tonic-gate 	 * Set must have proper dimension; infosize must be absent
1657c478bd9Sstevel@tonic-gate 	 * or properly sized.
1667c478bd9Sstevel@tonic-gate 	 */
1677c478bd9Sstevel@tonic-gate 	if (prpriv->pr_nsets != PRIV_NSET ||
1687c478bd9Sstevel@tonic-gate 	    prpriv->pr_setsize != PRIV_SETSIZE ||
1697c478bd9Sstevel@tonic-gate 	    (prpriv->pr_infosize & (sizeof (uint32_t) - 1)) != 0 ||
1707c478bd9Sstevel@tonic-gate 	    prpriv->pr_infosize > priv_info->priv_infosize ||
1717c478bd9Sstevel@tonic-gate 	    prpriv->pr_infosize < 0)
1727c478bd9Sstevel@tonic-gate 		return (EINVAL);
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_lock);
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	if (priv_proc_cred_perm(cr, p, &oldcred, VWRITE) != 0) {
1777c478bd9Sstevel@tonic-gate 		mutex_enter(&p->p_lock);
1787c478bd9Sstevel@tonic-gate 		return (EPERM);
1797c478bd9Sstevel@tonic-gate 	}
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 	newcred = crdup(oldcred);
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate 	/* Copy the privilege sets from prpriv to newcred */
1847c478bd9Sstevel@tonic-gate 	bcopy(prpriv->pr_sets, CR_PRIVSETS(newcred), PRIV_SETBYTES);
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 	cp = &newcred->cr_priv;
1877c478bd9Sstevel@tonic-gate 	ocp = &oldcred->cr_priv;
1887c478bd9Sstevel@tonic-gate 	eset = CR_OEPRIV(cr);
1897c478bd9Sstevel@tonic-gate 
1907c478bd9Sstevel@tonic-gate 	priv_intersect(&CR_LPRIV(oldcred), &eset);
1917c478bd9Sstevel@tonic-gate 
1927c478bd9Sstevel@tonic-gate 	/*
1937c478bd9Sstevel@tonic-gate 	 * Verify the constraints laid out:
1947c478bd9Sstevel@tonic-gate 	 * for the limit set, we require that the new set is a subset
1957c478bd9Sstevel@tonic-gate 	 * of the old limit set.
1967c478bd9Sstevel@tonic-gate 	 * for all other sets, we require that the new set is either a
1977c478bd9Sstevel@tonic-gate 	 * subset of the old set or a subset of the intersection of
1987c478bd9Sstevel@tonic-gate 	 * the old limit set and the effective set of the acting process.
1997c478bd9Sstevel@tonic-gate 	 */
2007c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_NSET; i++)
2017c478bd9Sstevel@tonic-gate 		if (!priv_issubset(&cp->crprivs[i], &ocp->crprivs[i]) &&
2027c478bd9Sstevel@tonic-gate 		    (i == PRIV_LIMIT || !priv_issubset(&cp->crprivs[i], &eset)))
2037c478bd9Sstevel@tonic-gate 			break;
2047c478bd9Sstevel@tonic-gate 
2057c478bd9Sstevel@tonic-gate 	crfree(oldcred);
2067c478bd9Sstevel@tonic-gate 
2077c478bd9Sstevel@tonic-gate 	if (i < PRIV_NSET || !priv_valid(newcred))
2087c478bd9Sstevel@tonic-gate 		goto err;
2097c478bd9Sstevel@tonic-gate 
2107c478bd9Sstevel@tonic-gate 	/* Load the settable privilege information */
2117c478bd9Sstevel@tonic-gate 	if (prpriv->pr_infosize > 0) {
2127c478bd9Sstevel@tonic-gate 		char *x = (char *)prpriv + PRIV_PRPRIV_INFO_OFFSET(prpriv);
2137c478bd9Sstevel@tonic-gate 		char *lastx = x + prpriv->pr_infosize;
2147c478bd9Sstevel@tonic-gate 
2157c478bd9Sstevel@tonic-gate 		while (x < lastx) {
2167c478bd9Sstevel@tonic-gate 			priv_info_t *pi = (priv_info_t *)x;
2177c478bd9Sstevel@tonic-gate 			priv_info_uint_t *pii;
2187c478bd9Sstevel@tonic-gate 
2197c478bd9Sstevel@tonic-gate 			switch (pi->priv_info_type) {
2207c478bd9Sstevel@tonic-gate 			case PRIV_INFO_FLAGS:
2217c478bd9Sstevel@tonic-gate 				pii = (priv_info_uint_t *)x;
2227c478bd9Sstevel@tonic-gate 				if (pii->info.priv_info_size != sizeof (*pii)) {
2237c478bd9Sstevel@tonic-gate 					err = EINVAL;
2247c478bd9Sstevel@tonic-gate 					goto err;
2257c478bd9Sstevel@tonic-gate 				}
2267c478bd9Sstevel@tonic-gate 				CR_FLAGS(newcred) &= ~PRIV_USER;
2277c478bd9Sstevel@tonic-gate 				CR_FLAGS(newcred) |= (pii->val & PRIV_USER);
2287c478bd9Sstevel@tonic-gate 				break;
2297c478bd9Sstevel@tonic-gate 			default:
2307c478bd9Sstevel@tonic-gate 				err = EINVAL;
2317c478bd9Sstevel@tonic-gate 				goto err;
2327c478bd9Sstevel@tonic-gate 			}
2337c478bd9Sstevel@tonic-gate 			/* Guarantee alignment and forward progress */
2347c478bd9Sstevel@tonic-gate 			if ((pi->priv_info_size & (sizeof (uint32_t) - 1)) ||
2357c478bd9Sstevel@tonic-gate 			    pi->priv_info_size < sizeof (*pi) ||
2367c478bd9Sstevel@tonic-gate 			    lastx - x > pi->priv_info_size) {
2377c478bd9Sstevel@tonic-gate 				err = EINVAL;
2387c478bd9Sstevel@tonic-gate 				goto err;
2397c478bd9Sstevel@tonic-gate 			}
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 			x += pi->priv_info_size;
2427c478bd9Sstevel@tonic-gate 		}
2437c478bd9Sstevel@tonic-gate 	}
2447c478bd9Sstevel@tonic-gate 
2457c478bd9Sstevel@tonic-gate 	/*
2467c478bd9Sstevel@tonic-gate 	 * We'll try to copy the privilege aware flag; but since the
2477c478bd9Sstevel@tonic-gate 	 * privileges sets are all individually set, they are set
2487c478bd9Sstevel@tonic-gate 	 * as if we're privilege aware.  If PRIV_AWARE wasn't set
2497c478bd9Sstevel@tonic-gate 	 * or was explicitely unset, we need to set the flag and then
2507c478bd9Sstevel@tonic-gate 	 * try to get rid of it.
2517c478bd9Sstevel@tonic-gate 	 */
2527c478bd9Sstevel@tonic-gate 	if ((CR_FLAGS(newcred) & PRIV_AWARE) == 0) {
2537c478bd9Sstevel@tonic-gate 		CR_FLAGS(newcred) |= PRIV_AWARE;
2547c478bd9Sstevel@tonic-gate 		priv_adjust_PA(newcred);
2557c478bd9Sstevel@tonic-gate 	}
2567c478bd9Sstevel@tonic-gate 
2577c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_crlock);
2587c478bd9Sstevel@tonic-gate 	oldcred = p->p_cred;
2597c478bd9Sstevel@tonic-gate 	p->p_cred = newcred;
2607c478bd9Sstevel@tonic-gate 	mutex_exit(&p->p_crlock);
2617c478bd9Sstevel@tonic-gate 	crfree(oldcred);
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2647c478bd9Sstevel@tonic-gate 	return (0);
2657c478bd9Sstevel@tonic-gate 
2667c478bd9Sstevel@tonic-gate err:
2677c478bd9Sstevel@tonic-gate 	crfree(newcred);
2687c478bd9Sstevel@tonic-gate 	mutex_enter(&p->p_lock);
2697c478bd9Sstevel@tonic-gate 	return (err);
2707c478bd9Sstevel@tonic-gate }
2717c478bd9Sstevel@tonic-gate 
2727c478bd9Sstevel@tonic-gate priv_impl_info_t
2737c478bd9Sstevel@tonic-gate *priv_hold_implinfo(void)
2747c478bd9Sstevel@tonic-gate {
2757c478bd9Sstevel@tonic-gate 	rw_enter(&privinfo_lock, RW_READER);
2767c478bd9Sstevel@tonic-gate 	return (priv_info);
2777c478bd9Sstevel@tonic-gate }
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate void
2807c478bd9Sstevel@tonic-gate priv_release_implinfo(void)
2817c478bd9Sstevel@tonic-gate {
2827c478bd9Sstevel@tonic-gate 	rw_exit(&privinfo_lock);
2837c478bd9Sstevel@tonic-gate }
2847c478bd9Sstevel@tonic-gate 
2857c478bd9Sstevel@tonic-gate size_t
2867c478bd9Sstevel@tonic-gate priv_get_implinfo_size(void)
2877c478bd9Sstevel@tonic-gate {
2887c478bd9Sstevel@tonic-gate 	return (privinfosize);
2897c478bd9Sstevel@tonic-gate }
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * Return the nth privilege set
2947c478bd9Sstevel@tonic-gate  */
2957c478bd9Sstevel@tonic-gate const priv_set_t *
2967c478bd9Sstevel@tonic-gate priv_getset(const cred_t *cr, int set)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	ASSERT(PRIV_VALIDSET(set));
2997c478bd9Sstevel@tonic-gate 
3007c478bd9Sstevel@tonic-gate 	if ((CR_FLAGS(cr) & PRIV_AWARE) == 0)
3017c478bd9Sstevel@tonic-gate 		switch (set) {
3027c478bd9Sstevel@tonic-gate 		case PRIV_EFFECTIVE:
3037c478bd9Sstevel@tonic-gate 			return (&CR_OEPRIV(cr));
3047c478bd9Sstevel@tonic-gate 		case PRIV_PERMITTED:
3057c478bd9Sstevel@tonic-gate 			return (&CR_OPPRIV(cr));
3067c478bd9Sstevel@tonic-gate 		}
3077c478bd9Sstevel@tonic-gate 	return (&CR_PRIVS(cr)->crprivs[set]);
3087c478bd9Sstevel@tonic-gate }
3097c478bd9Sstevel@tonic-gate 
3107c478bd9Sstevel@tonic-gate /*
3117c478bd9Sstevel@tonic-gate  * Buf must be allocated by caller and contain sufficient space to
3127c478bd9Sstevel@tonic-gate  * contain all additional info structures using priv_info.priv_infosize.
3137c478bd9Sstevel@tonic-gate  * The buffer must be properly aligned.
3147c478bd9Sstevel@tonic-gate  */
3157c478bd9Sstevel@tonic-gate /*ARGSUSED*/
3167c478bd9Sstevel@tonic-gate void
3177c478bd9Sstevel@tonic-gate priv_getinfo(const cred_t *cr, void *buf)
3187c478bd9Sstevel@tonic-gate {
3197c478bd9Sstevel@tonic-gate 	struct priv_info_uint *ii;
3207c478bd9Sstevel@tonic-gate 
3217c478bd9Sstevel@tonic-gate 	ii = buf;
3227c478bd9Sstevel@tonic-gate 	ii->val = CR_FLAGS(cr);
3237c478bd9Sstevel@tonic-gate 	ii->info.priv_info_size = (uint32_t)sizeof (*ii);
3247c478bd9Sstevel@tonic-gate 	ii->info.priv_info_type = PRIV_INFO_FLAGS;
3257c478bd9Sstevel@tonic-gate }
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate int
3287c478bd9Sstevel@tonic-gate priv_getbyname(const char *name, uint_t flag)
3297c478bd9Sstevel@tonic-gate {
3307c478bd9Sstevel@tonic-gate 	int i;
3317c478bd9Sstevel@tonic-gate 	int wheld = 0;
3327c478bd9Sstevel@tonic-gate 	int len;
3337c478bd9Sstevel@tonic-gate 	char *p;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (flag != 0 && flag != PRIV_ALLOC)
3367c478bd9Sstevel@tonic-gate 		return (-EINVAL);
3377c478bd9Sstevel@tonic-gate 
3387c478bd9Sstevel@tonic-gate 	if (strncasecmp(name, "priv_", 5) == 0)
3397c478bd9Sstevel@tonic-gate 		name += 5;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	rw_enter(&privinfo_lock, RW_READER);
3427c478bd9Sstevel@tonic-gate rescan:
3437c478bd9Sstevel@tonic-gate 	for (i = 0; i < nprivs; i++)
3447c478bd9Sstevel@tonic-gate 		if (strcasecmp(priv_names[i], name) == 0) {
3457c478bd9Sstevel@tonic-gate 			rw_exit(&privinfo_lock);
3467c478bd9Sstevel@tonic-gate 			return (i);
3477c478bd9Sstevel@tonic-gate 		}
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	if (!wheld) {
3517c478bd9Sstevel@tonic-gate 		if (!(flag & PRIV_ALLOC)) {
3527c478bd9Sstevel@tonic-gate 			rw_exit(&privinfo_lock);
3537c478bd9Sstevel@tonic-gate 			return (-EINVAL);
3547c478bd9Sstevel@tonic-gate 		}
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 		/* check length, validity and available space */
3577c478bd9Sstevel@tonic-gate 		len = strlen(name) + 1;
3587c478bd9Sstevel@tonic-gate 
3597c478bd9Sstevel@tonic-gate 		if (len > PRIVNAME_MAX) {
3607c478bd9Sstevel@tonic-gate 			rw_exit(&privinfo_lock);
3617c478bd9Sstevel@tonic-gate 			return (-ENAMETOOLONG);
3627c478bd9Sstevel@tonic-gate 		}
3637c478bd9Sstevel@tonic-gate 
3647c478bd9Sstevel@tonic-gate 		for (p = (char *)name; *p != '\0'; p++) {
3657c478bd9Sstevel@tonic-gate 			char c = *p;
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 			if (!((c >= 'A' && c <= 'Z') ||
3687c478bd9Sstevel@tonic-gate 			    (c >= 'a' && c <= 'z') ||
3697c478bd9Sstevel@tonic-gate 			    (c >= '0' && c <= '9') ||
3707c478bd9Sstevel@tonic-gate 			    c == '_')) {
3717c478bd9Sstevel@tonic-gate 				rw_exit(&privinfo_lock);
3727c478bd9Sstevel@tonic-gate 				return (-EINVAL);
3737c478bd9Sstevel@tonic-gate 			}
3747c478bd9Sstevel@tonic-gate 		}
3757c478bd9Sstevel@tonic-gate 
3767c478bd9Sstevel@tonic-gate 		if (!rw_tryupgrade(&privinfo_lock)) {
3777c478bd9Sstevel@tonic-gate 			rw_exit(&privinfo_lock);
3787c478bd9Sstevel@tonic-gate 			rw_enter(&privinfo_lock, RW_WRITER);
3797c478bd9Sstevel@tonic-gate 			wheld = 1;
3807c478bd9Sstevel@tonic-gate 			/* Someone may have added our privilege */
3817c478bd9Sstevel@tonic-gate 			goto rescan;
3827c478bd9Sstevel@tonic-gate 		}
3837c478bd9Sstevel@tonic-gate 	}
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	if (nprivs == MAX_PRIVILEGE || len + privbytes > maxprivbytes) {
3867c478bd9Sstevel@tonic-gate 		rw_exit(&privinfo_lock);
3877c478bd9Sstevel@tonic-gate 		return (-ENOMEM);
3887c478bd9Sstevel@tonic-gate 	}
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 	priv_names[i] = p = priv_str + privbytes;
3917c478bd9Sstevel@tonic-gate 
3927c478bd9Sstevel@tonic-gate 	bcopy(name, p, len);
3937c478bd9Sstevel@tonic-gate 
3947c478bd9Sstevel@tonic-gate 	/* make the priv_names[i] and privilege name globally visible */
3957c478bd9Sstevel@tonic-gate 	membar_producer();
3967c478bd9Sstevel@tonic-gate 
3977c478bd9Sstevel@tonic-gate 	/* adjust priv count and bytes count */
3987c478bd9Sstevel@tonic-gate 	priv_ninfo->cnt = priv_info->priv_max = ++nprivs;
3997c478bd9Sstevel@tonic-gate 	privbytes += len;
4007c478bd9Sstevel@tonic-gate 
4017c478bd9Sstevel@tonic-gate 	rw_exit(&privinfo_lock);
4027c478bd9Sstevel@tonic-gate 	return (i);
4037c478bd9Sstevel@tonic-gate }
4047c478bd9Sstevel@tonic-gate 
4057c478bd9Sstevel@tonic-gate /*
4067c478bd9Sstevel@tonic-gate  * We can't afford locking the privileges here because of the locations
4077c478bd9Sstevel@tonic-gate  * we call this from; so we make sure that the privileges table
4087c478bd9Sstevel@tonic-gate  * is visible to us; it is made visible before the value of nprivs is
4097c478bd9Sstevel@tonic-gate  * updated.
4107c478bd9Sstevel@tonic-gate  */
4117c478bd9Sstevel@tonic-gate const char *
4127c478bd9Sstevel@tonic-gate priv_getbynum(int priv)
4137c478bd9Sstevel@tonic-gate {
4147c478bd9Sstevel@tonic-gate 	int maxpriv = nprivs;
4157c478bd9Sstevel@tonic-gate 
4167c478bd9Sstevel@tonic-gate 	membar_consumer();
4177c478bd9Sstevel@tonic-gate 
4187c478bd9Sstevel@tonic-gate 	if (priv >= 0 && priv < maxpriv)
4197c478bd9Sstevel@tonic-gate 		return (priv_names[priv]);
4207c478bd9Sstevel@tonic-gate 
4217c478bd9Sstevel@tonic-gate 	return (NULL);
4227c478bd9Sstevel@tonic-gate }
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate const char *
4257c478bd9Sstevel@tonic-gate priv_getsetbynum(int setno)
4267c478bd9Sstevel@tonic-gate {
4277c478bd9Sstevel@tonic-gate 	if (!PRIV_VALIDSET(setno))
4287c478bd9Sstevel@tonic-gate 		return (NULL);
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	return (priv_setnames[setno]);
4317c478bd9Sstevel@tonic-gate }
4327c478bd9Sstevel@tonic-gate 
4337c478bd9Sstevel@tonic-gate /*
4347c478bd9Sstevel@tonic-gate  * Privilege sanity checking when setting: E <= P.
4357c478bd9Sstevel@tonic-gate  */
4367c478bd9Sstevel@tonic-gate static boolean_t
4377c478bd9Sstevel@tonic-gate priv_valid(const cred_t *cr)
4387c478bd9Sstevel@tonic-gate {
4397c478bd9Sstevel@tonic-gate 	return (priv_issubset(&CR_EPRIV(cr), &CR_PPRIV(cr)));
4407c478bd9Sstevel@tonic-gate }
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate /*
4437c478bd9Sstevel@tonic-gate  * Privilege manipulation functions
4447c478bd9Sstevel@tonic-gate  *
4457c478bd9Sstevel@tonic-gate  * Without knowing the details of the privilege set implementation,
4467c478bd9Sstevel@tonic-gate  * opaque pointers can be used to manipulate sets at will.
4477c478bd9Sstevel@tonic-gate  */
4487c478bd9Sstevel@tonic-gate void
4497c478bd9Sstevel@tonic-gate priv_emptyset(priv_set_t *set)
4507c478bd9Sstevel@tonic-gate {
4517c478bd9Sstevel@tonic-gate 	bzero(set, sizeof (*set));
4527c478bd9Sstevel@tonic-gate }
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate void
4557c478bd9Sstevel@tonic-gate priv_fillset(priv_set_t *set)
4567c478bd9Sstevel@tonic-gate {
4577c478bd9Sstevel@tonic-gate 	int i;
4587c478bd9Sstevel@tonic-gate 
4597c478bd9Sstevel@tonic-gate 	/* memset? */
4607c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_SETSIZE; i++)
4617c478bd9Sstevel@tonic-gate 		set->pbits[i] = ~(priv_chunk_t)0;
4627c478bd9Sstevel@tonic-gate }
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate void
4657c478bd9Sstevel@tonic-gate priv_addset(priv_set_t *set, int priv)
4667c478bd9Sstevel@tonic-gate {
4677c478bd9Sstevel@tonic-gate 	ASSERT(priv >= 0 && priv < MAX_PRIVILEGE);
4687c478bd9Sstevel@tonic-gate 	__PRIV_ASSERT(set, priv);
4697c478bd9Sstevel@tonic-gate }
4707c478bd9Sstevel@tonic-gate 
4717c478bd9Sstevel@tonic-gate void
4727c478bd9Sstevel@tonic-gate priv_delset(priv_set_t *set, int priv)
4737c478bd9Sstevel@tonic-gate {
4747c478bd9Sstevel@tonic-gate 	ASSERT(priv >= 0 && priv < MAX_PRIVILEGE);
4757c478bd9Sstevel@tonic-gate 	__PRIV_CLEAR(set, priv);
4767c478bd9Sstevel@tonic-gate }
4777c478bd9Sstevel@tonic-gate 
4787c478bd9Sstevel@tonic-gate boolean_t
4797c478bd9Sstevel@tonic-gate priv_ismember(const priv_set_t *set, int priv)
4807c478bd9Sstevel@tonic-gate {
4817c478bd9Sstevel@tonic-gate 	ASSERT(priv >= 0 && priv < MAX_PRIVILEGE);
4827c478bd9Sstevel@tonic-gate 	return (__PRIV_ISASSERT(set, priv) ? B_TRUE : B_FALSE);
4837c478bd9Sstevel@tonic-gate }
4847c478bd9Sstevel@tonic-gate 
4857c478bd9Sstevel@tonic-gate #define	PRIV_TEST_BODY(test) \
4867c478bd9Sstevel@tonic-gate 	int i; \
4877c478bd9Sstevel@tonic-gate \
4887c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_SETSIZE; i++) \
4897c478bd9Sstevel@tonic-gate 		if (!(test)) \
4907c478bd9Sstevel@tonic-gate 			return (B_FALSE); \
4917c478bd9Sstevel@tonic-gate \
4927c478bd9Sstevel@tonic-gate 	return (B_TRUE)
4937c478bd9Sstevel@tonic-gate 
4947c478bd9Sstevel@tonic-gate boolean_t
4957c478bd9Sstevel@tonic-gate priv_isequalset(const priv_set_t *a, const priv_set_t *b)
4967c478bd9Sstevel@tonic-gate {
4977c478bd9Sstevel@tonic-gate 	return ((boolean_t)(bcmp(a, b, sizeof (*a)) == 0));
4987c478bd9Sstevel@tonic-gate }
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate boolean_t
5017c478bd9Sstevel@tonic-gate priv_isemptyset(const priv_set_t *set)
5027c478bd9Sstevel@tonic-gate {
5037c478bd9Sstevel@tonic-gate 	PRIV_TEST_BODY(set->pbits[i] == 0);
5047c478bd9Sstevel@tonic-gate }
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate boolean_t
5077c478bd9Sstevel@tonic-gate priv_isfullset(const priv_set_t *set)
5087c478bd9Sstevel@tonic-gate {
5097c478bd9Sstevel@tonic-gate 	PRIV_TEST_BODY(set->pbits[i] == ~(priv_chunk_t)0);
5107c478bd9Sstevel@tonic-gate }
5117c478bd9Sstevel@tonic-gate 
5127c478bd9Sstevel@tonic-gate /*
5137c478bd9Sstevel@tonic-gate  * Return true if a is a subset of b
5147c478bd9Sstevel@tonic-gate  */
5157c478bd9Sstevel@tonic-gate boolean_t
5167c478bd9Sstevel@tonic-gate priv_issubset(const priv_set_t *a, const priv_set_t *b)
5177c478bd9Sstevel@tonic-gate {
5187c478bd9Sstevel@tonic-gate 	PRIV_TEST_BODY((a->pbits[i] | b->pbits[i]) == b->pbits[i]);
5197c478bd9Sstevel@tonic-gate }
5207c478bd9Sstevel@tonic-gate 
5217c478bd9Sstevel@tonic-gate #define	PRIV_CHANGE_BODY(a, op, b) \
5227c478bd9Sstevel@tonic-gate 	int i; \
5237c478bd9Sstevel@tonic-gate \
5247c478bd9Sstevel@tonic-gate 	for (i = 0; i < PRIV_SETSIZE; i++) \
5257c478bd9Sstevel@tonic-gate 		a->pbits[i] op b->pbits[i]
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate /* B = A ^ B */
5287c478bd9Sstevel@tonic-gate void
5297c478bd9Sstevel@tonic-gate priv_intersect(const priv_set_t *a, priv_set_t *b)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 	/* CSTYLED */
5327c478bd9Sstevel@tonic-gate 	PRIV_CHANGE_BODY(b, &=, a);
5337c478bd9Sstevel@tonic-gate }
5347c478bd9Sstevel@tonic-gate 
5357c478bd9Sstevel@tonic-gate /* B = A v B */
5367c478bd9Sstevel@tonic-gate void
5377c478bd9Sstevel@tonic-gate priv_union(const priv_set_t *a, priv_set_t *b)
5387c478bd9Sstevel@tonic-gate {
5397c478bd9Sstevel@tonic-gate 	/* CSTYLED */
5407c478bd9Sstevel@tonic-gate 	PRIV_CHANGE_BODY(b, |=, a);
5417c478bd9Sstevel@tonic-gate }
5427c478bd9Sstevel@tonic-gate 
5437c478bd9Sstevel@tonic-gate /* A = ! A */
5447c478bd9Sstevel@tonic-gate void
5457c478bd9Sstevel@tonic-gate priv_inverse(priv_set_t *a)
5467c478bd9Sstevel@tonic-gate {
5477c478bd9Sstevel@tonic-gate 	PRIV_CHANGE_BODY(a, = ~, a);
5487c478bd9Sstevel@tonic-gate }
5497c478bd9Sstevel@tonic-gate 
5507c478bd9Sstevel@tonic-gate /*
5517c478bd9Sstevel@tonic-gate  * Can the source cred act on the target credential?
5527c478bd9Sstevel@tonic-gate  *
5537c478bd9Sstevel@tonic-gate  * We will you allow to gain uids this way but not privileges.
5547c478bd9Sstevel@tonic-gate  */
5557c478bd9Sstevel@tonic-gate int
5567c478bd9Sstevel@tonic-gate priv_proc_cred_perm(const cred_t *scr, proc_t *tp, cred_t **pcr, int mode)
5577c478bd9Sstevel@tonic-gate {
5587c478bd9Sstevel@tonic-gate 	const priv_set_t *eset;
5597c478bd9Sstevel@tonic-gate 	int idsmatch;
5607c478bd9Sstevel@tonic-gate 	cred_t *tcr;
5617c478bd9Sstevel@tonic-gate 	int res = 0;
5627c478bd9Sstevel@tonic-gate 
5637c478bd9Sstevel@tonic-gate 	/* prevent the cred from going away */
5647c478bd9Sstevel@tonic-gate 	mutex_enter(&tp->p_crlock);
5657c478bd9Sstevel@tonic-gate 	crhold(tcr = tp->p_cred);
5667c478bd9Sstevel@tonic-gate 	mutex_exit(&tp->p_crlock);
5677c478bd9Sstevel@tonic-gate 
568*30cbf0d2SCasper H.S. Dik 	if (scr == tcr && !(tp->p_flag & SNOCD))
5697c478bd9Sstevel@tonic-gate 		goto out;
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 	idsmatch = (scr->cr_uid == tcr->cr_uid &&
5727c478bd9Sstevel@tonic-gate 	    scr->cr_uid == tcr->cr_ruid &&
5737c478bd9Sstevel@tonic-gate 	    scr->cr_uid == tcr->cr_suid &&
5747c478bd9Sstevel@tonic-gate 	    scr->cr_gid == tcr->cr_gid &&
5757c478bd9Sstevel@tonic-gate 	    scr->cr_gid == tcr->cr_rgid &&
5767c478bd9Sstevel@tonic-gate 	    scr->cr_gid == tcr->cr_sgid &&
5777c478bd9Sstevel@tonic-gate 	    !(tp->p_flag & SNOCD));
5787c478bd9Sstevel@tonic-gate 
5797c478bd9Sstevel@tonic-gate 	/*
5807c478bd9Sstevel@tonic-gate 	 * Source credential must have the proc_zone privilege if referencing
5817c478bd9Sstevel@tonic-gate 	 * a process in another zone.
5827c478bd9Sstevel@tonic-gate 	 */
5837c478bd9Sstevel@tonic-gate 	if (scr->cr_zone != tcr->cr_zone && secpolicy_proc_zone(scr) != 0) {
5847c478bd9Sstevel@tonic-gate 		res = EACCES;
5857c478bd9Sstevel@tonic-gate 		goto out;
5867c478bd9Sstevel@tonic-gate 	}
5877c478bd9Sstevel@tonic-gate 
5887c478bd9Sstevel@tonic-gate 	if (!(mode & VWRITE)) {
5897c478bd9Sstevel@tonic-gate 		if (!idsmatch && secpolicy_proc_owner(scr, tcr, 0) != 0)
5907c478bd9Sstevel@tonic-gate 			res = EACCES;
5917c478bd9Sstevel@tonic-gate 		goto out;
5927c478bd9Sstevel@tonic-gate 	}
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 	/*
5957c478bd9Sstevel@tonic-gate 	 * For writing, the effective set of scr must dominate all sets of tcr,
5967c478bd9Sstevel@tonic-gate 	 * We test Pt <= Es (Et <= Pt so no need to test) and It <= Es
5977c478bd9Sstevel@tonic-gate 	 * The Limit set of scr must be a superset of the limitset of
5987c478bd9Sstevel@tonic-gate 	 * tcr.
5997c478bd9Sstevel@tonic-gate 	 */
6007c478bd9Sstevel@tonic-gate 	eset = &CR_OEPRIV(scr);
6017c478bd9Sstevel@tonic-gate 
6027c478bd9Sstevel@tonic-gate 	if (!priv_issubset(&CR_IPRIV(tcr), eset) ||
6037c478bd9Sstevel@tonic-gate 	    !priv_issubset(&CR_OPPRIV(tcr), eset) ||
6047c478bd9Sstevel@tonic-gate 	    !priv_issubset(&CR_LPRIV(tcr), &CR_LPRIV(scr)) ||
6057c478bd9Sstevel@tonic-gate 	    !idsmatch && secpolicy_proc_owner(scr, tcr, mode) != 0)
6067c478bd9Sstevel@tonic-gate 		res = EACCES;
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate out:
6097c478bd9Sstevel@tonic-gate 	if (res == 0 && pcr != NULL)
6107c478bd9Sstevel@tonic-gate 		*pcr = tcr;
6117c478bd9Sstevel@tonic-gate 	else
6127c478bd9Sstevel@tonic-gate 		crfree(tcr);
6137c478bd9Sstevel@tonic-gate 	return (res);
6147c478bd9Sstevel@tonic-gate }
6157c478bd9Sstevel@tonic-gate 
6167c478bd9Sstevel@tonic-gate /*
6177c478bd9Sstevel@tonic-gate  * Set the privilege aware bit, adding L to E/P if
6187c478bd9Sstevel@tonic-gate  * necessasry.
6197c478bd9Sstevel@tonic-gate  */
6207c478bd9Sstevel@tonic-gate void
6217c478bd9Sstevel@tonic-gate priv_set_PA(cred_t *cr)
6227c478bd9Sstevel@tonic-gate {
6237c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
6247c478bd9Sstevel@tonic-gate 
6257c478bd9Sstevel@tonic-gate 	if (CR_FLAGS(cr) & PRIV_AWARE)
6267c478bd9Sstevel@tonic-gate 		return;
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	CR_FLAGS(cr) |= PRIV_AWARE;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	if (cr->cr_uid == 0)
6317c478bd9Sstevel@tonic-gate 		priv_union(&CR_LPRIV(cr), &CR_EPRIV(cr));
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	if (cr->cr_uid == 0 || cr->cr_suid == 0 || cr->cr_ruid == 0)
6347c478bd9Sstevel@tonic-gate 		priv_union(&CR_LPRIV(cr), &CR_PPRIV(cr));
6357c478bd9Sstevel@tonic-gate }
6367c478bd9Sstevel@tonic-gate 
6377c478bd9Sstevel@tonic-gate boolean_t
6387c478bd9Sstevel@tonic-gate priv_can_clear_PA(const cred_t *cr)
6397c478bd9Sstevel@tonic-gate {
6407c478bd9Sstevel@tonic-gate 	/*
6417c478bd9Sstevel@tonic-gate 	 * We can clear PA in the following cases:
6427c478bd9Sstevel@tonic-gate 	 *
6437c478bd9Sstevel@tonic-gate 	 * None of the uids are 0.
6447c478bd9Sstevel@tonic-gate 	 * Any uid == 0 and P == L and (Euid != 0 or E == L)
6457c478bd9Sstevel@tonic-gate 	 */
6467c478bd9Sstevel@tonic-gate 	return ((cr->cr_suid != 0 && cr->cr_ruid != 0 && cr->cr_uid != 0) ||
6477c478bd9Sstevel@tonic-gate 	    priv_isequalset(&CR_PPRIV(cr), &CR_LPRIV(cr)) &&
6487c478bd9Sstevel@tonic-gate 	    (cr->cr_uid != 0 || priv_isequalset(&CR_EPRIV(cr), &CR_LPRIV(cr))));
6497c478bd9Sstevel@tonic-gate }
6507c478bd9Sstevel@tonic-gate 
6517c478bd9Sstevel@tonic-gate /*
6527c478bd9Sstevel@tonic-gate  * Clear privilege aware bit if it is an idempotent operation and by
6537c478bd9Sstevel@tonic-gate  * clearing it the process cannot get to uid 0 and all privileges.
6547c478bd9Sstevel@tonic-gate  *
6557c478bd9Sstevel@tonic-gate  * This function should be called with caution as it may cause "E" to be
6567c478bd9Sstevel@tonic-gate  * lost once a processes assumes euid 0 again.
6577c478bd9Sstevel@tonic-gate  */
6587c478bd9Sstevel@tonic-gate void
6597c478bd9Sstevel@tonic-gate priv_adjust_PA(cred_t *cr)
6607c478bd9Sstevel@tonic-gate {
6617c478bd9Sstevel@tonic-gate 	ASSERT(cr->cr_ref <= 2);
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 	if (!(CR_FLAGS(cr) & PRIV_AWARE) ||
6647c478bd9Sstevel@tonic-gate 	    !priv_can_clear_PA(cr))
6657c478bd9Sstevel@tonic-gate 		return;
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (CR_FLAGS(cr) & PRIV_AWARE_INHERIT)
6687c478bd9Sstevel@tonic-gate 		return;
6697c478bd9Sstevel@tonic-gate 
6707c478bd9Sstevel@tonic-gate 	/*
6717c478bd9Sstevel@tonic-gate 	 * We now need to adjust P/E in those cases when uids
6727c478bd9Sstevel@tonic-gate 	 * are zero; the rules are P' = I & L, E' = I & L;
6737c478bd9Sstevel@tonic-gate 	 * but since P = L and E = L, we can use P &= I, E &= I,
6747c478bd9Sstevel@tonic-gate 	 * depending on which uids are 0.
6757c478bd9Sstevel@tonic-gate 	 */
6767c478bd9Sstevel@tonic-gate 	if (cr->cr_suid == 0 || cr->cr_ruid == 0 || cr->cr_uid == 0) {
6777c478bd9Sstevel@tonic-gate 		if (cr->cr_uid == 0)
6787c478bd9Sstevel@tonic-gate 			priv_intersect(&CR_IPRIV(cr), &CR_EPRIV(cr));
6797c478bd9Sstevel@tonic-gate 		priv_intersect(&CR_IPRIV(cr), &CR_PPRIV(cr));
6807c478bd9Sstevel@tonic-gate 	}
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 	CR_FLAGS(cr) &= ~PRIV_AWARE;
6837c478bd9Sstevel@tonic-gate }
684